Jornadas CRC (Ciudad Real-Cartagena)
Universidad Politecnica de Cartagena
Simulacion Numerica conFreeFem++
Cartagena, 30-31 de Enero de 2013
Eliseo Chacon VeraDepartamento de Matematicas,
Facultad de Matematicas, Universidad de Murcia,[email protected]
31 de enero de 2013
Plan de la presentacion:
Introduccion a FreeFem++
Generacion de triangulaciones 2D/3D
Problemas variacionales
Ecuaciones de Navier-Stokes
FreeFem++ 3D
FreeFem++ 1D (ver wiki)
1 FreeFem++ Cartagena CRC 2013
2 FreeFem++ Cartagena CRC 2013
3 FreeFem++ Cartagena CRC 2013
http://www.freefem.org/ff++/index.htm
4 FreeFem++ Cartagena CRC 2013
http://www.um.es/freefem/ff++/pmwiki.php
5 FreeFem++ Cartagena CRC 2013
Escrito en C/C++
EDPs en forma variacional
Elementos finitos 2d/3d
triangulaciones 2d/3d
Resolutores lineales matrices llenas y huecas
Enlaza resolutores lineales paralelos para matri-
ces huecas sparse, MUMPS, pARMS, HIPS, etc...
Enlaza con ordenes basicas MPI
Software libre
Puede tratar problemas complejos
Bueno para investigacion y docencia
6 FreeFem++ Cartagena CRC 2013
Un poco de historia:
1985: MacFEM-PCFEM
1995: freefem+ ( Hecht y otros)
2000: freefem++ ( Hecht )
2000: freefem3D ( Del Pino and others)
2009: freefem++3D ( F. Hecht)
http://www.freefem.org/ff++/index.htm
Ideas principales:
Sigue matematica −→ formulacion variacional
uso de Elementos Finitos
Algoritmos responsabilidad del usuario
Bloques: Problemas elıpticos
Generadores de triangulaciones con adaptacion
de malla
¡Si es MEF se puede hacer con Freefem++!
7 FreeFem++ Cartagena CRC 2013
Archivo curvas.edp:
real b=1,a=1;
func real phix(real t)
return (a+b)*cos(t)-b*cos(t*(a+b)/b);
func real phiy(real t)
return (a+b)*sin(t)-b*sin(t*(a+b)/b);
border C(t=0,2*pi) x=phix(t); y=phiy(t);
border OX(t=-0.5,5)x=t;y=0;
border OY(t=-0.5,5)x=0;y=t;
border G(t=-2,2)x=t;y=t^2;
string text="Some parametric curves";
plot(cmm=text,OX(1),OY(1),C(30),G(10),
wait=1,ps="curves.eps");Some parametric curves
8 FreeFem++ Cartagena CRC 2013
1 TRIANGULACIONES 2D
1. Triangulaciones 2D
Archivo rectangle.edp:
border a(t=0,2)x=t;y=0;label=1;;
border b(t=0,1)x=2;y=t;label=1;;
border c(t=0,2)x=2-t;y=1;label=2;;
border d(t=0,1)x=0;y=1-t;label=1;;
int n=5;
mesh th=buildmesh(a(2*n)+b(n)+c(2*n)+d(n));
plot(th,wait=1,ps="rectangulo.eps");
Orientacion es la clave: Interior a la izquierda de la curva
Palabras clave para geometrıa 2D :
border define trozo de contorno
label asigna una a cada trozo de contorno
mesh tipo de variable
buildmesh genera la triangulacion
9 FreeFem++ Cartagena CRC 2013
1 TRIANGULACIONES 2D
Archivo hole.edp
real pi=4*atan(1.0);
border a(t=0,2*pi)x=2*cos(t);y=sin(t);
border b(t=0,2*pi)x=.3+.3*cos(t);y=.3*sin(t);
border c(t=0,2*pi)x=-.5+.3*cos(t);y=0.3+.3*sin(t);
mesh nohole=buildmesh(a(50)+b(30)+c(20));
mesh hole=buildmesh(a(50)+b(-30)+c(-20));
plot(nohole,wait=1,ps="nohole.eps");
plot(hole,wait=1,ps="hole.eps");
La orientacion de la curva es la clave
10 FreeFem++ Cartagena CRC 2013
1 TRIANGULACIONES 2D
Archivo triangle2d.edp
border a(t=0,1)x=t;y=0;label=1;
border b(t=0,1)x=1-t;y=t;label=2;
border c(t=0,1)x=0;y=1-t;label=3;
int nn=2;
mesh th=buildmesh(a(nn)+b(nn)+c(nn));
savemesh(th,"triangle2d.msh");
th=readmesh("triangle2d.msh");
plot(th,wait=1,ps="triangle2d.eps");
11 FreeFem++ Cartagena CRC 2013
1 TRIANGULACIONES 2D
Archivo triangle2d.mesh
10 9 9
0 0 3
0.323801507627 0 1
0.647603015254 0 1
1 0 2
0 0.323801507627 3
0.30928980099 0.324146353802 0
0.675445396471 0.324554603529 2
0 0.647603015254 3
0.324554603529 0.675445396471 2
0 1 3
10 8 9 0
8 5 6 0
6 9 8 0
1 6 5 0
7 9 6 0
1 2 6 0
6 2 3 0
4 7 3 0
6 3 7 0
10 8 3
8 5 3
5 1 3
4 7 2
7 9 2
9 10 2
1 2 1
2 3 1
3 4 1
nv nt neb nv= # vertices, nt= # triangles, nt= # edges on boundaryx y label...
......
n1 n2 n3 r n1, n2,n3 = numbering of vertices each triangle, r= # region...
......
ne1 ne2 label ne1, ne2 = # numbering of vertices each bundary edge...
......
12 FreeFem++ Cartagena CRC 2013
1 TRIANGULACIONES 2D
LANZAR tworectangletests.edp
int n = 10,m=1,lower = 1,upper = 2, inner = 3;
real cOX=-0.5,cOY=-1.25;
border OX(t=-.1,1)x = cOX+t; y =cOY;
border OY(t=-.1,1)x = cOX; y =cOY+t;
border C01(t=0,1)x = 0; y = -t;label = lower;
border C02(t=0,1)x = 1.5*t; y=-1;label = lower;
...
border C13(t=0,1)x = 1;y = -0.5+0.5*t; label = inner;
plot(C01(n),OX(m),OY(m),wait=true);
plot(C01(n),C02(n),OX(m),OY(m),wait=true);
...
mesh Th = buildmesh(C01(n)+C02(n)+C03(n)+
C04(n)+C05(n)+C06(n)+ C11(-n)+C12(-n)+C13(-n));
plot(Th, OX(m),OY(m),cmm="Triangulation + Axis ");
Triangulation + Axis
13 FreeFem++ Cartagena CRC 2013
2 TRIANGULACIONES 3D
2. Triangulaciones 3d
Uso de capas. Visualizacion con medit y plotArchivo triangle3d.edp:
load "msh3"
load "medit"
border a(t=0,1)x=t;y=0;
border b(t=0,1)x=1-t;y=t;
border c(t=0,1)x=0;y=1-t;
int nn=1;
mesh Th2=buildmesh(a(nn)+b(nn)+c(nn));
int nlayers=1;
real zmin=0,zmax=1;
mesh3 Th=buildlayers(Th2,nlayers,zbound=[zmin,zmax]);
savemesh(Th,"Th3d.mesh");
medit(" TriangleOneLayer", Th);
plot(Th);
14 FreeFem++ Cartagena CRC 2013
2 TRIANGULACIONES 3D
Th3d.mesh tiene toda la informacion
MeshVersionFormatted 1
Dimension 3
Vertices
6
0 1 0 3
0 1 1 3
0 0 0 3
0 0 1 3
1 0 0 2
1 0 1 2
Tetrahedra
3
1 6 3 5 0
1 4 3 6 0
1 6 2 4 0
Triangles
8
2 4 6 0
5 3 1 1
1 3 4 2
4 2 1 2
5 1 6 3
2 6 1 3
3 5 6 4
6 4 3 4
End
15 FreeFem++ Cartagena CRC 2013
2 TRIANGULACIONES 3D
Los tres tetrahedros triangle3D.edp
Numeros en las caras triangle3D.edp
16 FreeFem++ Cartagena CRC 2013
2 TRIANGULACIONES 3D
Dos capas con triangle3D.edp
17 FreeFem++ Cartagena CRC 2013
2 TRIANGULACIONES 3D
Archivo cube.edp:
load "msh3"
load "medit"
int nn=1,nlayers=1;
mesh Th2=square(nn,nn);
real zmin=0,zmax=2;
mesh3 Th=buildlayers(Th2,nlayers,zbound=[zmin,zmax]);
medit("Cube", Th);
plot(Th);
cout<<"------------------------------------ "<<endl;
cout<<"Number tetrahedra "<< Th.nt <<endl;
cout<<"------------------------------------ "<<endl;
Triangulacion de un cubo cube.edp
8 vertices, 12 triangles, 6 tetrahedra
18 FreeFem++ Cartagena CRC 2013
2 TRIANGULACIONES 3D
Dos capas cube.edp
Siete capas cube.edp
19 FreeFem++ Cartagena CRC 2013
2 TRIANGULACIONES 3D
zmin y zmax pueden ser funcionesLANZAR container.edp
load "msh3"
load "medit"
real L=6;
border aa(t=0,1)x=t; y=0 ;;
border bb(t=0,14)x=1+t; y= - 0.1*t ;;
border cc(t=-1.4,L)x=15; y=t ;;
border dd(t=15,0)x= t ; y = L;;
border ee(t=L,0.5) x=0; y=t ;;
border ff(t=0.5,0) x=0; y=t ;;
border C1(t=0,2*pi) x=3+0.5*cos(t); y=3+0.5*sin(t);;
int n=6, MaxLayer=10;
mesh Th2d=buildmesh(aa(n)+bb(9*n)+cc(4*n)+dd(10*n)
+ee(6*n) + ff(n)+C1(-6*n));
func zmin=-1.5*(x<3)+(+1.5-x)*(x>3)*(x<7)-5.5*(x>7);
func zmax=1;
mesh3 Th3=buildlayers(Th, MaxLayer,zbound=[zmin,zmax]);
medit("container",Th3);
20 FreeFem++ Cartagena CRC 2013
2 TRIANGULACIONES 3D
Profundidad variable y orilla:LANZAR shore.edp
load "msh3"
load "medit"
real eps=0.01;// shore thickness
int nn=10;
border cc(t=0,2*pi)x=cos(t);y=sin(t);;
mesh Th2= buildmesh(cc(100));
func zmin= 2-sqrt(4-(x*x+y*y));// bottom
func zmax= eps+ 2-sqrt(3.);//surface
mesh3 Th=buildlayers(Th2,nn,
coef= max((zmax-zmin)/zmax,1./(nn*2)),
zbound=[zmin,zmax]);
medit("myshore",Th);
21 FreeFem++ Cartagena CRC 2013
2 TRIANGULACIONES 3D
Vistas diferentes con grosores distintos shore.edp
22 FreeFem++ Cartagena CRC 2013
2 TRIANGULACIONES 3D
Triangulacion por capas 3D desde una triangulacion 2D:
Ω3D = Ω2D × [zmin, zmax]
funciones zmin < zmax : Ω2D 7→ R.
Por defecto la superficie del fondo se etiqueta con 0y las laterales se heredan de las etiquetas 2d.
Se pueden cambiar las etiquetas de las superficies
labelup=[0,2] asigna 2 superficie superior.
labeldown=[0,1] asigna 1 superficie inferior.
labelmid=[1,1,2,1,3,1,4,1,5,1] asigna 1 a todaslas caras sobre lados 1, 2, 3,4,5.
23 FreeFem++ Cartagena CRC 2013
3 FORMULACION VARIACIONAL
3. Formulacion variacional
problem P(u,v) = a(u,v) - l(f,v)
+ (boundary condition);
Tres formas problem, solve y varf
problem test()...; define problema variacional. Plan-tea y resuelve con la orden test;
solve test()...; define y resuelve.
varf construye matriz y termino independiente.
Integracion numerica: th triangulacion de Ω, Γ1 ⊂ ∂Ω
int1d(th)(v) =∫∂Ω v(x) dx, int1d(th, 1)(v) =
∫Γ1v(x) dx
int2d(th)(v) =∫Ω v(x) dx
Similar con int2d(th)(v) e int3d(th)(v)
intalledges(th)(v) =∑
τ∈th∫∂τ v
Resolutores lineales: Basicamente tres.definidos via solver=...
CG Conjugate Gradient
UMFPACK
GMRES
Conectado con sparsesolvers en paralelo o directos viampi: hips, mumps, SuperLU, etc...
24 FreeFem++ Cartagena CRC 2013
3 FORMULACION VARIACIONAL
LANZAR Poisson-ellipse.edp
border a(t=0,2*pi)x=2*cos(t);y=sin(t);;
mesh Th= buildmesh (a(150));
plot(Th);
fespace Vh(Th,P2);
Vh u, v;
func f=sin(x*y);
problem laplace(u,v) =
int2d(Th)(dx(u)*dx(v)+dy(u)*dy(v)) //bilinear part
-int2d(Th)(f*v) // right hand side
+on(a,u=0); // Dirichlet BC
laplace; //solve the pde
plot(u); //visualizacion
25 FreeFem++ Cartagena CRC 2013
3 FORMULACION VARIACIONAL
int2d(th)(...) y similares, se usan
con producto de incognitas y funciones tests
solo con funciones test y datos
NO CON AMBAS A LA VEZ
No correcto
int2d(th)(dx(u)*dx(v)+dy(u)*dy(v)-f*v)
+on(1,u=0)
correcto
int2d(th)(dx(u)*dx(v)+dy(u)*dy(v)) // bilinear part
+int2d(th)(-f*v) // right hand side
+on(1,u=0)
26 FreeFem++ Cartagena CRC 2013
4 NAVIER-STOKES
4. Navier-Stokes
Stokes test cavidad en [0, 1]× [0, 1] con el par P2-P1.u ∈ H1(Ω)2 y p ∈ L2(Ω) tal que
−∆u+∇p = f en Ωdiv(u) = 0 en Ω+C.C.
o bien,
(∇u,∇v)Ω − (p, div(u))Ω = (f, v) en Ω−(q, div(u))Ω = 0 en Ω
+C.C.
LANZAR Stokes2dCavity.edp:
mesh Th=square(10,10);
fespace Xh(Th,P2);
fespace Mh(Th,P1);
Xh u1,v1,u2,v2;
Mh p,q;
solve Stokes (u1,u2,p,v1,v2,q) =
int2d(Th)( dx(u1)*dx(v1)+dy(u1)*dy(v1)
+dx(u2)*dx(v2)+dy(u2)*dy(v2)
-p*dx(v1)-p*dy(v2)
-q*dx(u1)-q*dy(u2)
)
+on(1,2,4,u1=0,u2=0)
+on(3,u1=1,u2=0); // C.C. en borde superior
plot(cmm=" Velocidad [u1,u2] ",[u1,u2]);
plot(cmm=" Presion ",p);
plot(cmm=" Velocidad y Presion ",[u1,u2],p);
27 FreeFem++ Cartagena CRC 2013
4 NAVIER-STOKES
Vec Value00.05263430.1052690.1579030.2105370.2631720.3158060.368440.4210750.4737090.5263430.5789780.6316120.6842470.7368810.7895150.842150.8947840.9474181.00005
Velocidad [u1,u2]
Velocidad Stokes2dCavity.edp
28 FreeFem++ Cartagena CRC 2013
4 NAVIER-STOKES
IsoValue-49.7096-44.6966-39.6837-34.6708-29.6578-24.6449-19.632-14.619-9.6061-4.593170.4197625.4326910.445615.458620.471525.484430.497435.510340.523245.5362
Presion
Presion Stokes2dCavity.edp
29 FreeFem++ Cartagena CRC 2013
4 NAVIER-STOKES
IsoValue-49.71-44.697-39.6841-34.6711-29.6581-24.6452-19.6322-14.6192-9.60627-4.59330.419675.4326410.445615.458620.471525.484530.497535.510440.523445.5364
Vec Value00.05263430.1052690.1579030.2105370.2631720.3158060.368440.4210750.4737090.5263430.5789780.6316120.6842470.7368810.7895150.842150.8947840.9474181.00005
Velocidad y presion Stokes2dCavity.edp
30 FreeFem++ Cartagena CRC 2013
4 NAVIER-STOKES
Navier-Stokes evolutivo:
u(t) ∈ H1(Ω)2 y p(t) ∈ L2(Ω) tal que
∂tu+ (u · ∇)u−∆u+∇p = f en Ωdiv(u) = 0 en Ω+C.C.
o bien,
( DDtu, v) + (∇u,∇v)Ω − (p, div(u))Ω = (f, v) en Ω
−(q, div(u))Ω = 0 en Ω+C.C.
Nonlinealidad tratada con convect que resuelve:
∂tϕ+ u∇ϕ = 0,
ϕ(0, x) = ϕ0(x).
Lanzar NS2d.edp
Lanzar obstaculo.edp
Acoplamiento Navier-Stokes - Temperatura:Lanzar NS2dT.edp
31 FreeFem++ Cartagena CRC 2013
5 FREEFEM++ 3D
5. FreeFem++ 3D
LANZAR Laplaciano3d.edp:
load "msh3"
load "medit"
int nn=8;
mesh Th2=square(nn,nn);// 2D mesh
real zmin=0,zmax=2;
int[int] rup=[0,200],rdown=[0,100];
int[int] rmid=[1,1,2,1,3,1,4,1];
mesh3 Th=buildlayers(Th2,nn, zbound=[zmin,zmax],
labelup=rup, labeldown=rdown, labelmid=rmid);
func f=1. ;
fespace Vh(Th,P13d);
Vh u,v;
macro Grad3(u) [dx(u),dy(u),dz(u)] // EOM
problem Lap3d(u,v,solver=CG) =
int3d(Th)(Grad3(v)’ *Grad3(u))
- int3d(Th)(f*v)+ on(1,u=0);
Lap3d;
medit(" Laplacian3D ", Th, u );
32 FreeFem++ Cartagena CRC 2013
5 FREEFEM++ 3D
−∆u = 1 con u = 0 en laterales.
33 FreeFem++ Cartagena CRC 2013
5 FREEFEM++ 3D
LANZAR shoreLaplace3d.edp:
load "msh3"
load "medit"
real eps=.015;// shore thickness
int nn=20;
border cc(t=0,2*pi)x=cos(t);y=sin(t);label=1;;
mesh Th2= buildmesh(cc(100));
func zmin= 2-sqrt(4-(x*x+y*y));// bottom
func zmax= eps+ 2-sqrt(3.);//surface
int[int] rup=[0,200],rdown=[0,100];
int[int] rmid=[1,1];
mesh3 Th=buildlayers(Th2,nn,
coef= max((zmax-zmin)/zmax,1./(nn*2)),
zbound=[zmin,zmax],
labelup=rup,
labeldown=rdown,
labelmid=rmid);
func f=1. ;
fespace Vh(Th,P13d);
Vh u,v;
//
macro Grad3(u) [dx(u),dy(u),dz(u)] // EOM
//
problem Lap3d(u,v,solver=CG) =
int3d(Th)(Grad3(u)’ *Grad3(v))
- int3d(Th)(f*v)
+ on(1,100, u=0);
Lap3d;
medit("myshore",Th,u);
34 FreeFem++ Cartagena CRC 2013
5 FREEFEM++ 3D
Cavidad 3D:Archivo Stokes3dcavity.edp:
load "msh3";
load "medit";
int nn=15;
mesh Th2=square(nn,nn);// 2D mesh
fespace Vh2(Th2,P2);// cross-section space
Vh2 ux,uz,p2;
real zmin=0,zmax=1,yy;
int[int] rup=[0,200],rdown=[0,100];
int[int] rmid=[1,1,2,1,3,1,4,1];
mesh3 Th=buildlayers(Th2,nn, zbound=[zmin,zmax],
labelup=rup, labeldown=rdown, labelmid=rmid);// 3D mesh
fespace VVh(Th,[P23d,P23d,P23d,P13d]);
VVh [u1,u2,u3,p];
VVh [v1,v2,v3,q];
macro Grad(u) [dx(u),dy(u),dz(u)]// EOM
macro div(u1,u2,u3) (dx(u1)+dy(u2)+dz(u3)) //EOM
varf vStokes([u1,u2,u3,p],[v1,v2,v3,q]) =
int3d(Th)(Grad(u1)’*Grad(v1) +Grad(u2)’*Grad(v2)
+Grad(u3)’*Grad(v3)
- div(u1,u2,u3)*q - div(v1,v2,v3)*p
)
+on(1,100,u1=0,u2=0,u3=0)+ on(200,u1=1.,u2=0,u3=0) ;
matrix A=vStokes(VVh,VVh);
set(A,solver=GMRES);
real[int] b= vStokes(0,VVh);
p[]= A^-1 * b;
for(int i=1;i<10;i++)
35 FreeFem++ Cartagena CRC 2013
5 FREEFEM++ 3D
yy=i/10.;
ux= u1(x,yy,y);
uz= u3(x,yy,y);
p2= p(x,yy,y);
plot([ux,uz],p2,cmm=" cut y = "+yy,wait= 1);
if (i==5)
plot([ux,uz],p2,cmm=" cut y = "+yy,wait= 1);;
;
medit("cavity3d",Th,p);
medit("cavity3d",Th,u3);
medit("cavity3d",Th,u2);
medit("cavity3d",Th,u1);
36 FreeFem++ Cartagena CRC 2013
5 FREEFEM++ 3D
cut y = 0.5
37 FreeFem++ Cartagena CRC 2013
5 FREEFEM++ 3D
Cavity Stokes.
38 FreeFem++ Cartagena CRC 2013
5 FREEFEM++ 3D
Puerto Cartagena:
≈ 0.9 Km. entrada, ≈ 1.7 Km. de largo,≈ 0.6 Km. Dique la Curra≈ 11 metros de fondo, ≈ 240 metros boca mas estrecha.
39 FreeFem++ Cartagena CRC 2013
5 FREEFEM++ 3D
Ecuaciones de Boussinesq bahia de Cartagena:
40 FreeFem++ Cartagena CRC 2013