source: XIOS/dev/XIOS_DEV_CMIP6/src/transformation/scalar_algorithm_reduce_domain.cpp @ 1313

Last change on this file since 1313 was 1313, checked in by ymipsl, 3 years ago

Add "local" attribute to reduce_domain_to_scalar transformation. The reduction is only apply on local domain.

YM

File size: 5.6 KB
Line 
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
18namespace xios {
19CGenericAlgorithmTransformation* 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
39bool CScalarAlgorithmReduceDomain::registerTrans()
40{
41  CGridTransformationFactory<CScalar>::registerTransformation(TRANS_REDUCE_DOMAIN_TO_SCALAR, create);
42}
43
44CScalarAlgorithmReduceDomain::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  local = algo->local ;
79}
80
81void CScalarAlgorithmReduceDomain::apply(const std::vector<std::pair<int,double> >& localIndex,
82                                         const double* dataInput,
83                                         CArray<double,1>& dataOut,
84                                         std::vector<bool>& flagInitial,                     
85                                         bool ignoreMissingValue, bool firstPass)
86{
87  reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue, firstPass);
88}
89
90void CScalarAlgorithmReduceDomain::updateData(CArray<double,1>& dataOut)
91{
92  reduction_->updateData(dataOut);
93}
94
95CScalarAlgorithmReduceDomain::~CScalarAlgorithmReduceDomain()
96{
97  if (0 != reduction_) delete reduction_;
98}
99
100void CScalarAlgorithmReduceDomain::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
101{
102  this->transformationMapping_.resize(1);
103  this->transformationWeight_.resize(1);
104
105  TransformationIndexMap& transMap = this->transformationMapping_[0];
106  TransformationWeightMap& transWeight = this->transformationWeight_[0];
107
108  int ni_glo = domainSrc_->ni_glo ;
109  int nj_glo = domainSrc_->nj_glo ;
110  int nbDomainIdx ;
111 
112  if (local)
113  {
114      const CArray<int, 1>& i_index = domainSrc_-> i_index.getValue() ;
115      const CArray<int, 1>& j_index = domainSrc_-> j_index.getValue() ;
116      const CArray<bool,1>& localMask = domainSrc_-> localMask ;
117      int nbDomainIdx = i_index.numElements();
118
119      for (int idxDomain = 0; idxDomain < nbDomainIdx; ++idxDomain)
120      {
121        if (localMask(idxDomain))
122        { 
123          transMap[0].push_back(j_index(idxDomain)* ni_glo + i_index(idxDomain));
124          transWeight[0].push_back(1.0) ;
125        }
126      }
127  }
128  else
129  { 
130    nbDomainIdx = ni_glo * nj_glo;
131    transMap[0].resize(nbDomainIdx);
132    transWeight[0].resize(nbDomainIdx, 1.0);
133    for (int idxDomain = 0; idxDomain < nbDomainIdx; ++idxDomain) transMap[0][idxDomain] = idxDomain;   
134  }
135 
136}
137
138}
Note: See TracBrowser for help on using the repository browser.