Skip to content

andyandreolli/FreeFEM-Cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

FreeFEM++ Cheatsheet

Data types: numbers

dataType variable_name = expression;

You can also initialise variables without assigning them a value; multiple initialisations are allowed.

  • int > integer number
  • real > real number
  • complex > complex number
    • complex a = 12 + 3i;

Strings

string s = "This is a string";

Global variables

  • P > returns current point, eg P.x and P.y are coordinates
  • x, y, z > return coordinates of current point
  • label > returns the label of the boundary the selected point belongs to, 0 if doesn't belong to any
  • region > returns region no of current point
  • N > returns outwards facing versor for current point if on a curve defined by border

Arithmetics

  • +, -, *, /, ^ > classic operators
  • % > yields remainder
    • notice that if operands are real, this command calculates the remainder of the integer parts
    • cannot be used with complex numbers
  • conj(no.), real(no.), imag(no.) > returns conjugate
  • max(a,b), min(a,b)
    • cannot be used with complex numbers
  • a ? b : c > is equal to b if a is true, otherwise it's equal to c (arithmetical if)

Arrays

real[int] = array_name(size);
//alternatively, like in MATLAB; size is calculated automatically
real[int] = array_name(1:3:10);

Size can be left unallocated; multiple statements are allowed.

array_name.resize(newsize);

Matrices

matrix M = [[0,1,0],
            [1,0,0],
            [0,0,1]];
  • to change solver: set(M,solver=sparsesolver);
    • available: LU, CG, Crout, Cholesky, GMRES, UMFPACK
    • default: GMRES
  • transposition: '
  • term by term operations: .*, ./
  • solve linear system: b = A^-1 x
    • notice: A^-1 is an operator, basically

Loops

for (int i=1; i<=10; i++)
  {run code}
while (i<=10)
 {run code}

continue statement will bypass the code from continue to the end of the loop; break ends the loop.


Built in functions

  • pow(x,y), exp(x,y), log(x) [natural], log10(x) [base 10]
  • sin(x), cos(x), tan(x), asin(x), acos(x), atan(x)
  • sinh(x), cosh(x), tanh(x), asinh(x), acosh(x), atanh(x)
  • floor(x) > returns largest integer not greater than x
  • ceil(x) > returns smallest integer not less than x
  • rint(x) > returns closer integer

Analyitical functions

func f = cos(x) + sin(x);

Remark: function type is given by expression type


Mesh generation

border gamma(t=a,b) {x=f(t); y=g(t); label=1};

This generates a border that is oriented from a to b, domain must be on the left, so on the right you have the outwards facing versor. Then border is discretised:

gamma(m); //negative values for reversed orientation

To build mesh, borders must intersect on extremes only and result must be a closed curve.

mesh Th1 = buildmesh(gamma1(10)+gamma2(5));

FE Space

fespace Xh(Th,P1);
Xh uh,vh; //initialises FE functions
Xh fef = x^2 + y^2; //automatically interpolated

To calculate integrals on the defined space:

int2d(mesh)(expression)//integration on the mesh
int1d(mesh, label) //integration on boundary "label"
on(label,label,...,uh=0) //boundary condition

To calculate derivatives of FE functions - DOESN'T WORK WITH ANALYTICAL ONES:

dx(fef);

To access only the degrees of freedom of a FE function, use function_name[] (returns FE function as a vector).

Solve with problem

problem laplace(uh,vh) = int2d(mesh)(integrand)
                         + on(label,condition);
                         //implicitly: integral = 0
laplace; //solution of the equation

You can set the solver and the preconditioner:

problem laplace(uh,vh,solver=CG,precon=P) = ...;

Solve with varf

varf B(u,v) = int2d(Th)(integrand)
              + on(label, condition); //variational form
//define fespace, functions
matrix K = B(Xh,Xh); //stiffness matrix
g[] = -B(0,Xh); //known term
uh[] = K^-1 * g; //solution of linear system

Plotting

plot(thing_to_plot, value=1, fill=1);

You might want to pass argument nbiso=40 as well. Don't really know what it does.

Input/output

Syntax is the same as C++: uses cin, cout, endl, <<, >>. To print some text on the console:

cout << "Text that is printed" <<endl;

To gather some input from the console:

cin >> i;

To read and write on files, first define their path:

ofstream of_name("filename.extension"); //for simple output
ofstream of_name("filename.extension",append); //to append
ifstream in_name("filename.extension");

Then, usage is the same as cin and cout:

of_name << variable <<"string" <<endl;
if_name >> i;

Best practices

  • don't use ^ or pow() for powers of 10; use e instead

About

Cheatsheet and notes for FreeFEM++

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published