source: XIOS/dev/dev_olga/src/transformation/scalar_algorithm_reduce_domain.cpp @ 1612

Last change on this file since 1612 was 1612, checked in by oabramkina, 22 months ago

Dev: adding exception handling.

To activate it, compilation flag -DXIOS_EXCEPTION should be added.

File size: 5.7 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)
28TRY
29{
30  std::vector<CScalar*> scalarListDestP = gridDst->getScalars();
31  std::vector<CDomain*> domainListSrcP  = gridSrc->getDomains();
32
33  CReduceDomainToScalar* reduceDomain = dynamic_cast<CReduceDomainToScalar*> (transformation);
34  int scalarDstIndex = elementPositionInGridDst2ScalarPosition[elementPositionInGrid];
35  int domainSrcIndex = elementPositionInGridSrc2DomainPosition[elementPositionInGrid];
36
37  return (new CScalarAlgorithmReduceDomain(scalarListDestP[scalarDstIndex], domainListSrcP[domainSrcIndex], reduceDomain));
38}
39CATCH
40
41bool CScalarAlgorithmReduceDomain::registerTrans()
42TRY
43{
44  CGridTransformationFactory<CScalar>::registerTransformation(TRANS_REDUCE_DOMAIN_TO_SCALAR, create);
45}
46CATCH
47
48CScalarAlgorithmReduceDomain::CScalarAlgorithmReduceDomain(CScalar* scalarDestination, CDomain* domainSource, CReduceDomainToScalar* algo)
49 : CScalarAlgorithmTransformation(scalarDestination, domainSource),
50   reduction_(0)
51TRY
52{
53  algo->checkValid(scalarDestination, domainSource);
54 
55  StdString op;
56  switch (algo->operation)
57  {
58    case CReduceDomainToScalar::operation_attr::sum:
59      op = "sum";
60      break;
61    case CReduceDomainToScalar::operation_attr::min:
62      op = "min";
63      break;
64    case CReduceDomainToScalar::operation_attr::max:
65      op = "max";
66      break;
67    case CReduceDomainToScalar::operation_attr::average:
68      op = "average";
69      break;
70    default:
71        ERROR("CScalarAlgorithmReduceDomain::CScalarAlgorithmReduceDomain(CDomain* domainDestination, CDomain* domainSource, CReduceDomainToScalar* algo)",
72         << "Operation must be defined."
73         << "Domain source " <<domainSource->getId() << std::endl
74         << "Scalar destination " << scalarDestination->getId());
75
76  }
77 
78  if (CReductionAlgorithm::ReductionOperations.end() == CReductionAlgorithm::ReductionOperations.find(op))
79    ERROR("CScalarAlgorithmReduceDomain::CScalarAlgorithmReduceDomain(CDomain* domainDestination, CDomain* domainSource, CReduceDomainToScalar* algo)",
80       << "Operation '" << op << "' not found. Please make sure to use a supported one"
81       << "Domain source " <<domainSource->getId() << std::endl
82       << "Scalar destination " << scalarDestination->getId());
83
84  reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations[op]);
85  local = algo->local ;
86}
87CATCH
88
89void CScalarAlgorithmReduceDomain::apply(const std::vector<std::pair<int,double> >& localIndex,
90                                         const double* dataInput,
91                                         CArray<double,1>& dataOut,
92                                         std::vector<bool>& flagInitial,                     
93                                         bool ignoreMissingValue, bool firstPass)
94TRY
95{
96  reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue, firstPass);
97}
98CATCH
99
100void CScalarAlgorithmReduceDomain::updateData(CArray<double,1>& dataOut)
101TRY
102{
103  reduction_->updateData(dataOut);
104}
105CATCH
106
107CScalarAlgorithmReduceDomain::~CScalarAlgorithmReduceDomain()
108TRY
109{
110  if (0 != reduction_) delete reduction_;
111}
112CATCH
113
114void CScalarAlgorithmReduceDomain::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
115TRY
116{
117  this->transformationMapping_.resize(1);
118  this->transformationWeight_.resize(1);
119
120  TransformationIndexMap& transMap = this->transformationMapping_[0];
121  TransformationWeightMap& transWeight = this->transformationWeight_[0];
122
123  int ni_glo = domainSrc_->ni_glo ;
124  int nj_glo = domainSrc_->nj_glo ;
125  int nbDomainIdx ;
126 
127  if (local)
128  {
129      const CArray<int, 1>& i_index = domainSrc_-> i_index.getValue() ;
130      const CArray<int, 1>& j_index = domainSrc_-> j_index.getValue() ;
131      const CArray<bool,1>& localMask = domainSrc_-> localMask ;
132      int nbDomainIdx = i_index.numElements();
133
134      for (int idxDomain = 0; idxDomain < nbDomainIdx; ++idxDomain)
135      {
136        if (localMask(idxDomain))
137        { 
138          transMap[0].push_back(j_index(idxDomain)* ni_glo + i_index(idxDomain));
139          transWeight[0].push_back(1.0) ;
140        }
141      }
142  }
143  else
144  { 
145    nbDomainIdx = ni_glo * nj_glo;
146    transMap[0].resize(nbDomainIdx);
147    transWeight[0].resize(nbDomainIdx, 1.0);
148    for (int idxDomain = 0; idxDomain < nbDomainIdx; ++idxDomain) transMap[0][idxDomain] = idxDomain;   
149  }
150 
151}
152CATCH
153
154}
Note: See TracBrowser for help on using the repository browser.