1 | /*! |
---|
2 | \file axis_algorithm_reduce_axis.cpp |
---|
3 | \author Ha NGUYEN |
---|
4 | \since 23 June 2016 |
---|
5 | \date 23 June 2016 |
---|
6 | |
---|
7 | \brief Algorithm for reduce a axis to an axis |
---|
8 | */ |
---|
9 | #include "axis_algorithm_reduce_axis.hpp" |
---|
10 | #include "reduce_axis_to_axis.hpp" |
---|
11 | #include "axis.hpp" |
---|
12 | #include "grid.hpp" |
---|
13 | #include "grid_transformation_factory_impl.hpp" |
---|
14 | |
---|
15 | namespace xios { |
---|
16 | CGenericAlgorithmTransformation* CAxisAlgorithmReduceAxis::create(CGrid* gridDst, CGrid* gridSrc, |
---|
17 | CTransformation<CAxis>* transformation, |
---|
18 | int elementPositionInGrid, |
---|
19 | std::map<int, int>& elementPositionInGridSrc2ScalarPosition, |
---|
20 | std::map<int, int>& elementPositionInGridSrc2AxisPosition, |
---|
21 | std::map<int, int>& elementPositionInGridSrc2DomainPosition, |
---|
22 | std::map<int, int>& elementPositionInGridDst2ScalarPosition, |
---|
23 | std::map<int, int>& elementPositionInGridDst2AxisPosition, |
---|
24 | std::map<int, int>& elementPositionInGridDst2DomainPosition) |
---|
25 | TRY |
---|
26 | { |
---|
27 | std::vector<CAxis*> axisListDestP = gridDst->getAxis(); |
---|
28 | std::vector<CAxis*> axisListSrcP = gridSrc->getAxis(); |
---|
29 | |
---|
30 | CReduceAxisToAxis* reduceAxis = dynamic_cast<CReduceAxisToAxis*> (transformation); |
---|
31 | int axisDstIndex = elementPositionInGridDst2AxisPosition[elementPositionInGrid]; |
---|
32 | int axisSrcIndex = elementPositionInGridSrc2AxisPosition[elementPositionInGrid]; |
---|
33 | |
---|
34 | return (new CAxisAlgorithmReduceAxis(axisListDestP[axisDstIndex], axisListSrcP[axisSrcIndex], reduceAxis)); |
---|
35 | } |
---|
36 | CATCH |
---|
37 | |
---|
38 | bool CAxisAlgorithmReduceAxis::registerTrans() |
---|
39 | TRY |
---|
40 | { |
---|
41 | CGridTransformationFactory<CAxis>::registerTransformation(TRANS_REDUCE_AXIS_TO_AXIS, create); |
---|
42 | } |
---|
43 | CATCH |
---|
44 | |
---|
45 | |
---|
46 | CAxisAlgorithmReduceAxis::CAxisAlgorithmReduceAxis(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToAxis* algo) |
---|
47 | : CAxisAlgorithmTransformation(axisDestination, axisSource), reduction_(0) |
---|
48 | TRY |
---|
49 | { |
---|
50 | eliminateRedondantSrc_= false ; |
---|
51 | algo->checkValid(axisDestination, axisSource); |
---|
52 | StdString op; |
---|
53 | switch (algo->operation) |
---|
54 | { |
---|
55 | case CReduceAxisToAxis::operation_attr::sum: |
---|
56 | op = "sum"; |
---|
57 | break; |
---|
58 | case CReduceAxisToAxis::operation_attr::min: |
---|
59 | op = "min"; |
---|
60 | break; |
---|
61 | case CReduceAxisToAxis::operation_attr::max: |
---|
62 | op = "max"; |
---|
63 | break; |
---|
64 | case CReduceAxisToAxis::operation_attr::average: |
---|
65 | op = "average"; |
---|
66 | break; |
---|
67 | default: |
---|
68 | ERROR("CAxisAlgorithmReduceAxis::CAxisAlgorithmReduceAxis(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToAxis* algo)", |
---|
69 | << "Operation is wrongly defined. Supported operations: sum, min, max, average." << std::endl |
---|
70 | << "Axis source " <<axisSource->getId() << std::endl |
---|
71 | << "Axis destination " << axisDestination->getId()); |
---|
72 | |
---|
73 | } |
---|
74 | if(CReductionAlgorithm::ReductionOperations_ptr == 0) |
---|
75 | { |
---|
76 | CReductionAlgorithm::initReductionOperation(); |
---|
77 | } |
---|
78 | |
---|
79 | reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations_ptr->at(op)); |
---|
80 | } |
---|
81 | CATCH |
---|
82 | |
---|
83 | void CAxisAlgorithmReduceAxis::apply(const std::vector<std::pair<int,double> >& localIndex, |
---|
84 | const double* dataInput, |
---|
85 | CArray<double,1>& dataOut, |
---|
86 | std::vector<bool>& flagInitial, |
---|
87 | bool ignoreMissingValue, bool firstPass) |
---|
88 | TRY |
---|
89 | { |
---|
90 | reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue, firstPass); |
---|
91 | } |
---|
92 | CATCH |
---|
93 | |
---|
94 | void CAxisAlgorithmReduceAxis::updateData(CArray<double,1>& dataOut) |
---|
95 | TRY |
---|
96 | { |
---|
97 | reduction_->updateData(dataOut); |
---|
98 | } |
---|
99 | CATCH |
---|
100 | |
---|
101 | CAxisAlgorithmReduceAxis::~CAxisAlgorithmReduceAxis() |
---|
102 | TRY |
---|
103 | { |
---|
104 | if (0 != reduction_) delete reduction_; |
---|
105 | } |
---|
106 | CATCH |
---|
107 | |
---|
108 | void CAxisAlgorithmReduceAxis::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs) |
---|
109 | TRY |
---|
110 | { |
---|
111 | this->transformationMapping_.resize(1); |
---|
112 | this->transformationWeight_.resize(1); |
---|
113 | |
---|
114 | TransformationIndexMap& transMap = this->transformationMapping_[0]; |
---|
115 | TransformationWeightMap& transWeight = this->transformationWeight_[0]; |
---|
116 | CArray<int,1>& axisDstIndex = axisDest_->index; |
---|
117 | int nbAxisIdx = axisDstIndex.numElements(); |
---|
118 | for (int idxAxis = 0; idxAxis < nbAxisIdx; ++idxAxis) |
---|
119 | { |
---|
120 | int globalAxisIdx = axisDstIndex(idxAxis); |
---|
121 | transMap[globalAxisIdx].resize(1); |
---|
122 | transWeight[globalAxisIdx].resize(1); |
---|
123 | transMap[globalAxisIdx][0]=globalAxisIdx ; |
---|
124 | transWeight[globalAxisIdx][0] = 1.0 ; |
---|
125 | } |
---|
126 | } |
---|
127 | CATCH |
---|
128 | |
---|
129 | } |
---|