OOPS
test/base/ObsErrorCovariance.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_BASE_OBSERRORCOVARIANCE_H_
12 #define TEST_BASE_OBSERRORCOVARIANCE_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/ObsErrorBase.h"
24 #include "oops/runs/Test.h"
26 #include "test/TestEnvironment.h"
27 
28 namespace test {
29 
30 // -----------------------------------------------------------------------------
31 /// Tests creation and destruction of ObsErrorCovariances
32 template <typename OBS> void testConstructor() {
33  typedef ObsTestsFixture<OBS> Test_;
34  typedef oops::ObsErrorBase<OBS> Covar_;
35  typedef oops::ObsVector<OBS> ObsVector_;
36 
37  oops::instantiateObsErrorFactory<OBS>();
38 
39  std::vector<eckit::LocalConfiguration> conf;
40  TestEnvironment::config().get("observations", conf);
41 
42  for (std::size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
43  ObsVector_ obserr(Test_::obspace()[jj], "ObsError");
44  obserr.save("EffectiveError");
45 
46  const eckit::LocalConfiguration rconf(conf[jj], "obs error");
47  std::unique_ptr<Covar_> R(
48  oops::ObsErrorFactory<OBS>::create(rconf, Test_::obspace()[jj]));
49  EXPECT(R.get());
50 
51  R.reset();
52  EXPECT(!R.get());
53  }
54 }
55 
56 // -----------------------------------------------------------------------------
57 /// Tests that \f$R*R^{-1}*dy = dy\f$ and \f$R^{-1}*R*dy = dy\f$
58 template <typename OBS> void testMultiplies() {
59  typedef ObsTestsFixture<OBS> Test_;
60  typedef oops::ObsErrorBase<OBS> Covar_;
61  typedef oops::ObsVector<OBS> ObsVector_;
62 
63  oops::instantiateObsErrorFactory<OBS>();
64 
65  std::vector<eckit::LocalConfiguration> conf;
66  TestEnvironment::config().get("observations", conf);
67 
68  for (std::size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
69  ObsVector_ obserr(Test_::obspace()[jj], "ObsError");
70  obserr.save("EffectiveError");
71 
72  const eckit::LocalConfiguration rconf(conf[jj], "obs error");
73  std::unique_ptr<Covar_> R(
74  oops::ObsErrorFactory<OBS>::create(rconf, Test_::obspace()[jj]));
75 
76  // RMSE should be equal to the rms that was read from the file
77  EXPECT(oops::is_close(R->getRMSE(), obserr.rms(), 1.e-10));
78 
79  // create random vector dy and its copies dy1, dy2
80  ObsVector_ dy(Test_::obspace()[jj]);
81  dy.random();
82  ObsVector_ dy1(dy);
83  ObsVector_ dy2(dy);
84  oops::Log::info() << "Random vector dy: " << dy << std::endl;
85 
86  R->multiply(dy1);
87  R->inverseMultiply(dy1);
88  // dy1 = R^{-1}*R*dy
89  oops::Log::info() << "R^{-1}*R*dy: " << dy1 << std::endl;
90  EXPECT(oops::is_close(dy1.rms(), dy.rms(), 1.e-10));
91 
92  R->inverseMultiply(dy2);
93  R->multiply(dy2);
94  // dy2 = R*R^P-1}*dy
95  oops::Log::info() << "R*R^{-1}*dy: " << dy2 << std::endl;
96  EXPECT(oops::is_close(dy2.rms(), dy.rms(), 1.e-10));
97  }
98 }
99 
100 
101 // -----------------------------------------------------------------------------
102 
103 template <typename OBS>
106  public:
108  virtual ~ObsErrorCovariance() {}
109  private:
110  std::string testid() const override {return "test::ObsErrorCovariance<" + OBS::name() + ">";}
111 
112  void register_tests() const override {
113  std::vector<eckit::testing::Test>& ts = eckit::testing::specification();
114 
115  ts.emplace_back(CASE("interface/ObsErrorCovariance/testConstructor")
116  { testConstructor<OBS>(); });
117  ts.emplace_back(CASE("interface/ObsErrorCovariance/testMultiplies")
118  { testMultiplies<OBS>(); });
119  }
120 
121  void clear() const override {
122  Test_::reset();
123  }
124 };
125 
126 // -----------------------------------------------------------------------------
127 
128 } // namespace test
129 
130 #endif // TEST_BASE_OBSERRORCOVARIANCE_H_
oops::ObsErrorFactory
ObsErrorFactory Factory.
Definition: ObsErrorBase.h:59
test::ObsTestsFixture
Definition: ObsTestsFixture.h:29
test::ObsTestsFixture::reset
static void reset()
Definition: ObsTestsFixture.h:37
test::ObsErrorCovariance::Test_
ObsTestsFixture< OBS > Test_
Definition: test/base/ObsErrorCovariance.h:105
oops::ObsErrorBase
Base class for observation error covariance matrices.
Definition: ObsErrorBase.h:32
test::ObsErrorCovariance::~ObsErrorCovariance
virtual ~ObsErrorCovariance()
Definition: test/base/ObsErrorCovariance.h:108
test::CASE
CASE("test_linearmodelparameterswrapper_valid_name")
Definition: LinearModelFactory.cc:22
instantiateObsErrorFactory.h
test
Definition: LinearModelFactory.cc:20
oops::ObsVector
Definition: oops/interface/ObsSpace.h:36
test::ObsErrorCovariance::ObsErrorCovariance
ObsErrorCovariance()
Definition: test/base/ObsErrorCovariance.h:107
Test.h
test::ObsErrorCovariance::clear
void clear() const override
Definition: test/base/ObsErrorCovariance.h:121
test::TestEnvironment::config
static const eckit::Configuration & config()
Definition: TestEnvironment.h:40
ObsTestsFixture.h
test::ObsErrorCovariance::register_tests
void register_tests() const override
Definition: test/base/ObsErrorCovariance.h:112
TestEnvironment.h
test::testConstructor
void testConstructor()
Tests creation and destruction of ObsErrorCovariances.
Definition: test/base/ObsErrorCovariance.h:32
test::ObsErrorCovariance
Definition: test/base/ObsErrorCovariance.h:104
oops::Test
Definition: Test.h:39
ObsVector.h
ObsErrorBase.h
test::testMultiplies
void testMultiplies()
Tests that and .
Definition: test/base/ObsErrorCovariance.h:58
test::ObsErrorCovariance::testid
std::string testid() const override
Definition: test/base/ObsErrorCovariance.h:110