OOPS
Increment4D.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 OOPS_ASSIMILATION_INCREMENT4D_H_
12 #define OOPS_ASSIMILATION_INCREMENT4D_H_
13 
14 #include <cmath>
15 #include <ostream>
16 #include <sstream>
17 #include <string>
18 #include <vector>
19 
20 #include <boost/ptr_container/ptr_map.hpp>
21 
22 #include "atlas/field.h"
23 
24 #include "eckit/config/LocalConfiguration.h"
25 #include "eckit/exception/Exceptions.h"
26 
29 #include "oops/base/Variables.h"
32 #include "oops/util/DateTime.h"
33 #include "oops/util/dot_product.h"
34 #include "oops/util/Duration.h"
35 #include "oops/util/Logger.h"
36 #include "oops/util/Printable.h"
37 #include "oops/util/Serializable.h"
38 
39 namespace oops {
40 
41 /// State increment
42 /*!
43  * The state increment contains the increment to the 3D or 4D state part of
44  * the VDA control variable.
45  */
46 
47 // -----------------------------------------------------------------------------
48 template<typename MODEL> class Increment4D : public util::Printable,
49  public util::Serializable {
54 
55  public:
56  static const std::string classname() {return "Increment4D";}
57 
58 /// Constructor, destructor
59  explicit Increment4D(const JbState_ &);
60  explicit Increment4D(const Increment_ &);
61  Increment4D(const Geometry_ &, const Variables &, const std::vector<util::DateTime> &);
62  Increment4D(const Increment4D &, const bool copy = true);
63  Increment4D(const Geometry_ &, const Increment4D &);
64  ~Increment4D();
65 
66 /// Interfacing
67  Increment_ & incr4d() {return *incr4d_;}
68  const Increment_ & incr4d() const {return *incr4d_;}
69 
70 /// Linear algebra operators
71  void diff(const State4D_ &, const State4D_ &);
72  void zero();
73  void random();
74  void ones();
75  void dirac(std::vector<eckit::LocalConfiguration>);
79  Increment4D & operator*=(const double);
80  void axpy(const double, const Increment4D &, const bool check = true);
81  double dot_product_with(const Increment4D &) const;
82  void schur_product_with(const Increment4D &);
83 
84 /// I/O and diagnostics
85  void read(const eckit::Configuration &);
86  void write(const eckit::Configuration &) const;
87 
88 /// Get geometry
89  Geometry_ geometry() const {return this->get(first_).geometry();}
90 
91 /// ATLAS FieldSet
92  void setAtlas(atlas::FieldSet *) const;
93  void toAtlas(atlas::FieldSet *) const;
94  void fromAtlas(atlas::FieldSet *);
95 
96 /// Get model space control variable
97  Increment_ & operator[](const int ii) {return this->get(ii);}
98  const Increment_ & operator[](const int ii) const {return this->get(ii);}
99  int first() const {return first_;}
100  int last() const {return last_;}
101  size_t size() const {return last_-first_+1;}
102 
103 /// To be removed
104  void shift_forward();
105  void shift_backward();
106 
107 /// Serialize and deserialize
108  size_t serialSize() const override;
109  void serialize(std::vector<double> &) const override;
110  void deserialize(const std::vector<double> &, size_t &) override;
111 
112  private:
113  Increment_ & get(const int);
114  const Increment_ & get(const int) const;
115  void print(std::ostream &) const override;
116 
117  typedef typename boost::ptr_map<int, Increment_>::iterator iter_;
118  typedef typename boost::ptr_map<int, Increment_>::const_iterator icst_;
119  boost::ptr_map<int, Increment_> incr4d_;
120  int first_;
121  int last_;
122 };
123 
124 // =============================================================================
125 template <typename MODEL>
127  Log::trace() << "operator+=(State4D, Increment4D) starting" << std::endl;
128  for (size_t ii = 0; ii < xx.size(); ++ii) {
129  xx[ii] += dx[ii];
130  }
131  Log::trace() << "operator+=(State4D, Increment4D) done" << std::endl;
132  return xx;
133 }
134 // ----------------------------------------------------------------------------
135 template<typename MODEL>
137  : incr4d_(), first_(0), last_(jb.nstates() - 1)
138 {
139  for (int jsub = 0; jsub <= last_; ++jsub) {
140  Increment_ * incr = jb.newStateIncrement(jsub);
141  incr4d_.insert(jsub, incr);
142  }
143  Log::trace() << "Increment4D:Increment4D created." << std::endl;
144 }
145 // -----------------------------------------------------------------------------
146 template<typename MODEL>
148  : incr4d_(), first_(0), last_(0)
149 {
150  Increment_ * incr = new Increment_(dx);
151  incr4d_.insert(0, incr);
152  Log::trace() << "Increment4D:Increment4D created." << std::endl;
153 }
154 // -----------------------------------------------------------------------------
155 template<typename MODEL>
157  const Variables & vars,
158  const std::vector<util::DateTime> & timeslots)
159  : incr4d_(), first_(0), last_(timeslots.size() - 1)
160 {
161  for (int jsub = 0; jsub <= last_; ++jsub) {
162  Increment_ * incr = new Increment_(resol, vars, timeslots[jsub]);
163  incr4d_.insert(jsub, incr);
164  }
165  Log::trace() << "Increment4D:Increment4D created." << std::endl;
166 }
167 // -----------------------------------------------------------------------------
168 template<typename MODEL>
169 Increment4D<MODEL>::Increment4D(const Increment4D & other, const bool copy)
170  : incr4d_(), first_(other.first_), last_(other.last_)
171 {
172  for (icst_ jsub = other.incr4d_.begin(); jsub != other.incr4d_.end(); ++jsub) {
173  int isub = jsub->first;
174  Increment_ * tmp = new Increment_(*jsub->second, copy);
175  incr4d_.insert(isub, tmp);
176  }
177  Log::trace() << "Increment4D:Increment4D copied." << std::endl;
178 }
179 // -----------------------------------------------------------------------------
180 // -----------------------------------------------------------------------------
181 template<typename MODEL>
183  : incr4d_(), first_(other.first_), last_(other.last_)
184 {
185  for (icst_ jsub = other.incr4d_.begin(); jsub != other.incr4d_.end(); ++jsub) {
186  int isub = jsub->first;
187  Increment_ * tmp = new Increment_(geom, *jsub->second);
188  incr4d_.insert(isub, tmp);
189  }
190  Log::trace() << "Increment4D:Increment4D copied." << std::endl;
191 }
192 // -----------------------------------------------------------------------------
193 template<typename MODEL>
195  iter_ it = incr4d_.find(ii);
196  ASSERT(it != incr4d_.end());
197  return *it->second;
198 }
199 // -----------------------------------------------------------------------------
200 template<typename MODEL>
201 const Increment<MODEL> & Increment4D<MODEL>::get(const int ii) const {
202  icst_ it = incr4d_.find(ii);
203  ASSERT(it != incr4d_.end());
204  return *it->second;
205 }
206 // -----------------------------------------------------------------------------
207 template<typename MODEL>
209 // -----------------------------------------------------------------------------
210 template<typename MODEL>
212  for (iter_ jsub = incr4d_.begin(); jsub != incr4d_.end(); ++jsub) {
213  jsub->second->zero();
214  }
215 }
216 // -----------------------------------------------------------------------------
217 template<typename MODEL>
219  for (iter_ jsub = incr4d_.begin(); jsub != incr4d_.end(); ++jsub) {
220  jsub->second->random();
221  }
222 }
223 // -----------------------------------------------------------------------------
224 template<typename MODEL>
226  for (iter_ jsub = incr4d_.begin(); jsub != incr4d_.end(); ++jsub) {
227  jsub->second->ones();
228  }
229 }
230 // -----------------------------------------------------------------------------
231 template<typename MODEL>
232 void Increment4D<MODEL>::dirac(std::vector<eckit::LocalConfiguration> confs) {
233  this->zero();
234  for (const auto & conf : confs) {
235  const util::DateTime date(conf.getString("date"));
236  for (iter_ jsub = incr4d_.begin(); jsub != incr4d_.end(); ++jsub) {
237  if (date == jsub->second->validTime()) {
238  jsub->second->dirac(conf);
239  }
240  }
241  }
242 }
243 // -----------------------------------------------------------------------------
244 template<typename MODEL>
245 void Increment4D<MODEL>::diff(const State4D_ & cv1, const State4D_ & cv2) {
246  for (iter_ jsub = incr4d_.begin(); jsub != incr4d_.end(); ++jsub) {
247  jsub->second->diff(cv1[jsub->first], cv2[jsub->first]);
248  }
249 }
250 // -----------------------------------------------------------------------------
251 template<typename MODEL>
253  incr4d_ = rhs.incr4d_;
254  return *this;
255 }
256 // -----------------------------------------------------------------------------
257 template<typename MODEL>
259  for (int jsub = rhs.first(); jsub <= rhs.last(); ++jsub) {
260  this->get(jsub) += rhs[jsub];
261  }
262  return *this;
263 }
264 // -----------------------------------------------------------------------------
265 template<typename MODEL>
267  for (int jsub = rhs.first(); jsub <= rhs.last(); ++jsub) {
268  this->get(jsub) -= rhs[jsub];
269  }
270  return *this;
271 }
272 // -----------------------------------------------------------------------------
273 template<typename MODEL>
275  for (iter_ jsub = incr4d_.begin(); jsub != incr4d_.end(); ++jsub) {
276  *jsub->second *= zz;
277  }
278  return *this;
279 }
280 // -----------------------------------------------------------------------------
281 template<typename MODEL>
282 void Increment4D<MODEL>::axpy(const double zz, const Increment4D & rhs, const bool check) {
283  for (iter_ jsub = incr4d_.begin(); jsub != incr4d_.end(); ++jsub) {
284  jsub->second->axpy(zz, rhs[jsub->first], check);
285  }
286 }
287 // -----------------------------------------------------------------------------
288 template<typename MODEL>
289 void Increment4D<MODEL>::read(const eckit::Configuration & config) {
290  for (iter_ jsub = incr4d_.begin(); jsub != incr4d_.end(); ++jsub) {
291  std::stringstream ss;
292  ss << jsub->first+1;
293  std::string query = "increment[@indx='" + ss.str() + "']";
294  eckit::LocalConfiguration fileConfig(config, query);
295  jsub->second->read(fileConfig);
296  Log::info() << "Increment4D:read increment" << *jsub->second << std::endl;
297  }
298 }
299 // -----------------------------------------------------------------------------
300 template<typename MODEL>
301 void Increment4D<MODEL>::write(const eckit::Configuration & config) const {
302  for (icst_ jsub = incr4d_.begin(); jsub != incr4d_.end(); ++jsub) {
303  jsub->second->write(config);
304  }
305 }
306 // -----------------------------------------------------------------------------
307 template<typename MODEL>
308 void Increment4D<MODEL>::setAtlas(atlas::FieldSet * afieldset) const {
309  for (icst_ jsub = incr4d_.begin(); jsub != incr4d_.end(); ++jsub) {
310  jsub->second->setAtlas(afieldset);
311  }
312 }
313 // -----------------------------------------------------------------------------
314 template<typename MODEL>
315 void Increment4D<MODEL>::toAtlas(atlas::FieldSet * afieldset) const {
316  for (icst_ jsub = incr4d_.begin(); jsub != incr4d_.end(); ++jsub) {
317  jsub->second->toAtlas(afieldset);
318  }
319 }
320 // -----------------------------------------------------------------------------
321 template<typename MODEL>
322 void Increment4D<MODEL>::fromAtlas(atlas::FieldSet * afieldset) {
323  for (iter_ jsub = incr4d_.begin(); jsub != incr4d_.end(); ++jsub) {
324  jsub->second->fromAtlas(afieldset);
325  }
326 }
327 // -----------------------------------------------------------------------------
328 template <typename MODEL>
329 void Increment4D<MODEL>::print(std::ostream & outs) const {
330  for (icst_ jsub = incr4d_.begin(); jsub != incr4d_.end(); ++jsub) {
331  outs << *jsub->second << std::endl;
332  }
333 }
334 // -----------------------------------------------------------------------------
335 template<typename MODEL>
337  double zz = 0.0;
338  for (icst_ jsub = incr4d_.begin(); jsub != incr4d_.end(); ++jsub) {
339  zz += dot_product(*jsub->second, x2[jsub->first]);
340  }
341  return zz;
342 }
343 // -----------------------------------------------------------------------------
344 template<typename MODEL>
346  for (iter_ jsub = incr4d_.begin(); jsub != incr4d_.end(); ++jsub) {
347  jsub->second->schur_product_with(x2[jsub->first]);
348  }
349 }
350 // -----------------------------------------------------------------------------
351 template<typename MODEL>
353  typedef typename boost::ptr_map<int, Increment_>::reverse_iterator rit;
354  for (rit jsub = incr4d_.rbegin(); jsub != incr4d_.rend(); ++jsub) {
355  const int isub = jsub->first;
356  if (isub > first_) this->get(isub) = this->get(isub-1);
357  }
358  incr4d_.erase(first_);
359  Log::info() << "Increment4D::shift_forward erased " << first_ << std::endl;
360  first_ += 1;
361 }
362 // -----------------------------------------------------------------------------
363 template<typename MODEL>
365  for (iter_ jsub = incr4d_.begin(); jsub != incr4d_.end(); ++jsub) {
366  const int isub = jsub->first;
367  if (isub < last_) this->get(isub) = this->get(isub+1);
368  }
369  incr4d_.erase(last_);
370  Log::info() << "Increment4D::shift_backward erased " << last_ << std::endl;
371  last_ -= 1;
372 }
373 // -----------------------------------------------------------------------------
374 template<typename MODEL>
376  size_t ss = 1;
377  for (icst_ jsub = incr4d_.begin(); jsub != incr4d_.end(); ++jsub) {
378  ss += jsub->second->serialSize();
379  ++ss;
380  }
381  return ss;
382 }
383 // -----------------------------------------------------------------------------
384 template<typename MODEL>
385 void Increment4D<MODEL>::serialize(std::vector<double> & vect) const {
386  vect.push_back(-98765.4321);
387  for (icst_ jsub = incr4d_.begin(); jsub != incr4d_.end(); ++jsub) {
388  jsub->second->serialize(vect);
389  vect.push_back(-98765.4321);
390  }
391 }
392 // -----------------------------------------------------------------------------
393 template<typename MODEL>
394 void Increment4D<MODEL>::deserialize(const std::vector<double> & vect, size_t & current) {
395  ASSERT(vect.at(current) == -98765.4321);
396  ++current;
397  for (iter_ jsub = incr4d_.begin(); jsub != incr4d_.end(); ++jsub) {
398  jsub->second->deserialize(vect, current);
399  ASSERT(vect.at(current) == -98765.4321);
400  ++current;
401  }
402 }
403 
404 // -----------------------------------------------------------------------------
405 } // namespace oops
406 
407 #endif // OOPS_ASSIMILATION_INCREMENT4D_H_
oops::Increment4D::classname
static const std::string classname()
Definition: Increment4D.h:56
oops
The namespace for the main oops code.
Definition: ErrorCovarianceL95.cc:22
oops::operator+=
State4D< MODEL > & operator+=(State4D< MODEL > &xx, const Increment4D< MODEL > &dx)
Definition: Increment4D.h:126
oops::Increment4D::incr4d_
boost::ptr_map< int, Increment_ > incr4d_
Definition: Increment4D.h:119
oops::Increment4D::fromAtlas
void fromAtlas(atlas::FieldSet *)
Definition: Increment4D.h:322
oops::Increment4D::operator[]
const Increment_ & operator[](const int ii) const
Definition: Increment4D.h:98
oops::Increment4D::Increment4D
Increment4D(const JbState_ &)
Constructor, destructor.
Definition: Increment4D.h:136
oops::Increment4D::random
void random()
Definition: Increment4D.h:218
oops::Increment4D::geometry
Geometry_ geometry() const
Get geometry.
Definition: Increment4D.h:89
oops::Increment::geometry
Geometry_ geometry() const
Get geometry.
Definition: oops/interface/Increment.h:397
oops::Increment4D::last
int last() const
Definition: Increment4D.h:100
oops::Increment4D::shift_forward
void shift_forward()
To be removed.
Definition: Increment4D.h:352
oops::Increment4D::get
Increment_ & get(const int)
Definition: Increment4D.h:194
oops::Increment4D::print
void print(std::ostream &) const override
Definition: Increment4D.h:329
oops::CostJbState
Jb Cost Function Base Class.
Definition: CostJbState.h:39
oops::Increment4D::serialSize
size_t serialSize() const override
Serialize and deserialize.
Definition: Increment4D.h:375
oops::CostJbState::newStateIncrement
virtual Increment_ * newStateIncrement() const =0
Create new increment (set to 0).
oops::Increment4D::JbState_
CostJbState< MODEL > JbState_
Definition: Increment4D.h:52
oops::Increment4D::serialize
void serialize(std::vector< double > &) const override
Definition: Increment4D.h:385
oops::Increment4D::iter_
boost::ptr_map< int, Increment_ >::iterator iter_
Definition: Increment4D.h:117
oops::Increment4D::incr4d
Increment_ & incr4d()
Interfacing.
Definition: Increment4D.h:67
oops::Increment4D::shift_backward
void shift_backward()
Definition: Increment4D.h:364
oops::Increment4D::State4D_
State4D< MODEL > State4D_
Definition: Increment4D.h:53
oops::Increment4D::incr4d
const Increment_ & incr4d() const
Definition: Increment4D.h:68
oops::Increment4D::zero
void zero()
Definition: Increment4D.h:211
oops::Increment4D::icst_
boost::ptr_map< int, Increment_ >::const_iterator icst_
Definition: Increment4D.h:118
oops::Increment4D::operator+=
Increment4D & operator+=(const Increment4D &)
Definition: Increment4D.h:258
oops::Increment4D::size
size_t size() const
Definition: Increment4D.h:101
oops::Increment4D::write
void write(const eckit::Configuration &) const
Definition: Increment4D.h:301
oops::Increment4D::dot_product_with
double dot_product_with(const Increment4D &) const
Definition: Increment4D.h:336
oops::Increment4D::Increment_
Increment< MODEL > Increment_
Definition: Increment4D.h:51
oops::Increment4D::first
int first() const
Definition: Increment4D.h:99
oops::Increment4D::operator*=
Increment4D & operator*=(const double)
Definition: Increment4D.h:274
oops::Increment4D::first_
int first_
Definition: Increment4D.h:120
CostJbState.h
oops::Increment4D::~Increment4D
~Increment4D()
Definition: Increment4D.h:208
oops::Increment4D::schur_product_with
void schur_product_with(const Increment4D &)
Definition: Increment4D.h:345
oops::Increment4D::ones
void ones()
Definition: Increment4D.h:225
oops::Increment4D::diff
void diff(const State4D_ &, const State4D_ &)
Linear algebra operators.
Definition: Increment4D.h:245
oops::Increment4D::deserialize
void deserialize(const std::vector< double > &, size_t &) override
Definition: Increment4D.h:394
oops::State4D
Four dimensional state.
Definition: State4D.h:35
oops::Geometry
Geometry class used in oops; subclass of interface class above.
Definition: oops/interface/Geometry.h:189
oops::Increment4D::dirac
void dirac(std::vector< eckit::LocalConfiguration >)
Definition: Increment4D.h:232
oops::State4D::size
size_t size() const
Definition: State4D.h:53
oops::Increment4D
State increment.
Definition: Increment4D.h:49
oops::Increment4D::operator-=
Increment4D & operator-=(const Increment4D &)
Definition: Increment4D.h:266
oops::Increment4D::operator=
Increment4D & operator=(const Increment4D &)
Definition: Increment4D.h:252
State4D.h
oops::Increment4D::toAtlas
void toAtlas(atlas::FieldSet *) const
Definition: Increment4D.h:315
oops::Increment4D::read
void read(const eckit::Configuration &)
I/O and diagnostics.
Definition: Increment4D.h:289
oops::Increment4D::operator[]
Increment_ & operator[](const int ii)
Get model space control variable.
Definition: Increment4D.h:97
oops::Variables
Definition: oops/base/Variables.h:23
oops::Increment4D::last_
int last_
Definition: Increment4D.h:121
oops::Increment
Increment Class: Difference between two states.
Definition: CostJbState.h:27
oops::Increment4D::axpy
void axpy(const double, const Increment4D &, const bool check=true)
Definition: Increment4D.h:282
Variables.h
oops::Increment4D::Geometry_
Geometry< MODEL > Geometry_
Definition: Increment4D.h:50
oops::Increment4D::setAtlas
void setAtlas(atlas::FieldSet *) const
ATLAS FieldSet.
Definition: Increment4D.h:308
Geometry.h
Increment.h