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