Skip to content

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, which supports the drawing of meshes, isovalues of real FE-functions, and of vector fields, all by the command plot (see Plot section below). For publishing purpose, FreeFem++ can store these plots as postscript files.

  • Another method is to use external tools, for example, gnuplot (see Gnuplot section, medit section, Paraview section) using the command system to launch them and/or to save the data in text files.

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 double arrays, to plot respectively a mesh, a function, a vector field, or a curve defined by the two double arrays.

Note

The length of an arrow is always bound to be in [5‰, 5%] of the screen size in order to see something.

The plot command parameters are listed in the Reference part.

The keyboard shortcuts are :

  • enter tries to show plot
  • p previous plot (10 plots saved)
  • ? shows this help
  • +,- zooms in/out around the cursor 3/2 times
  • = resets the view
  • r refreshes plot
  • up, down, left, right special keys to tanslate
  • 3 switches 3d/2d plot keys :

    - z,Z focal zoom and zoom out - H,h increases or decreases the Z scale of the plot - mouse motion - left button rotates - right button zooms (ctrl+button on mac) - right button +alt tanslates (alt+ctrl+button on mac)

  • a,A increases or decreases the arrow size

  • B switches between showing the border meshes or not
  • i,I updates or not: the min/max bound of the functions to the window
  • n,N decreases or increases the number of iso value arrays
  • b switches between black and white or color plotting
  • g switches between grey or color plotting
  • f switches between filling iso or iso line
  • l switches between lighting or not
  • v switches between show or not showing the numerical value of colors
  • m switches between show or not showing the meshes
  • w window dump in file ffglutXXXX.ppm
  • * keep/drop viewpoint for next plot
  • k complex data / change view type
  • ESC closes the graphics process before version 3.22, after no way to close
  • otherwise does nothing

For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
real[int] xx(10), yy(10);

mesh Th = square(5,5);

fespace Vh(Th, P1);

//plot scalar and vectorial FE function
Vh uh=x*x+y*y, vh=-y^2+x^2;
plot(Th, uh, [uh, vh], value=true, ps="three.eps", wait=true);

//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=true, fill=true, value=true, ps="threeg.eps");

//compute a cut
for (int 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([xx, yy], ps="likegnu.eps", wait=true);
Fig. 1: mesh, isovalue, and vector Fig. 2: Enlargement in grey of isovalue, and vector
Three Threeg

Fig. 3: Plots a cut of uh. Note that a refinement of the same can be obtained in combination with gnuplot
likegnu

To change the color table and to choose the value of iso line you can do :

 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
// from: \url{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
// Hue, the color type (such as red, blue, or yellow):
// Ranges from 0-360 (but normalized to 0-100% in some applications, like 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%

mesh Th = square(10, 10, [2*x-1, 2*y-1]);

fespace Vh(Th, P1);
Vh uh=2-x*x-y*y;

real[int] colorhsv=[ // color hsv model
    4./6., 1 , 0.5, // dark blue
    4./6., 1 , 1, // blue
    5./6., 1 , 1, // magenta
    1, 1. , 1, // red
    1, 0.5 , 1 // light red
    ];
 real[int] viso(31);

 for (int i = 0; i < viso.n; i++)
    viso[i] = i*0.1;

 plot(uh, viso=viso(0:viso.n-1), value=true, fill=true, wait=true, hsv=colorhsv);
Fig. 4: hsv color cylinder Fig. 5: isovalue with an other color table
hsv threehsv

Note

See HSV.edp for the complete script.

Example Membrane shows how to generate a gnuplot from a FreeFem++ file. Here is another technique which has the advantage of being online, i.e. one doesn't need to quit FreeFem++ to generate a gnuplot.

However, this works only if gnuplot is installed, and only on an Unix-like computer.

Add to the previous example:

1
2
3
4
5
6
7
8
9
{// file for gnuplot
    ofstream gnu("plot.gp");
    for (int i = 0; i < n; i++)
        gnu << xx[i] << " " << yy[i] << endl;
}

// to call gnuplot command and wait 5 second (due to the Unix command)
// and make postscript plot
exec("echo 'plot \"plot.gp\" w l \n pause 5 \n set term postscript \n set output \"gnuplot.eps\" \n replot \n quit' | gnuplot");
Fig. 6: Plots a cut of uh with gnuplot
gnuplot

Note

See Plot.edp for the complete script.

As said above, medit is a freeware display package by Pascal Frey using OpenGL. Then you may run the following example.

Now medit software is included in FreeFem++ under ffmedit name.

The medit command parameters are listed in the Reference part.

Fig. 7: medit plot
medit2

With version 3.2 or later

1
2
3
4
5
6
7
8
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("u", Th, u);

Before:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
mesh Th = square(10, 10, [2*x-1, 2*y-1]);

fespace Vh(Th, P1);
Vh u=2-x*x-y*y;

savemesh(Th, "u", [x, y, u*.5]); //save u.points and u.faces file
// build a u.bb file for medit
{
    ofstream file("u.bb");
    file << "2 1 1 " << u[].n << " 2 \n";
    for (int j = 0; j < u[].n; j++)
        file << u[][j] << endl;
}
//call medit command
exec("ffmedit u");
//clean files on unix-like OS
exec("rm u.bb u.faces u.points");

Note

See Medit.edp for the complete script.

One can also export mesh or results in the .vtk format in order to post-process data using Paraview.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
load "iovtk"

mesh Th = square(10, 10, [2*x-1, 2*y-1]);

fespace Vh(Th, P1);
Vh u=2-x*x-y*y;

int[int] Order = [1];
string DataName = "u";
savevtk("u.vtu", Th, u, dataname=DataName, order=Order);
Fig. 8: Paraview plot
Paraview

Note

See Paraview.edp for the complete script.

In order to create plots from FreeFem++ simulations in Octave and Matlab the FEM mesh and the FE function must be exported to text files:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
mesh Th = square(10, 10, [2*x-1, 2*y-1]);

fespace Vh(Th, P1);
Vh u=2-x*x-y*y;

savemesh(Th,"export_mesh.msh");

ofstream file("export_data.txt");
for (int j=0; j<u[].n; j++)
   file << u[][j] << endl;

Within Matlab or Octave the files can be processed with the ffmatlib library:

1
2
3
4
5
addpath('path to ffmatlib');
[p,b,t]=ffreadmesh('export_mesh.msh');
u=ffreaddata('export_data.txt');
ffpdeplot(p,b,t,'XYData',u,'ZStyle','continuous','Mesh','on');
grid;

Fig. 9: Matlab / Octave plot
Matlab / Octave

Note

For more Matlab / Octave plot examples have a look at the tutorial section Matlab / Octave Examples or visit the ffmatlib library at github.