software libre en ciencias e ingenier´ıas. simulaci´on num´erica … · 2012-09-08 · software...

36
Software libre en Ciencias e Ingenier´ ıas. Simulaci´onNum´ erica con FreeFem++. Adaptaci´on del manual de Fr´ ed´ eric Hecht EliseoChac´onVera Departamento de Ecuaciones Diferenciales y An´alisis Num´ erico, Facultad de Matem´aticas, Universidad de Sevilla C´adiz, 6 de Julio de 2011 1

Upload: others

Post on 23-Mar-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

Software libre en Ciencias e Ingenierıas.Simulacion Numerica con FreeFem++.

Adaptacion del manual de Frederic Hecht

Eliseo Chacon VeraDepartamento de Ecuaciones Diferenciales y Analisis Numerico,

Facultad de Matematicas, Universidad de Sevilla

Cadiz, 6 de Julio de 2011

1

Page 2: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

1 FREEFEM 2D

1. FreeFem 2d

Gramatica similar a C/C++

Ejemplo aritmetica.edp:

real x=3.14,y;

int i,j;

complex c;

cout<< " x = "<< x << "\n";

x=1;y=2;x=y;i=0;j=1;

cout<< 1+3 << " " << 1./3 << "\n";

cout<< 10^10. << "\n";

cout<< 10^-10. << "\n";

cout<< -10^-2.+5 << "==4.99 \n";

cout<< 10^-2.+5 << "==5.01 \n";

cout<< " 8^(1/3) = "<< (8)^(1./3.) <<"\n";

cout<< "-------------complex ---------- \n";

cout<< 10-10i <<"\n";

cout<< " -1^(1/3) = "<< (-1+0i)^(1./3.) <<"\n";

2 FreeSoft in Cadiz:Freefem++

Page 3: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

1 FREEFEM 2D

Ciclo for: for.edp:

int i;

for (i=0;i<10;i=i+1)

cout <<i <<"\n";

Ciclo mientras: while.edp:

int i;

i=0;

real eps=1;

while (eps+1!=1)

{

eps=eps/2;

i++;

if(eps+1==1) break;

cout <<" iter = "<< i <<"; eps = "<<eps<<

"; eps+1= "<<eps+1<<"\n";

}

3 FreeSoft in Cadiz:Freefem++

Page 4: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

2 TRIANGULACIONES 2D

2. Triangulaciones 2D

Rectangulo, ejemplo: rectangle.edp

border a(t=0,2){x=t;y=0;label=1;};border b(t=0,1){x=2;y=t;label=2;};border c(t=0,2){x=2-t;y=1;};border d(t=0,1){x=0;y=1-t;label=4;};int n=5;mesh th=buildmesh(a(2*n)+b(n)+c(2*n)+d(n));plot(th,wait=1,ps=”rectangulo.eps”);

Triangulacion no uniforme generada con rectangle.edp

Palabras clave:

border define una parte del contorno

label= asigna una etiqueta (condiciones decontorno)

mesh tipo de la variable th (como int o real)

buildmesh genera la triangulacion

4 FreeSoft in Cadiz:Freefem++

Page 5: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

2 TRIANGULACIONES 2D

Ejemplo tworectangles.edp

real x0=1.2,x1=1.8;

real y0=0.,y1=1.;

int n=5,m=20;

mesh Th=square(n,m,[x0+(x1-x0)*x,y0+(y1-y0)*y]);

mesh th=square(4,5);

plot(Th,th, cmm="dos rectangulos");

dos rectangulos

Triangulaciones generadas por tworectangles.edp

square da una triangulacion uniforme. Observar suformato general y los papeles de x0, x1,y0,y1:

x0 ! x ! x1, y0 ! y ! y1.

5 FreeSoft in Cadiz:Freefem++

Page 6: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

2 TRIANGULACIONES 2D

Ejemplo hueco.edp

real pi=4*atan(1.0);

border a(t=0,2*pi){x=2*cos(t);y=sin(t);label=1;}

border b(t=0,2*pi){x=.3+.3*cos(t);y=.3*sin(t);label=2;}

mesh thwithouthole=buildmesh(a(50)+b(30));

