# Symmetry compliant mesh generation

**URL:** <https://community.freefem.org/t/symmetry-compliant-mesh-generation/2276>\
**Category:** General Discussion\
**Created:** [February 13, 2023, 4:02pm UTC](https://community.freefem.org/t/symmetry-compliant-mesh-generation/2276 "2023-02-13T16:02:42Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![avezz194](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@avezz194](https://community.freefem.org/u/avezz194)\
**Post date:** [February 13, 2023, 4:02pm UTC](https://community.freefem.org/t/symmetry-compliant-mesh-generation/2276/1 "2023-02-13T16:02:43Z")

</div>

I need to generate a 2D mesh using triangular elements inside an hexagonal domain. I want my mesh to respect all the symmetries of the hexagon symmetry group. Is there a simple way to do that?

---

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [February 13, 2023, 5:45pm UTC](https://community.freefem.org/t/symmetry-compliant-mesh-generation/2276/2 "2023-02-13T17:45:46Z")

</div>

Technically, I suggest you generate the vertices yourself, and use the `triangulate` command ([Mesh Generation](https://doc.freefem.org/documentation/mesh-generation.html#the-keyword-triangulate)) to generate the mesh. However, I have no idea how you should generate the vertices.

---

<div class="post-metadata">

**Author:** ![marchywka](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@marchywka](https://community.freefem.org/u/marchywka)\
**Post date:** [February 13, 2023, 6:17pm UTC](https://community.freefem.org/t/symmetry-compliant-mesh-generation/2276/3 "2023-02-13T18:17:46Z")

</div>

What do you hope to gain? There is always the issue of floating point round off  
error in these things. Some code seems to take great care of this,  
fixed or indefinite precision math, while I’m not sure what others do for tolerances.  
If it really is symmetric whatever you are running maybe should know that.

If you are going to solve in all 6 sections don’t you want your result to be fairly  
robust against mesh details?

---

<div class="post-metadata">

**Author:** ![avezz194](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@avezz194](https://community.freefem.org/u/avezz194)\
**Post date:** [February 13, 2023, 7:00pm UTC](https://community.freefem.org/t/symmetry-compliant-mesh-generation/2276/4 "2023-02-13T19:00:33Z")

</div>

Thank you for your answer. I am solving the Schrodinger equation on hexagonal domains. If the system has inversion symmetry, each of the resulting eigenvalues is doubly degenerate. Nevertheless, if I solve the eignevalue problem on a mesh that is not perfectly symmetric (of course, neglecting the floating point round off errors you mentioned), the degeneracy of the energy eigenvalues is slightly broken.

---

<div class="post-metadata">

**Author:** ![avezz194](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@avezz194](https://community.freefem.org/u/avezz194)\
**Post date:** [February 13, 2023, 7:06pm UTC](https://community.freefem.org/t/symmetry-compliant-mesh-generation/2276/5 "2023-02-13T19:06:51Z")

</div>

Thank you for your quick response. Let’s assume I am able to generate the vertices manually as you suggested. I would also need to specify edge labels for the outer boundary and to give different labels to triangles in different regions of the mesh. This is because I want to simulate a core-shell hexagonal structure, with different material properties for the core and the shell region respectively. Would it be possible to add these properties to the mesh object after having generated it with the `triangulate` command?

---

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [February 13, 2023, 8:07pm UTC](https://community.freefem.org/t/symmetry-compliant-mesh-generation/2276/6 "2023-02-13T20:07:52Z")

</div>

I think you can achieve that with the `change` command. However, I have never used it myself. Consult the documentation and the forum on the `change` function for more info.

---

<div class="post-metadata">

**Author:** ![marchywka](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@marchywka](https://community.freefem.org/u/marchywka)\
**Post date:** [February 13, 2023, 8:08pm UTC](https://community.freefem.org/t/symmetry-compliant-mesh-generation/2276/7 "2023-02-13T20:08:16Z")

</div>

There is a 2D harmonic oscillator example. I just ran that with whatever mesh it makes for  
a square. The mesh AFAICT is a perfect grid but I guess if you wanted to play with that you  
could run it with a distroted mesh or maybe move a few verticieis and see what happens  
to the energies. The first few are below and good to a few sig figs. You could  
also play with the element types I guess. Are you looking for something subtle like  
a small Jahn-Teller ? In any case you probably want to make sure it is not a function  
of numerical parameters 🙂

Degeneracy  
of course may be a physical and math problem 🙂

```auto
Eigenvalue #0 = 1.00104
 Eigenvalue #1 = 2.00187
 Eigenvalue #2 = 2.00437
 Eigenvalue #3 = 3.00332
 Eigenvalue #4 = 3.00561

```

---

<div class="post-metadata">

**Author:** ![julienG](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/julieng/32/222_2.png) [@julienG](https://community.freefem.org/u/julienG)\
**Post date:** [February 14, 2023, 4:16am UTC](https://community.freefem.org/t/symmetry-compliant-mesh-generation/2276/8 "2023-02-14T04:16:05Z")

</div>

If I had to do that, I would create the elementary triangle (of the “coarse” mesh if you want), then rotate it, and glue it 6 times… This surely won’t have the D\_6 symmetry, but I guess if you do it now with equilateral triangles 12 times your mesh should be invariant under the D\_6 group.  
Otherwise did you look at this [thread](https://community.freefem.org/t/how-to-build-a-structured-mesh/2175)?  
Hope this helps

---

<div class="post-metadata">

**Author:** ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)\
**Post date:** [February 14, 2023, 6:27pm UTC](https://community.freefem.org/t/symmetry-compliant-mesh-generation/2276/9 "2023-02-14T18:27:11Z")

</div>

What about something like this:

```auto
real L = 1.0;
border C1(t=0, 1){x=L*t; y=0.0;}
border C2(t=0, 1){x=L*(1.0-t*0.5); y=L*(0.5*t*sqrt(3.0));}
border C3(t=0, 1){x=L*0.5*(1.0-t); y=L*0.5*(1.0-t)*sqrt(3.0);}
int nn = 20;
mesh Th1 = buildmesh(C1(nn) + C2(nn) + C3(nn));
mesh Th2 = movemesh(Th1,[L+(x*cos(pi/3.0)-y*sin(pi/3.0)),x*sin(pi/3.0)+y*cos(pi/3.0)]);
mesh Th3 = movemesh(Th1,[-(x*cos(pi/3.0)-y*sin(pi/3.0)),(x*sin(pi/3.0)+y*cos(pi/3.0))]);
mesh Thhalf1 = Th1+Th2+Th3;
mesh Thhalf2 = movemesh(Thhalf1,[x,L*sqrt(3.0)-y]);
mesh Thwhole = Thhalf1+Thhalf2;
plot(Thwhole);

```

---

<div class="post-metadata">

**Author:** ![avezz194](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@avezz194](https://community.freefem.org/u/avezz194)\
**Post date:** [February 15, 2023, 9:08am UTC](https://community.freefem.org/t/symmetry-compliant-mesh-generation/2276/10 "2023-02-15T09:08:49Z")

</div>

Hi, thank you for your interest in the topic. Yeah, I agree with you. The most simple way to do that should be to create the “elementary triangle” and then use a combination of the symmetry operations of the group to get the desired mesh invariance. I did not see the thread yuo mentioned, thanks a lot.

---

<div class="post-metadata">

**Author:** ![avezz194](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@avezz194](https://community.freefem.org/u/avezz194)\
**Post date:** [February 15, 2023, 9:18am UTC](https://community.freefem.org/t/symmetry-compliant-mesh-generation/2276/11 "2023-02-15T09:18:49Z")

</div>

Hi, thank you for your answer. Your proposal is inspiring. Probably the correct “elementay triangle” is _half_ the starting equilateral triangle. Otherwise, for some values of `nn`, the number of edges for each border, we could lose the invariance under reflection with respect some planes, for instance x=0 and y=0 planes. But I’m going to develop your idea, since I think it’s the proper starting point.

---

<div class="post-metadata">

**Author:** ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)\
**Post date:** [February 15, 2023, 9:33am UTC](https://community.freefem.org/t/symmetry-compliant-mesh-generation/2276/12 "2023-02-15T09:33:47Z")

</div>

Good point. Then simply do this:

```auto
real L = 1.0;
border C1(t=0, 1){x=0.5*L*t; y=0.0;}
border C2(t=0, 1){x=0.5*L; y=L*(0.5*t*sqrt(3.0));}
border C3(t=0, 1){x=L*0.5*(1.0-t); y=L*0.5*(1.0-t)*sqrt(3.0);}
int nn = 20;
mesh Th01 = buildmesh(C1(0.5*L*nn) + C2(0.5*L*sqrt(3.0)*nn) + C3(L*nn));
mesh Th02 = movemesh(Th01,[L-x,y]);
mesh Th1 = Th01 + Th02;
mesh Th2 = movemesh(Th1,[L+(x*cos(pi/3.0)-y*sin(pi/3.0)),x*sin(pi/3.0)+y*cos(pi/3.0)]);
mesh Th3 = movemesh(Th1,[-(x*cos(pi/3.0)-y*sin(pi/3.0)),(x*sin(pi/3.0)+y*cos(pi/3.0))]);
mesh Thhalf1 = Th1 + Th2 + Th3;
mesh Thhalf2 = movemesh(Thhalf1,[x,L*sqrt(3.0)-y]);
mesh Thwhole = Thhalf1+Thhalf2;
plot(Thwhole);

```

---

<div class="post-metadata">

**Author:** ![avezz194](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@avezz194](https://community.freefem.org/u/avezz194)\
**Post date:** [February 15, 2023, 10:07am UTC](https://community.freefem.org/t/symmetry-compliant-mesh-generation/2276/14 "2023-02-15T10:07:53Z")

</div>

Thank you, it seems to work properly. That’s nice! Now I think that I can use the `change` command to redefine border lables and region labels the way I need.
