1 | /*! |
---|
2 | \file scalar_algorithm_reduce_scalar.cpp |
---|
3 | \author Ha NGUYEN |
---|
4 | \since 23 June 2016 |
---|
5 | \date 23 June 2016 |
---|
6 | |
---|
7 | \brief Algorithm for reduce an axis to a scalar |
---|
8 | */ |
---|
9 | #include "scalar_algorithm_reduce_axis.hpp" |
---|
10 | #include "axis.hpp" |
---|
11 | #include "scalar.hpp" |
---|
12 | #include "reduce_axis_to_scalar.hpp" |
---|
13 | #include "grid.hpp" |
---|
14 | #include "grid_transformation_factory_impl.hpp" |
---|
15 | |
---|
16 | #include "reduction.hpp" |
---|
17 | |
---|
18 | namespace xios { |
---|
19 | CGenericAlgorithmTransformation* CScalarAlgorithmReduceScalar::create(CGrid* gridDst, CGrid* gridSrc, |
---|
20 | CTransformation<CScalar>* transformation, |
---|
21 | int elementPositionInGrid, |
---|
22 | std::map<int, int>& elementPositionInGridSrc2ScalarPosition, |
---|
23 | std::map<int, int>& elementPositionInGridSrc2AxisPosition, |
---|
24 | std::map<int, int>& elementPositionInGridSrc2DomainPosition, |
---|
25 | std::map<int, int>& elementPositionInGridDst2ScalarPosition, |
---|
26 | std::map<int, int>& elementPositionInGridDst2AxisPosition, |
---|
27 | std::map<int, int>& elementPositionInGridDst2DomainPosition) |
---|
28 | { |
---|
29 | std::vector<CScalar*> scalarListDestP = gridDst->getScalars(); |
---|
30 | std::vector<CAxis*> axisListSrcP = gridSrc->getAxis(); |
---|
31 | |
---|
32 | CReduceAxisToScalar* reduceAxis = dynamic_cast<CReduceAxisToScalar*> (transformation); |
---|
33 | int scalarDstIndex = elementPositionInGridDst2ScalarPosition[elementPositionInGrid]; |
---|
34 | int axisSrcIndex = elementPositionInGridSrc2AxisPosition[elementPositionInGrid]; |
---|
35 | |
---|
36 | return (new CScalarAlgorithmReduceScalar(scalarListDestP[scalarDstIndex], axisListSrcP[axisSrcIndex], reduceAxis)); |
---|
37 | } |
---|
38 | |
---|
39 | bool CScalarAlgorithmReduceScalar::registerTrans() |
---|
40 | { |
---|
41 | CGridTransformationFactory<CScalar>::registerTransformation(TRANS_REDUCE_AXIS_TO_SCALAR, create); |
---|
42 | } |
---|
43 | |
---|
44 | CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CAxis* axisSource, CReduceAxisToScalar* algo) |
---|
45 | : CScalarAlgorithmTransformation(scalarDestination, axisSource), |
---|
46 | reduction_(0) |
---|
47 | { |
---|
48 | if (algo->operation.isEmpty()) |
---|
49 | ERROR("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToScalar* algo)", |
---|
50 | << "Operation must be defined." |
---|
51 | << "Axis source " <<axisSource->getId() << std::endl |
---|
52 | << "Scalar destination " << scalarDestination->getId()); |
---|
53 | StdString op = algo->operation; |
---|
54 | if (CReductionAlgorithm::ReductionOperations.end() == CReductionAlgorithm::ReductionOperations.find(op)) |
---|
55 | ERROR("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToScalar* algo)", |
---|
56 | << "Operation '" << op << "' not found. Please make sure to use a supported one" |
---|
57 | << "Axis source " <<axisSource->getId() << std::endl |
---|
58 | << "Scalar destination " << scalarDestination->getId()); |
---|
59 | |
---|
60 | reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations[op]); |
---|
61 | } |
---|
62 | |
---|
63 | void CScalarAlgorithmReduceScalar::apply(const std::vector<std::pair<int,double> >& localIndex, |
---|
64 | const double* dataInput, |
---|
65 | CArray<double,1>& dataOut, |
---|
66 | std::vector<bool>& flagInitial, |
---|
67 | const double& defaultValue) |
---|
68 | { |
---|
69 | reduction_->apply(localIndex, dataInput, dataOut, flagInitial); |
---|
70 | } |
---|
71 | |
---|
72 | CScalarAlgorithmReduceScalar::~CScalarAlgorithmReduceScalar() |
---|
73 | { |
---|
74 | if (0 != reduction_) delete reduction_; |
---|
75 | } |
---|
76 | |
---|
77 | void CScalarAlgorithmReduceScalar::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs) |
---|
78 | { |
---|
79 | this->transformationMapping_.resize(1); |
---|
80 | this->transformationWeight_.resize(1); |
---|
81 | |
---|
82 | TransformationIndexMap& transMap = this->transformationMapping_[0]; |
---|
83 | TransformationWeightMap& transWeight = this->transformationWeight_[0]; |
---|
84 | |
---|
85 | CArray<int,1>& axisSrcIndex = axisSrc_->index; |
---|
86 | int globalIndexSize = axisSrcIndex.numElements(); |
---|
87 | |
---|
88 | for (int idx = 0; idx < globalIndexSize; ++idx) |
---|
89 | { |
---|
90 | transMap[0].push_back(axisSrcIndex(idx)); |
---|
91 | transWeight[0].push_back(1.0); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | } |
---|