[888] | 1 | /*! |
---|
| 2 | \file scalar_algorithm_reduce_scalar.cpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \since 23 June 2016 |
---|
| 5 | \date 23 June 2016 |
---|
| 6 | |
---|
| 7 | \brief Algorithm for reduce an axis to a scalar |
---|
| 8 | */ |
---|
| 9 | #include "scalar_algorithm_reduce_axis.hpp" |
---|
| 10 | #include "axis.hpp" |
---|
| 11 | #include "scalar.hpp" |
---|
| 12 | #include "reduce_axis_to_scalar.hpp" |
---|
[933] | 13 | #include "grid.hpp" |
---|
| 14 | #include "grid_transformation_factory_impl.hpp" |
---|
[979] | 15 | #include "reduction.hpp" |
---|
[888] | 16 | |
---|
[977] | 17 | #include "reduction.hpp" |
---|
| 18 | |
---|
[888] | 19 | namespace xios { |
---|
[980] | 20 | CGenericAlgorithmTransformation* CScalarAlgorithmReduceAxis::create(CGrid* gridDst, CGrid* gridSrc, |
---|
[933] | 21 | CTransformation<CScalar>* transformation, |
---|
| 22 | int elementPositionInGrid, |
---|
| 23 | std::map<int, int>& elementPositionInGridSrc2ScalarPosition, |
---|
| 24 | std::map<int, int>& elementPositionInGridSrc2AxisPosition, |
---|
| 25 | std::map<int, int>& elementPositionInGridSrc2DomainPosition, |
---|
| 26 | std::map<int, int>& elementPositionInGridDst2ScalarPosition, |
---|
| 27 | std::map<int, int>& elementPositionInGridDst2AxisPosition, |
---|
| 28 | std::map<int, int>& elementPositionInGridDst2DomainPosition) |
---|
| 29 | { |
---|
| 30 | std::vector<CScalar*> scalarListDestP = gridDst->getScalars(); |
---|
| 31 | std::vector<CAxis*> axisListSrcP = gridSrc->getAxis(); |
---|
[888] | 32 | |
---|
[933] | 33 | CReduceAxisToScalar* reduceAxis = dynamic_cast<CReduceAxisToScalar*> (transformation); |
---|
| 34 | int scalarDstIndex = elementPositionInGridDst2ScalarPosition[elementPositionInGrid]; |
---|
| 35 | int axisSrcIndex = elementPositionInGridSrc2AxisPosition[elementPositionInGrid]; |
---|
| 36 | |
---|
[980] | 37 | return (new CScalarAlgorithmReduceAxis(scalarListDestP[scalarDstIndex], axisListSrcP[axisSrcIndex], reduceAxis)); |
---|
[933] | 38 | } |
---|
| 39 | |
---|
[980] | 40 | bool CScalarAlgorithmReduceAxis::registerTrans() |
---|
[933] | 41 | { |
---|
[1850] | 42 | return CGridTransformationFactory<CScalar>::registerTransformation(TRANS_REDUCE_AXIS_TO_SCALAR, create); |
---|
[933] | 43 | } |
---|
| 44 | |
---|
[980] | 45 | CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CScalar* scalarDestination, CAxis* axisSource, CReduceAxisToScalar* algo) |
---|
[888] | 46 | : CScalarAlgorithmTransformation(scalarDestination, axisSource), |
---|
| 47 | reduction_(0) |
---|
| 48 | { |
---|
| 49 | if (algo->operation.isEmpty()) |
---|
[980] | 50 | ERROR("CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToScalar* algo)", |
---|
[888] | 51 | << "Operation must be defined." |
---|
| 52 | << "Axis source " <<axisSource->getId() << std::endl |
---|
| 53 | << "Scalar destination " << scalarDestination->getId()); |
---|
[980] | 54 | StdString op; |
---|
| 55 | switch (algo->operation) |
---|
| 56 | { |
---|
| 57 | case CReduceAxisToScalar::operation_attr::sum: |
---|
| 58 | op = "sum"; |
---|
| 59 | break; |
---|
| 60 | case CReduceAxisToScalar::operation_attr::min: |
---|
| 61 | op = "min"; |
---|
| 62 | break; |
---|
| 63 | case CReduceAxisToScalar::operation_attr::max: |
---|
| 64 | op = "max"; |
---|
| 65 | break; |
---|
| 66 | case CReduceAxisToScalar::operation_attr::average: |
---|
| 67 | op = "average"; |
---|
| 68 | break; |
---|
| 69 | default: |
---|
| 70 | ERROR("CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CScalar* scalarDestination, CAxis* axisSource, CReduceAxisToScalar* algo)", |
---|
| 71 | << "Operation is wrongly defined. Supported operations: sum, min, max, average." << std::endl |
---|
| 72 | << "Domain source " <<axisSource->getId() << std::endl |
---|
| 73 | << "Scalar destination " << scalarDestination->getId()); |
---|
| 74 | |
---|
| 75 | } |
---|
| 76 | |
---|
[888] | 77 | if (CReductionAlgorithm::ReductionOperations.end() == CReductionAlgorithm::ReductionOperations.find(op)) |
---|
[980] | 78 | ERROR("CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToScalar* algo)", |
---|
[888] | 79 | << "Operation '" << op << "' not found. Please make sure to use a supported one" |
---|
| 80 | << "Axis source " <<axisSource->getId() << std::endl |
---|
| 81 | << "Scalar destination " << scalarDestination->getId()); |
---|
| 82 | |
---|
| 83 | reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations[op]); |
---|
| 84 | } |
---|
| 85 | |
---|
[1260] | 86 | void CScalarAlgorithmReduceAxis::apply(const std::vector<std::pair<int,double> >& localIndex, const double* dataInput, CArray<double,1>& dataOut, |
---|
| 87 | std::vector<bool>& flagInitial, bool ignoreMissingValue, bool firstPass) |
---|
[888] | 88 | { |
---|
[1260] | 89 | reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue, firstPass); |
---|
[888] | 90 | } |
---|
| 91 | |
---|
[980] | 92 | void CScalarAlgorithmReduceAxis::updateData(CArray<double,1>& dataOut) |
---|
[979] | 93 | { |
---|
| 94 | reduction_->updateData(dataOut); |
---|
| 95 | } |
---|
| 96 | |
---|
[980] | 97 | CScalarAlgorithmReduceAxis::~CScalarAlgorithmReduceAxis() |
---|
[888] | 98 | { |
---|
| 99 | if (0 != reduction_) delete reduction_; |
---|
| 100 | } |
---|
| 101 | |
---|
[980] | 102 | void CScalarAlgorithmReduceAxis::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs) |
---|
[888] | 103 | { |
---|
| 104 | this->transformationMapping_.resize(1); |
---|
| 105 | this->transformationWeight_.resize(1); |
---|
| 106 | |
---|
| 107 | TransformationIndexMap& transMap = this->transformationMapping_[0]; |
---|
| 108 | TransformationWeightMap& transWeight = this->transformationWeight_[0]; |
---|
| 109 | |
---|
[1297] | 110 | int globalIndexSize = axisSrc_-> n_glo; |
---|
[888] | 111 | |
---|
| 112 | for (int idx = 0; idx < globalIndexSize; ++idx) |
---|
| 113 | { |
---|
[1297] | 114 | transMap[0].push_back(idx); |
---|
[888] | 115 | transWeight[0].push_back(1.0); |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | } |
---|