source: XIOS/dev/dev_trunk_omp/src/transformation/scalar_algorithm_reduce_scalar.cpp @ 1646

Last change on this file since 1646 was 1646, checked in by yushan, 5 years ago

branch merged with trunk @1645. arch file (ep&mpi) added for ADA

  • Property svn:eol-style set to native
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
12
13
14namespace xios {
15CGenericAlgorithmTransformation* CScalarAlgorithmReduceScalar::create(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::registerTrans()
38TRY
39{
40  CGridTransformationFactory<CScalar>::registerTransformation(TRANS_REDUCE_SCALAR_TO_SCALAR, create);
41}
42CATCH
43
44CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)
45 : CScalarAlgorithmTransformation(scalarDestination, scalarSource),
46   reduction_(0)
47TRY
48{
49  eliminateRedondantSrc_= false ;
50  if (algo->operation.isEmpty())
51    ERROR("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)",
52           << "Operation must be defined."
53           << "Scalar source " <<scalarSource->getId() << std::endl
54           << "Scalar destination " << scalarDestination->getId());
55  StdString op;
56  switch (algo->operation)
57  {
58    case CReduceScalarToScalar::operation_attr::sum:
59      op = "sum";
60      break;
61    case CReduceScalarToScalar::operation_attr::min:
62      op = "min";
63      break;
64    case CReduceScalarToScalar::operation_attr::max:
65      op = "max";
66      break;
67    case CReduceScalarToScalar::operation_attr::average:
68      op = "average";
69      break;
70    default:
71        ERROR("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)",
72         << "Operation is wrongly defined. Supported operations: sum, min, max, average." << std::endl
73         << "Scalar source " <<scalarSource->getId() << std::endl
74         << "Scalar destination " << scalarDestination->getId());
75
76  }
77
78  if(CReductionAlgorithm::ReductionOperations_ptr == 0) 
79  {
80    CReductionAlgorithm::initReductionOperation();
81  }
82 
83  if (CReductionAlgorithm::ReductionOperations_ptr->end() == CReductionAlgorithm::ReductionOperations_ptr->find(op))
84    ERROR("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)",
85       << "Operation '" << op << "' not found. Please make sure to use a supported one"
86       << "Scalar source " <<scalarSource->getId() << std::endl
87       << "Scalar destination " << scalarDestination->getId());
88
89  reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations_ptr->at(op));
90}
91CATCH
92
93void CScalarAlgorithmReduceScalar::apply(const std::vector<std::pair<int,double> >& localIndex, const double* dataInput, CArray<double,1>& dataOut,
94                                         std::vector<bool>& flagInitial, bool ignoreMissingValue, bool firstPass)
95TRY
96{
97  reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue, firstPass);
98}
99CATCH
100
101void CScalarAlgorithmReduceScalar::updateData(CArray<double,1>& dataOut)
102TRY
103{
104  reduction_->updateData(dataOut);
105}
106CATCH
107
108CScalarAlgorithmReduceScalar::~CScalarAlgorithmReduceScalar()
109TRY
110{
111  if (0 != reduction_) delete reduction_;
112}
113CATCH
114
115void CScalarAlgorithmReduceScalar::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
116TRY
117{
118  this->transformationMapping_.resize(1);
119  this->transformationWeight_.resize(1);
120
121  TransformationIndexMap& transMap = this->transformationMapping_[0];
122  TransformationWeightMap& transWeight = this->transformationWeight_[0];
123
124  transMap[0].push_back(0);
125  transWeight[0].push_back(1.0);
126
127}
128CATCH
129
130}
Note: See TracBrowser for help on using the repository browser.