Error between numerical solutions

Hey guys,

is it possible to compute the error between two numerical solutions in freefem?
Say I have a problem without an exact solution, but I can do a reference one using a refined mesh, can a compute the error (L2, H1,…)?

Yes, you can :slight_smile:

FreeFEM can do the interpolation for you from mesh 1 onto mesh 2 simply by writing

u_mesh2 = u_mesh1

Hey, thanks for the reply.
I tried that. I compile a reference solution and saved in a file. Then, I want read from that file and use as an exact solution to compare to results in a coarse mesh, like this

ifstream fileR(filename);
fileR >> u[];

but I’m getting the following error:

length on the array 6 != 153 length in file 
  current line = 523
Exec error : Fatal Error: incompatible length in read array (Op_ReadKN)
   -- number :1
Exec error : Fatal Error: incompatible length in read array (Op_ReadKN)
   -- number :1
 err code 8 ,  mpirank 0

do you have an ideia of what might be?

Hi,
you have to declare u in the fespace used to save the reference solution (the fespace on the fine mesh).
Then fileR >> u[]; recovers the reference solution on the fine mesh
You can afterwards declare v in the fespace on the coarse mesh, and write
v=u;
so that v will contain the interpolation of u on the coarse mesh.
At end you can compute for example
real error=sqrt(int2d(Th)((ucomp-v)^2));
where ucomp is your computed solution on the coarse mesh Th.
An important point is that you need to have both meshes at hand in order to do it. Hence you need to save not only the reference solution, but also the (fine) mesh that comes with it.

1 Like

I use triangulate and a text file with (x,y,value) lines.
I guess you could put this table mesh in a scoped block and
assign the result to a target fespace. This code appears to work
although it could be cleaned up a bit :slight_smile:

macro mmloadds(u,Th,fn,nthings,nentry)
{
Th =triangulate(fn);
/* this needs to be a P1 space */
fespace Vhxxx(Th,P1);
Vhxxx data;
real[int] mdata(Th.nv);
try{
ifstream ifs(fn);
real xass;
for (int i=0; i<Th.nv; ++i)
{
for (int j=0; j<nthings; ++j)
{ if (j==nentry) ifs>> mdata[i];
else ifs>> xass;
}
}
} catch (...) { cout<<" throw during load of "<<fn<<endl; cout.flush;  }
data=0;
data[]=mdata;
u=0;
u=data;
} // EOM mmloadds

2 Likes