mesh thwithhole=buildmesh(a(50)+b(-30));

plot(thwithouthole,wait=1,cmm="th sin hueco");

plot(thwithhole,wait=1,ps="thconhueco.eps");

Dos mallas complementarias generadas con hueco.edp

Malla con hueco generada con hueco.edp

6 FreeSoft in Cadiz:Freefem++

Page 7: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

2 TRIANGULACIONES 2D

Ejemplo ele.edp

border a(t=0,1){x=t;y=0;label=1;};

border b(t=0,0.5){x=1;y=t;label=1;};

border c(t=0,0.5){x=1-t;y=0.5;label=1;};

border d(t=0.5,1){x=0.5;y=t;label=1;};

border e(t=0.5,1){x=1-t;y=1;label=1;};

border f(t=0,1){x=0;y=1-t;label=1;};

int n=5;

mesh rh=buildmesh(a(n)+b(n)+c(n)+d(n)+e(n)+f(n));

plot(rh,wait=1);

savemesh(rh,"ele.msh");

mesh th=readmesh("ele.msh");

plot(th,wait=1,ps="ele.eps");

Triangulacion de L ele.edp

Observa uso de savemesh y readmesh.

7 FreeSoft in Cadiz:Freefem++

Page 8: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

2 TRIANGULACIONES 2D

Archivo ele.msh contiene toda la informacion sobre lamalla

60 88 30

1 0.5 1

1 0.403333333499 1

.....

1 0.113333333996 1

0.933333332821 0.371111110804 0

0.9 0.5 1

.....

.....

0.132724239102 0.90544151017 0

0.210000000497 1 1

0.113333333996 1 1

58 57 51 0

52 51 45 0

.....

.....

36 37 42 0

47 46 41 0

57 51 1

51 45 1

.....

.....

16 9 1

9 3 1

—————————–60 : number of nodes88: number of triangles30: number of edges

8 FreeSoft in Cadiz:Freefem++

Page 9: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

3 PROBLEMAS VARIACIONALES

3. Problemas variacionales

problem P(u,v) = a(u,v) - l(f,v)

+ (boundary condition);

Tres formatos problem, solve y varf

problem test()...; define un problema variacional. Seplantea y resuelve con la orden test;

solve test()...; define y resuelve.

varf construye la forma variacional. Permite obtenerla matriz y termino independiente.

Resolutores lineales: Basicamente tres.Fijados con el parametro solver=...

LU Factorizacion LU.

CG Gradiente conjugado.

UMFPACK (por defecto es el usado en MATLAB).

GMRES.

Espacios de elementos finitos: Principalmente P0,P1,P2,....haymasFijados con la orden fespace

fespace Vh(th,P0);

fespace Miespacio(th,P1);

fespace PP(th,P2);

9 FreeSoft in Cadiz:Freefem++

Page 10: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

3 PROBLEMAS VARIACIONALES

Informacion sobre la triangulacion:

mesh Th=square(10*(n+1),10*(n+1));

int nbvertices=Th.nv;

fespace Mh(Th,P0);

Mh hhh = hTriangle;

cout << "talla triangulacion = "<< hhh[].max

<<" nb of vertices = " << nbvertices<<endl;

Dada Vh(Th,*) tenemos

hTriangle el lado de triangulo ms largo

Th.nv numero de vertices.

Vh.nt numero de elementos de Vh

Vh.ndof numero de grados de libertad de Vh, i.e.,dimension de Vh

Vh.ndofK numero de grados de libertad en cada eachelemento K de Th

Vh(i,k) numero del grado de libertad i en elemento k

10 FreeSoft in Cadiz:Freefem++

Page 11: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

3 PROBLEMAS VARIACIONALES

Ejemplo FE.edp:

mesh Th=square(5,5);

fespace Wh(Th,P2);

cout << " dofs : " << Wh.ndof << endl;

cout << " dofs por elemento: " << Wh.ndofK << endl;

int k= 2;

int kdf= Wh.ndofK ;

cout << " df de elemento " << k << ": " ;

for (int i=0;i<kdf;i++)

cout << Wh(k,i) << " ";

cout << endl;

