source: XIOS/trunk/src/transformation/axis_algorithm_interpolate.cpp @ 912

Last change on this file since 912 was 912, checked in by mhnguyen, 6 years ago

Fixing bug in axis interpolation

+) Correct index for searching interpolant values

Test
+) On local
+) Ok

File size: 11.2 KB
Line 
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 <algorithm>
11#include "context.hpp"
12#include "context_client.hpp"
13#include "utils.hpp"
14#include "grid.hpp"
15#include "distribution_client.hpp"
16
17namespace xios {
18
19CAxisAlgorithmInterpolate::CAxisAlgorithmInterpolate(CAxis* axisDestination, CAxis* axisSource, CInterpolateAxis* interpAxis)
20: CAxisAlgorithmTransformation(axisDestination, axisSource), coordinate_(), transPosition_()
21{
22  interpAxis->checkValid(axisSource);
23  order_ = interpAxis->order.getValue();
24  if (!interpAxis->coordinate.isEmpty())
25  {
26    coordinate_ = interpAxis->coordinate.getValue();
27    this->idAuxInputs_.resize(1);
28    this->idAuxInputs_[0] = coordinate_;
29  }
30}
31
32/*!
33  Compute the index mapping between axis on grid source and one on grid destination
34*/
35void CAxisAlgorithmInterpolate::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
36{
37  CContext* context = CContext::getCurrent();
38  CContextClient* client=context->client;
39  int nbClient = client->clientSize;
40  CArray<bool,1>& axisMask = axisSrc_->mask;
41  int srcSize  = axisSrc_->n_glo.getValue();
42  std::vector<CArray<double,1> > vecAxisValue;
43
44  // Fill in axis value from coordinate
45  fillInAxisValue(vecAxisValue, dataAuxInputs);
46  std::vector<double> valueSrc(srcSize);
47  std::vector<double> recvBuff(srcSize);
48  std::vector<int> indexVec(srcSize);
49
50  for (int idx = 0; idx < vecAxisValue.size(); ++idx)
51  {
52    CArray<double,1>& axisValue = vecAxisValue[idx];
53    retrieveAllAxisValue(axisValue, axisMask, recvBuff, indexVec);
54    XIOSAlgorithms::sortWithIndex<double, CVectorStorage>(recvBuff, indexVec);
55    for (int i = 0; i < srcSize; ++i) valueSrc[i] = recvBuff[indexVec[i]];
56    computeInterpolantPoint(valueSrc, indexVec, idx);
57  }
58}
59
60/*!
61  Compute the interpolant points
62  Assume that we have all value of axis source, with these values, need to calculate weight (coeff) of Lagrange polynomial
63  \param [in] axisValue all value of axis source
64  \param [in] tranPos position of axis on a domain
65*/
66void CAxisAlgorithmInterpolate::computeInterpolantPoint(const std::vector<double>& axisValue,
67                                                        const std::vector<int>& indexVec,
68                                                        int transPos)
69{
70  std::vector<double>::const_iterator itb = axisValue.begin(), ite = axisValue.end();
71  std::vector<double>::const_iterator itLowerBound, itUpperBound, it;
72  const double sfmax = NumTraits<double>::sfmax();
73
74  int ibegin = axisDest_->begin.getValue();
75  CArray<double,1>& axisDestValue = axisDest_->value;
76  int numValue = axisDestValue.numElements();
77  std::map<int, std::vector<std::pair<int,double> > > interpolatingIndexValues;
78
79  for (int idx = 0; idx < numValue; ++idx)
80  {
81    double destValue = axisDestValue(idx);
82    itLowerBound = std::lower_bound(itb, ite, destValue);
83    itUpperBound = std::upper_bound(itb, ite, destValue);
84    if ((ite != itUpperBound) && (sfmax == *itUpperBound)) itUpperBound = ite;
85
86    // If the value is not in the range, that means we'll do extra-polation
87    if (ite == itLowerBound) // extra-polation
88    {
89      itLowerBound = itb;
90      itUpperBound = itb + order_+1;
91    }
92    else if (ite == itUpperBound) // extra-polation
93    {
94      itLowerBound = itUpperBound - order_-1;
95    }
96    else
97    {
98      if (itb != itLowerBound) --itLowerBound;
99      if (ite != itUpperBound) ++itUpperBound;
100      int order = (order_ + 1) - 2;
101      bool down = true;
102      for (int k = 0; k < order; ++k)
103      {
104        if ((itb != itLowerBound) && down)
105        {
106          --itLowerBound;
107          down = false;
108          continue;
109        }
110        if ((ite != itUpperBound) && (sfmax != *itUpperBound))
111        {
112          ++itUpperBound;
113          down = true;
114        }
115      }
116    }
117
118    for (it = itLowerBound; it != itUpperBound; ++it)
119    {
120      int index = std::distance(itb, it);
121      interpolatingIndexValues[idx+ibegin].push_back(make_pair(indexVec[index],*it));
122    }
123  }
124  computeWeightedValueAndMapping(interpolatingIndexValues, transPos);
125}
126
127/*!
128  Compute weight (coeff) of Lagrange's polynomial
129  \param [in] interpolatingIndexValues the necessary axis value to calculate the coeffs
130*/
131void CAxisAlgorithmInterpolate::computeWeightedValueAndMapping(const std::map<int, std::vector<std::pair<int,double> > >& interpolatingIndexValues, int transPos)
132{
133  TransformationIndexMap& transMap = this->transformationMapping_[transPos];
134  TransformationWeightMap& transWeight = this->transformationWeight_[transPos];
135  std::map<int, std::vector<std::pair<int,double> > >::const_iterator itb = interpolatingIndexValues.begin(), it,
136                                                                      ite = interpolatingIndexValues.end();
137  int ibegin = axisDest_->begin.getValue();
138  for (it = itb; it != ite; ++it)
139  {
140    int globalIndexDest = it->first;
141    double localValue = axisDest_->value(globalIndexDest - ibegin);
142    const std::vector<std::pair<int,double> >& interpVal = it->second;
143    int interpSize = interpVal.size();
144    transMap[globalIndexDest].resize(interpSize);
145    transWeight[globalIndexDest].resize(interpSize);
146    for (int idx = 0; idx < interpSize; ++idx)
147    {
148      int index = interpVal[idx].first;
149      double weight = 1.0;
150
151      for (int k = 0; k < interpSize; ++k)
152      {
153        if (k == idx) continue;
154        weight *= (localValue - interpVal[k].second);
155        weight /= (interpVal[idx].second - interpVal[k].second);
156      }
157      transMap[globalIndexDest][idx] = index;
158      transWeight[globalIndexDest][idx] = weight;
159      if (!transPosition_.empty())
160      {
161        (this->transformationPosition_[transPos])[globalIndexDest] = transPosition_[transPos];
162      }
163    }
164  }
165}
166
167/*!
168  Each client retrieves all values of an axis
169  \param [in/out] recvBuff buffer for receiving values (already allocated)
170  \param [in/out] indexVec mapping between values and global index of axis
171*/
172void CAxisAlgorithmInterpolate::retrieveAllAxisValue(const CArray<double,1>& axisValue, const CArray<bool,1>& axisMask,
173                                                     std::vector<double>& recvBuff, std::vector<int>& indexVec)
174{
175  CContext* context = CContext::getCurrent();
176  CContextClient* client=context->client;
177  int nbClient = client->clientSize;
178
179  int srcSize  = axisSrc_->n_glo.getValue();
180  int numValue = axisValue.numElements();
181
182  if (srcSize == numValue)  // Only one client or axis not distributed
183  {
184    for (int idx = 0; idx < srcSize; ++idx)
185    {
186      if (axisMask(idx))
187      {
188        recvBuff[idx] = axisValue(idx);
189        indexVec[idx] = idx;
190      }
191      else
192      {
193        recvBuff[idx] = NumTraits<double>::sfmax();
194        indexVec[idx] = -1;
195      }
196    }
197
198  }
199  else // Axis distributed
200  {
201    double* sendValueBuff = new double [numValue];
202    int* sendIndexBuff = new int [numValue];
203    int* recvIndexBuff = new int [srcSize];
204
205    int ibegin = axisSrc_->begin.getValue();
206    for (int idx = 0; idx < numValue; ++idx)
207    {
208      if (axisMask(idx))
209      {
210        sendValueBuff[idx] = axisValue(idx);
211        sendIndexBuff[idx] = idx + ibegin;
212      }
213      else
214      {
215        sendValueBuff[idx] = NumTraits<double>::sfmax();
216        sendIndexBuff[idx] = -1;
217      }
218    }
219
220    int* recvCount=new int[nbClient];
221    MPI_Allgather(&numValue,1,MPI_INT,recvCount,1,MPI_INT,client->intraComm);
222
223    int* displ=new int[nbClient];
224    displ[0]=0 ;
225    for(int n=1;n<nbClient;n++) displ[n]=displ[n-1]+recvCount[n-1];
226
227    // Each client have enough global info of axis
228    MPI_Allgatherv(sendIndexBuff,numValue,MPI_INT,recvIndexBuff,recvCount,displ,MPI_INT,client->intraComm);
229    MPI_Allgatherv(sendValueBuff,numValue,MPI_DOUBLE,&(recvBuff[0]),recvCount,displ,MPI_DOUBLE,client->intraComm);
230
231    for (int idx = 0; idx < srcSize; ++idx)
232    {
233      indexVec[idx] = recvIndexBuff[idx];
234    }
235
236    delete [] displ;
237    delete [] recvCount;
238    delete [] recvIndexBuff;
239    delete [] sendIndexBuff;
240    delete [] sendValueBuff;
241  }
242}
243
244/*!
245  Fill in axis value dynamically from a field whose grid is composed of a domain and an axis
246  \param [in/out] vecAxisValue vector axis value filled in from input field
247*/
248void CAxisAlgorithmInterpolate::fillInAxisValue(std::vector<CArray<double,1> >& vecAxisValue,
249                                                const std::vector<CArray<double,1>* >& dataAuxInputs)
250{
251  if (coordinate_.empty())
252  {
253    vecAxisValue.resize(1);
254    vecAxisValue[0].resize(axisSrc_->value.numElements());
255    vecAxisValue[0] = axisSrc_->value;
256    this->transformationMapping_.resize(1);
257    this->transformationWeight_.resize(1);
258  }
259  else
260  {
261    CField* field = CField::get(coordinate_);
262    CGrid* grid = field->grid;
263
264    std::vector<CDomain*> domListP = grid->getDomains();
265    std::vector<CAxis*> axisListP = grid->getAxis();
266    if (domListP.empty() || axisListP.empty() || (1 < domListP.size()) || (1 < axisListP.size()))
267      ERROR("CAxisAlgorithmInterpolate::fillInAxisValue(std::vector<CArray<double,1> >& vecAxisValue)",
268             << "XIOS only supports dynamic interpolation with coordinate (field) associated with grid composed of a domain and an axis"
269             << "Coordinate (field) id = " <<field->getId() << std::endl
270             << "Associated grid id = " << grid->getId());
271
272    CDomain* dom = domListP[0];
273    size_t vecAxisValueSize = dom->i_index.numElements();
274    int niGlobDom = dom->ni_glo.getValue();
275    vecAxisValue.resize(vecAxisValueSize);
276    if (transPosition_.empty())
277    {
278      transPosition_.resize(vecAxisValueSize);
279      for (size_t idx = 0; idx < vecAxisValueSize; ++idx)
280      {
281        transPosition_[idx].resize(1);
282        transPosition_[idx][0] = (dom->i_index)(idx) + niGlobDom * (dom->j_index)(idx);
283      }
284    }
285    this->transformationMapping_.resize(vecAxisValueSize);
286    this->transformationWeight_.resize(vecAxisValueSize);
287    this->transformationPosition_.resize(vecAxisValueSize);
288
289    const CDistributionClient::GlobalLocalDataMap& globalLocalIndexSendToServer = grid->getDistributionClient()->getGlobalLocalDataSendToServer();
290    CDistributionClient::GlobalLocalDataMap::const_iterator itIndex, iteIndex = globalLocalIndexSendToServer.end();
291    size_t axisSrcSize = axisSrc_->index.numElements();
292    std::vector<int> globalDimension = grid->getGlobalDimension();
293
294    for (size_t idx = 0; idx < vecAxisValueSize; ++idx)
295    {
296      size_t axisValueSize = 0;
297      for (size_t jdx = 0; jdx < axisSrcSize; ++jdx)
298      {
299        size_t globalIndex = ((dom->i_index)(idx) + (dom->j_index)(idx)*globalDimension[0]) + (axisSrc_->index)(jdx)*globalDimension[0]*globalDimension[1];
300        if (iteIndex != globalLocalIndexSendToServer.find(globalIndex))
301        {
302          ++axisValueSize;
303        }
304      }
305
306      vecAxisValue[idx].resize(axisValueSize);
307      axisValueSize = 0;
308      for (size_t jdx = 0; jdx < axisSrcSize; ++jdx)
309      {
310        size_t globalIndex = ((dom->i_index)(idx) + (dom->j_index)(idx)*globalDimension[0]) + (axisSrc_->index)(jdx)*globalDimension[0]*globalDimension[1];
311        itIndex = globalLocalIndexSendToServer.find(globalIndex);
312        if (iteIndex != itIndex)
313        {
314          vecAxisValue[idx](axisValueSize) = (*dataAuxInputs[0])(itIndex->second);
315          ++axisValueSize;
316        }
317      }
318    }
319  }
320}
321
322}
Note: See TracBrowser for help on using the repository browser.