Tips and tricks

From Gerris

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

← Previous diff
Revision as of 16:21, 7 April 2012
Dfuster (Talk | contribs)

Next diff →
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
-== Generating several movies on-the-fly ==+Then edit your .emacs file and add the following lines:
-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?+ ;; gerris mode
 + (add-to-list 'load-path "/usr/share/gerris")
 + (require 'gfs-mode)
-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:+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):
- #!/bin/sh+ M-x gfs-mode
- +
- 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:+where M- stands for the "emacs Meta key" (usually mapped to "esc" on your keyboard).
- OutputSimulation { istep = 10 } stdout+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.
- EventScript { istep = 10 } { echo "Save stdout { width = 1024 height = 768 }" }+
-== Compressing simulation files ==+===Indentation===
-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:+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.
- OutputSimulation { istep = 100 } sim-%ld.gfs+To automatically indent a block of text, use Ctrl-space (or the mouse) to select the block and type:
- EventScript { istep = 100 } { gzip -f -q sim-*.gfs }+
-GfsView can read compressed GFS files directly.+ M-x indent-region
 +(note that you can use the Tab key to autocomplete command names).
-== Writing generic or customized gerris output ==+===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
-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.+ M-/
- +
-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. +repeatedly. This will cycle through all the Gerris keywords starting with "Out".
-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.+===Comments===
-Tecplot format is briefly as follows:+You can comment out (resp. uncomment) selected blocks of text using
-1) Header (include list of field variables, number of vertexes, number of elements and elements type)+ M-x comment-region (resp. M-x uncomment-region)
-2) Spatial coordinates plus filed variables related to listed variables in header+== Generating several movies on-the-fly ==
-3) Element connectivity+''Note that using [[GfsOutputView]] is simpler and more efficient than the technique described in this section.''
-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]+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 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:
-=== Brief algorithm ===+ % gerris3D mysimulation.gfs | gfsview-batch3D
-1) Construct mesh connectivity 4/8 vertex per cell.+with a simulation file <code>mysimulation.gfs</code> containing the lines:
-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).+ 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
 + }
-3) Interpolation of cell-wise data to vertexes.+== Compressing simulation files ==
-4) Writing results on a text file.+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 }
-=== Function Prototype ===+GfsView can read compressed GFS files directly.
-Prototype of this function is as follows (6 input arguments):+== Adding objects after a simulation has completed ==
-<source lang="c">+If you want to add and execute Gerris objects after a simulation has completed, you can use something like:
- void gfs_write_generic_output (gchar *type, +
- gint n_filed, +
- gchar **filed_name, +
- gint plot_depth, +
- GtsBBox * box, +
- GfsDomain * domain);+
-</source>+
-<source lang="c">+ % gerris2D -e "[[GfsOutputScalarStats|OutputScalarStats]] stderr { v = P }" end.gfs > /dev/null
-1. type: type of desired output, "VTK" or "TECPLOT" at the moment are supported.+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:
-2. n_filed: number of filed data to be visualized, 0 mean just visualization of mesh.+ % gerris3D -e "[[GfsVariableDistance|VariableDistance]] D T" end.gfs | gfsview3D -s iso.gfv
-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.+== Masking out parts of the domain in GfsView ==
-4. plot_depth: depth of cell which are desired to visualize, -1 mean visiting all levels.+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:
-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).+ (P > 0 ? P : NODATA)
-6. domain: simulation domain.+will only display the pressure field where it is positive. This trick also works for [[GfsOutputPPM]] and [[GfsOutputGRD]].
-</source>+== VIM features and Gerris ==
 +There are plenty of vim features that can be customized to make your life easier when you work with gerris.
