netxsimdg
Loading...
Searching...
No Matches
ModelArrayRefDebug_test.cpp
Go to the documentation of this file.
1
8#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
9#include <doctest/doctest.h>
10
12
13namespace Nextsim {
14
15class MiniModelComponent {
16public:
17 enum class ProtectedArray {
18 H_ICE,
19 SW_IN,
20 COUNT
21 };
22 enum class SharedArray {
23 H_ICE,
24 COUNT
25 };
26 static void registerSharedArray(SharedArray type, ModelArray* p)
27 {
28 sharedArrays[static_cast<size_t>(type)] = p;
29 }
30 static void registerProtectedArray(ProtectedArray type, ModelArray* p)
31 {
32 protectedArrays[static_cast<size_t>(type)] = p;
33 }
34 static const MARConstBackingStore& getProtectedArrays() { return protectedArrays; }
35 static const MARBackingStore& getSharedArrays() { return sharedArrays; }
36protected:
37 static MARBackingStore sharedArrays;
38 static MARConstBackingStore protectedArrays;
39};
40
41MARBackingStore MiniModelComponent::sharedArrays(static_cast<size_t>(SharedArray::COUNT));
42MARConstBackingStore MiniModelComponent::protectedArrays(static_cast<size_t>(ProtectedArray::COUNT));
43
44class IceThermo : public MiniModelComponent {
45public:
46 IceThermo()
47 : hice(MiniModelComponent::getSharedArrays())
48 {
49 }
50
51 void update(int tStep)
52 {
53 hice[0] *= (1. + tStep) / tStep;
54 }
55private:
56 ModelArrayRef<SharedArray::H_ICE, MARBackingStore, RW> hice;
57};
58
59class IceCalc : public MiniModelComponent {
60public:
61 IceCalc()
62 : hice0(MiniModelComponent::getProtectedArrays())
63 {
64 registerSharedArray(SharedArray::H_ICE, &hice);
65 }
66 void configure()
67 {
68 hice.resize();
69 }
70 void update(int tStep)
71 {
72 hice[0] = hice0[0];
73 thermo.update(tStep);
74 }
75 void getData(double& dataOut)
76 {
77 dataOut = hice[0];
78 }
79
80private:
81 HField hice;
82 ModelArrayRef<ProtectedArray::H_ICE, MARConstBackingStore> hice0;
83
84 IceThermo thermo;
85};
86
87TEST_SUITE_BEGIN("ModelArrayRefDebug");
88TEST_CASE("No registered array")
89{
90 ModelArray::setDimensions(ModelArray::Type::H, {1,1});
91
92 IceThermo iceThermo;
93 REQUIRE_THROWS_AS(iceThermo.update(1), std::invalid_argument);
94}
95
96TEST_CASE("Correct access")
97{
98 ModelArray::setDimensions(ModelArray::Type::H, {1,1});
99
100 IceThermo iceThermo;
101 IceCalc iceCalc;
102 iceCalc.configure();
103 REQUIRE_NOTHROW(iceThermo.update(1));
104}
105TEST_SUITE_END();
106}
static void setDimensions(Type, const MultiDim &)
Sets the number and size of the dimensions of a specified type of ModelArray.