[895] | 1 | /*! |
---|
| 2 | \file axis_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 an axis |
---|
| 8 | */ |
---|
| 9 | #include "axis_algorithm_reduce_domain.hpp" |
---|
| 10 | #include "reduce_domain_to_axis.hpp" |
---|
| 11 | #include "axis.hpp" |
---|
| 12 | #include "domain.hpp" |
---|
[933] | 13 | #include "grid.hpp" |
---|
| 14 | #include "grid_transformation_factory_impl.hpp" |
---|
[977] | 15 | #include "reduction.hpp" |
---|
[895] | 16 | |
---|
| 17 | namespace xios { |
---|
[933] | 18 | CGenericAlgorithmTransformation* CAxisAlgorithmReduceDomain::create(CGrid* gridDst, CGrid* gridSrc, |
---|
| 19 | CTransformation<CAxis>* transformation, |
---|
| 20 | int elementPositionInGrid, |
---|
| 21 | std::map<int, int>& elementPositionInGridSrc2ScalarPosition, |
---|
| 22 | std::map<int, int>& elementPositionInGridSrc2AxisPosition, |
---|
| 23 | std::map<int, int>& elementPositionInGridSrc2DomainPosition, |
---|
| 24 | std::map<int, int>& elementPositionInGridDst2ScalarPosition, |
---|
| 25 | std::map<int, int>& elementPositionInGridDst2AxisPosition, |
---|
| 26 | std::map<int, int>& elementPositionInGridDst2DomainPosition) |
---|
[1612] | 27 | TRY |
---|
[933] | 28 | { |
---|
| 29 | std::vector<CAxis*> axisListDestP = gridDst->getAxis(); |
---|
| 30 | std::vector<CDomain*> domainListSrcP = gridSrc->getDomains(); |
---|
[895] | 31 | |
---|
[933] | 32 | CReduceDomainToAxis* reduceDomain = dynamic_cast<CReduceDomainToAxis*> (transformation); |
---|
| 33 | int axisDstIndex = elementPositionInGridDst2AxisPosition[elementPositionInGrid]; |
---|
| 34 | int domainSrcIndex = elementPositionInGridSrc2DomainPosition[elementPositionInGrid]; |
---|
| 35 | |
---|
| 36 | return (new CAxisAlgorithmReduceDomain(axisListDestP[axisDstIndex], domainListSrcP[domainSrcIndex], reduceDomain)); |
---|
| 37 | } |
---|
[1612] | 38 | CATCH |
---|
[933] | 39 | |
---|
| 40 | bool CAxisAlgorithmReduceDomain::registerTrans() |
---|
[1612] | 41 | TRY |
---|
[933] | 42 | { |
---|
| 43 | CGridTransformationFactory<CAxis>::registerTransformation(TRANS_REDUCE_DOMAIN_TO_AXIS, create); |
---|
| 44 | } |
---|
[1612] | 45 | CATCH |
---|
[933] | 46 | |
---|
| 47 | |
---|
[895] | 48 | CAxisAlgorithmReduceDomain::CAxisAlgorithmReduceDomain(CAxis* axisDestination, CDomain* domainSource, CReduceDomainToAxis* algo) |
---|
| 49 | : CAxisAlgorithmTransformation(axisDestination, domainSource), reduction_(0) |
---|
[1612] | 50 | TRY |
---|
[895] | 51 | { |
---|
| 52 | algo->checkValid(axisDestination, domainSource); |
---|
[980] | 53 | StdString op; |
---|
| 54 | switch (algo->operation) |
---|
| 55 | { |
---|
| 56 | case CReduceDomainToAxis::operation_attr::sum: |
---|
| 57 | op = "sum"; |
---|
| 58 | break; |
---|
| 59 | case CReduceDomainToAxis::operation_attr::min: |
---|
| 60 | op = "min"; |
---|
| 61 | break; |
---|
| 62 | case CReduceDomainToAxis::operation_attr::max: |
---|
| 63 | op = "max"; |
---|
| 64 | break; |
---|
| 65 | case CReduceDomainToAxis::operation_attr::average: |
---|
| 66 | op = "average"; |
---|
| 67 | break; |
---|
| 68 | default: |
---|
| 69 | ERROR("CAxisAlgorithmReduceDomain::CAxisAlgorithmReduceDomain(CAxis* axisDestination, CDomain* domainSource, CReduceDomainToAxis* algo)", |
---|
| 70 | << "Operation is wrongly defined. Supported operations: sum, min, max, average." << std::endl |
---|
| 71 | << "Domain source " <<domainSource->getId() << std::endl |
---|
| 72 | << "Axis destination " << axisDestination->getId()); |
---|
| 73 | |
---|
| 74 | } |
---|
| 75 | |
---|
[938] | 76 | dir_ = (CReduceDomainToAxis::direction_attr::iDir == algo->direction) ? iDir : jDir; |
---|
[895] | 77 | reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations[op]); |
---|
[1299] | 78 | local = algo->local ; |
---|
[895] | 79 | } |
---|
[1612] | 80 | CATCH |
---|
[895] | 81 | |
---|
| 82 | void CAxisAlgorithmReduceDomain::apply(const std::vector<std::pair<int,double> >& localIndex, |
---|
[918] | 83 | const double* dataInput, |
---|
| 84 | CArray<double,1>& dataOut, |
---|
[1158] | 85 | std::vector<bool>& flagInitial, |
---|
[1260] | 86 | bool ignoreMissingValue, bool firstPass) |
---|
[1612] | 87 | TRY |
---|
[895] | 88 | { |
---|
[1260] | 89 | reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue, firstPass); |
---|
[895] | 90 | } |
---|
[1612] | 91 | CATCH |
---|
[895] | 92 | |
---|
[979] | 93 | void CAxisAlgorithmReduceDomain::updateData(CArray<double,1>& dataOut) |
---|
[1612] | 94 | TRY |
---|
[979] | 95 | { |
---|
| 96 | reduction_->updateData(dataOut); |
---|
| 97 | } |
---|
[1612] | 98 | CATCH |
---|
[979] | 99 | |
---|
[895] | 100 | CAxisAlgorithmReduceDomain::~CAxisAlgorithmReduceDomain() |
---|
[1612] | 101 | TRY |
---|
[895] | 102 | { |
---|
| 103 | if (0 != reduction_) delete reduction_; |
---|
| 104 | } |
---|
[1612] | 105 | CATCH |
---|
[895] | 106 | |
---|
| 107 | void CAxisAlgorithmReduceDomain::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs) |
---|
[1612] | 108 | TRY |
---|
[895] | 109 | { |
---|
| 110 | this->transformationMapping_.resize(1); |
---|
| 111 | this->transformationWeight_.resize(1); |
---|
| 112 | |
---|
| 113 | TransformationIndexMap& transMap = this->transformationMapping_[0]; |
---|
| 114 | TransformationWeightMap& transWeight = this->transformationWeight_[0]; |
---|
| 115 | |
---|
| 116 | CArray<int,1>& axisDstIndex = axisDest_->index; |
---|
| 117 | int ni_glo = domainSrc_->ni_glo, nj_glo = domainSrc_->nj_glo; |
---|
[1204] | 118 | if (iDir == dir_) |
---|
[895] | 119 | { |
---|
[1299] | 120 | if (local) |
---|
[895] | 121 | { |
---|
[1299] | 122 | const CArray<int, 1>& i_index = domainSrc_-> i_index.getValue() ; |
---|
| 123 | const CArray<int, 1>& j_index = domainSrc_-> j_index.getValue() ; |
---|
| 124 | const CArray<bool,1>& localMask = domainSrc_-> localMask ; |
---|
| 125 | int nbDomainIdx = i_index.numElements(); |
---|
| 126 | |
---|
| 127 | for (int idxDomain = 0; idxDomain < nbDomainIdx; ++idxDomain) |
---|
[895] | 128 | { |
---|
[1299] | 129 | if (localMask(idxDomain)) |
---|
| 130 | { |
---|
| 131 | transMap[j_index(idxDomain)].push_back(j_index(idxDomain)* ni_glo + i_index(idxDomain)); |
---|
| 132 | transWeight[j_index(idxDomain)].push_back(1.0) ; |
---|
| 133 | } |
---|
[895] | 134 | } |
---|
| 135 | } |
---|
[1299] | 136 | else |
---|
| 137 | { |
---|
| 138 | int nbAxisIdx = axisDstIndex.numElements(); |
---|
| 139 | for (int idxAxis = 0; idxAxis < nbAxisIdx; ++idxAxis) |
---|
| 140 | { |
---|
| 141 | int globalAxisIdx = axisDstIndex(idxAxis); |
---|
| 142 | transMap[globalAxisIdx].resize(ni_glo); |
---|
| 143 | transWeight[globalAxisIdx].resize(ni_glo); |
---|
| 144 | for (int idx = 0; idx < ni_glo; ++idx) |
---|
| 145 | { |
---|
| 146 | transMap[globalAxisIdx][idx] = globalAxisIdx * ni_glo + idx; |
---|
| 147 | transWeight[globalAxisIdx][idx] = 1.0; |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | } |
---|
[895] | 151 | } |
---|
[1204] | 152 | else if (jDir == dir_) |
---|
[895] | 153 | { |
---|
| 154 | int nbAxisIdx = axisDstIndex.numElements(); |
---|
[1299] | 155 | if (local) |
---|
[895] | 156 | { |
---|
[1299] | 157 | const CArray<int, 1>& i_index = domainSrc_-> i_index.getValue() ; |
---|
| 158 | const CArray<int, 1>& j_index = domainSrc_-> j_index.getValue() ; |
---|
| 159 | const CArray<bool,1>& localMask = domainSrc_-> localMask ; |
---|
| 160 | int nbDomainIdx = i_index.numElements(); |
---|
| 161 | |
---|
| 162 | for (int idxDomain = 0; idxDomain < nbDomainIdx; ++idxDomain) |
---|
[895] | 163 | { |
---|
[1299] | 164 | if (localMask(idxDomain)) |
---|
| 165 | { |
---|
| 166 | transMap[i_index(idxDomain)].push_back(j_index(idxDomain)* ni_glo + i_index(idxDomain)); |
---|
| 167 | transWeight[i_index(idxDomain)].push_back(1.0) ; |
---|
| 168 | } |
---|
[895] | 169 | } |
---|
| 170 | } |
---|
[1299] | 171 | else |
---|
| 172 | { |
---|
| 173 | for (int idxAxis = 0; idxAxis < nbAxisIdx; ++idxAxis) |
---|
| 174 | { |
---|
| 175 | int globalAxisIdx = axisDstIndex(idxAxis); |
---|
| 176 | transMap[globalAxisIdx].resize(nj_glo); |
---|
| 177 | transWeight[globalAxisIdx].resize(nj_glo); |
---|
| 178 | for (int idx = 0; idx < nj_glo; ++idx) |
---|
| 179 | { |
---|
| 180 | transMap[globalAxisIdx][idx] = globalAxisIdx + ni_glo*idx; |
---|
| 181 | transWeight[globalAxisIdx][idx] = 1.0; |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | } |
---|
[895] | 185 | } |
---|
| 186 | else |
---|
| 187 | {} |
---|
| 188 | } |
---|
[1612] | 189 | CATCH |
---|
[895] | 190 | |
---|
| 191 | } |
---|