FV3 Bundle
test/interface/Model.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2009-2016 ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation nor
8  * does it submit to any jurisdiction.
9  */
10 
11 #ifndef TEST_INTERFACE_MODEL_H_
12 #define TEST_INTERFACE_MODEL_H_
13 
14 #include <cmath>
15 #include <iostream>
16 #include <string>
17 
18 #define BOOST_TEST_NO_MAIN
19 #define BOOST_TEST_ALTERNATIVE_INIT_API
20 #define BOOST_TEST_DYN_LINK
21 
22 #include <boost/test/unit_test.hpp>
23 
24 #include <boost/noncopyable.hpp>
25 #include <boost/scoped_ptr.hpp>
26 
27 #include "eckit/config/LocalConfiguration.h"
30 #include "oops/interface/Model.h"
32 #include "oops/interface/State.h"
33 #include "oops/runs/Test.h"
34 #include "oops/util/DateTime.h"
35 #include "oops/util/Duration.h"
36 #include "test/TestEnvironment.h"
37 
38 namespace test {
39 
40 // =============================================================================
41 
42 template <typename MODEL> class ModelFixture : private boost::noncopyable {
47 
48  public:
49  static const eckit::Configuration & test() {return *getInstance().test_;}
50  static const Geometry_ & resol() {return *getInstance().resol_;}
51  static const State_ & xref() {return *getInstance().xref_;}
52  static const ModelAux_ & bias() {return *getInstance().bias_;}
53  static const Model_ & model() {return *getInstance().model_;}
54 
55  private:
57  static ModelFixture<MODEL> theModelFixture;
58  return theModelFixture;
59  }
60 
62  test_.reset(new eckit::LocalConfiguration(TestEnvironment::config(), "ModelTest"));
63 
64  const eckit::LocalConfiguration resolConfig(TestEnvironment::config(), "Geometry");
65  resol_.reset(new Geometry_(resolConfig));
66 
67  const eckit::LocalConfiguration biasConf(TestEnvironment::config(), "ModelBias");
68  bias_.reset(new ModelAux_(*resol_, biasConf));
69 
70  const eckit::LocalConfiguration conf(TestEnvironment::config(), "Model");
71  model_.reset(new Model_(*resol_, conf));
72 
73  const eckit::LocalConfiguration iniConf(TestEnvironment::config(), "State");
74  xref_.reset(new State_(*resol_, model_->variables(), iniConf));
75  }
76 
78 
79  boost::scoped_ptr<const eckit::LocalConfiguration> test_;
80  boost::scoped_ptr<const Geometry_> resol_;
81  boost::scoped_ptr<const State_> xref_;
82  boost::scoped_ptr<const ModelAux_> bias_;
83  boost::scoped_ptr<const Model_> model_;
84 };
85 
86 // =============================================================================
87 
88 template <typename MODEL> void testModelConstructor() {
89  typedef ModelFixture<MODEL> Test_;
90 
91  const util::Duration zero(0);
92  BOOST_CHECK(Test_::model().timeResolution() > zero);
93 }
94 
95 // -----------------------------------------------------------------------------
96 
97 template <typename MODEL> void testModelNoForecast() {
98  typedef ModelFixture<MODEL> Test_;
99  typedef oops::State<MODEL> State_;
100 
101  const double ininorm = Test_::xref().norm();
102  State_ xx(Test_::xref());
103  const util::DateTime vt(xx.validTime());
104 
105  const util::Duration zero(0);
107 
108  Test_::model().forecast(xx, Test_::bias(), zero, post);
109 
110  BOOST_CHECK_EQUAL(xx.validTime(), vt);
111  BOOST_CHECK_EQUAL(xx.norm(), ininorm);
112 
113 // Recomputing initial norm to make sure nothing bad happened
114  BOOST_CHECK_EQUAL(Test_::xref().norm(), ininorm);
115 }
116 
117 // -----------------------------------------------------------------------------
118 
119 template <typename MODEL> void testModelForecast() {
120  typedef ModelFixture<MODEL> Test_;
121  typedef oops::State<MODEL> State_;
122 
123  const double fnorm = Test_::test().getDouble("finalnorm");
124  const double tol = Test_::test().getDouble("tolerance");
125  const util::Duration len(Test_::test().getString("fclength"));
126 
127  const double ininorm = Test_::xref().norm();
128  State_ xx(Test_::xref());
129  const util::DateTime vt(xx.validTime()+len);
130 
132 
133  Test_::model().forecast(xx, Test_::bias(), len, post);
134 
135  BOOST_CHECK_EQUAL(xx.validTime(), vt);
136  BOOST_CHECK_CLOSE(xx.norm(), fnorm, tol);
137 
138 // Recomputing initial norm to make sure nothing bad happened
139  BOOST_CHECK_EQUAL(Test_::xref().norm(), ininorm);
140 }
141 
142 // =============================================================================
143 
144 template <typename MODEL> class Model : public oops::Test {
145  public:
146  Model() {}
147  virtual ~Model() {}
148  private:
149  std::string testid() const {return "test::Model<" + MODEL::name() + ">";}
150 
151  void register_tests() const {
152  boost::unit_test::test_suite * ts = BOOST_TEST_SUITE("interface/Model");
153 
154  ts->add(BOOST_TEST_CASE(&testModelConstructor<MODEL>));
155  ts->add(BOOST_TEST_CASE(&testModelNoForecast<MODEL>));
156  ts->add(BOOST_TEST_CASE(&testModelForecast<MODEL>));
157 
158  boost::unit_test::framework::master_test_suite().add(ts);
159  }
160 };
161 
162 // =============================================================================
163 
164 } // namespace test
165 
166 #endif // TEST_INTERFACE_MODEL_H_
void register_tests() const
oops::Geometry< MODEL > Geometry_
static const Geometry_ & resol()
static const eckit::Configuration & test()
Definition: conf.py:1
boost::scoped_ptr< const Geometry_ > resol_
Encapsulates the model state.
static const eckit::Configuration & config()
program test
double norm() const
character(len=32) name
oops::ModelAuxControl< MODEL > ModelAux_
real(double), parameter zero
boost::scoped_ptr< const Model_ > model_
real, dimension(:,:,:), allocatable vt
oops::Model< MODEL > Model_
boost::scoped_ptr< const eckit::LocalConfiguration > test_
boost::scoped_ptr< const State_ > xref_
Encapsulates the nonlinear forecast model.
static ModelFixture< MODEL > & getInstance()
std::string testid() const
void testModelForecast()
static const Model_ & model()
static const ModelAux_ & bias()
oops::State< MODEL > State_
boost::scoped_ptr< const ModelAux_ > bias_
Control model post processing.
Definition: PostProcessor.h:31
static const State_ & xref()
void testModelConstructor()
void testModelNoForecast()