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

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

branch_openmp merged with trunk r1597

  • Property svn:eol-style set to native
File size: 5.1 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)
24{
25  std::vector<CScalar*> scalarListDestP = gridDst->getScalars();
26  std::vector<CScalar*> scalarListSrcP  = gridSrc->getScalars();
27
28  CReduceScalarToScalar* reduceScalar = dynamic_cast<CReduceScalarToScalar*> (transformation);
29  int scalarDstIndex = elementPositionInGridDst2ScalarPosition[elementPositionInGrid];
30  int scalarSrcIndex = elementPositionInGridSrc2AxisPosition[elementPositionInGrid];
31
32  return (new CScalarAlgorithmReduceScalar(scalarListDestP[scalarDstIndex], scalarListSrcP[scalarSrcIndex], reduceScalar));
33}
34
35bool CScalarAlgorithmReduceScalar::registerTrans()
36{
37  CGridTransformationFactory<CScalar>::registerTransformation(TRANS_REDUCE_SCALAR_TO_SCALAR, create);
38}
39
40CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)
41 : CScalarAlgorithmTransformation(scalarDestination, scalarSource),
42   reduction_(0)
43{
44  eliminateRedondantSrc_= false ;
45  if (algo->operation.isEmpty())
46    ERROR("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)",
47           << "Operation must be defined."
48           << "Scalar source " <<scalarSource->getId() << std::endl
49           << "Scalar destination " << scalarDestination->getId());
50  StdString op;
51  switch (algo->operation)
52  {
53    case CReduceScalarToScalar::operation_attr::sum:
54      op = "sum";
55      break;
56    case CReduceScalarToScalar::operation_attr::min:
57      op = "min";
58      break;
59    case CReduceScalarToScalar::operation_attr::max:
60      op = "max";
61      break;
62    case CReduceScalarToScalar::operation_attr::average:
63      op = "average";
64      break;
65    default:
66        ERROR("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)",
67         << "Operation is wrongly defined. Supported operations: sum, min, max, average." << std::endl
68         << "Scalar source " <<scalarSource->getId() << std::endl
69         << "Scalar destination " << scalarDestination->getId());
70
71  }
72
73  if(CReductionAlgorithm::ReductionOperations_ptr == 0) 
74  {
75    CReductionAlgorithm::initReductionOperation();
76  }
77 
78  if (CReductionAlgorithm::ReductionOperations_ptr->end() == CReductionAlgorithm::ReductionOperations_ptr->find(op))
79    ERROR("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)",
80       << "Operation '" << op << "' not found. Please make sure to use a supported one"
81       << "Scalar source " <<scalarSource->getId() << std::endl
82       << "Scalar destination " << scalarDestination->getId());
83
84  reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations_ptr->at(op));
85}
86
87void CScalarAlgorithmReduceScalar::apply(const std::vector<std::pair<int,double> >& localIndex, const double* dataInput, CArray<double,1>& dataOut,
88                                         std::vector<bool>& flagInitial, bool ignoreMissingValue, bool firstPass)
89{
90  reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue, firstPass);
91}
92
93void CScalarAlgorithmReduceScalar::updateData(CArray<double,1>& dataOut)
94{
95  reduction_->updateData(dataOut);
96}
97
98CScalarAlgorithmReduceScalar::~CScalarAlgorithmReduceScalar()
99{
100  if (0 != reduction_) delete reduction_;
101}
102
103void CScalarAlgorithmReduceScalar::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
104{
105  this->transformationMapping_.resize(1);
106  this->transformationWeight_.resize(1);
107
108  TransformationIndexMap& transMap = this->transformationMapping_[0];
109  TransformationWeightMap& transWeight = this->transformationWeight_[0];
110
111  transMap[0].push_back(0);
112  transWeight[0].push_back(1.0);
113
114}
115
116}
Note: See TracBrowser for help on using the repository browser.