OOPS
DRMinimizer.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_DRMINIMIZER_H_
12 #define OOPS_ASSIMILATION_DRMINIMIZER_H_
13 
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 
19 #include "eckit/config/Configuration.h"
25 
26 #include "oops/util/dot_product.h"
27 #include "oops/util/formats.h"
28 #include "oops/util/Logger.h"
29 
30 namespace oops {
31 
32 /// DR (Derber and Rosati) Minimizers
33 /*!
34  * DRMinimizer is the base class for all minimizers that use \f$ B\f$ to
35  * precondition the variational minimisation problem and use the auxiliary
36  * variable \f$ \hat{x}=B^{-1}x\f$ and to update it in parallel to \f$ x\f$
37  * based on Derber and Rosati, 1989, J. Phys. Oceanog. 1333-1347.
38  * \f$ J_b\f$ is then computed as \f$ x^T\hat{x}\f$ eliminating the need for
39  * \f$ B^{-1}\f$ or \f$ B^{1/2}\f$.
40  */
41 
42 // -----------------------------------------------------------------------------
43 
44 template<typename MODEL, typename OBS> class DRMinimizer : public Minimizer<MODEL, OBS> {
50 
51  public:
52  explicit DRMinimizer(const CostFct_ & J): Minimizer_(J), J_(J), gradJb_(), costJ0Jb_(0) {}
54  const std::string classname() const override = 0;
55 
56  private:
57  CtrlInc_ * doMinimize(const eckit::Configuration &) override;
58  virtual double solve(CtrlInc_ &, CtrlInc_ &, CtrlInc_ &,
59  const Bmat_ &, const HtRinvH_ &,
60  const double, const double,
61  const int, const double) = 0;
62 
63  const CostFct_ & J_;
64  std::unique_ptr<CtrlInc_> gradJb_;
65  std::vector<CtrlInc_> dxh_;
66  double costJ0Jb_;
67 };
68 
69 // =============================================================================
70 
71 template<typename MODEL, typename OBS>
73 DRMinimizer<MODEL, OBS>::doMinimize(const eckit::Configuration & config) {
74  int ninner = config.getInt("ninner");
75  double gnreduc = config.getDouble("gradient norm reduction");
76 
77  bool runOnlineAdjTest = config.getBool("online diagnostics.online adj test", false);
78 
79  if (gradJb_) {
80  gradJb_.reset(new CtrlInc_(J_.jb().resolution(), *gradJb_));
81  } else {
82  gradJb_.reset(new CtrlInc_(J_.jb()));
83  }
84 
85  Log::info() << std::endl;
86  Log::info() << classname() << ": max iter = " << ninner
87  << ", requested norm reduction = " << gnreduc << std::endl;
88 
89 // Define the matrices
90  const Bmat_ B(J_);
91  const HtRinvH_ HtRinvH(J_, runOnlineAdjTest);
92 
93 // Compute RHS (sum B^{-1} dx_{i}) + H^T R^{-1} d
94 // dx_i = x_i - x_{i-1}; dx_1 = x_1 - x_b
95  CtrlInc_ rhs(J_.jb());
96  J_.computeGradientFG(rhs);
97  J_.jb().addGradientFG(rhs, *gradJb_);
98  rhs *= -1.0;
99  Log::info() << classname() << " rhs" << rhs << std::endl;
100 
101 // Define minimisation starting point
102  // dx
103  CtrlInc_ * dx = new CtrlInc_(J_.jb());
104  // dxh = B^{-1} dx
105  CtrlInc_ dxh(J_.jb());
106 
107 // Set J[0] = 0.5 (x_i - x_b)^T B^{-1} (x_i - x_b) + 0.5 d^T R^{-1} d
108  const double costJ0Jb = costJ0Jb_;
109  const double costJ0JoJc = J_.getCostJoJc();
110 
111 // Solve the linear system
112  double reduc = this->solve(*dx, dxh, rhs, B, HtRinvH, costJ0Jb, costJ0JoJc, ninner, gnreduc);
113 
114  Log::test() << classname() << ": reduction in residual norm = " << reduc << std::endl;
115  Log::info() << classname() << " output increment:" << *dx << std::endl;
116 
117 // Update gradient Jb
118  *gradJb_ += dxh;
119  dxh_.push_back(dxh);
120 
121 // Update Jb component of J[0]: 0.5 (x_i - x_b)^T B^-1 (x_i - x_b)
122  costJ0Jb_ += 0.5 * dot_product(*dx, dxh);
123  for (unsigned int jouter = 1; jouter < dxh_.size(); ++jouter) {
124  CtrlInc_ dxhtmp(dx->geometry(), dxh_[jouter-1]);
125  costJ0Jb_ += dot_product(*dx, dxhtmp);
126  }
127 
128  return dx;
129 }
130 
131 // -----------------------------------------------------------------------------
132 
133 } // namespace oops
134 
135 #endif // OOPS_ASSIMILATION_DRMINIMIZER_H_
oops::DRMinimizer::DRMinimizer
DRMinimizer(const CostFct_ &J)
Definition: DRMinimizer.h:52
oops
The namespace for the main oops code.
Definition: ErrorCovarianceL95.cc:22
HtRinvHMatrix.h
oops::DRMinimizer::solve
virtual double solve(CtrlInc_ &, CtrlInc_ &, CtrlInc_ &, const Bmat_ &, const HtRinvH_ &, const double, const double, const int, const double)=0
oops::Minimizer
A Minimizer knows how to minimize a cost function.
Definition: Minimizer.h:37
oops::DRMinimizer::gradJb_
std::unique_ptr< CtrlInc_ > gradJb_
Definition: DRMinimizer.h:64
CostFunction.h
oops::DRMinimizer::~DRMinimizer
~DRMinimizer()
Definition: DRMinimizer.h:53
oops::ControlIncrement
Definition: ControlIncrement.h:46
oops::DRMinimizer::costJ0Jb_
double costJ0Jb_
Definition: DRMinimizer.h:66
oops::DRMinimizer::Bmat_
BMatrix< MODEL, OBS > Bmat_
Definition: DRMinimizer.h:45
oops::DRMinimizer::Minimizer_
Minimizer< MODEL, OBS > Minimizer_
Definition: DRMinimizer.h:49
oops::DRMinimizer::doMinimize
CtrlInc_ * doMinimize(const eckit::Configuration &) override
Definition: DRMinimizer.h:73
oops::DRMinimizer::dxh_
std::vector< CtrlInc_ > dxh_
Definition: DRMinimizer.h:65
oops::HtRinvHMatrix
The matrix.
Definition: HtRinvHMatrix.h:35
oops::DRMinimizer::classname
const std::string classname() const override=0
BMatrix.h
oops::DRMinimizer
DR (Derber and Rosati) Minimizers.
Definition: DRMinimizer.h:44
oops::DRMinimizer::CtrlInc_
ControlIncrement< MODEL, OBS > CtrlInc_
Definition: DRMinimizer.h:47
oops::DRMinimizer::HtRinvH_
HtRinvHMatrix< MODEL, OBS > HtRinvH_
Definition: DRMinimizer.h:48
Minimizer.h
ControlIncrement.h
oops::DRMinimizer::CostFct_
CostFunction< MODEL, OBS > CostFct_
Definition: DRMinimizer.h:46
oops::ControlIncrement::geometry
Geometry_ geometry() const
Get geometry.
Definition: ControlIncrement.h:83
oops::BMatrix
The matrix.
Definition: BMatrix.h:27
oops::CostFunction
Cost Function.
Definition: CostFunction.h:53
oops::DRMinimizer::J_
const CostFct_ & J_
Definition: DRMinimizer.h:63