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 | |
---|
14 | namespace xios { |
---|
15 | CGenericAlgorithmTransformation* 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 | TRY |
---|
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 | } |
---|
35 | CATCH |
---|
36 | |
---|
37 | bool CScalarAlgorithmReduceScalar::registerTrans() |
---|
38 | TRY |
---|
39 | { |
---|
40 | CGridTransformationFactory<CScalar>::registerTransformation(TRANS_REDUCE_SCALAR_TO_SCALAR, create); |
---|
41 | } |
---|
42 | CATCH |
---|
43 | |
---|
44 | CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo) |
---|
45 | : CScalarAlgorithmTransformation(scalarDestination, scalarSource), |
---|
46 | reduction_(0) |
---|
47 | TRY |
---|
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.end() == CReductionAlgorithm::ReductionOperations.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[op]); |
---|
85 | } |
---|
86 | CATCH |
---|
87 | |
---|
88 | void CScalarAlgorithmReduceScalar::apply(const std::vector<std::pair<int,double> >& localIndex, const double* dataInput, CArray<double,1>& dataOut, |
---|
89 | std::vector<bool>& flagInitial, bool ignoreMissingValue, bool firstPass) |
---|
90 | TRY |
---|
91 | { |
---|
92 | reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue, firstPass); |
---|
93 | } |
---|
94 | CATCH |
---|
95 | |
---|
96 | void CScalarAlgorithmReduceScalar::updateData(CArray<double,1>& dataOut) |
---|
97 | TRY |
---|
98 | { |
---|
99 | reduction_->updateData(dataOut); |
---|
100 | } |
---|
101 | CATCH |
---|
102 | |
---|
103 | CScalarAlgorithmReduceScalar::~CScalarAlgorithmReduceScalar() |
---|
104 | TRY |
---|
105 | { |
---|
106 | if (0 != reduction_) delete reduction_; |
---|
107 | } |
---|
108 | CATCH |
---|
109 | |
---|
110 | void CScalarAlgorithmReduceScalar::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs) |
---|
111 | TRY |
---|
112 | { |
---|
113 | this->transformationMapping_.resize(1); |
---|
114 | this->transformationWeight_.resize(1); |
---|
115 | |
---|
116 | TransformationIndexMap& transMap = this->transformationMapping_[0]; |
---|
117 | TransformationWeightMap& transWeight = this->transformationWeight_[0]; |
---|
118 | |
---|
119 | transMap[0].push_back(0); |
---|
120 | transWeight[0].push_back(1.0); |
---|
121 | |
---|
122 | } |
---|
123 | CATCH |
---|
124 | |
---|
125 | } |
---|