FV3 Bundle
test/interface/Localization.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_LOCALIZATION_H_
12 #define TEST_INTERFACE_LOCALIZATION_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/noncopyable.hpp>
23 #include <boost/scoped_ptr.hpp>
24 #include <boost/test/unit_test.hpp>
25 
26 #include "eckit/config/LocalConfiguration.h"
27 #include "oops/base/Variables.h"
32 #include "oops/runs/Test.h"
33 #include "oops/util/DateTime.h"
34 #include "test/TestEnvironment.h"
35 
36 namespace test {
37 
38 // -----------------------------------------------------------------------------
39 
40 template <typename MODEL> class LocalizationFixture : private boost::noncopyable {
43 
44  public:
45  static const Geometry_ & resol() {return *getInstance().resol_;}
46  static const oops::Variables & ctlvars() {return *getInstance().ctlvars_;}
47  static const util::DateTime & time() {return *getInstance().time_;}
48  static const Localization_ & localization() {return *getInstance().local_;}
49 
50  private:
52  static LocalizationFixture<MODEL> theLocalizationFixture;
53  return theLocalizationFixture;
54  }
55 
57  const eckit::LocalConfiguration resolConfig(TestEnvironment::config(), "Geometry");
58  resol_.reset(new Geometry_(resolConfig));
59 
60  const eckit::LocalConfiguration varConfig(TestEnvironment::config(), "Variables");
61  ctlvars_.reset(new oops::Variables(varConfig));
62 
63  time_.reset(new util::DateTime(TestEnvironment::config().getString("TestDate")));
64 
65 // Setup the localization matrix
66  oops::instantiateLocalizationFactory<MODEL>();
67  const eckit::LocalConfiguration conf(TestEnvironment::config(), "Localization");
68  local_.reset(new Localization_(*resol_, conf));
69  }
70 
72 
73  boost::scoped_ptr<const Geometry_> resol_;
74  boost::scoped_ptr<const oops::Variables> ctlvars_;
75  boost::scoped_ptr<const util::DateTime> time_;
76  boost::scoped_ptr<Localization_> local_;
77 };
78 
79 // -----------------------------------------------------------------------------
80 
81 template <typename MODEL> void testLocalizationZero() {
82  typedef LocalizationFixture<MODEL> Test_;
83  typedef oops::Increment<MODEL> Increment_;
84 
85  Increment_ dx(Test_::resol(), Test_::ctlvars(), Test_::time());
86 
87  BOOST_CHECK_EQUAL(dx.norm(), 0.0);
88  Test_::localization().multiply(dx);
89  BOOST_CHECK_EQUAL(dx.norm(), 0.0);
90 }
91 
92 // -----------------------------------------------------------------------------
93 
94 template <typename MODEL> void testLocalizationMultiply() {
95  typedef LocalizationFixture<MODEL> Test_;
96  typedef oops::Increment<MODEL> Increment_;
97 
98  Increment_ dx(Test_::resol(), Test_::ctlvars(), Test_::time());
99  dx.random();
100 
101  BOOST_CHECK(dx.norm() > 0.0);
102  Test_::localization().multiply(dx);
103  BOOST_CHECK(dx.norm() > 0.0);
104 }
105 
106 // -----------------------------------------------------------------------------
107 
108 template <typename MODEL> class Localization : public oops::Test {
109  public:
111  virtual ~Localization() {}
112  private:
113  std::string testid() const {return "test::Localization<" + MODEL::name() + ">";}
114 
115  void register_tests() const {
116  boost::unit_test::test_suite * ts = BOOST_TEST_SUITE("interface/Localization");
117 
118  ts->add(BOOST_TEST_CASE(&testLocalizationZero<MODEL>));
119  ts->add(BOOST_TEST_CASE(&testLocalizationMultiply<MODEL>));
120 
121  boost::unit_test::framework::master_test_suite().add(ts);
122  }
123 };
124 
125 // -----------------------------------------------------------------------------
126 
127 } // namespace test
128 
129 #endif // TEST_INTERFACE_LOCALIZATION_H_
static LocalizationFixture< MODEL > & getInstance()
boost::scoped_ptr< const Geometry_ > resol_
static const util::DateTime & time()
Definition: conf.py:1
void testLocalizationZero()
static const oops::Variables & ctlvars()
static const eckit::Configuration & config()
character(len=32) name
boost::scoped_ptr< Localization_ > local_
static const Localization_ & localization()
void testLocalizationMultiply()
Increment Class: Difference between two states.
oops::Localization< MODEL > Localization_
boost::scoped_ptr< const util::DateTime > time_
boost::scoped_ptr< const oops::Variables > ctlvars_
static const Geometry_ & resol()