OOPS
CostFct3DVar.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_COSTFCT3DVAR_H_
12 #define OOPS_ASSIMILATION_COSTFCT3DVAR_H_
13 
14 #include <memory>
15 
16 #include "eckit/config/Configuration.h"
17 #include "eckit/mpi/Comm.h"
22 #include "oops/base/Geometry.h"
23 #include "oops/base/Increment.h"
26 #include "oops/base/State.h"
28 #include "oops/base/Variables.h"
29 #include "oops/util/DateTime.h"
30 #include "oops/util/Duration.h"
31 #include "oops/util/Logger.h"
32 
33 namespace oops {
34 
35 /// 3D-Var Cost Function
36 /*!
37  * This class is not really necessary since it is only a special
38  * case of the more general 4D-Var cost function. It is provided
39  * for readability.
40  */
41 
42 // -----------------------------------------------------------------------------
43 
44 template<typename MODEL, typename OBS> class CostFct3DVar : public CostFunction<MODEL, OBS> {
51 
52  public:
53  CostFct3DVar(const eckit::Configuration &, const eckit::mpi::Comm &);
54  virtual ~CostFct3DVar() {}
55 
57  PostProcessor<Increment_>, const bool) const override;
59  PostProcessor<Increment_>, const bool) const override;
60  void zeroAD(CtrlInc_ &) const override;
61 
62  void runNL(CtrlVar_ &, PostProcessor<State_>&) const override;
63 
64  private:
65  void addIncr(CtrlVar_ &, const CtrlInc_ &, PostProcessor<Increment_>&) const override;
66 
67  CostJb3D<MODEL> * newJb(const eckit::Configuration &, const Geometry_ &,
68  const CtrlVar_ &) const override;
69  CostJo<MODEL, OBS> * newJo(const eckit::Configuration &) const override;
70  CostTermBase<MODEL, OBS> * newJc(const eckit::Configuration &, const Geometry_ &) const override;
71  void doLinearize(const Geometry_ &, const eckit::Configuration &,
72  const CtrlVar_ &, const CtrlVar_ &,
74  const Geometry_ & geometry() const override {return resol_;}
75 
76  util::Duration windowLength_;
77  util::DateTime windowBegin_;
78  util::DateTime windowEnd_;
79  util::DateTime windowHalf_;
80  const eckit::mpi::Comm & comm_;
83 };
84 
85 // =============================================================================
86 
87 template<typename MODEL, typename OBS>
88 CostFct3DVar<MODEL, OBS>::CostFct3DVar(const eckit::Configuration & config,
89  const eckit::mpi::Comm & comm)
90  : CostFunction<MODEL, OBS>::CostFunction(config),
91  windowLength_(), windowHalf_(), comm_(comm),
92  resol_(eckit::LocalConfiguration(config, "geometry"), comm),
93  ctlvars_(config, "analysis variables")
94 {
95  Log::trace() << "CostFct3DVar::CostFct3DVar start" << std::endl;
96  windowLength_ = util::Duration(config.getString("window length"));
97  windowBegin_ = util::DateTime(config.getString("window begin"));
100 
101  this->setupTerms(config); // Background is read here
102 
103  Log::info() << "3DVar window: begin = " << windowBegin_ << ", end = " << windowEnd_ << std::endl;
104  Log::trace() << "CostFct3DVar::CostFct3DVar done" << std::endl;
105 }
106 
107 // -----------------------------------------------------------------------------
108 
109 template <typename MODEL, typename OBS>
110 CostJb3D<MODEL> * CostFct3DVar<MODEL, OBS>::newJb(const eckit::Configuration & jbConf,
111  const Geometry_ & resol,
112  const CtrlVar_ & xb) const {
113  Log::trace() << "CostFct3DVar::newJb" << std::endl;
114  return new CostJb3D<MODEL>(jbConf, resol, ctlvars_, util::Duration(0), xb.state());
115 }
116 
117 // -----------------------------------------------------------------------------
118 
119 template <typename MODEL, typename OBS>
120 CostJo<MODEL, OBS> * CostFct3DVar<MODEL, OBS>::newJo(const eckit::Configuration & joConf) const {
121  Log::trace() << "CostFct3DVar::newJo" << std::endl;
122  return new CostJo<MODEL, OBS>(joConf, comm_, windowBegin_, windowEnd_);
123 }
124 
125 // -----------------------------------------------------------------------------
126 
127 template <typename MODEL, typename OBS>
128 CostTermBase<MODEL, OBS> * CostFct3DVar<MODEL, OBS>::newJc(const eckit::Configuration & jcConf,
129  const Geometry_ &) const {
130  Log::trace() << "CostFct3DVar::newJc" << std::endl;
131 // For now there is no Jc that can work with 3D-Var
132  CostTermBase<MODEL, OBS> * pjc = 0;
133  return pjc;
134 }
135 
136 // -----------------------------------------------------------------------------
137 
138 template <typename MODEL, typename OBS>
140  Log::trace() << "CostFct3DVar::runNL start" << std::endl;
141  ASSERT(xx.state().validTime() == windowHalf_);
142 
143  post.initialize(xx.state(), windowHalf_, windowLength_);
144  post.process(xx.state());
145  post.finalize(xx.state());
146 
147  ASSERT(xx.state().validTime() == windowHalf_);
148  Log::trace() << "CostFct3DVar::runNL done" << std::endl;
149 }
150 
151 // -----------------------------------------------------------------------------
152 
153 template<typename MODEL, typename OBS>
154 void CostFct3DVar<MODEL, OBS>::doLinearize(const Geometry_ & res, const eckit::Configuration & conf,
155  const CtrlVar_ &, const CtrlVar_ &,
157  PostProcessorTLAD<MODEL> & pptraj) {
158  Log::trace() << "CostFct3DVar::doLinearize start" << std::endl;
159  pp.enrollProcessor(new TrajectorySaver<MODEL>(conf, res, pptraj));
160  Log::trace() << "CostFct3DVar::doLinearize done" << std::endl;
161 }
162 
163 // -----------------------------------------------------------------------------
164 
165 template <typename MODEL, typename OBS>
169  const bool) const {
170  Log::trace() << "CostFct3DVar::runTLM start" << std::endl;
171  ASSERT(dx.state().validTime() == windowHalf_);
172 
173  cost.initializeTL(dx.state(), windowHalf_, windowLength_);
174  post.initialize(dx.state(), windowHalf_, windowLength_);
175 
176  cost.processTL(dx.state());
177  post.process(dx.state());
178 
179  cost.finalizeTL(dx.state());
180  post.finalize(dx.state());
181 
182  ASSERT(dx.state().validTime() == windowHalf_);
183  Log::trace() << "CostFct3DVar::runTLM done" << std::endl;
184 }
185 
186 // -----------------------------------------------------------------------------
187 
188 template <typename MODEL, typename OBS>
190  Log::trace() << "CostFct3DVar::zeroAD start" << std::endl;
191  dx.state().zero(windowHalf_);
192  dx.modVar().zero();
193  dx.obsVar().zero();
194  Log::trace() << "CostFct3DVar::zeroAD done" << std::endl;
195 }
196 
197 // -----------------------------------------------------------------------------
198 
199 template <typename MODEL, typename OBS>
203  const bool) const {
204  Log::trace() << "CostFct3DVar::runADJ start" << std::endl;
205  ASSERT(dx.state().validTime() == windowHalf_);
206 
207  post.initialize(dx.state(), windowHalf_, windowLength_);
208  cost.initializeAD(dx.state(), windowHalf_, windowLength_);
209 
210  cost.processAD(dx.state());
211  post.process(dx.state());
212 
213  cost.finalizeAD(dx.state());
214  post.finalize(dx.state());
215 
216  ASSERT(dx.state().validTime() == windowHalf_);
217 
218  Log::trace() << "CostFct3DVar::runADJ done" << std::endl;
219 }
220 
221 // -----------------------------------------------------------------------------
222 
223 template<typename MODEL, typename OBS>
225  PostProcessor<Increment_> &) const {
226  Log::trace() << "CostFct3DVar::addIncr start" << std::endl;
227  ASSERT(xx.state().validTime() == windowHalf_);
228  ASSERT(dx.state().validTime() == windowHalf_);
229  xx.state() += dx.state();
230  Log::trace() << "CostFct3DVar::addIncr done" << std::endl;
231 }
232 
233 // -----------------------------------------------------------------------------
234 
235 } // namespace oops
236 
237 #endif // OOPS_ASSIMILATION_COSTFCT3DVAR_H_
ObsAuxIncrs_ & obsVar()
Get augmented observation control variable.
Increment_ & state()
Get state control variable.
ModelAuxIncr_ & modVar()
Get augmented model control variable.
Control variable.
State_ & state()
Get state control variable.
3D-Var Cost Function
Definition: CostFct3DVar.h:44
CostFct3DVar(const eckit::Configuration &, const eckit::mpi::Comm &)
Definition: CostFct3DVar.h:88
void addIncr(CtrlVar_ &, const CtrlInc_ &, PostProcessor< Increment_ > &) const override
Definition: CostFct3DVar.h:224
void zeroAD(CtrlInc_ &) const override
Definition: CostFct3DVar.h:189
const eckit::mpi::Comm & comm_
Definition: CostFct3DVar.h:80
virtual ~CostFct3DVar()
Definition: CostFct3DVar.h:54
void runTLM(CtrlInc_ &, PostProcessorTLAD< MODEL > &, PostProcessor< Increment_ >, const bool) const override
Definition: CostFct3DVar.h:166
void doLinearize(const Geometry_ &, const eckit::Configuration &, const CtrlVar_ &, const CtrlVar_ &, PostProcessor< State_ > &, PostProcessorTLAD< MODEL > &) override
Definition: CostFct3DVar.h:154
ControlIncrement< MODEL, OBS > CtrlInc_
Definition: CostFct3DVar.h:46
util::DateTime windowEnd_
Definition: CostFct3DVar.h:78
CostJb3D< MODEL > * newJb(const eckit::Configuration &, const Geometry_ &, const CtrlVar_ &) const override
Definition: CostFct3DVar.h:110
State< MODEL > State_
Definition: CostFct3DVar.h:50
const Variables ctlvars_
Definition: CostFct3DVar.h:82
util::DateTime windowHalf_
Definition: CostFct3DVar.h:79
Geometry< MODEL > Geometry_
Definition: CostFct3DVar.h:49
util::Duration windowLength_
Definition: CostFct3DVar.h:76
Increment< MODEL > Increment_
Definition: CostFct3DVar.h:45
CostFunction< MODEL, OBS > CostFct_
Definition: CostFct3DVar.h:48
CostTermBase< MODEL, OBS > * newJc(const eckit::Configuration &, const Geometry_ &) const override
Definition: CostFct3DVar.h:128
void runNL(CtrlVar_ &, PostProcessor< State_ > &) const override
Definition: CostFct3DVar.h:139
void runADJ(CtrlInc_ &, PostProcessorTLAD< MODEL > &, PostProcessor< Increment_ >, const bool) const override
Definition: CostFct3DVar.h:200
util::DateTime windowBegin_
Definition: CostFct3DVar.h:77
CostJo< MODEL, OBS > * newJo(const eckit::Configuration &) const override
Definition: CostFct3DVar.h:120
const Geometry_ & geometry() const override
Definition: CostFct3DVar.h:74
ControlVariable< MODEL, OBS > CtrlVar_
Definition: CostFct3DVar.h:47
Cost Function.
Definition: CostFunction.h:53
void setupTerms(const eckit::Configuration &)
Definition: CostFunction.h:194
Jb Cost Function.
Definition: CostJb3D.h:43
Jo Cost Function.
Definition: CostJo.h:52
Base Class for Cost Function Terms.
Definition: CostTermBase.h:36
Geometry class used in oops; subclass of interface class interface::Geometry.
Increment class used in oops.
void zero()
Zero out this ModelAuxIncrement.
Control model post processing.
Definition: PostProcessor.h:30
void enrollProcessor(PostBase_ *pp)
Definition: PostProcessor.h:38
void process(const FLDS &xx)
Definition: PostProcessor.h:56
void finalize(const FLDS &xx)
Definition: PostProcessor.h:62
void initialize(const FLDS &xx, const util::DateTime &end, const util::Duration &step)
Definition: PostProcessor.h:49
Control model post processing.
void finalizeAD(Increment_ &dx)
void processTL(const Increment_ &dx)
void processAD(Increment_ &dx)
void finalizeTL(const Increment_ &dx)
void initializeTL(const Increment_ &dx, const util::DateTime &end, const util::Duration &step)
Tangent linear methods.
void initializeAD(Increment_ &dx, const util::DateTime &bgn, const util::Duration &step)
Adjoint methods.
State class used in oops; subclass of interface class interface::State.
Save trajectory during forecast run.
void zero()
Zero out this Increment.
const util::DateTime validTime() const
Accessor to the time of this Increment.
const util::DateTime validTime() const
Accessor to the time of this State.
Definition: FieldL95.h:22
The namespace for the main oops code.