Chapter 3  The Gory Details

3.1  Programs

Gfem is a small language which generally follows the syntax of the language Pascal. See the list below for the reserved words of this language.

The reserved word begin can be replaced by { and end by }.

C programmers: caution the syntax is "...};" while most C constructs use " ...;}"

Example 1: Triangulates a circle and plot f = x*y

 border(1,0,6.28,20) begin x:=cos(t); y:=sin(t); end; buildmesh(200); f=x*y; plot(f); 

Example 2: on the circle solve the Dirichlet problem

-Δ(u) = x*y with u=0 on ∂ Ω

 border(1,0,6.28,20) begin x:=cos(t); y:=sin(t); end; buildmesh(200); solve(u) begin onbdy(1) u =0; pde(u) -laplace(u) = x*y ; end; plot(u); 

3.2  List of reserved words

 Keywords Explanations begin, { Begin a new block end, } End a block if, then, else, or, and,iter Conditionnals and Loops x,y,t,ib,iv,region,nx,ny Mesh variables log, exp, sqrt, abs, min, max sin, asin, tan, atan, cos, acos Mathematical Functions cosh, sinh, tanh I, Re, Im Complex Numbers complex Enter Complex Number Mode buildmesh, savemesh, loadmesh, adaptmesh Mesh related Functions build, save , load and mesh adaptation one, scal, dx, dy, dxx, dyy, dxy, dyx, convect Mathematical operators can be called wherever you want solve Enter Solver Mode pde, id, dnu laplace, div onbdy Solver Functions plot, plot3d graphical functions: plot isolines in 2D and Elevated Surface in 3D save, load, saveall Saving and Loading Functions Change the Wait State: if wait wait, nowait, changewait then the user must click in the window to continue precise, halt, include, evalfct, exec, user Miscellaneous Functions
 Table 3.1: Gfem Keywords

3.3  Building a mesh

3.3.1  Triangulations

To create a triangulation you must either

• Open a project
• Read an old triangulation stored in text format
• Execute a program which contains the keyword buildmesh
• Create one by hand drawing the boundary of Ω and activate the menu Triangulate (Macintosh only).

In integrated environments, once created, triangulations can be displayed, stored on disk in Gfem format or in text format or even a zoom of its graphic display can be stored in Postscript format (to include it in a TeX file for example).

Gfem stores for each vertex its number and boundary identification number and for each triangle its number and region identification number. Edges number are not stored, but can be recovered by program if needed.

3.3.2  Border(), buildmesh(), polygon()

Use it to triangulate domain defined by its boundary. The syntax is

  border(ib,t_min,t_max,nb_t) begin ...x:=f(t); ...y:=g(t)... end; buildmesh(nb_max); 

where each line with border could be replaced by a line with polygon

 polygon(ib,'file_name'[,nb_t]); 

where f,g are generic functions and the [...] denotes an optional addition. The boundary is given in parametric form. The name of the parameter must be t and the two coordinates must be x and y . When the parameter goes from t_min to t_max the boundary must be scanned so as to have Ω on its left, meaning counter clockwise if it is the outer boundary and clockwise if it is the boundary of a hole. Boundaries must be closed but they may be entered by parts, each part with one instruction border , and have inner dividing curves; nb_t points are put on the boundary with values t = tmin + i * (tmaxtmin) / (nbt−1) where i takes integer values from 0 to nb_t-1 .

The triangulation is created by a Delaunay-Voronoi method with nb_max vertices at most. The size of the triangles is monitored by the size of the nearest boundary edge. Therefore local refinement can be achieved by adding inner artificial boundaries.

Triangulation may have boundaries with several connected components. Each connected component is numbered by the integer ib .

Inner boundaries (i.e. boundaries having the domain on both sides) can be useful either to separate regions or to monitor the triangulation by forcing vertices and edges in it. They must be oriented so that they leave Ω on their right if they are closed. If they do not carry any boundary conditions they should be given identification number ib=0 .

