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

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

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

Go to the source code of this file.

Macros

#define ERF_STRUCTURE_MODEL_CONCAT_(a, b)   a##b
 
#define ERF_STRUCTURE_MODEL_CONCAT(a, b)   ERF_STRUCTURE_MODEL_CONCAT_(a, b)
 
#define ERF_REGISTER_STRUCTURE_MODEL(Class, name)
 Register model class Class under name: erf.structures.model = name selects it. Class is constructed as Class(int max_level). More...
 

Typedefs

using StructureModelCreator = std::function< std::unique_ptr< StructureModel >(int max_level)>
 Creates a model; the model reads its own inputs in its constructor. More...
 

Functions

bool register_structure_model (const std::string &name, StructureModelCreator creator)
 Add a model to the registry; aborts on an empty name or a name registered twice. More...
 
std::vector< std::string > structure_model_names ()
 Names erf.structures.model accepts, sorted; empty when no model is compiled in. More...
 
std::unique_ptr< StructureModel > make_structure_model (const std::string &name, int max_level)
 The model registered as name, or nullptr when no model has that name. More...
 
std::unique_ptr< StructureModel > make_structure_model_from_inputs (int max_level)
 The model erf.structures.model selects, or nullptr when the key is not set. More...
 
void check_structure_plot_names (const StructureModel &model, const std::vector< std::string > &taken)
 Stop the run when the model's plot_var_names() cannot all be plotted. More...
 

Detailed Description

The structure models compiled into this executable.

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

ERF_REGISTER_STRUCTURE_MODEL(MyModel, "mymodel")

at namespace scope, and erf.structures.model then selects it by that name. The model's directory (STRUCTURE_HOME in GNU make, ERF_STRUCTURE_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).

Macro Definition Documentation

◆ ERF_REGISTER_STRUCTURE_MODEL

#define ERF_REGISTER_STRUCTURE_MODEL (   Class,
  name 
)
Value:
namespace { \
[[maybe_unused]] const bool ERF_STRUCTURE_MODEL_CONCAT(erf_structure_model_registered_, __LINE__) = \
register_structure_model((name), [](int max_level) -> std::unique_ptr<StructureModel> { \
return std::make_unique<Class>(max_level); \
}); \
}
std::string name
Definition: ERF_Plotfile2DCatalog.cpp:139
#define ERF_STRUCTURE_MODEL_CONCAT(a, b)
Definition: ERF_StructureModelRegistry.H:65

Register model class Class under name: erf.structures.model = name selects it. Class is constructed as Class(int max_level).

◆ ERF_STRUCTURE_MODEL_CONCAT

#define ERF_STRUCTURE_MODEL_CONCAT (   a,
  b 
)    ERF_STRUCTURE_MODEL_CONCAT_(a, b)

◆ ERF_STRUCTURE_MODEL_CONCAT_

#define ERF_STRUCTURE_MODEL_CONCAT_ (   a,
  b 
)    a##b

Typedef Documentation

◆ StructureModelCreator

using StructureModelCreator = std::function<std::unique_ptr<StructureModel>(int max_level)>

Creates a model; the model reads its own inputs in its constructor.

Function Documentation

◆ check_structure_plot_names()

void check_structure_plot_names ( const StructureModel &  model,
const std::vector< std::string > &  taken 
)

Stop the run when the model's plot_var_names() cannot all be plotted.

Aborts, naming the model and the name, on an empty name, a name the model lists twice, or a name in taken (the names ERF and the other models already provide). ERF's plotfile assembly would otherwise drop the name from the list and still fill it, and fail with a less helpful message.

74 {
75  const std::vector<std::string> names = model.plot_var_names();
76  for (std::size_t i = 0; i < names.size(); ++i) {
77  const std::string& n = names[i];
78  const std::string who = std::string("structure model \"") + model.name() + "\"";
79  if (n.empty()) {
80  amrex::Abort(who + " lists an empty plot variable name");
81  }
82  if (std::find(names.begin(), names.begin() + static_cast<std::ptrdiff_t>(i), n) != names.begin() + static_cast<std::ptrdiff_t>(i)) {
83  amrex::Abort(who + " lists the plot variable \"" + n + "\" twice");
84  }
85  if (std::find(taken.begin(), taken.end(), n) != taken.end()) {
86  amrex::Abort(who + " lists the plot variable \"" + n
87  + "\", which ERF or another model already provides; give the model's names its own prefix");
88  }
89  }
90 }
virtual std::vector< std::string > plot_var_names() const
Fields the ERF plotfile can carry, by name.
Definition: ERF_StructureModel.H:304
virtual const char * name() const =0
Name of the model, as erf.structures.model selects it.

Referenced by ERF::appendPlot3DVariables().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_structure_model()

std::unique_ptr<StructureModel> make_structure_model ( const std::string &  name,
int  max_level 
)

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

46 {
47  const auto& registry = structure_model_registry();
48  const auto it = registry.find(name);
49  if (it == registry.end()) { return nullptr; }
50  return it->second(max_level);
51 }

Referenced by make_structure_model_from_inputs().

Here is the caller graph for this function:

◆ make_structure_model_from_inputs()

std::unique_ptr<StructureModel> make_structure_model_from_inputs ( int  max_level)

The model erf.structures.model selects, or nullptr when the key is not set.

Aborts when the key names no registered model, listing the names this executable accepts.

55 {
56  amrex::ParmParse pp("erf.structures");
57  std::string name;
58  if (!pp.query("model", name)) { return nullptr; }
59 
60  std::unique_ptr<StructureModel> model = make_structure_model(name, max_level);
61  if (!model) {
62  std::string known;
63  for (const auto& n : structure_model_names()) { known += (known.empty() ? "" : ", ") + ("\"" + n + "\""); }
64  amrex::Abort("erf.structures.model = \"" + name + "\" is not one of the structure models compiled in ("
65  + (known.empty() ? std::string("none; add a model directory with ERF_STRUCTURE_MODEL_DIRS"
66  " in CMake or STRUCTURE_HOME in GNU make")
67  : known) + ")");
68  }
69  return model;
70 }
ParmParse pp("prob")
std::unique_ptr< StructureModel > make_structure_model(const std::string &name, int max_level)
The model registered as name, or nullptr when no model has that name.
Definition: ERF_StructureModelRegistry.cpp:45
std::vector< std::string > structure_model_names()
Names erf.structures.model accepts, sorted; empty when no model is compiled in.
Definition: ERF_StructureModelRegistry.cpp:37

Referenced by ERF::ERF_shared().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ register_structure_model()

bool register_structure_model ( const std::string &  name,
StructureModelCreator  creator 
)

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

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

23 {
24  auto& registry = structure_model_registry();
25  if (name.empty()) {
26  amrex::Abort("ERF_REGISTER_STRUCTURE_MODEL: a structure model registered an empty name");
27  }
28  if (registry.count(name) > 0) {
29  amrex::Abort("ERF_REGISTER_STRUCTURE_MODEL: two structure models registered the name \"" + name
30  + "\"; each model directory in the build must use its own name");
31  }
32  registry.emplace(name, std::move(creator));
33  return true;
34 }

◆ structure_model_names()

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

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

38 {
39  std::vector<std::string> names;
40  for (const auto& kv : structure_model_registry()) { names.push_back(kv.first); }
41  return names; // std::map keeps them sorted
42 }

Referenced by make_structure_model_from_inputs().

Here is the caller graph for this function: