FV3 Bundle
test/lorenz95/ModelBias.cc
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 #include <fstream>
12 #include <iostream>
13 
14 #include <boost/lexical_cast.hpp>
15 #include <boost/scoped_ptr.hpp>
16 #include <boost/shared_ptr.hpp>
17 #include <boost/test/unit_test.hpp>
18 
19 #include "./TestConfig.h"
20 #include "eckit/config/LocalConfiguration.h"
21 #include "lorenz95/ModelBias.h"
24 #include "lorenz95/Resolution.h"
25 #include "test/TestFixture.h"
26 
27 namespace test {
28 
29 // -----------------------------------------------------------------------------
31  public:
33  eckit::LocalConfiguration res(TestConfig::config(), "resolution");
34  resol_.reset(new lorenz95::Resolution(res));
35  biasconf_.reset(new eckit::LocalConfiguration(TestConfig::config(), "ModelBias"));
36  nobias_.reset(new eckit::LocalConfiguration());
37  eckit::LocalConfiguration covarCfg(TestConfig::config(), "Covariance");
38  lorenz95::ModelBiasCovariance covar(covarCfg, *resol_);
39  bias1_ = biasconf_->getDouble("bias");
40  bias2_ = 2.5 * bias1_;
41  dbias_.reset(new lorenz95::ModelBiasCorrection(*resol_, covar.config()));
42  dbias_->bias() = bias2_;
43  }
45  boost::scoped_ptr<lorenz95::Resolution> resol_;
46  boost::scoped_ptr<const eckit::LocalConfiguration> biasconf_;
47  boost::scoped_ptr<const eckit::LocalConfiguration> nobias_;
48  boost::scoped_ptr<lorenz95::ModelBiasCorrection> dbias_;
49  double bias1_;
50  double bias2_;
51 };
52 // -----------------------------------------------------------------------------
53 
54 // -----------------------------------------------------------------------------
55 BOOST_FIXTURE_TEST_SUITE(test_modelBias, ModBiasTestFixture)
56 // -----------------------------------------------------------------------------
57  BOOST_AUTO_TEST_CASE(test_modelBias_constructor_bias) {
58  lorenz95::ModelBias mbias(*resol_, *biasconf_);
59  BOOST_CHECK_EQUAL(mbias.bias(), bias1_);
60  }
61 // -----------------------------------------------------------------------------
62  BOOST_AUTO_TEST_CASE(test_modelBias_constructor_nobias) {
63  lorenz95::ModelBias mbias(*resol_, *nobias_);
64 
65  // because the biasconf_ is empty when used,
66  // the active_ flag is false and the bias_ value is 0.0
67  BOOST_CHECK_EQUAL(mbias.bias(), 0.0);
68 }
69 // -----------------------------------------------------------------------------
70  BOOST_AUTO_TEST_CASE(test_modelBias_constructor_active) {
71  lorenz95::ModelBias mbias1(*resol_, *biasconf_);
72  lorenz95::ModelBias mbias2(*resol_, mbias1);
73  BOOST_CHECK_EQUAL(mbias2.bias(), bias1_);
74  }
75 // -----------------------------------------------------------------------------
76  BOOST_AUTO_TEST_CASE(test_modelBias_constructor_inactive) {
77  lorenz95::ModelBias mbias1(*resol_, *nobias_);
78  lorenz95::ModelBias mbias2(*resol_, mbias1);
79 
80  // because the biasconf_ is empty when used,
81  // the active_ flag is false and the bias_ value is 0.0
82  BOOST_CHECK_EQUAL(mbias2.bias(), 0.0);
83  }
84 // -----------------------------------------------------------------------------
85  BOOST_AUTO_TEST_CASE(test_modelBias_constructor_active_copy) {
86  lorenz95::ModelBias mbias1(*resol_, *biasconf_);
87  lorenz95::ModelBias mbias2(mbias1, true);
88  BOOST_CHECK_EQUAL(mbias2.bias(), bias1_);
89  }
90 // -----------------------------------------------------------------------------
91  BOOST_AUTO_TEST_CASE(test_modelBias_constructor_active_nocopy) {
92  lorenz95::ModelBias mbias1(*resol_, *biasconf_);
93  lorenz95::ModelBias mbias2(mbias1, false);
94 
95  // because copy flag is set to false, bias is 0.0
96  BOOST_CHECK_EQUAL(mbias2.bias(), 0.0);
97  }
98 // -----------------------------------------------------------------------------
99  BOOST_AUTO_TEST_CASE(test_modelBias_constructor_inactive_copy) {
100  lorenz95::ModelBias mbias1(*resol_, *nobias_);
101  lorenz95::ModelBias mbias2(mbias1, true);
102 
103  // because the biasconf_ is empty when used,
104  // the active_ flag is false and the bias_ value is 0.0
105  BOOST_CHECK_EQUAL(mbias2.bias(), 0.0);
106  }
107 // -----------------------------------------------------------------------------
108  BOOST_AUTO_TEST_CASE(test_modelBias_constructor_inactive_nocopy) {
109  lorenz95::ModelBias mbias1(*resol_, *nobias_);
110  lorenz95::ModelBias mbias2(mbias1, false);
111  BOOST_CHECK_EQUAL(mbias2.bias(), 0.0);
112  }
113 // -----------------------------------------------------------------------------
114  BOOST_AUTO_TEST_CASE(test_modelBias_destructor) {
115  // not yet implemented
116  }
117 // -----------------------------------------------------------------------------
118  BOOST_AUTO_TEST_CASE(test_modelBias_classname) {
119  lorenz95::ModelBias mbias(*resol_, *biasconf_);
120  BOOST_CHECK_EQUAL(mbias.classname(), "lorenz95::ModelBias");
121  }
122 // -----------------------------------------------------------------------------
123  BOOST_AUTO_TEST_CASE(test_modelBias_compound_assignment_active) {
124  lorenz95::ModelBias mbias(*resol_, *biasconf_);
125 
126  mbias += *dbias_;
127 
128  BOOST_CHECK_EQUAL(mbias.bias(), bias1_ + bias2_);
129  }
130 // -----------------------------------------------------------------------------
131  BOOST_AUTO_TEST_CASE(test_modelBias_compound_assignment_inactive) {
132  lorenz95::ModelBias mbias(*resol_, *nobias_);
133 
134  mbias += *dbias_;
135 
136  // because the biasconf_ is empty,
137  // mbias bias value is not modified from 0.0
138  BOOST_CHECK_EQUAL(mbias.bias(), 0.0);
139  }
140 // -----------------------------------------------------------------------------
141  BOOST_AUTO_TEST_CASE(test_modelBias_bias) {
142  lorenz95::ModelBias mbias(*resol_, *biasconf_);
143  BOOST_CHECK_EQUAL(mbias.bias(), bias1_);
144  }
145 // -----------------------------------------------------------------------------
146  BOOST_AUTO_TEST_CASE(test_modelBias_read) {
147  // not yet implemented
148  }
149 // -----------------------------------------------------------------------------
150  BOOST_AUTO_TEST_CASE(test_modelBias_write) {
151  // not yet implemented
152  }
153 // -----------------------------------------------------------------------------
154  BOOST_AUTO_TEST_CASE(test_modelBias_stream_output) {
155  lorenz95::ModelBias mbias(*resol_, *biasconf_);
156 
157  // use the operator<< method to write the value to a file
158  std::filebuf fb;
159  std::string filename("ModelBiasTest.txt");
160  fb.open(filename.c_str(), std::ios::out);
161  std::ostream os(&fb);
162  os << mbias;
163  fb.close();
164 
165  // then read the value that was written to the file
166  std::string inputString;
167  std::string inputBias;
168  double testBias = bias1_;
169  double bias = 0.0;
170  int biasStartPos = 12; // length of "ModelBias = " is 12
171  std::ifstream inputFile(filename.c_str());
172  if (inputFile.is_open()) {
173  getline(inputFile, inputString); // ignore first (blank) line
174  getline(inputFile, inputString);
175 
176  inputBias = inputString.substr(biasStartPos);
177 
178  try {
179  bias = boost::lexical_cast<double>(inputBias);
180  }
181  catch(boost::bad_lexical_cast const&) {
182  BOOST_ERROR("operator<< incorrectly output a non-double");
183  }
184 
185  BOOST_CHECK_CLOSE(testBias, bias, 0.0001);
186  } else {
187  // if we can't open the file then we can't
188  // verify that the value was correctly written
189  BOOST_ERROR("operator<< functionality cannot be determined");
190  }
191  inputFile.close();
192  }
193 // -----------------------------------------------------------------------------
194 
195 BOOST_AUTO_TEST_SUITE_END()
196 } // namespace test
boost::scoped_ptr< lorenz95::ModelBiasCorrection > dbias_
Handles resolution.
Definition: Resolution.h:25
boost::scoped_ptr< const eckit::LocalConfiguration > biasconf_
boost::scoped_ptr< lorenz95::Resolution > resol_
static const std::string classname()
boost::scoped_ptr< const eckit::LocalConfiguration > nobias_
const eckit::Configuration & config() const
const double & bias() const
Model error for Lorenz 95 model.
BOOST_AUTO_TEST_CASE(test_GomL95_constructor)
static const eckit::Configuration & config()
Definition: TestConfig.h:30