FV3 Bundle
test/interface/GeometryIterator.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_GEOMETRYITERATOR_H_
12 #define TEST_INTERFACE_GEOMETRYITERATOR_H_
13 
14 #include <math.h>
15 #include <string>
16 #include <vector>
17 
18 #define BOOST_TEST_NO_MAIN
19 #define BOOST_TEST_ALTERNATIVE_INIT_API
20 #define BOOST_TEST_DYN_LINK
21 #include <boost/test/unit_test.hpp>
22 
23 #include <boost/noncopyable.hpp>
24 #include <boost/scoped_ptr.hpp>
25 
26 #include "eckit/config/Configuration.h"
27 #include "oops/base/GridPoint.h"
28 #include "oops/base/Variables.h"
31 #include "oops/interface/State.h"
32 #include "oops/runs/Test.h"
33 #include "test/TestEnvironment.h"
34 
35 namespace test {
36 
37 // -----------------------------------------------------------------------------
38 template <typename MODEL> class GeometryIteratorFixture : private boost::noncopyable {
39  public:
40  static const eckit::Configuration & getConfig() {return *getInstance().conf_;}
41 
42  private:
44  static GeometryIteratorFixture<MODEL> theGeometryIteratorFixture;
45  return theGeometryIteratorFixture;
46  }
47 
49  conf_.reset(new eckit::LocalConfiguration(TestEnvironment::config(), "GeometryIterator"));
50  }
51 
53 
54  boost::scoped_ptr<const eckit::LocalConfiguration> conf_;
55 };
56 // -----------------------------------------------------------------------------
57 
58 template <typename MODEL> void testConstructor() {
59  typedef oops::GeometryIterator<MODEL> GeometryIterator_;
60  typedef oops::Geometry<MODEL> Geometry_;
61 
62  const eckit::LocalConfiguration
63  geomConfig(GeometryIteratorFixture<MODEL>::getConfig(), "Geometry");
64  Geometry_ geom(geomConfig);
65 
66  boost::scoped_ptr<GeometryIterator_> iter(new GeometryIterator_(geom.begin()));
67  BOOST_CHECK(iter.get());
68 
69  iter.reset();
70  BOOST_CHECK(!iter.get());
71 }
72 
73 // -----------------------------------------------------------------------------
74 
75 template <typename MODEL> void testIterator() {
76  typedef oops::Geometry<MODEL> Geometry_;
77  typedef oops::GeometryIterator<MODEL> GeometryIterator_;
78  typedef oops::State<MODEL> State_;
79 
80  const eckit::LocalConfiguration
81  geomConfig(GeometryIteratorFixture<MODEL>::getConfig(), "Geometry");
82  const eckit::LocalConfiguration stateConfig(GeometryIteratorFixture<MODEL>::getConfig(), "State");
83 
84  const oops::Variables vars(stateConfig);
85 
86  Geometry_ geom(geomConfig);
87  State_ state(geom, vars, stateConfig);
88 
89  const double rms_conf = stateConfig.getDouble("rms");
90  const double tol = stateConfig.getDouble("tolerance");
91 
92  double rms = 0;
93  int n = 0;
94  for (GeometryIterator_ i = geom.begin(); i != geom.end(); ++i, ++n) {
95  oops::GridPoint gp = state.getPoint(i);
96  oops::Log::debug() << *i << gp << std::endl;
97  std::vector<double> vals = gp.getVals();
98  rms += std::inner_product(vals.begin(), vals.end(), vals.begin(), 0.) /
99  vals.size();
100  }
101  rms = sqrt(rms/n);
102 
103  oops::Log::debug() << n << " rms from iterator: " << rms << std::endl;
104  oops::Log::debug() << "rms from config: " << rms_conf << std::endl;
105 
106  BOOST_CHECK_CLOSE(rms, rms_conf, tol);
107 }
108 
109 // -----------------------------------------------------------------------------
110 
111 template <typename MODEL> class GeometryIterator : public oops::Test {
112  public:
114  virtual ~GeometryIterator() {}
115  private:
116  std::string testid() const {return "test::GeometryIterator<" + MODEL::name() + ">";}
117 
118  void register_tests() const {
119  boost::unit_test::test_suite * ts = BOOST_TEST_SUITE("interface/GeometryIterator");
120 
121  ts->add(BOOST_TEST_CASE(&testConstructor<MODEL>));
122  ts->add(BOOST_TEST_CASE(&testIterator<MODEL>));
123 
124  boost::unit_test::framework::master_test_suite().add(ts);
125  }
126 };
127 
128 // =============================================================================
129 
130 } // namespace test
131 
132 #endif // TEST_INTERFACE_GEOMETRYITERATOR_H_
l_size ! loop over number of fields ke do je do i
const std::vector< double > getVals()
Definition: GridPoint.h:28
static const eckit::Configuration & getConfig()
Encapsulates the model state.
static const eckit::Configuration & config()
character(len=32) name
logical debug
Definition: mpp.F90:1297
void testConstructor()
boost::scoped_ptr< const eckit::LocalConfiguration > conf_
type(var_state_type), dimension(2) state
static GeometryIteratorFixture< MODEL > & getInstance()