Tips and tricks

From Gerris

Jump to: navigation, search

Contents

Emacs mode for Gerris files

The default Gerris installation comes with an emacs major mode to edit Gerris simulation files. To enable it, you need to find where Gerris is installed. For example do:

% which gerris2D
/usr/bin/gerris2D

Then edit your .emacs file and add the following lines:

;; gerris mode
(add-to-list 'load-path "/usr/share/gerris")
(require 'gfs-mode)

This will setup emacs so that opening files with the ".gfs" extension automatically uses the "gfs-mode" major mode. Alternatively you can manually enable this mode on any file using (within emacs):

M-x gfs-mode

where M- stands for the "emacs Meta key" (usually mapped to "esc" on your keyboard).

If you now restart emacs and open a .gfs file, you will see that emacs highlights Gerris keywords. These keywords are also "clickable". Clicking on a keyword will open the corresponding documentation in your web browser. If you want, you can customize the way links are opened by configuring the 'browse-url' emacs function which is used by gfs-mode.

Indentation

The gfs-mode also knows how to properly indent Gerris simulation files. A line can easily be indented by pressing the 'Tab' key with the cursor positioned anywhere on the line.

To automatically indent a block of text, use Ctrl-space (or the mouse) to select the block and type:

M-x indent-region

(note that you can use the Tab key to autocomplete command names).

Keyword auto-completion

gfs-mode configures dynamic abbrevs to provide auto-completion for Gerris keywords. Dynamic abbrevs are usually enabled by default in emacs. They work with any buffer. To use auto-completion, type the beginning of the Gerris keyword (e.g. "Out") and type

M-/

repeatedly. This will cycle through all the Gerris keywords starting with "Out".

Comments

You can comment out (resp. uncomment) selected blocks of text using

M-x comment-region (resp. M-x uncomment-region)

Generating several movies on-the-fly

Note that using GfsOutputView is simpler and more efficient than the technique described in this section.

While it is fairly simple to use the scripting mode of gfsview and unix pipes to generate a movie on the fly from a running simulation, how does one generate several movies simultaneously?

Using named unix fifos it is fairly easy too. For example if one has three gfsview files called wide.gfv, closeup.gfv and overview.gfv and want to generate the three corresponding movies wide.mpg, closeup.mpg and overview.mpg in one go, one could use the following command:

% gerris3D mysimulation.gfs | gfsview-batch3D

with a simulation file mysimulation.gfs containing the lines:

EventScript { start = 0 } {
  movies="wide closeup overview"
  rm -f $movies
  mkfifo $movies
  for movie in $movies; do
    ppm2mpeg < $movie > $movie.mpg &
  done
}
OutputSimulation { step = 0.01 } stdout
EventScript { step = 0.01 } {
  movies="wide closeup overview"
  for movie in $movies; do
    echo "Clear"
    cat $movie.gfv
    echo "Append $movie { width = 1024 height = 768 }"
  done
}

Compressing simulation files

When it is useful to save simulation results at regular intervals, the size of the files can be reduced by using on-the-fly compression. This can be done like this:

OutputSimulation { istep = 100 } sim-%ld.gfs
EventScript { istep = 100 } { gzip -f -q sim-*.gfs }

GfsView can read compressed GFS files directly.

Adding objects after a simulation has completed

If you want to add and execute Gerris objects after a simulation has completed, you can use something like:

% gerris2D -e "OutputScalarStats stderr { v = P }" end.gfs > /dev/null

Another example would be computing a distance function (i.e. a levelset function) from a VOF interface description for visualisation using isosurfaces in GfsView. This is easily done using something like:

% gerris3D -e "VariableDistance D T" end.gfs | gfsview3D -s iso.gfv

Masking out parts of the domain in GfsView

If you want to display only a subset of the mesh in GfsView you can use the NODATA value in a GfsFunction. For example, setting the scalar field of "Squares" in GfsView to:

(P > 0 ? P : NODATA)

will only display the pressure field where it is positive. This trick also works for GfsOutputPPM and GfsOutputGRD.

VIM features and Gerris

There are plenty of vim features that can be customized to make your life easier when you work with gerris.

Using tab, you can autocomplete the keywords of gerris. To do that, add the following lines to your .vimrc:

function! Tab_Or_Complete()
 if col('.')>1 && strpart( getline('.'), col('.')-2, 3 ) =~ '^\w'
   return "\<C-N>"
 else
   return "\<Tab>"
 endif
endfunction
:inoremap <Tab> <C-R>=Tab_Or_Complete()<CR>

if has("autocmd")
"set complete+=k/etc/dictionaries-common/words isk+=.,(
set complete+=k/usr/share/gerris/gerris.dic isk+=.,(
endif " has("autocmd"

where /usr/share/gerris/gerris.dic may vary according to how gerris was installed on your system.

Another interesting feature is to open in firefox the syntax reference page of the command under the cursor. To do that, you have to add to your .vimrc:

function! OnlineDoc()
       let s:browser = "firefox"
       let s:wordUnderCursor = expand("<cword>")
        if s:wordUnderCursor =~ 'Gfs'
               let s:url = "http://gfs.sourceforge.net/wiki/index.php/".s:wordUnderCursor
       else
               let s:url = "http://gfs.sourceforge.net/wiki/index.php/Gfs".s:wordUnderCursor
       endif
       let s:cmd = "silent !" . s:browser . " " . s:url
       "echo  s:cmd
       execute  s:cmd
       redraw!
endfunction

and then to map this function with some sequence of letters (in my case y use \w )

map <Leader>w :call OnlineDoc()<CR>

Remember that you can create your own vim plugin for gfs files as

au! BufNewFile,BufRead *.gfs set filetype=gfs

and to place all this stuff in .vim/after/ftplugin/gfs.vim (you can also to load the color scheme of c.vim!!!)

Restarting parallel simulations

Starting from version 2010-07-10 it should be possible to restart large parallel simulations. Just add the standard line:

OutputSimulation { step = 1 } sim-%g.gfs

and do e.g.

mpirun -np 8 gerris3D sim-10.gfs

with the same number of processors you initially used to create sim-10.gfs.

Note that if you use this (simple) method each processor will need to read the entire simulation file. If you notice that this phase takes a significant amount of time (e.g. due to filesystem performance when being accessed simultaneously by many processes), you can optimise things using:

OutputSimulation { step = 1 } sim-%d-%g.gfs

which will generate a separate file for each process. You can then restart the simulation using e.g.

mpirun -np 8 gerris3D sim-%d-10.gfs

Note that this requires a version of Gerris later than 2010-07-20.

Parallel compilation

If you have a multi-core system (let's say 4), you can compile Gerris (and other sources) 4 times faster using:

% make -j4

Visualizing VTK parallel files using Visit

Visit is a nice visualization tool that allows us to visualize, among many other formats, VTK files. For parallel runs, one can generate one vtk file per processor. Then, in order to visualize all the simulation domain, we need to generate a text file with extension .visit that gathers the names of the different vtk files at a given timestep. For example, if we have generated file-0-0.vtk and file-0-1.vtk at t=0, we just need to create the following text file:

 !NBLOCKS 2
 file-0-0.vtk
 file-0-1.vtk

If you have solutions at different time steps and you want to load them at once, then you will need to do something as follows:

 !NBLOCKS 2
 file-0-0.vtk
 file-0-1.vtk
 file-1-0.vtk
 file-1-1.vtk

Further information can be found here.

How to move in 3D in a 2D space ?

If you tried to visualize a Z scalar field in a linear object of GfsView2D you noticed it's difficult to move in 3D because you are in a plane ! ;-)

May be you tried to modify the q0,q1,q2,q3 parameters of your gfv file by hand, but if you don't like sine or cosine the solution is to use the shift key of the keyboard when you move the mouse.

you can see what happens before and after the magic shift

or look in the Tsunami runup onto a complex three-dimensional beach example.

Views
Navigation
communication