Chapter 7
Visualization

Results created by FEM are huge data, so it is very important to render them visible. 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 later use, FreeFem++ can store these plots as postscript files.

Another method is to use the external tools, for example, gnuplot (see Section 7.2), medit (see Section 7.3) using the command systemto 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 a arrow is always bound to be in [5o,5%] of the screen size, to see something not a porcupine.

The parameters are

wait=
boolean expression to wait or not (by default no wait). If true we wait for a keyboard up event or mouse event, they respond to an event by the following characters
+
to zoom in around the mouse cursor,
-
to zoom out around the mouse cursor,
=
to restore de initial graphics state,
c
to decrease the vector arrow coef,
C
to increase the vector arrow coef,
r
to refresh the graphic window,
f
to toggle the filling between isovalues,
b
to toggle the black and white,
g
to toggle to grey or color ,
v
to toggle the plotting of value,
?
to show all actives keyboard char,
enter
wait for the next plot,
ESC
close the graphics process.
otherwise
do nothing.
ps=
string expression to save the plot on postscript file
coef=
the vector arrow coef between arrow unit and domain unit.
fill=
to fill between isovalues.
cmm=
string expression to write in the graphic window
value=
to plot the value of isoline and the value of vector arrow.
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 value of isovalues (an array real[int] of increasing values)
varrow=
sets the array value of color arrows (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.
hsv=
(array of float) to defined color of 3*n value in HSV color model declare with for example


      real[int] colors = [h1,s1,v1,... ,  hn,vn,vn];
    

where hi,si,viis the ith color to defined the color table.

boundary=
(bool) to plot or not the boundary of the domain (true by default).
dim=
(int) sets dim of the plot 2d or 3d (2 by default)

For example:


real[int] xx(10),yy(10);
mesh Th=square(5,5);
fespace Vh(Th,P1);
Vh uh=x⋆x+y⋆y,vh=-y^2+x^2;
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


PIC

Figure 7.1: mesh, isovalue, and vector

PIC

Figure 7.2: enlargement in grey of isovalue, and vector



PIC


Figure 7.3: Plots a cut of uh. Note that a refinement of the same can be obtained in combination with gnuplot


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%
//
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=1,fill=1,wait=1,hsv=colorhsv);


PIC

Figure 7.4: hsv color cylinder

PIC

Figure 7.5: isovalue with an other color table


7.2 link with gnuplot

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 doesn’t 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
// to call gnuplot command and wait 5 second (thanks to unix command)
// and make postscript plot
exec("echo 'plot \"plot.gp\" w l \
pause 5 \
set term postscript \
set output \"gnuplot.eps\" \
replot \
quit' | gnuplot");


PIC


Figure 7.6: Plots a cut of uh with gnuplot


7.3 link with medit

First you must install medit 2 , a freeware display package by Pascal Frey using OpenGL. Then you may run the follwoing example.

remark, now meditsoftware is include in FreeFem++under ffmeditname.


PIC


Figure 7.7: medit plot



mesh Th=square(10,10,[2⋆x-1,2⋆y-1]);
fespace Vh(Th,P1);
Vh u=2-x⋆x-y⋆y;
   savemesh(Th,"mm",[x,y,u⋆.5]);  // save mm.points and mm.faces file
// for medit
    // build a mm.bb file
  { ofstream file("mm.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 mm");
     // clean files on unix OS
    exec("rm mm.bb      mm.faces   mm.points");