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

Last change on this file since 1396 was 1396, checked in by ymipsl, 5 years ago

reduce domain to scalar transformation : attribute "local" was not set to default value if not present.

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  algo->checkValid(scalarDestination, domainSource);
49 
50  StdString op;
51  switch (algo->operation)
52  {
53    case CReduceDomainToScalar::operation_attr::sum:
54      op = "sum";
55      break;
56    case CReduceDomainToScalar::operation_attr::min:
57      op = "min";
58      break;
59    case CReduceDomainToScalar::operation_attr::max:
60      op = "max";
61      break;
62    case CReduceDomainToScalar::operation_attr::average:
63      op = "average";
64      break;
65    default:
66        ERROR("CScalarAlgorithmReduceDomain::CScalarAlgorithmReduceDomain(CDomain* domainDestination, CDomain* domainSource, CReduceDomainToScalar* algo)",
67         << "Operation must be defined."
68         << "Domain source " <<domainSource->getId() << std::endl
69         << "Scalar destination " << scalarDestination->getId());
70
71  }
72 
73  if (CReductionAlgorithm::ReductionOperations.end() == CReductionAlgorithm::ReductionOperations.find(op))
74    ERROR("CScalarAlgorithmReduceDomain::CScalarAlgorithmReduceDomain(CDomain* domainDestination, CDomain* domainSource, CReduceDomainToScalar* algo)",
75       << "Operation '" << op << "' not found. Please make sure to use a supported one"
76       << "Domain source " <<domainSource->getId() << std::endl
77       << "Scalar destination " << scalarDestination->getId());
78
79  reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations[op]);
80  local = algo->local ;
81}
82
83void CScalarAlgorithmReduceDomain::apply(const std::vector<std::pair<int,double> >& localIndex,
84                                         const double* dataInput,
85                                         CArray<double,1>& dataOut,
86                                         std::vector<bool>& flagInitial,                     
87                                         bool ignoreMissingValue, bool firstPass)
88{
89  reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue, firstPass);
90}
91
92void CScalarAlgorithmReduceDomain::updateData(CArray<double,1>& dataOut)
93{
94  reduction_->updateData(dataOut);
95}
96
97CScalarAlgorithmReduceDomain::~CScalarAlgorithmReduceDomain()
98{
99  if (0 != reduction_) delete reduction_;
100}
101
102void CScalarAlgorithmReduceDomain::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
103{
104  this->transformationMapping_.resize(1);
105  this->transformationWeight_.resize(1);
106
107  TransformationIndexMap& transMap = this->transformationMapping_[0];
108  TransformationWeightMap& transWeight = this->transformationWeight_[0];
109
110  int ni_glo = domainSrc_->ni_glo ;
111  int nj_glo = domainSrc_->nj_glo ;
112  int nbDomainIdx ;
113 
114  if (local)
115  {
116      const CArray<int, 1>& i_index = domainSrc_-> i_index.getValue() ;
117      const CArray<int, 1>& j_index = domainSrc_-> j_index.getValue() ;
118      const CArray<bool,1>& localMask = domainSrc_-> localMask ;
119      int nbDomainIdx = i_index.numElements();
120
121      for (int idxDomain = 0; idxDomain < nbDomainIdx; ++idxDomain)
122      {
123        if (localMask(idxDomain))
124        { 
125          transMap[0].push_back(j_index(idxDomain)* ni_glo + i_index(idxDomain));
126          transWeight[0].push_back(1.0) ;
127        }
128      }
129  }
130  else
131  { 
132    nbDomainIdx = ni_glo * nj_glo;
133    transMap[0].resize(nbDomainIdx);
134    transWeight[0].resize(nbDomainIdx, 1.0);
135    for (int idxDomain = 0; idxDomain < nbDomainIdx; ++idxDomain) transMap[0][idxDomain] = idxDomain;   
136  }
137 
138}
139
140}
Note: See TracBrowser for help on using the repository browser.