FV3 Bundle
Departures.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_BASE_DEPARTURES_H_
12 #define OOPS_BASE_DEPARTURES_H_
13 
14 #include <cstddef>
15 #include <iostream>
16 #include <string>
17 #include <vector>
18 
19 #include <boost/shared_ptr.hpp>
20 
22 #include "oops/base/ObsSpaces.h"
23 #include "oops/interface/GeoVaLs.h"
28 #include "oops/util/DateTime.h"
29 #include "oops/util/dot_product.h"
30 #include "oops/util/Duration.h"
31 #include "oops/util/Logger.h"
32 #include "oops/util/Printable.h"
33 
34 namespace oops {
35 
36 template<typename MODEL> class Departures;
37 
38 /// Difference between two observation vectors.
39 /*!
40  * A departure is the difference between two observations.
41  * The archetypal example is \f$ \mathbf{y} - {\cal H}(\mathbf{x}) \f$.
42  *
43  * Keeping an observation space vector here is necessary for the implementation
44  * of generic observation error covariance matrices.
45  */
46 
47 // -----------------------------------------------------------------------------
48 template <typename MODEL>
49 class Departures : public util::Printable,
50  public GeneralizedDepartures {
57 
58  public:
59 // Constructors and destructor
60  explicit Departures(const ObsSpace_ &);
61  explicit Departures(std::vector<boost::shared_ptr<ObsVector_> >);
62  explicit Departures(const Departures &);
63  ~Departures();
64 
65 /// Access
66  std::size_t size() const {return dep_.size();}
67  ObsVector_ & operator[](const std::size_t ii) {return *dep_.at(ii);}
68  const ObsVector_ & operator[](const std::size_t ii) const {return *dep_.at(ii);}
69 
70 // Linear algebra operators
71  Departures & operator=(const Departures &);
72  Departures & operator+=(const Departures &);
73  Departures & operator-=(const Departures &);
74  Departures & operator*=(const double &);
75  Departures & operator*=(const Departures &);
76  Departures & operator/=(const Departures &);
77  void zero();
78  void invert();
79  void axpy(const double &, const Departures &);
80  double dot_product_with(const Departures &) const;
81 
82 /// Save departures values
83  void save(const std::string &) const;
84 
85  private:
86  void print(std::ostream &) const;
87 
88  std::vector<boost::shared_ptr<ObsVector_> > dep_;
89 };
90 
91 // =============================================================================
92 
93 template<typename MODEL>
94 Departures<MODEL>::Departures(const ObsSpace_ & obsgeom): dep_(0)
95 {
96  for (std::size_t jj = 0; jj < obsgeom.size(); ++jj) {
97  boost::shared_ptr<ObsVector_> tmp(new ObsVector_(obsgeom[jj]));
98  dep_.push_back(tmp);
99  }
100  Log::trace() << "Departures created" << std::endl;
101 }
102 // -----------------------------------------------------------------------------
103 template<typename MODEL>
104 Departures<MODEL>::Departures(std::vector<boost::shared_ptr<ObsVector_> > val)
105  : dep_(val)
106 {
107  Log::trace() << "Departures created" << std::endl;
108 }
109 // -----------------------------------------------------------------------------
110 template<typename MODEL>
112 {
113 // We want deep copy here
114  for (std::size_t jj = 0; jj < other.dep_.size(); ++jj) {
115  boost::shared_ptr<ObsVector_> tmp(new ObsVector_(*other.dep_[jj]));
116  dep_.push_back(tmp);
117  }
118  Log::trace() << "Departures copy-created" << std::endl;
119 }
120 // -----------------------------------------------------------------------------
121 template<typename MODEL>
123  Log::trace() << "Departures destructed" << std::endl;
124 }
125 // -----------------------------------------------------------------------------
126 template<typename MODEL>
128  for (std::size_t jj = 0; jj < dep_.size(); ++jj) {
129  *dep_[jj] = *rhs.dep_[jj];
130  }
131  return *this;
132 }
133 // -----------------------------------------------------------------------------
134 template<typename MODEL>
136  for (std::size_t jj = 0; jj < dep_.size(); ++jj) {
137  *dep_[jj] += *rhs.dep_[jj];
138  }
139  return *this;
140 }
141 // -----------------------------------------------------------------------------
142 template<typename MODEL>
144  for (std::size_t jj = 0; jj < dep_.size(); ++jj) {
145  *dep_[jj] -= *rhs.dep_[jj];
146  }
147  return *this;
148 }
149 // -----------------------------------------------------------------------------
150 template<typename MODEL>
152  for (std::size_t jj = 0; jj < dep_.size(); ++jj) {
153  *dep_[jj] *= zz;
154  }
155  return *this;
156 }
157 // -----------------------------------------------------------------------------
158 template<typename MODEL>
160  for (std::size_t jj = 0; jj < dep_.size(); ++jj) {
161  *dep_[jj] *= *rhs.dep_[jj];
162  }
163  return *this;
164 }
165 // -----------------------------------------------------------------------------
166 template<typename MODEL>
168  for (std::size_t jj = 0; jj < dep_.size(); ++jj) {
169  *dep_[jj] /= *rhs.dep_[jj];
170  }
171  return *this;
172 }
173 // -----------------------------------------------------------------------------
174 template<typename MODEL>
176  for (std::size_t jj = 0; jj < dep_.size(); ++jj) {
177  dep_[jj]->zero();
178  }
179 }
180 // -----------------------------------------------------------------------------
181 template<typename MODEL>
183  for (std::size_t jj = 0; jj < dep_.size(); ++jj) {
184  dep_[jj]->invert();
185  }
186 }
187 // -----------------------------------------------------------------------------
188 template<typename MODEL>
189 void Departures<MODEL>::axpy(const double & zz, const Departures & rhs) {
190  for (std::size_t jj = 0; jj < dep_.size(); ++jj) {
191  dep_[jj]->axpy(zz, *rhs.dep_[jj]);
192  }
193 }
194 // -----------------------------------------------------------------------------
195 template<typename MODEL>
196 double Departures<MODEL>::dot_product_with(const Departures & other) const {
197  double zz = 0.0;
198  for (std::size_t jj = 0; jj < dep_.size(); ++jj) {
199  zz += dot_product(*dep_[jj], *other.dep_[jj]);
200  }
201  return zz;
202 }
203 // -----------------------------------------------------------------------------
204 template <typename MODEL>
205 void Departures<MODEL>::save(const std::string & name) const {
206  for (std::size_t jj = 0; jj < dep_.size(); ++jj) {
207  dep_[jj]->save(name);
208  }
209 }
210 // -----------------------------------------------------------------------------
211 template <typename MODEL>
212 void Departures<MODEL>::print(std::ostream & os) const {
213  for (std::size_t jj = 0; jj < dep_.size(); ++jj) {
214  os << *dep_[jj];
215  }
216 }
217 // -----------------------------------------------------------------------------
218 } // namespace oops
219 
220 #endif // OOPS_BASE_DEPARTURES_H_
ObsSpaces< MODEL > ObsSpace_
Definition: Departures.h:55
Departures(const ObsSpace_ &)
Definition: Departures.h:94
ObsAuxIncrement< MODEL > ObsAuxIncr_
Definition: Departures.h:52
Departures & operator=(const Departures &)
Definition: Departures.h:127
void axpy(const double &, const Departures &)
Definition: Departures.h:189
Difference between two observation vectors.
Definition: Departures.h:36
const ObsVector_ & operator[](const std::size_t ii) const
Definition: Departures.h:68
GeoVaLs< MODEL > GeoVaLs_
Definition: Departures.h:51
character(len=32) name
The namespace for the main oops code.
void save(const std::string &) const
Save departures values.
Definition: Departures.h:205
void print(std::ostream &) const
Definition: Departures.h:212
Departures & operator*=(const double &)
Definition: Departures.h:151
ObsVector< MODEL > ObsVector_
Definition: Departures.h:56
Departures & operator/=(const Departures &)
Definition: Departures.h:167
Abstract base class for quantities.
ObsErrorBase< MODEL > ObsErrorBase_
Definition: Departures.h:53
Base class for observation error covariance matrices.
Definition: ObsErrorBase.h:39
double dot_product_with(const Departures &) const
Definition: Departures.h:196
ObsVector_ & operator[](const std::size_t ii)
Definition: Departures.h:67
std::vector< boost::shared_ptr< ObsVector_ > > dep_
Definition: Departures.h:88
Departures & operator+=(const Departures &)
Definition: Departures.h:135
std::size_t size() const
Access.
Definition: ObsSpaces.h:51
Departures & operator-=(const Departures &)
Definition: Departures.h:143
std::size_t size() const
Access.
Definition: Departures.h:66
LinearObsOperator< MODEL > LinearObsOperator_
Definition: Departures.h:54