FV3 Bundle
HtRinvHMatrix.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_HTRINVHMATRIX_H_
12 #define OOPS_ASSIMILATION_HTRINVHMATRIX_H_
13 
14 #include <boost/noncopyable.hpp>
15 #include <boost/scoped_ptr.hpp>
16 #include <boost/shared_ptr.hpp>
17 
23 #include "oops/util/dot_product.h"
24 #include "oops/util/formats.h"
25 #include "oops/util/Logger.h"
26 #include "oops/util/PrintAdjTest.h"
27 
28 namespace oops {
29 
30 /// The \f$ H^T R^{-1} H \f$ matrix.
31 /*!
32  * The solvers represent matrices as objects that implement a "multiply"
33  * method. This class defines objects that apply a generalized
34  * \f$ H^T R^{-1} H \f$ matrix that also includes the equivalent
35  * operators for the other terms of the cost function.
36  */
37 
38 template<typename MODEL> class HtRinvHMatrix : private boost::noncopyable {
42 
43  public:
44  explicit HtRinvHMatrix(const CostFct_ & j, const bool test = false);
45 
46  void multiply(const CtrlInc_ & dx, CtrlInc_ & dz) const;
47 
48  private:
49  CostFct_ const & j_;
50  bool test_;
51  mutable int iter_;
52 };
53 
54 // -----------------------------------------------------------------------------
55 
56 template<typename MODEL>
58  : j_(j), test_(test), iter_(0)
59 {}
60 
61 // -----------------------------------------------------------------------------
62 
63 template<typename MODEL>
64 void HtRinvHMatrix<MODEL>::multiply(const CtrlInc_ & dx, CtrlInc_ & dz) const {
65 // Increment counter
66  iter_++;
67 
68 // Setup TL terms of cost function
70  for (unsigned jj = 0; jj < j_.nterms(); ++jj) {
71  costtl.enrollProcessor(j_.jterm(jj).setupTL(dx));
72  }
73 
74 // Run TLM
75  CtrlInc_ mdx(dx);
76  j_.runTLM(mdx, costtl);
77 
78 // Get TLM outputs, multiply by covariance inverses, and setup ADJ forcing terms
79  j_.zeroAD(dz);
81 
84 
85  for (unsigned jj = 0; jj < j_.nterms(); ++jj) {
86  ww.append(costtl.releaseOutputFromTL(jj));
87  zz.append(j_.jterm(jj).multiplyCoInv(*ww.getv(jj)));
88  costad.enrollProcessor(j_.jterm(jj).setupAD(zz.getv(jj), dz));
89  }
90 
91 // Run ADJ
92  j_.runADJ(dz, costad);
93 
94  if (test_) {
95  // <G dx, dy>, where dy = Rinv H dx
96  double adj_tst_fwd = dot_product(ww, zz);
97  // <dx, Gt dy> , where dy = Rinv H dx
98  double adj_tst_bwd = dot_product(dx, dz);
99 
100  Log::info() << "Online adjoint test, iteration: " << iter_ << std::endl
101  << util::PrintAdjTest(adj_tst_fwd, adj_tst_bwd, "G")
102  << std::endl;
103  }
104 }
105 
106 // -----------------------------------------------------------------------------
107 
108 } // namespace oops
109 
110 #endif // OOPS_ASSIMILATION_HTRINVHMATRIX_H_
GeneralizedDepartures * releaseOutputFromTL(unsigned int ii)
Get TL dual space output.
Container of dual space vectors for all terms of the cost function.
Definition: DualVector.h:34
HtRinvHMatrix(const CostFct_ &j, const bool test=false)
Definition: HtRinvHMatrix.h:57
ControlIncrement< MODEL > CtrlInc_
Definition: HtRinvHMatrix.h:40
CostFunction< MODEL > CostFct_
Definition: HtRinvHMatrix.h:41
Increment< MODEL > Increment_
Definition: HtRinvHMatrix.h:39
Cost Function.
Definition: CostFunction.h:56
l_size ! loop over number of fields ke do j
CostFct_ const & j_
Definition: HtRinvHMatrix.h:49
The namespace for the main oops code.
void multiply(const CtrlInc_ &dx, CtrlInc_ &dz) const
Definition: HtRinvHMatrix.h:64
subroutine, public info(self)
Control model post processing.
void enrollProcessor(PostBaseTLAD_ *pp)
Increment Class: Difference between two states.
void append(GeneralizedDepartures *)
Definition: DualVector.h:107
boost::shared_ptr< const GeneralizedDepartures > getv(const unsigned) const
Definition: DualVector.h:127