OOPS
RTPP.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2019-2020 UCAR.
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  */
7 
8 #ifndef OOPS_RUNS_RTPP_H_
9 #define OOPS_RUNS_RTPP_H_
10 
11 #include <memory>
12 #include <string>
13 
14 #include "eckit/config/LocalConfiguration.h"
20 #include "oops/interface/State.h"
21 #include "oops/mpi/mpi.h"
22 #include "oops/runs/Application.h"
23 #include "oops/util/Logger.h"
24 
25 namespace oops {
26 
27 /// \brief Application for relaxation to prior perturbation (RTPP) inflation
28 template <typename MODEL> class RTPP : public Application {
33 
34  public:
35 // -----------------------------------------------------------------------------
36 
37  explicit RTPP(const eckit::mpi::Comm & comm = oops::mpi::world()) : Application(comm) {}
38 
39 // -----------------------------------------------------------------------------
40 
41  virtual ~RTPP() = default;
42 
43 // -----------------------------------------------------------------------------
44 
45  int execute(const eckit::Configuration & fullConfig) const {
46  // Setup geometry
47  const eckit::LocalConfiguration geometryConfig(fullConfig, "geometry");
48  const Geometry_ geometry(geometryConfig, this->getComm());
49 
50  // Get configurations
51  const eckit::LocalConfiguration bgConfig(fullConfig, "background");
52  const eckit::LocalConfiguration anConfig(fullConfig, "analysis");
53  const float factor = fullConfig.getFloat("factor");
54 
55  // Read all ensemble members
56  StateEnsemble_ bgens(geometry, bgConfig);
57  StateEnsemble_ anens(geometry, anConfig);
58  const size_t nens = bgens.size();
59  ASSERT(nens == anens.size());
60 
61  Variables anvars = anens.variables();
62  if (fullConfig.has("analysis variables"))
63  anvars = Variables(fullConfig, "analysis variables");
64 
65  // calculate ensemble means
66  State_ bg_mean = bgens.mean();
67  State_ an_mean = anens.mean();
68 
69  Log::test() << "Background member 1:" << bgens[0] << std::endl;
70  Log::test() << "Analysis member 1:" << anens[0] << std::endl;
71 
72  // update analysis with RTPP
73  for (size_t jj = 0; jj < nens; ++jj) {
74  // calculate RTPP perturbation
75  Increment_ pertTot(geometry, anvars, anens[jj].validTime());
76  pertTot.zero();
77 
78  Increment_ pert(pertTot);
79 
80  pert.diff(bgens[jj], bg_mean);
81  pertTot.axpy(factor, pert);
82 
83  pert.diff(anens[jj], an_mean);
84  pertTot.axpy((1.0 - factor), pert);
85 
86  // an_mean contains copies of anens[0] for non-state variables
87  // using zero+accumul instead of "=" ensures that only
88  // state variables are modified in anens[jj]
89  anens[jj].zero();
90  anens[jj].accumul(1.0, an_mean);
91 
92  // add analysis variable perturbations
93  anens[jj] += pertTot;
94  }
95  Log::test() << "Updated Analysis member 1:" << anens[0] << std::endl;
96 
97  // save the analysis mean
98  an_mean = anens.mean(); // calculate analysis mean
99  Log::test() << "Analysis mean:" << an_mean << std::endl;
100  eckit::LocalConfiguration outConfig(fullConfig, "output");
101  outConfig.set("member", 0);
102  an_mean.write(outConfig);
103 
104  // save the analysis ensemble
105  size_t mymember;
106  for (size_t jj=0; jj < nens; ++jj) {
107  mymember = jj+1;
108  outConfig.set("member", mymember);
109  anens[jj].write(outConfig);
110  }
111 
112  return 0;
113  }
114 
115 // -----------------------------------------------------------------------------
116 
117  private:
118  std::string appname() const {
119  return "oops::RTPP<" + MODEL::name() + ">";
120  }
121 
122 // -----------------------------------------------------------------------------
123 };
124 
125 } // namespace oops
126 #endif // OOPS_RUNS_RTPP_H_
oops
The namespace for the main oops code.
Definition: ErrorCovarianceL95.cc:22
oops::RTPP::~RTPP
virtual ~RTPP()=default
oops::RTPP::RTPP
RTPP(const eckit::mpi::Comm &comm=oops::mpi::world())
Definition: RTPP.h:37
mpi.h
oops::Increment::zero
void zero()
Linear algebra operators.
Definition: oops/interface/Increment.h:206
Increment4D.h
oops::Increment::diff
void diff(const State_ &, const State_ &)
Interactions with State.
Definition: oops/interface/Increment.h:196
oops::Increment::axpy
void axpy(const double &, const Increment &, const bool check=true)
Definition: oops/interface/Increment.h:290
oops::StateEnsemble::variables
const Variables & variables() const
Information.
Definition: StateEnsemble.h:43
StateEnsemble.h
oops::StateEnsemble
Ensemble of states.
Definition: StateEnsemble.h:26
oops::State::write
void write(const eckit::Configuration &) const
Definition: oops/interface/State.h:191
oops::RTPP::appname
std::string appname() const
Definition: RTPP.h:118
Application.h
oops::RTPP::Geometry_
Geometry< MODEL > Geometry_
Definition: RTPP.h:29
oops::Application::getComm
const eckit::mpi::Comm & getComm() const
Definition: Application.h:36
oops::Geometry
Geometry class used in oops; subclass of interface class above.
Definition: oops/interface/Geometry.h:189
State4D.h
oops::StateEnsemble::mean
State_ mean() const
calculate ensemble mean
Definition: StateEnsemble.h:68
oops::State
Encapsulates the model state.
Definition: CostJbState.h:28
oops::StateEnsemble::size
size_t size() const
Accessors.
Definition: StateEnsemble.h:38
oops::Application
Definition: Application.h:29
oops::RTPP::StateEnsemble_
StateEnsemble< MODEL > StateEnsemble_
Definition: RTPP.h:32
oops::RTPP::execute
int execute(const eckit::Configuration &fullConfig) const
Definition: RTPP.h:45
State.h
oops::mpi::world
const eckit::mpi::Comm & world()
Default communicator with all MPI tasks (ie MPI_COMM_WORLD)
Definition: oops/mpi/mpi.cc:22
oops::Variables
Definition: oops/base/Variables.h:23
oops::Increment
Increment Class: Difference between two states.
Definition: CostJbState.h:27
oops::RTPP::Increment_
Increment< MODEL > Increment_
Definition: RTPP.h:30
oops::RTPP
Application for relaxation to prior perturbation (RTPP) inflation.
Definition: RTPP.h:28
Geometry.h
Increment.h
oops::RTPP::State_
State< MODEL > State_
Definition: RTPP.h:31