netxsimdg
Loading...
Searching...
No Matches
NewModelArrayRef_test.cpp
Go to the documentation of this file.
1
8#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
9#include <doctest/doctest.h>
10
12
13#include <iostream>
14
15namespace Nextsim {
16
18public:
19 enum class ProtectedArray {
20 H_ICE,
21 SW_IN,
22 COUNT
23 };
24 enum class SharedArray {
25 H_ICE,
26 COUNT
27 };
28 static void registerSharedArray(SharedArray type, ModelArray* p)
29 {
30 sharedArrays[static_cast<size_t>(type)] = p;
31 }
32 static void registerProtectedArray(ProtectedArray type, ModelArray* p)
33 {
34 protectedArrays[static_cast<size_t>(type)] = p;
35 }
36 static const MARConstBackingStore& getProtectedArrays() { return protectedArrays; }
37 static const MARBackingStore& getSharedArrays() { return sharedArrays; }
38protected:
39 static MARBackingStore sharedArrays;
40 static MARConstBackingStore protectedArrays;
41};
42
43MARBackingStore MiniModelComponent::sharedArrays(static_cast<size_t>(SharedArray::COUNT));
44MARConstBackingStore MiniModelComponent::protectedArrays(static_cast<size_t>(ProtectedArray::COUNT));
45
46class AtmIn : public MiniModelComponent {
47public:
48 AtmIn()
49 {
50 registerProtectedArray(ProtectedArray::H_ICE, &hice);
51 registerProtectedArray(ProtectedArray::SW_IN, &swin);
52 }
53 void configure()
54 {
55 hice.resize();
56 swin.resize();
57 }
58 void setData(const std::vector<double>& values)
59 {
60 hice = values[0];
61 swin = values[1];
62 }
63private:
64
65 HField hice;
66 HField swin;
67};
68
70public:
71 IceThermo()
72 : hice(MiniModelComponent::getSharedArrays())
73 {
74 }
75
76 void update(int tStep)
77 {
78 hice[0] *= (1. + tStep) / tStep;
79 }
80private:
82};
83
85public:
86 IceCalc()
87 : hice0(MiniModelComponent::getProtectedArrays())
88 {
89 registerSharedArray(SharedArray::H_ICE, &hice);
90 }
91 void configure()
92 {
93 hice.resize();
94 }
95 void update(int tStep)
96 {
97 hice[0] = hice0[0];
98 thermo.update(tStep);
99 }
100 void getData(double& dataOut)
101 {
102 dataOut = hice[0];
103 }
104
105private:
106 HField hice;
108
109 IceThermo thermo;
110};
111
112TEST_SUITE_BEGIN("ModelArrayRef");
113TEST_CASE("Accessing the data")
114{
115 AtmIn atmIn;
116 double hice0 = 0.56;
117 double swin = 311;
118 ModelArray::setDimensions(ModelArray::Type::H, {1,1});
119 atmIn.configure();
120 atmIn.setData({hice0, swin});
121
122 IceCalc iceCalc;
123 iceCalc.configure();
124 int tStep = 40;
125 iceCalc.update(tStep);
126
127 double hicef;
128 iceCalc.getData(hicef);
129 double target = hice0 * (1. + tStep) / tStep;
130 REQUIRE(hicef == doctest::Approx(target).epsilon(1e-8));
131}
132
133enum class couplFields {
134 SWIN,
135 COUNT
136};
137
138static const double targetFlux = 320;
139
141{
142public:
143 CouplEr(MARBackingStore& bs)
144 : swFlux(bs)
145 {
146 }
147 void update() { swFlux[0] = targetFlux; }
148private:
150};
151
153{
154public:
155 CouplIn()
156 : coupledFields(static_cast<size_t>(couplFields::COUNT))
157 , coupler(coupledFields)
158 {
159 registerProtectedArray(ProtectedArray::H_ICE, &hice);
160 registerProtectedArray(ProtectedArray::SW_IN, &swin);
161 // Set the address of the swin array in the local reference backing store
162 coupledFields[static_cast<size_t>(couplFields::SWIN)] = &swin;
163 }
164 void configure()
165 {
166 hice.resize();
167 swin.resize();
168 }
169 void setData()
170 {
171 hice[0] = 0.5;
172 swin[0] = 350;
173 }
174 void update()
175 {
176 coupler.update();
177 }
178 const MARBackingStore& bs() { return coupledFields; }
179private:
180 HField hice;
181 HField swin;
182 MARBackingStore coupledFields;
183 CouplEr coupler;
184 };
185
186TEST_CASE("Accessing the data two ways")
187{
188 CouplIn couplIn;
189 ModelArray::setDimensions(ModelArray::Type::H, {1,1});
190 couplIn.configure();
191 ModelArrayRef<couplFields::SWIN, MARBackingStore> swin(couplIn.bs());
192 couplIn.setData();
193
194 REQUIRE(swin[0] != targetFlux);
195 couplIn.update();
196 REQUIRE(swin[0] == targetFlux);
197}
198TEST_SUITE_END();
199
200};
A class that holds the array data for the model.
static void setDimensions(Type, const MultiDim &)
Sets the number and size of the dimensions of a specified type of ModelArray.
A class which provides indirect access to ModelArray.
const double epsilon
Thermal emissivity of smooth ice [0..1].
Definition constants.hpp:43