netxsimdg
Loading...
Searching...
No Matches
ModelComponent.cpp
Go to the documentation of this file.
1
9
11
12namespace Nextsim {
13
14std::unordered_map<std::string, ModelComponent*> ModelComponent::registeredModules;
15MARBackingStore ModelComponent::sharedArrays(static_cast<size_t>(SharedArray::COUNT));
16MARConstBackingStore ModelComponent::protectedArrays(static_cast<size_t>(ProtectedArray::COUNT));
17ModelArray* ModelComponent::p_oceanMaskH = nullptr;
18size_t ModelComponent::nOcean;
19std::vector<size_t> ModelComponent::oceanIndex;
20
21ModelComponent::ModelComponent() { noLandMask(); }
22
23void ModelComponent::setAllModuleData(const ModelState& stateIn)
24{
25 for (auto entry : registeredModules) {
26 entry.second->setData(stateIn.data);
27 }
28}
29ModelState ModelComponent::getAllModuleState()
30{
31 ModelState overallState;
32 for (auto entry : registeredModules) {
33 overallState.data.merge(entry.second->getState().data);
34 }
35 return overallState;
36}
37
38void ModelComponent::registerModule() { registeredModules[getName()] = this; }
39
40void ModelComponent::unregisterAllModules() { registeredModules.clear(); }
41
42void ModelComponent::getAllFieldNames(std::unordered_set<std::string>& uF,
43 std::unordered_set<std::string>& vF, std::unordered_set<std::string>& zF)
44{
45 for (auto entry : registeredModules) {
46 uF.merge(entry.second->uFields());
47 vF.merge(entry.second->vFields());
48 zF.merge(entry.second->zFields());
49 }
50}
51
53{
54 // Assignment of pointer in array
55 sharedArrays[static_cast<size_t>(type)] = addr;
56}
57
58void ModelComponent::registerProtectedArray(ProtectedArray type, const ModelArray* addr)
59{
60 // Assignment of pointer in array
61 protectedArrays[static_cast<size_t>(type)] = addr;
62}
63
64/*
65 * This assumes that the HField array size has already been set in the restart
66 * reading routine. The mask, like all ModelArrays, is double precision,
67 * where 0 (false) is land, >0 (true) is ocean.
68 */
70{
71 if (p_oceanMaskH)
72 delete p_oceanMaskH;
73 p_oceanMaskH = new ModelArray(ModelArray::Type::H);
74 ModelArray& oceanMaskH = *p_oceanMaskH;
75 oceanMaskH.resize();
76 oceanMaskH = mask;
77
78 // Generate the oceanIndex to grid index mapping
79 // 1. Count the number of non-land squares
80 for (size_t i = 0; i < ModelArray::size(ModelArray::Type::H); ++i) {
81 if (oceanMaskH[i] > 0)
82 ++nOcean;
83 }
84 oceanIndex.resize(nOcean);
85 size_t iOceanIndex = 0;
86 for (size_t i = 0; i < ModelArray::size(ModelArray::Type::H); ++i) {
87 if (oceanMaskH[i] > 0) {
88 oceanIndex[iOceanIndex++] = i;
89 }
90 }
91}
92
93// Fills the nOcean and OceanIndex variables for the zero land case
95{
96 if (p_oceanMaskH)
97 delete p_oceanMaskH;
98 p_oceanMaskH = new ModelArray(ModelArray::Type::H);
99 p_oceanMaskH->resize();
100 *p_oceanMaskH = 1.; // All ocean
101
102 nOcean = ModelArray::size(ModelArray::Type::H);
103 oceanIndex.resize(nOcean);
104 for (size_t i = 0; i < ModelArray::size(ModelArray::Type::H); ++i) {
105 oceanIndex[i] = i;
106 }
107}
108
110{
111 switch (data.getType()) {
112 default: {
113 return ModelArray(data);
114 break;
115 }
116 case (ModelArray::Type::H):
117 case (ModelArray::Type::U):
118 case (ModelArray::Type::V): {
119 return data * oceanMask() + MissingData::value * (1 - oceanMask());
120 break;
121 }
122 case (ModelArray::Type::Z): {
123 ModelArray copy = ModelArray::ZField();
124 copy = MissingData::value;
125 size_t nZ = data.dimensions()[data.nDimensions() - 1];
126 for (size_t iOcean = 0; iOcean < nOcean; ++iOcean) {
127 size_t i = oceanIndex[iOcean];
128 for (size_t k = 0; k < nZ; ++k) {
129 copy.zIndexAndLayer(i, k) = data.zIndexAndLayer(i, k);
130 }
131 }
132 return copy;
133 break;
134 }
135 }
136}
137
138const ModelArray& ModelComponent::oceanMask() { return *p_oceanMaskH; }
139
140} /* namespace Nextsim */
A class that holds the array data for the model.
double & zIndexAndLayer(size_t hIndex, size_t layer)
Special access function for ZFields.
void resize()
Conditionally updates the size of the object data buffer to match the class specification.
size_t size() const
Returns the total number of elements of this type of ModelArray.
static const ModelArray & oceanMask()
Returns the ocean mask.
virtual std::string getName() const =0
Returns the name of the component.
static void registerProtectedArray(ProtectedArray type, const ModelArray *addr)
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 registerSharedArray(SharedArray type, ModelArray *addr)