source: XIOS/dev/dev_olga/src/transformation/scalar_algorithm_reduce_axis.cpp @ 1686

Last change on this file since 1686 was 1686, checked in by yushan, 15 months ago

backup for trunk with graph

File size: 5.1 KB
Line 
1/*!
2   \file scalar_algorithm_reduce_scalar.cpp
3   \author Ha NGUYEN
4   \since 23 June 2016
5   \date 23 June 2016
6
7   \brief Algorithm for reduce an axis to a scalar
8 */
9#include "scalar_algorithm_reduce_axis.hpp"
10#include "axis.hpp"
11#include "scalar.hpp"
12#include "reduce_axis_to_scalar.hpp"
13#include "grid.hpp"
14#include "grid_transformation_factory_impl.hpp"
15
16#include "reduction.hpp"
17
18namespace xios {
19CGenericAlgorithmTransformation* CScalarAlgorithmReduceAxis::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<CAxis*> axisListSrcP  = gridSrc->getAxis();
32
33  CReduceAxisToScalar* reduceAxis = dynamic_cast<CReduceAxisToScalar*> (transformation);
34  int scalarDstIndex = elementPositionInGridDst2ScalarPosition[elementPositionInGrid];
35  int axisSrcIndex = elementPositionInGridSrc2AxisPosition[elementPositionInGrid];
36
37  return (new CScalarAlgorithmReduceAxis(scalarListDestP[scalarDstIndex], axisListSrcP[axisSrcIndex], reduceAxis));
38}
39CATCH
40
41bool CScalarAlgorithmReduceAxis::registerTrans()
42TRY
43{
44  CGridTransformationFactory<CScalar>::registerTransformation(TRANS_REDUCE_AXIS_TO_SCALAR, create);
45}
46CATCH
47
48CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CScalar* scalarDestination, CAxis* axisSource, CReduceAxisToScalar* algo)
49 : CScalarAlgorithmTransformation(scalarDestination, axisSource),
50   reduction_(0)
51TRY
52{
53  if (algo->operation.isEmpty())
54    ERROR("CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToScalar* algo)",
55           << "Operation must be defined."
56           << "Axis source " <<axisSource->getId() << std::endl
57           << "Scalar destination " << scalarDestination->getId());
58  StdString op;
59  switch (algo->operation)
60  {
61    case CReduceAxisToScalar::operation_attr::sum:
62      op = "sum";
63      break;
64    case CReduceAxisToScalar::operation_attr::min:
65      op = "min";
66      break;
67    case CReduceAxisToScalar::operation_attr::max:
68      op = "max";
69      break;
70    case CReduceAxisToScalar::operation_attr::average:
71      op = "average";
72      break;
73    default:
74        ERROR("CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CScalar* scalarDestination, CAxis* axisSource, CReduceAxisToScalar* algo)",
75         << "Operation is wrongly defined. Supported operations: sum, min, max, average." << std::endl
76         << "Domain source " <<axisSource->getId() << std::endl
77         << "Scalar destination " << scalarDestination->getId());
78
79  }
80 
81  if (CReductionAlgorithm::ReductionOperations.end() == CReductionAlgorithm::ReductionOperations.find(op))
82    ERROR("CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToScalar* algo)",
83       << "Operation '" << op << "' not found. Please make sure to use a supported one"
84       << "Axis source " <<axisSource->getId() << std::endl
85       << "Scalar destination " << scalarDestination->getId());
86
87  reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations[op]);
88}
89CATCH
90
91void CScalarAlgorithmReduceAxis::apply(const std::vector<std::pair<int,double> >& localIndex, const double* dataInput, CArray<double,1>& dataOut,
92                                         std::vector<bool>& flagInitial, bool ignoreMissingValue, bool firstPass)
93TRY
94{
95  reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue, firstPass);
96}
97CATCH
98
99void CScalarAlgorithmReduceAxis::updateData(CArray<double,1>& dataOut)
100TRY
101{
102  reduction_->updateData(dataOut);
103}
104CATCH
105
106CScalarAlgorithmReduceAxis::~CScalarAlgorithmReduceAxis()
107TRY
108{
109  if (0 != reduction_) delete reduction_;
110}
111CATCH
112
113void CScalarAlgorithmReduceAxis::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
114TRY
115{
116  this->transformationMapping_.resize(1);
117  this->transformationWeight_.resize(1);
118
119  TransformationIndexMap& transMap = this->transformationMapping_[0];
120  TransformationWeightMap& transWeight = this->transformationWeight_[0];
121
122  int globalIndexSize = axisSrc_-> n_glo;
123
124  for (int idx = 0; idx < globalIndexSize; ++idx)
125  {
126    transMap[0].push_back(idx);
127    transWeight[0].push_back(1.0);
128  }
129}
130CATCH
131
132}
Note: See TracBrowser for help on using the repository browser.