FV3 Bundle
StateEnsemble.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_BASE_STATEENSEMBLE_H_
12 #define OOPS_BASE_STATEENSEMBLE_H_
13 
14 #include <string>
15 #include <vector>
16 
17 #include <boost/ptr_container/ptr_vector.hpp>
18 #include <boost/scoped_ptr.hpp>
19 
20 #include "eckit/config/LocalConfiguration.h"
21 #include "oops/base/Accumulator.h"
23 #include "oops/base/Variables.h"
26 #include "oops/interface/State.h"
27 #include "oops/util/DateTime.h"
28 #include "oops/util/dot_product.h"
29 #include "oops/util/Duration.h"
30 #include "oops/util/Logger.h"
31 
32 namespace oops {
33 
34 // -----------------------------------------------------------------------------
35 
36 /// Ensemble
37 
38 template<typename MODEL> class StateEnsemble {
43 
44  typedef typename boost::ptr_vector<LinearVariableChangeBase_> ChvarVec_;
45  typedef typename ChvarVec_::const_reverse_iterator ircst_;
46 
47  public:
48 /// Constructor
49  StateEnsemble(const util::DateTime &, const eckit::Configuration &);
50 
51 /// Destructor
52  virtual ~StateEnsemble() {}
53 
54  /// Accessors
55  unsigned int size() const {
56  return rank_;
57  }
58  Increment_ & operator[](const int ii) {
59  return ensemblePerturbs_[ii];
60  }
61  const Increment_ & operator[](const int ii) const {
62  return ensemblePerturbs_[ii];
63  }
64 
65  void linearize(const State_ &, const State_ &, const Geometry_ &);
66 
67  const Variables & controlVariables() const {return vars_;}
68 
69  private:
70  const eckit::LocalConfiguration config_;
71 
72  unsigned int rank_;
73  const util::DateTime validTime_;
75  boost::scoped_ptr<const Geometry_> resol_;
76 
77  boost::ptr_vector<Increment_> ensemblePerturbs_;
78 };
79 
80 // ====================================================================================
81 
82 template<typename MODEL>
83 StateEnsemble<MODEL>::StateEnsemble(const util::DateTime & validTime,
84  const eckit::Configuration & conf)
85  : config_(conf), rank_(conf.getInt("members")), validTime_(validTime),
86  vars_(eckit::LocalConfiguration(conf, "variables")),
87  resol_(), ensemblePerturbs_()
88 {
89  Log::trace() << "StateEnsemble:contructor done" << std::endl;
90 }
91 
92 // -----------------------------------------------------------------------------
93 
94 template<typename MODEL>
96  const Geometry_ & resol) {
97  ASSERT(xb.validTime() == validTime_);
98  resol_.reset(new Geometry_(resol));
99 
100 // Setup change of variable
101  ChvarVec_ chvars;
102  if (config_.has("variable_changes")) {
103  std::vector<eckit::LocalConfiguration> chvarconfs;
104  config_.get("variable_changes", chvarconfs);
105  for (const auto & conf : chvarconfs) {
106  chvars.push_back(LinearVariableChangeFactory<MODEL>::create(xb, fg, resol, conf));
107  }
108  }
109 
110 // Read ensemble and compute mean
111  State_ xblr(*resol_, xb);
112  Accumulator<MODEL, State_, State_> bgmean(*resol_, vars_, validTime_);
113  const double rr = 1.0/static_cast<double>(rank_);
114 
115  std::vector<eckit::LocalConfiguration> confs;
116  config_.get("state", confs);
117  ASSERT(confs.size() == rank_);
118 
119  State_ xread(xblr);
120  std::vector<State_> ensemble;
121  for (unsigned int jm = 0; jm < rank_; ++jm) {
122  xread.read(confs[jm]);
123  ASSERT(xread.validTime() == validTime_);
124  ensemble.push_back(xread);
125 
126 // Compute ensemble mean
127  bgmean.accumul(rr, xread);
128  }
129 
130  const double rk = 1.0 / sqrt((static_cast<double>(rank_) - 1.0));
131  for (unsigned int jm = 0; jm < rank_; ++jm) {
132 // Ensemble will be centered around ensemble mean
133  boost::scoped_ptr<Increment_> dx(new Increment_(*resol_, vars_, validTime_));
134  dx->diff(ensemble[jm], bgmean);
135 
136 // Apply inverse of the linear balance operator
137 
138  // K_1^{-1} K_2^{-1} .. K_N^{-1}
139  for (ircst_ it = chvars.rbegin(); it != chvars.rend(); ++it) {
140  Increment_ dxchvarout = it->multiplyInverse(*dx);
141  dx.reset(new Increment_(dxchvarout));
142  }
143 
144  Increment_ * dxunbalptr = new Increment_(*dx);
145  ensemblePerturbs_.push_back(dxunbalptr);
146 
147 // Rescale
148  ensemblePerturbs_[jm] *= rk;
149  }
150 }
151 
152 // -----------------------------------------------------------------------------
153 } // namespace oops
154 
155 #endif // OOPS_BASE_STATEENSEMBLE_H_
const util::DateTime validTime_
Definition: StateEnsemble.h:73
boost::ptr_vector< Increment_ > ensemblePerturbs_
Definition: StateEnsemble.h:77
unsigned int size() const
Accessors.
Definition: StateEnsemble.h:55
Definition: conf.py:1
const Variables vars_
Definition: StateEnsemble.h:74
Increment_ & operator[](const int ii)
Definition: StateEnsemble.h:58
Encapsulates the model state.
virtual ~StateEnsemble()
Destructor.
Definition: StateEnsemble.h:52
The namespace for the main oops code.
void accumul(const double &zz, const FLDS &xx)
Definition: Accumulator.h:30
boost::ptr_vector< LinearVariableChangeBase_ > ChvarVec_
Definition: StateEnsemble.h:44
Increment< MODEL > Increment_
Definition: StateEnsemble.h:42
real(fp), parameter xb
Definition: ufo_aod_mod.F90:41
const Increment_ & operator[](const int ii) const
Definition: StateEnsemble.h:61
LinearVariableChangeFactory Factory.
Geometry< MODEL > Geometry_
Definition: StateEnsemble.h:40
Base class for generic variable transform.
boost::scoped_ptr< const Geometry_ > resol_
Definition: StateEnsemble.h:75
unsigned int rank_
Definition: StateEnsemble.h:72
StateEnsemble(const util::DateTime &, const eckit::Configuration &)
Constructor.
Definition: StateEnsemble.h:83
const eckit::LocalConfiguration config_
Definition: StateEnsemble.h:70
Increment Class: Difference between two states.
void linearize(const State_ &, const State_ &, const Geometry_ &)
Definition: StateEnsemble.h:95
ChvarVec_::const_reverse_iterator ircst_
Definition: StateEnsemble.h:45
LinearVariableChangeBase< MODEL > LinearVariableChangeBase_
Definition: StateEnsemble.h:39
State< MODEL > State_
Definition: StateEnsemble.h:41
const Variables & controlVariables() const
Definition: StateEnsemble.h:67