21cmfast / 21cmFAST

Official repository for 21cmFAST: a code for generating fast simulations of the cosmological 21cm signal
MIT License
58 stars 37 forks source link

Interpolation Table initialization is brittle #296

Closed steven-murray closed 2 months ago

steven-murray commented 1 year ago

Currently we have a bunch of interpolation tables that need to be initialized at various places before they are interpolated. These are typically global variables and are accessed from all over the place in different C files / functions. Therefore, the initialization code has to be called numerous times in different places.

This gets more tricky because the user could sometimes read in a file (eg. the spin temperature) and compute the ionization box from that spin temperature, which means all the initialization code in ComputeTsBox is not called. Therefore it has to be called again in IonizationBox, but we don't want to call it twice if we don't have to.

Currently we're trying to do this by having a bunch of logical conditions (eg. if (!flag_options.USE_TS_FLUCT)...`) but these are bound to fail in corner cases.

I suggest we use a more foolproof system: let each table be a small struct that has a reference to its size, minimum and maximum values, the data itself, and whatever parameters are required to compute it. Whenever the initialize method for that struct is called, if all of its metadata is the same as the global struct, then just exit, otherwise do the initialization. That way we never have to worry about the current state in the actual code.