OOPS
HessianMatrix.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_HESSIANMATRIX_H_
12 #define OOPS_ASSIMILATION_HESSIANMATRIX_H_
13 
14 #include <boost/noncopyable.hpp>
15 
19 #include "oops/util/PrintAdjTest.h"
20 
21 namespace oops {
22  template<typename MODEL> class JqTermTLAD;
23 
24 /// The Hessian matrix: \f$ B^{-1} + H^T R^{-1} H \f$.
25 /*!
26  * The solvers represent matrices as objects that implement a "multiply"
27  * method. This class defines objects that apply a generalized Hessian
28  * matrix which includes all the terms of the cost function.
29  */
30 
31 template<typename MODEL, typename OBS> class HessianMatrix : private boost::noncopyable {
35 
36  public:
37  explicit HessianMatrix(const CostFct_ & j,
38  const bool test = false);
39 
40  void multiply(const CtrlInc_ & dx, CtrlInc_ & dz) const;
41 
42  private:
43  CostFct_ const & j_;
44  bool test_;
45  mutable int iter_;
46 };
47 
48 // -----------------------------------------------------------------------------
49 
50 template<typename MODEL, typename OBS>
52  : j_(j), test_(test), iter_(0)
53 {}
54 
55 // -----------------------------------------------------------------------------
56 
57 template<typename MODEL, typename OBS>
59 // Increment counter
60  iter_++;
61 
62 // Setup TL terms of cost function
64  JqTermTLAD_ * jqtl = j_.jb().initializeTL();
65  costtl.enrollProcessor(jqtl);
66  unsigned iq = 0;
67  if (jqtl) iq = 1;
68  for (unsigned jj = 0; jj < j_.nterms(); ++jj) {
69  costtl.enrollProcessor(j_.jterm(jj).setupTL(dx));
70  }
71 
72 // Run TLM
73  CtrlInc_ mdx(dx);
74  j_.runTLM(mdx, costtl);
75 
76 // Finalize Jb+Jq
77 
78 // Get TLM outputs, multiply by covariance inverses and setup ADJ forcing terms
80  dz.zero();
81  CtrlInc_ dw(j_.jb());
82 
83 // Jb
84  CtrlInc_ tmp(j_.jb());
85  j_.jb().finalizeTL(jqtl, dx, dw);
86  j_.jb().multiplyBinv(dw, tmp);
87  JqTermTLAD_ * jqad = j_.jb().initializeAD(dz, tmp);
88  costad.enrollProcessor(jqad);
89 
90  j_.zeroAD(dw);
91 
94 
95 // Jo + Jc
96  for (unsigned jj = 0; jj < j_.nterms(); ++jj) {
97  ww.append(costtl.releaseOutputFromTL(iq+jj));
98  zz.append(j_.jterm(jj).multiplyCoInv(*ww.getv(jj)));
99  costad.enrollProcessor(j_.jterm(jj).setupAD(zz.getv(jj), dw));
100  }
101 
102 // Run ADJ
103  j_.runADJ(dw, costad);
104  dz += dw;
105  j_.jb().finalizeAD(jqad);
106 
107  if (test_) {
108  // <G dx, dy>, where dy = Rinv H dx
109  double adj_tst_fwd = dot_product(ww, zz);
110  // <dx, Gt dy> , where dy = Rinv H dx
111  double adj_tst_bwd = dot_product(dx, dw);
112 
113  Log::info() << "Online adjoint test, iteration: " << iter_ << std::endl
114  << util::PrintAdjTest(adj_tst_fwd, adj_tst_bwd, "G")
115  << std::endl;
116  }
117 }
118 
119 // -----------------------------------------------------------------------------
120 
121 } // namespace oops
122 
123 #endif // OOPS_ASSIMILATION_HESSIANMATRIX_H_
oops
The namespace for the main oops code.
Definition: ErrorCovarianceL95.cc:22
oops::HessianMatrix::CostFct_
CostFunction< MODEL, OBS > CostFct_
Definition: HessianMatrix.h:33
CostFunction.h
oops::DualVector
Container of dual space vectors for all terms of the cost function.
Definition: DualVector.h:34
oops::HessianMatrix::HessianMatrix
HessianMatrix(const CostFct_ &j, const bool test=false)
Definition: HessianMatrix.h:51
oops::HessianMatrix::iter_
int iter_
Definition: HessianMatrix.h:45
oops::ControlIncrement
Definition: ControlIncrement.h:46
oops::ControlIncrement::zero
void zero()
Linear algebra operators.
Definition: ControlIncrement.h:189
oops::DualVector::append
void append(std::unique_ptr< GeneralizedDepartures > &&)
Definition: DualVector.h:107
oops::JqTermTLAD
Definition: CostJb3D.h:29
test
Definition: LinearModelFactory.cc:20
oops::HessianMatrix::j_
CostFct_ const & j_
Definition: HessianMatrix.h:43
oops::PostProcessorTLAD::releaseOutputFromTL
std::unique_ptr< GeneralizedDepartures > releaseOutputFromTL(unsigned int ii)
Get TL dual space output.
Definition: PostProcessorTLAD.h:95
oops::HessianMatrix::CtrlInc_
ControlIncrement< MODEL, OBS > CtrlInc_
Definition: HessianMatrix.h:32
oops::PostProcessorTLAD::enrollProcessor
void enrollProcessor(PostBaseTLAD_ *pp)
Definition: PostProcessorTLAD.h:43
oops::HessianMatrix::test_
bool test_
Definition: HessianMatrix.h:44
oops::HessianMatrix
The Hessian matrix: .
Definition: HessianMatrix.h:31
oops::PostProcessorTLAD
Control model post processing.
Definition: PostProcessorTLAD.h:33
oops::HessianMatrix::JqTermTLAD_
JqTermTLAD< MODEL > JqTermTLAD_
Definition: HessianMatrix.h:34
oops::DualVector::getv
std::shared_ptr< const GeneralizedDepartures > getv(const unsigned) const
Definition: DualVector.h:126
ControlIncrement.h
oops::HessianMatrix::multiply
void multiply(const CtrlInc_ &dx, CtrlInc_ &dz) const
Definition: HessianMatrix.h:58
oops::CostFunction
Cost Function.
Definition: CostFunction.h:53
PostProcessorTLAD.h