netxsimdg
Loading...
Searching...
No Matches
DevGrid_test.cpp
1
8#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
9#include <doctest/doctest.h>
10
12#include "include/DevGrid.hpp"
13#include "include/DevGridIO.hpp"
15#include "include/NZLevels.hpp"
16
17#include <cstdio>
18#include <fstream>
19
20namespace Nextsim {
21
22const std::string stateFilename = "modelState.test.nc";
23
24TEST_SUITE_BEGIN("DevGrid");
25TEST_CASE("Write and read a ModelState-based DevGrid restart file")
26{
27 DevGrid grid;
28 grid.setIO(new DevGridIO(grid));
29
30 // Fill in the data. It is not real data.
31 size_t nx = DevGrid::nx;
32 size_t ny = DevGrid::nx;
33 double yFactor = 0.01;
34 double xFactor = 0.0001;
35
36 NZLevels::set(1);
37 ModelArray::setDimensions(ModelArray::Type::H, { nx, ny });
38 ModelArray::setDimensions(ModelArray::Type::Z, { nx, ny, NZLevels::get() });
39
40 HField fractional(ModelArray::Type::H);
41 HField mask(ModelArray::Type::H);
42 for (int j = 0; j < ny; ++j) {
43 for (int i = 0; i < nx; ++i) {
44 fractional(i, j) = j * yFactor + i * xFactor;
45 mask(i, j) = (i + j) % 2;
46 }
47 }
48
49 HField hice = fractional + 1;
50 HField cice = fractional + 2;
51 HField sst = fractional + 3;
52 HField sss = fractional + 4;
53 HField hsnow = fractional + 5;
54
55 HField ticeValue = -(fractional + 1);
56 ZField tice = ModelArray::ZField();
57 tice.setData(ticeValue);
58
59 ModelState state = {{
60 { "mask", mask },
61 { "hice", hice },
62 { "cice", cice },
63 { "sst", sst },
64 { "sss", sss },
65 { "hsnow", hsnow },
66 { "tice", tice },
67 }, {}};
68
69 ModelMetadata metadata;
70 metadata.setTime(TimePoint("2000-01-01T00:00:00Z"));
71
72 grid.dumpModelState(state, metadata, stateFilename);
73
74 ModelArray::setDimensions(ModelArray::Type::H, { 1, 1 });
75 REQUIRE(ModelArray::dimensions(ModelArray::Type::H)[0] == 1);
76 DevGrid gridIn;
77 size_t targetX = 3;
78 size_t targetY = 7;
79
80 gridIn.setIO(new DevGridIO(grid));
81 ModelState ms = gridIn.getModelState(stateFilename);
82
83 REQUIRE(ModelArray::dimensions(ModelArray::Type::H)[0] == DevGrid::nx);
84 REQUIRE(ModelArray::dimensions(ModelArray::Type::H)[1] == DevGrid::nx);
85 REQUIRE(ms.data.at("hice")(targetX, targetY) != 0);
86 REQUIRE(ms.data.at("hice")(targetX, targetY) > 1);
87 REQUIRE(ms.data.at("hice")(targetX, targetY) < 2);
88 REQUIRE(ms.data.at("hice")(targetX, targetY) == 1.0703);
89
90 ZField ticeIn = ms.data.at("tice");
91
92 REQUIRE(ticeIn.dimensions()[2] == 1);
93 REQUIRE(ticeIn(targetX, targetY, 0U) == -1.0703);
94
95 std::remove(stateFilename.c_str());
96}
97TEST_SUITE_END();
98
99}
const MultiDim & dimensions() const
Returns a vector<size_t> of the size of each dimension of this type of ModelArray.
static void setDimensions(Type, const MultiDim &)
Sets the number and size of the dimensions of a specified type of ModelArray.