source: XIOS/dev/XIOS_DEV_CMIP6/src/transformation/axis_algorithm_reduce_axis.cpp @ 1301

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

Add new transformation : reduce axis_to_axis
This a global reduction implying all process whih own the same source axis global point.

YM

  • Property svn:eol-style set to native
File size: 4.6 KB
Line 
1/*!
2   \file axis_algorithm_reduce_axis.cpp
3   \author Ha NGUYEN
4   \since 23 June 2016
5   \date 23 June 2016
6
7   \brief Algorithm for reduce a axis to an axis
8 */
9#include "axis_algorithm_reduce_axis.hpp"
10#include "reduce_axis_to_axis.hpp"
11#include "axis.hpp"
12#include "grid.hpp"
13#include "grid_transformation_factory_impl.hpp"
14#include "reduction.hpp"
15
16namespace xios {
17CGenericAlgorithmTransformation* CAxisAlgorithmReduceAxis::create(CGrid* gridDst, CGrid* gridSrc,
18                                                                   CTransformation<CAxis>* transformation,
19                                                                   int elementPositionInGrid,
20                                                                   std::map<int, int>& elementPositionInGridSrc2ScalarPosition,
21                                                                   std::map<int, int>& elementPositionInGridSrc2AxisPosition,
22                                                                   std::map<int, int>& elementPositionInGridSrc2DomainPosition,
23                                                                   std::map<int, int>& elementPositionInGridDst2ScalarPosition,
24                                                                   std::map<int, int>& elementPositionInGridDst2AxisPosition,
25                                                                   std::map<int, int>& elementPositionInGridDst2DomainPosition)
26{
27  std::vector<CAxis*> axisListDestP = gridDst->getAxis();
28  std::vector<CAxis*> axisListSrcP  = gridSrc->getAxis();
29
30  CReduceAxisToAxis* reduceAxis = dynamic_cast<CReduceAxisToAxis*> (transformation);
31  int axisDstIndex   = elementPositionInGridDst2AxisPosition[elementPositionInGrid];
32  int axisSrcIndex = elementPositionInGridSrc2AxisPosition[elementPositionInGrid];
33
34  return (new CAxisAlgorithmReduceAxis(axisListDestP[axisDstIndex], axisListSrcP[axisSrcIndex], reduceAxis));
35}
36
37bool CAxisAlgorithmReduceAxis::registerTrans()
38{
39  CGridTransformationFactory<CAxis>::registerTransformation(TRANS_REDUCE_AXIS_TO_AXIS, create);
40}
41
42
43CAxisAlgorithmReduceAxis::CAxisAlgorithmReduceAxis(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToAxis* algo)
44 : CAxisAlgorithmTransformation(axisDestination, axisSource), reduction_(0)
45{
46  eliminateRedondantSrc_= false ;
47  algo->checkValid(axisDestination, axisSource);
48  StdString op;
49  switch (algo->operation)
50  {
51    case CReduceAxisToAxis::operation_attr::sum:
52      op = "sum";
53      break;
54    case CReduceAxisToAxis::operation_attr::min:
55      op = "min";
56      break;
57    case CReduceAxisToAxis::operation_attr::max:
58      op = "max";
59      break;
60    case CReduceAxisToAxis::operation_attr::average:
61      op = "average";
62      break;
63    default:
64        ERROR("CAxisAlgorithmReduceAxis::CAxisAlgorithmReduceAxis(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToAxis* algo)",
65         << "Operation is wrongly defined. Supported operations: sum, min, max, average." << std::endl
66         << "Axis source " <<axisSource->getId() << std::endl
67         << "Axis destination " << axisDestination->getId());
68
69  }
70
71  reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations[op]);
72  local = algo->local ;
73}
74
75void CAxisAlgorithmReduceAxis::apply(const std::vector<std::pair<int,double> >& localIndex,
76                                       const double* dataInput,
77                                       CArray<double,1>& dataOut,
78                                       std::vector<bool>& flagInitial,                     
79                                       bool ignoreMissingValue, bool firstPass)
80{
81  reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue, firstPass);
82}
83
84void CAxisAlgorithmReduceAxis::updateData(CArray<double,1>& dataOut)
85{
86  reduction_->updateData(dataOut);
87}
88
89CAxisAlgorithmReduceAxis::~CAxisAlgorithmReduceAxis()
90{
91  if (0 != reduction_) delete reduction_;
92}
93
94void CAxisAlgorithmReduceAxis::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
95{
96  this->transformationMapping_.resize(1);
97  this->transformationWeight_.resize(1);
98
99  TransformationIndexMap& transMap = this->transformationMapping_[0];
100  TransformationWeightMap& transWeight = this->transformationWeight_[0];
101  CArray<int,1>& axisDstIndex = axisDest_->index;
102  int nbAxisIdx = axisDstIndex.numElements();
103  for (int idxAxis = 0; idxAxis < nbAxisIdx; ++idxAxis)
104  {
105    int globalAxisIdx = axisDstIndex(idxAxis);
106    transMap[globalAxisIdx].resize(1);
107    transWeight[globalAxisIdx].resize(1);
108    transMap[globalAxisIdx][0]=globalAxisIdx ;     
109    transWeight[globalAxisIdx][0] = 1.0 ;     
110  }
111}
112
113}
Note: See TracBrowser for help on using the repository browser.