[976] | 1 | /*! |
---|
| 2 | \file scalar_algorithm_reduce_domain.cpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \since 23 June 2016 |
---|
| 5 | \date 23 June 2016 |
---|
| 6 | |
---|
| 7 | \brief Algorithm for reduce a domain to a scalar |
---|
| 8 | */ |
---|
| 9 | #include "scalar_algorithm_reduce_domain.hpp" |
---|
| 10 | #include "domain.hpp" |
---|
| 11 | #include "scalar.hpp" |
---|
| 12 | #include "reduce_domain_to_scalar.hpp" |
---|
| 13 | #include "grid.hpp" |
---|
| 14 | #include "grid_transformation_factory_impl.hpp" |
---|
| 15 | |
---|
| 16 | #include "reduction.hpp" |
---|
| 17 | |
---|
| 18 | namespace xios { |
---|
| 19 | CGenericAlgorithmTransformation* CScalarAlgorithmReduceDomain::create(CGrid* gridDst, CGrid* gridSrc, |
---|
| 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) |
---|
| 28 | { |
---|
| 29 | std::vector<CScalar*> scalarListDestP = gridDst->getScalars(); |
---|
| 30 | std::vector<CDomain*> domainListSrcP = gridSrc->getDomains(); |
---|
| 31 | |
---|
| 32 | CReduceDomainToScalar* reduceDomain = dynamic_cast<CReduceDomainToScalar*> (transformation); |
---|
| 33 | int scalarDstIndex = elementPositionInGridDst2ScalarPosition[elementPositionInGrid]; |
---|
| 34 | int domainSrcIndex = elementPositionInGridSrc2DomainPosition[elementPositionInGrid]; |
---|
| 35 | |
---|
| 36 | return (new CScalarAlgorithmReduceDomain(scalarListDestP[scalarDstIndex], domainListSrcP[domainSrcIndex], reduceDomain)); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | bool CScalarAlgorithmReduceDomain::registerTrans() |
---|
| 40 | { |
---|
| 41 | CGridTransformationFactory<CScalar>::registerTransformation(TRANS_REDUCE_DOMAIN_TO_SCALAR, create); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | CScalarAlgorithmReduceDomain::CScalarAlgorithmReduceDomain(CScalar* scalarDestination, CDomain* domainSource, CReduceDomainToScalar* algo) |
---|
| 45 | : CScalarAlgorithmTransformation(scalarDestination, domainSource), |
---|
| 46 | reduction_(0) |
---|
| 47 | { |
---|
| 48 | StdString op; |
---|
| 49 | switch (algo->operation) |
---|
| 50 | { |
---|
| 51 | case CReduceDomainToScalar::operation_attr::sum: |
---|
| 52 | op = "sum"; |
---|
| 53 | break; |
---|
| 54 | case CReduceDomainToScalar::operation_attr::min: |
---|
| 55 | op = "min"; |
---|
| 56 | break; |
---|
| 57 | case CReduceDomainToScalar::operation_attr::max: |
---|
| 58 | op = "max"; |
---|
| 59 | break; |
---|
| 60 | case CReduceDomainToScalar::operation_attr::average: |
---|
| 61 | op = "average"; |
---|
| 62 | break; |
---|
| 63 | default: |
---|
| 64 | ERROR("CScalarAlgorithmReduceDomain::CScalarAlgorithmReduceDomain(CDomain* domainDestination, CDomain* domainSource, CReduceDomainToScalar* algo)", |
---|
| 65 | << "Operation must be defined." |
---|
| 66 | << "Domain source " <<domainSource->getId() << std::endl |
---|
| 67 | << "Scalar destination " << scalarDestination->getId()); |
---|
| 68 | |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | if (CReductionAlgorithm::ReductionOperations.end() == CReductionAlgorithm::ReductionOperations.find(op)) |
---|
| 72 | ERROR("CScalarAlgorithmReduceDomain::CScalarAlgorithmReduceDomain(CDomain* domainDestination, CDomain* domainSource, CReduceDomainToScalar* algo)", |
---|
| 73 | << "Operation '" << op << "' not found. Please make sure to use a supported one" |
---|
| 74 | << "Domain source " <<domainSource->getId() << std::endl |
---|
| 75 | << "Scalar destination " << scalarDestination->getId()); |
---|
| 76 | |
---|
| 77 | reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations[op]); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | void CScalarAlgorithmReduceDomain::apply(const std::vector<std::pair<int,double> >& localIndex, |
---|
| 81 | const double* dataInput, |
---|
| 82 | CArray<double,1>& dataOut, |
---|
[1076] | 83 | std::vector<bool>& flagInitial, |
---|
| 84 | bool ignoreMissingValue) |
---|
[976] | 85 | { |
---|
[1076] | 86 | reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue); |
---|
[976] | 87 | } |
---|
| 88 | |
---|
[979] | 89 | void CScalarAlgorithmReduceDomain::updateData(CArray<double,1>& dataOut) |
---|
| 90 | { |
---|
| 91 | reduction_->updateData(dataOut); |
---|
| 92 | } |
---|
| 93 | |
---|
[976] | 94 | CScalarAlgorithmReduceDomain::~CScalarAlgorithmReduceDomain() |
---|
| 95 | { |
---|
| 96 | if (0 != reduction_) delete reduction_; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | void CScalarAlgorithmReduceDomain::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs) |
---|
| 100 | { |
---|
| 101 | this->transformationMapping_.resize(1); |
---|
| 102 | this->transformationWeight_.resize(1); |
---|
| 103 | |
---|
| 104 | TransformationIndexMap& transMap = this->transformationMapping_[0]; |
---|
| 105 | TransformationWeightMap& transWeight = this->transformationWeight_[0]; |
---|
| 106 | |
---|
| 107 | int globalIndexSize = domainSrc_->ni_glo * domainSrc_->nj_glo; |
---|
| 108 | |
---|
| 109 | transMap[0].resize(globalIndexSize); |
---|
| 110 | transWeight[0].resize(globalIndexSize, 1.0); |
---|
| 111 | for (int idx = 0; idx < globalIndexSize; ++idx) |
---|
| 112 | { |
---|
| 113 | transMap[0][idx] = idx; |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | } |
---|