source: XIOS/trunk/src/transformation/scalar_algorithm_reduce_axis.cpp @ 979

Last change on this file since 979 was 979, checked in by mhnguyen, 4 years ago

Implementing AVERAGE operation

+) Add average operation for reduction transformation

Test
+) On Curie
+) Correct

File size: 4.5 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#include "reduction.hpp"
16
17#include "reduction.hpp"
18
19namespace xios {
20CGenericAlgorithmTransformation* CScalarAlgorithmReduceScalar::create(CGrid* gridDst, CGrid* gridSrc,
21                                                                     CTransformation<CScalar>* transformation,
22                                                                     int elementPositionInGrid,
23                                                                     std::map<int, int>& elementPositionInGridSrc2ScalarPosition,
24                                                                     std::map<int, int>& elementPositionInGridSrc2AxisPosition,
25                                                                     std::map<int, int>& elementPositionInGridSrc2DomainPosition,
26                                                                     std::map<int, int>& elementPositionInGridDst2ScalarPosition,
27                                                                     std::map<int, int>& elementPositionInGridDst2AxisPosition,
28                                                                     std::map<int, int>& elementPositionInGridDst2DomainPosition)
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 CScalarAlgorithmReduceScalar(scalarListDestP[scalarDstIndex], axisListSrcP[axisSrcIndex], reduceAxis));
38}
39
40bool CScalarAlgorithmReduceScalar::registerTrans()
41{
42  CGridTransformationFactory<CScalar>::registerTransformation(TRANS_REDUCE_AXIS_TO_SCALAR, create);
43}
44
45CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CAxis* axisSource, CReduceAxisToScalar* algo)
46 : CScalarAlgorithmTransformation(scalarDestination, axisSource),
47   reduction_(0)
48{
49  if (algo->operation.isEmpty())
50    ERROR("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToScalar* algo)",
51           << "Operation must be defined."
52           << "Axis source " <<axisSource->getId() << std::endl
53           << "Scalar destination " << scalarDestination->getId());
54  StdString op = algo->operation;
55  if (CReductionAlgorithm::ReductionOperations.end() == CReductionAlgorithm::ReductionOperations.find(op))
56    ERROR("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToScalar* algo)",
57       << "Operation '" << op << "' not found. Please make sure to use a supported one"
58       << "Axis source " <<axisSource->getId() << std::endl
59       << "Scalar destination " << scalarDestination->getId());
60
61  reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations[op]);
62}
63
64void CScalarAlgorithmReduceScalar::apply(const std::vector<std::pair<int,double> >& localIndex,
65                                         const double* dataInput,
66                                         CArray<double,1>& dataOut,
67                                         std::vector<bool>& flagInitial,
68                                         const double& defaultValue)
69{
70  reduction_->apply(localIndex, dataInput, dataOut, flagInitial);
71}
72
73void CScalarAlgorithmReduceScalar::updateData(CArray<double,1>& dataOut)
74{
75  reduction_->updateData(dataOut);
76}
77
78CScalarAlgorithmReduceScalar::~CScalarAlgorithmReduceScalar()
79{
80  if (0 != reduction_) delete reduction_;
81}
82
83void CScalarAlgorithmReduceScalar::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
84{
85  this->transformationMapping_.resize(1);
86  this->transformationWeight_.resize(1);
87
88  TransformationIndexMap& transMap = this->transformationMapping_[0];
89  TransformationWeightMap& transWeight = this->transformationWeight_[0];
90
91  CArray<int,1>& axisSrcIndex = axisSrc_->index;
92  int globalIndexSize = axisSrcIndex.numElements();
93
94  for (int idx = 0; idx < globalIndexSize; ++idx)
95  {
96    transMap[0].push_back(axisSrcIndex(idx));
97    transWeight[0].push_back(1.0);
98  }
99}
100
101}
Note: See TracBrowser for help on using the repository browser.