ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_StructureModelRegistry.H
Go to the documentation of this file.
1 #ifndef ERF_STRUCTURE_MODEL_REGISTRY_H
2 #define ERF_STRUCTURE_MODEL_REGISTRY_H
3 
4 #include <ERF_StructureModel.H>
5 
6 #include <functional>
7 #include <memory>
8 #include <string>
9 #include <vector>
10 
11 /**
12  * @file ERF_StructureModelRegistry.H
13  * @brief The structure models compiled into this executable
14  *
15  * ERF lists no model itself. Each model registers itself from its own .cpp with
16  *
17  * ERF_REGISTER_STRUCTURE_MODEL(MyModel, "mymodel")
18  *
19  * at namespace scope, and erf.structures.model then selects it by that name. The
20  * model's directory (STRUCTURE_HOME in GNU make, ERF_STRUCTURE_MODEL_DIRS in
21  * CMake) adds that .cpp to the build, so adding a model changes no file of ERF.
22  *
23  * The registration runs during static initialization, so the .cpp that holds it
24  * must be linked as an object file (ERF's CMake build compiles model directories
25  * into its OBJECT library, and GNU make links every object). This is the scheme of
26  * the fire engines (ERF_FireEngineRegistry.H).
27  */
28 
29 /// Creates a model; the model reads its own inputs in its constructor.
30 using StructureModelCreator = std::function<std::unique_ptr<StructureModel>(int max_level)>;
31 
32 /**
33  * @brief Add a model to the registry; aborts on an empty name or a name registered twice
34  *
35  * Called by ERF_REGISTER_STRUCTURE_MODEL; returns true so the macro can hold the
36  * result in a static variable.
37  */
38 bool register_structure_model (const std::string& name, StructureModelCreator creator);
39 
40 /// Names erf.structures.model accepts, sorted; empty when no model is compiled in.
41 std::vector<std::string> structure_model_names ();
42 
43 /// The model registered as name, or nullptr when no model has that name.
44 std::unique_ptr<StructureModel> make_structure_model (const std::string& name, int max_level);
45 
46 /**
47  * @brief The model erf.structures.model selects, or nullptr when the key is not set
48  *
49  * Aborts when the key names no registered model, listing the names this
50  * executable accepts.
51  */
52 std::unique_ptr<StructureModel> make_structure_model_from_inputs (int max_level);
53 
54 /**
55  * @brief Stop the run when the model's plot_var_names() cannot all be plotted
56  *
57  * Aborts, naming the model and the name, on an empty name, a name the model lists
58  * twice, or a name in taken (the names ERF and the other models already provide).
59  * ERF's plotfile assembly would otherwise drop the name from the list and still
60  * fill it, and fail with a less helpful message.
61  */
62 void check_structure_plot_names (const StructureModel& model, const std::vector<std::string>& taken);
63 
64 #define ERF_STRUCTURE_MODEL_CONCAT_(a, b) a##b
65 #define ERF_STRUCTURE_MODEL_CONCAT(a, b) ERF_STRUCTURE_MODEL_CONCAT_(a, b)
66 
67 /**
68  * @brief Register model class Class under name: erf.structures.model = name
69  * selects it. Class is constructed as Class(int max_level).
70  */
71 #define ERF_REGISTER_STRUCTURE_MODEL(Class, name) \
72  namespace { \
73  [[maybe_unused]] const bool ERF_STRUCTURE_MODEL_CONCAT(erf_structure_model_registered_, __LINE__) = \
74  register_structure_model((name), [](int max_level) -> std::unique_ptr<StructureModel> { \
75  return std::make_unique<Class>(max_level); \
76  }); \
77  }
78 
79 #endif
std::string name
Definition: ERF_Plotfile2DCatalog.cpp:139
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.
Definition: ERF_StructureModelRegistry.cpp:54
std::function< std::unique_ptr< StructureModel >(int max_level)> StructureModelCreator
Creates a model; the model reads its own inputs in its constructor.
Definition: ERF_StructureModelRegistry.H:30
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
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.
Definition: ERF_StructureModelRegistry.cpp:22
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
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.
Definition: ERF_StructureModelRegistry.cpp:73
The interface between ERF and a model of structures in the flow.
A model of structures in the flow, behind ERF's structure coupling.
Definition: ERF_StructureModel.H:164