-
Notifications
You must be signed in to change notification settings - Fork 32
Grid_Enum and adding passive scalars
A C++ Enum has interesting properties which make it a useful way to keep track of data field offsets in grid.
"If the first enumerator does not have an initializer, the associated value is zero. For any other enumerator whose definition does not have an initializer, the associated value is the value of the previous enumerator plus one."
The reason we use Enum is to more easily support additional physics fields. Imagine having fields A,B,C, needing to know their offsets a,b,c, and supporting all 7 non-trivial combinations (it would be 8 if you included the case of nothing on).
A: a=0
B: b=0
C: c=0
AB: a=0 b=1
BC: b=0 c=1
AC: a=0 c=1
ABC: a=0 b=1 c=2
Rather than put ourselves through the if/else
gauntlet, using enum is a much easier way to get the desired behavior, where the next required offset is defined if needed, and given the right value (1+ the previous) if defined.
To add a new scalar field, add it to the grid_enum.h
list of scalars, wrapped in the appropriate #ifdef
s.
To access it, use density[id+n_cells*grid_enum::your_scalar_name]
. n_cells
may be H.n_cells
or G.H.n_cells
as appropriate.
WARNING: adding a new field in grid_enum
does NOT create Conserved objects that look like C.your_scalar_name
or C.d_your_scalar_name
--if you want those, you'll have to create them yourself.
You'll also want to make sure it outputs correctly, and if applicable, reads appropriately when init=Read_Grid
(e.g. for restarts).
Cholla will automatically advect the new scalar as long as it has been enrolled within the scalar block of grid_enum.