source: XIOS/dev/dev_ym/XIOS_COUPLING/src/transformation/scalar_algorithm/scalar_algorithm_reduce_scalar.cpp @ 1985

Last change on this file since 1985 was 1985, checked in by ymipsl, 4 years ago

intermediate commit for new tranformation engine?
YM

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 5.2 KB
Line 
1/*!
2   \file scalar_algorithm_reduce_scalar.cpp
3 
4   \brief Algorithm for reduce an scalar to a scalar
5 */
6#include "scalar_algorithm_reduce_scalar.hpp"
7#include "scalar.hpp"
8#include "reduce_scalar_to_scalar.hpp"
9#include "grid.hpp"
10#include "grid_transformation_factory_impl.hpp"
11#include "reduction.hpp"
12
13
14namespace xios {
15CGenericAlgorithmTransformation* CScalarAlgorithmReduceScalar::create(bool isSource, CGrid* gridDst, CGrid* gridSrc,
16                                                                     CTransformation<CScalar>* transformation,
17                                                                     int elementPositionInGrid,
18                                                                     std::map<int, int>& elementPositionInGridSrc2ScalarPosition,
19                                                                     std::map<int, int>& elementPositionInGridSrc2AxisPosition,
20                                                                     std::map<int, int>& elementPositionInGridSrc2DomainPosition,
21                                                                     std::map<int, int>& elementPositionInGridDst2ScalarPosition,
22                                                                     std::map<int, int>& elementPositionInGridDst2AxisPosition,
23                                                                     std::map<int, int>& elementPositionInGridDst2DomainPosition)
24TRY
25{
26  std::vector<CScalar*> scalarListDestP = gridDst->getScalars();
27  std::vector<CScalar*> scalarListSrcP  = gridSrc->getScalars();
28
29  CReduceScalarToScalar* reduceScalar = dynamic_cast<CReduceScalarToScalar*> (transformation);
30  int scalarDstIndex = elementPositionInGridDst2ScalarPosition[elementPositionInGrid];
31  int scalarSrcIndex = elementPositionInGridSrc2AxisPosition[elementPositionInGrid];
32
33  return (new CScalarAlgorithmReduceScalar(scalarListDestP[scalarDstIndex], scalarListSrcP[scalarSrcIndex], reduceScalar));
34}
35CATCH
36
37bool CScalarAlgorithmReduceScalar::dummyRegistered_ = CScalarAlgorithmReduceScalar::registerTrans();
38bool CScalarAlgorithmReduceScalar::registerTrans()
39TRY
40{
41  return CGridTransformationFactory<CScalar>::registerTransformation(TRANS_REDUCE_SCALAR_TO_SCALAR, create);
42}
43CATCH
44
45CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)
46 : CScalarAlgorithmTransformation(scalarDestination, scalarSource),
47   reduction_(0)
48TRY
49{
50  eliminateRedondantSrc_= false ;
51  if (algo->operation.isEmpty())
52    ERROR("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)",
53           << "Operation must be defined."
54           << "Scalar source " <<scalarSource->getId() << std::endl
55           << "Scalar destination " << scalarDestination->getId());
56  StdString op;
57  switch (algo->operation)
58  {
59    case CReduceScalarToScalar::operation_attr::sum:
60      op = "sum";
61      break;
62    case CReduceScalarToScalar::operation_attr::min:
63      op = "min";
64      break;
65    case CReduceScalarToScalar::operation_attr::max:
66      op = "max";
67      break;
68    case CReduceScalarToScalar::operation_attr::average:
69      op = "average";
70      break;
71    default:
72        ERROR("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)",
73         << "Operation is wrongly defined. Supported operations: sum, min, max, average." << std::endl
74         << "Scalar source " <<scalarSource->getId() << std::endl
75         << "Scalar destination " << scalarDestination->getId());
76
77  }
78 
79  if (CReductionAlgorithm::ReductionOperations.end() == CReductionAlgorithm::ReductionOperations.find(op))
80    ERROR("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)",
81       << "Operation '" << op << "' not found. Please make sure to use a supported one"
82       << "Scalar source " <<scalarSource->getId() << std::endl
83       << "Scalar destination " << scalarDestination->getId());
84
85  reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations[op]);
86}
87CATCH
88
89void CScalarAlgorithmReduceScalar::apply(const std::vector<std::pair<int,double> >& localIndex, const double* dataInput, CArray<double,1>& dataOut,
90                                         std::vector<bool>& flagInitial, bool ignoreMissingValue, bool firstPass)
91TRY
92{
93  reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue, firstPass);
94}
95CATCH
96
97void CScalarAlgorithmReduceScalar::updateData(CArray<double,1>& dataOut)
98TRY
99{
100  reduction_->updateData(dataOut);
101}
102CATCH
103
104CScalarAlgorithmReduceScalar::~CScalarAlgorithmReduceScalar()
105TRY
106{
107  if (0 != reduction_) delete reduction_;
108}
109CATCH
110
111void CScalarAlgorithmReduceScalar::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
112TRY
113{
114  this->transformationMapping_.resize(1);
115  this->transformationWeight_.resize(1);
116
117  TransformationIndexMap& transMap = this->transformationMapping_[0];
118  TransformationWeightMap& transWeight = this->transformationWeight_[0];
119
120  transMap[0].push_back(0);
121  transWeight[0].push_back(1.0);
122
123}
124CATCH
125
126}
Note: See TracBrowser for help on using the repository browser.