FV3 Bundle
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 "eckit/config/Configuration.h"
22 #include "oops/base/Variables.h"
25 #include "oops/interface/Model.h"
26 #include "oops/interface/State.h"
27 #include "oops/util/DateTime.h"
28 #include "oops/util/Duration.h"
29 #include "oops/util/Logger.h"
30 
31 namespace oops {
32 
33 /// 3D-Var Cost Function
34 /*!
35  * This class is not really necessary since it is only a special
36  * case of the more general 4D-Var cost function. It is provided
37  * for readability.
38  */
39 
40 // -----------------------------------------------------------------------------
41 
42 template<typename MODEL> class CostFct3DVar : public CostFunction<MODEL> {
51 
52  public:
53  CostFct3DVar(const eckit::Configuration &, const Geometry_ &, const Model_ &);
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> * newJo(const eckit::Configuration &) const override;
70  CostTermBase<MODEL> * newJc(const eckit::Configuration &, const Geometry_ &) const override;
71  void doLinearize(const Geometry_ &, const eckit::Configuration &,
72  const CtrlVar_ &, const CtrlVar_ &) override;
73 
74  util::Duration windowLength_;
75  util::DateTime windowBegin_;
76  util::DateTime windowEnd_;
77  util::DateTime windowHalf_;
78  util::Duration zero_;
80  boost::scoped_ptr<ChangeVar_> an2model_;
81 };
82 
83 // =============================================================================
84 
85 template<typename MODEL>
86 CostFct3DVar<MODEL>::CostFct3DVar(const eckit::Configuration & config,
87  const Geometry_ & resol, const Model_ & model)
88  : CostFunction<MODEL>::CostFunction(config, resol, model),
89  windowLength_(), windowHalf_(), zero_(0), ctlvars_(config), an2model_()
90 {
91  Log::trace() << "CostFct3DVar::CostFct3DVar start" << std::endl;
92  windowLength_ = util::Duration(config.getString("window_length"));
93  windowBegin_ = util::DateTime(config.getString("window_begin"));
96  this->setupTerms(config);
97  Log::trace() << "CostFct3DVar::CostFct3DVar done" << std::endl;
98 }
99 
100 // -----------------------------------------------------------------------------
101 
102 template <typename MODEL>
103 CostJb3D<MODEL> * CostFct3DVar<MODEL>::newJb(const eckit::Configuration & jbConf,
104  const Geometry_ & resol,
105  const CtrlVar_ & xb) const {
106  Log::trace() << "CostFct3DVar::newJb" << std::endl;
107  ASSERT(xb.state().checkStatesNumber(1));
108  return new CostJb3D<MODEL>(jbConf, resol, ctlvars_, zero_, xb.state()[0]);
109 }
110 
111 // -----------------------------------------------------------------------------
112 
113 template <typename MODEL>
114 CostJo<MODEL> * CostFct3DVar<MODEL>::newJo(const eckit::Configuration & joConf) const {
115  Log::trace() << "CostFct3DVar::newJo" << std::endl;
116  return new CostJo<MODEL>(joConf, windowBegin_, windowEnd_, windowLength_);
117 }
118 
119 // -----------------------------------------------------------------------------
120 
121 template <typename MODEL>
122 CostTermBase<MODEL> * CostFct3DVar<MODEL>::newJc(const eckit::Configuration & jcConf,
123  const Geometry_ &) const {
124  Log::trace() << "CostFct3DVar::newJc" << std::endl;
125 // For now there is no Jc that can work with 3D-Var
126  CostTermBase<MODEL> * pjc = 0;
127  return pjc;
128 }
129 
130 // -----------------------------------------------------------------------------
131 
132 template <typename MODEL>
134  PostProcessor<State_> & post) const {
135  Log::trace() << "CostFct3DVar::runNL start" << std::endl;
136  ASSERT(xx.state().checkStatesNumber(1));
137  ASSERT(xx.state()[0].validTime() == windowHalf_);
138  State_ xm(xx.state()[0].geometry(), CostFct_::getModel().variables(), windowHalf_);
139  xm = xx.state()[0];
140  CostFct_::getModel().forecast(xm, xx.modVar(), zero_, post);
141  xx.state()[0] = xm;
142  ASSERT(xx.state()[0].validTime() == windowHalf_);
143  Log::trace() << "CostFct3DVar::runNL done" << std::endl;
144 }
145 
146 // -----------------------------------------------------------------------------
147 
148 template<typename MODEL>
150  const eckit::Configuration & innerConf,
151  const CtrlVar_ & bg, const CtrlVar_ & fg) {
152  Log::trace() << "CostFct3DVar::doLinearize start" << std::endl;
153  eckit::LocalConfiguration conf(innerConf, "linearmodel");
154  an2model_.reset(LinearVariableChangeFactory<MODEL>::create(bg.state()[0], fg.state()[0],
155  resol, conf));
156  an2model_->setInputVariables(ctlvars_);
157  an2model_->setOutputVariables(CostFct_::getTLM().variables());
158  Log::trace() << "CostFct3DVar::doLinearize done" << std::endl;
159 }
160 
161 // -----------------------------------------------------------------------------
162 
163 template <typename MODEL>
167  const bool) const {
168  Log::trace() << "CostFct3DVar::runTLM start" << std::endl;
169  ASSERT(dx.state()[0].validTime() == windowHalf_);
170  Increment_ dxmodel(dx.state()[0].geometry(), CostFct_::getTLM().variables(), windowHalf_);
171  an2model_->multiply(dx.state()[0], dxmodel);
172  CostFct_::getTLM().forecastTL(dxmodel, dx.modVar(), zero_, post, cost);
173  an2model_->multiplyInverse(dxmodel, dx.state()[0]);
174  ASSERT(dx.state()[0].validTime() == windowHalf_);
175  Log::trace() << "CostFct3DVar::runTLM done" << std::endl;
176 }
177 
178 // -----------------------------------------------------------------------------
179 
180 template <typename MODEL>
182  Log::trace() << "CostFct3DVar::zeroAD start" << std::endl;
183  dx.state()[0].zero(windowHalf_);
184  dx.modVar().zero();
185  dx.obsVar().zero();
186  Log::trace() << "CostFct3DVar::zeroAD done" << std::endl;
187 }
188 
189 // -----------------------------------------------------------------------------
190 
191 template <typename MODEL>
195  const bool) const {
196  Log::trace() << "CostFct3DVar::runADJ start" << std::endl;
197  ASSERT(dx.state()[0].validTime() == windowHalf_);
198  Increment_ dxmodel(dx.state()[0].geometry(), CostFct_::getTLM().variables(), windowHalf_);
199  an2model_->multiplyInverseAD(dx.state()[0], dxmodel);
200  CostFct_::getTLM().forecastAD(dxmodel, dx.modVar(), zero_, post, cost);
201  an2model_->multiplyAD(dxmodel, dx.state()[0]);
202  ASSERT(dx.state()[0].validTime() == windowHalf_);
203  Log::trace() << "CostFct3DVar::runADJ done" << std::endl;
204 }
205 
206 // -----------------------------------------------------------------------------
207 
208 template<typename MODEL>
210  PostProcessor<Increment_> &) const {
211  Log::trace() << "CostFct3DVar::addIncr start" << std::endl;
212  ASSERT(xx.state().checkStatesNumber(1));
213  ASSERT(xx.state()[0].validTime() == windowHalf_);
214  ASSERT(dx.state()[0].validTime() == windowHalf_);
215  xx.state()[0] += dx.state()[0];
216  Log::trace() << "CostFct3DVar::addIncr done" << std::endl;
217 }
218 
219 // -----------------------------------------------------------------------------
220 
221 } // namespace oops
222 
223 #endif // OOPS_ASSIMILATION_COSTFCT3DVAR_H_
util::Duration zero_
Definition: CostFct3DVar.h:78
State4D_ & state()
Get state control variable.
ControlVariable< MODEL > CtrlVar_
Definition: CostFct3DVar.h:45
void runADJ(CtrlInc_ &, PostProcessorTLAD< MODEL > &, PostProcessor< Increment_ >, const bool) const override
Definition: CostFct3DVar.h:192
State< MODEL > State_
Definition: CostFct3DVar.h:48
Geometry< MODEL > Geometry_
Definition: CostFct3DVar.h:47
CostJb3D< MODEL > * newJb(const eckit::Configuration &, const Geometry_ &, const CtrlVar_ &) const override
Definition: CostFct3DVar.h:103
CostFct3DVar(const eckit::Configuration &, const Geometry_ &, const Model_ &)
Definition: CostFct3DVar.h:86
ObsAuxIncr_ & obsVar()
Get augmented observation control variable.
Model< MODEL > Model_
Definition: CostFct3DVar.h:49
ModelAuxIncr_ & modVar()
Get augmented model control variable.
Definition: conf.py:1
boost::scoped_ptr< ChangeVar_ > an2model_
Definition: CostFct3DVar.h:80
Cost Function.
Definition: CostFunction.h:56
util::Duration windowLength_
Definition: CostFct3DVar.h:74
Encapsulates the model state.
CostTermBase< MODEL > * newJc(const eckit::Configuration &, const Geometry_ &) const override
Definition: CostFct3DVar.h:122
Control variable.
void doLinearize(const Geometry_ &, const eckit::Configuration &, const CtrlVar_ &, const CtrlVar_ &) override
Definition: CostFct3DVar.h:149
The namespace for the main oops code.
CostFunction< MODEL > CostFct_
Definition: CostFct3DVar.h:46
void runTLM(CtrlInc_ &, PostProcessorTLAD< MODEL > &, PostProcessor< Increment_ >, const bool) const override
Definition: CostFct3DVar.h:164
ModelAux_ & modVar()
Get augmented model control variable.
Increment4D_ & state()
Get state control variable.
virtual ~CostFct3DVar()
Definition: CostFct3DVar.h:54
real(fp), parameter xb
Definition: ufo_aod_mod.F90:41
Increment< MODEL > Increment_
Definition: CostFct3DVar.h:43
LinearVariableChangeFactory Factory.
Control model post processing.
CostJo< MODEL > * newJo(const eckit::Configuration &) const override
Definition: CostFct3DVar.h:114
const Variables ctlvars_
Definition: CostFct3DVar.h:79
Base class for generic variable transform.
Encapsulates the nonlinear forecast model.
ControlIncrement< MODEL > CtrlInc_
Definition: CostFct3DVar.h:44
Increment Class: Difference between two states.
Jo Cost Function.
Definition: CostJo.h:54
void addIncr(CtrlVar_ &, const CtrlInc_ &, PostProcessor< Increment_ > &) const override
Definition: CostFct3DVar.h:209
Base Class for Cost Function Terms.
Definition: CostTermBase.h:37
Geometry_ geometry() const
Get geometry.
Definition: Increment4D.h:73
LinearVariableChangeBase< MODEL > ChangeVar_
Definition: CostFct3DVar.h:50
void zeroAD(CtrlInc_ &) const override
Definition: CostFct3DVar.h:181
Jb Cost Function.
Definition: CostJb3D.h:48
bool checkStatesNumber(const unsigned int nn) const
Get model space control variable.
Definition: State4D.h:53
void runNL(CtrlVar_ &, PostProcessor< State_ > &) const override
Definition: CostFct3DVar.h:133
Control model post processing.
Definition: PostProcessor.h:31
util::DateTime windowEnd_
Definition: CostFct3DVar.h:76
void setupTerms(const eckit::Configuration &)
Definition: CostFunction.h:212
3D-Var Cost Function
Definition: CostFct3DVar.h:42
util::DateTime windowHalf_
Definition: CostFct3DVar.h:77
util::DateTime windowBegin_
Definition: CostFct3DVar.h:75