1 | /*! |
---|
2 | \file axis_algorithm_reduce_domain.cpp |
---|
3 | \author Ha NGUYEN |
---|
4 | \since 23 June 2016 |
---|
5 | \date 23 June 2016 |
---|
6 | |
---|
7 | \brief Algorithm for reduce a domain to an axis |
---|
8 | */ |
---|
9 | #include "axis_algorithm_reduce_domain.hpp" |
---|
10 | #include "reduce_domain_to_axis.hpp" |
---|
11 | #include "axis.hpp" |
---|
12 | #include "domain.hpp" |
---|
13 | #include "grid.hpp" |
---|
14 | #include "grid_transformation_factory_impl.hpp" |
---|
15 | |
---|
16 | |
---|
17 | namespace xios { |
---|
18 | CGenericAlgorithmTransformation* CAxisAlgorithmReduceDomain::create(CGrid* gridDst, CGrid* gridSrc, |
---|
19 | CTransformation<CAxis>* transformation, |
---|
20 | int elementPositionInGrid, |
---|
21 | std::map<int, int>& elementPositionInGridSrc2ScalarPosition, |
---|
22 | std::map<int, int>& elementPositionInGridSrc2AxisPosition, |
---|
23 | std::map<int, int>& elementPositionInGridSrc2DomainPosition, |
---|
24 | std::map<int, int>& elementPositionInGridDst2ScalarPosition, |
---|
25 | std::map<int, int>& elementPositionInGridDst2AxisPosition, |
---|
26 | std::map<int, int>& elementPositionInGridDst2DomainPosition) |
---|
27 | TRY |
---|
28 | { |
---|
29 | std::vector<CAxis*> axisListDestP = gridDst->getAxis(); |
---|
30 | std::vector<CDomain*> domainListSrcP = gridSrc->getDomains(); |
---|
31 | |
---|
32 | CReduceDomainToAxis* reduceDomain = dynamic_cast<CReduceDomainToAxis*> (transformation); |
---|
33 | int axisDstIndex = elementPositionInGridDst2AxisPosition[elementPositionInGrid]; |
---|
34 | int domainSrcIndex = elementPositionInGridSrc2DomainPosition[elementPositionInGrid]; |
---|
35 | |
---|
36 | return (new CAxisAlgorithmReduceDomain(axisListDestP[axisDstIndex], domainListSrcP[domainSrcIndex], reduceDomain)); |
---|
37 | } |
---|
38 | CATCH |
---|
39 | |
---|
40 | bool CAxisAlgorithmReduceDomain::registerTrans() |
---|
41 | TRY |
---|
42 | { |
---|
43 | CGridTransformationFactory<CAxis>::registerTransformation(TRANS_REDUCE_DOMAIN_TO_AXIS, create); |
---|
44 | } |
---|
45 | CATCH |
---|
46 | |
---|
47 | |
---|
48 | CAxisAlgorithmReduceDomain::CAxisAlgorithmReduceDomain(CAxis* axisDestination, CDomain* domainSource, CReduceDomainToAxis* algo) |
---|
49 | : CAxisAlgorithmTransformation(axisDestination, domainSource), reduction_(0) |
---|
50 | TRY |
---|
51 | { |
---|
52 | algo->checkValid(axisDestination, domainSource); |
---|
53 | StdString op; |
---|
54 | switch (algo->operation) |
---|
55 | { |
---|
56 | case CReduceDomainToAxis::operation_attr::sum: |
---|
57 | op = "sum"; |
---|
58 | break; |
---|
59 | case CReduceDomainToAxis::operation_attr::min: |
---|
60 | op = "min"; |
---|
61 | break; |
---|
62 | case CReduceDomainToAxis::operation_attr::max: |
---|
63 | op = "max"; |
---|
64 | break; |
---|
65 | case CReduceDomainToAxis::operation_attr::average: |
---|
66 | op = "average"; |
---|
67 | break; |
---|
68 | default: |
---|
69 | ERROR("CAxisAlgorithmReduceDomain::CAxisAlgorithmReduceDomain(CAxis* axisDestination, CDomain* domainSource, CReduceDomainToAxis* algo)", |
---|
70 | << "Operation is wrongly defined. Supported operations: sum, min, max, average." << std::endl |
---|
71 | << "Domain source " <<domainSource->getId() << std::endl |
---|
72 | << "Axis destination " << axisDestination->getId()); |
---|
73 | |
---|
74 | } |
---|
75 | |
---|
76 | dir_ = (CReduceDomainToAxis::direction_attr::iDir == algo->direction) ? iDir : jDir; |
---|
77 | if(CReductionAlgorithm::ReductionOperations_ptr == 0) |
---|
78 | { |
---|
79 | CReductionAlgorithm::initReductionOperation(); |
---|
80 | } |
---|
81 | reduction_ = CReductionAlgorithm::createOperation(CReductionAlgorithm::ReductionOperations_ptr->at(op)); |
---|
82 | local = algo->local ; |
---|
83 | } |
---|
84 | CATCH |
---|
85 | |
---|
86 | void CAxisAlgorithmReduceDomain::apply(const std::vector<std::pair<int,double> >& localIndex, |
---|
87 | const double* dataInput, |
---|
88 | CArray<double,1>& dataOut, |
---|
89 | std::vector<bool>& flagInitial, |
---|
90 | bool ignoreMissingValue, bool firstPass) |
---|
91 | TRY |
---|
92 | { |
---|
93 | reduction_->apply(localIndex, dataInput, dataOut, flagInitial, ignoreMissingValue, firstPass); |
---|
94 | } |
---|
95 | CATCH |
---|
96 | |
---|
97 | void CAxisAlgorithmReduceDomain::updateData(CArray<double,1>& dataOut) |
---|
98 | TRY |
---|
99 | { |
---|
100 | reduction_->updateData(dataOut); |
---|
101 | } |
---|
102 | CATCH |
---|
103 | |
---|
104 | CAxisAlgorithmReduceDomain::~CAxisAlgorithmReduceDomain() |
---|
105 | TRY |
---|
106 | { |
---|
107 | if (0 != reduction_) delete reduction_; |
---|
108 | } |
---|
109 | CATCH |
---|
110 | |
---|
111 | void CAxisAlgorithmReduceDomain::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs) |
---|
112 | TRY |
---|
113 | { |
---|
114 | this->transformationMapping_.resize(1); |
---|
115 | this->transformationWeight_.resize(1); |
---|
116 | |
---|
117 | TransformationIndexMap& transMap = this->transformationMapping_[0]; |
---|
118 | TransformationWeightMap& transWeight = this->transformationWeight_[0]; |
---|
119 | |
---|
120 | CArray<int,1>& axisDstIndex = axisDest_->index; |
---|
121 | int ni_glo = domainSrc_->ni_glo, nj_glo = domainSrc_->nj_glo; |
---|
122 | if (iDir == dir_) |
---|
123 | { |
---|
124 | if (local) |
---|
125 | { |
---|
126 | const CArray<int, 1>& i_index = domainSrc_-> i_index.getValue() ; |
---|
127 | const CArray<int, 1>& j_index = domainSrc_-> j_index.getValue() ; |
---|
128 | const CArray<bool,1>& localMask = domainSrc_-> localMask ; |
---|
129 | int nbDomainIdx = i_index.numElements(); |
---|
130 | |
---|
131 | for (int idxDomain = 0; idxDomain < nbDomainIdx; ++idxDomain) |
---|
132 | { |
---|
133 | if (localMask(idxDomain)) |
---|
134 | { |
---|
135 | transMap[j_index(idxDomain)].push_back(j_index(idxDomain)* ni_glo + i_index(idxDomain)); |
---|
136 | transWeight[j_index(idxDomain)].push_back(1.0) ; |
---|
137 | } |
---|
138 | } |
---|
139 | } |
---|
140 | else |
---|
141 | { |
---|
142 | int nbAxisIdx = axisDstIndex.numElements(); |
---|
143 | for (int idxAxis = 0; idxAxis < nbAxisIdx; ++idxAxis) |
---|
144 | { |
---|
145 | int globalAxisIdx = axisDstIndex(idxAxis); |
---|
146 | transMap[globalAxisIdx].resize(ni_glo); |
---|
147 | transWeight[globalAxisIdx].resize(ni_glo); |
---|
148 | for (int idx = 0; idx < ni_glo; ++idx) |
---|
149 | { |
---|
150 | transMap[globalAxisIdx][idx] = globalAxisIdx * ni_glo + idx; |
---|
151 | transWeight[globalAxisIdx][idx] = 1.0; |
---|
152 | } |
---|
153 | } |
---|
154 | } |
---|
155 | } |
---|
156 | else if (jDir == dir_) |
---|
157 | { |
---|
158 | int nbAxisIdx = axisDstIndex.numElements(); |
---|
159 | if (local) |
---|
160 | { |
---|
161 | const CArray<int, 1>& i_index = domainSrc_-> i_index.getValue() ; |
---|
162 | const CArray<int, 1>& j_index = domainSrc_-> j_index.getValue() ; |
---|
163 | const CArray<bool,1>& localMask = domainSrc_-> localMask ; |
---|
164 | int nbDomainIdx = i_index.numElements(); |
---|
165 | |
---|
166 | for (int idxDomain = 0; idxDomain < nbDomainIdx; ++idxDomain) |
---|
167 | { |
---|
168 | if (localMask(idxDomain)) |
---|
169 | { |
---|
170 | transMap[i_index(idxDomain)].push_back(j_index(idxDomain)* ni_glo + i_index(idxDomain)); |
---|
171 | transWeight[i_index(idxDomain)].push_back(1.0) ; |
---|
172 | } |
---|
173 | } |
---|
174 | } |
---|
175 | else |
---|
176 | { |
---|
177 | for (int idxAxis = 0; idxAxis < nbAxisIdx; ++idxAxis) |
---|
178 | { |
---|
179 | int globalAxisIdx = axisDstIndex(idxAxis); |
---|
180 | transMap[globalAxisIdx].resize(nj_glo); |
---|
181 | transWeight[globalAxisIdx].resize(nj_glo); |
---|
182 | for (int idx = 0; idx < nj_glo; ++idx) |
---|
183 | { |
---|
184 | transMap[globalAxisIdx][idx] = globalAxisIdx + ni_glo*idx; |
---|
185 | transWeight[globalAxisIdx][idx] = 1.0; |
---|
186 | } |
---|
187 | } |
---|
188 | } |
---|
189 | } |
---|
190 | else |
---|
191 | {} |
---|
192 | } |
---|
193 | CATCH |
---|
194 | |
---|
195 | } |
---|