FV3 Bundle
test/lorenz95/StateL95.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 <algorithm>
12 #include <fstream>
13 #include <iostream>
14 #include <sstream>
15 
16 #include <boost/lexical_cast.hpp>
17 #include <boost/scoped_ptr.hpp>
18 #include <boost/test/unit_test.hpp>
19 
20 #include "./TestConfig.h"
21 #include "eckit/config/LocalConfiguration.h"
22 #include "lorenz95/FieldL95.h"
23 #include "lorenz95/GomL95.h"
24 #include "lorenz95/IncrementL95.h"
25 #include "lorenz95/LocsL95.h"
26 #include "lorenz95/ModelBias.h"
27 #include "lorenz95/ModelL95.h"
29 #include "lorenz95/Resolution.h"
30 #include "lorenz95/StateL95.h"
31 #include "oops/base/Variables.h"
32 #include "oops/util/DateTime.h"
33 #include "test/TestFixture.h"
34 
35 namespace test {
36 
37 // -----------------------------------------------------------------------------
39  public:
41  file_.reset(new eckit::LocalConfiguration(TestConfig::config(), "state"));
42  eckit::LocalConfiguration res(TestConfig::config(), "resolution");
43  resol_.reset(new lorenz95::Resolution(res));
44  date_str_ = file_->getString("date");
45  time_.reset(new util::DateTime(date_str_));
47  }
49  boost::scoped_ptr<const eckit::LocalConfiguration> file_;
50  boost::scoped_ptr<lorenz95::Resolution> resol_;
51  std::string date_str_;
52  boost::scoped_ptr<util::DateTime> time_;
53  boost::scoped_ptr<oops::Variables> vars_;
54 };
55 // -----------------------------------------------------------------------------
56 
57 // -----------------------------------------------------------------------------
58 BOOST_FIXTURE_TEST_SUITE(test_StateL95, StateTestFixture)
59 // -----------------------------------------------------------------------------
60  BOOST_AUTO_TEST_CASE(test_stateL95_constructor) {
61  boost::scoped_ptr<lorenz95::StateL95> xx(new lorenz95::StateL95(*resol_, *vars_, *time_));
62  BOOST_CHECK(xx.get() != NULL);
63  }
64 // -----------------------------------------------------------------------------
65  BOOST_AUTO_TEST_CASE(test_stateL95_readin_constructor) {
66  boost::scoped_ptr<lorenz95::StateL95> xx(new lorenz95::StateL95(*resol_, *vars_, *file_));
67  BOOST_CHECK(xx.get() != NULL);
68  }
69 // -----------------------------------------------------------------------------
70  BOOST_AUTO_TEST_CASE(test_stateL95_interpolation_constructor) {
71  lorenz95::StateL95 xx1(*resol_, *vars_, *time_);
72  boost::scoped_ptr<lorenz95::StateL95> xx2(new lorenz95::StateL95(*resol_, xx1));
73  BOOST_CHECK(xx2.get() != NULL);
74  }
75 // -----------------------------------------------------------------------------
76  BOOST_AUTO_TEST_CASE(test_stateL95_copy_constructor) {
77  lorenz95::StateL95 xx(*resol_, *vars_, *time_);
78  boost::scoped_ptr<lorenz95::StateL95> xx2(new lorenz95::StateL95(xx));
79  BOOST_CHECK(xx2.get() != NULL);
80  }
81 // -----------------------------------------------------------------------------
82  BOOST_AUTO_TEST_CASE(test_stateL95_destructor) {
83  // nothing to test
84  }
85 // -----------------------------------------------------------------------------
86  BOOST_AUTO_TEST_CASE(test_stateL95_getField) {
87  lorenz95::StateL95 xx(*resol_, *vars_, *time_);
88 
89  // there are 2 values in FieldL95: the 1st is the *resol_ value,
90  // the 2nd is a vector of doubles initialised to 0.0, the size of the
91  // vector is the *resol_ value
92  BOOST_CHECK_EQUAL(xx.getField().resol(), resol_->npoints());
93  }
94 // -----------------------------------------------------------------------------
95  BOOST_AUTO_TEST_CASE(test_stateL95_validTime) {
96  lorenz95::StateL95 xx(*resol_, *vars_, *time_);
97  BOOST_CHECK_EQUAL(xx.validTime().toString(), date_str_);
98  }
99 // -----------------------------------------------------------------------------
100  BOOST_AUTO_TEST_CASE(test_stateL95_assignment) {
101  util::DateTime tt(file_->getString("date"));
102  lorenz95::StateL95 xx1(*resol_, *vars_, tt);
103  xx1.read(*file_);
104 
105  // construct the second StateL95 object
106  std::string date_string2("2014-09-14T09:35:00Z");
107  util::DateTime dt2(date_string2);
108  lorenz95::StateL95 xx2(*resol_, *vars_, dt2);
109 
110  xx2 = xx1;
111 
112  // initially, xx1 held data, xx2 was initialised to 0, now
113  // ensure the two stateL95s are the same
114  BOOST_CHECK_EQUAL(xx1.validTime(), xx2.validTime());
115  BOOST_CHECK_EQUAL(xx1.getField().resol(), xx2.getField().resol());
116  for (int i = 0; i < xx1.getField().resol(); ++i) {
117  BOOST_CHECK_EQUAL(xx1.getField()[i], xx2.getField()[i]);
118  }
119  }
120 // -----------------------------------------------------------------------------
121  BOOST_AUTO_TEST_CASE(test_stateL95_compound_assignment) {
122  util::DateTime tt(file_->getString("date"));
123  lorenz95::StateL95 xx(*resol_, *vars_, tt);
124  xx.read(*file_);
125 
126  // construct the incrementL95 object
127  lorenz95::IncrementL95 dx(*resol_, *vars_, tt);
128  dx.read(*file_);
129 
130  xx += dx;
131 
132  // both xx and dx were initialised with the same data,
133  // so when the two are added together xx should be double incL95
134  for (int i = 0; i < xx.getField().resol(); ++i) {
135  BOOST_CHECK_EQUAL(xx.getField()[i], 2.0 * dx.getField()[i]);
136  }
137  }
138 // -----------------------------------------------------------------------------
139  BOOST_AUTO_TEST_CASE(test_stateL95_read) {
140  util::DateTime tt(file_->getString("date"));
141  lorenz95::StateL95 xx(*resol_, *vars_, tt);
142  xx.read(*file_);
143 
144  // to verify the information has been read correctly, we need to open
145  // and read the file using ifstream functionality
146  const std::string filename(file_->getString("filename"));
147  std::ifstream inStream(filename.c_str());
148  if (!inStream.is_open()) {
149  BOOST_ERROR("read functionality cannot be determined");
150  }
151 
152  int resolInt;
153  inStream >> resolInt;
154 
155  std::string time;
156  inStream >> time;
157 
158  std::vector<double> doubleVec(resolInt);
159  for (int i = 0; i < resolInt; ++i) {
160  inStream >> doubleVec[i];
161  }
162  inStream.close();
163 
164  for (int i = 0; i < resol_->npoints(); ++i) {
165  BOOST_CHECK_EQUAL((xx.getField())[i], doubleVec[i]);
166  }
167  }
168 // -----------------------------------------------------------------------------
169 /*
170  BOOST_AUTO_TEST_CASE(test_stateL95_stream_output) {
171  lorenz95::StateL95 xx(*resol_, *vars_, *time_);
172  xx.read(*file_);
173 
174  // use the operator<< method to write the value to a file
175 
176  std::filebuf fb;
177  std::string filename("StateL95Test.txt");
178  fb.open(filename.c_str(), std::ios::out);
179  std::ostream os(&fb);
180  os << xx;
181  fb.close();
182 
183  std::vector<double> doubleVec(xx.getField().resol());
184  for (int i = 0; i < xx.getField().resol(); ++i) {
185  doubleVec[i] = xx.getField()[i];
186  }
187  std::vector<double>::iterator iter;
188  iter = std::min_element(doubleVec.begin(), doubleVec.end());
189  double min = *iter;
190  iter = std::max_element(doubleVec.begin(), doubleVec.end());
191  double max = *iter;
192 // double avg = 0.0;
193 
194  // then read the values that were written to the file
195  int idxStart;
196  int idxEnd;
197  double inputDouble = 0.0;
198  std::string input;
199  std::string inputTest;
200  std::string inputTimeTest(" Valid time: " + file_->getString("date"));
201  std::ifstream inputFile(filename.c_str());
202  if (inputFile.is_open()) {
203  getline(inputFile, input); // ignore the first (blank) line
204  getline(inputFile, input);
205 
206  BOOST_CHECK_EQUAL(input, inputTimeTest);
207 
208  getline(inputFile, input);
209  // test min value
210  idxStart = input.find("=");
211  idxEnd = input.find(",");
212  inputTest = input.substr(idxStart + 1, (idxEnd - 1) - idxStart);
213  inputDouble = boost::lexical_cast<double>(inputTest);
214  BOOST_CHECK_CLOSE(inputDouble, min, 0.0001);
215  // test max value
216  idxStart = input.find("=", idxEnd);
217  idxEnd = input.find(",", idxEnd + 1);
218  inputTest = input.substr(idxStart + 1, (idxEnd - 1) - idxStart);
219  inputDouble = boost::lexical_cast<double>(inputTest);
220  BOOST_CHECK_CLOSE(inputDouble, max, 0.0001);
221  // test avg value
222 // idxStart = input.find("=", idxEnd + 1);
223 // idxEnd = input.find(",", idxEnd + 1);
224 // inputTest = input.substr(idxStart + 1);
225 // inputDouble = boost::lexical_cast<double>(inputTest);
226 // BOOST_CHECK_CLOSE(inputDouble, avg, 0.0001);
227  } else {
228  // if we can't open the file then we can't
229  // verify that the value was correctly written
230  BOOST_ERROR("operator<< functionality cannot be determined");
231  }
232  inputFile.close();
233  }
234 */
235 // -----------------------------------------------------------------------------
236 // BOOST_AUTO_TEST_CASE(test_stateL95_step_traj) {
237 // lorenz95::StateL95 xx(*resol_, *vars_, *time_);
238 // xx.read(*file_);
239 //
240 // // construct a ModelL95 object
241 // eckit::LocalConfiguration modelCfg(TestConfig::config(), "model");
242 // lorenz95::ModelL95 modelL95(*resol_, modelCfg);
243 //
244 // // construct a ModelBias object
245 // eckit::LocalConfiguration biasCfg(TestConfig::config(), "ModelBias");
246 // lorenz95::ModelBias modelBias(*resol_, biasCfg);
247 //
248 // // construct a ModelTrajectory object
249 // lorenz95::ModelTrajectory modelTraj(true);
250 // // copy construct a FieldL95 object to set params
251 // // in the ModelTrajectory object
252 // lorenz95::FieldL95 fieldL95(xx.getField());
253 // modelTraj.set(fieldL95);
254 //
255 // xx.stepTraj(modelL95, modelBias, modelTraj);
256 //
257 // // create test data
258 // modelL95.stepRK(fieldL95, modelBias, modelTraj);
259 //
260 // // test xx data against newly created fieldL95 test data
261 // for(int i = 0; i < xx.getField().resol(); ++i) {
262 // BOOST_CHECK_EQUAL(xx.getField()[i], fieldL95[i]);
263 // }
264 // }
265 // -----------------------------------------------------------------------------
266 
267 BOOST_AUTO_TEST_SUITE_END()
268 } // namespace test
l_size ! loop over number of fields ke do je do i
Increment Class: Difference between two states.
Definition: IncrementL95.h:51
const util::DateTime & validTime() const
Definition: StateL95.h:81
const FieldL95 & getField() const
Definition: StateL95.h:71
*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
const FieldL95 & getField() const
Access to data.
Definition: IncrementL95.h:97
const int & resol() const
Set and get.
Definition: FieldL95.h:66
boost::scoped_ptr< util::DateTime > time_
boost::scoped_ptr< lorenz95::Resolution > resol_
boost::scoped_ptr< const eckit::LocalConfiguration > file_
BOOST_AUTO_TEST_CASE(test_GomL95_constructor)
void read(const eckit::Configuration &)
Utilities.
static const eckit::Configuration & config()
Definition: TestConfig.h:30
L95 model state.
Definition: StateL95.h:50
boost::scoped_ptr< oops::Variables > vars_