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

Last change on this file since 933 was 933, checked in by mhnguyen, 8 years ago

Improving transformation selection. Instead of modifying directly grid_transformation
we only need to register a new transformation with the framework

+) Update all transformations with this new method

Test
+) On Curie
+) Basic tests pass

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