netxsimdg
Loading...
Searching...
No Matches
ModelComponent_test.cpp
Go to the documentation of this file.
1
8#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
9#include <doctest/doctest.h>
10
13
14#include <stdexcept>
15
16namespace Nextsim {
17
18// (Ab)use the exception mechanism to inform doctest that things are working correctly internally.
19class HappyExcept : public std::runtime_error {
20 using std::runtime_error::runtime_error;
21};
22
23class Module1 : public ModelComponent {
24public:
25 Module1() { registerModule(); }
26 std::string getName() const override { return "Module1"; }
27 void setData(const ModelState::DataMap& st) override
28 {
29 throw(HappyExcept(std::string("setData for ") + getName()));
30 }
31 ModelState getState() const override { return ModelState(); }
32 ModelState getState(const OutputLevel& lvl) const override { return getState(); }
33 std::unordered_set<std::string> uFields() const override { return { "u1" }; }
34 std::unordered_set<std::string> vFields() const override { return { "v1", "v2" }; }
35 std::unordered_set<std::string> zFields() const override { return { "z1", "z2", "z3" }; }
36};
37
38TEST_CASE("Register a new module")
39{
40 Module1 m1;
41 REQUIRE_THROWS_AS(ModelComponent::setAllModuleData(ModelState()), HappyExcept);
42
43 std::unordered_set<std::string> uu;
44 std::unordered_set<std::string> vv;
45 std::unordered_set<std::string> zz;
46
47 ModelComponent::getAllFieldNames(uu, vv, zz);
48 REQUIRE(uu.size() == 1);
49 REQUIRE(vv.size() == 2);
50 REQUIRE(zz.size() == 3);
51
52 ModelComponent::unregisterAllModules();
53}
54
56public:
58 : hice(ModelArray::HField())
59 , cice_ref(getProtectedArray())
60 {
61 registerModule();
62 registerProtectedArray(ProtectedArray::H_ICE, &hice);
63 }
64 void setData(const ModelState::DataMap& ms) override { hice[0] = hiceData; }
65 std::string getName() const override { return "SupplyAndWait"; }
66 ModelState getState() const override
67 {
68 return {{
69 { "hice", hice },
70 }, {}};
71 }
72 ModelState getState(const OutputLevel& lvl) const override { return getState(); }
73
74 const double hiceData = 1.2;
75 double data() { return hice[0]; }
76 double refData() { return cice_ref[0]; }
77
78private:
79 HField hice;
80 ModelArrayRef<ProtectedArray::C_ICE, MARConstBackingStore> cice_ref;
81};
82
84public:
86 : cice(ModelArray::HField())
87 , hice_ref(getProtectedArray())
88 {
89 registerModule();
90 registerProtectedArray(ProtectedArray::C_ICE, &cice);
91 }
92 void setData(const ModelState::DataMap& ms) override { cice[0] = ciceData; }
93 std::string getName() const override { return "SupplyAndWait"; }
94 ModelState getState() const override
95 {
96 return {{
97 { "cice", cice },
98 }, {}};
99 }
100 ModelState getState(const OutputLevel& lvl) const override { return getState(); }
101
102 const double ciceData = 0.6;
103 double data() { return cice[0]; }
104 double refData() { return hice_ref[0]; }
105
106private:
107 HField cice;
108 ModelArrayRef<ProtectedArray::H_ICE, MARConstBackingStore> hice_ref;
109};
110
111TEST_SUITE_BEGIN("ModelComponent");
112TEST_CASE("Test array registration")
113{
114 ModelArray::setDimensions(ModelArray::Type::H, { 1, 1 });
115 ModuleSupplyAndWait saw;
116 ModuleRequestAndSupply ras;
117
118 REQUIRE(ras.data() == saw.refData());
119 REQUIRE(saw.data() == ras.refData());
120}
121
123public:
125 : qic(ModelArray::HField())
126 , qio_ref(getSharedArray())
127 {
128 registerModule();
129 registerSharedArray(SharedArray::Q_IC, &qic);
130 }
131 void setData(const ModelState::DataMap& ms) override { qic[0] = qicData; }
132 std::string getName() const override { return "SemiShared"; }
133 ModelState getState() const override
134 {
135 return {{
136 { "qic", qic },
137 }, {}};
138 }
139 ModelState getState(const OutputLevel& lvl) const override { return getState(); }
140
141 const double qicData = 123;
142 double data() { return qic[0]; }
143 double refData() { return qio_ref[0]; }
144
145private:
146 HField qic;
147 ModelArrayRef<SharedArray::Q_IO, MARBackingStore, RO> qio_ref;
148};
149
151public:
153 : qio(ModelArray::HField())
154 , qic_ref(getSharedArray())
155 {
156 registerModule();
157 registerSharedArray(SharedArray::Q_IO, &qio);
158 }
159 void setData(const ModelState::DataMap& ms) override { qio[0]; }
160 std::string getName() const override { return "Shared"; }
161 ModelState getState() const override
162 {
163 return {{
164 { "qio", qio },
165 }, {}};
166 }
167 ModelState getState(const OutputLevel& lvl) const override { return getState(); }
168
169 const double qioData = 234;
170 const double qicData = 246;
171 double data() { return qio[0]; }
172 double& refData() { return qic_ref[0]; }
173 void setRefData() { qic_ref[0] = qicData; }
174
175private:
176 HField qio;
177 ModelArrayRef<SharedArray::Q_IC, MARBackingStore, RW> qic_ref;
178};
179
180TEST_CASE("Shared and semi-protected arrays")
181{
182 ModelArray::setDimensions(ModelArray::Type::H, { 1, 1 });
183
184 ModuleSemiShared semi;
185 ModuleShared share;
186
187 REQUIRE(share.data() == semi.refData());
188 REQUIRE(semi.data() == share.refData());
189
190 share.refData() = share.qicData;
191
192 REQUIRE(semi.data() == share.qicData);
193}
194TEST_SUITE_END();
195
196} /* namespace Nextsim */
static void setDimensions(Type, const MultiDim &)
Sets the number and size of the dimensions of a specified type of ModelArray.
static void registerProtectedArray(ProtectedArray type, const ModelArray *addr)
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 void registerSharedArray(SharedArray type, ModelArray *addr)
ModelState getState(const OutputLevel &lvl) const override
Returns a ModelState from this component at a specified level.
std::unordered_set< std::string > zFields() const override
Returns the names of all Type::Z ModelArrays defined in this component.
ModelState getState() const override
Returns a ModelState from this component.
std::string getName() const override
Returns the name of the component.
std::unordered_set< std::string > vFields() const override
Returns the names of all Type::V ModelArrays defined in this component.
std::unordered_set< std::string > uFields() const override
Returns the names of all Type::U ModelArrays defined in this component.
void setData(const ModelState::DataMap &st) override
Set the initial data of the component from the passed ModelState.
void setData(const ModelState::DataMap &ms) override
Set the initial data of the component from the passed ModelState.
ModelState getState(const OutputLevel &lvl) const override
Returns a ModelState from this component at a specified level.
ModelState getState() const override
Returns a ModelState from this component.
std::string getName() const override
Returns the name of the component.
std::string getName() const override
Returns the name of the component.
ModelState getState() const override
Returns a ModelState from this component.
ModelState getState(const OutputLevel &lvl) const override
Returns a ModelState from this component at a specified level.
void setData(const ModelState::DataMap &ms) override
Set the initial data of the component from the passed ModelState.
void setData(const ModelState::DataMap &ms) override
Set the initial data of the component from the passed ModelState.
ModelState getState() const override
Returns a ModelState from this component.
std::string getName() const override
Returns the name of the component.
ModelState getState(const OutputLevel &lvl) const override
Returns a ModelState from this component at a specified level.
std::string getName() const override
Returns the name of the component.
void setData(const ModelState::DataMap &ms) override
Set the initial data of the component from the passed ModelState.
ModelState getState() const override
Returns a ModelState from this component.
ModelState getState(const OutputLevel &lvl) const override
Returns a ModelState from this component at a specified level.