FV3 Bundle
WeightedMean.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_WEIGHTEDMEAN_H_
12 #define OOPS_BASE_WEIGHTEDMEAN_H_
13 
14 #include <cmath>
15 #include <map>
16 #include <boost/scoped_ptr.hpp>
17 
18 #include "oops/base/Accumulator.h"
20 #include "oops/base/PostBase.h"
21 #include "oops/base/Variables.h"
22 #include "oops/base/WeightingFct.h"
24 #include "oops/util/DateTime.h"
25 #include "oops/util/Duration.h"
26 
27 namespace oops {
28 
29 // -----------------------------------------------------------------------------
30 
31 /// Compute time average of states or increments during model run.
32 /*!
33  * Derived classes will compute different types of averages (plain
34  * mean, various types of digital filters) by overwriting the weights
35  * computation method.
36  */
37 
38 template <typename MODEL, typename FLDS>
39 class WeightedMean : public PostBase<FLDS> {
41 
42  public:
43  WeightedMean(const util::DateTime &, const util::Duration &,
44  const Geometry_ &, const eckit::Configuration &);
45  virtual ~WeightedMean() {}
46 
47  FLDS * releaseMean();
48 
49  private:
50  void doInitialize(const FLDS &, const util::DateTime &, const util::Duration &) override;
51 
52  void doProcessing(const FLDS &) override;
53 
54  boost::scoped_ptr<WeightingFct> wfct_;
55  std::map< util::DateTime, double > weights_;
56 // std::unique_ptr< Accumulator<MODEL, FLDS, FLDS> > avg_;
58  double sum_;
59  bool linit_;
60  const util::DateTime bgn_;
61  const util::DateTime end_;
62  util::DateTime endleg_;
63 };
64 
65 // =============================================================================
66 
67 template <typename MODEL, typename FLDS>
69  const util::Duration & span,
70  const Geometry_ & resol,
71  const eckit::Configuration & config)
72  : PostBase<FLDS>(vt-span/2, vt+span/2),
73  wfct_(), weights_(), avg_(0), sum_(0.0), linit_(false),
74  bgn_(vt-span/2), end_(vt+span/2), endleg_()
75 {
76  const Variables vars(config);
78 // wfct_.reset(WeightFactory::create(config)); YT
79  wfct_.reset(new DolphChebyshev(config));
80 }
81 
82 // -----------------------------------------------------------------------------
83 
84 template <typename MODEL, typename FLDS>
86  ASSERT(linit_);
87  ASSERT(std::abs(sum_ - 1.0) < 1.0e-8);
88  return avg_;
89 }
90 
91 // -----------------------------------------------------------------------------
92 
93 template <typename MODEL, typename FLDS>
95  const util::DateTime & end,
96  const util::Duration & tstep) {
97  const util::DateTime bgn(xx.validTime());
98  if (!linit_ && bgn <= end_ && end >= bgn_) {
99  weights_ = wfct_->setWeights(bgn_, end_, tstep);
100  linit_ = true;
101  }
102  endleg_ = end;
103 }
104 
105 // -----------------------------------------------------------------------------
106 
107 template <typename MODEL, typename FLDS>
109  const util::DateTime now(xx.validTime());
110  if (now != endleg_ || now == end_) {
111  ASSERT(weights_.find(now) != weights_.end());
112  const double zz = weights_[now];
113  avg_->accumul(zz, xx);
114  sum_ += zz;
115  Log::debug() << "WeightedMean: time = " << now
116  << ", weight = " << zz << ", sum = " << sum_ << std::endl;
117  }
118 }
119 
120 // -----------------------------------------------------------------------------
121 
122 } // namespace oops
123 
124 #endif // OOPS_BASE_WEIGHTEDMEAN_H_
util::DateTime endleg_
Definition: WeightedMean.h:62
virtual ~WeightedMean()
Definition: WeightedMean.h:45
const util::DateTime end_
Definition: WeightedMean.h:61
************************************************************************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
const util::DateTime bgn_
Definition: WeightedMean.h:60
integer(long), parameter false
Handles post-processing of model fields.
Definition: PostBase.h:33
The namespace for the main oops code.
boost::scoped_ptr< WeightingFct > wfct_
Definition: WeightedMean.h:54
real(fp), parameter, public e
logical debug
Definition: mpp.F90:1297
real, dimension(:,:,:), allocatable vt
Accumulator< MODEL, FLDS, FLDS > * avg_
Definition: WeightedMean.h:57
enddo ! cludge for now
Geometry< MODEL > Geometry_
Definition: WeightedMean.h:40
void doProcessing(const FLDS &) override
Actual processing.
Definition: WeightedMean.h:108
Compute time average of states or increments during model run.
Definition: WeightedMean.h:39
FLDS * releaseMean()
Definition: WeightedMean.h:85
std::map< util::DateTime, double > weights_
Definition: WeightedMean.h:55
WeightedMean(const util::DateTime &, const util::Duration &, const Geometry_ &, const eckit::Configuration &)
Definition: WeightedMean.h:68
void doInitialize(const FLDS &, const util::DateTime &, const util::Duration &) override
Definition: WeightedMean.h:94