# Gmsh and freefem

**URL:** <https://community.freefem.org/t/gmsh-and-freefem/352>\
**Category:** General Discussion\
**Created:** [April 18, 2020, 2:07pm UTC](https://community.freefem.org/t/gmsh-and-freefem/352 "2020-04-18T14:07:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrbenson](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mrbenson](https://community.freefem.org/u/mrbenson)\
**Post date:** [April 18, 2020, 2:07pm UTC](https://community.freefem.org/t/gmsh-and-freefem/352/1 "2020-04-18T14:07:45Z")

</div>

Hi,  
I’m quite new, i’d like to use freefem with gmsh file mesh.  
LoadMesh function make easy to import a 2D gmsh mesh file and labels(Th) function give all physical boundary tags (dim=1).  
1 - I’d like to know how to get all physical region tags (dim=2), is there a function return that list ?  
2 - I want to solve the heat equation with different kappa for each region, how to define a piecewise constant value for each subdomain identified by their physical tag ? All examples tackles the case where subdomains are easily defined by the indicator domain function, but in my case each domain are complex and only the physical region tag can help to indicate the label region. Is there an instruction to define kappa as the sum of indicator region function lika kappa = 4_on(region1) + 2_on(region2) ?

Best regards

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [April 18, 2020, 2:16pm UTC](https://community.freefem.org/t/gmsh-and-freefem/352/2 "2020-04-18T14:16:05Z")

</div>

```auto
fespace Ph(Th, P0);
Ph reg = region;
plot(reg);
int region1, region2;
Ph kappa = 4 * (region == region1) + 2 * (region == region2);

```

---

<div class="post-metadata">

**Author:** ![mrbenson](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mrbenson](https://community.freefem.org/u/mrbenson)\
**Post date:** [April 18, 2020, 2:39pm UTC](https://community.freefem.org/t/gmsh-and-freefem/352/3 "2020-04-18T14:39:43Z")

</div>

Thanks that works fine !  
is there a way to get the array of all physical tags ?

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [April 18, 2020, 2:56pm UTC](https://community.freefem.org/t/gmsh-and-freefem/352/4 "2020-04-18T14:56:13Z")

</div>

I’m guessing something like this should work fine.

```auto
real[int] tags;
Unique(reg[], tags);
cout << tags << endl;

```

---

<div class="post-metadata">

**Author:** ![mrbenson](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mrbenson](https://community.freefem.org/u/mrbenson)\
**Post date:** [April 18, 2020, 7:40pm UTC](https://community.freefem.org/t/gmsh-and-freefem/352/5 "2020-04-18T19:40:18Z")

</div>

In fact that’s works well !

I have many regions in my msh file (up to 100). Is there a trick to write kappa definition with many lines instead of having a very long line definition ? Is seems that += operator does not work.

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [April 19, 2020, 6:15am UTC](https://community.freefem.org/t/gmsh-and-freefem/352/6 "2020-04-19T06:15:22Z")

</div>

```auto
Ph kappa;
for(int i = 0; i < tags.n; ++i) {
  Ph tag = tags[i] * (region == tags[i]);
  kappa[] += tag[];
}

```

---

<div class="post-metadata">

**Author:** ![mrbenson](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mrbenson](https://community.freefem.org/u/mrbenson)\
**Post date:** [April 19, 2020, 10:25am UTC](https://community.freefem.org/t/gmsh-and-freefem/352/7 "2020-04-19T10:25:24Z")

</div>

Hi,

Thanks a lot for this short trick, may be you meants Ph tag= valueOfKappa[i] \* (region == tags[i]) ?

I’m very sorry to ask some basic questions but i didin’t find any help even if the freefem documentation is quite big.

1 - Moreover i want to define two boundary functions, for example temperature and conductance surface for each mixed boundary condition so that i coult add this in my weak formulation :

- int1d(Th)(Rs_u_v)

- int1d(Th)(Rs_Ts_v);

I guess “region” keyword won’t work with labels ? I didn’t find the equivalent keyword for boundaries…

2 - Is there a way to prescribe dirichlet condition for all boundary defined by an int-array ?

uBC = 273.15 \* (region==boundary1) + 293.15 \* (region==boundary2);

So that i could write this in the weak formula :  
+on(arrayDir, u=uDir)

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [April 19, 2020, 11:48am UTC](https://community.freefem.org/t/gmsh-and-freefem/352/8 "2020-04-19T11:48:59Z")

</div>

Please read the [documentation](https://doc.freefem.org/pdf/FreeFEM-documentation.pdf).

---

<div class="post-metadata">

**Author:** ![bcaval](https://avatars.discourse-cdn.com/v4/letter/b/d26b3c/32.png) [@bcaval](https://community.freefem.org/u/bcaval)\
**Post date:** [November 6, 2020, 7:04pm UTC](https://community.freefem.org/t/gmsh-and-freefem/352/10 "2020-11-06T19:04:29Z")

</div>

Hi everybody,

I have the same kind of problem but with general elasticity Cijkl. I have 3 domains, but also we could have more. And I wrote

fespace Ph(Th3,P0);

Ph[int] c(37);  
for (i=1;i\<=6;i++) {  
for (j=1;j\<=6;j++)  
c[(i-1)_6+j]=c1(i,j)_(region==1)+c2(i,j)_(region==2)+c3(i,j)_(region==3);  
}  
where cI(i,j) have been given before as real[int,int].

after I use macro  
macro C [[c[1], c[2], c[3], c[4], c[5], c[6] ],  
[c[7], c[8], c[9], c[10], c[11], c[12] ],  
[c[13], c[14], c[15], c[16], c[17], c[18] ],  
[c[19], c[20], c[21], c[22], c[23], c[24] ],  
[c[25], c[26], c[27], c[28], c[29], c[30] ],  
[c[31], c[32], c[33], c[34], c[35], c[36] ]] // EOM

and then a varf as  
varf vK(u,v)=int3d(Th3)( (C\*epsilon(u))’ \*epsilon(v)) etc ; where epsilon(u) describes strain usual parts.

But my model doesn’t work. And if I replace **Ph by real** it gives results but I imagine that my domains are not taken into account. Because how a real declaration can take into account a boolean part (region==k)?

So I know I am not very familiar with syntax and I aim to write elegant macro for simplicity but I didn’t achieve it for this general elastic, and then thermoelastic coupled problem.

Could you help me to find the right method to attribute physical properties to many domains (but tensor properties, not only nu or kappa or…) ?

Because I know that I could presumably use 3 varf with a mesh trunc for each domain but I hope a better solution.

Best regards.
