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