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