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