FV3 Bundle
CostJcDFI.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_COSTJCDFI_H_
12 #define OOPS_ASSIMILATION_COSTJCDFI_H_
13 
14 #include <boost/pointer_cast.hpp>
15 #include <boost/scoped_ptr.hpp>
16 #include <boost/shared_ptr.hpp>
17 
18 #include "eckit/config/LocalConfiguration.h"
23 #include "oops/base/PostBase.h"
24 #include "oops/base/PostBaseTLAD.h"
25 #include "oops/base/Variables.h"
26 #include "oops/base/WeightedDiff.h"
28 #include "oops/base/WeightingFct.h"
31 #include "oops/interface/State.h"
32 #include "oops/util/DateTime.h"
33 #include "oops/util/Duration.h"
34 #include "oops/util/Logger.h"
35 
36 namespace oops {
37 
38 // -----------------------------------------------------------------------------
39 
40 /// Jc DFI Cost Function
41 /*!
42  * Digital filter based constraint term for the cost function.
43  */
44 
45 template<typename MODEL> class CostJcDFI : public CostTermBase<MODEL> {
52 
53  public:
54 /// Construct \f$ J_c\f$.
55  CostJcDFI(const eckit::Configuration &, const Geometry_ &, const util::DateTime &,
56  const util::Duration &, const util::Duration & tstep = util::Duration(0));
57 
58 /// Destructor
59  virtual ~CostJcDFI() {}
60 
61 /// Initialize before nonlinear model integration.
62  boost::shared_ptr<PostBase<State_> > initialize(const CtrlVar_ &) const override;
63  boost::shared_ptr<PostBaseTLAD<MODEL> > initializeTraj(const CtrlVar_ &,
64  const Geometry_ &,
65  const eckit::Configuration &) override;
66 
67 /// Finalize computation after nonlinear model integration.
68  double finalize(const eckit::Configuration &) const override;
69  void finalizeTraj() override;
70 
71 /// Initialize \f$ J_c\f$ before starting the TL run.
72  boost::shared_ptr<PostBaseTLAD_> setupTL(const CtrlInc_ &) const override;
73 
74 /// Initialize \f$ J_c\f$ before starting the AD run.
75  boost::shared_ptr<PostBaseTLAD_> setupAD(
76  boost::shared_ptr<const GeneralizedDepartures>, CtrlInc_ &) const override;
77 
78 /// Multiply by \f$ C\f$ and \f$ C^{-1}\f$.
79  Increment_ * multiplyCovar(const GeneralizedDepartures &) const override;
80  Increment_ * multiplyCoInv(const GeneralizedDepartures &) const override;
81 
82 /// Provide new increment.
83  Increment_ * newDualVector() const override;
84 
85 /// Gradient of \f$ J_c\f$ at first guess.
86  Increment_ * newGradientFG() const override {return new Increment_(*gradFG_);}
87 
88 /// Reset trajectory.
89  void resetLinearization() override;
90 
91  private:
92  const eckit::LocalConfiguration conf_;
93  util::DateTime vt_;
94  util::Duration span_;
95  double alpha_;
96  boost::scoped_ptr<WeightingFct> wfct_;
97  boost::scoped_ptr<Increment_> gradFG_;
99  const util::Duration tstep_;
100  boost::scoped_ptr<Geometry_> tlres_;
101  util::Duration tlstep_;
102  mutable boost::shared_ptr<WeightedDiff<MODEL, Increment_, State_> > filter_;
103  mutable boost::shared_ptr<WeightedDiffTLAD<MODEL> > ftlad_;
104 };
105 
106 // =============================================================================
107 
108 template<typename MODEL>
109 CostJcDFI<MODEL>::CostJcDFI(const eckit::Configuration & conf, const Geometry_ & resol,
110  const util::DateTime & vt, const util::Duration & span,
111  const util::Duration & tstep)
112  : conf_(conf), vt_(vt), span_(span), alpha_(0), wfct_(), gradFG_(),
113  resol_(resol), tstep_(tstep), tlres_(), tlstep_(), filter_()
114 {
115  alpha_ = conf.getDouble("alpha");
116  if (conf.has("ftime")) vt_ = util::DateTime(conf.getString("ftime"));
117  if (conf.has("span")) span_ = util::Duration(conf.getString("span"));
118 // wfct_.reset(WeightFactory::create(config)); YT
119  wfct_.reset(new DolphChebyshev(conf_));
120  Log::debug() << "CostJcDFI created vt = " << vt_ << ", span = " << span_ << std::endl;
121  Log::trace() << "CostJcDFI created" << std::endl;
122 }
123 
124 // -----------------------------------------------------------------------------
125 
126 template<typename MODEL>
127 boost::shared_ptr<PostBase<State<MODEL> > >
129  filter_.reset(new WeightedDiff<MODEL, Increment_, State_>(conf_, vt_, span_,
130  tstep_, resol_, *wfct_));
131  return filter_;
132 }
133 
134 // -----------------------------------------------------------------------------
135 
136 template<typename MODEL>
137 double CostJcDFI<MODEL>::finalize(const eckit::Configuration &) const {
138  double zz = 0.5 * alpha_;
139  boost::scoped_ptr<Increment_> dx(filter_->releaseDiff());
140  zz *= dot_product(*dx, *dx);
141  Log::test() << "CostJcDFI: Nonlinear Jc = " << zz << std::endl;
142  return zz;
143 }
144 
145 // -----------------------------------------------------------------------------
146 
147 template<typename MODEL>
148 boost::shared_ptr<PostBaseTLAD<MODEL> >
150  const eckit::Configuration & innerConf) {
151  tlres_.reset(new Geometry_(tlres));
152  tlstep_ = util::Duration(innerConf.getString("linearmodel.tstep"));
153  ftlad_.reset(new WeightedDiffTLAD<MODEL>(conf_, vt_, span_, tstep_, *tlres_, *wfct_));
154  return ftlad_;
155 }
156 
157 // -----------------------------------------------------------------------------
158 
159 template<typename MODEL>
161  gradFG_.reset(ftlad_->releaseDiff());
162  *gradFG_ *= alpha_;
163 }
164 
165 // -----------------------------------------------------------------------------
166 
167 template<typename MODEL>
169  const Variables vars(conf_);
170  Increment_ * dx = new Increment_(*tlres_, vars, vt_);
171  return dx;
172 }
173 
174 // -----------------------------------------------------------------------------
175 
176 template<typename MODEL>
177 boost::shared_ptr<PostBaseTLAD<MODEL> >
179  ftlad_->setupTL(*tlres_);
180  return ftlad_;
181 }
182 
183 // -----------------------------------------------------------------------------
184 
185 template<typename MODEL>
186 boost::shared_ptr<PostBaseTLAD<MODEL> >
187 CostJcDFI<MODEL>::setupAD(boost::shared_ptr<const GeneralizedDepartures> pv,
188  CtrlInc_ &) const {
189  boost::shared_ptr<const Increment_>
190  dx = boost::dynamic_pointer_cast<const Increment_>(pv);
191  ftlad_->setupAD(dx);
192  return ftlad_;
193 }
194 
195 // -----------------------------------------------------------------------------
196 
197 template<typename MODEL>
199  const Increment_ & dx1 = dynamic_cast<const Increment_ &>(dv1);
200  Increment_ * dx2 = new Increment_(dx1);
201  const double za = 1.0/alpha_;
202  *dx2 *= za;
203  return dx2;
204 }
205 
206 // -----------------------------------------------------------------------------
207 
208 template<typename MODEL>
210  const Increment_ & dx1 = dynamic_cast<const Increment_ &>(dv1);
211  Increment_ * dx2 = new Increment_(dx1);
212  *dx2 *= alpha_;
213  return dx2;
214 }
215 
216 // -----------------------------------------------------------------------------
217 
218 template<typename MODEL>
220  gradFG_.reset();
221  ftlad_.reset();
222 }
223 
224 // -----------------------------------------------------------------------------
225 
226 } // namespace oops
227 
228 #endif // OOPS_ASSIMILATION_COSTJCDFI_H_
PostBaseTLAD< MODEL > PostBaseTLAD_
Definition: CostJcDFI.h:50
boost::shared_ptr< PostBase< State_ > > initialize(const CtrlVar_ &) const override
Initialize before nonlinear model integration.
Definition: CostJcDFI.h:128
boost::shared_ptr< WeightedDiffTLAD< MODEL > > ftlad_
Definition: CostJcDFI.h:103
double alpha_
Definition: CostJcDFI.h:95
State< MODEL > State_
Definition: CostJcDFI.h:51
double finalize(const eckit::Configuration &) const override
Finalize computation after nonlinear model integration.
Definition: CostJcDFI.h:137
Compute time average of states or increments during model run.
Definition: WeightedDiff.h:41
Definition: conf.py:1
Increment_ * multiplyCoInv(const GeneralizedDepartures &) const override
Definition: CostJcDFI.h:209
Handles post-processing of model fields related to cost function.
Definition: PostBaseTLAD.h:39
Increment_ * multiplyCovar(const GeneralizedDepartures &) const override
Multiply by and .
Definition: CostJcDFI.h:198
Encapsulates the model state.
Increment< MODEL > Increment_
Definition: CostJcDFI.h:49
program test
Jc DFI Cost Function.
Definition: CostJcDFI.h:45
Control variable.
CostJcDFI(const eckit::Configuration &, const Geometry_ &, const util::DateTime &, const util::Duration &, const util::Duration &tstep=util::Duration(0))
Construct .
Definition: CostJcDFI.h:109
The namespace for the main oops code.
const Geometry_ resol_
Definition: CostJcDFI.h:98
logical debug
Definition: mpp.F90:1297
boost::scoped_ptr< Geometry_ > tlres_
Definition: CostJcDFI.h:100
real, dimension(:,:,:), allocatable vt
util::Duration tlstep_
Definition: CostJcDFI.h:101
boost::shared_ptr< WeightedDiff< MODEL, Increment_, State_ > > filter_
Definition: CostJcDFI.h:102
virtual ~CostJcDFI()
Destructor.
Definition: CostJcDFI.h:59
boost::shared_ptr< PostBaseTLAD_ > setupAD(boost::shared_ptr< const GeneralizedDepartures >, CtrlInc_ &) const override
Initialize before starting the AD run.
Definition: CostJcDFI.h:187
util::DateTime vt_
Definition: CostJcDFI.h:93
const util::Duration tstep_
Definition: CostJcDFI.h:99
Increment Class: Difference between two states.
boost::scoped_ptr< WeightingFct > wfct_
Definition: CostJcDFI.h:96
Increment_ * newGradientFG() const override
Gradient of at first guess.
Definition: CostJcDFI.h:86
Base Class for Cost Function Terms.
Definition: CostTermBase.h:37
boost::scoped_ptr< Increment_ > gradFG_
Definition: CostJcDFI.h:97
boost::shared_ptr< PostBaseTLAD_ > setupTL(const CtrlInc_ &) const override
Initialize before starting the TL run.
Definition: CostJcDFI.h:178
Geometry< MODEL > Geometry_
Definition: CostJcDFI.h:48
ControlIncrement< MODEL > CtrlInc_
Definition: CostJcDFI.h:46
Increment_ * newDualVector() const override
Provide new increment.
Definition: CostJcDFI.h:168
Compute time average of states or increments during linear model run.
const eckit::LocalConfiguration conf_
Definition: CostJcDFI.h:92
Abstract base class for quantities.
util::Duration span_
Definition: CostJcDFI.h:94
void finalizeTraj() override
Definition: CostJcDFI.h:160
boost::shared_ptr< PostBaseTLAD< MODEL > > initializeTraj(const CtrlVar_ &, const Geometry_ &, const eckit::Configuration &) override
Definition: CostJcDFI.h:149
ControlVariable< MODEL > CtrlVar_
Definition: CostJcDFI.h:47
void resetLinearization() override
Reset trajectory.
Definition: CostJcDFI.h:219