source: XIOS/dev/XIOS_DEV_CMIP6/src/transformation/axis_algorithm_reduce_domain.cpp @ 1299

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

Add new attribute "local" in reduce_domain_to_axis transformation :

  • "local" mean that the reduction is apply locally ie only on current process.

YM

  • Property svn:executable set to *
File size: 6.6 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)
27{
28  std::vector<CAxis*> axisListDestP = gridDst->getAxis();
29  std::vector<CDomain*> domainListSrcP = gridSrc->getDomains();
30
31  CReduceDomainToAxis* reduceDomain = dynamic_cast<CReduceDomainToAxis*> (transformation);
32  int axisDstIndex   = elementPositionInGridDst2AxisPosition[elementPositionInGrid];
33  int domainSrcIndex = elementPositionInGridSrc2DomainPosition[elementPositionInGrid];
34
35  return (new CAxisAlgorithmReduceDomain(axisListDestP[axisDstIndex], domainListSrcP[domainSrcIndex], reduceDomain));
36}
37
38bool CAxisAlgorithmReduceDomain::registerTrans()
39{
40  CGridTransformationFactory<CAxis>::registerTransformation(TRANS_REDUCE_DOMAIN_TO_AXIS, create);
41}
42
43
44CAxisAlgorithmReduceDomain::CAxisAlgorithmReduceDomain(CAxis* axisDestination, CDomain* domainSource, CReduceDomainToAxis* algo)
45 : CAxisAlgorithmTransformation(axisDestination, domainSource), reduction_(0)
46{
47  algo->checkValid(axisDestination, domainSource);
48  StdString op;
49  switch (algo->operation)
50  {
51    case CReduceDomainToAxis::operation_attr::sum:
52      op = "sum";
53      break;
54    case CReduceDomainToAxis::operation_attr::min:
55      op = "min";
56      break;
57    case CReduceDomainToAxis::operation_attr::max:
58      op = "max";
59      break;
60    case CReduceDomainToAxis::operation_attr::average:
61      op = "average";
62      break;
63    default:
64        ERROR("CAxisAlgorithmReduceDomain::CAxisAlgorithmReduceDomain(CAxis* axisDestination, CDomain* domainSource, CReduceDomainToAxis* algo)",
65         << "Operation is wrongly defined. Supported operations: sum, min, max, average." << std::endl
66         << "Domain source " <<domainSource->getId() << std::endl
67         << "Axis destination " << axisDestination->getId());
68
69  }
70
71  dir_ = (CReduceDomainToAxis::direction_attr::iDir == algo->direction)  ? iDir : jDir;
72  reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations[op]);
73  local = algo->local ;
74}
75
76void CAxisAlgorithmReduceDomain::apply(const std::vector<std::pair<int,double> >& localIndex,
77                                       const double* dataInput,
78                                       CArray<double,1>& dataOut,
79                                       std::vector<bool>& flagInitial,                     
80                                       bool ignoreMissingValue, bool firstPass)
81{
82  reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue, firstPass);
83}
84
85void CAxisAlgorithmReduceDomain::updateData(CArray<double,1>& dataOut)
86{
87  reduction_->updateData(dataOut);
88}
89
90CAxisAlgorithmReduceDomain::~CAxisAlgorithmReduceDomain()
91{
92  if (0 != reduction_) delete reduction_;
93}
94
95void CAxisAlgorithmReduceDomain::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
96{
97  this->transformationMapping_.resize(1);
98  this->transformationWeight_.resize(1);
99
100  TransformationIndexMap& transMap = this->transformationMapping_[0];
101  TransformationWeightMap& transWeight = this->transformationWeight_[0];
102
103  CArray<int,1>& axisDstIndex = axisDest_->index;
104  int ni_glo = domainSrc_->ni_glo, nj_glo = domainSrc_->nj_glo;
105  if (iDir == dir_)
106  {
107    if (local)
108    {
109      const CArray<int, 1>& i_index = domainSrc_-> i_index.getValue() ;
110      const CArray<int, 1>& j_index = domainSrc_-> j_index.getValue() ;
111      const CArray<bool,1>& localMask = domainSrc_-> localMask ;
112      int nbDomainIdx = i_index.numElements();
113     
114      for (int idxDomain = 0; idxDomain < nbDomainIdx; ++idxDomain)
115      {
116        if (localMask(idxDomain))
117        { 
118          transMap[j_index(idxDomain)].push_back(j_index(idxDomain)* ni_glo + i_index(idxDomain));
119          transWeight[j_index(idxDomain)].push_back(1.0) ;
120        }
121      }
122    }
123    else
124    {
125      int nbAxisIdx = axisDstIndex.numElements();
126      for (int idxAxis = 0; idxAxis < nbAxisIdx; ++idxAxis)
127      {
128        int globalAxisIdx = axisDstIndex(idxAxis);
129        transMap[globalAxisIdx].resize(ni_glo);
130        transWeight[globalAxisIdx].resize(ni_glo);
131        for (int idx = 0; idx < ni_glo; ++idx)
132        {
133          transMap[globalAxisIdx][idx] = globalAxisIdx * ni_glo + idx;
134          transWeight[globalAxisIdx][idx] = 1.0;
135        }
136      }
137    }
138  }
139  else if (jDir == dir_)
140  {
141    int nbAxisIdx = axisDstIndex.numElements();
142    if (local)
143    {
144      const CArray<int, 1>& i_index = domainSrc_-> i_index.getValue() ;
145      const CArray<int, 1>& j_index = domainSrc_-> j_index.getValue() ;
146      const CArray<bool,1>& localMask = domainSrc_-> localMask ;
147      int nbDomainIdx = i_index.numElements();
148     
149      for (int idxDomain = 0; idxDomain < nbDomainIdx; ++idxDomain)
150      {
151        if (localMask(idxDomain))
152        { 
153          transMap[i_index(idxDomain)].push_back(j_index(idxDomain)* ni_glo + i_index(idxDomain));
154          transWeight[i_index(idxDomain)].push_back(1.0) ;
155        }
156      }
157    }
158    else
159    {
160      for (int idxAxis = 0; idxAxis < nbAxisIdx; ++idxAxis)
161      {
162        int globalAxisIdx = axisDstIndex(idxAxis);
163        transMap[globalAxisIdx].resize(nj_glo);
164        transWeight[globalAxisIdx].resize(nj_glo);
165        for (int idx = 0; idx < nj_glo; ++idx)
166        {
167          transMap[globalAxisIdx][idx] = globalAxisIdx + ni_glo*idx;
168          transWeight[globalAxisIdx][idx] = 1.0;
169        }
170      }
171    }
172  }
173  else
174  {}
175}
176
177}
Note: See TracBrowser for help on using the repository browser.