OOPS
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 <memory>
17 #include <string>
18 #include <vector>
19 
20 #define ECKIT_TESTING_SELF_REGISTER_CASES 0
21 
22 #include <boost/noncopyable.hpp>
23 
24 #include "eckit/config/LocalConfiguration.h"
25 #include "eckit/testing/Test.h"
26 #include "oops/base/Geometry.h"
27 #include "oops/base/Increment.h"
29 #include "oops/base/Localization.h"
30 #include "oops/base/Variables.h"
31 #include "oops/mpi/mpi.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 {
44 
45  public:
46  static const Geometry_ & resol() {return *getInstance().resol_;}
47  static const oops::Variables & ctlvars() {return *getInstance().ctlvars_;}
48  static const util::DateTime & time() {return *getInstance().time_;}
49  static const Localization_ & localization() {return *getInstance().local_;}
50 
51  private:
53  static LocalizationFixture<MODEL> theLocalizationFixture;
54  return theLocalizationFixture;
55  }
56 
58  const eckit::LocalConfiguration resolConfig(TestEnvironment::config(), "geometry");
59  resol_.reset(new Geometry_(resolConfig, oops::mpi::world()));
60 
61  ctlvars_.reset(new oops::Variables(TestEnvironment::config(), "loc variables"));
62 
63  time_.reset(new util::DateTime(TestEnvironment::config().getString("test date")));
64 
65 // Setup the localization matrix
66  const eckit::LocalConfiguration conf(TestEnvironment::config(), "localization");
67  local_.reset(new Localization_(*resol_, conf));
68 
69  oops::Log::test() << "Testing localization: " << *local_ << std::endl;
70  }
71 
72  ~LocalizationFixture<MODEL>() {}
73 
74  std::unique_ptr<const Geometry_> resol_;
75  std::unique_ptr<const oops::Variables> ctlvars_;
76  std::unique_ptr<const util::DateTime> time_;
77  std::unique_ptr<Localization_> local_;
78 };
79 
80 // -----------------------------------------------------------------------------
81 
82 template <typename MODEL> void testLocalizationRandomize() {
83  typedef LocalizationFixture<MODEL> Test_;
84  typedef oops::Increment<MODEL> Increment_;
85 
86  Increment_ dx(Test_::resol(), Test_::ctlvars(), Test_::time());
87 
88  Test_::localization().randomize(dx);
89  EXPECT(dx.norm() > 0.0);
90 }
91 
92 // -----------------------------------------------------------------------------
93 
94 template <typename MODEL> void testLocalizationZero() {
95  typedef LocalizationFixture<MODEL> Test_;
96  typedef oops::Increment<MODEL> Increment_;
97 
98  Increment_ dx(Test_::resol(), Test_::ctlvars(), Test_::time());
99 
100  EXPECT(dx.norm() == 0.0);
101  Test_::localization().multiply(dx);
102  EXPECT(dx.norm() == 0.0);
103 }
104 
105 // -----------------------------------------------------------------------------
106 
107 template <typename MODEL> void testLocalizationMultiply() {
108  typedef LocalizationFixture<MODEL> Test_;
109  typedef oops::Increment<MODEL> Increment_;
110 
111  Increment_ dx(Test_::resol(), Test_::ctlvars(), Test_::time());
112  dx.random();
113 
114  EXPECT(dx.norm() > 0.0);
115  Test_::localization().multiply(dx);
116  EXPECT(dx.norm() > 0.0);
117 }
118 
119 // -----------------------------------------------------------------------------
120 
121 template <typename MODEL> class Localization : public oops::Test {
122  public:
124  virtual ~Localization() {}
125  private:
126  std::string testid() const override {return "test::Localization<" + MODEL::name() + ">";}
127 
128  void register_tests() const override {
129  std::vector<eckit::testing::Test>& ts = eckit::testing::specification();
130 
131  ts.emplace_back(CASE("interface/Localization/testLocalizationRandomize")
132  { testLocalizationRandomize<MODEL>(); });
133  ts.emplace_back(CASE("interface/Localization/testLocalizationZero")
134  { testLocalizationZero<MODEL>(); });
135  ts.emplace_back(CASE("interface/Localization/testLocalizationMultiply")
136  { testLocalizationMultiply<MODEL>(); });
137  }
138 
139  void clear() const override {}
140 };
141 
142 // -----------------------------------------------------------------------------
143 
144 } // namespace test
145 
146 #endif // TEST_INTERFACE_LOCALIZATION_H_
Geometry class used in oops; subclass of interface class interface::Geometry.
Ensemble of inrements.
Increment class used in oops.
Abstract model-space localization class used by high level algorithms and applications.
static const oops::Variables & ctlvars()
oops::Localization< MODEL > Localization_
static const Localization_ & localization()
std::unique_ptr< const oops::Variables > ctlvars_
oops::IncrementEnsemble< MODEL > Ensemble_
std::unique_ptr< Localization_ > local_
std::unique_ptr< const util::DateTime > time_
std::unique_ptr< const Geometry_ > resol_
static const Geometry_ & resol()
static LocalizationFixture< MODEL > & getInstance()
static const util::DateTime & time()
void clear() const override
void register_tests() const override
std::string testid() const override
static const eckit::Configuration & config()
const eckit::mpi::Comm & world()
Default communicator with all MPI tasks (ie MPI_COMM_WORLD)
Definition: oops/mpi/mpi.cc:84
void testLocalizationRandomize()
void testLocalizationMultiply()
void testLocalizationZero()
CASE("test_linearmodelparameterswrapper_valid_name")