Skip to content

Grid_Enum and adding passive scalars

alwinm edited this page Dec 6, 2022 · 3 revisions

Grid Enum

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 #ifdefs.

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.

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.

Clone this wiki locally