ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_DustModelRegistry.H File Reference

The dust models compiled into this executable. More...

#include <ERF_DustModel.H>
#include <functional>
#include <memory>
#include <string>
#include <vector>
Include dependency graph for ERF_DustModelRegistry.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define ERF_DUST_MODEL_CONCAT_(a, b)   a##b
 
#define ERF_DUST_MODEL_CONCAT(a, b)   ERF_DUST_MODEL_CONCAT_(a, b)
 
#define ERF_REGISTER_DUST_MODEL(Class, name)
 Register model class Class under name: erf.dust.model = name selects it. Class is default-constructed. More...
 

Typedefs

using DustModelCreator = std::function< std::unique_ptr< DustModel >()>
 Creates a model; the model reads its inputs in initialize(). More...
 

Functions

bool register_dust_model (const std::string &name, DustModelCreator creator)
 Add a model to the registry; aborts on an empty name or a name registered twice. More...
 
std::vector< std::string > dust_model_names ()
 Names erf.dust.model accepts, sorted; empty when no model is compiled in. More...
 
std::unique_ptr< DustModel > make_dust_model (const std::string &name)
 The model registered as name, or nullptr when no model has that name. More...
 
std::unique_ptr< DustModel > make_dust_model_from_inputs ()
 The model the inputs select, or nullptr when erf.dust.enable is not true. More...
 

Detailed Description

The dust models compiled into this executable.

ERF lists no model itself. Each model registers itself from its own .cpp with

ERF_REGISTER_DUST_MODEL(MyModel, "mymodel")

at namespace scope, and erf.dust.model then selects it by that name. The model's directory (DUST_HOME in GNU make, ERF_DUST_MODEL_DIRS in CMake) adds that .cpp to the build, so adding a model changes no file of ERF.

The registration runs during static initialization, so the .cpp that holds it must be linked as an object file (ERF's CMake build compiles model directories into its OBJECT library, and GNU make links every object). This is the scheme of the fire engines (ERF_FireEngineRegistry.H) and the structure models.

Macro Definition Documentation

◆ ERF_DUST_MODEL_CONCAT

#define ERF_DUST_MODEL_CONCAT (   a,
  b 
)    ERF_DUST_MODEL_CONCAT_(a, b)

◆ ERF_DUST_MODEL_CONCAT_

#define ERF_DUST_MODEL_CONCAT_ (   a,
  b 
)    a##b

◆ ERF_REGISTER_DUST_MODEL

#define ERF_REGISTER_DUST_MODEL (   Class,
  name 
)
Value:
namespace { \
[[maybe_unused]] const bool ERF_DUST_MODEL_CONCAT(erf_dust_model_registered_, __LINE__) = \
register_dust_model((name), []() -> std::unique_ptr<DustModel> { \
return std::make_unique<Class>(); \
}); \
}
#define ERF_DUST_MODEL_CONCAT(a, b)
Definition: ERF_DustModelRegistry.H:57
std::string name
Definition: ERF_Plotfile2DCatalog.cpp:139

Register model class Class under name: erf.dust.model = name selects it. Class is default-constructed.

Typedef Documentation

◆ DustModelCreator

using DustModelCreator = std::function<std::unique_ptr<DustModel>()>

Creates a model; the model reads its inputs in initialize().

Function Documentation

◆ dust_model_names()

std::vector<std::string> dust_model_names ( )

Names erf.dust.model accepts, sorted; empty when no model is compiled in.

47 {
48  std::vector<std::string> names;
49  for (const auto& kv : dust_model_registry()) { names.push_back(kv.first); }
50  return names; // std::map keeps them sorted
51 }

Referenced by make_dust_model_from_inputs().

Here is the caller graph for this function:

◆ make_dust_model()

std::unique_ptr<DustModel> make_dust_model ( const std::string &  name)

The model registered as name, or nullptr when no model has that name.

55 {
56  const auto& registry = dust_model_registry();
57  const auto it = registry.find(name);
58  if (it == registry.end()) { return nullptr; }
59  return it->second();
60 }

Referenced by make_dust_model_from_inputs().

Here is the caller graph for this function:

◆ make_dust_model_from_inputs()

std::unique_ptr<DustModel> make_dust_model_from_inputs ( )

The model the inputs select, or nullptr when erf.dust.enable is not true.

erf.dust.model names the model; with one model compiled in it may be left out. Aborts when dust is enabled and no model, or no model of that name, is compiled in, listing the names this executable accepts, and when the model fills more dust scalars than the build has (NSCALARS_DUST).

64 {
65  amrex::ParmParse pp("erf.dust");
66  bool enable = false;
67  pp.query("enable", enable);
68  if (!enable) { return nullptr; }
69 
70  std::string name;
71  if (!pp.query("model", name)) {
72  const std::vector<std::string> names = dust_model_names();
73  if (names.size() != 1) {
74  amrex::Abort("erf.dust.enable = true needs erf.dust.model to name one of the dust models compiled in ("
75  + known_dust_models() + ")");
76  }
77  name = names.front();
78  }
79 
80  std::unique_ptr<DustModel> model = make_dust_model(name);
81  if (!model) {
82  amrex::Abort("erf.dust.model = \"" + name + "\" is not one of the dust models compiled in ("
83  + known_dust_models() + ")");
84  }
85  if (model->num_scalars() < 1 || model->num_scalars() > NSCALARS_DUST) {
86  amrex::Abort("dust model \"" + name + "\" fills " + std::to_string(model->num_scalars())
87  + " dust scalar(s), but this build has " + std::to_string(NSCALARS_DUST)
88  + "; rebuild with ERF_NUM_DUST_SCALARS (CMake) or NUM_DUST_SCALARS (GNU make) = "
89  + std::to_string(model->num_scalars()));
90  }
91  return model;
92 }
std::vector< std::string > dust_model_names()
Names erf.dust.model accepts, sorted; empty when no model is compiled in.
Definition: ERF_DustModelRegistry.cpp:46
std::unique_ptr< DustModel > make_dust_model(const std::string &name)
The model registered as name, or nullptr when no model has that name.
Definition: ERF_DustModelRegistry.cpp:54
#define NSCALARS_DUST
Definition: ERF_IndexDefines.H:30
ParmParse pp("prob")
Here is the call graph for this function:

◆ register_dust_model()

bool register_dust_model ( const std::string &  name,
DustModelCreator  creator 
)

Add a model to the registry; aborts on an empty name or a name registered twice.

Called by ERF_REGISTER_DUST_MODEL; returns true so the macro can hold the result in a static variable.

32 {
33  auto& registry = dust_model_registry();
34  if (name.empty()) {
35  amrex::Abort("ERF_REGISTER_DUST_MODEL: a dust model registered an empty name");
36  }
37  if (registry.count(name) > 0) {
38  amrex::Abort("ERF_REGISTER_DUST_MODEL: two dust models registered the name \"" + name
39  + "\"; each model directory in the build must use its own name");
40  }
41  registry.emplace(name, std::move(creator));
42  return true;
43 }