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

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

MARK: Dynamic workflow graph developement. Branch up to date with trunk @1663.

  • Property svn:eol-style set to native
File size: 5.3 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  std::cout<<"================================ CScalarAlgorithmReduceScalar::apply"<<std::endl;
98  reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue, firstPass);
99}
100CATCH
101
102void CScalarAlgorithmReduceScalar::updateData(CArray<double,1>& dataOut)
103TRY
104{
105  reduction_->updateData(dataOut);
106}
107CATCH
108
109CScalarAlgorithmReduceScalar::~CScalarAlgorithmReduceScalar()
110TRY
111{
112  if (0 != reduction_) delete reduction_;
113}
114CATCH
115
116void CScalarAlgorithmReduceScalar::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
117TRY
118{
119  this->transformationMapping_.resize(1);
120  this->transformationWeight_.resize(1);
121
122  TransformationIndexMap& transMap = this->transformationMapping_[0];
123  TransformationWeightMap& transWeight = this->transformationWeight_[0];
124
125  transMap[0].push_back(0);
126  transWeight[0].push_back(1.0);
127
128}
129CATCH
130
131}
Note: See TracBrowser for help on using the repository browser.