[1301] | 1 | /*! |
---|
| 2 | \file axis_algorithm_reduce_axis.cpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \since 23 June 2016 |
---|
| 5 | \date 23 June 2016 |
---|
| 6 | |
---|
| 7 | \brief Algorithm for reduce a axis to an axis |
---|
| 8 | */ |
---|
| 9 | #include "axis_algorithm_reduce_axis.hpp" |
---|
| 10 | #include "reduce_axis_to_axis.hpp" |
---|
| 11 | #include "axis.hpp" |
---|
| 12 | #include "grid.hpp" |
---|
| 13 | #include "grid_transformation_factory_impl.hpp" |
---|
| 14 | #include "reduction.hpp" |
---|
| 15 | |
---|
| 16 | namespace xios { |
---|
| 17 | CGenericAlgorithmTransformation* CAxisAlgorithmReduceAxis::create(CGrid* gridDst, CGrid* gridSrc, |
---|
| 18 | CTransformation<CAxis>* transformation, |
---|
| 19 | int elementPositionInGrid, |
---|
| 20 | std::map<int, int>& elementPositionInGridSrc2ScalarPosition, |
---|
| 21 | std::map<int, int>& elementPositionInGridSrc2AxisPosition, |
---|
| 22 | std::map<int, int>& elementPositionInGridSrc2DomainPosition, |
---|
| 23 | std::map<int, int>& elementPositionInGridDst2ScalarPosition, |
---|
| 24 | std::map<int, int>& elementPositionInGridDst2AxisPosition, |
---|
| 25 | std::map<int, int>& elementPositionInGridDst2DomainPosition) |
---|
[1622] | 26 | TRY |
---|
[1301] | 27 | { |
---|
| 28 | std::vector<CAxis*> axisListDestP = gridDst->getAxis(); |
---|
| 29 | std::vector<CAxis*> axisListSrcP = gridSrc->getAxis(); |
---|
| 30 | |
---|
| 31 | CReduceAxisToAxis* reduceAxis = dynamic_cast<CReduceAxisToAxis*> (transformation); |
---|
| 32 | int axisDstIndex = elementPositionInGridDst2AxisPosition[elementPositionInGrid]; |
---|
| 33 | int axisSrcIndex = elementPositionInGridSrc2AxisPosition[elementPositionInGrid]; |
---|
| 34 | |
---|
| 35 | return (new CAxisAlgorithmReduceAxis(axisListDestP[axisDstIndex], axisListSrcP[axisSrcIndex], reduceAxis)); |
---|
| 36 | } |
---|
[1622] | 37 | CATCH |
---|
[1301] | 38 | |
---|
| 39 | bool CAxisAlgorithmReduceAxis::registerTrans() |
---|
[1622] | 40 | TRY |
---|
[1301] | 41 | { |
---|
| 42 | CGridTransformationFactory<CAxis>::registerTransformation(TRANS_REDUCE_AXIS_TO_AXIS, create); |
---|
| 43 | } |
---|
[1622] | 44 | CATCH |
---|
[1301] | 45 | |
---|
| 46 | |
---|
| 47 | CAxisAlgorithmReduceAxis::CAxisAlgorithmReduceAxis(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToAxis* algo) |
---|
| 48 | : CAxisAlgorithmTransformation(axisDestination, axisSource), reduction_(0) |
---|
[1622] | 49 | TRY |
---|
[1301] | 50 | { |
---|
| 51 | eliminateRedondantSrc_= false ; |
---|
| 52 | algo->checkValid(axisDestination, axisSource); |
---|
| 53 | StdString op; |
---|
| 54 | switch (algo->operation) |
---|
| 55 | { |
---|
| 56 | case CReduceAxisToAxis::operation_attr::sum: |
---|
| 57 | op = "sum"; |
---|
| 58 | break; |
---|
| 59 | case CReduceAxisToAxis::operation_attr::min: |
---|
| 60 | op = "min"; |
---|
| 61 | break; |
---|
| 62 | case CReduceAxisToAxis::operation_attr::max: |
---|
| 63 | op = "max"; |
---|
| 64 | break; |
---|
| 65 | case CReduceAxisToAxis::operation_attr::average: |
---|
| 66 | op = "average"; |
---|
| 67 | break; |
---|
| 68 | default: |
---|
| 69 | ERROR("CAxisAlgorithmReduceAxis::CAxisAlgorithmReduceAxis(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToAxis* algo)", |
---|
| 70 | << "Operation is wrongly defined. Supported operations: sum, min, max, average." << std::endl |
---|
| 71 | << "Axis source " <<axisSource->getId() << std::endl |
---|
| 72 | << "Axis destination " << axisDestination->getId()); |
---|
| 73 | |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations[op]); |
---|
| 77 | } |
---|
[1622] | 78 | CATCH |
---|
[1301] | 79 | |
---|
| 80 | void CAxisAlgorithmReduceAxis::apply(const std::vector<std::pair<int,double> >& localIndex, |
---|
| 81 | const double* dataInput, |
---|
| 82 | CArray<double,1>& dataOut, |
---|
| 83 | std::vector<bool>& flagInitial, |
---|
| 84 | bool ignoreMissingValue, bool firstPass) |
---|
[1622] | 85 | TRY |
---|
[1301] | 86 | { |
---|
| 87 | reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue, firstPass); |
---|
| 88 | } |
---|
[1622] | 89 | CATCH |
---|
[1301] | 90 | |
---|
| 91 | void CAxisAlgorithmReduceAxis::updateData(CArray<double,1>& dataOut) |
---|
[1622] | 92 | TRY |
---|
[1301] | 93 | { |
---|
| 94 | reduction_->updateData(dataOut); |
---|
| 95 | } |
---|
[1622] | 96 | CATCH |
---|
[1301] | 97 | |
---|
| 98 | CAxisAlgorithmReduceAxis::~CAxisAlgorithmReduceAxis() |
---|
[1622] | 99 | TRY |
---|
[1301] | 100 | { |
---|
| 101 | if (0 != reduction_) delete reduction_; |
---|
| 102 | } |
---|
[1622] | 103 | CATCH |
---|
[1301] | 104 | |
---|
| 105 | void CAxisAlgorithmReduceAxis::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs) |
---|
[1622] | 106 | TRY |
---|
[1301] | 107 | { |
---|
| 108 | this->transformationMapping_.resize(1); |
---|
| 109 | this->transformationWeight_.resize(1); |
---|
| 110 | |
---|
| 111 | TransformationIndexMap& transMap = this->transformationMapping_[0]; |
---|
| 112 | TransformationWeightMap& transWeight = this->transformationWeight_[0]; |
---|
| 113 | CArray<int,1>& axisDstIndex = axisDest_->index; |
---|
| 114 | int nbAxisIdx = axisDstIndex.numElements(); |
---|
| 115 | for (int idxAxis = 0; idxAxis < nbAxisIdx; ++idxAxis) |
---|
| 116 | { |
---|
| 117 | int globalAxisIdx = axisDstIndex(idxAxis); |
---|
| 118 | transMap[globalAxisIdx].resize(1); |
---|
| 119 | transWeight[globalAxisIdx].resize(1); |
---|
| 120 | transMap[globalAxisIdx][0]=globalAxisIdx ; |
---|
| 121 | transWeight[globalAxisIdx][0] = 1.0 ; |
---|
| 122 | } |
---|
| 123 | } |
---|
[1622] | 124 | CATCH |
---|
[1301] | 125 | |
---|
| 126 | } |
---|