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