genera una salida

Nb Of Nodes = 121

Nb of DF = 121 F

ESpace:Gibbs: old skyline = 5841 new skyline = 1377

dofs : 121

dofs por elemento : 6

df de elemento 2: 78 95 83 87 79 92

11 FreeSoft in Cadiz:Freefem++

Page 12: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

3 PROBLEMAS VARIACIONALES

Como ver las funciones de base

trunc trunca una triangulacion. Dos parametros

label= etiqueta contorno (1 por defecto)

split= n"n divide en n los elementos (1 por defecto).

mesh Th3 = trunc(Th,1,split=3);

divide cada triangulo de Th in 3.

Ejemplo base.edp:

mesh Th=square(3,3);

fespace Vh(Th,P1);

Vh u;

int i,n=u.n;

u=0;

for (i=0;i<n;i++) // all degree of freedom

{

u[][i]=1; // the basic function i

plot(u,wait=1,ps="base"+i+".eps");

mesh Sh1=trunc(Th,abs(u)>1.e-10,split=5,label=2);

plot(Th,Sh1,wait=1,ps="trunc"+i+".eps"); // plot the mesh of

//the functions support

u[][i]=0; // reset

}

12 FreeSoft in Cadiz:Freefem++

Page 13: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

3 PROBLEMAS VARIACIONALES

Funcion de base base.edp

Soporte de la funcion de base base.edp

13 FreeSoft in Cadiz:Freefem++

Page 14: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

3 PROBLEMAS VARIACIONALES

Ejemplo de problema variacional Poisson-ellipse.edp

real cpu=clock();

border a(t=0,2*pi){x=2*cos(t);y=sin(t);label=5;};

mesh Th= buildmesh(a(150));

plot(Th,wait=1,ps="malla-ellipse.eps");

fespace Vh(Th,P2);

Vh u, v;

func f=sin(x*y);

problem laplace(u,v,solver=CG,init=1,eps=-1e-8) =

int2d(Th)(dx(u)*dx(v)+dy(u)*dy(v)) //bilinear part

-int2d(Th)(f*v) // right hand side

+on(5,u=0); // Dirichlet boundary condition

laplace; //solve the pde

plot(u,wait=1,cmm="u-Poisson-ellipse",fill=1,value=1,dim=3);

cout<< " CPU = " << clock() -cpu << endl;

El tiempo de CPU se calcula con la orden clock()

real d=clock();\\Get current CPU clock time and saves in d

...

cout<< " CPU = " << clock() - d << endl; \\Gives CPU time taken

\\for the process

14 FreeSoft in Cadiz:Freefem++

Page 15: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

3 PROBLEMAS VARIACIONALES

Comentarios despues de //

fspace Vh(Th,P1) construye espacio de nombre Vhy usa P1 funciones sobre Th.

Vh u,v,. . . define distintos elementos de Vh.

func g=. . . define funcion mediante expresion analıti-ca.

init booleano: falso o 0 construye el sistema lineal denuevo. Con init=1 no reconstruye el sistema lineal.

eps=... fija el test de parada para resolver el sistemalineal

• eps < 0 error absoluto

#Ax$ b# ! |eps|.

• eps > 0 error relativo

#Ax$ b# ! eps · #Ax0 $ b#.

15 FreeSoft in Cadiz:Freefem++

Page 16: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

3 PROBLEMAS VARIACIONALES

Triangulacion para la elipse Poisson-elipse.edp

Solucion de Poisson-elipse.edp

Resolver el mismo problema pero con dato de Neumanncero en un trozo de arco de la elipse

16 FreeSoft in Cadiz:Freefem++

Page 17: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

3 PROBLEMAS VARIACIONALES

Ejemplo Poisson-step.edp

real v1=4,v2=6,w=v1+v2;

border a(t=0,v1){x=t;y=0;label=1;}; // y=0, x on [0,v1]

border b(t=0,1){x=v1;y=-t;label=2;};// x=v1, y on [0,-1]

border c(t=0,v2){x=v1+t;y=-1;label=3;}; // y=-1, x on [v1,w]

border d(t=0,2){x=w;y=-1+t;label=4;};// x=w, y on [-1,1]

