OOPS
oops/interface/Model.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2009-2016 ECMWF.
3  * (C) Copyright 2018-2020 UCAR.
4  *
5  * This software is licensed under the terms of the Apache Licence Version 2.0
6  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
7  */
8 
9 #ifndef OOPS_INTERFACE_MODEL_H_
10 #define OOPS_INTERFACE_MODEL_H_
11 
12 #include <memory>
13 #include <string>
14 
15 #include <boost/noncopyable.hpp>
16 
17 #include "oops/base/ModelBase.h"
20 #include "oops/interface/State.h"
21 #include "oops/util/Duration.h"
22 #include "oops/util/Logger.h"
23 #include "oops/util/Printable.h"
24 #include "oops/util/Timer.h"
25 
26 namespace eckit {
27  class Configuration;
28 }
29 
30 namespace oops {
31 
32 /// \brief Encapsulates the nonlinear forecast model
33 /// Note: to see methods that need to be implemented in the forecast model implementation,
34 /// see ModelBase class.
35 ///
36 /// Note: implementations of this interface can opt to extract their settings either from
37 /// a Configuration object or from a subclass of ModelParametersBase.
38 ///
39 /// In the former case, they should provide a constructor with the following signature:
40 ///
41 /// Model(const Geometry_ &, const eckit::Configuration &);
42 ///
43 /// In the latter case, the implementer should first define a subclass of ModelParametersBase
44 /// holding the settings of the model in question. The implementation of the Model interface
45 /// should then typedef `Parameters_` to the name of that subclass and provide a constructor with
46 /// the following signature:
47 ///
48 /// Model(const Geometry_ &, const Parameters_ &);
49 // -----------------------------------------------------------------------------
50 
51 template <typename MODEL>
52 class Model : public util::Printable,
53  private boost::noncopyable,
54  private util::ObjectCounter<Model<MODEL> > {
59 
60  public:
61  static const std::string classname() {return "oops::Model";}
62 
63  Model(const Geometry_ &, const ModelParametersBase &);
64  Model(const Geometry_ &, const eckit::Configuration &);
65  virtual ~Model();
66 
67  /// \brief Run the forecast from state \p xx for \p len time, with \p post postprocessors
68  /// Does not need to be implemented in the models
69  void forecast(State_ & xx, const ModelAux_ &,
70  const util::Duration & len, PostProcessor<State_> & post) const;
71 
72  /// \brief Time step for running Model's forecast in oops (frequency with which the
73  /// State will be updated)
74  const util::Duration & timeResolution() const {return model_->timeResolution();}
75  /// \brief Model variables (only used in 4DVar)
76  const oops::Variables & variables() const {return model_->variables();}
77 
78  private:
79  /// \brief Forecast initialization, called before every forecast run
80  void initialize(State_ &) const;
81  /// \brief Forecast "step", called during forecast run; updates state to the next time
82  void step(State_ &, const ModelAux_ &) const;
83  /// \brief Forecast finalization; called after each forecast run
84  void finalize(State_ &) const;
85  /// \brief Print, used in logging
86  void print(std::ostream &) const;
87 
88  /// \brief Pointer to the Model implementation
89  std::unique_ptr<ModelBase_> model_;
90 };
91 
92 // =============================================================================
93 
94 template<typename MODEL>
95 Model<MODEL>::Model(const Geometry_ & resol, const ModelParametersBase & params)
96  : model_()
97 {
98  Log::trace() << "Model<MODEL>::Model starting" << std::endl;
99  util::Timer timer(classname(), "Model");
100  Log::info() << "Model configuration is:" << params << std::endl;
101  model_.reset(ModelFactory<MODEL>::create(resol, params));
102  Log::trace() << "Model<MODEL>::Model done" << std::endl;
103 }
104 
105 // -----------------------------------------------------------------------------
106 
107 template<typename MODEL>
108 Model<MODEL>::Model(const Geometry_ & resol, const eckit::Configuration & conf)
109  : Model(resol, validateAndDeserialize<ModelParametersWrapper<MODEL>>(conf).modelParameters)
110 {}
111 
112 // -----------------------------------------------------------------------------
113 
114 template<typename MODEL>
116  Log::trace() << "Model<MODEL>::~Model starting" << std::endl;
117  util::Timer timer(classname(), "~Model");
118  model_.reset();
119  Log::trace() << "Model<MODEL>::~Model done" << std::endl;
120 }
121 
122 // -----------------------------------------------------------------------------
123 
124 template<typename MODEL>
125 void Model<MODEL>::forecast(State_ & xx, const ModelAux_ & maux,
126  const util::Duration & len,
127  PostProcessor<State_> & post) const {
128  Log::trace() << "Model<MODEL>::forecast starting" << std::endl;
129 
130  const util::DateTime end(xx.validTime() + len);
131  Log::info() << "Model:forecast: forecast starting: " << xx << std::endl;
132  this->initialize(xx);
133  post.initialize(xx, end, model_->timeResolution());
134  post.process(xx);
135  while (xx.validTime() < end) {
136  this->step(xx, maux);
137  post.process(xx);
138  }
139  post.finalize(xx);
140  this->finalize(xx);
141  Log::info() << "Model:forecast: forecast finished: " << xx << std::endl;
142  ASSERT(xx.validTime() == end);
143 
144  Log::trace() << "Model<MODEL>::forecast done" << std::endl;
145 }
146 
147 // -----------------------------------------------------------------------------
148 
149 template<typename MODEL>
151  Log::trace() << "Model<MODEL>::initialize starting" << std::endl;
152  util::Timer timer(classname(), "initialize");
153  model_->initialize(xx.state());
154  Log::trace() << "Model<MODEL>::initialize done" << std::endl;
155 }
156 
157 // -----------------------------------------------------------------------------
158 
159 template<typename MODEL>
160 void Model<MODEL>::step(State_ & xx, const ModelAux_ & maux) const {
161  Log::trace() << "Model<MODEL>::step starting" << std::endl;
162  util::Timer timer(classname(), "step");
163  model_->step(xx.state(), maux.modelauxcontrol());
164  Log::trace() << "Model<MODEL>::step done" << std::endl;
165 }
166 
167 // -----------------------------------------------------------------------------
168 
169 template<typename MODEL>
170 void Model<MODEL>::finalize(State_ & xx) const {
171  Log::trace() << "Model<MODEL>::finalize starting" << std::endl;
172  util::Timer timer(classname(), "finalize");
173  model_->finalize(xx.state());
174  Log::trace() << "Model<MODEL>::finalize done" << std::endl;
175 }
176 
177 // -----------------------------------------------------------------------------
178 
179 template<typename MODEL>
180 void Model<MODEL>::print(std::ostream & os) const {
181  Log::trace() << "Model<MODEL>::print starting" << std::endl;
182  util::Timer timer(classname(), "print");
183  os << *model_;
184  Log::trace() << "Model<MODEL>::print done" << std::endl;
185 }
186 
187 // -----------------------------------------------------------------------------
188 
189 } // namespace oops
190 
191 #endif // OOPS_INTERFACE_MODEL_H_
oops::Model::ModelBase_
ModelBase< MODEL > ModelBase_
Definition: oops/interface/Model.h:55
oops::ModelBase
Base class for the forecasting model Defines the interfaces for a forecast model.
Definition: ModelBase.h:45
oops::State::validTime
const util::DateTime validTime() const
Time.
Definition: oops/interface/State.h:60
oops
The namespace for the main oops code.
Definition: ErrorCovarianceL95.cc:22
oops::PostProcessor::process
void process(const FLDS &xx)
Definition: PostProcessor.h:56
oops::Model::ModelAux_
ModelAuxControl< MODEL > ModelAux_
Definition: oops/interface/Model.h:57
oops::Model::model_
std::unique_ptr< ModelBase_ > model_
Pointer to the Model implementation.
Definition: oops/interface/Model.h:89
oops::PostProcessor::finalize
void finalize(const FLDS &xx)
Definition: PostProcessor.h:62
oops::Model::classname
static const std::string classname()
Definition: oops/interface/Model.h:61
oops::Model::~Model
virtual ~Model()
Definition: oops/interface/Model.h:115
oops::ModelAuxControl
Definition: oops/interface/ModelAuxControl.h:35
oops::Model::timeResolution
const util::Duration & timeResolution() const
Time step for running Model's forecast in oops (frequency with which the State will be updated)
Definition: oops/interface/Model.h:74
oops::Model::finalize
void finalize(State_ &) const
Forecast finalization; called after each forecast run.
Definition: oops/interface/Model.h:170
oops::Model::Geometry_
Geometry< MODEL > Geometry_
Definition: oops/interface/Model.h:56
ModelBase.h
oops::Model::print
void print(std::ostream &) const
Print, used in logging.
Definition: oops/interface/Model.h:180
oops::State::state
State_ & state()
Interfacing.
Definition: oops/interface/State.h:56
eckit
Definition: FieldL95.h:22
oops::Model::forecast
void forecast(State_ &xx, const ModelAux_ &, const util::Duration &len, PostProcessor< State_ > &post) const
Run the forecast from state xx for len time, with post postprocessors Does not need to be implemented...
Definition: oops/interface/Model.h:125
oops::Model::step
void step(State_ &, const ModelAux_ &) const
Forecast "step", called during forecast run; updates state to the next time.
Definition: oops/interface/Model.h:160
oops::Model::Model
Model(const Geometry_ &, const ModelParametersBase &)
Definition: oops/interface/Model.h:95
oops::PostProcessor::initialize
void initialize(const FLDS &xx, const util::DateTime &end, const util::Duration &step)
Definition: PostProcessor.h:49
oops::Geometry
Geometry class used in oops; subclass of interface class above.
Definition: oops/interface/Geometry.h:189
oops::Model::variables
const oops::Variables & variables() const
Model variables (only used in 4DVar)
Definition: oops/interface/Model.h:76
oops::ModelParametersWrapper
Contains a polymorphic parameter holding an instance of a subclass of ModelParametersBase.
Definition: ModelBase.h:116
oops::ModelParametersBase
Base class for classes storing model-specific parameters.
Definition: ModelBase.h:81
oops::Model::State_
State< MODEL > State_
Definition: oops/interface/Model.h:58
oops::State
Encapsulates the model state.
Definition: CostJbState.h:28
oops::PostProcessor
Control model post processing.
Definition: PostProcessor.h:30
State.h
oops::Variables
Definition: oops/base/Variables.h:23
oops::ModelFactory
Model factory.
Definition: ModelBase.h:76
oops::ModelAuxControl::modelauxcontrol
const ModelAuxControl_ & modelauxcontrol() const
Interfacing.
Definition: oops/interface/ModelAuxControl.h:48
oops::Model::initialize
void initialize(State_ &) const
Forecast initialization, called before every forecast run.
Definition: oops/interface/Model.h:150
oops::Model
Encapsulates the nonlinear forecast model Note: to see methods that need to be implemented in the for...
Definition: oops/interface/Model.h:54
ModelAuxControl.h
Geometry.h