FV3 Bundle
CostJo.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_COSTJO_H_
12 #define OOPS_ASSIMILATION_COSTJO_H_
13 
14 #include <string>
15 #include <boost/noncopyable.hpp>
16 #include <boost/pointer_cast.hpp>
17 #include <boost/scoped_ptr.hpp>
18 #include <boost/shared_ptr.hpp>
19 
20 #include "eckit/config/LocalConfiguration.h"
24 #include "oops/base/Departures.h"
26 #include "oops/base/ObsErrors.h"
27 #include "oops/base/Observations.h"
28 #include "oops/base/Observer.h"
29 #include "oops/base/ObserverTLAD.h"
30 #include "oops/base/ObsFilters.h"
31 #include "oops/base/ObsOperators.h"
32 #include "oops/base/ObsSpaces.h"
33 #include "oops/base/PostBase.h"
34 #include "oops/base/PostBaseTLAD.h"
38 #include "oops/interface/State.h"
39 #include "oops/util/DateTime.h"
40 #include "oops/util/Duration.h"
41 #include "oops/util/Logger.h"
42 
43 namespace oops {
44 
45 // -----------------------------------------------------------------------------
46 
47 /// Jo Cost Function
48 /*!
49  * The CostJo class encapsulates the Jo term of the cost function.
50  * The Observer to be called during the model integration is managed
51  * inside the CostJo class.
52  */
53 
54 template<typename MODEL> class CostJo : public CostTermBase<MODEL>,
55  private boost::noncopyable {
70 
71  public:
72  /// Construct \f$ J_o\f$ from \f$ R\f$ and \f$ y_{obs}\f$.
73  CostJo(const eckit::Configuration &, const util::DateTime &, const util::DateTime &,
74  const util::Duration &, const bool subwindows = false);
75 
76  /// Destructor
77  virtual ~CostJo() {}
78 
79  /// Initialize \f$ J_o\f$ before starting the integration of the model.
80  boost::shared_ptr<PostBase<State_> > initialize(const CtrlVar_ &) const override;
81  boost::shared_ptr<PostBaseTLAD_> initializeTraj(const CtrlVar_ &,
82  const Geometry_ &,
83  const eckit::Configuration &) override;
84  /// Finalize \f$ J_o\f$ after the integration of the model.
85  double finalize(const eckit::Configuration &) const override;
86  void finalizeTraj() override;
87 
88  /// Initialize \f$ J_o\f$ before starting the TL run.
89  boost::shared_ptr<PostBaseTLAD_> setupTL(const CtrlInc_ &) const override;
90 
91  /// Initialize \f$ J_o\f$ before starting the AD run.
92  boost::shared_ptr<PostBaseTLAD_> setupAD(
93  boost::shared_ptr<const GeneralizedDepartures>, CtrlInc_ &) const override;
94 
95  /// Multiply by \f$ R\f$ and \f$ R^{-1}\f$.
96  Departures_ * multiplyCovar(const GeneralizedDepartures &) const override;
97  Departures_ * multiplyCoInv(const GeneralizedDepartures &) const override;
98 
99  /// Provide new departure.
100  Departures_ * newDualVector() const override;
101 
102  /// Return gradient at first guess ie \f$ R^{-1} {\cal H}(x^t ) - y\f$.
103  Departures_ * newGradientFG() const override {return new Departures_(*gradFG_);}
104 
105  /// Reset obs operator trajectory.
106  void resetLinearization() override;
107 
108  /// Print Jo
109  double printJo(const Departures_ &, const Departures_ &) const;
110 
111  private:
116 
117  /// Gradient at first guess : \f$ R^{-1} (H(x_{fg})-y_{obs}) \f$.
118  boost::scoped_ptr<Departures_> gradFG_;
119 
120  /// Observer passed by \f$ J_o\f$ to the model during integration.
121  mutable boost::shared_ptr<Observer<MODEL, State_> > pobs_;
122 
123  /// Time slot.
124  const util::Duration tslot_;
125 
126  /// Linearized observation operators.
127  boost::shared_ptr<ObserverTLAD_> pobstlad_;
128  const bool subwindows_;
129 };
130 
131 // =============================================================================
132 
133 template<typename MODEL>
134 CostJo<MODEL>::CostJo(const eckit::Configuration & joConf,
135  const util::DateTime & winbgn, const util::DateTime & winend,
136  const util::Duration & tslot, const bool subwindows)
137  : obspace_(joConf, winbgn, winend),
138  hop_(obspace_), yobs_(obspace_), R_(obspace_),
139  gradFG_(), pobs_(), tslot_(tslot),
140  pobstlad_(), subwindows_(subwindows)
141 {
142  Log::trace() << "CostJo::CostJo start" << std::endl;
143  Log::debug() << "CostJo:setup tslot_ = " << tslot_ << std::endl;
144  yobs_.read(joConf);
145  Log::trace() << "CostJo::CostJo done" << std::endl;
146 }
147 
148 // -----------------------------------------------------------------------------
149 
150 template<typename MODEL>
151 boost::shared_ptr<PostBase<State<MODEL> > >
153  Log::trace() << "CostJo::initialize start" << std::endl;
154  const eckit::LocalConfiguration conf;
155  ObsFilters_ filter(obspace_, conf);
156  pobs_.reset(new Observer<MODEL, State_>(obspace_, hop_, xx.obsVar(), filter,
157  tslot_, subwindows_));
158  Log::trace() << "CostJo::initialize done" << std::endl;
159  return pobs_;
160 }
161 
162 // -----------------------------------------------------------------------------
163 
164 template<typename MODEL>
165 double CostJo<MODEL>::finalize(const eckit::Configuration & conf) const {
166  Log::trace() << "CostJo::finalize start" << std::endl;
167  boost::scoped_ptr<Observations_> yeqv(pobs_->release());
168  Log::info() << "Jo Observation Equivalent:" << *yeqv << std::endl;
169 
170  Departures_ ydep(*yeqv - yobs_);
171  Log::info() << "Jo Departures:" << ydep << std::endl;
172 
173  boost::scoped_ptr<Departures_> grad(R_.inverseMultiply(ydep));
174 
175  double zjo = this->printJo(ydep, *grad);
176 
177  if (conf.has("departures")) {
178  const std::string depname = conf.getString("departures");
179  ydep.save(depname);
180  }
181 
182  pobs_.reset();
183  Log::trace() << "CostJo::finalize done" << std::endl;
184  return zjo;
185 }
186 
187 // -----------------------------------------------------------------------------
188 
189 template<typename MODEL>
190 boost::shared_ptr<PostBaseTLAD<MODEL> >
192  const eckit::Configuration & conf) {
193  Log::trace() << "CostJo::initializeTraj start" << std::endl;
194  ObsFilters_ filter(obspace_, conf);
195  pobstlad_.reset(new ObserverTLAD_(obspace_, hop_, xx.obsVar(), filter,
196  tslot_, subwindows_));
197  Log::trace() << "CostJo::initializeTraj done" << std::endl;
198  return pobstlad_;
199 }
200 
201 // -----------------------------------------------------------------------------
202 
203 template<typename MODEL>
205  Log::trace() << "CostJo::finalizeTraj start" << std::endl;
206  boost::scoped_ptr<Observations_> yeqv(pobstlad_->release());
207  Log::info() << "Jo Traj Observation Equivalent:" << *yeqv << std::endl;
208  R_.linearize(*yeqv);
209 
210  Departures_ ydep(*yeqv - yobs_);
211  Log::info() << "Jo Traj Departures:" << ydep << std::endl;
212 
213  gradFG_.reset(R_.inverseMultiply(ydep));
214  Log::trace() << "CostJo::finalizeTraj done" << std::endl;
215 }
216 
217 // -----------------------------------------------------------------------------
218 
219 template<typename MODEL>
220 boost::shared_ptr<PostBaseTLAD<MODEL> > CostJo<MODEL>::setupTL(const CtrlInc_ & dx) const {
221  Log::trace() << "CostJo::setupTL start" << std::endl;
222  ASSERT(pobstlad_);
223  pobstlad_->setupTL(dx.obsVar());
224  Log::trace() << "CostJo::setupTL done" << std::endl;
225  return pobstlad_;
226 }
227 
228 // -----------------------------------------------------------------------------
229 
230 template<typename MODEL>
231 boost::shared_ptr<PostBaseTLAD<MODEL> > CostJo<MODEL>::setupAD(
232  boost::shared_ptr<const GeneralizedDepartures> pv,
233  CtrlInc_ & dx) const {
234  Log::trace() << "CostJo::setupAD start" << std::endl;
235  ASSERT(pobstlad_);
236  boost::shared_ptr<const Departures_> dy = boost::dynamic_pointer_cast<const Departures_>(pv);
237  pobstlad_->setupAD(dy, dx.obsVar());
238  Log::trace() << "CostJo::setupAD done" << std::endl;
239  return pobstlad_;
240 }
241 
242 // -----------------------------------------------------------------------------
243 
244 template<typename MODEL>
246  Log::trace() << "CostJo::multiplyCovar start" << std::endl;
247  const Departures_ & y1 = dynamic_cast<const Departures_ &>(v1);
248  return R_.multiply(y1);
249 }
250 
251 // -----------------------------------------------------------------------------
252 
253 template<typename MODEL>
255  Log::trace() << "CostJo::multiplyCoInv start" << std::endl;
256  const Departures_ & y1 = dynamic_cast<const Departures_ &>(v1);
257  return R_.inverseMultiply(y1);
258 }
259 
260 // -----------------------------------------------------------------------------
261 
262 template<typename MODEL>
264  Log::trace() << "CostJo::newDualVector start" << std::endl;
265  Departures_ * ydep = new Departures_(obspace_);
266  ydep->zero();
267  Log::trace() << "CostJo::newDualVector done" << std::endl;
268  return ydep;
269 }
270 
271 // -----------------------------------------------------------------------------
272 
273 template<typename MODEL>
275  Log::trace() << "CostJo::resetLinearization start" << std::endl;
276  pobstlad_.reset();
277  Log::trace() << "CostJo::resetLinearization done" << std::endl;
278 }
279 
280 // -----------------------------------------------------------------------------
281 
282 template<typename MODEL>
283 double CostJo<MODEL>::printJo(const Departures_ & dy, const Departures_ & grad) const {
284  Log::trace() << "CostJo::printJo start" << std::endl;
285  obspace_.printJo(dy, grad);
286 
287  double zjo = 0.0;
288  for (std::size_t jj = 0; jj < dy.size(); ++jj) {
289  const double zz = 0.5 * dot_product(dy[jj], grad[jj]);
290  const unsigned nobs = dy[jj].size();
291  if (nobs > 0) {
292  Log::test() << "CostJo : Nonlinear Jo = " << zz
293  << ", nobs = " << nobs << ", Jo/n = " << zz/nobs
294  << ", err = " << R_[jj].getRMSE() << std::endl;
295  } else {
296  Log::test() << "CostJo : Nonlinear Jo = " << zz << " --- No Observations" << std::endl;
297  Log::warning() << "CostJo: No Observations!!!" << std::endl;
298  }
299  zjo += zz;
300  }
301 
302  Log::trace() << "CostJo::printJo done" << std::endl;
303  return zjo;
304 }
305 
306 // -----------------------------------------------------------------------------
307 
308 } // namespace oops
309 
310 #endif // OOPS_ASSIMILATION_COSTJO_H_
boost::shared_ptr< PostBaseTLAD_ > setupAD(boost::shared_ptr< const GeneralizedDepartures >, CtrlInc_ &) const override
Initialize before starting the AD run.
Definition: CostJo.h:231
ControlVariable< MODEL > CtrlVar_
Definition: CostJo.h:56
CostJo(const eckit::Configuration &, const util::DateTime &, const util::DateTime &, const util::Duration &, const bool subwindows=false)
Construct from and .
Definition: CostJo.h:134
double printJo(const Departures_ &, const Departures_ &) const
Print Jo.
Definition: CostJo.h:283
LinearObsOperators< MODEL > LinearObsOperator_
Definition: CostJo.h:69
integer, parameter, public warning
void resetLinearization() override
Reset obs operator trajectory.
Definition: CostJo.h:274
double finalize(const eckit::Configuration &) const override
Finalize after the integration of the model.
Definition: CostJo.h:165
ObserverTLAD< MODEL > ObserverTLAD_
Definition: CostJo.h:67
Observations< MODEL > Observations_
Definition: CostJo.h:59
ObsAuxIncr_ & obsVar()
Get augmented observation control variable.
Difference between two observation vectors.
Definition: Departures.h:36
Geometry< MODEL > Geometry_
Definition: CostJo.h:60
Definition: conf.py:1
PostBaseTLAD< MODEL > PostBaseTLAD_
Definition: CostJo.h:68
const ObsOperator_ hop_
Definition: CostJo.h:113
Handles post-processing of model fields related to cost function.
Definition: PostBaseTLAD.h:39
virtual ~CostJo()
Destructor.
Definition: CostJo.h:77
Encapsulates the model state.
Observations Class.
Definition: Observations.h:36
program test
Control variable.
Departures_ * newGradientFG() const override
Return gradient at first guess ie .
Definition: CostJo.h:103
The namespace for the main oops code.
Observations_ yobs_
Definition: CostJo.h:114
void save(const std::string &) const
Save departures values.
Definition: Departures.h:205
logical debug
Definition: mpp.F90:1297
boost::shared_ptr< PostBaseTLAD_ > setupTL(const CtrlInc_ &) const override
Initialize before starting the TL run.
Definition: CostJo.h:220
boost::shared_ptr< PostBaseTLAD_ > initializeTraj(const CtrlVar_ &, const Geometry_ &, const eckit::Configuration &) override
Definition: CostJo.h:191
subroutine, public info(self)
ObsOperators< MODEL > ObsOperator_
Definition: CostJo.h:65
Increment< MODEL > Increment_
Definition: CostJo.h:62
ObsSpaces< MODEL > ObsSpace_
Definition: CostJo.h:66
Departures_ * newDualVector() const override
Provide new departure.
Definition: CostJo.h:263
ObsAuxIncrement< MODEL > ObsAuxIncr_
Definition: CostJo.h:63
Computes observation equivalent during model run.
Definition: Observer.h:43
Increment Class: Difference between two states.
Jo Cost Function.
Definition: CostJo.h:54
ControlIncrement< MODEL > CtrlInc_
Definition: CostJo.h:57
Base Class for Cost Function Terms.
Definition: CostTermBase.h:37
Departures< MODEL > Departures_
Definition: CostJo.h:58
void read(const eckit::Configuration &)
Definition: Observations.h:136
State< MODEL > State_
Definition: CostJo.h:61
boost::shared_ptr< Observer< MODEL, State_ > > pobs_
Observer passed by to the model during integration.
Definition: CostJo.h:121
Abstract base class for quantities.
Departures_ * multiplyCovar(const GeneralizedDepartures &) const override
Multiply by and .
Definition: CostJo.h:245
boost::shared_ptr< ObserverTLAD_ > pobstlad_
Linearized observation operators.
Definition: CostJo.h:127
ObsErrors< MODEL > R_
Definition: CostJo.h:115
boost::shared_ptr< PostBase< State_ > > initialize(const CtrlVar_ &) const override
Initialize before starting the integration of the model.
Definition: CostJo.h:152
const util::Duration tslot_
Time slot.
Definition: CostJo.h:124
Departures_ * multiplyCoInv(const GeneralizedDepartures &) const override
Definition: CostJo.h:254
const bool subwindows_
Definition: CostJo.h:128
ObsFilters< MODEL > ObsFilters_
Definition: CostJo.h:64
void finalizeTraj() override
Definition: CostJo.h:204
ObsAuxCtrl_ & obsVar()
Get augmented observation control variable.
const ObsSpace_ obspace_
Definition: CostJo.h:112
boost::scoped_ptr< Departures_ > gradFG_
Gradient at first guess : .
Definition: CostJo.h:118
std::size_t size() const
Access.
Definition: Departures.h:66
Computes observation equivalent TL and AD to/from increments.
Definition: ObserverTLAD.h:41