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 | #include "grid_algorithm_reduce.hpp" |
---|
13 | |
---|
14 | |
---|
15 | |
---|
16 | namespace xios { |
---|
17 | shared_ptr<CGenericAlgorithmTransformation> CScalarAlgorithmReduceScalar::create(bool isSource, CGrid* gridDst, CGrid* gridSrc, |
---|
18 | CTransformation<CScalar>* transformation, |
---|
19 | int elementPositionInGrid, |
---|
20 | std::map<int, int>& elementPositionInGridSrc2ScalarPosition, |
---|
21 | std::map<int, int>& elementPositionInGridSrc2AxisPosition, |
---|
22 | std::map<int, int>& elementPositionInGridSrc2DomainPosition, |
---|
23 | std::map<int, int>& elementPositionInGridDst2ScalarPosition, |
---|
24 | std::map<int, int>& elementPositionInGridDst2AxisPosition, |
---|
25 | std::map<int, int>& elementPositionInGridDst2DomainPosition) |
---|
26 | TRY |
---|
27 | { |
---|
28 | std::vector<CScalar*> scalarListDestP = gridDst->getScalars(); |
---|
29 | std::vector<CScalar*> scalarListSrcP = gridSrc->getScalars(); |
---|
30 | |
---|
31 | CReduceScalarToScalar* reduceScalar = dynamic_cast<CReduceScalarToScalar*> (transformation); |
---|
32 | int scalarDstIndex = elementPositionInGridDst2ScalarPosition[elementPositionInGrid]; |
---|
33 | int scalarSrcIndex = elementPositionInGridSrc2AxisPosition[elementPositionInGrid]; |
---|
34 | |
---|
35 | return make_shared<CScalarAlgorithmReduceScalar>(isSource, scalarListDestP[scalarDstIndex], scalarListSrcP[scalarSrcIndex], reduceScalar); |
---|
36 | } |
---|
37 | CATCH |
---|
38 | |
---|
39 | bool CScalarAlgorithmReduceScalar::dummyRegistered_ = CScalarAlgorithmReduceScalar::registerTrans(); |
---|
40 | bool CScalarAlgorithmReduceScalar::registerTrans() |
---|
41 | TRY |
---|
42 | { |
---|
43 | return CGridTransformationFactory<CScalar>::registerTransformation(TRANS_REDUCE_SCALAR_TO_SCALAR, create); |
---|
44 | } |
---|
45 | CATCH |
---|
46 | |
---|
47 | CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(bool isSource, CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo) |
---|
48 | : CAlgorithmTransformationReduce(isSource) |
---|
49 | TRY |
---|
50 | { |
---|
51 | scalarDestination->checkAttributes() ; |
---|
52 | if (algo->operation.isEmpty()) |
---|
53 | ERROR("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)", |
---|
54 | << "Operation must be defined." |
---|
55 | << "Scalar source " <<scalarSource->getId() << std::endl |
---|
56 | << "Scalar destination " << scalarDestination->getId()); |
---|
57 | StdString op; |
---|
58 | switch (algo->operation) |
---|
59 | { |
---|
60 | case CReduceScalarToScalar::operation_attr::sum: |
---|
61 | operator_ = EReduction::sum; |
---|
62 | break; |
---|
63 | case CReduceScalarToScalar::operation_attr::min: |
---|
64 | operator_ = EReduction::min; |
---|
65 | break; |
---|
66 | case CReduceScalarToScalar::operation_attr::max: |
---|
67 | operator_ = EReduction::max; |
---|
68 | break; |
---|
69 | case CReduceScalarToScalar::operation_attr::average: |
---|
70 | operator_ = EReduction::average; |
---|
71 | break; |
---|
72 | default: |
---|
73 | ERROR("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)", |
---|
74 | << "Operation is wrongly defined. Supported operations: sum, min, max, average." << std::endl |
---|
75 | << "Scalar source " <<scalarSource->getId() << std::endl |
---|
76 | << "Scalar destination " << scalarDestination->getId()); |
---|
77 | |
---|
78 | } |
---|
79 | |
---|
80 | auto& transMap = this->transformationMapping_; |
---|
81 | |
---|
82 | CArray<size_t,1> dstGlobalIndex ; |
---|
83 | scalarDestination->getLocalView(CElementView::WORKFLOW)->getGlobalIndexView(dstGlobalIndex) ; |
---|
84 | size_t nbIdx = dstGlobalIndex.numElements(); |
---|
85 | |
---|
86 | for (size_t idx = 0; idx < nbIdx; ++idx) |
---|
87 | { |
---|
88 | size_t globalIdx = dstGlobalIndex(idx); |
---|
89 | transMap[globalIdx].resize(1); |
---|
90 | transMap[globalIdx][0]=globalIdx ; |
---|
91 | } |
---|
92 | |
---|
93 | this->computeAlgorithm(scalarSource->getLocalView(CElementView::WORKFLOW), scalarDestination->getLocalView(CElementView::WORKFLOW)) ; |
---|
94 | } |
---|
95 | CATCH |
---|
96 | |
---|
97 | shared_ptr<CGridAlgorithm> CScalarAlgorithmReduceScalar::createGridAlgorithm(CGrid* gridSrc, CGrid* gridDst, int pos) |
---|
98 | { |
---|
99 | auto algo=make_shared<CGridAlgorithmReduce>(gridSrc, gridDst, pos, shared_from_this(), operator_) ; |
---|
100 | algo->computeAlgorithm(false) ; |
---|
101 | return algo ; |
---|
102 | } |
---|
103 | |
---|
104 | CScalarAlgorithmReduceScalar::~CScalarAlgorithmReduceScalar() |
---|
105 | TRY |
---|
106 | { |
---|
107 | } |
---|
108 | CATCH |
---|
109 | |
---|
110 | |
---|
111 | } |
---|