| 1 | --- |
| 2 | name: Periodic-3d |
| 3 | category: math |
| 4 | layout: 3d |
| 5 | --- |
| 6 | |
| 7 | ## Construct a periodic 3d mesh of a cube minus a cylinder |
| 8 | |
| 9 | We begin with a 2d mesh of a square with a hole |
| 10 | ~~~freefem |
| 11 | load "medit" |
| 12 | |
| 13 | searchMethod=1; |
| 14 | verbosity=1; |
| 15 | real a=1, d=0.5, h=0.5; |
| 16 | border b1(t=0.5,-0.5) {x=a*t; y=-a/2; label=1;}; |
| 17 | border b2(t=0.5,-0.5) {x=a/2; y=a*t; label=2;}; |
| 18 | border b3(t=0.5,-0.5) {x=a*t; y=a/2; label=3;}; |
| 19 | border b4(t=0.5,-0.5) {x=-a/2; y=a*t; label=4;}; |
| 20 | border i1(t=0,2*pi) {x=d/2*cos(t); y=-d/2*sin(t); label=7;}; |
| 21 | int nnb=10, nni=20; |
| 22 | mesh Th=buildmesh(b1(-nnb)+b3(nnb)+b2(-nnb)+b4(nnb)+i1(nni));//, fixedborder=true); |
| 23 | //Th=adaptmesh(Th,0.1,IsMetric=1,periodic=[[1,x],[3,x],[2,y],[4,y]]); |
| 24 | |
| 25 | ~~~ |
| 26 | Now we renumber the vertices |
| 27 | ~~~freefem |
| 28 | int nz=10; |
| 29 | { // Between braces to declare all var local, hence save on memory. |
| 30 | int[int] old2new(0:Th.nv-1); |
| 31 | fespace Vh2(Th,P1); |
| 32 | Vh2 sorder=x+y; |
| 33 | sort(sorder[],old2new); |
| 34 | int[int] new2old=old2new^-1; // inverse the permuation |
| 35 | //for(int i=0;i< Th.nv;++i) // so by hand. |
| 36 | // new2old[old2new[i]]=i; |
| 37 | Th= change(Th,renumv=new2old); |
| 38 | sorder[]=0:Th.nv-1; |
| 39 | } |
| 40 | ~~~ |
| 41 | Now the 3d mesh is built using $\texttt{buildlayers}$. |
| 42 | ~~~freefem |
| 43 | { |
| 44 | fespace Vh2(Th,P1); |
| 45 | Vh2 nu; |
| 46 | nu[]=0:Th.nv-1; |
| 47 | plot(nu,cmm="nu=",wait=1); |
| 48 | } |
| 49 | int[int] rup=[0,5], rlow=[0,6], rmid=[1,1,2,2,3,3,4,4,7,7], rtet=[0,41]; |
| 50 | func zmin=0; |
| 51 | func zmax=h; |
| 52 | mesh3 Th3=buildlayers(Th, nz, zbound=[zmin,zmax], |
| 53 | reftet=rtet,reffacemid=rmid, reffaceup=rup, reffacelow=rlow); |
| 54 | for(int i=1;i<=6;++i) |
| 55 | cout << " int " << i << " : " << int2d(Th3,i)(1.) << " " << int2d(Th3,i)(1./area) << endl; |
| 56 | //savemesh(Th3,"Th3.mesh"); |
| 57 | plot(Th3,wait=1); |
| 58 | //medit("Th3",Th3); |
| 59 | ~~~ |
| 60 | |
| 61 | |The 3d mesh | |
| 62 | |------------| |
| 63 | |![][_mesh] | |
| 64 | |
| 65 | The finite element space with periodic mesh can be define and a PDE can be solved |
| 66 | ~~~freefem |
| 67 | fespace Vh(Th3,P2, periodic=[[1,x,z],[3,x,z],[2,y,z],[4,y,z],[5,x,y],[6,x,y]]); |
| 68 | macro grad3(u) [dx(u),dy(u),dz(u)]// |
| 69 | func u3e =(sin(2*pi*x+1)*sin(2*pi*y+2)*sin(4*pi*z+3)); |
| 70 | Vh u3,v3,u3h=u3e; |
| 71 | real ccc= pi^2*(4+4+16); |
| 72 | func f3= ccc*u3e; |
| 73 | solve P3(u3,v3) = int3d(Th3)(grad3(u3)'*grad3(v3)//' |
| 74 | +1e-6*u3*v3) - int3d(Th3)(f3*v3) |
| 75 | + on(7,u3=u3e); |
| 76 | //plot(u3,u3h,cmm=1); |
| 77 | plot(u3,cmm=1); |
| 78 | u3[]-=u3h[]; |
| 79 | cout << " err3 = " << u3[].linfty << endl; |
| 80 | assert(u3[].linfty < 0.07); |
| 81 | ~~~ |
| 82 | |
| 83 | |The solution | |
| 84 | |------------------------| |
| 85 | |![][_solution] | |
| 86 | |
| 87 | [_mesh]: https://raw.githubusercontent.com/FreeFem/FreeFem-markdown-figures/main/examples/3d/periodic-3d/mesh.png |
| 88 | |
| 89 | [_solution]: https://raw.githubusercontent.com/FreeFem/FreeFem-markdown-figures/main/examples/3d/periodic-3d/solution.png |