FV3 Bundle
Dirac.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_RUNS_DIRAC_H_
12 #define OOPS_RUNS_DIRAC_H_
13 
14 #include <sstream>
15 #include <string>
16 
17 #include <boost/scoped_ptr.hpp>
18 
19 #include "eckit/config/Configuration.h"
23 #include "oops/base/StateWriter.h"
24 #include "oops/base/Variables.h"
28 #include "oops/interface/Model.h"
30 #include "oops/interface/State.h"
31 #include "oops/runs/Application.h"
32 #include "oops/util/DateTime.h"
33 #include "oops/util/Duration.h"
34 #include "oops/util/Logger.h"
35 
36 namespace oops {
37 
38 template <typename MODEL> class Dirac : public Application {
45 
46  public:
47 // -----------------------------------------------------------------------------
48  Dirac() {
49  instantiateCovarFactory<MODEL>();
50  }
51 // -----------------------------------------------------------------------------
52  virtual ~Dirac() {}
53 // -----------------------------------------------------------------------------
54  int execute(const eckit::Configuration & fullConfig) const {
55 // Setup resolution
56  const eckit::LocalConfiguration resolConfig(fullConfig, "resolution");
57  const Geometry_ resol(resolConfig);
58  Log::info() << "Setup resolution OK" << std::endl;
59 
60 // Setup variables
61  const eckit::LocalConfiguration varConfig(fullConfig, "variables");
62  const Variables vars(varConfig);
63  Log::info() << "Setup variables OK" << std::endl;
64 
65 // Setup initial state
66  const eckit::LocalConfiguration initialConfig(fullConfig, "initial");
67  const State_ xx(resol, vars, initialConfig);
68  Log::info() << "Setup initial state OK" << std::endl;
69 
70 // Setup time
71  const util::DateTime bgndate(xx.validTime());
72  Log::info() << "Setup times OK" << std::endl;
73 
74 // Setup Dirac and random increments
75  Increment_ dxdir(resol, vars, bgndate);
76  const eckit::LocalConfiguration diracConfig(fullConfig, "dirac");
77  dxdir.dirac(diracConfig);
78  Increment_ dxrnd(resol, vars, bgndate);
79  dxrnd.random();
80  Increment_ dxdirout(dxdir);
81  Increment_ dxrndout(dxrnd);
82  Increment_ x1(dxdir);
83  Increment_ x2(dxdir);
84  Increment_ x1save(dxdir);
85  Increment_ x2save(dxdir);
86  Log::info() << "Setup increments OK" << std::endl;
87 
88 // Setup B matrix
89  const eckit::LocalConfiguration covarConfig(fullConfig, "Covariance");
90  boost::scoped_ptr< ModelSpaceCovarianceBase<MODEL> > B(CovarianceFactory<MODEL>::create(
91  covarConfig, resol, vars, xx, xx));
92  Log::info() << "Setup full ensemble B matrix OK" << std::endl;
93 
94  if (covarConfig.has("localization")) {
95  // Setup localization
96  const eckit::LocalConfiguration locConfig(covarConfig, "localization");
97  boost::scoped_ptr<Localization_> loc_;
98  loc_.reset(new Localization_(resol, locConfig));
99  Log::trace() << "Setup localization OK" << std::endl;
100 
101  // Apply localization
102  loc_->multiply(dxdirout);
103  loc_->multiply(dxrndout);
104  Log::trace() << "Apply localization OK" << std::endl;
105 
106  // Write increment
107  const eckit::LocalConfiguration output_localization(fullConfig, "output_localization");
108  dxdirout.write(output_localization);
109  Log::trace() << "Write increment OK" << std::endl;
110  Log::test() << "Increment: " << dxrndout << std::endl;
111 
112  // Test adjoint
113  x1.random();
114  x2.random();
115  x1save = x1;
116  x2save = x2;
117  loc_->multiply(x1);
118  loc_->multiply(x2);
119  double p1 = x1.dot_product_with(x2save);
120  double p2 = x2.dot_product_with(x1save);
121  Log::test() << "Adjoint test: " << p1 << " / " << p2 << std::endl;
122  }
123 
124 // Apply B matrix to Dirac increment
125  B->multiply(dxdir, dxdirout);
126  B->multiply(dxrnd, dxrndout);
127  Log::info() << "Apply B matrix OK" << std::endl;
128 
129 // Write increment
130  const eckit::LocalConfiguration output_B(fullConfig, "output_B");
131  dxdirout.write(output_B);
132  Log::trace() << "Write increment OK" << std::endl;
133  Log::test() << "Increment: " << dxrndout << std::endl;
134 
135 // Test adjoint
136  x1.random();
137  x2.random();
138  x1save = x1;
139  x2save = x2;
140  B->multiply(x1save, x1);
141  B->multiply(x2save, x2);
142  double p1 = x1.dot_product_with(x2save);
143  double p2 = x2.dot_product_with(x1save);
144  Log::test() << "Adjoint test: " << p1 << " / " << p2 << std::endl;
145 
146  return 0;
147  }
148 // -----------------------------------------------------------------------------
149  private:
150  std::string appname() const {
151  return "oops::Dirac<" + MODEL::name() + ">";
152  }
153 // -----------------------------------------------------------------------------
154 };
155 
156 } // namespace oops
157 #endif // OOPS_RUNS_DIRAC_H_
State< MODEL > State_
Definition: Dirac.h:43
void write(const eckit::Configuration &) const
Encapsulates the model state.
program test
character(len=32) name
Geometry< MODEL > Geometry_
Definition: Dirac.h:39
The namespace for the main oops code.
std::string appname() const
Definition: Dirac.h:150
subroutine, public info(self)
Localization< MODEL > Localization_
Definition: Dirac.h:44
virtual ~Dirac()
Definition: Dirac.h:52
Encapsulates the nonlinear forecast model.
double dot_product_with(const Increment &) const
int execute(const eckit::Configuration &fullConfig) const
Definition: Dirac.h:54
Increment Class: Difference between two states.
Model< MODEL > Model_
Definition: Dirac.h:40
Increment< MODEL > Increment_
Definition: Dirac.h:42
real, parameter p2
Definition: sw_core_nlm.F90:47
void dirac(const eckit::Configuration &)
Dirac()
Definition: Dirac.h:48
real, parameter p1
Definition: sw_core_nlm.F90:46
ModelAuxControl< MODEL > ModelAux_
Definition: Dirac.h:41
const util::DateTime validTime() const
Time.