source: XIOS/dev/dev_olga/src/transformation/axis_algorithm_reduce_domain.cpp @ 1620

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

Dev: adding exception handling.

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

  • Property svn:executable set to *
File size: 6.7 KB
Line 
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"
13#include "grid.hpp"
14#include "grid_transformation_factory_impl.hpp"
15#include "reduction.hpp"
16
17namespace xios {
18CGenericAlgorithmTransformation* 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)
27TRY
28{
29  std::vector<CAxis*> axisListDestP = gridDst->getAxis();
30  std::vector<CDomain*> domainListSrcP = gridSrc->getDomains();
31
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}
38CATCH
39
40bool CAxisAlgorithmReduceDomain::registerTrans()
41TRY
42{
43  CGridTransformationFactory<CAxis>::registerTransformation(TRANS_REDUCE_DOMAIN_TO_AXIS, create);
44}
45CATCH
46
47
48CAxisAlgorithmReduceDomain::CAxisAlgorithmReduceDomain(CAxis* axisDestination, CDomain* domainSource, CReduceDomainToAxis* algo)
49 : CAxisAlgorithmTransformation(axisDestination, domainSource), reduction_(0)
50TRY
51{
52  algo->checkValid(axisDestination, domainSource);
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
76  dir_ = (CReduceDomainToAxis::direction_attr::iDir == algo->direction)  ? iDir : jDir;
77  reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations[op]);
78  local = algo->local ;
79}
80CATCH
81
82void CAxisAlgorithmReduceDomain::apply(const std::vector<std::pair<int,double> >& localIndex,
83                                       const double* dataInput,
84                                       CArray<double,1>& dataOut,
85                                       std::vector<bool>& flagInitial,                     
86                                       bool ignoreMissingValue, bool firstPass)
87TRY
88{
89  reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue, firstPass);
90}
91CATCH
92
93void CAxisAlgorithmReduceDomain::updateData(CArray<double,1>& dataOut)
94TRY
95{
96  reduction_->updateData(dataOut);
97}
98CATCH
99
100CAxisAlgorithmReduceDomain::~CAxisAlgorithmReduceDomain()
101TRY
102{
103  if (0 != reduction_) delete reduction_;
104}
105CATCH
106
107void CAxisAlgorithmReduceDomain::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
108TRY
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;
118  if (iDir == dir_)
119  {
120    if (local)
121    {
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)
128      {
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        }
134      }
135    }
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    }
151  }
152  else if (jDir == dir_)
153  {
154    int nbAxisIdx = axisDstIndex.numElements();
155    if (local)
156    {
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)
163      {
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        }
169      }
170    }
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    }
185  }
186  else
187  {}
188}
189CATCH
190
191}
Note: See TracBrowser for help on using the repository browser.