border e(t=0,w){x=w-t;y=1;label=5;};// y=1, x on [0,w]

border ff(t=0,1){x=0;y=1-t;label=6;};// x=0, y on [1,0]

int n=10;

mesh th=buildmesh(a(2*n)+b(n)+c(2*n)+d(2*n)+e(4*n)+ff(n));

plot(th,wait=1,ps="mesh-step.eps");

fespace Vh(th,P1);

Vh u,v;

func f=0;

problem laplace(u,v) =int2d(th)(

dx(u)*dx(v)+dy(u)*dy(v)

) // bilinear part

+int2d(th)(-f*v) // right hand side

+ int1d(th,6)(u*v)

+on(1,2,3,4,u=0)

+on(5,u=1);// Dirichlet boundary condition

laplace; //solve the pde

plot(u,wait=1,cmm="Solucion ",ps="Poisson-escalon.eps",

value=1,dim=3,fill=1);

17 FreeSoft in Cadiz:Freefem++

Page 18: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

3 PROBLEMAS VARIACIONALES

Mesh Poisson-step.edp

IsoValue-0.05263160.02631580.07894740.1315790.1842110.2368420.2894740.3421050.3947370.4473680.50.5526320.6052630.6578950.7105260.7631580.8157890.8684210.9210531.05263

Solucion

Solution with Poisson-step.edp

18 FreeSoft in Cadiz:Freefem++

Page 19: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

3 PROBLEMAS VARIACIONALES

int2d(th)(...) y similar se usan

con incognita y funcion test

solo con la funcion test

no se mezclan situaciones.

No es 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

+int1d(th,2)(u*v) //Robin B.C.

+on(1,u=g) //Dirichlet B.C.

Otra salida grafica es con la libreria medit de Pascal Frey.

load "medit";

.....

medit("Laplace",th,u);

19 FreeSoft in Cadiz:Freefem++

Page 20: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

3 PROBLEMAS VARIACIONALES

Guardar/leer datos

Escritura de datos

{

ofstream u1data("u1.txt");

u1data << uu1[];

ofstream u2data("u2.txt");

u2data << uu2[];

}

lectura

{

ifstream u1data("u1.txt");

u1data >> uu1[];

ifstream u2data("u2.txt");

u2data >> uu2[];

}

Aquı uu1 y uu2 son funciones de elementos finitos y uu1[]is el vector de valores asociado.

Como en C/C++

{

....

}

separa variables locales de globales.

20 FreeSoft in Cadiz:Freefem++

Page 21: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

4 PROBLEMAS DE EVOLUCION EN TIEMPO

4. Problemas de evolucion en tiempo

Ecuacion del calor

!tu$ "!u = f, (x, t) % "" (0, T )

u(x, 0) = u0, x % "u(x, t) = g, (x, t) % #D " (0, T )

!nu(x, t) = 0, (x, t) % #N " (0, T )

siendo !" = #N &#D. Usamos elementos finitos en espacioy diferencias finitas en tiempo. Para n ' 0 usamos

1

!t

!

!un+1v + "

!