The usual if... then ... else statement can be used with the compound statement: begin...end . This allows piecewise parametric definitions of complicated or polygonal boundaries.

The boundary identification number ib can be overwritten.For example:

 border(2,0,4,41) begin if(t<=1)then { x:=t; y:=0 }; if((t>1)and(t<2))then { x:=1; y:=t-1; ib=1 }; if((t>=2)and(t<=3))then { x:=3-t; y:=1 }; if(t>3)then { x:=0; y:=4-t } end; buildmesh(400); 

Recall that begin and { is the same and so is end and }. Here one side of the unit square has ib=1. The 3 other sides have ib=2.

The keyword polygon causes a sequence of boundary points to be read from the file file_name which must be in the same directory as the program. All points must be in sequential order and describing part of the boundary counter clockwise; the last one should be equal to the first one for a closed curve.

The format is

 x[0] y[0] x[1] y[1] .... 

each being separated by a tab or a carriage return. The last parameter nb_t is optional; it means that each segment will be divided into nb_t1+ equal segments (i.e. nb_t points are added on each segments).

For example

 polygon(1,'mypoints.dta',2); buildmesh(100); 

with the file mypoints.dta containing

 0. 0. 1. 0. 1. 1. 0. 1. 0. 0. 

triangulates the unit square with 4 points on each side and gives ib=1 to its boundary. Note that polygon(1,’mypoints.dta’) is like polygon(1,’mypoints.dta’,0).

buildmesh and domain decomposition

There is a problem with buildmesh when doing domain decomposition: by default Gfem swap the diagonals at the corners of the domain if the triangle has two boundary edges. This will lead to bad domain decomposition at the sub-domain interfaces.

To solve this, there is a new flag for buildmesh which is optional:

buildmesh(<max_number_of_vertices>, <flag>)
where <flag> =

 = 0  classic way: do diagonal swaping = 1  domain decomposition: no diagonal swaping

3.3.3  Geometric variables, inner and region bdy, normal

• x,y refers to the coordinates in the domain
• ib refers to the boundary identification number; it is zero inside the domain.
• nx and ny refer to the x-y components of the normal on the boundary vertices; it is zero on all inner vertices.
• region refers to the domain identification number which is itself based on an internal number, ngt, assigned to each triangle by the triangulation constructor.

Inner boundaries which divide the domain into several simply connected components are useful to define piecewise discontinuous functions such as dielectric constants, Young modulus...

Inner boundaries may meet other boundaries only at their vertices. Such inner boundaries will split the domain in several sub-domains.

3.3.4  Regions

A sub-domain is a piece of the domain which is simply connected and delimited by boundaries.

Each sub-domain has a region number assigned to it. This is done by Gfem, not by the user. Every time border is called, an internal number ngt is incremented by 1. Then when the key word border is invoked the last edge of this portion of boundary assigns this number to the triangle which lies on its left. Finally all triangles which are in this subdomain are reassigned this number.

At the end of the triangulation process, each triangle has a well defined number ngt. The number region is a piecewise linear continuous interpolation at the vertices of ngt. To be exact, the value of region at a vertex (x0,y0) is the value of ngt at (x0,y0−10−6), except if precise is set in which case region is equal to ngt.

3.4  Functions

3.4.1  Functions and scalars

Functions are either read or created.

• Functions can be read from a file if its values at the vertices of the triangulation are stored in text format. (Open a .dta example with a text editor to see the format).
• Functions can be created by executing a program. An instruction like f=x*y really means that f(x,y)=x*y

for all x and y. Here x and y refer to the coordinates in the domain represented by the triangulation.

• Functions can be created with other previously defined functions such as in g=sin(x*y); f=exp(g); .
• Four other variables can be used besides x, y, iv, t :  nx, ny, ib, region.

Most usual functions can be used:

 max, min, abs, atan, sqrt, cos, sin, tan, acos, asin, one, cosh, sinh, tanh, log, exp 

