[979] | 1 | /*! |
---|
| 2 | \file average.cpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \since 8 Sep 2016 |
---|
[1158] | 5 | \date 9 Jan 2017 |
---|
[979] | 6 | |
---|
| 7 | \brief average reduction |
---|
| 8 | */ |
---|
| 9 | #include "average_reduction.hpp" |
---|
[1158] | 10 | #include "utils.hpp" |
---|
[979] | 11 | |
---|
| 12 | namespace xios { |
---|
| 13 | |
---|
| 14 | CAverageReductionAlgorithm::CAverageReductionAlgorithm() |
---|
| 15 | : CReductionAlgorithm(), resetWeight_(true) |
---|
| 16 | { |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | CReductionAlgorithm* CAverageReductionAlgorithm::create() |
---|
| 20 | { |
---|
| 21 | return (new CAverageReductionAlgorithm()); |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | bool CAverageReductionAlgorithm::registerTrans() |
---|
| 25 | { |
---|
| 26 | return registerOperation(TRANS_REDUCE_AVERAGE, CAverageReductionAlgorithm::create); |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | void CAverageReductionAlgorithm::apply(const std::vector<std::pair<int,double> >& localIndex, |
---|
| 30 | const double* dataInput, |
---|
| 31 | CArray<double,1>& dataOut, |
---|
[1158] | 32 | std::vector<bool>& flagInitial, |
---|
[1260] | 33 | bool ignoreMissingValue, bool firstPass) |
---|
[979] | 34 | { |
---|
[1158] | 35 | if (resetWeight_) { weights_.resize(flagInitial.size()); weights_ = 1.0; resetWeight_ = false; } |
---|
[979] | 36 | |
---|
[1158] | 37 | if (ignoreMissingValue) |
---|
[979] | 38 | { |
---|
[1158] | 39 | int nbLocalIndex = localIndex.size(); |
---|
| 40 | int currentlocalIndex = 0; |
---|
| 41 | double currentWeight = 0.0; |
---|
[979] | 42 | |
---|
[1260] | 43 | if (firstPass) dataOut=std::numeric_limits<double>::quiet_NaN(); |
---|
[1158] | 44 | |
---|
| 45 | for (int idx = 0; idx < nbLocalIndex; ++idx) |
---|
[979] | 46 | { |
---|
[1158] | 47 | currentlocalIndex = localIndex[idx].first; |
---|
| 48 | currentWeight = localIndex[idx].second; |
---|
[1474] | 49 | if (!NumTraits<double>::isNan(*(dataInput + idx))) |
---|
[1158] | 50 | { |
---|
| 51 | if (flagInitial[currentlocalIndex]) |
---|
| 52 | { |
---|
| 53 | dataOut(currentlocalIndex) = *(dataInput + idx); |
---|
| 54 | flagInitial[currentlocalIndex] = false; |
---|
| 55 | } |
---|
| 56 | else |
---|
| 57 | { |
---|
| 58 | dataOut(currentlocalIndex) += *(dataInput + idx); |
---|
| 59 | weights_(currentlocalIndex) += 1.0; |
---|
| 60 | } |
---|
| 61 | } |
---|
[979] | 62 | } |
---|
[1158] | 63 | } |
---|
| 64 | else |
---|
| 65 | { |
---|
| 66 | int nbLocalIndex = localIndex.size(); |
---|
| 67 | int currentlocalIndex = 0; |
---|
| 68 | double currentWeight = 0.0; |
---|
| 69 | for (int idx = 0; idx < nbLocalIndex; ++idx) |
---|
[979] | 70 | { |
---|
[1158] | 71 | currentlocalIndex = localIndex[idx].first; |
---|
| 72 | currentWeight = localIndex[idx].second; |
---|
| 73 | |
---|
| 74 | if (flagInitial[currentlocalIndex]) |
---|
| 75 | { |
---|
| 76 | dataOut(currentlocalIndex) = *(dataInput + idx); |
---|
| 77 | flagInitial[currentlocalIndex] = false; |
---|
| 78 | } |
---|
| 79 | else |
---|
| 80 | { |
---|
| 81 | dataOut(currentlocalIndex) += *(dataInput + idx); |
---|
| 82 | weights_(currentlocalIndex) += 1.0; |
---|
| 83 | } |
---|
[979] | 84 | } |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | void CAverageReductionAlgorithm::updateData(CArray<double,1>& dataOut) |
---|
| 89 | { |
---|
| 90 | dataOut /= weights_; |
---|
| 91 | resetWeight_ = true; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | } |
---|