[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" |
---|
[888] | 15 | |
---|
[977] | 16 | #include "reduction.hpp" |
---|
| 17 | |
---|
[888] | 18 | namespace xios { |
---|
[980] | 19 | CGenericAlgorithmTransformation* CScalarAlgorithmReduceAxis::create(CGrid* gridDst, CGrid* gridSrc, |
---|
[933] | 20 | CTransformation<CScalar>* transformation, |
---|
| 21 | int elementPositionInGrid, |
---|
| 22 | std::map<int, int>& elementPositionInGridSrc2ScalarPosition, |
---|
| 23 | std::map<int, int>& elementPositionInGridSrc2AxisPosition, |
---|
| 24 | std::map<int, int>& elementPositionInGridSrc2DomainPosition, |
---|
| 25 | std::map<int, int>& elementPositionInGridDst2ScalarPosition, |
---|
| 26 | std::map<int, int>& elementPositionInGridDst2AxisPosition, |
---|
| 27 | std::map<int, int>& elementPositionInGridDst2DomainPosition) |
---|
[1622] | 28 | TRY |
---|
[933] | 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 | } |
---|
[1622] | 39 | CATCH |
---|
[933] | 40 | |
---|
[980] | 41 | bool CScalarAlgorithmReduceAxis::registerTrans() |
---|
[1622] | 42 | TRY |
---|
[933] | 43 | { |
---|
[1852] | 44 | return CGridTransformationFactory<CScalar>::registerTransformation(TRANS_REDUCE_AXIS_TO_SCALAR, create); |
---|
[933] | 45 | } |
---|
[1622] | 46 | CATCH |
---|
[933] | 47 | |
---|
[980] | 48 | CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CScalar* scalarDestination, CAxis* axisSource, CReduceAxisToScalar* algo) |
---|
[888] | 49 | : CScalarAlgorithmTransformation(scalarDestination, axisSource), |
---|
| 50 | reduction_(0) |
---|
[1622] | 51 | TRY |
---|
[888] | 52 | { |
---|
| 53 | if (algo->operation.isEmpty()) |
---|
[980] | 54 | ERROR("CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToScalar* algo)", |
---|
[888] | 55 | << "Operation must be defined." |
---|
| 56 | << "Axis source " <<axisSource->getId() << std::endl |
---|
| 57 | << "Scalar destination " << scalarDestination->getId()); |
---|
[980] | 58 | StdString op; |
---|
| 59 | switch (algo->operation) |
---|
| 60 | { |
---|
| 61 | case CReduceAxisToScalar::operation_attr::sum: |
---|
| 62 | op = "sum"; |
---|
| 63 | break; |
---|
| 64 | case CReduceAxisToScalar::operation_attr::min: |
---|
| 65 | op = "min"; |
---|
| 66 | break; |
---|
| 67 | case CReduceAxisToScalar::operation_attr::max: |
---|
| 68 | op = "max"; |
---|
| 69 | break; |
---|
| 70 | case CReduceAxisToScalar::operation_attr::average: |
---|
| 71 | op = "average"; |
---|
| 72 | break; |
---|
| 73 | default: |
---|
| 74 | ERROR("CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CScalar* scalarDestination, CAxis* axisSource, CReduceAxisToScalar* algo)", |
---|
| 75 | << "Operation is wrongly defined. Supported operations: sum, min, max, average." << std::endl |
---|
| 76 | << "Domain source " <<axisSource->getId() << std::endl |
---|
| 77 | << "Scalar destination " << scalarDestination->getId()); |
---|
| 78 | |
---|
| 79 | } |
---|
| 80 | |
---|
[888] | 81 | if (CReductionAlgorithm::ReductionOperations.end() == CReductionAlgorithm::ReductionOperations.find(op)) |
---|
[980] | 82 | ERROR("CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToScalar* algo)", |
---|
[888] | 83 | << "Operation '" << op << "' not found. Please make sure to use a supported one" |
---|
| 84 | << "Axis source " <<axisSource->getId() << std::endl |
---|
| 85 | << "Scalar destination " << scalarDestination->getId()); |
---|
| 86 | |
---|
| 87 | reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations[op]); |
---|
| 88 | } |
---|
[1622] | 89 | CATCH |
---|
[888] | 90 | |
---|
[1260] | 91 | void CScalarAlgorithmReduceAxis::apply(const std::vector<std::pair<int,double> >& localIndex, const double* dataInput, CArray<double,1>& dataOut, |
---|
| 92 | std::vector<bool>& flagInitial, bool ignoreMissingValue, bool firstPass) |
---|
[1622] | 93 | TRY |
---|
[888] | 94 | { |
---|
[1260] | 95 | reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue, firstPass); |
---|
[888] | 96 | } |
---|
[1622] | 97 | CATCH |
---|
[888] | 98 | |
---|
[980] | 99 | void CScalarAlgorithmReduceAxis::updateData(CArray<double,1>& dataOut) |
---|
[1622] | 100 | TRY |
---|
[979] | 101 | { |
---|
| 102 | reduction_->updateData(dataOut); |
---|
| 103 | } |
---|
[1622] | 104 | CATCH |
---|
[979] | 105 | |
---|
[980] | 106 | CScalarAlgorithmReduceAxis::~CScalarAlgorithmReduceAxis() |
---|
[1622] | 107 | TRY |
---|
[888] | 108 | { |
---|
| 109 | if (0 != reduction_) delete reduction_; |
---|
| 110 | } |
---|
[1622] | 111 | CATCH |
---|
[888] | 112 | |
---|
[980] | 113 | void CScalarAlgorithmReduceAxis::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs) |
---|
[1622] | 114 | TRY |
---|
[888] | 115 | { |
---|
| 116 | this->transformationMapping_.resize(1); |
---|
| 117 | this->transformationWeight_.resize(1); |
---|
| 118 | |
---|
| 119 | TransformationIndexMap& transMap = this->transformationMapping_[0]; |
---|
| 120 | TransformationWeightMap& transWeight = this->transformationWeight_[0]; |
---|
| 121 | |
---|
[1297] | 122 | int globalIndexSize = axisSrc_-> n_glo; |
---|
[888] | 123 | |
---|
| 124 | for (int idx = 0; idx < globalIndexSize; ++idx) |
---|
| 125 | { |
---|
[1297] | 126 | transMap[0].push_back(idx); |
---|
[888] | 127 | transWeight[0].push_back(1.0); |
---|
| 128 | } |
---|
| 129 | } |
---|
[1622] | 130 | CATCH |
---|
[888] | 131 | |
---|
| 132 | } |
---|