# Refine different regions with adaptmesh

**URL:** <https://community.freefem.org/t/refine-different-regions-with-adaptmesh/1404>\
**Category:** General Discussion\
**Created:** [December 29, 2021, 2:55pm UTC](https://community.freefem.org/t/refine-different-regions-with-adaptmesh/1404 "2021-12-29T14:55:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruffi92](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/ruffi92/32/921_2.png) [@ruffi92](https://community.freefem.org/u/ruffi92)\
**Post date:** [December 29, 2021, 2:55pm UTC](https://community.freefem.org/t/refine-different-regions-with-adaptmesh/1404/1 "2021-12-29T14:55:04Z")

</div>

Hi to all,

i have the following mesh and variational problem:

//Rectangle [0 2] x [-1 0]  
border b(t=0,1) {x=t; y=-1; label=1;};  
border c(t=1,2) {x=t; y=-1; label=2;};  
border d(t=-1,0) {x=2; y=t; label=3;};  
border e(t=0,1) {x=2-t; y=0; label=4;};  
border f(t=0,1) {x=1-t; y=0; label=5;};  
border a(t=0,1 ) {x=0 ; y=-t; label=6;};  
border g(t=-1,0) {x=1; y=t; label=7;};

mesh Th = buildmesh(a(5)+b(5)+c(5)+d(5)+e(5)+f(5)+g(10));

//TIME-DEPENDENT PROBLEM  
fespace Vhh(Th,P1);   
fespace Nh(Th,P0);  
Vhh u1,u2,v1,v2,uold1,uold2;  
real Dt=0.5;

uold1=1e+2;  
uold2=1e+2;

problem TIME(u1,u2,v1,v2,solver=sparsesolver,eps=1e-10)=  
int2d(Th,qforder=2)(D1\*(dx(u1)_dx(v1)+dy(u1)dy(v1)))  
+int2d(Th,qforder=2)(D2(dx(u2)dx(v2)+dy(u2)dy(v2)))  
+int2d(Th,qforder=2) (vinv1u1v1/Dt)  
-int2d(Th,qforder=2) (vinv1_uold1_v1/Dt)  
+int2d(Th,qforder=2) (vinv2_u2_v2/Dt)  
-int2d(Th,qforder=2) (vinv2_uold2_v2/Dt)  
+int2d(Th,qforder=2)((Sa1+Ss12)u1v1)  
+int2d(Th,qforder=2)((Sa2+Ss21)u2v2)  
-int2d(Th,qforder=2)(Ss21_u2_v1)  
-int2d(Th,qforder=2)(Ss12_u1_v2)  
-int2d(Th,qforder=2)(f0_v1)  
-int2d(Th,qforder=2)(Sf1_u1_v1+Sf2_u2_v1)  
+on(4,u1=1e-10,u2=1e-10);

//definition of metric “h” wrt u1 or u2  
(…)

Th = adaptmesh(Th,h, IsMetric=1, nbvx=800000,hmin=hmin,hmax=hmax);

My goal is refine ONLY the “first” square [0 1]x[-1 0] when i pass the “h” calculated wrt u1, refine ONLY the “second” square [1 2]x[-1 0] when i pass the “h” calculated wrt u2, and of course refine the ENTIRE domain if i calculate “h” as a linear combination of u1 and u2

How can i do this?  
Initially i try to split my mesh in 2 mesh like :

Mesh Th1 = \first square;  
Mesh Th2 = \second square;  
Mesh Th = Th1+Th2;

but the second one [1 2]x[-1 0] gives an error.  
Apart from this, i think that the “chi” function could help in this way:

u1=u1_chi(Th1);  
u2=u2_chi(Th2);

…but does not work as i expect.

Anyone can give me some tips? Thanks!

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [December 30, 2021, 1:29pm UTC](https://community.freefem.org/t/refine-different-regions-with-adaptmesh/1404/2 "2021-12-30T13:29:49Z")

</div>

The problem to refined only one once region is not so simple, but you can do  
this kind of trick

```auto

mesh Th1 = square(10,10,region=1);
mesh Th2 = square(10,10,region=2,[-x,y]);
// remarg the label = 4 is common line 
int[int] llr=[4]; // list of label of required edges 
mesh Th = Th1+Th2; 

//

Th2 = adaptmesh(Th2,1./20,IsMetric=1,requirededges=llr);

Th = Th1+Th2;

plot(Th,wait=1);

```

But bee careful with the compatibility of the metric and mes mesh border required

---

<div class="post-metadata">

**Author:** ![ruffi92](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/ruffi92/32/921_2.png) [@ruffi92](https://community.freefem.org/u/ruffi92)\
**Post date:** [December 31, 2021, 9:30am UTC](https://community.freefem.org/t/refine-different-regions-with-adaptmesh/1404/3 "2021-12-31T09:30:55Z")

</div>

and if i consider only the “full” domain and want refine only the top-left corner (for example) when i pass the metric “h” calculated wrt u1? There is a way to impose this using adaptmesh?

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [January 11, 2022, 12:15pm UTC](https://community.freefem.org/t/refine-different-regions-with-adaptmesh/1404/4 "2022-01-11T12:15:34Z")

</div>

To day no way to say a region is fixe in adaptmesh.  
But it is not so hard add this trick in adaptmesh.

---

<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:** [January 11, 2022, 12:39pm UTC](https://community.freefem.org/t/refine-different-regions-with-adaptmesh/1404/5 "2022-01-11T12:39:58Z")

</div>

I’m trying to do the same thing, but no success. I did something like this:

```auto
int globalRef = 2;
int localRef = 2;

mesh Th = square(globalRef,globalRef,region=1);
mesh Th1 = square(localRef,localRef, region=2, [x/2,y/2]);

plot(Th1,wait=1);

mesh P = Th + Th1;

plot(P,wait=1);

```

But what I really want to do is to refine each triangle differently

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [January 12, 2022, 7:37am UTC](https://community.freefem.org/t/refine-different-regions-with-adaptmesh/1404/6 "2022-01-12T07:37:28Z")

</div>

Remark,

The mesh Th is wrong because you have overlap region.

I have make a example a hard to build a mesh of a screw with this trick between line 200 to 220.  
Sorry this is a really an hard script to build the mesh.

[mesh-vis-3d.edp](https://community.freefem.org/uploads/short-url/uG1tp0Dk2AZtOmKy1DE17rIMTAk.edp) (8.6 KB)
