# Adapt mesh problems and question about MPI

**URL:** https://community.freefem.org/t/adapt-mesh-problems-and-question-about-mpi/24
**Category:** General Discussion
**Created:** [April 10, 2019, 5:50pm UTC](https://community.freefem.org/t/adapt-mesh-problems-and-question-about-mpi/24 "2019-04-10T17:50:55Z")
**Posts on this page:** 5
**Page:** 1

<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: [April 10, 2019, 5:50pm UTC](https://community.freefem.org/t/adapt-mesh-problems-and-question-about-mpi/24/1 "2019-04-10T17:50:56Z")

</div>

Hello,

I am new in the community of FreeFem. I am trying to use a code that we haven’t used in my lab for quite a long time. As you can see in the code below after loading a base mesh we use the tool adpatmesh and split mesh but the results obtained were a little bit weird . I have tested it on a rectangular domain. I have added attached the basic mesh ( left figure) that I used and the mesh after using adaptmesh ( right figure) and as you can see the mesh is absolutly randomly refined and I wanted a simple homogeneous mesh. Maybe I am not using adaptmesh correctly? Can someone see and error in the functions parameters?

I have also a question regarding the parallelization of a little part of my code. the splitmesh sometimes crashes for huge mesh and I wanted to know if it possible to use mpi to parallelize just this function?

I really thank you in advance for your help.

 ![basic_and_adapted_mesh](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/cd97501c72dd912dd11acc303b4ff8b70f744f90.jpeg)

Code used:

include “getARGV.idp”

// 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 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;  
{  
ifstream fid(urname);  
fid \>\> ur[];  
}  
{  
ifstream fid(utname);  
fid \>\> ut[];  
}  
{  
ifstream fid(uxname);  
fid \>\> ux[];  
}  
{  
ifstream fid(nuname);  
fid \>\> nu[];  
}

// interpolate implicitely on P2 space  
Xh U=ur, V=ut, W=ux, Visco=nu;  
Mh PrMean;

ratio = 1.1  
hmax = 1e-04

// adapt mesh to velocity field  
if(refine == 1) {  
Th = adaptmesh(Th, nbvx=10000, ratio=1.1, hmax=1e-04);  
U = U;  
V = V;  
W = W;  
Visco = Visco;  
PrMean = PrMean;  
}

// re-export the connectivity and coordinates for PFS format  
{ ofstream file(prefix+“coordinates”+suffix);  
for (int j=0;j\<Th.nv; j++) {  
file \<\< Th(j).x \<\< " " \<\< Th(j).y \<\< endl;}  
}

{ ofstream file(prefix+“connectivity”+suffix);  
int nbtriangle = Th.nt;  
for (int i=0;i\<Th.nt; i++){  
file \<\< Th[i][0]+1 \<\< " " \<\< Th[i][1]+1 \<\< " " \<\< Th[i][2]+1 \<\< endl;}  
}

// split it and save it again  
mesh Th2 = splitmesh(Th, 2);  
{ ofstream file(prefix+“coordinates-2”+suffix);  
for (int j=0;j\<Th2.nv; j++) {  
file \<\< Th2(j).x \<\< " " \<\< Th2(j).y \<\< endl;}  
}

{ ofstream file(prefix+“connectivity-2”+suffix);  
int nbtriangle = Th2.nt;  
for (int i=0;i\<Th2.nt; i++){  
file \<\< Th2[i][0]+1 \<\< " " \<\< Th2[i][1]+1 \<\< " " \<\< Th2[i][2]+1 \<\< endl;}  
}

---

<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: [April 10, 2019, 7:23pm UTC](https://community.freefem.org/t/adapt-mesh-problems-and-question-about-mpi/24/2 "2019-04-10T19:23:06Z")

</div>

Welcome in the FreeFEM community.

In order to refine your mesh in this way, you have to write the following instruction:

```auto
real h = 1e-4;
Th = adaptmesh(Th, h, IsMetric=true, nbvx=1e5);

```

For the parallelization, the best way is to use the domain decomposition method with split, see e.g  
[This example](https://github.com/FreeFem/FreeFem-sources/blob/master/examples/hpddm/diffusion-2d-PETSc.edp), but you have to parallelize the complete script.

---

<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: [April 12, 2019, 2:44pm UTC](https://community.freefem.org/t/adapt-mesh-problems-and-question-about-mpi/24/3 "2019-04-12T14:44:19Z")

</div>

Hello Simon,

Thank you for your quick reply. I tried exactly the instruction that you gave me . But as you can see in the picture below my mesh is still not homogeneous. Do you have other ideas?  
 ![45](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/7e728f860bacf61f0ef8978ff20dd714aa10f054.png)

Thanks again

---

<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: [April 12, 2019, 2:56pm UTC](https://community.freefem.org/t/adapt-mesh-problems-and-question-about-mpi/24/4 "2019-04-12T14:56:47Z")

</div>

Do you have a warning where `adaptmesh` function is called ?  
If you have not enought vertices (limit defined with `nbvx`), the mesh adaptation won’t run perfectly

---

<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: [April 12, 2019, 3:29pm UTC](https://community.freefem.org/t/adapt-mesh-problems-and-question-about-mpi/24/5 "2019-04-12T15:29:50Z")

</div>

Thanks it is working now! and thank you also for the parallelized code example 😉
