netxsimdg
Loading...
Searching...
No Matches
ModelComponent.hpp
Go to the documentation of this file.
1
8#ifndef MODELCOMPONENT_HPP
9#define MODELCOMPONENT_HPP
10
11#include "include/Logged.hpp"
15#include "include/Time.hpp"
16
17#include <functional>
18#include <string>
19#include <unordered_map>
20#include <unordered_set>
21
22namespace Nextsim {
23
24class ModelComponent;
25
32public:
33 typedef Logged::level OutputLevel;
34
35 enum class ProtectedArray {
36 // Prognostic model fields
37 H_ICE, // Ice thickness, cell average, m
38 C_ICE, // Ice concentration
39 H_SNOW, // Snow depth, cell average, m
40 T_ICE, // Ice temperature, ˚C
41 // External data fields
42 T_AIR, // Air temperature, ˚C
43 DEW_2M, // Dew point at 2 m, ˚C
44 P_AIR, // sea level air pressure, Pa
45 MIXRAT, // water vapour mass mixing ratio
46 SW_IN, // incoming shortwave flux, W m⁻²
47 LW_IN, // incoming longwave flux, W m⁻²
48 MLD, // mixed layer depth, m
49 SNOW, // snow fall, kg m⁻² s⁻¹
50 SSS, // sea surface salinity, PSU
51 SST, // sea surface temperature, ˚C
52 EXT_SSS, // sea surface salinity from coupling or forcing, PSU
53 EXT_SST, // sea surface temperature from coupling or forcing, ˚C
54 EVAP_MINUS_PRECIP, // E-P atmospheric freshwater flux, kg s⁻¹ m⁻²
55 // Derived fields, calculated once per timestep
56 ML_BULK_CP, // Mixed layer bulk heat capacity J K⁻¹ m⁻²
57 TF, // Ocean freezing temperature, ˚C
58 WIND_SPEED, // Wind speed, m s⁻¹
59 WIND_U, // wind velocity x component, m s⁻¹
60 WIND_V, // wind velocity y component, m s⁻¹
61 HTRUE_ICE, // Ice thickness, ice average, m
62 HTRUE_SNOW, // Snow thickness, ice average, m
63 OCEAN_U, // x(east)-ward ocean current, m s⁻¹
64 OCEAN_V, // y(north)-ward ocean current, m s⁻¹
65 ICE_U, // x(east)-ward ice velocity, m s⁻¹
66 ICE_V, // y(north)-ward ice velocity, m s⁻¹
67 // Slab ocean fields
68 SLAB_SST, // Slab ocean sea surface temperature, ˚C
69 SLAB_SSS, // Slab ocean sea surface salinity, ˚C
70 SLAB_QDW, // Slab ocean temperature nudging heat flux, W m⁻²
71 SLAB_FDW, // Slab ocean salinity nudging water flux, kg s⁻¹ m⁻²
72 COUNT // Count of enum values
73 };
74
75#ifdef DEBUG_MODELARRAYREF
76 static const size_t SharedArrayOffset = static_cast<size_t>(ProtectedArray::COUNT);
77#else
78 static const size_t SharedArrayOffset = 0;
79#endif
80 enum class SharedArray {
81 // Values of the prognostic fields updated during the timestep
82 H_ICE = SharedArrayOffset, // Updated ice thickness, ice average, m
83 C_ICE, // Updated ice concentration
84 H_SNOW, // Updated snow depth, ice average, m
85 T_ICE, // Updated ice temperatures, ˚C
86 // Heat fluxes
87 Q_IA, // Ice to atmosphere heat flux W m⁻²
88 Q_IC, // Ice conduction heat flux W m⁻²
89 Q_IO, // Ice to ocean heat flux W m⁻²
90 Q_OW, // Open water heat flux W m⁻²
91 DQIA_DT, // Derivative of Qᵢₐ w.r.t. ice surface temperature W m⁻² K⁻¹
92 Q_PEN_SW, // Short-wave flux penetrating the very surface of the ice W m⁻²
93 // Mass fluxes
94 HSNOW_MELT, // Thickness of snow that melted, m
95 // Atmospheric conditions
96 SUBLIM, // Upward sublimation rate kg m⁻² s⁻¹
97 DELTA_HICE, // Change in sea ice thickness, m
98 DELTA_CICE, // Change in sea ice concentration
99 // Ice growth (that is not included above)
100 NEW_ICE, // Volume of new ice formed [m]
101 COUNT // Count of enum values
102 };
103 typedef std::function<void(size_t, const TimestepTime&)> IteratedFn;
104
106 virtual ~ModelComponent() = default;
107
109 virtual std::string getName() const = 0;
110
116 virtual void setData(const ModelState::DataMap& state) = 0;
127 virtual ModelState getState() const = 0;
136 virtual ModelState getState(const OutputLevel&) const = 0;
137
145 virtual ModelState getStateRecursive(const OutputSpec& os) const
146 {
147 return os ? getState() : ModelState();
148 }
149
151 virtual std::unordered_set<std::string> hFields() const { return {}; }
153 virtual std::unordered_set<std::string> uFields() const { return {}; }
155 virtual std::unordered_set<std::string> vFields() const { return {}; }
157 virtual std::unordered_set<std::string> zFields() const { return {}; }
158
159 static void setAllModuleData(const ModelState& stateIn);
160 static ModelState getAllModuleState();
161 static void unregisterAllModules();
162
163 static void getAllFieldNames(std::unordered_set<std::string>& uF,
164 std::unordered_set<std::string>& vF, std::unordered_set<std::string>& zF);
165
170 static void registerExternalSharedArray(SharedArray type, ModelArray* addr)
171 {
172 registerSharedArray(type, addr);
173 }
178 static void registerExternalProtectedArray(ProtectedArray type, ModelArray* addr)
179 {
180 registerProtectedArray(type, addr);
181 }
182
186 static const MARBackingStore& getSharedArray() { return sharedArrays; }
187
191 static const MARConstBackingStore& getProtectedArray() { return protectedArrays; }
192
193protected:
194 void registerModule();
195
202 static void registerSharedArray(SharedArray type, ModelArray* addr);
203
210 static void registerProtectedArray(ProtectedArray type, const ModelArray* addr);
211
212 inline static void overElements(IteratedFn fn, const TimestepTime& tst)
213 {
214 for (size_t i = 0; i < nOcean; ++i) {
215 fn(oceanIndex[i], tst);
216 }
217 }
218
224 static void setOceanMask(const ModelArray& mask);
229 static void noLandMask();
230
236 static ModelArray mask(const ModelArray& data);
237
241 static const ModelArray& oceanMask();
242
243protected:
244 static ModelArray* p_oceanMaskH;
245
246private:
247 static MARBackingStore sharedArrays;
248 static MARConstBackingStore protectedArrays;
249 static std::unordered_map<std::string, ModelComponent*> registeredModules;
250
251 static size_t nOcean;
252 static std::vector<size_t> oceanIndex;
253};
254
255} /* namespace Nextsim */
256
257#endif /* MODELCOMPONENT_HPP */
A class that holds the array data for the model.
virtual std::unordered_set< std::string > vFields() const
Returns the names of all Type::V ModelArrays defined in this component.
virtual std::unordered_set< std::string > zFields() const
Returns the names of all Type::Z ModelArrays defined in this component.
static const ModelArray & oceanMask()
Returns the ocean mask.
virtual ModelState getStateRecursive(const OutputSpec &os) const
Returns the state of the ModelComponent and any ModelComponents it depends on.
virtual std::string getName() const =0
Returns the name of the component.
virtual void setData(const ModelState::DataMap &state)=0
Set the initial data of the component from the passed ModelState.
static void registerProtectedArray(ProtectedArray type, const ModelArray *addr)
virtual ModelState getState() const =0
Returns a ModelState from this component.
static const MARBackingStore & getSharedArray()
Returns a const reference to the store for SharedArray fields.
static const MARConstBackingStore & getProtectedArray()
Returns a const reference to the store for ProtectedArray fields.
static ModelArray mask(const ModelArray &data)
Returns a copy of the provided ModelArray, masked according to the land-ocean mask.
static void setOceanMask(const ModelArray &mask)
Sets the model-wide land-ocean mask (for HField arrays).
static void registerExternalProtectedArray(ProtectedArray type, ModelArray *addr)
Registers a ModelArray into a ProtectedArray slot from outside any ModelComponent object....
virtual std::unordered_set< std::string > uFields() const
Returns the names of all Type::U ModelArrays defined in this component.
static void registerSharedArray(SharedArray type, ModelArray *addr)
virtual std::unordered_set< std::string > hFields() const
Returns the names of all Type::H ModelArrays defined in this component.
static void registerExternalSharedArray(SharedArray type, ModelArray *addr)
Registers a ModelArray into a SharedArray slot from outside any ModelComponent object....
virtual ModelState getState(const OutputLevel &) const =0
Returns a ModelState from this component at a specified level.