1 | /*! |
---|
2 | \file axis_algorithm_interpolate.cpp |
---|
3 | \author Ha NGUYEN |
---|
4 | \since 23 June 2015 |
---|
5 | \date 02 Jul 2015 |
---|
6 | |
---|
7 | \brief Algorithm for interpolation on an axis. |
---|
8 | */ |
---|
9 | #include "axis_algorithm_interpolate.hpp" |
---|
10 | #include "axis.hpp" |
---|
11 | #include "interpolate_axis.hpp" |
---|
12 | #include <algorithm> |
---|
13 | #include "context.hpp" |
---|
14 | #include "context_client.hpp" |
---|
15 | #include "utils.hpp" |
---|
16 | #include "grid.hpp" |
---|
17 | #include "grid_transformation_factory_impl.hpp" |
---|
18 | #include "distribution_client.hpp" |
---|
19 | #include "timer.hpp" |
---|
20 | |
---|
21 | namespace xios { |
---|
22 | CGenericAlgorithmTransformation* CAxisAlgorithmInterpolate::create(CGrid* gridDst, CGrid* gridSrc, |
---|
23 | CTransformation<CAxis>* transformation, |
---|
24 | int elementPositionInGrid, |
---|
25 | std::map<int, int>& elementPositionInGridSrc2ScalarPosition, |
---|
26 | std::map<int, int>& elementPositionInGridSrc2AxisPosition, |
---|
27 | std::map<int, int>& elementPositionInGridSrc2DomainPosition, |
---|
28 | std::map<int, int>& elementPositionInGridDst2ScalarPosition, |
---|
29 | std::map<int, int>& elementPositionInGridDst2AxisPosition, |
---|
30 | std::map<int, int>& elementPositionInGridDst2DomainPosition) |
---|
31 | TRY |
---|
32 | { |
---|
33 | std::vector<CAxis*> axisListDestP = gridDst->getAxis(); |
---|
34 | std::vector<CAxis*> axisListSrcP = gridSrc->getAxis(); |
---|
35 | |
---|
36 | CInterpolateAxis* interpolateAxis = dynamic_cast<CInterpolateAxis*> (transformation); |
---|
37 | int axisDstIndex = elementPositionInGridDst2AxisPosition[elementPositionInGrid]; |
---|
38 | int axisSrcIndex = elementPositionInGridSrc2AxisPosition[elementPositionInGrid]; |
---|
39 | |
---|
40 | return (new CAxisAlgorithmInterpolate(axisListDestP[axisDstIndex], axisListSrcP[axisSrcIndex], interpolateAxis)); |
---|
41 | } |
---|
42 | CATCH |
---|
43 | |
---|
44 | bool CAxisAlgorithmInterpolate::registerTrans() |
---|
45 | TRY |
---|
46 | { |
---|
47 | CGridTransformationFactory<CAxis>::registerTransformation(TRANS_INTERPOLATE_AXIS, create); |
---|
48 | } |
---|
49 | CATCH |
---|
50 | |
---|
51 | CAxisAlgorithmInterpolate::CAxisAlgorithmInterpolate(CAxis* axisDestination, CAxis* axisSource, CInterpolateAxis* interpAxis) |
---|
52 | : CAxisAlgorithmTransformation(axisDestination, axisSource), coordinate_(), transPosition_() |
---|
53 | TRY |
---|
54 | { |
---|
55 | interpAxis->checkValid(axisSource); |
---|
56 | order_ = interpAxis->order.getValue(); |
---|
57 | if (!interpAxis->coordinate.isEmpty()) |
---|
58 | { |
---|
59 | coordinate_ = interpAxis->coordinate.getValue(); |
---|
60 | this->idAuxInputs_.resize(1); |
---|
61 | this->idAuxInputs_[0] = coordinate_; |
---|
62 | } |
---|
63 | } |
---|
64 | CATCH |
---|
65 | |
---|
66 | /*! |
---|
67 | Compute the index mapping between axis on grid source and one on grid destination |
---|
68 | */ |
---|
69 | void CAxisAlgorithmInterpolate::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs) |
---|
70 | TRY |
---|
71 | { |
---|
72 | CTimer::get("CAxisAlgorithmInterpolate::computeIndexSourceMapping_").resume() ; |
---|
73 | CContext* context = CContext::getCurrent(); |
---|
74 | CContextClient* client=context->client; |
---|
75 | int nbClient = client->clientSize; |
---|
76 | CArray<bool,1>& axisMask = axisSrc_->mask; |
---|
77 | int srcSize = axisSrc_->n_glo.getValue(); |
---|
78 | std::vector<CArray<double,1> > vecAxisValue; |
---|
79 | |
---|
80 | // Fill in axis value from coordinate |
---|
81 | fillInAxisValue(vecAxisValue, dataAuxInputs); |
---|
82 | std::vector<double> valueSrc(srcSize); |
---|
83 | std::vector<double> recvBuff(srcSize); |
---|
84 | std::vector<int> indexVec(srcSize); |
---|
85 | |
---|
86 | for (int idx = 0; idx < vecAxisValue.size(); ++idx) |
---|
87 | { |
---|
88 | CArray<double,1>& axisValue = vecAxisValue[idx]; |
---|
89 | retrieveAllAxisValue(axisValue, axisMask, recvBuff, indexVec); |
---|
90 | XIOSAlgorithms::sortWithIndex<double, CVectorStorage>(recvBuff, indexVec); |
---|
91 | for (int i = 0; i < srcSize; ++i) valueSrc[i] = recvBuff[indexVec[i]]; |
---|
92 | computeInterpolantPoint(valueSrc, indexVec, idx); |
---|
93 | } |
---|
94 | CTimer::get("CAxisAlgorithmInterpolate::computeIndexSourceMapping_").suspend() ; |
---|
95 | } |
---|
96 | CATCH |
---|
97 | |
---|
98 | /*! |
---|
99 | Compute the interpolant points |
---|
100 | Assume that we have all value of axis source, with these values, need to calculate weight (coeff) of Lagrange polynomial |
---|
101 | \param [in] axisValue all value of axis source |
---|
102 | \param [in] tranPos position of axis on a domain |
---|
103 | */ |
---|
104 | void CAxisAlgorithmInterpolate::computeInterpolantPoint(const std::vector<double>& axisValue, |
---|
105 | const std::vector<int>& indexVec, |
---|
106 | int transPos) |
---|
107 | TRY |
---|
108 | { |
---|
109 | std::vector<double>::const_iterator itb = axisValue.begin(), ite = axisValue.end(); |
---|
110 | std::vector<double>::const_iterator itLowerBound, itUpperBound, it, iteRange, itfirst, itsecond; |
---|
111 | const double sfmax = NumTraits<double>::sfmax(); |
---|
112 | const double precision = NumTraits<double>::dummy_precision(); |
---|
113 | |
---|
114 | int ibegin = axisDest_->begin.getValue(); |
---|
115 | CArray<double,1>& axisDestValue = axisDest_->value; |
---|
116 | int numValue = axisDestValue.numElements(); |
---|
117 | std::map<int, std::vector<std::pair<int,double> > > interpolatingIndexValues; |
---|
118 | |
---|
119 | for (int idx = 0; idx < numValue; ++idx) |
---|
120 | { |
---|
121 | bool outOfRange = false; |
---|
122 | double destValue = axisDestValue(idx); |
---|
123 | if (destValue < *itb) outOfRange = true; |
---|
124 | |
---|
125 | itLowerBound = std::lower_bound(itb, ite, destValue); |
---|
126 | itUpperBound = std::upper_bound(itb, ite, destValue); |
---|
127 | if ((ite != itUpperBound) && (sfmax == *itUpperBound)) itUpperBound = ite; |
---|
128 | |
---|
129 | if ((ite == itLowerBound) || (ite == itUpperBound)) outOfRange = true; |
---|
130 | |
---|
131 | // We don't do extrapolation FOR NOW, maybe in the future |
---|
132 | if (!outOfRange) |
---|
133 | { |
---|
134 | if ((itLowerBound == itUpperBound) && (itb != itLowerBound)) --itLowerBound; |
---|
135 | double distanceToLower = destValue - *itLowerBound; |
---|
136 | double distanceToUpper = *itUpperBound - destValue; |
---|
137 | int order = (order_ + 1) - 2; |
---|
138 | bool down = (distanceToLower < distanceToUpper) ? true : false; |
---|
139 | for (int k = 0; k < order; ++k) |
---|
140 | { |
---|
141 | if ((itb != itLowerBound) && down) |
---|
142 | { |
---|
143 | --itLowerBound; |
---|
144 | distanceToLower = destValue - *itLowerBound; |
---|
145 | down = (distanceToLower < distanceToUpper) ? true : false; |
---|
146 | continue; |
---|
147 | } |
---|
148 | if ((ite != itUpperBound) && (sfmax != *itUpperBound)) |
---|
149 | { |
---|
150 | ++itUpperBound; |
---|
151 | distanceToUpper = *itUpperBound - destValue; |
---|
152 | down = (distanceToLower < distanceToUpper) ? true : false; |
---|
153 | |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | iteRange = (ite == itUpperBound) ? itUpperBound : itUpperBound + 1; |
---|
158 | itsecond = it = itLowerBound; ++itsecond; |
---|
159 | while (it < iteRange) |
---|
160 | { |
---|
161 | while ( (itsecond < ite) && ((*itsecond -*it) < precision) ) |
---|
162 | { ++itsecond; ++it; } |
---|
163 | int index = std::distance(itb, it); |
---|
164 | interpolatingIndexValues[idx+ibegin].push_back(make_pair(indexVec[index],*it)); |
---|
165 | ++it; ++itsecond; |
---|
166 | } |
---|
167 | |
---|
168 | } |
---|
169 | } |
---|
170 | computeWeightedValueAndMapping(interpolatingIndexValues, transPos); |
---|
171 | } |
---|
172 | CATCH |
---|
173 | |
---|
174 | /*! |
---|
175 | Compute weight (coeff) of Lagrange's polynomial |
---|
176 | \param [in] interpolatingIndexValues the necessary axis value to calculate the coeffs |
---|
177 | */ |
---|
178 | void CAxisAlgorithmInterpolate::computeWeightedValueAndMapping(const std::map<int, std::vector<std::pair<int,double> > >& interpolatingIndexValues, int transPos) |
---|
179 | TRY |
---|
180 | { |
---|
181 | TransformationIndexMap& transMap = this->transformationMapping_[transPos]; |
---|
182 | TransformationWeightMap& transWeight = this->transformationWeight_[transPos]; |
---|
183 | std::map<int, std::vector<std::pair<int,double> > >::const_iterator itb = interpolatingIndexValues.begin(), it, |
---|
184 | ite = interpolatingIndexValues.end(); |
---|
185 | int ibegin = axisDest_->begin.getValue(); |
---|
186 | for (it = itb; it != ite; ++it) |
---|
187 | { |
---|
188 | int globalIndexDest = it->first; |
---|
189 | double localValue = axisDest_->value(globalIndexDest - ibegin); |
---|
190 | const std::vector<std::pair<int,double> >& interpVal = it->second; |
---|
191 | int interpSize = interpVal.size(); |
---|
192 | transMap[globalIndexDest].resize(interpSize); |
---|
193 | transWeight[globalIndexDest].resize(interpSize); |
---|
194 | for (int idx = 0; idx < interpSize; ++idx) |
---|
195 | { |
---|
196 | int index = interpVal[idx].first; |
---|
197 | double weight = 1.0; |
---|
198 | |
---|
199 | for (int k = 0; k < interpSize; ++k) |
---|
200 | { |
---|
201 | if (k == idx) continue; |
---|
202 | weight *= (localValue - interpVal[k].second); |
---|
203 | weight /= (interpVal[idx].second - interpVal[k].second); |
---|
204 | } |
---|
205 | transMap[globalIndexDest][idx] = index; |
---|
206 | transWeight[globalIndexDest][idx] = weight; |
---|
207 | if (!transPosition_.empty()) |
---|
208 | { |
---|
209 | (this->transformationPosition_[transPos])[globalIndexDest] = transPosition_[transPos]; |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|
213 | if (!transPosition_.empty() && this->transformationPosition_[transPos].empty()) |
---|
214 | (this->transformationPosition_[transPos])[0] = transPosition_[transPos]; |
---|
215 | |
---|
216 | } |
---|
217 | CATCH |
---|
218 | |
---|
219 | /*! |
---|
220 | Each client retrieves all values of an axis |
---|
221 | \param [in/out] recvBuff buffer for receiving values (already allocated) |
---|
222 | \param [in/out] indexVec mapping between values and global index of axis |
---|
223 | */ |
---|
224 | void CAxisAlgorithmInterpolate::retrieveAllAxisValue(const CArray<double,1>& axisValue, const CArray<bool,1>& axisMask, |
---|
225 | std::vector<double>& recvBuff, std::vector<int>& indexVec) |
---|
226 | TRY |
---|
227 | { |
---|
228 | CContext* context = CContext::getCurrent(); |
---|
229 | CContextClient* client=context->client; |
---|
230 | int nbClient = client->clientSize; |
---|
231 | |
---|
232 | int srcSize = axisSrc_->n_glo.getValue(); |
---|
233 | int numValue = axisValue.numElements(); |
---|
234 | |
---|
235 | if (srcSize == numValue) // Only one client or axis not distributed |
---|
236 | { |
---|
237 | for (int idx = 0; idx < srcSize; ++idx) |
---|
238 | { |
---|
239 | if (axisMask(idx)) |
---|
240 | { |
---|
241 | recvBuff[idx] = axisValue(idx); |
---|
242 | indexVec[idx] = idx; |
---|
243 | } |
---|
244 | else |
---|
245 | { |
---|
246 | recvBuff[idx] = NumTraits<double>::sfmax(); |
---|
247 | indexVec[idx] = -1; |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | } |
---|
252 | else // Axis distributed |
---|
253 | { |
---|
254 | double* sendValueBuff = new double [numValue]; |
---|
255 | int* sendIndexBuff = new int [numValue]; |
---|
256 | int* recvIndexBuff = new int [srcSize]; |
---|
257 | |
---|
258 | int ibegin = axisSrc_->begin.getValue(); |
---|
259 | for (int idx = 0; idx < numValue; ++idx) |
---|
260 | { |
---|
261 | if (axisMask(idx)) |
---|
262 | { |
---|
263 | sendValueBuff[idx] = axisValue(idx); |
---|
264 | sendIndexBuff[idx] = idx + ibegin; |
---|
265 | } |
---|
266 | else |
---|
267 | { |
---|
268 | sendValueBuff[idx] = NumTraits<double>::sfmax(); |
---|
269 | sendIndexBuff[idx] = -1; |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | int* recvCount=new int[nbClient]; |
---|
274 | MPI_Allgather(&numValue,1,MPI_INT,recvCount,1,MPI_INT,client->intraComm); |
---|
275 | |
---|
276 | int* displ=new int[nbClient]; |
---|
277 | displ[0]=0 ; |
---|
278 | for(int n=1;n<nbClient;n++) displ[n]=displ[n-1]+recvCount[n-1]; |
---|
279 | |
---|
280 | // Each client have enough global info of axis |
---|
281 | MPI_Allgatherv(sendIndexBuff,numValue,MPI_INT,recvIndexBuff,recvCount,displ,MPI_INT,client->intraComm); |
---|
282 | MPI_Allgatherv(sendValueBuff,numValue,MPI_DOUBLE,&(recvBuff[0]),recvCount,displ,MPI_DOUBLE,client->intraComm); |
---|
283 | |
---|
284 | for (int idx = 0; idx < srcSize; ++idx) |
---|
285 | { |
---|
286 | indexVec[idx] = recvIndexBuff[idx]; |
---|
287 | } |
---|
288 | |
---|
289 | delete [] displ; |
---|
290 | delete [] recvCount; |
---|
291 | delete [] recvIndexBuff; |
---|
292 | delete [] sendIndexBuff; |
---|
293 | delete [] sendValueBuff; |
---|
294 | } |
---|
295 | } |
---|
296 | CATCH |
---|
297 | |
---|
298 | /*! |
---|
299 | Fill in axis value dynamically from a field whose grid is composed of a domain and an axis |
---|
300 | \param [in/out] vecAxisValue vector axis value filled in from input field |
---|
301 | */ |
---|
302 | void CAxisAlgorithmInterpolate::fillInAxisValue(std::vector<CArray<double,1> >& vecAxisValue, |
---|
303 | const std::vector<CArray<double,1>* >& dataAuxInputs) |
---|
304 | TRY |
---|
305 | { |
---|
306 | if (coordinate_.empty()) |
---|
307 | { |
---|
308 | vecAxisValue.resize(1); |
---|
309 | vecAxisValue[0].resize(axisSrc_->value.numElements()); |
---|
310 | vecAxisValue[0] = axisSrc_->value; |
---|
311 | this->transformationMapping_.resize(1); |
---|
312 | this->transformationWeight_.resize(1); |
---|
313 | } |
---|
314 | else |
---|
315 | { |
---|
316 | CField* field = CField::get(coordinate_); |
---|
317 | CGrid* grid = field->grid; |
---|
318 | |
---|
319 | std::vector<CDomain*> domListP = grid->getDomains(); |
---|
320 | std::vector<CAxis*> axisListP = grid->getAxis(); |
---|
321 | if (domListP.empty() || axisListP.empty() || (1 < domListP.size()) || (1 < axisListP.size())) |
---|
322 | ERROR("CAxisAlgorithmInterpolate::fillInAxisValue(std::vector<CArray<double,1> >& vecAxisValue)", |
---|
323 | << "XIOS only supports dynamic interpolation with coordinate (field) associated with grid composed of a domain and an axis" |
---|
324 | << "Coordinate (field) id = " <<field->getId() << std::endl |
---|
325 | << "Associated grid id = " << grid->getId()); |
---|
326 | |
---|
327 | CDomain* dom = domListP[0]; |
---|
328 | size_t vecAxisValueSize = dom->i_index.numElements(); |
---|
329 | size_t vecAxisValueSizeWithMask = 0; |
---|
330 | for (size_t idx = 0; idx < vecAxisValueSize; ++idx) |
---|
331 | { |
---|
332 | if (dom->domainMask(idx)) ++vecAxisValueSizeWithMask; |
---|
333 | } |
---|
334 | |
---|
335 | int niGlobDom = dom->ni_glo.getValue(); |
---|
336 | vecAxisValue.resize(vecAxisValueSizeWithMask); |
---|
337 | if (transPosition_.empty()) |
---|
338 | { |
---|
339 | size_t indexMask = 0; |
---|
340 | transPosition_.resize(vecAxisValueSizeWithMask); |
---|
341 | for (size_t idx = 0; idx < vecAxisValueSize; ++idx) |
---|
342 | { |
---|
343 | if (dom->domainMask(idx)) |
---|
344 | { |
---|
345 | transPosition_[indexMask].resize(1); |
---|
346 | transPosition_[indexMask][0] = (dom->i_index)(idx) + niGlobDom * (dom->j_index)(idx); |
---|
347 | ++indexMask; |
---|
348 | } |
---|
349 | |
---|
350 | } |
---|
351 | } |
---|
352 | this->transformationMapping_.resize(vecAxisValueSizeWithMask); |
---|
353 | this->transformationWeight_.resize(vecAxisValueSizeWithMask); |
---|
354 | this->transformationPosition_.resize(vecAxisValueSizeWithMask); |
---|
355 | |
---|
356 | const CDistributionClient::GlobalLocalDataMap& globalLocalIndexSendToServer = grid->getDistributionClient()->getGlobalLocalDataSendToServer(); |
---|
357 | CDistributionClient::GlobalLocalDataMap::const_iterator itIndex, iteIndex = globalLocalIndexSendToServer.end(); |
---|
358 | size_t axisSrcSize = axisSrc_->index.numElements(); |
---|
359 | std::vector<int> globalDimension = grid->getGlobalDimension(); |
---|
360 | |
---|
361 | size_t indexMask = 0; |
---|
362 | for (size_t idx = 0; idx < vecAxisValueSize; ++idx) |
---|
363 | { |
---|
364 | if (dom->domainMask(idx)) |
---|
365 | { |
---|
366 | size_t axisValueSize = 0; |
---|
367 | for (size_t jdx = 0; jdx < axisSrcSize; ++jdx) |
---|
368 | { |
---|
369 | size_t globalIndex = ((dom->i_index)(idx) + (dom->j_index)(idx)*globalDimension[0]) + (axisSrc_->index)(jdx)*globalDimension[0]*globalDimension[1]; |
---|
370 | if (iteIndex != globalLocalIndexSendToServer.find(globalIndex)) |
---|
371 | { |
---|
372 | ++axisValueSize; |
---|
373 | } |
---|
374 | } |
---|
375 | |
---|
376 | vecAxisValue[indexMask].resize(axisValueSize); |
---|
377 | axisValueSize = 0; |
---|
378 | for (size_t jdx = 0; jdx < axisSrcSize; ++jdx) |
---|
379 | { |
---|
380 | size_t globalIndex = ((dom->i_index)(idx) + (dom->j_index)(idx)*globalDimension[0]) + (axisSrc_->index)(jdx)*globalDimension[0]*globalDimension[1]; |
---|
381 | itIndex = globalLocalIndexSendToServer.find(globalIndex); |
---|
382 | if (iteIndex != itIndex) |
---|
383 | { |
---|
384 | vecAxisValue[indexMask](axisValueSize) = (*dataAuxInputs[0])(itIndex->second); |
---|
385 | ++axisValueSize; |
---|
386 | } |
---|
387 | } |
---|
388 | ++indexMask; |
---|
389 | } |
---|
390 | } |
---|
391 | } |
---|
392 | } |
---|
393 | CATCH |
---|
394 | |
---|
395 | } |
---|