FV3 Bundle
oops/src/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 <string>
13 #include <vector>
14 
15 #define BOOST_TEST_NO_MAIN
16 #define BOOST_TEST_ALTERNATIVE_INIT_API
17 #define BOOST_TEST_DYN_LINK
18 #include <boost/test/unit_test.hpp>
19 
20 #include <boost/noncopyable.hpp>
21 #include <boost/scoped_ptr.hpp>
22 
23 #include "eckit/config/LocalConfiguration.h"
24 #include "oops/base/ObsSpaces.h"
25 #include "oops/interface/GeoVaLs.h"
28 #include "oops/runs/Test.h"
29 #include "oops/util/dot_product.h"
30 #include "test/TestEnvironment.h"
31 
32 namespace test {
33 
34 // -----------------------------------------------------------------------------
35 
36 template <typename MODEL>
37 class GeoVaLsFixture : private boost::noncopyable {
39 
40  public:
41  static const util::DateTime & tbgn() {return *getInstance().tbgn_;}
42  static const util::DateTime & tend() {return *getInstance().tend_;}
43  static ObsSpaces_ & obspace() {return *getInstance().ospaces_;}
44 
45  private:
47  static GeoVaLsFixture<MODEL> theGeoVaLsFixture;
48  return theGeoVaLsFixture;
49  }
50 
52  tbgn_.reset(new util::DateTime(TestEnvironment::config().getString("window_begin")));
53  tend_.reset(new util::DateTime(TestEnvironment::config().getString("window_end")));
54 
55  const eckit::LocalConfiguration conf(TestEnvironment::config(), "Observations");
56  ospaces_.reset(new ObsSpaces_(conf, *tbgn_, *tend_));
57  }
58 
60 
61  boost::scoped_ptr<const util::DateTime> tbgn_;
62  boost::scoped_ptr<const util::DateTime> tend_;
63  boost::scoped_ptr<ObsSpaces_> ospaces_;
64 };
65 
66 // -----------------------------------------------------------------------------
67 
68 template <typename MODEL> void testConstructor() {
69  typedef GeoVaLsFixture<MODEL> Test_;
70  typedef oops::GeoVaLs<MODEL> GeoVaLs_;
71  typedef oops::Locations<MODEL> Locations_;
72  typedef oops::ObsOperator<MODEL> ObsOperator_;
73 
74  for (std::size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
75  ObsOperator_ hop(Test_::obspace()[jj]);
76 
77  Locations_ locs(Test_::obspace()[jj].locations(Test_::tbgn(), Test_::tend()));
78  boost::scoped_ptr<GeoVaLs_> ov(new GeoVaLs_(locs, hop.variables()));
79  BOOST_CHECK(ov.get());
80 
81  ov.reset();
82  BOOST_CHECK(!ov.get());
83  }
84 }
85 
86 // -----------------------------------------------------------------------------
87 
88 template <typename MODEL> void testUtils() {
89  typedef GeoVaLsFixture<MODEL> Test_;
90  typedef oops::GeoVaLs<MODEL> GeoVaLs_;
91  typedef oops::Locations<MODEL> Locations_;
92  typedef oops::ObsOperator<MODEL> ObsOperator_;
93 
94  for (std::size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
95  ObsOperator_ hop(Test_::obspace()[jj]);
96 
97  Locations_ locs(Test_::obspace()[jj].locations(Test_::tbgn(), Test_::tend()));
98  GeoVaLs_ gval(locs, hop.variables());
99 
100  gval.random();
101  const double zz1 = dot_product(gval, gval);
102  BOOST_CHECK(zz1 > 0.0);
103 
104  gval.zero();
105  const double zz2 = dot_product(gval, gval);
106  BOOST_CHECK_EQUAL(zz2, 0.0);
107  }
108 }
109 
110 // -----------------------------------------------------------------------------
111 
112 template <typename MODEL> void testRead() {
113  typedef GeoVaLsFixture<MODEL> Test_;
114  typedef oops::GeoVaLs<MODEL> GeoVaLs_;
115  typedef oops::Locations<MODEL> Locations_;
116  typedef oops::ObsOperator<MODEL> ObsOperator_;
117 
118  const eckit::LocalConfiguration obsconf(TestEnvironment::config(), "Observations");
119  std::vector<eckit::LocalConfiguration> conf;
120  obsconf.get("ObsTypes", conf);
121 
122  const double tol = 1.0e-8;
123  for (std::size_t jj = 0; jj < Test_::obspace().size(); ++jj) {
124  ObsOperator_ hop(Test_::obspace()[jj]);
125 
126  eckit::LocalConfiguration gconf(conf[jj], "GeoVaLs");
127  Locations_ locs(Test_::obspace()[jj].locations(Test_::tbgn(), Test_::tend()));
128 
129  GeoVaLs_ gval(gconf, hop.variables());
130 
131  const double xx = gconf.getDouble("norm");
132  const double zz = sqrt(dot_product(gval, gval));
133  BOOST_CHECK_CLOSE(xx, zz, tol);
134  }
135 }
136 
137 // -----------------------------------------------------------------------------
138 
139 template <typename MODEL> class GeoVaLs : public oops::Test {
140  public:
141  GeoVaLs() {}
142  virtual ~GeoVaLs() {}
143  private:
144  std::string testid() const {return "test::GeoVaLs<" + MODEL::name() + ">";}
145 
146  void register_tests() const {
147  boost::unit_test::test_suite * ts = BOOST_TEST_SUITE("interface/GeoVaLs");
148 
149  ts->add(BOOST_TEST_CASE(&testConstructor<MODEL>));
150 // ts->add(BOOST_TEST_CASE(&testUtils<MODEL>)); dh: turned off until ufo geovals allocation fix
151  ts->add(BOOST_TEST_CASE(&testRead<MODEL>));
152 
153  boost::unit_test::framework::master_test_suite().add(ts);
154  }
155 };
156 
157 // =============================================================================
158 
159 } // namespace test
160 
161 #endif // TEST_INTERFACE_GEOVALS_H_
boost::scoped_ptr< const util::DateTime > tbgn_
Definition: conf.py:1
boost::scoped_ptr< ObsSpaces_ > ospaces_
static const util::DateTime & tend()
static const eckit::Configuration & config()
static GeoVaLsFixture< MODEL > & getInstance()
character(len=32) name
oops::ObsSpaces< MODEL > ObsSpaces_
void testConstructor()
boost::scoped_ptr< const util::DateTime > tend_
static const util::DateTime & tbgn()