diff --git a/.clang-format-ignore b/.clang-format-ignore index f6fca214f..0072faee9 100644 --- a/.clang-format-ignore +++ b/.clang-format-ignore @@ -64,17 +64,17 @@ src/include/grackle_float.h.in src/include/grackle_misc.h src/include/grackle_rate_functions.h src/include/grackle_types.h -tests/unit/grtest_cmd.cpp -tests/unit/grtest_cmd.hpp -tests/unit/grtest_os.cpp -tests/unit/grtest_os.hpp -tests/unit/grtest_utils.cpp -tests/unit/grtest_utils.hpp +tests/grtestutils/cmd.cpp +tests/grtestutils/cmd.hpp +tests/grtestutils/os.cpp +tests/grtestutils/os.hpp +tests/grtestutils/utils.cpp +tests/grtestutils/utils.hpp +tests/grtestutils/googletest/check_allclose.hpp tests/unit/test_chemistry_struct_synced.cpp tests/unit/test_ghost_zone.cpp tests/unit/test_interpolators_comparisons.cpp tests/unit/test_linalg.cpp tests/unit/test_status_reporting.cpp tests/unit/test_unit_interpolators_g.cpp -tests/unit/utest_helpers.hpp diff --git a/src/clib/CMakeLists.txt b/src/clib/CMakeLists.txt index 693d6ea37..174bfb431 100644 --- a/src/clib/CMakeLists.txt +++ b/src/clib/CMakeLists.txt @@ -120,10 +120,11 @@ add_library(Grackle_Grackle lookup_cool_rates1d.hpp make_consistent.cpp make_consistent.hpp opaque_storage.hpp - rate_utils.cpp + ratequery.cpp ratequery.hpp solve_chemistry.cpp scale_fields.cpp scale_fields.hpp solve_rate_cool_g-cpp.cpp solve_rate_cool_g-cpp.h + support/SimpleVec.hpp step_rate_gauss_seidel.hpp step_rate_newton_raphson.hpp time_deriv_0d.hpp diff --git a/src/clib/Make.config.objects b/src/clib/Make.config.objects index 7a4bb87af..d3a721cbd 100644 --- a/src/clib/Make.config.objects +++ b/src/clib/Make.config.objects @@ -49,7 +49,7 @@ OBJS_CONFIG_LIB = \ calc_tdust_3d.lo \ calc_grain_size_increment_1d.lo \ rate_functions.lo \ - rate_utils.lo \ + ratequery.lo \ gaussj_g.lo \ utils.lo \ utils-cpp.lo \ diff --git a/src/clib/chemistry_solver_funcs.hpp b/src/clib/chemistry_solver_funcs.hpp index f36db0a7f..ad3f8a760 100644 --- a/src/clib/chemistry_solver_funcs.hpp +++ b/src/clib/chemistry_solver_funcs.hpp @@ -1,6 +1,11 @@ -// See LICENSE file for license and copyright information - -/// @file chemistry_solver_funcs.hpp +//===----------------------------------------------------------------------===// +// +// See the LICENSE file for license and copyright information +// SPDX-License-Identifier: NCSA AND BSD-3-Clause +// +//===----------------------------------------------------------------------===// +/// +/// @file /// @brief Defines chemistry reaction related functions invoked by the /// grackle solver in order to integrate the species densities over time. /// @@ -14,6 +19,8 @@ /// be decoupled from the derivative calculation for primoridial species /// - it may also make sense to further divide logic by the kinds of species /// that are affected (e.g. primordial vs grains) +/// +//===----------------------------------------------------------------------===// #ifndef CHEMISTRY_SOLVER_FUNCS_HPP #define CHEMISTRY_SOLVER_FUNCS_HPP @@ -579,7 +586,9 @@ inline void species_density_updates_gauss_seidel( if ( (my_chemistry->metal_chemistry == 1) && (itmask_metal[i] != MASK_FALSE) ) { - scoef = scoef; + // we comment out the following line that assigns scoef to itself since + // it has no practical impact and produces a compiler warning + // scoef = scoef; acoef = acoef + kcol_buf.data[CollisionalRxnLUT::kz44][i] * CII(i,j,k) / 12. + kcol_buf.data[CollisionalRxnLUT::kz45][i] * OII(i,j,k) / 16. @@ -1932,7 +1941,9 @@ inline void species_density_derivatives_0d( if ((my_chemistry->metal_chemistry == 1) && (itmask_metal[0] != MASK_FALSE)) { - scoef = scoef; + // we comment out the following line that assigns scoef to itself since + // it has no practical impact and produces a compiler warning + // scoef = scoef; acoef = acoef + kcr_buf.data[CollisionalRxnLUT::kz44][0] * CII / 12. + kcr_buf.data[CollisionalRxnLUT::kz45][0] * OII / 16. diff --git a/src/clib/initialize_chemistry_data.cpp b/src/clib/initialize_chemistry_data.cpp index b8b39fabf..0a196abe2 100644 --- a/src/clib/initialize_chemistry_data.cpp +++ b/src/clib/initialize_chemistry_data.cpp @@ -28,6 +28,8 @@ #include "internal_types.hpp" // drop_CollisionalRxnRateCollection #include "opaque_storage.hpp" // gr_opaque_storage #include "phys_constants.h" +#include "ratequery.hpp" +#include "status_reporting.h" #ifdef _OPENMP #include @@ -229,9 +231,15 @@ static void initialize_empty_chemistry_data_storage_struct(chemistry_data_storag my_rates->opaque_storage = NULL; } -extern "C" int local_initialize_chemistry_data(chemistry_data *my_chemistry, - chemistry_data_storage *my_rates, - code_units *my_units) +/// core logic of local_initialize_chemistry_data_ +/// +/// @note +/// This has been separated from local_initialize_chemistry_data to ensure that +/// any memory allocations tracked by reg_builder can be appropriately freed +/// (this is somewhat unavoidable in C++ without destructors) +static int local_initialize_chemistry_data_( + chemistry_data *my_chemistry, chemistry_data_storage *my_rates, + code_units *my_units, grackle::impl::ratequery::RegBuilder* reg_builder) { /* Better safe than sorry: Initialize everything to NULL/0 */ @@ -403,6 +411,7 @@ extern "C" int local_initialize_chemistry_data(chemistry_data *my_chemistry, init_empty_interp_grid_props_( &my_rates->opaque_storage->h2dust_grain_interp_props); my_rates->opaque_storage->grain_species_info = nullptr; + my_rates->opaque_storage->registry = nullptr; double co_length_units, co_density_units; if (my_units->comoving_coordinates == TRUE) { @@ -418,7 +427,8 @@ extern "C" int local_initialize_chemistry_data(chemistry_data *my_chemistry, // Compute rate tables. if (grackle::impl::initialize_rates(my_chemistry, my_rates, my_units, - co_length_units, co_density_units) + co_length_units, co_density_units, + reg_builder) != GR_SUCCESS) { fprintf(stderr, "Error in initialize_rates.\n"); return GR_FAIL; @@ -456,6 +466,16 @@ extern "C" int local_initialize_chemistry_data(chemistry_data *my_chemistry, /* store a copy of the initial units */ my_rates->initial_units = *my_units; + // initialize the registry + if (grackle::impl::ratequery::RegBuilder_misc_recipies(reg_builder, + my_chemistry) + != GR_SUCCESS){ + return GrPrintAndReturnErr("error in RegBuilder_misc_recipies"); + } + my_rates->opaque_storage->registry = new grackle::impl::ratequery::Registry( + grackle::impl::ratequery::RegBuilder_consume_and_build(reg_builder) + ); + if (grackle_verbose) { time_t timer; char tstr[80]; @@ -503,6 +523,20 @@ extern "C" int local_initialize_chemistry_data(chemistry_data *my_chemistry, return GR_SUCCESS; } + +extern "C" int local_initialize_chemistry_data(chemistry_data *my_chemistry, + chemistry_data_storage *my_rates, + code_units *my_units) +{ + namespace rate_q = grackle::impl::ratequery; + rate_q::RegBuilder reg_builder = rate_q::new_RegBuilder(); + + int out = local_initialize_chemistry_data_(my_chemistry, my_rates, my_units, + ®_builder); + rate_q::drop_RegBuilder(®_builder); + return out; +} + extern "C" int initialize_chemistry_data(code_units *my_units) { if (local_initialize_chemistry_data(grackle_data, &grackle_rates, @@ -659,6 +693,15 @@ extern "C" int local_free_chemistry_data(chemistry_data *my_chemistry, delete my_rates->opaque_storage->grain_species_info; } + if (my_rates->opaque_storage->registry != nullptr) { + // delete contents of registry + grackle::impl::ratequery::drop_Registry( + my_rates->opaque_storage->registry + ); + // delete registry, itself + delete my_rates->opaque_storage->registry; + } + delete my_rates->opaque_storage; my_rates->opaque_storage = nullptr; diff --git a/src/clib/initialize_rates.cpp b/src/clib/initialize_rates.cpp index 18909b011..a5198b5d4 100644 --- a/src/clib/initialize_rates.cpp +++ b/src/clib/initialize_rates.cpp @@ -385,12 +385,79 @@ int init_kcol_rate_tables( return GR_SUCCESS; } + +/// a function that encodes the algorithm for looking up the `i`th pointer +/// collisional reaction rate from an instance of `chemistry_data_storage`. +/// +/// This is intended to be registered with RegBuilder in order to provide +/// access to the various collisional reaction rates. +/// +/// @param my_rates The object from which the rate Entry is loaded +/// @param i the index of the queried rate +grackle::impl::ratequery::Entry get_CollisionalRxn_Entry + (chemistry_data_storage* my_rates, int i) { + // sanity check! (this shouldn't actually happen) + if ((my_rates == nullptr) || (my_rates->opaque_storage == nullptr) || + (my_rates->opaque_storage->kcol_rate_tables == nullptr)) { + return grackle::impl::ratequery::mk_invalid_Entry(); + } + + // import new_Entry into the current scope (so we don't need the full name) + using ::grackle::impl::ratequery::new_Entry; + + double** data = my_rates->opaque_storage->kcol_rate_tables->data; + + // this implementation leverages the following properties of the + // CollisionalRxnLUT enum: + // - each entry of collisional_rxn_rate_members.def has a corresponding + // enumeration-constant + // - the very first enumeration constant has a value of 0 (since a value + // wasn't explicitly specified) + // - the value of each other enumeration constants is 1 larger than the value + // of the previous value (if a value isn't explicitly specified) + // - CollisionalRxnLUT::NUM_ENTRIES specifies the number of other enumeration + // constants (excluding CollisionalRxnLUT::NUM_ENTRIES) in the enum + switch (i) { +#define TO_STR(s) #s +#define ENTRY(NAME) \ + case CollisionalRxnLUT::NAME: { return new_Entry(data[i], TO_STR(NAME)); } +#include "collisional_rxn_rate_members.def" + +#undef ENTRY +#undef TO_STR + default: { + return grackle::impl::ratequery::mk_invalid_Entry(); + } + } +} + + +/// a function that encodes the algorithm for looking up the k13dd +/// collisional reaction rate from an instance of `chemistry_data_storage`. +/// +/// This is intended to be registered with RegBuilder in order to provide +/// access to the various collisional reaction rates. +/// +/// @param my_rates The object from which the rate Entry is loaded +/// @param i the index of the queried rate +grackle::impl::ratequery::Entry get_k13dd_Entry( + chemistry_data_storage* my_rates, int i) { + if (i == 0) { + double* ptr = (my_rates == nullptr) ? nullptr : my_rates->k13dd; + return grackle::impl::ratequery::new_Entry(ptr, "k13dd"); + } else { + return grackle::impl::ratequery::mk_invalid_Entry(); + } +} + + } // anonymous namespace //Definition of the initialise_rates function. int grackle::impl::initialize_rates( chemistry_data *my_chemistry, chemistry_data_storage *my_rates, - code_units *my_units, double co_length_unit, double co_density_unit) + code_units *my_units, double co_length_unit, double co_density_unit, + ratequery::RegBuilder* reg_builder) { // TODO: we REALLY need to do an error check that // my_chemistry->NumberOfTemperatureBins >= 2 @@ -493,6 +560,15 @@ int grackle::impl::initialize_rates( return GR_FAIL; } + // register the recipe for looking up the "standard" collisional rates + if (grackle::impl::ratequery::RegBuilder_recipe_1d( + reg_builder, CollisionalRxnLUT::NUM_ENTRIES, + &get_CollisionalRxn_Entry, my_chemistry->NumberOfTemperatureBins + ) != GR_SUCCESS) { + return GrPrintAndReturnErr("error registering standard collisional " + "reaction rates"); + } + //--------Calculate coefficients for density-dependent collisional H2 dissociation rate-------- // // idt = 0 calculates coefficients for direct collisional dissociation idt = 1 @@ -508,6 +584,14 @@ int grackle::impl::initialize_rates( // coeff7(idt=0, TbinFinal), coeff1(idt=1, Tbin1), ..., coeff7(idt=1, TbinFinal)} add_k13dd_reaction_rate(&my_rates->k13dd, kUnit, my_chemistry); + // maybe k13dd should be considered multi-dimensional? + if (grackle::impl::ratequery::RegBuilder_recipe_1d( + reg_builder, 1, &get_k13dd_Entry, + my_chemistry->NumberOfTemperatureBins * 14) != GR_SUCCESS) { + return GrPrintAndReturnErr("error registering k13dd rate"); + } + + //H2 formation on dust grains requires loop over the dust temperature. add_h2dust_reaction_rate(&my_rates->h2dust, kUnit, my_chemistry); diff --git a/src/clib/initialize_rates.hpp b/src/clib/initialize_rates.hpp index 0a004895c..99e2e9b21 100644 --- a/src/clib/initialize_rates.hpp +++ b/src/clib/initialize_rates.hpp @@ -14,12 +14,14 @@ #define INITIALIZE_RATES_HPP #include "grackle.h" +#include "ratequery.hpp" namespace grackle::impl { int initialize_rates(chemistry_data* my_chemistry, chemistry_data_storage* my_rates, code_units* my_units, - double co_length_unit, double co_density_unit); + double co_length_unit, double co_density_unit, + ratequery::RegBuilder* reg_builder); } // namespace grackle::impl diff --git a/src/clib/opaque_storage.hpp b/src/clib/opaque_storage.hpp index a118d42c4..e79776094 100644 --- a/src/clib/opaque_storage.hpp +++ b/src/clib/opaque_storage.hpp @@ -16,6 +16,7 @@ #include "grackle.h" #include "dust/grain_species_info.hpp" #include "internal_types.hpp" +#include "ratequery.hpp" /// a struct that used to wrap some private storage details /// @@ -95,6 +96,9 @@ struct gr_opaque_storage { /// > calculations). An alternative would be to briefly initialize an /// > instance during setup and then repack the data. grackle::impl::GrainSpeciesInfo* grain_species_info; + + /// used to implement the experimental ratequery machinery + grackle::impl::ratequery::Registry* registry; }; #endif /* OPAQUE_STORAGE_HPP */ diff --git a/src/clib/rate_utils.cpp b/src/clib/rate_utils.cpp deleted file mode 100644 index f24cf66fd..000000000 --- a/src/clib/rate_utils.cpp +++ /dev/null @@ -1,239 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// See the LICENSE file for license and copyright information -// SPDX-License-Identifier: NCSA AND BSD-3-Clause -// -//===----------------------------------------------------------------------===// -/// -/// @file -/// Defines functions that perform basic utilities related to rate data. -/// -//===----------------------------------------------------------------------===// - -#include // bool, true, and false are defined -#include // strcmp -#include // LLONG_MAX -#include "grackle.h" -#include "internal_types.hpp" // CollisionalRxnRateCollection -#include "LUT.hpp" // CollisionalRxnLUT -#include "opaque_storage.hpp" // gr_opaque_storage - -// In comparison to the dynamic API for accessing elements of chemistry_data, -// we have explicitly opted NOT to make use of offsetof to access arbitrary -// values within a struct. -// -> while `offsetof` may lead to somewhat simpler code, the validity of using -// it with pointer-arithmetic to access members of structs is murky at best. -// - Things become murky when you consider that the C standard models an -// abstract machine and doesn't place the strongest (or clearest) -// constraints on a compiler when you start arbitrarily messing with -// memory -// - C's object model (and maybe also pointer provenance) is relevant -// -> An extended discussion can be found in this stackoverflow answer and in -// the comments about the answer (https://stackoverflow.com/a/69936600). -// - the most compelling point is that it would be useless for the standard -// to define the `offsetof` if these semantics didn't work -// -> In any case, I'm much more skeptical that valid C++ code can make use of -// offsetof in this fashion (the object model is stricter and C++ provides a -// other machinery to accomplish similar things). Since we want to support -// compilation with a C++ compiler, this is the most important point -// offsetof in this fashion -// - -// we have reserved the right to change this value at any time -enum { UNDEFINED_RATE_ID_ = 0 }; - -// introduce some basic machinery to help us implement dynamic lookup of rates - -typedef struct { - double* data; - const char* name; -} rateprop_; - -static inline rateprop_ mk_rateprop_(double* rate, const char* name) { - rateprop_ out; - out.data = rate; - out.name = name; - return out; -} - -static inline rateprop_ mk_rateprop_standard_kcol_( - chemistry_data_storage* my_rates, const char* name, int index) { - if ((my_rates == nullptr) || (my_rates->opaque_storage == nullptr) || - (my_rates->opaque_storage->kcol_rate_tables == nullptr)) { - return mk_rateprop_(nullptr, name); - } else { - return mk_rateprop_(my_rates->opaque_storage->kcol_rate_tables->data[index], - name); - } -} - -#define MKPROP_(PTR, NAME) \ - mk_rateprop_(((PTR) == NULL) ? NULL : (PTR)->NAME, #NAME) -#define MKPROP_SCALAR_(PTR, NAME) \ - mk_rateprop_(((PTR) == NULL) ? NULL : &((PTR)->NAME), #NAME) -#define MKPROP_STANDARD_KCOL_(PTR, NAME, INDEX) \ - mk_rateprop_standard_kcol_(PTR, #NAME, INDEX) - -// Create machinery to lookup Standard-Form Collisional Reaction Rates -// ------------------------------------------------------------------- -// see the next section for other macros - -// this fn leverages the following properties of the CollisionalRxnLUT enum: -// - each entry of collisional_rxn_rate_members.def has a corresponding -// enumeration-constant -// - the very first enumeration constant has a value of 0 (since a value wasn't -// explicitly specified) -// - the value of each other enumeration constants is 1 larger than the value -// of the previous value (if a value isn't explicitly specified) -// - CollisionalRxnLUT::NUM_ENTRIES specifies the number of other enumeration -// constants (excluding CollisionalRxnLUT::NUM_ENTRIES) in the enum -static rateprop_ get_CollisionalRxn_rateprop_(chemistry_data_storage* my_rates, - int i) { - switch (i) { -#define ENTRY(NAME) \ - case CollisionalRxnLUT::NAME: { \ - return MKPROP_STANDARD_KCOL_(my_rates, NAME, CollisionalRxnLUT::NAME); \ - } -#include "collisional_rxn_rate_members.def" -#undef ENTRY - default: { - rateprop_ out = {NULL, NULL}; - return out; - } - } -} - -// Create machinery to lookup Other Miscellaneous Rates -// ---------------------------------------------------- - -enum MiscRxnRateKind_ { - MiscRxn_k13dd, - MiscRxn_k24, - MiscRxn_k25, - MiscRxn_k26, - MiscRxn_k27, - MiscRxn_k28, - MiscRxn_k29, - MiscRxn_k30, - MiscRxn_k31, - - MiscRxn_NRATES // <- will hold the number of reactions -}; - -static rateprop_ get_MiscRxn_rateprop_(chemistry_data_storage* my_rates, - int i) { - switch (i) { - // density dependent version of k13 (which is a CollisionalRxn) - case MiscRxn_k13dd: - return MKPROP_(my_rates, k13dd); - // Radiative rates for 6-species (for external field): - case MiscRxn_k24: - return MKPROP_SCALAR_(my_rates, k24); - case MiscRxn_k25: - return MKPROP_SCALAR_(my_rates, k25); - case MiscRxn_k26: - return MKPROP_SCALAR_(my_rates, k26); - // Radiative rates for 9-species - case MiscRxn_k27: - return MKPROP_SCALAR_(my_rates, k27); - case MiscRxn_k28: - return MKPROP_SCALAR_(my_rates, k28); - case MiscRxn_k29: - return MKPROP_SCALAR_(my_rates, k29); - case MiscRxn_k30: - return MKPROP_SCALAR_(my_rates, k30); - case MiscRxn_k31: - return MKPROP_SCALAR_(my_rates, k31); - default: { - rateprop_ out = {NULL, NULL}; - return out; - } - } -} - -// define some additional generic machinery -// ---------------------------------------- - -#define RATE_SET_COUNT 2 -typedef rateprop_ fetch_rateprop_fn(chemistry_data_storage*, int); -struct rateprop_set_ { - int id_offset; - int len; - fetch_rateprop_fn* fn; -}; -struct rate_registry_type_ { - int len; - struct rateprop_set_ sets[RATE_SET_COUNT]; -}; - -static const struct rate_registry_type_ rate_registry_ = { - /* len: */ RATE_SET_COUNT, - /* sets: */ { - {1000, CollisionalRxnLUT::NUM_ENTRIES, &get_CollisionalRxn_rateprop_}, - {2000, MiscRxn_NRATES, &get_MiscRxn_rateprop_}}}; - -struct ratequery_rslt_ { - grunstable_rateid_type rate_id; - rateprop_ prop; -}; - -/// internal function to search for the rate_property i -/// -/// We interpret i as rate_id, when use_rate_id is true. Otherwise, we just -/// look for the ith rateprop (we introduce an artificial distinction between -/// the 2 cases because we want to reserve the right to be able to change the -/// relationship if it becomes convenient in the future) -static struct ratequery_rslt_ query_rateprop_(chemistry_data_storage* my_rates, - long long i, bool use_rate_id) { - int total_len = 0; // <- we increment this as we go through the rates - - for (int set_idx = 0; set_idx < rate_registry_.len; set_idx++) { - const struct rateprop_set_ cur_set = rate_registry_.sets[set_idx]; - - const long long tmp = (use_rate_id) ? i - cur_set.id_offset : i - total_len; - if ((tmp >= 0) && (tmp < cur_set.len)) { - struct ratequery_rslt_ out; - out.rate_id = tmp + cur_set.id_offset; - out.prop = cur_set.fn(my_rates, tmp); - return out; - } - total_len += cur_set.len; - } - struct ratequery_rslt_ out = {UNDEFINED_RATE_ID_, {NULL, NULL}}; - return out; -} - -// here we implement the public API -// -------------------------------- - -extern "C" grunstable_rateid_type grunstable_ratequery_id(const char* name) { - if (name == NULL) { - return UNDEFINED_RATE_ID_; - } - - for (int set_idx = 0; set_idx < rate_registry_.len; set_idx++) { - const struct rateprop_set_ cur_set = rate_registry_.sets[set_idx]; - for (int i = 0; i < cur_set.len; i++) { - rateprop_ prop = cur_set.fn(NULL, i); - if (strcmp(name, prop.name) == 0) { - return cur_set.id_offset + i; - } - } - } - return UNDEFINED_RATE_ID_; -} - -extern "C" double* grunstable_ratequery_get_ptr( - chemistry_data_storage* my_rates, grunstable_rateid_type rate_id) { - return query_rateprop_(my_rates, rate_id, true).prop.data; -} - -extern "C" const char* grunstable_ith_rate( - unsigned long long i, grunstable_rateid_type* out_rate_id) { - const long long sanitized_i = (i < LLONG_MAX) ? (long long)i : -1; - struct ratequery_rslt_ tmp = query_rateprop_(NULL, sanitized_i, false); - if (out_rate_id != NULL) { - *out_rate_id = tmp.rate_id; - } - return tmp.prop.name; -} diff --git a/src/clib/ratequery.cpp b/src/clib/ratequery.cpp new file mode 100644 index 000000000..5c8a70d96 --- /dev/null +++ b/src/clib/ratequery.cpp @@ -0,0 +1,667 @@ +//===----------------------------------------------------------------------===// +// +// See the LICENSE file for license and copyright information +// SPDX-License-Identifier: NCSA AND BSD-3-Clause +// +//===----------------------------------------------------------------------===// +/// +/// @file +/// Defines functionality for querying rate data. +/// +//===----------------------------------------------------------------------===// + +#include // std::strcmp, std::strlen, std::strcpy +#include +#include "grackle.h" +#include "internal_types.hpp" // CollisionalRxnRateCollection +#include "LUT.hpp" // CollisionalRxnLUT +#include "opaque_storage.hpp" // gr_opaque_storage +#include "ratequery.hpp" +#include "status_reporting.h" + +#include + +// In comparison to the dynamic API for accessing elements of chemistry_data, +// we have explicitly opted NOT to make use of offsetof to access arbitrary +// values within a struct. +// -> while `offsetof` may lead to somewhat simpler code, the validity of using +// it with pointer-arithmetic to access members of structs is murky at best. +// - Things become murky when you consider that the C standard models an +// abstract machine and doesn't place the strongest (or clearest) +// constraints on a compiler when you start arbitrarily messing with +// memory +// - C's object model (and maybe also pointer provenance) is relevant +// -> An extended discussion can be found in this stackoverflow answer and in +// the comments about the answer (https://stackoverflow.com/a/69936600). +// - the most compelling point is that it would be useless for the standard +// to define the `offsetof` if these semantics didn't work +// -> In any case, I'm much more skeptical that valid C++ code can make use of +// offsetof in this fashion (the object model is stricter and C++ provides a +// other machinery to accomplish similar things). Since we want to support +// compilation with a C++ compiler, this is the most important point +// offsetof in this fashion +// + +namespace grackle::impl::ratequery { +// we have reserved the right to change this value at any time +enum { + UNDEFINED_RATE_ID_ = std::numeric_limits::max() +}; + +// Create machinery to lookup Other Miscellaneous Rates +// ---------------------------------------------------- +// -> ideally this should get relocated to a more sensible location (but not +// sure where that is... + +enum MiscRxnRateKind_ { + MiscRxn_k24, + MiscRxn_k25, + MiscRxn_k26, + MiscRxn_k27, + MiscRxn_k28, + MiscRxn_k29, + MiscRxn_k30, + MiscRxn_k31, + + MiscRxn_NRATES // <- will hold the number of reactions +}; + +static Entry get_MiscRxn_Entry(chemistry_data_storage* my_rates, int i) { + if (my_rates == nullptr) { // <- shouldn't come up + return mk_invalid_Entry(); + } + switch (i) { + // Radiative rates for 6-species (for external field): + case MiscRxn_k24: + return new_Entry(&my_rates->k24, "k24"); + case MiscRxn_k25: + return new_Entry(&my_rates->k25, "k25"); + case MiscRxn_k26: + return new_Entry(&my_rates->k26, "k26"); + // Radiative rates for 9-species + case MiscRxn_k27: + return new_Entry(&my_rates->k27, "k27"); + case MiscRxn_k28: + return new_Entry(&my_rates->k28, "k28"); + case MiscRxn_k29: + return new_Entry(&my_rates->k29, "k29"); + case MiscRxn_k30: + return new_Entry(&my_rates->k30, "k30"); + case MiscRxn_k31: + return new_Entry(&my_rates->k31, "k31"); + default: { + return mk_invalid_Entry(); + } + } +} +} // namespace grackle::impl::ratequery + +int grackle::impl::ratequery::RegBuilder_misc_recipies( + RegBuilder* ptr, const chemistry_data* my_chemistry) { + if (my_chemistry->primordial_chemistry != 0) { + return RegBuilder_recipe_scalar(ptr, MiscRxn_NRATES, &get_MiscRxn_Entry); + } + return GR_SUCCESS; +} + +namespace grackle::impl::ratequery { + +/// calls delete or delete[] on the specified pointer (depending on the value +/// of is_scalar). +/// +/// @note +/// This was originally function-like macro. We can go back to that if people +/// dislike this usage of a template function. +/// function-like macro +template +static void careful_delete_(T* ptr, bool is_scalar) { + (is_scalar) ? delete ptr : delete[] ptr; +} + +/// This deallocates data within a list of owned Entries (i.e. all pointers +/// within an Entry are deleted) +/// +/// Importantly, this does **NOT** call delete[] on entry_list +static void drop_owned_Entry_list_contents(Entry* entry_list, int n_entries) { + for (int entry_idx = 0; entry_idx < n_entries; entry_idx++) { + Entry* cur_entry = &entry_list[entry_idx]; + + // invariant: each Entry within an embedded-list should be valid and + // fully initialized + GR_INTERNAL_REQUIRE(cur_entry->name != nullptr, "sanity check!"); + GR_INTERNAL_REQUIRE(!cur_entry->data.is_null(), "sanity check!"); + + delete[] cur_entry->name; + + bool is_scalar = cur_entry->props.ndim == 0; + switch (cur_entry->data.tag()) { + case PtrKind::const_f64: { + careful_delete_(cur_entry->data.const_f64(), is_scalar); + break; + } + case PtrKind::mutable_f64: { + careful_delete_(cur_entry->data.mutable_f64(), is_scalar); + break; + } + case PtrKind::const_str: { + const char* const* ptr = cur_entry->data.const_str(); + + // get the number of strings referenced by cur_entry->data + int n_strings = 1; + for (int i = 0; i < cur_entry->props.ndim; i++) { + n_strings *= cur_entry->props.shape[i]; + } + + // delete each string within ptr + for (int i = 0; i < n_strings; i++) { + delete[] ptr[i]; + } + + // now actually delete ptr + careful_delete_(ptr, is_scalar); + break; + } + default: + GR_INTERNAL_UNREACHABLE_ERROR(); + } + } +} + +/// Describes a set of entries +/// +/// This can operate in 2 modes: +/// 1. Embedded-List-mode: +/// - the EntrySet directly holds a list of Entry instances that **ONLY** +/// exist for querying purposes and should **NEVER** be mutated by +/// external code. +/// - importantly, the EntrySet is responsible for managing the memory the +/// pointers to string each string and pointer referenced by a pointer in +/// this list. +/// 2. Recipe-mode: +/// - In this case, the EntrySet provides access to Entry instances that +/// directly reference data managed by `chemistry_data_storage` +struct EntrySet { + /// number of entries in the current set + int len; + + /// an embedded list of entries, where the allocation are directly owned and + /// managed by this instance. + /// + /// @important + /// this **must** be a nullptr if operating in Recipe-mode + Entry* embedded_list; + + /// a function pointer that can be used to access entries through a recipe + fetch_Entry_recipe_fn* recipe_fn; + + /// properties used by all entries accessed through a recipe + /// + /// In more detail, an entry returned by `recipe_fn` has its `props` member + /// overwritten by this value + /// + /// @note + /// only used in Recipe-mode + EntryProps common_recipe_props; +}; + +/// look up an Entry in an EntrySet +/// +/// @param[in] entry_set The container object being queried +/// @param[in] my_rates Used for looking up Entry in recipe-mode +/// @param[in] i The index to query +/// +/// @returns An instance that references memory owned by either my_rates or by +/// the entry_set, itself. +Entry EntrySet_access(const EntrySet* entry_set, + chemistry_data_storage* my_rates, int i) { + if (i > entry_set->len) { + return mk_invalid_Entry(); + } else if (entry_set->embedded_list == nullptr) { // in recipe-mode + Entry out = (entry_set->recipe_fn)(my_rates, i); + out.props = entry_set->common_recipe_props; + return out; + } else { // in embedded-list mode + return entry_set->embedded_list[i]; + } +} + +/// deallocate the contents of an EntrySet +void drop_EntrySet(EntrySet* ptr) { + if (ptr->embedded_list == nullptr) { + // nothing to deallocate in recipe-mode + } else { + // in embedded-list-mode, we need to deallocate each Entry in the list + // and the deallocate the actual list-pointer + drop_owned_Entry_list_contents(ptr->embedded_list, ptr->len); + + // deallocate the memory associated with embedded_list + delete[] ptr->embedded_list; + ptr->embedded_list = nullptr; + } +} + +/// Helper function that takes ownership of owned_data +static int RegBuilder_take_data_(RegBuilder* ptr, const char* raw_name, + PtrUnion owned_data, EntryProps props) { + if (raw_name == nullptr) { + return GrPrintAndReturnErr("raw_name is a nullptr"); + } else if (owned_data.is_null()) { + return GrPrintAndReturnErr("owned_data holds a nullptr"); + } else if (!owned_data.is_const_ptr()) { + return GrPrintAndReturnErr("owned_data isn't const"); + } else if (!EntryProps_is_valid(props)) { + return GrPrintAndReturnErr("common_props isn't valid"); + } + + std::size_t n_byte = std::strlen(raw_name) + 1; + char* name = new char[n_byte]; + // NOLINTNEXTLINE(bugprone-not-null-terminated-result) + std::memcpy(name, raw_name, n_byte); + + Entry tmp = mk_invalid_Entry(); + tmp.data = owned_data; + tmp.name = name; + tmp.props = props; + SimpleVec_push_back(ptr->owned_entries, tmp); + return GR_SUCCESS; +} + +static int RegBuilder_recipe_(RegBuilder* ptr, fetch_Entry_recipe_fn* recipe_fn, + int n_entries, EntryProps common_props) { + if (recipe_fn == nullptr) { + return GrPrintAndReturnErr("recipe_fn is a nullptr"); + } else if (n_entries <= 0) { + return GrPrintAndReturnErr("n_entries is not positive"); + } else if (!EntryProps_is_valid(common_props)) { + return GrPrintAndReturnErr("common_props isn't valid"); + } + + SimpleVec_push_back(ptr->recipe_sets, + EntrySet{n_entries, nullptr, recipe_fn, common_props}); + + return GR_SUCCESS; +} + +} // namespace grackle::impl::ratequery + +void grackle::impl::ratequery::drop_RegBuilder(RegBuilder* ptr) { + if (ptr->recipe_sets != nullptr) { + drop_SimpleVec(ptr->recipe_sets); + delete ptr->recipe_sets; + ptr->recipe_sets = nullptr; + } + if (ptr->owned_entries != nullptr) { + int n_entries = SimpleVec_len(ptr->owned_entries); + if (n_entries > 0) { + Entry* entry_l = SimpleVec_extract_ptr_and_make_empty(ptr->owned_entries); + drop_owned_Entry_list_contents(entry_l, n_entries); + } + drop_SimpleVec(ptr->owned_entries); + delete ptr->owned_entries; + ptr->owned_entries = nullptr; + } +} + +int grackle::impl::ratequery::RegBuilder_recipe_scalar( + RegBuilder* ptr, int n_entries, fetch_Entry_recipe_fn* recipe_fn) { + EntryProps common_props = mk_invalid_EntryProps(); + common_props.ndim = 0; + return RegBuilder_recipe_(ptr, recipe_fn, n_entries, common_props); +} + +int grackle::impl::ratequery::RegBuilder_recipe_1d( + RegBuilder* ptr, int n_entries, fetch_Entry_recipe_fn* recipe_fn, + int common_len) { + EntryProps common_props = mk_invalid_EntryProps(); + common_props.ndim = 1; + common_props.shape[0] = common_len; + return RegBuilder_recipe_(ptr, recipe_fn, n_entries, common_props); +} + +int grackle::impl::ratequery::RegBuilder_copied_str_arr1d( + RegBuilder* ptr, const char* name, const char* const* str_arr1d, int len) { + if (len <= 0) { + return GrPrintAndReturnErr("len must be positive"); + } + char** my_copy = new char*[len]; + for (int i = 0; i < len; i++) { + int nbytes = std::strlen(str_arr1d[i]) + 1; + my_copy[i] = new char[nbytes]; + std::memcpy(my_copy[i], str_arr1d[i], nbytes); + } + PtrUnion data(const_cast(my_copy)); + EntryProps props = mk_invalid_EntryProps(); + props.ndim = 1; + props.shape[0] = len; + return RegBuilder_take_data_(ptr, name, data, props); +} + +int grackle::impl::ratequery::RegBuilder_copied_f64_arr1d( + RegBuilder* ptr, const char* name, const double* f64_arr1d, int len) { + if (len <= 0) { + return GrPrintAndReturnErr("len must be positive"); + } + double* my_copy = new double[len]; + std::memcpy(my_copy, f64_arr1d, sizeof(double) * len); + PtrUnion data(const_cast(my_copy)); + EntryProps props = mk_invalid_EntryProps(); + props.ndim = 1; + props.shape[0] = len; + return RegBuilder_take_data_(ptr, name, data, props); +} + +/// build a new Registry. +/// +/// In the process, the current Registry is consumed; it's effectively reset to +/// the state immediately after it was initialized. (This lets us avoid +/// reallocating lots of memory) +grackle::impl::ratequery::Registry +grackle::impl::ratequery::RegBuilder_consume_and_build(RegBuilder* ptr) { + // try to construct an EntrySet that contains all owned entries + if (SimpleVec_len(ptr->owned_entries) > 0) { + EntrySet tmp{/* len = */ SimpleVec_len(ptr->owned_entries), + /* embedded_list = */ + SimpleVec_extract_ptr_and_make_empty(ptr->owned_entries), + /* recipe_fn = */ nullptr, + /* common_recipe_props = */ mk_invalid_EntryProps()}; + SimpleVec_push_back(ptr->recipe_sets, tmp); + } + + // now actually set up the registry + int n_sets = SimpleVec_len(ptr->recipe_sets); + if (n_sets == 0) { + drop_SimpleVec(ptr->recipe_sets); + return Registry{0, n_sets, nullptr, nullptr}; + } else { + EntrySet* sets = SimpleVec_extract_ptr_and_make_empty(ptr->recipe_sets); + // set up id_offsets and determine the total number of entries + int* id_offsets = new int[n_sets]; + int tot_entry_count = 0; + for (int i = 0; i < n_sets; i++) { + id_offsets[i] = tot_entry_count; + tot_entry_count += sets[i].len; + } + Registry out{tot_entry_count, n_sets, id_offsets, sets}; + return out; + } +} + +void grackle::impl::ratequery::drop_Registry(Registry* ptr) { + if (ptr->sets != nullptr) { + delete[] ptr->id_offsets; + ptr->id_offsets = nullptr; + for (int i = 0; i < ptr->n_sets; i++) { + drop_EntrySet(&ptr->sets[i]); + } + delete[] ptr->sets; + ptr->sets = nullptr; + } +} + +namespace grackle::impl::ratequery { + +struct ratequery_rslt_ { + grunstable_rateid_type rate_id; + Entry entry; +}; + +static ratequery_rslt_ invalid_rslt_() { + return {UNDEFINED_RATE_ID_, mk_invalid_Entry()}; +} + +static const Registry* get_registry(const chemistry_data_storage* my_rates) { + if ((my_rates == nullptr) || (my_rates->opaque_storage == nullptr)) { + return nullptr; + } + return my_rates->opaque_storage->registry; +} + +/// internal function to search for the rate description i +/// +/// @note +/// While it would be nice to enforce that rate data in a +/// `const chemistry_data_storage` can't be mutated, that is something that +/// should be done at the public API level. +static ratequery_rslt_ query_Entry(chemistry_data_storage* my_rates, + long long i) { + const Registry* registry = get_registry(my_rates); + + if (registry == nullptr || i >= registry->n_entries) { + return invalid_rslt_(); + } + + for (int set_idx = 0; set_idx < registry->n_sets; set_idx++) { + EntrySet* cur_set = ®istry->sets[set_idx]; + int tmp = i - registry->id_offsets[set_idx]; + if ((tmp >= 0) && (tmp < cur_set->len)) { + return ratequery_rslt_{i, EntrySet_access(cur_set, my_rates, tmp)}; + } + } + return invalid_rslt_(); +} + +/* +/// this only exists for debugging purposes +static void show_Entry(const Entry* entry) { + std::printf( + "{.data=%p, .name=\"%s\", .props={.ndim=%d, .shape={", + reinterpret_cast(entry->data), entry->name, entry->props.ndim); + // we should strongly consider factoring out logic for printing arrays + for (int i = 0; i < entry->props.ndim; i++) { + if (i > 0) { + std::printf(", "); + } + std::printf("%d", entry->props.shape[i]); + } + std::printf("}}\n"); +} +*/ + +/// compute the number of items in an Entry described by @p props +static long long get_n_items(EntryProps props) { + GR_INTERNAL_REQUIRE(props.ndim >= 0, "sanity check!"); + + if (props.ndim == 0) { + return 1LL; // a scalar always consists of 1 item + } + long long n_items = 1LL; + for (int i = 0; i < props.ndim; i++) { + n_items *= static_cast(props.shape[i]); + } + return n_items; +} + +} // namespace grackle::impl::ratequery + +// here we implement the public API +// -------------------------------- + +extern "C" grunstable_rateid_type grunstable_ratequery_id( + const chemistry_data_storage* my_rates, const char* name) { + namespace rate_q = grackle::impl::ratequery; + + const rate_q::Registry* registry = rate_q::get_registry(my_rates); + if ((name == nullptr) || (registry == nullptr)) { + return rate_q::UNDEFINED_RATE_ID_; + } + + for (int set_idx = 0; set_idx < registry->n_sets; set_idx++) { + rate_q::EntrySet* set = ®istry->sets[set_idx]; + int set_len = set->len; + for (int i = 0; i < set_len; i++) { + rate_q::Entry entry = rate_q::EntrySet_access( + set, const_cast(my_rates), i); + if (std::strcmp(name, entry.name) == 0) { + return registry->id_offsets[set_idx] + i; + } + } + } + return rate_q::UNDEFINED_RATE_ID_; +} + +extern "C" int grunstable_ratequery_get_f64(chemistry_data_storage* my_rates, + grunstable_rateid_type rate_id, + double* buf) { + namespace rate_q = grackle::impl::ratequery; + rate_q::Entry entry = rate_q::query_Entry(my_rates, rate_id).entry; + + if (entry.data.is_null()) { // in this case, the query failed + return GR_FAIL; + } + + const double* src; + switch (entry.data.tag()) { + case rate_q::PtrKind::const_f64: + src = entry.data.const_f64(); + break; + case rate_q::PtrKind::mutable_f64: + src = const_cast(entry.data.mutable_f64()); + break; + default: + return GR_FAIL; + } + + long long n_items = rate_q::get_n_items(entry.props); + for (long long i = 0; i < n_items; i++) { + buf[i] = src[i]; + } + return GR_SUCCESS; +} + +extern "C" int grunstable_ratequery_set_f64(chemistry_data_storage* my_rates, + grunstable_rateid_type rate_id, + const double* buf) { + namespace rate_q = grackle::impl::ratequery; + rate_q::Entry entry = rate_q::query_Entry(my_rates, rate_id).entry; + if (entry.data.is_null()) { // in this case, the query failed + return GR_FAIL; + } + + long long n_items = rate_q::get_n_items(entry.props); + if (entry.data.tag() == rate_q::PtrKind::mutable_f64) { + double* dst = entry.data.mutable_f64(); + for (long long i = 0; i < n_items; i++) { + dst[i] = buf[i]; + } + return GR_SUCCESS; + } else { // the retrieved pointer is either immutable or the wrong type + return GR_FAIL; + } +} + +extern "C" int grunstable_ratequery_get_str(chemistry_data_storage* my_rates, + grunstable_rateid_type rate_id, + char* const* buf) { + namespace rate_q = grackle::impl::ratequery; + rate_q::Entry entry = rate_q::query_Entry(my_rates, rate_id).entry; + + if (entry.data.is_null()) { // in this case, the query failed + return GR_FAIL; + } + + if (entry.data.tag() != rate_q::PtrKind::const_str) { + return GR_FAIL; + } + const char* const* src = entry.data.const_str(); + long long n_items = rate_q::get_n_items(entry.props); + for (long long i = 0; i < n_items; i++) { + std::strcpy(buf[i], src[i]); + } + return GR_SUCCESS; +} + +extern "C" int grunstable_ratequery_prop( + const chemistry_data_storage* my_rates, grunstable_rateid_type rate_id, + enum grunstable_ratequery_prop_kind prop_kind, long long* ptr) { + namespace rate_q = grackle::impl::ratequery; + + // short-term hack! (it's bad practice to "cast away the const") + rate_q::Entry entry = + rate_q::query_Entry(const_cast(my_rates), + rate_id) + .entry; + + const rate_q::EntryProps& props = entry.props; + if ((entry.name == nullptr) || !rate_q::EntryProps_is_valid(props)) { + return GR_FAIL; + } + + switch (prop_kind) { + case GRUNSTABLE_QPROP_NDIM: { + *ptr = static_cast(props.ndim); + return GR_SUCCESS; + } + case GRUNSTABLE_QPROP_SHAPE: { + for (int i = 0; i < props.ndim; i++) { + ptr[i] = static_cast(props.shape[i]); + } + return GR_SUCCESS; + } + case GRUNSTABLE_QPROP_MAXITEMSIZE: { + switch (entry.data.tag()) { + case rate_q::PtrKind::const_f64: + case rate_q::PtrKind::mutable_f64: { + *ptr = static_cast(sizeof(double)); + return GR_SUCCESS; + } + case rate_q::PtrKind::const_str: { + long long n_items = rate_q::get_n_items(entry.props); + const char* const* str_list = entry.data.const_str(); + + std::size_t max_size = 1; + for (long long i = 0; i < n_items; i++) { + // max_size holds the max number of bytes per element + max_size = std::max(max_size, std::strlen(str_list[i]) + 1); + } + *ptr = static_cast(max_size); + return GR_SUCCESS; + } + } + // if we reach here then we didn't handle a possible PtrKind + GR_INTERNAL_UNREACHABLE_ERROR(); + } + case GRUNSTABLE_QPROP_WRITABLE: { + *ptr = static_cast(!entry.data.is_const_ptr()); + return GR_SUCCESS; + } + case GRUNSTABLE_QPROP_DTYPE: { + switch (entry.data.tag()) { + case rate_q::PtrKind::const_f64: + case rate_q::PtrKind::mutable_f64: { + *ptr = static_cast(GRUNSTABLE_TYPE_F64); + return GR_SUCCESS; + } + case rate_q::PtrKind::const_str: { + *ptr = static_cast(GRUNSTABLE_TYPE_STR); + return GR_SUCCESS; + } + } + // if we reach here then we didn't handle a possible PtrKind + GR_INTERNAL_UNREACHABLE_ERROR(); + } + } + return GrPrintAndReturnErr("received an unknown prop_kind"); +} + +extern "C" const char* grunstable_ith_rate( + const chemistry_data_storage* my_rates, unsigned long long i, + grunstable_rateid_type* out_rate_id) { + namespace rate_q = grackle::impl::ratequery; + + const long long sanitized_i = + (i < std::numeric_limits::max()) ? (long long)i : -1; + // short-term hack! (it's bad practice to "cast away the const") + rate_q::ratequery_rslt_ tmp = rate_q::query_Entry( + const_cast(my_rates), sanitized_i); + if (out_rate_id != nullptr) { + *out_rate_id = tmp.rate_id; + } + return tmp.entry.name; +} + +extern "C" unsigned long long grunstable_ratequery_nrates( + const chemistry_data_storage* my_rates) { + namespace rate_q = grackle::impl::ratequery; + const rate_q::Registry* registry = my_rates->opaque_storage->registry; + return registry->n_entries; +} diff --git a/src/clib/ratequery.hpp b/src/clib/ratequery.hpp new file mode 100644 index 000000000..89474e7fe --- /dev/null +++ b/src/clib/ratequery.hpp @@ -0,0 +1,401 @@ +//===----------------------------------------------------------------------===// +// +// See the LICENSE file for license and copyright information +// SPDX-License-Identifier: NCSA AND BSD-3-Clause +// +//===----------------------------------------------------------------------===// +/// +/// @file +/// Declares functionality for querying rate data +/// +//===----------------------------------------------------------------------===// +#ifndef RATEQUERY_HPP +#define RATEQUERY_HPP + +#include "grackle.h" +#include "utils-cpp.hpp" // GRIMPL_FORCE_INLINE +#include "status_reporting.h" +#include "support/SimpleVec.hpp" + +namespace grackle::impl::ratequery { + +/// @defgroup Dynamic Rate Query Machinery +/// +/// This group of entities provides the machinery for implementing the API for +/// dynamically accessing rate data. +/// +/// The design of this machinery is based on a simple model: +/// - the query machinery queries a Registry +/// - you can think of a Registry as a container. Each item is an Entry that +/// describes a unique queryable rate. +/// +/// The actualy implementation is a little more sophisticated. In practice is +/// divided into subsets of `Entry` instances. Moreover, subsets are free to +/// treat `Entry` instances as ephemeral objects (i.e. a subset can lazily +/// construct a new `Entry` instance and is allowed to forget about the +/// instance). +/// +/// Design Considerations +/// ===================== +/// Ideally, this machinery should balance 3 design considerations: (1) memory +/// usage, (2) impact on grackle solver initialization, (3) Query Performance. +/// +/// The relative of importance of these considerations should be informed by +/// the fact that dynamic rate API logic is not central to querying logic is +/// not of central important to Grackle as a library: +/// - currently, none of the rates ever need to queried for regular Grackle +/// usage (i.e. they are only queried for debugging/experimentation) +/// - moreover, if we do need to query some information in certain +/// configurations (e.g. assumed grain species yields for different injection +/// pathways), the data probably isn't in the most useful format for +/// everyone. Even if queries were ultra-fast, a performance-oriented user +/// would only query that information once, repack the data so that it's +/// structured in a more useful way, and cache the repacked data. +/// +/// In principle, if we had an idea for an delivered significantly better +/// performance, at the cost of increased memory usage and/or longer +/// initialization, we could consider making construction of the registry (or +/// non-essential registry-entries) a runtime parameter. +/// +/// ASIDE: The current design very much prioritizes doing something easy +/// over runtime performance. +/** @{ */ + +enum struct PtrKind { + const_f64, + mutable_f64, + const_str, + // there's no circumstance where we ever want mutable_str +}; + +/// Represents a pointer union +/// +/// We use a class here because it's extremely easy to introduce undefined +/// behavior when interacting with a union. +/// - it's even easier to get undefined behavior in C++ than in C. In C, you +/// are allowed to access an inactive union member (but that's undefined in +/// C++) +/// +/// The compromise between safety and writing Grackle in a C-like subset of C++ +/// is to basically write a class with getters and setters that is equivalent +/// to the following C struct +/// ```C +/// struct PtrUnion{ +/// union { +/// const double* const_f64; +/// double* mutable_f64; +/// // ... +/// const char * const * const_str; +/// } pointer; +/// +/// enum PtrKind tag; +/// }; +/// ``` +/// The only difference is that this class enforces runtime-checks that will +/// crash the program if a mistake is made (rather than trigger undefined +/// behavior). In practice, if the union is used properly, most runtime checks +/// should get optimized out. +class PtrUnion { + // if we ever wanted to include something other than a pointer or fundamental + // type, then we almost certainly use std::variant rather than a union + union Storage { + const double* const_f64; + double* mutable_f64; + const char* const* const_str; + // there's no scenario where we *EVER* want a reason mutable_str + }; + + // define the data-members (we specify values for the default constructor) + + /// the actual union + Storage pointer = {nullptr}; // <- intializes the first member + /// the tag (that tracks the kind of pointer) + PtrKind tag_ = PtrKind::const_f64; + +public: + // explicitly declare the use of default constructor/assignment/destructor + PtrUnion() = default; + PtrUnion(const PtrUnion&) = default; + PtrUnion(PtrUnion&&) = default; + PtrUnion& operator=(const PtrUnion&) = default; + PtrUnion& operator=(PtrUnion&&) = default; + ~PtrUnion() = default; + + // introduce 2 convenienece methods related to nullptr + + /// constructor to use when is the `nullptr` literal is used + PtrUnion(std::nullptr_t) : PtrUnion() {} + + /// Convenience method to check whether the instance holds a nullptr + /// + /// @note + /// An argument could be made for converting this to a standalone function + /// so that things are more C-like + bool is_null() const { + // maybe we turn on errors for non-exhaustive switch statements with + // a pragma? + switch (this->tag_) { + case PtrKind::const_f64: + return pointer.const_f64 == nullptr; + case PtrKind::mutable_f64: + return pointer.mutable_f64 == nullptr; + case PtrKind::const_str: + return pointer.const_str == nullptr; + } + GR_INTERNAL_UNREACHABLE_ERROR(); + } + + /// Convenience method to check whether the tag holds a const pointer + /// + /// @note + /// An argument could be made for converting this to a standalone function + /// so that things are more C-like + bool is_const_ptr() const { + switch (this->tag_) { + case PtrKind::const_f64: + case PtrKind::const_str: + return true; + case PtrKind::mutable_f64: + return false; + } + GR_INTERNAL_UNREACHABLE_ERROR(); + } + + /// access the tag attribute + PtrKind tag() const { return this->tag_; } + + // methods associated with const_f64 + explicit PtrUnion(const double* ptr) { this->set_const_f64(ptr); } + + GRIMPL_FORCE_INLINE const double* const_f64() const { + GR_INTERNAL_REQUIRE(this->tag_ == PtrKind::const_f64, "has wrong tag"); + return this->pointer.const_f64; + } + + GRIMPL_FORCE_INLINE void set_const_f64(const double* ptr) { + this->tag_ = PtrKind::const_f64; + this->pointer.const_f64 = ptr; + } + + // methods associated with mutable_f64 + explicit PtrUnion(double* ptr) { this->set_mutable_f64(ptr); } + + GRIMPL_FORCE_INLINE double* mutable_f64() const { + GR_INTERNAL_REQUIRE(this->tag_ == PtrKind::mutable_f64, "has wrong tag"); + return this->pointer.mutable_f64; + } + + GRIMPL_FORCE_INLINE void set_mutable_f64(double* ptr) { + this->tag_ = PtrKind::mutable_f64; + this->pointer.mutable_f64 = ptr; + } + + // methods associated with const_str + explicit PtrUnion(const char* const* ptr) { this->set_const_str(ptr); } + + GRIMPL_FORCE_INLINE const char* const* const_str() const { + GR_INTERNAL_REQUIRE(this->tag_ == PtrKind::const_str, "has wrong tag"); + return this->pointer.const_str; + } + + GRIMPL_FORCE_INLINE void set_const_str(const char* const* ptr) { + this->tag_ = PtrKind::const_str; + this->pointer.const_str = ptr; + } +}; + +/// Describes properties about the data in an entry +struct EntryProps { + int ndim; + int shape[GRACKLE_CLOUDY_TABLE_MAX_DIMENSION]; +}; + +inline EntryProps mk_invalid_EntryProps() { + EntryProps out; + out.ndim = -1; + return out; +} + +inline bool EntryProps_is_valid(EntryProps obj) { return obj.ndim >= 0; } + +/// A queryable entity +struct Entry { + PtrUnion data; + const char* name; + EntryProps props; +}; + +inline Entry mk_invalid_Entry() { + return Entry{nullptr, nullptr, mk_invalid_EntryProps()}; +} + +/// Constructs an Entry +inline Entry new_Entry(double* rate, const char* name) { + return Entry{PtrUnion(rate), name, mk_invalid_EntryProps()}; +} + +/// a recipe for querying 1 or more entries from a chemistry_data_storage +/// pointer given an index. A recipe generally lazily creates an Entry as +/// the underlying data is fetched. +/// +/// If `N` denotes the number of entries that can be queried by a given recipe, +/// then this function should produce unique entries for each unique index that +/// satisfies `0 <= index <= (N-1)` +typedef Entry fetch_Entry_recipe_fn(chemistry_data_storage*, int); + +// temporary forward declaration +struct EntrySet; + +/// Describes a registry of queryable entries +/// +/// @note This is constructed from a RegBuilder +struct Registry { + /// number of entries + int n_entries; + /// number of contained EntrySets + int n_sets; + /// stores the minimum rate_id for each EntrySet + int* id_offsets; + /// stores sets of entries + EntrySet* sets; +}; + +/// deallocate the contents of a registry +void drop_Registry(Registry* ptr); + +/// An interface for gradually configuring a Registry +/// +/// @par Context within the codebase +/// This is intended to be an ephemeral object that only lives during within +/// the function that initializes a Grackle solver from user-specified +/// parameters +/// - the instance is created at the start of this function +/// - a pointer to the instance is passed to various initialization functions. +/// These functions can use the instance to register entries that will be +/// accessible in the resulting Registry +/// - if all configuration has gone well, this instance will be used to +/// initialize the Registry +/// - the instance is **always** destroyed when it is time to exit the solver +/// initialization function +/// +/// @par Motivation +/// There are 2 main sources of motivation +/// 1. It lets us locate a "recipe-routine" for accessing data next to the +/// routines that initialize the same data. This makes sense from a +/// code-organization perspective. Moreover, if we conditionally initialize +/// data, it will be easier to ensure that recipies won't try to provide +/// access to the uninitialized data +/// 2. this will make it easier for us to handle data that Grackle only +/// initializes for the sake of supporting user queries (at the moment, +/// this is hypothetical) +/// +/// @important +/// Other parts of grackle should refrain from directly accessing the internals +/// of this function (i.e. they should only use the associated methods) +struct RegBuilder { + /// a growable array that records recipies for accessing sets of entries + SimpleVec* recipe_sets; + /// a growable array of owned Entry instances + /// + /// The basic premise is that these Entry instances **ONLY** exist for the + /// purpose of supporting queries. Cleaning up each instance involves extra + /// effort. When a Registry instance is constructed, this pointer will be + /// transferred to an EntrySet. + SimpleVec* owned_entries; +}; + +/// initialize a new instance +inline RegBuilder new_RegBuilder() { + // by default SimpleVec is automatically initialized + return {new SimpleVec, new SimpleVec}; +} + +/// deallocates all storage within a RegBuilder instance +void drop_RegBuilder(RegBuilder* ptr); + +/// register a recipe for accessing scalar values +/// +/// @param[inout] ptr The RegBuilder that will be updated +/// @param[in] n_entries The number of entries accessible through the recipe +/// @param[in] recipe_fn The recipe being registered +/// +/// @returns GR_SUCCESS if successful, otherwise returns a different value +int RegBuilder_recipe_scalar(RegBuilder* ptr, int n_entries, + fetch_Entry_recipe_fn* recipe_fn); + +/// register a recipe for accessing 1D arrays +/// +/// @param[inout] ptr The RegBuilder that will be updated +/// @param[in] n_entries The number of entries accessible through the recipe +/// @param[in] recipe_fn The recipe being registered +/// @param[in] common_len The length shared by each 1D array accessible +/// through this recipe. +/// +/// @returns GR_SUCCESS if successful, otherwise returns a different value +int RegBuilder_recipe_1d(RegBuilder* ptr, int n_entries, + fetch_Entry_recipe_fn* recipe_fn, int common_len); + +/// registers miscellaneous recipes +/// +/// @note +/// This is a hack until we can figure out a better spot to put definitions of +/// some miscellaneous rates +int RegBuilder_misc_recipies(RegBuilder* ptr, + const chemistry_data* my_chemistry); + +/// copies a 1d array of strings to make a queryable entry +/// +/// The resulting Registry will have a queryable entry corresponding to the +/// specified 1D array of strings. Importantly, the RegBuilder and Registry +/// hold a deepcopy of the specified strings. +/// +/// @param[inout] ptr The RegBuilder that will be updated +/// @param[in] name The queryable name that corresponds to the provided data +/// @param[in] str_arr1d The 1d array of strings +/// @param[in] len The length of str_arr1d +/// +/// @returns GR_SUCCESS if successful, otherwise returns a different value +/// +/// @note +/// This is intended to be used for making information available that Grackle +/// otherwise does not need access to. +int RegBuilder_copied_str_arr1d(RegBuilder* ptr, const char* name, + const char* const* str_arr1d, int len); + +/// copies a 1d array of doubles to make a queryable entry +/// +/// The resulting Registry will have a queryable entry corresponding to the +/// specified 1D array of doubles. Importantly, the RegBuilder and Registry +/// hold a deepcopy of the values. +/// +/// @param[inout] ptr The RegBuilder that will be updated +/// @param[in] name The queryable name that corresponds to the provided data +/// @param[in] f64_arr1d The 1d array of doubles +/// @param[in] len The length of str_list +/// +/// @returns GR_SUCCESS if successful, otherwise returns a different value +/// +/// @note +/// This is intended to be used for making information available that Grackle +/// otherwise does not need access to. For example, it might be used to provide +/// information about assumed abundances that Grackle doesn't directly use (i.e. +/// the abundances could be "baked into" some tables), but external simulation +/// codes may want to know about to achieve better consistency. +int RegBuilder_copied_f64_arr1d(RegBuilder* ptr, const char* name, + const double* f64_arr1d, int len); + +/// build a new Registry. +/// +/// In the process, the current Registry is consumed; it's effectively reset to +/// the state immediately after it was initialized. (This lets us avoid +/// reallocating lots of memory) +/// +/// @note +/// For safety, the caller should still plan to call drop_RegBuilder +Registry RegBuilder_consume_and_build(RegBuilder* ptr); + +/** @}*/ // end of group + +} // namespace grackle::impl::ratequery + +#endif // RATEQUERY_HPP diff --git a/src/clib/solve_rate_cool_g-cpp.cpp b/src/clib/solve_rate_cool_g-cpp.cpp index f96243f58..bde08114e 100644 --- a/src/clib/solve_rate_cool_g-cpp.cpp +++ b/src/clib/solve_rate_cool_g-cpp.cpp @@ -791,7 +791,7 @@ int solve_rate_cool_g( // declare 2 variables (primarily used for subcycling, but also used in // error reporting) int iter; - double ttmin; + double ttmin = huge8; // ------------------ Loop over subcycles ---------------- diff --git a/src/clib/status_reporting.h b/src/clib/status_reporting.h index f52f24651..60373d4b0 100644 --- a/src/clib/status_reporting.h +++ b/src/clib/status_reporting.h @@ -230,6 +230,7 @@ ERRFMT_ATTR_(2) NORETURN_ATTR_ void grimpl_abort_with_internal_err_( #define GRIMPL_ERROR(...) \ { grimpl_abort_with_internal_err_(__GRIMPL_SRCLOC__, __VA_ARGS__); } + /// @def GR_INTERNAL_REQUIRE /// @brief implements functionality analogous to the assert() macro /// @@ -255,6 +256,24 @@ ERRFMT_ATTR_(2) NORETURN_ATTR_ void grimpl_abort_with_internal_err_( { if (!(cond)) \ { grimpl_abort_with_internal_err_(__GRIMPL_SRCLOC__, __VA_ARGS__); } } + +/// @def GR_INTERNAL_UNREACHABLE_ERROR() +/// @brief function-like macro that aborts with a (lethal) error message +/// indicating that +/// +/// This macro should be treated as a function with the signature: +/// +/// [[noreturn]] void GR_INTERNAL_UNREACHABLE_ERROR(); +/// +/// @note +/// Unlike gcc/clang's __builtin_unreachable or C++23's std::unreachable, this +/// aborts the program with an error if its executed (the other cases produce +/// undefined behavior). (An argument could be made for conditionally compiling +/// this macro into the alternatives to test speed) +#define GR_INTERNAL_UNREACHABLE_ERROR() \ + { grimpl_abort_with_internal_err_(__GRIMPL_SRCLOC__, \ + "location shouldn't be reachable"); } + // helper function ERRFMT_ATTR_(2) NODISCARD_ATTR_ int grimpl_print_and_return_err_( const struct grimpl_source_location_ locinfo, const char* msg, ... @@ -320,6 +339,22 @@ ERRFMT_ATTR_(2) void grimpl_print_err_msg_( #define GrPrintErrMsg(...) \ grimpl_print_err_msg_(__GRIMPL_SRCLOC__, __VA_ARGS__); +/// @def GR_INTERNAL_UNREACHABLE_ERROR() +/// @brief function-like macro that aborts with a (lethal) error message +/// indicating that +/// +/// This macro should be treated as a function with the signature: +/// +/// [[noreturn]] void GR_INTERNAL_UNREACHABLE_ERROR(); +/// +/// @note +/// Unlike gcc/clang's __builtin_unreachable or C++23's std::unreachable, this +/// aborts the program with an error if its executed (the other cases produce +/// undefined behavior). (An argument could be made for conditionally compiling +/// this macro into the alternatives to test speed) +#define GR_INTERNAL_UNREACHABLE_ERROR() \ +{ grimpl_abort_with_internal_err_(__GRIMPL_SRCLOC__, \ +"location shouldn't be reachable"); } // undefine the attributes so we avoid leaking them // ------------------------------------------------ diff --git a/src/clib/support/SimpleVec.hpp b/src/clib/support/SimpleVec.hpp new file mode 100644 index 000000000..74ede3b33 --- /dev/null +++ b/src/clib/support/SimpleVec.hpp @@ -0,0 +1,140 @@ +//===----------------------------------------------------------------------===// +// +// See the LICENSE file for license and copyright information +// SPDX-License-Identifier: NCSA AND BSD-3-Clause +// +//===----------------------------------------------------------------------===// +/// +/// @file +/// Implements the SimpleVec type +/// +//===----------------------------------------------------------------------===// +#ifndef SUPPORT_SIMPLEVEC_HPP +#define SUPPORT_SIMPLEVEC_HPP + +#include +// #include + +#include "status_reporting.h" + +namespace grackle::impl { + +/// This is a **VERY** simplified version of std::vector that is intended to be +/// very C-like. +/// +/// It is C-like in the sense that: +/// - it essentially acts like a struct with a bunch of associated functions +/// (the associated functions act like methods). +/// - **IMPORTANTLY:** the caller is responsible for explicitly calling the +/// destructor-function. +/// +/// @par Motivation +/// This type is motivated by the fact that to dynamically build up objects, +/// you commonly need to construct arrays where the instances are not +/// well-known ahead of time. This comes up with enough frequency that it is +/// useful to define an abstraction for this data-structure +/// +/// @todo +/// We should **STRONGLY** consider replacing this type with std::vector. In my +/// opinion, the primary "cost" is that std::vector is more "contagious." +/// Unlike the status quo where we can extract the underlying pointer (and take +/// ownership of it), storage can't be taken from a std::vector; you need to +/// either allocate a new pointer or continue carrying around the underlying +/// vector) +/// +/// @par Justification for making this a class template +/// The alternatives to making this a template are *MUCH* less desirable: +/// 1. We could use a macro to define a version of this type and all associated +/// functions for each contained type. This is messy, and we would need to +/// come up with unique names for each version of the primary struct. In +/// practice, the template is doing this under the hood +/// 2. We could convert data from `T*` to `void**`. While this is doable, it +/// will require extra memory. Every time we push back a value, we would +/// need to copy that value into newly allocated memory and cast a pointer +/// to that memory to `void*`. For cleanup, you would need to cast each +/// `void*` back to the original type before calling `delete` +template +struct SimpleVec { + // declare the struct-members + // - default-member initialization is used to ensure that this struct is in + // a valid state without calling an explicit constructor function + int capacity = 0; + int len = 0; + T* data = nullptr; + + // in practice, the following logic prevents other structs from directly + // embedding an instance of this type as a data member (a pointer to this + // struct must be stored) + // - There is always a risk of mistakes with a C-like API when it comes to + // dangling pointers. But, the SimpleVec_extract_ptr_and_make_empty + // function definitely amplifies the risk. + // - By forcing the use of pointers to this type, we are mitigating this risk + // to an extent at the cost of an extra pointer indirection. + // - (if we're willing to fully embrace C++, we could do a **LOT** better) + SimpleVec() = default; + SimpleVec(const SimpleVec&) = delete; + SimpleVec(SimpleVec&&) = delete; + SimpleVec& operator=(const SimpleVec&) = delete; + SimpleVec& operator=(SimpleVec&&) = delete; +}; + +/// Deletes internal data within a vec (acts like a destructor) +/// +/// As per usual, this does not try to directly deallocate the pointer itself +template +void drop_SimpleVec(SimpleVec* vec) { + if (vec->data != nullptr) { + delete[] vec->data; + } + vec->data = nullptr; + vec->capacity = 0; + vec->len = 0; +} + +/// returns the internal data pointer (the caller takes ownership of it) and +/// modifies the provided vec so that it's equivalent to an empty vector +/// +/// After this function executes, passing the argument to drop_SimpleVec or to +/// SimpleVec_push_back will **NOT** affect the extracted pointer. +template +T* SimpleVec_extract_ptr_and_make_empty(SimpleVec* vec) { + T* ptr = vec->data; + vec->data = nullptr; + vec->capacity = 0; + vec->len = 0; + return ptr; +} + +/// Deletes internal data within a vec +template +void SimpleVec_push_back(SimpleVec* vec, T value) { + if (vec->capacity == 0) { + vec->capacity = 5; + vec->data = new T[vec->capacity]; + } else if (vec->capacity == vec->len) { + int max_cap = std::numeric_limits::max(); + GR_INTERNAL_REQUIRE(vec->capacity < max_cap, "should never happen!"); + int new_cap = + ((max_cap / 2) <= vec->capacity) ? max_cap : vec->capacity * 2; + T* new_data = new T[new_cap]; + for (int i = 0; i < vec->len; i++) { + new_data[i] = vec->data[i]; + } + delete[] vec->data; + vec->data = new_data; + vec->capacity = new_cap; + } + + vec->data[vec->len] = value; + ++(vec->len); +} + +/// Deletes internal data within a vec +template +int SimpleVec_len(const SimpleVec* vec) { + return vec->len; +} + +} // namespace grackle::impl + +#endif // SUPPORT_SIMPLEVEC_HPP diff --git a/src/include/grackle.h b/src/include/grackle.h index 0473ebd0e..1a43077e4 100644 --- a/src/include/grackle.h +++ b/src/include/grackle.h @@ -188,7 +188,7 @@ int gr_initialize_field_data(grackle_field_data *my_fields); /// > anyways to support any variant of this API for dynamic rates) /// /// > [!note] -/// > There are a number of considerations before stablization. Some are +/// > There are a number of considerations before stabilization. Some are /// > discussed in docstrings. Below we summarize more generic considerations: /// > 1. Do we want to be able to use this API to eventually support a dynamic /// > set of rates? If so, then: @@ -198,22 +198,54 @@ int gr_initialize_field_data(grackle_field_data *my_fields); /// > - we should probably update all of the function signatures so that a /// > pointer to chemistry_data_storage* is passed as an argument to each /// > of the functions. -/// > 2. Should we prevent direct access to the underlying data pointers? -/// > Instead we would provide getter and setters. We discuss this further -/// > down below (in the relevant function's API). This is important for -/// > different backends. -/// > 3. We should generally consider whether the API is consistent enough with +/// > 2. We should generally consider whether the API is consistent enough with /// > APIs for accessing other data structures. /// > - currently the dynamic parameter API is grackle's only other /// > data-access API, but we could imagine creating an API for the fields /// > (in order to achieve ABI stability) +/// > 3. I think it may be beneficial to follow the example of yt and make keys +/// > 2 element strings (rather than just a single string). +/// > - this is attractive because it would let us group things together +/// > - for example, we could group k1, k2, k3, ... under a group of +/// > collisional reaction rates. It might be useful to list yields for +/// > different injection pathways (e.g. used in the multi-dust grain +/// > species model) under a separate group. +/// > 4. We should consider improving "ergonomics" of this API +/// > - accessing arrays of strings are currently very clunky. +/// > - Maybe we should avoid requiring a deep-copy? And just require the +/// > user to allocate a buffer to hold pointers to internal strings? I +/// > don't love this. +/// > - There's a piece of me that wonders if we could entirely eliminate +/// > arrays of strings (while still supporting single strings) by +/// > adopting keys composed of 2 strings and refactoring +/// > grackle_field_data to only provide access to strings through keys +/// > (as in PR 271) +/// > - property-querying also feels a little clunky... See the note +/// > attached to that docstring for more details +/// > 5. We need to come up with a consistent strategy when rates are not +/// > defined. +/// > - In a lot of cases, I think it's ok to simply "not" define the rate. +/// > For example, when primordial_chemistry=0, I don't think we should +/// > define rates like k1, k2, k3, ... If we ever want to support a +/// > dynamic API, I think this approach makes a lot of sense in a lot of +/// > cases. +/// > - There's a separate, related question of whether we should support the +/// > idea of an empty entry (i.e. a 1D array with 0 entries). This may +/// > come up in the context of arrays of strings +/// > - for example, if we normally list the names of all dust grain +/// > species, we may want an empty string when there are no dust grain +/// > grain species +/// > - but does it make sense to support an empty scalar? +/// > - as we discuss earlier in this list, it's plausible certain changes +/// > could make string-datasets unnecessary. In that case, this point +/// > may become moot. /** @{ */ /// @typedef grunstable_rateid_type /// @brief Type of rate-ids returned to the user /// /// > [!note] -/// > Before stablizing, we should consider: +/// > Before stabilizing, we should consider: /// > - if we should use `int64_t` rather than `long long` /// > - if we want to make this a more generic name like `gr_idtype` (i.e. if /// > we plan to use rates in other parts of the code) @@ -231,55 +263,153 @@ typedef long long grunstable_rateid_type; /// > [!note] /// > While this is unstable, not all rates may be known yet (but, the number /// > rates are all accessible). -grunstable_rateid_type grunstable_ratequery_id(const char* name); +grunstable_rateid_type grunstable_ratequery_id( + const chemistry_data_storage* my_rates, const char* name); -/// Access the pointer associated with the rateid from myrates +/// Copy data associated with the @p rate_id in @p my_rates into buf /// -/// The behavior is **NOT** currently defined if the `my_rates` struct isn't -/// configured to use the specified rate. +/// The behavior is currently undefined if: +/// - the @p my_rates argument is a NULL pointer +/// - the @p my_rates argument isn't configured to use the specified rate +/// - the @p buf argument is NULL +/// - the @p buf isn't large enough to store the copied rates /// -/// @return The pointer to the accessed data (whether the rate data corresponds -/// to an array or scalar). If an invalid `rate_id` is specified, this -/// returns NULL. +/// @param[in] my_rates The object from which data is retrieved +/// @param[in] rate_id The id of the rate for which the data is retrieved +/// @param[out] buf The buffer that the function writes to +/// +/// @return Returns GR_SUCCESS if successful. If an invalid @p rate_id is +/// specified, this returns a different value +/// +/// > [!note] +/// > Before stablizing, we should consider whether we want to add an argument +/// > that specifies the supplied size of `buf` (and provide well-defined +/// > behavior when `buf` is too small +int grunstable_ratequery_get_f64( + chemistry_data_storage* my_rates, grunstable_rateid_type rate_id, + double* buf +); + + +/// Overwrite the data associated with @p rate_id in @p my_rates with the +/// values provided by @p buf +/// +/// The behavior is currently undefined if: +/// - the @p my_rates argument is a NULL pointer +/// - the @p my_rates argument isn't configured to use the specified rate +/// - the @p buf argument is NULL +/// - the @p buf isn't large enough to store the copied rates +/// +/// @param[out] my_rates The object from which data is retrieved +/// @param[in] rate_id The id of the rate for which the data is retrieved +/// @param[in] buf The buffer that the function writes to +/// +/// @return Returns GR_SUCCESS if successful. If an invalid @p rate_id is +/// specified, this returns a different value /// /// > [!note] -/// > Before stablizing, we should consider whether we actually want this -/// > function. It may be better to replace it with a getter (that copies the -/// > rate data to the supplied buffer) and a setter (that copies the provided -/// > data out of the provided buffer). This may be appealing for a number of -/// > reasons: -/// > 1. When we add GPU support, the rate data may live permanently on the GPU -/// > (or some data may live on the GPU while other data lives on the CPU). -/// > With dedicated setters/getters, we could always require that the -/// > provided buffer data is on the CPU. -/// > 2. We have the option to conditionally disable the setter. For example, -/// > if we choose to support custom rates, I assume we will want to specify -/// > all custom choices as part of initialization (for simplicity) and then -/// > we may want to deprecate/remove the setter. (One could also imagine, -/// > disabling the setter when using GPUs). -/// > 3. It eliminates a certain class of memory-lifetime bugs (People could -/// > try use the pointer returned by the incarnation of this function after -/// > destroying Grackle. This problem can't happen with the proposed -/// > getter/setter) -double* grunstable_ratequery_get_ptr( - chemistry_data_storage* my_rates, grunstable_rateid_type rate_id +/// > Before stabilizing, we should consider whether we want to add an argument +/// > that specifies the supplied size of `buf` (and provide well-defined +/// > behavior when `buf` is too small +int grunstable_ratequery_set_f64( + chemistry_data_storage* my_rates, grunstable_rateid_type rate_id, + const double* buf ); +int grunstable_ratequery_get_str( + chemistry_data_storage* my_rates, grunstable_rateid_type rate_id, + char* const * buf +); + + +/// Describe types known to grackle +/// +/// > [!note] +/// > Before stabilizing, we should consider: +/// > 1. whether this is general enough to be useful throughout grackle. +/// > - For example, we could imagine adding a function in the future to the +/// > chemistry_data dynamic-access API that generically tries to query +/// > datatype. +/// > - In that case, we would want to reuse these macros +/// > 2. if these should actually be defined as macros (that may make it easier +/// > to support them from Fortran +enum grunstable_types { + GRUNSTABLE_TYPE_F64 = 1, + GRUNSTABLE_TYPE_STR = 2, +}; + +/// Describe Rate-Query Property Types +/// +/// > [!note] +/// > It may make more sense to use macros if we want to support these from +/// > Fortran +/// +/// > [!important] +/// > Users should obviously avoid hardcoding values in their codebase. +enum grunstable_ratequery_prop_kind { + GRUNSTABLE_QPROP_NDIM = 1, + GRUNSTABLE_QPROP_SHAPE = 2, + GRUNSTABLE_QPROP_MAXITEMSIZE = 3, + GRUNSTABLE_QPROP_WRITABLE = 4, + GRUNSTABLE_QPROP_DTYPE = 5, +}; + +/// Query a property of the specified rate +/// +/// @param[in] my_rates The object being queried +/// @param[in] rate_id The id of the rate for which the property is queried +/// @param[in] prop_kind The property to query +/// @param[out] ptr The pointer where the property is recorded +/// +/// @returns GR_SUCCESS if successful. Otherwise, a different value is returned. +/// +/// The behavior is undefined when @p my_rates is a `nullptr`, @p ptr is a +/// nullptr or @p ptr doesn't have enough space to store the queried property +/// +/// @note +/// It turns out that using this interface is a little clunky... +/// - before stabilization, it may be better to change this function so that +/// - it accepts an additional argument specifying the total size of the +/// provided buffer (and report an error if the buffer isn't long enough) +/// - it would also be great (but not essential) to change the return-value +/// to specify: +/// - the number of entries that were filled written to ptr by this +/// function +/// - aside: we need to return 0 for GRUNSTABLE_QPROP_SHAPE when +/// querying a scalar quantity +/// - a negative value would denote an error +/// - optionally, when ptr is a nullptr, we could have the function return +/// the number of required ptr needs (similar to the interface for +/// snprintf), but this isn't necessary +/// - Doing this would allow users to write extra code to handle queries of +/// GRUNSTABLE_QPROP_SHAPE in a special way (i.e. currently you **NEED** to +/// query GRUNSTABLE_QPROP_NDIM, first to make sure you allocate enough space) +int grunstable_ratequery_prop(const chemistry_data_storage* my_rates, + grunstable_rateid_type rate_id, + enum grunstable_ratequery_prop_kind prop_kind, + long long* ptr); + /// Query the name (and optionally the rate_id) of the ith registered rate /// /// > [!warning] /// > The order of parameters may change between different versions of Grackle /// -/// @param[in] i the index of the access rate +/// @param[in] my_rates The object being queried +/// @param[in] i the index of the accessed rate /// @param[out] out_rate_id A pointer to store the rate of the queried rate_id. /// The behavior is **NOT** currently well defined when there are `i` or /// fewer registered rates. /// @result Pointer to the string-literal specifying the rate's name. This is /// `NULL`, if there are `i` or fewer registered rates. const char* grunstable_ith_rate( - unsigned long long i, grunstable_rateid_type* out_rate_id + const chemistry_data_storage* my_rates, unsigned long long i, + grunstable_rateid_type* out_rate_id ); +/// Query the number of rates accessible through the ratequery API +unsigned long long grunstable_ratequery_nrates( + const chemistry_data_storage* my_rates); + /** @}*/ // end of group #ifdef __cplusplus diff --git a/src/python/gracklepy/grackle_defs.pxd b/src/python/gracklepy/grackle_defs.pxd index 7ac1eecd2..7f10e54c3 100644 --- a/src/python/gracklepy/grackle_defs.pxd +++ b/src/python/gracklepy/grackle_defs.pxd @@ -212,6 +212,7 @@ cdef extern from "grackle.h": # defined in "grackle.h" # ---------------------- + cdef int GR_SUCCESS cdef int GRACKLE_FAIL_VALUE "GR_FAIL" cdef int GR_SPECIFY_INITIAL_A_VALUE @@ -298,12 +299,44 @@ cdef extern from "grackle.h": # the unstable API ctypedef long long grunstable_rateid_type - grunstable_rateid_type grunstable_ratequery_id(const char* name) + grunstable_rateid_type grunstable_ratequery_id( + const c_chemistry_data_storage* my_rates, + const char* name) - double* grunstable_ratequery_get_ptr( + int grunstable_ratequery_get_f64( c_chemistry_data_storage* my_rates, - grunstable_rateid_type rate_id) + grunstable_rateid_type rate_id, + double* buf) + + int grunstable_ratequery_set_f64( + c_chemistry_data_storage* my_rates, + grunstable_rateid_type rate_id, + const double* buf) + + int grunstable_ratequery_get_str( + c_chemistry_data_storage* my_rates, + grunstable_rateid_type rate_id, + char* const * buf + ); + + cdef enum grunstable_types: + GRUNSTABLE_TYPE_F64 + GRUNSTABLE_TYPE_STR + + cdef enum grunstable_ratequery_prop_kind: + GRUNSTABLE_QPROP_NDIM + GRUNSTABLE_QPROP_SHAPE + GRUNSTABLE_QPROP_MAXITEMSIZE + GRUNSTABLE_QPROP_WRITABLE + GRUNSTABLE_QPROP_DTYPE + + int grunstable_ratequery_prop( + const c_chemistry_data_storage* my_rates, + grunstable_rateid_type rate_id, + grunstable_ratequery_prop_kind prop_kind, + long long* ptr) const char* grunstable_ith_rate( + const c_chemistry_data_storage* my_rates, unsigned long long i, grunstable_rateid_type* out_rate_id) diff --git a/src/python/gracklepy/grackle_wrapper.pyx b/src/python/gracklepy/grackle_wrapper.pyx index d8d827f88..718790b43 100644 --- a/src/python/gracklepy/grackle_wrapper.pyx +++ b/src/python/gracklepy/grackle_wrapper.pyx @@ -12,15 +12,51 @@ ######################################################################## import copy -import functools +import sys from gracklepy.utilities.physical_constants import \ boltzmann_constant_cgs, \ mass_hydrogen_cgs from libc.limits cimport INT_MAX +from libc.stdlib cimport malloc, free from .grackle_defs cimport * import numpy as np +# define a set of rate-related properties that the chemistry_data extension +# type must support as attributes: +# - historically, these rates have been accessible regardless of whether a +# chemistry solver class has been defined to use them. +# - in the near future, the _rate_mapping_access machinery may lose the ability +# to access rate buffers that are not being actively used +# - we will use this set to ensure that these types remain accessible +_legacy_rate_attrs = frozenset( + [ + "k1", "k2", "k3", "k4", "k5", "k6", "k7", "k8", "k9", "k10", "k11", + "k12", "k13", "k14", "k15", "k16", "k17", "k18", "k19", "k20", "k21", + "k22", "k23", "k50", "k51", "k52", "k53", "k54", "k55", "k56", "k57", + "k58", "k13dd", + # radiative rates: + "k24", "k25", "k26", "k27", "k28", "k29", "k30", "k31", + + # A question for another time (before releasing Grackle 3.5): + # - do we want to provide an alternative approach for people to query + # rates? (like a method or function?) + # - If so, then maybe we don't the following to be attributes of + # chemistry_data + # - for now, they are accessible as attributes of chemistry_data + + # 15 species rates (with DM, HDII, HeHII) + "k125", "k129", "k130", "k131", "k132", "k133", "k134", "k135", "k136", + "k137", "k148", "k149", "k150", "k151", "k152", "k153", + # metal species rates: + "kz15", "kz16", "kz17", "kz18", "kz19", "kz20", "kz21", "kz22", "kz23", + "kz24", "kz25", "kz26", "kz27", "kz28", "kz29", "kz30", "kz31", "kz32", + "kz33", "kz34", "kz35", "kz36", "kz37", "kz38", "kz39", "kz40", "kz41", + "kz42", "kz43", "kz44", "kz45", "kz46", "kz47", "kz48", "kz49", "kz50", + "kz51", "kz52", "kz53", "kz54", + ] +) + cdef class chemistry_data: cdef _wrapped_c_chemistry_data data cdef c_chemistry_data_storage rates @@ -38,12 +74,7 @@ cdef class chemistry_data: def __cinit__(self): self.data = _wrapped_c_chemistry_data() - self._rate_map = _rate_mapping_access.from_ptr_and_callback( - ptr=&self.rates, - callback=functools.partial( - _get_rate_shape, wrapped_chemistry_data_obj = self.data - ) - ) + self._rate_map = _rate_mapping_access.from_ptr(&self.rates) self.data_copy_from_init = None cdef void _try_uninitialize(self): @@ -99,10 +130,8 @@ cdef class chemistry_data: except KeyError: pass - try: - return self._rate_map[name] # case where name specifies a rate - except KeyError: - pass + if name in _legacy_rate_attrs: + return self._rate_map.get(name) # this method is expected to raise AttributeError when it fails raise AttributeError( @@ -149,11 +178,15 @@ cdef class chemistry_data: except KeyError: pass - try: - self._rate_map[name] = value - return # early exit - except KeyError: - pass + if name in _legacy_rate_attrs: + try: + self._rate_map[name] = value + return # early exit + except KeyError: + raise AttributeError( + f"attribute '{name}' of '{type(self).__name__}' can't be " + "mutated under the current configuration" + ) from None raise AttributeError( f"'{type(self).__name__}' object has no attribute '{name}'" @@ -871,44 +904,176 @@ cdef class _wrapped_c_chemistry_data: out[k] = self[k] return out -def _get_rate_shape(wrapped_chemistry_data_obj, rate_name): - # for now we need to manually keep this updated. - # -> in the future, we could add probably encode some/all of this - # information within Grackle's ratequery API - - def _is_standard_colrecombination_rate(rate_name): - if rate_name[:2] == 'kz' and rate_name[2:].isdecimal(): - return 11 <= int(rate_name[2:]) <= 54 - elif rate_name[:1] == 'k' and rate_name[1:].isdecimal(): - digit = int(rate_name[1:]) - return ( (1 <= digit <= 23) or - (50 <= digit <= 58) or - (125 <= digit <= 153) ) - return False - - if rate_name in ("k24", "k25", "k26", "k27", "k28", "k29", "k30", "k31"): - return () # the rate is a scalar - elif _is_standard_colrecombination_rate(rate_name): - return (wrapped_chemistry_data_obj['NumberOfTemperatureBins'],) - elif rate_name == 'k13dd': - return (wrapped_chemistry_data_obj['NumberOfTemperatureBins'] * 14,) + +def _portable_reshape(arr: np.ndarray, shape: tuple[int, ...]) -> np.ndarray: + """ + Reshape a numpy array (raises an error if a copy can't be avoided) + """ + if np.__version__.startswith("1.") or np.__version__.startswith("2.0."): + out = arr.view() + out.shape = shape + return out else: - raise RuntimeError( - "the shape of the rate {rate_name!r} has not been specified yet" + return arr.reshape(shape, order="C", copy=False) + + +class RatequeryFailException(Exception): + pass + +cdef long long rateq_raw_nonshape_prop( + c_chemistry_data_storage *ptr, + grunstable_rateid_type rate_id, + grunstable_ratequery_prop_kind prop_kind, +) except*: + """ + queries a non-shape property & raises RatequeryFailException on failure + + This **ONLY** exists to simplify the implementation of rateq_get_prop + """ + cdef long long buf + cdef int ret = grunstable_ratequery_prop(ptr, rate_id, prop_kind, &buf) + if ret != GR_SUCCESS: + raise RatequeryFailException() + return buf + +cdef object rateq_get_prop( + c_chemistry_data_storage *ptr, + grunstable_rateid_type rate_id, + grunstable_ratequery_prop_kind prop_kind, +) except*: + """ + query the property and coerce the result to the appropriate python type + + Notes + ----- + If performance becomes a concern, we should stop using + RatequeryFailException. + """ + cdef long long buf[7] + cdef int ret_code + if prop_kind == GRUNSTABLE_QPROP_SHAPE: + ndim = rateq_get_prop(ptr, rate_id, prop_kind=GRUNSTABLE_QPROP_NDIM) + if ndim > 7: + raise RuntimeError( + f"rate_id {rate_id} has a questionable number of dims: {ndim}" + ) + elif ndim == 0: + return () + else: + ret_code = grunstable_ratequery_prop( + ptr, rate_id, GRUNSTABLE_QPROP_SHAPE, &buf[0] + ) + if ret_code != GR_SUCCESS: + raise RuntimeError( + f"shape query fail after ndim query success, for {rate_id=}" + ) + return tuple(int(buf[i]) for i in range(ndim)) + elif (prop_kind == GRUNSTABLE_QPROP_NDIM or + prop_kind == GRUNSTABLE_QPROP_MAXITEMSIZE): + buf[0] = rateq_raw_nonshape_prop(ptr, rate_id, prop_kind) + #if sizeof(Py_ssize_t) > sizeof(long long): + # if + return int(buf[0]) + elif prop_kind == GRUNSTABLE_QPROP_WRITABLE: + buf[0] = rateq_raw_nonshape_prop(ptr, rate_id, prop_kind) + return bool(buf[0]) + elif prop_kind == GRUNSTABLE_QPROP_DTYPE: + buf[0] = rateq_raw_nonshape_prop(ptr, rate_id, prop_kind) + if buf[0] == GRUNSTABLE_TYPE_F64: + return float + elif buf[0] == GRUNSTABLE_TYPE_STR: + return str + else: + # did we forget to update this function after adding a dtype? + raise RuntimeError(f"{rate_id=} has an unknown dtype") + else: + # did we forget to update this function after adding a new prop_kind? + raise ValueError(f"recieved an unknown prop_kind: {prop_kind}") + +cdef list rateq_load_string( + c_chemistry_data_storage *ptr, + grunstable_rateid_type rate_id, + Py_ssize_t n_items, + object key +): + """ + Load the single string or list of strings associated with rate_id + + Notes + ----- + strings are very much a special case (compared to say floating point + values or integer values) + """ + # reminder: when Cython casts python's built-in (arbitrary precision) int type to a + # C type will check for overflows during the cast + # https://cython.readthedocs.io/en/3.0.x/src/quickstart/cythonize.html + + # query max number of bytes per string (includes space for trailing '\0') + cdef Py_ssize_t bytes_per_string + try: + bytes_per_string= ( + rateq_get_prop(ptr, rate_id, GRUNSTABLE_QPROP_MAXITEMSIZE) ) + except OverflowError: + raise RuntimeError(f"bytes per string is suspiciously big ({key=})") from None + + # let's confirm that the allocation size doesn't exceed the max value of Py_ssize_t + # -> technically, the max allocation size shouldn't exceed SIZE_MAX from C's + # header (i.e. the max value of size_t) + # -> for simplicity, we require that it doesn't exceed the max value of Py_ssize_t + # (by definition, the max value represented by Py_ssize_t uses 1 less bit of + # storage than SIZE_MAX) + cdef Py_ssize_t max_alloc_size = sys.maxsize # sys.maxsize is max val of Py_ssize_t + if (max_alloc_size / bytes_per_string) < n_items: + raise RuntimeError(f"{key=} requires too much memory to load") + + # allocate memory + cdef char* str_storage = malloc(bytes_per_string * n_items) + cdef char** str_array = malloc(sizeof(char*) * n_items) + cdef Py_ssize_t i + cdef list out = [] + try: + for i in range(n_items): + str_array[i] = &str_storage[i*bytes_per_string] + + if (grunstable_ratequery_get_str(ptr, rate_id, str_array) + != GR_SUCCESS): + raise RuntimeError(f"Error retrieving data for the \"{key}\" key") + + for i in range(n_items): + out.append((str_array[i]).decode("ASCII")) + finally: + free(str_array) + free(str_storage) + + return out + + +cdef object rateq_load_f64( + c_chemistry_data_storage *ptr, + grunstable_rateid_type rate_id, + Py_ssize_t n_items, + object key +): + """ + Returns a 1D numpy array of loaded 64-bit floats + + Notes + ----- + If we add more types (other than strings), it would be easier to use cython's + fused-type functionality to generalize this function + """ + out = np.empty(shape=(n_items,), dtype=np.float64) + cdef double[:] memview = out + if grunstable_ratequery_get_f64(ptr, rate_id, &memview[0]) != GR_SUCCESS: + raise RuntimeError(f"Error retrieving data for the \"{key}\" key") + + # we set the WRITABLE flag to False so that people don't mistakenly believe + # that by modifying the array in place that they are updating Grackle's + # internal buffers + out.setflags(write=False) + return out -@functools.lru_cache # (could use functools.cache starting in python3.9) -def _name_rateid_map(): - cdef dict out = {} - cdef const char* rate_name - cdef grunstable_rateid_type rate_id - cdef unsigned long long i = 0 - while True: - rate_name = grunstable_ith_rate(i, &rate_id) - if rate_name is NULL: - return out - out[rate_name.decode('UTF-8')] = int(rate_id) - i+=1 cdef class _rate_mapping_access: # This class is used internally by the chemistry_data extension class to @@ -924,32 +1089,33 @@ cdef class _rate_mapping_access: # we might make some different choices) cdef c_chemistry_data_storage *_ptr - cdef object _rate_shape_callback + cdef dict _cached_name_rateid_map def __cinit__(self): self._ptr = NULL - self._rate_shape_callback = None + self._cached_name_rateid_map = None def __init__(self): # Prevent accidental instantiation from normal Python code raise TypeError("This class cannot be instantiated directly.") @staticmethod - cdef _rate_mapping_access from_ptr_and_callback( - c_chemistry_data_storage *ptr, object callback - ): + cdef _rate_mapping_access from_ptr(c_chemistry_data_storage *ptr): + """ + Construct a _rate_mapping_access instance from the provided pointer + """ cdef _rate_mapping_access out = _rate_mapping_access.__new__( _rate_mapping_access ) out._ptr = ptr - out._rate_shape_callback = callback return out - def _access_rate(self, key, val): - # determine whether the rate needs to be updated - update_rate = (val is not _NOSETVAL) + @property + def _name_rateid_map(self): + """returns a mapping between rate names and rate id""" - rate_id = _name_rateid_map()[key] # will raise a KeyError if not known + if self._cached_name_rateid_map is not None: + return self._cached_name_rateid_map if self._ptr is NULL: raise RuntimeError( @@ -957,43 +1123,145 @@ cdef class _rate_mapping_access: "access retrieve data from" ) - # retrieve the pointer - cdef double* rate_ptr = grunstable_ratequery_get_ptr(self._ptr, rate_id) + cdef dict out = {} + cdef const char* rate_name + cdef grunstable_rateid_type rate_id + cdef unsigned long long i = 0 + while True: + rate_name = grunstable_ith_rate(self._ptr, i, &rate_id) + if rate_name is NULL: + self._cached_name_rateid_map = out + return out + out[rate_name.decode('UTF-8')] = int(rate_id) + i+=1 - # lookup the shape of the rates - callback = self._rate_shape_callback - shape = callback(rate_name=key) + def _try_get_shape_type_pair( + self, rate_id: int + ) -> tuple[tuple[int, ...], type[float] | type[str]]: + """ + try to query the shape associated with rate_id + """ + cdef grunstable_rateid_type casted_id = (rate_id) + try: + shape = rateq_get_prop(self._ptr, casted_id, GRUNSTABLE_QPROP_SHAPE) + dtype = rateq_get_prop(self._ptr, casted_id, GRUNSTABLE_QPROP_DTYPE) + return (shape, dtype) + except RatequeryFailException: + return None - # predeclare a memoryview to use with 1d arrays - cdef double[:] memview + def _get_rate(self, key: str): + """ + Returns a numpy array that holds a copy of grackle's internal data + associated with `key` + + Parameters + ---------- + key: str + The name of the quantity to access + + Returns + ------- + np.ndarray or float or list of strings or string + The retrieved value + """ - if shape == (): # handle the scalar case! - if update_rate: - rate_ptr[0] = val # cast performs type check - return float(rate_ptr[0]) - elif len(shape) == 1: - if update_rate: - raise RuntimeError( - f"You cannot assign a value to the {key!r} rate.\n\n" - "If you are looking to modify the rate's values, you " - f"should retrieve the {key!r} rate's value (a numpy " - "array), and modify the values in place" - ) - size = shape[0] - memview = (rate_ptr) - return np.asarray(memview) + # lookup the rate_id and shape of the rates + rate_id = self._name_rateid_map[key] + shape, dtype = self._try_get_shape_type_pair(rate_id) + + ndim = len(shape) + nelements = 1 if shape == () else np.prod(shape) + + if dtype == str: + if ndim > 1: + raise RuntimeError("multi-dimensional arrays of strings aren't alowed") + out = rateq_load_string(self._ptr, rate_id, nelements, key) + elif dtype == float: + out = rateq_load_f64(self._ptr, rate_id, nelements, key) else: - raise RuntimeError( - "no support is in place for higher dimensional arrays" + raise RuntimeError(f"no support (yet?) for rates of {dtype} values") + + # declare a few memoryview types + if shape == (): + return dtype(out[0]) + elif ndim != 1: + return _portable_reshape(out, shape=shape) + return out + + def _set_rate(self, key, val): + """ + Copies value(s) into the Grackle solver's internal buffer associated + with `key` + + Parameters + ---------- + key: str + The name of the quantity to modify + val: array_like + The values to be written + """ + # lookup the rate_id and shape of the rates + rate_id = self._name_rateid_map[key] + shape, dtype = self._try_get_shape_type_pair(rate_id) + + if dtype == str: + raise ValueError( + f"Grackle doesn't support assignment to keys (like \"{key}\") that are" + "associated with string values" ) + elif dtype != float: + raise RuntimeError(f"no support (yet?) for rates of {dtype} values") + + # validate that val meets expectations and then coerce to `buf` a 1D numpy + # array that we can feed to the C function + if np.shape(val) != shape: + if shape == (): + raise ValueError(f"The \"{key}\" key expects a scalar") + raise ValueError(f"The \"{key}\" expects a value with shape, {shape}") + elif shape == (): + coerced = float(val) + buf = np.array([coerced]) + else: + coerced = np.asanyarray(val, dtype=np.float64, order="C") + if hasattr(coerced, "unit") or hasattr(coerced, "units"): + # val is probably an astropy.Quantity or unyt.unyt_array instance + raise ValueError( + f"'{type(self).__name__}' can't handle numpy.ndarray subclasses " + "that attach unit information" + ) + buf = _portable_reshape(coerced, shape = (coerced.size,)) + + # create a memoryview of out (so we can access the underlying pointer) + cdef const double[:] memview = buf + + if grunstable_ratequery_set_f64(self._ptr, rate_id, &memview[0]) != GR_SUCCESS: + writable = rateq_get_prop(self._ptr, rate_id, GRUNSTABLE_QPROP_WRITABLE) + if writable: + raise RuntimeError( + f"Error writing to data associated with the \"{key}\" key" + ) + else: + raise ValueError( + f"the values associated with the \"{key}\" key can't be overwritten" + ) + + def get(self, key, default=None, /): + """ + Retrieve the value associated with key, if key is known. Otherwise, + return the default. + """ + try: + return self._get_rate(key) + except KeyError: + return default - def __getitem__(self, key): return self._access_rate(key, _NOSETVAL) + def __getitem__(self, key): return self._get_rate(key) - def __setitem__(self, key, value): self._access_rate(key, value) + def __setitem__(self, key, value): self._set_rate(key, value) - def __iter__(self): return iter(_name_rateid_map()) + def __iter__(self): return iter(self._name_rateid_map) - def __len__(self): return len(_name_rateid_map()) + def __len__(self): return len(self._name_rateid_map) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0af865f10..2320c1c58 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,3 +1,4 @@ +add_subdirectory(grtestutils) add_subdirectory(unit) # down below, we add tests for various code-examples diff --git a/tests/grtestutils/CMakeLists.txt b/tests/grtestutils/CMakeLists.txt new file mode 100644 index 000000000..42bb65246 --- /dev/null +++ b/tests/grtestutils/CMakeLists.txt @@ -0,0 +1,142 @@ +# Purpose: define "libraries" to aid with the testing of Grackle +# ============================================================== + +# first, define an interface "library" (i.e. nothing is compiled) for +# book-keeping purposes. dependents get "linked" against this library in order +# to be able to access grackle's internal functionality + +add_library(_grackle_internals INTERFACE) +target_link_libraries(_grackle_internals INTERFACE Grackle::Grackle) + +# short-term macro definitions (both will become unnecessary in the near future) +target_compile_definitions(_grackle_internals + # currently required for functions declared by fortran_func_wrappers.hpp + INTERFACE "$<$:LINUX>" + # suppresses warnings about C internal headers using our deprecated public + # headers (the files should include grackle.h instead). We're currently + # holding off on properly fixing this to minimize conflicts with pending PRs + # on the newchem-cpp branch + INTERFACE GRIMPL_PUBLIC_INCLUDE=1 +) + +# this makes it possible to write an include-directive of a header from +# src/clib, by writing something like +# #include "{name}.hpp" +# where {name} is replaced by the name of the header +# -> frankly, I don't love this because it's not immediately clear that we are +# including a private header. The alternative is to require the paths +# in the include directives to include the name of the directory holding +# the implementation files (e.g. "clib/{header}.hpp") +target_include_directories(_grackle_internals + INTERFACE ${PROJECT_SOURCE_DIR}/src/clib +) + +# grtestutils: testing utility library +# ==================================== +# - this is an internal library that defines reusable testing utilities +# - Frankly, I don't love the name. Originally I called it grtest, but I'm a +# a little concerned that may look a little too much like gtest (i.e. the +# shorthand that googletest uses). + +# Code Organization +# ----------------- +# This code in this directory is organized in a slightly peculiar manner +# - code in the main directory should not depend upon googletest +# - the googletest subdirectory is intended to define custom googletest +# assertions and fixtures. This subdirectory should **ONLY** include +# headers (so that, at most, this internal library has a transitive +# dependence on googletest) +# +# RATIONALE: +# - over time, the idea has been floated to introducing C++ logic to +# accelerate certain calculations provided by the python wrapper: +# - PR #343 proposed creating a C++ accelerated harness for performing +# certain kinds of semi-analytic calculations (like freefall or evolving +# constant density) +# - PR #370 proposed introducing a function to infer the internal energy at +# a desired temperature (yes, the PR proposes it as a python function, but +# it would be faster as a C++ function). +# - Relatedly, it would also be useful to be able to directly compute +# temperature or chemical equilibrium +# - in each case this functionality would also be extremely useful for the +# googletest framework or for writing simple C++-only benchmarking programs +# - this is extremely useful if you want to use tools like valgrind or +# compiler sanitizers. While it's generally possible to use these tools in +# a python extension module, it may require compiling CPython itself from +# source. Moreover it can be extremely slow (these tools introduce to +# basic C/C++ operations and that overhead will be apply to the Python +# interpretter) +# - this is important if we want to test grackle when compiled with various +# backends. Historically, the python bindings have never been able to use +# Grackle with the OpenMP backend. While we can and will address this, +# there will additional challenges as we introduce GPU acceleration +# - this library is intended as a location where that kind of hypothetical +# functionality can be introduced. The organization strategy ensures that +# it will be straightforward to provide this hypothetical functionality to +# the python bindings (and any C++ testing code) without making the python +# bindings link against googletest. + +add_library(grtestutils + # files outside of the googletest subdirectory + # -> these shouldn't include headers from the googletest subdirectory + cmd.hpp cmd.cpp + iterator_adaptor.hpp + os.hpp os.cpp + preset.hpp preset.cpp + utils.hpp utils.cpp + + # files in the googletest subdirectory (reminder: should just be headers!) + googletest/assertions.hpp + googletest/check_allclose.hpp + googletest/fixtures.hpp +) + +target_link_libraries(grtestutils + PRIVATE _grackle_internals + PUBLIC Grackle::Grackle + # to express the transitive dependency on googletest (it's transitive since + # its only referenced by header files), we should theoretically write + # INTERFACE GTest::GTest + # we choose not to do this since all test code pulls in this dependency by + # explicitly listing `testdeps` as a dependency (defined below) +) + +target_compile_features(grtestutils PUBLIC cxx_std_17) + +# configure the grtestutils target so that when a file outside of this +# directory includes a header from within this directory, the include +# directive looks something like: +# #include "grtestutils/.hpp" +# OR +# #include "grtestutils/googletest/.hpp +target_include_directories(grtestutils INTERFACE ${PROJECT_SOURCE_DIR}/tests) + +# these compile-definitions act as short-term hacks +target_compile_definitions(grtestutils + # this hack helps us get the path to the directory holding data files (we can + # remove it once we introduce automatic file management in PR 235, PR 237, + # and PR 246) + PRIVATE GR_DATADIR=${CMAKE_CURRENT_SOURCE_DIR}/../../grackle_data_files/input/ + + # this hack lets us use Operating-system specific functionality (Once PR #237 + # is merged, we should make use of the machinery introduced by that PR for + # enabling/disabling os-specific features) + PRIVATE "$<$:PLATFORM_GENERIC_UNIX>" +) + + +# 2. testdeps: bundles dependencies used by tests +# =============================================== +# - this is an interface library (i.e. nothing is compiled) that ONLY exists +# to aggregate dependencies, include directories and compile definitions +# - all tests should depend on this target + +add_library(testdeps INTERFACE) +target_link_libraries(testdeps + INTERFACE Grackle::Grackle grtestutils _grackle_internals + GTest::gtest_main GTest::gmock +) +# indicates that all consumers are written in C++17 or newer +target_compile_features(testdeps INTERFACE cxx_std_17) + + diff --git a/tests/grtestutils/README.md b/tests/grtestutils/README.md new file mode 100644 index 000000000..d7d59171f --- /dev/null +++ b/tests/grtestutils/README.md @@ -0,0 +1,3 @@ +Defines a simple testing utility library. + +See the CMakeLists.txt for a discussion of code organization. diff --git a/tests/unit/grtest_cmd.cpp b/tests/grtestutils/cmd.cpp similarity index 71% rename from tests/unit/grtest_cmd.cpp rename to tests/grtestutils/cmd.cpp index 131fa158b..36128da0b 100644 --- a/tests/unit/grtest_cmd.cpp +++ b/tests/grtestutils/cmd.cpp @@ -1,18 +1,13 @@ -#include "grtest_cmd.hpp" +#include "cmd.hpp" + +// internal Grackle routine: +#include "status_reporting.h" // GR_INTERNAL_ERROR, GR_INTERNAL_REQUIRE #include // FILE, fgets, fprintf, stderr, (popen/pclose on POSIX) -#include // std::exit #include // std::stringstream #include -// TODO: replace with Grackle's existing err machinery -[[noreturn]] static void err_(std::string msg="") { - const char* ptr = (msg.empty()) ? "" : msg.c_str(); - fprintf(stderr, "ERROR: %s\n", ptr); - std::exit(1); -} - #ifdef PLATFORM_GENERIC_UNIX #define TEMP_BUF_SIZE 128 @@ -29,14 +24,14 @@ grtest::ProcessStatusAndStdout grtest::capture_status_and_output( // fp represents a buffered pipe to the standard output of the command. FILE* fp = popen(command.data(), "r"); - if (fp == nullptr) { err_("there was a problem launching the command"); } + GR_INTERNAL_REQUIRE(fp != nullptr, "there's a problem launching the command"); // if our reads from the pipe outpace the rate at which the command writes to // the pipe, fgets won't return until more data becomes available. If the // processess ends, fgets encounters EOF (causing fgets to return) while (fgets(temp_buf, TEMP_BUF_SIZE, fp) != nullptr) { buf << temp_buf; } int status = pclose(fp); - if (status == -1) { err_("there was a problem closing the command"); } + GR_INTERNAL_REQUIRE(status != -1, "there was a problem closing the command"); return {status, buf.str()}; } @@ -45,7 +40,7 @@ grtest::ProcessStatusAndStdout grtest::capture_status_and_output( grtest::ProcessStatusAndStdout grtest::capture_status_and_output( const std::string& command ) { - err_("not implemented on this platform"); + GR_INTERNAL_ERROR("not implemented on this platform"); } #endif /* PLATFORM_GENERIC_UNIX */ diff --git a/tests/unit/grtest_cmd.hpp b/tests/grtestutils/cmd.hpp similarity index 100% rename from tests/unit/grtest_cmd.hpp rename to tests/grtestutils/cmd.hpp diff --git a/tests/grtestutils/googletest/assertions.hpp b/tests/grtestutils/googletest/assertions.hpp new file mode 100644 index 000000000..14177d7d8 --- /dev/null +++ b/tests/grtestutils/googletest/assertions.hpp @@ -0,0 +1,70 @@ +//===----------------------------------------------------------------------===// +// +// See the LICENSE file for license and copyright information +// SPDX-License-Identifier: NCSA AND BSD-3-Clause +// +//===----------------------------------------------------------------------===// +/// +/// @file +/// Declares some general-purpose assertions for testing grackle +/// +//===----------------------------------------------------------------------===// +#ifndef GRTEST_GOOGLETEST_ASSERTIONS_HPP +#define GRTEST_GOOGLETEST_ASSERTIONS_HPP + +#include + +#include +#include "grackle.h" + +namespace grtest { + +inline testing::AssertionResult IsGRSUCCESS(const char* expr1, int val1) { + if (val1 == GR_SUCCESS) { + return testing::AssertionSuccess(); + } + + std::string descr; + switch (val1) { + case GR_SUCCESS: { + descr = "GR_SUCCESS"; + break; + } + case GR_FAIL: { + descr = "GR_FAIL"; + break; + } + default: { + descr = "not a standard code"; + } + } + testing::AssertionResult out = testing::AssertionFailure(); + out << "Evaluated: " << expr1 << '\n' + << " Expected: GR_SUCCESS (aka " << GR_SUCCESS << ")\n" + << " Actual: " << val1 << " (" << descr << ')'; + return out; +} + +inline testing::AssertionResult IsGRError(const char* expr1, int val1) { + if (val1 != GR_SUCCESS) { + return testing::AssertionSuccess(); + } + + testing::AssertionResult out = testing::AssertionFailure(); + out << "Evaluated: " << expr1 << '\n' + << " Expected: a value other than GR_SUCCESS\n" + << " Actual: " << val1 << " (GR_SUCCESS)"; + return out; +} + +} // namespace grtest + +#define EXPECT_GR_SUCCESS(expr) EXPECT_PRED_FORMAT1(::grtest::IsGRSUCCESS, expr) + +#define ASSERT_GR_SUCCESS(expr) ASSERT_PRED_FORMAT1(::grtest::IsGRSUCCESS, expr) + +#define EXPECT_GR_ERR(expr) EXPECT_PRED_FORMAT1(::grtest::IsGRError, expr) + +#define ASSERT_GR_ERR(expr) ASSERT_PRED_FORMAT1(::grtest::IsGRError, expr) + +#endif // GRTEST_GOOGLETEST_ASSERTIONS_HPP diff --git a/tests/unit/utest_helpers.hpp b/tests/grtestutils/googletest/check_allclose.hpp similarity index 100% rename from tests/unit/utest_helpers.hpp rename to tests/grtestutils/googletest/check_allclose.hpp diff --git a/tests/grtestutils/googletest/fixtures.hpp b/tests/grtestutils/googletest/fixtures.hpp new file mode 100644 index 000000000..25c53bb22 --- /dev/null +++ b/tests/grtestutils/googletest/fixtures.hpp @@ -0,0 +1,94 @@ +//===----------------------------------------------------------------------===// +// +// See the LICENSE file for license and copyright information +// SPDX-License-Identifier: NCSA AND BSD-3-Clause +// +//===----------------------------------------------------------------------===// +/// +/// @file +/// Define machinery for creating GoogleTest Fixutures to help test Grackle's +/// C API. +/// +//===----------------------------------------------------------------------===// + +#ifndef GRTEST_FIXTURE +#define GRTEST_FIXTURE + +#include +// because we include gtest.h here, we should NOT include this file in any +// grtest source files (in other words, this should be a header-only file) + +#include "../preset.hpp" + +namespace grtest { + +/// test-fixture that can used to run one or more tests with a chemistry data +/// configuration initialized from a given chemistry presets. +/// +/// This sets up a GrackleCtxPack (where the contents are configured with +/// appropriate presets) and deallocates that memory at the end of the test. +/// +/// How To Use +/// ========== +/// To make use of this fixture in a test-suite called `MyFeatureTest`, you +/// need to either: +/// 1. make a type alias (via `using` or `typedef`), named `MyFeatureTest`, of +/// the relevant instantiation of this class template, OR +/// 2. make a subclass, named `MyFeatureTest`, of the relevant instantiation of +/// this class template +template +class ConfigPresetFixture : public testing::Test { +protected: + void SetUp() override { + // called immediately after the constructor (but before the test-case) + + grtest::InitStatus status; + pack = GrackleCtxPack::create(FullConfPreset{chem_preset, unit_preset}, + &status); + if (!pack.is_initialized()) { + if (status == InitStatus::datafile_notfound) { + GTEST_SKIP() << "something went wrong with finding the data file"; + } else { + FAIL() << "Error in initialize_chemistry_data."; + } + } + } + + GrackleCtxPack pack; +}; + +/// defines a parameterized test-fixture that can be used to run a parametrized +/// set of tests that are initialized with different chemistry data presets. +/// +/// This sets up a GrackleCtxPack (where the contents are configured with +/// appropriate presets) and deallocates that memory at the end of the test. +/// +/// How To Use +/// ========== +/// To make use of this fixture in a test-suite called `MyFeatureTest`, you +/// need to either: +/// 1. make a type alias (via `using` or `typedef`), named `MyFeatureTest`, of +/// this class, OR +/// 2. make a subclass, named `MyFeatureTest`, of this class +class ParametrizedConfigPresetFixture + : public testing::TestWithParam { +protected: + void SetUp() override { + // called immediately after the constructor (but before the test-case) + grtest::InitStatus status; + pack = GrackleCtxPack::create(GetParam(), &status); + if (!pack.is_initialized()) { + if (status == InitStatus::datafile_notfound) { + GTEST_SKIP() << "something went wrong with finding the data file"; + } else { + FAIL() << "Error in initialize_chemistry_data."; + } + } + } + + GrackleCtxPack pack; +}; + +} // namespace grtest + +#endif // GRTEST_FIXTURE diff --git a/tests/grtestutils/iterator_adaptor.hpp b/tests/grtestutils/iterator_adaptor.hpp new file mode 100644 index 000000000..71bd745a2 --- /dev/null +++ b/tests/grtestutils/iterator_adaptor.hpp @@ -0,0 +1,138 @@ +//===----------------------------------------------------------------------===// +// +// See the LICENSE file for license and copyright information +// SPDX-License-Identifier: NCSA AND BSD-3-Clause +// +//===----------------------------------------------------------------------===// +/// +/// @file +/// Declare and implement the IteratorAdaptor +/// +//===----------------------------------------------------------------------===// +#ifndef GRTESTUTILS_ITERATOR_ADAPTOR_HPP +#define GRTESTUTILS_ITERATOR_ADAPTOR_HPP + +#include +#include +#include + +#include "grackle.h" +#include "preset.hpp" + +namespace grtest { + +/// the standard value-type that an IteratorAdaptor instantiation refers to +struct NameIdPair { + std::string name; + long long id; +}; + +/// teach std::ostream how to format NameIdPair +/// +/// The motivation is to make it easier write detailed error messages +inline std::ostream& operator<<(std::ostream& os, const NameIdPair& pair) { + os << "{name=\"" << pair.name << "\", id=" << pair.id << "}"; + return os; +} + +/// implements a C++ style InputIterator by adapting a simple Plugin type +/// that wraps a set of Grackle functions +/// +/// This is useful for making use of C++ standard library algorithms and +/// (arguably more importantly) making use of range-based for-loops +template +class IteratorAdaptor { + unsigned long long counter_; + unsigned long long n_rates_; + Plugin plugin_; + NameIdPair current_pair_; + + /// Updates current_pair_ and returns `*this` + IteratorAdaptor& update_pair_and_ret_(unsigned long long current_count) { + if (current_count < this->n_rates_) { + this->current_pair_ = this->plugin_(current_count); + } + return *this; + } + +public: + using iterator_category = std::input_iterator_tag; + using value_type = NameIdPair; + using difference_type = std::ptrdiff_t; + using pointer = const NameIdPair*; + using reference = const NameIdPair; + + /// construct a new instance + IteratorAdaptor(unsigned long long counter, unsigned long long n_rates, + Plugin plugin) + : counter_(counter), n_rates_(n_rates), plugin_(plugin) { + update_pair_and_ret_(counter); + } + + /// implements the equality operation + bool operator==(const IteratorAdaptor& other) const { + return (counter_ == other.counter_) && (plugin_ == other.plugin_); + } + + /// implements the inequality operation + bool operator!=(const IteratorAdaptor& other) const { + return !(*this == other); + } + + /// implements the dereference operation + reference operator*() const { return current_pair_; } + + /// implements the prefix increment operation + /// + /// This effectively implements `++x`, which increments the value of `x` + /// before determining the returned value. In other words, `++x` returns the + /// value of `x` from **after** after the increment + IteratorAdaptor& operator++() { return update_pair_and_ret_(++counter_); } + + /// implements the prefix increment operation + /// + /// This effectively implements `x++`, which increments the value of `x` + /// after determining the returned value. In other words, `x++` returns the + /// value of `x` from **before** the increment + IteratorAdaptor operator++(int) { + IteratorAdaptor ret = *this; + ++(*this); + return ret; + } +}; + +// Now lets use this machinery to implement logic iterating over the names +// accessible through the ratequery api + +struct RateQueryPlugin { + chemistry_data_storage* my_rates; + + NameIdPair operator()(unsigned long long i) const { + grunstable_rateid_type tmp; + const char* name = grunstable_ith_rate(my_rates, i, &tmp); + return NameIdPair{name, tmp}; + } + + bool operator==(const RateQueryPlugin& other) const { + return my_rates == other.my_rates; + } +}; + +/// used for creating the iterator and within range-based for-loops +class RateQueryRange { + RateQueryPlugin plugin_; + using iterator = IteratorAdaptor; + long long n_rates_; + +public: + explicit RateQueryRange(grtest::GrackleCtxPack& pack) + : plugin_(RateQueryPlugin{pack.my_rates()}), + n_rates_(grunstable_ratequery_nrates(pack.my_rates())) {} + + iterator begin() { return iterator(0, n_rates_, plugin_); } + iterator end() { return iterator(n_rates_, n_rates_, plugin_); } +}; + +} // namespace grtest + +#endif // GRTESTUTILS_ITERATOR_ADAPTOR_HPP diff --git a/tests/unit/grtest_os.cpp b/tests/grtestutils/os.cpp similarity index 99% rename from tests/unit/grtest_os.cpp rename to tests/grtestutils/os.cpp index eba65b404..bbcd4538e 100644 --- a/tests/unit/grtest_os.cpp +++ b/tests/grtestutils/os.cpp @@ -1,4 +1,4 @@ -#include "grtest_os.hpp" +#include "os.hpp" // the following 2 headers are the c versions of the headers (rather than the // C++ versions) since it seems more likely that posix-specific functions are diff --git a/tests/unit/grtest_os.hpp b/tests/grtestutils/os.hpp similarity index 100% rename from tests/unit/grtest_os.hpp rename to tests/grtestutils/os.hpp diff --git a/tests/grtestutils/preset.cpp b/tests/grtestutils/preset.cpp new file mode 100644 index 000000000..53f3a64e3 --- /dev/null +++ b/tests/grtestutils/preset.cpp @@ -0,0 +1,117 @@ +//===----------------------------------------------------------------------===// +// +// See the LICENSE file for license and copyright information +// SPDX-License-Identifier: NCSA AND BSD-3-Clause +// +//===----------------------------------------------------------------------===// +/// +/// @file +/// implement logic pertaining to pre-defined configuration presets +/// +//===----------------------------------------------------------------------===// + +#include "./preset.hpp" +#include "./utils.hpp" + +#include "grackle.h" +#include "status_reporting.h" // GR_INTERNAL_UNREACHABLE_ERROR + +static std::string to_string(const grtest::ChemPreset& preset) { + switch (preset) { + case grtest::ChemPreset::primchem0: + return "pc=0"; + case grtest::ChemPreset::primchem1: + return "pc=1"; + case grtest::ChemPreset::primchem2: + return "pc=2"; + case grtest::ChemPreset::primchem3: + return "pc=3"; + case grtest::ChemPreset::primchem4_dustspecies3: + return "pc=3-dust_species=4"; + } + + GR_INTERNAL_UNREACHABLE_ERROR(); +} + +grtest::InitStatus grtest::setup_chemistry_data_from_preset( + chemistry_data* my_chem, ChemPreset preset) { + if (local_initialize_chemistry_parameters(my_chem) != GR_SUCCESS) { + return InitStatus::generic_fail; + } + + if (!grtest::set_standard_datafile(*my_chem, "CloudyData_UVB=HM2012.h5")) { + return InitStatus::datafile_notfound; + } + + my_chem->use_grackle = 1; // chemistry on + my_chem->use_isrf_field = 1; + my_chem->with_radiative_cooling = 1; // cooling on + my_chem->metal_cooling = 1; // metal cooling on + my_chem->UVbackground = 1; // UV background on + + switch (preset) { + case ChemPreset::primchem0: { + my_chem->primordial_chemistry = 0; + my_chem->dust_chemistry = 0; + return InitStatus::success; + } + case ChemPreset::primchem1: { + my_chem->primordial_chemistry = 1; + my_chem->dust_chemistry = 1; + return InitStatus::success; + } + case ChemPreset::primchem2: { + my_chem->primordial_chemistry = 2; + my_chem->dust_chemistry = 1; + return InitStatus::success; + } + case ChemPreset::primchem3: { + my_chem->primordial_chemistry = 3; + my_chem->dust_chemistry = 1; + return InitStatus::success; + } + case ChemPreset::primchem4_dustspecies3: { + my_chem->primordial_chemistry = 4; + my_chem->dust_chemistry = 1; + my_chem->metal_chemistry = 1; + my_chem->dust_species = 1; + my_chem->use_dust_density_field = 1; + return InitStatus::success; + } + } + GR_INTERNAL_UNREACHABLE_ERROR(); +} + +static std::string to_string(const grtest::InitialUnitPreset& preset) { + switch (preset) { + case grtest::InitialUnitPreset::simple_z0: + return "simpleUnit-z=0"; + } + GR_INTERNAL_UNREACHABLE_ERROR(); +} + +code_units grtest::setup_initial_unit(grtest::InitialUnitPreset preset) { + // since we return in the switch statement, the compiler should always warn + // us if we're missing an enumeration + switch (preset) { + case InitialUnitPreset::simple_z0: { + double initial_redshift = 0.; + code_units my_units; + my_units.comoving_coordinates = 0; // 1 if cosmological sim, 0 if not + my_units.density_units = 1.67e-24; + my_units.length_units = 1.0; + my_units.time_units = 1.0e12; + my_units.a_units = 1.0; // units for the expansion factor + // Set expansion factor to 1 for non-cosmological simulation. + my_units.a_value = 1. / (1. + initial_redshift) / my_units.a_units; + set_velocity_units(&my_units); + return my_units; + } + } + GR_INTERNAL_UNREACHABLE_ERROR(); +} + +void grtest::PrintTo(const grtest::FullConfPreset& preset, std::ostream* os) { + *os << "Preset{" << to_string(preset.chemistry) << ',' + << to_string(preset.unit) << '}'; +} diff --git a/tests/grtestutils/preset.hpp b/tests/grtestutils/preset.hpp new file mode 100644 index 000000000..738a79dad --- /dev/null +++ b/tests/grtestutils/preset.hpp @@ -0,0 +1,156 @@ +//===----------------------------------------------------------------------===// +// +// See the LICENSE file for license and copyright information +// SPDX-License-Identifier: NCSA AND BSD-3-Clause +// +//===----------------------------------------------------------------------===// +/// +/// @file +/// declare some standard pre-defined configuration presets +/// +//===----------------------------------------------------------------------===// +#ifndef GRTESTUTILS_PRESET_HPP +#define GRTESTUTILS_PRESET_HPP + +#include "grackle.h" +#include +#include +#include + +namespace grtest { + +/// this only exists so that we can determine the reason that a test fails +enum class InitStatus { + success, + generic_fail, + datafile_notfound, +}; + +/// represents different presets for initializing chemistry_data +/// +/// @note +/// In the future, we probably want to add more +enum class ChemPreset { + primchem0, + primchem1, + primchem2, + primchem3, + primchem4_dustspecies3, +}; + +/// override the settings of my_chem based on the specified preset +InitStatus setup_chemistry_data_from_preset(chemistry_data* my_chem, + ChemPreset preset); + +/// Preset for constructing the code_unit instance used for initializing the +/// Grackle Solver +/// +/// @note +/// In the future, we probably want to add more +enum class InitialUnitPreset { + simple_z0, // <- no cosmology, z=0 +}; + +/// return a code_unit instance initialized based on the specified preset +code_units setup_initial_unit(InitialUnitPreset preset); + +/// Represents the preset for creating a GrackleCtxPack +struct FullConfPreset { + ChemPreset chemistry; + InitialUnitPreset unit; +}; + +// teach googletest how to print FullConfPreset +void PrintTo(const FullConfPreset& preset, std::ostream* os); + +/// Tracks the group of Grackle objects needed for executing API functions +/// +/// The primary motivation for this object's existence is making sure that +/// the allocations get cleaned up when a test fails +/// +/// @note +/// Ideally, we would only make it possible to create a fully initialized +/// instance (in that case, we would delete the default constructor), but that +/// involves a bunch more work +/// +/// We choose to implement this in terms of std::unique_ptr (rather than raw +/// pointers) since it implements proper move semantics for us +class GrackleCtxPack { + /// units used for initializing chemistry_data + code_units initial_units_; + /// the fully initialized chemistry_data instance + std::unique_ptr my_chemistry_; + /// the fully initialized chemistry_data_storage_instance + std::unique_ptr my_rates_; + +public: + /// Construct an uninitialized instance + GrackleCtxPack() : my_chemistry_(nullptr), my_rates_(nullptr) {} + + GrackleCtxPack(GrackleCtxPack&&) = default; + GrackleCtxPack& operator=(GrackleCtxPack&&) = default; + + // forbid copy and assignment operations... + // -> we could re-enable them if we wanted to be able add to clone the + // types (or if we wanted to internally use std::shared_ptr + GrackleCtxPack(const GrackleCtxPack&) = delete; + GrackleCtxPack& operator=(const GrackleCtxPack&) = delete; + + ~GrackleCtxPack() { + if (!this->is_initialized()) { + return; + } + local_free_chemistry_data(this->my_chemistry_.get(), this->my_rates_.get()); + // unique_ptr destructor will handle calls to delete + } + + bool is_initialized() const { return this->my_chemistry_ != nullptr; } + + // getter functions + const code_units& initial_units() const { return this->initial_units_; } + + chemistry_data* my_chemistry() { return this->my_chemistry_.get(); } + const chemistry_data* my_chemistry() const { + return this->my_chemistry_.get(); + } + + chemistry_data_storage* my_rates() { return this->my_rates_.get(); } + const chemistry_data_storage* my_rates() const { + return this->my_rates_.get(); + } + + /// create an initialized instance from a preset + static GrackleCtxPack create(const FullConfPreset& preset, + InitStatus* status) { + std::unique_ptr my_chemistry(new chemistry_data); + InitStatus tmp = + setup_chemistry_data_from_preset(my_chemistry.get(), preset.chemistry); + if (tmp != InitStatus::success) { + if (status != nullptr) { + *status = tmp; + } + return GrackleCtxPack(); // return an unitialized instance + } + + code_units initial_unit = setup_initial_unit(preset.unit); + std::unique_ptr my_rates( + new chemistry_data_storage); + if (local_initialize_chemistry_data(my_chemistry.get(), my_rates.get(), + &initial_unit) != GR_SUCCESS) { + if (status != nullptr) { + *status = InitStatus::generic_fail; + } + return GrackleCtxPack(); // return an unitialized instance + } + + GrackleCtxPack out; + out.initial_units_ = initial_unit; + out.my_chemistry_ = std::move(my_chemistry); + out.my_rates_ = std::move(my_rates); + return out; + } +}; + +} // namespace grtest + +#endif // GRTESTUTILS_PRESET_HPP diff --git a/tests/unit/grtest_utils.cpp b/tests/grtestutils/utils.cpp similarity index 98% rename from tests/unit/grtest_utils.cpp rename to tests/grtestutils/utils.cpp index 85557d7af..d4781249f 100644 --- a/tests/unit/grtest_utils.cpp +++ b/tests/grtestutils/utils.cpp @@ -1,4 +1,4 @@ -#include "grtest_utils.hpp" +#include "utils.hpp" #include diff --git a/tests/unit/grtest_utils.hpp b/tests/grtestutils/utils.hpp similarity index 100% rename from tests/unit/grtest_utils.hpp rename to tests/grtestutils/utils.hpp diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index f1018e3ca..915a28c93 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -1,56 +1,7 @@ -# declare testdeps to bundle together dependencies used by all tests -# ------------------------------------------------------------------ -add_library(testdeps INTERFACE) -target_link_libraries(testdeps INTERFACE Grackle::Grackle GTest::gtest_main) +# NOTE: the testdeps target is defined within ../grtestutils/CMakeLists.txt -# compiler defs are short-term hacks to help tests invoke internal functions -# from Grackle (both of these will become unnecessary in the near future) -target_compile_definitions(testdeps - # needed to invoke Fortran functions from C - INTERFACE "$<$:LINUX>" - # suppresses warnings about C internal headers using our deprecated public - # headers (the files should include grackle.h instead). We're currently - # holding off on properly fixing this to minimize conflicts with pending PRs - # on the newchem-cpp branch - INTERFACE GRIMPL_PUBLIC_INCLUDE=1 -) - -# needed to let tests call internal functions from C -target_include_directories(testdeps INTERFACE ${PROJECT_SOURCE_DIR}/src/clib) -target_compile_features(testdeps INTERFACE cxx_std_17) - -# declare the grtest utility library -# ---------------------------------- -# -> this is an internal library that defines reusable testing utilities -# -> if we add more files to this library, we should consider relocating the -# library to a different directory - -add_library(grtest_utils - grtest_cmd.hpp grtest_cmd.cpp - grtest_utils.hpp grtest_utils.cpp - grtest_os.hpp grtest_os.cpp -) -# we are being a little lazy with our usage of testdeps right here -target_link_libraries(grtest_utils PUBLIC testdeps) -target_compile_features(grtest_utils PUBLIC cxx_std_17) - -# these compile-definitions act as short-term hacks -target_compile_definitions(grtest_utils - # this hack helps us get path input-file directory (we can remove it once we - # introduce automatic file management in PR 235, PR 237, and PR 246) - PRIVATE GR_DATADIR=${CMAKE_CURRENT_SOURCE_DIR}/../../grackle_data_files/input/ - - # this hack lets us use Operating-system specific functionality (Once PR #237 - # is merged, we should make use of the machinery introduced by that PR for - # enabling/disabling os-specific features) - PRIVATE "$<$:PLATFORM_GENERIC_UNIX>" -) - -# start declaring targets for tests -# --------------------------------- add_executable(runInterpolationTests test_unit_interpolators_g.cpp) target_link_libraries(runInterpolationTests testdeps) - gtest_discover_tests(runInterpolationTests) add_executable(runLinAlgTests test_linalg.cpp) @@ -62,7 +13,7 @@ target_link_libraries(runGrainSpeciesIntoTests testdeps) gtest_discover_tests(runGrainSpeciesIntoTests) add_executable(runStatusReporting test_status_reporting.cpp) -target_link_libraries(runStatusReporting grtest_utils) +target_link_libraries(runStatusReporting testdeps) gtest_discover_tests(runStatusReporting) # one might argue that the following is more of an integration or end-to-end @@ -71,11 +22,12 @@ add_executable(runVisitorTests test_visitor.cpp) target_link_libraries(runVisitorTests testdeps) gtest_discover_tests(runVisitorTests) -# one might argue that the following is more of an integration or end-to-end -# test than a unit-test -add_executable(runGhostZoneTests test_ghost_zone.cpp) -target_link_libraries(runGhostZoneTests grtest_utils testdeps) -gtest_discover_tests(runGhostZoneTests) +# tests of the API functions +# -> one might argue that these are better classified as integration or +# end-to-end tests than as unit-test +add_executable(runApiTests test_ghost_zone.cpp test_api_ratequery.cpp) +target_link_libraries(runApiTests testdeps) +gtest_discover_tests(runApiTests) # this target tests that the members of the chemistry_data struct can be # accessed through the "dynamic api." The test cases in this target are @@ -84,7 +36,7 @@ gtest_discover_tests(runGhostZoneTests) # problems for "death-tests", so these test-cases should remain separate from # the rest of the gtest framework add_executable(runSyncedChemistryData test_chemistry_struct_synced.cpp) -target_link_libraries(runSyncedChemistryData grtest_utils testdeps) +target_link_libraries(runSyncedChemistryData testdeps) target_compile_definitions(runSyncedChemistryData PRIVATE READER_PATH=${PROJECT_SOURCE_DIR}/tests/scripts/castxml_output_reader.py diff --git a/tests/unit/test_api_ratequery.cpp b/tests/unit/test_api_ratequery.cpp new file mode 100644 index 000000000..3fb69dde6 --- /dev/null +++ b/tests/unit/test_api_ratequery.cpp @@ -0,0 +1,508 @@ +//===----------------------------------------------------------------------===// +// +// See the LICENSE file for license and copyright information +// SPDX-License-Identifier: NCSA AND BSD-3-Clause +// +//===----------------------------------------------------------------------===// +/// +/// @file +/// Define some basic tests of the experimental ratequery API +/// +//===----------------------------------------------------------------------===// + +#include // std::min, std::max +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "grtestutils/iterator_adaptor.hpp" +#include "grtestutils/googletest/assertions.hpp" +#include "grtestutils/googletest/fixtures.hpp" + +#include "grackle.h" +#include "status_reporting.h" + +/// returns the rateid used to denote invalid rate names +grunstable_rateid_type get_invalid_rateid(const grtest::GrackleCtxPack& pack) { + return grunstable_ratequery_id(pack.my_rates(), nullptr); +} + +using SimpleRateQueryTest = + grtest::ConfigPresetFixture; + +TEST_F(SimpleRateQueryTest, InvalidIthRate) { + const char* name = grunstable_ith_rate( + pack.my_rates(), std::numeric_limits::max(), nullptr); + EXPECT_EQ(name, nullptr); +} + +TEST_F(SimpleRateQueryTest, IthRateChecks) { + chemistry_data_storage* my_rates = pack.my_rates(); + unsigned long long n_rates = grunstable_ratequery_nrates(my_rates); + ASSERT_GT(n_rates, 0); // <- sanity check! + EXPECT_NE(nullptr, grunstable_ith_rate(my_rates, n_rates - 1, nullptr)); + EXPECT_EQ(nullptr, grunstable_ith_rate(my_rates, n_rates, nullptr)); + EXPECT_EQ(nullptr, grunstable_ith_rate(my_rates, n_rates + 1, nullptr)); +} + +TEST_F(SimpleRateQueryTest, EmptyNameQuery) { + grunstable_rateid_type rateid = grunstable_ratequery_id(pack.my_rates(), ""); + EXPECT_EQ(rateid, get_invalid_rateid(pack)); +} + +TEST_F(SimpleRateQueryTest, InvalidNameQuery) { + grunstable_rateid_type rateid = + grunstable_ratequery_id(pack.my_rates(), "NotAValidName"); + EXPECT_EQ(rateid, get_invalid_rateid(pack)); +} + +// most of the remaining tests (and future planned tests) involve iterating +// through all rates made available via the ratequery interface. To make the +// tests themselves as easy to read as possible, we implate a C++-style +// iterator to wrap part of the interface + +std::string stringify_prop_kind(enum grunstable_ratequery_prop_kind kind) { + switch (kind) { + case GRUNSTABLE_QPROP_NDIM: + return "GRUNSTABLE_QPROP_NDIM"; + case GRUNSTABLE_QPROP_SHAPE: + return "GRUNSTABLE_QPROP_SHAPE"; + case GRUNSTABLE_QPROP_MAXITEMSIZE: + return "GRUNSTABLE_QPROP_MAXITEMSIZE"; + case GRUNSTABLE_QPROP_WRITABLE: + return "GRUNSTABLE_QPROP_WRITABLE"; + case GRUNSTABLE_QPROP_DTYPE: + return "GRUNSTABLE_QPROP_DTYPE"; + } + GR_INTERNAL_UNREACHABLE_ERROR(); +} + +std::string stringify_type(enum grunstable_types kind) { + switch (kind) { + case GRUNSTABLE_TYPE_F64: + return "GRUNSTABLE_TYPE_F64"; + case GRUNSTABLE_TYPE_STR: + return "GRUNSTABLE_TYPE_STR"; + } + GR_INTERNAL_UNREACHABLE_ERROR(); +} + +std::optional safe_type_enum_cast(long long val) { + if (static_cast(GRUNSTABLE_TYPE_F64) == val) { + return std::make_optional(GRUNSTABLE_TYPE_F64); + } else if (static_cast(GRUNSTABLE_TYPE_STR) == val) { + return std::make_optional(GRUNSTABLE_TYPE_STR); + } else { + return std::nullopt; + } +} + +/// return a vector of some invalid rateids +std::vector invalid_rateids( + grtest::GrackleCtxPack& pack) { + grunstable_rateid_type max_id = + std::numeric_limits::lowest(); + grunstable_rateid_type min_id = + std::numeric_limits::max(); + + // iterate over (parameter-name, rate-id) pairs + for (const grtest::NameIdPair pair : grtest::RateQueryRange(pack)) { + max_id = std::max(max_id, static_cast(pair.id)); + min_id = std::min(min_id, static_cast(pair.id)); + } + + std::vector bad_ids; + if (min_id > std::numeric_limits::lowest()) { + bad_ids.push_back(min_id - 1); + } + if (max_id < std::numeric_limits::max()) { + bad_ids.push_back(max_id + 1); + } + return bad_ids; +} + +TEST_F(SimpleRateQueryTest, PropertyInvalidRateID) { + std::vector invalid_ids = invalid_rateids(pack); + if (invalid_ids.empty()) { + GTEST_SKIP() << "unable to come up with known invalid rate ids to use for " + << "the test"; + } + + std::vector prop_kinds{ + GRUNSTABLE_QPROP_NDIM, GRUNSTABLE_QPROP_SHAPE, + GRUNSTABLE_QPROP_MAXITEMSIZE}; + std::vector buf; + + for (grunstable_rateid_type invalid_id : invalid_ids) { + for (enum grunstable_ratequery_prop_kind kind : prop_kinds) { + constexpr std::size_t BUF_LEN = 20; // <- arbitrarily large value + constexpr long long DEFAULT_VAL = -25634634LL; // <- arbitrary value + buf.assign(BUF_LEN, DEFAULT_VAL); + ASSERT_GR_ERR(grunstable_ratequery_prop(pack.my_rates(), invalid_id, kind, + buf.data())) + << "executed with invalid_id=" << invalid_id + << ", kind=" << stringify_prop_kind(kind); + EXPECT_THAT(buf, testing::Each(testing::Eq(DEFAULT_VAL))) + << "grunstable_ratequery_prop mutated the ptr even though it " + << "reported a failure. It was called with an invalid id of " + << invalid_id << " and a kind of " << stringify_prop_kind(kind); + } + } +} + +using ParametrizedRateQueryTest = grtest::ParametrizedConfigPresetFixture; + +TEST_P(ParametrizedRateQueryTest, InvalidIdCollision) { + grunstable_rateid_type invalid_id = + grunstable_ratequery_id(pack.my_rates(), nullptr); + for (const grtest::NameIdPair pair : grtest::RateQueryRange(pack)) { + EXPECT_NE(invalid_id, pair.id) + << "there is a collision between the canonical invalid id " + << "and the id associated with the \"" << pair.name << "\" rate"; + } +} + +TEST_P(ParametrizedRateQueryTest, AllUnique) { + std::set name_set; + std::set id_set; + for (const grtest::NameIdPair pair : grtest::RateQueryRange(pack)) { + ASSERT_TRUE(name_set.insert(pair.name).second) + << "the name, \"" << pair.name << "\" appears more than once"; + ASSERT_TRUE(id_set.insert(pair.id).second) + << "the id, " << pair.id << " appears more than once"; + } +} + +TEST_P(ParametrizedRateQueryTest, ConsistentIDs) { + for (const grtest::NameIdPair pair : grtest::RateQueryRange(pack)) { + grunstable_rateid_type rateid = + grunstable_ratequery_id(pack.my_rates(), pair.name.c_str()); + EXPECT_EQ(rateid, pair.id); + } +} + +TEST_P(ParametrizedRateQueryTest, Property) { + std::vector buf; + for (const grtest::NameIdPair pair : grtest::RateQueryRange(pack)) { + constexpr long long DEFAULT_VAL = -25634634LL; // <- arbitrary value + + // check ndim + long long ndim = DEFAULT_VAL; + EXPECT_GR_SUCCESS(grunstable_ratequery_prop(pack.my_rates(), pair.id, + GRUNSTABLE_QPROP_NDIM, &ndim)) + << "for " << pair; + ASSERT_GE(ndim, 0LL) << "for " << pair; + + // check shape + buf.assign((ndim == 0LL) ? 1 : ndim, DEFAULT_VAL); + EXPECT_GR_SUCCESS(grunstable_ratequery_prop( + pack.my_rates(), pair.id, GRUNSTABLE_QPROP_SHAPE, buf.data())) + << "for " << pair; + if (ndim == 0LL) { + EXPECT_EQ(buf[0], DEFAULT_VAL) + << "the buffer passed to grunstable_ratequery_prop was unexpectedly " + << "modified while querying the shape for the rate " << pair + << ". It shouldn't be modified since ndim=0."; + } else { + EXPECT_THAT(buf, testing::Each(testing::Gt(0))) + << "buf holds the shape queried for " << pair; + } + + long long tmp = DEFAULT_VAL; + EXPECT_GR_SUCCESS(grunstable_ratequery_prop(pack.my_rates(), pair.id, + GRUNSTABLE_QPROP_DTYPE, &tmp)) + << "for " << pair; + std::optional dtype_maybe = safe_type_enum_cast(tmp); + if (!dtype_maybe.has_value()) { + GTEST_FAIL() << "Error coercing " << tmp << ", the dtype for " << pair + << ", to an enum value"; + } + enum grunstable_types dtype = dtype_maybe.value(); + + long long maxitemsize = DEFAULT_VAL; + EXPECT_GR_SUCCESS(grunstable_ratequery_prop( + pack.my_rates(), pair.id, GRUNSTABLE_QPROP_MAXITEMSIZE, &maxitemsize)) + << "for " << pair; + if (dtype == GRUNSTABLE_TYPE_F64) { + EXPECT_EQ(maxitemsize, sizeof(double)) << "for " << pair; + } else { + EXPECT_GT(maxitemsize, 0) << "for " << pair; + } + + long long writable = DEFAULT_VAL; + EXPECT_GR_SUCCESS(grunstable_ratequery_prop( + pack.my_rates(), pair.id, GRUNSTABLE_QPROP_WRITABLE, &writable)) + << "for " << pair; + EXPECT_THAT(writable, ::testing::AnyOf(0LL, 1LL)) << "for " << pair; + } +} + +/// summarizes details about rate properties +struct RateProperties { + std::vector shape; + std::size_t maxitemsize; + enum grunstable_types dtype; + bool writable; + + bool operator==(const RateProperties& other) const { + return maxitemsize == other.maxitemsize && shape == other.shape && + dtype == other.dtype && writable == other.writable; + } + + long long n_items() const { + long long n_items = 1LL; + for (std::size_t i = 0; i < shape.size(); i++) { + n_items *= shape[i]; + } + return n_items; // this is correct even for scalars + } + + /// teach googletest how to print this type + friend void PrintTo(const RateProperties& props, std::ostream* os) { + *os << "{shape={"; + for (std::size_t i = 0; i < props.shape.size(); i++) { + if (i != 0) { + *os << ", "; + } + *os << props.shape[i]; + } + *os << "}, maxitemsize=" << props.maxitemsize + << ", dtype=" << stringify_type(props.dtype) + << ", writable=" << props.writable << '}'; + } +}; + +/// construct a RateProperties instance for the specified rate +/// +/// returns an empty optional if any there are any issues +/// +/// @note +/// After we query each property, we encode an extra sanity-check. We **only** +/// encode this in case there are other underlying issues (so that we don't end +/// up with an extremely crazy set of errors). The Property tests should +/// generally provide more details about these tests. To be clear, user-code +/// should never need to include these sanity check! +std::optional try_query_RateProperties( + chemistry_data_storage* my_rates, grunstable_rateid_type rateid) { + long long ndim = -1LL; + if (grunstable_ratequery_prop(my_rates, rateid, GRUNSTABLE_QPROP_NDIM, + &ndim) != GR_SUCCESS) { + return std::nullopt; + } + if (ndim < 0LL) { + return std::nullopt; // sanity-check failed! + } + + std::vector shape; + if (ndim > 0LL) { + shape.assign(ndim, 0LL); + if (grunstable_ratequery_prop(my_rates, rateid, GRUNSTABLE_QPROP_SHAPE, + shape.data()) != GR_SUCCESS) { + return std::nullopt; + } + } + if (std::count_if(shape.begin(), shape.end(), + [](long long x) { return x <= 0; }) > 0) { + return std::nullopt; // sanity check failed! + } + + long long maxitemsize = -1LL; + if (grunstable_ratequery_prop(my_rates, rateid, GRUNSTABLE_QPROP_MAXITEMSIZE, + &maxitemsize) != GR_SUCCESS) { + return std::nullopt; + } + if (maxitemsize <= 0LL) { + return std::nullopt; // sanity check failed! + } + + long long dtype_tmp; + if (grunstable_ratequery_prop(my_rates, rateid, GRUNSTABLE_QPROP_DTYPE, + &dtype_tmp) != GR_SUCCESS) { + return std::nullopt; + } + std::optional dtype = safe_type_enum_cast(dtype_tmp); + if (!dtype.has_value()) { + return std::nullopt; // sanity check failed! + } + + long long writable; + if (grunstable_ratequery_prop(my_rates, rateid, GRUNSTABLE_QPROP_WRITABLE, + &writable) != GR_SUCCESS) { + return std::nullopt; + } + if ((writable != 0LL) && (writable != 1LL)) { + return std::nullopt; + } + + return {RateProperties{shape, static_cast(maxitemsize), + dtype.value(), static_cast(writable)}}; +} + +// returns a value that differs from the input +static double remap_value(double in) { + if (in == 0.0 || !std::isfinite(in)) { + return 1.0; + } + return -in; +} + +TEST_P(ParametrizedRateQueryTest, SetAndGet) { + std::vector initial_buf; + std::vector post_update_buf; + + // iterate over every known (rate-name, rate-id) pair + for (const grtest::NameIdPair pair : grtest::RateQueryRange(pack)) { + // get the properties associated with the current rate + std::optional maybe_props = + try_query_RateProperties(pack.my_rates(), pair.id); + if (!maybe_props.has_value()) { + GTEST_FAIL() + << "something went wrong while trying to lookup the properties for " + << "the " << pair << " rate."; + } + RateProperties props = maybe_props.value(); + long long n_items = props.n_items(); + + if (!props.writable) { + continue; + } + + // load in data associated with the current rate + initial_buf.assign(n_items, NAN); + ASSERT_GR_SUCCESS(grunstable_ratequery_get_f64(pack.my_rates(), pair.id, + initial_buf.data())) + << "for " << pair; + + // overwrite each entry with a different value + for (long long i = 0; i < n_items; i++) { + initial_buf[i] = remap_value(initial_buf[i]); + } + + // write the new values back to my_rates + EXPECT_GR_SUCCESS(grunstable_ratequery_set_f64(pack.my_rates(), pair.id, + initial_buf.data())) + << "for " << pair; + + // finally, lets check that the values actually got updated + post_update_buf.assign(n_items, NAN); + EXPECT_GR_SUCCESS(grunstable_ratequery_get_f64(pack.my_rates(), pair.id, + post_update_buf.data())) + << "for " << pair; + + EXPECT_EQ(initial_buf, post_update_buf) << "for " << pair; + } +} + +using grtest::ChemPreset; +using grtest::FullConfPreset; +using grtest::InitialUnitPreset; + +INSTANTIATE_TEST_SUITE_P( + /* 1st arg is intentionally empty */, ParametrizedRateQueryTest, + ::testing::Values( + FullConfPreset{ChemPreset::primchem0, InitialUnitPreset::simple_z0}, + FullConfPreset{ChemPreset::primchem1, InitialUnitPreset::simple_z0}, + FullConfPreset{ChemPreset::primchem2, InitialUnitPreset::simple_z0}, + FullConfPreset{ChemPreset::primchem3, InitialUnitPreset::simple_z0}, + FullConfPreset{ChemPreset::primchem4_dustspecies3, + InitialUnitPreset::simple_z0})); + +// now, we are going to check on some well-established rates +// -> this may not be the most maintainable test (we may want to re-evaluate it +// in the future) + +enum RateKind { scalar_f64, simple_1d_rate, k13dd }; + +static RateProperties RateProperties_from_RateKind( + const chemistry_data* my_chemistry, RateKind kind) { + const enum grunstable_types f64dtype = GRUNSTABLE_TYPE_F64; + switch (kind) { + case RateKind::scalar_f64: { + std::vector shape = {}; // <-- intentionally empty + return RateProperties{shape, sizeof(double), f64dtype, true}; + } + case RateKind::simple_1d_rate: { + std::vector shape = {my_chemistry->NumberOfTemperatureBins}; + return RateProperties{shape, sizeof(double), f64dtype, true}; + } + case RateKind::k13dd: { + std::vector shape = {my_chemistry->NumberOfTemperatureBins * + 14}; + return RateProperties{shape, sizeof(double), f64dtype, true}; + } + } + GR_INTERNAL_UNREACHABLE_ERROR() +} + +/// returns a map between known rate names and the rate kind +std::map known_rates() { + std::map out; + // radiative rates: + for (int i = 24; i < 32; i++) { + out.insert({"k" + std::to_string(i), RateKind::scalar_f64}); + } + + // standard collisional rates + for (int i = 1; i < 24; i++) { + out.insert({"k" + std::to_string(i), RateKind::simple_1d_rate}); + } + for (int i = 50; i < 58; i++) { + out.insert({"k" + std::to_string(i), RateKind::simple_1d_rate}); + } + for (int i = 125; i < 154; i++) { + out.insert({"k" + std::to_string(i), RateKind::simple_1d_rate}); + } + + // metal chemistry rates + for (int i = 11; i < 55; i++) { + out.insert({"kz" + std::to_string(i), RateKind::simple_1d_rate}); + } + + out.insert({"k13dd", RateKind::k13dd}); + + return out; +} + +using KnownRateQueryTest = + grtest::ConfigPresetFixture; + +TEST_F(KnownRateQueryTest, CheckProperties) { + const std::map known_rate_map = known_rates(); + + // iterate over every known {parameter-name, key-id} pair + for (const grtest::NameIdPair pair : grtest::RateQueryRange(pack)) { + // check if the current rateid is in known_rate_map + std::map::const_iterator search = + known_rate_map.find(pair.name); + if (search == known_rate_map.end()) { + continue; // the rateid is **NOT** in known_rate_map + } + // construct the expected properties + RateProperties expected_props = + RateProperties_from_RateKind(pack.my_chemistry(), search->second); + + // load the actual props + std::optional maybe_actual_props = + try_query_RateProperties(pack.my_rates(), pair.id); + if (!maybe_actual_props.has_value()) { + GTEST_FAIL() + << "something went wrong while trying to lookup the properties for " + << "the " << pair << " rate."; + } + RateProperties actual_props = maybe_actual_props.value(); + + EXPECT_EQ(expected_props, actual_props) + << "this mismatch in the queried properties is for the " << pair + << " rate."; + } + + // it might be useful to repeat the tests using another chemistry_data + // instance with a different NumberOfTemperatureBins value +} diff --git a/tests/unit/test_chemistry_struct_synced.cpp b/tests/unit/test_chemistry_struct_synced.cpp index 6a3bd9642..ee02a02bb 100644 --- a/tests/unit/test_chemistry_struct_synced.cpp +++ b/tests/unit/test_chemistry_struct_synced.cpp @@ -1,4 +1,4 @@ -#include "grtest_cmd.hpp" +#include "grtestutils/cmd.hpp" #include diff --git a/tests/unit/test_ghost_zone.cpp b/tests/unit/test_ghost_zone.cpp index b009fbe7b..16628721f 100644 --- a/tests/unit/test_ghost_zone.cpp +++ b/tests/unit/test_ghost_zone.cpp @@ -24,7 +24,8 @@ #include #include -#include "grtest_utils.hpp" +#include "grtestutils/utils.hpp" +#include "grtestutils/googletest/fixtures.hpp" #include @@ -33,7 +34,8 @@ #define mh 1.67262171e-24 #define kboltz 1.3806504e-16 -typedef int (*property_func)(code_units*, grackle_field_data*, gr_float*); +typedef int (*property_func)(chemistry_data*, chemistry_data_storage*, + code_units*, grackle_field_data*, gr_float*); typedef std::map> val_vec_map_t; @@ -79,7 +81,8 @@ class FieldInitHelper{ // allocates the grackle_field_data struct void construct_field_data(grackle_field_data& my_fields, grid_props& my_grid_props, - code_units& my_units, + const chemistry_data* my_chemistry, + const code_units& my_units, val_vec_map_t& val_map, std::minstd_rand& generator){ @@ -162,11 +165,11 @@ void construct_field_data(grackle_field_data& my_fields, for (int ix = gx; ix < (mx - gx); ix++){ int i = ix + mx * (iy + my*iz); my_fields.density[i] = 1.0; - my_fields.HI_density[i] = grackle_data->HydrogenFractionByMass * + my_fields.HI_density[i] = my_chemistry->HydrogenFractionByMass * my_fields.density[i]; my_fields.HII_density[i] = tiny_number * my_fields.density[i]; my_fields.HM_density[i] = tiny_number * my_fields.density[i]; - my_fields.HeI_density[i] = (1.0 - grackle_data->HydrogenFractionByMass) + my_fields.HeI_density[i] = (1.0 - my_chemistry->HydrogenFractionByMass) * my_fields.density[i]; my_fields.HeII_density[i] = tiny_number * my_fields.density[i]; my_fields.HeIII_density[i] = tiny_number * my_fields.density[i]; @@ -177,7 +180,7 @@ void construct_field_data(grackle_field_data& my_fields, my_fields.HDI_density[i] = tiny_number * my_fields.density[i]; my_fields.e_density[i] = tiny_number * my_fields.density[i]; // solar metallicity - my_fields.metal_density[i] = grackle_data->SolarMetalFractionByMass * + my_fields.metal_density[i] = my_chemistry->SolarMetalFractionByMass * my_fields.density[i]; my_fields.x_velocity[i] = 0.0; @@ -196,7 +199,7 @@ void construct_field_data(grackle_field_data& my_fields, my_fields.RT_H2_dissociation_rate[i] = 0.0; my_fields.RT_heating_rate[i] = 0.0; - my_fields.isrf_habing[i] = grackle_data->interstellar_radiation_field; + my_fields.isrf_habing[i] = my_chemistry->interstellar_radiation_field; } } } @@ -246,116 +249,14 @@ bool equal_ghost_values(val_vec_map_t& ref, val_vec_map_t& actual, return true; } -namespace { // stuff within anonymous namespace is local to the current file +using APIGhostZoneTest = grtest::ParametrizedConfigPresetFixture; -/// the following is just a dummy struct that primarily exists to assist with -/// cleanup (and avoid memory leaks) -struct GrackleCtxPack { - bool successful_default = false; - bool successful_data_file = false; - bool successful_init = false; - code_units my_units; - chemistry_data* my_chemistry = nullptr; -}; - -void cleanup_grackle_conditions(GrackleCtxPack& pack) { - if (pack.successful_init) { free_chemistry_data(); } - if (pack.my_chemistry != nullptr) { delete pack.my_chemistry; } -} - -GrackleCtxPack setup_simple_grackle_conditions(int primordial_chemistry) { - /********************************************************************* - / Initial setup of units and chemistry objects. - *********************************************************************/ - - GrackleCtxPack pack; - - // Set initial redshift (for internal units). - double initial_redshift = 0.; - - // First, set up the units system. - // These are conversions from code units to cgs. - pack.my_units.comoving_coordinates = 0; // 1 if cosmological sim, 0 if not - pack.my_units.density_units = 1.67e-24; - pack.my_units.length_units = 1.0; - pack.my_units.time_units = 1.0e12; - pack.my_units.a_units = 1.0; // units for the expansion factor - // Set expansion factor to 1 for non-cosmological simulation. - pack.my_units.a_value = 1. / (1. + initial_redshift) / pack.my_units.a_units; - set_velocity_units(&pack.my_units); - - // Second, create a chemistry object for parameters. - pack.my_chemistry = new chemistry_data; - if (set_default_chemistry_parameters(pack.my_chemistry) != GR_SUCCESS) { - return pack; - } - pack.successful_default=true; - - // Set parameter values for chemistry. - // Access the parameter storage with the struct you've created - // or with the grackle_data pointer declared in grackle.h (see further below). - pack.my_chemistry->use_grackle = 1; // chemistry on - pack.my_chemistry->use_isrf_field = 1; - pack.my_chemistry->with_radiative_cooling = 1; // cooling on - pack.my_chemistry->primordial_chemistry = primordial_chemistry; - pack.my_chemistry->dust_chemistry = (primordial_chemistry == 0) ? 0 : 1; - pack.my_chemistry->metal_cooling = 1; // metal cooling on - pack.my_chemistry->UVbackground = 1; // UV background on - - pack.successful_data_file = grtest::set_standard_datafile( - *pack.my_chemistry, "CloudyData_UVB=HM2012.h5" - ); - if (!pack.successful_data_file) { return pack; } - - // Finally, initialize the chemistry object. - if (initialize_chemistry_data(&pack.my_units) != GR_SUCCESS) { return pack; } - pack.successful_init = true; - return pack; -} - -} // anonymous namespace - -// this defines a parameterized test-fixture (it is parameterized on -// primordial_chemistry) -// -> it has a GetParam() method to access the parameters -// -> to assist with avoiding memory leaks, I decided to also make this setup -// and teardown GrackleCtxPack. -// -> Frankly, I don't love this, but I think it is okay since the test -// really doesn't care how grackle is configured (other than that -// primordial_chemistry varies and that it will actually perform -// calculations) -class APIConventionTest : public testing::TestWithParam { - protected: - void SetUp() override { - // Disable output - grackle_verbose = 0; - - // called immediately after the constructor (but before the test-case) - int primordial_chemistry = GetParam(); - - pack_ = setup_simple_grackle_conditions(primordial_chemistry); - if (!pack_.successful_default) { - FAIL() << "Error in set_default_chemistry_parameters."; - } else if (!pack_.successful_data_file) { - GTEST_SKIP() << "something went wrong with finding the data file"; - } else if (!pack_.successful_init) { - FAIL() << "Error in initialize_chemistry_data."; - } - } - - void TearDown() override { - cleanup_grackle_conditions(this->pack_); - } - - GrackleCtxPack pack_; -}; - -TEST_P(APIConventionTest, GridZoneStartEnd) { +TEST_P(APIGhostZoneTest, GridZoneStartEnd) { grid_props my_grid_props = {{5,6,7}, {1,0,2}}; - // alias the pack_ attribute tracked by the fixture - GrackleCtxPack& pack = pack_; + // the pack attribute holds grtest::GrackleCtxPack + code_units my_units = pack.initial_units(); // initialize pseudo random number generator std::uint32_t seed = 1379069008; @@ -377,7 +278,7 @@ TEST_P(APIConventionTest, GridZoneStartEnd) { // For example: my_fields.density = my_field_map["density"].data() val_vec_map_t my_field_map; construct_field_data( - my_fields, my_grid_props, pack.my_units, my_field_map, generator + my_fields, my_grid_props, pack.my_chemistry(), my_units, my_field_map, generator ); // orig_field_map_copy is a deepcopy of my_field_map. We will use this as a @@ -393,9 +294,11 @@ TEST_P(APIConventionTest, GridZoneStartEnd) { // Evolving the chemistry. // some timestep - double dt = 3.15e7 * 1e6 / pack.my_units.time_units; + double dt = 3.15e7 * 1e6 / my_units.time_units; - if (solve_chemistry(&pack.my_units, &my_fields, dt) != GR_SUCCESS) { + if (local_solve_chemistry(pack.my_chemistry(), pack.my_rates(), + &my_units, &my_fields, + dt)!= GR_SUCCESS) { FAIL() << "Error running solve_chemistry"; } @@ -404,17 +307,17 @@ TEST_P(APIConventionTest, GridZoneStartEnd) { FAIL() << "Some ghost values were modified in solve_chemistry."; } - // Now check what hapens when computing various properties - const char* func_names[5] = {"calculate_cooling_time", - "calculate_temperature", - "calculate_pressure", - "calculate_gamma", - "calculate_dust_temperature"}; - property_func func_ptrs[5] = {&calculate_cooling_time, - &calculate_temperature, - &calculate_pressure, - &calculate_gamma, - &calculate_dust_temperature}; + // Now check what happens when computing various properties + const char* func_names[5] = {"local_calculate_cooling_time", + "local_calculate_temperature", + "local_calculate_pressure", + "local_calculate_gamma", + "local_calculate_dust_temperature"}; + property_func func_ptrs[5] = {&local_calculate_cooling_time, + &local_calculate_temperature, + &local_calculate_pressure, + &local_calculate_gamma, + &local_calculate_dust_temperature}; for (int i = 0; i < 5; i++){ std::uint32_t seed2 = 1860889605; std::minstd_rand generator2(seed2); @@ -426,7 +329,7 @@ TEST_P(APIConventionTest, GridZoneStartEnd) { // perform the calculation property_func func_ptr = func_ptrs[i]; - if ( (*func_ptr)(&pack.my_units, &my_fields, out_vals.data()) + if ( (*func_ptr)(pack.my_chemistry(), pack.my_rates(), &my_units, &my_fields, out_vals.data()) != GR_SUCCESS ) { FAIL() << "Error reported by " << func_names; } @@ -439,6 +342,15 @@ TEST_P(APIConventionTest, GridZoneStartEnd) { } +using grtest::FullConfPreset; +using grtest::ChemPreset; +using grtest::InitialUnitPreset; + INSTANTIATE_TEST_SUITE_P( - VaryingPrimordialChem, APIConventionTest, ::testing::Range(0, 4) + /* 1st arg is intentionally empty */, APIGhostZoneTest, + ::testing::Values( + FullConfPreset{ChemPreset::primchem0, InitialUnitPreset::simple_z0}, + FullConfPreset{ChemPreset::primchem1, InitialUnitPreset::simple_z0}, + FullConfPreset{ChemPreset::primchem2, InitialUnitPreset::simple_z0}, + FullConfPreset{ChemPreset::primchem3, InitialUnitPreset::simple_z0}) ); diff --git a/tests/unit/test_linalg.cpp b/tests/unit/test_linalg.cpp index 943b83d75..d3fb7d080 100644 --- a/tests/unit/test_linalg.cpp +++ b/tests/unit/test_linalg.cpp @@ -2,7 +2,7 @@ #include #include "fortran_func_wrappers.hpp" -#include "utest_helpers.hpp" +#include "grtestutils/googletest/check_allclose.hpp" /// Records the paramters for a linear algebra test-case diff --git a/tests/unit/test_status_reporting.cpp b/tests/unit/test_status_reporting.cpp index 1922fef2a..91ce3791c 100644 --- a/tests/unit/test_status_reporting.cpp +++ b/tests/unit/test_status_reporting.cpp @@ -12,7 +12,7 @@ #include "grackle.h" // GR_FAIL #include "status_reporting.h" -#include "grtest_os.hpp" +#include "grtestutils/os.hpp" testing::AssertionResult ContainsFormattedMessage_(int n) {