Adapt mesh problems and question about MPI



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.

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;}

(Simon Garnotel) #2

Welcome in the FreeFEM community.

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

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, but you have to parallelize the complete script.


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?

Thanks again

(Simon Garnotel) #4

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


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