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