source: XIOS/dev/dev_ym/XIOS_COUPLING/src/transformation/domain_algorithm_expand.cpp @ 1878

Last change on this file since 1878 was 1784, checked in by ymipsl, 4 years ago
  • Preparing coupling functionalities.
  • Make some cleaner things

YM

File size: 26.9 KB
Line 
1/*!
2   \file domain_algorithm_expand.cpp
3   \author Ha NGUYEN
4   \since 08 Aug 2016
5   \date 19 Sep 2016
6
7   \brief Algorithm for expanding an domain.
8 */
9#include "domain_algorithm_expand.hpp"
10#include "expand_domain.hpp"
11#include "mesh.hpp"
12#include "domain.hpp"
13#include "grid.hpp"
14#include "grid_transformation_factory_impl.hpp"
15#include "context.hpp"
16#include "context_client.hpp"
17
18namespace xios {
19CGenericAlgorithmTransformation* CDomainAlgorithmExpand::create(CGrid* gridDst, CGrid* gridSrc,
20                                                               CTransformation<CDomain>* transformation,
21                                                               int elementPositionInGrid,
22                                                               std::map<int, int>& elementPositionInGridSrc2ScalarPosition,
23                                                               std::map<int, int>& elementPositionInGridSrc2AxisPosition,
24                                                               std::map<int, int>& elementPositionInGridSrc2DomainPosition,
25                                                               std::map<int, int>& elementPositionInGridDst2ScalarPosition,
26                                                               std::map<int, int>& elementPositionInGridDst2AxisPosition,
27                                                               std::map<int, int>& elementPositionInGridDst2DomainPosition)
28TRY
29{
30  std::vector<CDomain*> domainListDestP = gridDst->getDomains();
31  std::vector<CDomain*> domainListSrcP  = gridSrc->getDomains();
32
33  CExpandDomain* expandDomain = dynamic_cast<CExpandDomain*> (transformation);
34  int domainDstIndex = elementPositionInGridDst2DomainPosition[elementPositionInGrid];
35  int domainSrcIndex = elementPositionInGridSrc2DomainPosition[elementPositionInGrid];
36
37  return (new CDomainAlgorithmExpand(domainListDestP[domainDstIndex], domainListSrcP[domainSrcIndex], expandDomain));
38}
39CATCH
40
41bool CDomainAlgorithmExpand::registerTrans()
42TRY
43{
44  CGridTransformationFactory<CDomain>::registerTransformation(TRANS_EXPAND_DOMAIN, create);
45}
46CATCH
47
48CDomainAlgorithmExpand::CDomainAlgorithmExpand(CDomain* domainDestination,
49                                               CDomain* domainSource,
50                                               CExpandDomain* expandDomain)
51: CDomainAlgorithmTransformation(domainDestination, domainSource),
52  isXPeriodic_(false), isYPeriodic_(false)
53TRY
54{
55  if (domainDestination == domainSource)
56  {
57    ERROR("CDomainAlgorithmExpand::CDomainAlgorithmExpand(CDomain* domainDestination,CDomain* domainSource, CExpandDomain* expandDomain)",
58           << "Domain source and domain destination are the same. Please make sure domain destination refers to domain source" << std::endl
59           << "Domain source " <<domainSource->getId() << std::endl
60           << "Domain destination " <<domainDestination->getId() << std::endl);
61  }
62
63  this->type_ = (ELEMENT_MODIFICATION_WITH_DATA);
64  // Make sure domain source have all valid attributes
65  // domainSource->checkAllAttributes();
66  expandDomain->checkValid(domainDestination);
67  if (!expandDomain->i_periodic.isEmpty()) isXPeriodic_ = expandDomain->i_periodic;
68  if (!expandDomain->j_periodic.isEmpty()) isYPeriodic_ = expandDomain->j_periodic;
69 
70  switch (expandDomain->type)
71  {
72    case CExpandDomain::type_attr::node :
73      expandDomainNodeConnectivity(domainDestination,
74                                   domainSource);
75      break;
76    case CExpandDomain::type_attr::edge :
77      expandDomainEdgeConnectivity(domainDestination,
78                                   domainSource);
79      break;
80    default:
81      break;
82  }
83}
84CATCH
85
86/*!
87 *  Expand domain with edge-type neighbor
88 *  \param[in/out] domainDestination domain destination and will be modified
89 *  \param[in] domainSource domain source
90 */
91void CDomainAlgorithmExpand::expandDomainEdgeConnectivity(CDomain* domainDestination,
92                                                          CDomain* domainSource)
93TRY
94{
95  CContext* context = CContext::getCurrent();
96 
97  int type = 1; // For edge
98  CMesh mesh;
99  CArray<double,2>& bounds_lon_src = domainSource->bounds_lon_1d;
100  CArray<double,2>& bounds_lat_src = domainSource->bounds_lat_1d;
101  CArray<int,2> neighborsSrc;
102  switch (domainSource->type) {
103   case CDomain::type_attr::unstructured:     
104      mesh.getGlobalNghbFaces(type, context->intraComm_, domainSource->i_index, bounds_lon_src, bounds_lat_src, neighborsSrc);
105      updateUnstructuredDomainAttributes(domainDestination, domainSource, neighborsSrc);
106      break;
107   default:
108      updateRectilinearDomainAttributes(domainDestination, domainSource, neighborsSrc);
109      break;     
110  } 
111}
112CATCH
113
114/*!
115 *  Expand domain with node-type neighbor
116 *  \param[in/out] domainDestination domain destination and will be modified
117 *  \param[in] domainSource domain source
118 */
119void CDomainAlgorithmExpand::expandDomainNodeConnectivity(CDomain* domainDestination,
120                                                          CDomain* domainSource)
121TRY
122{
123  CContext* context = CContext::getCurrent();
124
125  int type = 1; // For edge
126  CMesh mesh;
127  CArray<double,2>& bounds_lon_src = domainSource->bounds_lon_1d;
128  CArray<double,2>& bounds_lat_src = domainSource->bounds_lat_1d;
129  CArray<int,2> neighborsSrc;
130  switch (domainSource->type) {
131   case CDomain::type_attr::unstructured:     
132      mesh.getGlobalNghbFaces(type, context->intraComm_, domainSource->i_index, bounds_lon_src, bounds_lat_src, neighborsSrc);
133      updateUnstructuredDomainAttributes(domainDestination, domainSource, neighborsSrc);
134      break;
135   default:
136      updateRectilinearDomainAttributes(domainDestination, domainSource, neighborsSrc);
137      break;     
138  } 
139}
140CATCH
141
142/*!
143 *  Extend rectilinear or curvilinear domain destination and update its attributes
144 *  Suppose that domain destination and domain source have the same values for all attributes (by inheritance)
145 *  \param [in/out] domainDestination domain destination
146 *  \param [in] domainSource domain source
147 *  \param [in] neighborsDomainSrc neighbor of domain source. For now, we don't need it for rectilinear
148 */
149void CDomainAlgorithmExpand::updateRectilinearDomainAttributes(CDomain* domainDestination,
150                                                               CDomain* domainSource,
151                                                               CArray<int,2>& neighborsDomainSrc)
152TRY
153{
154  int index, globalIndex, idx;
155  int iindexDst, jindexDst, globIndexDst;
156  int iindexSrc, jindexSrc, globIndexSrc;
157  CContext* context = CContext::getCurrent();
158
159  // First of all, "copy" all attributes of domain source to domain destination
160  StdString domainDstRef = (!domainDestination->domain_ref.isEmpty()) ? domainDestination->domain_ref.getValue()
161                                                                      : "";
162  if (domainDstRef != domainSource->getId())
163  {
164    domainDestination->domain_ref.setValue(domainSource->getId());
165    domainDestination->solveRefInheritance(true);
166  }
167
168  if (domainDstRef.empty()) domainDestination->domain_ref.reset();
169  else domainDestination->domain_ref.setValue(domainDstRef);
170
171 
172  // Here are attributes of source need tranfering
173  int niGloSrc = domainSource->ni_glo;
174  int njGloSrc = domainSource->nj_glo;
175  int niSrc = domainSource->ni, ibegin = domainSource->ibegin;
176  int njSrc = domainSource->nj, jbegin = domainSource->jbegin;
177  int dataDimSrc = domainSource->data_dim;
178  CArray<bool,1>& mask_1d_src = domainSource->domainMask;
179  CArray<int,1>& i_index_src = domainSource->i_index;
180  CArray<int,1>& j_index_src = domainSource->j_index;
181  CArray<int,1>& data_i_index_src = domainSource->data_i_index;
182  CArray<int,1>& data_j_index_src = domainSource->data_j_index;
183  int data_i_begin_src = domainSource->data_ibegin;
184  int data_j_begin_src = domainSource->data_jbegin;
185  CArray<double,1>& lon_src = domainSource->lonvalue;
186  CArray<double,1>& lat_src = domainSource->latvalue;
187
188  // We need to generate boundary for longitude and latitude
189  if (domainSource->bounds_lon_1d.isEmpty() || domainSource->bounds_lat_1d.isEmpty())
190  {
191    CArray<double,1> lon = lon_src(Range(0,niSrc-1));
192    CArray<double,1> lat = lat_src(Range(0,lat_src.numElements()-niSrc,niSrc));
193    CArray<double,2>& bounds_lon_src = domainSource->bounds_lon_1d;
194    CArray<double,2>& bounds_lat_src = domainSource->bounds_lat_1d;
195    domainSource->fillInRectilinearBoundLonLat(lon_src, lat_src, bounds_lon_src, bounds_lat_src);
196  }
197
198
199  CArray<double,2>& bounds_lon_src = domainSource->bounds_lon_1d;
200  CArray<double,2>& bounds_lat_src = domainSource->bounds_lat_1d;
201
202  int nVertex       = bounds_lon_src.shape()[0];
203  int oldNbLocal = i_index_src.numElements();
204  // Calculate ni, nj by using i_index and j_index
205  int niSrcByIndex = max(i_index_src) - min(i_index_src) + 1;
206  int njSrcByIndex = max(j_index_src) - min(j_index_src) + 1; 
207  int dataIindexBoundSrc = (1 == dataDimSrc) ? (niSrcByIndex * njSrcByIndex) : niSrcByIndex;
208  int dataJindexBoundSrc = (1 == dataDimSrc) ? (niSrcByIndex * njSrcByIndex) : njSrcByIndex;
209
210  // Uncompress data_i_index, data_j_index
211  CArray<int,1> data_i_index_src_full(oldNbLocal);
212  CArray<int,1> data_j_index_src_full(oldNbLocal);
213  int nbUnMaskedPointOnLocalDomain = 0;
214  data_i_index_src_full = -1; // Suppose all values are masked
215  data_j_index_src_full = -1; // Suppose all values are masked
216  for (idx = 0; idx < data_i_index_src.numElements(); ++idx)
217  {
218    int dataIidx = data_i_index_src(idx) + data_i_begin_src;
219    int dataJidx = data_j_index_src(idx) + data_j_begin_src;
220    if ((0 <= dataIidx) && (dataIidx < dataIindexBoundSrc) &&
221        (0 <= dataJidx) && (dataJidx < dataJindexBoundSrc))
222    {
223      data_i_index_src_full(nbUnMaskedPointOnLocalDomain) = dataIidx;
224      data_j_index_src_full(nbUnMaskedPointOnLocalDomain) = dataJidx;
225      ++nbUnMaskedPointOnLocalDomain;
226    }
227  }
228
229  // Expand domain destination, not only local but also global
230  int niGloDst = niGloSrc + 2;
231  int njGloDst = njGloSrc + 2;
232  int niDst = niSrc + 2;
233  int njDst = njSrc + 2;
234  domainDestination->ni_glo.setValue(niGloDst);
235  domainDestination->nj_glo.setValue(njGloDst);
236  domainDestination->ni.setValue(niDst);
237  domainDestination->nj.setValue(njDst);
238
239  CArray<bool,1>& mask_1d_dst = domainDestination->domainMask;
240  CArray<int,1>& i_index_dst  = domainDestination->i_index;
241  CArray<int,1>& j_index_dst  = domainDestination->j_index; 
242  CArray<int,1>& data_i_index_dst  = domainDestination->data_i_index;
243  CArray<int,1>& data_j_index_dst  = domainDestination->data_j_index;
244 
245  // Make sure that we use only lonvalue_client, latvalue_client
246  if (!domainDestination->lonvalue_1d.isEmpty()) domainDestination->lonvalue_1d.reset();
247  if (!domainDestination->latvalue_1d.isEmpty()) domainDestination->latvalue_1d.reset();
248  if (!domainDestination->lonvalue_2d.isEmpty()) domainDestination->lonvalue_2d.reset();
249  if (!domainDestination->latvalue_2d.isEmpty()) domainDestination->latvalue_2d.reset();
250
251  // Recalculate i_index, j_index of extended domain
252  // Should be enough for common case, but if we have arbitrary distribution?
253  int newNbLocalDst = niDst * njDst;     
254
255  mask_1d_dst.resize(newNbLocalDst);
256  i_index_dst.resize(newNbLocalDst);
257  j_index_dst.resize(newNbLocalDst);
258  CArray<int,1> data_i_index_dst_full(newNbLocalDst);
259  CArray<int,1> data_j_index_dst_full(newNbLocalDst);
260
261  domainDestination->lonvalue.resizeAndPreserve(newNbLocalDst);
262  domainDestination->latvalue.resizeAndPreserve(newNbLocalDst);
263  domainDestination->bounds_lon_1d.resizeAndPreserve(nVertex, newNbLocalDst);
264  domainDestination->bounds_lat_1d.resizeAndPreserve(nVertex, newNbLocalDst);
265  CArray<double,1>& lon_dst   = domainDestination->lonvalue;
266  CArray<double,1>& lat_dst   = domainDestination->latvalue;
267  CArray<double,2>& bounds_lon_dst = domainDestination->bounds_lon_1d;
268  CArray<double,2>& bounds_lat_dst = domainDestination->bounds_lat_1d;
269
270  // Update i_index, j_index
271  for (int j = 0; j < njDst; ++j)
272    for (int i = 0; i < niDst; ++i)
273    {
274      idx = j * niDst + i; 
275      i_index_dst(idx) = i + ibegin;
276      j_index_dst(idx) = j + jbegin;
277    }
278 
279
280  // 1. Fill in array relating to global index (i_index, j_index, transmap, etc, ...)
281  // Global index mapping between destination and source
282  this->transformationMapping_.resize(1);
283  this->transformationWeight_.resize(1);
284  TransformationIndexMap& transMap = this->transformationMapping_[0];
285  TransformationWeightMap& transWeight = this->transformationWeight_[0];
286
287  transMap.rehash(std::ceil(newNbLocalDst/transMap.max_load_factor()));
288  transWeight.rehash(std::ceil(newNbLocalDst/transWeight.max_load_factor()));
289 
290  // Index mapping for local domain
291  // Mapping global index of expanded domain into original one
292  // (Representing global index of expanded domain in form of global index of original one)
293  CArray<size_t,1> globalIndexSrcOnDstDomain(newNbLocalDst); 
294  for (idx = 0; idx < newNbLocalDst; ++idx)
295  {
296    iindexDst = i_index_dst(idx);
297    jindexDst = j_index_dst(idx);
298    globIndexDst = jindexDst * niGloDst + iindexDst;
299    globIndexSrc = (((jindexDst-1)+njGloSrc) % njGloSrc) * niGloSrc + (((iindexDst-1)+niGloSrc) % niGloSrc) ;
300    globalIndexSrcOnDstDomain(idx) = globIndexSrc;
301
302    transMap[globIndexDst].push_back(globIndexSrc);
303    transWeight[globIndexDst].push_back(1.0); 
304  }
305
306  // 2. Exchange local info among domains (lon,lat,bounds,mask,etc,...)
307  CClientClientDHTDouble::Index2VectorInfoTypeMap localData;
308  localData.rehash(std::ceil(oldNbLocal/localData.max_load_factor()));
309
310  // Information exchanged among domains (attention to their order), number in parentheses presents size of data
311  // lon(1) + lat(1) + bounds_lon(nVertex) + bounds_lat(nVertex) + mask(1) + data_i_index(1)
312  int dataPackageSize =  1 + 1 + // lon + lat
313                         nVertex + nVertex + //bounds_lon + bounds_lat
314                         1 + // mask_1d_dst;
315                         1 + 1; // data_i_index + data_j_index
316  // Initialize database
317  for (int idx = 0; idx < oldNbLocal; ++idx)
318  {
319    index = i_index_src(idx) + j_index_src(idx) * niGloSrc;
320    localData[index].resize(dataPackageSize);
321    std::vector<double>& data = localData[index];
322
323    //Pack data
324    int dataIdx = 0;
325    data[dataIdx] = lon_src(idx);++dataIdx;
326    data[dataIdx] = lat_src(idx);++dataIdx;
327    for (int i = 0; i < nVertex; ++i)
328    {
329      data[dataIdx] = bounds_lon_src(i,idx); ++dataIdx;
330    }
331    for (int i = 0; i < nVertex; ++i)
332    {
333      data[dataIdx] = bounds_lat_src(i,idx); ++dataIdx;
334    }
335    data[dataIdx] = mask_1d_src(idx) ? 1.0 : -1.0; ++dataIdx;
336    data[dataIdx] = data_i_index_src_full(idx);++dataIdx;
337    data[dataIdx] = data_j_index_src_full(idx);
338  }
339
340  CClientClientDHTDouble dhtData(localData,context->intraComm_);
341  dhtData.computeIndexInfoMapping(globalIndexSrcOnDstDomain);
342  CClientClientDHTDouble::Index2VectorInfoTypeMap& neighborData = dhtData.getInfoIndexMap();
343  CClientClientDHTDouble::Index2VectorInfoTypeMap::iterator ite = neighborData.end(), it;
344
345  // Ok get all data for destination
346  // If domain is not periodic, then we mask all extended part.
347  int nbUnMaskedPointOnExtendedPart = 0, remainder = 0, dataIIndex, dataJIndex;
348  size_t nIdx;
349  double maskValue = 1.0;
350  for (index = 0; index < newNbLocalDst; ++index)
351  {
352     nIdx = globalIndexSrcOnDstDomain(index);
353     it = neighborData.find(nIdx);
354     if (ite != it)
355     {
356        std::vector<double>& data = it->second;
357        // Unpack data
358        int dataIdx = 0;
359        lon_dst(index) = data[dataIdx]; ++dataIdx;
360        lat_dst(index) = data[dataIdx]; ++dataIdx;
361        for (int i = 0; i < nVertex; ++i)
362        {
363          bounds_lon_dst(i,index) = data[dataIdx]; ++dataIdx;
364        }
365        for (int i = 0; i < nVertex; ++i)
366        {
367          bounds_lat_dst(i,index) = data[dataIdx]; ++dataIdx;
368        }
369       
370        // Check whether we have x periodic. If we don't, we should mask all point at 0 and niGloDst-1
371        maskValue = data[dataIdx];
372        if (!isXPeriodic_) 
373        {
374          remainder = i_index_dst(index) % (niGloDst-1);
375          if (0 == remainder) 
376          {
377            maskValue = -1.0;
378          }
379        }
380
381        if (!isYPeriodic_) 
382        {
383          remainder = j_index_dst(index) % (njGloDst-1);
384          if (0 == remainder) 
385          {
386            maskValue = -1.0;
387          }
388        }
389
390        mask_1d_dst(index) = (1.0 == maskValue) ? true : false; ++dataIdx;
391
392        dataIIndex = (int) data[dataIdx];
393        if (!isXPeriodic_) 
394        {
395          remainder = i_index_dst(index) % (niGloDst-1);
396          if (0 == remainder) 
397          {
398            dataIIndex = -1;
399          }
400        }
401        data_i_index_dst_full(index) = dataIIndex; ++dataIdx;
402       
403        dataJIndex = (int) data[dataIdx];
404        if (!isYPeriodic_) 
405        {
406          remainder = j_index_dst(index) % (njGloDst-1);
407          if (0 == remainder) 
408          {
409            dataJIndex = -1;
410          }
411        }       
412        data_j_index_dst_full(index) = dataJIndex;
413
414        if ((0 <= data_i_index_dst_full(index)) && (0 <= data_j_index_dst_full(index)))
415        {
416          ++nbUnMaskedPointOnExtendedPart;
417        }
418     }
419  }
420
421 
422  // Finally, update data_i_index, data_j_index
423  int dataDstDim = domainDestination->data_dim;
424  data_i_index_dst.resize(nbUnMaskedPointOnExtendedPart);
425  data_j_index_dst.resize(nbUnMaskedPointOnExtendedPart); 
426  int count = 0; 
427  for (idx = 0; idx < newNbLocalDst; ++idx)
428  {
429    dataIIndex = data_i_index_dst_full(idx);
430    dataJIndex = data_j_index_dst_full(idx);
431    if ((0 <= dataIIndex) && (0 <= dataJIndex))
432    {
433      data_i_index_dst(count) = (1 == dataDstDim) ? idx : i_index_dst(idx) - i_index_dst(0);
434      data_j_index_dst(count) = (1 == dataDstDim) ? 0   : j_index_dst(idx) - j_index_dst(0);
435      ++count;
436    }
437  }
438
439  // Update data_ni, data_nj
440 
441  domainDestination->data_ni.setValue((1==dataDstDim) ? niDst * njDst : niDst);
442  domainDestination->data_nj.setValue((1==dataDstDim) ? niDst * njDst : njDst); 
443  domainDestination->data_ibegin.setValue(0);
444  domainDestination->data_jbegin.setValue(0);
445
446  // Update longitude and latitude
447  if (niSrc == domainSource->lonvalue_1d.numElements() && njSrc == domainSource->latvalue_1d.numElements()) // Ok, we have rectilinear here
448  {
449     domainDestination->lonvalue_1d.resize(niDst);
450     domainDestination->lonvalue_1d = lon_dst(Range(0,niDst-1));
451     domainDestination->latvalue_1d.resize(njDst);
452     domainDestination->latvalue_1d = lat_dst(Range(0,lat_dst.numElements()-niDst,niDst));
453  }
454  else // It should be curvilinear
455  {
456     domainDestination->lonvalue_1d.resize(lon_dst.numElements());
457     domainDestination->lonvalue_1d = lon_dst;
458     domainDestination->latvalue_1d.resize(lat_dst.numElements());
459     domainDestination->latvalue_1d = (lat_dst);
460  }
461   domainDestination->mask_1d.resize(domainDestination->domainMask.numElements()) ;
462   domainDestination->mask_1d=domainDestination->domainMask ;
463   domainDestination->computeLocalMask() ;
464}
465CATCH
466
467/*!
468 *  Extend domain destination and update its attributes
469 *  Suppose that domain destination and domain source have the same values for all attributes (by inheritance)
470 *  \param [in/out] domainDestination domain destination
471 *  \param [in] domainSource domain source
472 *  \param [in] neighborsDomainSrc domain extended part
473 */
474void CDomainAlgorithmExpand::updateUnstructuredDomainAttributes(CDomain* domainDestination,
475                                                                CDomain* domainSource,
476                                                                CArray<int,2>& neighborsDomainSrc)
477TRY
478{
479
480  CContext* context = CContext::getCurrent();
481
482  // First of all, "copy" all attributes of domain source to domain destination
483  StdString domainDstRef = (!domainDestination->domain_ref.isEmpty()) ? domainDestination->domain_ref.getValue()
484                                                                      : "";
485  if (domainDstRef != domainSource->getId())
486  {
487    domainDestination->domain_ref.setValue(domainSource->getId());
488    domainDestination->solveRefInheritance(true);
489  }
490
491  if (domainDstRef.empty()) domainDestination->domain_ref.reset();
492  else domainDestination->domain_ref.setValue(domainDstRef);
493
494  // Now extend domain destination
495  int niGlob = domainSource->ni_glo;
496  CArray<bool,1>& mask_1d_src = domainSource->domainMask;
497  CArray<int,1>& i_index_src = domainSource->i_index;
498  CArray<double,1>& lon_src = domainSource->lonvalue_1d;
499  CArray<double,1>& lat_src = domainSource->latvalue_1d;
500  CArray<double,2>& bounds_lon_src = domainSource->bounds_lon_1d;
501  CArray<double,2>& bounds_lat_src = domainSource->bounds_lat_1d;
502  CArray<int,1>& data_i_index_src = domainSource->data_i_index;
503
504  int oldNbLocal = i_index_src.numElements(), index, globalIndex;
505  // Uncompress data_i_index
506  CArray<int,1> data_i_index_src_full(oldNbLocal);
507  int nbUnMaskedPointOnLocalDomain = 0;
508  data_i_index_src_full = -1; // Suppose all values are masked
509  for (int idx = 0; idx < data_i_index_src.numElements(); ++idx)
510  {
511    int dataIdx = data_i_index_src(idx);
512    if ((0 <= dataIdx) && (dataIdx < oldNbLocal))
513    {
514      data_i_index_src_full(nbUnMaskedPointOnLocalDomain) = dataIdx;
515      ++nbUnMaskedPointOnLocalDomain;
516    }
517  }
518
519  CArray<bool,1>& mask_1d_dst = domainDestination->domainMask;
520  CArray<int,1>& i_index_dst = domainDestination->i_index;
521  CArray<int,1>& j_index_dst = domainDestination->j_index;
522  CArray<double,1>& lon_dst = domainDestination->lonvalue_1d;
523  CArray<double,1>& lat_dst = domainDestination->latvalue_1d;
524  CArray<double,2>& bounds_lon_dst = domainDestination->bounds_lon_1d;
525  CArray<double,2>& bounds_lat_dst = domainDestination->bounds_lat_1d;
526  CArray<int,1>& data_i_index_dst = domainDestination->data_i_index;
527  CArray<int,1>& data_j_index_dst = domainDestination->data_j_index;
528
529  // Resize all array-like attributes of domain destination
530  int nbNeighbor    = neighborsDomainSrc.shape()[1];
531  int newNbLocalDst = nbNeighbor + oldNbLocal;
532  int nVertex       = bounds_lon_dst.shape()[0];
533
534  mask_1d_dst.resizeAndPreserve(newNbLocalDst);
535  i_index_dst.resizeAndPreserve(newNbLocalDst);
536  j_index_dst.resizeAndPreserve(newNbLocalDst);
537  lon_dst.resizeAndPreserve(newNbLocalDst);
538  lat_dst.resizeAndPreserve(newNbLocalDst);
539  bounds_lon_dst.resizeAndPreserve(nVertex, newNbLocalDst);
540  bounds_lat_dst.resizeAndPreserve(nVertex, newNbLocalDst);
541  CArray<int,1> data_i_index_dst_full(newNbLocalDst);
542  data_i_index_dst_full(Range(0,oldNbLocal-1)) = data_i_index_src_full;
543  data_i_index_dst_full(Range(oldNbLocal,newNbLocalDst-1)) = -1;
544
545  // 1. Fill in array relating to global index (i_index, j_index, transmap, etc, ...)
546  // Global index mapping between destination and source
547  this->transformationMapping_.resize(1);
548  this->transformationWeight_.resize(1);
549  TransformationIndexMap& transMap = this->transformationMapping_[0];
550  TransformationWeightMap& transWeight = this->transformationWeight_[0];
551
552  transMap.rehash(std::ceil(newNbLocalDst/transMap.max_load_factor()));
553  transWeight.rehash(std::ceil(newNbLocalDst/transWeight.max_load_factor()));
554  // First, index mapping for local domain
555  for (int idx = 0; idx < oldNbLocal; ++idx)
556  {
557    index = i_index_dst(idx);
558    transMap[index].push_back(index);
559    transWeight[index].push_back(1.0);
560  }
561  // Then, index mapping for extended part
562  for (int idx = 0; idx < nbNeighbor; ++idx)
563  {
564    index = idx + oldNbLocal;
565    globalIndex = neighborsDomainSrc(0,idx);
566    i_index_dst(index) = globalIndex;
567    j_index_dst(index) = 0;
568    transMap[globalIndex].push_back(globalIndex);
569    transWeight[globalIndex].push_back(1.0);
570  }
571
572  // 2. Exchange local info among domains (lon,lat,bounds,mask,etc,...)
573  CClientClientDHTDouble::Index2VectorInfoTypeMap localData;
574  localData.rehash(std::ceil(oldNbLocal/localData.max_load_factor()));
575  // Information exchanged among domains (attention to their order), number in parentheses presents size of data
576  // lon(1) + lat(1) + bounds_lon(nVertex) + bounds_lat(nVertex) + mask(1) + data_i_index(1)
577  int dataPackageSize =  1 + 1 + // lon + lat
578                         nVertex + nVertex + //bounds_lon + bounds_lat
579                         1 + // mask_1d_dst;
580                         1; // data_i_index
581  // Initialize database
582  for (int idx = 0; idx < oldNbLocal; ++idx)
583  {
584    index = i_index_src(idx);
585    localData[index].resize(dataPackageSize);
586    std::vector<double>& data = localData[index];
587
588    //Pack data
589    int dataIdx = 0;
590    data[dataIdx] = lon_src(idx);++dataIdx;
591    data[dataIdx] = lat_src(idx);++dataIdx;
592    for (int i = 0; i < nVertex; ++i)
593    {
594      data[dataIdx] = bounds_lon_src(i,idx); ++dataIdx;
595    }
596    for (int i = 0; i < nVertex; ++i)
597    {
598      data[dataIdx] = bounds_lat_src(i,idx); ++dataIdx;
599    }
600    data[dataIdx] = mask_1d_src(idx) ? 1.0 : -1.0; ++dataIdx;
601    data[dataIdx] = data_i_index_src_full(idx);
602  }
603
604  CClientClientDHTDouble dhtData(localData, context->intraComm_);
605  CArray<size_t,1> neighborInd(nbNeighbor);
606  for (int idx = 0; idx < nbNeighbor; ++idx)
607    neighborInd(idx) = neighborsDomainSrc(0,idx);
608
609  // Compute local data on other domains
610  dhtData.computeIndexInfoMapping(neighborInd);
611  CClientClientDHTDouble::Index2VectorInfoTypeMap& neighborData = dhtData.getInfoIndexMap();
612  CClientClientDHTDouble::Index2VectorInfoTypeMap::iterator ite = neighborData.end(), it;
613  // Ok get neighbor data
614  size_t nIdx;
615  int nbUnMaskedPointOnExtendedPart = 0;
616  for (int idx = 0; idx < nbNeighbor; ++idx)
617  {
618    nIdx  = neighborInd(idx);
619    it = neighborData.find(nIdx);
620    if (ite != it)
621    {
622      index = idx + oldNbLocal;
623      std::vector<double>& data = it->second;
624      // Unpack data
625      int dataIdx = 0;
626      lon_dst(index) = data[dataIdx]; ++dataIdx;
627      lat_dst(index) = data[dataIdx]; ++dataIdx;
628      for (int i = 0; i < nVertex; ++i)
629      {
630        bounds_lon_dst(i,index) = data[dataIdx]; ++dataIdx;
631      }
632      for (int i = 0; i < nVertex; ++i)
633      {
634        bounds_lat_dst(i,index) = data[dataIdx]; ++dataIdx;
635      }
636      mask_1d_dst(index) = (1.0 == data[dataIdx]) ? true : false; ++dataIdx;
637      data_i_index_dst_full(index) = (int)(data[dataIdx]);
638      if (0 <= data_i_index_dst_full(index))
639      {
640        data_i_index_dst_full(index) = index;
641        ++nbUnMaskedPointOnExtendedPart;
642      }
643    }
644  }
645
646
647  // Finally, update data_i_index
648  int nbUnMaskedPointOnNewDstDomain = (nbUnMaskedPointOnExtendedPart + nbUnMaskedPointOnLocalDomain);
649  int count = 0, dataIdx;
650  for (int idx = 0; idx < newNbLocalDst; ++idx)
651  {
652    dataIdx = data_i_index_dst_full(idx);
653    if ((0 <= dataIdx))
654    {
655      ++count;
656    }
657  }
658
659  data_i_index_dst.resize(count);
660  data_j_index_dst.resize(count);
661  data_j_index_dst = 0;
662
663  count = 0;
664  for (int idx = 0; idx < newNbLocalDst; ++idx)
665  {
666    dataIdx = data_i_index_dst_full(idx);
667    if ((0 <= dataIdx))
668    {
669      data_i_index_dst(count) = dataIdx;
670      ++count;
671    }
672  }
673
674  // Update ni
675  domainDestination->ni.setValue(newNbLocalDst);
676  domainDestination->mask_1d.resize(domainDestination->domainMask.numElements()) ;
677  domainDestination->mask_1d=domainDestination->domainMask ;
678  domainDestination->computeLocalMask() ;
679}
680CATCH
681
682/*!
683  Compute the index mapping between domain on grid source and one on grid destination
684*/
685void CDomainAlgorithmExpand::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
686{
687
688}
689
690}
Note: See TracBrowser for help on using the repository browser.