OOPS
test/interface/ObsVector.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_OBSVECTOR_H_
12 #define TEST_INTERFACE_OBSVECTOR_H_
13 
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 #define ECKIT_TESTING_SELF_REGISTER_CASES 0
19 
20 #include "eckit/testing/Test.h"
21 #include "oops/base/Variables.h"
23 #include "oops/runs/Test.h"
24 #include "oops/util/dot_product.h"
26 #include "test/TestEnvironment.h"
27 
28 namespace test {
29 
30 // -----------------------------------------------------------------------------
31 
32 template <typename OBS> void testConstructor() {
33  typedef ObsTestsFixture<OBS> Test_;
34  typedef oops::ObsVector<OBS> ObsVector_;
35 
36  for (std::size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
37  std::unique_ptr<ObsVector_> ov(new ObsVector_(Test_::obspace()[jj]));
38  EXPECT(ov.get());
39 
40  ov.reset();
41  EXPECT(!ov.get());
42  }
43 }
44 
45 // -----------------------------------------------------------------------------
46 
47 template <typename OBS> void testCopyConstructor() {
48  typedef ObsTestsFixture<OBS> Test_;
49  typedef oops::ObsVector<OBS> ObsVector_;
50 
51  for (std::size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
52  std::unique_ptr<ObsVector_> ov(new ObsVector_(Test_::obspace()[jj]));
53 
54  ov->random();
55 
56  std::unique_ptr<ObsVector_> other(new ObsVector_(*ov));
57  EXPECT(other.get());
58 
59  const double ov2 = dot_product(*ov, *ov);
60  const double other2 = dot_product(*other, *other);
61 
62  EXPECT(ov2 == other2);
63 
64  other.reset();
65  EXPECT(!other.get());
66 
67  EXPECT(ov.get());
68  }
69 }
70 
71 // -----------------------------------------------------------------------------
72 
73 template <typename OBS> void testNotZero() {
74  typedef ObsTestsFixture<OBS> Test_;
75  typedef oops::ObsVector<OBS> ObsVector_;
76  const double zero = 0.0;
77 
78  for (std::size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
79  ObsVector_ ov1(Test_::obspace()[jj]);
80  ov1.random();
81 
82  const double zz = dot_product(ov1, ov1);
83  EXPECT(zz > zero);
84 
85  ObsVector_ ov2(ov1);
86  ov2.zero();
87 
88  EXPECT(dot_product(ov2, ov1) == zero);
89  EXPECT(dot_product(ov2, ov2) == zero);
90  }
91 }
92 // -----------------------------------------------------------------------------
93 
94 template <typename OBS> void testLinearAlgebra() {
95  typedef ObsTestsFixture<OBS> Test_;
96  typedef oops::ObsVector<OBS> ObsVector_;
97  const double tolerance = 1.0e-8;
98  for (std::size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
99  ObsVector_ ov1(Test_::obspace()[jj]);
100  ov1.random();
101 
102  // test *=, += and -=
103  ObsVector_ ov2(ov1);
104  ov2 += ov1;
105  ov1 *= 2.0;
106  ov2 -= ov1;
107  EXPECT(dot_product(ov2, ov2) < tolerance);
108 
109  // test =
110  ObsVector_ ov3(ov1);
111  ov2 = ov1;
112  ov2 -= ov3;
113  EXPECT(dot_product(ov2, ov2) < tolerance);
114 
115  // test *=(const ObsVector &) and /=(const ObsVector &)
116  ov2 = ov1;
117  ov2 *= ov1;
118  ov2 /= ov1;
119  ov2 -= ov1;
120  EXPECT(dot_product(ov2, ov2) < tolerance);
121 
122  // test axpy
123  ov2 = ov1;
124  ov3 = ov1;
125  ov2.axpy(2.0, ov1);
126  ov3 *= 3;
127  ov2 -= ov3;
128  EXPECT(dot_product(ov2, ov2) < tolerance);
129 
130  // test invert()
131  ov2 = ov1;
132  ov2.invert();
133  ov2 *= ov1;
134  EXPECT(std::abs(dot_product(ov2, ov2) - ov2.nobs()) < tolerance);
135  }
136 }
137 
138 // -----------------------------------------------------------------------------
139 template <typename OBS> void testReadWrite() {
140  typedef ObsTestsFixture<OBS> Test_;
141  typedef oops::ObsVector<OBS> ObsVector_;
142  const double tolerance = 1.0e-8;
143  for (std::size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
144  ObsVector_ ov1(Test_::obspace()[jj]);
145  ov1.random();
146 
147  ov1.save("test");
148  ObsVector_ ov2(Test_::obspace()[jj], "test");
149  ov2 -= ov1;
150  EXPECT(dot_product(ov2, ov2) < tolerance);
151  }
152 }
153 // -----------------------------------------------------------------------------
154 template <typename OBS> void testPackEigen() {
155  typedef ObsTestsFixture<OBS> Test_;
156  typedef oops::ObsVector<OBS> ObsVector_;
157  const double tolerance = 1.0e-8;
158  for (std::size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
159  ObsVector_ ov1(Test_::obspace()[jj]);
160  ov1.random();
161  double rms1 = ov1.rms();
162 
163  Eigen::VectorXd vec = ov1.packEigen();
164  EXPECT(vec.size() == ov1.nobs());
165 
166  double rms2 = sqrt(vec.squaredNorm() / ov1.nobs());
167  EXPECT(std::abs(rms1-rms2) < tolerance);
168  }
169 }
170 // -----------------------------------------------------------------------------
171 
172 template <typename OBS>
173 class ObsVector : public oops::Test {
175 
176  public:
178  virtual ~ObsVector() {}
179 
180  private:
181  std::string testid() const override {return "test::ObsVector<" + OBS::name() + ">";}
182 
183  void register_tests() const override {
184  std::vector<eckit::testing::Test>& ts = eckit::testing::specification();
185 
186  ts.emplace_back(CASE("interface/ObsVector/testConstructor")
187  { testConstructor<OBS>(); });
188  ts.emplace_back(CASE("interface/ObsVector/testCopyConstructor")
189  { testCopyConstructor<OBS>(); });
190  ts.emplace_back(CASE("interface/ObsVector/testNotZero")
191  { testNotZero<OBS>(); });
192  ts.emplace_back(CASE("interface/ObsVector/testLinearAlgebra")
193  { testLinearAlgebra<OBS>(); });
194  ts.emplace_back(CASE("interface/ObsVector/testReadWrite")
195  { testReadWrite<OBS>(); });
196  ts.emplace_back(CASE("interface/ObsVector/testPackEigen")
197  { testPackEigen<OBS>(); });
198  }
199 
200  void clear() const override {
201  Test_::reset();
202  }
203 };
204 
205 // =============================================================================
206 
207 } // namespace test
208 
209 #endif // TEST_INTERFACE_OBSVECTOR_H_
test::ObsTestsFixture
Definition: ObsTestsFixture.h:29
test::testCopyConstructor
void testCopyConstructor()
Definition: test/base/Variables.h:50
test::ObsTestsFixture::reset
static void reset()
Definition: ObsTestsFixture.h:37
test::CASE
CASE("test_linearmodelparameterswrapper_valid_name")
Definition: LinearModelFactory.cc:22
test::ObsVector::ObsVector
ObsVector()
Definition: test/interface/ObsVector.h:177
test
Definition: LinearModelFactory.cc:20
oops::ObsVector
Definition: oops/interface/ObsSpace.h:36
test::testNotZero
void testNotZero()
Definition: test/interface/ObsVector.h:73
test::testLinearAlgebra
void testLinearAlgebra()
Definition: test/interface/ObsVector.h:94
test::ObsVector::~ObsVector
virtual ~ObsVector()
Definition: test/interface/ObsVector.h:178
test::ObsVector::Test_
ObsTestsFixture< OBS > Test_
Definition: test/interface/ObsVector.h:174
Test.h
ObsTestsFixture.h
test::ObsVector
Definition: test/interface/ObsVector.h:173
test::ObsVector::register_tests
void register_tests() const override
Definition: test/interface/ObsVector.h:183
test::testPackEigen
void testPackEigen()
Definition: test/interface/ObsVector.h:154
TestEnvironment.h
test::ObsVector::clear
void clear() const override
Definition: test/interface/ObsVector.h:200
test::testConstructor
void testConstructor()
Tests creation and destruction of ObsErrorCovariances.
Definition: test/base/ObsErrorCovariance.h:32
oops::Test
Definition: Test.h:39
test::ObsVector::testid
std::string testid() const override
Definition: test/interface/ObsVector.h:181
test::testReadWrite
void testReadWrite()
Definition: test/interface/ObsVector.h:139
ObsVector.h
Variables.h