netxsimdg
Loading...
Searching...
No Matches
DevGrid_example.cpp
Go to the documentation of this file.
1
8#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
9#include <doctest/doctest.h>
10
11#include "include/DevGrid.hpp"
12#include "include/DevGridIO.hpp"
14
15#include <fstream>
16
17const std::string filename = "example.nc";
18const std::string stateFilename = "modelState.nc";
19
20namespace Nextsim {
21
22TEST_CASE("Write out a ModelState-based DevGrid restart file")
23{
24 DevGrid grid;
25 grid.setIO(new DevGridIO(grid));
26
27 // Fill in the data. It is not real data.
28 size_t nx = DevGrid::nx;
29 size_t ny = DevGrid::nx;
30 double yFactor = 0.01;
31 double xFactor = 0.0001;
32
33 ModelArray::setDimensions(ModelArray::Type::H, {nx, ny});
34 ModelArray::setDimensions(ModelArray::Type::Z, {nx, ny, 1});
35
36 HField fractional(ModelArray::Type::H, "");
37 for (int j = 0; j < ny; ++j) {
38 for (int i = 0; i < nx; ++i) {
39 fractional(i, j) = j * yFactor + i * xFactor;
40 }
41 }
42
43 HField hice = fractional + 1;
44 HField cice = fractional + 2;
45 HField sst = fractional + 3;
46 HField sss = fractional + 4;
47 HField hsnow = fractional + 5;
48
49 HField ticeValue = -(fractional + 1);
50 ZField tice = ModelArray::ZField("tice");
51 tice.setData(ticeValue);
52
53 ModelState state = {
54 {"hice", hice},
55 {"cice", cice},
56 {"sst", sst},
57 {"sss", sss},
58 {"hsnow", hsnow},
59 {"tice", tice},
60 };
61
62 grid.dumpModelState(state, stateFilename);
63 WARN("File written to " << stateFilename);
64}
65
66TEST_CASE("Read the ModelState example file, if it exists")
67{
68 ModelArray::setDimensions(ModelArray::Type::H, {1, 1});
69 REQUIRE(ModelArray::dimensions(ModelArray::Type::H)[0] == 1);
70 DevGrid grid;
71 size_t targetX = 3;
72 size_t targetY = 7;
73
74 grid.setIO(new DevGridIO(grid));
75 ModelState ms = grid.getModelState(stateFilename);
76
77 REQUIRE(ModelArray::dimensions(ModelArray::Type::H)[0] == DevGrid::nx);
78 REQUIRE(ModelArray::dimensions(ModelArray::Type::H)[1] == DevGrid::nx);
79 REQUIRE(ms.at("hice")(targetX, targetY) != 0);
80 REQUIRE(ms.at("hice")(targetX, targetY) > 1);
81 REQUIRE(ms.at("hice")(targetX, targetY) < 2);
82 REQUIRE(ms.at("hice")(targetX, targetY) == 1.0703);
83}
84}
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.