Skip to content

Commit 0687ebf

Browse files
committed
shift to using ParameterMap in constructors
1 parent 58e7aac commit 0687ebf

2 files changed

Lines changed: 43 additions & 36 deletions

File tree

src/cooling/cool_components.h

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <cmath>
77

88
#include "../global/global.h"
9+
#include "../io/ParameterMap.h"
10+
#include "../utils/error_handling.h"
911

1012
/*! @defgroup coolcomp Cooling Component Logic
1113
*
@@ -158,23 +160,53 @@ __forceinline__ __device__ Real analytic_cie_lambda(Real log10T)
158160
* - If we started to model varying mmw, we could also adopt the TIGRESS strategy to more
159161
* smoothly turn off heating at higher temperatures
160162
*/
161-
struct PhotoelectricHeatingModel {
163+
class PhotoelectricHeatingModel
164+
{
162165
/*! This theoretically represents the mean density in the simulation volume. A value of 0.0
163166
* indicates that there is no heating.
164167
*
165168
* @note
166169
* I can't remember the precise interpretation, but I think the idea may be that it may be
167170
* used because it loosely relates to the rate of star formation...
168171
*/
169-
double n_av_cgs = 0.0;
172+
double n_av_cgs_;
173+
174+
inline static constexpr const char* use_photoelectric_parname = "chemistry.photoelectric_heating";
175+
inline static constexpr const char* photoelectric_n_av_parname = "chemistry.photoelectric_n_av_cgs";
176+
177+
public:
178+
__host__ static bool is_specified_by_params(ParameterMap& pmap)
179+
{
180+
return pmap.value_or(PhotoelectricHeatingModel::use_photoelectric_parname, false);
181+
}
182+
183+
__host__ explicit PhotoelectricHeatingModel(ParameterMap& pmap)
184+
{
185+
// In this case, we want to actually use photoelectric heating
186+
if (pmap.value_or(PhotoelectricHeatingModel::use_photoelectric_parname, false)) {
187+
double n_av_cgs = pmap.value_or(PhotoelectricHeatingModel::photoelectric_n_av_parname, 100.0);
188+
CHOLLA_ASSERT(n_av_cgs > 0.0, "The \"%s\" parameter cannot specify a non-positive value",
189+
PhotoelectricHeatingModel::photoelectric_n_av_parname);
190+
n_av_cgs_ = n_av_cgs;
191+
} else {
192+
// in this case, we initialize an instance that doesn't actually perform any
193+
// heating/cooling. We may want to get rid of this branch
194+
CHOLLA_ASSERT(!pmap.has_param(photoelectric_n_av_parname),
195+
"It is an error to specify the \"%s\" parameter when the \"%s\" hasn't "
196+
"explicitly been set to true.",
197+
PhotoelectricHeatingModel::photoelectric_n_av_parname,
198+
PhotoelectricHeatingModel::use_photoelectric_parname);
199+
n_av_cgs_ = 0.0; // <- this means that there isn't heating
200+
}
201+
}
170202

171-
bool is_active() const { return n_av_cgs != 0.0; }
203+
bool is_active() const { return n_av_cgs_ != 0.0; }
172204

173205
/*! \brief computes the heating rate per unit volume, erg /s / cm^3.
174206
*
175207
* This **NEVER** returns a negative value.
176208
*/
177-
__device__ Real operator()(Real n, Real T) const { return (T < 1e4) ? n * n_av_cgs * 1.0e-26 : 0.0; }
209+
__device__ Real operator()(Real n, Real T) const { return (T < 1e4) ? n * n_av_cgs_ * 1.0e-26 : 0.0; }
178210
};
179211

180212
} // namespace cool_component

src/cooling/cooling_cuda.cu

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,7 @@ class CoolRecipeCloudyAndPhotoHeating
264264
cool_component::PhotoelectricHeatingModel photoelectric_fn_;
265265

266266
public:
267-
__host__ CoolRecipeCloudyAndPhotoHeating(ParameterMap &pmap,
268-
cool_component::PhotoelectricHeatingModel photoelectric_fn)
269-
: net_cloudy_(pmap), photoelectric_fn_{photoelectric_fn}
270-
{
271-
}
267+
__host__ CoolRecipeCloudyAndPhotoHeating(ParameterMap &pmap) : net_cloudy_(pmap), photoelectric_fn_(pmap) {}
272268

