source: XIOS/dev/dev_trunk_graph/src/transformation/axis_algorithm/axis_algorithm_interpolate.cpp @ 2019

Last change on this file since 2019 was 2019, checked in by yushan, 3 years ago

Graph intermedia commit to a tmp branch

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