FV3 Bundle
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 
21 #include "oops/util/PrintAdjTest.h"
22 
23 namespace oops {
24 
25 /// The \f$ H B H^T \f$ matrix.
26 /*!
27  * The solvers represent matrices as objects that implement a "multiply"
28  * method. This class defines objects that apply a generalized
29  * \f$ H B H ^T\f$ matrix which includes \f$ H \f$ and the equivalent
30  * operators for the other terms of the cost function.
31  */
32 
33 template<typename MODEL> class HBHtMatrix : private boost::noncopyable {
38 
39  public:
40  explicit HBHtMatrix(const CostFct_ & j,
41  const bool test = false);
42 
43  void multiply(const Dual_ & dy, Dual_ & 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  const bool test)
56  : j_(j), test_(test), iter_(0)
57 {}
58 
59 // -----------------------------------------------------------------------------
60 
61 template<typename MODEL>
62 void HBHtMatrix<MODEL>::multiply(const Dual_ & dy, Dual_ & dz) const {
63 // Increment counter
64  iter_++;
65 
66 // Run ADJ
67  CtrlInc_ ww(j_.jb());
68  j_.zeroAD(ww);
70  for (unsigned jj = 0; jj < j_.nterms(); ++jj) {
71  costad.enrollProcessor(j_.jterm(jj).setupAD(dy.getv(jj), ww));
72  }
73  j_.runADJ(ww, costad);
74 
75 // Multiply by B
76  CtrlInc_ zz(j_.jb());
77  j_.jb().multiplyB(ww, zz);
78 
79 // Run TLM
81  for (unsigned jj = 0; jj < j_.nterms(); ++jj) {
82  costtl.enrollProcessor(j_.jterm(jj).setupTL(zz));
83  }
84  CtrlInc_ mzz(zz);
85  j_.runTLM(mzz, costtl);
86 
87 // Get TLM outputs
88  dz.clear();
89  for (unsigned jj = 0; jj < j_.nterms(); ++jj) {
90  dz.append(costtl.releaseOutputFromTL(jj));
91  }
92 
93  if (test_) {
94  // <G dx, dy >, where dx = B Gt dy
95  double adj_tst_fwd = dot_product(dz, dy);
96  // < dx, Gt dy>, where dx = B Gt dy
97  double adj_tst_bwd = dot_product(zz, ww);
98 
99  Log::info() << "Online adjoint test, iteration: " << iter_ << std::endl
100  << util::PrintAdjTest(adj_tst_fwd, adj_tst_bwd, "G")
101  << std::endl;
102  }
103 }
104 
105 // -----------------------------------------------------------------------------
106 
107 } // namespace oops
108 
109 #endif // OOPS_ASSIMILATION_HBHTMATRIX_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
CostFunction< MODEL > CostFct_
Definition: HBHtMatrix.h:36
Increment< MODEL > Increment_
Definition: HBHtMatrix.h:34
Cost Function.
Definition: CostFunction.h:56
l_size ! loop over number of fields ke do j
The namespace for the main oops code.
The matrix.
Definition: HBHtMatrix.h:33
DualVector< MODEL > Dual_
Definition: HBHtMatrix.h:37
CostFct_ const & j_
Definition: HBHtMatrix.h:46
subroutine, public info(self)
Control model post processing.
void enrollProcessor(PostBaseTLAD_ *pp)
Increment Class: Difference between two states.
ControlIncrement< MODEL > CtrlInc_
Definition: HBHtMatrix.h:35
void multiply(const Dual_ &dy, Dual_ &dz) const
Definition: HBHtMatrix.h:62
void append(GeneralizedDepartures *)
Definition: DualVector.h:107
boost::shared_ptr< const GeneralizedDepartures > getv(const unsigned) const
Definition: DualVector.h:127
HBHtMatrix(const CostFct_ &j, const bool test=false)
Definition: HBHtMatrix.h:54