# Error between numerical solutions

**URL:** <https://community.freefem.org/t/error-between-numerical-solutions/3005>\
**Category:** General Discussion\
**Created:** [February 28, 2024, 3:36pm UTC](https://community.freefem.org/t/error-between-numerical-solutions/3005 "2024-02-28T15:36:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![larismartins](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/larismartins/32/2712_2.png) [@larismartins](https://community.freefem.org/u/larismartins)\
**Post date:** [February 28, 2024, 3:36pm UTC](https://community.freefem.org/t/error-between-numerical-solutions/3005/1 "2024-02-28T15:36:09Z")

</div>

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,…)?

---

<div class="post-metadata">

**Author:** ![jsmacd](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jsmacd](https://community.freefem.org/u/jsmacd)\
**Post date:** [February 28, 2024, 9:14pm UTC](https://community.freefem.org/t/error-between-numerical-solutions/3005/2 "2024-02-28T21:14:31Z")

</div>

Yes, you can 🙂

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

`u_mesh2 = u_mesh1`

---

<div class="post-metadata">

**Author:** ![larismartins](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/larismartins/32/2712_2.png) [@larismartins](https://community.freefem.org/u/larismartins)\
**Post date:** [February 29, 2024, 11:06am UTC](https://community.freefem.org/t/error-between-numerical-solutions/3005/3 "2024-02-29T11:06:44Z")

</div>

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

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

```

but I’m getting the following error:

```auto
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?

---

<div class="post-metadata">

**Author:** ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)\
**Post date:** [February 29, 2024, 11:21am UTC](https://community.freefem.org/t/error-between-numerical-solutions/3005/4 "2024-02-29T11:21:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![marchywka](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@marchywka](https://community.freefem.org/u/marchywka)\
**Post date:** [February 29, 2024, 1:40pm UTC](https://community.freefem.org/t/error-between-numerical-solutions/3005/5 "2024-02-29T13:40:07Z")

</div>

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 🙂

```auto
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

```
