FV3 Bundle
test/interface/ObsAuxIncrement.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_OBSAUXINCREMENT_H_
12 #define TEST_INTERFACE_OBSAUXINCREMENT_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/test/unit_test.hpp>
23 
24 #include <boost/noncopyable.hpp>
25 #include <boost/scoped_ptr.hpp>
26 
27 #include "eckit/config/LocalConfiguration.h"
31 #include "oops/runs/Test.h"
32 #include "oops/util/DateTime.h"
33 #include "oops/util/dot_product.h"
34 #include "test/TestEnvironment.h"
35 
36 namespace test {
37 
38 // =============================================================================
39 
40 template <typename MODEL> class ObsAuxIncrementFixture : private boost::noncopyable {
44 
45  public:
46  static const eckit::Configuration & config() {return *getInstance().conf_;}
47  static const Covariance_ & covariance() {return *getInstance().covar_;}
48 
49  private:
51  static ObsAuxIncrementFixture<MODEL> theObsAuxIncrementFixture;
52  return theObsAuxIncrementFixture;
53  }
54 
56  conf_.reset(new eckit::LocalConfiguration(TestEnvironment::config(), "ObsBiasCovariance"));
57  covar_.reset(new Covariance_(*conf_));
58  }
59 
61 
62  boost::scoped_ptr<const eckit::LocalConfiguration> conf_;
63  boost::scoped_ptr<const Covariance_> covar_;
64 };
65 
66 // =============================================================================
67 
68 template <typename MODEL> void testObsAuxIncrementConstructor() {
69  typedef ObsAuxIncrementFixture<MODEL> Test_;
70  typedef oops::ObsAuxIncrement<MODEL> AuxIncr_;
71 
72  AuxIncr_ dx(Test_::config());
73 
74  BOOST_CHECK_EQUAL(dx.norm(), 0.0);
75 }
76 
77 // -----------------------------------------------------------------------------
78 
79 template <typename MODEL> void testObsAuxIncrementCopyConstructor() {
80  typedef ObsAuxIncrementFixture<MODEL> Test_;
81  typedef oops::ObsAuxIncrement<MODEL> AuxIncr_;
82 
83  AuxIncr_ dx1(Test_::config());
85 
86  AuxIncr_ dx2(dx1);
87  BOOST_CHECK(dx2.norm() > 0.0);
88  BOOST_CHECK_EQUAL(dx2.norm(), dx1.norm());
89 
90 // Check that the copy is equal to the original
91  dx2 -= dx1;
92  BOOST_CHECK_EQUAL(dx2.norm(), 0.0);
93 }
94 
95 // -----------------------------------------------------------------------------
96 
97 template <typename MODEL> void testObsAuxIncrementChangeRes() {
98  typedef ObsAuxIncrementFixture<MODEL> Test_;
99  typedef oops::ObsAuxIncrement<MODEL> AuxIncr_;
100 
101  AuxIncr_ dx1(Test_::config());
103 
104  AuxIncr_ dx2(dx1, Test_::config());
105  BOOST_CHECK(dx2.norm() > 0.0);
106  BOOST_CHECK_EQUAL(dx2.norm(), dx1.norm());
107 
108 // Check that the copy is equal to the original
109  dx2 -= dx1;
110  BOOST_CHECK_EQUAL(dx2.norm(), 0.0);
111 }
112 
113 // -----------------------------------------------------------------------------
114 
115 template <typename MODEL> void testObsAuxIncrementTriangle() {
116  typedef ObsAuxIncrementFixture<MODEL> Test_;
117  typedef oops::ObsAuxIncrement<MODEL> AuxIncr_;
118 
119  AuxIncr_ dx1(Test_::config());
121  AuxIncr_ dx2(Test_::config());
123 
124 // test triangle inequality
125  double dot1 = dx1.norm();
126  BOOST_CHECK(dot1 > 0.0);
127 
128  double dot2 = dx2.norm();
129  BOOST_CHECK(dot2 > 0.0);
130 
131  dx2 += dx1;
132  double dot3 = dx2.norm();
133  BOOST_CHECK(dot3 > 0.0);
134 
135  BOOST_CHECK(dot3 <= dot1 + dot2);
136 }
137 
138 // -----------------------------------------------------------------------------
139 
140 template <typename MODEL> void testObsAuxIncrementOpPlusEq() {
141  typedef ObsAuxIncrementFixture<MODEL> Test_;
142  typedef oops::ObsAuxIncrement<MODEL> AuxIncr_;
143 
144  AuxIncr_ dx1(Test_::config());
146  AuxIncr_ dx2(dx1);
147 
148 // test *= and +=
149  dx2 += dx1;
150  dx1 *= 2.0;
151 
152  dx2 -= dx1;
153  BOOST_CHECK_SMALL(dx2.norm(), 1e-8);
154 }
155 
156 // -----------------------------------------------------------------------------
157 
158 template <typename MODEL> void testObsAuxIncrementDotProduct() {
159  typedef ObsAuxIncrementFixture<MODEL> Test_;
160  typedef oops::ObsAuxIncrement<MODEL> AuxIncr_;
161 
162  AuxIncr_ dx1(Test_::config());
164  AuxIncr_ dx2(Test_::config());
166 
167 // test symmetry of dot product
168  double zz1 = dot_product(dx1, dx2);
169  double zz2 = dot_product(dx2, dx1);
170 
171  BOOST_CHECK_EQUAL(zz1, zz2);
172 }
173 
174 // -----------------------------------------------------------------------------
175 
176 template <typename MODEL> void testObsAuxIncrementZero() {
177  typedef ObsAuxIncrementFixture<MODEL> Test_;
178  typedef oops::ObsAuxIncrement<MODEL> AuxIncr_;
179 
180  AuxIncr_ dx(Test_::config());
182  BOOST_CHECK(dx.norm() > 0.0);
183 
184 // test zero
185  dx->zero();
186  BOOST_CHECK_EQUAL(dx.norm(), 0.0);
187 }
188 
189 // -----------------------------------------------------------------------------
190 
191 template <typename MODEL> void testObsAuxIncrementAxpy() {
192  typedef ObsAuxIncrementFixture<MODEL> Test_;
193  typedef oops::ObsAuxIncrement<MODEL> AuxIncr_;
194 
195  AuxIncr_ dx1(Test_::config());
197 
198 // test axpy
199  AuxIncr_ dx2(dx1);
200  dx2.axpy(2.0, dx1);
201 
202  dx2 -= dx1;
203  dx2 -= dx1;
204  dx2 -= dx1;
205 
206  BOOST_CHECK_SMALL(dx2.norm(), 1e-8);
207 }
208 
209 // =============================================================================
210 
211 template <typename MODEL> class ObsAuxIncrement : public oops::Test {
212  public:
214  virtual ~ObsAuxIncrement() {}
215  private:
216  std::string testid() const {return "test::ObsAuxIncrement<" + MODEL::name() + ">";}
217 
218  void register_tests() const {
219  boost::unit_test::test_suite * ts = BOOST_TEST_SUITE("interface/ObsAuxIncrement");
220 
221  ts->add(BOOST_TEST_CASE(&testObsAuxIncrementConstructor<MODEL>));
222  ts->add(BOOST_TEST_CASE(&testObsAuxIncrementCopyConstructor<MODEL>));
223  ts->add(BOOST_TEST_CASE(&testObsAuxIncrementChangeRes<MODEL>));
224  ts->add(BOOST_TEST_CASE(&testObsAuxIncrementTriangle<MODEL>));
225  ts->add(BOOST_TEST_CASE(&testObsAuxIncrementOpPlusEq<MODEL>));
226  ts->add(BOOST_TEST_CASE(&testObsAuxIncrementDotProduct<MODEL>));
227  ts->add(BOOST_TEST_CASE(&testObsAuxIncrementAxpy<MODEL>));
228 
229  boost::unit_test::framework::master_test_suite().add(ts);
230  }
231 };
232 
233 // =============================================================================
234 
235 } // namespace test
236 
237 #endif // TEST_INTERFACE_OBSAUXINCREMENT_H_
void testObsAuxIncrementDotProduct()
static const eckit::Configuration & config()
void testObsAuxIncrementConstructor()
void testObsAuxIncrementTriangle()
static const eckit::Configuration & config()
character(len=32) name
real(fp), parameter, public e
static const Covariance_ & covariance()
boost::scoped_ptr< const Covariance_ > covar_
static ObsAuxIncrementFixture< MODEL > & getInstance()
void testObsAuxIncrementOpPlusEq()
void testObsAuxIncrementCopyConstructor()
void randomize(ObsAuxIncrement_ &) const
oops::ObsAuxControl< MODEL > ObsAux_
oops::ObsAuxCovariance< MODEL > Covariance_
boost::scoped_ptr< const eckit::LocalConfiguration > conf_
void testObsAuxIncrementChangeRes()
oops::ObsAuxIncrement< MODEL > AuxIncr_