!(un+1(v =

1

!t

!

!unv

Ejemplo: Con u(x, y, t) = sin(x) cos(y)et, f = (1+2") )uel problema es:

!tu$ "!u = f (x, y, t) % "" (0, T ),

u(x, y, 0) = sin(x) cos(y) (x, y) % ",u(x, y, t) = sin(x) cos(y)et (x, y, t) % !"" (0, T ).

con " = (0, 1)" (0, 1) y T = 3.

21 FreeSoft in Cadiz:Freefem++

Page 22: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

4 PROBLEMAS DE EVOLUCION EN TIEMPO

Ejemplo calor.edp:

mesh Th=square(32,32);

fespace Vh(Th,P2);

Vh u,v,uu,f,g;

real dt=0.1, nu=0.001,error;

problem dcalor(u,v) =

int2d(Th)( u*v+dt*nu*(dx(u)*dx(v) + dy(u)*dy(v)))

+int2d(Th)(-uu*v-dt*f*v)

+ on(1,2,3,4,u=g);

real t=0;

uu=sin(x)*cos(y)*exp(t);

for (int m=0;m<=5/dt;m++)

{

t=t+dt;

f=(1+2*nu)*sin(x)*cos(y)*exp(t);

g=sin(x)*cos(y)*exp(t);

dcalor;

plot(u,wait=0,value=1);

error=sqrt(int2d(Th)((u-sin(x)*cos(y)*exp(t))^2));

uu=u;

cout<< "t= "<< t << " L2-Error = " << error << endl;

};

plot(u,wait=1,value=1,ps="dcalor.eps");

22 FreeSoft in Cadiz:Freefem++

Page 23: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

4 PROBLEMAS DE EVOLUCION EN TIEMPO

IsoValue3.4709510.412917.354824.296731.238638.180545.122452.064359.006265.948172.8979.831986.773893.7157100.658107.6114.541121.483128.425135.367

Solucion final obtenida con evolucion.edp

23 FreeSoft in Cadiz:Freefem++

Page 24: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

4 PROBLEMAS DE EVOLUCION EN TIEMPO

Ejemplo aplicado:Distribucion de temperatura en unaplaca 3D de seccion rectangular " = (0, 6)" (0, 1). La pla-ca recibe y mantiene una fuente de calor u = u0 desde susextremos laterales y esta rodeada de aire a temperaturaambiente uc. La temperatura cambia poco con la coorde-nada z y por lo tanto podemos considerar el problema como2D.Si ponemos #1 = {y = 0, y = 1} y #2 = {x = 0, y = 6},entonces se plantea como resolver la ecuacion del calor

!tu$( · (#(u) = 0, (x, t) % "" (0, T )

u(x, y, 0) = u0, x % "

con condiciones de contorno

#!nu+ $(u$ uc) = 0, (x, t) % #1 " (0, T )

u = u0 (x, t) % #2 " (0, T )

y en donde el coeficiente de difusion # toma dos valores,# = 0.2 sobre y = 0.5 y # = 2 bajo y = 0.5.Aplicamos el metodo de Euler implıcito y tendremos

((un $ un$1)/dt, v)! + (k(un,(v)! + ($(un $ uc), v)"1 = 0

24 FreeSoft in Cadiz:Freefem++

Page 25: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

4 PROBLEMAS DE EVOLUCION EN TIEMPO

Ejemplo termico.edp:

func u0 =10+90*x/6;//temp. inicial

func k = 1.8*(y<0.5)+0.2;

real ue = 25;//temp. exterior

real alpha=0.25, T=10, dt=0.1;

mesh Th=square(30,5,[6*x,y]);//[0,6]x[0,1]

plot(Th,wait=0);

fespace Vh(Th,P2);

Vh u=u0,v,uold;

problem thermic(u,v)=

int2d(Th)(u*v/dt+k*(dx(u)*dx(v) +dy(u) * dy(v)))

+ int1d(Th,1,3)(alpha*u*v)

- int1d(Th,1,3)(alpha*ue*v)

- int2d(Th)(uold*v/dt)

+ on(2,4,u=u0) ;

for(real t=0;t<T;t+=dt){

uold=u;

thermic;

plot(u,wait=0);

};

plot(u,wait=1,value=1,ps="termico.eps");

25 FreeSoft in Cadiz:Freefem++

Page 26: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

4 PROBLEMAS DE EVOLUCION EN TIEMPO

Solucion final obtenida con termico.edp

26 FreeSoft in Cadiz:Freefem++

Page 27: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

4 PROBLEMAS DE EVOLUCION EN TIEMPO

Otro problema es la ecuacion del calor con una turbina.Archivo turbina.edp:

border a(t=pi/2,2*pi){x=1.5*cos(t);y=1.5*sin(t);

label=1;};//exterior

border b(t=1.5,4){x=t;y=0;label=1;};//exterior

border c(t=0,1.5){x=4;y=t;label=2;};//salida turbina

border d(t=4,0){x=t;y=1.5;label=1;};//exterior

border e(t=2*pi,0){x=0.25*cos(t);y=0.25*sin(t);

label=3;};//Interior de turbina

mesh Th= buildmesh (a(20)+b(10)+c(6)+d(10)+e(20));

plot(Th,wait=1,ps="malla-turbina.eps");

fespace Vh(Th,P1);

Vh u, v,uold,f,g,s,ue=1;

func u0 =0, k=1;

int N=100;

real T=5*pi, dt=T/N;

uold=u0;

problem turbina(u,v)= int2d(Th)(

u*v/dt +k*(dx(u)*dx(v)+dy(u)*dy(v))

)

- int2d(Th)(uold*v/dt)

+on(1,u=ue)+ on(3,u=g);

for(real tt=0;tt<T;tt+=dt)

{

g=abs(sin(tt+pi/2));

turbina;

uold=u;

plot(u,wait=0,value=0);

};

plot(u,wait=1,fill=0,value=1,ps="u-turbina.eps");

27 FreeSoft in Cadiz:Freefem++

Page 28: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

4 PROBLEMAS DE EVOLUCION EN TIEMPO

Red inicial obtenida con turbina.edpIsoValue0.9600240.9620740.9641240.9661740.9682240.9702750.9723250.9743750.9764250.9784750.9805250.9825750.9846250.9866750.9887250.9907750.9928250.9948750.9969250.998975

Solucion obtenida con turbina.edp

28 FreeSoft in Cadiz:Freefem++

Page 29: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

5 FLUIDOS INCOMPRESIBLES: ECUACIONES DENAVIER-STOKES

5. Fluidos incompresibles: Ecuaciones de Navier-Stokes

Buscamos un campo de velicidades u y una presion pcon

$!u+(p = 0, "

div(u) = 0, "

+ C.C., !"

La formulacion variacional es!

!(u ·(v $

!

!p div(v) = 0

!

!div(u) q = 0

+C.C.

Las lıneas de corriente son las trayectorias de las particulastangentes al vector velocidad: curvas de nivel de $ tal que

rot($) = u, o mejor $!$ = rot(u) = !yu1 $ !xu2.

Problema de la cavidad para Stokes en [0, 1] " [0, 1] con(P2,P1).

29 FreeSoft in Cadiz:Freefem++

Page 30: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

5 FLUIDOS INCOMPRESIBLES: ECUACIONES DENAVIER-STOKES

Ejemplo Stokes2dCavity.edp:

mesh Th=square(10,10);

fespace Xh(Th,P2);

fespace Mh(Th,P1);

Xh u1,v1,u2,v2;

Mh p,q;

real eps=1e-10;

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*q*eps

-p*dx(v1)-p*dy(v2)-dx(u1)*q-dy(u2)*q

)

+on(1,2,4,u1=0,u2=0)+on(3,u1=1,u2=0) ;

plot(coef=.5,cmm=" Velocidad [u1,u2] ",value=true,[u1,u2],

wait=1,ps="Stokes-vel.eps");

plot(cmm=" Presion ",p,value=true,wait=1,ps="Stokes-pres.eps");

plot([u1,u2],p,wait=1,value=true,coef=0.5,

ps="Stokes-velypres.eps");

//

// obtencion de las lineas de corriente

//

Xh psi,phi;

solve streamlines(psi,phi) =

int2d(Th)(dx(psi)*dx(phi) + dy(psi)*dy(phi))

+int2d(Th)( -phi*(dy(u1)-dx(u2)))

+on(1,2,3,4,psi=0);

plot(cmm=" Lineas de corriente ",psi,wait=1,

value=true, ps="Stokes-stream.eps");

30 FreeSoft in Cadiz:Freefem++

Page 31: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

5 FLUIDOS INCOMPRESIBLES: ECUACIONES DENAVIER-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

IsoValue-76.4339-71.4209-66.4079-61.395-56.382-51.369-46.3561-41.3431-36.3301-31.3171-26.3042-21.2912-16.2782-11.2653-6.25231-1.239343.773638.786613.799618.8125

Presion

Presion Stokes2dCavity.edp

31 FreeSoft in Cadiz:Freefem++

Page 32: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

5 FLUIDOS INCOMPRESIBLES: ECUACIONES DENAVIER-STOKES

IsoValue0.002427880.007295460.0121630.01703060.02189820.02676580.03163330.03650090.04136850.04623610.05110360.05597120.06083880.06570640.07057390.07544150.08030910.08517670.09004420.0949118

Lineas de corriente

Lineas de corriente obtenidas con Stokes2dCavity.edp

32 FreeSoft in Cadiz:Freefem++

Page 33: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

5 FLUIDOS INCOMPRESIBLES: ECUACIONES DENAVIER-STOKES

Consideramos ahora Navier-Stokes:

!tu+ (u()u$!u+(p = 0, "

div(u) = 0, "

+ C.C., !"

La formulacion variacional es!

!

D

Dtuv +

!

!(u ·(v $

!

!p div(v) = 0

!

!div(u) q = 0

+C.C.

siendo DDtu la derivada material

D

Dtu = !tu+ (u()u.

La nolinearidad se calcula con la orden convect que resuel-ve:

!t%+ u(% = 0, %(x, t = 0) = %0(x).

Se escribe

D

Dtun+1 * 1

!tun+1 $ 1

!tun(Xn)

donde un(Xn) * un(x$ un(x)!t) se calcula como

convect([u1, u2],$!t, u1), convect([u1, u2],$!t, u2)

33 FreeSoft in Cadiz:Freefem++

Page 34: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

5 FLUIDOS INCOMPRESIBLES: ECUACIONES DENAVIER-STOKES

Ejemplo cavidad2d.edp:

real d=clock();

mesh Th=square(32,32);

fespace Xh(Th,P2);

fespace Mh(Th,P1);

Xh u2,v2, u1,v1,psi,phi,up1=0,up2=0;

Mh p,q;

real nu=0.00005,dt=0.1,alpha=1/dt,eps=1e-10;

int i=0;

problem NS(u1,u2,p,v1,v2,q,solver=UMFPACK,init=1) =

int2d(Th)( alpha*(u1*v1+u2*v2)

+nu*(dx(u1)*dx(v1)+dy(u1)*dy(v1)

+dx(u2)*dx(v2)+dy(u2)*dy(v2) )

+ p*q*eps-p*dx(v1)- p*dy(v2)+dx(u1)*q+ dy(u2)*q

)

+ int2d(Th)(-alpha*convect([up1,up2],-dt,up1)*v1

-alpha*convect([up1,up2],-dt,up2)*v2 )

+on(1,2,4,u1=0,u2=0)+on(3,u1=1,u2=0);

for (i=0;i<=1000;i++)

{NS; up1=u1; up2=u2;

solve streamlines(psi,phi) =

int2d(Th)(dx(psi)*dx(phi) + dy(psi)*dy(phi))

+int2d(Th)(-phi*(dy(up1)-dx(up2)))

+on(1,2,3,4,psi=0);

Th=adaptmesh(Th,psi);

if ( !(i % 20)) plot(Th,cmm=" Lineas de corriente, iter "+i,

psi,wait=0,value=true,fill=1,ps="stream-ns.eps");

};

cout<< " CPU = "<< clock() - d << endl;

34 FreeSoft in Cadiz:Freefem++

Page 35: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

5 FLUIDOS INCOMPRESIBLES: ECUACIONES DENAVIER-STOKES

IsoValue-0.00463591-0.0003875720.002444650.005276880.00810910.01094130.01377350.01660580.0194380.02227020.02510240.02793470.03076690.03359910.03643130.03926360.04209580.0449280.04776020.0548408

Lineas de corriente, iter 1000

lineas de corriente con cavidad2d.edp

Velocidad [u1,u2]

Velocidad cavidad2d.edp

35 FreeSoft in Cadiz:Freefem++

Page 36: Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica … · 2012-09-08 · Software libre en Ciencias e Ingenier´ıas. Simulaci´on Num´erica con FreeFem++. Adaptaci´on

5 FLUIDOS INCOMPRESIBLES: ECUACIONES DENAVIER-STOKES

Presion

Presion con cavidad2d.edp

Observar como

if (!(i% 20)) permite dibujar cada 20 iteraciones

Th=adaptmesh(Th,psi); adapta la triangulacion al hes-siano de psi

36 FreeSoft in Cadiz:Freefem++