source: XIOS/dev/XIOS_DEV_CMIP6/src/transformation/domain_algorithm_expand.cpp @ 1211

Last change on this file since 1211 was 1158, checked in by oabramkina, 7 years ago

Two server levels: merging with trunk r1137.
There are bugs.

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