OOPS
test/interface/GeoVaLs.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2017-2018 UCAR
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  */
7 
8 #ifndef TEST_INTERFACE_GEOVALS_H_
9 #define TEST_INTERFACE_GEOVALS_H_
10 
11 #include <cmath>
12 #include <memory>
13 #include <string>
14 #include <vector>
15 
16 #define ECKIT_TESTING_SELF_REGISTER_CASES 0
17 
18 #include <boost/noncopyable.hpp>
19 
20 #include "eckit/config/LocalConfiguration.h"
21 #include "eckit/testing/Test.h"
22 #include "oops/base/ObsSpaces.h"
23 #include "oops/base/Variables.h"
24 #include "oops/interface/GeoVaLs.h"
25 #include "oops/mpi/mpi.h"
26 #include "oops/runs/Test.h"
27 #include "oops/util/dot_product.h"
28 #include "oops/util/Logger.h"
29 #include "test/TestEnvironment.h"
30 
31 namespace test {
32 
33 // -----------------------------------------------------------------------------
34 
35 template <typename OBS>
36 class GeoVaLsFixture : private boost::noncopyable {
38 
39  public:
40  static ObsSpaces_ & obspace() {return *getInstance().ospaces_;}
41  static eckit::LocalConfiguration conf(const size_t ii) {return getInstance().confs_[ii];}
42  static void reset() {getInstance().ospaces_.reset();}
43 
44  private:
46  static GeoVaLsFixture<OBS> theGeoVaLsFixture;
47  return theGeoVaLsFixture;
48  }
49 
51  const util::DateTime tbgn(TestEnvironment::config().getString("window begin"));
52  const util::DateTime tend(TestEnvironment::config().getString("window end"));
53 
54  ospaces_.reset(new ObsSpaces_(TestEnvironment::config(), oops::mpi::world(), tbgn, tend));
55  TestEnvironment::config().get("observations", confs_);
56  }
57 
59 
60  std::unique_ptr<ObsSpaces_> ospaces_;
61  std::vector<eckit::LocalConfiguration> confs_;
62 };
63 
64 // -----------------------------------------------------------------------------
65 
66 template <typename OBS> void testConstructor() {
67  typedef GeoVaLsFixture<OBS> Test_;
68  typedef oops::GeoVaLs<OBS> GeoVaLs_;
69 
70  for (std::size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
71  eckit::LocalConfiguration gconf(Test_::conf(jj), "geovals");
72  oops::Variables geovars(gconf, "state variables");
73  std::unique_ptr<GeoVaLs_> ov(new GeoVaLs_(gconf, Test_::obspace()[jj], geovars));
74  EXPECT(ov.get());
75 
76  ov.reset();
77  EXPECT(!ov.get());
78  }
79 }
80 
81 // -----------------------------------------------------------------------------
82 
83 template <typename OBS> void testUtils() {
84  typedef GeoVaLsFixture<OBS> Test_;
85  typedef oops::GeoVaLs<OBS> GeoVaLs_;
86 
87  const double tol = 1e-6;
88 
89  for (std::size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
90  eckit::LocalConfiguration gconf(Test_::conf(jj), "geovals");
91  oops::Variables geovars(gconf, "state variables");
92  GeoVaLs_ gval(gconf, Test_::obspace()[jj], geovars);
93 
94  const double zz = dot_product(gval, gval);
95 
96  oops::Log::trace() << "Testing copy constructor (=) " << std::endl;
97 
98  GeoVaLs_ gval2 = gval;
99 
100  double zz0 = dot_product(gval2, gval2);
101 
102  EXPECT(zz0 == zz);
103 
104  oops::Log::trace() << "Testing *= double" << std::endl;
105 
106  gval2 *= 2.0;
107 
108  const double zz1 = dot_product(gval2, gval2);
109 
110  EXPECT(zz1/zz - 4.0 < tol);
111 
112  oops::Log::trace() << "Testing += GeoVals" << std::endl;
113 
114  gval2 += gval;
115 
116  const double zz2 = dot_product(gval2, gval2);
117 
118  EXPECT(zz2/zz - 9.0 < tol);
119 
120  oops::Log::trace() << "Testing -= GeoVals" << std::endl;
121 
122  gval2 -= gval;
123 
124  const double zz3 = dot_product(gval2, gval2);
125 
126  EXPECT(zz3/zz - 4.0 < tol);
127 
128  oops::Log::trace() << "Testing *= GeoVals" << std::endl;
129 
130  gval2 = gval;
131 
132  gval2 *= gval;
133 
134  GeoVaLs_ gval3 = gval2;
135 
136  gval3 *= gval;
137 
138  const double zz4 = dot_product(gval2, gval2);
139 
140  const double zz5 = dot_product(gval3, gval);
141 
142  EXPECT(zz4/zz5 - 1.0 < tol);
143 
144  oops::Log::trace() << "Testing random" << std::endl;
145 
146  gval.random();
147  const double zz6 = dot_product(gval, gval);
148  EXPECT(zz6 > 0.0);
149 
150  oops::Log::trace() << "Testing zero" << std::endl;
151 
152  gval.zero();
153  const double zz7 = dot_product(gval, gval);
154  EXPECT(zz7 == 0.0);
155  }
156 }
157 
158 // -----------------------------------------------------------------------------
159 
160 template <typename OBS> void testRead() {
161  typedef GeoVaLsFixture<OBS> Test_;
162  typedef oops::GeoVaLs<OBS> GeoVaLs_;
163 
164  const double tol = 1.0e-9;
165  for (std::size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
166  eckit::LocalConfiguration gconf(Test_::conf(jj), "geovals");
167  oops::Variables geovars(gconf, "state variables");
168  GeoVaLs_ gval(gconf, Test_::obspace()[jj], geovars);
169 
170  const double xx = gconf.getDouble("norm");
171  const double zz = sqrt(dot_product(gval, gval));
172 
173  oops::Log::debug() << "xx: " << std::fixed << std::setprecision(8) << xx << std::endl;
174  oops::Log::debug() << "zz: " << std::fixed << std::setprecision(8) << zz << std::endl;
175 
176  EXPECT(oops::is_close(xx, zz, tol));
177  }
178 }
179 
180 // -----------------------------------------------------------------------------
181 
182 template <typename OBS>
183 class GeoVaLs : public oops::Test {
185  public:
186  GeoVaLs() {}
187  virtual ~GeoVaLs() {}
188  private:
189  std::string testid() const override {return "test::GeoVaLs<" + OBS::name() + ">";}
190 
191  void register_tests() const override {
192  std::vector<eckit::testing::Test>& ts = eckit::testing::specification();
193 
194  ts.emplace_back(CASE("interface/GeoVaLs/testConstructor")
195  { testConstructor<OBS>(); });
196  ts.emplace_back(CASE("interface/GeoVaLs/testUtils")
197  { testUtils<OBS>(); });
198  ts.emplace_back(CASE("interface/GeoVaLs/testRead")
199  { testRead<OBS>(); });
200  }
201 
202  void clear() const override {
203  Test_::reset();
204  }
205 };
206 
207 // =============================================================================
208 
209 } // namespace test
210 
211 #endif // TEST_INTERFACE_GEOVALS_H_
test::GeoVaLs::~GeoVaLs
virtual ~GeoVaLs()
Definition: test/interface/GeoVaLs.h:187
test::GeoVaLsFixture::confs_
std::vector< eckit::LocalConfiguration > confs_
Definition: test/interface/GeoVaLs.h:61
test::GeoVaLsFixture::GeoVaLsFixture
GeoVaLsFixture()
Definition: test/interface/GeoVaLs.h:50
test::GeoVaLsFixture::getInstance
static GeoVaLsFixture< OBS > & getInstance()
Definition: test/interface/GeoVaLs.h:45
test::testUtils
void testUtils()
Definition: test/interface/GeoVaLs.h:83
ObsSpaces.h
test::GeoVaLs::clear
void clear() const override
Definition: test/interface/GeoVaLs.h:202
mpi.h
test::GeoVaLsFixture::~GeoVaLsFixture
~GeoVaLsFixture()
Definition: test/interface/GeoVaLs.h:58
test::CASE
CASE("test_linearmodelparameterswrapper_valid_name")
Definition: LinearModelFactory.cc:22
test
Definition: LinearModelFactory.cc:20
test::GeoVaLs::testid
std::string testid() const override
Definition: test/interface/GeoVaLs.h:189
test::testRead
void testRead()
Definition: test/interface/GeoVaLs.h:160
Test.h
test::GeoVaLsFixture::reset
static void reset()
Definition: test/interface/GeoVaLs.h:42
test::TestEnvironment::config
static const eckit::Configuration & config()
Definition: TestEnvironment.h:40
test::GeoVaLs::register_tests
void register_tests() const override
Definition: test/interface/GeoVaLs.h:191
test::GeoVaLsFixture::ObsSpaces_
oops::ObsSpaces< OBS > ObsSpaces_
Definition: test/interface/GeoVaLs.h:37
TestEnvironment.h
test::GeoVaLs::GeoVaLs
GeoVaLs()
Definition: test/interface/GeoVaLs.h:186
test::GeoVaLs::Test_
GeoVaLsFixture< OBS > Test_
Definition: test/interface/GeoVaLs.h:184
test::testConstructor
void testConstructor()
Tests creation and destruction of ObsErrorCovariances.
Definition: test/base/ObsErrorCovariance.h:32
oops::Test
Definition: Test.h:39
oops::mpi::world
const eckit::mpi::Comm & world()
Default communicator with all MPI tasks (ie MPI_COMM_WORLD)
Definition: oops/mpi/mpi.cc:22
oops::Variables
Definition: oops/base/Variables.h:23
test::GeoVaLsFixture::ospaces_
std::unique_ptr< ObsSpaces_ > ospaces_
Definition: test/interface/GeoVaLs.h:60
oops::ObsSpaces
Definition: ObsSpaces.h:41
test::GeoVaLsFixture
Definition: test/interface/GeoVaLs.h:36
GeoVaLs.h
test::GeoVaLsFixture::conf
static eckit::LocalConfiguration conf(const size_t ii)
Definition: test/interface/GeoVaLs.h:41
oops::GeoVaLs
Definition: oops/interface/GeoVaLs.h:32
test::GeoVaLsFixture::obspace
static ObsSpaces_ & obspace()
Definition: test/interface/GeoVaLs.h:40
test::GeoVaLs
Definition: test/interface/GeoVaLs.h:183
Variables.h