273269
__device__ Real cool_rate(Real n, Real T) const { return net_cloudy_(n, T) - photoelectric_fn_(n, T); }
274270
};
@@ -311,9 +307,7 @@ class CoolRecipeTI
311307
}
312308

313309
public:
314-
__host__ CoolRecipeTI(cool_component::PhotoelectricHeatingModel photoelectric_fn) : photoelectric_fn{photoelectric_fn}
315-
{
316-
}
310+
explicit __host__ CoolRecipeTI(ParameterMap &pmap) : photoelectric_fn(pmap) {}
317311

318312
__device__ Real cool_rate(Real n, Real T) { return cool_rate_only_(n, T) - photoelectric_fn(n, T); }
319313
};
@@ -323,34 +317,15 @@ std::function<void(Grid3D &)> configure_cooling_callback(std::string kind, Param
323317
// the caller of this function will is responsible for raising an error when:
324318
// - "chemistry.data_file" is set, but we aren't using a recipe that doesn't need a datafile
325319

326-
// First, we configure an instance of PhotoelectricHeatingModel, based off the parameters
327-
// -> to help provide informative error messages, we store the names of the parameters in variables
328-
// -> maybe we should only use a single parameter, to just specify the value of n_av_cgs?
329-
const char *use_photoelectric_parname = "chemistry.photoelectric_heating";
330-
const char *photoelectric_n_av_parname = "chemistry.photoelectric_n_av_cgs";
331-
332-
cool_component::PhotoelectricHeatingModel photoelectric_fn;
333-
if (pmap.value_or(use_photoelectric_parname, false)) {
334-
// In this case, we want to actually use photoelectric heating
335-
double n_av_cgs = pmap.value_or(photoelectric_n_av_parname, 100.0);
336-
CHOLLA_ASSERT(n_av_cgs > 0.0, "The \"%s\" parameter cannot specify a non-positive value",
337-
photoelectric_n_av_parname);
338-
photoelectric_fn = cool_component::PhotoelectricHeatingModel{n_av_cgs};
339-
} else {
340-
CHOLLA_ASSERT(!pmap.has_param(photoelectric_n_av_parname),
341-
"It is an error to specify the \"%s\" parameter when the \"%s\" hasn't "
342-
"explicitly been set to true.",
343-
photoelectric_n_av_parname, use_photoelectric_parname);
344-
photoelectric_fn = cool_component::PhotoelectricHeatingModel{0.0}; // this means that there isn't heating
345-
}
320+
bool use_photoelectric_heating = cool_component::PhotoelectricHeatingModel::is_specified_by_params(pmap);
346321

347322
// Next, we branch based on the cooling-recipe
348323
if (kind == "tabulated-cloudy") {
349324
// since photoelectric_fn can be configured to be inactive, we could probably just
350325
// consolidate the definitions of CoolRecipeCloudyAndPhotoHeating and CoolRecipeCloudy
351326

352-
if (photoelectric_fn.is_active()) {
353-
CoolRecipeCloudyAndPhotoHeating recipe(pmap, photoelectric_fn);
327+
if (use_photoelectric_heating) {
328+
CoolRecipeCloudyAndPhotoHeating recipe(pmap);
354329
CoolingUpdateExecutor<CoolRecipeCloudyAndPhotoHeating> updater(recipe);
355330
return {updater};
356331
} else {
@@ -359,13 +334,13 @@ std::function<void(Grid3D &)> configure_cooling_callback(std::string kind, Param
359334
return {updater};
360335
}
361336
} else if (kind == "piecewise-cie") {
362-
CHOLLA_ASSERT(not photoelectric_fn.is_active(),
337+
CHOLLA_ASSERT(not use_photoelectric_heating,
363338
"The \"%s\" cooling recipe is **NOT** compatible with photoelectric heating", kind.c_str());
364339
CoolRecipeCIE recipe{};
365340
CoolingUpdateExecutor<CoolRecipeCIE> updater(recipe);
366341
return {updater};
367342
} else if (kind == "piecewise-ti") {
368-
CoolRecipeTI recipe{photoelectric_fn};
343+
CoolRecipeTI recipe(pmap);
369344
CoolingUpdateExecutor<CoolRecipeTI> updater(recipe);
370345
return {updater};
371346
}

0 commit comments

Comments
 (0)