FV3 Bundle
ModelBase.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2018 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_BASE_MODELBASE_H_
9 #define OOPS_BASE_MODELBASE_H_
10 
11 #include <map>
12 #include <string>
13 
14 #include <boost/noncopyable.hpp>
15 
18 #include "oops/interface/State.h"
19 #include "oops/util/abor1_cpp.h"
20 #include "oops/util/Duration.h"
21 #include "oops/util/Logger.h"
22 #include "oops/util/ObjectCounter.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 // -----------------------------------------------------------------------------
33 
34 /// Base class for encapsulation of the forecast model.
35 /*!
36  * Defines the interfaces for a forecast model.
37  */
38 
39 template <typename MODEL>
40 class ModelBase : public util::Printable,
41  private boost::noncopyable {
45 
46  public:
47  static const std::string classname() {return "oops::ModelBase";}
48 
49  ModelBase() {}
50  virtual ~ModelBase() {}
51 
52 // Run the model forecast
53  void initialize(State_ &) const;
54  void step(State_ &, const ModelAux_ &) const;
55  void finalize(State_ &) const;
56 
57 // Information and diagnostics
58  virtual const util::Duration & timeResolution() const = 0;
59  virtual const oops::Variables & variables() const = 0;
60 
61  protected:
62 // Run the model forecast
63  virtual void initialize(typename MODEL::State &) const = 0;
64  virtual void step(typename MODEL::State &, const typename MODEL::ModelAuxControl &) const = 0;
65  virtual void finalize(typename MODEL::State &) const = 0;
66 
67  private:
68 // Information and diagnostics
69  virtual void print(std::ostream &) const = 0;
70 };
71 
72 // =============================================================================
73 
74 /// ModelFactory Factory
75 template <typename MODEL>
76 class ModelFactory {
78  public:
79  static ModelBase<MODEL> * create(const Geometry_ &, const eckit::Configuration &);
80  virtual ~ModelFactory() { getMakers().clear(); }
81  protected:
82  explicit ModelFactory(const std::string &);
83  private:
84  virtual ModelBase<MODEL> * make(const Geometry_ &, const eckit::Configuration &) = 0;
85  static std::map < std::string, ModelFactory<MODEL> * > & getMakers() {
86  static std::map < std::string, ModelFactory<MODEL> * > makers_;
87  return makers_;
88  }
89 };
90 
91 // -----------------------------------------------------------------------------
92 
93 template<class MODEL, class T>
94 class ModelMaker : public ModelFactory<MODEL> {
96  virtual ModelBase<MODEL> * make(const Geometry_ & geom, const eckit::Configuration & conf)
97  { return new T(geom.geometry(), conf); }
98  public:
99  explicit ModelMaker(const std::string & name) : ModelFactory<MODEL>(name) {}
100 };
101 
102 // -----------------------------------------------------------------------------
103 
104 template <typename MODEL>
106  if (getMakers().find(name) != getMakers().end()) {
107  Log::error() << name << " already registered in the model factory." << std::endl;
108  ABORT("Element already registered in ModelFactory.");
109  }
110  getMakers()[name] = this;
111 }
112 
113 // -----------------------------------------------------------------------------
114 
115 template <typename MODEL>
117  const eckit::Configuration & conf) {
118  Log::trace() << "ModelBase<MODEL>::create starting" << std::endl;
119  const std::string id = conf.getString("name");
120  typename std::map<std::string, ModelFactory<MODEL>*>::iterator
121  jerr = getMakers().find(id);
122  if (jerr == getMakers().end()) {
123  Log::error() << id << " does not exist in the model factory." << std::endl;
124  ABORT("Element does not exist in ModelFactory.");
125  }
126  ModelBase<MODEL> * ptr = jerr->second->make(geom, conf);
127  Log::trace() << "ModelBase<MODEL>::create done" << std::endl;
128  return ptr;
129 }
130 
131 // =============================================================================
132 
133 template<typename MODEL>
135  Log::trace() << "ModelBase<MODEL>::initialize starting" << std::endl;
136  util::Timer timer(classname(), "initialize");
137  this->initialize(xx.state());
138  Log::trace() << "ModelBase<MODEL>::initialize done" << std::endl;
139 }
140 
141 // -----------------------------------------------------------------------------
142 
143 template<typename MODEL>
144 void ModelBase<MODEL>::step(State_ & xx, const ModelAux_ & merr) const {
145  Log::trace() << "ModelBase<MODEL>::step starting" << std::endl;
146  util::Timer timer(classname(), "step");
147  this->step(xx.state(), merr.modelauxcontrol());
148  Log::trace() << "ModelBase<MODEL>::step done" << std::endl;
149 }
150 
151 // -----------------------------------------------------------------------------
152 
153 template<typename MODEL>
155  Log::trace() << "ModelBase<MODEL>::finalize starting" << std::endl;
156  util::Timer timer(classname(), "finalize");
157  this->finalize(xx.state());
158  Log::trace() << "ModelBase<MODEL>::finalize done" << std::endl;
159 }
160 
161 // -----------------------------------------------------------------------------
162 
163 } // namespace oops
164 
165 #endif // OOPS_BASE_MODELBASE_H_
subroutine finalize(self)
Definition: ncutils.F90:61
virtual ~ModelBase()
Definition: ModelBase.h:50
ModelMaker(const std::string &name)
Definition: ModelBase.h:99
************************************************************************GNU Lesser General Public License **This file is part of the GFDL Flexible Modeling System(FMS). ! *! *FMS is free software without even the implied warranty of MERCHANTABILITY or *FITNESS FOR A PARTICULAR PURPOSE See the GNU General Public License *for more details **You should have received a copy of the GNU Lesser General Public *License along with FMS If see< http:! ***********************************************************************!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !! MPP_TRANSMIT !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! subroutine MPP_TRANSMIT_(put_data, put_len, to_pe, get_data, get_len, from_pe, block, tag, recv_request, send_request)!a message-passing routine intended to be reminiscent equally of both MPI and SHMEM!put_data and get_data are contiguous MPP_TYPE_ arrays!at each call, your put_data array is put to to_pe 's get_data! your get_data array is got from from_pe 's put_data!i.e we assume that typically(e.g updating halo regions) each PE performs a put _and_ a get!special PE designations:! NULL_PE:to disable a put or a get(e.g at boundaries)! ANY_PE:if remote PE for the put or get is to be unspecific! ALL_PES:broadcast and collect operations(collect not yet implemented)!ideally we would not pass length, but this f77-style call performs better(arrays passed by address, not descriptor)!further, this permits< length > contiguous words from an array of any rank to be passed(avoiding f90 rank conformance check)!caller is responsible for completion checks(mpp_sync_self) before and after integer, intent(in) ::put_len, to_pe, get_len, from_pe MPP_TYPE_, intent(in) ::put_data(*) MPP_TYPE_, intent(out) ::get_data(*) logical, intent(in), optional ::block integer, intent(in), optional ::tag integer, intent(out), optional ::recv_request, send_request logical ::block_comm integer ::i MPP_TYPE_, allocatable, save ::local_data(:) !local copy used by non-parallel code(no SHMEM or MPI) integer ::comm_tag integer ::rsize if(.NOT.module_is_initialized) call mpp_error(FATAL, 'MPP_TRANSMIT:You must first call mpp_init.') if(to_pe.EQ.NULL_PE .AND. from_pe.EQ.NULL_PE) return block_comm=.true. if(PRESENT(block)) block_comm=block if(debug) then call SYSTEM_CLOCK(tick) write(stdout_unit,'(a, i18, a, i6, a, 2i6, 2i8)')&'T=', tick, ' PE=', pe, ' MPP_TRANSMIT begin:to_pe, from_pe, put_len, get_len=', to_pe, from_pe, put_len, get_len end if comm_tag=DEFAULT_TAG if(present(tag)) comm_tag=tag!do put first and then get if(to_pe.GE.0 .AND. to_pe.LT.npes) then!use non-blocking sends if(debug .and.(current_clock.NE.0)) call SYSTEM_CLOCK(start_tick)!z1l:truly non-blocking send.! if(request(to_pe).NE.MPI_REQUEST_NULL) then !only one message from pe-> to_pe in queue *PE waiting for to_pe ! call error else get_len so only do gets but you cannot have a pure get with MPI call a get means do a wait to ensure put on remote PE is complete error call increase mpp_nml request_multiply call MPP_TRANSMIT end
Geometry< MODEL > Geometry_
Definition: ModelBase.h:77
Definition: conf.py:1
static const std::string classname()
Definition: ModelBase.h:47
void initialize(State_ &) const
Definition: ModelBase.h:134
static ModelBase< MODEL > * create(const Geometry_ &, const eckit::Configuration &)
Definition: ModelBase.h:116
virtual ModelBase< MODEL > * make(const Geometry_ &geom, const eckit::Configuration &conf)
Definition: ModelBase.h:96
Encapsulates the model state.
virtual ~ModelFactory()
Definition: ModelBase.h:80
State_ & state()
Interfacing.
character(len=32) name
The namespace for the main oops code.
ModelAuxControl< MODEL > ModelAux_
Definition: ModelBase.h:43
subroutine, private initialize
virtual const util::Duration & timeResolution() const =0
************************************************************************GNU Lesser General Public License **This file is part of the GFDL Flexible Modeling System(FMS). ! *! *FMS is free software without even the implied warranty of MERCHANTABILITY or *FITNESS FOR A PARTICULAR PURPOSE See the GNU General Public License *for more details **You should have received a copy of the GNU Lesser General Public *License along with FMS If see< http:! ***********************************************************************!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !! MPP_TRANSMIT !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! subroutine MPP_TRANSMIT_(put_data, put_len, to_pe, get_data, get_len, from_pe, block, tag, recv_request, send_request)!a message-passing routine intended to be reminiscent equally of both MPI and SHMEM!put_data and get_data are contiguous MPP_TYPE_ arrays!at each call, your put_data array is put to to_pe 's get_data! your get_data array is got from from_pe 's put_data!i.e we assume that typically(e.g updating halo regions) each PE performs a put _and_ a get!special PE designations:! NULL_PE:to disable a put or a get(e.g at boundaries)! ANY_PE:if remote PE for the put or get is to be unspecific! ALL_PES:broadcast and collect operations(collect not yet implemented)!ideally we would not pass length, but this f77-style call performs better(arrays passed by address, not descriptor)!further, this permits< length > contiguous words from an array of any rank to be passed(avoiding f90 rank conformance check)!caller is responsible for completion checks(mpp_sync_self) before and after integer, intent(in) ::put_len, to_pe, get_len, from_pe MPP_TYPE_, intent(in) ::put_data(*) MPP_TYPE_, intent(out) ::get_data(*) logical, intent(in), optional ::block integer, intent(in), optional ::tag integer, intent(out), optional ::recv_request, send_request logical ::block_comm integer ::i MPP_TYPE_, allocatable, save ::local_data(:) !local copy used by non-parallel code(no SHMEM or MPI) integer ::comm_tag integer ::rsize if(.NOT.module_is_initialized) call mpp_error(FATAL, 'MPP_TRANSMIT:You must first call mpp_init.') if(to_pe.EQ.NULL_PE .AND. from_pe.EQ.NULL_PE) return block_comm=.true. if(PRESENT(block)) block_comm=block if(debug) then call SYSTEM_CLOCK(tick) write(stdout_unit,'(a, i18, a, i6, a, 2i6, 2i8)')&'T=', tick, ' PE=', pe, ' MPP_TRANSMIT begin:to_pe, from_pe, put_len, get_len=', to_pe, from_pe, put_len, get_len end if comm_tag=DEFAULT_TAG if(present(tag)) comm_tag=tag!do put first and then get if(to_pe.GE.0 .AND. to_pe.LT.npes) then!use non-blocking sends if(debug .and.(current_clock.NE.0)) call SYSTEM_CLOCK(start_tick)!z1l:truly non-blocking send.! if(request(to_pe).NE.MPI_REQUEST_NULL) then !only one message from pe-> to_pe in queue *PE waiting for to_pe ! call error else get_len so only do gets but you cannot have a pure get with MPI call a get means do a wait to ensure put on remote PE is complete error call increase mpp_nml request_multiply call MPP_TRANSMIT get_len end if return end subroutine MPP_TRANSMIT_ ! MPP_BROADCAST ! subroutine but that doesn t allow !broadcast to a subset of PEs This version and mpp_transmit will remain !backward compatible intent(inout) & T
virtual void print(std::ostream &) const =0
integer error
Definition: mpp.F90:1310
ModelFactory(const std::string &)
Definition: ModelBase.h:105
void finalize(State_ &) const
Definition: ModelBase.h:154
const ModelAuxControl_ & modelauxcontrol() const
Interfacing.
Geometry< MODEL > Geometry_
Definition: ModelBase.h:42
Geometry< MODEL > Geometry_
Definition: ModelBase.h:95
subroutine, public step(x, g)
const Geometry_ & geometry() const
Interfacing.
static std::map< std::string, ModelFactory< MODEL > *> & getMakers()
Definition: ModelBase.h:85
Base class for encapsulation of the forecast model.
Definition: ModelBase.h:40
virtual ModelBase< MODEL > * make(const Geometry_ &, const eckit::Configuration &)=0
void step(State_ &, const ModelAux_ &) const
Definition: ModelBase.h:144
virtual const oops::Variables & variables() const =0
State< MODEL > State_
Definition: ModelBase.h:44