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