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