[979] | 1 | /*! |
---|
| 2 | \file average.cpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \since 8 Sep 2016 |
---|
[1018] | 5 | \date 9 Jan 2017 |
---|
[979] | 6 | |
---|
| 7 | \brief average reduction |
---|
| 8 | */ |
---|
| 9 | #include "average_reduction.hpp" |
---|
[1018] | 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, |
---|
[1076] | 32 | std::vector<bool>& flagInitial, |
---|
| 33 | bool ignoreMissingValue) |
---|
[979] | 34 | { |
---|
[1076] | 35 | if (resetWeight_) { weights_.resize(flagInitial.size()); weights_ = 1.0; resetWeight_ = false; } |
---|
[979] | 36 | |
---|
[1076] | 37 | if (ignoreMissingValue) |
---|
[979] | 38 | { |
---|
[1018] | 39 | int nbLocalIndex = localIndex.size(); |
---|
| 40 | int currentlocalIndex = 0; |
---|
[1156] | 41 | double currentWeight = 0.0; |
---|
[1043] | 42 | |
---|
[1018] | 43 | for (int idx = 0; idx < nbLocalIndex; ++idx) |
---|
[979] | 44 | { |
---|
[1018] | 45 | currentlocalIndex = localIndex[idx].first; |
---|
| 46 | currentWeight = localIndex[idx].second; |
---|
| 47 | if (!NumTraits<double>::isnan(*(dataInput + idx))) |
---|
| 48 | { |
---|
| 49 | if (flagInitial[currentlocalIndex]) |
---|
| 50 | { |
---|
| 51 | dataOut(currentlocalIndex) = *(dataInput + idx); |
---|
| 52 | flagInitial[currentlocalIndex] = false; |
---|
| 53 | } |
---|
| 54 | else |
---|
| 55 | { |
---|
| 56 | dataOut(currentlocalIndex) += *(dataInput + idx); |
---|
| 57 | weights_(currentlocalIndex) += 1.0; |
---|
| 58 | } |
---|
| 59 | } |
---|
[1156] | 60 | else |
---|
| 61 | { |
---|
| 62 | if (flagInitial[currentlocalIndex]) |
---|
| 63 | dataOut(currentlocalIndex) = std::numeric_limits<double>::quiet_NaN(); |
---|
| 64 | } |
---|
[979] | 65 | } |
---|
[1018] | 66 | } |
---|
| 67 | else |
---|
| 68 | { |
---|
| 69 | int nbLocalIndex = localIndex.size(); |
---|
| 70 | int currentlocalIndex = 0; |
---|
| 71 | double currentWeight = 0.0; |
---|
| 72 | for (int idx = 0; idx < nbLocalIndex; ++idx) |
---|
[979] | 73 | { |
---|
[1018] | 74 | currentlocalIndex = localIndex[idx].first; |
---|
| 75 | currentWeight = localIndex[idx].second; |
---|
| 76 | |
---|
| 77 | if (flagInitial[currentlocalIndex]) |
---|
| 78 | { |
---|
| 79 | dataOut(currentlocalIndex) = *(dataInput + idx); |
---|
| 80 | flagInitial[currentlocalIndex] = false; |
---|
| 81 | } |
---|
| 82 | else |
---|
| 83 | { |
---|
| 84 | dataOut(currentlocalIndex) += *(dataInput + idx); |
---|
| 85 | weights_(currentlocalIndex) += 1.0; |
---|
| 86 | } |
---|
[979] | 87 | } |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | void CAverageReductionAlgorithm::updateData(CArray<double,1>& dataOut) |
---|
| 92 | { |
---|
| 93 | dataOut /= weights_; |
---|
| 94 | resetWeight_ = true; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | } |
---|