1 | #include "domain_algorithm_extract.hpp" |
---|
2 | #include "extract_domain.hpp" |
---|
3 | #include "domain.hpp" |
---|
4 | #include "grid.hpp" |
---|
5 | #include "grid_transformation_factory_impl.hpp" |
---|
6 | #include "attribute_template.hpp" |
---|
7 | |
---|
8 | namespace xios { |
---|
9 | shared_ptr<CGenericAlgorithmTransformation> CDomainAlgorithmExtract::create(bool isSource, CGrid* gridDst, CGrid* gridSrc, |
---|
10 | CTransformation<CDomain>* transformation, |
---|
11 | int elementPositionInGrid, |
---|
12 | std::map<int, int>& elementPositionInGridSrc2ScalarPosition, |
---|
13 | std::map<int, int>& elementPositionInGridSrc2AxisPosition, |
---|
14 | std::map<int, int>& elementPositionInGridSrc2DomainPosition, |
---|
15 | std::map<int, int>& elementPositionInGridDst2ScalarPosition, |
---|
16 | std::map<int, int>& elementPositionInGridDst2AxisPosition, |
---|
17 | std::map<int, int>& elementPositionInGridDst2DomainPosition) |
---|
18 | TRY |
---|
19 | { |
---|
20 | std::vector<CDomain*> domainListDestP = gridDst->getDomains(); |
---|
21 | std::vector<CDomain*> domainListSrcP = gridSrc->getDomains(); |
---|
22 | |
---|
23 | CExtractDomain* extractDomain = dynamic_cast<CExtractDomain*> (transformation); |
---|
24 | int domainDstIndex = elementPositionInGridDst2DomainPosition[elementPositionInGrid]; |
---|
25 | int domainSrcIndex = elementPositionInGridSrc2DomainPosition[elementPositionInGrid]; |
---|
26 | |
---|
27 | return make_shared<CDomainAlgorithmExtract>(isSource, domainListDestP[domainDstIndex], domainListSrcP[domainSrcIndex], extractDomain); |
---|
28 | } |
---|
29 | CATCH |
---|
30 | |
---|
31 | bool CDomainAlgorithmExtract::dummyRegistered_ = CDomainAlgorithmExtract::registerTrans(); |
---|
32 | bool CDomainAlgorithmExtract::registerTrans() |
---|
33 | TRY |
---|
34 | { |
---|
35 | return CGridTransformationFactory<CDomain>::registerTransformation(TRANS_EXTRACT_DOMAIN, create); |
---|
36 | } |
---|
37 | CATCH |
---|
38 | |
---|
39 | CDomainAlgorithmExtract::CDomainAlgorithmExtract(bool isSource, CDomain* domainDestination, CDomain* domainSource, CExtractDomain* extractDomain) |
---|
40 | : CAlgorithmTransformationTransfer(isSource), domainSrc_(domainSource), domainDest_(domainDestination) |
---|
41 | TRY |
---|
42 | { |
---|
43 | // Reset geometrical attributes to avoid incompatible (user/domainSource) attributs |
---|
44 | // attributs will be defined using domainSource and/or transformation attributs |
---|
45 | domainDestination->type.reset(); |
---|
46 | domainDestination->ni_glo.reset(); |
---|
47 | domainDestination->nj_glo.reset(); |
---|
48 | |
---|
49 | domainDestination->i_index.reset(); // defined using domainSource->getLocalElement() |
---|
50 | domainDestination->j_index.reset(); // " |
---|
51 | domainDestination->ibegin.reset(); // will be computed in domainDestination->checkDomain() (from checkAttributes()) |
---|
52 | domainDestination->ni.reset(); // " |
---|
53 | domainDestination->jbegin.reset(); // " |
---|
54 | domainDestination->nj.reset(); // " |
---|
55 | |
---|
56 | domainDestination->mask_1d.reset(); // defined scanning domainSource->getFullView() & domainSource->getWorkflowView() differencies |
---|
57 | domainDestination->mask_2d.reset(); // in all case domainDestination->mask_1d used as reference |
---|
58 | |
---|
59 | // domainDestination->data_* attributes will be computed in : |
---|
60 | domainDestination->data_dim.reset(); // domainDestination->checkDomainData() (from checkAttributes()) |
---|
61 | domainDestination->data_ni.reset(); |
---|
62 | domainDestination->data_nj.reset(); |
---|
63 | domainDestination->data_ibegin.reset(); |
---|
64 | domainDestination->data_jbegin.reset(); |
---|
65 | domainDestination->data_i_index.reset(); // domainDestination->checkCompression() (from checkAttributes()) |
---|
66 | domainDestination->data_j_index.reset(); |
---|
67 | |
---|
68 | // Next attributes will be set using domainSource->attributes |
---|
69 | domainDestination->lonvalue_1d.reset(); |
---|
70 | domainDestination->latvalue_1d.reset(); |
---|
71 | domainDestination->lonvalue_2d.reset(); |
---|
72 | domainDestination->latvalue_2d.reset(); |
---|
73 | domainDestination->nvertex.reset(); |
---|
74 | domainDestination->bounds_lon_1d.reset(); |
---|
75 | domainDestination->bounds_lat_1d.reset(); |
---|
76 | domainDestination->bounds_lon_2d.reset(); |
---|
77 | domainDestination->bounds_lat_2d.reset(); |
---|
78 | domainDestination->area.reset(); |
---|
79 | domainDestination->radius.reset(); |
---|
80 | |
---|
81 | |
---|
82 | extractDomain->checkValid(domainSource); |
---|
83 | extractIBegin_ = extractDomain->ibegin.getValue(); |
---|
84 | extractJBegin_ = extractDomain->jbegin.getValue(); |
---|
85 | |
---|
86 | extractNi_ = extractDomain->ni.getValue(); |
---|
87 | extractNj_ = extractDomain->nj.getValue(); |
---|
88 | |
---|
89 | extractIEnd_ = extractIBegin_ + extractNi_ - 1; |
---|
90 | extractJEnd_ = extractJBegin_ + extractNj_ - 1; |
---|
91 | |
---|
92 | if (extractNi_ > domainSource->ni_glo.getValue()) |
---|
93 | { |
---|
94 | ERROR("CDomainAlgorithmExtract::CDomainAlgorithmExtract(CDomain* domainDestination, CDomain* domainSource, CExtractDomain* extractDomain)", |
---|
95 | << "Extract size is greater than size of domain source" |
---|
96 | << "Size ni_glo of domain source " <<domainSource->getId() << " is " << domainSource->ni_glo.getValue() << std::endl |
---|
97 | << "Extract size is " << extractNi_ ); |
---|
98 | } |
---|
99 | |
---|
100 | if (extractNj_ > domainSource->nj_glo.getValue()) |
---|
101 | { |
---|
102 | ERROR("CDomainAlgorithmExtract::CDomainAlgorithmExtract(CDomain* domainDestination, CDomain* domainSource, CExtractDomain* extractDomain)", |
---|
103 | << "Extract size is greater than size of domain source" |
---|
104 | << "Size nj_glo of domain source " <<domainSource->getId() << " is " << domainSource->nj_glo.getValue() << std::endl |
---|
105 | << "Extract size is " << extractNj_ ); |
---|
106 | } |
---|
107 | |
---|
108 | // Calculate the size of local domain |
---|
109 | int ind, indLocSrc, indLocDest, iIdxSrc, jIdxSrc, destIBegin = -1, destJBegin = -1, niDest = 0, njDest = 0 ; |
---|
110 | int indGloDest, indGloSrc, niGloSrc = domainSrc_->ni_glo, iSrc, jSrc; |
---|
111 | for (int j = 0; j < domainSrc_->nj.getValue(); j++) |
---|
112 | { |
---|
113 | for (int i = 0; i < domainSrc_->ni.getValue(); i++) |
---|
114 | { |
---|
115 | ind = j*domainSrc_->ni + i; |
---|
116 | iIdxSrc = domainSrc_->i_index(ind); |
---|
117 | if ((iIdxSrc >= extractIBegin_) && (iIdxSrc <= extractIEnd_)) |
---|
118 | { |
---|
119 | jIdxSrc = domainSrc_->j_index(ind); |
---|
120 | if ((jIdxSrc >= extractJBegin_) && (jIdxSrc <= extractJEnd_)) |
---|
121 | { |
---|
122 | if ((niDest == 0) && (njDest == 0)) |
---|
123 | { |
---|
124 | destIBegin = i; |
---|
125 | destJBegin = j; |
---|
126 | } |
---|
127 | if (i == destIBegin) ++njDest; |
---|
128 | } |
---|
129 | if (j == destJBegin) ++niDest; |
---|
130 | |
---|
131 | } |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | // Set attributes for this transformation |
---|
137 | domainDest_->type = domainSrc_ -> type ; |
---|
138 | domainDest_->ni_glo.setValue(extractNi_); |
---|
139 | domainDest_->nj_glo.setValue(extractNj_); |
---|
140 | domainDest_->i_index.resize(niDest*njDest); |
---|
141 | domainDest_->j_index.resize(niDest*njDest); |
---|
142 | |
---|
143 | if (!domainSrc_->nvertex.isEmpty()) domainDest_->nvertex = domainSrc_->nvertex ; |
---|
144 | |
---|
145 | // Resize lon/lat, bounds, area arrays to local domain dimensions |
---|
146 | if (!domainSrc_->lonvalue_1d.isEmpty()) |
---|
147 | { |
---|
148 | if (domainDest_->type == CDomain::type_attr::rectilinear) |
---|
149 | { |
---|
150 | domainDest_->lonvalue_1d.resize(niDest); |
---|
151 | domainDest_->latvalue_1d.resize(njDest); |
---|
152 | } |
---|
153 | else |
---|
154 | { |
---|
155 | domainDest_->lonvalue_1d.resize(niDest*njDest); |
---|
156 | domainDest_->latvalue_1d.resize(niDest*njDest); |
---|
157 | } |
---|
158 | } |
---|
159 | else if (!domainSrc_->lonvalue_2d.isEmpty()) |
---|
160 | { |
---|
161 | domainDest_->lonvalue_2d.resize(niDest,njDest); |
---|
162 | domainDest_->latvalue_2d.resize(niDest,njDest); |
---|
163 | } |
---|
164 | if (domainSrc_->hasBounds) |
---|
165 | { |
---|
166 | if (!domainSrc_->bounds_lon_2d.isEmpty()) |
---|
167 | { |
---|
168 | domainDest_->bounds_lon_2d.resize(domainDest_->nvertex, niDest, njDest); |
---|
169 | domainDest_->bounds_lat_2d.resize(domainDest_->nvertex, niDest, njDest); |
---|
170 | } |
---|
171 | else if (!domainSrc_->bounds_lon_1d.isEmpty()) |
---|
172 | { |
---|
173 | domainDest_->bounds_lon_1d.resize(domainDest_->nvertex, niDest); |
---|
174 | domainDest_->bounds_lat_1d.resize(domainDest_->nvertex, niDest); |
---|
175 | } |
---|
176 | } |
---|
177 | if (domainSrc_->hasArea) domainDest_->area.resize(niDest,njDest); |
---|
178 | |
---|
179 | // Set attributes required to define domainDestination->localElement_ and associated views, full and workflow) |
---|
180 | CArray<size_t,1> sourceGlobalIdx = domainSource->getLocalElement()->getGlobalIndex(); |
---|
181 | int indexSize = sourceGlobalIdx.numElements(); |
---|
182 | domainDest_->data_i_index.resize(niDest*njDest); |
---|
183 | domainDestination->data_i_index = -1; |
---|
184 | domainDest_->data_j_index.resize(niDest*njDest); |
---|
185 | domainDestination->data_j_index = 0; |
---|
186 | |
---|
187 | CArray<int,1> sourceWorkflowIdx = domainSource->getLocalView(CElementView::WORKFLOW)->getIndex(); |
---|
188 | int srcWorkflowSize = sourceWorkflowIdx.numElements(); |
---|
189 | |
---|
190 | int iIdxSrcMin = INT_MAX; |
---|
191 | int jIdxSrcMin = INT_MAX; |
---|
192 | int IdxMin = INT_MAX; |
---|
193 | for (int countSrc = 0; countSrc < indexSize ; ++countSrc) |
---|
194 | { |
---|
195 | if ( sourceGlobalIdx(countSrc)%domainSource->ni_glo < iIdxSrcMin ) |
---|
196 | iIdxSrcMin = sourceGlobalIdx(countSrc)%domainSource->ni_glo; |
---|
197 | if ( sourceGlobalIdx(countSrc)/domainSource->ni_glo < jIdxSrcMin ) |
---|
198 | jIdxSrcMin = sourceGlobalIdx(countSrc)/domainSource->ni_glo; |
---|
199 | if ( sourceGlobalIdx(countSrc) < IdxMin ) |
---|
200 | IdxMin = sourceGlobalIdx(countSrc); |
---|
201 | } |
---|
202 | int countDest(0); // increment of the position in destination domain |
---|
203 | for (int countSrc = 0; countSrc < indexSize ; ++countSrc) |
---|
204 | { |
---|
205 | int iIdxSrc = sourceGlobalIdx(countSrc)%domainSource->ni_glo; |
---|
206 | int jIdxSrc = sourceGlobalIdx(countSrc)/domainSource->ni_glo; |
---|
207 | // check that point countSrc concerned by extract |
---|
208 | if ( (iIdxSrc >= extractIBegin_) && (iIdxSrc <= extractIEnd_) |
---|
209 | && (jIdxSrc >= extractJBegin_) && (jIdxSrc <= extractJEnd_) ) |
---|
210 | { |
---|
211 | // if concerned, convert source the global indexation in the extracted frame |
---|
212 | domainDest_->i_index(countDest) = iIdxSrc-extractIBegin_; |
---|
213 | domainDest_->j_index(countDest) = jIdxSrc-extractJBegin_; |
---|
214 | |
---|
215 | // ------------------ define transformation only if in the WF ------------------ |
---|
216 | // countSrc+IdxMin is the global position (not index) considering the distributed memory |
---|
217 | // - can be compared to the workflow view |
---|
218 | int iIdxSrc2 = (countSrc%domainSource->ni)+(IdxMin)%domainSource->ni_glo; |
---|
219 | int jIdxSrc2 = (countSrc/domainSource->ni)+(IdxMin)/domainSource->ni_glo; |
---|
220 | int convert_locally_global_idx = (jIdxSrc2-jIdxSrcMin)*domainSource->ni + (iIdxSrc2-iIdxSrcMin) ; |
---|
221 | bool concerned_by_WF(false); |
---|
222 | for ( int i = 0 ; i<sourceWorkflowIdx.numElements() ; ++i ) |
---|
223 | { |
---|
224 | if (sourceWorkflowIdx(i)==convert_locally_global_idx) |
---|
225 | { |
---|
226 | concerned_by_WF = true; |
---|
227 | break; |
---|
228 | } |
---|
229 | } |
---|
230 | if (concerned_by_WF) |
---|
231 | { |
---|
232 | transformationMapping_[extractNi_*(jIdxSrc-extractJBegin_)+iIdxSrc-extractIBegin_]=sourceGlobalIdx(countSrc); |
---|
233 | domainDest_->data_i_index( countDest ) = countDest; |
---|
234 | } |
---|
235 | // ----------------------------------------------------------------------------- |
---|
236 | |
---|
237 | int iIdxDestLocal = countDest%niDest; |
---|
238 | int jIdxDestLocal = countDest/niDest; |
---|
239 | int iIdxSrcLocal = countSrc%domainSource->ni; |
---|
240 | int jIdxSrcLocal = countSrc/domainSource->ni; |
---|
241 | |
---|
242 | // area |
---|
243 | if (!domainSrc_->area.isEmpty()) |
---|
244 | { |
---|
245 | domainDest_->area(iIdxDestLocal,jIdxDestLocal) = domainSrc_->area(iIdxSrcLocal,jIdxSrcLocal); |
---|
246 | } |
---|
247 | |
---|
248 | // bounds |
---|
249 | if (!domainDest_->bounds_lon_1d.isEmpty()) |
---|
250 | { |
---|
251 | for (int n = 0; n < domainSrc_->nvertex; ++n) |
---|
252 | { |
---|
253 | domainDest_->bounds_lon_1d(n, countDest) = domainSrc_->bounds_lon_1d(n,countSrc); |
---|
254 | domainDest_->bounds_lat_1d(n, countDest) = domainSrc_->bounds_lat_1d(n,countSrc); |
---|
255 | } |
---|
256 | } |
---|
257 | else if (!domainDest_->bounds_lon_2d.isEmpty()) |
---|
258 | { |
---|
259 | for (int n = 0; n < domainSrc_->nvertex; ++n) |
---|
260 | { |
---|
261 | domainDest_->bounds_lon_2d(n, iIdxDestLocal, jIdxDestLocal) = domainSrc_->bounds_lon_2d(n, iIdxSrcLocal, jIdxSrcLocal); |
---|
262 | domainDest_->bounds_lat_2d(n, iIdxDestLocal, jIdxDestLocal) = domainSrc_->bounds_lat_2d(n, iIdxSrcLocal, jIdxSrcLocal); |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | // lon/lat |
---|
267 | if (!domainDest_->lonvalue_1d.isEmpty()) |
---|
268 | { |
---|
269 | if (domainDest_->type == CDomain::type_attr::rectilinear) |
---|
270 | { |
---|
271 | // i : scan nbr of points in src |
---|
272 | domainDest_->lonvalue_1d(iIdxDestLocal) = domainSrc_->lonvalue_1d(iIdxSrcLocal); |
---|
273 | domainDest_->latvalue_1d(jIdxDestLocal) = domainSrc_->latvalue_1d(jIdxSrcLocal); |
---|
274 | } |
---|
275 | else |
---|
276 | { |
---|
277 | domainDest_->lonvalue_1d(countDest) = domainSrc_->lonvalue_1d(countSrc); |
---|
278 | domainDest_->latvalue_1d(countDest) = domainSrc_->latvalue_1d(countSrc); |
---|
279 | } |
---|
280 | } |
---|
281 | else if (!domainDest_->lonvalue_2d.isEmpty()) |
---|
282 | { |
---|
283 | domainDest_->lonvalue_2d(iIdxDestLocal, jIdxDestLocal) = domainSrc_->lonvalue_2d(iIdxSrcLocal,jIdxSrcLocal); |
---|
284 | domainDest_->latvalue_2d(iIdxDestLocal, jIdxDestLocal) = domainSrc_->latvalue_2d(iIdxSrcLocal,jIdxSrcLocal); |
---|
285 | } |
---|
286 | |
---|
287 | // if point i has been identified as extracted, increment position in destination domain for the next point |
---|
288 | countDest++; |
---|
289 | } |
---|
290 | |
---|
291 | } |
---|
292 | |
---|
293 | domainDestination->checkAttributes() ; |
---|
294 | this->computeAlgorithm(domainSource->getLocalView(CElementView::WORKFLOW), domainDestination->getLocalView(CElementView::WORKFLOW)) ; |
---|
295 | } |
---|
296 | CATCH |
---|
297 | |
---|
298 | |
---|
299 | } |
---|