FV3 Bundle
LinearVariableChangeBase.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_LINEARVARIABLECHANGEBASE_H_
9 #define OOPS_BASE_LINEARVARIABLECHANGEBASE_H_
10 
11 #include <map>
12 #include <string>
13 #include <boost/noncopyable.hpp>
14 
15 #include "oops/base/Variables.h"
18 #include "oops/interface/State.h"
19 #include "oops/util/abor1_cpp.h"
20 #include "oops/util/Printable.h"
21 
22 namespace eckit {
23  class Configuration;
24 }
25 
26 namespace oops {
27 
28 // -----------------------------------------------------------------------------
29 
30 /// Base class for generic variable transform
31 
32 template <typename MODEL>
33 class LinearVariableChangeBase : public util::Printable,
34  private boost::noncopyable {
38 
39  public:
40  explicit LinearVariableChangeBase(const eckit::Configuration &);
42 
43  void setInputVariables(const Variables & vars) { varin_.reset(new Variables(vars)); }
44  void setOutputVariables(const Variables & vars) { varout_.reset(new Variables(vars)); }
45 
46  virtual void multiply(const Increment_ &, Increment_ &) const = 0;
47  virtual void multiplyInverse(const Increment_ &, Increment_ &) const = 0;
48  virtual void multiplyAD(const Increment_ &, Increment_ &) const = 0;
49  virtual void multiplyInverseAD(const Increment_ &, Increment_ &) const = 0;
50 
51  Increment_ multiply(const Increment_ &) const;
52  Increment_ multiplyInverse(const Increment_ &) const;
53  Increment_ multiplyAD(const Increment_ &) const;
55 
56  private:
57  virtual void print(std::ostream &) const = 0;
58  boost::scoped_ptr<Variables> varin_;
59  boost::scoped_ptr<Variables> varout_;
60 };
61 
62 // -----------------------------------------------------------------------------
63 
64 /// LinearVariableChangeFactory Factory
65 template <typename MODEL>
69  public:
70  static LinearVariableChangeBase<MODEL> * create(const State_ &, const State_ &,
71  const Geometry_ &,
72  const eckit::Configuration &);
73  virtual ~LinearVariableChangeFactory() { getMakers().clear(); }
74  protected:
75  explicit LinearVariableChangeFactory(const std::string &);
76  private:
77  virtual LinearVariableChangeBase<MODEL> * make(const State_ &, const State_ &,
78  const Geometry_ &,
79  const eckit::Configuration &) = 0;
80  static std::map < std::string, LinearVariableChangeFactory<MODEL> * > & getMakers() {
81  static std::map < std::string, LinearVariableChangeFactory<MODEL> * > makers_;
82  return makers_;
83  }
84 };
85 
86 // -----------------------------------------------------------------------------
87 
88 template<class MODEL, class T>
92  virtual LinearVariableChangeBase<MODEL> * make(const State_ & bg, const State_ & fg,
93  const Geometry_ & geom,
94  const eckit::Configuration & conf)
95  { return new T(bg, fg, geom, conf); }
96  public:
97  explicit LinearVariableChangeMaker(const std::string & name)
99 };
100 
101 // -----------------------------------------------------------------------------
102 
103 template <typename MODEL>
105  if (getMakers().find(name) != getMakers().end()) {
106  Log::error() << name << " already registered in the variable change factory." << std::endl;
107  ABORT("Element already registered in LinearVariableChangeFactory.");
108  }
109  getMakers()[name] = this;
110 }
111 
112 // -----------------------------------------------------------------------------
113 
114 template <typename MODEL>
116  const State_ & bg, const State_ & fg,
117  const Geometry_ & geom, const eckit::Configuration & conf) {
118  Log::trace() << "LinearVariableChangeBase<MODEL>::create starting" << std::endl;
119  const std::string id = conf.getString("varchange");
120  typename std::map<std::string, LinearVariableChangeFactory<MODEL>*>::iterator
121  jerr = getMakers().find(id);
122 
123  if (jerr == getMakers().end()) {
124  Log::error() << id << " does not exist in LinearVariableChangeFactory." << std::endl;
125  Log::error() << "Factory contains " << getMakers().size() << " elements:" << std::endl;
126  for (typename std::map<std::string, LinearVariableChangeFactory<MODEL>*>::const_iterator
127  jj = getMakers().begin(); jj != getMakers().end(); ++jj) {
128  Log::error() << "A " << jj->first << " variable change option" << std::endl;
129  }
130  ABORT("Element does not exist in LinearVariableChangeFactory.");
131  }
132 
133  LinearVariableChangeBase<MODEL> * ptr = jerr->second->make(bg, fg, geom, conf);
134  Log::trace() << "LinearVariableChangeBase<MODEL>::create done" << std::endl;
135  return ptr;
136 }
137 
138 // =============================================================================
139 
140 template<typename MODEL>
142  : varin_(), varout_()
143 {
144  if (conf.has("inputVariables")) {
145  varin_.reset(new Variables(conf.getSubConfiguration("inputVariables")));
146  Log::trace() << "LinearVariableChangeBase<MODEL>::LinearVariableChangeBase inputvars: "
147  << *varin_ << std::endl;
148  }
149  if (conf.has("outputVariables")) {
150  varout_.reset(new Variables(conf.getSubConfiguration("outputVariables")));
151  Log::trace() << "LinearVariableChangeBase<MODEL>::LinearVariableChangeBase outputvars: "
152  << *varout_ << std::endl;
153  }
154 }
155 
156 // -----------------------------------------------------------------------------
157 
158 template<typename MODEL>
160  ASSERT(varin_);
161  Increment_ dxout(dxin.geometry(), *varin_, dxin.validTime());
162  this->multiply(dxin, dxout);
163  return dxout;
164 }
165 
166 // -----------------------------------------------------------------------------
167 
168 template<typename MODEL>
170  ASSERT(varout_);
171  Increment_ dxout(dxin.geometry(), *varout_, dxin.validTime());
172  this->multiplyAD(dxin, dxout);
173  return dxout;
174 }
175 
176 // -----------------------------------------------------------------------------
177 
178 template<typename MODEL>
180  ASSERT(varout_);
181  Increment_ dxout(dxin.geometry(), *varout_, dxin.validTime());
182  this->multiplyInverse(dxin, dxout);
183  return dxout;
184 }
185 
186 // -----------------------------------------------------------------------------
187 
188 template<typename MODEL>
190  ASSERT(varin_);
191  Increment_ dxout(dxin.geometry(), *varin_, dxin.validTime());
192  this->multiplyInverseAD(dxin, dxout);
193  return dxout;
194 }
195 
196 // -----------------------------------------------------------------------------
197 
198 } // namespace oops
199 
200 #endif // OOPS_BASE_LINEARVARIABLECHANGEBASE_H_
virtual void multiplyInverseAD(const Increment_ &, Increment_ &) const =0
virtual void multiplyAD(const Increment_ &, Increment_ &) const =0
Geometry_ geometry() const
Get geometry.
LinearVariableChangeMaker(const std::string &name)
static std::map< std::string, LinearVariableChangeFactory< MODEL > *> & getMakers()
************************************************************************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
Definition: conf.py:1
const util::DateTime validTime() const
Time.
virtual LinearVariableChangeBase< MODEL > * make(const State_ &bg, const State_ &fg, const Geometry_ &geom, const eckit::Configuration &conf)
Encapsulates the model state.
LinearVariableChangeBase(const eckit::Configuration &)
void setOutputVariables(const Variables &vars)
character(len=32) name
boost::scoped_ptr< Variables > varout_
The namespace for the main oops code.
subroutine, public multiply(self, geom, xctl, xmod)
void setInputVariables(const Variables &vars)
************************************************************************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
integer error
Definition: mpp.F90:1310
boost::scoped_ptr< Variables > varin_
virtual void print(std::ostream &) const =0
LinearVariableChangeFactory Factory.
Base class for generic variable transform.
Increment Class: Difference between two states.
virtual void multiply(const Increment_ &, Increment_ &) const =0
static LinearVariableChangeBase< MODEL > * create(const State_ &, const State_ &, const Geometry_ &, const eckit::Configuration &)
virtual void multiplyInverse(const Increment_ &, Increment_ &) 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) MPP_BROADCAST begin
virtual LinearVariableChangeBase< MODEL > * make(const State_ &, const State_ &, const Geometry_ &, const eckit::Configuration &)=0