FV3 Bundle
ModelSpaceCovarianceBase.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_MODELSPACECOVARIANCEBASE_H_
12 #define OOPS_BASE_MODELSPACECOVARIANCEBASE_H_
13 
14 #include <map>
15 #include <string>
16 #include <vector>
17 #include <boost/noncopyable.hpp>
18 #include <boost/ptr_container/ptr_vector.hpp>
19 
20 #include "eckit/config/LocalConfiguration.h"
22 #include "oops/base/Variables.h"
25 #include "oops/interface/State.h"
26 #include "oops/util/abor1_cpp.h"
27 #include "oops/util/Logger.h"
28 
29 namespace util {
30  class DateTime;
31 }
32 
33 namespace oops {
34 
35 // -----------------------------------------------------------------------------
36 
37 // Should this be one with the ErrorCovariance class in the interface directory? YT
38 
39 /// Abstract base class for model space error covariances.
40 template <typename MODEL>
46  typedef typename boost::ptr_vector<LinearVariableChangeBase_> ChvarVec_;
47  typedef typename ChvarVec_::iterator iter_;
48  typedef typename ChvarVec_::const_iterator icst_;
49  typedef typename ChvarVec_::const_reverse_iterator ircst_;
50 
51  public:
52  ModelSpaceCovarianceBase(const State_ &, const State_ &,
53  const Geometry_ &, const eckit::Configuration &);
55 
56 // const LinearVariableChangeBase_ & getK(const unsigned & ii) const {return *chvars_[ii];}
57 // bool hasK() const { return (chvars_.size() == 0) ? false : true; }
58 
59  void multiply(const Increment_ &, Increment_ &) const;
60  void inverseMultiply(const Increment_ &, Increment_ &) const;
61 
62  virtual void randomize(Increment_ &) const = 0;
63 
64  private:
65  virtual void doMultiply(const Increment_ &, Increment_ &) const = 0;
66  virtual void doInverseMultiply(const Increment_ &, Increment_ &) const = 0;
67 
69 };
70 
71 // -----------------------------------------------------------------------------
72 
73 /// Covariance Factory
74 template <typename MODEL>
78 
79  public:
80  static ModelSpaceCovarianceBase<MODEL> * create(const eckit::Configuration &,
81  const Geometry_ &, const Variables &,
82  const State_ &, const State_ &);
83  virtual ~CovarianceFactory() { getMakers().clear(); }
84  protected:
85  explicit CovarianceFactory(const std::string &);
86  private:
87  virtual ModelSpaceCovarianceBase<MODEL> * make(const eckit::Configuration &,
88  const Geometry_ &, const Variables &,
89  const State_ &, const State_ &) = 0;
90  static std::map < std::string, CovarianceFactory<MODEL> * > & getMakers() {
91  static std::map < std::string, CovarianceFactory<MODEL> * > makers_;
92  return makers_;
93  }
94 };
95 
96 // -----------------------------------------------------------------------------
97 
98 template<class MODEL, class COVAR>
99 class CovarMaker : public CovarianceFactory<MODEL> {
102 
103  virtual ModelSpaceCovarianceBase<MODEL> * make(const eckit::Configuration & conf,
104  const Geometry_ & resol, const Variables & vars,
105  const State_ & xb, const State_ & fg) {
106  return new COVAR(resol, vars, conf, xb, fg);
107  }
108  public:
109  explicit CovarMaker(const std::string & name) : CovarianceFactory<MODEL>(name) {}
110 };
111 
112 // =============================================================================
113 
114 template <typename MODEL>
116  if (getMakers().find(name) != getMakers().end()) {
117  Log::error() << name << " already registered in covariance factory." << std::endl;
118  ABORT("Element already registered in CovarianceFactory.");
119  }
120  getMakers()[name] = this;
121 }
122 
123 // -----------------------------------------------------------------------------
124 
125 template <typename MODEL>
127  const eckit::Configuration & conf,
128  const Geometry_ & resol,
129  const Variables & vars,
130  const State_ & xb, const State_ & fg) {
131  const std::string id = conf.getString("covariance");
132  Log::trace() << "ModelSpaceCovarianceBase type = " << id << std::endl;
133  typename std::map<std::string, CovarianceFactory<MODEL>*>::iterator jcov = getMakers().find(id);
134  if (jcov == getMakers().end()) {
135  Log::error() << id << " does not exist in CovarianceFactory." << std::endl;
136  Log::error() << "CovarianceFactory has " << getMakers().size() << " elements:" << std::endl;
137  for (typename std::map<std::string, CovarianceFactory<MODEL>*>::const_iterator
138  jj = getMakers().begin(); jj != getMakers().end(); ++jj) {
139  Log::error() << "A " << jj->first << " B" << std::endl;
140  }
141  ABORT("Element does not exist in CovarianceFactory.");
142  }
143  return (*jcov).second->make(conf, resol, vars, xb, fg);
144 }
145 
146 // =============================================================================
147 
148 template <typename MODEL>
150  const Geometry_ & resol,
151  const eckit::Configuration & conf) {
152  if (conf.has("variable_changes")) {
153  std::vector<eckit::LocalConfiguration> chvarconfs;
154  conf.get("variable_changes", chvarconfs);
155  for (const auto & config : chvarconfs) {
156  chvars_.push_back(LinearVariableChangeFactory<MODEL>::create(bg, fg, resol, config));
157  }
158  }
159 }
160 
161 // -----------------------------------------------------------------------------
162 
163 template <typename MODEL>
165  Increment_ & dxo) const {
166  if (chvars_.size()) {
167  // K_1^T K_2^T .. K_N^T
168  boost::scoped_ptr<Increment_> dxchvarin(new Increment_(dxi));
169  for (ircst_ it = chvars_.rbegin(); it != chvars_.rend(); ++it) {
170  Increment_ dxchvarout = it->multiplyAD(*dxchvarin);
171  dxchvarin.reset(new Increment_(dxchvarout));
172  }
173  Increment_ dxchvarout(*dxchvarin, false);
174 
175  this->doMultiply(*dxchvarin, dxchvarout);
176 
177  // K_N K_N-1 ... K_1
178  dxchvarin.reset(new Increment_(dxchvarout));
179  for (icst_ it = chvars_.begin(); it != chvars_.end(); ++it) {
180  Increment_ dxchvarout = it->multiply(*dxchvarin);
181  dxchvarin.reset(new Increment_(dxchvarout));
182  }
183  dxo = *dxchvarin;
184  } else {
185  this->doMultiply(dxi, dxo);
186  }
187 }
188 
189 // -----------------------------------------------------------------------------
190 
191 template <typename MODEL>
193  Increment_ & dxo) const {
194  if (chvars_.size()) {
195  // K_1^{-1} K_2^{-1} .. K_N^{-1}
196  boost::scoped_ptr<Increment_> dxchvarin(new Increment_(dxi));
197  for (ircst_ it = chvars_.rbegin(); it != chvars_.rend(); ++it) {
198  Increment_ dxchvarout = it->multiplyInverse(*dxchvarin);
199  dxchvarin.reset(new Increment_(dxchvarout));
200  }
201  Increment_ dxchvarout(*dxchvarin, false);
202 
203  this->doInverseMultiply(*dxchvarin, dxchvarout);
204 
205  // K_N^T^{-1} K_N-1^T^{-1} ... K_1^T^{-1}
206  dxchvarin.reset(new Increment_(dxchvarout));
207  for (icst_ it = chvars_.begin(); it != chvars_.end(); ++it) {
208  Increment_ dxchvarout = it->multiplyInverseAD(*dxchvarin);
209  dxchvarin.reset(new Increment_(dxchvarout));
210  }
211  dxo = *dxchvarin;
212  } else {
213  this->doInverseMultiply(dxi, dxo);
214  }
215 }
216 
217 // -----------------------------------------------------------------------------
218 
219 } // namespace oops
220 
221 #endif // OOPS_BASE_MODELSPACECOVARIANCEBASE_H_
************************************************************************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
CovarianceFactory(const std::string &)
Encapsulates the model state.
character(len=32) name
virtual ModelSpaceCovarianceBase< MODEL > * make(const eckit::Configuration &, const Geometry_ &, const Variables &, const State_ &, const State_ &)=0
The namespace for the main oops code.
Geometry< MODEL > Geometry_
virtual void doInverseMultiply(const Increment_ &, Increment_ &) const =0
LinearVariableChangeBase< MODEL > LinearVariableChangeBase_
void inverseMultiply(const Increment_ &, Increment_ &) const
static ModelSpaceCovarianceBase< MODEL > * create(const eckit::Configuration &, const Geometry_ &, const Variables &, const State_ &, const State_ &)
integer error
Definition: mpp.F90:1310
real(fp), parameter xb
Definition: ufo_aod_mod.F90:41
CovarMaker(const std::string &name)
LinearVariableChangeFactory Factory.
Base class for generic variable transform.
ChvarVec_::const_reverse_iterator ircst_
Abstract base class for model space error covariances.
Increment Class: Difference between two states.
virtual ModelSpaceCovarianceBase< MODEL > * make(const eckit::Configuration &conf, const Geometry_ &resol, const Variables &vars, const State_ &xb, const State_ &fg)
ModelSpaceCovarianceBase(const State_ &, const State_ &, const Geometry_ &, const eckit::Configuration &)
virtual void randomize(Increment_ &) const =0
void multiply(const Increment_ &, Increment_ &) const
boost::ptr_vector< LinearVariableChangeBase_ > ChvarVec_
virtual void doMultiply(const Increment_ &, Increment_ &) const =0
static std::map< std::string, CovarianceFactory< 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 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