Freefem Doc
Freefem Doc
Freefem Doc
49
http://www.freefem.org/ff++
F. Hecht,
O. Piro
A. Le Hyaric, K. O
FreeFem++
Third Edition, Version 3.49
http://www.freefem.org/ff++
Frederic Hecht1 2
mailto:frederic.hecht@upmc.fr
https://www.ljll.math.upmc.fr/hecht
In collaboration with:
Sylvian Auliac, mailto:auliac@ann.jussieu.fr, http://www.ann.jussieu.fr/
auliac is PHD student at LJLL , he do all the new optimization interface with nlopt, ipopt,
cmaes, ...
Olivier Pironneau, mailto:olivier.pironneau@upmc.fr, http://www.ann.jussieu.
fr/pironneau Olivier Pironneau is a professor of numerical analysis at the university of
Paris VI and at LJLL. His scientific contributions are in numerical methods for fluids. He is
a member of the Institut Universitaire de France and of the French Academy of Sciences
Jacques Morice, mailto:morice@ann.jussieu.fr. Jacaues Morice is a Post-Doct at
LJLL. His doing is Thesis in University of Bordeaux I on fast multipole method (FMM). In
this version, he do all three dimensions mesh generation and coupling with medit software.
Antoine Le Hyaric, mailto:lehyaric@ann.jussieu.fr, http://www.ann.jussieu.
fr/lehyaric/ Antoine Le Hyaric is a research engineer from the Centre National de la
Recherche Scientifique (CNRS) at LJLL . He is an expert in software engineering for scientific applications. He has applied his skills mainly to electromagnetics simulation, parallel
computing and three-dimensional visualization.
Kohji Ohtsuka,mailto:ohtsuka@hkg.ac.jp, http://www.comfos.org/ Kohji Ohtsuka is a professor at the Hiroshima Kokusai Gakuin University, Japan and chairman of the
World Scientific and Engineering academy and Society, Japan chapter. His research is in
fracture dynamics, modeling and computing.
Pierre Jolivet, mailto:pierre.jolivet@enseeiht.fr, http://jolivet.perso.
enseeiht.fr/ is a CNRS researcher, he did the MPI interface with PETSc, HPDDM...
Contents
1 Introduction
1.1 Installation . . . . . . . . . . . . . . . . . . .
1.1.1 For everyone: . . . . . . . . . . . . . .
1.1.2 For the pros: Installation from sources
1.2 How to use FreeFem++ . . . . . . . . . . .
1.3 Environment variables, and the init file . . . .
1.4 History . . . . . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
13
14
14
15
19
20
21
2 Getting Started
2.0.1 FEM by FreeFem++ : how does it work? . . . . . . . . . . . . . . .
2.0.2 Some Features of FreeFem++ . . . . . . . . . . . . . . . . . . . . .
2.1 The Development Cycle: EditRun/VisualizeRevise . . . . . . . . . . . . .
23
24
29
29
3 Learning by Examples
3.1 Membranes . . . . . . . . . . . . . . . . . . . . . . . . .
3.2 Heat Exchanger . . . . . . . . . . . . . . . . . . . . . . .
3.3 Acoustics . . . . . . . . . . . . . . . . . . . . . . . . . .
3.4 Thermal Conduction . . . . . . . . . . . . . . . . . . . .
3.4.1 Axisymmetry: 3D Rod with circular section . . .
3.4.2 A Nonlinear Problem : Radiation . . . . . . . . .
3.5 Irrotational Fan Blade Flow and Thermal effects . . . . .
3.5.1 Heat Convection around the airfoil . . . . . . . .
3.6 Pure Convection : The Rotating Hill . . . . . . . . . . .
3.7 The System of elasticity . . . . . . . . . . . . . . . . . .
3.8 The System of Stokes for Fluids . . . . . . . . . . . . . .
3.9 A Projection Algorithm for the Navier-Stokes equations
3.10 Newton Method for the Steady Navier-Stokes equations .
3.11 A Large Fluid Problem . . . . . . . . . . . . . . . . . . .
3.12 An Example with Complex Numbers . . . . . . . . . . .
3.13 Optimal Control . . . . . . . . . . . . . . . . . . . . . .
3.14 A Flow with Shocks . . . . . . . . . . . . . . . . . . . . .
3.15 Classification of the equations . . . . . . . . . . . . . . .
3.16 Optimize Time depend schema for Heat equation . . . .
3.17 Tutorial to write a transient Stokes solver in matrix form
31
31
36
38
39
41
42
42
44
45
49
51
52
56
58
62
63
65
67
70
71
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
6
4 Syntax
4.1 Data Types . . . . . . . . . . . . . . . . . . . . . . . .
4.2 List of major types . . . . . . . . . . . . . . . . . . . .
4.3 Global Variables . . . . . . . . . . . . . . . . . . . . . .
4.4 System Commands . . . . . . . . . . . . . . . . . . . .
4.5 Arithmetics . . . . . . . . . . . . . . . . . . . . . . . .
4.6 string expression . . . . . . . . . . . . . . . . . . . . .
4.7 Functions of one Variable . . . . . . . . . . . . . . . . .
4.8 Functions of two Variables . . . . . . . . . . . . . . . .
4.8.1 Formula . . . . . . . . . . . . . . . . . . . . . .
4.8.2 FE-functions . . . . . . . . . . . . . . . . . . .
4.9 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.9.1 Arrays with two integer indices versus matrices
4.9.2 Matrix construction and setting . . . . . . . . .
4.9.3 Matrix Operations . . . . . . . . . . . . . . . .
4.9.4 Other arrays . . . . . . . . . . . . . . . . . . . .
4.10 Map arrays . . . . . . . . . . . . . . . . . . . . . . . .
4.11 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.12 Input/Output . . . . . . . . . . . . . . . . . . . . . . .
4.12.1 Script arguments . . . . . . . . . . . . . . . . .
4.13 preprocessor . . . . . . . . . . . . . . . . . . . . . . . .
4.14 Exception handling . . . . . . . . . . . . . . . . . . . .
CONTENTS
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
5 Mesh Generation
5.1 Commands for Mesh Generation . . . . . . . . . . . . . . . . . . . .
5.1.1 Square . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.1.2 Border . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.1.3 Multy Border . . . . . . . . . . . . . . . . . . . . . . . . . .
5.1.4 Data Structures and Read/Write Statements for a Mesh . .
5.1.5 Mesh Connectivity . . . . . . . . . . . . . . . . . . . . . . .
5.1.6 The keyword triangulate . . . . . . . . . . . . . . . . . . .
5.2 Boundary FEM Spaces Built as Empty Meshes . . . . . . . . . . .
5.3 Remeshing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.3.1 Movemesh . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.4 Regular Triangulation: hTriangle . . . . . . . . . . . . . . . . . .
5.5 Adaptmesh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.6 Trunc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.7 Splitmesh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.8 Meshing Examples . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.9 How to change the label of elements and border elements of a mesh
5.10 Mesh in three dimensions . . . . . . . . . . . . . . . . . . . . . . . .
5.10.1 cube . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.10.2 Read/Write Statements for a Mesh in 3D . . . . . . . . . . .
5.10.3 TeGen: A tetrahedral mesh generator . . . . . . . . . . . .
5.10.4 Reconstruct/Refine a three dimensional mesh with TetGen .
5.10.5 Moving mesh in three dimensions . . . . . . . . . . . . . . .
5.10.6 Layer mesh . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
73
. 73
. 74
. 75
. 76
. 77
. 79
. 80
. 82
. 82
. 82
. 83
. 89
. 90
. 92
. 96
. 97
. 97
. 99
. 101
. 101
. 103
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
107
107
107
108
111
112
114
117
118
119
119
121
121
126
127
128
132
134
134
135
136
140
142
142
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
CONTENTS
5.11 Meshing examples . . . . . . . . . . . . . . . . .
5.11.1 Build a 3d mesh of a cube with a balloon
5.12 The output solution formats .sol and .solb . . .
5.13 medit . . . . . . . . . . . . . . . . . . . . . . . .
5.14 Mshmet . . . . . . . . . . . . . . . . . . . . . .
5.15 FreeYams . . . . . . . . . . . . . . . . . . . . .
5.16 mmg3d . . . . . . . . . . . . . . . . . . . . . . .
5.17 A first 3d isotope mesh adaptation process . . .
5.18 Build a 2d mesh from a isoline . . . . . . . . . .
7
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
147
149
150
152
155
157
159
162
163
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
167
172
173
174
174
174
175
176
177
178
178
180
182
183
184
185
189
192
197
199
7 Visualization
7.1 Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.2 link with gnuplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.3 link with medit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
201
201
206
206
209
209
212
212
213
214
215
220
222
222
223
6 Finite Elements
6.1 Use of fespace in 2d . . . . . . . . . . . . . . . .
6.2 Use of fespace in 3d . . . . . . . . . . . . . . . . . .
6.3 Lagrangian Finite Elements . . . . . . . . . . . . .
6.3.1 P0-element . . . . . . . . . . . . . . . . . .
6.3.2 P1-element . . . . . . . . . . . . . . . . . .
6.3.3 P2-element . . . . . . . . . . . . . . . . . .
6.4 P1 Nonconforming Element . . . . . . . . . . . . .
6.5 Other FE-space . . . . . . . . . . . . . . . . . . . .
6.6 Vector valued FE-function . . . . . . . . . . . . . .
6.6.1 Raviart-Thomas element . . . . . . . . . . .
6.7 A Fast Finite Element Interpolator . . . . . . . . .
6.8 Keywords: Problem and Solve . . . . . . . . . . . .
6.8.1 Weak form and Boundary Condition . . . .
6.9 Parameters affecting solve and problem . . . . .
6.10 Problem definition . . . . . . . . . . . . . . . . . .
6.11 Numerical Integration . . . . . . . . . . . . . . . .
6.12 Variational Form, Sparse Matrix, PDE Data Vector
6.13 Interpolation matrix . . . . . . . . . . . . . . . . .
6.14 Finite elements connectivity . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
CONTENTS
8.6
8.7
9 Mathematical Models
9.1 Static Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.1.1 Soap Film . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.1.2 Electrostatics . . . . . . . . . . . . . . . . . . . . . . . . .
9.1.3 Aerodynamics . . . . . . . . . . . . . . . . . . . . . . . . .
9.1.4 Error estimation . . . . . . . . . . . . . . . . . . . . . . .
9.1.5 Periodic Boundary Conditions . . . . . . . . . . . . . . .
9.1.6 Poisson Problems with mixed boundary condition . . . . .
9.1.7 Poisson with mixted finite element . . . . . . . . . . . . .
9.1.8 Metric Adaptation and residual error indicator . . . . . . .
9.1.9 Adaptation using residual error indicator . . . . . . . . . .
9.2 Elasticity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.2.1 Fracture Mechanics . . . . . . . . . . . . . . . . . . . . . .
9.3 Nonlinear Static Problems . . . . . . . . . . . . . . . . . . . . . .
9.3.1 Newton-Raphson algorithm . . . . . . . . . . . . . . . . .
9.4 Eigenvalue Problems . . . . . . . . . . . . . . . . . . . . . . . . .
9.5 Evolution Problems . . . . . . . . . . . . . . . . . . . . . . . . . .
9.5.1 Mathematical Theory on Time Difference Approximations.
9.5.2 Convection . . . . . . . . . . . . . . . . . . . . . . . . . .
9.5.3 2D Black-Scholes equation for an European Put option . .
9.6 Navier-Stokes Equation . . . . . . . . . . . . . . . . . . . . . . . .
9.6.1 Stokes and Navier-Stokes . . . . . . . . . . . . . . . . . . .
9.6.2 Uzawa Algorithm and Conjugate Gradients . . . . . . . . .
9.6.3 NSUzawaCahouetChabart.edp . . . . . . . . . . . . . . . .
9.7 Variational inequality . . . . . . . . . . . . . . . . . . . . . . . . .
9.8 Domain decomposition . . . . . . . . . . . . . . . . . . . . . . . .
9.8.1 Schwarz Overlap Scheme . . . . . . . . . . . . . . . . . . .
9.8.2 Schwarz non Overlap Scheme . . . . . . . . . . . . . . . .
9.8.3 Schwarz-gc.edp . . . . . . . . . . . . . . . . . . . . . . . .
9.9 Fluid/Structures Coupled Problem . . . . . . . . . . . . . . . . .
9.10 Transmission Problem . . . . . . . . . . . . . . . . . . . . . . . .
9.11 Free Boundary Problem . . . . . . . . . . . . . . . . . . . . . . .
9.12 Non linear Elasticity (nolinear-elas.edp) . . . . . . . . . . . . . . .
9.13 Compressible Neo-Hookean Materials: Computational Solutions .
9.13.1 Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.13.2 A Neo-Hookean Compressible Material . . . . . . . . . . .
9.13.3 An Approach to Implementation in FreeFem++ . . . . .
9.14 Whispering gallery modes . . . . . . . . . . . . . . . . . . . . . .
9.14.1 Wave equation for the WGMs . . . . . . . . . . . . . . . .
9.15 Weak formulation . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.15.1 A dielectric sphere example with FreeFem++ . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
233
233
233
235
237
239
240
243
245
246
248
250
254
257
258
259
262
264
266
268
270
270
275
276
279
281
281
282
284
286
289
292
295
298
299
299
300
301
301
302
302
CONTENTS
10 MPI
10.1
10.2
10.3
10.4
10.5
10.6
Parallel version
MPI keywords . . . . . . . .
MPI constants . . . . . . . .
MPI Constructor . . . . . .
MPI functions . . . . . . . .
MPI communicator operator
Schwarz example in parallel
10.6.1 True parallel Schwarz
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
example
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
303
303
303
303
304
304
305
307
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
313
313
316
316
319
321
323
323
328
330
333
333
336
337
337
337
338
340
341
.
.
.
.
.
343
343
344
344
345
345
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
355
355
355
356
356
357
357
10
CONTENTS
B Grammar
359
B.1 The bison grammar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
B.2 The Types of the languages, and cast . . . . . . . . . . . . . . . . . . . . . . 363
B.3 All the operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
C Dynamical link
C.1 A first example myfunction.cpp . . . . . . . . . . . . . .
C.2 Example: Discrete Fast Fourier Transform . . . . . . . .
C.3 Load Module for Dervieux P0-P1 Finite Volume Method
C.4 More on Adding a new finite element . . . . . . . . . . .
C.5 Add a new sparse solver . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
369
369
372
374
377
380
D Plugin
385
D.1 gsl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
D.2 ffrandom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
E Keywords
401
Preface
Fruit of a long maturing process, freefem, in its last avatar, FreeFem++ , is a high level
integrated development environment (IDE) for numerically solving partial differential equations (PDE) in dimension 2 and 3. It is the ideal tool for teaching the finite element method
but it is also perfect for research to quickly test new ideas or multi-physics and complex
applications.
FreeFem++ has an advanced automatic mesh generator, capable of a posteriori mesh adaptation; it has a general purpose elliptic solver interfaced with fast algorithms such as the
multi-frontal method UMFPACK, SuperLU, MUMPS . Hyperbolic and parabolic problems
are solved by iterative algorithms prescribed by the user with the high level language of
FreeFem++ . It has several triangular finite elements, including discontinuous elements.
Finally everything is there in FreeFem++ to prepare research quality reports: color display
online with zooming and other features and postscript printouts.
This manual is meant for students at Master level, for researchers at any level, and for
engineers (including financial engineering) with some understanding of variational methods
for partial differential equations.
11
12
CONTENTS
Chapter 1
Introduction
A partial differential equation is a relation between a function of several variables and its
(partial) derivatives. Many problems in physics, engineering, mathematics and even banking
are modeled by one or several partial differential equations.
FreeFem++ is a software to solve these equations numerically. As its name implies, it is
a free software (see the copyrights for full detail) based on the Finite Element Method; it
is not a package, it is an integrated product with its own high level programming language.
This software runs on all UNIX OS (with g++ 3.3 or later, and OpenGL) , on Window XP,
Vista and 7,8,10 and on MacOS 10 intel
Moreover FreeFem++ is highly adaptive. Many phenomena involve several coupled systems, for example: fluid-structure interactions, Lorentz forces for aluminium casting and
ocean-atmosphere problems are three such systems. These require different finite element
approximations and polynomial degrees, possibly on different meshes. Some algorithms like
Schwarz domain decomposition method also require data interpolation on multiple meshes
within one program. FreeFem++ can handle these difficulties, i.e. arbitrary finite element
spaces on arbitrary unstructured and adapted bi-dimensional meshes.
14
CHAPTER 1. INTRODUCTION
Metric-based anisotropic mesh adaptation. The metric can be computed automatically
from the Hessian of any FreeFem++ function [13].
High level user friendly typed input language with an algebra of analytic and finite
element functions.
Multiple finite element meshes within one application with automatic interpolation of
data on different meshes and possible storage of the interpolation matrices.
A large variety of triangular finite elements : linear, quadratic Lagrangian elements
and more, discontinuous P1 and Raviart-Thomas elements, elements of a non-scalar
type, the mini-element,. . . (but no quadrangles).
Tools to define discontinuous Galerkin finite element formulations P0, P1dc, P2dc
and keywords: jump, mean, intalledges.
A large variety of linear direct and iterative solvers (LU, Cholesky, Crout, CG, GMRES, UMFPACK, MUMPS, SuperLU, ...) and eigenvalue and eigenvector solvers
(ARPARK) .
Near optimal execution speed (compared with compiled C++ implementations programmed directly).
Online graphics, generation of ,.txt,.eps,.gnu, mesh files for further manipulations of input and output data.
Many examples and tutorials: elliptic, parabolic and hyperbolic problems, NavierStokes flows, elasticity, Fluid structure interactions, Schwarzs domain decomposition
method, eigenvalue problem, residual error indicator, ...
A parallel version using mpi
1.1
1.1.1
Installation
For everyone:
Install by double click on the appropriate file, under linux and MacOS the install file are respectively in directory /usr/local/bin, /usr/local/share/freefem++, /usr/local/lib/f
1.1. INSTALLATION
15
Windows binaries install First download the windows installation executable, then double
click it. to install FreeFem++ . In most cases just answer yes (or typr return) to all
questions. Otherwise in the Additional Task windows, check the box Add application
directory to your system path your system path . This is required otherwise the program
ffglut.exe will not be found.
By now you should have two new icons on your desktop:
FreeFem++ (VERSION).exe the FreeFem++ application.
FreeFem++ (VERSION) Examples a link to the FreeFem++ folder of examples.
where (VERSION) is the version of the files (for example 3.3-0-P4).
By default, the installed files are in
C:\Programs Files\FreeFem++
In this directory, you have all the .dll files and other applications: FreeFem++-nw.exe,ffglut.exe
... the FreeFem++ application without graphic windows.
The syntax for the command-line tools are the same as those of FreeFem.exe.
MacOS X binaries install Download the MacOS X binary version file, extract all the files
with a double click on the icon of the file, go the the directory and put the FreeFem+.app
application in the /Applications directory. If you want a terminal access to FreeFem++
just copy the file FreeFem++ in a directory of your $PATH shell environment variable.
If you want to automatically launch the FreeFem++.app, double click on a .edp file icon.
Under the finder pick a .edp in directory examples++-tutorial for example, select
menu File -> Get Info an change Open with: (choose FreeFem++.app) and click
on button change All....
Where to go from here An integrated environment called FreeFem++-cs, written by Antoine Le Hyaric, is provided with FreeFem++ . Unless you wish to profile now your own development environment, you may proceed to the next paragraph How to use FreeFem++.
1.1.2
This section is for those who for some reason do not wish to use the binaries and hence need
to recompile FreeFem++ or install it from the source code:
The documentation archive : The documentation is also open source; to regenerate it you
need a LATEX environment capable of compiling a CVS archive; under MS-Windows you will
have to use mingw/msys
http://www.mingw.org
and under MacOS X we have used Apples Developer Tools Xcode and LATEX from http:
//www.ctan.org/system/mac/texmac.
16
CHAPTER 1. INTRODUCTION
The C++ archive : FreeFem++ must be compiled from the source archive, as indicated
in
http://www.freefem.org/ff++/index.htm
To extract files from the compressed archive freefem++-(VERSION).tar.gz to a directory called
freefem++-(VERSION)
enter the following commands in a shell window :
tar zxvf freefem++-(VERSION).tar.gz
cd freefem++-(VERSION)
To compile and install FreeFem++ , just follow the INSTALL and README files. The
following programs are produced, depending on the system you are running :
1. FreeFem++ , standard version, with a graphical interface based on GLUT/OpenGL
(use ffglut visualization tool) or not just add -nw parameter.
2. ffglut the visualization tools through a pipe of freefem++ (remark: if ffglut is not
in the system path, you will have no plot)
3. FreeFem++-nw, postscript plot output only and ffmedit (batch version, no graphics
windows via ffglut )
4. FreeFem++-mpi, parallel version, postscript output only, but you can use the shell
script like
(a) ff-mpirun -np nbproc mympiscript.edp ## without graphic
(b) ff-mpirun -np nbproc mympiscript.edp -glut ffglut
graphic
## with
1.1. INSTALLATION
17
edpfilepath where
edpfilepath whe
If you are using nedit as your text editor, do one time nedit -import edp.nedit to
have coloring syntax for your .edp files.
Link with other text editors
notepad++ at http://notepad-plus.sourceforge.net/uk/site.htm
Open Notepad++ and Enter F5
In the new window enter the command launchff++ "$(FULL_CURRENT_PATH)"
Click on Save, and enter FreeFem++ in the box Name, now choose the short
cut key to launch directly FreeFem++ (for example alt+shift+R)
To add Color Syntax Compatible with FreeFem++ In Notepad++,
In Menu "Parameters"->"Configuration of the
proceed as follows:
In the list "Language" select C++
Add edp in the field "add ext"
Color Syntax"
18
CHAPTER 1. INTRODUCTION
Select "INSTRUCTION WORD" in the list "Description" and in the field
"supplementary key word", cut and past the following list:
P0 P1 P2 P3 P4 P5 P1dc P2dc P3dc P4dc P5dc RT0 RT1 RT2 RT3 RT4
RT5 macro plot int1d int2d solve movemesh adaptmesh trunc checkmovemesh
on func buildmesh square Eigenvalue min max imag exec LinearCG NLCG
Newton BFGS LinearGMRES catch try intalledges jump average mean load
savemesh convect abs sin cos tan atan asin acos cotan sinh cosh tanh cotanh
atanh asinh acosh pow exp log log10 sqrt dx dy endl cout
Select TYPE WORD in the list Description and ... supplementary
key word, cut and past the following list
mesh real fespace varf matrix problem string border complex ifstream ofstream
Click on Save & Close. Now nodepad++ is configured.
winedt for Windows : this is the best but it could be tricky to set up. Download it from
http://www.winedt.com
this is a multipurpose text editor with advanced features such as syntax coloring; a
macro is available on www.freefem.org to localize winedt to FreeFem++ without
disturbing the winedt functional mode for LateX, TeX, C, etc. However winedt is not
free after the trial period.
TeXnicCenter for Windows: this is the easiest and will be the best once we find a volunteer
to program the color syntax. Download it from
http://www.texniccenter.org/
It is also an editor for TeX/LaTeX. It has a tool menu which can be configured to
launch FreeFem++ programs as in:
Select the Tools/Customize item which will bring up a dialog box.
Select the Tools tab and create a new item: call it freefem.
in the 3 lines below,
1. search for FreeFem++.exe
2. select Main file with further option then Full path and click also on the 8.3
box
3. select main file full directory path with 8.3
nedit on the Mac OS, Cygwin/Xfree and linux, to import the color syntax do
nedit -import edp.nedit
textmate 2 for Mac 10.7 or better, download from https://macromates.com/download
and install it, when get the textmate freefem++ syntax from http://www.freefem.
org/ff++/textmate2.zip unzip and follow the explanation given in file How To.rtf.
19
1.2
Under MacOS X with Graphical Interface To test an .edp file, just drag and drop the file
icon on the MacOS application icon FreeFem++.app. You can also launch this application
and use the menu: File Open.
20
CHAPTER 1. INTRODUCTION
One of the best ways however on the Mac is to use a text editor like Smultron.app (see
above).
Figure 1.2: The 3 panels of the integrated environment built with the fraise Editor
with FreeFem++ in action. The Tools menu has an item to launch FreeFem++ by a
Ctrl+1 command.
1.3
FreeFem++ reads a users init file named freefem++.pref to initialize global variables:
verbosity, includepath, loadpath.
Remark 3 The variable verbosity changes the level of internal printing (0, nothing (unless there are syntax errors), 1 few, 10 lots, etc. ...), the default value is 2.
The include files are searched from the includepath list and the load files are searched
from loadpath list.
The syntax of the file is:
1.4. HISTORY
21
verbosity= 5
loadpath += "/Library/FreeFem++/lib"
loadpath += "/Users/hecht/Library/FreeFem++/lib"
includepath += "/Library/FreeFem++/edp"
includepath += "/Users/hecht/Library/FreeFem++/edp"
# comment
load += "funcTemplate"
load += "myfunction"
load += "MUMPS_seq"
The possible paths for this file are
under unix and MacOs
/etc/freefem++.pref
$(HOME)/.freefem++.pref
freefem++.pref
under windows
freefem++.pref
We can also use shell environment variable to change verbosity and the search rule before
the init files.
export FF_VERBOSITY=50
export FF_INCLUDEPATH="dir;;dir2"
export FF_LOADPATH="dir;;dir3""
Remark: the separator between directories must be ; and not : because : is used under
Windows.
Remark, to show the list of init of FreeFem++ , do
export FF_VERBOSITY=100; ./FreeFem++-nw
-- verbosity is set to 100
insert init-files /etc/freefem++.pref $
...
1.4
History
The project has evolved from MacFem, PCfem, written in Pascal. The first C version lead
to freefem 3.4; it offered mesh adaptativity on a single mesh only.
A thorough rewriting in C++ led to freefem+ (freefem+ 1.2.10 was its last release),
which included interpolation over multiple meshes (functions defined on one mesh can be
used on any other mesh); this software is no longer maintained but still in use because it
handles a problem description using the strong form of the PDEs. Implementing the interpolation from one unstructured mesh to another was not easy because it had to be fast and
22
CHAPTER 1. INTRODUCTION
non-diffusive; for each point, one had to find the containing triangle. This is one of the basic
problems of computational geometry (see Preparata & Shamos[23] for example). Doing it
in a minimum number of operations was the challenge. Our implementation is O(n log n)
and based on a quadtree. This version also grew out of hand because of the evolution of the
template syntax in C++.
We have been working for a few years now on FreeFem++ , entirely re-written again in
C++ with a thorough usage of template and generic programming for coupled systems of
unknown size at compile time. Like all versions of freefem it has a high level user friendly
input language which is not too far from the mathematical writing of the problems.
The freefem language allows for a quick specification of any partial differential system of
equations. The language syntax of FreeFem++ is the result of a new design which makes
use of the STL [31], templates and bison for its implementation; more detail can be found
in [17]. The outcome is a versatile software in which any new finite element can be included
in a few hours; but a recompilation is then necessary. Therefore the library of finite elements
available in FreeFem++ will grow with the version number and with the number of users
who program more new elements. So far we have discontinuous P0 elements,linear P1 and
quadratic P2 Lagrangian elements, discontinuous P1 and Raviart-Thomas elements and a
few others like bubble elements.
Chapter 2
Getting Started
To illustrate with an example, let us explain how FreeFem++ solves Poissons equation:
for a given function f (x, y), find a function u(x, y) satisfying
u(x, y) = f (x, y) for all (x, y) ,
u(x, y) = 0 for all (x, y) on , .
(2.1)
(2.2)
2
Here is the boundary of the bounded open set R2 and u = xu2 + yu2 .
The following is a FreeFem++ program which computes u when f (x, y) = xy and is the
unit disk. The boundary C = is
C = {(x, y)| x = cos(t), y = sin(t), 0 t 2}
Note that in FreeFem++ the domain is assumed to described by its boundary that is on
the left side of its boundary oriented by the parameter.
As illustrated in Fig. 2.2, we can see the isovalue of u by using plot (see line 13 below).
Example 2.1
//
defining the boundary
1: border C(t=0,2*pi){x=cos(t); y=sin(t);}
23
24
//
the triangulated domain Th is on the left side of its boundary
2: mesh Th = buildmesh (C(50));
//
the finite element space defined over Th is called here Vh
3; fespace Vh(Th,P1);
4: Vh u,v;
//
defines u and v as piecewise-P1 continuous functions
5: func f= x*y;
//
definition of a called f function
6: real cpu=clock();
//
get the clock in second
7: solve Poisson(u,v,solver=LU) =
//
defines the PDE
8:
int2d(Th)(dx(u)*dx(v) + dy(u)*dy(v))
//
bilinear part
9:
- int2d(Th)( f*v)
//
right hand side
10:
+ on(C,u=0) ;
//
Dirichlet boundary condition
11: plot(u);
12: cout << " CPU time = " << clock()-cpu << endl;
Note that the qualifier solver=LU is not required and by default a multi-frontal LU would
have been used. Note also that the lines containing clock are equally not required. Finally
note how close to the mathematics FreeFem++ input language is. Line 8 and 9 correspond
to the mathematical variational equation
Z
Z
u v u v
+
)dxdy =
f vdxdy
(
y y
Th
Th x x
for all v which are in the finite element space Vh and zero on the boundary C.
Exercise : Change P1 into P2 and run the program.
2.0.1
This first example shows how FreeFem++ executes with no effort all the usual steps required by the finite element method (FEM). Let us go through them one by one.
1st line: the
is described analytically by a parametric equation for x and for y.
Pboundary
J
When = j=0 j then each curve j , must be specified and crossings of j are not allowed
except at end points .
The keyword label can be added to define a group of boundaries for later use (boundary
conditions for instance). Hence the circle could also have been described as two half circle
with the same label:
border Gamma1(t=0,pi)
{x=cos(t); y=sin(t); label=C}
border Gamma2(t=pi,2*pi){x=cos(t); y=sin(t); label=C}
Boundaries can be referred to either by name ( Gamma1 for example) or by label ( C here)
or even by its internal number here 1 for the first half circle and 2 for the second (more
examples are in Section 5.8).
2nd line: the triangulation Th of is automatically generated by buildmesh(C(50)) using
50 points on C as in Fig. 2.1.
The domain is assumed to be on the left side of the boundary which is implicitly oriented
by the parametrization. So an elliptic hole can be added by
25
then the inside of the ellipse would be triangulated as well as the outside.
Automatic mesh generation is based on the Delaunay-Voronoi algorithm. Refinement of the
mesh are done by increasing the number of points on , for example, buildmesh(C(100)),
because inner vertices are determined by the density of points on the boundary. Mesh
adaptation can be performed also against a given function f by calling adaptmesh(Th,f).
Now the name Th (Th in FreeFem++ ) refers to the family {Tk }k=1, ,nt of triangles shown
in figure 2.1. Traditionally h refers to the mesh size, nt to the number of triangles in Th and
nv to the number of vertices, but it is seldom that we will have to use them explicitly. If is
not a polygonal domain, a skin remains between the exact domain and its approximation
t
h = nk=1
Tk . However, we notice that all corners of h = h are on .
3rd line: A finite element space is, usually, a space of polynomial functions on elements,
triangles here only, with certain matching properties at edges, vertices etc. Here fespace
Vh(Th,P1) defines Vh to be the space of continuous functions which are affine in x, y on
each triangle of Th . As it is a linear vector space of finite dimension, basis can be found.
The canonical basis is made of functions, called the hat functions k which are continuous
piecewise affine and are equal to 1 on one vertex and 0 on all others. A typical hat function
is shown on figure 2.4 1 . Then
)
(
M
X
wk k (x, y), wk are real numbers
(2.3)
Vh (Th , P1 ) = w(x, y) w(x, y) =
k=1
where M is the dimension of Vh , i.e. the number of vertices. The wk are called the degree of
freedom of w and M the number of the degree of freedom.
It is said also that the nodes of this finite element method are the vertices.
Currently FreeFem++ implements the following elements in 2d, (see section 6
for the full description)
P0 piecewise constant,
P1 continuous piecewise linear,
P2 continuous piecewise quadratic,
P3 continuous piecewise cubic, (need load "Element_P3")
P4 continuous piecewise quartic,(need load "Element_P4")
RT0 Raviart-Thomas piecewise constant,
1
The easiest way to define k is by making use of the barycentric coordinates i (x, y), i = 1, 2, 3 of a
point q = (x, y) T , defined by
X
X
i = 1,
i q i = q
i
i
where q , i = 1, 2, 3 are the 3 vertices of T . Then it is easy to see that the restriction of k on T is precisely
k .
26
2
6
1
8
4
6
3
5
8
2
7
8
5
7
3
7
4
3
4
27
M
1
X
uk k (x, y)
(2.4)
k=0
5th line: the right hand side f is defined analytically using the keyword func.
7th9th lines: defines the bilinear form of equation (2.1) and its Dirichlet boundary conditions (2.2).
This variational formulation is derived by multiplying (2.1) by v(x, y) and integrating the
result over :
Z
Z
vf dxdy
vu dxdy =
Then, by Greens formula, the problem is converted into finding u such that
a(u, v) `(f, v) = 0
v satisfying v = 0 on .
Z
Z
with a(u, v) =
u v dxdy, `(f, v) =
f v dxdy
(2.5)
(2.6)
//
In FreeFem++ bilinear terms and linear terms should not be under the same integral;
indeed to construct the linear systems FreeFem++ finds out which integral contributes to
the bilinear form by checking if both terms , the unknown (here u) and test functions (here
v) are present.
Step4: Solution and visualization
6th line: The current time in seconds is stored into the real-valued variable cpu.
7th line The problem is solved.
28
11th line: The visualization is done as illustrated in Fig. 2.2 (see Section 7.1 for zoom,
postscript and other commands).
12th line: The computing time (not counting graphics) is written on the console Notice the
C++-like syntax; the user needs not study C++ for using FreeFem++ , but it helps to
guess what is allowed in the language.
Access to matrices and vectors
Internally FreeFem++ will solve a linear system of the type
M
1
X
Z
Aij uj Fi = 0,
i = 0, , M 1;
Fi =
f i dxdy
(2.7)
j=0
which is found by using (2.4) and replacing v by i in (2.5). And the Dirichlet conditions are
implemented by penalty, namely by setting Aii = 1030 and Fi = 1030 0 if i is a boundary
degree of freedom. Note, that the number 1030 is called tgv (tr`es grande valeur ) and it is
generally possible to change this value , see the index item solve!tgv=.
The matrix A = (Aij ) is called stiffness matrix .
If the user wants to access A directly he can do so by using (see section 6.12 page 193 for
details)
varf a(u,v) = int2d(Th)( dx(u)*dx(v) + dy(u)*dy(v))
+ on(C,u=0) ;
matrix A=a(Vh,Vh);
//
stiffness matrix,
//
Note 2.1 Here u and F are finite element function, and u[] and F[] give the array of
value associated ( u[] (ui )i=0,...,M 1 and F[] (Fi )i=0,...,M 1 ). So we have
u(x, y) =
M
1
X
i=0
F(x, y) =
M
1
X
F[][i]i (x, y)
i=0
where i , i = 0..., , M 1 are the basis functions of Vh like in equation (2.3), and M =
Vh.ndof is the number of degree of freedom (i.e. the dimension of the space Vh).
The linear system (2.7) is solved by UMFPACK unless another option is mentioned specifically
as in
Vh u,v; problem Poisson(u,v,solver=CG) = int2d(...
meaning that Poisson is declared only here and when it is called (by simply writing
Poisson; ) then (2.7) will be solved by the Conjugate Gradient method.
2.0.2
29
The language of FreeFem++ is typed, polymorphic and reentrant with macro generation
(see 9.12). Every variable must be typed and declared in a statement each statement separated from the next by a semicolon ;. The syntax is that of C++ by default augmented
with something that is more akin to TEX. For the specialist, one key guideline is that
FreeFem++ rarely generates an internal finite element array; this was adopted for speed
and consequently FreeFem++ could be hard to beat in terms of execution speed, except
for the time lost in the interpretation of the language (which can be reduced by a systematic
usage of varf and matrices instead of problem.
2.1
30
Vh g = sin(pi*x + cos(pi*y));
plot(g,wait=debug);
//
Changing debug to false will make the plots flow continuously; watching the flow of
graphs on the screen (while drinking coffee) can then become a pleasant experience.
Error messages are displayed in the console window. They are not always very explicit
because of the template structure of the C++ code, (we did our best)! Nevertheless
they are displayed at the right place. For example, if you forget parenthesis as in
bool debug = true;
mesh Th = square(10,10);
plot(Th);
token ;
If you find that the program isnt doing what you want you may also use cout to
display in text format on the console window the value of variables, just as you would
do in C++.
The following example works:
...;
fespace Vh...; Vh u;...
cout<<u;...
matrix A=a(Vh,Vh);...
cout<<A;
Another trick is to comment in and out by using the // as in C++. For example
real aaa =1;
//
real aaa;
Chapter 3
Learning by Examples
This chapter is for those, like us, who dont like to read manuals. A number of simple
examples cover a good deal of the capacity of FreeFem++ and are self-explanatory. For the
modeling part this chapter continues at Chapter 9 where some PDEs of physics, engineering
and finance are studied in greater depth.
3.1
Membranes
Summary Here we shall learn how to solve a Dirichlet and/or mixed Dirichlet Neumann
problem for the Laplace operator with application to the equilibrium of a membrane under
load. We shall also check the accuracy of the method and interface with other graphics packages.
| = 0
|1 = z,
n 2
= n. Let us recall also that the Laplace operator
where 1 = 2 ; recall that
n
is defined by:
2 2
+ 2.
=
x21
x2
31
32
With such mixed boundary conditions the problem has a unique solution (see (1987),
Dautray-Lions (1988), Strang (1986) and Raviart-Thomas (1983)); the easiest proof is to
notice that is the state of least energy, i.e.
Z
1
E() = min E(v), with E(v) = ( |v|2 f v)
zV
2
and where V is the subspace of the Sobolev space H 1 () of functions which have zero trace
on 1 . Recall that (x Rd , d = 2 here)
H 1 () = {u L2 () : u (L2 ())d }
Calculus of variation shows that the minimum must satisfy, what is known as the weak form
of the PDE or its variational formulation (also known here as the theorem of virtual work)
Z
Z
w =
f w w V
Next an integration by parts (Greens formula) will show that this is equivalent to the PDE
when second derivatives exist.
WARNING Unlike freefem+ which had both weak and strong forms, FreeFem++ implements only weak formulations. It is not possible to go further in using this software if you
dont know the weak form (i.e. variational formulation) of your problem: either you read a
book, or ask help form a colleague or drop the matter. Now if you want to solve a system
of PDE like A(u, v) = 0, B(u, v) = 0 dont close this manual, because in weak form it is
Z
(A(u, v)w1 + B(u, v)w2 ) = 0 w1 , w2 ...
Example Let an ellipse have the length of the semimajor axis a = 2, and unitary the
semiminor axis Let the surface force be f = 1. Programming this case with FreeFem++
gives:
Example 3.1 (membrane.edp)
real theta=4.*pi/3.;
real a=2.,b=1.;
//
func z=x;
//
file membrane.edp
border Gamma1(t=0,theta)
{ x = a * cos(t); y = b*sin(t); }
border Gamma2(t=theta,2*pi) { x = a * cos(t); y = b*sin(t); }
mesh Th=buildmesh(Gamma1(100)+Gamma2(50));
fespace Vh(Th,P2);
Vh phi,w, f=1;
//
//
//
Plot phi
Plot Th
3.1. MEMBRANES
33
"phi.txt"
2
1.5
1
0.5
0
-0.5
-1
-1.5
-2
2
1.5
1
0.5
0
-0.5
-1
-1.5
-2
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1 -2
-1.5
-1
-0.5
0.5
1.5
Figure 3.1: Mesh and level lines of the membrane deformation. Below: the same in 3D
drawn by gnuplot from a file generated by FreeFem++ .
34
plot(phi,wait=true,fill=true);
and print the error in both cases as well as the log of their ratio an indication of the rate of
convergence.
Example 3.2 (membranerror.edp)
//
file membranerror.edp
verbosity =0;
//
to remove all default output
real theta=4.*pi/3.;
real a=1.,b=1.;
//
the length of the semimajor axis and semiminor axis
border Gamma1(t=0,theta)
{ x = a * cos(t); y = b*sin(t); }
border Gamma2(t=theta,2*pi) { x = a * cos(t); y = b*sin(t); }
func f=-4*(cos(x2+y2-1) -(x2+y2)*sin(x2+y2-1));
func phiexact=sin(x2+y2-1);
real[int] L2error(2);
//
for(int n=0;n<2;n++)
{
mesh Th=buildmesh(Gamma1(20*(n+1))+Gamma2(10*(n+1)));
fespace Vh(Th,P2);
Vh phi,w;
3.1. MEMBRANES
35
plot(Th,phi,wait=true,ps="membrane.eps");
//
L2error[n]= sqrt(int2d(Th)((phi-phiexact)2));
}
for(int n=0;n<2;n++)
cout << " L2error " << n << " = "<<
L2error[n] <<endl;
<<endl;
the output is
L2error 0 = 0.00462991
L2error 1 = 0.00117128
convergence rate = 1.9829
times: compile 0.02s, execution 6.94s
We find a rate of 1.93591, which is not close enough to the 3 predicted by the theory. The
Geometry is always a polygon so we lose one order due to the geometry approximation in
O(h2 )
Now if you are not satisfied with the .eps plot generated by FreeFem++ and you want to
use other graphic facilities, then you must store the solution in a file very much like in C++.
It will be useless if you dont save the triangulation as well, consequently you must do
{
ofstream ff("phi.txt");
ff << phi[];
}
savemesh(Th,"Th.msh");
For the triangulation the name is important: it is the extension that determines the format.
Still that may not take you where you want. Here is an interface with gnuplot to produce
the right part of figure 3.2.
//
to build a gnuplot data file
{ ofstream ff("graph.txt");
for (int i=0;i<Th.nt;i++)
{ for (int j=0; j <3; j++)
ff<<Th[i][j].x << "
"<< Th[i][j].y<< " "<<phi[][Vh(i,j)]<<endl;
ff<<Th[i][0].x << "
"<< Th[i][0].y<< " "<<phi[][Vh(i,0)]<<"\n\n\n"
}
}
We use the finite element numbering, where Wh(i,j) is the global index of j T h degrees of
freedom of triangle number i.
Then open gnuplot and do
set palette rgbformulae 30,31,32
splot "graph.txt" w l pal
This works with P2 and P1, but not with P1nc because the 3 first degrees of freedom of
P2 or P2 are on vertices and not with P1nc.
36
3.2
Heat Exchanger
Summary Here we shall learn more about geometry input and triangulation files, as well
as read and write operations.
The problem Let {Ci }1,2 , be 2 thermal conductors within an enclosure C0 . The first one is
held at a constant temperature u1 the other one has a given thermal conductivity 2 5 times
larger than the one of C0 . We assume that the border of enclosure C0 is held at temperature
20 C and that we have waited long enough for thermal equilibrium.
In order to know u(x) at any point x of the domain , we must solve
(u) = 0 in ,
u| = g
where is the interior of C0 minus the conductors C1 and is the boundary of , that is
C0 C1 Here g is any function of x equal to ui on Ci . The second equation is a reduced form
for:
u = ui on
Ci ,
i = 0, 1.
The variational formulation for this problem is in the subspace H01 () H 1 () of functions
which have zero traces on .
Z
1
u g H0 () :
uv = 0 v H01 ()
Let us assume that C0 is a circle of radius 5 centered at the origin, Ci are rectangles, C1
being at the constant temperature u1 = 60 C.
Example 3.3 (heatex.edp)
//
file heatex.edp
int C1=99, C2=98;
//
could be anything such that 6= 0 and C1 6= C2
border C0(t=0,2*pi){x=5*cos(t); y=5*sin(t);}
border
border
border
border
C11(t=0,1){
C12(t=0,1){
C13(t=0,1){
C14(t=0,1){
x=1+t;
x=2;
x=2-t;
x=1;
y=3;
y=3-6*t;
y=-3;
y=-3+6*t;
label=C1;}
label=C1;}
label=C1;}
label=C1;}
border
border
border
border
C21(t=0,1){
C22(t=0,1){
C23(t=0,1){
C24(t=0,1){
x=-2+t;
x=-1;
x=-1-t;
x=-2;
y=3;
y=3-6*t;
y=-3;
y=-3+6*t;
label=C2;}
label=C2;}
label=C2;}
label=C2;}
plot(
C0(50)
//
+ C11(5)+C12(20)+C13(5)+C14(20)
+ C21(-5)+C22(-20)+C23(-5)+C24(-20),
wait=true, ps="heatexb.eps");
mesh Th=buildmesh(
plot(Th,wait=1);
C0(50)
+ C11(5)+C12(20)+C13(5)+C14(20)
+ C21(-5)+C22(-20)+C23(-5)+C24(-20));
37
IsoValue
15.7895
22.1053
26.3158
30.5263
34.7368
38.9474
43.1579
47.3684
51.5789
55.7895
60
64.2105
68.4211
72.6316
76.8421
81.0526
85.2632
89.4737
93.6842
104.211
Exercise Use the symmetry of the problem with respect to the axes; triangulate only one
half of the domain, and set Dirichlet conditions on the vertical axis, and Neumann conditions
on the horizontal axis.
38
Writing and reading triangulation files Suppose that at the end of the previous program
we added the line
savemesh(Th,"condensor.msh");
and then later on we write a similar program but we wish to read the mesh from that file.
Then this is how the condenser should be computed:
mesh Sh=readmesh("condensor.msh");
fespace Wh(Sh,P1); Wh us,vs;
solve b(us,vs)= int2d(Sh)(dx(us)*dx(vs)+dy(us)*dy(vs))
+on(1,us=0)+on(99,us=1)+on(98,us=-1);
plot(us);
Note that the names of the boundaries are lost but either their internal number (in the case
of C0) or their label number (for C1 and C2) are kept.
3.3
Acoustics
Summary Here we go to grip with ill posed problems and eigenvalue problems
Pressure variations in air at rest are governed by the wave equation:
2u
c2 u = 0.
2
t
When the solution wave is monochromatic (and that depend on the boundary and initial
conditions), u is of the form u(x, t) = Re(v(x)eikt ) where v is a solution of Helmholtzs
equation:
k 2 v + c2 v = 0 in ,
v
| = g.
n
(3.1)
where g is the source. Note the + sign in front of the Laplace operator and that k > 0 is
real. This sign may make the problem ill posed for some values of kc , a phenomenon called
resonance.
At resonance there are non-zero solutions even when g = 0. So the following program may
or may not work:
Example 3.4 (sound.edp)
real kc2=1;
func g=y*(1-y);
border
border
border
border
border
border
border
a0(t=0,1)
a1(t=0,1)
a2(t=0,1)
a3(t=0,1)
a4(t=0,1)
a5(t=0,1)
a6(t=0,1)
{
{
{
{
{
{
{
x= 5; y= 1+2*t ;}
x=5-2*t; y= 3 ;}
x= 3-2*t; y=3-2*t ;}
x= 1-t; y= 1 ;}
x= 0; y= 1-t ;}
x= t; y= 0 ;}
x= 1+4*t; y= t ;}
//
file sound.edp
39
Results are on Figure 3.3. But when kc2 is an eigenvalue of the problem, then the solution
is not unique: if ue 6= 0 is an eigen state, then for any given solution u + ue is another a
solution. To find all the ue one can do the following
real sigma = 20;
//
value of the shift
//
OP = A - sigma B ; // the shifted matrix
varf op(u1,u2)= int2d(Th)( dx(u1)*dx(u2) + dy(u1)*dy(u2) - sigma* u1*u2 );
varf b([u1],[u2]) = int2d(Th)( u1*u2 ) ;
//
no Boundary condition see note
9.1
matrix OP= op(Vh,Vh,solver=Crout,factorize=1);
matrix B= b(Vh,Vh,solver=CG,eps=1e-20);
int nev=2;
//
real[int] ev(nev);
Vh[int] eV(nev);
int k=EigenValue(OP,B,sym=true,sigma=sigma,value=ev,vector=eV,
tol=1e-10,maxit=0,ncv=0);
cout<<ev(0)<<" 2 eigen values "<<ev(1)<<endl;
v=eV[0];
plot(v,wait=1,ps="eigen.eps");
3.4
Thermal Conduction
Summary Here we shall learn how to deal with a time dependent parabolic problem. We
shall also show how to treat an axisymmetric problem and show also how to deal with a
nonlinear problem.
How air cools a plate We seek the temperature distribution in a plate (0, Lx) (0, Ly)
(0, Lz) of rectangular cross section = (0, 6) (0, 1); the plate is surrounded by air at
temperature ue and initially at temperature u = u0 + Lx u1 . In the plane perpendicular
to the plate at z = Lz/2, the temperature varies little with the coordinate z; as a first
approximation the problem is 2D.
We must solve the temperature equation in in a time interval (0,T).
t u (u) = 0 in (0, T ),
u(x, y, 0) = u0 + xu1
u
+ (u ue ) = 0 on (0, T ).
n
(3.2)
40
Figure 3.3: Left:Amplitude of an acoustic signal coming from the left vertical wall. Right:
first eigen state ( = (k/c)2 = 19.4256) close to 20 of eigenvalue problem : = and
= 0 on
n
Here the diffusion will take two values, one below the middle horizontal line and ten times
less above, so as to simulate a thermostat. The term (u ue ) accounts for the loss of
temperature by convection in air. Mathematically this boundary condition is of Fourier (or
Robin, or mixed) type.
The variational formulation is in L2 (0, T ; H 1 ()); in loose terms and after applying an
implicit Euler finite difference approximation in time; we shall seek un (x, y) satisfying for all
w H 1 ():
Z
Z
un un1
n
(
w + u w) + (un uu e)w = 0
t
func u0 =10+90*x/6;
func k = 1.8*(y<0.5)+0.2;
real ue = 25, alpha=0.25, T=5, dt=0.1 ;
mesh Th=square(30,5,[6*x,y]);
fespace Vh(Th,P1);
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);
ofstream ff("thermic.dat");
for(real t=0;t<T;t+=dt){
uold=u;
//
uold un1 = un u
thermic; //
here solve the thermic problem
ff<<u(3,0.5)<<endl;
41
plot(u);
}
Notice that we must separate by hand the bilinear part from the linear one.
Notice also that the way we store the temperature at point (3,0.5) for all times in file
thermic.dat. Should a one dimensional plot be required, the same procedure can be
(x, 0.9) one would do
used. For instance to print x 7 u
y
for(int i=0;i<20;i++) cout<<dy(u)(6.0*i/20.0,0.9)<<endl;
56
"thermic.dat"
54
52
50
48
46
44
42
40
38
36
34
0
0.5
1.5
2.5
3.5
4.5
Figure 3.4: Temperature at T=4.9. Right: decay of temperature versus time at x=3, y=0.5
3.4.1
Let us now deal with a cylindrical rod instead of a flat plate. For simplicity we take = 1.
In cylindrical coordinates, the Laplace operator becomes (r is the distance to the axis, z is
the distance along the axis, polar angle in a fixed plane perpendicular to the axis):
1
1 2
2
u = r (rr u) + 2
u + zz
.
r
r
Symmetry implies that we loose the dependence with respect to ; so the domain is again
a rectangle ]0, R[]0, |[ . We take the convention of numbering of the edges as in square()
(1 for the bottom horizontal ...); the problem is now:
rt u r (rr u) z (rz u) = 0 in ,
z
u(t = 0) = u0 + (u1 u)
Lz
u|4 = u0 ,
u|2 = u1 ,
(u ue ) +
u
| = 0.
n 1 3
(3.3)
42
Notice that the bilinear form degenerates at x = 0. Still one can prove existence and
uniqueness for u and because of this degeneracy no boundary conditions need to be imposed
on 1 .
3.4.2
Heat loss through radiation is a loss proportional to the absolute temperature to the fourth
power (Stefans Law). This adds to the loss by convection and gives the following boundary
condition:
u
+ (u ue ) + c[(u + 273)4 (ue + 273)4 ] = 0
n
The problem is nonlinear, and must be solved iteratively. If m denotes the iteration index,
a semi-linearization of the radiation condition gives
um+1
+ (um+1 ue ) + c(um+1 ue )(um + ue + 546)((um + 273)2 + (ue + 273)2 ) = 0,
n
because we have the identity a4 b4 = (a b)(a + b)(a2 + b2 ). The iterative process will
work with v = u ue .
...
fespace Vh(Th,P1);
//
finite element space
real rad=1e-8, uek=ue+273;
//
def of the physical constants
Vh vold,w,v=u0-ue,b;
problem thermradia(v,w)
= int2d(Th)(v*w/dt + k*(dx(v) * dx(w) + dy(v) * dy(w)))
+ int1d(Th,1,3)(b*v*w)
- int2d(Th)(vold*w/dt) + on(2,4,v=u0-ue);
for(real t=0;t<T;t+=dt){
vold=v;
for(int m=0;m<5;m++){
b= alpha + rad * (v + 2*uek) * ((v+uek)2 + uek2);
thermradia;
}
}
vold=v+ue; plot(vold);
3.5
Summary Here we will learn how to deal with a multi-physics system of PDEs on a Complex
geometry, with multiple meshes within one problem. We also learn how to manipulate the
region indicator and see how smooth is the projection operator from one mesh to another.
43
Incompressible flow Without viscosity and vorticity incompressible flows have a velocity
given by:
!
u=
x2
x
1
where is solution of
= 0
Airfoil Let us consider a wing profile S in a uniform flow. Infinity will be represented by
a large circle C where the flow is assumed to be of uniform velocity; one way to model this
problem is to write
= 0 in ,
|S = 0,
|C = u y,
(3.4)
where = C S
The NACA0012 Airfoil An equation for the upper surface of a NACA0012 (this is a classical wing profile in aerodynamics) is:
//
file potential.edp
real S=99;
border C(t=0,2*pi) { x=5*cos(t); y=5*sin(t);}
border Splus(t=0,1){ x = t; y = 0.17735*sqrt(t)-0.075597*t
- 0.212836*(t2)+0.17363*(t3)-0.06254*(t4); label=S;}
border Sminus(t=1,0){ x =t; y= -(0.17735*sqrt(t)-0.075597*t
-0.212836*(t2)+0.17363*(t3)-0.06254*(t4)); label=S;}
mesh Th= buildmesh(C(50)+Splus(70)+Sminus(70));
fespace Vh(Th,P2); Vh psi,w;
solve potential(psi,w)=int2d(Th)(dx(psi)*dx(w)+dy(psi)*dy(w))+
on(C,psi = y) + on(S,psi=0);
plot(psi,wait=1);
44
Figure 3.5: Zoom around the NACA0012 airfoil showing the streamlines (curve = constant). To obtain such a plot use the interactive graphic command: + and p. Right:
temperature distribution at time T=25 (now the maximum is at 90 instead of 120). Note
that an incidence angle has been added here (see Chapter 9).
3.5.1
Now let us assume that the airfoil is hot and that air is there to cool it. Much like in the
previous section the heat equation for the temperature v is
t v (v) + u v = 0, v(t = 0) = v0 ,
v
|C = 0
n
But now the domain is outside AND inside S and takes a different value in air and in steel.
Furthermore there is convection of heat by the flow, hence the term u v above. Consider
the following, to be plugged at the end of the previous program:
...
border D(t=0,2){x=1+t;y=0;}
//
added to have a fine mesh at trail
mesh Sh = buildmesh(C(25)+Splus(-90)+Sminus(-90)+D(200));
fespace Wh(Sh,P1); Wh v,vv;
int steel=Sh(0.5,0).region, air=Sh(-1,0).region;
fespace W0(Sh,P0);
W0 k=0.01*(region==air)+0.1*(region==steel);
W0 u1=dy(psi)*(region==air), u2=-dx(psi)*(region==air);
Wh vold = 120*(region==steel);
real dt=0.05, nbT=50;
int i;
problem thermic(v,vv,init=i,solver=LU)= int2d(Sh)(v*vv/dt
+ k*(dx(v) * dx(vv) + dy(v) * dy(vv))
+ 10*(u1*dx(v)+u2*dy(v))*vv)- int2d(Sh)(vold*vv/dt);
for(i=0;i<nbT;i++){
v=vold; thermic;
plot(v);
45
Notice here
how steel and air are identified by the mesh parameter region which is defined when
buildmesh is called and takes an integer value corresponding to each connected component of ;
how the convection terms are added without upwinding. Upwinding is necessary when
the Pecley number |u|L/ is large (here is a typical length scale), The factor 10 in
front of the convection terms is a quick way of multiplying the velocity by 10 (else it
is too slow to see something).
The solver is Gauss LU factorization and when init6= 0 the LU decomposition is
reused so it is much faster after the first iteration.
3.6
Summary Here we will present two methods for upwinding for the simplest convection
problem. We will learn about Characteristics-Galerkin and Discontinuous-Galerkin Finite
Element Methods.
Let be the unit disk centered at 0; consider the rotation vector field
u = [u1, u2],
u1 = y,
u2 = x.
Pure convection by u is
t c + u.c = 0 in (0, T ) c(t = 0) = c0 in .
The exact solution c(xt , t) at time t en point xt is given by
c(xt , t) = c0 (x, 0)
where xt is the particle path in the flow starting at point x at time 0. So xt are solutions of
xt = u(xt ),
xt=0 = x,
where xt =
d(t 7 xt )
dt
The ODE are reversible and we want the solution at point x at time t ( not at point xt ) the
initial point is xt , and we have
c(x, t) = c0 (xt , 0)
The game consists in solving the equation until T = 2, that is for a full revolution and to
compare the final solution with the initial one; they should be equal.
46
x =0 = x.
When u is piecewise constant; this is possible because x is then a polygonal curve which
can be computed exactly and the solution exists always when u is divergence free; convect
returns c(xdf ) = C X.
Example 3.6 (convects.edp)
//
file convects.edp
c[].max);
Remark 4 3D plots can be done by adding the qualifyer dim=3 to the plot instruction.
The method is very powerful but has two limitations: a/ it is not conservative, b/ it may
diverge in rare cases when |u| is too small due to quadrature error.
Solution by Discontinuous-Galerkin FEM Discontinuous Galerkin methods take advantage
of the discontinuities of c at the edges to build upwinding. There are may formulations
possible. We shall implement here the so-called dual-P1DC formulation (see Ern[16]):
Z
Z
Z n+1
1
c
cn
|n u|cw w
+ u c)w + (|n u| n u)[c]w =
(
t
2
E
E
where E is the set of inner edges and E is the set of boundary edges where u n < 0 (in our
case there is no such edges). Finally [c] is the jump of c across an edge with the convention
that c+ refers to the value on the right of the oriented edge.
Example 3.7 (convects end.edp)
//
file convects.edp
...
fespace Vh(Th,P1dc);
Vh w, ccold, v1 = y, v2 = -x, cc = exp(-10*((x-0.3)2 +(y-0.3)2));
real u, al=0.5; dt = 0.05;
47
Notice the new keywords, intalledges to integrate on all edges of all triangles
XZ
intalledges(Th)
T Th
(3.5)
(so all internal edges are see two times ), nTonEdge which is one if the triangle has a boundary
edge and two otherwise, jump to implement [c]. Results of both methods are shown on Figure
3.6 with identical levels for the level line; this is done with the plot-modifier viso.
Notice also the macro where the parameter u is not used (but the syntax needs one) and
which ends with a //; it simply replaces the name n by (N.x*v1+N.y*v2). As easily
guessed N.x,N.y is the normal to the edge.
IsoValue
-0.1
0
0.5
0.1
0.5
0.2
0.25
0.3
0.35
0.4
0.45
0.5
0.55
0.6
0.65
0.7
0.75
0.8
0.9
1
IsoValue
-0.1
0
0.5
0.1
0.5
0.2
0.25
0.3
0.35
0.4
0.45
0.5
0.55
0.6
0.65
0.7
0.75
0.8
0.9
1
Figure 3.6: The rotated hill after one revolution, left with Characteristics-Galerkin, on the
right with Discontinuous P1 Galerkin FEM.
48
//
the same DG very much faster
varf aadual(cc,w) = int2d(Th)((cc/dt+(v1*dx(cc)+v2*dy(cc)))*w)
+ intalledges(Th)((1-nTonEdge)*w*(al*abs(n)-n/2)*jump(cc));
varf bbdual(ccold,w) = - int2d(Th)(ccold*w/dt);
matrix AA= aadual(Vh,Vh);
matrix BB = bbdual(Vh,Vh);
set (AA,init=t,solver=sparsesolver);
Vh rhs=0;
for ( t=0; t< 2*pi ; t+=dt)
{
ccold=cc;
rhs[] = BB* ccold[];
cc[] = AA-1*rhs[];
plot(cc,fill=0,cmm="t="+t + ", min=" + cc[].min + ", max=" + cc[].max);
};
Notice the new keyword set to specify a solver in this framework; the modifier init is used
to tel the solver that the matrix has not changed (init=true), and the name parameter are
the same that in problem definition (see. 6.9) .
Finite Volume Methods can also be handled with FreeFem++ but it requires programming. For instance the P0 P1 Finite Volume Method of Dervieux et al associates to each
P0 function c1 a P0 function c0 with constant value around each vertex q i equal to c1 (q i ) on
the cell i made by all the medians of all triangles having q i as vertex. Then upwinding is
done by taking left or right values at the median:
Z
Z
1 1 n+1
1n
(c
c )+
u nc = 0 i
i
i t
It can be programmed as
load "mat_dervieux";
//
external module in C++ must be loaded
border a(t=0, 2*pi){ x = cos(t); y = sin(t); }
mesh th = buildmesh(a(100));
fespace Vh(th,P1);
Vh vh,vold,u1 = y, u2 = -x;
Vh v = exp(-10*((x-0.3)2 +(y-0.3)2)), vWall=0, rhs =0;
real dt = 0.025;
//
qf1pTlump means mass lumping is used
FVM(v,vh) = int2d(th,qft=qf1pTlump)(v*vh/dt)
- int2d(th,qft=qf1pTlump)(vold*vh/dt)
+ int1d(th,a)(((u1*N.x+u2*N.y)<0)*(u1*N.x+u2*N.y)*vWall*vh)
+ rhs[] ;
problem
matrix A;
MatUpWind0(A,th,vold,[u1,u2]);
for ( int t=0; t< 2*pi ; t+=dt){
vold=v;
rhs[] = A * vold[] ; FVM;
49
plot(v,wait=0);
};
the mass lumping parameter forces a quadrature formula with Gauss points at the vertices
so as to make the mass matrix diagonal; the linear system solved by a conjugate gradient
method for instance will then converge in one or two iterations.
The right hand side rhs is computed by an external C++ function MatUpWind0(...)
which is programmed as
int
//
computes matrix a on a triangle for the Dervieux FVM
fvmP1P0(double q[3][2],
//
the 3 vertices of a triangle T
double u[2],
//
convection velocity on T
double c[3],
//
the P1 function on T
double a[3][3],
//
output matrix
double where[3] )
//
where>0 means were on the boundary
{
for(int i=0;i<3;i++) for(int j=0;j<3;j++) a[i][j]=0;
for(int i=0;i<3;i++){
int ip = (i+1)%3, ipp =(ip+1)%3;
double unL =-((q[ip][1]+q[i][1]-2*q[ipp][1])*u[0]
-(q[ip][0]+q[i][0]-2*q[ipp][0])*u[1])/6;
if(unL>0) { a[i][i] += unL; a[ip][i]-=unL;}
else{ a[i][ip] += unL; a[ip][ip]-=unL;}
if(where[i]&&where[ip]){
//
this is a boundary edge
unL=((q[ip][1]-q[i][1])*u[0] -(q[ip][0]-q[i][0])*u[1])/2;
if(unL>0) { a[i][i]+=unL; a[ip][ip]+=unL;}
}
}
return 1;
}
It must be inserted into a larger .cpp file, shown in Appendix A, which is the load module
linked to FreeFem++ .
3.7
Elasticity Solid objects deform under the action of applied forces: a point in the solid,
originally at (x, y, z) will come to (X, Y, Z) after some time; the vector u = (u1 , u2 , u3 ) =
(X x, Y y, Z z) is called the displacement. When the displacement is small and the
solid is elastic, Hookes law gives a relationship between the stress tensor (u) = (ij (u))
and the strain tensor (u) = ij (u)
ij (u) = ij .u + 2ij (u),
where the Kronecker symbol ij = 1 if i = j, 0 otherwise, with
1 ui uj
+
),
ij (u) = (
2 xj
xi
50
and where , are two constants that describe the mechanical properties of the solid, and
are themselves related to the better known constants E, Youngs modulus, and , Poissons
ratio:
E
E
=
, =
.
2(1 + )
(1 + )(1 2)
Lames system Let us consider a beam with axis Oz and with perpendicular section . The
components along x and y of the strain u(x) in a section subject to forces f perpendicular
to the axis are governed by
u ( + )(.u) = f in ,
where , are the Lame coefficients introduced above.
Remark, we do not used this equation because the associated variationnal form does not
give the right boundary condition, we simply use
div() = f
in
P
where : denote the tensor scalar product, i.e. a : b = i,j aij bij .
So the variationnal form can be written as :
Z
Z
.u.v + 2(u) : (v) dx vf dx = 0;
Example Consider elastic plate with the undeformed rectangle shape [0, 20] [1, 1]. The
body force is the gravity force f and the boundary force g is zero on lower, upper and right
sides. The left vertical sides of the beam is fixed. The boundary conditions are
.n = g = 0 on 1 , 4 , 3 ,
u = 0 on 2
Here u = (u, v) has two components.
The above two equations are strongly coupled by their mixed derivatives, and thus any
iterative solution on each of the components is risky. One should rather use FreeFem++ s
system approach and write:
Example 3.8 (lame.edp)
mesh Th=square(10,10,[20*x,2*y-1]);
fespace Vh(Th,P2);
Vh u,v,uu,vv;
real sqrt2=sqrt(2.);
macro epsilon(u1,u2) [dx(u1),dy(u2),(dy(u1)+dx(u2))/sqrt2]
//
file lame.edp
//
EOM
51
epsilon(u1,u2)* epsilon(v1,v2)
//
EOM
//
max
x = "<< dxmin<< " y=" << dymin << endl;
(20,0) = " << u(20,0) << " " << v(20,0) << endl;
The numerical results are shown on figure 3.7 and the output is:
-- square mesh : nb vertices =121 , nb triangles = 200 ,
-- Solve :
min -0.00174137 max 0.00174105
min -0.0263154 max 1.47016e-29
- dep. max
x = -0.00174137 y=-0.0263154
dep. (20,0) = -1.8096e-07 -0.0263154
times: compile 0.010219s, execution 1.5827s
3.8
nb boundary edges 40
In the case of a flow invariant with respect to the third coordinate (two-dimensional flow),
flows at low Reynolds number (for instance micro-organisms) satisfy,
u + p = 0
u=0
where u = (u1 , u2 ) is the fluid velocity and p its pressure.
The driven cavity is a standard test. It is a box full of liquid with its lid moving horizontally
at speed one. The pressure and the velocity must be discretized in compatible fintie element
spaces for the LBB conditions to be satisfied:
sup
pPh
(u, p)
|u| u Uh
|p|
//
file stokes.edp
52
Figure 3.7: Solution of Lames equations for elasticity for a 2D beam deflected by its own
weight and clamped by its left vertical side; result are shown with a amplification factor
equal to 100. Remark: the size of the arrow is automatically bound, but the color gives the
real length
int n=3;
mesh Th=square(10*n,10*n);
fespace Uh(Th,P1b); Uh u,v,uu,vv;
fespace Ph(Th,P1); Ph p,pp;
solve stokes([u,v,p],[uu,vv,pp]) =
int2d(Th)(dx(u)*dx(uu)+dy(u)*dy(uu) + dx(v)*dx(vv)+ dy(v)*dy(vv)
+ dx(p)*uu + dy(p)*vv + pp*(dx(u)+dy(v))
- 1e-10*p*pp)
+ on(1,2,4,u=0,v=0) + on(3,u=1,v=0);
plot([u,v],p,wait=1);
Remark, we add a stabilization term -10e-10*p*pp to fixe the constant part of the pressure.
Results are shown on figure 3.8
3.9
Summary Fluid flows require good algorithms and good triangultions. We show here an
example of a complex algorithm and or first example of mesh adaptation.
An incompressible viscous fluid satisfies:
t u + u u + p u = 0,
u|t=0 = u0 ,
u=0
u| = u .
in ]0, T [,
53
Figure 3.8: Solution of Stokes equations for the driven cavity problem, showing the velocity
field and the pressure level lines.
u| = u , n u|out = 0
where uoX(x) = u(x u(x)t) since t u + u u is approximated by the method of characteristics, as in the previous section.
We use the s Chorins algorithm with free boundary condition at outlet (i.e. p = 0, n u = 0),
to compute a correction, q, to the pressure.
q = u,
q = 0 on out
and define
um+1 = u + P qt,
pm+1 = pm q
where u is the (um+1 , v m+1 ) of Chorins algorithm, and where P is the L2 projection with
mass lumping ( a sparse matrix).
The backward facing step The geometry is that of a channel with a backward facing step
so that the inflow section is smaller than the outflow section. This geometry produces a fluid
recirculation zone that must be captured correctly.
This can only be done if the triangulation is sufficiently fine, or well adapted to the flow.
Remark (FH), The are a technical difficulty is the example, is the output B.C., here we put
p = 0 and n u = 0.
54
//
file NSprojection.edp
//
Version july 2014,
FH. Change B.C on u on outpout , more simple ..
//
............
verbosity=0;
border a0(t=1,0){ x=-2;
y=t;
label=1;}
border a1(t=-2,0){ x=t;
y=0;
label=2;}
border a2(t=0,-0.5){ x=0;
y=t;
label=2;}
border a3(t=0,1){ x=18*t1.2; y=-0.5;
label=2;}
border a4(t=-0.5,1){ x=18;
y=t;
label=3;}
border a5(t=1,0){ x=-2+20*t; y=1;
label=4;}
int n=1;
mesh Th= buildmesh(a0(3*n)+a1(20*n)+a2(10*n)+a3(150*n)+a4(5*n)+a5(100*n));
plot(Th);
fespace Vh(Th,P1);
real nu = 0.0025, dt = 0.2;
//
Reynolds=200
//
//
Eps CG ..
matrix dtM1x,dtM1y;
macro BuildMat()
{ /* for memory managenemt */
varf vM(unused,v) = int2d(Th)(v) ;
varf vdx(u,v) = int2d(Th)(v*dx(u)*dt) ;
varf vdy(u,v) = int2d(Th)(v*dy(u)*dt) ;
real[int] Mlump = vM(0,Vh);
real[int] one(Vh.ndof); one = 1;
real[int] M1 = one ./ Mlump;
matrix dM1 = M1;
matrix Mdx = vdx(Vh,Vh);
matrix Mdy = vdy(Vh,Vh);
dtM1x = dM1*Mdx;
dtM1y = dM1*Mdy;
}//
EOF
BuildMat
real err=1, outflux=1;
for(int n=0;n<300;n++)
{
Vh uold = u, vold = v, pold=p;
solve pb4u(u,w,init=n,solver=CG,eps=epsu)
=int2d(Th)(u*w/dt +nu*(dx(u)*dx(w)+dy(u)*dy(w)))
-int2d(Th)((convect([uold,vold],-dt,uold)/dt-dx(p))*w)
+ on(1,u = 4*y*(1-y)) + on(2,4,u = 0) ;
//
Neuman on Gamma3
plot(u);
solve pb4v(v,w,init=n,solver=CG,eps=epsv)
55
= int2d(Th)(v*w/dt +nu*(dx(v)*dx(w)+dy(v)*dy(w)))
-int2d(Th)((convect([uold,vold],-dt,vold)/dt-dy(p))*w)
+on(1,2,3,4,v = 0);
solve pb4p(q,w,solver=CG,init=n,eps=epsp) = int2d(Th)(dx(q)*dx(w)+dy(q)*dy(w))
- int2d(Th)((dx(u)+ dy(v))*w/dt)+ on(3,q=0);
//
epsv = -abs(epsv);
epsu = -abs(epsu);
epsp = -abs(epsp);
p = pold-q;
u[] += dtM1x*q[];
v[] += dtM1y*q[];
if(n%50==49){
Th = adaptmesh(Th,[u,v],q,err=0.04,nbvx=100000);
plot(Th, wait=true);
BuildMat
//
rebuild mat.
}
err = sqrt(int2d(Th)(square(u-uold)+square(v-vold))/Th.area) ;
outflux = int1d(Th)( [u,v]*[N.x,N.y]) ;
cout << " iter " << n << " Err L2 = " << err
<< " flux sortant = "<< outflux << endl;
if(err < 1e-3) break;
}
assert(abs(outflux)< 2e-3);
plot(p,wait=1,ps="NSprojP.eps");
plot(u,wait=1,ps="NSprojU.eps");
//
verifaction ...
Figure 3.9: Rannachers projection algorithm: result on an adapted mesh (top) showing the
pressure (middle) and the horizontal velocity u at Reynolds 400.
We show in figure 3.9 the numerical results obtained for a Reynolds number of 400 where
mesh adaptation is done after 50 iterations on the first mesh.
56
3.10
The problem is find the velocity field u = (ui )di=1 and the pressure p of a Flow satisfying in
the domain Rd (d = 2, 3):
(u )u u + p = 0,
u = 0
where is the viscosity of the fluid, = (i )di=1 , the dot product is , and = with
the some boundary conditions ( u is given on )
The weak form is find u, p such than for v (zero on ), and q
Z
((u )u).v + u : v p v q u = 0
(3.6)
F (u, p) =
Z
DF (u, p)(u, p) =
+ u : v p v q u
stabilization term
57
nu*(Grad(u1,u2)*Grad(v1,v2) )
+ UgradV(u1,u2, u1, u2)*[v1,v2]
- div(u1,u2)*q - div(v1,v2)*p
)
+ on(1,du1=0,du2=0) ;
u1[] -= du1[]; u2[] -= du2[]; p[] -= dp[];
err= du1[].linfty + du2[].linfty + dp[].linfty;
if(err < eps) break;
if( n>3 && err > 10.) break;
//
blowup ????
//
//
//
func bb=[[-1,-2],[4,2]];
/ FE Space Taylor Hood
fespace Xh(Th,P2);
fespace Mh(Th,P1);
Xh u1,u2,v1,v2,du1,du2,u1p,u2p;
Mh p,q,dp,pp;
//
//
//
for volicity
for pressure
u1 = ( x2+y2) > 2;
u2=0;
Finally we use trick to make continuation on the viscosity , because the Newton method
blowup owe start with the final viscosity
//
Physical parameter
real eps=1e-6;
verbosity=0;
while(1)
{
int n;
real err=0;
//
//
Loop on viscosity
58
algo here
Figure 3.10: Mesh and the velocity and pressure at Reynolds 200
3.11
59
(3.7)
We use a time discretization which preserves positivity and uses the method of characteristics
(X m (x) x um (x)t)
1 m+1
m+1
(
m X m ) (m
)=0
T
t
1 m+1
m+1
(u
um X m ) (m
) + pm+1 + e(m+1 0 )e2 , um+1 = 0
T u
t
1 m+1
m
m
m+1
(k
k m X m ) + k m+1 m (m
) = T |um + umT |2
T k
t
km
2
c m m+1
c1
1 m+1
m
m
m+1
(
X ) + c2
(T
) = k m |um + umT |2
m
t
k
c
2
m+1 2
k
m+1
= c m+1 , m+1
= m+1
(3.8)
T
T
T
In variational form and with appropriated boundary conditions the problem is:
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 ;}
int n=8;
mesh Th=buildmesh(aa(n)+bb(9*n) + cc(4*n) + dd(10*n)+ee(6*n) + ff(n));
real s0=clock();
fespace Vh2(Th,P1b);
fespace Vh(Th,P1);
fespace V0h(Th,P0);
Vh2 u2,v2,up1=0,up2=0;
Vh2 u1,v1;
Vh u1x=0,u1y,u2x,u2y, vv;
//
//
//
velocity space
pressure space
for gradients
real reylnods=500;
//
cout << " Enter the reynolds number :"; cin >> reylnods;
assert(reylnods>1 && reylnods < 100000);
60
up1=0;
up2=0;
func g=(x)*(1-x)*4;
//
inflow
Vh p=0,q, temp1,temp=35, k=0.001,k1,ep=0.0001,ep1;
V0h muT=1,prodk,prode, kappa=0.25e-4, stress;
real alpha=0, eee=9.81/303, c1m = 1.3/0.09 ;
real nu=1, numu=nu/sqrt( 0.09), nuep=pow(nu,1.5)/4.1;
int i=0,iter=0;
real dt=0;
problem TEMPER(temp,q) =
//
temperature equation
int2d(Th)(
alpha*temp*q + kappa * ( dx(temp)*dx(q) + dy(temp)*dy(q) ))
//
+ int1d(Th,aa,bb)(temp*q* 0.1)
+ int2d(Th) ( -alpha*convect([up1,up2],-dt,temp1)*q )
+ on(ff,temp=25)
+ on(aa,bb,temp=35) ;
problem kine(k,q)=
//
get the kinetic turbulent energy
int2d(Th)(
(ep1/k1+alpha)*k*q + muT * ( dx(k)*dx(q) + dy(k)*dy(q) ))
//
+ int1d(Th,aa,bb)(temp*q*0.1)
+ int2d(Th) ( prodk*q-alpha*convect([up1,up2],-dt,k1)*q )
+ on(ff,k=0.0001) + on(aa,bb,k=numu*stress) ;
problem viscturb(ep,q)=
//
get the rate of turbulent viscous energy
int2d(Th)(
(1.92*ep1/k1+alpha)*ep*q + c1m*muT * ( dx(ep)*dx(q) + dy(ep)*dy(q)
))
//
+ int1d(Th,aa,bb)(temp*q*0.1)
+ int2d(Th) ( prode*q-alpha*convect([up1,up2],-dt,ep1)*q )
+ on(ff,ep= 0.0001) + on(aa,bb,ep=nuep*pow(stress,1.5)) ;
solve NS ([u1,u2,p],[v1,v2,q]) = //
Navier-Stokes k-epsilon and Boussinesq
int2d(Th)(
alpha*( u1*v1 + u2*v2)
+ muT * (dx(u1)*dx(v1)+dy(u1)*dy(v1)+dx(u2)*dx(v2)+dy(u2)*dy(v2))
//
( 2*dx(u1)*dx(v1) + 2*dy(u2)*dy(v2)+(dy(u1)+dx(u2))*(dy(v1)+dx(v2)))
+ p*q*(0.000001)
- p*dx(v1) - p*dy(v2)
- dx(u1)*q - dy(u2)*q
)
+ int1d(Th,aa,bb,dd)(u1*v1* 0.1)
+ int2d(Th) (eee*(temp-35)*v1 -alpha*convect([up1,up2],-dt,up1)*v1
-alpha*convect([up1,up2],-dt,up2)*v2 )
+ on(ff,u1=3,u2=0)
+ on(ee,u1=0,u2=0)
+ on(aa,dd,u2=0)
+ on(bb,u2= -up1*N.x/N.y)
+ on(cc,u2=0) ;
plot(coef=0.2,cmm=" [u1,u2] et p ",p,[u1,u2],ps="StokesP2P1.eps",value=1,wait=1);
{
real[int] xx(21),yy(21),pp(21);
for (int i=0;i<21;i++)
{
yy[i]=i/20.;
61
xx[i]=u1(0.5,i/20.);
pp[i]=p(i/20.,0.999);
}
cout << " " << yy << endl;
//
//
plot([xx,yy],wait=1,cmm="u1 x=0.5 cup");
plot([yy,pp],wait=1,cmm="pressure y=0.999 cup");
}
dt = 0.05;
int nbiter = 3;
real coefdt = 0.25(1./nbiter);
real coefcut = 0.25(1./nbiter) , cut=0.01;
real tol=0.5,coeftol = 0.5(1./nbiter);
nu=1./reylnods;
for (iter=1;iter<=nbiter;iter++)
{
cout << " dt = " << dt << " ------------------------ " << endl;
alpha=1/dt;
for (i=0;i<=500;i++)
{
up1=u1;
up2=u2;
temp1=max(temp,25);
temp1=min(temp1,35);
k1=k; ep1=ep;
muT=0.09*k*k/ep;
NS; plot([u1,u2],wait=1);
//
Solves Navier-Stokes
prode =0.126*k*(pow(2*dx(u1),2)+pow(2*dy(u2),2)+2*pow(dx(u2)+dy(u1),2))/2;
prodk= prode*k/ep*0.09/0.126;
kappa=muT/0.41;
stress=abs(dy(u1));
kine; plot(k,wait=1);
viscturb; plot(ep,wait=1);
TEMPER;
//
solves temperature equation
if ( !(i % 5)){
plot(temp,value=1,fill=true,ps="temp_"+iter+"_"+i+".ps");
plot(coef=0.2,cmm=" [u1,u2] et p ",p,[u1,u2],ps="plotNS_"+iter+"_"+i+".ps");
}
cout << "CPU " << clock()-s0 << "s " << endl;
}
if (iter>= nbiter) break;
Th=adaptmesh(Th,[dx(u1),dy(u1),dx(u1),dy(u2)],splitpbedge=1,
abserror=0,cutoff=cut,err=tol, inquire=0,ratio=1.5,hmin=1./1000);
plot(Th,ps="ThNS.eps");
dt = dt*coefdt;
tol = tol *coeftol;
cut = cut *coefcut;
}
cout << "CPU " <<clock()-s0 << "s " << endl;
62
3.12
In a microwave oven heat comes from molecular excitation by an electromagnetic field. For
a plane monochromatic wave, amplitude is given by Helmholtzs equation:
v + v = 0.
We consider a rectangular oven where the wave is emitted by part of the upper wall. So the
boundary of the domain is made up of a part 1 where v = 0 and of another part 2 = [c, d]
).
where for instance v = sin( yc
cd
Within an object to be cooked, denoted by B, the heat source is proportional to v 2 . At
equilibrium, one has
= v 2 IB ,
= 0
Figure 3.11: A microwave oven: real (left) and imaginary (middle) parts of wave and temperature (right).
In the program below = 1/(1 I/2) in the air and 2/(1 I/2) in the object (i = 1):
//
file muwave.edp
63
+a4(10*n)+a5(10*n)+b0(5*n)+b1(10*n)+b2(5*n)+b3(10*n));
plot(Th,wait=1);
fespace Vh(Th,P1);
real meat = Th(a-f-e/2,g+l/2).region, air= Th(0.01,0.01).region;
Vh R=(region-air)/(meat-air);
Vh<complex> v,w;
solve muwave(v,w) = int2d(Th)(v*w*(1+R)
-(dx(v)*dx(w)+dy(v)*dy(w))*(1-0.5i))
+ on(1,v=0) + on(2, v=sin(pi*(y-c)/(c-d)));
Vh vr=real(v), vi=imag(v);
plot(vr,wait=1,ps="rmuonde.ps", fill=true);
plot(vi,wait=1,ps="imuonde.ps", fill=true);
fespace Uh(Th,P1); Uh u,uu, ff=1e5*(vr2 + vi2)*R;
solve temperature(u,uu)= int2d(Th)(dx(u)* dx(uu)+ dy(u)* dy(uu))
- int2d(Th)(ff*uu) + on(1,2,u=0);
plot(u,wait=1,ps="tempmuonde.ps", fill=true);
3.13
Optimal Control
Thanks to the function BFGS it is possible to solve complex nonlinear optimization problem
within FreeFem++ . For example consider the following inverse problem
Z
min J = (u ud )2 : ((b, c, d) u) = 0, u| = u
b,c,dR
where the desired state ud , the boundary data u and the observation set E are all
given. Furthermore let us assume that
(x) = 1 + bIB (x) + cIC (x) + dID (x) x
where B, C, D are separated subsets of .
To solve this problem by the quasi-Newton BFGS method we need the derivatives of J with
respect to b, c, d. We self explanatory notations, if b, c, d are variations of b, c, d we have
Z
J 2 (u ud )u, ( u) ( u) u| = 0
E
64
fespace Vh(th,P1);
Vh Ib=((x2+y2)<1.0001),
Ic=(((x+3)2+ y2)<1.0001),
Id=((x2+(y+3)2)<1.0001),
Ie=(((x-1)2+ y2)<=4),
ud,u,uh,du;
real[int] z(3);
problem A(u,uh) =int2d(th)((1+z[0]*Ib+z[1]*Ic+z[2]*Id)*(dx(u)*dx(uh)
+dy(u)*dy(uh))) + on(aa,u=x3-y3);
z[0]=2; z[1]=3; z[2]=4;
A; ud=u;
ofstream f("J.txt");
func real J(real[int] & Z)
{
for (int i=0;i<z.n;i++)z[i]=Z[i];
A; real s= int2d(th)(Ie*(u-ud)2);
f<<s<<"
"; return s;
}
real[int] dz(3), dJdz(3);
problem B(du,uh)
=int2d(th)((1+z[0]*Ib+z[1]*Ic+z[2]*Id)*(dx(du)*dx(uh)+dy(du)*dy(uh)))
+int2d(th)((dz[0]*Ib+dz[1]*Ic+dz[2]*Id)*(dx(u)*dx(uh)+dy(u)*dy(uh)))
+on(aa,du=0);
func real[int] DJ(real[int] &Z)
{
for(int i=0;i<z.n;i++)
{ for(int j=0;j<dz.n;j++) dz[j]=0;
dz[i]=1; B;
dJdz[i]= 2*int2d(th)(Ie*(u-ud)*du);
}
return dJdz;
}
real[int] Z(3);
for(int j=0;j<z.n;j++) Z[j]=1;
BFGS(J,DJ,Z,eps=1.e-6,nbiter=15,nbiterline=20);
cout << "BFGS: J(z) = " << J(Z) << endl;
for(int j=0;j<z.n;j++) cout<<z[j]<<endl;
plot(ud,value=1,ps="u.eps");
In this example the sets B, C, D, E are circles of boundaries bb, cc, dd, ee are the domain
is the circle of boundary aa. The desired state ud is the solution of the PDE for b = 2, c =
3, d = 4. The unknowns are packed into array z. Notice that it is necessary to recopy
Z into z because one is a local variable while the other one is global. The program found
b = 2.00125, c = 3.00109, d = 4.00551. Figure 3.12 shows u at convergence and the successive
function evaluations of J. Note that an adjoint state could have been used. Define p by
(p) = 2IE (u ud ),
Consequently
Z
J =
( (p))u
p| = 0
65
IsoValue
-118.75
-106.25
-93.75
-81.25
-68.75
-56.25
-43.75
-31.25
-18.75
-6.25
6.25
18.75
31.25
43.75
56.25
68.75
81.25
93.75
106.25
118.75
45
"J.txt"
40
35
30
25
20
15
10
0
0
10
15
20
25
30
35
Figure 3.12: On the left the level lines of u. On the right the successive evaluations of J by
BFGS (5 values above 500 have been removed for readability)
Z
(p u) =
(p u)
(3.9)
3.14
Compressible Euler equations should be discretized with Finite Volumes or FEM with flux
up-winding scheme but these are not implemented in FreeFem++ . Nevertheless acceptable
results can be obtained with the method of characteristics provided that the mean values
f = 12 (f + + f ) are used at shocks in the scheme, and finally mesh adaptation .
t + u + u = 0
u
(t u + u + p = 0
t p + up + ( 1)
p u = 0
(3.10)
(3.11)
66
Figure 3.13: Pressure for a Euler flow around a disk at Mach 2 computed by (3.11)
//
67
ifstream ggg("r.txt");ggg>>r1[];
plot(u1,ps="eta.eps", value=1,wait=1);
err0=err0/10; dt = dt/10;
}
problem eul(u,v,r,uh,vh,rh)
= int2d(Th)( (u*uh+v*vh+r*rh)/dt
+ ((dx(r)*uh+ dy(r)*vh) - (dx(rh)*u + dy(rh)*v))
)
+ int2d(Th)(-(rh*convect([u1,v1],-dt,r1) + uh*convect([u1,v1],-dt,u1)
+ vh*convect([u1,v1],-dt,v1))/dt)
+int1d(Th,6)(rh*u)
//
+int1d(Th,1)(rh*v)
+ on(2,r=0) + on(2,u=u0) + on(2,v=0);
int j=80;
for(int k=0;k<3;k++)
{
if(k==20){ err0=err0/10; dt = dt/10; j=5;}
for(int i=0;i<j;i++){
eul; u1=u; v1=v; r1=abs(r);
cout<<"k="<<k<<" E="<<int2d(Th)(u2+v2+r)<<endl;
plot(r,wait=0,value=1);
}
Th = adaptmesh (Th,r, nbvx=40000,err=err0,
abserror=1,nbjacoby=2, omega=1.8,ratio=1.8, nbsmooth=3,
splitpbedge=1, maxsubdiv=5,rescaling=1) ;
plot(Th,wait=0);
u=u;v=v;r=r;
savemesh(Th,"Th_circle.mesh");
ofstream f("u.txt");f<<u[];
ofstream ff("v.txt");ff<<v[];
ofstream fff("r.txt");fff<<r[];
r1 = sqrt(u*u+v*v);
plot(r1,ps="mach.eps", value=1);
r1=r;
}
3.15
Summary It is usually not easy to determine the type of a system. Yet the approximations
and algorithms suited to the problem depend on its type:
Finite Elements compatible (LBB conditions) for elliptic systems
Finite difference on the parabolic variable and a time loop on each elliptic subsystem
of parabolic systems; better stability diagrams when the schemes are implicit in time.
Upwinding, Petrov-Galerkin, Characteristics-Galerkin, Discontinuous-Galerkin, Finite
Volumes for hyperbolic systems plus, possibly, a time loop.
When the system changes type, then expect difficulties (like shock discontinuities)!
68
2
m
(x), ,
(x), 2 (x), , m (x)) = 0 x Rd .
x1
xd
x1
xd
The range of x over which the equation is taken, here , is called the domain of the PDE.
The highest derivation index, here m, is called the order. If F and are vector valued
functions, then the PDE is actually a system of PDEs.
Unless indicated otherwise, here by convention one PDE corresponds to one scalar valued
F and . If F is linear with respect to its arguments, then the PDE is said to P
be linear.
2
d
The general form of a second order, linear scalar PDE is xi x
and
A
:
B
means
i,j=1 aij bij .
j
+ a + B : () = f
Rd ,
in
where f (x), (x) R, a(x) Rd , B(x) Rdd are the PDE coefficients. If the coefficients
are independent of x, the PDE is said to have constant coefficients.
To a PDE we associate a quadratic form, by replacing by 1, /xi by zi and 2 /xi xj
by zi zj , where z is a vector in Rd :
+ a z + z T Bz = f.
If it is the equation of an ellipse (ellipsoid if d 2), the PDE is said to be elliptic; if it is
the equation of a parabola or a hyperbola, the PDE is said to be parabolic or hyperbolic. If
A 0, the degree is no longer 2 but 1, and for reasons that will appear more clearly later,
the PDE is still said to be hyperbolic.
These concepts can be generalized to systems, by studying whether or not the polynomial
system P (z) associated with the PDE system has branches at infinity (ellipsoids have no
branches at infinity, paraboloids have one, and hyperboloids have several).
If the PDE is not linear, it is said to be non linear. Those are said to be locally elliptic,
parabolic, or hyperbolic according to the type of the linearized equation.
For example, for the non linear equation
2 2
= 1,
t2
x x2
we have d = 2, x1 = t, x2 = x and its linearized form is:
2 u u 2 2 u
= 0,
t2
x x2
x x2
which for the unknown u is locally elliptic if
2 2
2
+
+
+
= f,
x21
x22
x2d
x Rd .
= f
t
x Rd ,
t ]0, T [.
> 0.
69
in
Q.
+ a = f.
t
The biharmonic equation is elliptic:
() = f
in
Boundary conditions A relation between a function and its derivatives is not sufficient to
define the function. Additional information on the boundary = of , or on part of
is necessary.
Such information is called a boundary condition. For example,
(x) given, x ,
is called a Dirichlet boundary condition. The Neumann condition is
(x) given on .
n
given on 2 ,
n
1 2 = ,
1 2 = .
Parabolic and hyperbolic equations rarely require boundary conditions on all of ]0, T [.
For instance, the heat equation is well posed with
given at t = 0 and Dirichlet or Neumann or mixed conditions on .
Here t is time so the first condition is called an initial condition. The whole set of conditions
are also called Cauchy conditions.
The wave equation is well posed with
and
70
3.16
First, it is possible to define variational forms, and use this forms to build matrix and vector
to make very fast script (4 times faster here) (see the example Heat.edp in chap3 examples
directory).
For example solve the Thermal Conduction problem of section 3.4 We must solve the temperature equation in in a time interval (0,T).
t u (u) = 0 in (0, T ),
u(x, y, 0) = u0 + xu1
u
u = 30 on 24 (0, T ),
+ (u ue ) = 0 on (0, T ).
n
(3.12)
Where with
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
Z 1
if i 24 , and j = i
=
n wj wi /dt else
Z
= n
uue wi
13
bcl = u0
b0 = vthermic0(0,Vh);
bcn = vthermic(0,Vh);
//
bcl=tgv*u0[];
//
constant part of RHS
//
tgv on Dirichlet part
we have for the node i : i 24 bcn[i] 6= 0
//
the Dirichlet B.C. part
//
for(real t=0;t<T;t+=dt){
real[int] b = b0 ;
b += M*u[];
b = bcn ? bcl : b;
u[] = A-1*b;
plot(u);
}
3.17
//
//
for the RHS
//
add the the time dependent part
do i: b[i] = bcn[i] ? bcl[i] : b[i] ;
//
Solve linear problem
Consider the following script to solve a time dependent Stokes problem in a cavity
mesh Th=square(10,10);
fespace Vh(Th,P2), Qh(Th,P1);
Vh u,v,uu,vv, uold=0,vold=0;
Qh p,pp;
real nu=0.1, T=1., dt = 0.1;
int m, M= T/dt;
problem stokes(u, v, p, uu, vv, pp)=
int2d(Th)( (u*uu+v*vv)/dt + nu*(dx(u)*dx(uu) + dy(u)*dy(uu)
+ dx(v)*dx(vv) + dy(v)*dy(vv))
- p*pp*1.e-6
- p*(dx(uu) +dy(vv))- pp*(dx(u)+dy(v))
) - int2d(Th)((uold*uu+vold*vv)/dt)
+ on(1,2,4,u=0,v=0) + on(3,u=1,v=0);
for(m=0;m<M;m++){
stokes; uold=u; vold=v;
}
plot(p,[u,v],value=true, wait=true, cmm="t="+m*dt);
Every iteration is in fact of the form A[u, v, p] = B[uold, vold, pold] + b where A, B are
matrices and b is a vector containing the boundary conditions. The A, B, b are constructed
by
fespace Xh(Th,[P2,P2,P1]);
varf aa ([u,v,p],[uu,vv,pp])
= int2d(Th)( (u*uu+v*vv)/dt + nu*(dx(u)*dx(uu) + dy(u)*dy(uu)
+ dx(v)*dx(vv) + dy(v)*dy(vv))
- p*pp*1.e-6 - p*(dx(uu) +dy(vv))- pp*(dx(u)+dy(v))
) + on(1,2,4,u=0,v=0) + on(3,u=1,v=0);
varf bb ([uold,vold,pold],[uu,vv,pp])
72
= int2d(Th)((uold*uu+vold*vv)/dt)
//
+ on(1,2,4,uold=0,vold=0) + on(3,uold=0,vold=0);
Note that the boundary conditions are not specified in bb. Removing the comment //
would cause the compiler to multiply the diagonal terms corresponding to a Dirichlet degree
of freedom by a very large term (tgv); if so b would not be needed, on the condition that
uold = 1 on boundary 3 initially. Note also that b has a tgv on the Dirichlet nodes, by
construction, and so does A.
The loop will them be
real[int] sol(Xh.ndof), aux(Xh.ndof);
for(m=0;m<M;m++){
aux=B*sol; aux+=b;
sol=A-1*aux;
}
There is yet a difficulty with the initialization of sol and with the solution from sol. For
this we need a temporary vector in Xh and here is a solution
Xh [w1,w2,wp]=[uold,vold,pp];
sol=w1[];
//
cause also the copy of w2 and wp
for(m=0;m<M;m++){
aux=B*sol; aux+=b;
sol=A-1*aux;
}
w1[]=sol; u=w1; v= w2; p=wp;
plot(p,[u,v],value=true, wait=true, cmm="t="+m*dt);
The freefem team agrees that the line sol=w1[]; is mysterious as it copies also w2 and
wp into sol. Structured data such as vectors of Xh here cannot be written component by
component. Hence w1=u is not allowed.
Chapter 4
Syntax
4.1
Data Types
In essence FreeFem++ is a compiler: its language is typed, polymorphic, with exception and reentrant. Every variable must be declared of a certain type, in a declarative
statement; each statement are separated from the next by a semicolon ;. The language
allows the manipulation of basic types integers (int), reals (real), strings (string), arrays (example: real[int]), bidimensional (2D) finite element meshes (mesh), 2D finite
element spaces (fespace) , analytical functions (func), arrays of finite element functions
(func[basic type]), linear and bilinear operators, sparse matrices, vectors , etc. For instance
int i,n=20;
//
i, n are integer.
real[int] xx(n),yy(n);
//
two array of size n
for (i=0;i<=20;i++)
//
which can be used in statements such as
{ xx[i]= cos(i*pi/10); yy[i]= sin(i*pi/10); }
The life of a variable is the current block {. . .}, except the fespace variable, and the
variables local to a block are destroyed at the end of the block as follows.
Example 4.1
real r= 0.01;
mesh Th=square(10,10);
fespace Vh(Th,P1);
Vh u = x+ exp(y);
func f = z * x + r * log(y);
plot(u,wait=true);
{
real r = 2;
fespace Vh(Th,P1);
}
//
//
//
unit square mesh
P1 lagrange finite element space
//
new block
//
not the same r
error because Vh is a global name
//
end of block
//
here r back to 0.01
The type declarations are compulsory in FreeFem++ ; in the end this feature is an asset
because it is easy to make bugs in a language with many implicit types. The variable name
is just an alphanumeric string, the underscore character is not allowed, because it will
be used as an operator in the future.
73
74
CHAPTER 4. SYNTAX
4.2
bool is used for logical expression and flow-control. The result of a comparison is a boolean
type as in
bool fool=(1<2);
which makes fool to be true. Similar examples can be built with ==, <=, >=, <, >, ! =.
int declares an integer (i.e. long in C++)..
string declare the variable to store a text enclosed within double quotes, such as:
"This is a string in double quotes."
real declares the variable to store a number such as 12.345. (i.e. double in C++).
a =
"a +
"a "a *
"a /
1i,
b =
b =
b =
b =
b
"
"
"
"
= 2 + 3i;
<< a + b <<
<< a + b <<
<< a * b <<
<< a / b <<
endl;
endl;
endl;
endl;
+
*
/
b
b
b
b
=
=
=
=
(2,4)
(-2,-2)
(-3,2)
(0.230769,0.153846)
:
2
3.33333
real[string ] declares a variable that store multiple real numbers with string index.
string[string ] declares a variable that store multiple strings with string index.
75
func defines a function without argument, if independent variables are x, y. For example
func f=cos(x)+sin(y) ;
Remark that the functions type is given by the expressions type. Raising functions
to a numerical power is done, for instance, by x1, y0.23.
mesh creates the triangulation, see Section 5.
fespace defines a new type of finite element space, see Section Section 6.
problem declares the weak form of a partial differential problem without solving it.
solve declares a problem and solves it.
varf defines a full variational form.
matrix defines a sparse matrix.
4.3
Global Variables
76
CHAPTER 4. SYNTAX
nuEdge gives the index of the current edge in the triangle (int value).
nTonEdge gives the number of adjacent triangle of the current edge (integer ).
area give the area of the current triangle (real value).
volume give the volume of the current tetrahedra (real value).
cout is the standard output device (default is console). On MS-Windows, the standard
output is only the console, at this time. ostream
cin is the standard input device (default is keyboard). (istreamvalue).
endl adds an end of line to the input/output flow.
true means true in bool value.
false means false in bool value.
pi is the realvalue approximation value of .
4.4
System Commands
Here is how to show all the types, and all the operator and functions of a FreeFem++
program:
dumptable(cout);
To execute a system command in the string (not implemented on Carbon MacOS), which
return the value of the system call.
system("shell command");
exec("shell command");
//
This is useful to launch another executable from within FreeFem++ . On MS-Windows, the
full path of the executable. For example, if there is the command ls.exe in the subdirectory
c:\cygwin\bin\, then we must write
exec("c:\\cygwin\\bin\\ls.exe");
4.5. ARITHMETICS
4.5
77
Arithmetics
By the relation integer real, the operators +, , , /, % and max, min, are
extended to real numbers or variables. However, % calculates the remainder of the integer
parts of two real numbers.
The following are examples similar to Example 4.2
real
cout
cout
cout
cout
cout
a=sqrt(2.), b = pi;
<<"plus, minus of "<<a<<" and "<<pi<<" are "<< a+b <<", "<< a-b << endl;
<<"multiplication, quotient of them are "<<a*b<<", "<<a/b<< endl;
<<"remainder from division of "<<a<<" by "<<b<<" is "<< a%b << endl;
<<"the minus of "<<a<<" is "<< -a << endl;
<<a<<" plus -"<<b<<" need bracket :"<<a<<"+(-"<<b<<")="<<a + (-b) << endl;
78
CHAPTER 4. SYNTAX
By the relation
bool int real complex,
the operators +, , , / and are also applicable on complex-typed
variables, but %,
max, min cannot be used. Complex numbers such as 5+9i, i= 1, are validexpressions.
With real variables a=2.45, b=5.33,complex numbers like a + i b and a + i 2.0 must be
declared by
complex z1 = a+b*1i, z2=a+sqrt(2.0)*1i;
The imaginary and real parts of a complex number z can be obtained with imag and real.
The conjugate of a + bi (a, b are reals) is defined by a bi, which can also be computed
with the operator conj, by conj(a+b*1i) in FreeFem++ .
Internally the complex number z = a + ib is considered as the pair (a, b) of real numbers a, b.
We can attach to it the point (a, b) in the Cartesian plane where the x-axis is for the real
part and the y-axis for the imaginary part. The same point (a,b) has a representation with
polar coordinate (r, ), So z his also z = r(cos + i sin ), r = a2 + b2 and = tan1 (b/a);
r is called the modulus and the argument of z. In the following example, we shall show
them using FreeFem++ programming, and de Moivres formula z n = rn (cos n + i sin n).
Example 4.3
real a=2.45, b=5.33;
complex z1=a+b*1i, z2 = a+sqrt(2.)*1i;
func string pc(complex z)
//
printout complex to (real)+i(imaginary)
{
string r = "("+real(z);
if (imag(z)>=0) r = r+"+";
return r+imag(z)+"i)";
}
//
printout complex to |z|*(cos(arg(z))+i*sin(arg(z)))
func string toPolar(complex z)
{
return abs(z)+"*(cos("+arg(z)+")+i*sin("+arg(z)+"))";
}
cout <<"Standard output of the complex "<<pc(z1)<<" is the pair "
<<z1<<endl;
cout <<"Plus, minus of "<<pc(z1)<<" and "<<pc(z2)<<" are "<< pc(z1+z2)
<<", "<< pc(z1-z2) << endl;
cout <<"Multiplication, quotient of them are "<<pc(z1*z2)<<", "
<<pc(z1/z2)<< endl;
cout <<"Real/imaginary part of "<<pc(z1)<<" is "<<real(z1)<<", "
<<imag(z1)<<endl;
cout <<"Absolute of "<<pc(z1)<<" is "<<abs(z1)<<endl;
cout <<pc(z2)<<" = "<<toPolar(z2)<<endl;
cout <<" and polar("<<abs(z2)<<","<<arg(z2)<<") = "
79
<< pc(polar(abs(z2),arg(z2)))<<endl;
cout <<"de Moivres formula: "<<pc(z2)<<"3 = "<<toPolar(z23)<<endl;
cout <<"conjugate of "<<pc(z2)<<" is "<<pc(conj(z2))<<endl;
cout <<pc(z1)<<""<<pc(z2)<<" is "<< pc(z1z2) << endl;
4.6
string expression
//
string concatenation
//
new operator
t2 ="12340005678";
t2(4:3) = "abcdefghijk-";
string t55=t2(4:3);
//
t2 = "12340abcdefghijk-005678";
cout << t2 << endl;
cout << " find abc " << t2.find("abc") << endl;
cout << "r find abc " << t2.rfind("abc") << endl;
cout << " find abc from 10 " << t2.find("abc",10) << endl;
cout << " ffind abc from 10 " <<t2.rfind("abc",10) << endl;
cout << "
" << string("abcc").length << endl;
cout << " t55 " << t55 << endl;
{
//
add getline version 3.0-6 jan 2009 FH
string s;
ifstream toto("xyf");
for (int i=0;i<10;++i)
{
getline(toto,s);
cout << i << " : " << s << endl;
}
}
80
4.7
CHAPTER 4. SYNTAX
Fundamental functions are built into FreeFem++ as well as The power function x y
= pow(x,y)= xy ;, the exponent function exp(x) (= ex ), the logarithmic function log(x)(= ln x) or log10(x) (= log10 x); the trigonometric functions sin(x),
cos(x), tan(x) assume angles measured in radians; the inverse of sin x, cos x, tan x
(called circular function or inverse trigonometric function ) asin(x)(=arcsin x),
acos(x)(=arccos x), atan(x)(=arctan x) are also implemented; the atan2(x,y)
function computes the principal value of the arc tangent of y/x, using the signs of both
arguments to determine the quadrant of the return value;
the hyperbolic functions,
sinh x = ex ex /2,
cosh x = ex + ex /2.
sinh1 x = ln x + x2 + 1 ,
cosh1 x = ln x + x2 1 .
The real function which rounds a real to an integer floor(x) rounds to largest integral
value not greater than x, ceil(x) round to smallest integral value not less than x;
similarly rint(x) returns the integral value nearest to x (according to the prevailing
rounding mode) in floating-point format)..
Elementary Functions denotes for us the class of functions presented above (polynomials, exponential, logarithmic, trigonometric, circular) and the functions obtained
from those by the four arithmetic operations
f (x) + g(x), f (x) g(x), f (x)g(x), f (x)/g(x)
and by composition f (g(x)), each applied a finite number of times. In FreeFem++ ,
all elementary functions can thus be created. The derivative of an elementary function is also an elementary function; however, the indefinite integral of an elementary
function cannot always be expressed in terms of elementary functions.
Example 4.4 The following is an example where an elementary function is used to
build the border of a domain. Cardioid
real b = 1.;
real a = b;
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); }
mesh Th = buildmesh(C(50));
81
82
CHAPTER 4. SYNTAX
The erf(x) function calculates the error function, where erf(x) = 2
(pi)
Rx
0
exp(t2 )dt.
4.8
4.8.1
The general form of real functions of two independent variables a, b is usually written as
c = f (a, b). In FreeFem++ , x, y and z are reserved word as explained in in Section 4.3.
So when the two variables of the function are x and y, we may define the function without
its argument, for example
func f=cos(x)+sin(y) ;
Remark that the function type is given by the expression type. The power operator can be
used in functions such as x1, y0.23. In func, we can write an elementary function
as follows
func f = sin(x)*cos(y);
func g = (x2+3*y2)*exp(1-x2-y2);
func h = max(-0.5,0.1*log(f2+g2));
//
//
//
//
] , [2
//
z = x +iy
//
f == z
g = | sin z/10 exp z 2 /10|
contour lines of f
contour lines of g
We call also construct elementary functions of two variables from elementary functions f (x)
or g(y) by the four arithmetic operations plus composition applied a finite number of times.
4.8.2
FE-functions
Finite element functions are also constructed like elementary functions by an arithmetic formula involving elementary functions. The difference is that they are evaluated at declaration
time and FreeFem++ stores the array of its values at the places associated with he degree
of freedom of the finite element type. By opposition elementary functions are evaluated only
when needed. Hence FE-functions are not defined only by their formula but also by the
mesh and the finite element which enter in their definitions. If the value of a FE-function is
requested at a point which is not a degree of freedom, an interpolation is used, leading to an
interpolation error, while by contrast, an elementary function can be evaluated at any point
exactly.
4.9. ARRAYS
83
func f=x2*(1+y)3+y2;
mesh Th = square(20,20,[-2+4*x,-2+4*y]);
//
square ] 2, 2[2
fespace Vh(Th,P1);
Vh fh=f;
//
fh is the projection of f to Vh (real value)
func zf=(x2*(1+y)3+y2)*exp(x+1i*y);
Vh<complex> zh = zf;
//
zh is the projection of zf
//
to complex value Vh space
4.9
//
//
//
//
Fig.
Fig.
] , [2
//
z = x +iy
//
f == z
g = | sin z/10 exp z 2 /10|
4.1 isovalue of f
4.2 isovalue of g
Arrays
An array stores multiple objects, and there are 2 kinds of arrays: The first is similar to
vector, i.e. arrays with with integer indices and the second type is arrays with string indices.
In the first case, the size of the array must be known at execution time, and implementation
is done with the KN<> class and all the vector operator of KN<> are implemented. For
instance
real [int] tab(10), tab1(10);
real [int] tab2;
//
//
2 array of 10 real
bug array with no size
84
CHAPTER 4. SYNTAX
tab = 1.03;
//
set all the array to 1.03
tab[1]=2.15;
cout << tab[1] << " " << tab[9] << " size of tab = "
<< tab.n << " min: " << tab.min << " max:" << tab.max
<< " sum : "
<< tab.sum <<
endl;
//
tab.resize(12);
//
change the size of array tab
//
to 12 with preserving first value
tab(10:11)=3.14;
//
set unset value
cout <<" resize tab: " << tab << endl;
real [string] tt;
tt["+"]=1.5;
cout<<tt["a"]<<" "<<tt["+"]<<endl;
real[int] a(5),b(5),c(5),d(5);
a = 1;
b = 2;
c = 3;
a[2]=0;
d = ( a ? b : c );
//
for i = 0, n-1 : d[i] = a[i] ? b[i] : c[i] ,
cout << " d = ( a ? b : c ) is " << d << endl;
d = ( a ? 1 : c ); //
for i = 0, n-1: d[i] = a[i] ? 1 : c[i] , (v2.23-1)
d = ( a ? b : 0 ); //
for i = 0, n-1: d[i] = a[i] ? b[i] : 0 , (v2.23-1)
d = ( a ? 1 : 0 ); //
for i = 0, n-1: d[i] = a[i] ? 0 : 1 ,
(v2.23-1)
tab.sort ;
//
sort the array tab (version 2.18)
cout << " tab (after sort) " << tab << endl;
int[int] ii(0:d.n-1);
//
set array ii to 0,1, ..., d.n-1
(v3.2)
d=-1:-5;
//
set d to -1,-2, .. -5
(v3.2)
sort(d,ii);
//
sort array d and ii in parallel
cout << " d " << d << "\n ii = " << ii << endl;
Arrays can be set like in matlab or scilab with the operator ::, the array generator of a:c
is equivalent to a:1:c, and the array set by a:b:c is set to size b|(b a)/c| + 1c and the
value i is set by a + i(b a)/c.
There are int,real, complex arrays with, in the third case, two operators (.in, .re) to
generate the real and imaginary real array from the complex array (without copy) :
4.9. ARRAYS
85
//
//
{
int[int] tt(2:10);
int[int] t1(2:3:10);
cout << " tt(2:10)= " << tt << endl;
cout << " t1(2:3:10)= " << t1 << endl;
tt=1:2:5;
cout << " 1.:2:5 => " << tt << endl;
}
2,3,4,5,6,7,8,9,10
//
2,5,8,
{
real[int] tt(2:10);
//
2,3,4,5,6,7,8,9,10
real[int] t1(2.:3:10.);
//
2,5,8,
cout << " tt(2:10)= " << tt << endl;
cout << " t1(2:3:10)= " << t1 << endl;
tt=1.:0.5:3.999;
cout << " 1.:0.5:3.999 => " << tt << endl;
}
{
complex[int] tt(2.+0i:10.+0i);
//
2,3,4,5,6,7,8,9,10
complex[int] t1(2.:3.:10.);
//
2,5,8,
cout << " tt(2.+0i:10.+0i)= " << tt << endl;
cout << " t1(2.:3.:10.)= " << t1 << endl;
cout << " tt.re real part array
" << tt.re << endl ;
//
the real part array of the complex array
cout << " tt.im imag part array
" << tt.im << endl ;
//
the imag part array of the complex array
}
The output is :
tt(2:10)= 9
2
3
4
5
6
7
8
9 10
t1(2:3:10)= 3
2
5
8
1.:2:5 => 3
1
3
5
tt(2:10) = = 9
2
3
4
5
6
7
8
9 10
t1(2.:3:10.)= 3
2
5
8
1.:0.5:3.999 => 6
1 1.5
2 2.5
3
3.5
tt(2.+0i:10.+0i)= 9
(2,0) (3,0) (4,0) (5,0) (6,0)
(7,0) (8,0) (9,0) (10,0)
t1(2.:3.:10.);= 3
(2,0) (5,0) (8,0)
tt.re real part array
86
CHAPTER 4. SYNTAX
2
3
4
5
6
7
8
9 10
tt.im imag part array
0
0
0
0
0
0
0
0
0
{
int N=5;
real[int] a(N),b(N),c(N);
a =1;
a(0:4:2) = 2;
a(3:4) = 4;
cout <<" a = " << a << endl;
b = a+ a;
cout <<" b = a+a : " << b << endl;
b += a;
cout <<" b += a : " << b << endl;
b += 2*a;
cout <<" b += 2*a : " << b << endl;
b /= 2;
cout <<" b /= 2 : " << b << endl;
b .*= a;
cout << "b.*=a; b =" << b << endl;
b ./= a;
cout << "b./=a; b =" << b << endl;
c = a+b;
cout << " c =a+b : c=" << c << endl;
c = 2*a+4*b;
cout << " c =2*a+4b : c= " << c << endl;
c = a+4*b;
cout << " c =a+4b : c= " << c << endl;
c = -a+4*b;
cout << " c =-a+4b : c= " << c << endl;
c = -a-4*b;
cout << " c =-a-4b : c= " << c << endl;
c = -a-b;
cout << " c =-a-b : c= " << c << endl;
c = a .* b;
cout << " c
c = a ./ b;
cout << " c
c = 2 * b;
cout << " c
c = b*2 ;
cout << " c
=a.*b
=a./b
=2*b
=b*2
//
same b = b .* a
//
same b = b ./ a
4.9. ARRAYS
87
||a||_1
||a||_2
||a||_infty
sum a_i
max a_i
=
=
=
=
=
"
"
"
"
"
<<
<<
<<
<<
<<
min a_i
= " <<
//
---- the methods -endl;
//
endl;
//
endl;
//
endl;
//
a[ " << a.imax << " ] = " << a[a.imax]
//
<< " a[ " << a.imin << " ] = " << a[a.imin]
//
a.l1
<<
a.l2
<<
a.linfty <<
a.sum
<<
a.max << "
a.min
//
//
array mapping
//
int[int] I=[2,3,4,-1,3];
b=c=-3;
b= a(I);
//
for( i=0;i<b.n;i++) if(I[i] >=0) b[i]=a[I[i]];
c(I)= a;
//
for( i=0;i<I.n;i++) if(I[i] >=0) C(I[i])=a[i];
cout << " b = a(I) : " << b << "\n c(I) = a " << c << endl;
c(I) += a;
//
for( i=0;i<I.n;i++) if(I[i] >=0) C(I[i])+=a[i];
cout << " b = a(I) : " << b << "\n c(I) = a " << c << endl;
}
3
3
2
2
3
3
3
3
b = a+a : 5
4
b += a : 5
6
12
12
b += 2*a : 5
10
10
20
20
b /= 2 : 5
5
2.5
10
10
b.*=a; b =5
10
2.5
10
40
40
b./=a; b =5
5
2.5
10
10
3.5
14
14
c =2*a+4b : c= 5
24
12
24
48
48
c =a+b : c=5
7
88
CHAPTER 4. SYNTAX
c =a+4b : c= 5
22
11
22
44
44
18
36
36
c =-a-4b : c= 5
-22
-11
-22
-44
-44
c =-a-b : c= 5
-7
-3.5
-7
-14
-14
: c= 5
10
2.5
10
40
40
: c= 5
0.4
0.4
0.4
0.4
0.4
c =-a+4b : c= 5
18
c =a.*b
c =a./b
c =2*b
c =b*2
: c= 5
10
10
20
20
: c= 5
10
10
20
20
||a||_1
= 13
||a||_2
= 6.403124237
||a||_infty = 4
sum a_i
= 13
max a_i
= 4 a[ 3 ] = 4
min a_i
= 1 a[ 1 ] = 1
a*a
= 41
a quantile 0.2 = 2
b = a(I) : 5
2
4
4 -3
4
c(I) = a 5
-3 -3
2
b = a(I) : 5
2
4
4 -3
c(I) = a 5
-3 -3
4
Note 4.2 Quantiles are points taken at regular intervals from the cumulative distribution
function of a random variable. Here the array values are random.
This statisticial function a.quantile(q) computes v from an array a of size n for a
given number q ]0, 1[ such that
#{i/a[i] < v} q n
it is equivalent to v = a[q n] when the array a is sorted.
4.9. ARRAYS
89
Example of array with renumbering (version 2.3 or better) . The renumbering is always
given by an integer array, and if a value in the array is negative, the mapping is not imaged,
so the value is not set.
int[int] I=[2,3,4,-1,0];
//
the integer mapping to set the renumbering
b=c=-3;
b= a(I);
//
for( i=0;i<b.n;i++) if(I[i] >=0) b[i]=a[I[i]];
c(I)= a;
//
for( i=0;i<I.n;i++) if(I[i] >=0) C(I[i])=a[i];
cout << " b = a(I) : " << b << "\n c(I) = a " << c << endl;
The output is
b = a(I) : 5
2
c(I) = a 5
4
4.9.1
-3
-3
Some example are given below to transform full matrices into sparse matrices.
int N=3,M=4;
real[int,int] A(N,M);
real[int] b(N),c(M);
b=[1,2,3];
c=[4,5,6,7];
complex[int,int] C(N,M);
complex[int] cb=[1,2,3],cc=[10i,20i,30i,40i];
b=[1,2,3];
int [int] I=[2,0,1];
int [int] J=[2,0,1,3];
A=1;
A(2,:) = 4;
A(:,1) = 5;
A(0:N-1,2) = 2;
A(1,0:2) = 3;
//
//
outer product
90
CHAPTER 4. SYNTAX
B=A(I,J);
B=A(I-1,J-1);
//
//
A = 2.*b*c;
cout << " A = " << A << endl;
B = b*c;
B = b*c;
B = (2*b*c)(I,J);
//
B = (3.*b*c)(I-1,J-1);
//
cout << "B = (3.*b*c)(I-1,J-1) =
B(i,j)= A(I(i),J(j))
B(I(i),J(j))= A(i,j)
//
//
//
outer
outer
" << B
the output is
b = a(I) : 5
2
-3
c(I) = a 5
4
-3
A = 3 4
1
3
4
5
3
5
2
3
2
1
1
4
C = 3 4
(-50,-40) (-100,-80) (-150,-120) (-200,-160)
(-100,-80) (-200,-160) (-300,-240) (-400,-320)
(-150,-120) (-300,-240) (-450,-360) (-600,-480)
A = 3 4
8
16
24
4.9.2
10
20
30
12
24
36
14
28
42
outer product
4.9. ARRAYS
91
[ 4,0 , 0, 0]];
Asd[0] ,0
,0
,0
,Csd[0]
0
,Asd[1] ,0
,0
,Csd[1]
0
,0
,Asd[2] ,0
,Csd[2]
0
,0
,0
,Asd[3] ,Csd[3]
Csd[0],Csd[1],Csd[2],Csd[3],DD
],
],
],
],
]
];
//
to now to pack the right hand side
real[int] bb =[rhssd[0][], rhssd[1][],rhssd[2][],rhssd[3][],rhsl[] ];
set(M,solver=sparsesolver);
xx = M-1 * bb;
[usd[0][],usd[1][],usd[2][],usd[3][],lh[]] = xx;
//
to dispatch
//
the solution on each part.
where Asd and Csd are arrays of matrices (from example mortar-DN-4.edp of
examples++-tuturial).
To set or get all the indices and coefficients of the sparse matrix A, let I,J,C be
respectively two int[int] arrays and a real[int] array. The three arrays define
the matrix as follows
X
C[k]MI[k],J[k]
where Mab = (ia jb )ij
A=
k
one has: Mab a basic matrix with the only non zero term mab = 1.
One can write [I,J,C]=A ; to get all the term of the matrix A (the arrays are
automatically resized), and A=[I,J,C] ; to change all the term matrices. Note
that the size of the matrix is with n= I.max and m=J.max. Remark that I,J is
forgotten to build a diagonal matrix, and similarly for the n, m of the matrix.
matrix renumbering
int[int] I(15),J(15);
//
//
matrix B;
B = A;
B=A(I,J);
B=A(I-1,J-1);
B.resize(10,20);
terms
//
//
copie matrix A
//
B(i,j) = A(I(i),J(j))
//
B(I(i),J(j))= A(i,j)
resize the sparse matrix and remove out of bound
92
CHAPTER 4. SYNTAX
complex versu real sparse matrix:
matrix<complex> C=vv(Xh,Xh);
matrix R=vr(Xh,Xh);
matrix<complex> CR=R; C=R;
//
create or copy real matrix tp complex
matrix
R=C.im; R=C.re;
//
get real or imagery part of complex sparse matrix
matrix CI=C.im, CR=C.re;
//
get real or imagery part of complex sparse
matrix
4.9.3
Matrix Operations
b = A-1 x)
* is the compound of transposition and matrix product, so it is the dot product (example real DotProduct=a*b) , in complex case you get the Hermitian product,
so mathematically we have a*b= aT b .
Example 4.5
mesh Th = square(2,1);
fespace Vh(Th,P1);
Vh f,g;
f = x*y;
g = sin(pi*x);
Vh<complex> ff,gg;
//
a complex valued finite element function
ff= x*(y+1i);
gg = exp(pi*x*1i);
varf mat(u,v) =
int2d(Th)(1*dx(u)*dx(v)+2*dx(u)*dy(v)+3*dy(u)*dx(v)+4*dy(u)*dy(v))
+ on(1,2,3,4,u=1);
varf mati(u,v) =
int2d(Th)(1*dx(u)*dx(v)+2i*dx(u)*dy(v)+3*dy(u)*dx(v)+4*dy(u)*dy(v))
+ on(1,2,3,4,u=1);
matrix A = mat(Vh,Vh); matrix<complex> AA = mati(Vh,Vh);
//
a complex
sparse matrix
Vh m0; m0[] = A*f[];
Vh m01; m01[] = A*f[];
4.9. ARRAYS
93
//
A.diag = diagofA ;
//
//
//
//
For the second case, it is just a map of the STL1 [31] so no operations on vector are allowed,
except the selection of an item .
The transpose or Hermitian conjugation operator is as in Matlab or Scilab, so the way to
compute the dot product of two array a,b is real ab= a*b.
The resizing of a sparse matrix A is also allowed:
A.resize(10,100);
Note that the new size can be greater or smaller than the previous size; all new term are set
to zero.
1
94
CHAPTER 4. SYNTAX
1030 0.5
0.
30. 2.5 0.
0.
1030 0.5
0.
0.5 2.5
30
0.
0.
10
0.
0.
0.5
A =
30
0.5
0.
0.
10
0.
0.
30
2.5 0.5
0.
0.5 10
0.
0. 2.5 0.
0.
0.5 1030
T
0 0 0 0 0.5 1
{v} = f[] =
0 1 1.2 1016 0 1 1.2 1016
{w} = g[] =
T
1.25 2.25 0.5 0 5 1029 1030
A*f[] =
(= A{v})
T
1.25 2.25 0 0.25 5 1029 1030
A*f[] =
(= AT {v})
T
0 0 0 0 0.5 1.2 1016
f[].*g[] =
= (v1 w1 vM wM )T
T
NaN 0 0 NaN 0.5 8.1 1015
f[]./g[] =
= (v1 /w1 vM /wM )T
f[]*g[] = 0.5
0
1
4
5
0
2
4
5
1
2
4
1
3
4
0
4
3
1
1
5
0
4
4
2
1
5
1
5
3
2
0
4
1e+30
0.5
1e+30
-2.5
0.5
-2.5
-2.5
0.5
-2.5
1e+30
0.5
1e+30
1e+30
0.5
0.5
0.5
0.5
1e+30
J= 18
C= 18
The output of a diagonal sparse matrix D (Warning du to fortran interface the indices start
on the output at one, but in FreeFem++ in index as in C begin at zero);
D = # Sparce Matrix (Morse)
# first line: n m (is symmetic) nbcoef
# after for each nonzero coefficient:
i j a_ij where (i,j) \in
6 6 1 6
1
1 1.0000000000000000199e+30
2
2 1.0000000000000000199e+30
3
3 1.0000000000000000199e+30
4
4 1.0000000000000000199e+30
5
5 1.0000000000000000199e+30
6
6 1.0000000000000000199e+30
{1,...,n}x{1,...,m}
Note 4.3 The operators -1 cannot be used to create a matrix; the following gives an error
matrix AAA = A-1;
4.9. ARRAYS
95
In examples++-load/lapack.edp a full matrix is inverted using the lapack library and this
small dynamic link interface (see for more detail section C page 369).
load "lapack"
load "fflapack"
int n=5;
real[int,int] A(n,n),A1(n,n),B(n,n);
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
A(i,j)= (i==j) ? n+1 : 1;
cout << A << endl;
A1=A-1;
cout << A1 << endl;
//
B=0;
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
for(int k=0;k<n;++k)
B(i,j) +=A(i,k)*A1(k,j);
cout << B << endl;
//
inv(A1);
cout << A1 << endl;
1
6
1
1
1
1
1
6
1
1
1
1
1
6
1
1
1
1
1
6
error: dgesv_ 0
5 5
0.18 -0.02 -0.02 -0.02 -0.02
-0.02 0.18 -0.02 -0.02 -0.02
-0.02 -0.02 0.18 -0.02 -0.02
-0.02 -0.02 -0.02 0.18 -0.02
-0.02 -0.02 -0.02 -0.02 0.18
5 5
1 -1.387778781e-17 -1.040834086e-17 3.469446952e-17
0
-1.040834086e-17
1 -1.040834086e-17 -2.081668171e-17
0
3.469446952e-18 -5.551115123e-17
1 -2.081668171e-17 -2.775557562e-17
1.387778781e-17 -4.510281038e-17 -4.857225733e-17
1 -2.775557562e-17
-1.387778781e-17 -9.714451465e-17 -5.551115123e-17 -4.163336342e-17
1
5 5
6
1
1
1
1
1
6
1
1
1
1
1
6
1
1
1
1
1
6
1
1
1
1
1
6
96
CHAPTER 4. SYNTAX
to compile lapack.cpp or fflapack.cpp you must have the library lapack on you system
and try in directory examples++-load
ff-c++ lapack.cpp -llapack
ff-c++ fflapack.cpp -llapack
4.9.4
Other arrays
It is also possible to make an array of FE functions, with the same syntax, and we can
treat them as vector valued function if we need them. , the syntax for space or vector finite
function is
int n = 100;
Vh[int] wh(n);
Wh[int] [uh,vh](n);
Vh<complex>[int] cwh(n);
Wh<complex>[int] [cuh,cvh](n);
[cuh[2],cvh[2]]= [x,y];
//
//
//
real [int][int] V(10);
matrix[int] B(10);
real [int,int][int] A(10);
Array of Array
//
//
//
Example 4.6 In the following example, Poissons equation is solved for 3 different given
functions f = 1, sin(x) cos(y), |x 1||y 1|, whose solutions are stored in an array of FE
function.
mesh Th=square(20,20,[2*x,2*y]);
fespace Vh(Th,P1);
Vh u, v, f;
problem Poisson(u,v) =
int2d(Th)( dx(u)*dx(v) + dy(u)*dy(v))
+ int2d(Th)( -f*v ) + on(1,2,3,4,u=0) ;
Vh[int] uu(3);
f=1;
Poisson; uu[0] = u;
f=sin(pi*x)*cos(pi*y);
Poisson; uu[1] = u;
f=abs(x-1)*abs(y-1);
Poisson; uu[2] = u;
for (int i=0; i<3; i++)
plot(uu[i], wait=true);
//
an array of FE function
//
problem1
//
//
problem2
//
problem3
4.10
97
Map arrays
For the second case, it is just a map of the STL2 [31] so no operations on vector are allowed,
except the selection of an item .
real[string] map;
for (i=0;i<10;i=i+1)
{
tab[i] = i*i;
cout << i << " " << tab[i] << "\n";
};
map["1"]=2.0;
map[2]=3.0;
//
//
a dynamic array
cout << " map[\"1\"] = " << map["1"] << "; "<< endl;
cout << " map[2] = " << map[2] << "; "<< endl;
4.11
Loops
The for and while loops are implemented in FreeFem++ together with break and
continue keywords.
In for-loop, there are three parameters; the INITIALIZATION of a control variable, the
CONDITION to continue, the CHANGE of the control variable. While CONDITION is true,
for-loop continue.
for (INITIALIZATION; CONDITION; CHANGE)
{ BLOCK of calculations }
The while-loop
while (CONDITION) {
BLOCK of calculations or change of control variables
}
is executed repeatedly until CONDITION become false. The sum from 1 to 10 can also be
computed by while as follows,
int i=1, sum=0;
while (i<=10) {
sum += i; i++;
}
We can exit from a loop in midstream by break. The continue statement will pass the
part from continue to the end of the loop.
2
98
CHAPTER 4. SYNTAX
Example 4.7
for (int i=0;i<10;i=i+1)
cout << i << "\n";
real eps=1;
while (eps>1e-5)
{ eps = eps/2;
if( i++ <100) break;
cout << eps << endl;}
for (int j=0; j<20; j++) {
if (j<10) continue;
cout << "j = " << j << endl;
}
From version 3.43, we have new kind of implicit loop to set array, map, sparse matrices.
Lhe syntaxe is
Example 4.8
real [int,int] a(10,10);
real [int] b(10);
for [i,bi : b] {bi=i+1; cout << i << " " << bi << endl;}
cout << " b="<< b<< endl;
for [i,j,aij : a]
{
aij= 1./(2+i+j);
if(abs(aij)<0.2) aij=0;
}
cout << " a= "<< a << endl;
matrix A=a;
string[string] ss;
ss["1"]= 1;
ss["2"]= 2;
ss["3"]= 5;
for [i,bi : ss]
bi=i+6+"-dddd";
cout <<" ss = "<< ss << endl;
int[string] si;
si[1]=2;
si[50]=1;
for [i,vi : si]
{
cout << " i " << setw(3) << i << " " << setw(10) <<vi << endl;
vi= atoi(i)*2;
}
//
a map
4.12. INPUT/OUTPUT
99
4.12
Input/Output
The syntax of input/output statements is similar to C++ syntax. It 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).
The word append in ofstream ofile("filename",append|binary); means opening a file in append and binary mode.
Note 4.4 The file is closed at the exit of the enclosing block,
Example 4.9
int i;
cout << " std-out" << endl;
cout << " enter i= ? ";
cin >> i ;
{
ofstream f("toto.txt");
f << i << "coucou\n";
};
//
{
ifstream f("toto.txt");
f >> i;
}
{
ofstream f("toto.txt",append);
//
to append to the existing file "toto.txt"
f << i << "coucou\n";
};
//
close the file f because the variable f is delete
cout << i << endl;
int nold=f.precision(n) Sets the number of digits printed to the right of the
decimal point. This applies to all subsequent floating point numbers written to that
output stream. However, this wont make floating-point integers print with a decimal
point. Its necessary to use fixed for that effect.
100
CHAPTER 4. SYNTAX
//
//
cout << " where in file " << where << endl;
file << " # comment bla bla ... 0.3 \n";
file << 0.2 << endl;
file.flush;
//
}
//
Idea to skip comment in a file ...
func ifstream skipcomment(ifstream &ff)
{
while(1)
{
int where = ff.tellg();
string comment;
ff >> comment;
if ( ! ff.good() ) break;
if( comment(0:0)=="#") {
getline(ff,comment);
cout << " -- #" << comment << endl;
}
else {
ff.seekg(where);
break;
}
}
return ff;
//
//
4.13. PREPROCESSOR
101
}
{
ifstream file("f.txt",binary); //
for windows (pb CRNL EOL translation )
cout << " where " << file.seekg << endl;
file.seekg(where);
file >> xx;
cout << " xx = " << xx << " good ? " << file.good() << endl;
assert(xx==0.1);
skipcomment(file) >> xx;
assert(xx==0.2);
file.seekg(0);
//
rewind ...
cout << " where " << file.tellg() << " " << file.good() << endl;
file >> g[] ;
}
4.12.1
Script arguments
There is a very useful predefined array in FreeFem++ ARGV that contains all the arguments
of the script used in the command line. The following code prints out the first three of these
arguments:
//
version 3.8-1
for(int i=0;i<ARGV.n;++i)
{
cout << ARGV[i] << endl;
}
getARGV(n,defaultvalue)
getARGV(after,defaultvalue)
//
4.13
preprocessor
The preprocessor handles directives for source file inclusion (include script-name.idp),
macro definitions.
There are two types of macros, object-like and function-like. Object-like macros do not
take parameters; function-like macros do. The generic syntax for declaring an identifier as
a macro of each type is, respectively,
macro <identifier>() <replacement token list>
//
EOM a // comment to end
the macro
macro <identifier>(<parameter list>) <replacement token list>
//
EOM
//
102
CHAPTER 4. SYNTAX
1
2
3
4
//
//
quoting parameter the {} are remove
toto({real i=0;int j=0;cout << i << " " << j << endl;})
//
and only one level of {} are remove
toto({{real i=0;int j=0;cout << i << " " << j << endl;}})
: macro toto(i )
i //
: // quoting parameter the \{\} are remove
:
real i=0;int j=0;cout << i << " " << j << endl;
: // and only one level of \{\} are remove
:
{real i=0;int j=0;cout << i << " " << j << endl;}
Use a macro as parameter of macro to transforme full matrix in formal array like in :
real[int,int] CC(7,7),EE(6,3),EEps(4,4);
macro VIL6(v,i) [ v(1,i), v(2,i),v(4,i), v(5,i),v(6,i) ]
macro VIL3(v,i) [ v(1,i), v(2,i) ]
//
apply v on array
macro VV6(v,vv) [ v(vv,1), v(vv,2),
v(vv,4), v(vv,5), v(vv,6) ]
macro VV3(v,vv) [ v(vv,1), v(vv,2) ]
//
so formal matrix to build
func C5x5 = VV6(VIL6,CC);
func E5x2 = VV6(VIL3,EE);
func Eps = VV3(VIL3,EEps);
//
EOM
//
EOM
element :
//
EOM
//
EOM
problem..
: real[int,int] CC(7,7),EE(6,3),EEps(4,4);
:
:
macro VIL6(v,i )
[ v(1,i), v(2,i),v(4,i), v(5,i),v(6,i) ] //
:
macro VIL3(v,i )
[ v(1,i), v(2,i) ] // EOM
: // apply v on array element :
:
macro VV6(v,vv )
[ v(vv,1), v(vv,2),
:
v(vv,4), v(vv,5), v(vv,6) ] // EOM
:
macro VV3(v,vv )
[ v(vv,1), v(vv,2) ] // EOM
: // so formal matrix to build problem..
: func C5x5 =
:
[
[ CC(1,1), CC(2,1),CC(4,1), CC(5,1),CC(6,1) ]
[ CC(1,2), CC(2,2),CC(4,2), CC(5,2),CC(6,2) ] ,
1 :
[ CC(1,4), CC(2,4),CC(4,4), CC(5,4),CC(6,4) ] ,
[ CC(1,5), CC(2,5),CC(4,5), CC(5,5),CC(6,5) ] ,
26 :
1 :
1 :
27 :
103
28 :
finally the operator # to do concatenation of parameter: to build vectorial operation, like in
//
EOM
: macro div(u )
(dx(u#1)+ dy(u#2)) //EOM
: mesh Th=square(2,2); fespace Vh(Th,P1);
: Vh v1=x,v2=y;
: cout << int2d(Th)(
(dx(v1)+ dy(v2)) ) << endl;
//
//
EOM
empty line
the result:
36 :
37 :
38 :
macro foo(i,j,k )
i j k//EOM
// empty line
int [ int] a(10 );
To defined macro in a macro you can use the two new word NewMacro , EndMacro key
word to set and and claose de macro definition (version 3.11, and not well tested).
4.14
Exception handling
In the version 2.3 of FreeFem++ , exception handing was added as in C++. But today only
the C++ exceptions are caught. Note that in C++ all the errors attached to ExecError,
assert, exit, ... call exceptions too so it may be hard to find the cause of the error.
The exceptions handle all ExecError:
Example 4.10 A simple example: catch a division by zero:
real a;
104
CHAPTER 4. SYNTAX
try {
a=1./0.;
}
catch (...)
//
in versions > 2.3 all exceptions can be caught
{
cout << " Catch an ExecError " << endl;
a =0;
}
The output is
1/0 : d d d
current line = 3
Exec error : Div by 0
-- number :1
Try:: catch (...) exception
Catch an ExecError
//
solve the problem
//
to see the result
: " << -cpu+clock()
//
The output is
-- square mesh : nb vertices =36 , nb triangles = 50 ...
Nb of edges on Mortars = 0
Nb of edges on Boundary = 20, neb = 20
Nb Mortars 0
number of real boundary edges 20
Number of Edges
= 85
catch all
105
106
CHAPTER 4. SYNTAX
Chapter 5
Mesh Generation
5.1
Let us begin with the two important keywords border and buildmesh
All examples in this section come from the files mesh.edp and tablefunction.edp.
5.1.1
Square
generates a 4 5 grid in the unit square [0, 1]2 . The labels of the boundaries are shown in
Fig. 5.1. To construct a n m grid in the rectangle [x0 , x1 ] [y0 , y1 ], proceeds as follows:
la b e l= 3
la b e l
= 2
la b e l
= 4
la b e l= 1
107
108
Adding the named parameter label=labs will change the 4 default label numbers to labs[i-1],
for example int[int] labs=[11,12,13,14],
and adding the named parameter region=10 will change the region number to 10, for
instance (v 3.8).
To see all these fags at work, try the file examples++/square-mesh.edp :
for (int i=0;i<5;++i)
{
int[int] labs=[11,12,13,14];
mesh Th=square(3,3,flags=i,label=labs,region=10);
plot(Th,wait=1,cmm=" square flags = "+i );
}
5.1.2
Border
Boundaries are defined piecewise by parametrized curves. The pieces can only intersect at
their endpoints, but it is possible to join more than two endpoints. This can be used to
structure the mesh if an area thouches a border and create new regions by dividing larger
ones:
int upper = 1;
int others = 2;
int inner = 3;
border
border
border
border
border
border
border
border
C01(t=0,1){x
C02(t=0,1){x
C03(t=0,1){x
C04(t=0,1){x
C05(t=0,1){x
C06(t=0,1){x
C11(t=0,1){x
C12(t=0,1){x
=
=
=
=
=
=
=
=
0;
1.5-1.5*t;
1.5;
1+0.5*t;
0.5+0.5*t;
0.5*t;
0.5;
0.5+0.5*t;
y
y
y
y
y
y
y
y
=
=
=
=
=
=
=
=
-1+t;
-1;
-t;
0;
0;
0;
-0.5*t;
-0.5;
label
label
label
label
label
label
label
label
=
=
=
=
=
=
=
=
upper;}
upper;}
upper;}
others;}
others;}
others;}
inner;}
inner;}
109
y = -0.5+0.5*t;
label = inner;}
int n = 10;
plot(C01(-n)+C02(-n)+C03(-n)+C04(-n)+C05(-n)+C06(-n)+
C11(n)+C12(n)+C13(n), 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, wait=true);
//
figure 5.3
cout << "Part 1 has region number " << Th(0.75, -0.25).region << endl;
cout << "Part 2 has redion number " << Th(0.25, -0.25).region << endl;
Triangulation keywords assume that the domain is defined as being on the left (resp right)
of its oriented parameterized boundary
j = {(x, y) | x = x (t), y = y (t), aj t bj }
To check the orientation plot t 7 (x (t), y (t)), t0 t t1 . If it is as in Fig. 5.4, then the
domain lies on the shaded area, otherwise it lies on the opposite side
The general expression to define a triangulation with buildmesh is
mesh
where mj are positive or negative numbers to indicate how many vertices should be on
j , = Jj=1 J , and the optional parameter (separed with comma) can be
nbvx=<int value> , to set the maximal number of vertices in the mesh.
fixedborder=<bool value> , to say if the mesh generator can change the boundary
mesh or not (by default the boundary mesh can change; beware that with periodic
boundary conditions (see. 6), it can be dangerous .
110
t = t1
G
t = t1
t = t0
(x = j x(t), y = j y(t))
t = t0
(d j x(t)/d t, d j y(t)/d t)
t = t1
t = t0
(x = t, y = -t)
(x = t, y = t)
Note 5.2 Notice that the orientation is changed by b(-30) in 5th line. In 7th line,
ps="fileName" is used to generate a postscript file with identification shown on the figure.
Note 5.3 Borders are evaluated only at the time plot or buildmesh is called so the global
variable are defined at this time andhere since r is changed between the two border calls the
following code will not work because the first border will be computed with r=0.3:
111
real r=1;
border a(t=0,2*pi){ x=r*cos(t); y=r*sin(t);label=1;}
r=0.3
;
border b(t=0,2*pi){ x=r*cos(t); y=r*sin(t);label=1;}
mesh Thwithhole
= buildmesh(a(50)+b(-30));
//
bug (a trap) because
//
the two circle have the same radius = 0.3
5.1.3
Multy Border
(Version 3.30)
Some time it can be useful to to make array of border, but unfortunately it is incompatible with the FreeFem++ syntax. So to bypass this , thes dea is small, if the number of segment of the discretization n is a array, we make a loop on all the value of the
array, and the index variable i of the loop is defined after parameter definition, like in
border a(t=0,2*pi;i) ....
A first very small example:
1: border a(t=0,2*pi;i){ x=(i+1)*cos(t); y=(i+1)*sin(t);label=1;}
2: int[int] nn=[10,20,30];
3: plot(a(nn));
//
plot 3 circle with 10,20,30 points ..
And more complex exemple (take from mesh.edp example ) to defined a square with small
circies:
//
real[int] xx=[0,1,1,0],
yy=[0,0,1,1];
//
real[int] RC=[ 0.1, 0.05, 0.05, 0.1],
XC= [0.2,0.8,0.2,0.8],
YC= [0.2,0.8,0.8,0.2];
int[int] NC=[-10,-11,-12,13];
//
//
border bb(t=0,1;i)
{
//
i is the the index variable of the multi border loop
int ii = (i+1)%4; real t1 = 1-t;
x = xx[i]*t1 + xx[ii]*t;
y = yy[i]*t1 + yy[ii]*t;
label = 0; ;
}
border cc(t=0,2*pi;i)
{
x = RC[i]*cos(t)+XC[i];
y = RC[i]*sin(t)+YC[i];
label = i+1;
}
int[int] nn=[4,4,5,7]; //
4 border , with 4,4,5,7 segment respectively .
plot(bb(nn),cc(NC),wait=1);
mesh th= buildmesh(bb(nn)+cc(NC)) ;
plot(th,wait=1);
112
5.1.4
Users who want to read a triangulation made elsewhere should see the structure of the file
generated below:
border C(t=0,2*pi) { x=cos(t); y=sin(t); }
mesh Th = buildmesh(C(10));
savemesh("mesh_sample.msh");
1 1
5
3
4
8
1 2
9
q 1 = (0.309016994375, 0.951056516295)
..
..
..
.
.
.
14
q = (0.309016994375, 0.951056516295)
1 3
2
9
7
6
8
1 0
1 5
The
..
.
1 3
1
1 2
vertices of T1 are q 9 , q 12 , q 10 .
..
..
.
.
1 4
1 0
1 4
1 3
The
..
.
edge
..
.
//
113
Explanation
nv
nt
ne
1
1
boundary label=1
qy
qx
2
2
qx
qy
boundary label=1
-0.309016994375 -0.951056516295 1
9 12 10 0
5960
9 10 6 0
651
521
10 6 1
qx14
qy14
11 12 13
21 22 23
boundary label=1
region label=0
region label=0
102
boundary label=1
the
unit square
border right(t=0,1){ x=1; y=t; label=5;};
border ceiling(t=1,0){ x=t; y=1; label=5;};
border left(t=1,0){ x=0; y=t; label=5;};
int n=10;
mesh th= buildmesh(floor(n)+right(n)+ceiling(n)+left(n));
savemesh(th,"toto.am_fmt");
//
format "formated Marrocco"
savemesh(th,"toto.Th");
//
format database db mesh "bamg"
savemesh(th,"toto.msh");
//
format freefem
savemesh(th,"toto.nopo");
//
modulef format see [14]
mesh th2 = readmesh("toto.msh");
fespace femp1(th,P1);
femp1 f = sin(x)*cos(y),g;
{
//
save solution
ofstream file("f.txt");
file << f[] << endl;
}
//
close the file (end block)
{
//
read
ifstream file("f.txt");
file >> g[] ;
}
//
close reading file (end block)
114
fespace Vh2(th2,P1);
Vh2 u,v;
plot(g);
//
find u such that
//
u + u = g in ,
u
//
u = 0 on 1 and n
= g on 2
solve pb(u,v) =
int2d(th)( u*v - dx(u)*dx(v)-dy(u)*dy(v) )
+ int2d(th)(-g*v)
+ int1d(th,5)( g*v)
+ on(1,u=0) ;
plot (th2,u);
5.1.5
//
u
n
= g on 2
Mesh Connectivity
//
mesh Th=square(2,2);
//
get data of the mesh
int nbtriangles=Th.nt;
cout << " nb of Triangles = " << nbtriangles << endl;
for (int i=0;i<nbtriangles;i++)
for (int j=0; j <3; j++)
cout << i << " " << j << " Th[i][j] = "
<< Th[i][j] << " x = "<< Th[i][j].x << " , y= "<< Th[i][j].y
<< ", label=" << Th[i][j].label << endl;
//
//
fespace femp1(Th,P1);
femp1 Thx=x,Thy=y;
//
int nbvertices=Th.nv;
cout << " nb of vertices = " << nbvertices << endl;
for (int i=0;i<nbvertices;i++)
cout << "Th(" <<i << ") : "
//
<< endl;
<< Th(i).x << " " << Th(i).y << " " << Th(i).label
//
v 2.19
<< "
old method: " << Thx[][i] << " " << Thy[][i] << endl;
//
//
//
//
//
//
info of a triangle
//
new in version 2.19
//
new in version 2.19
same as region in this case.
115
//
------------------------------------------------------fespace femp0(Th,P0);
femp0 nuT;
//
a P0 function to get triangle numbering
for (int i=0;i<Th.nt;i++)
nuT[][i]=i;
femp0 nuReg=region;
//
a P0 function to get the region number
//
inquire
int it0=nuT(0.55,0.6);
//
number of triangle Ths containing (0.55,0,6);
int nr0=nuReg(0.55,0.6); //
number of region of Ths containing (0.55,0,6);
//
//
dump
-------------------------------------------------------
cout << " point (0.55,0,6) :triangle number " << it00 << " " << it00
<< ", region = " << nr0 << " == " << nr00 << ", area K " << area00 << endl;
//
int k=0,l=1,e=1;
Th.nbe ; //
return the number of boundary element
Th.be(k);
//
return the boundary element k {0, ..., T h.nbe 1}
Th.be(k)[l];
//
return the vertices l {0, 1} of boundary elmt k
Th.be(k).Element ;
//
return the triangle containing the boundary elmt
k
Th.be(k).whoinElement ;
containing
//
//
the boundary elmt k
Th[k].adj(e) ;
//
return adjacent triangle to k by edge e, and change
//
the value of e to the corresponding edge in the adjacent triangle
Th[k] == Th[k].adj(e)
//
non adjacent triangle return the same
Th[k] != Th[k].adj(e)
//
true adjacent triangle
cout << " print mesh connectivity " << endl;
int nbelement = Th.nt;
for (int k=0;k<nbelement;++k)
cout << k << " : " << int(Th[k][0]) << " " << int(Th[k][1])
<< " " << int(Th[k][2])
<< " , label " << Th[k].label << endl;
//
for (int k=0;k<nbelement;++k)
for (int e=0,ee;e<3;++e)
//
remark FH hack: set ee to e, and ee is change by method adj,
//
in () to make difference with named parameters.
cout << k << " " << e << " <=> " << int(Th[k].adj((ee=e))) << " " << ee
<< " adj: " << ( Th[k].adj((ee=e)) != Th[k]) << endl;
//
note : if k == int(Th[k].adj(ee=e)) not adjacent element
116
real[int] bb(4);
boundingbox(Th,bb);
//
//
bb[0] = xmin, bb[1] = xmax, bb[2] = ymin, bb[3] =ymax
cout << "\n boundingbox xmin: " << bb[0] << " xmax: " << bb[1]
<< " ymin: " << bb[2] << " ymax: " << bb[3] << endl;
}
2
2
1
1
boundingbox
5.1.6
117
The third column of each line is left untouched by the triangulate command. But you
can use this third value to define a table function with rows of the form: x y f(x,y).
The following example shows how to make a mesh from the file xyf with the format stated
just above. The command triangulate command use only use 1st and 2nd rows.
mesh Thxy=triangulate("xyf"); //
build the Delaunay mesh of the convex hull
//
points are defined by the first 2 columns of file xyf
plot(Thxy,ps="Thxyf.ps");
//
(see figure 5.8)
fespace Vhxy(Thxy,P1);
Vhxy fxy;
//
{ ifstream file("xyf");
real xx,yy;
for(int i=0;i<fxy.n;i++)
file >> xx >>yy >> fxy[][i];
//
create a P1 interpolation
//
the function
//
//
118
}
plot(fxy,ps="xyf.eps");
//
One new way to build a mesh is to have two arrays one the x values and the other for the y
values (version 2.23-2):
Vhxy xx=x,yy=y;
mesh Th=triangulate(xx[],yy[]);
5.2
//
To define a Finite Element space on a boundary, we came up with the idea of a mesh with no
internal points (call empty mesh). It can be useful to handle Lagrange multipliers in mixed
and mortar methods.
So the function emptymesh remove all the internal points of a mesh except points on
internal boundaries.
{
//
//
assert(version>=1.40);
border a(t=0,2*pi){ x=cos(t); y=sin(t);label=1;}
mesh Th=buildmesh(a(20));
Th=emptymesh(Th);
plot(Th,wait=1,ps="emptymesh-1.eps");
//
//
new stuff 2004 emptymesh (version 1.40)
//
-- useful to build Multiplicator space
//
build a mesh without internal point
//
of peusdo sub domain
//
----assert(version>=1.40);
mesh Th=square(10,10);
int[int] ssd(Th.nt);
for(int i=0;i<ssd.n;i++)
//
build the pseudo region numbering
{ int iq=i/2;
//
because 2 triangle per quad
int ix=iq%10;
//
int iy=iq/10;
//
ssd[i]= 1 + (ix>=5) + (iy>=5)*2;
}
Th=emptymesh(Th,ssd);
//
build emtpy with
//
all edge e = T 1 T 2 and ssd[T 1] 6= ssd[T 2]
plot(Th,wait=1,ps="emptymesh-2.eps");
//
see figure 5.11
savemesh(Th,"emptymesh-2.msh");
5.3. REMESHING
Figure 5.10:
ary
119
The empty mesh with bound- Figure 5.11: An empty mesh defined from a
pseudo region numbering of triangle
5.3
Remeshing
5.3.1
Movemesh
Meshes can be translated, rotated and deformed by movemesh; this is useful for elasticity
to watch the deformation due to the displacement (x, y) = (1 (x, y), 2 (x, y)) of shape. It
is also useful to handle free boundary problems or optimal shape problems.
If is triangulated as Th (), and is a displacement vector then (Th ) is obtained by
mesh
Th=movemesh(Th,[1,2]);
Sometimes the transformed mesh is invalid because some triangle have flip over (now has
negative area).To spot such problems one may check the minimum triangle area in the
transformed mesh with checkmovemesh before any real transformation.
Example 5.2 1 (x, y) = x+k sin(y )/10), 2 (x, y) = y +k cos(y)/10) for a big number
k > 1.
verbosity=4;
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;};
func uu= sin(y*pi)/10;
func vv= cos(x*pi)/10;
mesh Th = buildmesh ( a(6) + b(4) + c(4) +d(4) + e(4) + f(6));
plot(Th,wait=1,fill=1,ps="Lshape.eps");
//
see figure 5.12
real coef=1;
real minT0= checkmovemesh(Th,[x,y]);
//
the min triangle area
120
while(1)
//
find a correct move mesh
{
real minT=checkmovemesh(Th,[x+coef*uu,y+coef*vv]); //
the min triangle area
if (minT > minT0/5) break ;
//
if big enough
coef/=1.5;
}
Th=movemesh(Th,[x+coef*uu,y+coef*vv]);
plot(Th,wait=1,fill=1,ps="movemesh.eps");
//
Figure 5.13:
moved L-shape
Note 5.4 Consider a function u defined on a mesh Th. A statement like Th=movemesh(Th...)
does not change u and so the old mesh still exists. It will be destroyed when no function use
it. A statement like u = u redefines u on the new mesh Th with interpolation and therefore
destroys the old Th if u was the only function using it.
Example 5.3 (movemesh.edp) Now, we given an example of moving mesh with a lagrangian
function u defined on the moving mesh.
//
mesh Th=square(10,10);
fespace Vh(Th,P1);
real t=0;
//
//
--//
the problem is how to build data without interpolation
so the data u is moving with the mesh as you can see in the plot
//
---
Vh u=y;
for (int i=0;i<4;i++)
{
t=i*0.1;
Vh f= x*t;
real minarea=checkmovemesh(Th,[x,y+f]);
if (minarea >0 )
//
movemesh will be ok
121
Th=movemesh(Th,[x,y+f]);
cout << " Min area
real[int] tmp(u[].n);
tmp=u[];
//
save the value
u=0;
//
to change the FEspace and mesh associated with u
u[]=tmp;
//
set the value of u without any mesh update
plot(Th,u,wait=1);
};
//
In this program, since u is only defined on the last mesh, all the
//
previous meshes are deleted from memory.
//
--------
5.4
diam(Tk )
for all Tk Th
5.5
Adaptmesh
The function
f (x, y) = 10.0x3 + y 3 + tan1 [/(sin(5.0y) 2.0x)]
= 0.0001
sharply varies in value and the initial mesh given by one of the commands of Section 5.1
cannot reflect its sharp variations.
122
Example 5.4
real
real
real
func
eps = 0.0001;
h=1;
hmin=0.05;
f = 10.0*x3+y3+h*atan2(eps,sin(5.0*y)-2.0*x);
mesh Th=square(5,5,[-1+2*x,-1+2*y]);
fespace Vh(Th,P1);
Vh fh=f;
plot(fh);
for (int i=0;i<2;i++)
{
Th=adaptmesh(Th,fh);
fh=f;
plot(Th,fh,wait=1);
}
//
F ir s t
a d a p t a t io n
In it ia l
m e s h
S e c o n d
a d a p t a t io n
Figure 5.14: 3D graphs for the initial mesh and 1st and 2nd mesh adaptation
FreeFem++ uses a variable metric/Delaunay automatic meshing algorithm. The command
5.5. ADAPTMESH
123
of a function (formula or FE-function). Mesh adaptation is a very powerful tool when the
solution of a problem varies locally and sharply.
Here we solve the problem (2.1)-(2.2), when f = 1 and is a L-shape domain.
b e
b d
b c
b f
b b
b a
Figure 5.15: L-shape domain and its bound- Figure 5.16:
ary name
adaptation
Example 5.5 (Adapt.edp) The solution has the singularity r3/2 , r = |x | at the point
of the intersection of two lines bc and bd (see Fig. 5.15).
border ba(t=0,1.0){x=t;
y=0; label=1;};
border bb(t=0,0.5){x=1;
y=t; label=1;};
border bc(t=0,0.5){x=1-t; y=0.5;label=1;};
border bd(t=0.5,1){x=0.5; y=t; label=1;};
border be(t=0.5,1){x=1-t; y=1; label=1;};
border bf(t=0.0,1){x=0;
y=1-t;label=1;};
mesh Th = buildmesh ( ba(6)+bb(4)+bc(4)+bd(4)+be(4)+bf(6) );
fespace Vh(Th,P1);
//
set FE space
Vh u,v;
//
set unknown and test function
func f = 1;
real error=0.1;
//
level of error
problem Poisson(u,v,solver=CG,eps=1.0e-6) =
int2d(Th)( dx(u)*dx(v) + dy(u)*dy(v))
- int2d(Th) ( f*v )
+ on(1,u=0) ;
for (int i=0;i< 4;i++)
{
Poisson;
Th=adaptmesh(Th,u,err=error);
error = error/2;
} ;
plot(u);
To speed up the adaptation the default parameter err of adaptmesh is changed by hand;
it specifies the required precision, so as to make the new mesh finer or coarser.
The problem is coercive and symmetric, so the linear system can be solved with the conjugate
gradient method (parameter solver=CG with the stopping criteria on the residual, here
124
eps=1.0e-6). By adaptmesh, the slope of the final solution is correctly computed near
the point of intersection of bc and bd as in Fig. 5.16.
This method is described in detail in [13]. It has a number of default parameters which can
be modified :
Si f1,f2 sont des functions et thold, Thnew des maillages.
Thnew = adaptmesh(Thold, f1 ... );
Thnew = adaptmesh(Thold, f1,f2 ... ]);
Thnew = adaptmesh(Thold, [f1,f2] ... );
The additional paramters of adaptmesh not written here, hence the ...
hmin= Minimum edge size. (val is a real. Its default is related to the size of the domain
to be meshed and the precision of the mesh generator).
hmax= Maximum edge size. (val is a real. It defaults to the diameter of the domain to be
meshed)
err= P1 interpolation error level (0.01 is the default).
errg= Relative geometrical
error. By default this error is 0.01, and in any case it must be
lower than 1/ 2. Meshes created with this option may have some edges smaller than
the -hmin due to geometrical constraints.
nbvx= Maximum number of vertices generated by the mesh generator (9000 is the default).
1
err coef2
|H|
max(CutOff, ||)
p
(5.1)
5.5. ADAPTMESH
125
1
err coef2
|H|
sup() inf()
p
.
(5.2)
cutoff= lower limit for the relative error evaluation (1.0e-6 is the default).
verbosity= informational messages level (can be chosen between 0 and ). Also changes
the value of the global variable verbosity (obsolete).
inquire= To inquire graphically about the mesh (false is the default).
splitpbedge= If true, splits all internal edges in half with two boundary vertices (true is
the default).
maxsubdiv= Changes the metric such that the maximum subdivision of a background edge
is bound by val (always limited by 10, and 10 is also the default).
rescaling= if true, the function with respect to which the mesh is adapted is rescaled to
be between 0 and 1 (true is the default).
keepbackvertices= if true, tries to keep as many vertices from the original mesh as
possible (true is the default).
isMetric= if true, the metric is defined explicitly (false is the default). If the 3 functions
m11 , m12 , m22 are given, they directly define a symmetric matrix field whose Hessian
is computed to define a metric. If only one function is given, then it represents the
isotropic mesh size at every point.
For example, if the partial derivatives fxx (= 2 f /x2 ), fxy (= 2 f /xy), fyy
(= 2 f /y 2 ) are given, we can set
Th=adaptmesh(Th,fxx,fxy,fyy,IsMetric=1,nbvx=10000,hmin=hmin);
power= exponent power of the Hessian used to compute the metric (1 is the default).
thetamax= minimum corner angle of in degrees (default is 10 ) where the corner is ABC
and the angle is the angle of the two vectors AB, BC, (0 imply no corner, 90 imply
perp. corner , ...).
splitin2= boolean value. If true, splits all triangles of the final mesh into 4 sub-triangles.
metric= an array of 3 real arrays to set or get metric data information. The size of these
three arrays must be the number of vertices. So if m11,m12,m22 are three P1 finite elements related to the mesh to adapt, you can write: metric=[m11[],m12[],m22[]]
(see file convect-apt.edp for a full example)
nomeshgeneration= If true, no adapted mesh is generated (useful to compute only a
metric).
126
5.6
//
//
//
Trunc
Two operators have been introduce to remove triangles from a mesh or to divide them.
Operator trunc has two parameters
label= sets the label number of new boundary item (one by default)
split= sets the level n of triangle splitting. each triangle is splitted in n n ( one by
default).
To create the mesh Th3 where alls triangles of a mesh Th are splitted in 33 , just write:
mesh Th3 = trunc(Th,1,split=3);
5.7. SPLITMESH
127
The truncmesh.edp example construct all trunc mesh to the support of the basic
function of the space Vh (cf. abs(u)>0), split all the triangles in 55, and put a label
number to 2 on new boundary.
mesh Th=square(3,3);
fespace Vh(Th,P1);
Vh u;
int i,n=u.n;
u=0;
for (i=0;i<n;i++)
//
{
u[][i]=1;
//
plot(u,wait=1);
mesh Sh1=trunc(Th,abs(u)>1.e-10,split=5,label=2);
plot(Th,Sh1,wait=1,ps="trunc"+i+".eps");
//
u[][i]=0;
}
//
plot the mesh of
the functions support
//
reset
Figure 5.20: mesh of support the function Figure 5.21: mesh of support the function
P1 number 0, splitted in 55
P1 number 6, splitted in 55
5.7
Splitmesh
//
new stuff 2004 splitmesh (version 1.37)
assert(version>=1.37);
border a(t=0,2*pi){ x=cos(t); y=sin(t);label=1;}
mesh Th=buildmesh(a(20));
plot(Th,wait=1,ps="nosplitmesh.eps");
//
see figure 5.22
Th=splitmesh(Th,1+5*(square(x-0.5)+y*y));
plot(Th,wait=1,ps="splitmesh.eps");
//
see figure 5.23
128
5.8
Figure 5.23:
all left mesh triangle
is
split
conformaly
in
int(1+5*(square(x-0.5)+y*y)2
triangles.
Meshing Examples
Fig.
5.24
Fig.
5.25
//
(0 ,-1 0 )
Figure 5.24:
side
129
d
c
th
b
h
T H
(1 0 ,-1 0 )
//
Fig.
5.26
//
Fig.
5.27
Figure 5.26:
boundary
Domain with Cardioid curve Figure 5.27: Domain with Cassini Egg curve
boundary
130
//
Fig 5.28
131
G 4
L 7
L 5
L 4
L 1
L 3
G 3
L 2
L 1
G 1
G 2
Figure 5.28:
curves
(-c a ,c b )
L 1
C 1
C 3
( tip ,d )
C 2
( tip ,- d )
s e g 1
R
s e g 2
L 2
(c a ,c b )
B
L 6
132
int m=5;
real a=1.5, b=2, c=0.7, e=0.01;
border F(t=0,2*pi) { x=a*cos(t); y=b*sin(t); }
border E1(t=0,2*pi) { x=0.2*cos(t)-0.5; y=0.2*sin(t)+0.5; }
border E2(t=0,2*pi) { x=0.2*cos(t)+0.5; y=0.2*sin(t)+0.5; }
func real st(real t) {
return sin(pi*t)-pi/2;
}
border C1(t=-0.5,0.5) { x=(1-d)*c*cos(st(t)); y=(1-d)*c*sin(st(t)); }
border C2(t=0,1){x=((1-d)+d*t)*c*cos(st(0.5));y=((1-d)+d*t)*c*sin(st(0.5));}
border C3(t=0.5,-0.5) { x=c*cos(st(t)); y=c*sin(st(t)); }
border C4(t=0,1) { x=(1-d*t)*c*cos(st(-0.5)); y=(1-d*t)*c*sin(st(-0.5));}
border C0(t=0,2*pi) { x=0.1*cos(t); y=0.1*sin(t); }
mesh Th=buildmesh(F(10*m)+C1(2*m)+C2(3)+C3(2*m)+C4(3)
+C0(m)+E1(-2*m)+E2(-2*m));
plot(Th,ps="SmileFace.eps",bw=1);
}
//
see Fig.
5.32
5.9
Changing the label of elements and border elements will be done using the keyword change.
The parameters for this command line are for a two dimensional and dimensional case:
label = is a vector of integer that contains successive pair of the old label number to the
new label number .
region = is a vector of integer that contains successive pair of the old region number to
new region number.
5.9. HOW TO CHANGE THE LABEL OF ELEMENTS AND BORDER ELEMENTS OF A MESH133
E 1
(-b ,a )
E 2
T o p 2
L o a d T o p 1
L e ft
C 4
E 1
C 2
C 1
B o t1
B o t2
F ix 1 F ix 2
R ig h t
B o t3 (b ,-a )
flabel = is a integer function with given the new value of the label (version 3.21).
fregion = is a integer function with given the new value of the region .
These vectors are composed of nl successive pair of number O, N where nl is the number
(label or region) that we want to change. For example, we have
label = [O1 , N1 , ..., Onl , Nnl ]
region = [O1 , N1 , ..., Onl , Nnl ]
(5.3)
(5.4)
1:
2: mesh Th1=square(10,10);
3: mesh Th2=square(20,10,[x+1,y]);
4: verbosity=3;
5: int[int] r1=[2,0], r2=[4,0];
6: plot(Th1,wait=1);
7: Th1=change(Th1,label=r1);
//
Change the label of Edges 2 in 0.
8: plot(Th1,wait=1);
9: Th2=change(Th2,label=r2);
//
Change the label of Edges 4 in 0.
10: mesh Th=Th1+Th2;
//
gluing together of meshes Th1 and Th2
11: cout << " nb lab = " << int1d(Th1,1,3,4)(1./lenEdge)+int1d(Th2,1,2,3)(1./lenEdge)
12:
<< " == " << int1d(Th,1,2,3,4)(1./lenEdge) <<" == " << ((10+20)+10)*2
<< endl;
13: plot(Th,wait=1);
14: fespace Vh(Th,P1);
15: macro Grad(u) [dx(u),dy(u)];
//
definition of a macro
16: Vh u,v;
17: solve P(u,v)=int2d(Th)(Grad(u)*Grad(v))-int2d(Th)(v)+on(1,3,u=0);
18: plot(u,wait=1);
134
gluing different mesh In line 10 of previous file, the method to gluing different mesh
of the same dimension in FreeFem++ is using. This function is the operator + between
meshes. The method implemented need that the point in adjacent mesh are the same.
5.10
5.10.1
cube
From version (3.38-2), a new function cube like the function square in 2d is the simple
way to build cubic object, in plugin msh3 (need load "msh3").
The following code
mesh3 Th=cube(3,4,5);
face
face
face
face
face
face
y = 0;
x = 1,
y = 1,
x = 0,
z = 0,
z = 1,
135
5.10.2
In three dimensions, the file mesh format supported for input and output files by FreeFem++
are the extension .msh and .mesh. These formats are described in the chapter on Mesh Files
in two dimensions.
extension file .msh The structure of the files with extension .msh in 3D is given in Table
5.2. In this structure, nv denotes the number of vertices, ntet the number of tetrahedra and
ntri the number of triangles For each vertex q i , i = 1, , nv , we denote by (qxi , qyi , qzi ) the
x-coordinate, the y-coordinate and the z-coordinate. Each tetrahedra Tk , k = 1, , ntet has
136
ntet
qy1
qy2
..
.
ntri
qz1
qz2
..
.
Vertex label
Vertex label
..
.
qxnv
11
21
..
.
qynv
12
22
..
.
qznv
13
23
..
.
Vertex label
14
24
..
.
(ntet )1
11
21
..
.
(ntet )2
12
22
..
.
(ntet )3
13
23
..
.
(ntet )4
region label
boundary label
boundary label
..
.
(nt ri)1
(ntri )2
(ntri )3
boundary label
region label
region label
..
.
Table 5.2: The structure of mesh file format .msh in three dimensions.
extension file .mesh The data structure for a three dimensional mesh is composed of the
data structure presented in Section 12.1 and a data structure for tetrahedra. The tetrahedra
of a three dimensional mesh are refereed using the following field:
Tetrahedra
(I) NbOfTetrahedrons
( ( @@Vertexji , j=1,4 ) , (I) Ref tet
i
i=1 , NbOfTetrahedrons )
5.10.3
TetGen TetGen is a software developed by Dr. Hang Si of Weierstrass Institute for Applied Analysis and Stochastics of Berlin in Germany [41]. TetGen is a free for research and non-commercial
uses. For any commercial licence utilization, a commercial licence is available upon request to Hang
Si.
This software is a tetrahedral mesh generator of a three dimensional domain defined by its boundary.
The input domain take into account a polyhedral or a piecewise linear complex. This tetrahedralization is a constrained Delaunay tetrahedralization.
The method used in TetGen to control the quality of the mesh is a Delaunay refinement due to
Shewchuk [42]. The quality measure of this algorithm is the Radius-Edge Ratio (see Section 1.3.1
[41] for more details). A theoretical bounds of this ratio of the algorithm of Shewchuk is obtained
for a given complex of vertices, constrained segments and facets of surface mesh, with no input
angle less than 90 degree. This theoretical bounds is 2.0.
The launch of Tetgen is done with the keyword tetg. The parameters of this command line is:
label = is a vector of integer that contains the old labels number at index 2i and the new labels
number at index 2i + 1 of Triangles. This parameters is initialized as label for the keyword
change (5.3).
137
switch = A string expression. This string corresponds to the command line switch of Tetgen see
Section 3.2 of [41].
nbofholes= Number of holes (default value size of holelist/3 (version 3.11) ).
holelist = This array correspond to holelist of tetgenio data structure [41]. A real vector of
size 3 nbofholes. In TetGen, each hole is associated with a point inside this domain.
This vector is xh1 , y1h , z1h , xh2 , y2h , z2h , , where xhi , yih , zih is the associated point with the ith
hole.
nbofregions = Number of regions (size of regionlist/5 (version 3.11) ).
regionlist = This array corresponds to regionlist of tetgenio data structure [41]. The attribute
and the volume constraint of region are given in this real vector of size 5 nbofregions.
The ith region is described by five elements: xcoordinate, ycoordinate and zcoordinate of
a point inside this domain (xi , yi , zi ); the attribute (ati ) and the maximum volume for tetrahedra (mvoli ) for this region. The regionlist vector is: x1 , y1 , z1 , at1 , mvol1 , x2 , y2 , z2 , at2 , mvol2 , .
nboffacetcl= Number of facets constraints size of facetcl/2 (version 3.11) ).
facetcl= This array corresponds to facetconstraintlist of tetgenio data structure [41]. The ith
facet constraint is defined by the facet marker Refif c and the maximum area for faces
mareafi c . The facetcl array is: Ref1f c , mareaf1 c , Ref2f c , mareaf2 c , . This parameters
has no effect if switch q is not selected.
Principal switch parameters in TetGen:
p Tetrahedralization of boundary.
q Quality mesh generation. The bound of Radius-Edge Ratio will be given after the option q.
By default, this value is 2.0.
a Construct with the volumes constraints on tetrahedra. These volumes constraints are defined
with the bound of the previous switch q or in the parameter regionlist.
A Attributes reference to region given in the regionlist. The other regions have label 0.
The option AA gives a different label at each region. This switch work with the option p.
If option r is used, this switch has no effect.
r Reconstructs and Refines a previously generated mesh. This character is only used with the
command line tetgreconstruction.
Y This switch allow to preserve the mesh on the exterior boundary. This switch must be used
to ensure conformal mesh between two adjacents mesh.
YY This switch allow to preserve the mesh on the exterior and interior boundary.
C The consistency of the results mesh is testing by TetGen.
CC The consistency of the results mesh is testing by TetGen and also checks constrained delaunay
mesh (if p switch is selected) or the consistency of Conformal Delaunay (if q switch is
selected).
V Give information of the work of TetGen. More information can be obtained in specified VV
or VVV.
138
transfo = [1, 2, 3] set the displacement vector of transformation (x, y) = [1(x, y), 2(x, y), 3(x
label = set integer label of triangles
orientation= set integer orientation of mesh.
ptmerge = A real expression. When you transform a mesh, some points can be merged. This
parameters is the criteria to define two merging points. By default, we use
ptmerge = 1e 7 V ol(B),
where B is the smallest axis parallel boxes containing the discretized domain of and V ol(B)
is the volume of this box.
We can do a gluing of surface meshes using the process given in Section 5.9. An example to
obtain a three dimensional mesh using the command line tetg and movemesh23 is given in the
file tetgencube.edp.
Example 5.18 (tetgencube.edp)
//
load "msh3"
load "tetgen"
real x0,x1,y0,y1;
x0=1.; x1=2.; y0=0.; y1=2*pi;
mesh Thsq1 = square(5,35,[x0+(x1-x0)*x,y0+(y1-y0)*y]);
func
func
func
func
ZZ1min = 0;
ZZ1max = 1.5;
XX1 = x;
YY1 = y;
file tetgencube.edp
139
//
///////////////////////////////
x0=1.; x1=2.; y0=0.; y1=1.5;
mesh Thsq2 = square(5,8,[x0+(x1-x0)*x,y0+(y1-y0)*y]);
func
func
func
func
ZZ2 = y;
XX2 = x;
YY2min = 0.;
YY2max = 2*pi;
and
The keyword tetgtransfo This keyword correspond to a composition of command line tetg
and movemesh23:
tetgtransfo( Th2, transfo= [1, 2, 3] ), ) = tetg( Th3surf, ),
where Th3surf = movemesh23( Th2,tranfo=[1, 2, 3] ) and Th2 is the input two dimensional
mesh of tetgtransfo.
The parameters of this command line are on the one hand the parameters:
label, switch, regionlist nboffacetcl facetcl
of keyword tetg and on the other hand the parameter ptmerge of keyword movemesh23.
140
Remark: To use tetgtransfo, the results mesh of movemesh23 must be an closed surface
and define one region only. Therefore, the parameter regionlist is defined for one region.
An example of this keyword can be found in line of file buildlayers.edp
y1
y2
..
.
z1
z2
..
.
xnv
ynv
znv
The second way is to give three arrays that correspond respectively to the xcoordinates, ycoordinates
and zcoordinates.
The parameters of this command line are
switch = A string expression. This string corresponds to the command line switch of TetGen
see Section 3.2 of [41].
reftet = An integer expression. set the label of tetrahedra.
label = An integer expression. set the label of triangles.
In the string switch, we cant used the option p and q of tetgen.
5.10.4
Meshes in three dimension can be refined using TetGen with the command line tetgreconstruction.
The parameter of this keyword are
region= an integer array that allow to change the region number of tetrahedra. This array is
defined as the parameter reftet in the keyword change.
label= an integer array that allow to change the label of boundary triangles. This array is defined
as the parameter label in the keyword change.
sizevolume= a reel function. This function allows to constraint volume size of tetrahedra in the
domain. (see example 5.31 to build 3d adapt mesh )
The parameter switch nbofregions, regionlist, nboffacetcl and facetcl of the command line which call TetGen (tetg) is used for tetgrefine.
In the parameter switch=, the character r should be used without the character p. For instance,
see the manual of TetGen [41] for effect of r to other character.
The parameter regionlist allows to define a new volume constraint in the region. The label in
the regionlist will be the previous label of region. This parameter and nbofregions cant
be used with parameter sizevolume.
Example:
141
//
file refinesphere.edp
load "msh3"
load "tetgen"
load "medit"
mesh Th=square(10,20,[x*pi-pi/2,2*y*pi]);
//
//
] pi
2 , f racpi2[]0, 2[
a parametrization of a sphere
func f1 =cos(x)*cos(y);
func f2 =cos(x)*sin(y);
func f3 = sin(x);
//
func
func
func
func
func
func
f1x=sin(x)*cos(y);
f1y=-cos(x)*sin(y);
f2x=-sin(x)*sin(y);
f2y=cos(x)*cos(y);
f3x=cos(x);
f3y=0;
//
M = DF t DF
func m11=f1x2+f2x2+f3x2;
func m21=f1x*f1y+f2x*f2y+f3x*f3y;
func m22=f1y2+f2y2+f3y2;
func perio=[[4,y],[2,y],[1,x],[3,x]];
real hh=0.1;
real vv= 1/square(hh);
verbosity=2;
Th=adaptmesh(Th,m11*vv,m21*vv,m22*vv,IsMetric=1,periodic=perio);
Th=adaptmesh(Th,m11*vv,m21*vv,m22*vv,IsMetric=1,periodic=perio);
plot(Th,wait=1);
verbosity=2;
//
real
func
func
func
Rmin
f1min
f2min
f3min
=
=
=
=
1.;
Rmin*f1;
Rmin*f2;
Rmin*f3;
mesh3 Th3=movemesh23(Th,transfo=[f1min,f2min,f3min]);
real[int] domain = [0.,0.,0.,145,0.01];
mesh3 Th3sph=tetg(Th3,switch="paAAQYY",nbofregions=1,regionlist=domain);
int[int] newlabel = [145,18];
real[int] domainrefine = [0.,0.,0.,145,0.0001];
mesh3 Th3sphrefine=tetgreconstruction(Th3sph,switch="raAQ",reftet=newlabel,
nbofregions=1,regionlist=domain,refinesizeofvolume=0.0001);
int[int] newlabel2 = [145,53];
func fsize = 0.01/(( 1 + 5*sqrt( (x-0.5)2+(y-0.5)2+(z-0.5)2) )3);
mesh3 Th3sphrefine2=tetgreconstruction(Th3sph,switch="raAQ",reftet=newlabel2,
sizeofvolume=fsize);
medit(sphere,Th3sph);
142
medit(isotroperefine ,Th3sphrefine);
medit(anisotroperefine,Th3sphrefine2);
5.10.5
Meshes in three dimensions can be translated rotated and deformed using the command line
movemesh as in the 2D case (see section movemesh in chapiter 5). If is tetrahedrized as Th (),
and (x, y) = (1(x, y, z), 1(x, y, z), 3(x, y, z)) is a displacement vector then (Th ) is obtained
by
mesh3 Th = movemesh( Th, [1, 2, 3], ... );
5.10.6
Layer mesh
In this section, we present the command line to obtain a Layer mesh: buildlayermesh. This
mesh is obtained by extending a two dimensional mesh in the z-axis.
The domain 3d defined by the layer mesh is equal to 3d = 2d [zmin, zmax] where 2d is the
domain define by the two dimensional mesh, zmin and zmax are function of 2d in R that defines
respectively the lower surface and upper surface of 3d .
For a vertex of a two dimensional mesh Vi2d = (xi , yi ), we introduce the number of associated
vertices in the zaxis Mi + 1. We denote by M the maximum of Mi over the vertices of the two
dimensional mesh. This value are called the number of layers (if i, Mi = M then there are M
layers in the mesh of 3d ). Vi2d generated M + 1 vertices which are defined by
j = 0, . . . , M,
3d
= (xi , yi , i (zi,j )),
Vi,j
143
upper surface
Middle surface
Lower surface
zmax(Vi2d ) zmin(Vi2d )
.
M
3d
3d
3d
3d
3d
3d
Hj = (Vi1,j
, Vi2,j
, Vi3,j
, Vi1,j+1
, Vi2,j+1
, Vi3,j+1
).
144
zbound = [zmin,zmax] where zmin and zmax are functions expression. Theses functions define
the lower surface mesh and upper mesh of surface mesh.
coef = A function expression between [0,1]. This parameter is used to introduce degenerate
element in mesh. The number of associated points or vertex Vi2d is the integer part of
coef (Vi2d )M .
region = This vector is used to initialized the region of tetrahedra. This vector contain successive
pair of the 2d region number at index 2i and the corresponding 3d region number at index
2i + 1, like (5.3). become the
labelmid = This vector is used to initialized the 3d labels number of the vertical face or mid
face form the 2d label number. This vector contains successive pair of the 2d label number
at index 2i and the corresponding 3d label number at index 2i + 1, like (5.3).
labelup = This vector is used to initialized the 3d label numbers of the upper/top face form the
2d region number. This vector contains successive pair of the 2d region number at index 2i
and the corresponding 3d label number at index 2i + 1, like (5.3).
labeldown = Same as the previous case but for the lower/down face label .
Moreover, we also add post processing parameters that allow to moving the mesh. These parameters
correspond to parameters transfo, facemerge and ptmerge of the command line movemesh.
The vector region, labelmid, labelup and labeldown These vectors are composed of nl
successive pairs of number Oi , Nl where nl is the number (label or region) that we want to get.
An example of this command line is given in buildlayermesh.edp.
Example 5.20 (cube.idp)
load "medit"
load "msh3"
func mesh3 Cube(int[int] & NN,real[int,int] &BB ,int[int,int] & L)
{
//
first build the 6 faces of the hex.
real x0=BB(0,0),x1=BB(0,1);
real y0=BB(1,0),y1=BB(1,1);
real z0=BB(2,0),z1=BB(2,1);
int nx=NN[0],ny=NN[1],nz=NN[2];
mesh Thx = square(nx,ny,[x0+(x1-x0)*x,y0+(y1-y0)*y]);
int[int] rup=[0,L(2,1)], rdown=[0,L(2,0)],
rmid=[1,L(1,0), 2,L(0,1), 3, L(1,1), 4, L(0,0) ];
mesh3 Th=buildlayers(Thx,nz,
zbound=[z0,z1],
labelmid=rmid,
labelup = rup,
labeldown = rdown);
return Th;
}
145
L=[[1,2],[3,4],[5,6]];
//
//
mesh3 Th=Cube(NN,BB,L);
medit("Th",Th);
//
number of layers
//
height 2 pi
//
trick function:
func deg= max(.01,y/max(x/HH,0.4) /RR);
//
the function defined the
proportion
//
of number layer close to axis with reference MaxLayersT
mesh3 Th3T=buildlayers(Th2,coef= deg, MaxLayersT,
zbound=[zminT,zmaxT],transfo=[fx,fy,fz],
facemerge=0, region=r1T, labelmid=r2T);
medit("cone",Th3T);
//
see figure 5.37
//
file buildlayermesh.edp
load "msh3"
load "tetgen"
//
int C1=99, C2=98;
border C01(t=0,pi){ x=t; y=0;
label=1;}
border C02(t=0,2*pi){ x=pi; y=t; label=1;}
border C03(t=0,pi){ x=pi-t; y=2*pi;
label=1;}
border C04(t=0,2*pi){ x=0;
y=2*pi-t; label=1;}
border C11(t=0,0.7){ x=0.5+t; y=2.5;
border C12(t=0,2){ x=1.2;
y=2.5+t;
border C13(t=0,0.7){ x=1.2-t; y=4.5;
label=C1;}
label=C1;}
label=C1;}
//
Test 1
could be anything
146
Figure 5.36: the mesh of a cube made with Figure 5.37: the mesh of a cone made with
cube.edp
cone.edp
y=4.5-t; label=C1;}
C21(t=0,0.7){ x= 2.3+t;
C22(t=0,2){
x=3;
C23(t=0,0.7){
x=3-t;
C24(t=0,2){
x=2.3;
y=2.5; label=C2;}
y=2.5+t; label=C2;}
y=4.5; label=C2;}
y=4.5-t; label=C2;}
mesh Th=buildmesh(
C01(10)+C02(10)+ C03(10)+C04(10)
+ C11(5)+C12(5)+C13(5)+C14(5)
+ C21(-5)+C22(-5)+C23(-5)+C24(-5));
mesh Ths=buildmesh(
C01(10)+C02(10)+ C03(10)+C04(10)
+ C11(5)+C12(5)+C13(5)+C14(5) );
//
func zmin=0.;
func zmax=1.;
int MaxLayer=10;
func XX = x*cos(y);
func YY = x*sin(y);
func ZZ = z;
int[int] r1=[0,41], r2=[98,98, 99,99, 1,56];
int[int] r3=[4,12];
//
The triangles of uppper surface mesh
//
generated by the triangle in the 2D region of mesh Th of label 4 as
label 12.
int[int] r4=[4,45];
//
The triangles of lower surface mesh
147
5.11
Meshing examples
//
file tetgenholeregion.edp
//
pi
//
] pi
2 , 2 []0, 2[
a parametrization of a sphere
load "msh3
load "tetgen"
mesh Th=square(10,20,[x*pi-pi/2,2*y*pi]);
func f1 =cos(x)*cos(y);
func f2 =cos(x)*sin(y);
func f3 = sin(x);
//
func f1x=sin(x)*cos(y);
148
func
func
func
func
func
M = DF t DF
func m11=f1x2+f2x2+f3x2;
func m21=f1x*f1y+f2x*f2y+f3x*f3y;
func m22=f1y2+f2y2+f3y2;
func perio=[[4,y],[2,y],[1,x],[3,x]];
real hh=0.1;
real vv= 1/square(hh);
verbosity=2;
Th=adaptmesh(Th,m11*vv,m21*vv,m22*vv,IsMetric=1,periodic=perio);
Th=adaptmesh(Th,m11*vv,m21*vv,m22*vv,IsMetric=1,periodic=perio);
plot(Th,wait=1);
verbosity=2;
//
real
func
func
func
Rmin
f1min
f2min
f3min
=
=
=
=
1.;
Rmin*f1;
Rmin*f2;
Rmin*f3;
Rmax
f1max
f2max
f3max
=
=
=
=
2.;
Rmax*f1;
Rmax*f2;
Rmax*f3;
149
5.11.1
First the MeshSurface.idp file to build boundary mesh of a Hexaedra and of a Sphere.
func mesh3 SurfaceHex(int[int] & N,real[int,int] &B ,int[int,int] & L,int orientation)
{
real x0=B(0,0),x1=B(0,1);
real y0=B(1,0),y1=B(1,1);
real z0=B(2,0),z1=B(2,1);
int nx=N[0],ny=N[1],nz=N[2];
mesh Thx = square(ny,nz,[y0+(y1-y0)*x,z0+(z1-z0)*y]);
mesh Thy = square(nx,nz,[x0+(x1-x0)*x,z0+(z1-z0)*y]);
mesh Thz = square(nx,ny,[x0+(x1-x0)*x,y0+(y1-y0)*y]);
int[int] refx=[0,L(0,0)],refX=[0,L(0,1)];
renumbering
int[int] refy=[0,L(1,0)],refY=[0,L(1,1)];
renumbering
int[int] refz=[0,L(2,0)],refZ=[0,L(2,1)];
renumbering
//
//
//
150
func m21=f1x*f1y+f2x*f2y+f3x*f3y;
func m22=f1y2+f2y2+f3y2;
func perio=[[4,y],[2,y],[1,x],[3,x]];
//
real hh=h/R;
//
hh mesh size on unite sphere
real vv= 1/square(hh);
Th=adaptmesh(Th,m11*vv,m21*vv,m22*vv,IsMetric=1,periodic=perio);
Th=adaptmesh(Th,m11*vv,m21*vv,m22*vv,IsMetric=1,periodic=perio);
Th=adaptmesh(Th,m11*vv,m21*vv,m22*vv,IsMetric=1,periodic=perio);
Th=adaptmesh(Th,m11*vv,m21*vv,m22*vv,IsMetric=1,periodic=perio);
int[int] ref=[0,L];
The test of the two functions and the call to tetgen mesh generator
load "tetgen"
include "MeshSurface.idp"
real hs = 0.1;
int[int] N=[20,20,20];
real [int,int] B=[[-1,1],[-1,1],[-1,1]];
int [int,int] L=[[1,2],[3,4],[5,6]];
mesh3 ThH = SurfaceHex(N,B,L,1);
mesh3 ThS =Sphere(0.5,hs,7,1);
//
boundary meshes
//
mesh3 ThHS=ThH+ThS;
savemesh(ThHS,"Hex-Sphere.mesh");
exec("ffmedit Hex-Sphere.mesh;rm Hex-Sphere.mesh");
//
see 5.38
real voltet=(hs3)/6.;
cout << " voltet = " << voltet << endl;
real[int] domaine = [0,0,0,1,voltet,0,0,0.7,2,voltet];
mesh3 Th = tetg(ThHS,switch="pqaAAYYQ",nbofregions=2,regionlist=domaine);
medit("Cube-With-Ball",Th);
//
see 5.39
5.12
With the keyword savesol, we can store a scalar functions, a scalar FE functions, a vector fields, a
vector FE fields, a symmetric tensor and a symmetric FE tensor.. Such format is used in medit.
extension file .sol The first two lines of the file are
MeshVersionFormatted 0
Dimension (I) dim
151
Figure 5.38: The surface mesh of the Hex Figure 5.39: The tet mesh of the cube with
with internal Sphere
internal ball
The following fields begin with one of the following keyword: SolAtVertices, SolAtEdges, SolAtTriangles, SolAtQuadrilaterals, SolAtTetrahedra, SolAtPentahedra, SolAtHexahedra.
In each field, we give then in the next line the number of elements in the solutions (SolAtVertices:
number of vertices, SolAtTriangles: number of triangles, ...). In other lines, we give the number of
solutions , the type of solution (1: scalar, 2: vector, 3: symmetric tensor). And finally, we give the
values of the solutions on the elements.
The file must be ended with the keyword End.
The real element of symmetric tensor
3d ST 3d ST 3d
STxx
xy
xz
2d ST 2d
STxx
2d
xy
3d ST 3d ST 3d
ST
=
ST 3d = STyx
(5.5)
yy
yz
2d ST 2d
STyx
yy
3d
3d
3d
STzx STzy STzz
3d , ST 3d , ST 3d , ST 3d , ST 3d , ST 3d and ST 2d , ST 2d , ST 2d
stored in the extension .sol are respectively STxx
yx
yy
zx
zy
zz
xx
yx
yy
An example of field with the keyword SolAtTetrahedra:
SolAtTetrahedra
(I) NbOfTetrahedrons
nbsol typesol1 ... typesoln
Ukij , i {1, ..., nbrealsolk } ,
k {1, ...nbsol}
where
nbsol is an integer equal to the number of solutions
typesolk , type of the solution number k, is
typesolk = 1 the solution k is scalar.
152
This field is written with the notation of Section 12.1. The format .solb is the same as format .sol
but in binary (read/write is faster, storage is less).
A real scalar functions f 1, a vector fields = [1, 2, 3] and a symmetric tensor ST 3d (5.5) at
the vertices of the three dimensional mesh Th3 is stored in the file f1PhiTh3.sol using
savesol("f1PhiST3dTh3.sol",Th3, f 1, [1, 2, 3], VV3, order=1);
3d , ST 3d , ST 3d , ST 3d , ST 3d , ST 3d ]. For a two dimensional mesh Th, A real scalar
where V V 3 = [STxx
yx
yy
zx
zy
zz
functions f 2, a vector fields = [1, 2] and a symmetric tensor ST 2d (5.5) at triangles is stored
in the file f2PsiST2dTh3.solb using
order = 0 is the solution is given at the center of gravity of elements. 1 is the solution is given
at the vertices of elements.
In the file, solutions are stored in this order : scalar solutions, vector solutions and finally symmetric
tensor solutions.
5.13
medit
The keyword medit allows to dipslay a mesh alone or a mesh and one or several functions defined
on the mesh using the Pascal Freys freeware medit. Medit opens its own window and uses OpenGL
extensively. Naturally to use this command medit must be installed.
A vizualisation with medit of scalar solutions f 1 and f 2 continuous, piecewise linear and known at
the vertices of the mesh Th is obtained using
medit("sol1 sol2",Th, f 1, f 2, order=1);
The first plot named sol1 display f1. The second plot names sol2 display f2.
The arguments of function medit are the name of the differents scenes (separated by a space) of
medit, a mesh and solutions. Each solution is associated with one scene. The scalar, vector and
symmetric tensor solutions are specified in the format described in the section dealing with the
keyword savesol.
The parameters of this command line are
5.13. MEDIT
153
order = 0 is the solution is given at the center of gravity of elements. 1 is the solution is given
at the vertices of elements.
meditff = set the name of execute command of medit. By default, this string is medit.
save = set the name of a file .sol or .solb to save solutions.
This command line allows also to represent two differents meshes and solutions on them in the same
windows. The nature of solutions must be the same. Hence, we can vizualize in the same window
the different domains in a domain decomposition method for instance. A vizualisation with medit
of scalar solutions h1 and h2 at vertices of the mesh Th1 and Th2 respectively are obtained using
medit("sol2domain",Th1, h1, Th2, h2, order=1);
//
meditddm.edp
load "medit"
//
//
//
//
//
Initial Problem:
//
Resolution of the following EDP:
us = f on = {(x, y)|1 sqrt(x2 + y 2 ) 2}
u1 = f 1 on 1 = {(x, y)|0.5 sqrt(x2 + y 2 ) 1.}
u = 1 on + Null Neumman condition on 1 and on 2
We find the solution u in solving two EDP defined on domain and 1
//
This solution is visualize with medit
verbosity=3;
border Gamma(t=0,2*pi){x=cos(t); y=sin(t); label=1;};
border Gamma1(t=0,2*pi){x=2*cos(t); y=2*sin(t); label=2;};
border Gamma2(t=0,2*pi){x=0.5*cos(t); y=0.5*sin(t); label=3;};
//
mesh Th=buildmesh(Gamma1(40)+Gamma(-40));
fespace Vh(Th,P2);
func f=sqrt(x*x+y*y);
Vh us,v;
macro Grad2(us) [dx(us),dy(us)]
//
EOM
//
EOM
154
//
assert(version>1.18);
real s0=clock();
mesh Th=square(10,10);
fespace Xh(Th,P2),Mh(Th,P1);
Xh u1,u2,v1,v2;
Mh p,q,ppp;
LinearCG(divup,p[],q[],eps=1.e-6,nbiter=50);
divup(p[]);
plot([u1,u2],p,wait=1,value=true,coef=0.1);
medit("velocity pressure",Th,[u1,u2],p,order=1);
5.14. MSHMET
5.14
155
Mshmet
Mshmet is a software developped by P. Frey that allows to compute an anisotropic metric based on
solutions (i.e. Hessian-based). This sofware can return also an isotropic metric. Moreover, mshmet
can construct also a metric suitable for level sets interface capturing. The solution can be defined
on 2D or 3D structured/unstructured meshes. For example, the solution can be an error estimate
of a FE solutions.
Solutions for mshmet are given as an argument. The solution can be a func, a vector func, a
symmetric tensor, a FE func, a FE vector func and a FE symmetric tensor. The symmetric tensor
argument is defined as this type of data for datasol argument. This software accepts more than
one solution.
For example, the metric M computed with mshmet for the solution u defined on the mesh T h is
obtained by writing.
fespace Vh(Th,P1);
Vh u;
real[int] M = mshmet(Th,u);
//
a scalar FE func
156
The result of the keyword mshmet is a real[int] which contains the metric computed by mshmet
at the different vertices Vi of the mesh.
With nv is the number of vertices, the structure of this vector is
Miso = (m(V0 ), m(V1 ), . . . , m(Vnv ))t
5.15. FREEYAMS
157
real[int] bb=mshmet(Th3,u3);
cout << bb << endl;
for(int ii=0; ii<Th3.nv; ii++)
usol[][ii]=bb[ii];
savesol("metrictest.bis.sol",Th3,usol);
5.15
FreeYams
FreeYams is a surface mesh adaptation software which is developed by P. Frey. This software
is a new version of yams. The adapted surface mesh is constructed with a geometric metric
tensor field. This field is based on the intrinsic properties of the discrete surface. Also this
software allows to construct a simplification of a mesh. This decimation is based on the Hausdorff distance between the initial and the current triangulation. Compared to the software yams,
FreeYams can be used also to produce anisotropic triangulations adapted to level set simulations. A technical report on FreeYams is not available yet but a documentation on yams exists at
http://www.ann.jussieu.fr/frey/software.html [45].
To call FreeYams in FreeFem++ , we used the keyword freeyams. The arguments of this function
are the initial mesh and/or metric. The metric with freeyams are a function, a FE function, a
symmetric tensor function, a symmetric tensor FE function or a vector of double. If the metric is
vector of double, this data must be given in metric parameter. Otherwise, the metric is given in
the argument.
For example, the adapted mesh of T hinit defined by the metric u defined as FE function is obtained
in writing.
fespace Vh(Thinit,P1);
Vh u;
mesh3 Th=freeyams(Thinit,u);
The symmetric tensor argument for freeyams keyword is defined as this type of data for datasol
argument.
aniso = <b> aniso or iso metric (default 0, iso)
mem = <l> memory of for freeyams in Mb (delaulf -1, freeyams choose)
hmin = <d>
hmax = <d>
gradation = <d>
option = <l>
0 : mesh optimization (smoothing+swapping)
1 : decimation+enrichment adaptated to a metric map. (default)
-1 : decimation adaptated to a metric map.
2 : decimation+enrichment with a Hausdorff-like method
-2 : decimation with a Hausdorff-like method
4 : split triangles recursively.
9 : No-Shrinkage Vertex Smoothing
ridgeangle = <d>
158
0:
1:
-1:
2:
-2:
5.16. MMG3D
159
4 : split triangles recursively.
9 : No-Shrinkage Vertex Smoothing
doptions= a vector of double of size 11. This vectors contains the real options of freeyams.
doptions(0): Set the geometric approximation (Tangent plane deviation) (default 0.01).
doptions(1): Set the lamda parameter (default -1. ).
doptions(2): Set the mu parmeter (default -1. ).
doptions(3): Set the gradation value (Mesh density control) (default 1.3).
doptions(4): Set the minimal size(hmin) (default -2.0: the size is automatically computed).
doptions(5): Set the maximal size(hmax) (default -2.0: the size is automatically computed).
doptions(6): Set the tolerance of the control of Chordal deviation (default -2.0).
doptions(7): Set the quality of degradation (default 0.599).
doptions(8): Set the declic parameter (default 2.0).
doptions(9): Set the angular walton limitation parameter (default 45 degree).
doptions(10): Set the angular ridge detection (default 45 degree).
Example 5.28 (freeyams.edp)
load "msh3"
load "medit"
load "freeyams"
int nn=20;
mesh Th2=square(nn,nn);
fespace Vh2(Th2,P2);
Vh2 ux,uz,p2;
int[int] rup=[0,2], rdown=[0,1], rmid=[1,1,2,1,3,1,4,1];
real zmin=0,zmax=1;
mesh3 Th=buildlayers(Th2,nn, zbound=[zmin,zmax],
reffacemid=rmid, reffaceup = rup, reffacelow = rdown);
mesh3 Th3 = freeyams(Th);
medit("maillagesurfacique",Th3,wait=1);
5.16
mmg3d
160
Remark 5 :
(a) If no metric is given, an isotropic metric is computed by analyzing the size of the edges in the
initial mesh.
(b) if a displacement is given, the vertices of the surface triangles are moved without verifying the
geometrical structure of the new surface mesh.
The parameters of mmg3d are :
options= vector expression. This vector contains the option parameters of mmg3d. It is a
vector of 6 values, with the following meaning:
(0) optimization parameters : (default 1)
0 : mesh optimization.
1 : adaptation with metric (deletion and insertion vertices) and optimization.
-1: adaptation with metric (deletion and insertion vertices) without optimization.
4 : split tetrahedra (be careful modify the surface).
9 : moving mesh with optimization.
-9: moving mesh without optimization.
(1) debug mode : (default 0)
1 : turn on debug mode.
0 : otherwise.
(2) Specify the size of bucket per dimension ( default 64)
(3) swapping mode : (default 0)
1 : no edge or face flipping.
0 : otherwise.
(4) insert points mode : (default 0)
1 : no edge splitting or collapsing and no insert points.
0 : otherwise.
(5) verbosity level (default 3)
memory= integer expression. Set the maximum memory size of new mesh in Mbytes. By
default the number of maximum vertices, tetrahedra and triangles are respectively 500 000,
3000 000, 100000 which represent approximately a memory of 100 Mo.
metric= vector expression. This vector contains the metric given at mmg3d. It is a vector
of size nv or 6 nv respectively for an istropic and anisotropic metric where nv is the number
of vertices in the initial mesh. The structure of metric vector is described in the mshmets
section(section 5.14).
displacement= [1, 2, 3] set the displacement vector of the initial mesh
(x, y) = [1(x, y), 2(x, y), 3(x, y)].
displVect= sets the vector displacement in a vector expression. This vector contains the
displacement at each point of the initial mesh. It is a vector of size 3 nv.
An example using this function is given in mmg3d.edp:
Example 5.29 (mmg3d.edp)
//
load "msh3"
load "medit"
test mmg3d
5.16. MMG3D
161
load "mmg3d"
include "../examples++-3d/cube.idp"
int n=6;
int[int] Nxyz=[12,12,12];
real [int,int] Bxyz=[[0.,1.],[0.,1.],[0.,1.]];
int [int,int] Lxyz=[[1,1],[2,2],[2,2]];
mesh3 Th=Cube(Nxyz,Bxyz,Lxyz);
real[int] isometric(Th.nv);{
for( int ii=0; ii<Th.nv; ii++)
isometric[ii]=0.17;
}
mesh3 Th3=mmg3d(
Th,
memory=100, metric=isometric);
medit("init",Th);
medit("isometric",Th3);
load "tetgen"
load "medit"
load "mmg3d"
include "MeshSurface.idp"
//
real hs = 0.8;
int[int] N=[4/hs,8/hs,11.5/hs];
real [int,int] B=[[-2,2],[-2,6],[-10,1.5]];
int [int,int] L=[[311,311],[311,311],[311,311]];
mesh3 ThH = SurfaceHex(N,B,L,1);
mesh3 ThSg =Sphere(1,hs,300,-1);
mesh3 ThSd =Sphere(1,hs,310,-1);
ThSd=movemesh3(ThSd,transfo=[x,4+y,z]);
mesh3 ThHS=ThH+ThSg+ThSd;
//
gluing surface meshes
medit("ThHS", ThHS);
//
see surface mesh
real voltet=(hs3)/6.;
real[int] domaine = [0,0,-4,1,voltet];
real [int] holes=[0,0,0,0,4,0];
mesh3 Th = tetg(ThHS,switch="pqaAAYYQ",nbofregions=1,regionlist=domaine, nbofholes=2,hol
medit("Box-With-two-Ball",Th);
//
End build mesh
int[int] opt=[9,0,64,0,0,3];
real[int] vit=[0,0,-0.3];
func zero = 0.;
func dep = vit[2];
fespace Vh(Th,P1);
macro Grad(u) [dx(u),dy(u),dz(u)]
//
//
Vh uh,vh;
//
to compute the displacemnt field
problem Lap(uh,vh,solver=CG) = int3d(Th)(Grad(uh)*Grad(vh))
//
) for
emacs
+ on(310,300,uh=dep) +on(311,uh=0.);
162
5.17
int nn = 6;
int[int] l1111=[1,1,1,1],l01=[0,1],l11=[1,1]; //
label numbering to have all
label to 1
mesh3 Th3=buildlayers(square(nn,nn,region=0,label=l1111),
nn, zbound=[0,1], labelmid=l11,
labelup = l01, labeldown = l01);
Th3 = trunc(Th3,(x<0.5) | (y < 0.5) | (z < 0.5) ,label=1);
//
remove the
]0.5, 1[3 cube
//
end of build initial mesh
fespace Vh(Th3,P1);
Vh u,v,usol,h;
macro Grad(u) [dx(u),dy(u),dz(u)]
//
EOM
5.18
163
The idea is get the discretization of a isoline to fluid meshes, this tool can be useful to construct
meshes
from image. First, we give an example of the isovalue meshes 0.2 of analytical function
p
(x 1/2)2 + (y 1/2)2 , on unit square.
Example 5.32 (isoline.edp)
load "isoline"
//
real[int,int] xy(3,1);
//
to store the isoline points
int[int] be(1);
//
to store the begin , end couple of lines
{
//
a block for memory management
mesh Th=square(10,10);
//
,[x*.5,y*0.5]);
fespace Vh(Th,P1);
Vh u= sqrt(square(x-0.5)+square(y-0.5));
real iso= 0.2 ;
real[int] viso=[iso];
plot(u,viso=viso,Th);
//
to see the iso line
int nbc= isoline(Th,u,xy,close=1,iso=iso,beginend=be,smoothing=0.1);
The isoline parameters are Th the mesh, the expression u , the bidimentionnal array xy to store
the list coordinate of the points. The list of named parameter are:
iso= value of the isoline to compute (0 is the default value)
close= close the iso line with the border (def. true), we add the part of the mesh border such the
value is less than the iso value
smoothing= nb of smoothing process is the lr s where l is the length of the current line component,
r the ratio, s is smoothing value. The smoothing default value is 0.
ratio= the ratio ( 1 by default).
eps= relative (see code ??) (def 1e-10 )
beginend= array to get begin, end couple of each of sub line (resize automatically)
file= to save the data curve in data file for gnu plot
In the array xy you get the list of vertices of the isoline, each connex line go from i = ic0 , . . . , ic1 1
with ic0 = be(2 c) ic1 = be(2 c + 1), and where xi = xy(0, i), yi = yx(1, i), li = xy(2, i). Here li is
the length of the line (the origin of the line is point ic0 ).
The sense of the isoline is such that the upper part is at the left size of the isoline. So here : the
minimum is a point 0.5, 05 so the curve 1 turn in the clockwise sense, the order of each component
are sort such the the number of point by component is decreasing .
cout << " nb of the line component
cout << " n = " << xy.m << endl;
cout << "be = " << be << endl;
164
}
}
//
last
We also have a new function to parametrize easly a discret Curve defined by couple be, xy.
border Curve0(t=0,1)
{ int c =0;
int i0 = be[2*c], i1 = be[2*c+1]-1;
P=Curve(xy,i0,i1,t);
label=1;
}
//
border Curve1(t=0,1)
{ int c =1;
int i0 = be[2*c], i1 = be[2*c+1]-1;
P=Curve(xy,i0,i1,t);
label=1;
}
//
component 1
//
Curve 1
//
show curve.
because
//
//
plot(Curve1(100));
mesh Th= buildmesh(Curve1(-100));
plot(Th,wait=1);
Curve 0
Secondly, we use this idea to build meshes from image, we use the plugins ppm2rnm to read pgm
gray scale image, and we extract the gray contour at level 0.25.
Example 5.33 (Leman-mesh.edp)
//
//
//
nb of curve
//
read image (figure 5.40)
//
and set to an rect. array
int nx = ff1.n, ny=ff1.m;
//
grey value between 0 to 1 (dark)
//
build a Cartesian mesh such that the origin is qt the right place.
mesh Th=square(nx-1,ny-1,[(nx-1)*(x),(ny-1)*(1-y)]);
//
warning the numbering is of the vertices (x,y) is
//
given by i = x/nx + nx y/ny
fespace Vh(Th,P1);
Vh f1; f1[]=ff1;
//
transforme array in finite element function.
nc=isoline(Th,f1,iso=0.25,close=1,Curves,beginend=be,smoothing=.1,ratio=0.5);
165
}
//
the longest isoline : the lac ..
int ic0=be(0), ic1=be(1)-1;
plot([Curves(0,ic0:ic1),Curves(1,ic0:ic1)], wait=1);
int NC= Curves(2,ic1)/hsize;
border G(t=0,1) { P=Curve(Curves,ic0,ic1,t); label= 1 + (x>xl)*2 + (y<yl);}
plot(G(-NC),wait=1);
mesh Th=buildmesh(G(-NC));
plot(Th,wait=1);
real scale = sqrt(AreaLac/Th.area);
Th=movemesh(Th,[x*scale,y*scale]);
//
resize the mesh
cout << " Th.area = " << Th.area << " Km2 " << " == " << AreaLac << "
Km2 "
<< endl ;
plot(Th,wait=1,ps="leman.eps");
//
see figure 5.41
Figure 5.40:
meshes
166
Chapter 6
Finite Elements
As stated in Section 2. FEM approximates all functions w as
w(x, y) ' w0 0 (x, y) + w1 1 (x, y) + + wM 1 M 1 (x, y)
with finite element basis functions k (x, y) and numbers wk (k = 0, , M 1). The functions
k (x, y) are constructed from the triangle Tik , and called shape functions. In FreeFem++ the
finite element space
Vh = {w | w0 0 + w1 1 + + wM 1 M 1 , wi R }
is easily created by
fespace IDspace(IDmesh,<IDFE>) ;
and in 3d
fespace IDspace(IDmesh,<IDFE>,
periodic=[[la 1,sa 1,ta 1],[lb 1,sb 1,tb 1],
...
[la k,sa k,ta k],[lb k,sb `,tb `]]);
where
IDspace is the name of the space (e.g. Vh),
IDmesh is the name of the associated mesh and <IDFE> is a identifier of finite element type.
In 2D we have a pair of periodic boundary condition, if [la i,sa i],[lb i,sb i] is a pair of
int, and the 2 labels la i and lb i refer to 2 pieces of boundary to be in equivalence.
If [la i,sa i],[lb i,sb i] is a pair of real, then sa i and sb i give two common abscissa
on the two boundary curve, and two points are identified as one if the two abscissa are equal.
In 2D, we have a pair of periodic boundary condition,if [la i,sa i,ta i],[lb i,sb i,tb i] is
a pair of int, the 2 labels la i and lb i define the 2 piece of boundary to be in equivalence.
167
168
If [la i,sa i,ta i],[lb i,sb i,tb i] is a pair of real, then sa i,ta i and sb i,tb i give
two common parameters on the two boundary surface, and two points are identified as one if the
two parameters are equal.
Remark 6 The 2D mesh of the two identified borders must be the same, so to be sure, use the parameter fixedborder=true in buildmesh command (see 5.1.2) like in example periodic2bis.edp
(see 9.7).
As of today, the known types of finite element are:
P0,P03d piecewise constant discontinuous finite element (2d, 3d), the degrees of freedom are the
barycenter element value.
P 0h = v L2 () for all K Th there is K R : v|K = K
(6.1)
P1,P13d piecewise linear continuous finite element (2d, 3d), the degrees of freedom are the vertices
values.
P 1h = v H 1 () K Th v|K P1
(6.2)
P1dc piecewise linear discontinuous finite element
P 1dch = v L2 () K Th
v|K P1
(6.3)
Warning, due to interpolation problem, the degree of freedom is not the vertices but three
vectices move inside with T (X) = G+.99(X G) where G is the barycenter, (version 2.24-4).
P1b,P1b3d piecewise linear continuous finite element plus bubble (2d, 3d)
The 2d case:
P 1bh = v H 1 () K Th
K K
v|K P1 Span{K
0 1 2 }
(6.4)
The 3d case:
P 1bh = v H 1 () K Th
K K K
v|K P1 Span{K
0 1 2 3 }
(6.5)
where K
i , i = 0, .., d are the d + 1 barycentric coordinate functions of the element K (triangle
or tetrahedron).
P1bl,P1bl3d piecewise linear continuous finite element plus linear bubble (2d, 3d) the bulle is
build by spliting the K a barycenter in d + 1 sub element. (need load "Element_P1bl")
P2,P23d piecewise P2 continuous finite element (2d, 3d),
P 2h = v H 1 () K Th
v|K P2
(6.6)
(6.7)
169
P2dc piecewise P2 discontinuous finite element,
P 2dch = v L2 () K Th
v|K P2
(6.8)
Warning, due to interpolation problem, the degree of freedom is not the six P2 nodes but six
nodes move inside with T (X) = G + .99(X G) where G is the barycenter, (version 2.24-4).
P3 piecewise P3 continuous finite element (2d) (need load "Element_P3")
P 2h = v H 1 () K Th v|K P3
(6.9)
(6.10)
(6.11)
(6.12)
(6.13)
170
load "Morley"
fespace Vh(Th,P2Morley);
//
the Morley finite element space
macro bilaplacien(u,v) ( dxx(u)*dxx(v)+dyy(u)*dyy(v)+2.*dxy(u)*dxy(v))
//
fin macro
real f=1;
Vh [u,ux,uy],[v,vx,vy];
solve bilap([u,ux,uy],[v,vx,vy]) =
int2d(Th)( bilaplacien(u,v) )
- int2d(Th)(f*v)
+ on(1,2,3,4,u=0,ux=0,uy=0)
HCT P3 C 1 conform finite element (2d) (need load "Element_HCT") one 3 sub triangles (version 3.40).
Let call Th4 the sub mesh of Th where all triangle are split in 3 at the barycenter.
n
o
P HCTh = v C 1 () K Th4 v|K P3
(6.14)
where P3 is the set of polynomials of R2 of degrees 3. The degree of freedom are the value
and derivative at vertices and normal derivative a middle edge point of initial meshes,and
thank to [15].
Warning to build the interplant of a function u (scalar) for this finite element, we need the
function and 2 partial derivatives (u, ux , uy ), so this vectorial finite element with 3 components
(u, ux , uy ) like in previous Finite Element.
P2BR (need load "BernadiRaugel") the Bernadi Raugel Finite Element is a Vectorial element
(2d) with 2 components, See Bernardi, C., Raugel, G.: Analysis of some finite elements for
the Stokes problem. Math. Comp. 44, 71-79 (1985). It is a 2d coupled FE, with the
Polynomial space is P12 + 3 normals bubbles edges function (P2 ) and the degre of freedom is
6 values at of the 2 components at the 3 vertices and the 3 flux on the 3 edges So the number
degrees of freedom is 9.
RT0,RT03d Raviart-Thomas finite element of degree 0.
The 2d case:
n
RT 0h = v H(div) K Th
1
o
x
v|K (x, y) = K
2 + K | y
(6.15)
1
)
K
x
2
y
v|K (x, y, z) = K + K z
3
(6.16)
The 3d case:
v H(div) K Th
(
RT 0h =
Pd
i=1 wi /xi
n
o
H(div) = w L2 ()d div w L2 ()
1 , 2 , 3 , are real numbers.
and where K
K
K
K
171
RT0Ortho Raviart-Thomas Orthogonal, or Nedelec finite element type I of degree 0 in dimension
2
1
n
o
y
RT 0Orthoh = v H(curl) K Th v|K (x, y) = K
+
|
(6.17)
K x
2
K
(
Edge0h =
1
1
)
K
K
x
2
v|K (x, y, z) = 2K + K
yz
3
3
K
(6.18)
w2 /x3 w3 /x2
where by writing curlw = w3 /x1 w1 /x3 with w = (wi )di=1 ,
w /x w /x
1
n
o
H(curl) = w L2 ()d curl w L2 ()d
1 , 2 , 3 , 1 , 2 , 3 are real numbers.
and K
K
K
K
K
K
Edge13d (need load "Element_Mixte3d", version 3.34) 3d Nedelec finite element or Edge
Element of degree 1.
Edge23d (need load "Element_Mixte3d", version 3.34) 3d Nedelec finite element or Edge
Element of degree 2.
P1nc piecewise linear element continuous at the middle of edge only in 2D ?????.
RT1 (need load "Element_Mixte", version 3.13)
1
n
o
x
2
2
3
RT 1h = v H(div) K Th (K , K , K ) P1 , v|K (x, y) = K
2 + K | y
(6.19)
|
(6.20)
K x
2
K
172
6.1
Use of fespace in 2d
v|K P1 }
.
v|K P2 }
K
v|K (x, y) = K
+ K | xy }
when Th is a mesh 10 10 of the unit square ]0, 1[2 , we only write in FreeFem++ :
mesh Th=square(10,10);
fespace Xh(Th,P1);
fespace Xph(Th,P1,
periodic=[[2,y],[4,y],[1,x],[3,x]]);
fespace Mh(Th,P2);
fespace Rh(Th,RT0);
//
scalar FE
//
bi-periodic FE
//
scalar FE
//
vectorial FE
and Vh = VV xh
yh
6.2
173
Use of fespace in 3d
v|K P1 }
.
v|K P2 }
K
v|K (x, y) = K
+ K | xy }
//
array of 10 function in Xh
//
array of 10 functions in Rh.
the 6th function at point (0.5, 0.5, 0.5)
//
the array of the degre of
V zh
Note 6.1 One hard problem of the periodic boundary condition is the mesh must be the same au
equivalence face, the BuildLayer mesh generator split each quadrilateral faces with the diagonal passing through vertex with maximal number, so to be sure to have the same mesh one both face periodic
the 2d numbering in corresponding edges must be compatible (for example the same variation). By
Default, the numbering of square vertex are correct.
To change the mesh numbering you can used the change function like:
{
//
for cleanning memory..
int[int] old2new(0:Th.nv-1);
//
array set on 0, 1, .., nv-1
fespace Vh2(Th,P1);
Vh2 sorder=x+y; //
choose an ordering increasing on 4 square borders with x
or y
sort(sorder[],old2new);
//
build the inverse permutation
int[int] new2old=old2new-1;
//
inverse the permutation
174
Th= change(Th,renumv=new2old);change}
}
6.3
6.3.1
For each triangle (d=2) or tetrahedron (d=3) Tk , the basis function k in Vh(Th,P0) is given by
k (x) = 1 if (x) Tk ,
k (x) = 0 if (x) 6 Tk
If we write
Vh(Th,P0);
See Fig. 6.3 for the projection of f (x, y) = sin(x) cos(y) on Vh(Th,P0) when the mesh Th is
a 4 4-grid of [1, 1]2 as in Fig. 6.2.
6.3.2
P1-element
k
(b )
(a )
T
p
q
k
k
6
i (q j ) = 0 if i 6= j
The basis function k1 (x, y) with the vertex q k1 in Fig. 6.1(a) at point p = (x, y) in triangle Tk
simply coincide with the barycentric coordinates k1 (area coordinates) :
k1 (x, y) = k1 (x, y) =
If we write
area of triangle(p, q k2 , q k3 )
area of triangle(q k1 , q k2 , q k3 )
175
Vh(Th,P1); Vh fh=g(x.y);
then
fh = fh (x, y) =
nv
X
f (q i )i (x, y)
i=1
See Fig. 6.4 for the projection of f (x, y) = sin(x) cos(y) into Vh(Th,P1).
6.3.3
P2-element
i (q j ) = 0 if i 6= j
The basis function k1 (x, y) with the vertex q k1 in Fig. 6.1(b) is defined by the barycentric coordinates:
k1 (x, y) = k1 (x, y)(2k1 (x, y) 1)
and for the midpoint q k2
k2 (x, y) = 4k1 (x, y)k4 (x, y)
If we write
Vh(Th,P2); Vh fh=f (x.y);
then
fh = fh (x, y) =
M
X
f (q i )i (x, y)
i=1
See Fig. 6.5 for the projection of f (x, y) = sin(x) cos(y) into Vh(Th,P2).
176
6.4
P1 Nonconforming Element
Refer to [28] for details; briefly, we now consider non-continuous approximations so we shall lose
the property
wh Vh H 1 ()
If we write
Vh(Th,P1nc); Vh fh=f (x.y);
then
fh = fh (x, y) =
nv
X
f (mi )i (x, y)
i=1
Here the basis function i associated with the midpoint mi = (q ki + q ki+1 )/2 where q ki is the i-th
point in Tk , and we assume that j + 1 = 0 if j = 3:
i (x, y) = aki + bki x + cki y for (x, y) Tk ,
i (mi ) = 1,
i (mj ) = 0 if i 6= j
Strictly speaking i /x, i /y contain Dirac distribution Tk . The numerical calculations will
automatically ignore them. In [28], there is a proof of the estimation
nv Z
X
k=1
!1/2
|w wh |2 dxdy
= O(h)
Tk
2. f 0 uh 0
if i 6= j
177
if i 6= j
6.5
Other FE-space
For each triangle Tk Th , let k1 (x, y), k2 (x, y), k3 (x, y) be the area cordinate of the triangle
(see Fig. 6.1), and put
k (x, y) = 27k1 (x, y)k2 (x, y)k3 (x, y)
called bubble function on Tk . The bubble function has the feature:
1. k (x, y) = 0
if (x, y) Tk .
q k1 +q k2 +q k3
.
3
If we write
Vh(Th,P1b); Vh fh=f (x.y);
then
fh = fh (x, y) =
nv
X
i=1
f (q i )i (x, y) +
nt
X
f (q kb )k (x, y)
k=1
See Fig. 6.7 for the projection of f (x, y) = sin(x) cos(y) into Vh(Th,P1b).
(6.23)
178
6.6
Functions from R2 to RN with N = 1 is called scalar function and called vector valued when N > 1.
When N = 2
fespace Vh(Th,[P0,P1]) ;
6.6.1
Raviart-Thomas element
In the Raviart-Thomas finite element RT 0h , the degree ofR freedom are the fluxes across edges e of
the mesh, where the flux of the function f : R2 R2 is e f .ne , ne is the unit normal of edge e.
This implies a orientation of all the edges of the mesh, for example we can use the global numbering
of the edge vertices and we just go from small to large numbers.
To compute the flux, we use a quadrature with one Gauss point, the middle point of the edge.
Consider a triangle Tk with three vertices (a, b, c). Let denote the vertices numbers by ia , ib , ic ,
and define the three edge vectors e1 , e2 , e3 by sgn(ib ic )(bc), sgn(ic ia )(ca), sgn(ia ib )(ab),
We get three basis functions,
k1 =
sgn(ib ic )
(x a),
2|Tk |
sgn(ic ia )
(x b),
2|Tk |
k2 =
k3 =
sgn(ia ib )
(x c),
2|Tk |
then
fh = f h (x, y) =
nt X
6
X
k=1 l=1
T
n
2
a
n
b
1
(6.24)
179
//
//
vh = Uxh;
//
plot(uh,ps="onoldmesh.eps");
uh = uh;
//
plot(uh,ps="onnewmesh.eps");
vh([x-1/2,y])= x2 + y2;
Figure 6.9:
vh Iso on mesh 2 2
ok vectorial FE function
//
vh
//
change the mesh
//
Xh is unchange
//
compute on the new Xh
error: impossible to set only 1 component
//
of a vector FE function.
//
ok
//
and now uh use the 5x5 mesh
but the fespace of vh is alway the 2x2 mesh
//
figure 6.9
do a interpolation of vh (old) of 5x5 mesh
//
to get the new vh on 10x10 mesh.
//
figure 6.10
//
interpolate vh = ((x 1/2)2 + y 2 )
vh Iso on mesh 5 5
Figure 6.10:
//
//
//
//
//
To get the value of the array associated to the FE function uh, one writes
180
//
//
Note 6.2 For a none scalar finite element function [Uxh,Uyh] the two array Uxh[] and Uyh[]
are the same array, because the degree of freedom can touch more than one component.
6.7
In practice one may discretize the variational equations by the Finite Element method. Then there
will be one mesh for 1 and another one for 2 . The computation of integrals of products of
functions defined on different meshes is difficult. Quadrature formulae and interpolations from one
mesh to another at quadrature points are needed. We present below the interpolation operator
which we have used and which is new, to the best of our knowledge. Let Th0 = k Tk0 , Th1 = k Tk1
be two triangulations of a domain . Let
V (T ih ) = {C 0 (ih ) : f |T i P0 },
k
i = 0, 1
181
j f (qj0 )
j=1,2,3
End
Figure 6.11: To interpolate a function at q 0 the knowledge of the triangle which contains
q 0 is needed. The algorithm may start at q 1 Tk0 and stall on the boundary (thick line)
because the line q 0 q 1 is not inside . But if the holes are triangulated too (doted line) then
the problem does not arise.
Two problems need to be solved:
What if q 1 is not in 0h ? Then Step 5 will stop with a boundary triangle. So we add a step
which test the distance of q 1 with the two adjacent boundary edges and select the nearest,
and so on till the distance grows.
What if 0h is not convex and the marching process of Step 4 locks on a boundary? By
construction Delaunay-Vorono mesh generators always triangulate the convex hull of the
vertices of the domain. So we make sure that this information is not lost when Th0 , Th1 are
constructed and we keep the triangles which are outside the domain in a special list. Hence
in step 5 we can use that list to step over holes if needed.
Note 6.3 Some time in rare case the interpolation process miss some point, we cane change the
seach algorithm through global variable searchMethod
searchMethod=0;
//
default value for fast search algorithm
searchMethod=1;
//
safe seach algo, use brute force in case of missing
point
//
(warning can be very expensive in case of lot point of ouside domain)
searchMethod=2;
//
use alway the brute force very very expensive
Note 6.4 Step 3 requires an array of pointers such that each vertex points to one triangle of the
triangulation.
182
Note 6.5 The operator = is the interpolation operator of FreeFem++ , The continuous finite functions are extended by continuity to the outside of the domain. Try the following example
mesh Ths= square(10,10);
mesh Thg= square(30,30,[x*3-1,y*3-1]);
plot(Ths,Thg,ps="overlapTh.eps",wait=1);
fespace Ch(Ths,P2); fespace Dh(Ths,P2dc);
fespace Fh(Thg,P2dc);
Ch us= (x-0.5)*(y-0.5);
Dh vs= (x-0.5)*(y-0.5);
Fh ug=us,vg=vs;
plot(us,ug,wait=1,ps="us-ug.eps");
plot(vs,vg,wait=1,ps="vs-vg.eps");
//
//
Figure 6.12: Extension of a continuous FE- Figure 6.13: Extention of discontinuous FEfunction
function, see warning 6
6.8
For FreeFem++ a problem must be given in variational form, so we need a bilinear form a(u, v) ,
a linear form `(f, v), and possibly a boundary condition form must be added.
problem P(u,v) =
a(u,v) - `(f,v)
+ (boundary condition);
Note 6.6 When you want to formulate the problem and to solve it in the same time, you can use
the keywork solve.
6.8.1
183
To present the principles of Variational Formulations or also called weak forms fr the PDEs, let us
take a model problem : a Poisson equation with Dirichlet and Robin Boundary condition .
The problem is: Find u a real function defined on domain of Rd (d = 2, 3) such that
.(u) = f,
in
au +
u
= b on
n
r ,
u=g
on
(6.25)
where
if d = 2 then .(u) = x (x u) + y (y u) with x u =
u
x
and y u =
u
y
u
x
, y u =
u
y
and ,
0 < 0 .
v
u v
u v
where if d = 2 the v.u = ( u
x x + y y ) , where if d = 3 the v.u = ( x x +
and where n is the unitary outside normal of .
u
Now we note that n
= au + b on r and v = 0 on d and = d n thus
Z
Z
Z
u
v
=
auv
bv
n
r
r
bv d,
v V0
(6.26)
u v
y y
u v
z z )
(6.27)
where V0 = {v H 1 ()/v = 0 on d }
Except in the case of Neumann conditions everywhere, the problem (6.27) is well posed when
0 > 0.
Note 6.8 If we have only Neumann boundary condition, linear algebra tells us that the right hand
side must be orthogonal to the kernel of the operator for the solution to exist. One way of writing
the compatibility condition is:
Z
Z
f d +
b d = 0
184
v H 1 ()
(6.28)
R
dy(u)*dy(v)) ) // R
v.u d
//
auv
d
Rr
//
f
v
d
R
//
bv d
r
//
u = g on d
where Th is a mesh of the the bidimensional domain , and gd and gn are respectively the boundary
label of boundary d and n .
And the the three dimensional problem (6.27) becomes
macro Grad(u) [dx(u),dy(u),dz(u) ] //
EOM : definition of the 3d Grad macro
problem Pw(u,v) =
R
int3d(Th)( kappa*( Grad(u)*Grad(v) ) ) //
v.u
R d
+ int2d(Th,gn)( a * u*v )
//
auv d
Rr
- int3d(Th)(f*v)
//
R f v d
- int2d(Th,gn)( b * v )
//
bv d
r
+ on(gd)(u= g) ;
//
u = g on d
where Th is a mesh of the three dimensional domain , and gd and gn are respectively the boundary
label of boundary d and n .
6.9
The parameters are FE functions real or complex, the number n of parameters is even (n = 2 k),
the k first function parameters are unknown, and the k last are test functions.
Note 6.9 If the functions are a part of vectoriel FE then you must give all the functions of the
vectorial FE in the same order (see laplaceMixte problem for example).
Note 6.10 Dont mix complex and real parameters FE function.
Bug: 1 The mixing of fespace with different periodic boundary condition is not implemented. So
all the finite element spaces used for test or unknown functions in a problem, must have the same
type of periodic boundary condition or no periodic boundary condition. No clean message is given
and the result is impredictible, Sorry.
The parameters are:
185
||
||Ax0 b||
init= boolean expression, if it is false or 0 the matrix is reconstructed. Note that if the mesh
changes the matrix is reconstructed too.
precon= name of a function (for example P) to set the preconditioner.
function P must be
func real[int]
tgv= Huge value (1030 ) used to implement Dirichlet boundary conditions, see page 188 for description.
tolpivot= set the tolerance of the pivot in UMFPACK (101 ) and, LU, Crout, Cholesky factorisation (1020 ).
tolpivotsym= set the tolerance of the pivot sym in UMFPACK
strategy= set the integer UMFPACK strategy (0 by default).
6.10
Problem definition
int3d(Th)( K*v*w) =
XZ
T Th T
Kvw
186
int3d(Th,1)( K*v*w) =
Kvw
T
T Th,T 1
int3d(Th,levelset=phi)( K*v*w) =
XZ
Kvw
T Th T,<0
int3d(Th,l,levelset=phi)( K*v*w) =
Kvw
T,<0
T Th,T l
XZ
int2d(Th,2,5)( K*v*w) =
Kvw
T Th (T )(2 5 )
int2d(Th,1)( K*v*w) =
Kvw
T
T Th,T 1
XZ
int2d(Th,2,5)( K*v*w) =
Kvw
T Th (T )(2 5 )
int2d(Th,levelset=phi)( K*v*w) =
XZ
Kvw
T Th T,=0
int2d(Th,l,levelset=phi)( K*v*w) =
Kvw
T,=0
T Th,T l
intallfaces(Th)( K*v*w) =
XZ
Kvw
T Th T
intallfaces(Th,1)( K*v*w) =
Kvw
T
T Th,T 1
- they contribute to the sparse matrix of type matrix which, whether declared explicitly
or not is contructed by FreeFem++ .
bilinear part for 2d meshes Th
-
int2d(Th)( K*v*w) =
XZ
Kvw
T Th T
int2d(Th,1)( K*v*w) =
T Th,T 1
Z
Kvw
T
int2d(Th,levelset=phi)( K*v*w) =
XZ
Kvw
T Th T,<0
int2d(Th,l,levelset=phi)( K*v*w) =
T Th,T l
int1d(Th,2,5)( K*v*w) =
XZ
Kvw
T Th (T )(2 5 )
int1d(Th,1)( K*v*w) =
T Th,T 1
int1d(Th,2,5)( K*v*w) =
Z
Kvw
T
XZ
T Th (T )(2 5 )
Kvw
Kvw
T,<0
187
int1d(Th,levelset=phi)( K*v*w) =
XZ
Kvw
T Th T,=0
int1d(Th,l,levelset=phi)( K*v*w) =
Kvw
T Th,T l
XZ
intalledges(Th)( K*v*w) =
T,=0
Kvw
T Th T
intalledges(Th,1)( K*v*w) =
Kvw
T
T Th,T 1
- they contribute to the sparse matrix of type matrix which, whether declared explicitly
or not is contructed by FreeFem++ .
the right hand-side of the PDE in 3d, the terms of the linear form: for given functions K, f :
XZ
- int3d(Th)( K*w) =
Kw
T Th T
int3d(Th,l)( K*w) =
T Th,T l
Z
Kw
T
int3d(Th,levelset=phi)( K*w) =
XZ
Kw
T Th T,<0
int3d(Th,l,levelset=phi)( K*w) =
Kw
T Th,T l
int2d(Th,2,5)( K*w) =
XZ
T,<0
Kw
T Th (T )(2 5 )
int2d(Th,levelset=phi)( K*w) =
XZ
Kw
T Th T,=0
int2d(Th,l,levelset=phi)( K*w) =
Kw
T Th,T l
intallfaces(Th)( f*w) =
XZ
T,=0
fw
T Th T
int2d(Th,l)( K*w) =
X
T Th,T l
Z
Kw
T
int2d(Th,levelset=phi)( K*w) =
XZ
Kw
T Th T,<0
int2d(Th,l,levelset=phi)( K*w) =
X
T Th,T l
Z
Kw
T,<0
188
int1d(Th,2,5)( K*w) =
XZ
Kw
T Th (T )(2 5 )
int1d(Th,levelset=phi)( K*w) =
XZ
Kw
T Th T,=0
int1d(Th,l,levelset=phi)( K*w) =
X
T Th,T l
intalledges(Th)( f*w) =
XZ
Z
Kw
T,=0
fw
T Th T
on(1, u = g )
The meaning is for all degree of freedom i of the boundary refered by 1, the diagonal
term of the matrix aii = tgv with the terrible geant value tgv (=1030 by default) and
the right hand side b[i] = (h g)[i] tgv, where the (h g)g[i] is the boundary node
value given by the interpolation of g.
remark, if tgv < 0 then we put to 0 all term of the line i in the matrix, except diagonal
term aii = 1, and b[i] = (h g)[i]. (version 3.10) .
An on vectorial form (for Dirichlet ) :
on(1,u1=g1,u2=g2) If you have vectorial finite element like RT0, the 2 components are coupled, and so you have : b[i] =
(h (g1, g2))[i] tgv, where h is the vectorial finite element interpolant.
a linear form on (for Neumann in 2d )
f*w)
Note 6.11
If needed, the different kind of terms in the sum can appear more than once.
the integral mesh and the mesh associated to test function or unknown function can be different in the case of linear form.
N.x, N.y and N.z are the normals components.
Important: it is not possible to write in the same integral the linear part and the bilinear part such
as in int1d(Th)( K*v*w - f*w) .
6.11
189
Numerical Integration
L
X
c` f ( ` )
(6.29)
`=1
then we have an error estimate (see Crouzeix-Mignot (1984)), and then there exists a constant
C > 0 such that,
Z
L
X
` f ( ` ) C|D|hr+1
(6.30)
f (x)
D
`=1
for any function r + 1 times continuously differentiable f in D, where h is the diameter of D and
|D| its measure (a point in the segment [q i q j ] is given as
{(x, y)| x = (1 t)qxi + tqxj , y = (1 t)qyi + tqyj , 0 t 1}).
For a domain h =
Pnt
k=1 Tk ,
Z
f (x)ds = int1d(Th)(f)
h
= int1d(Th,qfe=*)(f)
= int1d(Th,qforder=*)(f)
where * stands for the name of the quadrature formula or the precision (order) of the Gauss formula.
Quadature formula on an edge
qforder= point in [q i q j ](= t)
`
2
1/2
|q i q j |
p
3
(1 p1/3)/2
|q i q j |/2
6
(1 3/5)/2
(5/18)|q i q j |
(8/18)|q i q j |
1/2
L
1
2
3
(qfe=)
qf1pE
qf2pE
qf3pE
qf4pE
qf5pE
10
30
(1 525+70
35 )/2.
30
)/2.
(1 52570
35
(1
245+14 70
)/2
21
1/2
(1
2
qf1pElump
24514 70
)/2
21
0
+1
18 30 i j
|q q |
72
18+ 30 i j
|q q |
72
32213 70 i j
|q q |
1800
64 i j
|q
q
|
225
322+13 70 i j
|q q |
1800
|q i q j |/2
exact on Pk , k =
1
3
5
7
|q i q j |/2
where |q i q j | is the length of segment q i q j . For a part 1 of h with the label 1, we can calculate
the integral over 1 by
Z
f (x, y)ds = int1d(Th,1)(f)
1
= int1d(Th,1,qfe=qf2pE)(f)
190
[q k1 q k2 q k3 ]
= int2d(Th,qft=*)(f)
= int2d(Th,qforder=*)(f)
where * stands for the name of quadrature formula or the order of the Gauss formula.
L
1
3
qft=
qf1pT
qf2pT
qf5pT
qf1pTlump
qf2pT4P1
15
21
qf7pT
qf9pT
6 15 6 15
,
21
21
6 15 9+2 15
,
21
21
9+2 15 6 15
,
21
21
6+ 15 6+ 15
21 , 21
6+ 15 92 15
,
21
21
92 15 6+ 15
,
21
21
8
10
(0, 0)
(1, 0)
(0, 1)
1 3
4, 4
3 1
4 , 4
0, 14
0, 34
1
4 , 0
3
4 , 0
1 1
4, 4
1 1
4, 2
1 1
2, 4
see [43] for detail
see [43] for detail
exact on Pk , k =
1
2
(155 15)|Tk |
1200
(155 15)|Tk |
1200
(155 15)|Tk |
1200
(155+ 15)|Tk |
1200
(155+ 15)|Tk |
1200
(155+ 15)|Tk |
1200
|Tk |/3
|Tk |/3
|Tk |/3
|Tk |/12
|Tk |/12
|Tk |/12
|Tk |/12
|Tk |/12
|Tk |/12
|Tk |/6
|Tk |/6
|Tk |/6
7
9
191
P t
Tk , Th = {Tk }, we can calculate the integral over h
For a three dimensional domain h = nk=1
by
Z
f (x, y) = int3d(Th)(f)
h
= int3d(Th,qfV=*)(f)
= int3d(Th,qforder=*)(f)
where * stands for the name of quadrature formula or the order of the Gauss formula.
L
1
4
14
qfV=
qfV1
qfV2
qfV5
qfV1lump
exact on Pk , k =
1
2
5
Where G4(a, b, b) such that a + 3b = 1 is the set of the four point in barycentric coordinate
{(a, b, b, b), (b, a, b, b), (b, b, a, b), (b, b, b, a)}
and where G6(a, b, b) such that 2a + 2b = 1 is the set of the six points in barycentric coordinate
{(a, a, b, b), (a, b, a, b), (a, b, b, a), (b, b, a, a), (b, a, b, a), (b, a, a, b)}.
Note 6.12 These tetrahedral quadrature formulae come from http://www.cs.kuleuven.be/
nines/research/ecf/mtables.html
Note 6.13 By default, we use the formula which is exact for polynomials of degree 5 on triangles
or edges (in bold in three tables).
This possible to add an own quadrature formulae with using plugin "qf11to25" on segment ,
triangle or Tetrahedron. The quadrature formulae in D dimension is a bidimentional array of size
Nq (D + 1) such that the D + 1 value of on row i = 0, ..., Np 1 are wi , x
i1 , ..., x
iD where
P
i1 , ..., x
iD is the barycentric coordinate
ik , x
wi is the weight of the quadrature point, and 1 D
k=1 x
the quadrature point.
//
load "qf11to25"
(version 3.19-1)
//
load plugin
//
Quadrature on segment
real[int,int] qq1=[
[0.5,0],
[0.5,1]];
QF1 qf1(1,qq1);
//
def of quadrature formulae qf1 on segment
//
remark:
//
1 is the order of the quadrature exact for polynome of degree < 1)
192
//
Quadrature on triangle
real[int,int] qq2=[
[1./3,0,0],
[1./3.,1,0],
[1./3.,0,1]];
QF2 qf2(1,qq2);
//
def of quadrature formulae qf2 on triangle
//
remark:
//
1 is the order of the quadrature exact for polynome of degree < 1)
P
//
so must have = wi = 1
//
Quadrature on Tetrahedron
real[int,int] qq3=[
[1./4,0,0,0],
[1./4.,1,0,0],
[1./4.,0,1,0],
[1./4.,0,0,1]];
QF3 qf3(1,qq3);
//
def of quadrature formulae qf3 on get.
//
remark:
//
1 is the order of the quadrature exact for polynome of degree < 1)
//
real I1 = int1d(Th,qfe=qf1)(x2) ;
real I1l = int1d(Th,qfe=qf1pElump)(x2) ;
real I2 = int2d(Th,qft=qf2)(x2) ;
real I2l = int2d(Th,qft=qf1pTlump)(x2) ;
cout <<
cout <<
assert(
assert(
the output is
1.67 == 1.67
0.335 == 0.335
6.12
In FreeFem++ it is possible to define variational forms, and use them to build matrices and vectors
and store them to speed-up the script (4 times faster here).
For example let us solve the Thermal Conduction problem of section 3.4.
The variational formulation is in L2 (0, T ; H 1 ()); we shall seek un satisfying
Z
w V0 ;
un un1
w + un w) +
t
(un uue )w = 0
193
So the to code the method with the matrices A = (Aij ), M = (Mij ), and the vectors un , bn , b0 , b, bcl
( notation if w is a vector then wi is a component of the vector).
n
u =A
Where with
Aij
Mij
b0,i
1 n
b ,
b = b0 + M u
n1
1
b = bcl ,
bni
=
bi if i 24
b0i else if 6 24
(6.31)
= tgv = 1030 :
1
if i 24 , andj = i
Z
Z
=
wj wi /dt + k(wj .wi ) +
wj wi else if i 6 24 , orj 6= i
13
1
Z
if i 24 , andj = i
=
wj wi /dt else if i 6 24 , orj 6= i
Z
uue wi
=
13
0
bcl = u
//
(6.32)
(6.33)
(6.34)
(6.35)
int1d(Th,1,3)(alpha*ue*v);
+ on(2,4,u=1);
real[int]
real[int]
real[int]
b0 = vthermic0(0,Vh);
//
constant part of the RHS
bcn = vthermic(0,Vh); //
tgv on Dirichlet boundary node ( !=0 )
//
we have for the node i : i 24 bcn[i] 6= 0
bcl=tgv*u0[];
//
the Dirichlet boundary condition part
194
Note 6.14 The boundary condition is implemented by penalization and vector bcn contains the
contribution of the boundary condition u= 1 , so to change the boundary condition, we have just to
multiply the vector bc[] by the current value f of the new boundary condition term by term with the
operator .*. Section 9.6.2 Examples++-tutorial/StokesUzawa.edp gives a real example of
using all this features.
And the new version of the algorithm is now:
ofstream ff("thermic.dat");
for(real t=0;t<T;t+=dt){
real[int] b = b0 ;
b += M*u[];
//
do i:
b = bcn ? bcl : b ;
//
u[] = A-1*b;
ff << t <<" "<<u(3,0.5)<<endl;
plot(u);
//
for the RHS
add the the time dependent part
//
lock boundary part:
b[i] = bcn[i] ? bcl[i] : b[i] ;
}
for(int i=0;i<20;i++)
cout<<dy(u)(6.0*i/20.0,0.9)<<endl;
plot(u,fill=true,wait=1,ps="thermic.eps");
Note 6.15 The functions appearing in the variational form are formal and local to the varf definition, the only important thing is the order in the parameter list, like in
varf vb1([u1,u2],q) = int2d(Th)( (dy(u1)+dy(u2)) *q) + int2d(Th)(1*q);
varf vb2([v1,v2],p) = int2d(Th)( (dy(v1)+dy(v2)) *p) + int2d(Th)(1*p);
To build matrix A from the bilinear part the variational form a of type varf simply write:
A = a(Vh,Wh [, ...] );
//
where
//
Vh is "fespace" for the unknow fields with a correct number of component
//
Wh is "fespace" for the test fields with a correct number of component
" are
195
||
||Ax0 b||
real b(Vh.ndof);
b = a(0,Vh);
A first example to compute the area of each triangle K of mesh T h, just do:
fespace Nh(Th,P0);
Nh areaK;
varf varea(unused,chiK) =
etaK[]= varea(0,Ph);
//
int2d(Th)(chiK);
Effectively, the basic functions of space N h, are the characteristic function of the element of Th,
and the numbering is the numeration of the element, so by construction:
Z
Z
1;
etaK[i] = 1|Ki =
Ki
Now, we can use this to compute error indicators like in examples AdaptResidualErrorIndicator.edp in directory examples++-tutorial.
First to compute a continuous approximation to the function h density mesh size of the mesh
T h.
fespace Vh(Th,P1);
Vh h ;
real[int] count(Th.nv);
varf vmeshsizen(u,v)=intalledges(Th,qfnbpE=1)(v);
varf vedgecount(u,v)=intalledges(Th,qfnbpE=1)(v/lenEdge);
//
computation of the mesh size
//
----------------------------count=vedgecount(0,Vh);
//
number of edge / vertex
196
//
//
he |[
uh 2
]|
n
where hK is size of the longest edge ( hTriangle), he is the size of the current edge ( lenEdge),
n the normal.
fespace Nh(Th,P0);
//
the space function contant / triangle
Nh etak;
varf vetaK(unused,chiK) =
intalledges(Th)(chiK*lenEdge*square(jump(N.x*dx(u)+N.y*dy(u))))
+int2d(Th)(chiK*square(hTriangle*(f+dxx(u)+dyy(u))) );
etak[]= vetaK(0,Ph);
We add automatic expression optimization by default, if this optimization creates problems, it can
be removed with the keyword optimize as in the following example : ,
varf a(u1,u2)= int2d(Th,optimize=0)( dx(u1)*dx(u2) + dy(u1)*dy(u2) )
+ on(1,2,4,u1=0) + on(3,u1=1) ;
or you can also do optimisation and remove the check by setting optimize=2 (version 3.38).
Remark, it is all possible to build interpolation matrix, like in the following example:
mesh
mesh
mesh
TH = square(3,4);
th = square(2,3);
Th = square(4,4);
fespace VH(TH,P1);
fespace Vh(th,P1);
fespace Wh(Th,P1);
matrix B= interpolate(VH,Vh);
matrix BB= interpolate(Wh,Vh);
//
//
and after some operations on sparse matrices are available for example
int N=10;
real [int,int] A(N,N);
real [int] a(N),b(N);
A =0;
for (int i=0;i<N;i++)
{
A(i,i)=1+i;
if(i+1 < N)
A(i,i+1)=-i;
a[i]=i;
}
b=A*b;
cout << "xxxx\n";
matrix sparseA=A;
cout << sparseA << endl;
sparseA = 2*sparseA+sparseA;
//
a full matrix
197
sparseA = 4*sparseA+sparseA*5;
matrix sparseB=sparseA+sparseA+sparseA; ;
cout << "sparseB = " << sparseB(0,0) << endl;
6.13
Interpolation matrix
It is also possible to store the matrix of a linear interpolation operator from a finite element space
Vh to another Wh to interpolate(Wh ,Vh ,...) a function. Note that the continuous finite
functions are extended by continuity outside of the domain.
The named parameters of function interpolate are:
inside= set true to create zero-extension.
t= set true to get the transposed matrix
op= set an integer written below
0 the default value and interpolate of the function
1 interpolate the x
2 interpolate the y
3 interpolate the z
U2Vc= set the which is the component of Wh come in Vh in interpolate process in a int array so
the size of the array is number of component of Wh , if the put 1 then component is set to
0, like in the following example: (by default the component number is unchanged).
fespace V4h(Th4,[P1,P1,P1,P1]);
fespace V3h(Th,[P1,P1,P1]);
int[int] u2vc=[1,3,-1];
//
-1 => put zero on the component
matrix IV34= interpolate(V3h,V4h,inside=0,U2Vc=u2vc);
//
V3h <- V4h
V4h [a1,a2,a3,a4]=[1,2,3,4];
V3h [b1,b2,b3]=[10,20,30];
b1[]=IV34*a1[];
So here we have:
b1 == 2, b2 == 4, b3 == 0 . .
//
//
//
here v == vv =>
198
//
cout << " IV Vh<-Vh4 (inside=1) " << IV0 << endl;
matrix IVt0= interpolate(Vh,Vh4,inside=1,t=1);
cout << " IV Vh<-Vh4t (inside=1) " << IVt0 << endl;
matrix IV4t0= interpolate(Vh4,Vh);
cout << " IV Vh4<-Vht " << IV4t0 << endl;
matrix IW4= interpolate(Wh4,Wh);
cout << " IV Wh4<-Wh " << IW4 << endl;
matrix IW4V= interpolate(Wh4,Vh);
cout << " IV Wh4<-Vh " << IW4 << endl;
(u1 u0 )
+ (u1 u0 ) = 0 on .
n
v
+
u
v
=
fv
i
i
i
i
Z
Z
(uj v f v) +
uj v, v H01 (), i, j = [0, 1] [1, 0]
j
To discretized with the P 1 triangular LagrangianR finite element space Vh simply replace H01 () by
Vh (0 ) Vh (1 ). Then difficulty is to compute j uj v when v is a basis function of Vh (i ),
i 6= j. It is done as follows (with = )
//
fespace Vh0(Th[0],P1),Vh1(Th[1],P1);
Vh0 u0=0;Vh1 u1=0;
macro grad(u) [dx(u),dy(u)]
//
varf a(u,v)=int2d(Th[i])(grad(u)*grad(v))+int1d(Th[i],Interface)(alpha*u*v)
+on(Gamma,u=0);
varf b(u,v)=int2d(Th[i])(f*v)+on(Gamma,u=0);
199
varf du1dn(u,v)=-int2d(Th[1])(grad(u1)*grad(v)-f*v)+int1d(Th[1],Sigma)(alpha*u1*v)
+on(Gamma,u=0);
varf du0dn(u,v)=-int2d(Th[0])(grad(u0)*grad(v)-f*v)+int1d(Th[0],Sigma)(alpha*u0*v)
+on(Gamma,u=0);
matrix I01=interpolate(Vh1,Vh0);
matrix I10=interpolate(Vh0,Vh1);
matrix[int] A(2);
A[0]=a(Vh0,Vh0);
A[1]=a(Vh1,Vh1);
for(int iter=0;iter<Niter;iter++)
{
//
Solve on Th[0]
//
Solve on Th[1]
{
real[int] b0=b(0,Vh0);
real[int] Du1dn=du1dn(0,Vh1);
real[int] Tdu1dn(Vh0.ndof); Tdu1dn=I01*Du1dn;
b0+=Tdu1dn;
u0[]=A[0]-1*b0;
}
{
real[int] b1=b(0,Vh1);
real[int] Du0dn=du0dn(0,Vh0);
real[int] Tdu0dn(Vh1.ndof); Tdu0dn=I10*Du0dn;
b1+=Tdu0dn;
u1[]=A[1]-1*b1;
}
plot(u0,u1,cmm="iter="+iter);
}
6.14
Here, we show how to get informations on a finite element space Wh (Tn , ), where * may be P1,
P2, P1nc, etc.
Wh.nt gives the number of element of Wh
Wh(Th,P2);
" nb of degree of freedom
: " << Wh.ndof << endl;
" nb of degree of freedom / ELEMENT : " << Wh.ndofK << endl;
2, kdf= Wh.ndofK ;;
//
element 2
200
Chapter 7
Visualization
Results created by the finite element method can be a huge set of data, so it is very important to
render them easy to grasp. There are two ways of visualization in FreeFem++ : One, the default
view, supports the drawing of meshes, isovalues of real FE-functions and of vector fields, all by
the command plot (see Section 7.1 below). For publishing purpose, FreeFem++ can store these
plots as postscript files.
Another method is to use external tools, for example, gnuplot (see Section 7.2), medit (see Section
7.3) using the command system to launch them and/or to save the data in text files.
7.1
Plot
With the command plot, meshes, isovalues of scalar functions and vector fields can be displayed.
The parameters of the plot command can be , meshes, real FE functions , arrays of 2 real FE
functions, arrays of two arrays of double, to plot respectively a mesh, a function, a vector field, or
a curve defined by the two arrays of double.
Note 7.1 The length of an arrow is always bound to be in [5, 5%] of the screen size, to see
something (else it will only look like porcupine).
The parameters are
wait= boolean expression to wait or not (by default no wait). If true we wait for a keyboard event
or mouse event, they respond to an event by the following characters
enter try to show plot
p previous plot (10 plots saved)
? show this help
+,- zoom in/out around the cursor 3/2 times
= reset vue
r refresh plot
up, down, left, right special keys to tanslate
3 switch 3d/2d plot keys :
z,Z) focal zoom unzoom
H,h switch increase or decrease the Z scale of the plot
mouse motion
201
202
CHAPTER 7. VISUALIZATION
- left button rotate
- right button zoom (ctrl+button on mac)
- right button +alt tanslate (alt+ctrl+button on mac)
a,A increase or decrease the arrow size
B switch between show border meshes or not
i,I update or not: the min/max bound of the functions to the window
n,N decrease or increase the number of iso value array
b switch between black and white or color plotting
g switch between grey or color plotting
f switch between filling iso or iso line
l switch between lighting or not
v switch between show or not the numerical value of colors
m switch between show or not meshes
w window dump in file ffglutXXXX.ppm
* keep/unkeep viewpoint for next plot
k complex data / change view type
ESC close the graphics process before version 3.22, after no way to close.
otherwise do nothing.
ps= string expression for the name of the file to save the plot in postscript (sorry no save of 3d
plot)
coef= the vector arrow size between arrow unit and domain unit.
fill= fill color between iso-values (mandatory of P0 finite element).
cmm= string expression to write the graphic window into
value= to plot the value of isolines and the value of vector arrows.
aspectratio= boolean to be sure that the aspect ratio of plot is preserved or not.
bb= array of 2 array ( like [[0.1,0.2],[0.5,0.6]]), to set the bounding box and specify a
partial view where the box defined by the two corner points [0.1,0.2] and [0.5,0.6].
nbiso= (int) sets the number of isovalues (20 by default)
nbarrow= (int) sets the number of colors of arrow values (20 by default)
viso= sets the array of isovalues (an array real[int] of increasing values)
varrow= sets the array of color arrows values (an array real[int])
bw= (bool) sets or not the plot in black and white color.
grey= (bool) sets or not the plot in grey color.
7.1. PLOT
203
hsv= (array of float) to defined color of 3*n value in HSV color model declared for example by
real[int] colors = [h1,s1,v1,... ,
hn,vn,vn];
204
CHAPTER 7. VISUALIZATION
For example:
real[int] xx(10),yy(10);
mesh Th=square(5,5);
fespace Vh(Th,P1);
Vh uh=x*x+y*y,vh=-y2+x2;
int i;
//
compute a cut
for (i=0;i<10;i++)
{
x=i/10.; y=i/10.;
xx[i]=i;
yy[i]=uh;
//
value of uh at point (i/10. , i/10.)
}
plot(Th,uh,[uh,vh],value=true,ps="three.eps",wait=true);
//
figure 7.1
//
zoom on box defined by the two corner points [0.1,0.2] and [0.5,0.6]
plot(uh,[uh,vh],bb=[[0.1,0.2],[0.5,0.6]],
wait=true,grey=1,fill=1,value=1,ps="threeg.eps");
//
figure 7.2
plot([xx,yy],ps="likegnu.eps",wait=true);
//
figure 7.3
Figure 7.1:
IsoValue
0.05
0.15
0.25
0.35
0.45
0.55
0.65
0.75
0.85
0.95
1.05
1.15
1.25
1.35
1.45
1.55
1.65
1.75
1.85
1.95
IsoValue
-0.105263
0.0526316
0.157895
0.263158
0.368421
0.473684
0.578947
0.684211
0.789474
0.894737
1
1.10526
1.21053
1.31579
1.42105
1.52632
1.63158
1.73684
1.84211
2.10526
Vec Value
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
Vec Value
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
To change the color table and to choose the value of iso line you can do :
//
//
//
//
//
//
//
//
//
//
//
//
from: http://en.wikipedia.org/wiki/HSV_color_space
The HSV (Hue, Saturation, Value) model,
defines a color space in terms of three constituent components:
HSV color space as a color wheel 7.4
Hue, the color type (such as red, blue, or yellow):
Ranges from 0-360 (but normalized to 0-100% in some applications Here)
Saturation, the "vibrancy" of the color: Ranges from 0-100%
The lower the saturation of a color, the more "grayness" is present
and the more faded the color will appear.
Value, the brightness of the color:
Ranges from 0-100%
7.1. PLOT
205
Figure 7.3: Plots a cut of uh. Note that a refinement of the same can be obtained in
combination with gnuplot
//
real[int] colorhsv=[
4./6., 1 , 0.5,
4./6., 1 , 1,
5./6., 1 , 1,
1, 1. , 1,
1, 0.5 , 1
];
real[int] viso(31);
//
206
CHAPTER 7. VISUALIZATION
7.2
Example 3.2 shows how to generate a gnu-plot from a FreeFem++ file. Let us present here another
technique which has the advantage of being online, i.e. one doesnt need to quit FreeFem++ to
generate a gnu-plot. But this work only if gnuplot1 is installed , and only on unix computer.
Add to the previous example:
{
//
file for gnuplot
ofstream gnu("plot.gp");
for (int i=0;i<=n;i++)
{
gnu << xx[i] << " " << yy[i] << endl;
}
}
//
the file plot.gp is close because the variable gnu is delete
//
1.5
0.5
0
0
10
15
20
7.3
As said above, medit 2 is a freeware display package by Pascal Frey using OpenGL. Then you may
run the following example.
Remark: Now medit software is include in FreeFem++ under ffmedit name.
Now with version 3.2 or better
load "medit"
mesh Th=square(10,10,[2*x-1,2*y-1]);
fespace Vh(Th,P1);
Vh u=2-x*x-y*y;
medit("mm",Th,u);
1
2
http://www.gnuplot.info/
http://www-rocq.inria.fr/gamma/medit/medit.html
207
//
{
ofstream file("mm.bb");
file << "2 1 1 "<< u[].n << " 2 \n";
for (int j=0;j<u[].n ; j++)
file << u[][j] << endl;
}
//
exec("ffmedit mm");
//
exec("rm mm.bb
mm.faces
mm.points");
208
CHAPTER 7. VISUALIZATION
Chapter 8
Algorithms and Optimization
The complete example is in algo.edp file.
8.1
conjugate Gradient/GMRES
Suppose we want to solve the Euler problem (here x has nothing to do with the reserved variable
for the first coordinate in FreeFem++ ): find x Rn such that
J
(x) = 0
(8.1)
J(x) =
xi
where J is a functional (to minimize for example) from Rn to R.
If the function is convex we can use the conjugate gradient to solve the problem, and we just need
the function (named dJ for example) which compute J, so the parameters are the name of that
function with prototype
func real[int] dJ(real[int] & xx);
which compute J, and a vector x of type ( of course the number 20 can be changed)
real[int] x(20);
to initialize the process and get the result.
Given an initial value x(0) , a maximum number imax of iterations, and an error tolerance 0 < < 1:
Put x = x(0) and write
NLCG(J, x, precon= M , nbiter= imax , eps= , stop= stopf unc);
will give the solution of x of J(x) = 0. We can omit parameters precon, nbiter, eps,
stop. Here M is the preconditioner whose default is the identity matrix. The stopping test is
kJ(x)kP kJ(x(0) )kP
Writing the minus value in eps=, i.e.,
NLCG(J, x, precon= M , nbiter= imax , eps= );
we can use the stopping test
kJ(x)k2P
The parameters of these three functions are:
nbiter= set the number of iteration (by default 100)
209
210
precon= set the preconditioner function (P for example) by default it is the identity, remark the
prototype is func real[int] P(real[int] &x).
eps= set the value of the stop test (= 106 by default) if positive then relative test ||J(x)||P
||J(x0 )||P , otherwise the absolute test is ||J(x)||2P ||.
veps= set and return the value of the stop test, if positive then relative test ||J(x)||P
||J(x0 )||P , otherwise the absolute test is ||J(x)||2P ||. The return value is minus
the real stop test (remark: it is useful in loop).
stop= stopf unc add your test function to stop before (after version 3.31). The prototype for the
function stopf unc is
func
where u is the current solution, and g the current gradient not preconditioned.
Example 8.1 (algo.edp) For a given function b, let us find the minimizer u of the functional
Z
Z
1
2
f (|u| )
ub
J(u) =
2
x
1
f (x) = ax + x ln(1 + x), f 0 (x) = a +
, f 00 (x) =
1+x
(1 + x)2
under the boundary condition u = 0 on .
func real J(real[int] & u)
{
Vh w;w[]=u;
//
copy array u in the finite element function w
real r=int2d(Th)(0.5*f( dx(w)*dx(w) + dy(w)*dy(w) ) - b*w) ;
cout << "J(u) =" << r << " " << u.min << " " << u.max << endl;
return r;
}
//
----------------------Vh u=0;
//
Ph alpha;
int iter=0;
alpha=df( dx(u)*dx(u) + dy(u)*dy(u) );
optimization
211
We want to construct also a preconditioner C with solving the problem: find uh V0h such that
Z
Z
vh V0h ,
uh .vh =
bvh
where = f 0 (|u|2 ). */
varf alap(uh,vh)=
+ on(1,2,3,4,uh=0);
//
//
//
/* To solve the problem, we make 10 iteration of the conjugate gradient, recompute the preconditioner and restart the conjugate gradient: */
verbosity=5;
int conv=0;
real eps=1e-6;
for(int i=0;i<20;i++)
{
conv=NLCG(dJ,u[],nbiter=10,precon=C,veps=eps);
//
if (conv) break;
//
if converge break loop
alpha=df( dx(u)*dx(u) + dy(u)*dy(u) ); //
recompute alpha optimization
Alap = alap(Vh,Vh,solver=Cholesky,factorize=1);
cout << " restart with new preconditionner " << conv
<< " eps =" << eps << endl;
}
plot (u,wait=1,cmm="solution with NLCG");
212
8.2
Two algorithms of COOOL a package [32] are interfaced with the Newton Raphson method (call
Newton) and the BFGS method. These two ones are directly available in FreeFem (no dynamical
link to load). Be careful with these algorithms, because their implementation uses full matrices.
We also provide several optimization algorithms from the NLopt library [47] as well as an interface
for Hansens implementation of CMAES (a MPI version of this one is also available). These last
algorithms can be found as dynamical links in the example++-load folder as the ff-NLopt and
CMA ES files (CMA ES MPI from the example++-mpi folder for the mpi version).
8.2.1
real[int] b(10),u(10);
func real J(real[int] & u)
{
real s=0;
for (int i=0;i<u.n;i++)
s +=(i+1)*u[i]*u[i]*0.5 - b[i]*u[i];
cout << "J ="<< s << " u =" << u[0] << " " << u[1] << "...\n" ;
return s;
}
//
the grad of J (this is a affine version (the RHS is in )
func real[int] DJ(real[int] &u)
{
for (int i=0;i<u.n;i++)
u[i]=(i+1)*u[i]-b[i];
return u;
//
return of global variable ok
};
b=1; u=2;
//
set right hand side and initial gest
BFGS(J,dJ,u,eps=1.e-6,nbiter=20,nbiterline=20);
cout << "BFGS: J(u) = " << J(u) << endl;
Using the CMA evolution strategy is almost the same, except that, as it is a derivative free optimizer, the dJ argument is omitted and there are some other named parameters to control the
behaviour of the algorithm. With the same objective function as above, an example of utilization
would be (see cmaes-VarIneq.edp for a complete example):
load "ff-cmaes"
...
//
define J, u and all here
real min = cmaes(J,u,stopTolFun=1e-6,stopMaxIter=3000);
cout << "minimal value is " << min << " for u = " << u << endl;
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
(see [48] for more details). Thus, some specific parameters can be passed to control the starting
distribution, size of the sample generations etc... Named parameters for this are the following :
seed= Seed for random number generator (val is an integer). No specified value will lead to a
clock based seed initialization.
8.3. IPOPT
213
initialStdDev= Value for the standard deviations of the initial covariance matrix (val is a
real). If the value is passed, the initial covariance matrix will be set to I. The expected
initial distance between initial X and the argmin should be roughly initialStdDev. Default
is 0.3.
initialStdDevs= Same as above except that the argument is an array allowing to set a value
of the initial standard deviation for each parameter. Entries differing by several orders of
magnitude should be avoided (if it cant be, try rescaling the problem).
stopTolFun= Stops the algorithm if function values differences are smaller than the passed one,
default is 1012 .
stopTolFunHist= Stops the algorithm if function value differences of the best values are smaller
than the passed one, default is 0 (unused).
stopTolX= Stopping criteria triggered if step sizes in the parameters space are smaller than this
real value, default is 0.
stopTolXFactor= Stopping criteria triggered when the standard deviation increases more than
this value. The default value is 103 .
stopMaxFunEval= Stops the algorithm when stopMaxFunEval function evaluations have been
done. Set to 900(n + 3)2 by default, where n is the parameters space dimension .
stopMaxIter= Integer stopping the search when stopMaxIter generations has been sampled.
Unused by default.
popsize= Integer value used to change the sample size. The default value is 4 + b3 ln(n)c, see [48]
for more details. Increasing the population size usually improves the global search capabilities
at the cost of an at most linear reduction of the convergence speed with respect to popsize.
paramFile= This string type parameter allows the user to pass all the parameters using an extern file as in Hansens original code. More parameters related to the CMA-ES algorithm can
be changed with this file. A sample of it can be found in the examples++-load/ffCMAES/
folder under the name initials.par. Note that the parameters passed to the CMAES
function in the FreeFem script will be ignored if an input parameters file is given.
8.3
IPOPT
The ff-Ipopt package is an interface for the IPOPT [49] optimizer. IPOPT is a software library
for large scale, non-linear, constrained optimization. Detailed informations about it are in [49] and
https://projects.coin-or.org/Ipopt. It implements a primal-dual interior point method
along with filter method based line searchs. IPOPT need a direct sparse symmetric linear solver. If
your version of FreeFem has been compiled with the --enable-downlad tag, it will automatically
be linked with a sequential version of MUMPS. An alternative to MUMPS would be to download the
HSL subroutines (see http://www.coin-or.org/Ipopt/documentation/node16.html)
and place them in the /ipopt/Ipopt-3.10.2/ThirdParty/HSL directory of the FreeFem++
downloads folder before compiling.
214
8.3.1
In this section, we give a very brief glimpse at the underlying mathematics of IPOPT. For a deeper
introduction on interior methods for nonlinear smooth optimization, one may consults [50], or [49]
for more IPOPT specific elements. IPOPT is designed to perform optimization for both equality
and inequality constrained problems. Though, nonlinear inequalities are rearranged before the
beginning of the optimization process in order to restrict the panel of nonlinear constraints to
those of the equality kind. Each nonlinear inequality ones are transformed into a pair of simple
bound inequality and nonlinear equality constraint by the introduction of as many slack variables
as is needed : ci (x) 0 becomes ci (x) + si = 0 and si 0, where si is added to the initial variables
of the problems xi . Thus, for convenience, we will assume that the minimization problem does not
contain any nonlinear inequality constraint. It means that, given a function f : Rn 7 R, we want
to find :
x0 = argminf (x)
xV
(8.2)
with V = {x Rn | c(x) = 0 and xl x xu }
Where c : Rn Rm and xl , xu Rn and inequalities hold componentwise. The f function as well
as the constraints c should be twice-continuously differentiable.
As a barrier method, interior points algorithms try to find a Karush-Kuhn-Tucker point for (8.2)
by solving a sequence of problems, unconstrained with respect to the inequality constraints, of the
form :
for a given > 0, find x = argmin B(x, )
(8.3)
xRn | c(x)=0
n
X
ln(xu,i xi )
i=1
m
X
ln(xi xl,i ).
i=1
The remaining equality constraints are handled with the usual Lagrange multipliers method. If the
sequence of barrier parameters converge to 0, intuition suggests that the sequence of minimizers
of (8.3) converge to a local constrained minimizer of (8.2). For a given , (8.3) is solved by finding
(x , ) Rn Rm such that :
B(x , ) +
m
X
,i ci (x ) = B(x , ) + Jc (x )T = 0
and c(x ) = 0
(8.4)
i=1
/(xu,1 x1 )
/(x1 xl,1 )
..
..
B(x, ) = f (x) +
.
.
/(xu,n xn )
/(xn xl,n )
If we respectively call zu (x, ) = (/(xu,1 x1 ), . . . , /(xu,n xn )) and zl (x, ) the other vector
appearing in the above equation, then the optimum (x , ) satisfies :
f (x ) + Jc (x )T + zu (x , ) zl (x , ) = 0
and
c(x ) = 0
(8.5)
In this equation, the zl and zu vectors seems to play the role of Lagrange multipliers for the
simple bounds inequalities, and indeed, when 0, they converge toward some suitable Lagrange
multipliers for the KKT conditions, provided some technical assumptions are fulfilled (see [50]).
Equation 8.5 is solved by performing a Newton method in order to find a solution of (8.4) for each
of the decreasing values of . Some order 2 conditions are also taken into account to avoid convergence to local maximizer, see [50] for precision about them. In the most classical IP algorithms, the
Newton method is directly applied to (8.4). This is in most case inefficient due to frequent computation of infeasible points. These difficulties are avoided in Primal-Dual interior points methods
8.3. IPOPT
215
where (8.4) is transformed into an extended system where zu and zl are treated as unknowns and
the barrier problems are finding (x, , zu , zl ) Rn Rm Rn Rn such that :
f (x) + Jc (x)T + zu zl = 0
c(x) = 0
(8.6)
(Xu X)zu e = 0
(X Xl )zl e = 0
Where if a is a vector of Rn , A denotes the diagonal matrix A = (ai ij )1i,jn and e Rn =
(1, 1, . . . , 1). Solving this nonlinear system by the Newton methods is known as being the primaldual interior points method. Here again, more details are available in [50]. Most actual implementations introduce features in order to globalize the convergence capability of the method, essentially
by adding some line-search steps to the Newton algorithm, or by using trust regions. For the purpose of IPOPT, this is achieved by a filter line search methods, the details of which can be found
in [?].
More IPOPT specific features or implementation details can be found in [49]. We will just retain
that IPOPT is a smart Newton method for solving constrained optimization problem, with global
convergence capabilities due to a robust line search method (in the sense that the algorithm will
convergence no matter the initializer). Due to the underlying Newton method, the optimization
process requires expressions of all derivatives up to the order 2 of the fitness function as well as
those of the constraints. For problems whose hessian matrices are difficult to compute or lead to
high dimensional dense matrices, it is possible to use a BFGS approximation of these objects at
the cost of a much slower convergence rate.
8.3.2
IPOPT in FreeFem++
Calling 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.
lb
i m, ci ci (x) cub
(constraints
functions)
i
(8.7)
Where ub and lb stand for upper bound and lower bound. If for some i, 1 i m we have
ub
lb
ub
clb
i = ci , it means that ci is an equality constraint, and an inequality one if ci < ci .
There are different ways to pass the fitness function and constraints. The more general one is to
define the functions using the keyword func. Any returned matrix must be a sparse one (type
matrix, not a real[int,int]) :
func real J(real[int] &X) {...}
//
func real[int] gradJ(real[int] &X) {...}
func real[int] C(real[int] &X) {...}
func matrix jacC(real[int] &X) {...}
//
//
Constraints
Constraints jacobian
Warning 1 : in the current version of FreeFem++ , returning a matrix object local to a function
block leads to undefined results. For each sparse matrix returning function you define, an extern
matrix object has to be declared, whose associated function will overwrite and return on each call.
Here is an example for jacC :
matrix jacCBuffer;
//
216
//
fill jacCBuffer
Warning 2: IPOPT requires the structure of each matrix at the initialization of the algorithm. Some
errors may occur if the matrices are not constant and are built with the matrix A = [I,J,C]
syntax, or with an intermediary full matrix (real[int,int]), because any null coefficient is
discarded during the construction of the sparse matrix. It is also the case when making matrices
linear combinations, for which any zero coefficient will result in the suppression of the matrix from
the combination. Some controls are available to avoid such problems. Check the named parameters
descriptions (checkindex, structhess and structjac can help). We strongly advice to use
varf as much as possible for the matrix forging.
The hessian returning function is somewhat different because it has to be the hessian of the lam
X
grangian function : (x, f , ) 7 f 2 f (x) +
i 2 ci (x) where Rm and R. Your hessian
i=1
If the constraints functions are all affine, or if there are only simple bounds constraints or no
constraint at all, the lagrangian hessian is equal to the fitness function hessian, one can then omit
the sigma and lambda parameters :
matrix hessianJBuffer;
func matrix hessianJ(real[int] &X) {...}
constraints are affine
//
If the hessian is omitted, the interface will tell IPOPT to use the (L)BFGS approximation (it can
also be enabled with a named parameter, see further). Simple bounds or unconstrained problems
do not require the constraints part, so the following expressions are valid :
IPOPT(J,gradJ,C,jacC,Xi, ... );
IPOPT(J,gradJ,hessianJ,Xi, ... );
IPOPT(J,gradJ,Xi, ... );
//
//
IPOPT with BFGS
Newton IPOPT without constraints
//
BFGS, no constraints
Simple bounds are passed using the lb and ub named parameters, while constraints bounds are
passed with the clb and cub ones. Unboundedness in some directions can be achieved by using
the 1e19 and 1e19 values that IPOPT recognizes as + and :
real[int] xlb(n),xub(n),clb(m),cub(m);
...
//
fill the arrays...
IPOPT(J,gradJ,hessianL,C,jacC,Xi,lb=xlb,ub=xub,clb=clb,cub=cub, /*some other
named parameters*/ );
8.3. IPOPT
217
P2 fitness function and affine constraints function : In the case where the fitness function or
constraints function can be expressed respectively in the following forms :
x Rn , f (x) = 12 hAx, xi + hb, xi (A, b) Mn,n (R) Rn
or, C(x) = Ax + b
(A, b) Mn,m (R) Rm
where A and b are constant, it is possible to directly pass the (A, b) pair instead of defining 3 (or
2) functions. It also indicates to IPOPT that some objects are constant and that they have to be
evaluated only once, thus avoiding multiple copies of the same matrix. The syntax is :
//
Affine constraints with "standard" fitness function
matrix A= ... ;
//
Linear part of the constraints
real[int] b = ... ;
//
Constant part of constraints
IPOPT(J,gradJ,hessianJ, [A,b] ,Xi, /*bounds and named params*/);
//
[b,A] would work as well... Scatterbrains pampering...
Note that if you define the constraints in this way, they doesnt contribute to the hessian, so the
hessian should only take one real[int] as argument.
//
Affine constraints and P2 fitness func:
matrix A= ... ;
//
Bilinear form matrix
real[int] b = ... ;
//
Linear contribution to f
matrix Ac= ... ;
//
Linear part of the constraints
real[int] bc= ... ;
//
Constant part of constraints
IPOPT([A,b], [Ac,bc] ,Xi, /*bounds and named params*/);
If both objective and constraints functions are given this way, it automatically activates the IPOPT
mehrotra algorithm option (better for linear and quadratric programming according to the
documentation). Otherwise, this option can only be set through the option file (see the named
parameters section).
A spurious case is the one of defining f in this manner while using standard functions for the
constraints :
matrix A= ... ;
//
Bilinear form matrix
real[int] b = ... ;
//
Linear contribution to f
func real[int] C(real[int] &X) {...}
//
Constraints
func matrix jacC(real[int] &X) {...}
//
Constraints jacobian
IPOPT([A,b],C,jacC,Xi, /*bounds and named params*/);
Indeed, when passing [A,b] in order to define f , the lagrangian hessian is automatically build
has the constant x 7 A function, with no way to add possible constraints contributions, leading
to incorrect second order derivatives. So, a problem should be defined like that in only two cases :
1) constraints are nonlinear but you want to use the BFGS mode (then add bfgs=1 to the named
parameter), 2) constraints are affine, but in this case, why not passing them in the same way?
Here are some other valid definitions of the problem (cases when f is a pure quadratic or linear
form, or C a pure linear function, etc...) :
//
Pure quadratic f - A is a matrix:
IPOPT(A, /*constraints args*/, Xi, /*bounds and named params*/);
//
Pure linear f - b i a real[int] :
IPOPT(b, /*constraints args*/, Xi, /*bounds and named params*/);
//
linear constraints - Ac is a matrix
IPOPT(/*fitness func args*/, Ac, Xi, /*bounds and named params*/);
218
Returned Value : The IPOPT function returns an error code of type int. A zero value is obtained
when the algorithm succeeds and positive values reflects the fact that IPOPT encounters minor
troubles. Negative values reveals more problematic cases. The associated IPOPT return tags are
listed in the table below. The IPOPT pdf documentation provides more accurate description of
these return status :
0
1
2
3
4
5
6
-10
-11
-12
-13
Success
Solve Succeeded
Solved To Acceptable Level
Infeasible Problem Detected
Search Direction Becomes Too Small
Diverging Iterates
User Requested Stop
Feasible Point Found
Problem definition issues
Not Enough Degrees Of Freedom
Invalid Problem Definition
Invalid Option
Invalid Number Detected
-1
-2
-3
-4
-100
-101
-102
-199
Failures
Maximum Iterations Exceeded
Restoration Failed
Error In Step Computation
Maximum CpuTime Exceeded
Critical errors
Unrecoverable Exception
NonIpopt Exception Thrown
Insufficient Memory
Internal Error
Named Parameters : The available named parameters in this interface are those we thought to be
the most subject to variations from one optimization to another, plus a few ones that are interface
specific. Though, as one could see at http://www.coin-or.org/Ipopt/documentation/
node59.html, there are many parameters that can be changed within IPOPT, affecting the
algorithm behaviour. These parameters can still be controlled by placing an option file in the
execution directory. Note that IPOPTs pdf documentation may provides more informations than
the previously mentioned online version for certain parameters. The in-script available parameters
are :
lb, ub : real[int] for lower and upper simple bounds upon the search variables, must be of size
n (search space dimension). If two components of same index in these arrays are equal then
the corresponding search variable is fixed. By default IPOPT will remove any fixed variable
from the optimization process and always use the fixed value when calling functions. It can
be changed using the fixedvar parameter.
clb, cub : real[int] of size m (number of constraints) for lower and upper constraints bounds.
Equality between two components of same index i in clb and cub reflect an equality constraint.
structjacc : To pass the greatest possible structure (indexes of non null coefficients) of the
constraints jacobian under the form [I,J] where I and J are two integer arrays. If not
defined, the structure of the constraints jacobian, evaluated in Xi, is used (no issue if the
jacobian is constant or always defined with the same varf, hazardous if it is with triplet
array or if a full matrix is involved).
structhess : Same as above but for the hessian function (unused if f is P2 or less and constraints
are affine). Here again, keep in mind that it is the hessian of the lagrangian function (which
is equal to the hessian of f only if constraints are affine). If no structure is given with
this parameter, the lagrangian hessian is evaluated on the starting point, with = 1 and
= (1, 1, . . . , 1) (it is safe if all the constraints and fitness function hessians are constant or
build with varf, and here again less reliable if built with triplet array or full matrix).
8.3. IPOPT
219
checkindex : A bool that triggers an index dichotomic search when matrices are copied from
FreeFem functions to IPOPT arrays. It is used to avoid wrong index matching when some
null coefficients are removed from the matrices by FreeFem. It will not solve the problems
arising when a too small structure has been given at the initialization of the algorithm.
Enabled by default (except in cases where all matrices are obviously constant).
warmstart : If set to true, the constraints dual variables , and simple bounds dual variables
are initialized with the values of the arrays passed to lm, lz and uz named parameters (see
below).
lm : real[int] of size m, which is used to get the final values of the constraints dual variables
and/or initialize them in case of a warm start (the passed array is also updated to the last
dual variables values at the end of the algorithm).
lz, uz : real[int] of size n to get the final values and/or initialize (in case of warm start) the
dual variables associated to simple bounds.
tol : real, convergence tolerance for the algorithm, the default value is 108 .
maxiter : int, maximum number of iterations with 3000 as default value.
maxcputime : real value, maximum runtime duration. Default is 106 (almost 11 days and a
half).
bfgs : bool enabling or not the (low-storage) BFGS approximation of the lagrangian hessian. It
is set to false by default, unless there is no way to compute the hessian with the functions
that have been passed to IPOPT.
derivativetest : Used to perform a comparison of the derivatives given to IPOPT with finite
differences computation. The possible string values are : "none" (default), "first-order",
"second-order" and "only-second-order". The associated derivative error tolerance
can be changed via the option file. One should not care about any error given by it before
having tried, and failed, to perform a first optimization.
dth : Perturbation parameter for the derivative test computations with finite differences. Set by
default to 108 .
dttol : Tolerance value for the derivative test error detection (default value unknown yet, maybe
105 ).
optfile : string parameter to specify the IPOPT option file name. IPOPT will look for a
ipopt.opt file by default. Options set in the file will overwrite those defined in the FreeFem
script.
printlevel : An int to control IPOPT output print level, set to 5 by default, the possible
values are from 0 to 12. A description of the output informations is available in the pdf
documentation of IPOPT.
fixedvar : string for the definition of simple bounds equality constraints treatment : use
"make parameter" (default value) to simply remove them from the optimization process
(the functions will always be evaluated with the fixed value for those variables), "make constraint"
to treat them as any other constraint or "relax bounds" to relax fixing bound constraints.
mustrategy : a string to choose the update strategy for the barrier parameter . The two possible tags are "monotone", to use the monotone (Fiacco-McCormick) strategy, or "adaptive"
(default setting).
220
muinit : real positive value for the barrier parameter initialization. It is only relevant when
mustrategy has been set to monotone.
pivtol : real value to set the pivot tolerance for the linear solver. A smaller number pivots for
sparsity, a larger number pivots for stability. The value has to be in the [0, 1] interval and is
set to 106 by default.
brf : Bounds relax factor : before starting the optimization, the bounds given by the user are
relaxed. This option sets the factor for this relaxation. If it is set to zero, then the bounds
relaxation is disabled. This real has to be positive and its default value is 108 .
objvalue : An identifier to a real type variable to get the last value of the objective function
(best value in case of succes).
mumin : Minimal value for the barrier parameter , a real with 1011 as default value.
linesearch : A boolean which disables the line search when set to false. The line search is
activated by default. When disabled, the method becomes a standard Newton algorithm
other the primal-dual system. The global convergence is then no longer assured, meaning
that many initializers could lead to diverging iterates. But on the other hand, it can be useful
when trying to catch a precise local minimum without having some out of control process
making the iterate caught by some other near optimum.
8.4
load "ff-Ipopt";
int nn=20;
mesh Th=square(nn,nn);
fespace Vh(Th,P1);
//
//
//
func f = 1.;
//
rhs function
real r=0.03,s=0.1;
//
some parameters for g
func g = r - r/2*exp(-0.5*(square(x-0.5)+square(y-0.5))/ square(s));
//
g is constant minus a gaussian
macro Grad(u) [dx(u),dy(u)]
//
the gradient operator
varf vP(u,v) = int2d(Th)(Grad(u)*Grad(v)) - int2d(Th)(f*v);
Here we build the matrix and second member associated to the functional to minimize once and for
all. The [A,b] syntax for the fitness function is then used to pass it to IPOPT.
matrix A = vP(Vh,Vh,solver=CG);
real[int] b = vP(0,Vh);
We use simple bounds to impose the boundary condition u = 0 on , as well as the u g condition.
Vh lb=-1.e19;
Vh ub=g;
varf vGamma(u,v) = on(1,2,3,4,u=1);
//
//
//
221
Vh u=0;
IPOPT([A,b],u[],lb=lb[],ub=ub[]);
plot(u,wait=1);
//
//
starting point
solve the problem
The problem consists in finding (numerically) two functions (u1 , u2 ) = argmin J(v1 , v2 ).
(v1 ,v2 )V
load "ff-IpOpt";
mesh Th=square(10,10);
fespace Vh(Th,[P1,P1] );
fespace Wh(Th,[P1] );
int iter=0;
func
func
func
func
f1
f2
g1
g2
= 10;
= -15;
= -0.1;
= 0.1;
//
//
while(++iter)
//
mesh adaptation loop
{
macro Grad(u) [dx(u),dy(u)]
//
gradient macro
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);
real[int] b = vP(0,Vh);
//
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)
//
Boundary conditions
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 ;
222
Vh [u1,u2]=[0,0];
//
starting point
IPOPT([b,A],CC,u1[],lb=lb1[],ub=ub1[],clb=cl[]);
plot(u1,u2,wait=1,nbiso=60,dim=3);
if(iter > 1) break;
Th= adaptmesh(Th,[u1,u2],err=0.004,nbvx=100000);
}
8.5
8.5.1
This example aimed at numerically solving some constrained minimal surface problems with the
IPOPT algorithm. We restrain to C k (k 1), closed, spherically parametrizable surfaces, i.e.
surfaces S such that :
(, )
, (, ) [0, 2] [0, ]
0
C k ([0, 2] [0, ])|S = X =
0
Where the components are expressed in the spherical coordinate system. Lets call the [0, 2]
[0, ] angular parameters set. In order to exclude self crossing and opened shapes, the following
assumptions upon are made :
0 and , (0, ) = (2, )
For a given function the first fundamental form (the metric) of the defined surface has the following
matrix representation :
2 2
sin () + ( )2
G=
(8.8)
2 + ( )2
This metric is used to express the area of the surface. Let g = det(G), then we have :
Z
Z
Z q
g=
2 ( )2 + 4 sin2 () + 2 ( )2 sin2 ()dd
A() =
k X Xk =
(8.9)
223
The volume of the space enclosed within the shape is easier to express :
Z Z
V() =
8.5.2
(,)
r2 sin()drdd =
1
3
3 sin()dd
(8.10)
Derivatives
In order to use a newton based interior point optimization algorithm, one must be able to evaluate
the derivatives of A and V with respect to rho. Concerning the area we have the following result :
Z
1 d
g ()(v)
1
v C () , hdA(), vi =
dd
g
2
Where g is the application mapping the (, ) 7 g(, ) scalar field to . This leads to the following
expression, easy to transpose in a freefem script using :
Z
v C 1 () , hdA(), vi =
23 sin2 () + ( )2 + ( )2 sin2 () v
Z
(8.11)
2 v + 2 sin2 () v
+
With a similar approach, one can derive an expression for second order derivatives. Though comporting no specific difficulties, the detailed calculus are tedious, the result is that these derivatives
can be write using a 3 3 matrix B whose coefficients are expressed in term of and its derivatives
with respect to and , such that :
Z
v
w w w B v dd
(w, v) C 1 () , d2 A()(w, v) =
(8.12)
v
Deriving the volume function derivatives is again an easier task. We immediately get the following
expressions :
Z
v , hdV(), vi =
2 sin()v dd
Z
(8.13)
w, v , d2 V()(w, v) =
2 sin()wv dd
8.5.3
The whole code is available in IpoptMinSurfVol.edp. We propose to solve the following problem
:
Example 8.4 Given a positive function object piecewise continuous, and a scalar Vmax > V(object ),
find 0 such that :
0 = argmin A() , s.t. 0 object and V(0 ) Vmax
C 1 ()
If object is the spherical parametrization of the surface of a 3-dimensional object (domain) O, it can
be interpreted as finding the surface with minimal area enclosing the object with a given maximal
volume. If Vmax is close to V(object ), so should be 0 and object . With higher values of Vmax ,
should be closer to the unconstrained minimal surface surrounding O which is obtained as soon as
224
load "msh3";
load "medit";
load "ff-Ipopt";
int np=40;
mesh Th = square(2*np,np,[2*pi*x,pi*y]);
//
fespace Vh(Th,P1,periodic=[[2,y],[4,y]]);
Vh startshape=5;
//
initial shape
We create some finite element functions whose underlying arrays will be used to store the values of
dual variables associated to all the constraints in order to reinitialize the algorithm with it in the
case where we use mesh adaptation. Doing so, the algorithm will almost restart at the accuracy
level it reached before mesh adaptation, thus saving many iterations.
Vh uz=1.,lz=1.;
real[int] lm=[1];
//
//
Simple bounds dual variable
dual variable for volume constraint
Then, follows the mesh adaptation loop, and a rendering function, Plot3D, using 3D mesh to
display the shape it is passed with medit (the movemesh23 procedure often crashes when called
with ragged shapes).
int nadapt=1;
for(int kkk=0;kkk<nadapt;++kkk)
{
int iter=0;
func sin2 = square(sin(y));
//
//
//
iterations count
a function that will be often used
Here are the functions related to the area computation and its shape derivative, according to
equations 8.9 and 8.11 :
func real Area(real[int] &X)
{
Vh rho;
rho[] = X;
Vh rho2 = square(rho);
Vh rho4 = square(rho2);
real res = int2d(Th)( sqrt( rho4*sin2
+ rho2*square(dx(rho))
+ rho2*sin2*square(dy(rho)) )
);
++iter;
225
The function returning the hessian of the area for a given shape is a bit blurry, thus we wont show
here all of equation 8.12 coefficients definition, they can be found in the edp file.
matrix hessianA;
//
226
return res;
}
func real[int] GradVolume(real[int] &X)
{
Vh rho;
rho[]=X;
varf dVolume(u,v) = int2d(Th)(rho*rho*sin(y)*v);
real[int] grad = dVolume(0,Vh);
return grad;
}
matrix hessianV;
func matrix HessianVolume(real[int] &X)
{
Vh rho;
rho[]=X;
varf d2Volume(w,v) = int2d(Th)(2*rho*sin(y)*v*w);
hessianV = d2Volume(Vh,Vh);
return hessianV;
}
//
buffer
If we want to use the volume as a constraint function we must wrap it and its derivatives in some
FreeFem++ functions returning the appropriate types. It is not done in the above functions in
case where one wants to use it as fitness function. The lagrangian hessian also have to be wrapped
since the Volume is not linear with respect to , it has some non-null second order derivatives.
func real[int] ipVolume(real[int] &X) {real[int] vol = [Volume(X)]; return vol;}
matrix mdV;
//
buffer
func matrix ipGradVolume(real[int] &X)
{
//
transforms a vector into a sparse matrix
real[int,int] dvol(1,Vh.ndof);
dvol(0,:)=GradVolume(X);
mdV=dvol;
return mdV;
}
matrix HLagrangian;
//
func matrix ipHessianLag(real[int] &X,real objfact,real[int] &lambda)
{
HLagrangian = objfact*HessianArea(X) + lambda[0]*HessianVolume(X);
return HLagrangian;
}
buffer
The ipGradVolume function could bring some troubles during the optimization process because
the gradient vector is transformed in a sparse matrix, so any null coefficient will be discarded. We
are here obliged to give IPOPT the structure by hand and use the checkindex named-parameter
to avoid bad indexing during copies. This gradient is actually dense, there is no reason for some
components to be constantly zero :
//
int[int] gvi(Vh.ndof),gvj=0:Vh.ndof-1;
gvi=0;
227
These two arrays will be passed to IPOPT with structjacc=[gvi,gvj]. The last remaining
things are the bounds definition. The simple lower bounds must be equal to the components of the
P1 projection of object . And we choose [0, 1] to set Vmax to (1 )V(object ) + 43 kobject k3
:
real e=0.1,r0=0.25,rr=2-r0;
real E=1./(e*e),RR=1./(rr*rr);
//
An indented disc
func disc1 = sqrt(1./(RR+(E-RR)*cos(y)*cos(y)))*(1+0.1*cos(9*x));
//
Almost a standard disc
func disc2 = sqrt(1./(RR+(E-RR)*cos(x)*cos(x)*sin2)) ;
Vh lb = max(disc1, disc2);
//
glue the object parts
real Vobj = Volume(lb[]);
//
object volume
real Vnvc = 4./3.*pi*pow(lb[].linfty,3);
//
V for no volume constraint
real alpha=0.1;
Plot3D(lb[],"object_inside",0);
real[int] clb=0.,cub=[(1-alpha)*Vobj + alpha*Vnvc];
Calling IPOPT :
IPOPT(Area,GradArea,ipHessianLag,
ipVolume,ipGradVolume,rc[],
//
functions and starting point
ub=ub[],lb=lb[],clb=clb,cub=cub,
//
simple bounds and volume bounds
checkindex=1,structjacc=[gvi,gvj],
//
for safe matrices copies
maxiter=kkk<nadapt-1 ? 40:150,
//
accurate optim only for last mesh
adaptation iteration
warmstart=kkk,lm=lm,uz=uz[],lz=lz[],
//
warmstart handling
tol=0.00001);
Plot3D(rc[],"Shape_at_"+kkk,0);
//
At last, before closing the mesh adaptation loop, we have to perform the said adaptation. The
mesh is adaptated with respect to the X = (, 0, 0) (in spherical coordinates) vector field, not
directly with respect to , otherwise the true curvature of the 3D-shape would not be well taken
into account.
if(kkk<nadapt-1)
{
Th = adaptmesh(Th,
rc*cos(x)*sin(y),
rc*sin(x)*sin(y),
rc*cos(y),
nbvx=50000,
periodic=[[2,y],[4,y]]);
plot(Th);
startshape = rc;
uz=uz;
lz=lz;
}
}
//
//
//
//
//
X
Y
Z
//
//
end if
en of mesh adaptation loop
Here are some pictures of the resulting surfaces obtained for decreasing values of (and a slightly
more complicated object than two orthogonal discs). We get back the enclosed object when = 0
:
228
8.6
The ff-NLopt package provides a FreeFem interface to the free/open-source library for nonlinear
optimization, thus easing the use of several different free optimization (constrained or not) routines
available online along with the PDE solver. All the algorithms are well documented in [47], thus
no exhaustive informations concerning their mathematical specificities will be found here and we
will focus on the way they are called in a FreeFem script. One needing detailed informations about
these algorithms should visit the said cite where a description of each of them is given, as well
as many bibliographical links. Most of the gradient based algorithm of nlOpt uses a full matrix
approximation of the hessian, so if youre planing to solve a large scale problem, our advise would
be to use the IPOPT optimizer which definitely surpass them. Finally, an examples of use can be
found in the examples++-load/ directory under the name VarIneq2.edp
All the nlOpt features are called that way :
load "ff-NLopt"
...
//
define J, u, and maybe grad(J), some constraints etc...
real min = nloptXXXXXX(J,u,
//
unavoidable part
grad = <name of grad(J)> ,
//
if needed
lb =
//
lower bounds array
ub=
//
upper bounds array
...
//
some optional arguments :
//
constraints functions names,
229
//
//
algo.
stopping criterions,
specific parameters,
//
etc...
);
XXXXXX refers to the algorithm tag (not necessarily 6 characters long). u is the starting position
(a real[int] type array) which will be overwritten by the algorithm, the value at the end being
the found argmin. And as usual, J is a function taking a real[int] type array as argument and
returning a real. grad, lb and ub are half-optionnal arguments, in the sense that they are
obligatory for some routines but not all.
The possible optional named parameters are the following, note that they are not used by all algorithm s (some does not supports constraints, or a type of constraints, some are gradient-based
and other are derivative free, etc...). One can refer to the table after the parameters description to
check which are the named parameters supported by a specific algorithm. Using an unsupported
parameter will not stop the compiler work and seldom breaks runtime, it will just be ignored. That
said, when it is obvious you are missusing a routine, you will get a warning message at runtime (for
exemple if you pass a gradient to a derivative free algorithm, or set the population of a non-genetic
one, etc...). In the following description, n stands for the dimension of the search space.
Half-optional parameters :
grad= The name of the function which computes the gradient of the cost function (prototype
should be real[int] real[int], both argument and result should have the size
n). This is needed as soon s a gradient-based method is involved, ignored if defined in
a derivative free context.
lb/ub = Lower and upper bounds arrays (real[int] type) of size n. Used to define the
bounds within which the search variable is allowed to move. Needed for some algorithms,
optional or unsupported for others.
subOpt : Only enabled for the Augmented Lagrangian and MLSL method who need a suboptimizer in order to work. Just pass the tag of the desired local algorithm with a
string.
Constraints related parameters (optional - unused if not specified):
IConst/EConst : Allows to pass the name of a function implementing some inequality
(resp. equality) constraints on the search space. The function type must be real[int]
real[int] where the size of the returned array is equal to the number of constraints
(of the same type - it means that all the constraints are computed in one vectorial
function). In order to mix inequality and equality constraints in a same minimization
attempt, two vectorial functions have to be defined and passed. See example ?? for
more details about how these constraints have to be implemented.
gradIConst/gradEConst : Use to provide the inequality (resp. equality) constraints gradient. These are real[int] real[int,int] type functions. Assuming we have
defined a constraint function (either inequality or equality) with p constraints, the size
of the matrix returned by its associated gradient must be p n (the i-th line of the
matrix is the gradient of the i-th constraint). It is needed in a gradient-based context
as soon as an inequality or equality constraint function is passed to the optimizer and
ignored in all other cases.
230
Stopping criteria :
stopFuncValue : Makes the algorithm end when the objective function reaches this real
value.
stopRelXTol : Stops the algorithm when the relative moves in each direction of the search
space is smaller than this real value.
stopAbsXTol : Stops the algorithm when the moves in each direction of the search space
is smaller than the corresponding value in this real[int] array.
stopRelFTol : Stops the algorithm when the relative variation of the objective function is
smaller than this real value.
stopAbsFTol : Stops the algorithm when the variation of the objective function is smaller
than this real value.
stopMaxFEval : Stops the algorithm when the number of fitness evaluations reaches this
integer value.
stopTime : Stops the algorithm when the otpimization time in second exceeds this real
value. This is not a strict maximum: the time may exceed it slightly, depending upon
the algorithm and on how slow your function evaluation is.
Note that when an AUGLAG or MLSL method is used, the meta-algorithm and the subalgorithm may have different termination criteria. Thus, for algorithms of this kind, the
following named parameters has been defined (just adding the SO prefix - for Sub-Optimizer)
to set the ending condition of the sub-algorithm (the meta one uses the ones above) :
SOStopFuncValue, SOStopRelXTol, and so on... If these ones are not used, the suboptimizer will use those of the master routine.
Other named parameters :
popSize : integer used to change the size of the sample for stochastic search methods.
Default value is a peculiar heuristic to the chosen algorithm.
SOPopSize : Same as above, but when the stochastic search is passed to a meta-algorithm.
nGradStored : The number (interger type) of gradients to remember from previous
optimization steps: increasing this increases the memory requirements but may speed
convergence. It is set to a heuristic value by default. If used with AUGLAG or MLSL,
it will only affect the given subsidiary algorithm.
The following table sums up the main characteristics of each algorithm, providing the more important information about which features are supported by which algorithm and what are the
unavoidable arguments they need. More details can be found in [47].
Id Tag
DIRECT
DIRECTL
231
Full Name
Dividing rectangles
Locally biased dividing
rectangles
DIRECTLRand
DIRECTNoScal
DIRECTLNoScal
DIRECTLRandNoScal
OrigDIRECT
OrigDIRECTL
StoGO
StoGORand
LBFGS
PRAXIS
Var2
TNewtonPrecond
TNewtonRestartPrecond
Truncated Newton
Steepest descent restarting
truncated Newton
BFGS preconditionned
truncated Newton
BFGS preconditionned truncated
Newton with steepest descent restarting
MMA
COBYLA
Constrained optimization by
linear approximations
NEWUOA
NEWUOA
NEWUOABound
NelderMead
Nelder-Mead simplex
Subplex
BOBYQA
ISRES
SLSQP
Sequential least-square
quadratic programming
MLSL
Multi-level single-linkage
MLSLLDS
AUGLAG
Constraints augmented
lagrangian
BOBYQA
AUGLAGEQ
CRS2
Sbplx
Gradient
Constraints
SubStochastic
-Based
Equality Inequality Opt
Principal AXIS
Var1
TNewtonRestart
Low-storage BFGS
TNewton
Bounds
Legend :
232
8.7
The only quick way to use the previously presented algorithms on a parallel architecture lies in
parallelizing the used cost function (which is in most real life case, the expensive part of the
algorithm). Somehow, we provide a parallel version of the CMA-ES algorithm. The parallelization
principle is the trivial one of evolving/genetic algorithms : at each iteration the cost function has to
be evaluated N times without any dependence at all, these N calculus are then equally distributed
to each processes. Calling the MPI version of CMA-ES is nearly the same as calling its sequential
version (a complete example of use can be found in the cmaes-mpi-VarIneq.edp file).:
load "mpi-cmaes"
...
//
define J, u and all here
real min = cmaesMPI(J,u,stopTolFun=1e-6,stopMaxIter=3000);
cout << "minimal value is " << min << " for u = " << u << endl;
If the population size is not changed using the popsize parameter, it will use the heuristic value
slightly changed to be equal to the closest greater multiple of the size of the communicator used
by the optimizer. 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++ .
Chapter 9
Mathematical Models
Summary This chapter goes deeper into a number of problems that FreeFem++ can solve. It is
a complement to chapter 3 which was only an introduction. Users are invited to contribute to make
this data base of problems grow.
9.1
9.1.1
Static Problems
Soap Film
Our starting point here will be the mathematical model to find the shape of soap film which is
glued to the ring on the xyplane
C = {(x, y); x = cos t, y = sin t, 0 t 2}.
We assume the shape of the film is described by the graph (x, y, u(x, y)) of the vertical displacement
u(x, y) (x2 + y 2 < 1) under a vertical pressure p in terms of force per unit area and an initial tension
in terms of force per unit length.
Consider the small plane ABCD, A:(x, y, u(x, y)), B:(x, y, u(x + x, y)), C:(x, y, u(x + x, y + y))
and D:(x, y, u(x, y + y)). Denote by n(x, y) = (nx (x, y), ny (x, y), nz (x, y)) the normal vector of
the surface z = u(x, y). We see that the vertical force due to the tension acting along the edge
AD is nx (x, y)y and the the vertical force acting along the edge AD is
nx
nx (x + x, y)y ' nx (x, y) +
x (x, y)y.
x
Similarly, for the edges AB and DC we have
u ( x ,y + d y )
u ( x + d x ,y + d y )
T
T
u ( x ,y )
ny (x, y)x,
( u / x )d x
T
u ( x + d x ,y )
233
234
The force in the vertical direction on the surface ABCD due to the tension is given by
(nx /x) xy + T (ny /y) yx.
Assuming small displacements, we have
p
x = (u/x)/ 1 + (u/x)2 + (u/y)2 ' u/x,
p
y = (u/y)/ 1 + (u/x)2 + (u/y)2 ' u/y.
Letting x dx, y dy, we have the equilibrium of the vertical displacement of soap film on
ABCD by p
dxdy 2 u/x2 + dxdy 2 u/y 2 + pdxdy = 0.
Using the Laplace operator = 2 /x2 + 2 /y 2 , we can find the virtual displacement write the
following
u = f in
(9.1)
where f = p/, = {(x, y); x2 + y 2 < 1}. Poissons equation (2.1) appears also in electrostatics
taking the form of f = / where is the charge density, the dielectric constant and u is named
as electrostatic potential. The soap film is glued to the ring = C, then we have the boundary
condition
u = 0 on
(9.2)
If the force is gravity, for simplify, we assume that f = 1.
Example 9.1 (a tutorial.edp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
//
//
//
bilinear form
//
linear form
boundary condition
ue: exact solution
plot (u,ps="aTutorial.eps",value=true,wait=true);
plot(err,value=true,wait=true);
cout << "error L2=" << sqrt(int2d(disk)( err2) )<< endl;
cout << "error H10=" << sqrt( int2d(disk)((dx(u)-x/2)2)
+ int2d(disk)((dy(u)-y/2)2))<< endl;
disk = adaptmesh(disk,u,err=0.01);
plot(disk,wait=1);
laplace;
plot (u,value=true,wait=true);
err = u - ue;
//
235
: plot(err,value=true,wait=true);
: cout << "error L2=" << sqrt(int2d(disk)( err2) )<< endl;
: cout << "error H10=" << sqrt(int2d(disk)((dx(u)-x/2)2)
:
+ int2d(disk)((dy(u)-y/2)2))<< endl;
IsoValue
-0.243997
-0.231485
-0.218972
-0.206459
-0.193947
-0.181434
-0.168921
-0.156409
-0.143896
-0.131383
-0.11887
-0.106358
-0.0938451
-0.0813324
-0.0688198
-0.0563071
-0.0437944
-0.0312817
-0.018769
-0.00625634
1/2
|uh ue | dxdy
and from 20th line to 21th line, the H 1 -error seminorm estimation
Z
|uh ue |1, =
1/2
|uh ue | dxdy
are done on the initial mesh. The results are kuh ue k0, = 0.000384045, |uh ue |1, = 0.0375506.
After the adaptation, we hava kuh ue k0, = 0.000109043, |uh ue |1, = 0.0188411. So the
numerical solution is improved by adaptation of mesh.
9.1.2
Electrostatics
We assume that there is no current and a time independent charge distribution. Then the electric
field E satisfies
divE = /,
curlE = 0
(9.3)
where is the charge density and is called the permittivity of free space. From the second equation
in (9.3), we can introduce the electrostatic potential such that E = . Then we have Poissons
equation = f , f = /. We now obtain the equipotential line which is the level curve of ,
when there are no charges except conductors {Ci }1, ,K . Let us assume K conductors C1 , , CK
within an enclosure C0 . Each one is held at an electrostatic potential i . We assume that the
enclosure C0 is held at potential 0. In order to know (x) at any point x of the domain , we
must solve
= 0 in ,
(9.4)
236
P
where is the interior of C0 minus the conductors Ci , and is the boundary of , that is N
i=0 Ci .
Here g is any function of x equal to i on Ci and to 0 on C0 . The boundary equation is a reduced
form for:
= i on Ci , i = 1...N, = 0 on C0 .
(9.5)
//
figure 9.3
//
P1 FE-space
unknown and test function.
definition of the problem
//
bilinear
boundary condition on C0
//
+1 volt on C1
//
-1 volt on C2
Electro;
//
solve the problem, see figure 9.4 for the solution
plot(uh,ps="electro.eps",wait=true);
//
figure 9.4
9.1.3
237
Aerodynamics
Let us consider a wing profile S in a uniform flow. Infinity will be represented by a large circle .
As previously, we must solve
= 0
in ,
|S = c,
| = u1x u2x
(9.6)
where is the area occupied by the fluid, u is the air speed at infinity, c is a constant to
be determined so that n is continuous at the trailing edge P of S (so-called Kutta-Joukowski
condition). Lift is proportional to c. To find c we use a superposition method. As all equations in
(9.6) are linear, the solution c is a linear function of c
c = 0 + c1 ,
(9.7)
where 0 is a solution of (9.6) with c = 0 and 1 is a solution with c = 1 and zero speed at infinity.
With these two fields computed, we shall determine c by requiring the continuity of /n at the
trailing edge. An equation for the upper surface of a NACA0012 (this is a classical wing profile in
aerodynamics; the rear of the wing is called the trailing edge) is:
|1 = y 0.1x,
in ,
|2 = c,
(9.9)
where 2 is the wing profile and 1 is an approximation of infinity. One finds c by solving:
0 = 0 in ,
0 |1 = y 0.1x,
1 = 0 in ,
1 |1 = 0,
0 |2 = 0,
(9.10)
1 |2 = 1.
(9.11)
The solution = 0 + c1 allows us to find c by writing that n has no jump at the trailing edge
P = (1, 0). We have n ((P + ) (P ))/ where P + is the point just above P in the direction
normal to the profile at a distance . Thus the jump of n is (0 |P + + c(1 |P + 1)) + (0 |P +
c(1 |P 1)) divided by because the normal changes sign between the lower and upper surfaces.
Thus
0 |P + + 0 |P
c=
,
(9.12)
(1 |P + + 1 |P 2)
which can be programmed as:
c=
(9.13)
Example 9.3
//
//
Computation of the potential flow around a NACA0012 airfoil.
The method of decomposition is used to apply the Joukowski condition
//
The solution is seeked in the form psi0 + beta psi1 and beta is
//
adjusted so that the pressure is continuous at the trailing edge
y=5*sin(t); };
border upper(t=0,1) { x = t;
y = 0.17735*sqrt(t)-0.075597*t
- 0.212836*(t2)+0.17363*(t3)-0.06254*(t4); }
border lower(t=1,0) { x = t;
//
approximates infinity
238
y= -(0.17735*sqrt(t)-0.075597*t
-0.212836*(t2)+0.17363*(t3)-0.06254*(t4)); }
border c(t=0,2*pi) { x=0.8*cos(t)+0.5; y=0.8*sin(t); }
wait = true;
mesh Zoom = buildmesh(c(30)+upper(35)+lower(35));
mesh Th = buildmesh(a(30)+upper(35)+lower(35));
fespace Vh(Th,P2);
Vh psi0,psi1,vh;
//
fespace ZVh(Zoom,P2);
//
P1 FE space
unknown and test function.
solve Joukowski0(psi0,vh) =
//
int2d(Th)( dx(psi0)*dx(vh) + dy(psi0)*dy(vh) )
+ on(a,psi0=y-0.1*x)
//
+ on(upper,lower,psi0=0);
plot(psi0);
solve Joukowski1(psi1,vh) =
//
int2d(Th)( dx(psi1)*dx(vh) + dy(psi1)*dy(vh) )
+ on(a,psi1=0)
//
+ on(upper,lower,psi1=1);
plot(psi1);
//
continuity of pressure at trailing edge
real beta = psi0(0.99,0.01)+psi0(0.99,-0.01);
beta = -beta / (psi1(0.99,0.01)+ psi1(0.99,-0.01)-2);
Vh psi = beta*psi1+psi0;
plot(psi);
ZVh Zpsi=psi;
plot(Zpsi,bw=true);
ZVh cp = -dx(psi)2 - dy(psi)2;
plot(cp);
ZVh Zcp=cp;
plot(Zcp,nbiso=40);
9.1.4
239
Error estimation
There are famous estimation between the numerical result uh and the exact solution u of the problem
2.1 and 2.2: If triangulations {Th }h0 is regular (see Section 5.4), then we have the estimates
|u uh |0, C1 h
ku uh k0, C2 h
(9.14)
2
(9.15)
Z
Z
f uh dxdy
f (u 2uh ) dxdy +
=
The constants C1 , C2 are depend on Th and f , so we will find them by FreeFem++ . In general, we
cannot get the solution u as a elementary functions (see Section 4.8) even if spetical functions are
added. Instead of the exact solution, here we use the approximate solution u0 in Vh (Th , P2 ), h 0.
Example 9.4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
//
//
sin(pi*x)*cos(pi*y);
//
bilinear form
//
linear form
boundary condition
plot(u0);
real[int] errL2(10), errH1(10);
for (int i=1; i<=10; i++) {
mesh Th = square(5+i*3,5+i*3);
fespace Vh(Th,P1);
fespace Ph(Th,P0);
Ph h = hTriangle;
//
get the size of all triangles
Vh u,v;
solve Poisson(u,v) =
int2d(Th)( dx(u)*dx(v) + dy(u)*dy(v) )
//
bilinear form
- int2d(Th)( f*v )
//
linear form
+ on(1,2,3,4,u=0) ;
//
boundary condition
V0h uu = u;
errL2[i-1] = sqrt( int2d(Th0)((uu - u0)2) )/h[].max2;
errH1[i-1] = sqrt( int2d(Th0)( f*(u0-2*uu+uu) ) )/h[].max;
}
cout << "C1 = " << errL2.max <<"("<<errL2.min<<")"<< endl;
cout << "C2 = " << errH1.max <<"("<<errH1.min<<")"<< endl;
240
9.1.5
//
periodic condition
the curve abscissa
the curve abscissa
and test function.
hand side function
;
laplace;
//
solve the problem plot(uh); // to see the result
plot(uh,ps="period.eps",value=true);
IsoValue
-0.441699
-0.391928
-0.342157
-0.292387
-0.242616
-0.192845
-0.143075
-0.0933038
-0.0435331
0.00623761
0.0560083
0.105779
0.15555
0.20532
0.255091
0.304862
0.354633
0.404403
0.454174
0.503945
The periodic condition does not necessarily require parallel boundaries. Example 9.6 give such
example.
Example 9.6 (periodic4.edp)
real r=0.25;
//
241
Figure 9.8: The isovalue of solution u for u = ((y + x)2 + 1)((y x)2 + 1) k, in and
n u = 0 on hole,and with two periodic boundary condition on external border
An other example with no equal border, just to see if the code works.
242
real Ax=0.9,Ay=1;
real Cx=2.5,Cy=2.5;
real gx = (Ax+Bx+Cx+Dx)/4.;
real Bx=2,By=1;
real Dx=1,Dy=2;
real gy = (Ay+By+Cy+Dy)/4.;
LINEBORDER(A,B,1)
LINEBORDER(B,C,2)
LINEBORDER(C,D,3)
LINEBORDER(D,A,4)
int n=10;
real
real
real
real
func
func
func
func
l1=dist(Ax,Ay,Bx,By);
l2=dist(Bx,By,Cx,Cy);
l3=dist(Cx,Cy,Dx,Dy);
l4=dist(Dx,Dy,Ax,Ay);
s1=dist(Ax,Ay,x,y)/l1;
s2=dist(Bx,By,x,y)/l2;
s3=dist(Cx,Cy,x,y)/l3;
s4=dist(Dx,Dy,x,y)/l4;
//
//
//
//
absisse
absisse
absisse
absisse
on
on
on
on
mesh Th=buildmesh(AB(n)+BC(n)+CD(n)+DA(n),fixedborder=1);
AB
BC
CD
DA
=
=
=
=
||AX||/||AB||
||BX||/||BC||
||CX||/||CD||
||DX||/||DA||
//
verbosity=6;
//
to see the abscisse value pour the periodic condition.
fespace Vh(Th,P1,periodic=[[1,s1],[3,s3],[2,s2],[4,s4]]);
verbosity=1;
Vh u,v;
real cc=0;
cc= int2d(Th)((x-gx)*(y-gy)-cc)/Th.area;
cout << " compatibility =" << int2d(Th)((x-gx)*(y-gy)-cc) <<endl;
solve Poission(u,v)=int2d(Th)(Grad(u)*Grad(v)+ 1e-10*u*v)
-int2d(Th)(10*v*((x-gx)*(y-gy)-cc));
plot(u,wait=1,value=1);
243
mesh3 Th;
try {
//
a way to build one time the mesh an read if the file exist.
Th=readmesh3("Th-hex-sph.mesh");
}
catch(...) { buildTh=1;}
if( buildTh ){
...
put the code example page
//
5.11.1149
without the first line
}
fespace Ph(Th,P0);
verbosity=50;
fespace Vh(Th,P1,periodic=[[3,x,z],[4,x,z],[1,y,z],[2,y,z],[5,x,y],[6,x,y]]);//
back and front
verbosity=1;
Ph reg=region;
cout << " centre = " << reg(0,0,0) << endl;
cout << " exterieur = " << reg(0,0,0.7) << endl;
macro Grad(u) [dx(u),dy(u),dz(u)]
//
EOM
Vh uh,vh;
real x0=0.3,y0=0.4,z0=06;
func f= sin(x*2*pi+x0)*sin(y*2*pi+y0)*sin(z*2*pi+z0);
real gn = 1.;
real cf= 1;
problem P(uh,vh)=
int3d(Th,1)( Grad(uh)*Grad(vh)*100)
+ int3d(Th,2)( Grad(uh)*Grad(vh)*2)
+ int3d(Th) (vh*f)
;
P;
plot(uh,wait=1, nbiso=6);
medit("
uh ",Th, uh);
9.1.6
Here we consider the Poisson equation with mixed boundary conditons: For given functions f and
g, find u such that
u = f
in
u = g
on D ,
u/n = 0
on N
(9.16)
where D is a part of the boundary and N = \ D . The solution u has the singularity at
the points {1 , 2 } = D N . When = {(x, y); 1 < x < 1, 0 < y < 1}, N = {(x, y); 1
x < 0, y = 0}, D = \ N , the singularity will appear at 1 = (0, 0), 2 (1, 0), and u has the
expression
u = Ki uS + uR , uR H 2 (near i ), i = 1, 2
1/2
with a constants Ki . Here uS = rj sin(j /2) by the local polar coordinate (rj , j at j such that
(r1 , 1 ) = (r, ). Instead of poler coordinate system (r, ), we use that r = sqrt( x
2+y
2 ) and
= atan2(y,x) in FreeFem++ .
244
Figure 9.10:
Figure 9.9: view of the surface isovalue of view a the cut of the solution uh with ffmedit
periodic solution uh
Example 9.9 Assume that f = 2 30(x2 + y 2 ) and g = ue = 10(x2 + y 2 )1/4 sin [tan1 (y/x)]/2 +
30(x2 y 2 ), where ue S is the exact solution.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
border
border
border
border
border
//
//
bilinear form
//
linear form
boundary condition
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
245
//
//
bilinear form
//
linear form
boundary condition
From 24th line to 28th, adaptation of meshes are done using the base of singular term. In 42th
line, H1e=kue k1, is calculated. In last 2 lines, the relative errors are calculated, that is,
ku0h ue k1, /H1e = 0.120421
kuah ue k1, /H1e = 0.0150581
where u0h is the numerical solution in T0h and uah is u in this program.
9.1.7
Here we consider the Poisson equation with mixed boundary value problems: For given functions
f , gd , gn , find p such that
p = 1
in
p = gd
on D ,
p/n = gn
on N
(9.17)
in
.u = f
in
p = gd
on D ,
u.n = gn .n
on N
(9.18)
p.v
+ vv =
gd v.n
d
q.u
qf
u.n
= gn .n
on N
(9.19)
246
and
V0 = {v V;
v.n = 0
on N }.
To write, the FreeFem++ example, we have just to choose the finites elements spaces. here V
space is discretize with Raviart-Thomas finite element RT0 and P is discretize by constant finite
element P0.
Example 9.10 (LaplaceRT.edp)
mesh Th=square(10,10);
fespace Vh(Th,RT0);
fespace Ph(Th,P0);
func gd = 1.;
func g1n = 1.;
func g2n = 1.;
Vh [u1,u2],[v1,v2];
Ph p,q;
problem laplaceMixte([u1,u2,p],[v1,v2,q],
solver=GMRES,eps=1.0e-10,
tgv=1e30,dimKrylov=150)
=
int2d(Th)( p*q*1e-15
//
this term is here to be sur
//
that all sub matrix are inversible (LU requirement)
+ u1*v1 + u2*v2 + p*(dx(v1)+dy(v2)) + (dx(u1)+dy(u2))*q )
+ int2d(Th) ( q)
- int1d(Th,1,2,3)( gd*(v1*N.x +v2*N.y))
//
on D
+ on(4,u1=g1n,u2=g2n);
//
on N
laplaceMixte;
plot([u1,u2],coef=0.1,wait=1,ps="lapRTuv.eps",value=true);
plot(p,fill=1,wait=1,ps="laRTp.eps",value=true);
9.1.8
We do metric mesh adaption and compute the classical residual error indicator T on the element
T for the Poisson problem.
Example 9.11 (adaptindicatorP2.edp)
1
2
3
4
5
6
:
:
:
:
:
:
border
border
border
border
border
border
ba(t=0,1.0){x=t;
bb(t=0,0.5){x=1;
bc(t=0,0.5){x=1-t;
bd(t=0.5,1){x=0.5;
be(t=0.5,1){x=1-t;
bf(t=0.0,1){x=0;
y=0; label=1;};
y=t; label=2;};
y=0.5;label=3;};
y=t; label=4;};
y=1; label=5;};
y=1-t;label=6;};
//
see Fig,5.15
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
247
X
eEK
uh 2
he || [
] || 2
nk L (e)
where hT is the longests edge of T , ET is the set of T edge not on = , nT is the outside unit
normal to K, he is the length of edge e, [g] is the jump of the function g across edge (left value
minus right value).
Of course, we can use a variational form to compute T2 , with test function constant function in
each triangle.
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
45
46
47
48
49
50
51
:
:
:
:
:
:
:
*************/
varf indicator2(uu,chiK) =
intalledges(Th)(chiK*lenEdge*square(jump(N.x*dx(u)+N.y*dy(u))))
+int2d(Th)(chiK*square(hTriangle*(f+dxx(u)+dyy(u))) );
for (int i=0;i< 4;i++)
{
Probem1;
cout << u[].min << " " << u[].max << endl;
plot(u,wait=1);
cout << " indicator2 " << endl;
rho[] = indicator2(0,Nh);
rho=sqrt(rho);
cout << "rho =
min " << rho[].min << " max=" << rho[].max << endl;
plot(rho,fill=1,wait=1,cmm="indicator density ",ps="rhoP2.eps",
value=1,viso=viso,nbiso=viso.n);
plot(Th,wait=1,cmm="Mesh ",ps="ThrhoP2.eps");
Th=adaptmesh(Th,[dx(u),dy(u)],err=error,anisomax=1);
plot(Th,wait=1);
u=u;
rho=rho;
error = error/2;
} ;
If the method is correct, we expect to look the graphics by an almost constant function on your
computer as in Fig. 9.11.
248
Mesh
IsoValue
1e-08
3.16228e-08
1e-07
3.16228e-07
1e-06
3.16228e-06
1e-05
3.16228e-05
0.0001
0.000316228
0.001
0.00316228
0.01
0.0316228
0.1
0.316228
1
3.16228
10
31.6228
100
9.1.9
In the previous example we compute the error indicator, now we use it, to adapt the mesh.
The new mesh size is given by the following formulae:
hn+1 (x) =
hn (x)
fn (K (x))
where n (x) is the level of error at point x given by the local error indicator, hn is the previous mesh
size field, and fn is a user function define by fn = min(3, max(1/3, n /n )) where n = mean(n )c,
and c is an user coefficient generally close to one.
Example 9.12 (AdaptResidualErrorIndicator.edp)
First a macro MeshSizecomputation to get a P1 mesh size as the average of edge length.
//
macro the get the current mesh size
//
parameter
//
in: Th the mesh
//
Vh P1 fespace on Th
//
out :
//
h: the Vh finite element finite set to the current mesh size
macro MeshSizecomputation(Th,Vh,h)
{ /* Th mesh
Vh P1 finite element space
h
the P1 mesh size value */
real[int] count(Th.nv);
/* mesh size (lenEdge = integral(e) 1 ds) */
varf vmeshsizen(u,v)=intalledges(Th,qfnbpE=1)(v);
/* number of edge / par vertex */
varf vedgecount(u,v)=intalledges(Th,qfnbpE=1)(v/lenEdge);
/*
computation of the mesh size
----------------------------- */
249
count=vedgecount(0,Vh);
h[]=0.;
h[]=vmeshsizen(0,Vh);
cout << " count min = "<< count.min << " " << count.max << endl;
h[]=h[]./count;
cout << " -- bound meshsize = " <<h[].min << " " << h[].max << endl;
} //
end of macro MeshSizecomputation
macro ReMeshIndicator(Th,Ph,Vh,vindicator,coef)
{
Vh h=0;
/*evalutate the mesh size */
MeshSizecomputation(Th,Vh,h);
Ph etak;
etak[]=vindicator(0,Ph);
etak[]=sqrt(etak[]);
real etastar= coef*(etak[].sum/etak[].n);
cout << " etastar = " << etastar << " sum=" << etak[].sum << " " << endl;
/* here etaK is discontinous
we use the P1 L2 projection with mass lumping . */
Vh fn,sigma;
varf veta(unused,v)=int2d(Th)(etak*v);
varf vun(unused,v)=int2d(Th)(1*v);
fn[] = veta(0,Vh);
sigma[]= vun(0,Vh);
fn[]= fn[]./ sigma[];
fn = max(min(fn/etastar,3.),0.3333) ;
/* new mesh size */
h = h / fn ;
/* plot(h,wait=1); */
/* build the new mesh */
Th=adaptmesh(Th,IsMetric=1,h,splitpbedge=1,nbvx=10000);
}
fespace Vh(Th,P1);
fespace Ph(Th,P0);
//
real hinit=0.2;
Vh
h=hinit;
//
//
FE space definition --for the mesh size and solution
//
for the error indicator
//
initial mesh size
//
the FE function for the mesh size
to build a mesh with a given mesh size : meshsize
250
Th=adaptmesh(Th,h,IsMetric=1,splitpbedge=1,nbvx=10000);
plot(Th,wait=1,ps="RRI-Th-init.eps");
Vh u,v;
func f=(x-y);
problem Poisson(u,v) =
int2d(Th,qforder=5)( u*v*1.0e-10+
- int2d(Th,qforder=5)( f*v);
dx(u)*dx(v) + dy(u)*dy(v))
varf indicator2(unused,chiK) =
intalledges(Th)(chiK*lenEdge*square(jump(N.x*dx(u)+N.y*dy(u))))
+int2d(Th)(chiK*square(hTriangle*(f+dxx(u)+dyy(u))) );
for (int i=0;i< 10;i++)
{
u=u;
Poisson;
plot(Th,u,wait=1);
real cc=0.8;
if(i>5) cc=1;
ReMeshIndicator(Th,Ph,Vh,indicator2,cc);
plot(Th,wait=1);
}
IsoValue
5.89664e-05
0.000111887
0.000147167
0.000182447
0.000217727
0.000253008
0.000288288
0.000323568
0.000358848
0.000394129
0.000429409
0.000464689
0.000499969
0.000535249
0.00057053
0.00060581
0.00064109
0.00067637
0.000711651
0.000799851
IsoValue
-123.488
-123.476
-123.464
-123.452
-123.44
-123.428
-123.416
-123.404
-123.392
-123.38
-123.368
-123.356
-123.344
-123.332
-123.32
-123.308
-123.296
-123.284
-123.272
-123.26
Figure 9.12: the error indicator with isotropic P1 , the mesh and isovalue of the solution
9.2
Elasticity
Consider an elastic plate with undeformed shape ] h, h[ in R3 , R2 . By the deformation of the plate, we assume that a point P (x1 , x2 , x3 ) moves to P(1 , 2 , 3 ). The vector
9.2. ELASTICITY
251
u = (u1 , u2 , u3 ) = (1 x1 , 2 x2 , 3 x3 ) is called the displacement vector . By the deformation, the line segment x, x + x moves approximately to x + u(x), x + x + u(x + x) for
small , where x = (x1 , x2 , x3 ), x = (x1 , x2 , x3 ). We now calculate the ratio between two
segments
( ) = 1 |x|1 (|u(x + x) u(x) + x| |x|)
then we have (see e.g. [21, p.32])
lim ( ) = (1 + 2eij i j )
1/2
uk uk
+
2eij =
xi xj
1,
uj
ui
+
xj
xi
uj
ui
+
xj
xi
(9.20)
where ij is Kroneckers delta. We assume that the elastic plate is fixed on D ] h, h[, D
. If the body force f = (f1 , f2 , f3 ) is given in ] h, h[ and surface force g is given in
N ] h, h[, N = \ D , then the equation of equilibrium is given as follows:
j ij
ij nj
= fi in ] h, h[,
= gi on N ] h, h[,
i = 1, 2, 3
ui = 0 on D ] h, h[,
(9.21)
i = 1, 2, 3
(9.22)
252
Generalized plain stress: The cylinder is subjected to no load at x3 = h. Introducing the mean
values with respect to thickness,
1
ui (x1 , x2 ) =
2h
u(x1 , x2 , x3 )dx3
h
and we derive u3 0. Similarly we define the mean values f , g of the body force and
surface force as well as the mean values ij and ij of the components of stress and strain,
respectively.
In what follows we omit the overlines of u, f , g, ij and ij . Then we obtain similar equation of
equilibrium given in (9.21) replacing ] h, h[ with and changing i = 1, 2. In the case of plane
stress, ij = ij divu + 2ij , = (2)/( + ).
The equations of elasticity are naturally written in variational form for the displacement vector
u(x) V as
Z
Z
Z
g v, v V
f v+
int bottombeam = 2;
border a(t=2,0) { x=0; y=t ;label=1;};
//
left beam
border b(t=0,10) { x=t; y=0 ;label=bottombeam;};
//
bottom of beam
border c(t=0,2) { x=10; y=t ;label=1;};
//
rigth beam
border d(t=0,10) { x=10-t; y=2; label=3;};
//
top beam
real E = 21.5;
real sigma = 0.29;
real mu = E/(2*(1+sigma));
real lambda = E*sigma/((1+sigma)*(1-2*sigma));
real gravity = -0.05;
mesh th = buildmesh( b(20)+c(5)+d(20)+a(5));
fespace Vh(th,[P1,P1]);
Vh [uu,vv], [w,s];
cout << "lambda,mu,gravity ="<<lambda<< " " << mu << " " << gravity << endl;
//
deformation of a beam under its own weight
real sqrt2=sqrt(2.);
//
see lame.edp example 3.8
macro epsilon(u1,u2) [dx(u1),dy(u2),(dy(u1)+dx(u2))/sqrt2]
//
EOM
macro div(u,v) ( dx(u)+dy(v) )
//
EOM
solve bb([uu,vv],[w,s])=
int2d(th)(
lambda*div(w,s)*div(uu,vv)
+2.*mu*( epsilon(w,s)*epsilon(uu,vv) )
)
+ int2d(th) (-gravity*s)
+ on(1,uu=0,vv=0)
;
plot([uu,vv],wait=1);
9.2. ELASTICITY
253
plot([uu,vv],wait=1,bb=[[-0.5,2.5],[2.5,-0.5]]);
mesh th1 = movemesh(th, [x+uu, y+vv]);
plot(th1,wait=1);
Example 9.14 (beam-3d.edp) Consider elastic box with the undeformed parallelepiped shape ]0, 5[]0, 1[]0, 1[.
The body force is the gravity force f and the boundary force g is zero on all face except one the one
vertical left face where the beam is fixed.
include "cube.idp"
int[int] Nxyz=[20,5,5];
real [int,int] Bxyz=[[0.,5.],[0.,1.],[0.,1.]];
int [int,int] Lxyz=[[1,2],[2,2],[2,2]];
mesh3 Th=Cube(Nxyz,Bxyz,Lxyz);
real
real
real
real
fespace Vh(Th,[P1,P1,P1]);
Vh [u1,u2,u3], [v1,v2,v3];
cout << "lambda,mu,gravity ="<<lambda<< " " << mu << " " << gravity << endl;
real sqrt2=sqrt(2.);
macro epsilon(u1,u2,u3) [dx(u1),dy(u2),dz(u3),(dz(u2)+dy(u3))/sqrt2,
(dz(u1)+dx(u3))/sqrt2,(dy(u1)+dx(u2))/sqrt2] //
macro div(u1,u2,u3) ( dx(u1)+dy(u2)+dz(u3) )
//
EOM
EOM
solve Lame([u1,u2,u3],[v1,v2,v3])=
int3d(Th)(
lambda*div(u1,u2,u3)*div(v1,v2,v3)
+2.*mu*( epsilon(u1,u2,u3)*epsilon(v1,v2,v3) )
//
)
)
- int3d(Th) (gravity*v3)
+ on(1,u1=0,u2=0,u3=0)
;
real dmax= u1[].max;
cout << " max displacement = " << dmax << endl;
real coef= 0.1/dmax;
int[int] ref2=[1,0,2,0];
mesh3 Thm=movemesh3(Th,transfo=[x+u1*coef,y+u2*coef,z+u3*coef],label=ref2);
Thm=change(Thm,label=ref2);
plot(Th,Thm, wait=1,cmm="coef amplification = "+coef );
//
see fig ??
254
9.2.1
Fracture Mechanics
Consider the plate with the crack whose undeformed shape is a curve with the two edges 1 , 2 .
We assume the stress tensor ij is the state of plate stress regarding (x, y) = \ . Here
stands for the undeformed shape of elastic plate without crack. If the part N of the boundary
is fixed and a load L = (f , g) L2 ()2 L2 (N )2 is given, then the displacement u is the
minimizer of the potential energy functional
Z
Z
E(v; L, ) =
{w(x, v) f v}
gv
on D = \ N ,
(Cijkl :
Hookes tensor).
If the elasticity is homogeneous isotropic, then the displacement u(x) is decomposed in an open
neighborhood Uk of k as in (see e.g. [22])
u(x) =
2
X
1/2
C
(k ) + uk,R (x)
Kl (k )rk Skl
for x Uk , k = 1, 2
(9.23)
l=1
= {(x, y) : 1 x 0, y = 0}
with only one crack tip = (0, 0). Unfortunately, FreeFem++ cannot treat crack, so we use the
modification of the domain with U-shape channel (see Fig. 5.30) with d = 0.0001. The undeformed
crack is approximated by
d = {(x, y) : 1 x 10 d, d y d}
{(x, y) : 10 d x 0, d + 0.1 x y d 0.1 x}
and D = R in Fig. 5.30. In this example, we use three technique:
Fast Finite Element Interpolator from the mesh Th to Zoom for the scale-up of near .
9.2. ELASTICITY
255
After obtaining the displacement vector u = (u, v), we shall watch the deformation of the
crack near as follows,
mesh Plate = movemesh(Zoom,[x+u,y+v]);
plot(Plate);
//
fixed
256
//
//
deformation near
Fig.
Fig.
9.13
9.13
//
//
Fig.
Fig.
9.14
9.14
Figure 9.13:
Crack open displacement Figure 9.14: COD and Principal stress dif(COD) and Principal stress difference in the ference in the last adaptive mesh
first mesh
It is difficult to create mode II deformation by the opposed shear force on B and T that is observed
in a laboratory. So we use the body shear force along , that is, the x-component f1 of the body
force f is given by
f1 (x, y) = H(y 0.001) H(0.1 y) H(y 0.001) H(y + 0.1)
where H(t) = 1 if t > 0; = 0 if t < 0.
257
2*mu*(dx(u)*dx(w)+ ((dx(v)+dy(u))*(dx(s)+dy(w)))/4 )
+ lambda*(dx(u)+dy(v))*(dx(w)+dy(s))/2
)
-int2d(Th)(fx*w)
+on(R,u=0)+on(R,v=0);
//
fixed
;
for (int i=1; i<=3; i++)
{
mesh Plate = movemesh(Zoom,[x+u,y+v]);
Sx = lambda*(dx(u)+dy(v)) + 2*mu*dx(u);
Sy = lambda*(dx(u)+dy(v)) + 2*mu*dy(v);
Sxy = mu*(dy(u) + dx(v));
N = 0.1*1*sqrt((Sx-Sy)2+4*Sxy2);
if (i==1) {
plot(Plate,ps="1stCOD2.eps",bw=1);
plot(N,ps="1stPhoto2.eps",bw=1);
} else if (i==3) {
plot(Plate,ps="LastCOD2.eps",bw=1);
plot(N,ps="LastPhoto2.eps",bw=1);
break;
}
Th=adaptmesh(Th,[u,v]);
Problem;
}
//
//
deformation near
Fig.
Fig.
9.16
9.15
//
//
Fig.
Fig.
9.16
9.16
Figure 9.15: (COD) and Principal stress dif- Figure 9.16: COD and Principal stress difference in the first mesh
ference in the last adaptive mesh
9.3
Here we propose to solve the following non-linear academic problem of minimization of a functional
Z
1
f (|u|2 ) u b
J(u) =
2
f 0 (x) = a +
x
,
1+x
f 00 (x) =
1
(1 + x)2
258
9.3.1
Newton-Raphson algorithm
Now, we solve the Euler problem J(u) = 0 with Newton-Raphson algorithm, that is,
un+1 = un (2 J(un ))1 J(un )
First we introduce the two variational form vdJ and vhJ to compute respectively J and 2 J
//
dJ 1
) dJ(un )
ui
//
--------------------------------------------Ph dalpha ;
//
to store 2f 00 (|u|2 ) optimisation
//
the variational form of evaluate dJ = J
//
-------------------------------------//
dJ = f()*( dx(u)*dx(vh) + dy(u)*dy(vh)
varf vdJ(uh,vh) = int2d(Th)( alpha*( dx(u)*dx(vh) + dy(u)*dy(vh) ) - b*vh)
+ on(1,2,3,4, uh=0);
//
the Newton algorithm
Vh v,w;
u=0;
for (int i=0;i<100;i++)
{
alpha =
df( dx(u)*dx(u) + dy(u)*dy(u) ) ;
dalpha = 2*ddf( dx(u)*dx(u) + dy(u)*dy(u) ) ;
v[]= vdJ(0,Vh);
real res= v[]*v[];
cout << i << " residu2 = " << res << endl;
if( res< 1e-12) break;
matrix H= vhJ(Vh,Vh,factorize=1,solver=LU);
w[]=H-1*v[];
u[] -= w[];
}
plot (u,wait=1,cmm="solution with Newton-Raphson");
//
//
//
optimization
optimization
//
v = J(u)
the dot product
//
9.4
259
Eigenvalue Problems
This section depends on your installation of FreeFem++ ; you need to have compiled (see README arpack),
ARPACK. This tools is available in FreeFem++ if the word eigenvalue appear in line Load:,
like:
-- FreeFem++ v1.28 (date Thu Dec 26 10:56:34 CET 2002)
file : LapEigenValue.edp
Load: lg_fem lg_mesh eigenvalue
This tools is based on the arpack++ 1 the object-oriented version of ARPACK eigenvalue package
[1].
The function EigenValue computes the generalized eigenvalue of Au = Bu where sigma = is the
shift of the method. The matrix OP is defined with A B. The return value is the number of
converged eigenvalue (can be greater than the number of eigen value nev=)
int k=EigenValue(OP,B,nev= , sigma= );
where the matrix OP = A B with a solver and boundary condition, and the matrix B.
From version 3.31 you have also a functional interface like
int k=EigenValue(n,FOP1,FB,nev= , sigma= );
where n is the size of the matrix and the now the matrix defined throught function which defined
the respectively the matrix product of OP 1 and B, like in
int n = OP1.n;
func real[int] FOP1(real[int] & u) { real[int] Au=OP-1*u;return Au;}
func real[int] FB(real[int] & u) { real[int] Au=B*u;return Au;}
sym= the problem is symmetric (all the eigen value are real)
nev= the number desired eigenvalues (nev) close to the shift.
value= the array to store the real part of the eigenvalues
ivalue= the array to store the imag. part of the eigenvalues
vector= the FE function array to store the eigenvectors
1
http://www.caam.rice.edu/software/ARPACK/
260
Example 9.17 (lapEignenValue.edp) In the first example, we compute the eigenvalues and the eigenvectors of the Dirichlet problem on square =]0, [2 .
The problem is to find: , and u in RH01 ()
Z
Z
u v = uv v H01 ()
The exact eigenvalues are n,m = (n2 + m2 ), (n, m) N 2 with the associated eigenvectors are
um,n = sin(nx) sin(my).
We use the generalized inverse shift mode of the arpack++ library, to find 20 eigenvalues and
eigenvectors close to the shift value = 20.
//
//
//
//
//
//
//
u v =
//
verbosity=10;
mesh Th=square(20,20,[pi*x,pi*y]);
fespace Vh(Th,P2);
Vh u1,u2;
real sigma = 20;
varf
//
//
OP = A - sigma B ; // the shifted matrix
op(u1,u2)= int2d(Th)( dx(u1)*dx(u2) + dy(u1)*dy(u2) - sigma* u1*u2 )
+ on(1,2,3,4,u1=0) ;
//
Boundary condition
//
important remark:
//
261
//
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 just on a variational form
//
and not on b variational form.
//
because we solve w = OP 1 B v
int nev=20;
//
real[int] ev(nev);
Vh[int] eV(nev);
//
//
int k=EigenValue(OP,B,sym=true,sigma=sigma,value=ev,vector=eV,
tol=1e-10,maxit=0,ncv=0);
//
//
//
//
Nb of edges on Mortars = 0
Nb of edges on Boundary = 80, neb = 80
Nb Of Nodes = 1681
Nb of DF = 1681
Real symmetric eigenvalue problem: A*x - B*x*lambda
of the system
:
requested eigenvalues :
converged eigenvalues :
Arnoldi vectors generated:
iterations taken
:
Eigenvalues:
lambda[1]:
lambda[2]:
lambda[3]:
lambda[4]:
lambda[5]:
5.0002
8.00074
10.0011
10.0011
13.002
1681
20
20
41
2
262
lambda[6]: 13.0039
lambda[7]: 17.0046
lambda[8]: 17.0048
lambda[9]: 18.0083
lambda[10]: 20.0096
lambda[11]: 20.0096
lambda[12]: 25.014
lambda[13]: 25.0283
lambda[14]: 26.0159
lambda[15]: 26.0159
lambda[16]: 29.0258
lambda[17]: 29.0273
lambda[18]: 32.0449
lambda[19]: 34.049
lambda[20]: 34.0492
-------------------------------------------------------------
9.5
0 5.0002 err= -0.000225891 --1 8.00074 err= -0.000787446 --2 10.0011 err= -0.00134596 --3 10.0011 err= -0.00134619 --4 13.002 err= -0.00227747 --5 13.0039 err= -0.004179 --6 17.0046 err= -0.00623649 --7 17.0048 err= -0.00639952 --8 18.0083 err= -0.00862954 --9 20.0096 err= -0.0110483 --10 20.0096 err= -0.0110696 --11 25.014 err= -0.0154412 --12 25.0283 err= -0.0291014 --13 26.0159 err= -0.0218532 --14 26.0159 err= -0.0218544 --15 29.0258 err= -0.0311961 --16 29.0273 err= -0.0326472 --17 32.0449 err= -0.0457328 --18 34.049 err= -0.0530978 --19 34.0492 err= -0.0536275 ---
Evolution Problems
(9.26)
on ]0, T [.
with a positive viscosity coefficient and homogeneous Neumann boundary conditions. We solve
(9.26) by FEM in space and finite differences in time. We use the definition of the partial derivative
of the solution in the time derivative,
u
u(x, y, t) u(x, y, t )
(x, y, t) = lim
0
t
263
Figure 9.17:
u4,3 u3,4
IsoValue
-0.807681
-0.722662
-0.637643
-0.552624
-0.467605
-0.382586
-0.297567
-0.212548
-0.127529
-0.0425095
0.0425095
0.127529
0.212548
0.297567
0.382586
0.467605
0.552624
0.637643
0.722662
0.807681
u0 (x) = u0 (x) in ;
um+1 /n(x) = 0
(9.27)
for all m = 0, , [T / ],
on ,
which is so-called backward Euler method for (9.27). To obtain the variational formulation, multiply
with the test function v both sides of the equation:
Z
Z
m+1
m+1
{u
v u
v} = {um + f m+1 }v .
m+1
/n v =
(9.28)
m in a
Using the identity just above, we can calculate the finite element approximation um
h of u
step-by-step manner with respect to t.
264
Example 9.18 We now solve the following example with the exact solution u(x, y, t) = tx4 .
u
u = x4 12tx2 in ]0, 3[, =]0, 1[2
t
u(x, y, 0) = 0 on ,
u| = t x4
//
mesh Th=square(16,16);
fespace Vh(Th,P1);
Vh u,v,uu,f,g;
real dt = 0.1, mu = 0.01;
problem dHeat(u,v) =
int2d(Th)( u*v + dt*mu*(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;
//
start from t=0
uu = 0;
//
u(x,y,0)=0
for (int m=0;m<=3/dt;m++)
{
t=t+dt;
f = x4-mu*t*12*x2;
g = t*x4;
dHeat;
plot(u,wait=true);
uu = u;
cout <<"t="<<t<<"L2-Error="<<sqrt( int2d(Th)((u-t*x4)2) ) << endl;
}
R
2 1/2
In the last statement, the L2 -error u tx4
is calculated at t = m, = 0.1. At t = 0.1,
the error is 0.000213269. The errors increase with m and 0.00628589 at t = 3.
The iteration of the backward Euler (9.28) is made by for loop (see Section 4.11).
Note 9.2 The stiffness matrix in the loop is used over and over again. FreeFem++ support reuses
of stiffness matrix.
9.5.1
In this section, we show the advantage of implicit schemes. Let V, H be separable Hilbert space
and V p
is dense in H. Let a be a continuous bilinear form over V V with coercivity and symmetry.
Then a(v, v) become equivalent to the norm kvk of V .
Problem Ev(f, ): For a given f L2 (0, T ; V 0 ), u0 H
d
(u(t), v) + a(u(t), v) = (f (t), v)
dt
u(0) = u0
v V, ,
a.e. t [0, T ]
(9.29)
where V 0 is the dual space of V . Then, there is an unique solution u L (0, T ; H) L2 (0, T ; V ).
265
Let us denote the time step by > 0, NT = [T / ]. For the discretization, we put un = u(n ) and
consider the time difference for each [0, 1]
1 n+1
uh unh , i + a uhn+ , i = hf n+ , i i
i = 1, , m, n = 0, , NT
un+
= un+1
+ (1 )unh ,
h
h
(9.30)
f n+ = f n+1 + (1 )f n
Formula (9.30) is the forward Euler scheme if = 0, Crank-Nicolson scheme if = 1/2, the
backward Euler scheme if = 1.
T
Unknown vectors un = (u1h , , uM
h ) in
unh (x) = un1 1 (x) + + unm m (x),
un1 , , unm R
mij = (j , i ),
A = (aij ),
(9.31)
aij = a(j , i )
Refer [27, pp.7075] for solvability of (9.31). The stability of (9.31) is in [27, Theorem 2.13]:
Let {Th }h0 be regular triangulations (see Section 5.4). Then there is a number
c0 > 0 independent of h such that,
n
o
1 |u0 |2 + Pn1 kf k+ k2 0
[0, 1/2)
k=0
h
(9.32)
|unh |2
Pn1 k+ 2 Vh
2
0
|uh | + k=0 kf
kV 0
[1/2, 1]
h
2(1 ) 2
h
(1 2)c20
(9.33)
266
Vh u,v,oldU;
Vh f1, f0;
problem aTau(u,v) =
int2d(Th)( u*v + theta*tau*(dx(u)*dx(v) + dy(u)*dy(v) + u*v))
- int2d(Th)(oldU*v - (1-theta)*tau*(dx(oldU)*dx(v)+dy(oldU)*dy(v)+oldU*v))
- int2d(Th)(tau*( theta*f1+(1-theta)*f0 )*v );
while (theta <= 1.0) {
real t = 0, T=3;
oldU = 0;
out <<theta<<",";
for (int n=0;n<T/tau;n++) {
t = t+tau;
f0 = f(n*tau); f1 = f((n+1)*tau);
aTau;
oldU = u;
plot(u);
Vh uex = t*x2*(1-x)2;
Vh err = u - uex;
out<< abs(err[].max)/abs(uex[].max) <<",";
}
out << endl;
theta = theta + 0.1;
}
//
//
from t=0 to T
u(x,y,0)=0
//
exact sol.= tx2 (1 x)2
//
err =FE-sol - exact
//
kerrkL () /kuex kL ()
9.5.2
Convection
(9.34)
appears frequently in scientific problems, for example in the Navier-Stokes equations, in the ConvectionDiffusion equation, etc.
267
In the case of 1-dimensional space, we can easily find the general solution (x, t) 7 u(x, t) =
u0 (x t) of the following equation, if is constant,
u(x, 0) = u0 (x),
t u + x u = 0,
(9.35)
(0, t) X(t) = x
In this equation is the variable and x, t are parameters, and we denote the solution by Xx,t ( ).
Then it is noticed that (x, t) v(X( ), ) in = t satisfies the equation
t v + x v = t X v + ax X v = 0
and by the definition t X = X = + and x X = x x in = t, because if = t we have X( ) = x.
The general solution of (9.35) is thus the value of the boundary condition in Xx,t (0), that is to say
u(x, t) = u0 (Xx,t (0)) where Xx,t (0) is on the x axis, u(x, t) = u0 (Xx,t (0)) if Xx,t (0) is on the axis
of t.
In higher dimension Rd , d = 2, 3, the equation for the convection is written
t u + u = 0 in (0, T )
where a(x, t) Rd . FreeFem++ implements the Characteristic-Galerkin method for convection
operators. Recall that the equation (9.34) can be discretized as
du
dX
Du
= f i.e.
(X(t), t) = f (X(t), t) where
(t) = (X(t), t)
Dt
dt
dt
where D is the total derivative operator. So a good scheme is one step of backward convection by
the method of Characteristics-Galerkin
1 m+1
u
(x) um (X m (x)) = f m (x)
(9.36)
u (X(m )) = u (X((m + 1) ))
m
d
X
um
i=1
m
xi
(X((m + 1) ))
Xi
((m + 1) ) + o( )
t
(9.37)
where Xi (t) are the i-th component of X(t), um (x) = u(x, m ) and we used the chain rule and
x = X((m + 1) ). From (9.37), it follows that
um (X m (x)) = um (x) m (x) um (x) + o( ).
Also we apply Taylors expansion for t 7 um (x m (x)t), 0 t , then
um (x ) = um (x) m (x) um (x) + o( ).
(9.38)
268
Putting
convect (, , um ) um (x m ) ,
A classical convection problem is that of the rotating bell (quoted from [19][p.16]). Let be
the unit disk centered at 0, with its center rotating with speed 1 = y, 2 = x We consider the
problem (9.34) with f = 0 and the initial condition u(x, 0) = u0 (x), that is, from (9.36)
um+1 (x) = um (X m (x)) convect(, , um ).
The exact solution is u(x, t) = u(X(t)) where X equals x rotated around the origin by an angle
= t (rotate in clockwise). So, if u0 in a 3D perspective looks like a bell, then u will have exactly
the same shape, but rotated by the same amount. The program consists in solving the equation
until T = 2, that is for a full revolution and to compare the final solution with the initial one;
they should be equal.
y=sin(t); }; //
the
give u0
real dt = 0.17,t=0;
//
time step
Vh a1 = -y, a2 = x;
//
rotation velocity
Vh u;
//
um+1
for (int m=0; m<2*pi/dt ; m++) {
t += dt;
u=convect([a1,a2],-dt,u0);
//
um+1 = um (X m (x))
u0=u;
//
m++
plot(u,cmm=" t="+t + ", min=" + u[].min + ", max=" + u[].max,wait=0);
};
Note 9.3 The scheme convect is unconditionally stable, then the bell become lower and lower (the
maximum of u37 is 0.406 as shown in Fig. 9.21).
9.5.3
t u +
(9.39)
(9.40)
269
convection: t=6.29, min=1.55289e-09, max=0.40659m=37
2 +(y0.3)2 )
Boundary conditions for this problem may not be so easy to device. As in the one dimensional
case the PDE contains boundary conditions on the axis x1 = 0 and on the axis x2 = 0, namely
two one dimensional Black-Scholes equations driven respectively by the data u (0, +, T ) and
u (+, 0, T ). These will be automatically accounted for because they are embedded in the PDE.
So if we do nothing in the variational form (i.e. if we take a Neumann boundary condition at these
two axis in the strong form) there will be no disturbance to these. At infinity in one of the variable,
as in 1D, it makes sense to impose u = 0. We take
1 = 0.3, 2 = 0.3, = 0.3, r = 0.05, K = 40, T = 0.5
(9.41)
An implicit Euler scheme is used and a mesh adaptation is done every 10 time steps. To have
an unconditionally stable scheme, the first order terms are treated by the Characteristic Galerkin
method, which, roughly, approximates
u
u
u
1 n+1
+ a1
+ a2
u
(x) un (x )
t
x
y
Example 9.21
(9.42)
[BlackSchol.edp]
//
file BlackScholes2D.edp
int m=30,L=80,LL=80, j=100;
real sigx=0.3, sigy=0.3, rho=0.3, r=0.05, K=40, dt=0.01;
mesh th=square(m,m,[L*x,LL*y]);
fespace Vh(th,P1);
Vh u=max(K-max(x,y),0.);
Vh xveloc, yveloc, v,uold;
for (int n=0; n*dt <= 1.0; n++)
{
if(j>20) { th = adaptmesh(th,u,verbosity=1,abserror=1,nbjacoby=2,
err=0.001, nbvx=5000, omega=1.8, ratio=1.8, nbsmooth=3,
splitpbedge=1, maxsubdiv=5,rescaling=1) ;
j=0;
xveloc = -x*r+x*sigx2+x*rho*sigx*sigy/2;
yveloc = -y*r+y*sigy2+y*rho*sigx*sigy/2;
270
u=u;
};
uold=u;
solve eq1(u,v,init=j,solver=LU) = int2d(th)( u*v*(r+1/dt)
+ dx(u)*dx(v)*(x*sigx)2/2 + dy(u)*dy(v)*(y*sigy)2/2
+ (dy(u)*dx(v) + dx(u)*dy(v))*rho*sigx*sigy*x*y/2)
- int2d(th)( v*convect([xveloc,yveloc],dt,w)/dt) + on(2,3,u=0);
j=j+1;
};
plot(u,wait=1,value=1);
9.6
9.6.1
Navier-Stokes Equation
Stokes and Navier-Stokes
in
(9.43)
where u = (u1 , u2 ) is the velocity vector and p the pressure. For simplicity, let us choose Dirichlet
boundary conditions on the velocity, u = u on .
271
Z
f w
ui vi =
for all v V
By multiplying the first equation in (9.43) with v V and the second with q W , subsequent
integration over , and an application of Greens formula, we have
Z
Z
Z
u v
divv p =
f v
Z
divu q = 0
This yields the weak form of (9.43): Find (u, p) V W such that
a(u, v) + b(v, p) = (f , v)
b(u, q) = 0
(9.44)
(9.45)
a(u, v) =
2 Z
X
i=1
ui vi
(9.46)
Z
b(u, q) =
divu q
(9.47)
Now, we consider finite element spaces V h V and Wh W , and we assume the following basis
functions
V h = Vh Vh ,
Vh = {vh | vh = v1 1 + + vMV MV },
Wh = {qh | qh = q1 1 + + qMW MW }
The discrete weak form is: Find (uh , ph ) V h Wh such that
a(uh , v h ) + b(v h , p) = (f , v h ), v h V h
b(uh , qh )
= 0,
qh Wh
(9.48)
272
for all v h Zh
where
Zh = {v h V h | b(wh , qh ) = 0
for all qh Wh }
for all qh Wh
(9.49)
i = 1, , MW ; j = 1, , MV
then (9.48) is written by
A B
B 0
Uh
{ph }
=
Fh
0
(9.50)
where
A=
A 0
0 A
B =
BxT
By T
Uh =
{u1,h }
{u2,h }
R
{R f1 i }
Fh =
{ f2 i }
Penalty method: This method consists of replacing (9.48) by a more regular problem: Find
h satisfying
(v h , ph ) V h W
a(uh , v h ) + b(v h , ph ) = (f , v h ), v h V h
h
b(uh , qh ) (ph , qh ) = 0,
qh W
h L2 (). Formally, we have
where W
divuh = ph
and the corresponding algebraic problem
A B
U h
Fh
=
B I
{ph }
0
(9.51)
273
(9.52)
Since the matrix A + (1/)B B is symmetric, positive-definite, and sparse, (9.52) can be solved by
known technique. There is a constant C > 0 independent of such that
kuh uh k1, + kph ph k0, C
(see e.g. [25, 17.2])
Example 9.22 (Cavity.edp) The driven cavity flow problem is solved first at zero Reynolds number
(Stokes flow) and then at Reynolds 100. The velocity pressure formulation is used first and then
the calculation is repeated with the stream function vorticity formulation.
We solve the driven cavity problem by the penalty method (9.51) where u n = 0 and u s = 1
on the top boundary and zero elsewhere ( n is the unit normal to , and s the unit tangent to ).
The mesh is constructed by
mesh Th=square(8,8);
v|K P2
Mh = v H 1 (]0, 1[2 ) K Th
v|K P1
and
//
The Stokes operator is implemented as a system-solve for the velocity (u1, u2) and the pressure p.
The test function for the velocity is (v1, v2) and q for the pressure, so the variational form (9.48)
in freefem language is:
solve Stokes (u1,u2,p,v1,v2,q,solver=Crout) =
int2d(Th)( ( dx(u1)*dx(v1) + dy(u1)*dy(v1)
+ dx(u2)*dx(v2) + dy(u2)*dy(v2) )
- p*q*(0.000001)
- p*dx(v1) - p*dy(v2)
- dx(u1)*q - dy(u2)*q
)
+ on(3,u1=1,u2=0)
+ on(1,2,4,u1=0,u2=0);
//
see Section 5.1.1 for labels 1,2,3,4
274
If the streamlines are required, they can be computed by finding such that rot = u or better,
= u
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);
u=0
un X n ) un+1 + pn+1 = 0,
un+1
=0
u
t
+ u u, giving a
(9.53)
The term un X n (x) un (x un (x) ) will be computed by the operator convect , so we obtain
int i=0;
real nu=1./100.;
real dt=0.1;
real alpha=1/dt;
Xh up1,up2;
problem NS (u1,u2,p,v1,v2,q,solver=Crout,init=i) =
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*(0.000001)
- 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(3,u1=1,u2=0)
+ on(1,2,4,u1=0,u2=0)
;
for (i=0;i<=10;i++)
{
up1=u1;
275
up2=u2;
NS;
if ( !(i % 10))
plot(coef=0.2,cmm=" [u1,u2] and p
} ;
//
",p,[u1,u2]);
9.6.2
We solve Stokes problem without penalty. The classical iterative method of Uzawa is described by
the algorithm (see e.g.[25, 17.3], [34, 13] or [35, 13] ):
Initialize: Let p0h be an arbitrary chosen element of L2 ().
Calculate uh : Once pnh is known, v nh is the solution of
unh = A1 (f h B pnh )
Advance ph : Let pn+1
be defined by
h
= pnh + n Bunh
pn+1
h
There is a constant > 0 such that n 2 for each n, then unh converges to the solution uh ,
and then Bv nh 0 as n from the Advance ph . This method in general converges quite slowly.
First we define mesh, and the Taylor-Hood approximation. So Xh is the velocity space, and Mh
is the pressure space.
Example 9.23 (StokesUzawa.edp)
mesh Th=square(10,10);
fespace Xh(Th,P2),Mh(Th,P1);
Xh u1,u2,v1,v2;
Mh p,q,ppp;
//
//
//
//
By)
B = (Bx
276
b[]
//
u1[] = A-1*b[];
//
u2[] = A-1*b[];
compute u1(pp)
compute u2(pp)
//
//
ppp = Bxu1
+Byu2
};
9.6.3
NSUzawaCahouetChabart.edp
In this example we solve the Navier-Stokes equation past a cylinder with the Uzawa algorithm
preconditioned by the Cahouet-Chabart method (see [36] for all the details).
The idea of the preconditioner is that in a periodic domain, all differential operators commute and
the Uzawa algorithm comes to solving the linear operator .((Id + )1 , where Id is the
identity operator. So the preconditioner suggested is 1 + Id.
To implement this, we do
Example 9.24 (NSUzawaCahouetChabart.edp)
real D=0.1, H=0.41;
real cx0 = 0.2, cy0 = 0.2;
//
center of cyl.
real xa = 0.15, ya=0.2, xe = 0.25,ye =0.2;
border fr1(t=0,2.2){x=t; y=0; label=1;}
border fr2(t=0,H){x=2.2; y=t; label=2;}
border fr3(t=2.2,0){x=t; y=H; label=1;}
border fr4(t=H,0){x=0; y=t; label=1;}
border fr5(t=2*pi,0){x=cx0+D*sin(t)/2; y=cy0+D*cos(t)/2; label=3;}
int nn=15;
Th=buildmesh(fr1(5*nn)+fr2(nn)+fr3(5*nn)+fr4(nn)+fr5(-nn*3));
Um= 1.5;
//
max velocity (Rey 100)
Ub = Um*2./3.;
nu = 1e-3;
Rey = Ub*D/nu;
//
Boundary condition
func U1 = 4.*Um*y*(H-y)/(H*H) ;
func U2 = 0. ;
mesh
real
func
real
real
real T=2,t=0;
real dt = D/nn/Um;
cout << " dt = " << dt <<endl;
real alpha=1/dt,epspq=1e-10;
//
CFL = 1
277
fespace Mh(Th,[P1]);
fespace Xh(Th,[P2]);
fespace Wh(Th,[P1dc]);
macro grad(u) [dx(u),dy(u)]
macro div(u1,u2) (dx(u1)+dy(u2))
varf von1([u1,u2,p],[v1,v2,q]) =
//
//
on(3,u1=0,u2=0) + on(1,u1=U1,u2=U2);
//
remark : the value 100 in next line is manualy fitted, because free
outlet.
varf vA(p,q) =int2d(Th)((grad( p ) *grad(q)) ) + int1d(Th,2)(100*p*q) ;
varf vM(p,q) =int2d(Th,qft=qf2pT)(
p*q )+ on(2,p=0);
matrix pAM=vM(Mh,Mh,solver=UMFPACK);
matrix pAA=vA(Mh,Mh,solver=UMFPACK);
matrix AU=vu(Xh,Xh,solver=UMFPACK);
matrix B1=vu1(Mh,Xh);
matrix B2=vu2(Mh,Xh);
Xh u1,u2;
Mh p;
varf vonu1([u1],[v1]) = on(1,u1=U1) + on(3,u1=0);
varf vonu2([u1],[v1]) = on(1,u1=U2) + on(3,u1=0);
vrhs1(uu,vv)
vrhs2(v2,v1)
= int2d(Th) (convect([u1,u2],-dt,u1)*vv*alpha)+vonu1 ;
= int2d(Th) (convect([u1,u2],-dt,u2)*v1*alpha)+vonu2;
real[int]
Precon(real[int] & p)
278
{
real[int] pa= pAA-1*p;
real[int] pm= pAM-1*p;
real[int] pp= alpha*pa+nu*pm;
return pp;
}
The loop in time. Warning with the stop test of the conjugate gradient, because we start from the
previous solution and the end the previous solution is close to the final solution, dont take a relative
stop test to the first residual, take an absolute stop test ( negative here)
verbosity = 0;
p=0;
Wh w;
//
to store vorticity ..
real eps=1e-6;
int ndt = T/dt;
for(int i=0;i<ndt;++i)
{
brhs1 = vrhs1(0,Xh);
brhs2 = vrhs2(0,Xh);
int res=LinearCG(JUzawa,p[],precon=Precon,nbiter=100,verbosity=10,veps=eps);
assert(res==1) ;
eps = -abs(eps);
w = -dy(u1)+dx(u2);
plot(w,fill=1,wait=0, nbiso=40);
dt = min(dt,T-t);
t += dt;
if( dt < 1e-10*T) break;
}
plot(w,fill=1,wait=0, nbiso=40,ps="NScahouetChabart");
//
see fig.
9.24
Figure 9.24: The vorticity at Reynolds number 100 a time 2s with the Cahouet-Chabart
method.
9.7
279
Variational inequality
u.v + v d =
f u,
v H01 ()
(u g)+ = 0,
L2 (), 0, 0,
(9.54)
uk+1 .vk+1 d =
f vk+1 ,
vk+1 V0,k+1
where <, > is the duality bracket between H01 () and H 1 (), and c is a penalty
constant (large enough).
You can find all the mathematics about this algorithm in [38].
Now how to do that in FreeFem++
The full example is:
280
//
//
//
//
P1 FE space
//
number of Degree of freedom
//
solution and previous one
to def the set where the containt is reached.
to store the right and side of the equation
//
the penalty parameter of the algoritm
//
right hand side function
//
Dirichlet boundary condition function
//
the discret function g
to store the diagonal of the matrix 2 version
//
//
the variatonal form of the problem:
varf a(uh,vh) =
//
int2d(Th)( dx(uh)*dx(vh) + dy(uh)*dy(vh) )
- int2d(Th)( f*vh )
+ on(1,2,3,4,uh=fd) ;
//
//
two version of the matrix of the problem
matrix A=a(Vh,Vh,tgv=tgv,solver=CG);
matrix AA=a(Vh,Vh,solver:GC);
//
//
one changing
one for computing residual
//
the mass Matrix construction:
varf vM(uh,vh) = int2d(Th)(uh*vh);
matrix M=vM(Vh,Vh);
//
to do a fast computing of L2 norm :
u*(w=M*u))
Aii=A.diag;
//
sqrt(
rhs = a(0,Vh,tgv=tgv);
Ik =0;
uhp=-tgv;
//
previous value is
Vh lambda=0;
for(int iter=0;iter<100;++iter)
{
real[int] b(n) ; b=rhs;
//
get a copy of the Right hand side
real[int] Ak(n);
//
the complementary of Ik ( !Ik = (Ik-1))
//
Today the operator Ik- 1. is not implement so we do:
Ak= 1.; Ak -= Ik[];
//
build Ak = ! Ik
//
adding new locking condition on b and on the diagonal if (Ik ==1 )
b = Ik[] .* g[];
b *= tgv;
b -= Ak .* rhs;
Aiin = Ik[] * tgv;
Aiin += Ak .* Aii;
//
set Aii= tgv i Ik
A.diag = Aiin;
//
set the matrix diagonal (appear in version 1.46-1)
set(A,solver=CG);
//
important to change preconditioning for solving
uh[] = A-1* b;
//
solve the problem with more locking condition
lambda[] = AA * uh[];
//
compute the residual ( fast with matrix)
R
lambda[] += rhs;
//
remark rhs = f v
Ik = ( lambda + c*( g- uh)) < 0.;
//
281
//
Remark, as you can see on this example, some vector , or matrix operator are not implemented so
a way is to skip the expression and we use operator +=, -= to merge the result.
9.8
Domain decomposition
We present, three classic examples, of domain decomposition technique: first, Schwarz algorithm
with overlapping, second Schwarz algorithm without overlapping (also call Shur complement), and
last we show to use the conjugate gradient to solve the boundary problem of the Shur complement.
9.8.1
To solve
u = f, in = 1 2
u| = 0
|1 = un2
un+1
1
= f in 2
un+1
2
|2 = un1
un+1
2
where i is the boundary of i and on the condition that 1 2 6= and that ui are zero at
iteration 1.
Here we take 1 to be a quadrangle, 2 a disk and we apply the algorithm starting from zero.
Example 9.26 (Schwarz-overlap.edp)
int inside = 2;
//
inside boundary
int outside = 1;
//
outside boundary
border a(t=1,2){x=t;y=0;label=outside;};
border b(t=0,1){x=2;y=t;label=outside;};
border c(t=2,0){x=t ;y=1;label=outside;};
border d(t=1,0){x = 1-t; y = t;label=inside;};
border e(t=0, pi/2){ x= cos(t); y = sin(t);label=inside;};
border e1(t=pi/2, 2*pi){ x= cos(t); y = sin(t);label=outside;};
int n=4;
mesh th = buildmesh( a(5*n) + b(5*n) + c(10*n) + d(5*n));
mesh TH = buildmesh( e(5*n) + e1(25*n) );
plot(th,TH,wait=1);
//
to see the 2 meshes
282
=
)
+ on(outside,U= 0 ) ;
=
)
+ on(outside,u = 0 ) ;
9.8.2
To solve
u = f in = 1 2
u| = 0,
the Schwarz algorithm for domain decomposition without overlapping runs like this
Let introduce i is common the boundary of 1 and 2 and ie = i \ i .
The problem find such that (u1 |i = u2 |i ) where ui is solution of the following Laplace problem:
ui = f in i
ui |i =
ui |ie = 0
283
(u1 u2 )
2
284
plot(th,TH,wait=1,ps="schwarz-no-u.eps");
fespace vh(th,P1);
fespace VH(TH,P1);
vh u=0,v; VH U,V;
vh lambda=0;
int i=0;
problem PB(U,V,init=i,solver=Cholesky) =
int2d(TH)( dx(U)*dx(V)+dy(U)*dy(V) )
+ int2d(TH)( -V)
+ int1d(TH,inside)(lambda*V) +
on(outside,U= 0 ) ;
problem pb(u,v,init=i,solver=Cholesky) =
int2d(th)( dx(u)*dx(v)+dy(u)*dy(v) )
+ int2d(th)( -v)
+ int1d(th,inside)(-lambda*v) +
on(outside,u = 0 ) ;
Figure 9.28: Isovalues of the solution at iteration 0 and iteration 9 without overlapping
9.8.3
Schwarz-gc.edp
To solve
u = f in = 1 2
u| = 0,
the Schwarz algorithm for domain decomposition without overlapping runs like this
Let introduce i is common the boundary of 1 and 2 and ie = i \ i .
285
The problem find such that (u1 |i = u2 |i ) where ui is solution of the following Laplace problem:
ui = f in i
ui |i =
ui |ie = 0
The version of this example uses the Shur complement. The problem on the border is solved by a
conjugate gradient method.
First, we construct the two domain
Example 9.28 (Schwarz-gc.edp)
//
Schwarz without overlapping (Shur complement for Neumann to Dirichet)
real cpu=clock();
int inside = 2;
int outside = 1;
border Gamma1(t=1,2){x=t;y=0;label=outside;};
border Gamma2(t=0,1){x=2;y=t;label=outside;};
border Gamma3(t=2,0){x=t ;y=1;label=outside;};
border GammaInside(t=1,0){x = 1-t; y = t;label=inside;};
border GammaArc(t=pi/2, 2*pi){ x= cos(t); y = sin(t);label=outside;};
int n=4;
//
build the mesh of 1 and 2
mesh Th1 = buildmesh( Gamma1(5*n) + Gamma2(5*n) + GammaInside(5*n) + Gamma3(5*n));
mesh Th2 = buildmesh ( GammaInside(-5*n) + GammaArc(25*n) );
plot(Th1,Th2);
//
fespace Vh1(Th1,P1),
Vh2(Th2,P1);
Note 9.6 It is impossible to define a function just on a part of boundary, so the lambda function
must be defined on the all domain 1 such as
Vh1 lambda=0;
//
take Vh1
or, we define a border matrix , because the lambda function is none zero inside the domain 1 :
varf b(u2,v2,solver=CG) =int1d(Th1,inside)(u2*v2);
matrix B= b(Vh1,Vh1,solver=CG);
286
(u1 u2 )v1
i
//
//
no refactorization i !=0
Note 9.7 The difference between the two notations v1 and v1[] is: v1 is the finite element
function and v1[] is the vector in the canonical basis of the finite element function v1 .
Vh1 p=0,q=0;
//
solve the problem with Conjugate Gradient
LinearCG(BoundaryProblem,p[],eps=1.e-6,nbiter=100);
//
compute the final solution, because CG works with increment
BoundaryProblem(p[]);
//
solve again to have right u1,u2
cout << " -- CPU time
plot(u1,u2);
9.9
schwarz-gc:" <<
plot
This problem involves the Lame system of elasticity and the Stokes system for viscous fluids with
velocity u and pressure p:
u + p = 0, u = 0, in , u = u on =
where u is the velocity of the boundaries. The force that the fluid applies to the boundaries is the
normal stress
h = (u + uT )n pn
Elastic solids subject to forces deform: a point in the solid, at (x,y) goes to (X,Y) after. When the
displacement vector v = (v1 , v2 ) = (X x, Y y) is small, Hookes law relates the stress tensor
inside the solid to the deformation tensor :
vj
1 vi
+
)
ij = ij .v + 2ij , ij = (
2 xj
xi
where is the Kronecker symbol and where , are two constants describing the material mechanical properties in terms of the modulus of elasticity, and Youngs modulus.
The equations of elasticity are naturally written in variational form for the displacement vector
v(x) V as
Z
Z
Z
gw+
The data are the gravity force g and the boundary stress h.
h w, w V
287
Example 9.29 (fluidStruct.edp) In our example the Lame system and the Stokes system are coupled
by a common boundary on which the fluid stress creates a displacement of the boundary and hence
changes the shape of the domain where the Stokes problem is integrated. The geometry is that of
a vertical driven cavity with an elastic lid. The lid is a beam with weight so it will be deformed by
its own weight and by the normal stress due to the fluid reaction. The cavity is the 10 10 square
and the lid is a rectangle of height l = 2.
A beam sits on a box full of fluid rotating because the left vertical side has velocity one. The beam
is bent by its own weight, but the pressure of the fluid modifies the bending.
The bending displacement of the beam is given by (uu,vv) whose solution is given as follows.
//
int bottombeam = 2;
//
label of bottombeam
border a(t=2,0) { x=0; y=t ;label=1;};
//
left beam
border b(t=0,10) { x=t; y=0 ;label=bottombeam;};
//
bottom of beam
border c(t=0,2) { x=10; y=t ;label=1;};
//
rigth beam
border d(t=0,10) { x=10-t; y=2; label=3;};
//
top beam
real E = 21.5;
real sigma = 0.29;
real mu = E/(2*(1+sigma));
real lambda = E*sigma/((1+sigma)*(1-2*sigma));
real gravity = -0.05;
mesh th = buildmesh( b(20)+c(5)+d(20)+a(5));
fespace Vh(th,P1);
Vh uu,w,vv,s,fluidforce=0;
cout << "lambda,mu,gravity ="<<lambda<< " " << mu << " " << gravity << endl;
//
deformation of a beam under its own weight
solve bb([uu,vv],[w,s]) =
int2d(th)(
lambda*div(w,s)*div(uu,vv)
+2.*mu*( epsilon(w,s)*epsilon(uu,vv) )
)
+ int2d(th) (-gravity*s)
+ on(1,uu=0,vv=0)
+ fluidforce[];
;
plot([uu,vv],wait=1);
mesh th1 = movemesh(th, [x+uu, y+vv]);
plot(th1,wait=1);
Then Stokes equation for fluids ast low speed are solved in the box below the beam, but the beam has
deformed the box (see border h):
border
border
border
border
e(t=0,10)
f(t=0,10)
g(t=0,10)
h(t=0,10)
{
{
{
{
//
Stokes on square b,e,f,g driven cavite on left side g
x=t; y=-10; label= 1; };
//
bottom
x=10; y=-10+t ; label= 1; };
//
right
x=0; y=-t ;label= 2;};
//
left
x=t; y=vv(t,0)*( t>=0.001 )*(t <= 9.999);
label=3;};
//
top of cavity deformed
mesh sh = buildmesh(h(-20)+f(10)+e(10)+g(10));
plot(sh,wait=1);
288
We use the Uzawa conjugate gradient to solve the Stokes problem like in example Section 9.6.2
fespace Xh(sh,P2),Mh(sh,P1);
Xh u1,u2,v1,v2;
Mh p,q,ppp;
Now the beam will feel the stress constraint from the fluid:
Vh sigma11,sigma22,sigma12;
Vh uu1=uu,vv1=vv;
sigma11([x+uu,y+vv]) = (2*dx(u1)-p);
sigma22([x+uu,y+vv]) = (2*dy(u2)-p);
sigma12([x+uu,y+vv]) = (dx(u1)+dy(u2));
289
[u1,u2],p
IsoValue
-2.62541
-2.26528
-1.90515
-1.54503
-1.1849
-0.824776
-0.46465
-0.104524
0.255603
0.615729
0.975855
1.33598
1.69611
2.05623
2.41636
2.77649
3.13661
3.49674
3.85686
4.21699
Vec Value
0
0.0499861
0.0999722
0.149958
0.199944
0.249931
0.299917
0.349903
0.399889
0.449875
0.499861
0.549847
0.599833
0.649819
0.699806
0.749792
0.799778
0.849764
0.89975
0.949736
Figure 9.29: Fluid velocity and pressure (left) and displacement vector (center) of the
structure and displaced geometry (right) in the fluid-structure interaction of a soft side and
a driven cavity
solve bbst([uu,vv],[w,s],init=i) =
int2d(th)(
lambda*div(w,s)*div(uu,vv)
+2.*mu*( epsilon(w,s)*epsilon(uu,vv) )
)
+ int2d(th) (-gravity*s)
+ int1d(th,bottombeam)( -coef*(
sigma11*N.x*w + sigma22*N.y*s
+ sigma12*(N.y*w+N.x*s) ) )
+ on(1,uu=0,vv=0);
plot([uu,vv],wait=1);
real err = sqrt(int2d(th)( (uu-uu1)2 + (vv-vv1)2 ));
cout << " Erreur L2 = " << err << "----------\n";
Notice that the matrix generated by bbst is reused (see init=i). Finally we deform the beam
th1 = movemesh(th, [x+0.2*uu, y+0.2*vv]);
plot(th1,wait=1);
}
9.10
//
end of loop
Transmission Problem
Consider an elastic plate whose displacement change vertically, which is made up of three plates of
different materials, welded on each other. Let i , i = 1, 2, 3 be the domain occupied by i-th material
with tension i (see Section 9.1.1). The computational domain is the interior of 1 2 3 .
The vertical displacement u(x, y) is obtained from
i u = f in i
i n u|i
= j n u|j
(9.55)
on i j
if 1 i < j 3
(9.56)
290
where n u|i denotes the value of the normal derivative n u on the boundary i of the domain i .
By introducing the characteristic function i of i , that is,
i (x) = 1
if x i ;
i (x) = 0
if x 6 i
(9.57)
we can easily rewrite (9.55) and (9.56) to the weak form. Here we assume that u = 0 on = .
problem Transmission: For a given function f , find u such that
Z
u v,
a(u, v) =
a(u, v) = `(f, v)
Z
fv
`(f, v) =
(9.58)
//
border
border
border
border
border
border
//
example using region keyword
construct a mesh with 4 regions (sub-domains)
a(t=0,1){x=t;y=0;};
b(t=0,0.5){x=1;y=t;};
c(t=0,0.5){x=1-t;y=0.5;};
d(t=0.5,1){x=0.5;y=t;};
e(t=0.5,1){x=1-t;y=1;};
f(t=0,1){x=0;y=1-t;};
//
internal boundary
border i1(t=0,0.5){x=t;y=1-t;};
border i2(t=0,0.5){x=t;y=t;};
border i3(t=0,0.5){x=1-t;y=t;};
mesh th = buildmesh (a(6) + b(4) + c(4) +d(4) + e(4) +
f(6)+i1(6)+i2(6)+i3(6));
fespace Ph(th,P0);
//
constant discontinuous functions / element
fespace Vh(th,P1);
//
P1 continuous functions / element
Ph reg=region;
//
defined the P0 function associated to region number
plot(reg,fill=1,wait=1,value=1);
region is a keyword of FreeFem++ which is in fact a variable depending of the current position
(is not a function today, use Ph reg=region; to set a function). This variable value returned
is the number of the subdomain of the current position. This number is defined by buildmesh
which scans while building the mesh all its connected component. So to get the number of a region
containing a particular point one does:
int nupper=reg(0.4,0.9);
//
get the region number of point (0.4,0.9)
int nlower=reg(0.9,0.1);
//
get the region number of point (0.4,0.1)
cout << " nlower " << nlower << ", nupper = " << nupper<< endl;
//
defined the characteristics functions of upper and lower region
Ph nu=1+5*(region==nlower) + 10*(region==nupper);
plot(nu,fill=1,wait=1);
This is particularly useful to define discontinuous functions such as might occur when one part of
the domain is copper and the other one is iron, for example.
291
IsoValue
-0.315789
0.157895
0.473684
0.789474
1.10526
1.42105
1.73684
2.05263
2.36842
2.68421
3
3.31579
3.63158
3.94737
4.26316
4.57895
4.89474
5.21053
5.52632
6.31579
IsoValue
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
292
9.11
border
border
border
border
//
//
a(t=0,L){x=t;y=0;};
b(t=0,h1){x=L;y=t;};
f(t=L,0){x=t;y=t*(h1-h)/L+h;};
d(t=h,0){x=0;y=t;};
//
//
//
//
//
longueur du domaine
hauteur du bord gauche
hauteur du bord droite
//
maillage dun tapeze
bottom: a
right: b
free surface: f
left: d
int n=4;
mesh Th=buildmesh (a(10*n)+b(6*n)+f(8*n)+d(3*n));
plot(Th,ps="dTh.eps");
293
u = 0
u=y
u
=0
n
u
q
= nx and u = y
n
K
in
on b
on d a
on f
u = 0 in n
u = y on nb
u
= 0 on nd na
u = y on nf
The variational formulation is:
find u on V = H 1 (n ), such than u = y on nb and nf
Z
uu0 = 0,
u0 V with u0 = 0 on nb nf
v = 0
in n
v =0
on na
v
=0
on nb nd
v
u
q
=
nx on nf
n
n K
The variational formulation is:
find v on V , such than v = 0 on na
Z
Z
u
q
0
vv =
(
nx )v 0 , v 0 V with v 0 = 0 on na
n K
n
n
f
Finally the new domain n+1 = F(n )
Example 9.30 (freeboundary.edp) The FreeFem++ :implementation is:
real q=0.02;
real K=0.5;
//
//
flux entrant
permeabilit
e
fespace Vh(Th,P1);
int j=0;
Vh u,v,uu,vv;
problem Pu(u,uu,solver=CG) = int2d(Th)( dx(u)*dx(uu)+dy(u)*dy(uu))
+ on(b,f,u=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(u)*N.x+dy(u)*N.y)));
294
real errv=1;
real erradap=0.001;
verbosity=1;
while(errv>1e-6)
{
j++;
Pu;
Pv;
plot(Th,u,v ,wait=0);
errv=int1d(Th,f)(v*v);
real coef=1;
//
real mintcc = checkmovemesh(Th,[x,y])/5.;
real mint = checkmovemesh(Th,[x,y-v*coef]);
if (mint<mintcc || j%10==0) {
Th=adaptmesh(Th,u,err=erradap ) ;
mintcc = checkmovemesh(Th,[x,y])/5.;
}
//
while (1)
{
real mint = checkmovemesh(Th,[x,y-v*coef]);
if (mint>mintcc) break;
cout << " min |T]
coef /= 1.5;
}
Th=movemesh(Th,[x,y-coef*v]);
//
calcul de la deformation
cout << "\n\n"<<j <<"------------ errv = " << errv << "\n\n";
}
plot(Th,ps="d_Thf.eps");
plot(u,wait=1,ps="d_u.eps");
295
9.12
The nonlinear elasticity problem is: find the displacement (u1 , u2 ) minimizing J
Z
Z
Pa u2
f (F 2)
min J(u1 , u2 ) =
p
where F 2(u1 , u2 ) = A(E[u1 , u2 ], E[u1 , u2 ]) and A(X, Y ) is bilinear sym. positive form with respect
two matrix X, Y . where f is a given C 2 function, and E[u1 , u2 ] = (Eij )i=1,2, j=1,2 is the Green-Saint
Venant deformation tensor defined with:
X
Eij = 0.5 (i uj + j ui ) +
i uk j uk
k
Pa v2
//
//
EOM ENL
296
(dy(u)*dy(uu)+dy(v)*dy(vv)) ]
macro
macro
macro
macro
macro
macro
//
E(u,v) (EL(u,v)+ENL(u,v))
dE(u,v,uu,vv) (EL(uu,vv)+dENL(u,v,uu,vv))
ddE(u,v,uu,vv,uuu,vvv) dENL(uuu,vvv,uu,vv)
F2(u,v) (E(u,v)*A*E(u,v))
dF2(u,v,uu,vv) (E(u,v)*A*dE(u,v,uu,vv)*2. )
ddF2(u,v,uu,vv,uuu,vvv) (
(dE(u,v,uu,vv)*A*dE(u,v,uuu,vvv))*2.
+ (E(u,v)*A*ddE(u,v,uu,vv,uuu,vvv))*2. )
//
//
EOM
un = un du,
w, s
vn = vn dv
The way to implement this algorithm in FreeFem++ is use a macro tool to implement A and F 2,
f , f 0 ,f 00 .
A macro is like in ccp preprocessor of C++ , but this begin by macro and the end of the macro
definition is before the comment //. In this case the macro is very useful because the type of
parameter can be change. And it is easy to make automatic differentiation.
//
//
//
macro f(u) (u)
macro df(u) (1)
macro ddf(u) (0)
//
297
real mu = 0.012e5;
real lambda = 0.4e5;
//
//
= 2E + tr(E)Id
//
A(u, v) = (u) : E(v)
//
//
( a b )
//
( b c )
//
//
tr*Id : (a,b,c) -> (a+c,0,a+c)
//
so the associed matrix is:
//
( 1 0 1 )
//
( 0 0 0 )
//
( 1 0 1 )
//
//
//
real a11= 2*mu +
real a22= mu ;
lambda
a33=
a12=
a13=
a23=
------------------v
;
//
real
real
real
real
kg/cm2
kg/cm2
//
because [0, 2 t12, 0]0 A[0, 2 s12, 0] =
= 2 mu (t12 s12 + t21 s21) = 4 mu t12 s12
2*mu +
lambda ;
0 ;
lambda ;
0 ;
//
symetric part
//
the matrix A.
//
a pressure of 100 Pa
plot(Th);
fespace Wh(Th,P1dc);
fespace Vh(Th,[P1,P1]);
fespace Sh(Th,P1);
Wh e2,fe2,dfe2,ddfe2;
Wh ett,ezz,err,erz;
Vh [uu,vv], [w,s],[un,vn];
[un,vn]=[0,0];
[uu,vv]=[0,0];
//
//
//
optimisation
optimisation
intialisation
298
9.13
//
9.13.1
Notation
In what follows, the symbols u, F, B, C, denote, respectively, the displacement field, the deformation gradient, the left Cauchy-Green strain tensor B = FFT , the right Cauchy-Green strain tensor
C = FT F, and the Cauchy stress tensor. We also introduce the symbols I1 := tr C and J := det F.
Use will be made of the identity
J
= JC1
(9.59)
C
The symbol I denotes the identity tensor. The symbol 0 denotes the reference configuration of the
body to be deformed. The unit volume in the reference (resp., deformed) configuration is denoted
dV (resp., dV0 ); these two are related by
dV = JdV0 ,
which allows an integral over involving the Cauchy stress T to be rewritten as an integral of the
Kirchhoff stress = JT over 0 .
Recommended References
For an exposition of nonlinear elasticity and of the underlying linear- and tensor algebra, see [39].
For an advanced mathematical analysis of the Finite Element Method, see [40]. An explanation
of the Finite Element formulation of a nonlinear elastostatic boundary value problem, see http://
www.engin.brown.edu/courses/en222/Notes/FEMfinitestrain/FEMfinitestrain.
htm.
9.13.2
Constitutive Theory and Tangent Stress Measures The strain energy density function is
given by
W =
(I1 tr I 2 ln J)
2
(9.60)
W
(Fn ) = (I C1 )
E
(9.61)
(9.62)
(9.63)
The Weak Form of the BVP in the Absence of Body (External) Forces The 0 we are
considering is an elliptical annulus, whose boundary consists of two concentric ellipses (each allowed
to be a circle as a special case), with the major axes parallel. Let P denote the dead stress load
(traction) on a portion t0 (= the inner ellipse) of the boundary 0 . On the rest of the boundary,
we prescribe zero displacement.
300
For brevity, in the rest of this section we assume P = 0. The provided FreeFem++ code, however,
does not rely on this assumption and allows for a general value and direction of P .
Given a Newton approximation un of the displacement field u satisfying the BVP, we seek the
correction un+1 to obtain a better approximation
un+1 = un + un+1
by solving the weak formulation
R
R
1
0 = 0 [F
+
F
]
:
(
w)(F
+
F
)
n
n+1
n
n+1
0 P N0
o
R n
= 0 [Fn ] + F
[Fn ]Fn+1 : ( w)(Fn + Fn+1 )1
n
o
R
2
= 0 [Fn ] + F
[Fn ]Fn+1 : ( w)(F1
n + Fn Fn+1 )
R
= 0 [Fn ] : ( w)F1
n
R
2
0 [F
n ] : ( ow)(Fn Fn+1 )
n
R
(9.64)
9.13.3
[Fn ]Fn+1
(9.65)
< T anK >:=
F
will be implemented as the macros < T anK11 >, < T anK12 >, . . ..
The individual components of the tensor quantities
D1 := Fn (Fn+1 )T + Fn+1 (Fn )T ,
D2 := FT
n Fn+1 ,
D3 := ( w)F2
n Fn+1 ,
and
D4 := ( w)F1
n ,
will be implemented as the macros
< d1Aux11 >, < d1Aux12 >,
< d2Aux11 >, < d2Aux12 >,
< d3Aux11 >, < d3Aux12 >,
< d4Aux11 >, < d4Aux12 >,
...
...
...
...
(9.66)
301
respectively.
In the above notation, the tangent Kirchhoff stress term becomes
(Fn ) Fn+1 = D1
F
while the weak BVP formulation acquires the form
R
0 = 0 [Fn ] : D4
R
0 [F
n ] : D3
o
R n
+ 0 F [Fn ]Fn+1 : D4
9.14
(9.67)
(9.68)
Written by I. S. Grudinin.
In whispering gallery mode (WGM) resonators, which are typically spheres or disks, electromagnetic
field is trapped by total internal reflections from the boundary. Modes of such resonators are
distinguished by compact volume and record high quality factors (Q) in a broad range of frequencies.
Modern applications of such resonators include microwave and optical cavities for atomic clocks,
cavity optomechanics, nonlinear and quantum optics. Analytical solutions for WG modes are only
available for a limited number of idealized geometries, such as sphere or ellipsoid. Since resonator
dimensions are typically much larger than optical wavelength, direct application of numerical 3D
finite difference time domain (FDTD) or finite element methods (FEM) is not practical. Its
possible to solve the vectorial wave equation by reducing it to a two dimensional case by taking
axial symmetry into account. Such reduction leads to a system of 3 equations to be solved in a
2D z section of a resonator. Please refer to [51] for a detailed derivation and to [52] for an
example of using FreeFem++ to compute WGMs.
9.14.1
Since electric field is discontinuous on the surface of a dielectric and magnetic field is typically not,
we derive our equations for the magnetic field. The electric field can be easily derived at a later
stage from E = i 0 1 H. Following a standard procedure starting with Maxwell equations
we derive a wave equation in a single-axis anisotropic medium such as an optical crystal:
1 H k02 H ( H) = 0
(9.69)
Here k0 = /c is the wavenumber, is the penalty term added to fight spurious FEM solutions.
For anisotropic single-axis medium with
/ = 0 in cylindrical system of coordinates we have:
0 0
= 0 0 .
0 0 z
We now assume axial symmetry of our electromagnetic fields and insert an imaginary unity in front
of the H to allow all field components to be real numbers and also to account for the phase shift
of this component H(, , z) = {H (, z), iH (, z), Hz (, z)} eim .
We write the wave equation (9.69) explicitly in cylindrical coordinates, thus obtaining a set of
three differential equations for the domain given by the resonators cross section and some space
302
outside:
A1 {Ht , Ht , Hzt } = 0
A2 {Ht , Ht , Hzt }
A3 {Ht , Ht , Hzt }
(9.70)
= 0
= 0
The numerical solutions of these equations and boundary conditions can be found with FreeFem++
if we write the system in the weak, or integral form.
9.15
Weak formulation
In general, to obtain the integral or weak statements equivalent to system (9.70) and boundary
conditions we form a scalar dot product between an arbitrary magnetic field test function H t =
{Ht , Ht , Hzt } and the components of our vectorial equation A1 , A2 , A3 , and integrate over the
resonators cross section domain (and its boundary for the boundary conditions):
Z
(Ht A1 + Ht A2 + Hzt A3 )d
(9.71)
We can reduce the order of partial derivatives in this integral by using the Greens formula for
integration by parts. For example:
Z
Z
I
2
Hzt Hz
Hz
t Hz
Hz
d =
d + Hzt
n d
(9.72)
2
Thus converting equations (9.70) we obtain a large expression for the weak form (see [1])
9.15.1
We now compute the fundamental mode frequency for a fused silica sphere. The sphere is 36
micrometer in diameter, the refractive index is 1.46, the boundary condition is the magnetic
wall (which can actually be omitted as it holds automatically). The example can be found in
examples++-eigenvalue/WGM-sphere.edp in the distribution.
Chapter 10
MPI Parallel version
A first attempt of parallelization of FreeFem++ is made here with mpi. An extended interface
with MPI has been added to FreeFem++ version 3.5, (see the MPI documentation for the functionality of the language at http://www.mpi-forum.org/docs/mpi21-report.pdf).
10.1
MPI keywords
10.2
MPI constants
10.3
MPI Constructor
int[int] proc1=[1,2,3],proc2=[0,4];
mpiGroup grp(procs);
//
set MPI Group to proc 1,2,3 in MPI COMM WORLD
303
304
//
//
10.4
comm=mpiCommWorld;
ncomm(mpiCommWorld,grp);
//
MPI functions
mpiSize(comm) ;
mpiRank(comm) ;
//
//
processor(i)
//
return processor i with no Resquest in MPI_COMM_WORLD
processor(mpiAnySource)
//
return processor any source
//
with no Resquest in MPI_COMM_WORLD
processor(i,comm)
//
return processor i with no Resquest in comm
processor(comm,i)
//
return processor i with no Resquest in comm
processor(i,rq,comm)
//
return processor i with Resquest rq in comm
processor(i,rq)
//
return processor i with Resquest rq in
//
MPI_COMM_WORLD
processorblock(i)
//
return processor i in MPI_COMM_WORLD
//
in block mode for synchronously communication
processorblock(mpiAnySource)
//
return processor any source
//
in MPI_COMM_WORLD in block mode for synchronously communication
processorblock(i,comm)
//
return processor i in in comm in block mode
mpiBarrier(comm) ;
mpiWait(rq);
mpiWaitAll(arq);
mpiWtime() ;
mpiWtick() ;
//
where a processor is just a integer rank, pointer to a MPI_comm and pointer to a MPI_Request,
and processorblock with a special MPI_Request.
10.5
int status;
//
to get the MPI status of send / recv
processor(10) << a << b;
//
send a,b asynchronously to the process 1,
processor(10) >> a >> b; //
receive a,b synchronously from the process 10,
broadcast(processor(10,comm),a);
//
broadcast from processor
305
//
where the data type of a can be of type of int,real, complex, int[int], double[int],
complex[int], int[int,int], double[int,int], complex[int,int], mesh, mesh3,
mesh[int], mesh3[int], matrix, matrix<complex>
processor(10,rq) << a ;
processor(10,rq) >> a ;
//
//
send asynchronously to
//
the data
receive asynchronously from
//
the data
the process 10
a with request
the process 10
a with request
If a,b are arrays or full matrices of int, real, or complex, we can use the following MPI functions:
mpiAlltoall(a,b[,comm]) ;
mpiAllgather(a,b[,comm]) ;
mpiGather(a,b,processor(..) ) ;
mpiScatter(a,b,processor(..)) ;
mpiReduce(a,b,processor(..),mpiMAX) ;
mpiAllReduce(a,b,comm, mpiMAX) ;
mpiReduceScatter(a,b,comm, mpiMAX) ;
See the examples++-mpi/essai.edp to test of all this functionality and Thank, to Guy-Antoine
Atenekeng Kahou, for his help to code this interface.
10.6
//
//
//
//
//
//
//
306
//
//
if ( mpisize != 2 ) {
cout << " sorry, number of processors !=2 " << endl;
exit(1);}
verbosity=3;
int interior = 2;
int exterior = 1;
border a(t=1,2){x=t;y=0;label=exterior;};
border b(t=0,1){x=2;y=t;label=exterior;};
border c(t=2,0){x=t ;y=1;label=exterior;};
border d(t=1,0){x = 1-t; y = t;label=interior;};
border e(t=0, pi/2){ x= cos(t); y = sin(t);label=interior;};
border e1(t=pi/2, 2*pi){ x= cos(t); y = sin(t);label=exterior;};
int n=4;
mesh[int] Th(mpisize);
if (mpirank == 0)
Th[0] = buildmesh( a(5*n) + b(5*n) + c(10*n) + d(5*n));
else
Th[1] = buildmesh ( e(5*n) + e1(25*n) );
broadcast(processor(0),Th[0]);
broadcast(processor(1),Th[1]);
fespace Vh(Th[mpirank],P1);
fespace Vhother(Th[1-mpirank],P1);
Vh u=0,v;
Vhother U=0;
int i=0;
problem pb(u,v,init=i,solver=Cholesky) =
int2d(Th[mpirank])( dx(u)*dx(v)+dy(u)*dy(v) )
- int2d(Th[mpirank])( v)
+ on(interior,u = U) + on(exterior,u= 0 ) ;
for ( i=0 ;i< 20; i++)
{
cout << mpirank << " looP " << i << endl;
pb;
//
send u to the other proc, receive in U
processor(1-mpirank) << u[];
processor(1-mpirank) >> U[];
real err0,err1;
err0 = int1d(Th[mpirank],interior)(square(U-u)) ;
//
send err0 to the other proc, receive in err1
processor(1-mpirank)<<err0;
processor(1-mpirank)>>err1;
real err= sqrt(err0+err1);
cout <<" err = " << err << " err0 = " << err0
<< ", err1 = " << err1 << endl;
if(err<1e-3) break;
};
if (mpirank==0)
plot(u,U,ps="uU.eps");
10.6.1
307
This is a explanation of the two script examples++-mpi/MPIGMRES[2]D.edp, a Schwarz parallel with a complexity almost independent of the number of process (with a coarse grid preconditioner).
Thank to F. Nataf.
To solve the following Poisson problem on domain with boundary in L2 () :
u = f, in , and u = g on ,
1
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
(10.1)
(10.2)
After discretization with the Lagrange finite element method, with a compatible mesh Thi of i , i.
e., the exist a global mesh Th such that Thi is include in Th . Let us denote:
Vhi the finite element space corresponding to domain i ,
Nhi is the set of the degree of freedom ik ,
i
is the set of the degree of freedom of Vhi on the boundary i of i ,
Nhi
ik (vh ) = 0},
Remark we never use finite element space associated to the full domain because it to expensive.
We have to defined to operator to build the previous algorithm:
We denote u`h|i the restriction of u`h on Vhi , so the discrete problem on i of problem (10.1) is find
i
.
u`hi Vhi such that: where gik is the value of g associated to the degree of freedom k Nhi
`
In FreeFem++ , it can be written has with U is the vector corresponding to uh|i and the vector
U1 is the vector corresponding to u`hi is the solution of:
real[int] U1(Ui.n);
308
real[int] b= onG .* U;
b = onG ? b : Bi ;
U1 = Ai-1*b;
where onG[i] = (i i \ )?1 : 0, and Bi the right of side of the problem, are defined by
fespace Whi(Thi,P2);
//
def of the Finite element space.
varf vPb(U,V)= int3d(Thi)(grad(U)*grad(V)) + int3d(Thi)(F*V) +on(1,U=g) + on(10,U=G);
varf vPbon(U,V)=on(10,U=1)+on(1,U=0);
matrix Ai = vPb(Whi,Whi,solver=sparsesolver);
real[int] onG = vPbon(0,Whi);
real[int] Bi=vPb(0,Whi);
//
//
U[j]=0;}
First gmres algorithm: you can easily accelerate the fixe point algorithm by using a parallel GMRES
algorithm after the introduction the following affine Ai operator sub domain i .
func real[int] DJ0(real[int]& U) {
real[int] V(U.n)
, b= onG .* U;
b = onG ? b : Bi ;
V = Ai-1*b;
Update(V,U);
309
return V; }
Where the parallel MPIGMRES or MPICG algorithm is just a simple way to solve in parallel the
following Ai xi = bi , i = 1, .., Np by just changing the dot product by reduce the local dot product
of all process with the following MPI code:
template<class R> R ReduceSum1(R s,MPI_Comm * comm)
{
R r=0;
MPI_Allreduce( &s, &r, 1 ,MPI_TYPE<R>::TYPE(),
return r; }
MPI_SUM,
*comm );
//
//
//
the preconditioner
//
//
interpolate(Whi,VhC);
//
the projection on coarse grid.
Pci*Pii;
//
the Restiction on Process i grid with the partition i
func bool
{
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;
}
310
//
//
//
//
-C2*Uo
U = (I-A C2) Uo
( C1( I -A C2) Uo
//
We have all ingredient to solve in parallel if we have et the partitions of the unity. To build this
partition we do: the initial step on process 1 tp build a coarse mesh, Th of the full domain, and
build the partition function constant equal to i on each sub domain Oi , i = 1, .., Np , of the grid
with the Metis graph partitioner [?] and on each process i in 1.., Np do
1. Broadcast from process 1, the mesh Th (call Thii in FreeFem++ script), and function,
2. remark that the characteristic function 1IOi of domain Oi , is defined by ( = i)?1 : 0,
311
3. let us call 2P (resp. 2V ) the L2 on Ph the space of the constant finite element function per
element on Th (resp. Vh the space of the affine continuous finite element per element on
Th ). and build in parallel the i and i , such that Oi i where Oi = supp((2V 2C )m 1IOi ),
and m is a the overlaps size on the coarse mesh (generally one),
(this is done in function AddLayers(Thii,suppii[],nlayer,phii[]); We choose a
function i = (21 20 )m 1IOi so the partition of the unity is simply defined by
i = PNpi
j=1 j
(10.3)
The set Ji of neighborhood of the domain i , and the local version on Vhi can be defined the
array jpart and njpart with:
Vhi pii=i ;
pij[j]
Vhi[int] pij(npij);
//
int[int] jpart(npart);
int njpart=0;
Vhi sumphi = i ;
for (int i=0;i<npart;++i)
if(i != ipart ) {
if(int3d(Thi)( j )>0) {
pij[njpart]=j ;
sumphi[] += pij[njpart][];
jpart[njpart++]=i;}}}
pii[]=pii[] ./ sumphi[];
for (int j=0;j<njpart;++j) pij[j][] = pij[j][] ./ sumphi[];
jpart.resize(njpart);
4. We call Th ij the sub mesh part of Thi where j are none zero. and tank to the function
trunc to build this array,
for(int jp=0;jp<njpart;++jp)
aThij[jp] = trunc(Thi,pij[jp]>1e-10,label=10);
5. At this step we have all on the coarse mesh , so we can build the fine final mesh by splitting
all meshes : Thi, Thij[j],Thij[j] with FreeFem++ trunc mesh function which do
restriction and slipping.
6. The construction of the send/recv matrices sMj and rMj : can done with this code:
mesh3 Thij=Thi;
//
variable meshes
fespace Whij(Thij,Pk);
//
variable fespace ..
matrix Pii; Whi wpii=pii; Pii = wpii[];
//
Diagonal matrix
corresponding i
matrix[int] sMj(njpart), rMj(njpart);
//
M send/rend case..
for(int jp=0;jp<njpart;++jp)
{ int j=jpart[jp];
Thij = aThij[jp];
//
change mesh to change Whij,Whij
matrix I = interpolate(Whij,Whi);
//
Whij <- Whi
sMj[jp] = I*Pii;
//
Whi -> s Whij
rMj[jp] = interpolate(Whij,Whi,t=1); }}
//
Whij -> Whi
312
To buil a not to bad application, I have add code tout change variable from parametre value with
the following code
include "getARGV.idp"
verbosity=getARGV("-vv",0);
int vdebug=getARGV("-d",1);
int ksplit=getARGV("-k",10);
int nloc = getARGV("-n",25);
string sff=getARGV("-p,","");
int gmres=getARGV("-gmres",3);
bool dplot=getARGV("-dp",0);
int nC = getARGV("-N" ,max(nloc/10,4));
And small include to make graphic in parallel of distribute solution of vector u on mesh Th with
the following interface:
include "MPIplot.idp"
func bool plotMPIall(mesh &Th,real[int] & u,string cm)
{ PLOTMPIALL(mesh,Pk, Th, u,{ cmm=cm,nbiso=20,fill=1,dim=3,value=1}); return 1;}
remark the {cmm=cm, ... =1} in the macro argument is a way to quote macro argument so
the argument is cmm=cm, ... =1.
Chapter 11
Parallel sparse solvers
Parallel sparse solvers use several processors to solve linear systems of equation. Like sequential,
parallel linear solvers can be direct or iterative. In FreeFem++ both are available.
11.1
We recall that the solver parameters are defined in the following commands: solve, problem,
set (setting parameter of a matrix) and in the construction of the matrix corresponding to a
bilinear form. In these commands, the parameter solver must be set to sparsesolver for
parallel sparse solver. We have added specify parameters to these command lines for parallel
sparse solvers. These are
lparams: vector of integer parameters (l is for the c++ type long)
dparams: vector of real parameters
sparams: string parameters
datafilename: name of the file which contains solver parameters
The following four parameters are only for direct solvers and are vectors. These parameters allow the
user to preprocess the matrix (see the section on sparse direct solver above for more information).
permr: row permutation (integer vector)
permc: column permutation or inverse row permutation (integer vector)
scaler: row scaling (real vector)
scalec: column scaling (real vector)
There are two possibilities to control solver parameters. The first method defines parameters with
lparams, dparams and sparams in .edp file. The second one reads the solver parameters from a
data file. The name of this file is specified by datafilename. If lparams, dparams, sparams
or datafilename is not provided by the user, the solvers default value is used.
To use parallel solver in FreeFem++ , we need to load the dynamic library corresponding to this
solver. For example to use MUMPS solver as parallel solver in FreeFem, write in the .edp file load
MUMPS FreeFem.
If the libraries are not loaded, the default sparse solver will be loaded (default sparse solver is
UMFPACK). The table 11.1 gives this new value for the different libraries.
313
314
1],
10 , 10],
0, 2],
22, 0.],
10,
2,
315
0., 22]];
defaulttoMUMPS();
//
default solver :
{
matrix A =
[[ 1, 2,
2, 1,
[ 2,
12,
0,
[ 2,
0,
1,
[ 1,
10,
0,
[ 1,
10,
2,
1],
10 , 10],
0, 2],
22, 0.],
0., 22]];
316
11.2
In this section, we present the sparse direct solvers interfaced with FreeFem++ .
11.2.1
MUMPS solver
MUltifrontal Massively Parallel Solver (MUMPS) is a free library [?, ?, ?]. This package solves
linear system of the form A x = b where A is a square sparse matrix with a direct method. The
square matrix considered in MUMPS can be either unsymmetric, symmetric positive definite or
general symmetric. The method implemented in MUMPS is a direct method based on a multifrontal
approach [?]. It constructs a direct factorization A = L U , A = Lt D L depending of the symmetry
of the matrix A. MUMPS uses the following libraries : BLAS[?, ?], BLACS and ScaLAPACK[?].
Remark 7 MUMPS does not solve linear system with a rectangular matrix.
Installation of MUMPS To used MUMPS in FreeFem++ , you have to install the MUMPS
package into your computer. MUMPS is written in Fortran 90. The parallel version is constructed
using MPI [?] for message passing and BLAS [?, ?], BLACS and ScaLAPACK[?]. Therefore, a
fortran compiler is needed, and MPI, BLAS, BLACS and ScaLAPACK . An installation procedure
to obtain this package is given in the file README COMPILE in the directory src/solver of
FreeFem++ .
Creating Library of MUMPS interface for FreeFem++ : The MUMPS interface for FreeFem++
is given in file MUMPS freefem.cpp (directory src/solver/ ). This interface works with the release
3.8.3 and 3.8.4 of MUMPS. To used MUMPS in FreeFem++ , we need the library corresponding
to this interface. A description to obtain this library is given in the file README COMPILE in the
directory src/solver of FreeFem++ . We recall here the procedure. Go to the directory src/solver
in FreeFem++ package. Edit the file makefile-sparsesolver.inc to yours system: comment Section
1, comment line corresponding to libraries BLAS, BLACS, ScaLAPACK, Metis, scotch in Section
2 and comment in Section 3 the paragraph corresponding to MUMPS solver. And then type make
mumps in a terminal window.
Now we give a short description of MUMPS parameters before describing the method to call
MUMPS in FreeFem++ .
317
MUMPS parameters:
There are four input parameters in MUMPS (see [?]). Two integers
SYM and PAR, a vector of integer of size 40 INCTL and a vector of real of size 15 CNTL. The first
parameter gives the type of the matrix: 0 for unsymmetric matrix, 1 for symmetric positive matrix
and 2 for general symmetric. The second parameter defined if the host processor work during the
factorization and solves steps : PAR=1 host processor working and PAR=0 host processor not
working. The parameter INCTL and CNTL is the control parameter of MUMPS. The vectors
ICNTL and CNTL in MUMPS becomes with index 1 like vector in fortran. A short description of
all parameters of ICNTL and CNTL is given in ffmumps fileparam.txt. For more details see the
users guide [?].
We describe now some elements of the main parameters of ICNTL for MUMPS.
Input matrix parameter The input matrix is controlled by parameters ICNTL(5) and ICNTL(18). The matrix format (resp. matrix pattern and matrix entries) are controlled by INCTL(5)
(resp. INCTL(18)). The different values of ICNTL(5) are 0 for assembled format and 1 for element
format. In the current release of Freefem++, we consider that FE matrix or matrix is storage in
assembled format. Therefore, INCTL(5) is treated as 0 value. The main option for ICNTL(18):
INCLTL(18)=0 centrally on the host processor, ICNTL(18)=3 distributed the input matrix pattern
and the entries (recommended option for distributed matrix by developer of MUMPS). For other
values of ICNTL(18) see the users guide of MUMPS. These values can be used also in FreeFem++
.
The default option implemented in FreeFem++ are ICNTL(5)=0 and ICNTL(18)=0.
Preprocessing parameter The preprocessed matrix Ap that will be effectively factored is defined
by
Ap = P Dr A Qc Dc P t
where P is the permutation matrix, Qc is the column permutation, Dr and Dc are diagonal matrix
for respectively row and column scaling. The ordering strategy to obtain P is controlled by parameter ICNTL(7). The permutation of zero free diagonal Qc is controlled by parameter ICNTL(6).
The row and column scaling is controlled by parameter ICNTL(18). These option are connected
and also strongly related with ICNTL(12) (see documentation of mumps for more details [?]). The
parameters permr, scaler, and scalec in FreeFem++ allow to give permutation matrix(P ), row
scaling (Dr ) and column scaling (Dc ) of the user respectively.
To call MUMPS in FreeFem++ , we need to load the dynamic library MUMPS freefem.dylib (MacOSX), MUMPS freefem.so (Unix) or MUMPS freefem.dll
(Windows). This is done in typing load MUMPS freefem in the .edp file. We give now the two
methods to give the option of MUMPS solver in FreeFem++ .
Solver parameters is defined in .edp file: In this method, we need to give the parameters
lparams and dparams. These parameters are defined for MUMPS by
i = 1, . . . , 40,
lparams[0] = SYM,
lparams[1] = PAR,
lparams[i+1] = ICNTL(i).
i = 1, . . . , 15,
dparams[i 1] = CNTL(i).
318
Reading solver parameters on a file: The structure of data file for MUMPS in FreeFem++ is :
first line parameter SYM and second line parameter PAR and in the following line the different value
of vectors ICNTL and CNTL. An example of this parameter file is given in ffmumpsfileparam.txt.
0
/* SYM :: 0 for non symmetric matrix, 1 for symmetric definite positive
matrix and 2 general symmetric matrix*/
1
/* PAR :: 0 host not working during factorization and solves steps, 1
host working during factorization and solves steps*/
-1
/* ICNTL(1) :: output stream for error message */
-1
/* ICNTL(2) :: output for diagnostic printing, statics and warning message
*/
-1
/* ICNTL(3) :: for global information */
0
/* ICNTL(4) :: Level of printing for error, warning and diagnostic message
/
*
0
/* ICNTL(5) :: matrix format : 0 assembled format, 1 elemental format.
/
*
7
/* ICNTL(6) :: control option for permuting and/or scaling the matrix
in analysis phase */
3
/* ICNTL(7) :: pivot order strategy : AMD, AMF, metis, pord scotch*/
77
/* ICNTL(8) :: Row and Column scaling strategy */
1
/* ICNTL(9) :: 0 solve Ax = b, 1 solve the transposed system At x =
b : parameter is not considered in the current release of freefem++*/
0
/* ICNTL(10) :: number of steps of iterative refinement */
0
/* ICNTL(11) :: statics related to linear system depending on ICNTL(9)
*/
1
/* ICNTL(12) :: constrained ordering strategy for general symmetric matrix
*/
0
/* ICNTL(13) :: method to control splitting of the root frontal matrix
*/
20
/* ICNTL(14) :: percentage increase in the estimated working space (default
20%)*/
0
/* ICNTL(15) :: not used in this release of MUMPS */
0
/* ICNTL(16) :: not used in this release of MUMPS */
0
/* ICNTL(17) :: not used in this release of MUMPS */
3
/* ICNTL(18) :: method for given : matrix pattern and matrix entries
: */
0
/* ICNTL(19) :: method to return the Schur complement matrix */
0
/* ICNTL(20) :: right hand side form ( 0 dense form, 1 sparse form) :
parameter will be set to 0 for freefem++ */
0
/* ICNTL(21) :: 0, 1 kept distributed solution : parameter is not considered
in the current release of freefem++ */
0
/* ICNTL(22) :: controls the in-core/out-of-core (OOC) facility */
0
/* ICNTL(23) :: maximum size of the working memory in Megabyte than MUMPS
can allocate per working processor */
0
/* ICNTL(24) :: control the detection of null pivot */
0
/* ICNTL(25) :: control the computation of a null space basis */
0
/* ICNTL(26) :: This parameter is only significant with Schur option
(ICNTL(19) not zero). : parameter is not considered in the current release of freefem++
*/
-8
/* ICNTL(27) (Experimental parameter subject to change in next release
of MUMPS) :: control the blocking factor for multiple righthand side during the
solution phase : parameter is not considered in the current release of freefem++
*/
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
319
example A simple example of calling MUMPS in FreeFem++ with this two methods is given in
the file testsolver MUMPS.edp in the directory examples++-mpi.
11.2.2
The package SuperLU DIST [?, ?] solves linear systems using LU factorization. It is a free scientific
library under BSD license. The web site of this project is http://crd.lbl.gov/xiaoye/SuperLU. This
library provides functions to handle square or rectangular matrix in real and complex arithmetics.
The method implemented in SuperLU DIST is a supernodal method [?]. New release of this package
includes a parallel symbolic factorization [?]. This scientific library is written in C and MPI for
communications.
Installation of SuperLU DIST: To use SuperLU DIST in FreeFem++ , you have to install
SuperLU DIST package. We need MPI and ParMetis library to do this compilation. An installation procedure to obtain this package is given in the file README COMPILE in the directory
src/solver/ of the FreeFem++ package.
320
Creating Library of SuperLU DIST interface for FreeFem++ : The FreeFem++ interface
to SuperLU DIST for real (resp. complex) arithmetics is given in file
real SuperLU DIST FreeFem.cpp (resp. complex SuperLU DIST FreeFem.cpp). These files are in
the directory src/solver/. These interfaces are compatible with the release 3.2.1 of SuperLU DIST.
To use SuperLU DIST in FreeFem++ , we need libraries corresponding to these interfaces. A
description to obtain these libraries is given in the file README COMPILE in the directory
src/solver of FreeFem++ . We recall here the procedure. Go to the directory src/solver in
FreeFem++ package. Edit the file makefile-sparsesolver.inc in your system : comment Section
1, comment line corresponding to libraries BLAS, Metis, ParMetis in Section 2 and comment in
Section 3 the paragraph corresponding to SuperLU DIST solver. And just type make rsludist (resp.
make csludist) in the terminal to obtain the dynamic library of interface for real (resp. complex)
arithmetics.
Now we give a short description of SuperLU DIST parameters before describing the method to call
SuperLU DIST in FreeFem++ .
SuperLU DIST parameters: We describe now some parameters of SuperLU DIST. The SuperLU DIST library use a 2D-logical process group. This process grid is specifies by nprow (process
row) and npcol (process column) such that Np = nprow npcol where Np is the number of all process
allocated for SuperLU DIST.
The input matrix parameters is controlled by matrix= in sparams for internal parameter or in
the third line of parameters file. The different value are
matrix = assembled
global matrix are available on all process
matrix = distributedglobal the global matrix is distributed among all the process
matrix = distributed
the input matrix is distributed (not yet implemented)
The option arguments of SuperLU DIST are described in the section Users-callable routine of [?].
The parameter Fact and TRANS are specified in FreeFem++ interfaces to SuperLU DIST during
the different steps. For this reason, the value given by the user for this option is not considered.
The factorization LU is calculated in SuperLU DIST on the matrix Ap .
Ap = Pc Pr Dr A Dc Pct
where Pc and Pr is the row and column permutation matrix respectively, Dr and Dc are diagonal
matrix for respectively row and column scaling. The option argument RowPerm (resp. ColPerm)
control the row (resp. column) permutation matrix. Dr and Dc is controlled by the parameter
DiagScale. The parameter permr, permc, scaler, and scalec in FreeFem++ is provided to give
row permutation, column permutation, row scaling and column scaling of the user respectively.
The other parameters for LU factorization are ParSymFact and ReplaceTinyPivot. The parallel
symbolic factorization works only on a power of two processes and need the ParMetis ordering [?].
The default option argument of SuperLU DIST are given in the file ffsuperlu dist fileparam.txt.
Solver parameters is defined in .edp file: To call SuperLU DIST with internal parameter, we
used the parameters sparams. The value of parameters of SuperLU DIST in sparams is defined by
321
sparams
Reading solver parameters on a file: The structure of data file for SuperLU DIST in FreeFem++
is given in the file ffsuperlu dist fileparam.txt (default value of the FreeFem++ interface).
1
/* nprow : integer value
*/
1
/* npcol : integer value
*/
distributedglobal
/* matrix input : assembled, distributedglobal, distributed
*/
DOFACT
/* Fact : DOFACT, SamePattern, SamePattern_SameRowPerm,
FACTORED */
NO
/* Equil : NO, YES */
NO
/* ParSymbFact : NO, YES */
MMD_AT_PLUS_A /* ColPerm : NATURAL, MMD_AT_PLUS_A, MMD_ATA, METIS_AT_PLUS_A, PARMETIS,
MY_PERMC */
LargeDiag
/* RowPerm : NOROWPERM, LargeDiag, MY_PERMR */
1.0
/* DiagPivotThresh : real value */
DOUBLE
/* IterRefine : NOREFINE, SINGLE, DOUBLE, EXTRA */
NOTRANS
/* Trans
: NOTRANS, TRANS, CONJ*/
NO
/* ReplaceTinyPivot : NO, YES*/
NO
/* SolveInitialized : NO, YES*/
NO
/* RefineInitialized : NO, YES*/
NO
/* PrintStat : NO, YES*/
NOEQUIL
/* DiagScale : NOEQUIL, ROW, COL, BOTH*/
11.2.3
Pastix solver
Pastix (Parallel Sparse matrix package) is a free scientific library under CECILL-C license. This
package solves sparse linear system with a direct and block ILU(k) iterative methods. This solver
can be applied to a real or complex matrix with a symmetric pattern [?].
Installation of Pastix: To used Pastix in FreeFem++ , you have to install pastix package in
first. To compile this package, we need a fortran 90 compiler, scotch [?] or Metis [?] ordering
library and MPI. An installation procedure to obtain this package is given in the file .src/solver/
README COMPILE in the section pastix of the FreeFem++ package.
322
Creating Library of pastix interface for FreeFem++ : The FreeFem++ interface to pastix
is given in file real pastix FreeFem.cpp (resp. complex pastix FreeFem.cpp) for real (resp.complex)
arithmetics. This interface is compatible with the release 2200 of pastix and is designed for a
global matrix. We have also implemented interface for distributed matrices. To use pastix in
FreeFem++ , we need the library corresponding to this interface. A description to obtain this
library is given in the file README COMPILE in the directory src/solver of FreeFem++ . We
recall here the procedure. Go to the directory src/solver in FreeFem++ package. Edit the file
makefile-sparsesolver.inc to yours system : comment Section 1, comment line corresponding to
libraries BLAS, METIS and SCOTCH in Section 2 and comment in Section 3 the paragraph
corresponding to pastix solver. And just type make rpastix (resp. make cpastix) in the terminal
to obtain the dynamic library of interface for real (resp. complex) arithmetics.
Now we give a short description of pastix parameters before describing the method to call pastix
in FreeFem++ .
Pastix parameters: The input matrix parameter of FreeFem++ depend on pastix interface.
matrix=assembled for non distributed matrix. It is the same parameter for SuperLU DIST. There
are four parameters in Pastix : iparm, dparm, perm and invp. These parameters are respectively
the integer parameters (vector of size 64), real parameters (vector of size 64), permutation matrix
and inverse permutation matrix respectively. iparm and dparm vectors are described in [?]. The
parameters permr and permc in FreeFem++ are provided to give permutation matrix and inverse
permutation matrix of the user respectively.
Solver parameters defined in .edp file: To call Pastix in FreeFem++ in this case, we need to
specify the parameters lparams and dparams. These parameters are defined by
i = 0, . . . , 63,
lparams[i] = iparm[i].
i = 0, . . . , 63,
dparams[i] = dparm[i].
Reading solver parameters on a file: The structure of data file for pastix parameters in
FreeFem++ is : first line structure parameters of the matrix and in the following line the value of
vectors iparm and dparm in this order.
assembled
iparm[0]
iparm[1]
...
...
iparm[63]
dparm[0]
dparm[1]
...
...
dparm[63]
An example of this file parameter is given in ffpastix iparm dparm.txt with a description of these
parameters. This file is obtained with the example file iparm.txt and dparm.txt including in the
square matrix
sym pattern unsym
yes
yes
yes
yes
yes
no
sym
yes
no
no
323
rectangular matrix
sym pattern unsym
yes
yes
no
no
no
no
Table 11.3: Type of matrix used by the different direct sparse solver
pastix package.
If no solver parameter is given, we use the default option of pastix solver.
Example: A simple example of calling pastix in FreeFem++ with this two methods is given in
the file testsolver pastix.edp in the directory examples++-mpi.
In Table 11.3, we recall the different matrix considering in the different direct solvers.
11.3
Concerning iterative solvers, we have chosen pARMS [?] , HIPS [?] and Hypre [?]. Each software
implements a different type of parallel preconditioner. So, pARMS implements algebraic domain
decomposition preconditioner type such as additive Schwartz [?] and interface method [?]; while
HIPS implement hierarchical incomplete factorization [?] and finally HYPRE implements multilevel
preconditioner are AMG(Algebraic MultiGrid) [?] and parallel approximated inverse [?].
To use one of these programs in FreeFem++ , you have to install it independently of FreeFem++
. It is also necessary to install the MPI communication library which is essential for communication
between the processors and, in some cases, software partitioning graphs like METIS [?] or Scotch
[?].
All this preconditioners are used with Krylov subspace methods accelerators. Krylov subspace
methods are iterative methods which consist in finding a solution x of linear system Ax = b inside
the affine space x0 + Km by imposing that b AxLm , where Km is Krylov subspace of dimension
m defined by Km = {r0 , Ar0 , A2 r0 , ..., Am1 r0 } and Lm is another subspace of dimension m which
depends on type of Krylov subspace. For example in GMRES, Lm = AKm .
We realized an interface which is easy to use, so that the call of these different softwares in
FreeFem++ is done in the same way. You just have to load the solver and then specify the
parameters to apply to the specific solvers. In the rest of this chapter, when we talk about Krylov
subspace methods we mean one among GMRES, CG and BICGSTAB.
11.3.1
pARMS solver
pARMS ( parallel Algebraic Multilevel Solver ) is a software developed by Youssef Saad and al
at University of Minnesota [?]. This software is specialized in the resolution of large sparse non
symmetric linear systems of equation. Solvers developed in pARMS is the Krylov subspace type. It
consists of variants of GMRES like FGMRES(Flexible GMRES) , DGMRES(Deflated GMRES) [?]
and BICGSTAB. pARMS also implements parallel preconditioner like RAS (Restricted Additive
Schwarz)[?] and Schur Complement type preconditioner [?].
324
All these parallel preconditioners are based on the principle of domain decomposition. Thus, the
matrix A is partitioned into sub matrices Ai (i = 1, ..., p) where p represents the number of partitions
one needs. The union of Ai forms the original matrix. The solution of the overall system is obtained
by solving the local systems on Ai (see [?]). Therefore, a distinction is made between iterations on A
and the local iterations on Ai . To solve the local problem on Ai there are several preconditioners as
ilut (Incomplete LU with threshold), iluk(Incomplete LU with level of fill in) and ARMS( Algebraic
Recursive Multilevel Solver). But to use pAMRS in FreeFem++ you have first to install pAMRS.
Installation of pARMS To install pARMS, you must first download the pARMS package at
[?]. Once the download is complete, you must unpack package pARMS and follow the installation
procedure described in file README to create the library libparms.a.
Using pARMS as interface to FreeFem++ Before calling pARMS solver inside FreeFem++
, you must compile file parms F reeF em.cpp to create a dynamic library parms F reeF em.so. To
do this, move to the directory src/solver of FreeFem++ , edit the file makef ileparms.inc to
specify the following variables:
P ARM S DIR :
Directory of pARMS
P ARM S IN CLU DE :
Directory for header of pARMS
M ET IS :
METIS directory
M ET IS LIB :
METIS librairy
MPI :
MPI directory
M P I IN CLU DE :
MPI headers
F REEF EM :
FreeFem++ directory
F REEF EM IN CLU DE : FreeFem++ header for sparse linear solver
LIBBLAS :
Blas library
After that, in the command line type make parms to create parms F reeF em.so.
As usual in FreeFem++ , we will show by examples how to call pARMS in FreeFem++ . There
are three ways of doing this:
Example 1: Default parameters This example comes from user guide of FreeFem++ [?] at
page 12.
Example 11.3
1: load parms_freefem
//
Tell FreeFem that you will use pARMS
2: border C(t=0,2*pi){x=cos(t); y=sin(t);label=1;}
3: mesh Th = buildmesh (C(50));
4: fespace Vh(Th,P2);
5: Vh u,v;
6: func f= x*y;
7: problem Poisson(u,v,solver=sparsesolver) =
//
bilinear part will use
8:
int2d(Th)(dx(u)*dx(v) + dy(u)*dy(v))
//
a sparse solver, in this
case pARMS
9:
- int2d(Th)( f*v)
//
right hand side
10:
+ on(1,u=0) ;
//
Dirichlet boundary condition
11:
12: real cpu=clock();
13: Poisson;
//
SOLVE THE PDE
14: plot(u);
15: cout << " CPU time = " << clock()-cpu << endl;
325
In line 1 of example 11.3 we load in memory the pARMS dynamic library with interface FreeFem++
. After this, in line 7 we specify that the bilinear form will be solved by the last sparse linear solver
load in memory which, in this case, is pARMS.
The parameter used in pARMS in this case is the default one since the user does not have to
provide any parameter.
Here are some default parameters:
solver=FGMRES, Krylov dimension=30, Maximum of Krylov=1000, Tolerance for convergence=1e
08.(see book of Saad [?] to understand all this parameters.)
preconditionner=Restricted Additif Schwarz [?], Inner Krylov dimension=5, Maximum of inner
Krylov dimension=5, Inner preconditionner=ILUK.
To specify the parameters to apply to the solver, the user can either give an integer vector for
integer parameters and real vectors for real parameters or provide a file which contains those
parameters.
Example 2: User specifies parameters inside two vectors Lets us consider Navier Stokes
example 11.4 . In this example we solve linear systems coming from discretization of Navier Stokes
equation with pARMS. Parameters of solver is specified by user.
We need two vectors to specify the parameters of the linear solver. In line 1 of example 11.4 we
have declared these vectors(int[int] iparm(16); real[int] dparm(6);) . In line 3 we have initialized
these vectors by negative values. We do this because all parameters values in pARMS are positive
and if you do not change the negative values of one entry of this vector, the default value will be
set. In tables (table 11.4 and 11.5) , we have the meaning of differents entries of these vectors.
We run example 11.4 on cluster paradent of Grid5000 and report results in table 11.8.
326
Entries of iparm
iparm[0]
iparm[1]
iparm[2]
iparm[3]
iparm[4]
iparm[5]
iparm[6]
iparm[7]
iparm[8]
iparm[9]
iparm[10]
iparm[11]
iparm[12]
iparm[13]
iparm[14]
iparm[15]
iparm[16]
327
328
matrix size
number of non null entries inside matrix
number of iteration for convergence
Time for convergence
Time for constructing finite element matrix
number of processor
Table 11.9: Legend of table 11.8
In this example, we fix the matrix size (in term of finite element, we fix the mesh) and increase the
number of processors used to solve the linear system. We saw that, when the number of processors
increases, the time for solving the linear equation decreases, even if the number of iteration increases.
This proves that, using pARMS as solver of linear systems coming from discretization of partial
differential equation in FreeFem++ can decrease drastically the total time of simulation.
11.3.2
HIPS ( Hierarchical Iterative Parallel Solver ) is a scientific library that provides an efficient parallel
iterative solver for very large sparse linear systems. HIPS is available as free software under the
CeCILL-C licence. The interface that we realized is compatible with release 1.2 beta.rc4 of HIPS.
HIPS implements two solver classes which are the iteratives class ( GMRES, PCG) and the Direct class. Concerning preconditionners, HIPS implements a type of multilevel ILU. For further
informations on those preconditionners see [?, ?].
Installation of HIPS To install HIPS, first download the HIPS package at [?], unpack it and
go to the HIPS source directory. The installation of HIPS is machine dependence. For example,
to install HIPS on a linux cluster copy the file M akef ile Inc Examples/makef ile.inc.gnu on the
root directory of HIPS with the name makefile.inc. After this, edit makefile.inc to set values of
different variables and type make all.
Using HIPS as the interface to FreeFem++ Before calling the HIPS solver inside FreeFem++
, you must compile file hips F reeF em.cpp to create dynamic library hips F reeF em.so. To do this,
move to the directory src/solver of FreeFem++ and edit the file makef ile.inc to specify the following variables:
HIP S DIR :
HIP S IN CLU DE:
LIB DIR :
LIBHIP SSEQU EN T IAL :
LIBHIP S :
F REEF EM :
F REEF EM IN CLU DE :
M ET IS :
M ET IS LIB :
MPI :
M P I IN CLU DE :
After specifies all the variables,
create hips F reeF em.so.
Directory of HIPS
-I$(HIP S DIR)/SRC/INCLUDE : Directory for HIPS headers
-L$(HIP S DIR)/LIB : Librairies directory
$(HIP S DIR)/LIB/libhipssequential.a: HIPS utilities library
$(HIP S DIR)/LIB/libhips.a: HIPS library
FreeFem++ directory
FreeFem headers for sparse linear solver
METIS directory
METIS library
MPI directory
MPI headers
in the command line in the directory src/solver type make hips to
329
Like with pARMS, the calling of HIPS in FreeFem++ can be done in three different manners. We
will present only one example where the user specifies the parameters through keywords lparams
and dparams.
Laplacian 3D solve with HIPS Let us consider the 3D Laplacian example inside FreeFem++
package where after discretization we want to solve the linear equation with Hips. Example 11.5 is
Laplacian3D using Hips as linear solver. We first load Hips solver at line 2. From line 4 to 15 we
specify the parameters for the Hips solver and in line 46 of example 11.5 we set these parameters
in the linear solver.
In Table 11.10 results of running example 11.5 on Cluster Paradent of Grid5000 are reported. We
can see in this running example the efficiency of parallelism.
Example 11.5 (Laplacian3D.edp) 1: load "msh3"
2: load "hips_FreeFem"
//
load library
3: int nn=10,iii;
4: int[int] iparm(14);
5: real[int] dparm(6);
6: for(iii=0;iii<14;iii++)iparm[iii]=-1;
7: for(iii=0;iii<6;iii++) dparm[iii]=-1;
8: iparm[0]=0;
//
use iterative solver
9: iparm[1]=1;
//
PCG as Krylov method
10:iparm[4]=0;
//
Matrix are symmetric
11:iparm[5]=1;
//
Pattern are also symmetric
12: iparm[9]=1;
//
Scale matrix
13:dparm[0]=1e-13;
//
Tolerance to convergence
14: dparm[1]=5e-4;
//
Threshold in ILUT
15: dparm[2]=5e-4;
//
Threshold for Schur preconditionner
16: mesh Th2=square(nn,nn);
17: fespace Vh2(Th2,P2);
18: Vh2 ux,uz,p2;
19: int[int] rup=[0,2], rdown=[0,1], rmid=[1,1,2,1,3,1,4,1];
20:real zmin=0,zmax=1;
21: mesh3 Th=buildlayers(Th2,nn,
zbound=[zmin,zmax],
reffacemid=rmid,
reffaceup = rup,
reffacelow = rdown);
22: savemesh(Th,"copie.mesh");
23: mesh3 Th3("copie.mesh");
24: fespace Vh(Th,P2);
25: func ue =
2*x*x + 3*y*y + 4*z*z + 5*x*y+6*x*z+1;
26: func uex=
4*x+ 5*y+6*z;
27: func uey=
6*y + 5*x;
28: func uez=
8*z +6*x;
29: func f= -18. ;
30: Vh uhe = ue;
//
31: cout << " uhe min: " << uhe[].min << " max:" << uhe[].max << endl;
32: Vh u,v;
33: macro Grad3(u) [dx(u),dy(u),dz(u)]
//
EOM
34: varf va(u,v)= int3d(Th)(Grad3(v) *Grad3(u))
//
) for emacs
+ int2d(Th,2)(u*v)
- int3d(Th)(f*v)
- int2d(Th,2) ( ue*v + (uex*N.x +uey*N.y +uez*N.z)*v )
+ on(1,u=ue);
330
Te=221.34
time
120.34
61.08
31.70
23.44
Table 11.10: Iterations and Timing of solving linear system from example 11.5
35:
36:
37:
38:
39:
40:
real cpu=clock();
matrix Aa;
Aa=va(Vh,Vh);
varf l(unused,v)=int3d(Th)(f*v);
Vh F; F[]=va(0,Vh);
if(mpirank==0){
cout << "Taille " << Aa.n << endl;
cout << "Non zeros " << Aa.nbcoef << endl;
}
41:
if(mpirank==0)
42:
cout << "CPU TIME FOR FORMING MATRIX = " << clock()-cpu << endl;
43:
set(Aa,solver=sparsesolver,dparams=dparm, lparams=iparm); //
Set hips
as linear solver
44:
u[]=Aa-1*F[];
11.3.3
Installation of HYPRE To install HYPRE, first download the HYPRE package at [?], unpack
it and go to the HYPRE/src source directory and do ./configure to configure Hypre. After this
just type make all to create libHYPRE.a.
Entries of iparm
iparm[0]
iparm[1]
iparm[2]
iparm[3]
iparm[4]
iparm[5]
iparm[6]
iparm[7]
iparm[8]
iparm[9]
iparm[10]
iparm[11]
iparm[12]
iparm[13]
iparm[14]
331
332
Using HYPRE as interface to FreeFem++ Before calling HYPRE solver inside FreeFem++
, you must compile the file hypre F reeF em.cpp to create dynamic library hypre F reeF em.so. To
do this, move to the directory src/solver of FreeFem++ , edit the file makef ile.inc to specify
the following variables:
HY P RE DIR :
Directory of HYPRE
HY P RE IN CLU DE =
-I$(HY P RE DIR)src/hypre/include/ :
Directory for header of HYPRE
HY P RE LIB =
-L$(HIP S DIR)/src/lib/ -lHYPRE : Hypre Library
F REEF EM :
FreeFem++ directory
F REEF EM IN CLU DE : FreeFem header for sparse linear solver
M ET IS :
METIS directory
M ET IS LIB :
METIS library
MPI :
MPI directory
M P I IN CLU DE :
MPI headers
Like with pARMS, the calling of HIPS in FreeFem++ can be done in three manners. We will
present only one example where the user specifies its parameters through keywords lparams and
dparams.
Laplacian 3D solve with HYPRE Let us consider again the 3D Laplacian example inside
FreeFem++ package where after discretization we want to solve the linear equation with Hypre.
Example 11.6 is the Laplacian3D using Hypre as linear solver. Example 11.6 is the same as 11.5,
so we just show here the lines where we set some Hypre parameters.
We first load the Hypre solver at line 2. From line 4 to 15 we specifies the parameters to set to
Hypre solver and in line 43 we set parameters to Hypre solver.
It should be noted that the meaning of the entries of these vectors is different from those of Hips .
In the case of HYPRE, the meaning of differents entries of vectors iparm and dparm are given in
tables 11.13 to 11.17.
In Table ?? the results of running example 11.6 on Cluster Paradent of Grid5000 are reported. We
can see in this running example the efficiency of parallelism, in particular when AMG are use as
preconditioner.
333
set(Aa,solver=sparsesolver,dparams=dparm, lparams=iparm);
Solver identification:
0: BiCGStab, 1: GMRES, 2: PCG. By default=1
Preconditioner identification:
iparms[1]
0: BOOMER AMG, 1: PILUT, 2: Parasails, 3: Schwartz Default=0
iparms[2] Maximum of iteration: Default=1000
iparms[3] Krylov subspace dim: Default= 40
iparms[4] Solver print info level: Default=2
iparms[5] Solver log : Default=1
iparms[6] Solver stopping criteria only for BiCGStab : Default=1
dparms[0] Tolerance for convergence : Def ault = 1.0e 11
iparms[0]
Table 11.13: Definitions of common entries of iparms and dparms vectors for every preconditioner in HYPRE
11.3.4
Conclusion
With the different runs presented here, we wanted to illustrate the gain in time when we increase
the number of processors used for the simulations. We saw that in every case the time for the
construction of the finite element matrix is constant. This is normal because until now this phase
is sequential in FreeFem++ . In contrast, phases for solving the linear system are parallel. We
saw on several examples presented here that when we increase the number of processors, in general
we decrease the time used for solving the linear systems. But this not true in every case. In several
case, when we increase the number of processors the time to convergence also increases. There are
two main reasons for this. First, the increase of processors can lead to the increase of volume of
exchanged data across processors consequently increasing the time for solving the linear systems.
Furthermore, in decomposition domain type preconditioners, the number of processors generally
corresponds to the number of sub domains. In subdomain methods, generally when we increase the
number of subdomains we decrease convergence quality of the preconditioner. This can increase
the time used for solving linear equations.
To end this, we should note that good use of the preconditioners interfaced in FreeFem++ is
empiric, because it is difficult to know what is a good preconditioner for some type of problems.
Although, the efficiency of preconditioners sometimes depends on how its parameters are set. For
this reason we advise the user to pay attention to the meaning of the parameters in the user guide
of the iterative solvers interfaced in FreeFem++ .
11.4
Domain decomposition
In the previous section, we saw that the phases to construct a matrix are sequential. One strategy
to construct the matrix in parallel is to divide geometrically the domain into subdomains. In every
subdomain we construct a local submatrix and after that we assemble every submatrix to form the
global matrix.
We can use this technique to solve pde directly in domain . In this case, in every subdomains
you have to define artificial boundary conditions to form consistent equations in every subdomains.
After this, you solve equation in every subdomains and define a strategy to obtain the global
solution.
334
iparms[7]
iparms[8]
iparms[9]
iparms[10]
iparms[11]
iparms[12]
iparms[13]
iparms[14]
iparms[15]
iparms[16]
iparms[17]
iparms[18]
iparms[19]
dparms[1]
dparms[2]
dparms[3]
dparms[3]
Table 11.14: Definitions of other entries of iparms and dparms if preconditioner is BOOMER
AMG
335
iparms[7]
n=4 106
np
8
16
32
64
Table 11.18: Convergence and time for solving linear system from example 11.4
336
In terms of parallel programming for FreeFem++ , with MPI, this means that the user must be
able to divide processors avaible for computation into subgroups of processors and also must be
able to realize different type of communications in FreeFem++ script. Here is a wrapper of some
MPI functions.
11.4.1
Groups
mpiGroup grpe(mpiGroup gp,KN < long >): Create M P I Group from existing group gp by
given vector
Communicators
Communicators is an abstract MPI object which allows MPI user to communicate across group of
processors. Communicators can be Intracommunicators(involves a single group) or Intercommunicators
(involves two groups). When we not specify type of communicator it will be Intracommunicators
mpiComm cc(mpiComm comm, mpiGroup gp): Creates a new communicator. comm communicator(handle), gp group which is a subset of the group of comm (handle). Return new communicator
mpiComm cc(mpiGroup gp): Same as previous constructor but default comm here is MPI COMM WORLD.
mpiComm cc(mpiComm comm, int color, int key): Creates new communicators based on colors
and key. This constructor is based on MPI Comm split routine of MPI.
mpiComm cc(MPIrank p,int key): Same constructor than the last one. Here colors and comm is
defined in MPIrank. This constructor is based on MPI Comm split routine of MPI.
Example 11.7 (commsplit.edp) 1: int color=mpiRank(comm)%2;
2: mpiComm ccc(processor(color,comm),0);
3: mpiComm qpp(comm,);
4: mpiComm cp(cc,color,0);
mpiComm cc(mpiComm comm, int high): Creates an intracommunicator from an intercommunicator. comm intercommunicator, high Used to order the groups within comm (logical) when creating
the new communicator. This constructor is based on MPI Intercomm merge routine of MPI.
mpiComm cc(MPIrank p1, MPIrank p2, int tag): This constructor creates an intercommuncator
from two intracommunicators. p1 defined local (intra)communicator and rank in local comm of
leader (often 0) while p2 defined remote communicator and rank in peer comm of remote leader
(often 0). tag Message tag to use in constructing intercommunicator. This constructor is based on
MPI Intercomm create.
int color=mpiRank(comm)%2;
int rk=mpiRank(comm);
int size=mpiSize(comm);
cout << "Color values " << color << endl;
mpiComm ccc(processor((rk<size/2),comm),rk);
mpiComm cp(cc,color,0);
int rleader;
if (rk == 0) { rleader = size/2; }
else if (rk == size/2) { rleader = 0;}
337
11.4.2
Process
In FreeFem++ we wrap MPI process by function call processor which create internal FreeFem++
object call MPIrank. This mean that do not use MPIrank in FreeFem++ script.
processor(int rk): Keep process rank inside object MPIrank. Rank is inside MPI COMM WORLD.
processor(int rk, mpiComm cc) and processor(mpiComm cc,int rk) process rank inside communicator cc.
processor(int rk, mpiComm cc) and processor(mpiComm cc,int rk) process rank inside communicator cc.
processorblock(int rk) : This function is exactlly the same than processor(int rk) but is use in case
of blocking communication.
processorblock(int rk, mpiComm cc) : This function is exactlly the same than processor(int
rk,mpiComm cc) but use a synchronization point.
11.4.3
11.4.4
Global operations
338
mpiAlltoall(Data a,Data b, mpiComm cc): Sends data a from all to all processes. Receive buffer
is Data b. This is done inside communicator cc.
mpiGather(Data a,Data b,processor(mpiComm,int rk) : Gathers together values Data a from
a group of processes. Process of rank rk get data on communicator rk. This function is like
MPI Gather
mpiAllgather(Data a,Data b) : Gathers Data a from all processes and distribute it to all in Data
b. This is done inside communicator MPI COMM WORLD. This function is like MPI Allgather
mpiAllgather(Data a,Data b, mpiComm cc) : Gathers Data a from all processes and distribute it
to all in Data b. This is done inside communicator cc. This function is like MPI Allgather
mpiScatter(Data a,Data b,processor(int rk, mpiComm cc)) : Sends Data a from one process whith
rank rk to all other processes in group represented by communicator mpiComm cc.
mpiReduce(Data a,Data b,processor(int rk, mpiComm cc),MPI Op op), Reduces values Data a
on all processes to a single value Data b on process of rank rk and communicator cc. Operation
use in reduce is: MPI Op op which can be: mpiMAX, mpiMIN, mpiSUM, mpiPROD, mpiLAND,
mpiLOR, mpiLXOR, mpiBAND, mpiBXOR, mpiMAXLOC, mpiMINLOC.
Note that, for all global operations, only int[int] and real[int] are data type take in account in
FreeFem++ .
11.5
HPDDM solvers
The corresponding examples are in the directory examples++-hpddm. Real valued problems
(diffusion, heat, elasticity and Stokes) and complex valued problems (Maxwell and Helmholtz) are
given in both 2D and 3D. We detail here the 3D elasticity problem and the 3D time-dependent
heat problem.
Example 11.9 (elasticity-3d.edp) A three dimensional elasticity problem is defined. The solver is
a domain decomposition method. Domain decomposition methods are a natural framework for parallel computers. The scripts in the directory examples++-hpddm run on multicores computers
(from 2 to tens of thousands of cores). Recall that like in any MPI code the number of MPI
processes, mpisize, is given in the command line via the option -np. We focus on the script
elasticity-3d.edp but the other scripts have the same structure. The command line to run
the example on four processes with ffglut visualization is:
ff-mpirun -np 4 elasticity-3d.edp -glut ffglut
47
51
55
59
63
67
71
75
339
340
set(A, sparams = hpddm schwarz method ras hpddm schwarz coarse correction balanced , hpddm variant right hpddm verbosity 1 hpddm geneo nu 10);
117
121
real[int] err(u[].n);
err = A u[]; // global matrixvector product
err = rhs;
125
129
11.5.1
u(0, ) := 0 in .
is discretized by an implicit Euler scheme. At each time step n, we shall seek un (x, y, z) satisfying
for all w H 1 ():
Z n
Z
u un1
w + un w =
w, u0 := 0 in .
t
341
is built only once and then reused for all subsequent solves with matrix A := M + dt K. The
distributed matrix vector product with matrix M is made through the call to the function dmv using
the partition of unity associated to matrix A.
99
103
107
111
11.5.2
We give here some hints on the way vectors are distributed among np processes when using
FreeFem++ interfaced with HPDDM. The set of degrees of freedom N is decomposed into np
overlapping sets (Ni )1inp . A MPI-process is in charge of each subset. Let n := #N be the number of degrees of freedom of the global finite element space. Let Ri denote the restriction operator
from Rn onto R#Ni . We have also defined local diagonal matrices Di R#Ni R#Ni so that we
have a partition of unity at the algebraic level:
U=
np
X
RiT Di Ri U
U Rn .
(11.1)
i=1
A global vector U Rn is actually not stored. Rather, it is stored in a distributed way. Each
process i, 1 i N , stores the local vector Ui := Ri U R#Ni .
It is important to ensure that the result of all linear algebra operators applied to this representation
are coherent.
As an example, consider the scalar product of two distributed vectors U, V Rn . Using the
partition of unity (11.1), we have:
!
np
np
X
X
T
(U, V) = U,
Ri Di Ri V =
(Ri U, Di Ri V)
i=1
i=1
np
X
(Ui , Di Vi ) .
i=1
np
X
i=1
(Ri U, Di Ri V) .
342
Local scalar products are performed concurrently. Thus, the implementation is parallel except for
the sum which corresponds to a MPI Reduce call across the np MPI processes. Note also that the
implementation relies on the knowledge of a partition of unity so that the FreeFem++ syntax is
dscalprod(D,u,v).
A axpy procedure y x + y for x, y Rn and R is easily implemented concurrently for
distributed vectors in the form:
yi xi + yi , 1 i np .
The matrix vector product is more involved and details are given in the SIAM book An Introduction
to Domain Decomposition Methods: algorithms, theory and parallel implementation [7] and even
more details are given in P. Jolivets PhD manuscrit.
Chapter 12
Mesh Files
12.1
The mesh data structure, output of a mesh generation algorithm, refers to the geometric data
structure and in some case to another mesh data structure.
In this case, the fields are
MeshVersionFormatted 0
Dimension (I) dim
Vertices (I) NbOfVertices
( ( (R) xji , j=1,dim ) , (I) Ref vi , i=1 , NbOfVertices )
Edges (I) NbOfEdges
( @@Vertex1i , @@Vertex2i , (I) Ref ei , i=1 , NbOfEdges )
Triangles (I) NbOfTriangles
( ( @@Vertexji , j=1,3 ) , (I) Ref ti
i=1 , NbOfTriangles )
VertexOnGeometricVertex
(I) NbOfVertexOnGeometricVertex
( @@Vertexi , @@Vertexgeo
i , i=1,NbOfVertexOnGeometricVertex )
EdgeOnGeometricEdge
(I) NbOfEdgeOnGeometricEdge
( @@Edgei , @@Edgegeo
i , i=1,NbOfEdgeOnGeometricEdge )
CrackedEdges (I) NbOfCrackedEdges
( @@Edge1i , @@Edge2i , i=1 , NbOfCrackedEdges )
343
344
i=1,NbOfVertexOnSupportEdge )
VertexOnSupportTriangle
(I) NbOfVertexOnSupportTriangle
( @@Vertexi , @@Triasupp
, (R) usupp
, (R) visupp
i
i
i=1 , NbOfVertexOnSupportTriangle )
VertexOnSupportQuadrilaterals
(I) NbOfVertexOnSupportQuadrilaterals
( @@Vertexi , @@Quadsupp
, (R) usupp
, (R) visupp
i
i
i=1 , NbOfVertexOnSupportQuadrilaterals )
12.2
12.3
345
12.4
Metric File
12.5
The mesh is only composed of triangles and can be defined with the help of the following
two integers and four arrays:
nbt
nbv
346
c(1:2,nbv)
refs(nbv)
reft(nbv)
347
348
Chapter 13
Addition of a new finite element
13.1
Some notations
kPi1
X
(13.1)
k=0
K
+ K | xy }
v|K (x, y) = K
(13.2)
sgn(ib ic )
(x a),
2|T |
1K =
sgn(ic ia )
(x b),
2|T |
349
2K =
sgn(ia ib )
(x c),
2|T |
(13.3)
350
ik = {0, 0, 1, 1, 2, 2},
pk = {0, 0, 1, 1, 2, 2} , jk = {0, 1, 0, 1, 0, 1, 0, 1}.
13.2
Fem2D {
//
the set of Point in K
for (int p=0,kk=0;p<3;p++) {
P_Pi_h[p]=Pt[p];
for (int j=0;j<2;j++)
pij_alpha[kk++]= IPJ(p,p,j); }}
//
definition of ik , pk , jk in (13.1)
void FB(const bool * watdd, const Mesh & Th,const Triangle & K,
const R2 &PHat, RNMK_ & val) const;
351
where the array data is form with the concatenation of five array of size NbDoF and one array of
size N.
This array is:
int TypeOfFE_RTortho::Data[]={
//
3,4,5,//
0,0,0,//
0,1,2,//
0,0,0,//
0,1,2,//
0,0 }; //
where the support is a number 0, 1, 2 for vertex support, 3, 4, 5 for edge support, and finaly 6 for
element support.
The function to defined the function iK , this function return the value of all the basics function
or this derivatives in array val, computed at point PHat on the reference triangle corresponding
to point R2 P=K(Phat); on the current triangle K.
The index i, j, k of the array val(i, j, k) corresponding to:
i is basic function number on finite element i [0, N oF [
j is the value of component j [0, N [
k is the type of computed value f (P ), dx(f )(P ), dy(f )(P ), ... i [0, last operatortype[. Remark
for optimization, this value is computed only if whatd[k] is true, and the numbering is defined
with
enum operatortype { op_id=0,
op_dx=1,op_dy=2,
op_dxx=3,op_dyy=4,
op_dyx=5,op_dxy=5,
op_dz=6,
op_dzz=7,
op_dzx=8,op_dxz=8,
op_dzy=9,op_dyz=9
};
const int last_operatortype=10;
352
val=0;
R a=1./(2*K.area);
R a0=
K.EdgeOrientation(0) * a ;
R a1=
K.EdgeOrientation(1) * a ;
R a2=
K.EdgeOrientation(2) * a ;
if (whatd[op_id])
{
assert(val.K()>op_id);
RN_ f0(val(.,0,0));
RN_ f1(val(.,1,0));
f1[0] = (P.x-A.x)*a0;
f0[0] = -(P.y-A.y)*a0;
//
//
-----------value of the function
//
//
f1[1] = (P.x-B.x)*a1;
f0[1] = -(P.y-B.y)*a1;
f1[2] = (P.x-C.x)*a2;
f0[2] = -(P.y-C.y)*a2;
}
if (whatd[op_dx])
{
assert(val.K()>op_dx);
val(0,1,op_dx) = a0;
val(1,1,op_dx) = a1;
val(2,1,op_dx) = a2;
}
if (whatd[op_dy])
{
assert(val.K()>op_dy);
val(0,0,op_dy) = -a0;
val(1,0,op_dy) = -a1;
val(2,0,op_dy) = -a2;
}
//
//
---------------value of the dx of function
353
Now , we just need to add a new key work in FreeFem++, Two way, with static or dynamic link
so at the end of the file, we add :
With dynamic link is very simple (see section C of appendix), just add before the end of FEM2d
namespace add:
//
//
FEM2d namespace
//
let the 2 globals variables
static TypeOfFE_RTortho The_TypeOfFE_RTortho;
//
//
----- the name in freefem ---static ListOfTFE typefemRTOrtho("RT0Ortho", & The_TypeOfFE_RTortho);
//
//
link with FreeFem++ do not work with static library .a
//
FH so add a extern name to call in init static FE
//
(see end of FESpace.cpp)
void init_FE_ADD() { };
//
--- end -}
//
FEM2d namespace
To inforce in loading of this new finite element, we have to add the two new lines close to the end
of files src/femlib/FESpace.cpp like:
//
correct Problem of static library link with new make file
void init_static_FE()
{
//
list of other FE file.o
extern void init_FE_P2h() ;
init_FE_P2h() ;
extern void init_FE_ADD() ;
//
new line 1
init_FE_ADD();
//
new line 2
}
354
\
\
\
\
\
\
\
Appendix A
Table of Notations
Here mathematical expressions and corresponding FreeFem++ commands are explained.
A.1
Generalities
A.2
356
A.3
Numbers
A.4
Differential Calculus
A.5. MESHES
A.5
357
Meshes
h the boundary of h
nv the number of vertices in Th (get by Th.nv)
hmin the minimal edge size of Th (get by Th.hmin)
hmax the maximal edge size of Th (get by Th.hmax)
[q i q j ] the segment connecting q i and q j
q k1 , q k2 , q k3 the vertices of a triangle Tk with anti-clock direction (get the coordinate of q kj
by (Th[k-1][j-1].x, Th[k-1][j-1].y))
I the set {i N| q i 6 h }
A.6
Z
2
L2 () the set w(x, y)
|w(x, y)| dxdy <
Z
1/2
norm: kwk0, =
Z
2
H () the set w L ()
|w/x| + |w/y|
dxdy <
|| w
L2 () = (1 , 2 ) N2 , || = 1 + 2
1 y 2
x
X Z
scalar product: (v, w)1, =
D vD w
Z
2
H () the set w L ()
m
1/2
||m
358
Appendix B
Grammar
B.1
start:
input ENDOFFILE;
input:
instructions ;
instructions: instruction
| instructions instruction
list_of_id_args:
| id
| id = no_comma_expr
| FESPACE id
| type_of_dcl id
| type_of_dcl & id
| [ list_of_id_args ]
| list_of_id_args , id
| list_of_id_args , [ list_of_id_args ]
| list_of_id_args , id = no_comma_expr
| list_of_id_args , FESPACE id
| list_of_id_args , type_of_dcl id
| list_of_id_args , type_of_dcl & id ;
list_of_id1:
id
| list_of_id1 , id
id: ID | FESPACE ;
list_of_dcls:
|
|
|
ID
ID =
no_comma_expr
ID ( parameters_list )
list_of_dcls , list_of_dcls
parameters_list:
no_set_expr
| FESPACE ID
| ID = no_set_expr
359
360
APPENDIX B. GRAMMAR
| parameters_list , no_set_expr
| parameters_list , id = no_set_expr ;
type_of_dcl:
TYPE
| TYPE [ TYPE ] ;
ID_space:
ID
| ID [ no_set_expr ]
| ID = no_set_expr
| [ list_of_id1 ]
| [ list_of_id1 ] [ no_set_expr ]
| [ list_of_id1 ] = no_set_expr ;
ID_array_space:
ID ( no_set_expr )
| [ list_of_id1 ] ( no_set_expr ) ;
fespace: FESPACE ;
spaceIDa
spaceIDb
ID_array_space
spaceIDa , ID_array_space
ID_space
spaceIDb , ID_space ;
spaceIDs :
fespace
fespace [ TYPE ]
spaceIDb
spaceIDa
fespace_def: ID ( parameters_list ) ;
fespace_def_list:
fespace_def
| fespace_def_list , fespace_def ;
declaration:
type_of_dcl list_of_dcls ;
| fespace fespace_def_list
;
| spaceIDs ;
| FUNCTION ID = Expr ;
| FUNCTION type_of_dcl ID ( list_of_id_args ) {
instructions}
| FUNCTION ID ( list_of_id_args )
=
no_comma_expr
;
begin: {
end:
}
for_loop:
while_loop:
;
;
for
;
while ;
instruction:
;
| include STRING
| load STRING
| Expr ;
| declaration
| for_loop ( Expr ; Expr ; Expr ) instruction
361
bornes instruction
no_comma_expr
| Expr , Expr ;
unop:
|
|
|
|
-
+
!
++
--
no_comma_expr:
no_set_expr
| no_set_expr = no_comma_expr
| no_set_expr += no_comma_expr
| no_set_expr -= no_comma_expr
| no_set_expr *= no_comma_expr
| no_set_expr /= no_comma_expr
| no_set_expr .*= no_comma_expr
| no_set_expr ./= no_comma_expr ;
no_set_expr:
no_ternary_expr
| no_ternary_expr ? no_set_expr : no_set_expr ;
no_ternary_expr:
unary_expr
| no_ternary_expr
| no_ternary_expr
| no_ternary_expr
| no_ternary_expr
| no_ternary_expr
| no_ternary_expr
| no_ternary_expr
| no_ternary_expr
| no_ternary_expr
| no_ternary_expr
| no_ternary_expr
| no_ternary_expr
| no_ternary_expr
* no_ternary_expr
.* no_ternary_expr
./ no_ternary_expr
/ no_ternary_expr
% no_ternary_expr
+ no_ternary_expr
- no_ternary_expr
<< no_ternary_expr
>> no_ternary_expr
& no_ternary_expr
&& no_ternary_expr
| no_ternary_expr
|| no_ternary_expr
362
APPENDIX B. GRAMMAR
|
|
|
|
|
|
no_ternary_expr
no_ternary_expr
no_ternary_expr
no_ternary_expr
no_ternary_expr
no_ternary_expr
< no_ternary_expr
<= no_ternary_expr
> no_ternary_expr
>= no_ternary_expr
== no_ternary_expr
!= no_ternary_expr ;
sub_script_expr:
no_set_expr
|
:
|
no_set_expr : no_set_expr
|
no_set_expr : no_set_expr : no_set_expr ;
parameters:
|
|
|
|
|
|
|
array:
no_set_expr
FESPACE
id = no_set_expr
sub_script_expr
parameters , FESPACE
parameters , no_set_expr
parameters , id = no_set_expr ;
no_comma_expr
| array , no_comma_expr ;
unary_expr:
pow_expr
| unop pow_expr %prec UNARY ;
pow_expr: primary
|
primary unary_expr
|
primary _ unary_expr
|
primary
;
primary:
|
|
|
|
|
|
|
|
|
|
|
|
|
ID
LNUM
DNUM
CNUM
STRING
primary ( parameters )
primary [ Expr ]
primary [ ]
primary . ID
primary ++
primary --
TYPE ( Expr ) ;
( Expr )
[ array ] ;
//
transpose
B.2
B.3
operator() :
operator() :
operator() :
operator() :
363
364
APPENDIX B. GRAMMAR
operator() :
operator() :
operator() :
operator() :
365
operator() :
<Fem2D::Mesh>... )
operator() :
<Fem2D::Mesh>... )
<LinearComb<MDroit, C_F0>> )
<LinearComb<MGauche, C_F0>> )
operator() :
type :<long>
366
APPENDIX B. GRAMMAR
<LinearComb<MDroit, C_F0>> )
<LinearComb<MGauche, C_F0>> )
operator() :
<Fem2D::Mesh> :
367
<long>, <long>, <E_Array> )
368
APPENDIX B. GRAMMAR
Appendix C
Dynamical link
Now, its possible to add built-in functionnalites in FreeFem++ under the three environnents Linux, Windows and MacOS X 10.3 or newer. It is agood idea to, first try the example
load.edp in directory example++-load.
You will need to install a c++ compiler (generally g++/gcc compiler) to compile your
function.
Windows Install the cygwin environnent or the mingw
MacOs Install the developer tools xcode on the apple DVD
Linux/Unix Install the correct compiler (gcc for instance)
Now, assume that you are in a shell window (a cygwin window under Windows) in the
directory example++-load. Remark that in the sub directory include they are all the
FreeFem++ include file to make the link with FreeFem++ .
Note C.1 If you try to load dynamically a file with command load "xxx"
Under unix (Linux or MacOs), the file xxx.so twill be loaded so it must be
either in the search directory of routine dlopen (see the environment variable
$LD_LIBRARY_PATH or in the current directory, and the suffix ".so" or the prefix
"./" is automatically added.
Under Windows, The file xxx.dll will be loaded so it must be in the loadLibary
search directory which includes the directory of the application,
The compilation of your module: the script ff-c++ compiles and makes the link with
FreeFem++ , but be careful, the script has no way to known if you try to compile for a
pure Windows environment or for a cygwin environment so to build the load module under
cygwin you must add the -cygwin parameter.
C.1
The following defines a new function call myfunction with no parameter, but using the
x, y current value.
369
370
#include <iostream>
#include <cfloat>
using namespace std;
#include "error.hpp"
#include "AFunction.hpp"
#include "rgraph.hpp"
#include "RNM.hpp"
#include "fem.hpp"
#include "FESpace.hpp"
#include "MeshPoint.hpp"
using namespace Fem2D;
double myfunction(Stack stack)
{
//
to get FreeFem++ data
MeshPoint &mp= *MeshPointStack(stack); //
the struct to get x,y, normal ,
value
double x= mp.P.x;
//
get the current x value
double y= mp.P.y;
//
get the current y value
//
cout << "x = " << x << " y=" << y << endl;
return sin(x)*cos(y);
}
Now the Problem is to build the link with FreeFem++ , to do that we need two classes,
one to call the function myfunction
All FreeFem++ evaluable expression must be a struct/class C++ which derive from E F0.
By default this expression does not depend of the mesh position, but if they derive from
E F0mps the expression depends of the mesh position, and for more details see [17].
//
//
A class build the link with FreeFem++
//
generaly this class are already in AFunction.hpp
but unfortunatly, I have no simple function with no parameter
//
in FreeFem++ depending of the mesh,
template<class R>
class OneOperator0s : public OneOperator {
//
};
typedef R (*func)(Stack ) ;
func f;
public:
//
the function which build the FreeFem++ byte code
E_F0 * code(const basicAC_F0 & ) const { return new E_F0_F(f);}
//
the constructor to say ff is a function without parameter
OneOperator0s(func
};
371
//
and returning a R
ff): OneOperator(map_type[typeid(R).name()]),f(ff){}
To finish we must add this new function in FreeFem++ table , to do that include :
void init(){
Global.Add("myfunction","(",new OneOperator0s<double>(myfunction));
}
LOADFUNC(init);
:
:
:
:
:
mesh Th=square(5,5);
fespace Vh(Th,P1);
Vh uh=myfunction();
//
warning do not forget ()
cout << uh[].min << " " << uh[].max << endl;
sizestack + 1024 =1240 ( 216 )
nb boundary edges
Under Windows, launch FreeFem++ with the mouse (or ctrl O) on the example.
372
C.2
m1
n1
XX
j 0 =0 j=0
So the classical
discrete DFFT is f = dfft(f, 1)/ n and the reverse dFFT f =
dfft(f, 1)/ n
Remark: the 2D Laplace operator is
n1
XX
m1
0
fi+nj e2i(xj+yj )
f (x, y) = 1/ N
j 0 =0 j=0
and we have
fk+nl = f (k/n, l/m)
So
dkl = ((2)2 ((k)
2 + (l)2 ))fc
f
kl
where k = k if k n/2 else k = k n and l = l if l m/2 else l = l m.
And to have a real function we need all modes to be symmetric around zero, so n and m
must be odd.
Compile to build a new library
% ff-c++ dfft.cpp ../download/install/lib/libfftw3.a -I../download/install/include
export MACOSX_DEPLOYMENT_TARGET=10.3
g++ -c -Iinclude -I../download/install/include dfft.cpp
g++ -bundle -undefined dynamic_lookup dfft.o -o ./dfft.dylib ../download/install/lib/libfftw3.a
To test ,
-- FreeFem++ v 1.4800028 (date Mon Oct 10 16:53:28 EEST 2005)
file : dfft.edp
Load: lg_fem cadna lg_mesh eigenvalue UMFPACK
1 :
//
Example of dynamic function load
2 :
//
-------------------------------3 :
//
Id : f reef em + +doc.tex, v1.1102010/06/0411 : 27 : 24hechtExp
4 :
//
Discret Fast Fourier Transform
5 :
//
------------------------------6 : load "dfft" lood: init dfft
373
:
:
:
:
:
:
:
:
43
44
45
46
47
48
49
50
51
52
53
54
:
:
:
:
:
:
:
:
:
:
:
:
Vh<complex> ff = f;
Vh<complex> fhat;
fhat[] = dfft(ff[],ny,-1);
Vh<complex> wij;
//
warning in fact we take mode between -nx/2, nx/2 and -ny/2,ny/2
//
thank to the operator ?:
wij = square(2.*pi)*(square(( x<0.5?x*nx:(x-1)*nx))
+ square((y<0.5?y*ny:(y-1)*ny)));
wij[][0] = 1e-5;
//
to remove div / 0
fhat[] = fhat[]./ wij[];
//
u[]=dfft(fhat[],ny,1);
u[] /= complex(N);
ur = real(u);
//
the solution
w = real(ue);
//
the exact solution
plot(w,ur,value=1 ,cmm=" ue
", wait=1);
w[] -= ur[];
//
array sub
real err= abs(w[].max)+abs(w[].min) ;
cout << " err = " << err << endl;
assert( err < 1e-6);
sizestack + 1024 =3544 ( 2520 )
2815
Alloc: 111320
374
C.3
375
//
the matrix expression
a the expression to get the mesh
the expression to get c (must be a
//
a array expression [ a, b]
const E_Array * a= dynamic_cast<const E_Array*>((Expression) args[3]);
if (a->size() != 2) CompileError("syntax: MatrixUpWind0(Th,rhi,[u1,u2])");
int err =0;
expu1= CastTo<double>((*a)[0]);
//
fist exp of the array (must be a
double)
expu2= CastTo<double>((*a)[1]);
//
second exp of the array (must be a
double)
}
MatrixUpWind0(){
}
static ArrayOfaType typeargs()
{ return ArrayOfaType(atype<Matrice_Creuse<R>*>(),
atype<pmesh>(),atype<double>(),atype<E_Array>());}
static E_F0 * f(const basicAC_F0 & args){ return new MatrixUpWind0(args);}
AnyType operator()(Stack s) const ;
};
int
fvmP1P0(double q[3][2], double u[2],double c[3], double a[3][3], double
where[3] )
{
//
computes matrix a on a triangle for the
Dervieux FVM
for(int i=0;i<3;i++) for(int j=0;j<3;j++) a[i][j]=0;
for(int i=0;i<3;i++){
int ip = (i+1)%3, ipp =(ip+1)%3;
double unL =-((q[ip][1]+q[i][1]-2*q[ipp][1])*u[0]
-(q[ip][0]+q[i][0]-2*q[ipp][0])*u[1])/6;
if(unL>0) { a[i][i] += unL; a[ip][i]-=unL;}
else{ a[i][ip] += unL; a[ip][ip]-=unL;}
if(where[i]&&where[ip]){
//
this is a boundary edge
unL=((q[ip][1]-q[i][1])*u[0] -(q[ip][0]-q[i][0])*u[1])/2;
if(unL>0) { a[i][i]+=unL; a[ip][ip]+=unL;}
}
}
return 1;
}
//
the evaluation routine
AnyType MatrixUpWind0::operator()(Stack stack) const
{
Matrice_Creuse<R> * sparse_mat =GetAny<Matrice_Creuse<R>* >((*emat)(stack));
MatriceMorse<R> * amorse =0;
MeshPoint *mp(MeshPointStack(stack)) , mps=*mp;
376
//
377
void init()
{
cout << " lood: init Mat Chacon " << endl;
Global.Add("MatUpWind0","(", new OneOperatorCode<MatrixUpWind0 >( ));
}
LOADFUNC(init);
C.4
First read the section 13 of the appendix, we add two new finite elements examples in the
directory examples++-load.
The Bernardi-Raugel Element The Bernardi-Raugel finite element is meant to solve the
Navier Stokes equations in u, p formulation; the velocity space PKbr is minimal to prove the
inf-sup condition with piecewise constant pressure by triangle.
The finite element space Vh is
Vh = {u H 1 ()2 ;
K Th , u|K PKbr }
where
K K
K
PKbr = span{K
i ek }i=1,2,3,k=1,2 {i i+1 ni+2 }i=1,2,3
#include "error.hpp"
#include "AFunction.hpp"
#include "rgraph.hpp"
using namespace std;
#include "RNM.hpp"
#include "fem.hpp"
-----------------------
378
#include "FESpace.hpp"
#include "AddNewFE.h"
namespace
Fem2D {
TypeOfFE { public:
TypeOfFE_P2BRLagrange(): TypeOfFE(6+3+0,
2,
Data,
4,
1,
6+3*(2+2),
//
nb coef to build interpolation
9,
//
np point to build interpolation
0)
{
....
//
to long see the source
}
void FB(const bool * whatd, const Mesh & Th,const Triangle & K,const R2
&P, RNMK_ & val) const;
void TypeOfFE_P2BRLagrange::Pi_h_alpha(const baseFElement & K,KN_<double>
& v) const;
} ;
//
on what nu df on node node of df
int TypeOfFE_P2BRLagrange::Data[]={
0,0, 1,1, 2,2, 3,4,5,
0,1, 0,1, 0,1, 0,0,0,
0,0, 1,1, 2,2, 3,4,5,
0,0, 0,0, 0,0, 0,0,0,
0,1, 2,3, 4,5, 6,7,8,
0,0
};
void TypeOfFE_P2BRLagrange::Pi_h_alpha(const baseFElement & K,KN_<double> & v)
const
{
const Triangle & T(K.T);
int k=0;
//
coef pour les 3 sommets fois le 2 composantes
for (int i=0;i<6;i++)
v[k++]=1;
//
integration sur les aretes
for (int i=0;i<3;i++)
{
R2 N(T.Edge(i).perp());
=
T.EdgeOrientation(i)
*
*0.5 ;
v[k++]= N.x;
v[k++]= N.y;
v[k++]= N.x;
v[k++]= N.y;
}
379
//
//
a bascis functions
do the interpolation
cout << " dx(a1)(x1,y1) = " << dx(a1)(x1,y1) << " == " << DD(a1,h,0) <<
endl;
assert( abs(dx(a1)(x1,y1)-DD(a1,h,0) ) < 1e-5);
assert( abs(dx(a2)(x1,y1)-DD(a2,h,0) ) < 1e-5);
380
A real example using this finite element, just a small modification of the NSP2P1.edp
examples, just the begenning is change to
load "BernadiRaugel"
real s0=clock();
mesh Th=square(10,10);
fespace Vh2(Th,P2BR);
fespace Vh(Th,P0);
Vh2 [u1,u2],[up1,up2];
Vh2 [v1,v2];
And the plot instruction is also changed because the pressure is constant, and we cannot
plot isovalues of peacewise constant functions.
The Morley Element See the example bilapMorley.edp.
C.5
Warning the sparse solver interface as been completely rewritten in version 3.2 , so the
section is obsolete, the example in are correct/
Only a fast sketch of the code is given here; for details see the .cpp code from SuperLU.cpp
or NewSolve.cpp.
First the include files:
#include <iostream>
using namespace std;
#include "rgraph.hpp"
#include "error.hpp"
#include "AFunction.hpp"
//
#include "lex.hpp"
#include "MatriceCreuse_tpl.hpp"
#include "slu_ddefs.h"
#include "slu_zdefs.h"
381
double version
To get Matrix value, we have just to remark that the Morse Matrice the storage, is the
SLU NR format is the compressed row storage, this is the transpose of the compressed column
storage.
So if AA is a MatriceMorse you have with SuperLU notation.
n=AA.n;
m=AA.m;
nnz=AA.nbcoef;
a=AA.a;
asub=AA.cl;
xa=AA.lg;
options.Trans = TRANS;
Dtype_t R_SLU = SuperLUDriver<R>::R_SLU_T();
Create_CompCol_Matrix(&A, m, n, nnz, a, asub, xa, SLU_NC, R_SLU, SLU_GE);
The two BuildSolverSuperLU function, to change the default sparse solver variable
DefSparseSolver<double>::solver
MatriceMorse<double>::VirtualSolver *
BuildSolverSuperLU(DCL_ARG_SPARSE_SOLVER(double,A))
{
if(verbosity>9)
cout << " BuildSolverSuperLU<double>" << endl;
return new SolveSuperLU<double>(*A,ds.strategy,ds.tgv,ds.epsilon,ds.tol_pivot,ds.tol
}
MatriceMorse<Complex>::VirtualSolver *
BuildSolverSuperLU(DCL_ARG_SPARSE_SOLVER(Complex,A))
{
if(verbosity>9)
cout << " BuildSolverSuperLU<Complex>" << endl;
return new SolveSuperLU<Complex>(*A,ds.strategy,ds.tgv,ds.epsilon,ds.tol_pivot,ds.tol_
}
382
TypeSolveMatdefaultvalue=TypeSolveMat::defaultvalue;
383
LOADFUNC(init);
-o supe
-L$HOME/work/LinearSolver/SuperLU_3.0/ -lsuperlu_
384
}
if(i==0)defaulttoGMRES();
if(i==1)defaultsolver();
}
Appendix D
Plugin
D.1
gsl
385
386
APPENDIX D. PLUGIN
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
D.1. GSL
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
cdf
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
387
flat P(a,b,c) 7 gslcdfflatP(a,b,c)
flat Q(a,b,c) 7 gslcdfflatQ(a,b,c)
flat Pinv(a,b,c) 7 gslcdfflatPinv(a,b,c)
flat Qinv(a,b,c) 7 gslcdfflatQinv(a,b,c)
lognormal P(a,b,c) 7 gslcdflognormalP(a,b,c)
lognormal Q(a,b,c) 7 gslcdflognormalQ(a,b,c)
lognormal Pinv(a,b,c) 7 gslcdflognormalPinv(a,b,c)
lognormal Qinv(a,b,c) 7 gslcdflognormalQinv(a,b,c)
gumbel1 P(a,b,c) 7 gslcdfgumbel1P(a,b,c)
gumbel1 Q(a,b,c) 7 gslcdfgumbel1Q(a,b,c)
gumbel1 Pinv(a,b,c) 7 gslcdfgumbel1Pinv(a,b,c)
gumbel1 Qinv(a,b,c) 7 gslcdfgumbel1Qinv(a,b,c)
gumbel2 P(a,b,c) 7 gslcdfgumbel2P(a,b,c)
gumbel2 Q(a,b,c) 7 gslcdfgumbel2Q(a,b,c)
gumbel2 Pinv(a,b,c) 7 gslcdfgumbel2Pinv(a,b,c)
gumbel2 Qinv(a,b,c) 7 gslcdfgumbel2Qinv(a,b,c)
weibull P(a,b,c) 7 gslcdfweibullP(a,b,c)
weibull Q(a,b,c) 7 gslcdfweibullQ(a,b,c)
weibull Pinv(a,b,c) 7 gslcdfweibullPinv(a,b,c)
weibull Qinv(a,b,c) 7 gslcdfweibullQinv(a,b,c)
pareto P(a,b,c) 7 gslcdfparetoP(a,b,c)
pareto Q(a,b,c) 7 gslcdfparetoQ(a,b,c)
pareto Pinv(a,b,c) 7 gslcdfparetoPinv(a,b,c)
pareto Qinv(a,b,c) 7 gslcdfparetoQinv(a,b,c)
logistic P(a,b) 7 gslcdflogisticP(a,b)
logistic Q(a,b) 7 gslcdflogisticQ(a,b)
logistic Pinv(a,b) 7 gslcdflogisticPinv(a,b)
logistic Qinv(a,b) 7 gslcdflogisticQinv(a,b)
binomial P(a,b,c) 7 gslcdfbinomialP(a,b,c)
binomial Q(a,b,c) 7 gslcdfbinomialQ(a,b,c)
poisson P(a,b) 7 gslcdfpoissonP(a,b)
poisson Q(a,b) 7 gslcdfpoissonQ(a,b)
geometric P(a,b) 7 gslcdfgeometricP(a,b)
geometric Q(a,b) 7 gslcdfgeometricQ(a,b)
negative binomial P(a,b,c) 7 gslcdfnegativebinomialP(a,b,c)
negative binomial Q(a,b,c) 7 gslcdfnegativebinomialQ(a,b,c)
pascal P(a,b,c) 7 gslcdfpascalP(a,b,c)
pascal Q(a,b,c) 7 gslcdfpascalQ(a,b,c)
bernoulli pdf(a,b) 7 gslranbernoullipdf(a,b)
beta(a,b,c) 7 gslranbeta(a,b,c)
beta pdf(a,b,c) 7 gslranbetapdf(a,b,c)
binomial pdf(a,b,c) 7 gslranbinomialpdf(a,b,c)
exponential(a,b) 7 gslranexponential(a,b)
exponential pdf(a,b) 7 gslranexponentialpdf(a,b)
exppow(a,b,c) 7 gslranexppow(a,b,c)
exppow pdf(a,b,c) 7 gslranexppowpdf(a,b,c)
cauchy(a,b) 7 gslrancauchy(a,b)
cauchy pdf(a,b) 7 gslrancauchypdf(a,b)
chisq(a,b) 7 gslranchisq(a,b)
chisq pdf(a,b) 7 gslranchisqpdf(a,b)
388
APPENDIX D. PLUGIN
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
ran
erlang(a,b,c) 7 gslranerlang(a,b,c)
erlang pdf(a,b,c) 7 gslranerlangpdf(a,b,c)
fdist(a,b,c) 7 gslranfdist(a,b,c)
fdist pdf(a,b,c) 7 gslranfdistpdf(a,b,c)
flat(a,b,c) 7 gslranflat(a,b,c)
flat pdf(a,b,c) 7 gslranflatpdf(a,b,c)
gamma(a,b,c) 7 gslrangamma(a,b,c)
gamma int(a,b) 7 gslrangammaint(a,b)
gamma pdf(a,b,c) 7 gslrangammapdf(a,b,c)
gamma mt(a,b,c) 7 gslrangammamt(a,b,c)
gamma knuth(a,b,c) 7 gslrangammaknuth(a,b,c)
gaussian(a,b) 7 gslrangaussian(a,b)
gaussian ratio method(a,b) 7 gslrangaussianratiomethod(a,b)
gaussian ziggurat(a,b) 7 gslrangaussianziggurat(a,b)
gaussian pdf(a,b) 7 gslrangaussianpdf(a,b)
ugaussian(a) 7 gslranugaussian(a)
ugaussian ratio method(a) 7 gslranugaussianratiomethod(a)
ugaussian pdf(a) 7 gslranugaussianpdf(a)
gaussian tail(a,b,c) 7 gslrangaussiantail(a,b,c)
gaussian tail pdf(a,b,c) 7 gslrangaussiantailpdf(a,b,c)
ugaussian tail(a,b) 7 gslranugaussiantail(a,b)
ugaussian tail pdf(a,b) 7 gslranugaussiantailpdf(a,b)
landau(a) 7 gslranlandau(a)
landau pdf(a) 7 gslranlandaupdf(a)
geometric pdf(a,b) 7 gslrangeometricpdf(a,b)
gumbel1(a,b,c) 7 gslrangumbel1(a,b,c)
gumbel1 pdf(a,b,c) 7 gslrangumbel1pdf(a,b,c)
gumbel2(a,b,c) 7 gslrangumbel2(a,b,c)
gumbel2 pdf(a,b,c) 7 gslrangumbel2pdf(a,b,c)
logistic(a,b) 7 gslranlogistic(a,b)
logistic pdf(a,b) 7 gslranlogisticpdf(a,b)
lognormal(a,b,c) 7 gslranlognormal(a,b,c)
lognormal pdf(a,b,c) 7 gslranlognormalpdf(a,b,c)
logarithmic pdf(a,b) 7 gslranlogarithmicpdf(a,b)
negative binomial pdf(a,b,c) 7 gslrannegativebinomialpdf(a,b,c)
pascal pdf(a,b,c) 7 gslranpascalpdf(a,b,c)
pareto(a,b,c) 7 gslranpareto(a,b,c)
pareto pdf(a,b,c) 7 gslranparetopdf(a,b,c)
poisson pdf(a,b) 7 gslranpoissonpdf(a,b)
rayleigh(a,b) 7 gslranrayleigh(a,b)
rayleigh pdf(a,b) 7 gslranrayleighpdf(a,b)
rayleigh tail(a,b,c) 7 gslranrayleightail(a,b,c)
rayleigh tail pdf(a,b,c) 7 gslranrayleightailpdf(a,b,c)
tdist(a,b) 7 gslrantdist(a,b)
tdist pdf(a,b) 7 gslrantdistpdf(a,b)
laplace(a,b) 7 gslranlaplace(a,b)
laplace pdf(a,b) 7 gslranlaplacepdf(a,b)
levy(a,b,c) 7 gslranlevy(a,b,c)
weibull(a,b,c) 7 gslranweibull(a,b,c)
weibull pdf(a,b,c) 7 gslranweibullpdf(a,b,c)
D.1. GSL
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
389
airy Ai(a,b) 7 gslsfairyAi(a,b)
airy Bi(a,b) 7 gslsfairyBi(a,b)
airy Ai scaled(a,b) 7 gslsfairyAiscaled(a,b)
airy Bi scaled(a,b) 7 gslsfairyBiscaled(a,b)
airy Ai deriv(a,b) 7 gslsfairyAideriv(a,b)
airy Bi deriv(a,b) 7 gslsfairyBideriv(a,b)
airy Ai deriv scaled(a,b) 7 gslsfairyAiderivscaled(a,b)
airy Bi deriv scaled(a,b) 7 gslsfairyBiderivscaled(a,b)
airy zero Ai(a) 7 gslsfairyzeroAi(a)
airy zero Bi(a) 7 gslsfairyzeroBi(a)
airy zero Ai deriv(a) 7 gslsfairyzeroAideriv(a)
airy zero Bi deriv(a) 7 gslsfairyzeroBideriv(a)
bessel J0(a) 7 gslsfbesselJ0(a)
bessel J1(a) 7 gslsfbesselJ1(a)
bessel Jn(a,b) 7 gslsfbesselJn(a,b)
bessel Y0(a) 7 gslsfbesselY0(a)
bessel Y1(a) 7 gslsfbesselY1(a)
bessel Yn(a,b) 7 gslsfbesselYn(a,b)
bessel I0(a) 7 gslsfbesselI0(a)
bessel I1(a) 7 gslsfbesselI1(a)
bessel In(a,b) 7 gslsfbesselIn(a,b)
bessel I0 scaled(a) 7 gslsfbesselI0scaled(a)
bessel I1 scaled(a) 7 gslsfbesselI1scaled(a)
bessel In scaled(a,b) 7 gslsfbesselInscaled(a,b)
bessel K0(a) 7 gslsfbesselK0(a)
bessel K1(a) 7 gslsfbesselK1(a)
bessel Kn(a,b) 7 gslsfbesselKn(a,b)
bessel K0 scaled(a) 7 gslsfbesselK0scaled(a)
bessel K1 scaled(a) 7 gslsfbesselK1scaled(a)
bessel Kn scaled(a,b) 7 gslsfbesselKnscaled(a,b)
bessel j0(a) 7 gslsfbesselj0(a)
bessel j1(a) 7 gslsfbesselj1(a)
bessel j2(a) 7 gslsfbesselj2(a)
bessel jl(a,b) 7 gslsfbesseljl(a,b)
bessel y0(a) 7 gslsfbessely0(a)
bessel y1(a) 7 gslsfbessely1(a)
bessel y2(a) 7 gslsfbessely2(a)
bessel yl(a,b) 7 gslsfbesselyl(a,b)
bessel i0 scaled(a) 7 gslsfbesseli0scaled(a)
bessel i1 scaled(a) 7 gslsfbesseli1scaled(a)
bessel i2 scaled(a) 7 gslsfbesseli2scaled(a)
bessel il scaled(a,b) 7 gslsfbesselilscaled(a,b)
bessel k0 scaled(a) 7 gslsfbesselk0scaled(a)
bessel k1 scaled(a) 7 gslsfbesselk1scaled(a)
bessel k2 scaled(a) 7 gslsfbesselk2scaled(a)
bessel kl scaled(a,b) 7 gslsfbesselklscaled(a,b)
bessel Jnu(a,b) 7 gslsfbesselJnu(a,b)
bessel Ynu(a,b) 7 gslsfbesselYnu(a,b)
bessel Inu scaled(a,b) 7 gslsfbesselInuscaled(a,b)
bessel Inu(a,b) 7 gslsfbesselInu(a,b)
390
APPENDIX D. PLUGIN
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
D.1. GSL
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
391
fermi dirac m1(a) 7 gslsffermidiracm1(a)
fermi dirac 0(a) 7 gslsffermidirac0(a)
fermi dirac 1(a) 7 gslsffermidirac1(a)
fermi dirac 2(a) 7 gslsffermidirac2(a)
fermi dirac int(a,b) 7 gslsffermidiracint(a,b)
fermi dirac mhalf(a) 7 gslsffermidiracmhalf(a)
fermi dirac half(a) 7 gslsffermidirachalf(a)
fermi dirac 3half(a) 7 gslsffermidirac3half(a)
fermi dirac inc 0(a,b) 7 gslsffermidiracinc0(a,b)
lngamma(a) 7 gslsflngamma(a)
gamma(a) 7 gslsfgamma(a)
gammastar(a) 7 gslsfgammastar(a)
gammainv(a) 7 gslsfgammainv(a)
taylorcoeff(a,b) 7 gslsftaylorcoeff(a,b)
fact(a) 7 gslsffact(a)
doublefact(a) 7 gslsfdoublefact(a)
lnfact(a) 7 gslsflnfact(a)
lndoublefact(a) 7 gslsflndoublefact(a)
lnchoose(a,b) 7 gslsflnchoose(a,b)
choose(a,b) 7 gslsfchoose(a,b)
lnpoch(a,b) 7 gslsflnpoch(a,b)
poch(a,b) 7 gslsfpoch(a,b)
pochrel(a,b) 7 gslsfpochrel(a,b)
gamma inc Q(a,b) 7 gslsfgammaincQ(a,b)
gamma inc P(a,b) 7 gslsfgammaincP(a,b)
gamma inc(a,b) 7 gslsfgammainc(a,b)
lnbeta(a,b) 7 gslsflnbeta(a,b)
beta(a,b) 7 gslsfbeta(a,b)
beta inc(a,b,c) 7 gslsfbetainc(a,b,c)
gegenpoly 1(a,b) 7 gslsfgegenpoly1(a,b)
gegenpoly 2(a,b) 7 gslsfgegenpoly2(a,b)
gegenpoly 3(a,b) 7 gslsfgegenpoly3(a,b)
gegenpoly n(a,b,c) 7 gslsfgegenpolyn(a,b,c)
hyperg 0F1(a,b) 7 gslsfhyperg0F1(a,b)
hyperg 1F1 int(a,b,c) 7 gslsfhyperg1F1int(a,b,c)
hyperg 1F1(a,b,c) 7 gslsfhyperg1F1(a,b,c)
hyperg U int(a,b,c) 7 gslsfhypergUint(a,b,c)
hyperg U(a,b,c) 7 gslsfhypergU(a,b,c)
hyperg 2F0(a,b,c) 7 gslsfhyperg2F0(a,b,c)
laguerre 1(a,b) 7 gslsflaguerre1(a,b)
laguerre 2(a,b) 7 gslsflaguerre2(a,b)
laguerre 3(a,b) 7 gslsflaguerre3(a,b)
laguerre n(a,b,c) 7 gslsflaguerren(a,b,c)
lambert W0(a) 7 gslsflambertW0(a)
lambert Wm1(a) 7 gslsflambertWm1(a)
legendre Pl(a,b) 7 gslsflegendrePl(a,b)
legendre P1(a) 7 gslsflegendreP1(a)
legendre P2(a) 7 gslsflegendreP2(a)
legendre P3(a) 7 gslsflegendreP3(a)
legendre Q0(a) 7 gslsflegendreQ0(a)
392
APPENDIX D. PLUGIN
D.2
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
gsl
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
sf
ffrandom
D.2. FFRANDOM
393
plugin to linux randow functons the range is random generator in the range from 0 to
(231 ) 1
load "ffrandom"
srandomdev();
//
//
set a true ramdom seed ..
//
warning under window this
change the seed by randinit(random()) ) so all
//
freefem++ random function are change..
394
APPENDIX D. PLUGIN
396
APPENDIX D. PLUGIN
2. You may modify your copy or copies of the Library or any portion of it, thus forming a
work based on the Library, and copy and distribute such modifications or work under the
terms of Section 1 above, provided that you also meet all of these conditions:
a) The modified work must itself be a software library.
b) You must cause the files modified to carry prominent notices stating that you changed
the files and the date of any change.
c) You must cause the whole of the work to be licensed at no charge to all third parties
under the terms of this License.
d) If a facility in the modified Library refers to a function or a table of data to be supplied
by an application program that uses the facility, other than as an argument passed when the
facility is invoked, then you must make a good faith effort to ensure that, in the event an
application does not supply such function or table, the facility still operates, and performs
whatever part of its purpose remains meaningful.
(For example, a function in a library to compute square roots has a purpose that is entirely well-defined independent of the application. Therefore, Subsection 2d requires that
any application-supplied function or table used by this function must be optional: if the
application does not supply it, the square root function must still compute square roots.)
These requirements apply to the modified work as a whole. If identifiable sections of that
work are not derived from the Library, and can be reasonably considered independent and
separate works in themselves, then this License, and its terms, do not apply to those sections
when you distribute them as separate works. But when you distribute the same sections as
part of a whole which is a work based on the Library, the distribution of the whole must
be on the terms of this License, whose permissions for other licensees extend to the entire
whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest your rights to work written
entirely by you; rather, the intent is to exercise the right to control the distribution of
derivative or collective works based on the Library.
In addition, mere aggregation of another work not based on the Library with the Library
(or with a work based on the Library) on a volume of a storage or distribution medium does
not bring the other work under the scope of this License.
3. You may opt to apply the terms of the ordinary GNU General Public License instead of
this License to a given copy of the Library. To do this, you must alter all the notices that
refer to this License, so that they refer to the ordinary GNU General Public License, version
2, instead of to this License. (If a newer version than version 2 of the ordinary GNU General
Public License has appeared, then you can specify that version instead if you wish.) Do not
make any other change in these notices.
Once this change is made in a given copy, it is irreversible for that copy, so the ordinary
GNU General Public License applies to all subsequent copies and derivative works made
from that copy.
This option is useful when you wish to copy part of the code of the Library into a program
that is not a library.
4. You may copy and distribute the Library (or a portion or derivative of it, under Section 2)
in object code or executable form under the terms of Sections 1 and 2 above provided that
you accompany it with the complete corresponding machine-readable source code, which
must be distributed under the terms of Sections 1 and 2 above on a medium customarily
used for software interchange.
If distribution of object code is made by offering access to copy from a designated place,
D.2. FFRANDOM
397
then offering equivalent access to copy the source code from the same place satisfies the
requirement to distribute the source code, even though third parties are not compelled to
copy the source along with the object code.
5. A program that contains no derivative of any portion of the Library, but is designed to
work with the Library by being compiled or linked with it, is called a work that uses the
Library. Such a work, in isolation, is not a derivative work of the Library, and therefore
falls outside the scope of this License.
However, linking a work that uses the Library with the Library creates an executable that
is a derivative of the Library (because it contains portions of the Library), rather than a
work that uses the library. The executable is therefore covered by this License. Section 6
states terms for distribution of such executables.
When a work that uses the Library uses material from a header file that is part of the
Library, the object code for the work may be a derivative work of the Library even though
the source code is not. Whether this is true is especially significant if the work can be linked
without the Library, or if the work is itself a library. The threshold for this to be true is not
precisely defined by law.
If such an object file uses only numerical parameters, data structure layouts and accessors,
and small macros and small inline functions (ten lines or less in length), then the use of the
object file is unrestricted, regardless of whether it is legally a derivative work. (Executables
containing this object code plus portions of the Library will still fall under Section 6.)
Otherwise, if the work is a derivative of the Library, you may distribute the object code for
the work under the terms of Section 6. Any executables containing that work also fall under
Section 6, whether or not they are linked directly with the Library itself.
6. As an exception to the Sections above, you may also combine or link a work that uses
the Library with the Library to produce a work containing portions of the Library, and
distribute that work under terms of your choice, provided that the terms permit modification of the work for the customers own use and reverse engineering for debugging such
modifications.
You must give prominent notice with each copy of the work that the Library is used in it
and that the Library and its use are covered by this License. You must supply a copy of
this License. If the work during execution displays copyright notices, you must include the
copyright notice for the Library among them, as well as a reference directing the user to the
copy of this License. Also, you must do one of these things:
a) Accompany the work with the complete corresponding machine-readable source code for
the Library including whatever changes were used in the work (which must be distributed
under Sections 1 and 2 above); and, if the work is an executable linked with the Library,
with the complete machine-readable work that uses the Library, as object code and/or
source code, so that the user can modify the Library and then relink to produce a modified
executable containing the modified Library. (It is understood that the user who changes
the contents of definitions files in the Library will not necessarily be able to recompile the
application to use the modified definitions.)
b) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (1) uses at run time a copy of the library already present on the users
computer system, rather than copying library functions into the executable, and (2) will
operate properly with a modified version of the library, if the user installs one, as long as
the modified version is interface-compatible with the version that the work was made with.
c) Accompany the work with a written offer, valid for at least three years, to give the same
398
APPENDIX D. PLUGIN
user the materials specified in Subsection 6a, above, for a charge no more than the cost of
performing this distribution.
d) If distribution of the work is made by offering access to copy from a designated place,
offer equivalent access to copy the above specified materials from the same place.
e) Verify that the user has already received a copy of these materials or that you have already
sent this user a copy.
For an executable, the required form of the work that uses the Library must include any
data and utility programs needed for reproducing the executable from it. However, as a
special exception, the materials to be distributed need not include anything that is normally
distributed (in either source or binary form) with the major components (compiler, kernel,
and so on) of the operating system on which the executable runs, unless that component
itself accompanies the executable.
It may happen that this requirement contradicts the license restrictions of other proprietary
libraries that do not normally accompany the operating system. Such a contradiction means
you cannot use both them and the Library together in an executable that you distribute.
7. You may place library facilities that are a work based on the Library side-by-side in a
single library together with other library facilities not covered by this License, and distribute
such a combined library, provided that the separate distribution of the work based on the
Library and of the other library facilities is otherwise permitted, and provided that you do
these two things:
a) Accompany the combined library with a copy of the same work based on the Library,
uncombined with any other library facilities. This must be distributed under the terms of
the Sections above.
b) Give prominent notice with the combined library of the fact that part of it is a work
based on the Library, and explaining where to find the accompanying uncombined form of
the same work.
8. You may not copy, modify, sublicense, link with, or distribute the Library except as
expressly provided under this License. Any attempt otherwise to copy, modify, sublicense,
link with, or distribute the Library is void, and will automatically terminate your rights
under this License. However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such parties remain in full
compliance.
9. You are not required to accept this License, since you have not signed it. However, nothing
else grants you permission to modify or distribute the Library or its derivative works. These
actions are prohibited by law if you do not accept this License. Therefore, by modifying or
distributing the Library (or any work based on the Library), you indicate your acceptance of
this License to do so, and all its terms and conditions for copying, distributing or modifying
the Library or works based on it.
10. Each time you redistribute the Library (or any work based on the Library), the recipient
automatically receives a license from the original licensor to copy, distribute, link with or
modify the Library subject to these terms and conditions. You may not impose any further
restrictions on the recipients exercise of the rights granted herein. You are not responsible
for enforcing compliance by third parties with this License.
11. If, as a consequence of a court judgment or allegation of patent infringement or for
any other reason (not limited to patent issues), conditions are imposed on you (whether by
court order, agreement or otherwise) that contradict the conditions of this License, they do
not excuse you from the conditions of this License. If you cannot distribute so as to satisfy
D.2. FFRANDOM
399
simultaneously your obligations under this License and any other pertinent obligations, then
as a consequence you may not distribute the Library at all. For example, if a patent license
would not permit royalty-free redistribution of the Library by all those who receive copies
directly or indirectly through you, then the only way you could satisfy both it and this
License would be to refrain entirely from distribution of the Library.
If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply, and the section as a whole is intended
to apply in other circumstances.
It is not the purpose of this section to induce you to infringe any patents or other property
right claims or to contest validity of any such claims; this section has the sole purpose of
protecting the integrity of the free software distribution system which is implemented by
public license practices. Many people have made generous contributions to the wide range
of software distributed through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing to distribute software
through any other system and a licensee cannot impose that choice.
This section is intended to make thoroughly clear what is believed to be a consequence of
the rest of this License.
12. If the distribution and/or use of the Library is restricted in certain countries either by
patents or by copyrighted interfaces, the original copyright holder who places the Library
under this License may add an explicit geographical distribution limitation excluding those
countries, so that distribution is permitted only in or among countries not thus excluded. In
such case, this License incorporates the limitation as if written in the body of this License.
13. The Free Software Foundation may publish revised and/or new versions of the Lesser
General Public License from time to time. Such new versions will be similar in spirit to the
present version, but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Library specifies a version
number of this License which applies to it and any later version, you have the option of
following the terms and conditions either of that version or of any later version published by
the Free Software Foundation. If the Library does not specify a license version number, you
may choose any version ever published by the Free Software Foundation.
14. If you wish to incorporate parts of the Library into other free programs whose distribution
conditions are incompatible with these, write to the author to ask for permission. For
software which is copyrighted by the Free Software Foundation, write to the Free Software
Foundation; we sometimes make exceptions for this. Our decision will be guided by the two
goals of preserving the free status of all derivatives of our free software and of promoting the
sharing and reuse of software generally.
NO WARRANTY
15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE LIBRARY AS IS WITHOUT
WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE LIBRARY
PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
400
APPENDIX D. PLUGIN
Appendix E
Keywords
Main Keywords
adaptmesh
Cmatrix
R3
bool
border
break
buildmesh
catch
cin
complex
continue
cout
element
else
end
fespace
for
func
if
ifstream
include
int
intalledge
load
macro
matrix
mesh
movemesh
ofstream
plot
problem
real
return
savemesh
solve
string
try
throw
vertex
varf
while
wait
ps
solver
CG
LU
UMFPACK
factorize
init
endl
401
402
APPENDIX E. KEYWORDS
Bibliography
[1] R. B. Lehoucq, D. C. Sorensen, and C. Yang ARPACK Users Guide: Solution
of Large-Scale Eigenvalue Problems with Implicitly Restarted Arnoldi Methods SIAM,
ISBN 0-89871-407-9 // http://www.caam.rice.edu/software/ARPACK/
[2] I. Babbu
ska, Error bounds for finite element method, Numer. Math. 16, 322-333.
[3] Y. Achdou and O. Pironneau, Computational Methods for Option Pricing. SIAM
monograph (2005).
[4] Satish Balay, Shrirang Abhyankar, Mark F. Adams, Jed Brown, Peter Brune, Kris
Buschelman, Victor Eijkhout, William D. Gropp, Dinesh Kaushik, Matthew G. Knepley,
Lois Curfman McInnes, Karl Rupp, Barry F. Smith, and Hong Zhang. PETSc Web
page. http://www.mcs.anl.gov/petsc, 2014.
[5] Satish Balay, William D. Gropp, Lois Curfman McInnes, and Barry F. Smith. Efficient
management of parallelism in object oriented numerical software libraries. In E. Arge,
A. M. Bruaset, and H. P. Langtangen, editors, Modern Software Tools in Scientific
Computing, pages 163202. Birkhauser Press, 1997.
[6] Pierre Jolivet and Frederic Nataf. Hpddm: High-Performance Unified framework for Domain Decomposition methods, MPI-C++ library. https://github.com/hpddm/hpddm.
[7] Dolean,
Jolivet and Nataf.
An Introduction to Domain Decomposition
Methods:
algorithms,
theory
and
parallel
implementation,
2015
https://hal.archives-ouvertes.fr/cel-01100932,
LectureNotestoappearinSIAMcollection,
[8] D. Bernardi, F.Hecht, K. Ohtsuka, O. Pironneau, freefem+ documentation,
on the web at ftp://www.freefem.org/freefemplus.
[9] D. Bernardi, F.Hecht, O. Pironneau, C. Prudhomme, freefem documentation,
on the web at http://www.freefem.fr/freefem
[10] T.A. Davis: Algorithm 8xx: UMFPACK V4.1, an unsymmetric-pattern multifrontal
method TOMS, 2003 (submitted for publication) http://www.cise.ufl.edu/
research/sparse/umfpack
[11] P.L. George, Automatic triangulation, Wiley 1996.
[12] F. Hecht, Outils et algorithmes pour la methode des elements finis, HdR, Universite
Pierre et Marie Curie, France, 1992
403
404
BIBLIOGRAPHY
[13] F. Hecht, The mesh adapting software: bamg. INRIA report 1998.
[14] A. Perronnet et. al. Library Modulef , INRIA, http://www.inria-rocq/
modulef
[15] M. Bernadou , J-M Boisserie and K. Hassan Sur limplmentation des
lments finis de HsieH-Clough-Tocher complet et rduit, RR 4 INRIA, (1980)
inria- 00076557
[16] A. Ern and J.-L. Guermond, Discontinuous Galerkin methods for Friedrichs symmetric systems and Second-order PDEs, SIAM J. Numer. Anal., (2005). See also:
Theory and Practice of Finite Elements, vol. 159 of Applied Mathematical Sciences,
Springer-Verlag, New York, NY, 2004.
[17] F. Hecht. C++ Tools to construct our user-level language. Vol 36, N, 2002 pp
809-836, Model. math et Anal Numer.
[18] J.L. Lions, O. Pironneau: Parallel Algorithms for boundary value problems, Note
CRAS. Dec 1998. Also : Superpositions for composite domains (to appear)
[19] B. Lucquin, O. Pironneau: Introduction to Scientific Computing Wiley 1998.
[20] I. Danaila, F. Hecht, and O. Pironneau. Simulation numerique en C++. Dunod,
Paris, 2003.
as, L. Hlava
c
ek, Mathematical theory of elastic and elasto-plastic bodies:
[21] J. Nec
An introduction, Elsevier, 1981.
[22] K. Ohtsuka, O. Pironneau and F. Hecht: Theoretical and Numerical analysis
of energy release rate in 2D fracture, INFORMATION 3 (2000), 303315.
[23] F. Preparata, M. Shamos Computational Geometry Springer series in Computer
sciences, 1984.
[24] R. Rannacher: On Chorins projection method for the incompressible Navier-Stokes
equations, in Navier-Stokes Equations: Theory and Numerical Methods (R. Rautmann, et al., eds.), Proc. Oberwolfach Conf., August 19-23, 1991, Springer, 1992
[25] J.E. Roberts and Thomas J.-M: Mixed and Hybrid Methods, Handbook of Numerical Anaysis, Vol.II, North-Holland, 1993
[26] J.L. Steger: The Chimera method of flow simulation, Workshop on applied CFD,
Univ of Tennessee Space Institute, August 1991.
[27] M. Tabata: Numerical solutions of partial differential equations II (in Japanese),
Iwanami Applied Math., 1994
[28] F. Thomasset: Implementation of finite element methods of Navier-Stokes Equations,
Springer-Verlag, 1981
[29] N. Wirth: Algorithms + Data Structures = Programs, Prentice Hall, 1976
BIBLIOGRAPHY
405
406
BIBLIOGRAPHY
[46] Astrid Sabine Sinwel, A New Family of Mixed Finite Elements for Elasticity
http://www.numa.uni-linz.ac.at/Teaching/PhD/Finished/
sinwel-diss.pdf, Thesis, 2009, Johannes Kepler Universitt, Austria
[47] Steven G. Johnson, The NLopt nonlinear-optimization package, http://
ab-initio.mit.edu/nlopt
[48] N. Hansen, The CMA Evolution Strategy, http://www.lri.fr/hansen/
cmaesintro.html
[49] A. Wchter and L. T. Biegler, On the Implementation of a Primal-Dual Interior
Point Filter Line Search Algorithm for Large-Scale Nonlinear Programming, Mathematical Programming 106(1), pp. 25-57, 2006
[50] A. Forsgren, P. E. Gill and M. H. Wright, Interior Methods for Nonlinear
Optimization, SIAM Review, Vol. 44, No 4. pp. 525-597, 2002
[51] M. Oxborrow, Traceable 2-D finite-element simulation of the whispering-gallery modes
of axisymmetric electromagnetic resonators, IEEE T. Microw. Theory 55, 12091218
(2007).
[52] I. S. Grudinin, and N. Yu, Finite-element modeling of coupled optical microdisk resonators for displacement sensing, J. Opt. Soc. Am. B 29, 3010-3014 (2012).
Index
<<, 99
matrix, 94
>>, 99
.*, 92
.*, 194
./, 92
=, 182
?:, 77
[], 28, 179, 286
, 73
, 92
3d
beam-3d.edp, 253
periodic, 242
time dependent, 39
accuracy, 31
acos, 80
acosh, 80
adaptation, 121, 140, 162
adaptmesh, 53, 65, 121, 124
abserror=, 124
anisomax=, 247
cutoff=, 125
err=, 124
errg=, 124
hmax=, 124
hmin=, 124
inquire=, 125
IsMetric=, 125
iso=, 124
keepbackvertices=, 125
maxsubdiv=, 125
metric=, 125
nbjacoby=, 124
nbsmooth=, 124
nbvx=, 124
nomeshgeneration=, 125
omega=, 124
periodic=, 126
powerin=, 125
ratio=, 124
rescaling=, 125
splitin2=, 125
splitpbedge=, 125
thetamax=, 125
uniform, 126
verbosity=, 125
adj, 114
AffineCG, 209
AffineGMRES, 209
alphanumeric, 73
append, 99
area, 76
area coordinate, 190
argument, 78
ARGV, 101
array, 74, 83, 96, 192
.imax, 87
.imin, 87
.l1, 87
.l2, 87
.linfty, 87
.max, 87
.min, 87
.sum, 87
::, 84
?:, 84
= + - * / .* ./ += -= /= *=
.*= ./= , 86
array, 96
column, 89
dot, 93
dot product, 87
FE function, 96
fespace, 172, 173
full matrix, 96
im, 84, 85
407
408
line, 89
mapping, 87
matrix, 96
max, 84
mesh, 305
min, 84, 97
quantile, 87
re, 84, 85
renumbering, 89
resize, 84
sort, 84
sum, 84
varf, 195
asin, 80
asinh, 80
assert(), 76
atan, 80
atan2, 80
atanh, 80
axisymmetric, 39
backward Euler method, 263
barycentric coordinates, 25
BDM1, 171
BDM1Ortho, 171
be, 114
whoinElement, 115
bessel, 81
BFGS, 212
BiLaplacien, 170
binary, 99
block matrix, 91
bool, 74
border, 108, 110
multy, 111
boundary condition, 194
boundingbox, 116
break, 97
broadcast, 305
bubble, 177
buildlayers, 145
buildmesh, 34
fixedborder, 109
fixedborder=, 109
fixedborder=1, 242
nbvx=, 109
INDEX
catch, 103
Cauchy, 69
ceil, 80
CG, 184
change, 133
Characteristics-Galerkin, 45
checkmovemesh, 119
Cholesky, 184, 211
cin, 76, 99
clock, 23
CMAES, 212
initialStdDev=, 213
seed=, 212
column, 89
compatibility condition, 183
compiler, 73
Complex, 74
complex, 62, 78, 92
Complex geometry, 42
concatenation, 123
cone, 145
conj, 78
connectivity, 199
continue, 97
convect, 46, 274
cos, 80
cosh, 80
cout, 76, 99
Crout, 184
cube, 134, 135, 144
flags=, 135
label=, 135
region=, 135
Curve, 164
de Moivres formula, 78
default, 99
defaultsolver, 382
defaulttoGMRES, 383
defaulttoSuperLU, 382
degree of freedom, 25
DFFT, 372
diag, 93, 280
diagonal matrix, 93
dimKrylov=, 246
Dirichlet, 31, 37, 69, 183
discontinuous functions, 290
INDEX
Discontinuous-Galerkin, 45
displacement vector, 251
divide
term to term, 92
domain decomposition, 282, 284
dot, 93
dot product, 92, 93, 97
dumptable, 76
Edge03d, 171
Edge13d, 171
Edge23d, 171
EigenValue, 261
ivalue=, 259
maxit=, 260
ncv=, 260
nev=, 259
rawvector=, 260
sigma=, 260
sym=, 259
tol=, 260
value=, 259
vector=, 259
eigenvalue problems, 38
Element, 114
elements, 25, 26
emptymesh, 118
endl, 99
erf, 81
erfc, 81
exception, 103
exec, 76, 206, 207
exit, 305
exp, 80
expression optimization, 196
external C++ function, 49
factorize=, 211
false, 74, 76
FE function
[], 179
complex, 62, 83, 92
n, 179
value, 179
FE space, 172, 173
FE-function, 82, 172, 173
FEQF, 171
409
FEQF1, 171
FEQF13d, 172
FEQF2, 171
FEQF23d, 172
FEQF5, 171
FEQF53d, 172
FEQF7, 172
FEQF9, 172
FEspace
(int ,int ), 199
ndof, 199
nt, 199
fespace, 167
BDM1, 25, 171
BDM1Ortho, 25, 171
Edge03d, 26, 171
Edge13d, 26, 171
Edge23d, 26, 171
FEQF, 171
FEQF1, 171
FEQF13d, 172
FEQF2, 171
FEQF23d, 172
FEQF5, 171
FEQF53d, 172
FEQF7, 172
FEQF9, 172
HCT, 25, 170
Morley, 25
P0, 25, 168
P03d, 26
P0Edge, 169
P1, 25, 168
P13d, 26, 168
P1b, 25, 168
P1b3d, 26, 168
P1bl, 168
P1bl3d, 168
P1dc, 25, 168
P1Edge, 169
P1nc, 25, 171
P2, 25, 168
P23d, 26
P2b, 25
P2BR, 25, 170, 377
P2dc, 25, 169
P2Edge, 169
410
P2h, 25
P2Morley, 169
P3, 25, 169
P3dc, 25, 169
P3Edge, 169
P4, 25, 169
P4dc, 25, 169
P4Edge, 169
periodic=, 184, 240
RT0, 25, 170
RT03d, 26
RT0Ortho, 25, 171
RT1, 25, 171
RT1Ortho, 25, 171
ff-mpirun, 16
ffmedit, 206
ffNLOpt, 229
FFT, 372
file
am, 345, 346
am fmt, 113, 345, 346
amdba, 345, 346
bamg, 113, 343
data base, 343
ftq, 347
mesh, 113
msh, 346
nopo, 113
finite element space, 167
Finite Volume Methods, 48
fixed, 99
flabel
change, 133
floor, 80
fluid, 273
flush, 100
for, 97
Fourier, 40
fregion
change, 133
func, 75
function
tables, 117
functions, 80
gamma, 81
geometry input , 36
INDEX
getline, 79
global
area, 76
cin, 76
endl, 76
false, 76
hTriangle, 75
label, 75
lenEdge, 75
N, 75
nTonEgde, 76
nuEdge, 76
nuTriangle, 75
pi, 76
region, 75
searchMethod, 181
true, 76
volume, 76
x, 75
y, 75
z, 75
GMRES, 184
gnuplot, 206
good, 100
graphics packages, 31
gslinterpakima, 385
gslinterpakimaperiodic, 385
gslinterpcspline, 386
gslinterpcsplineperiodic, 385
gslinterplinear, 385
gslinterppolynomial, 385
gslinterpsteffen, 385
gslspline, 385
d, 385
dd, 385
eval, 385
hat function, 25
HCT, 170
Helmholtz, 62
hmax, 357
hmin, 357
hTriangle, 75, 247
ifstream, 99
binary, 99
ill posed problems, 38
INDEX
im, 84, 85, 92
imag, 78
include, 20, 101
includepath, 20
init, 48
init=, 185
initial condition, 69
inside=, 197
int, 74
int1d, 185
int2d, 185
int3d, 184, 185
intalledges, 185, 247
interpolate, 196
inside=, 197
op=, 197
t=, 197
interpolation, 182
Irecv, 305
Isend, 305
isoline, 163
isotropic, 251
j0, 81
j1, 81
jn, 81
jump, 247
l1, 86
l2, 86
label, 75, 107, 108, 111
change, 133
label on the boundaries, 37
label=, 126
lagrangian, 120
Laplace operator, 31
lenEdge, 75, 247
level line, 47
levelset, 185
lgamma, 81
line, 89
LinearCG, 209
eps=, 209
nbiter=, 209
precon=, 209
veps=, 209
LinearGMRES, 209
411
eps=, 209
nbiter=, 209
precon=, 209
veps=, 209
linfty, 86
load, 20
loadpath, 20
log, 80
log10, 80
loop
implicite, 98
LU, 184
m, 381
macro, 47, 102, 296
parameter, 102
quote, 312
quoting, 102, 103
with parameter, 50
without parameter, 47
mass lumping, 49
matrix, 28, 75, 211
array, 89
block, 91
complex, 92
constant, 90
diag, 93, 280
factorize=, 258
im, 92
interpolate, 196
re, 92
real to complex, 92
renumbering, 89, 91
resize, 91, 93
set, 90
stiffness matrix, 28
varf, 90, 194
eps=, 194
precon=, 195
solver=, 194
solver=factorize, 194
tgv=, 195
tolpivot =, 195
MatUpWind0, 49
max, 84
maximum, 77
mean, 14
412
medit, 206, 207
meditff=, 153
order=, 153
save=, 153
membrane, 31
mesh, 75
(), 114
+, 134
[], 114
3point bending, 132
adaptation, 53, 65, 121
adj, 115
adjacent, 115
be, 115
whoinElement, 115
beam, 128
Bezier curve, 130
boundary, 115
boundingbox, 116
Cardioid, 129
Cassini Egg, 129
change, 133
connectivity, 114
Element, 115
NACA0012, 128
nbe, 115
regular, 121
Section of Engine, 130
Smiling face, 131
U-shape channel, 130
uniform, 126
V-shape cut, 131
mesh adaptation, 52
mesh3, 138
min, 84, 97
minimum, 77
mixed, 40
mixed Dirichlet Neumann, 31
modulus, 78
Morley, 169
movemesh, 119, 138
movemesh23, 138
orientation=, 138
ptmerge=, 138
transfo=, 138
mpiAllgather, 305
mpiAllReduce, 305
INDEX
mpiAlltoall, 305
mpiAnySource, 303
mpiBAND, 303
mpiBarrier, 304
mpiBXOR, 303
mpiComm, 303
mpiCommWorld, 303
mpiGather, 305
mpiGroup, 303
mpiLAND, 303
mpiLOR, 303
mpiLXOR, 303
mpiMAX, 303
mpiMIN, 303
mpiPROD, 303
mpiRank, 304
mpirank, 303
mpiReduce, 305
mpiReduceScatter, 305
mpiRequest, 303
mpiScatter, 305
mpiSize, 304
mpisize, 303
mpiSUM, 303
mpiUndefined, 303
mpiWait, 304
mpiWtick, 304
mpiWtime, 304
multi-physics system, 42
multiple meshes, 42
N, 75, 188
n, 179, 381
Navier-Stokes, 274, 276
nbcoef, 381
nbe, 114
ndof, 199
ndofK, 199
Neumann, 69, 188
Newton, 212, 258
Newton Algorithm, 56
NLCG, 211
eps=, 209
nbiter=, 209
veps=, 209
nodes, 25
non-homogeneous Dirichlet, 31
INDEX
nonlinear, 42
nonlinear problem, 39
norm, 372
normal, 47, 188
noshowbase, 99
noshowpos, 99
nt, 199, 357
nTonEdge, 47, 76
nuEdge, 76
number of degrees of freedom, 199
number of element, 199
nuTriangle, 75
nv, 357
ofstream, 99
append, 99
binary, 99
flush, 100
good, 100
seekg, 100
teelp, 100
on, 188
intersection, 275
scalar, 188
optimize=, 196
outer product, 89, 92
P, 75
P0, 168
P0Edge, 169
P1, 168
P1b, 168
P1bl, 168
P1dc, 168
P1Edge, 169
P1nc, 171
P2, 168
P2BR, 170
P2dc, 169
P2Edge, 169
P2Morley, 169
P3, 169
P3dc, 169
P4, 169
P4dc, 169
P4Edge, 169
P5Edge, 169
413
parabolic, 39
periodic, 167, 184, 240
3d, 242
pi, 76
plot, 201
aspectratio=, 202
bb=, 202
border, 110
boundary=, 203
bw=, 202
cmm=, 202
coef=, 202
cut, 204
dim=, 203
grey=, 202
hsv=, 203
mesh, 110
nbarrow=, 202
nbiso=, 202
ps=, 202
value=, 202
varrow=, 202
viso=, 47, 202
plugin
ffrandom, 81, 392
gls, 81, 82
gsl, 81, 82, 385
point
region, 114
triange, 114
polar, 78
pow, 80
ppm2rnm, 164
precision, 99
precon=, 185, 195
problem, 48, 75, 182
eps=, 185
init=, 185
precon=, 185
solver=, 185
strategy =, 185, 195
tgv=, 185
tolpivot =, 185
tolpivotsym =, 185, 195
processor, 304, 305
processorblock, 304
product
414
INDEX
Hermitian dot, 92
dot, 92, 93
outer, 92
term to term, 92
INDEX
Cholesky, 184
Crout, 184
GMRES, 184
LU, 184
sparsesolver, 184
UMFPACK, 184
sort, 84, 173
sparsesolver, 184
split=, 126
splitmesh, 127
square, 107, 247
flags=, 108
label=, 108
region=, 108
srandom, 81
srandomdev, 81
Stokes, 273
stokes, 270
stop test, 185
absolue, 278
strain tensor, 251
strategy=, 185
streamlines, 274
stress tensor, 251
string, 74
concatenation, 79
find, 79
subdomains, 290
sum, 84
tan, 80
tanh, 80
Taylor-Hood, 275
teelp, 100
tetg, 136
facetcl=, 137
holelist=, 137
nboffacetcl=, 137
nbofholes=, 137
nbofregions=, 137
regionlist=, 137
switch=, 136
tetgconvexhull, 140
tetgreconstruction, 137
tetgtransfo, 139
facetcl=, 139
nboffacetcl=, 139
415
ptmerge=, 139
refface=, 139
regionlist=, 139
switch=, 139
tgamma, 81
tgv=
< 0, 188
tolpivot=, 185
tolpivotsym=, 185
transpose, 92, 93, 97, 362
triangle
[], 114
area, 114
label, 114
region, 114
triangulate, 117
triangulation files, as well as read and write,
36
true, 74, 76
trunc, 126
label=, 126
split=, 126
try, 103
tutorial
LaplaceRT.edp, 246
adapt.edp, 123
adaptindicatorP2.edp, 246
AdaptResidualErrorIndicator.edp, 248
aTutorial.edp, 234
beam.edp, 252
BlackSchol.edp, 269
convect.edp, 268
fluidStruct.edp, 287
freeboundary.edp, 293
movemesh.edp, 120
NSUzawaCahouetChabart.edp, 276
periodic.edp, 240
periodic4.edp, 240
periodic4bis.edp, 242
readmesh.edp, 113
Schwarz-gc.edp, 285
Schwarz-no-overlap.edp, 283
Schwarz-overlap.edp, 281
StokesUzawa.edp, 275
tutotial
VI.edp, 280
type of finite element, 168
416
UMFPACK, 184
upwinding, 45
varf, 28, 75, 185, 192
array, 194
matrix, 194
optimize=, 196
variable, 73
variational formulation, 32
veps=, 211
verbosity, 17, 20
version, 76
vertex
label, 114
x, 114
y, 114
viso, 47
volume, 76
weak form, 32
while, 97
whoinElement, 114
write files, 113
x, 75
y, 75
y0, 81
y1, 81
yn, 81
z, 75
INDEX
Book Description
Fruit of a long maturing process freefem, in its last avatar, FreeFem++ , is a high
level integrated development environment (IDE) for partial differential equations
(PDE). It is the ideal tool for teaching the finite element method but it is also perfect
for research to quickly test new ideas or multi-physics and complex applications.
FreeFem++ has an advanced automatic mesh generator, capable of a posteriori mesh adaptation; it has a general purpose elliptic solver interfaced with fast
algorithms such as the multi-frontal method UMFPACK. Hyperbolic and parabolic
problems are solved by iterative algorithms prescribed by the user with the high
level language of FreeFem++ . It has several triangular finite elements, including
discontinuous elements. Finally everything is there in FreeFem++ to prepare research quality reports: color display online with zooming and other features and
postscript printouts.
This book is ideal for students at Master level, for researchers at any level and for
engineers also in financial mathematics.
Editorial Reviews
. . . Impossible to put the book down, suspense right up to the last page. . .
A. Tanh, Siam Chronicle.
. . . The chapter on discontinuous fems is so hilarious . . . .
.
B. Galerkine,