FV3 Bundle
WeightedDiff.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_WEIGHTEDDIFF_H_
12 #define OOPS_BASE_WEIGHTEDDIFF_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 #include "oops/util/Logger.h"
27 #include "oops/util/printStackTrace.h"
28 
29 namespace oops {
30 
31 // -----------------------------------------------------------------------------
32 
33 /// Compute time average of states or increments during model run.
34 /*!
35  * Derived classes will compute different types of averages (plain
36  * mean, various types of digital filters) by overwriting the weights
37  * computation method.
38  */
39 
40 template <typename MODEL, typename INCR, typename FLDS>
41 class WeightedDiff : public PostBase<FLDS> {
43 
44  public:
45  WeightedDiff(const eckit::Configuration &, const util::DateTime &, const util::Duration &,
46  const util::Duration &, const Geometry_ &, WeightingFct &);
47  virtual ~WeightedDiff() {}
48 
49  INCR * releaseDiff();
50 
51  private:
52  void doInitialize(const FLDS &, const util::DateTime &, const util::Duration &) override;
53  void doProcessing(const FLDS &) override;
54 
56  std::map< util::DateTime, double > weights_;
58  double sum_;
59  bool linit_;
60  const util::DateTime vtime_;
61  const util::DateTime bgn_;
62  const util::DateTime end_;
63  util::Duration tstep_;
64  util::DateTime bgnleg_;
65  util::DateTime endleg_;
66  util::DateTime current_;
67 };
68 
69 // -----------------------------------------------------------------------------
70 
71 template <typename MODEL, typename INCR, typename FLDS>
72 WeightedDiff<MODEL, INCR, FLDS>::WeightedDiff(const eckit::Configuration & config,
73  const util::DateTime & vt,
74  const util::Duration & span,
75  const util::Duration & tstep,
76  const Geometry_ & resol,
77  WeightingFct & wfct)
78  : PostBase<FLDS>(vt-span/2, vt+span/2),
79  wfct_(wfct), weights_(), avg_(0), sum_(0.0), linit_(false),
80  vtime_(vt), bgn_(vt-span/2), end_(vt+span/2), tstep_(tstep),
81  bgnleg_(), endleg_(), current_()
82 {
83  const Variables vars(config);
84  avg_ = new Accumulator<MODEL, INCR, FLDS>(resol, vars, vtime_);
85 }
86 
87 // -----------------------------------------------------------------------------
88 
89 template <typename MODEL, typename INCR, typename FLDS>
91  Log::debug() << "WeightedDiff: release sum = " << sum_
92  << ", bgnleg_ = " << bgnleg_ << ", endleg_ = " << endleg_
93  << ", bgn_ = " << bgn_ << ", end_ = " << end_ << std::endl;
94  ASSERT(linit_);
95  ASSERT(std::abs(sum_) < 1.0e-8);
96  return avg_;
97 }
98 
99 // -----------------------------------------------------------------------------
100 
101 template <typename MODEL, typename INCR, typename FLDS>
103  const util::DateTime & end,
104  const util::Duration & tstep) {
105  const util::DateTime bgn(xx.validTime());
106  if (!linit_ && bgn <= end_ && end >= bgn_) {
107  if (tstep_ == util::Duration(0)) tstep_ = tstep;
108  ASSERT(tstep_ > util::Duration(0));
109  weights_ = wfct_.setWeights(bgn_, end_, tstep_);
110  linit_ = true;
111  ASSERT(weights_.find(vtime_) != weights_.end());
112  weights_[vtime_] -= 1.0;
113  }
114  bgnleg_ = bgn;
115  endleg_ = end;
116  current_ = bgn-tstep;
117  Log::debug() << "WeightedDiff: initialized"
118  << " bgnleg_ = " << bgnleg_ << ", endleg_ = " << endleg_
119  << ", bgn_ = " << bgn_ << ", end_ = " << end_ << std::endl;
120 }
121 
122 // -----------------------------------------------------------------------------
123 
124 template <typename MODEL, typename INCR, typename FLDS>
126  const util::DateTime now(xx.validTime());
127  printStackTrace();
128  ASSERT(now > current_);
129  if (((bgnleg_ < end_ && endleg_ > bgn_) || bgnleg_ == endleg_) &&
130  (now != endleg_ || now == end_ || now == bgnleg_)) {
131  ASSERT(weights_.find(now) != weights_.end());
132  const double zz = weights_[now];
133  avg_->accumul(zz, xx);
134  sum_ += zz;
135  Log::debug() << "WeightedDiff: time = " << now
136  << ", weight = " << zz << ", sum = " << sum_ << std::endl;
137  }
138  current_ = now;
139 }
140 
141 // -----------------------------------------------------------------------------
142 
143 } // namespace oops
144 
145 #endif // OOPS_BASE_WEIGHTEDDIFF_H_
void doProcessing(const FLDS &) override
Actual processing.
Definition: WeightedDiff.h:125
util::DateTime bgnleg_
Definition: WeightedDiff.h:64
const util::DateTime bgn_
Definition: WeightedDiff.h:61
void doInitialize(const FLDS &, const util::DateTime &, const util::Duration &) override
Definition: WeightedDiff.h:102
Compute time average of states or increments during model run.
Definition: WeightedDiff.h:41
************************************************************************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
integer(long), parameter false
util::DateTime endleg_
Definition: WeightedDiff.h:65
Handles post-processing of model fields.
Definition: PostBase.h:33
The namespace for the main oops code.
real(fp), parameter, public e
logical debug
Definition: mpp.F90:1297
real, dimension(:,:,:), allocatable vt
virtual ~WeightedDiff()
Definition: WeightedDiff.h:47
const util::DateTime end_
Definition: WeightedDiff.h:62
INCR * releaseDiff()
Definition: WeightedDiff.h:90
util::DateTime current_
Definition: WeightedDiff.h:66
std::map< util::DateTime, double > weights_
Definition: WeightedDiff.h:56
util::Duration tstep_
Definition: WeightedDiff.h:63
Geometry< MODEL > Geometry_
Definition: WeightedDiff.h:42
enddo ! cludge for now
WeightingFct & wfct_
Definition: WeightedDiff.h:55
Accumulator< MODEL, INCR, FLDS > * avg_
Definition: WeightedDiff.h:57
WeightedDiff(const eckit::Configuration &, const util::DateTime &, const util::Duration &, const util::Duration &, const Geometry_ &, WeightingFct &)
Definition: WeightedDiff.h:72
Weighting Function.
Definition: WeightingFct.h:36
const util::DateTime vtime_
Definition: WeightedDiff.h:60