The core of a fluid simulation

Let us assume that as a computational physicists you are most interested in how the Navier-Stokes equations are read. You are familiar with Stephane's J. Comput. Phys. article and you want to see how it is implemented in code. The core of the simulation in simulation.c specifically the following lines. We start with some bookkeeping:

Miscellaneous

Cast to the parent class

The example shows how one can extract information from an object if one knows in which
parent it is stored. For example, if we want to use the information of the event variable
which came from the GfsEvent class, we can have access to it by means
of the following variable "parentdata":

GfsEvent * parentdata = GFS_EVENT (event);

In this way, we load the information of the "event" object coming from
its parent GfsEvent into this variable and we can either use it or
manipulate if we want. For example
parentdata->step will give us the temporal step which have been
read in the input.

GFS_VARIABLE macro

This macro is used everywhere in the code to access the value of a GFS_VARIABLE in a cell. An example of its use is found in the init_periodic object showed in the previous section:

In the "magical" approach we just now that GFS_VARIABLE(somecell,somevariable->i)allows us to write to the variable somevariablein the cell somecell. For us dummies a question may arise: what value should the index i have ? Is it the cell number ? Well i is not an index: it is a member of each GfsVariable. We do not have to set it: it is already there in the variable.

In the rational approach, we would like to read the code and understand the definition of GFS_VARIABLE. However the definition of the GFS_VARIABLE macro is a bit baffling. It can be found in fluid.h. (It is a good idea to try the following: find any place in the code where the GFS_VARIABLE macro is used ---that should be easy--- and then click on it. Type M-. in emacs ---or the equivalent in vim--- and get to the definition.)