FV3 Bundle
test/lorenz95/ModelBiasCorrection.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/test/unit_test.hpp>
17 
18 #include "./TestConfig.h"
19 #include "eckit/config/LocalConfiguration.h"
20 #include "lorenz95/ModelBias.h"
22 #include "lorenz95/Resolution.h"
23 #include "test/TestFixture.h"
24 
25 namespace test {
26 
27 // -----------------------------------------------------------------------------
28 class ModBiasTestFixture : TestFixture {
29  public:
31  eckit::LocalConfiguration res(TestConfig::config(), "resolution");
32  resol_.reset(new lorenz95::Resolution(res));
33  conf_.reset(new eckit::LocalConfiguration(TestConfig::config(), "ModelBiasCovariance"));
34  nobias_.reset(new eckit::LocalConfiguration());
35  bias1_ = TestConfig::config().getDouble("ModelBias.bias");
36  bias2_ = 2.5 * bias1_;
37  fact_ = 1.2345;
38  }
40  boost::scoped_ptr<lorenz95::Resolution> resol_;
41  boost::scoped_ptr<const eckit::LocalConfiguration> conf_;
42  boost::scoped_ptr<const eckit::LocalConfiguration> nobias_;
43  double bias1_;
44  double bias2_;
45  double fact_;
46 };
47 // -----------------------------------------------------------------------------
48 
49 // -----------------------------------------------------------------------------
50 BOOST_FIXTURE_TEST_SUITE(test_modelBiasCorrection, ModBiasTestFixture)
51 // -----------------------------------------------------------------------------
52  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_constructor_config) {
53  boost::scoped_ptr<lorenz95::ModelBiasCorrection> dx(
54  new lorenz95::ModelBiasCorrection(*resol_, *conf_));
55  BOOST_CHECK(dx.get() != NULL);
56  }
57 // -----------------------------------------------------------------------------
58  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_constructor_no_config) {
59  boost::scoped_ptr<lorenz95::ModelBiasCorrection> dx(
60  new lorenz95::ModelBiasCorrection(*resol_, *nobias_));
61  BOOST_CHECK(dx.get() != NULL);
62 }
63 // -----------------------------------------------------------------------------
64  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_copy_ctor_active_copy) {
65  lorenz95::ModelBiasCorrection dx1(*resol_, *conf_);
66  dx1.bias() = bias1_;
67 
68  lorenz95::ModelBiasCorrection dx2(dx1, true);
69 
70  BOOST_CHECK_EQUAL(dx2.bias(), bias1_);
71  }
72 // -----------------------------------------------------------------------------
73  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_copy_ctor_active_no_copy) {
74  lorenz95::ModelBiasCorrection dx1(*resol_, *conf_);
75  dx1.bias() = bias1_;
76 
77  // construct a copy of it with the copy flag set to false
78  lorenz95::ModelBiasCorrection dx2(dx1, false);
79 
80  // because the copy is false,
81  // the active_ flag is true and the bias_ value is 0.0
82  BOOST_CHECK_EQUAL(dx2.bias(), 0.0);
83  }
84 // -----------------------------------------------------------------------------
85  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_copy_ctor_inactive_copy) {
86  lorenz95::ModelBiasCorrection dx1(*resol_, *nobias_);
87  dx1.bias() = bias1_;
88 
89  // construct a copy of it with the copy flag set to true
90  lorenz95::ModelBiasCorrection dx2(dx1, true);
91 
92  // because the cfg is empty when used,
93  // the active_ flag is false and the bias_ value is 0.0
94  BOOST_CHECK_EQUAL(dx2.bias(), 0.0);
95  }
96 // -----------------------------------------------------------------------------
97  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_copy_ctor_inactive_no_copy) {
98  lorenz95::ModelBiasCorrection dx1(*resol_, *nobias_);
99  dx1.bias() = bias1_;
100 
101  // construct a copy of it with the copy flag set to false
102  lorenz95::ModelBiasCorrection dx2(dx1, false);
103 
104  // because the cfg is empty when used and the copy flag is false,
105  // the active_ flag is false and the bias_ value is 0.0
106  BOOST_CHECK_EQUAL(dx2.bias(), 0.0);
107  }
108 // -----------------------------------------------------------------------------
109  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_copy_ctor_config_active) {
110  lorenz95::ModelBiasCorrection dx1(*resol_, *conf_);
111  dx1.bias() = bias1_;
112 
113  lorenz95::ModelBiasCorrection dx2(dx1, *conf_);
114 
115  BOOST_CHECK_EQUAL(dx2.bias(), bias1_);
116  }
117 // -----------------------------------------------------------------------------
118  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_copy_ctor_config_inactive) {
119  lorenz95::ModelBiasCorrection dx1(*resol_, *nobias_);
120  dx1.bias() = bias1_;
121 
122  lorenz95::ModelBiasCorrection dx2(dx1, *conf_);
123 
124  // because the covarCfg is empty when used (regardless of the cfg),
125  // the active_ flag is false and the bias_ value is 0.0
126  BOOST_CHECK_EQUAL(dx2.bias(), 0.0);
127  }
128 // -----------------------------------------------------------------------------
129  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_diff_active) {
130  lorenz95::ModelBias xx1(*resol_, *conf_);
131  xx1.bias() = bias1_;
132  lorenz95::ModelBias xx2(*resol_, *conf_);
133  xx2.bias() = bias2_;
134 
135  lorenz95::ModelBiasCorrection dx(*resol_, *conf_);
136 
137  dx.diff(xx1, xx2);
138 
139  BOOST_CHECK_EQUAL(dx.bias(), bias1_ - bias2_);
140  }
141 // -----------------------------------------------------------------------------
142  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_diff_inactive) {
143  lorenz95::ModelBias xx1(*resol_, *conf_);
144  xx1.bias() = bias1_;
145  lorenz95::ModelBias xx2(*resol_, *conf_);
146  xx2.bias() = bias2_;
147 
148  lorenz95::ModelBiasCorrection dx(*resol_, *nobias_);
149 
150  dx.diff(xx1, xx2);
151 
152  // because the active_ flag is false, the bias cannot be updated
153  BOOST_CHECK_EQUAL(dx.bias(), 0.0);
154  }
155 // -----------------------------------------------------------------------------
156  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_zero) {
157  lorenz95::ModelBiasCorrection dx(*resol_, *conf_);
158  dx.bias() = bias1_;
159 
160  dx.zero();
161 
162  BOOST_CHECK_EQUAL(dx.bias(), 0.0);
163  }
164 // -----------------------------------------------------------------------------
165  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_assignment_active) {
166  lorenz95::ModelBiasCorrection dx1(*resol_, *conf_);
167  dx1.bias() = bias1_;
168  lorenz95::ModelBiasCorrection dx2(*resol_, *conf_);
169  dx2.bias() = bias2_;
170 
171  dx1 = dx2;
172 
173  // the original MBC should have the same bias value as the copy MBC
174  BOOST_CHECK_EQUAL(dx1.bias(), bias2_);
175  }
176 // -----------------------------------------------------------------------------
177  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_assignment_inactive) {
178  lorenz95::ModelBiasCorrection dx1(*resol_, *nobias_);
179  dx1.bias() = bias1_;
180 
181  lorenz95::ModelBiasCorrection dx2(*resol_, *conf_);
182  dx2.bias() = bias2_;
183 
184  dx1 = dx2;
185 
186  // the active_ value is zero, so the bias will be zero
187  BOOST_CHECK_EQUAL(dx1.bias(), 0.0);
188  }
189 // -----------------------------------------------------------------------------
190  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_assignment_add_active) {
191  lorenz95::ModelBiasCorrection dx1(*resol_, *conf_);
192  dx1.bias() = bias1_;
193  lorenz95::ModelBiasCorrection dx2(*resol_, *conf_);
194  dx2.bias() = bias2_;
195 
196  dx1 += dx2;
197 
198  BOOST_CHECK_EQUAL(dx1.bias(), bias1_ + bias2_);
199  }
200 // -----------------------------------------------------------------------------
201  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_assignment_add_inactive) {
202  lorenz95::ModelBiasCorrection dx1(*resol_, *nobias_);
203  dx1.bias() = bias1_;
204  lorenz95::ModelBiasCorrection dx2(*resol_, *nobias_);
205  dx2.bias() = bias2_;
206 
207  dx1 += dx2;
208 
209  // the active_ value is zero, so the bias will be unchanged
210  BOOST_CHECK_EQUAL(dx1.bias(), bias1_);
211  }
212 // -----------------------------------------------------------------------------
213  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_assignment_subtract_active) {
214  lorenz95::ModelBiasCorrection dx1(*resol_, *conf_);
215  dx1.bias() = bias1_;
216  lorenz95::ModelBiasCorrection dx2(*resol_, *conf_);
217  dx2.bias() = bias2_;
218 
219  dx1 -= dx2;
220 
221  BOOST_CHECK_EQUAL(dx1.bias(), bias1_ - bias2_);
222  }
223 // -----------------------------------------------------------------------------
224  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_assignment_subtract_inactive) {
225  lorenz95::ModelBiasCorrection dx1(*resol_, *nobias_);
226  dx1.bias() = bias1_;
227  lorenz95::ModelBiasCorrection dx2(*resol_, *nobias_);
228  dx2.bias() = bias2_;
229 
230  dx1 -= dx2;
231 
232  // the active_ value is zero, so the bias will be unchanged
233  BOOST_CHECK_EQUAL(dx1.bias(), bias1_);
234  }
235 // -----------------------------------------------------------------------------
236  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_assignment_multiply_active) {
237  lorenz95::ModelBiasCorrection dx(*resol_, *conf_);
238  dx.bias() = bias1_;
239 
240  dx *= fact_;
241 
242  BOOST_CHECK_EQUAL(dx.bias(), bias1_ * fact_);
243  }
244 // -----------------------------------------------------------------------------
245  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_assignment_multiply_inactive) {
246  lorenz95::ModelBiasCorrection dx(*resol_, *nobias_);
247  dx.bias() = bias1_;
248 
249  dx *= fact_;
250 
251  // the active_ value is zero, so the bias will be unchanged
252  BOOST_CHECK_EQUAL(dx.bias(), bias1_);
253  }
254 // -----------------------------------------------------------------------------
255  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_axpy_active) {
256  lorenz95::ModelBiasCorrection dx1(*resol_, *conf_);
257  dx1.bias() = bias1_;
258  lorenz95::ModelBiasCorrection dx2(*resol_, *conf_);
259  dx2.bias() = bias2_;
260 
261  dx1.axpy(fact_, dx2);
262 
263  BOOST_CHECK_EQUAL(dx1.bias(), (bias1_ + fact_ * bias2_));
264  }
265 // -----------------------------------------------------------------------------
266  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_axpy_inactive) {
267  lorenz95::ModelBiasCorrection dx1(*resol_, *nobias_);
268  dx1.bias() = bias1_;
269  lorenz95::ModelBiasCorrection dx2(*resol_, *nobias_);
270  dx2.bias() = bias2_;
271 
272  dx1.axpy(fact_, dx2);
273 
274  // the active_ value is zero, so the bias will be unchanged
275  BOOST_CHECK_EQUAL(dx1.bias(), bias1_);
276  }
277 // -----------------------------------------------------------------------------
278  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_dot_product_with_active) {
279  lorenz95::ModelBiasCorrection dx1(*resol_, *conf_);
280  dx1.bias() = bias1_;
281  lorenz95::ModelBiasCorrection dx2(*resol_, *conf_);
282  dx2.bias() = bias2_;
283 
284  double dpwResult = dx1.dot_product_with(dx2);
285 
286  BOOST_CHECK_EQUAL(dpwResult, bias1_ * bias2_);
287  }
288 // -----------------------------------------------------------------------------
289  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_dot_product_with_inactive) {
290  lorenz95::ModelBiasCorrection dx1(*resol_, *nobias_);
291  dx1.bias() = bias1_;
292  lorenz95::ModelBiasCorrection dx2(*resol_, *nobias_);
293  dx2.bias() = bias2_;
294 
295  double dpwResult = dx1.dot_product_with(dx2);
296 
297  // because of the empty config, the result is 0.0
298  BOOST_CHECK_EQUAL(dpwResult, 0.0);
299  }
300 // -----------------------------------------------------------------------------
301  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_read) {
302  // nothing to test
303  }
304 // -----------------------------------------------------------------------------
305  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_write) {
306  // nothing to test
307  }
308 // -----------------------------------------------------------------------------
309  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_stream_output) {
310  lorenz95::ModelBiasCorrection dx(*resol_, *conf_);
311  dx.bias() = bias1_;
312 
313  // use the operator<< method to write the value to a file
314  std::filebuf fb;
315  std::string filename("ModelBiasCorrectionTest.txt");
316  fb.open(filename.c_str(), std::ios::out);
317  std::ostream os(&fb);
318  os << dx;
319  fb.close();
320 
321  // then read the value that was written to the file
322  std::string inputString;
323  std::string inputBias;
324  double testBias = bias1_;
325  double bias = 0.0;
326  int biasStartPos = 22; // length of "ModelBiasCorrection = " is 22
327  std::ifstream inputFile(filename.c_str());
328  if (inputFile.is_open()) {
329  getline(inputFile, inputString); // ignore first (blank) line
330  getline(inputFile, inputString);
331 
332  inputBias = inputString.substr(biasStartPos);
333 
334  try {
335  bias = boost::lexical_cast<double>(inputBias);
336  }
337  catch(boost::bad_lexical_cast const&) {
338  BOOST_ERROR("operator<< incorrectly output a non-double");
339  }
340 
341  BOOST_CHECK_CLOSE(testBias, bias, 0.0001);
342  } else {
343  // if we can't open the file then we can't
344  // verify that the value was correctly written
345  BOOST_ERROR("operator<< functionality cannot be determined");
346  }
347  inputFile.close();
348  }
349 // -----------------------------------------------------------------------------
350  BOOST_AUTO_TEST_CASE(test_modelBiasCorrection_bias) {
351  lorenz95::ModelBiasCorrection dx(*resol_, *conf_);
352  dx.bias() = bias1_;
353 
354  // this one test checks both the setting and getting of the bias value
355  BOOST_CHECK_EQUAL(dx.bias(), bias1_);
356  }
357 // -----------------------------------------------------------------------------
358 
359 BOOST_AUTO_TEST_SUITE_END()
360 } // namespace test
boost::scoped_ptr< const eckit::LocalConfiguration > conf_
void diff(const ModelBias &, const ModelBias &)
Linear algebra operators.
*f90 *************************************************************************GNU Lesser General Public License **This file is part of the GFDL Flexible Modeling System(FMS). ! *! *FMS is free software without even the implied warranty of MERCHANTABILITY or *FITNESS FOR A PARTICULAR PURPOSE See the GNU General Public License *for more details **You should have received a copy of the GNU Lesser General Public *License along with FMS If see< http:! ***********************************************************************! this routine is used to retrieve scalar boundary data for symmetric domain. subroutine MPP_GET_BOUNDARY_2D_(field, domain, ebuffer, sbuffer, wbuffer, nbuffer, flags, &position, complete, tile_count) type(domain2D), intent(in) ::domain MPP_TYPE_, intent(in) ::field(:,:) MPP_TYPE_, intent(inout), optional ::ebuffer(:), sbuffer(:), wbuffer(:), nbuffer(:) integer, intent(in), optional ::flags, position, tile_count logical, intent(in), optional ::complete MPP_TYPE_ ::field3D(size(field, 1), size(field, 2), 1) MPP_TYPE_, allocatable, dimension(:,:) ::ebuffer2D, sbuffer2D, wbuffer2D, nbuffer2D integer ::xcount, ycount integer ::ntile logical ::need_ebuffer, need_sbuffer, need_wbuffer, need_nbuffer integer(LONG_KIND), dimension(MAX_DOMAIN_FIELDS, MAX_TILES), save ::f_addrs=-9999 integer(LONG_KIND), dimension(4, MAX_DOMAIN_FIELDS, MAX_TILES), save ::b_addrs=-9999 integer, save ::bsize(4)=0, isize=0, jsize=0, ksize=0, pos, list=0, l_size=0, upflags integer ::buffer_size(4) integer ::max_ntile, tile, update_position, ishift, jshift logical ::do_update, is_complete, set_mismatch character(len=3) ::text MPP_TYPE_ ::d_type type(overlapSpec), pointer ::bound=> NULL() ntile
Handles resolution.
Definition: Resolution.h:25
boost::scoped_ptr< lorenz95::Resolution > resol_
boost::scoped_ptr< const eckit::LocalConfiguration > nobias_
double dot_product_with(const ModelBiasCorrection &) 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
void axpy(const double, const ModelBiasCorrection &)