FV3 Bundle
CostJb3D.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_COSTJB3D_H_
12 #define OOPS_ASSIMILATION_COSTJB3D_H_
13 
14 #include <memory>
15 #include <boost/scoped_ptr.hpp>
16 
17 #include "eckit/config/LocalConfiguration.h"
22 #include "oops/base/Variables.h"
25 #include "oops/interface/State.h"
26 #include "oops/util/DateTime.h"
27 #include "oops/util/dot_product.h"
28 #include "oops/util/Duration.h"
29 #include "oops/util/Logger.h"
30 
31 namespace oops {
32  template<typename MODEL> class ControlIncrement;
33  template<typename MODEL> class JqTerm;
34  template<typename MODEL> class JqTermTLAD;
35 
36 // -----------------------------------------------------------------------------
37 
38 /// Jb Cost Function
39 /*!
40  * The CostJb3D encapsulates the Jb term of the cost function for a
41  * 3 dimensional background.
42  *
43  * This class is not really necessary since it is only a special
44  * case of the more general CostJbJq weak constraint term
45  * with one sub-window. It is provided for readability.
46  */
47 
48 template<typename MODEL> class CostJb3D : public CostJbState<MODEL> {
55 
56  public:
57 /// Construct \f$ J_b\f$.
58  CostJb3D(const eckit::Configuration &, const Geometry_ &, const Variables &,
59  const util::Duration &, const State_ &);
60 
61 /// Destructor
62  virtual ~CostJb3D() {}
63 
64 /// Empty Jq observer.
65  JqTerm<MODEL> * initializeJq() const override {return 0;}
66  JqTermTLAD<MODEL> * initializeJqTLAD() const override {return 0;}
67 
68 /// Get increment from state (usually first guess).
69  void computeIncrement(const State4D_ &, const State4D_ &, Increment4D_ &) const override;
70 
71 /// Linearize before the linear computations.
72  void linearize(const State4D_ &, const Geometry_ &) override;
73 
74 /// Add Jb gradient.
75  void addGradient(const Increment4D_ &, Increment4D_ &, Increment4D_ &) const override;
76 
77 /// Empty TL Jq observer.
78  JqTermTLAD<MODEL> * initializeJqTL() const override {return 0;}
79 
80 /// Empty AD Jq observer.
81  JqTermTLAD<MODEL> * initializeJqAD(const Increment4D_ &) const override {return 0;}
82 
83 /// Multiply by \f$ B\f$ and \f$ B^{-1}\f$.
84  void Bmult(const Increment4D_ &, Increment4D_ &) const override;
85  void Bminv(const Increment4D_ &, Increment4D_ &) const override;
86 
87 /// Randomize
88  void randomize(Increment4D_ &) const override;
89 
90 /// Create new increment (set to 0).
91  unsigned int nstates() const override {return 1;}
92  Increment_ * newStateIncrement(const unsigned int) const override;
93 
94  private:
95  const State_ & xb_;
96  boost::scoped_ptr< ModelSpaceCovarianceBase<MODEL> > B_;
97  const util::Duration winLength_;
99  boost::scoped_ptr<const Geometry_> resol_;
100  boost::scoped_ptr<const util::DateTime> time_;
101  const eckit::LocalConfiguration conf_;
102 };
103 
104 // =============================================================================
105 
106 // Jb Term of Cost Function
107 // -----------------------------------------------------------------------------
108 
109 template<typename MODEL>
110 CostJb3D<MODEL>::CostJb3D(const eckit::Configuration & config, const Geometry_ &,
111  const Variables & ctlvars, const util::Duration & len,
112  const State_ & xb)
113  : xb_(xb), B_(), winLength_(len), controlvars_(ctlvars), resol_(), time_(),
114  conf_(config, "Covariance")
115 {
116  Log::trace() << "CostJb3D constructed." << std::endl;
117 }
118 
119 // -----------------------------------------------------------------------------
120 
121 template<typename MODEL>
122 void CostJb3D<MODEL>::linearize(const State4D_ & fg, const Geometry_ & lowres) {
123  ASSERT(fg.checkStatesNumber(1));
124  resol_.reset(new Geometry_(lowres));
125  time_.reset(new util::DateTime(fg[0].validTime()));
126  B_.reset(CovarianceFactory<MODEL>::create(conf_, lowres, controlvars_, xb_, fg[0]));
127 }
128 
129 // -----------------------------------------------------------------------------
130 
131 template<typename MODEL>
133  Increment4D_ & dx) const {
134  dx.diff(fg, xb);
135 }
136 
137 // -----------------------------------------------------------------------------
138 
139 template<typename MODEL>
141  Increment4D_ & gradJb) const {
142  grad += gradJb;
143 }
144 
145 // -----------------------------------------------------------------------------
146 
147 template<typename MODEL>
148 void CostJb3D<MODEL>::Bmult(const Increment4D_ & dxin, Increment4D_ & dxout) const {
149  B_->multiply(dxin[0], dxout[0]);
150 }
151 
152 // -----------------------------------------------------------------------------
153 
154 template<typename MODEL>
155 void CostJb3D<MODEL>::Bminv(const Increment4D_ & dxin, Increment4D_ & dxout) const {
156  Log::warning() << "*** B inverse might not always exist ***" << std::endl;
157  B_->inverseMultiply(dxin[0], dxout[0]);
158  Log::warning() << "*** B inverse might not always exist ***" << std::endl;
159 }
160 
161 // -----------------------------------------------------------------------------
162 
163 template<typename MODEL>
165  B_->randomize(dx[0]);
166 }
167 
168 // -----------------------------------------------------------------------------
169 
170 template<typename MODEL>
172 CostJb3D<MODEL>::newStateIncrement(const unsigned int) const {
173  Increment_ * incr = new Increment_(*resol_, controlvars_, *time_);
174  return incr;
175 }
176 
177 // -----------------------------------------------------------------------------
178 
179 } // namespace oops
180 
181 #endif // OOPS_ASSIMILATION_COSTJB3D_H_
void randomize(Increment4D_ &) const override
Randomize.
Definition: CostJb3D.h:164
boost::scoped_ptr< ModelSpaceCovarianceBase< MODEL > > B_
Definition: CostJb3D.h:96
integer, parameter, public warning
void Bminv(const Increment4D_ &, Increment4D_ &) const override
Definition: CostJb3D.h:155
JqTermTLAD< MODEL > * initializeJqAD(const Increment4D_ &) const override
Empty AD Jq observer.
Definition: CostJb3D.h:81
State< MODEL > State_
Definition: CostJb3D.h:50
void diff(const State4D_ &, const State4D_ &)
Linear algebra operators.
Definition: Increment4D.h:171
Encapsulates the model state.
boost::scoped_ptr< const util::DateTime > time_
Definition: CostJb3D.h:100
const Variables controlvars_
Definition: CostJb3D.h:98
ControlIncrement< MODEL > CtrlInc_
Definition: CostJb3D.h:53
The namespace for the main oops code.
State4D< MODEL > State4D_
Definition: CostJb3D.h:51
Increment4D< MODEL > Increment4D_
Definition: CostJb3D.h:52
JqTerm< MODEL > * initializeJq() const override
Empty Jq observer.
Definition: CostJb3D.h:65
unsigned int nstates() const override
Create new increment (set to 0).
Definition: CostJb3D.h:91
void addGradient(const Increment4D_ &, Increment4D_ &, Increment4D_ &) const override
Add Jb gradient.
Definition: CostJb3D.h:140
real(fp), parameter xb
Definition: ufo_aod_mod.F90:41
Increment< MODEL > Increment_
Definition: CostJb3D.h:49
State increment.
Definition: CostJbState.h:28
void computeIncrement(const State4D_ &, const State4D_ &, Increment4D_ &) const override
Get increment from state (usually first guess).
Definition: CostJb3D.h:132
Four dimensional state.
Definition: CostJbState.h:29
const State_ & xb_
Definition: CostJb3D.h:95
const util::Duration winLength_
Definition: CostJb3D.h:97
Increment_ * newStateIncrement(const unsigned int) const override
Definition: CostJb3D.h:172
Jb Cost Function Base Class.
Definition: CostJbState.h:41
JqTermTLAD< MODEL > * initializeJqTLAD() const override
Definition: CostJb3D.h:66
Increment Class: Difference between two states.
virtual ~CostJb3D()
Destructor.
Definition: CostJb3D.h:62
Geometry< MODEL > Geometry_
Definition: CostJb3D.h:54
Jb Cost Function.
Definition: CostJb3D.h:48
bool checkStatesNumber(const unsigned int nn) const
Get model space control variable.
Definition: State4D.h:53
boost::scoped_ptr< const Geometry_ > resol_
Definition: CostJb3D.h:99
const eckit::LocalConfiguration conf_
Definition: CostJb3D.h:101
JqTermTLAD< MODEL > * initializeJqTL() const override
Empty TL Jq observer.
Definition: CostJb3D.h:78
CostJb3D(const eckit::Configuration &, const Geometry_ &, const Variables &, const util::Duration &, const State_ &)
Construct .
Definition: CostJb3D.h:110
void linearize(const State4D_ &, const Geometry_ &) override
Linearize before the linear computations.
Definition: CostJb3D.h:122
void Bmult(const Increment4D_ &, Increment4D_ &) const override
Multiply by and .
Definition: CostJb3D.h:148