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