Escolar Documentos
Profissional Documentos
Cultura Documentos
F. Hecht
Laboratoire Jacques-Louis Lions
Universit Pierre et Marie Curie
Paris, France
http://www.freefem.org
F. Hecht et al.
mailto:hecht@ann.jussieu.fr
FreeFem++ Lecture
1 / 112
Outline
1
Introduction
Tools
Academic Examples
Numerics Tools
Advance exercice
No Linear Problem
F. Hecht et al.
FreeFem++ Lecture
2 / 112
Outline
1
Introduction
History
The main characteristics
Changes
Basement
Weak form
Poisson Equation
Tools
Academic Examples
Numerics Tools
F. Hecht et al.
FreeFem++ Lecture
3 / 112
News
Message of P. jolivet on Curie computer at CEA : (see Tutorial to morrow
afternoon).
Mthode de dcomposition de domaine deux niveaux pour rsoudre des
problmes elliptiques trs htrognes
22 109 dinconnues en 2D (rsolu en 200 secondes sur plus de 16000 coeurs)
0.3 109 dinconnues en 3D (rsolu en 200 secondes sur plus de 8000 coeurs).
The 6th workskop FreeFem++ : Jussieu, Paris 9,10,11 dcembre, 2014.
Morning : presentation
Afternoon : tutorial
F. Hecht et al.
FreeFem++ Lecture
4 / 112
Introduction
FreeFem++ is a software to solve numerically partial differential equations (PDE) in
IR2 ) and in IR3 ) with finite elements methods. We used a user language to set and
control the problem. The FreeFem++ language allows for a quick specification of
linear PDEs, with the variational formulation of a linear steady state problem and the
user can write they own script to solve no linear problem and time depend problem. You
can solve coupled problem or problem with moving domain or eigenvalue problem, do
mesh adaptation , compute error indicator, etc ...
By the way, FreeFem++ is build to play with abstract linear, bilinear form on Finite
Element Space and interpolation operator.
FreeFem++ is a freeware and this run on Mac, Unix and Window architecture, in
parallel with MPI.
F. Hecht et al.
FreeFem++ Lecture
5 / 112
History
1987 MacFem/PCFem les anctres (O. Pironneau en Pascal) payant.
1992 FreeFem rcriture de C++ (P1,P0 un maillage) O. Pironneau, D. Bernardi, F.
Hecht , C. Prudhomme (adaptation Maillage, bamg).
1996 FreeFem+ rcriture de C++ (P1,P0 plusieurs maillages) O. Pironneau, D.
Bernardi, F. Hecht (algbre de fonction).
1998 FreeFem++ rcriture avec autre noyau lment fini, et un autre langage
utilisateur ; F. Hecht, O. Pironneau, K.Ohtsuka.
1999 FreeFem 3d (S. Del Pino) , Une premire version de freefem en 3d avec des
mthodes de domaine fictif.
2008 FreeFem++ v3 rcriture du noyau lment fini pour prendre en compte les cas
multidimensionnels : 1d,2d,3d...
F. Hecht et al.
FreeFem++ Lecture
6 / 112
R&D
Academic Research ,
Algorithmes prototyping
Numerical experimentation
F. Hecht et al.
FreeFem++ Lecture
7 / 112
(2D)(3D)
Wide range of finite elements : continuous P1,P2 elements, discontinuous P0, P1,
RT0,RT1,BDM1, elements ,Edge element, vectorial element, mini-element, ...
F. Hecht et al.
FreeFem++ Lecture
8 / 112
(2D)(3D)
F. Hecht et al.
FreeFem++ Lecture
9 / 112
F. Hecht et al.
FreeFem++ Lecture
10 / 112
11 / 112
0/4
//
//
complex , so
func heavyside = real(x>0.);
...;};
F. Hecht et al.
FreeFem++ Lecture
12 / 112
Element of syntax
1/4
x,y,z , label,
//
current coord., label of BC.
N.x, N.y, N.z,
//
normal
int i = 0;
//
an integer
real a=2.5;
//
a reel
bool b=(a<3.);
real[int] array(10) ;
//
a real array of 10 value
mesh Th; mesh3 Th3;
//
a 2d mesh and a 3d mesh
fespace Vh(Th,P2); //
Def. of scalaire 2d finite element space;
fespace Vh3(Th3,P1);
//
Def. of scalaire 3d finite element
space;
Vh u=x;
//
a finite element function or array
Vh3<complex> uc = x+ 1i *y;
//
complex valued FE
u(.5,.6,.7);
//
value of FE function u at point (.5, .6, .7)
u[];
//
the array of DoF value assoc. to FE function u
u[][5];
//
6th value of the array (numbering begin
//
at 0 like in C)
F. Hecht et al.
FreeFem++ Lecture
13 / 112
Element of syntax
2/4
fespace V3h(Th,[P2,P2,P1]);
V3h [u1,u2,p]=[x,y,z];
//
F. Hecht et al.
FreeFem++ Lecture
14 / 112
Element of syntax
3/4
func Heaveside=(x>0);
//
a formal line function
func real g(int i, real a) { .....; return i+a;}
A = A + A; A = A*A
//
matrix operation (only 1/1)
A = [ [ A,0],[0,A] ];
//
Block matrix.
int[int] I(15),J(15);
//
two array for renumbering
//
the aim is to transform a matrix into a sparse matrix
matrix B;
B = A;
//
copie matrix A
B=A(I,J);
//
B(i,j) = A(I(i),J(j))
B=A(I^-1,J^-1);
//
B(I(i),J(j))= A(i,j)
B.resize(10,20);
//
resize the sparse matrix
//
and remove out of bound terms
int[int] I(1),J(1); real[int] C(1);
[I,J,C]=A;
//
get of the sparse term of the matrix A
//
(the array are resized)
A=[I,J,C];
//
set a new matrix
matrix D=[diagofA] ;
//
set a diagonal matrix D
//
from the array diagofA.
real[int] a=2:12;
//
set a[i]=i+2; i=0 to 10.
Freefem++, Paris, Decembre 2014.
F. Hecht et al.
FreeFem++ Lecture
15 / 112
Element of syntax
F. Hecht et al.
FreeFem++ Lecture
16 / 112
List of Plugin
ls
/usr/local/lib/ff++/3.33/lib/
BEC
BernadiRaugel
BinaryIO
DxWriter
Element_Mixte
Element_P1dc1
Element_P3
Element_P3dc
Element_P4
Element_P4dc
Element_PkEdge
FreeFemQA
MPICG
MUMPS
MUMPS_FreeFem
MUMPS_seq
MetricKuate
MetricPk
Morley
NewSolver
PARDISO
SuperLu
UMFPACK64
VTK_writer
VTK_writer_3d
addNewType
bfstream
dSuperLU_DIST
dfft
ff-Ipopt
ff-NLopt
ff-cmaes
fflapack
ffnewuoa
ffrandom
freeyams
funcTemplate
gmsh
F. Hecht et al.
gsl
hips_FreeFem
ilut
interfacepastix
iovtk
isoline
isolineP1
lapack
lgbmo
mat_dervieux
mat_psi
medit
metis
mmg3d-v4.0
mpi-cmaes
msh3
mshmet
myfunction
myfunction2
FreeFem++ Lecture
parms_FreeFem
pcm2rnm
pipe
ppm2rnm
qf11to25
scotch
shell
splitedges
splitmesh3
splitmesh6
symmetrizeCSR
tetgen
thresholdings
complex_SuperLU_DIST_FreeFe
complex_pastix_FreeFem
real_SuperLU_DIST_FreeFem
real_pastix_FreeFem
17 / 112
Important Plugin
qf11to25 add more quadrature formulae in 1d , 2d, and tools to build own quadrature
Element_*,Morlay,BernadiRaugel add new kind of 2d finite element
SuperLu,UMFPACK64,SuperLu,MUMPS_seq add sequentiel sparse solver
metis,scotch mesh Partitioning
ffrandom true random number generator : srandomdev,srandom, random
gsl the gsl lib interface (lots of special function)
shell,pipe directory and file interface, pipe interface
dfft interface with fftw3 library for FFT.
msh3,tetgen 3d mesh tools and tetgen interface
lapack a small lacpack,interface of full linear solver, full eigen value problem.
ff-Ipopt interface with Ipopt optimisation software
ppm2rnm interface with ppm library to read ppm bitmap.
isoline build a border from isoline.
freeyams, meshmet, mmg3d-v4, medit interface of library of P. Frey to adapt mesh
in 3d.
Freefem++, Paris, Decembre 2014.
F. Hecht et al.
FreeFem++ Lecture
18 / 112
F. Hecht et al.
FreeFem++ Lecture
19 / 112
u = 2 on 2 ,
u
= 0 on e
~n
(1)
Denote Vg = {v H 1 ()/v|2 = g} .
The Basic variationnal formulation with is : find u V2 () , such that
Z
Z
Z
u
u.v =
1v+
v, v V0 ()
(2)
The finite element method is just : replace Vg with a finite element space, and the
FreeFem++ code :
F. Hecht et al.
FreeFem++ Lecture
20 / 112
//
//
read a mesh 3d
define the P1 EF space
Vh u,v;
//
set test and unknown function in Vh.
macro Grad(u) [dx(u),dy(u),dz(u)]
//
EOM Grad def
solve laplace(u,v,solver=CG) =
int3d(Th)( Grad(u)*Grad(v)
)
- int3d(Th) ( 1*v)
+ on(2,u=2);
//
int on 2
plot(u,fill=1,wait=1,value=0,wait=1);
Run:fish.edp
Run:fish3d.edp
F. Hecht et al.
FreeFem++ Lecture
21 / 112
Outline
1
Introduction
Tools
Remarks on weak form and boundary conditions
Mesh generation
Build mesh from image
3d mesh
Mesh tools
Anisotropic Mesh adaptation
Academic Examples
Numerics Tools
F. Hecht et al.
FreeFem++ Lecture
22 / 112
F. Hecht et al.
FreeFem++ Lecture
23 / 112
F. Hecht et al.
FreeFem++ Lecture
24 / 112
Build Mesh 2d
First a 10 10 grid mesh of unit square ]0, 1[2
int[int] labs=[10,20,30,40];
//
bot., right, top, left
mesh Th1 = square(10,10,label=labs,region=0,[x,y]);
//
plot(Th1,wait=1);
int[int] old2newlabs=[10,11, 30,31];
//
10 -> 11, 30 -> 31
Th1=change(Th1,label=old2newlabs) ;
//
//
do Change in 2d or in 3d. region=a, fregion=f ,
//
flabel=f
a L shape domain ]0, 1[2 \[ 12 , 1[2
mesh Th = trunc(Th1,(x<0.5) | (y < 0.5),label=1);
plot(Th,cmm="Th");
mesh Thh = movemesh(Th,[-x,y]);
mesh Th3 = Th+movemesh(Th,[-x,y]);
//
plot(Th3,cmm="Th3");
//
glumesh ...
Run:mesh1.edp
F. Hecht et al.
FreeFem++ Lecture
25 / 112
Build Mesh 2d
a L shape domain ]0, 1[2 \[ 12 , 1[2
border a(t=0,1.0){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.0,1){x=0;
y=1-t;label=1;};
plot(a(6) + b(4) + c(4) +d(4) + e(4) + f(6),wait=1);
mesh Th2 =buildmesh(a(6) + b(4) + c(4) +d(4) + e(4) + f(6));
Get a extern mesh
mesh Th2("april-fish.msh");
build with emc2, bamg, modulef, etc...
F. Hecht et al.
FreeFem++ Lecture
26 / 112
F. Hecht et al.
FreeFem++ Lecture
27 / 112
Run:lac.edp
Freefem++, Paris, Decembre 2014.
//
Curve 0
show curve.
//
//
FreeFem++ Lecture
28 / 112
F. Hecht et al.
FreeFem++ Lecture
29 / 112
F. Hecht et al.
FreeFem++ Lecture
30 / 112
F. Hecht et al.
FreeFem++ Lecture
31 / 112
F. Hecht et al.
FreeFem++ Lecture
32 / 112
F. Hecht et al.
FreeFem++ Lecture
33 / 112
Mesh tools
change to change label and region numbering in 2d and 3d.
movemesh checkmovemesh movemesh23 movemesh3
triangulate (2d) , tetgconvexhull (3d) build mesh mesh for a set of point
emptymesh (2d) built a empty mesh for Lagrange multiplier
freeyams to optimize surface mesh
mmg3d to optimize volume mesh with constant surface mesh
mshmet to compute metric
isoline to extract isoline (2d)
trunc to remove peace of mesh and split all element (2d,3d)
splitmesh to split 2d mesh in no regular way.
F. Hecht et al.
FreeFem++ Lecture
34 / 112
1p
The key-idea is to construct a mesh where the lengths of the edges are close to 1
accordingly to M.
F. Hecht et al.
FreeFem++ Lecture
35 / 112
Metrix intersection
The unit ball BM in a metric M plot the maximum mesh size on all the direction, is a
ellipse.
If you we have two unknowns u and v, we just compute the metric Mu and Mv , find
a metric Muv call intersection with the biggest ellipse such that :
B(Mv ) B(Mu ) B(Mv )
F. Hecht et al.
FreeFem++ Lecture
36 / 112
Example of mesh
u = (10 x3 + y 3 ) + atan2(0.001, (sin(5 y) 2 x))
v = (10 y 3 + x3 ) + atan2(0.01, (sin(5 x) 2 y)).
Run:Adapt-uv.edp
F. Hecht et al.
FreeFem++ Lecture
37 / 112
A corner singularity
The domain is an L-shaped polygon =]0, 1[2 \[ 21 , 1]2 and the PDE is
Find u H01 () such that u = 1 in ,
The solution has a singularity at the reentrant angle and we wish to capture it
numerically.
F. Hecht et al.
FreeFem++ Lecture
38 / 112
F. Hecht et al.
FreeFem++ Lecture
39 / 112
1
1
|u| = |H| where H = u
1
1
(detM` ) `p+2 M`
Run:AdaptP3.edp
F. Hecht et al.
FreeFem++ Lecture
40 / 112
Outline
1
Introduction
Tools
Academic Examples
Laplace/Poisson
3d Poisson equation with mesh adaptation
Linear PDE
Linear elasticty equation
Stokes equation
Optimize Time depend schema
Numerics Tools
F. Hecht et al.
FreeFem++ Lecture
41 / 112
p = gd on d ,
n p = gn on n
(3)
Z
Z
Z
q.~u+ p.~v + ~u.~v =
f q+
gd~v .~n,
F. Hecht et al.
FreeFem++ Lecture
42 / 112
F. Hecht et al.
FreeFem++ Lecture
43 / 112
F. Hecht et al.
FreeFem++ Lecture
44 / 112
where b(u, ) =
mesh Th=square(10,10);
fespace Vh(Th,P1);
//
P1 FE space
int n = Vh.ndof, n1 = n+1; func f=1+x-y;
macro Grad(u) [dx(u),dy(u)]
//
EOM
varf va(uh,vh) = int2d(Th)( Grad(uh)*Grad(vh) ) ;
varf vL(uh,vh) = int2d(Th)( f*vh ) ;
varf vb(uh,vh)= int2d(Th)(1.*vh);
matrix A=va(Vh,Vh);
real[int] b=vL(0,Vh), B = vb(0,Vh);
real[int] bb(n1),x(n1),b1(1),l(1); b1=0;
matrix AA = [ [ A , B ] , [ B, 0 ] ] ; bb = [ b, b1];
set(AA,solver=UMFPACK);
//
set the type of linear solver.
x = AA^-1*bb;
[uh[],l] = x;
//
solve the linear systeme
plot(uh,wait=1);
//
set the value
Run:Laplace-lagrange-mult.edp
Freefem++, Paris, Decembre 2014.
F. Hecht et al.
FreeFem++ Lecture
45 / 112
//
//
label numbering
remove ]0.5, 1[3
fespace Vh(Th3,P1);
Vh u,v;
//
FE. space definition
macro Grad(u) [dx(u),dy(u),dz(u)]
//
EOM
problem Poisson(u,v,solver=CG) =
int3d(Th3)( Grad(u)*Grad(v) ) -int3d(Th3)( 1*v ) + on(1,u=0);
real errm=1e-2;
//
level of error
for(int ii=0; ii<5; ii++)
{
Poisson;
Vh h;
h[]=mshmet(Th3,u,normalization=1,aniso=0,nbregul=1,hmin=1e-3,
hmax=0.3,err=errm);
errm*= 0.8;
//
change the level of error
Th3=tetgreconstruction(Th3,switch="raAQ"
,sizeofvolume=h*h*h/6.);
medit("U-adap-iso-"+ii,Th3,u,wait=1);
}
Run:Laplace-Adapt-3d.edp
F. Hecht et al.
FreeFem++ Lecture
46 / 112
u = 0 on d ,
(u).n = 0 on n
(4)
Where (u) = 21 (u + t u) and (u) = A(u) with A the linear positif operator on
symmetric d d matrix corresponding to the material propriety. Denote
Vg = {v H 1 ()d /v|d = g} .
The Basic displacement variational formulation is : find u V0 (), such that :
Z
Z
Z
(v) : A(u) =
v.f + ((A(u)).n).v, v V0 ()
(5)
F. Hecht et al.
FreeFem++ Lecture
47 / 112
F. Hecht et al.
FreeFem++ Lecture
Alu ...
];
48 / 112
//
EOM
F. Hecht et al.
FreeFem++ Lecture
49 / 112
Run:beam-3d.edp
Run:beam-EV-3d.edp
F. Hecht et al.
FreeFem++ Lecture
Run:beam-3d-Adapt.edp
50 / 112
Stokes equation
The Stokes equation is find a velocity field u = (u1 , .., ud ) and the pressure p on
domain of Rd , such that
u + p = 0
u =0
u = u
in
in
on
F. Hecht et al.
FreeFem++ Lecture
51 / 112
F. Hecht et al.
FreeFem++ Lecture
52 / 112
(6)
F. Hecht et al.
FreeFem++ Lecture
53 / 112
Aij
Mij
b0,i
= tgv = 1030 :
1
if i 24 , and j = i
Z
Z
=
wj wi /dt + k(wj .wi ) +
wj wi else
13
1
Z
if i 24 , and j = i
=
wj wi /dt else
Z
=
uue wi
Where with
13
0
bcl = u
F. Hecht et al.
FreeFem++ Lecture
54 / 112
F. Hecht et al.
FreeFem++ Lecture
55 / 112
b0 = vthermic0(0,Vh);
//
constant part of RHS
bcn = vthermic(0,Vh);
//
tgv on Dirichlet part
//
we have for the node i : i 24 bcn[i] 6= 0
bcl=tgv*u0[];
//
the Dirichlet B.C. part
F. Hecht et al.
FreeFem++ Lecture
56 / 112
Outline
1
Introduction
Tools
Academic Examples
Numerics Tools
Connectivity
Input/Output
Eigenvalue
Eigenvalue/ Eigenvector
Optimization Tools
MPI/Parallel
F. Hecht et al.
FreeFem++ Lecture
57 / 112
Get Connectivity
mesh Th=square(5,5);
fespace Wh(Th,P2);
cout " nb of DoF
: " Wh.ndof endl;
cout " nb of DoF / K : " Wh.ndofK endl;
int k= 2, kdf= Wh.ndofK ;;
//
element 2
cout " df of element " k ":" ;
for (int i=0;i<kdf;i++) cout Wh(k,i) " ";
cout endl;
Remark on local numbering of Dof by element is
for each sub finite element Pk in [P2,P2,P1] get fist DoF on vertex, second Dof on
edge (opposite to vertex), second on K.
Run:Mesh-info.edp
F. Hecht et al.
FreeFem++ Lecture
58 / 112
Save/Restore
uses cout, cin, endl, ,.
To write to (resp. read from) a file,
declare a new variable ofstream ofile("filename");
or
ofstream ofile("filename",append); (resp. ifstream
ifile("filename"); )
and use ofile (resp. ifile) as cout (resp. cin).
You can use pipe to transfer data to a other code here (gnuplot), see pipe.edp example :
Run:pipe.edp
Run:io.edp
F. Hecht et al.
FreeFem++ Lecture
59 / 112
the boundary condition is make with exact penalization : we put 1e30 = tgv on the
diagonal term of the lock degree of freedom. So take Dirichlet boundary condition only
with a variational form and not on b variational form , because we compute eigenvalue
of
1
v = A1 Bv
F. Hecht et al.
FreeFem++ Lecture
60 / 112
//
EOM
on(1,u1=0) ;
//
no BC
int nev=40;
//
number of computed eigenvalue close to 0
real[int] ev(nev);
//
to store nev eigenvalue
Vh[int] eV(nev);
//
to store nev eigenvector
int k=EigenValue(A,B,sym=true,value=ev,vector=eV);
k=min(k,nev);
for (int i=0;i<k;i++)
plot(eV[i],cmm="ev "+i+" v =" + ev[i],wait=1,value=1);
Execute Lap3dEigenValue.edp
Execute LapEigenValue.edp
F. Hecht et al.
FreeFem++ Lecture
61 / 112
Ipopt optimizer
The IPOPT optimizer in a FreeFem++ script is done with the IPOPT function
included in the ff-Ipopt dynamic library. IPOPT is designed to solve constrained
minimization problem in the form :
find x0 = argminf (x)
xRn
ub
i n, xlb
(simple bounds)
i xi xi
s.t.
ub
lb
(constraints functions)
i m, ci ci (x) ci
Where ub and lb stand for "upper bound" and "lower bound". If for some
ub
i, 1 i m we have clb
i = ci , it means that ci is an equality constraint, and an
lb
ub
inequality one if ci < ci .
F. Hecht et al.
FreeFem++ Lecture
62 / 112
//
//
Fitness Function,
//
Gradient
//
Constraints
Constraints jacobian
matrix jacCBuffer;
//
just declare, no need to define yet
func matrix jacC(real[int] &X)
{
...
//
fill jacCBuffer
return jacCBuffer;
}
The hessian returning function is somewhat different because it has to be the hessian of the lagrangian
m
X
function : (x, f , ) 7 f 2 f (x) +
i 2 ci (x) where Rm and R. Your hessian function should
i=1
F. Hecht et al.
FreeFem++ Lecture
63 / 112
Ipopt Call
real[int] Xi = ... ;
IPOPT(J,gradJ,hessianL,C,jacC,Xi, ... );
IPOPT(J,gradJ,C,jacC,Xi,...);
IPOPT(J,gradJ,hessianJ,Xi,...);
F. Hecht et al.
starting point
//
//
IPOPT(J,gradJ,Xi, ... );
//
IPOPT(J,gradJ,Xi, ... );
//
IPOPT([b,A],CC,ui1[],lb=lb1[],clb=cl[]..);
...
//
FreeFem++ Lecture
64 / 112
Ipopt interface
load "ff-Ipopt"
varf vP([u1,u2],[v1,v2]) = int2d(Th)(Grad(u1)*Grad(v1)+ Grad(u2)*Grad(v2))
- int2d(Th)(f1*v1+f2*v2);
matrix A = vP(Vh,Vh);
//
Fitness function matrix...
real[int] b = vP(0,Vh);
//
and linear form
int[int] II1=[0],II2=[1];
//
Constraints matrix
matrix C1 = interpolate (Wh,Vh, U2Vc=II1);
matrix C2 = interpolate (Wh,Vh, U2Vc=II2);
matrix CC = -1*C1 + C2;
//
u2 - u1 >0
Wh cl=0;
//
constraints lower bounds (no upper bounds)
varf vGamma([u1,u2],[v1,v2]) = on(1,2,3,4,u1=1,u2=1);
real[int] onGamma=vGamma(0,Vh);
Vh [ub1,ub2]=[g1,g2];
Vh [lb1,lb2]=[g1,g2];
ub1[] = onGamma ? ub1[] : 1e19 ;
//
Unbounded in interior
lb1[] = onGamma ? lb1[] : -1e19 ;
Vh [uzi,uzi2]=[uz,uz2],[lzi,lzi2]=[lz,lz2],[ui1,ui2]=[u1,u2];;
Wh lmi=lm;
IPOPT([b,A],CC,ui1[],lb=lb1[],clb=cl[],ub=ub1[],warmstart=iter>1,uz=uzi[],lz=lzi[],lm=lmi
Run:IpoptLap.edp
Run:IpoptVI2.edp
F. Hecht et al.
FreeFem++ Lecture
Run:IpoptMinSurfVol.edp
65 / 112
NLopt interface
load "ff-NLopt"
...
if(kas==1)
mini = nloptAUGLAG(J,start,grad=dJ,lb=lo,ub=up,IConst=IneqC,
gradIConst=dIneqC,subOpt="LBFGS",stopMaxFEval=10000,
stopAbsFTol=starttol);
else if(kas==2)
mini = nloptMMA(J,start,grad=dJ,lb=lo,ub=up,stopMaxFEval=10000,
stopAbsFTol=starttol);
else if(kas==3)
mini = nloptAUGLAG(J,start,grad=dJ,IConst=IneqC,gradIConst=dIneqC,
EConst=BC,gradEConst=dBC,
subOpt="LBFGS",stopMaxFEval=200,stopRelXTol=1e-2);
else if(kas==4)
mini = nloptSLSQP(J,start,grad=dJ,IConst=IneqC,gradIConst=dIneqC,
EConst=BC,gradEConst=dBC,
stopMaxFEval=10000,stopAbsFTol=starttol);
Run:VarIneq2.edp
F. Hecht et al.
FreeFem++ Lecture
66 / 112
Stochastic interface
This algorithm works with a normal multivariate distribution in the parameters space
and try to adapt its covariance matrix using the information provides by the successive
function evaluations. Syntaxe : cmaes(J,u[],..) ( )
From http://www.lri.fr/~hansen/javadoc/fr/inria/optimization/
cmaes/package-summary.html
F. Hecht et al.
FreeFem++ Lecture
67 / 112
Stochastic Exemple
load
"ff-cmaes"
load
"mpi-cmaes"
remark, the FreeFem mpicommworld is used by default. The user can specify his own MPI communicator with
the named parameter "comm=", see the MPI section of this manual for more informations about communicators
in FreeFem++.
F. Hecht et al.
FreeFem++ Lecture
68 / 112
Run:Heat3d.edp
//
resolution
Run:NSCaraCyl-100-mpi.edp
F. Hecht et al.
FreeFem++ Lecture
69 / 112
Outline
1
Introduction
Tools
Academic Examples
Numerics Tools
Advance exercice
F. Hecht et al.
FreeFem++ Lecture
70 / 112
i 0 and
Np
X
i = 1.
i=1
Denote i the sub domain which is the support of i function and also denote i the
boundary of i .
The parallel Schwarz method is Let ` = 0 the iterator and a initial guest u0 respecting the
boundary condition (i.e. u0| = g).
i = 1.., Np :
u`i = f, in i ,
PNp
u`+1 = i=1
i u`i
F. Hecht et al.
and u`i = u` on i
FreeFem++ Lecture
(7)
(8)
71 / 112
Some Remark
We never use finite element space associated to the full domain because it to expensive. So
we use on each domain i we defined Ji = {j 1, . . . , Np / i j 6= } and we have
X
(u`+1 )|i =
(j u`j )|i
(9)
jJi
We denote u`h|i the restriction of u`h on Vhi , so the discrete problem on i of problem (7) is
find u`hi Vhi such that :
Z
Z
vhi V0i :
vhi .u`hi =
f vhi ,
i
i
k Nhi
: ik (u`hi ) = ik (u`h|i )
i
where Nhi
is the set of the degree of freedom (Dof) on i and ik the Dof of Vhi .
F. Hecht et al.
FreeFem++ Lecture
72 / 112
F. Hecht et al.
FreeFem++ Lecture
73 / 112
parallel GMRES
Finally you can easily accelerate the fixe point algorithm by using a parallel GMRES algorithm
after the introduction the following affine Si operator sub domain i .
func real[int] Si(real[int]& U) {
real[int] V(U.n) ; b= onG .* U;
b = onG ? b : Bi ;
V = Ai^-1*b;
Update(V,U);
V -= U;
return V; }
//
//
(7)
(??)
F. Hecht et al.
FreeFem++ Lecture
74 / 112
CoarseSolve(real[int]& V,real[int]& U,
mpiComm& comm)
{
if(AC.n==0 && mpiRank(comm)==0)
//
AC = vPbC(VhC,VhC,solver=sparsesolver);
real[int] Uc(Rci.n),Bc(Uc.n);
Uc= Rci*U;
//
mpiReduce(Uc,Bc,processor(0,comm),mpiSUM);
if(mpiRank(comm)==0)
Uc = AC^-1*Bc;
//
broadcast(processor(0,comm),Uc);
V = Pci*Uc;
//
Fine to Coarse
solve of proc 0
Coarse to Fine
}
Limitation : if the initial problem, data have oscillation, you must use homogenization technic
on coarse problem, or use the F. Nataf and co, preconditionner.
F. Hecht et al.
FreeFem++ Lecture
75 / 112
The basic schwarz algorithm u`+1 = S(u` ), where S is one iteration of schwarz process.
Use the method with two level precondicionneur RAS and Coarse.
100
100000
computation
200/n
Computation
2 1O^-6 n^1.2
Elapse time/s
10000
1000
100
10
10
1
100000
1
1e+06
1e+07
Nb of DoF
1e+08
1e+09
F. Hecht et al.
FreeFem++ Lecture
10
Nb of process
100
76 / 112
Run:DDM-Schwarz-Lap-2dd.edp
Run:DDM-Schwarz-Lame-3d.edp
F. Hecht et al.
Run:DDM-Schwarz-Lame-2d.edp
Run:DDM-Schwarz-Stokes-2d.edp
FreeFem++ Lecture
77 / 112
Outline
1
Introduction
Tools
Academic Examples
Numerics Tools
Advance exercice
Heat equation with thermic resistance
No Linear Problem
FreeFem++ Lecture
78 / 112
in i
[u.n] = 0,
on 0
F. Hecht et al.
FreeFem++ Lecture
79 / 112
F. Hecht et al.
FreeFem++ Lecture
80 / 112
Outline
1
Introduction
Tools
Academic Examples
Numerics Tools
Advance exercice
No Linear Problem
Navier-Stokes
Variational Inequality
Ground water
F. Hecht et al.
FreeFem++ Lecture
81 / 112
u0 a initial guest
do
find wn solution of DF (un )wn = F (un )
un+1 = un wn
if( ||wn || < ) break ;
Z
DF (u, p)(w, wp ) =
(w.)u.v + (u.)w.v
Z
w : v q.w wp .v + BC0
Run:cavityNewton.edp
Freefem++, Paris, Decembre 2014.
Run:NSNewtonCyl-100-mpi.edp
F. Hecht et al.
FreeFem++ Lecture
82 / 112
un X n ) un+1 + pn+1 = 0,
un+1
=0
(11)
F. Hecht et al.
FreeFem++ Lecture
83 / 112
Variational Inequality
To solve just make a change of variable u = u+ u , u > 0 and v = u+ + u , and we
get a classical VI problem on u and and the Poisson on v.
So we can use the algorithm of Primal-Dual Active set strategy as a semi smooth
Newton Method HinterMuller , K. Ito, K. Kunisch SIAM J. Optim. V 13, I 3, 2002.
In this case , we just do all implementation by hand in FreeFem++ language
Run:VI-2-membrane-adap.edp
F. Hecht et al.
FreeFem++ Lecture
85 / 112
F. Hecht et al.
FreeFem++ Lecture
86 / 112
p = 0
in
p
=
y
on b
p
=0
on d a
n
= Kq nx on f
(N eumann)
p =y
on f
(Dirichlet)
where the input water flux is q = 0.02, and K = 0.5. The velocity u of the water is
given by u = p.
Freefem++, Paris, Decembre 2014.
F. Hecht et al.
FreeFem++ Lecture
87 / 112
algorithm
We use the following fix point method : (with bad main B.C. Execute freeboundaryPB.edp
) let be, k = 0, k = . First step, we forgot the Neumann BC and we solve the
problem : Find p in V = H 1 (k ), such p = y on kb et on kf
Z
pp0 = 0, p0 V with p0 = 0 on kb kf
k
F. Hecht et al.
FreeFem++ Lecture
88 / 112
Implementation
The new domain is : k+1 = F(k ) Warning if is the movement is too large we can
have triangle overlapping.
problem Pp(p,pp,solver=CG) =
int2d(Th)( dx(p)*dx(pp)+dy(p)*dy(pp))
+ on(b,f,p=y) ;
problem Pv(v,vv,solver=CG) =
int2d(Th)( dx(v)*dx(vv)+dy(v)*dy(vv))
+ on (a, v=0)
+ int1d(Th,f)(vv*
((Q/K)*N.y-(dx(p)*N.x+dy(p)*N.y)));
while(errv>1e-6)
{
j++; Pp; Pv;
errv=int1d(Th,f)(v*v);
coef = 1;
//
Here french cooking if overlapping see the example
Th=movemesh(Th,[x,y-coef*v]);
}
Execute freeboundary.edp
F. Hecht et al.
FreeFem++ Lecture
//
deformation
89 / 112
y
x
. u
Run:BEC.edp
F. Hecht et al.
FreeFem++ Lecture
90 / 112
where J1 = I1 I3 3 , J2 = I2 I3 3 ,J = I32 ,
let u the deplacement, when
F = Id + u
C = tF F
I1 = tr(C)
I2 = 12 (tr(C)2 tr(C 2 ))
I3 = det(C)
The problem is find
u = argmin W (I1 , I2 , I3 )
u
F. Hecht et al.
FreeFem++ Lecture
91 / 112
Run:ElasticLaw2d.edp
F. Hecht et al.
FreeFem++ Lecture
Run:CiarletGemona.edp
92 / 112
Outline
1
Introduction
Tools
Academic Examples
Numerics Tools
Advance exercice
No Linear Problem
FreeFem++ Lecture
93 / 112
install MPI for // version HPC Pack 2008 SDK and HPC Pack 2008 R2 Service Pack 2
F. Hecht et al.
FreeFem++ Lecture
94 / 112
ff-c++ myppm2rnm.cpp
F. Hecht et al.
FreeFem++ Lecture
95 / 112
read_image( pRnm
//
//
//
real[int,int] array
real[int] array
the ff++ string
//
the function
variable type
variable type
variable type
to read image
//
Remark, TheOperators is the ff++ variable to store all world operator, Global is
to store function.
F. Hecht et al.
FreeFem++ Lecture
96 / 112
How to extend
A true simple example How to make dynamic gnuplot
Idea : use a pipe to speak with gnuplot the C code :
FILE * gp = popen("gnuplot");
for( double f=0; f < 3.14; f += 0.01)
fprintf(gp,"plot sin(x+%f)\n",f);
To do this add a new constructor of ofstream in freefem++
F. Hecht et al.
FreeFem++ Lecture
97 / 112
F. Hecht et al.
FreeFem++ Lecture
98 / 112
F. Hecht et al.
FreeFem++ Lecture
99 / 112
//
T[i];
F. Hecht et al.
FreeFem++ Lecture
100 / 112
//
F. Hecht et al.
FreeFem++ Lecture
//
number of triangle K
//
number of triangle V
number of Seg de bord BE
//
check.
101 / 112
Outline
1
Introduction
Tools
Academic Examples
Numerics Tools
Advance exercice
No Linear Problem
F. Hecht et al.
FreeFem++ Lecture
102 / 112
u given
in s
t u + (u)u + .u + p = cT e2
in f
(12)
.u = 0
in f
F. Hecht et al.
FreeFem++ Lecture
103 / 112
tanh(50( m )))
.
2Ste
F. Hecht et al.
FreeFem++ Lecture
104 / 112
F. Hecht et al.
FreeFem++ Lecture
105 / 112
the Algorithm
We apply a fixed point algorithm for the phase change part (the domain f is fixed at
each iteration) and a full no-linear Euler implicit scheme with a fixed domain for the
rest. We use a Newton method to solve the non-linearity.
if we dont make mesh adaptation, the Newton method do not converge
if we use explicit method diverge too,
if we implicit the dependance in s the method also diverge.
This is a really difficult problem.
F. Hecht et al.
FreeFem++ Lecture
106 / 112
This mean, we adapt with all variable plus the 2 melting phase a time n + 1 and n and
we smooth the metric with a ratio of 1.2 to account for the movement of the melting
front.
F. Hecht et al.
FreeFem++ Lecture
107 / 112
F. Hecht et al.
FreeFem++ Lecture
108 / 112
F. Hecht et al.
FreeFem++ Lecture
109 / 112
F. Hecht et al.
FreeFem++ Lecture
110 / 112
So now, a real problem, get the physical parameter of the real experiment.
Run:Orange-Newton.edp
F. Hecht et al.
FreeFem++ Lecture
111 / 112
Conclusion/Future
Freefem++ v3.23 is
very good tool to solve non standard PDE in 2D/3D
to try new domain decomposition domain algorithm
F. Hecht et al.
FreeFem++ Lecture
112 / 112