one(xy<0)+ for instance means 1 if xy<0+ and 0 otherwise.

Operators:

 and, or, < , <=, < , >=, ==, +, -, *, /, ^ x^2 means x*x 

Functions created by a program are displayed only if the key word plot() or plot3d() is used ( here plot(f) ).

Derivatives of functions can be created by the keywords dx() and dy() .

Unless precise is set, they are interpolated so the results is also continuous piecewise linear (or discontinuous when precise is set). Similarly the convection operator convect(f,u1,u2,dt) defines a new function which is approximately

 f(x−u1(x,y)dt , y−u2(x,y)dt)

Scalars are also helpful to create functions. Since no data array is attached to a scalar the symbol := is useful to create them, as in

  a:= (1+sqrt(5))/2; f= x*cos(a*pi*y); 

Here f is a function, a is a scalar and pi is a (predefined) a scalar.

It is possible to evaluate a function at a point as in a:=f(1,0) Here the value of a will be 1 because f(1,0) means f at x=1 and y=0 .

3.4.2  Building functions

There are 6 predefined functions: x,y,ib,region, nx, ny .

• The coordinate x is horizontal and y is vertical.
• ib = {

 0  inside  Ω > 0  on  ∂ Ω

On ∂ Ω it is equal to the boundary identification number.

The usual if... then ... else statement can be used with an important restriction on the logical expression which must return a scalar value:

 if( logical expression) then { statement; ....; statement; } else { ..... }; 

The logical expression controls the if by its return being 0 or >0, it is evaluated only once (i.e. with x, y being the coordinates of the first vertex, if there are functions inside the logical expression). Auxiliary variables can be used.

In order to minimize the memory the symbol := tells the compiler not to allocate a data array to this variable. Thus v=sin(a*pi*x); generates an array for v but no array is assigned to a in the statement a:=2 .

3.4.3  Value of a function at one point

If f has been defined earlier then it is possible to write a:=f(1.2,3.1); Then a has the value of f at x=1.2 and y=3.1 .

It is also allowed to do

 x1:=1.2; y1:=1.6; a:=f(x1,2*y1); save('a.dta',a); 

Remark: Recall that ,a being a scalar, its value is appended to the file a.dta.

3.4.4  Special functions:dx(), dy(), convect()

dx(f) is the partial derivative of f with respect to x ; the result is piecewise constant when precise is set and interpolated with mass lumping as a the piecewise linear function when precise is not set.

Note that dx() is a non local operator so statements like f=dx(f) would give the wrong answer because the new value for f is place before the end of the use of the old one.

The Finite Element Method does not handle convection terms properly when they dominate the viscous terms: upwinding is necessary; convect provides a tool for Lagrangian upwinding. By g=convect(f,u,v,dt) Gfem construct an approximation of

 f(x−u1(x,y)dt,y−u2(x,y)dt)

Recall that when

 ∂ f ∂ t
+ u
 ∂ f ∂ x
+ v
 ∂ f ∂ y
limdt → 0
 f(x,y,t) − f(x−u(x,y)dt,y−v(x,y)dt,t−dt) dt

Thus to solve

 ∂ f ∂ t
+ u
 ∂ f ∂ x
v
 ∂ f ∂ y

in a much more stable way that if the operator dx(.) and dy(.) were use, the following scheme can be used:

 iter(n) begin id(f)/dt - laplace(f)*mu =g + convect(oldf,u,v,dt)/dt; oldf = f end; 

Remark: Note that convect is a nonlocal operator. The statement f = convect(f,u,v,dt) would give an incorrect result because it modifies f at some vertex where the old value is still needed later. It is necessary to do

 g=convect(f,u,v,dt); f=g; 

3.5  Global operators

It is important to understand the difference between a global operator such as dx() and a local operator such as sin() .

To compute dx(f) at vertex q we need f at all neighbors of q. Therefore evaluation of dx(2*f) require the computation of 2*f at all neighbor vertices of q before applying dx() ; but in which memory would the result be stored? Therefore Gfem does not allow this and forces the user to declare a function g =2*f before evaluation of dx(g) ; Hence in

 g = 2*f; h = dx(g) * dy(f); 

the equal sign forces the evaluation of g at all vertices, then when the second equal signs forces the evaluation of the expression on the right of h at all vertices , everything is ready for it.

Global operators are

 dx(), dy(), convect(), intt[], int()[] 

Example of forbidden expressions:

 intt[f+g], dx(dy(g)), dx(sin(f)), convect(2*u...) 

3.6  Integrals

• effect:

intt returns a complex or real number, an integral with respect to x,y int returns a complex or real number, an integral on a curve

• syntax:

intt[f] or intt(n1)[f] or intt(n1,n2)[f] or intt(n1,n2,n3)[f] int(n1)[f] or int(n1,n2)[f] or int(n1,n2,n3)[f]

where n1,n2,n3 are boundary or subdomain identification numbers and where f is an array function.

 border(1)... end; /* a border has number 1 */ ... buildmesh(...); f = 2 * x; /* * nx,ny are the components of the boundary normal */ g = f * (nx + ny); /* * can't do r:= int[2*x] */ r:= int[f]; s:=int(1)[g]; /* * this is the only way to display the result */ save('r.dta',r); save('s.dta',s); 
• Restrictions:

int and intt are global operators, so the values of the integrands are needed at all vertices at once, therefore you can’t put an expression for the integrand, it must be a function.

Be careful to check that the region number are correct when you use intt(n)[f] .

Unfortunately freefem does not store the edges numbers. Hence there are ambiguities at vertices which are at the intersections of 2 boundaries. The following convention is used: int(n)[g] computes the integral of g on all segments of the boundary (both ends have id boundary number !=0) with one vertex boundary id number = n. (Remember that you can control the boundary id number of the boundary ends by the order in which you place the corresponding border call or by an extra argument in border )

3.7  Solving an equation

3.7.1  Onbdy()

Its purpose is to define a boundary condition for a Partial Differential Equation (PDE).

The general syntax is

 onbdy(ib1, ib2,...) id(u)+*dnu(u) = g onbdy(ib1, ib2,...) u = g 

where ib’s are boundary identification numbers, <expression> is a generic expression and g a generic function.

The term id(u) may be absent as in -dnu(u)=g . dnu(u) represents the conormal of the PDE, i.e.

±
 → ∇
u . n

when the PDE operator is

a * u −
 → ∇
.(±
 → ∇
u)

3.7.2  Pde()

The syntax for pde is

 pde(u) [+-] op1(u)[*/]exp1 [+-] op2(u)[*/]exp2...=exp3 

It defines a partial differential equation with non constant coefficients where op is one of the operator:

• id()
• dx()
• dy()
• laplace()
• dxx()
• dxy()
• dyx()
• dyy()

and where [*/] means either a * or a / and similarly for ±. Note that the expressions are necessarily AFTER the operator while in practice they are between the 2 differentiations for laplace...dyy . Thus laplace(u)*(x+y) means

∇.((x+y)∇ u)

.Similarly dxy(u)*f means

fu/∂ y/∂ x.

3.7.3  Solve()

The syntax for a single unknown function u solution of a PDE is

 solve(u) begin onbdy()...; onbdy()...; ...; pde(u)... end; 

For 2-systems and the use of solve(u,v), see the section 2-Systems . It defines a PDE and its boundary conditions. It will be solved by the Finite Element Method of degree 1 on triangles and a Gauss factorization.

Once the matrix is built and factorized solve may be called again by solve(u,-1)...; then the matrix is not rebuilt nor factorized and only a solution of the linear system is performed by an up and a down sweep in the Gauss algorithm only. This saves a lot of CPU time whenever possible. Several matrices can be stored and used simultaneously, in which case the sequence is

 solve(u,i)...; ... solve(u,-i)...; 

where i is a scalar variable (not an array function).

However matrices must be constructed in the natural order: i=1 first then i=2.... after they can be re-used in any order. One can also re-use an old matrix with a new definition, as in

 solve(u,i)...; ... solve(u,i)...; solve(u,\pm i)...; 

Notice that solve(u) is equivalent to solve(u,1) .

Remark: 2-Systems have their own matrices, so they do not count in the previous ordering.

3.7.4  2-Systems

Before going to systems make sure that your 2 pde’s are indeed coupled and that no simple iteration loop will solve it, because 2-systems are significantly more computer intensive than scalar equations.

Systems with 2 unknowns can be solved by

  solve(u,v) begin onbdy(..) ...dnu(u)...=.../* defines a bdy condition for u */ onbdy(..) u =... /* defines a bdy conditions for v */ pde(u) ... /* defines PDE for u */ onbdy(..) /* defines bdy conditions for v */ pde(v) ... /* defines PDE for u */ end; 

The syntax for solve is the same as for scalar PDEs; so solve(u,v,1) is ok for instance. The equations above can be in any orders; several onbdy() can be used in case of multiple boundaries...

The syntax for onbdy is the same as in the scalar case; either Dirichlet or mixed-Neumann, but the later can have more than one id() and only one dnu() .

Dirichlet is treated as if it was mixed Neumann with a small coefficient. For instance u=2 is replaced by dnu(u)+1.e10*u=2.e10 , with quadrature at the vertices.

Conditions coupling u,v are allowed for mixed Neumann only, such as id(u)+id(v)+dnu(v)=1. (As said later this is an equation for v ).

In solve(u,v,i) begin .. end; when i>0 the linear system is built factorized and solved. When i<0 , it is only solved; this is useful when only the right hand side in the boundary conditions or in the equations have change. When i<0, i refers to a linear system i>0 of SAME TYPE, so that scalar systems and 2-systems have their own count.

Remark: saveall('filename',u,v) works also.

The syntax for pde() is the same as for the scalar case. Deciding which equation is an equation for u or v is important in the resolution of the linear system (which is done by Gauss block factorization) because some block may not be definite matrices if the equations are not well chosen.

• A boundary condition like  onbdy(...) ... dnu(u) ... = ...;  is a boundary condition associated to u, even if it contains id(v) .
• Obviously a boundary condition like  onbdy(...) u...=...; is also associated with u (the syntax forbids any v -operator in this case).
• If u is the array function in a pde(u) then what follows is the PDE associated to u .

3.7.5  Boundary conditions at corners

Corners where boundary conditions change from Neumann to Dirichlet are ambiguous because Dirichlet conditions are assigned to vertices while Neumann conditions should be assigned to boundary edges; yet Gfem does not give access to edge numbers. Understanding how these are implemented helps overcome the difficulty.

All boundary conditions are converted to mixed Fourier/Robin conditions:

 id(u) a + dnu(u) b = c; 

For Dirichlet conditions a is set to 1.0e12 and c is multiplied by the same; for Neumann a=0 . Thus Neumann condition is present even when there is Dirichlet but the later overrules the former because of the large penalty number. Functions a,b,c are piecewise linear continuous, or discontinuous if precise is set.

In case of Dirichlet-Neumann corner (with Dirichlet on one side and Neumann on the other) it is usually better to put a Dirichlet logic at the corner. But if fine precision is needed then the option precise can guarantee that the integral on the edge near the corner on the Neumann side is properly taken into account because then the corner has a Dirichlet value and a Neumann value by the fact that functions are discontinuous.

3.7.6  Weak formulation

The new keyword varsolve allows the user to enter PDEs in weak form. Syntax:

 varsolve(; ,<>) <> : >; 

where

• <unknown function list> and
• <test function list> are one or two function names separated by a comma.
• <int> is a positive or negative integer
• instruction is one instruction or more if they are enclosed within begin end or {}
• <expression> is an expression returning a real or complex number

We have used the notation << >> whenever the entities can be omitted. Examples

 varsolve(u;w) /* Dirichlet problem -laplace(u) =x*y */ begin onbdy(1) u = 0; f = dx(u)*dx(w) + dy(u)*dy(w) g = x*y; end : intt[f] - intt[g,w]; varsolve(u;w,-1) /* same with prefactorized matrix */ begin onbdy(1) u = 0; f = dx(u)*dx(w) + dy(u)*dy(w) g = x*y; end : intt[f] - intt[g,w]; varsolve(u;w) /* Neuman problem u-laplace(u) = x*y */ begin f = dx(u)*dx(w) + dy(u)*dy(w) -x*y; g = x; end : intt[f] + int[u,w] - int(1)[g,w]; varsolve(u,v;w,s) /* Lame's equations */ begin onbdy(1) u=0; onbdy(1) v=0; e11 = dx(u); e22 = dy(v); e12 = 0.5*(dx(v)+dy(u)); e21 = e12; dive = e11 + e22; s11w=2*(lambda*dive+2*mu*e11)*dx(w); s22s=2*(lambda*dive+2*mu+e22)*dy(s); s12s = 2*mu*e12*(dy(w)+dx(s)); s21w = s12s; a = s11w+s22s+s12s+s21w +0.1*s; end : intt[a]; 

How does it works The interpreter evaluates the expression after the ":" for each triangle and for each 3 vertices; if there is an instruction prior the ":" it is also evaluated similarly. Each evaluation is done with one of the unknown and one of the test functions being 1 at one vertices and zero at the 2 others. This will give an element of the contribution of the triangle to the linear system of the problem. The right hand side is constructed by having all unknowns equal to zero and one test function equal to one at one vertex. whenever integrals appear they are computed on the current triangle only.

Note that varsolve takes longer than solve because derivatives like dx(u) are evaluated 9 times instead of once.

3.8  Results

Within a program the keyword plot(u) will display u .

Instruction save(’filename’,u) will save the data array u on disk. If u is a scalar variable then the (single) value of u is appended to the file (this is useful for time dependent problems or any problem with iteration loop.).

Instruction savemesh(’filename’) will save the triangulation on disk.

The file must be in the default directory, else it won’t be found. The file format is best seen by opening them with a text editor. For a data array f it is:

 ns f[0] .... f[ns-1] 

(ns is the number of vertices)

If f is a constant, its single value is appended to the end of the file; this is useful for time dependent problems or any problem with iteration loop.

If precise is set still the function stored by save is interpolated on the vertices as the P1 continuous function given by mass lumping (see above).

For triangulations the file format is (nt = number of triangles):

 ns nt q[0].x q[0].y ib[i] ... q[n-1].x q[n-1].y ib[n-1] me[0][0] me[0][1] me[0][2] ngt[0] ... me[n-1][0] me[n-1][1] me[n-1][2] ngt[n-1] 
Remark:

Gfem uses the Fortran standard for me[][] and numbers the vertices starting from number 1 instead of 0 as in the C-standard. Thus in C-programs one must use me[][]-1 .

Remark:

Other formats are also recognized by freefem via their file name extensions for our own internal use we have defined .amdba and .am_fmt. You can do the same if your format is not ours.

  loadmesh('mesh.amdba'); /* amdba format (Dassault aviation) */ loadmesh('mesh.am_fmt'); /* am_fmt format of MODULEF */ 
Remark:

There is an optional arguments for the functions load, save, loadmesh, savemesh. This is the 2nd or 3rd argument of these functions. Here are the prototypes:

 save(, [,]) load(, [,]) savemesh([,]) loadmesh([,]) 

As an example see nsstepad.pde which use this feature to save the mesh and the solution at each adaptation of the mesh. This special feature allows you to save or load a generic filename with a counter, the final filename is built like this ’<generic filename>-<counter>’.

3.8.2  Saveall()

The purpose is to solve all the data for a PDE or a 2-system with only one instruction. It is meant for those who want to write their own solvers.

The syntax is:

 saveall('file_name', var_name1,...) 

The syntax is exactly the same as that of solve(,) except that the first parameter is the file name. The other parameters are used only to indicate to the interpreter which is/are the unknown function.

The file format for the scalar equation (laplace is decomposed on nuxx, nuyy)

 u=p if Dirichlet c u+dnu(u)=g if Neumann b u-dx(nuxx dx(u))-dx(nuxy dy(u))-dy(nuyx dx))-dy(nuyy dy(u)) + a1 dx(u) + a2 dy(u) =f 

