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