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

Last change on this file since 2270 was 2270, checked in by ymipsl, 3 years ago

Tracking memory leak :
Tranformations and algorithms are now managed with shared_ptr.

YM

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