-At the moment this function could be used to write data on-the-fly but it is easy to write a short code to convert "*.sim" data (result of Gerris) to described format. Since I prefer to analyze results on-the-fly I did not attent to a separate tool. Maybe in future a write such a tool (if you need such tool you could let me know or write its yourself, for this follow instruction same as "gfs2oogl.c", to load simulation object).+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"
-=== How to Use ===+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:
-To call this function on-the-fly it is sufficient to include this + function! OnlineDoc()
-file in "simulation.c" file, i.e. add statement + 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
-<source lang="c">+and then to map this function with some sequence of letters (in my case y use \w )
- #include "generic_output.c"+
-</source>+
-after include statements in the "simulation.c" file of your gerris and add its desired function call in time step loop of "simulation_run" function e.g. at a place like this (determined by "######")+ map <Leader>w :call OnlineDoc()<CR>
-<source lang="c">+Remember that you can create your own vim plugin for gfs files as
- static void simulation_run (GfsSimulation * sim)+ au! BufNewFile,BufRead *.gfs set filetype=gfs
- {+
- ...+
- ...+
- ...+
- while (sim->time.t < sim->time.end &&+and to place all this stuff in .vim/after/ftplugin/gfs.vim (you can also to load the color scheme of c.vim!!!)
- sim->time.i < sim->time.iend) {+
- ...+== Restarting parallel simulations ==
- ...+
- ...+
- sim->time.t = sim->tnext;+Starting from version 2010-07-10 it should be possible to restart large parallel simulations. Just add the standard line:
- sim->time.i++;+
- gts_range_add_value (&domain->timestep, gfs_clock_elapsed(domain->timer) - tstart);+ [[GfsOutputSimulation|OutputSimulation]] { step = 1 } sim-%g.gfs
- gts_range_update (&domain->timestep);+
- gts_range_add_value (&domain->size, gfs_domain_size (domain, FTT_TRAVERSE_LEAFS, -1));+
- gts_range_update (&domain->size);+
- "######"+and do e.g.
- }+
-</source>+ mpirun -np 8 gerris3D sim-10.gfs
-assume we like to run "heated.gfs" example +with the same number of processors you initially used to create <code>sim-10.gfs</code>.
-use the following statements at the specified place (writing output file after each 100 time steps):+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:
-<source lang="c">+ [[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.
- gint n_field = 5, plot_depth = -1;+
- gchar *filed_name[5] = {"U", "V", "P", "T", "Vorticity"};+
- GtsBBox *box= NULL; +
- if(sim->time.i % 100 == 0) gfs_write_generic_output ("TECPLOT", n_field, filed_name, plot_depth, box, domain);+ mpirun -np 8 gerris3D sim-%d-10.gfs
- // or +Note that this requires a version of Gerris later than 2010-07-20.
- if(sim->time.i % 100 == 0) gfs_write_generic_output ("VTK", n_field, filed_name, plot_depth, box, domain);+== Parallel compilation ==
- }+If you have a multi-core system (let's say 4), you can compile Gerris (and other sources) 4 times faster using:
-</source>+ % make -j4
- +
-or if you like to limit visulization within a box use this:+
-<source lang="c">+== Visualizing VTK parallel files using Visit ==
- {+[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:
- gint n_field = 5, plot_depth = -1;+
- gchar *filed_name[5] = {"U", "V", "P", "T", "Vorticity"};+
- +
- GtsBBox *box= gts_bbox_new (gts_bbox_class (), NULL, +
- -.5, -.5, -.5,+
- .5, .5, .5);+
- +
- if(sim->time.i % 100 == 0) gfs_write_generic_output ("TECPLOT", n_field, filed_name, plot_depth, box, domain);+
- // or + !NBLOCKS 2
 + file-0-0.vtk
 + file-0-1.vtk
- if(sim->time.i % 100 == 0) gfs_write_generic_output ("VTK", n_field, filed_name, plot_depth, box, domain);+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
-</source>+Further information can be found [https://wci.llnl.gov/codes/visit/2.0.0/GettingDataIntoVisIt2.0.0.pdf here].
- +
-note that resulted files are names as:+
- +
- tecplot%i.plt or paraview%i.vtk+
- +
-where %i determine file number, it is automatically incremented after each output+
- +
-Note that you could write various type or a type with various proberties (e.g. writing both hole data and limited data by a box) simultaneously. For this for this purpose you just include suitable call of function in simulation loop.+
- +
-After modification of code, you should recompile your gerris code to activate this added feature. Of course it is better to include such data in input "*.gfs", i.e. treat its like an event, but prefarably this job should be performed by Gerris mantainer, if he detect that this feature is useful for a portion of Gerris users. +
- +
-(in the case of interest, you could announce your interest to this feature in GSF user mailing list).+

Revision as of 16:21, 7 April 2012

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.

Personal tools
communication