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

Last change on this file since 1158 was 1158, checked in by oabramkina, 3 years ago

Two server levels: merging with trunk r1137.
There are bugs.

File size: 5.2 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* CScalarAlgorithmReduceAxis::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 CScalarAlgorithmReduceAxis(scalarListDestP[scalarDstIndex], axisListSrcP[axisSrcIndex], reduceAxis));
38}
39
40bool CScalarAlgorithmReduceAxis::registerTrans()
41{
42  CGridTransformationFactory<CScalar>::registerTransformation(TRANS_REDUCE_AXIS_TO_SCALAR, create);
43}
44
45CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CScalar* scalarDestination, CAxis* axisSource, CReduceAxisToScalar* algo)
46 : CScalarAlgorithmTransformation(scalarDestination, axisSource),
47   reduction_(0)
48{
49  if (algo->operation.isEmpty())
50    ERROR("CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(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;
55  switch (algo->operation)
56  {
57    case CReduceAxisToScalar::operation_attr::sum:
58      op = "sum";
59      break;
60    case CReduceAxisToScalar::operation_attr::min:
61      op = "min";
62      break;
63    case CReduceAxisToScalar::operation_attr::max:
64      op = "max";
65      break;
66    case CReduceAxisToScalar::operation_attr::average:
67      op = "average";
68      break;
69    default:
70        ERROR("CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CScalar* scalarDestination, CAxis* axisSource, CReduceAxisToScalar* algo)",
71         << "Operation is wrongly defined. Supported operations: sum, min, max, average." << std::endl
72         << "Domain source " <<axisSource->getId() << std::endl
73         << "Scalar destination " << scalarDestination->getId());
74
75  }
76 
77  if (CReductionAlgorithm::ReductionOperations.end() == CReductionAlgorithm::ReductionOperations.find(op))
78    ERROR("CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToScalar* algo)",
79       << "Operation '" << op << "' not found. Please make sure to use a supported one"
80       << "Axis source " <<axisSource->getId() << std::endl
81       << "Scalar destination " << scalarDestination->getId());
82
83  reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations[op]);
84}
85
86void CScalarAlgorithmReduceAxis::apply(const std::vector<std::pair<int,double> >& localIndex,
87                                         const double* dataInput,
88                                         CArray<double,1>& dataOut,
89                                         std::vector<bool>& flagInitial,
90                                       bool ignoreMissingValue)
91{
92  reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue);
93}
94
95void CScalarAlgorithmReduceAxis::updateData(CArray<double,1>& dataOut)
96{
97  reduction_->updateData(dataOut);
98}
99
100CScalarAlgorithmReduceAxis::~CScalarAlgorithmReduceAxis()
101{
102  if (0 != reduction_) delete reduction_;
103}
104
105void CScalarAlgorithmReduceAxis::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
106{
107  this->transformationMapping_.resize(1);
108  this->transformationWeight_.resize(1);
109
110  TransformationIndexMap& transMap = this->transformationMapping_[0];
111  TransformationWeightMap& transWeight = this->transformationWeight_[0];
112
113  CArray<int,1>& axisSrcIndex = axisSrc_->index;
114  int globalIndexSize = axisSrcIndex.numElements();
115
116  for (int idx = 0; idx < globalIndexSize; ++idx)
117  {
118    transMap[0].push_back(axisSrcIndex(idx));
119    transWeight[0].push_back(1.0);
120  }
121}
122
123}
Note: See TracBrowser for help on using the repository browser.