# A problem with adaptmesh?

**URL:** https://community.freefem.org/t/a-problem-with-adaptmesh/94
**Category:** General Discussion
**Created:** [July 13, 2019, 5:54pm UTC](https://community.freefem.org/t/a-problem-with-adaptmesh/94 "2019-07-13T17:54:10Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Charles](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@Charles](https://community.freefem.org/u/Charles)
#### Post date: [July 13, 2019, 5:54pm UTC](https://community.freefem.org/t/a-problem-with-adaptmesh/94/1 "2019-07-13T17:54:11Z")

</div>

Dear all,  
I am trying to run an example involving the resolution of several time-dependent pdes on meshes adapted at each time step, and I very soon get stuck with problems related to bad memory allocation. After some checks, I found a weird behavior related to the adaptmesh function which might explain my troubles: the finite element functions defined on the mesh are not automatically resized (or they are sometimes, but a little bit unpredicatably…).

Here is a minimal example:

/\* Mesh of the domain \*/  
border left(t=0.0,1.5){x=0.0; y=1.5-t; label=0;};  
border bot(t=0.0,1.0){x=t; y=0; label=1;};  
border right(t=0.0,1.5){x=1.0; y=t; label=0;};  
border top(t=0.0,1.0){x=1.0-t; y=1.5; label=1;};

mesh Th = buildmesh(left(75)+bot(50)+right(75)+top(50));  
/\* Finite element spaces \*/  
fespace Ph(Th,P1);  
fespace Ph0(Th,P0);

/\* Finite element functions \*/  
Ph u;  
Ph0 p;

/\* Before adaptation; the number of vertices coincide from both sources \*/  
cout\<\<"Number of vertices “\<\<Th.nv\<\<” in u "\<\<u[].n\<\<endl;  
cout\<\<"Number of trias “\<\<Th.nt\<\<” in p "\<\<p[].n\<\<endl;

/\* Adaptation \*/  
Th = adaptmesh(Th,hmin=0.01,hmax=0.01,nbvx=30000,iso=1);

/\* After adaptation; the size of u and p is not changed (in some of my examples, it is  
quite randomly \*/  
cout\<\<"Number of vertices “\<\<Th.nv\<\<” in u "\<\<u[].n\<\<endl;  
cout\<\<"Number of trias “\<\<Th.nt\<\<” in p "\<\<p[].n\<\<endl;

Is there something I am missing about this?  
Thank you so much for your help in advance, and have a good day!

---

<div class="post-metadata">

### Author: ![simon.garnotel](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/simon.garnotel/32/11_2.png) [@simon.garnotel](https://community.freefem.org/u/simon.garnotel)
#### Post date: [July 13, 2019, 11:09pm UTC](https://community.freefem.org/t/a-problem-with-adaptmesh/94/2 "2019-07-13T23:09:40Z")

</div>

Hi,

You have to interpolate your variables just after the `adaptmesh` function:

```auto
u = u;
p = p;

```

I think that fix your issue.

Best

---

<div class="post-metadata">

### Author: ![Charles](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@Charles](https://community.freefem.org/u/Charles)
#### Post date: [July 14, 2019, 3:55pm UTC](https://community.freefem.org/t/a-problem-with-adaptmesh/94/3 "2019-07-14T15:55:18Z")

</div>

Thanks a lot! That solves my problem!  
Thanks again for your help!

---

<div class="post-metadata">

### Author: ![JMMC](https://avatars.discourse-cdn.com/v4/letter/j/8491ac/32.png) [@JMMC](https://community.freefem.org/u/JMMC)
#### Post date: [July 15, 2019, 11:21am UTC](https://community.freefem.org/t/a-problem-with-adaptmesh/94/4 "2019-07-15T11:21:52Z")

</div>

Dear all,

I am also having trouble with adaptmesh. I am trying to create a mesh from a metric but the results is not very smooth as shown in the picture attached. Note that I have tried to increase the number of points and the number of iterations of the smoothing procedure but nothing changes…

Here is my code:

// read the parameters  
real m = 1;  
string meshname = getARGV("-mesh", “BASE/ffem-mesh.msh”);  
string urname = getARGV("-ur", “BASE/ffem-ur.dat”);  
string utname = getARGV("-ut", “BASE/ffem-ut.dat”);  
string uxname = getARGV("-ux", “BASE/ffem-ux.dat”);  
string nuname = getARGV("-nu", “BASE/ffem-nu.dat”);  
string metricname = getARGV("-metric", “BASE/ffem-metric.dat”);  
string prefix = getARGV("-prefix", “BASE/ffem-”);  
string suffix = getARGV("-suffix", “.dat”);  
string lindir = getARGV("-lin", “LIN”);  
int refine = getARGV("-refine", 0);  
real cRatio = getARGV("-ratio", 1.2);  
real cHmax = getARGV("-hmax", 5.0e-03);  
int nol = getARGV("-nol", 1);

// import dimensionless mesh  
mesh Th = readmesh(meshname);

// prepare the vector spaces  
fespace Xh(Th, P2);  
fespace Mh(Th, P1);

// import meanfields on P1 space  
Mh ur, ut, ux, nu, metric;  
{  
ifstream fid(urname);  
fid \>\> ur[];  
}  
{  
ifstream fid(utname);  
fid \>\> ut[];  
}  
{  
ifstream fid(uxname);  
fid \>\> ux[];  
}  
{  
ifstream fid(nuname);  
fid \>\> nu[];  
}

{  
ifstream fid(metricname);  
fid \>\> metric[];  
}  
plot(Th,ps=“mesh\_read.eps”,wait=true);  
// interpolate implicitely on P2 space  
Xh U=ur, V=ut, W=ux, Visco=nu, Metri=metric;  
Mh PrMean;  
real h=0.3e-3;  
//real h=1e-4;  
// adapt mesh to velocity field  
cout \<\< " – max min metric = " \<\<Metri[].min \<\< " " \<\< Metri[].max \<\< endl;

if(refine == 1) {  
cout \<\< “refinement”\<\< endl;  
// Th = adaptmesh(Th,h, nbvx=1e7, IsMetric=true);  
Th = adaptmesh(Th,Metri, nbvx=2e7,nbsmooth=20);  
// Th = adaptmesh(Th,Metri, nbvx=1e7,IsMetric=true);  
U = U;  
V = V;  
W = W;  
Metri=Metri;  
Visco = Visco;  
PrMean = PrMean;  
}  
Thank you for your help in advance

Matthieu

![49](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/9ece5ca3708e3a70a237bf6f5215cd4f28bab34d.png)

---

<div class="post-metadata">

### Author: ![UgisL](https://avatars.discourse-cdn.com/v4/letter/u/c0e974/32.png) [@UgisL](https://community.freefem.org/u/UgisL)
#### Post date: [July 23, 2019, 1:20pm UTC](https://community.freefem.org/t/a-problem-with-adaptmesh/94/5 "2019-07-23T13:20:55Z")

</div>

Hi,

you could try to force the adaptmesh to create isotropic meshes. Add the following keyword:  
adaptmesh(…, iso=true, …);  
and see if this helps.

Best,  
Ugis

---

<div class="post-metadata">

### Author: ![kellen11](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kellen11](https://community.freefem.org/u/kellen11)
#### Post date: [April 21, 2024, 11:29am UTC](https://community.freefem.org/t/a-problem-with-adaptmesh/94/6 "2024-04-21T11:29:36Z")

</div>

Dear all.  
I want to know whether the function adpatmesh in freefem++ can adapt the specific elements based on the initial mesh fixed or not. That is to say, can AdaptMesh refine mesh cells that meet certain error requirements while keeping the original mesh unchanged? I’m looking forward to your reply. Thank you very much!