is that each line has all the values for x,y being a vertex: f, g, p, b, c, a1, a2, nuxx, nuxy, nuyx, nuyy.

The actual routine is in C++

 int saveparam(fcts *param, triangulation* t, char *filename, int N) { int k, ns = t->np; ofstream file(filename); file<f[k]<<" " ; file<<" "; file << (param)->g[k]<<" " ; file<<" "; file << (param)->p[k]<<" " ; file<<" "; file << (param)->b[k]<<" " ; file<<" "; file << (param)->c[k]<<" " ; file<<" "; file << (param)->a1[k]<<" " ; file<<" "; file << (param)->a2[k]<<" " ; file<<" "; file << (param)->nuxx[k]<<" " ; file<<" "; file << (param)->nuxy[k]<<" " ; file<<" "; file << (param)->nuyx[k]<<" " ; file<<" "; file << (param)->nuyy[k]<<" " ; file<<" "; file << endl; } } 

The same function is used for complex coefficients, by overloading the operator <<:

 friend ostream& operator<<(ostream& os, const complex& a) { os<

For 2-systems also the same is used with

 ostream& operator<<(ostream& os, cvect& a) { for(int i=0; i

where N=2 .

A Dirichlet condition is applied whenever p[k](?). ( Dirichlet conditions with value 0 are changed to value 1e-10 )

3.9  Other features

Gfem supports other interesting features:

3.9.1  Iter

The syntax is:

 iter(j){....} 

where j refers to the number of loops; j can be the result of an expression (as in iter(i*k)).

Imbedded loops are not allowed. You can use iter with the adaptation features of Gfem.

3.9.2  Complex numbers

Gfem can handle complex coefficients with 4 dedicated keywords:

• complex : to tell Gfem that complex number will be used. When it is used it must be located at the beginning of the program before any function declarations, otherwise the results will be incorrect. It can appear more than once in the program but only the first occurrence counts.
• I: for sqrt(-1) .
• Re : for the real part.
• Im : for the imaginary part.

There is purposely no conjug function but barz=Re(z)-I*Im(z) will do.

By default all graphics display the real part. To display the imaginary part do plot(Im(f)).

The functions implemented for complex numbers are:

• cos
• sin
• zx where z is complex and x is a float

The linear systems for the PDE are solved by a Gauss complex LU factorization.

WARNING: failure to declare complex in the program implies all computation will be done in real, even if I is used.

3.9.3  Scal()

The instruction a:=scal(f,g); does

a =
 Ω
f(x,y
 g
(x,ydxdy

where Ω is the triangulated domain.

3.9.4  Wait, nowait, changewait

Whenever there is a plot command, Gfem stops to let the user see the result. By using nowait no stop will be made;

• wait turns back the stop option on.
• changewait toggles the option from on to off or off to on.

Remark under X11: If you click the right button in the window, the next time the solver will give the hand to the plotter the program will stop.

3.9.5  One Dimensional Plots

This function is only available under integrated environments.

The last function defined by the keyword plot is displayed. It can be visualized in several fashion, one of which being a one dimensional plot along any segment defined by the mouse. Selection of this menu brings causes Gfem to waits for the user input which should be the line segment on which the function is to be displayed. Thus one should press the mouse at the beginning point then drag the mouse and release the button at the final point

It is safe to click in the window after to check that the function display is correct. What is seen is a

 t → f(x(t),y(t))

Plot where [x(t),y(t)]t is the segment drawn by the user and f is the last function displayed in the plot window.

The abscissa is the distance with the beginning point.

3.9.6  Precise

This keyword warns Gfem that precise quadrature formula must be used. Hence array-functions are discretized as piecewise linear discontinuous functions on the triangulation. Then all integrals are computed with 3 inner Gauss points slightly inside each triangle.

This option consumes more memory (3nt instead of ns per functions, i.e. 9 times more approximately, but still it is nothing compared with the memory that a matrix of the linear system of a PDE requires) because each array-function is stored by 3 values on each triangles.

It is a good idea to use it in conjunction with convect and/or discontinuous nonlinear terms.

3.9.7  Exec(), user(), how to link an external function to Gfem

exec('prog_name') will launch the application prog_name . It is useful to execute an external PDE solver for instance especially under Unix. It is not implemented for Macintosh because there is no simple way to return to MacGfem after progname has ended. The same can be achieved manually by a suitable combination of saveall, wait and load and simultaneous execution under multifinder.

user(what,f) calls the C++ function in a j-loop:

 for (int j=0; j
• creal is a scalar (float ) or a complex number if complex has been set; nquad=3*nt if precise is set and ns otherwise.
• what is intended for users who need several such functions. Then all can be put in the super function user and selection is by an if statement on what .
• Within gfemuser access to all global variables are of course possible: the triangulation (ns,nt, me, q, ng,ngt, area... ) ... refer to the file fem.C for more details.

Remark: An example of such gfemuser function is in fem.C ; if you wish to put your own you must compile and link it. Under Unix, it is easy. Under the Macintosh system, either you use freefem, which is Gfem’s kernel, or you must ask us a library version of MacGfem.

3.10  Language internals

• Like most interpreters it has a lexical and a syntaxic part. In the lexical part the source is broken into tokens and recognized as symbols (see the enum symbol in the source file lexical.h). For instance if the first character is a digit then it is a number and the symbol type associated is cste. This job is done by function nextsym.In addition it constructs a table of constants and variables (which for convenience contains also the reserved words of the language).
• The lexical analyzer is a function called by the syntax analyzer. Hence the second is the main routine in the program except for a few initialization; it’s name is instruction .The syntax analysis is driven by the syntax rules because the language is LL(1). Thus there is C-function for each non-terminal.
• The program does not generate an object code but a tree. For example, parsing x * y would generate a tree with root  *  and two branches x and y . Trees are C-struct with four pointers for the 4 branches (here two would point to NULL) and a symbol for the operator. The C-function which builds trees is called plante . In the end the program is transformed into a full tree and to execute the program there is only one thing to do: evaluate the operator at the root of the tree.
• The evaluation of the program is done by the C-function eval . It looks at the root symbol and perform the corresponding operation. Here it would do: return "value pointed by L1"*"value pointed by L2" if L1 and L2 where the addresses of the two branches.
• The art of the game is to associate a tree to each operation. For example when the value of the variable x is required, this is also done by a tree which has the operator "oldvar" as root. The trickiest of all is the compound instruction {...;....}; . Here { is considered as an operator with one branch on the current instruction and one branch on the next one. Similarly for the  if...then... else instruction.

Suppose one wants to add an instruction to freefem, here is what must be done:

• Make sure the syntax is LL(1) and does not conflict with the old one.
• Add reserved words to the table with installe . As there will be a new Symbol, update the list of symbols (an enum structure). Add the C-functions for the syntax analysis according to the diagrams. Modify eval by adding to the switch the new case.

Here is the very simple structure we used for the nodes of the tree:

 typedef struct noeud { Symbol symb; creal value; ident *name; long junk; char *path; struct noeud *l1, *l2, *l3, *l4; } noeud, *arbre; `