11 #ifndef TEST_INTERFACE_STATE_H_
12 #define TEST_INTERFACE_STATE_H_
20 #define ECKIT_TESTING_SELF_REGISTER_CASES 0
22 #include <boost/noncopyable.hpp>
24 #include "eckit/config/LocalConfiguration.h"
25 #include "eckit/testing/Test.h"
31 #include "oops/util/DateTime.h"
32 #include "oops/util/dot_product.h"
33 #include "oops/util/Logger.h"
40 template <
typename MODEL>
class StateFixture :
private boost::noncopyable {
51 return theStateFixture;
61 ~StateFixture<MODEL>() {}
63 std::unique_ptr<const eckit::LocalConfiguration>
test_;
73 const double norm = Test_::test().getDouble(
"norm file");
74 const double tol = Test_::test().getDouble(
"tolerance");
75 const util::DateTime vt(Test_::test().getString(
"date"));
78 const eckit::LocalConfiguration conf(Test_::test(),
"statefile");
79 std::unique_ptr<State_> xx1(
new State_(Test_::resol(), conf));
82 const double norm1 = xx1->norm();
83 EXPECT(oops::is_close(norm1, norm, tol));
84 EXPECT(xx1->validTime() == vt);
87 std::unique_ptr<State_> xx2(
new State_(*xx1));
89 EXPECT(oops::is_close(xx2->norm(), norm, tol));
90 EXPECT(xx2->validTime() == vt);
97 const double norm2 = xx1->norm();
98 EXPECT(norm1 == norm2);
102 State_ xx3(Test_::resol(), vars, vt);
103 EXPECT(xx3.norm() == 0);
104 EXPECT(xx3.validTime() == vt);
105 EXPECT(xx3.variables() == vars);
108 State_ xx4(Test_::resol(), *xx1);
109 EXPECT(oops::is_close(xx4.norm(), norm, tol));
110 EXPECT(xx4.validTime() == vt);
111 EXPECT(xx4.variables() == xx1->variables());
115 State_ xx5(xx1->state());
116 EXPECT(xx5.variables() == xx1->variables());
149 if (!Test_::test().
has(
"state generate")) {
150 oops::Log::warning() <<
"Bypassing Analytical Initial Condition Test";
154 const eckit::LocalConfiguration confgen(Test_::test(),
"state generate");
155 const State_ xx(Test_::resol(), confgen);
156 const double norm = Test_::test().getDouble(
"norm generated state");
157 const double tol = Test_::test().getDouble(
"tolerance");
159 oops::Log::debug() <<
"xx.norm(): " << std::fixed << std::setprecision(8) << xx.norm()
161 oops::Log::debug() <<
"norm: " << std::fixed << std::setprecision(8) << norm << std::endl;
163 EXPECT(oops::is_close(xx.norm(), norm, tol));
182 const eckit::LocalConfiguration conf(Test_::test(),
"statefile");
183 State_ xx(Test_::resol(), conf);
184 const double tol = Test_::test().getDouble(
"tolerance");
188 EXPECT(xx.norm() == 0.0);
191 const State_ yy(Test_::resol(), conf);
192 const std::vector<double> mults {3.0, 0.0, -3.0};
193 for (
const auto & mult : mults) {
195 xx.accumul(mult, yy);
196 EXPECT(oops::is_close(xx.norm(), std::abs(mult) * yy.norm(), tol));
200 State_ zz(Test_::resol(), conf);
202 EXPECT_NOT(oops::is_close(zz.norm(), yy.norm(), tol, oops::TestVerbosity::SILENT));
221 const eckit::LocalConfiguration conf(Test_::test(),
"statefile");
222 State_ xx(Test_::resol(), conf);
225 const util::Duration onehour(3600);
226 xx.updateTime(onehour);
227 xx.updateTime(onehour);
230 State_ yy(Test_::resol(), conf);
233 const util::Duration twohours(7200);
234 yy.updateTime(twohours);
236 EXPECT(xx.validTime() == yy.validTime());
239 xx.updateTime(onehour);
240 EXPECT_NOT(xx.validTime() == yy.validTime());
259 const eckit::LocalConfiguration conf(Test_::test(),
"statefile");
260 State_ xx(Test_::resol(), conf);
261 const double tol = Test_::test().getDouble(
"tolerance");
264 const double norm = xx.norm();
273 EXPECT(xx.norm() == norm);
275 if (Test_::test().
has(
"statefileout")) {
277 const double mult = 2.0;
278 xx.accumul(mult, xx);
281 const double normout = xx.norm();
284 const eckit::LocalConfiguration confout(Test_::test(),
"statefileout");
290 State_ yy(Test_::resol(), confout);
293 EXPECT(oops::is_close(yy.norm(), normout, tol));
296 EXPECT_NOT(oops::is_close(norm, normout, tol, oops::TestVerbosity::SILENT));
302 template <
typename MODEL>
308 std::string
testid()
const override {
return "test::State<" + MODEL::name() +
">";}
311 std::vector<eckit::testing::Test>& ts = eckit::testing::specification();
313 ts.emplace_back(
CASE(
"interface/State/testStateConstructors")
314 { testStateConstructors<MODEL>(); });
315 ts.emplace_back(
CASE(
"interface/State/testStateAnalyticInitialCondition")
316 { testStateAnalyticInitialCondition<MODEL>(); });
317 ts.emplace_back(
CASE(
"interface/State/testStateZeroAndAccumul")
318 { testStateZeroAndAccumul<MODEL>(); });
319 ts.emplace_back(
CASE(
"interface/State/testStateDateTime")
320 { testStateDateTime<MODEL>(); });
321 ts.emplace_back(
CASE(
"interface/State/testStateReadWrite")
322 { testStateReadWrite<MODEL>(); });
332 #endif // TEST_INTERFACE_STATE_H_