# 3D mesh: periodic boundary conditions and unstructured mesh

**URL:** https://community.freefem.org/t/3d-mesh-periodic-boundary-conditions-and-unstructured-mesh/2781
**Category:** General Discussion
**Created:** [November 6, 2023, 6:41pm UTC](https://community.freefem.org/t/3d-mesh-periodic-boundary-conditions-and-unstructured-mesh/2781 "2023-11-06T18:41:30Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jsmacd](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jsmacd](https://community.freefem.org/u/jsmacd)
#### Post date: [November 6, 2023, 6:41pm UTC](https://community.freefem.org/t/3d-mesh-periodic-boundary-conditions-and-unstructured-mesh/2781/1 "2023-11-06T18:41:30Z")

</div>

Dear Community,

I understand that the faces have to be the same for periodic boundary conditions, but I also would like to use an unstructured mesh. Does anyone know how I can accomplish this? The code below is what does not work at the moment.

```auto
// build an unstructured mesh for the cube
// run in terminal with FreeFEM++ unstrucCubeMesh.edp

load "distance"
load "msh3"
load "tetgen"
load "medit"
load "mshmet"
load "iovtk"

include "Cube.idp"

int[int] NN = [10,10,10]; //the number of step in each direction
real [int, int] BB = [[0,1],[0,1],[0,1]]; //the bounding box
int [int, int] LB = [[1,2],[3,4],[5,6]]; //the label of the 6 face left,right, front, back, down, right
mesh3 Th = Cube(NN, BB, LB);

medit("Th", Th);
real L = 1.;
int meshSize = 30.;
real[int] domain = [0, 0, 0, 0, 1];

Th = tetgreconstruction(Th, switch = "raAQ", regionlist = domain, sizeofvolume = (L*1./meshSize)^3/6);

medit("Th", Th);

fespace Vh(Th, P1, periodic = [[5, x, y], [6, x, y]]); // periodic boundary conditions on z = 0, 1

```

---

<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: [November 6, 2023, 7:01pm UTC](https://community.freefem.org/t/3d-mesh-periodic-boundary-conditions-and-unstructured-mesh/2781/2 "2023-11-06T19:01:10Z")

</div>

Use Mmg with the parameter `requiredTriangle`.

---

<div class="post-metadata">

### Author: ![jsmacd](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jsmacd](https://community.freefem.org/u/jsmacd)
#### Post date: [November 6, 2023, 7:59pm UTC](https://community.freefem.org/t/3d-mesh-periodic-boundary-conditions-and-unstructured-mesh/2781/3 "2023-11-06T19:59:24Z")

</div>

Thank you! So this would be instead of tetgen right?

I’ve found this example using mshmet and mmg (laplace-adapt-aniso-3d.edp). Basically all the examples I’m finding with mmg are using mshmet. I’m trying to understand the parameters here, but don’t understand this `metric` parameter. In the below code I am trying to just understand how things work without solving poisson.

```auto

{
load "msh3"
load "tetgen"
load "mshmet"
load "medit"
load "mmg"

int nn = 6;

int[int] lc=[1,2,2,1,1,2]; // label numbering

mesh3 Th3=cube(nn,nn,nn,label=lc);
Th3 = trunc(Th3,(x<0.5) | (y < 0.5) | (z < 0.5) ,label=1);

//fespace Vh(Th3,P1);
fespace Mh(Th3,[P1,P1,P1,P1,P1,P1]);
//Vh u,v,usol,h3;
Mh [m11,m21,m22,m31,m32,m33];
//macro Grad(u) [dx(u),dy(u),dz(u)] // EOM

//problem Poisson(u,v,solver=CG) = int3d(Th3)( Grad(u)'*Grad(v) ) // ') for emacs
// -int3d(Th3)( 1*v ) + on(1,u=0);

real lerr=0.05;
verbosity=4;

//for(int ii=0; ii<4; ii++) // BUG trap in interation 3
//{
  ///Poisson;
  //plot(u,wait=1);
  //h3=0;
  [m11,m21,m22,m31,m32,m33]=[0,0,0,0,0,0];
  //cout <<" u min, max = " << u[].min << " "<< u[].max << endl;
  //real cc=(u[].max-u[].min);// rescale coefficiant

  real[int] met=mshmet(Th3,hmin=1e-3,hmax=0.2,err=lerr,aniso=1);
  m11[]=met;
// savemesh(Th3,"oo/Th3.mesh");
// savesol("oo/Th3.sol",Th3, [m11,m21,m22,m31,m32,m33]);
// exec("mmg3d_O3 oo/Th3.mesh -sol oo/Th3.sol -hgrad 2.3 -bucket 700 -v 3");
  Th3=mmg3d(Th3,metric=m11[],hgrad=2.3);//("oo/Th3.o.mesh");
  plot(Th3);

  lerr *= 0.6;// change the level of error
  cout << " Th3" << Th3.nv < " " << Th3.nt << endl;
  // u=u;
  //if(ii>3) medit("U-adap-iso-"+ii,Th3,u,wait=1);

cout <<"end Laplace Adapt aniso 3d. edp " <<endl;

}

```

> [@prj](#):
>
> requiredTriangle

---

<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: [November 6, 2023, 8:04pm UTC](https://community.freefem.org/t/3d-mesh-periodic-boundary-conditions-and-unstructured-mesh/2781/4 "2023-11-06T20:04:11Z")

</div>

You would be using Mmg instead of TetGen indeed. If you do not want to use a metric, you can adapt the mesh with respect to some other quantities, such as a level-set function. There are some more examples in the distribution.

---

<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: [November 6, 2023, 10:07pm UTC](https://community.freefem.org/t/3d-mesh-periodic-boundary-conditions-and-unstructured-mesh/2781/5 "2023-11-06T22:07:20Z")

</div>

I think there are options to freeze the boundary meshes within tetgen… from some old codes, I think the option for tetgen `switch="pqaAAYYQ"`does the job… in any case, this should be available in the documentation  
hope this helps.

---

<div class="post-metadata">

### Author: ![jsmacd](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jsmacd](https://community.freefem.org/u/jsmacd)
#### Post date: [November 6, 2023, 10:11pm UTC](https://community.freefem.org/t/3d-mesh-periodic-boundary-conditions-and-unstructured-mesh/2781/6 "2023-11-06T22:11:13Z")

</div>

Thank you Julien! I was looking around in the documentation, this will help refine my search 🙂

---

<div class="post-metadata">

### Author: ![jsmacd](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jsmacd](https://community.freefem.org/u/jsmacd)
#### Post date: [March 19, 2024, 9:56pm UTC](https://community.freefem.org/t/3d-mesh-periodic-boundary-conditions-and-unstructured-mesh/2781/7 "2024-03-19T21:56:52Z")

</div>

Just for anyone who comes across this, adding the YY to my switch seemed to work, as it seems it is supposed to fix the boundaries to be the same.
