Tips and tricks

From Gerris

(Difference between revisions)
Jump to: navigation, search
Revision as of 17:29, 29 January 2008
Rohtav (Talk | contribs)

← Previous diff
Current revision
Ray (Talk | contribs)
(How to move in 3D in a 2D space ?)
Line 1: Line 1:
== Emacs mode for Gerris files == == Emacs mode for Gerris files ==
-Well, not really but something approaching. Add the following to your <code>.emacs</code>+The default Gerris installation comes with an emacs [http://www.gnu.org/software/emacs/manual/html_node/emacs/Major-Modes.html major mode] to edit Gerris simulation files. To enable it, you need to find where Gerris is installed. For example do:
- (setq auto-mode-alist (cons '("\\.gfs\\'" . shell-script-mode) auto-mode-alist))+ % 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 [http://www.gnu.org/software/emacs/manual/html_node/emacs/Dynamic-Abbrevs.html 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 == == 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 [http://gfs.sourceforge.net/examples/examples/boussinesq.html generate a movie] on the fly from a running simulation, how does one generate several movies simultaneously? While it is fairly simple to use the scripting mode of gfsview and unix pipes to [http://gfs.sourceforge.net/examples/examples/boussinesq.html generate a movie] on the fly from a running simulation, how does one generate several movies simultaneously?
-Using named unix fifos and the <code>tee</code> utility it is fairly easy too. For example if one has three gfsview files called <code>wide.gfv</code>, <code>closeup.gfv</code> and <code>overview.gfv</code> and want to generate the three corresponding movies <code>wide.mpg</code>, <code>closeup.mpg</code> and <code>overview.mpg</code> in one go, one could use the following script:+Using named unix fifos it is fairly easy too. For example if one has three gfsview files called <code>wide.gfv</code>, <code>closeup.gfv</code> and <code>overview.gfv</code> and want to generate the three corresponding movies <code>wide.mpg</code>, <code>closeup.mpg</code> and <code>overview.mpg</code> in one go, one could use the following command:
- #!/bin/sh+ % gerris3D mysimulation.gfs | gfsview-batch3D
- +
- movies="wide closeup overview"+
- rm -f $movies+
- mkfifo $movies+
- +
- gerris3D mysimulation.gfs | tee $movies > /dev/null &+
- for movie in $movies; do+
- gfsview-batch3D $movie.gfv < $movie | ppm2mpeg > $movie.mpg &+
- done+
- sleep 10+
- rm -f $movies+
-of course the simulation file <code>mysimulation.gfs</code> should contain lines looking like:+with a simulation file <code>mysimulation.gfs</code> containing the lines:
- OutputSimulation { istep = 10 } stdout+ EventScript { start = 0 } {
- EventScript { istep = 10 } { echo "Save stdout { width = 1024 height = 768 }" }+ 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 == == Compressing simulation files ==
Line 38: Line 83:
GfsView can read compressed GFS files directly. 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 "[[GfsOutputScalarStats|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 "[[GfsVariableDistance|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 <code>NODATA</code> 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 <code>/usr/share/gerris/gerris.dic</code> 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
-== Writing generic or customized gerris output ==+ 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!!!)
-Since most of visualization package on the market (or as open-source) do not support quadtree/octree data format, to use benefits of third party visualization packages rather than GFSView we should convert Gerris results to a general unstructured data.+== Restarting parallel simulations ==
- +
-VTK format could be visualized by powerful free package: [http://www.paraview.org/HTML/Index.html Paraview]. +
-The function "gfs_write_generic_output" is written for this purpose. +Starting from version 2010-07-10 it should be possible to restart large parallel simulations. Just add the standard line:
-This function could write output data in Tecplot or [http://www.cacr.caltech.edu/~slombey/asci/vtk/vtk_formats.simple.html VTK format]. I mean generic output because Tecplot format is almost a general unstructured data type and you could easily modify its (code or resulted file) for your own need. Alternatively you could contact me and I could do its for you (just describe your format). All data are written in ASCII format.+ [[GfsOutputSimulation|OutputSimulation]] { step = 1 } sim-%g.gfs
-Tecplot format is briefly as follows:+and do e.g.
-1) Header (include list of field variables, number of vertexes, number of elements and elements type)+ mpirun -np 8 gerris3D sim-10.gfs
-2) Spatial coordinates plus filed variables related to listed variables in header+with the same number of processors you initially used to create <code>sim-10.gfs</code>.
-3) Element connectivity+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:
-For more details regarding to VTK format refer to VTK file format: [http://www.vtk.org/pdf/file-formats.pdf PDF] or [http://www.cacr.caltech.edu/~slombey/asci/vtk/vtk_formats.simple.html HTML]+ [[GfsOutputSimulation|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.
-=== Brief algorithm ===+ mpirun -np 8 gerris3D sim-%d-10.gfs
-1) Construct mesh connectivity 4/8 vertex per cell.+Note that this requires a version of Gerris later than 2010-07-20.
-2) Optimize connectivity by removing repeated vertexes, this considerably reduce data size and improve considerably speed of rendering (very important for 3d or large scale simulation).+== Parallel compilation ==
-3) Interpolation of cell-wise data to vertexes.+If you have a multi-core system (let's say 4), you can compile Gerris (and other sources) 4 times faster using:
-4) Writing results on a text file.+ % make -j4
 +== Visualizing VTK parallel files using Visit ==
-=== How to use ===+[https://wci.llnl.gov/codes/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:
-Prototype of this function is as follows (6 input arguments):+ !NBLOCKS 2
 + file-0-0.vtk
 + file-0-1.vtk
-<source lang="c">+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:
- void gfs_write_generic_output ( gchar *type, +
- gint n_filed, +
- gchar **filed_name, +
- gint plot_depth, +
- GtsBBox * box, +
- GfsDomain * domain+
- );+
-</source>+
-</code>+ !NBLOCKS 2
 + file-0-0.vtk
 + file-0-1.vtk
 + file-1-0.vtk
 + file-1-1.vtk
-1. type: type of desired output, "VTK" or "TECPLOT" at the moment are supported.+Further information can be found [https://wci.llnl.gov/codes/visit/2.0.0/GettingDataIntoVisIt2.0.0.pdf here].
-2. n_filed: number of filed data to be visualized, 0 mean just visualization of mesh.+== How to move in 3D in a 2D space ? ==
-3. filed_name: string array, each item is correspond to each filed, you should include your desired field name, i.e., "U", "V", "W", "P" and desired tracer, e.g., "T", ..., also you could include "Vorticity" to compute and write vorticity field. If for any field, function can not find data pointer from name of variable (except for vorticity), this field is skiped.+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 ! ;-)
-4. plot_depth: depth of cell which are desired to visualize, -1 mean visiting all levels.+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.
-5. box: if box=NULL, all domain are considered for visualization, but user could define a box to visualize only portion of domain located in the box, to define box you should specify its two corner (by six coordinate, for 2d simulation define two z-value arbitrary).+you can see what happens [http://gfs.sourceforge.net/wiki/images/c/c6/Visu_2D3D_before.jpg before] and [http://gfs.sourceforge.net/wiki/images/8/8f/Visu_2D3D_after.jpg after] the magic shift
-6. domain: simulation domain.+or look in the [http://gerris.dalembert.upmc.fr/gerris/examples/examples/monai.html#htoc25 Tsunami runup onto a complex three-dimensional beach] example.

Current revision

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.

Personal tools
communication