source: XIOS/trunk/src/transformation/domain_algorithm_expand.cpp @ 941

Last change on this file since 941 was 941, checked in by mhnguyen, 8 years ago

Finishing the implementation of expand domain transformation

+) Make use of updated new functions of class Mesh to compute neighboring cells
+) Make change to some minor stuffs

Test
+) On Curie
+) The transformation works correctly

File size: 13.4 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 = elementPositionInGridDst2AxisPosition[elementPositionInGrid];
34  int domainSrcIndex = elementPositionInGridSrc2AxisPosition[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{
49  if (domainDestination == domainSource)
50  {
51    ERROR("CDomainAlgorithmExpand::CDomainAlgorithmExpand(CDomain* domainDestination,CDomain* domainSource, CExpandDomain* expandDomain)",
52           << "Domain source and domain destination are the same. Please make sure domain destination refers to domain source" << std::endl
53           << "Domain source " <<domainSource->getId() << std::endl
54           << "Domain destination " <<domainDestination->getId() << std::endl);
55  }
56
57  if (!domainDestination->hasRefTo(domainSource))
58  {
59    ERROR("CDomainAlgorithmExpand::CDomainAlgorithmExpand(CDomain* domainDestination,CDomain* domainSource, CExpandDomain* expandDomain)",
60           << "Domain domain destination must refer to domain source (directly or indirectly) by domain_ref " << std::endl
61           << "Domain source " <<domainSource->getId() << std::endl
62           << "Domain destination " <<domainDestination->getId() << std::endl);
63  }
64
65  this->type_ = (ELEMENT_MODIFICATION_WITH_DATA);
66  expandDomain->checkValid(domainDestination);
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
83/*!
84 *  Expand domain with edge-type neighbor
85 *  \param[in/out] domainDestination domain destination and will be modified
86 *  \param[in] domainSource domain source
87 */
88void CDomainAlgorithmExpand::expandDomainEdgeConnectivity(CDomain* domainDestination,
89                                                          CDomain* domainSource)
90{
91  CContext* context = CContext::getCurrent();
92  CContextClient* client=context->client;
93
94  CArray<double,2>& bounds_lon_src = domainSource->bounds_lon_1d;
95  CArray<double,2>& bounds_lat_src = domainSource->bounds_lat_1d;
96  CArray<int,2> neighborsSrc;
97
98  int type = 1; // For edge
99  CMesh mesh;
100  mesh.getGlobalNghbFaces(type, client->intraComm, domainSource->i_index, bounds_lon_src, bounds_lat_src, neighborsSrc);
101  updateDomainAttributes(domainDestination, domainSource, neighborsSrc);
102}
103
104/*!
105 *  Expand domain with node-type neighbor
106 *  \param[in/out] domainDestination domain destination and will be modified
107 *  \param[in] domainSource domain source
108 */
109void CDomainAlgorithmExpand::expandDomainNodeConnectivity(CDomain* domainDestination,
110                                                          CDomain* domainSource)
111{
112  CContext* context = CContext::getCurrent();
113  CContextClient* client=context->client;
114
115  CArray<double,2>& bounds_lon_src = domainSource->bounds_lon_1d;
116  CArray<double,2>& bounds_lat_src = domainSource->bounds_lat_1d;
117  CArray<int,2> neighborsSrc;
118
119  int type = 0; // For node
120  CMesh mesh;
121  mesh.getGlobalNghbFaces(type, client->intraComm, domainSource->i_index, bounds_lon_src, bounds_lat_src, neighborsSrc);
122  updateDomainAttributes(domainDestination, domainSource, neighborsSrc);
123}
124
125/*!
126 *  Extend domain destination and update its attributes
127 *  Suppose that domain destination and domain source have the same values for all attributes (by inheritance)
128 *  \param [in/out] domainDestination domain destination
129 *  \param [in] domainSource domain source
130 *  \param [in] neighborsDomainSrc domain extended part
131 */
132void CDomainAlgorithmExpand::updateDomainAttributes(CDomain* domainDestination,
133                                                    CDomain* domainSource,
134                                                    CArray<int,2>& neighborsDomainSrc)
135{
136  CContext* context = CContext::getCurrent();
137  CContextClient* client=context->client;
138
139  // First of all, "copy" all attributes of domain source to domain destination
140  StdString domainDstRef = (!domainDestination->domain_ref.isEmpty()) ? domainDestination->domain_ref.getValue()
141                                                                      : "";
142  domainDestination->domain_ref.setValue(domainSource->getId());
143  domainDestination->solveRefInheritance(true);
144  if (domainDstRef.empty()) domainDestination->domain_ref.reset();
145  else domainDestination->domain_ref.setValue(domainDstRef);
146
147  // Now extend domain destination
148  int niGlob = domainSource->ni_glo;
149  CArray<bool,1>& mask_1d_src = domainSource->mask_1d;
150  CArray<int,1>& i_index_src = domainSource->i_index;
151  CArray<double,1>& lon_src = domainSource->lonvalue_1d;
152  CArray<double,1>& lat_src = domainSource->latvalue_1d;
153  CArray<double,2>& bounds_lon_src = domainSource->bounds_lon_1d;
154  CArray<double,2>& bounds_lat_src = domainSource->bounds_lat_1d;
155  CArray<int,1>& data_i_index_src = domainSource->data_i_index;
156
157  int oldNbLocal = i_index_src.numElements(), index, globalIndex;
158  // Uncompress data_i_index
159  CArray<int,1> data_i_index_src_full(oldNbLocal);
160  int nbUnMaskedPointOnLocalDomain = 0;
161  data_i_index_src_full = -1; // Suppose all values are masked
162  for (int idx = 0; idx < data_i_index_src.numElements(); ++idx)
163  {
164    int dataIdx = data_i_index_src(idx);
165    if ((0 <= dataIdx) && (dataIdx < oldNbLocal))
166    {
167      data_i_index_src_full(nbUnMaskedPointOnLocalDomain) = dataIdx;
168      ++nbUnMaskedPointOnLocalDomain;
169    }
170  }
171
172  CArray<bool,1>& mask_1d_dst = domainDestination->mask_1d;
173  CArray<int,1>& i_index_dst = domainDestination->i_index;
174  CArray<int,1>& j_index_dst = domainDestination->j_index;
175  CArray<double,1>& lon_dst = domainDestination->lonvalue_1d;
176  CArray<double,1>& lat_dst = domainDestination->latvalue_1d;
177  CArray<double,2>& bounds_lon_dst = domainDestination->bounds_lon_1d;
178  CArray<double,2>& bounds_lat_dst = domainDestination->bounds_lat_1d;
179  CArray<int,1>& data_i_index_dst = domainDestination->data_i_index;
180  CArray<int,1>& data_j_index_dst = domainDestination->data_j_index;
181
182  // Resize all array-like attributes of domain destination
183  int nbNeighbor    = neighborsDomainSrc.shape()[1];
184  int newNbLocalDst = nbNeighbor + oldNbLocal;
185  int nVertex       = bounds_lon_dst.shape()[0];
186
187  mask_1d_dst.resizeAndPreserve(newNbLocalDst);
188  i_index_dst.resizeAndPreserve(newNbLocalDst);
189  j_index_dst.resizeAndPreserve(newNbLocalDst);
190  lon_dst.resizeAndPreserve(newNbLocalDst);
191  lat_dst.resizeAndPreserve(newNbLocalDst);
192  bounds_lon_dst.resizeAndPreserve(nVertex, newNbLocalDst);
193  bounds_lat_dst.resizeAndPreserve(nVertex, newNbLocalDst);
194  CArray<int,1> data_i_index_dst_full(newNbLocalDst);
195  data_i_index_dst_full(Range(0,oldNbLocal-1)) = data_i_index_src_full;
196  data_i_index_dst_full(Range(oldNbLocal,newNbLocalDst-1)) = -1;
197
198  // 1. Fill in array relating to global index (i_index, j_index, transmap, etc, ...)
199  // Global index mapping between destination and source
200  this->transformationMapping_.resize(1);
201  this->transformationWeight_.resize(1);
202  TransformationIndexMap& transMap = this->transformationMapping_[0];
203  TransformationWeightMap& transWeight = this->transformationWeight_[0];
204
205  transMap.rehash(std::ceil(newNbLocalDst/transMap.max_load_factor()));
206  transWeight.rehash(std::ceil(newNbLocalDst/transWeight.max_load_factor()));
207  // First, index mapping for local domain
208  for (int idx = 0; idx < oldNbLocal; ++idx)
209  {
210    index = i_index_dst(idx);
211    transMap[index].push_back(index);
212    transWeight[index].push_back(1.0);
213  }
214  // Then, index mapping for extended part
215  for (int idx = 0; idx < nbNeighbor; ++idx)
216  {
217    index = idx + oldNbLocal;
218    globalIndex = neighborsDomainSrc(0,idx);
219    i_index_dst(index) = globalIndex;
220    j_index_dst(index) = 0;
221    transMap[globalIndex].push_back(globalIndex);
222    transWeight[globalIndex].push_back(1.0);
223  }
224
225  // 2. Exchange local info among domains (lon,lat,bounds,mask,etc,...)
226  CClientClientDHTDouble::Index2VectorInfoTypeMap localData;
227  localData.rehash(std::ceil(oldNbLocal/localData.max_load_factor()));
228  // Information exchanged among domains (attention to their order), number in parentheses presents size of data
229  // lon(1) + lat(1) + bounds_lon(nVertex) + bounds_lat(nVertex) + mask(1) + data_i_index(1)
230  int dataPackageSize =  1 + 1 + // lon + lat
231                         nVertex + nVertex + //bounds_lon + bounds_lat
232                         1 + // mask_1d_dst;
233                         1; // data_i_index
234  // Initialize database
235  for (int idx = 0; idx < oldNbLocal; ++idx)
236  {
237    index = i_index_src(idx);
238    localData[index].resize(dataPackageSize);
239    std::vector<double>& data = localData[index];
240
241    //Pack data
242    int dataIdx = 0;
243    data[dataIdx] = lon_src(idx);++dataIdx;
244    data[dataIdx] = lat_src(idx);++dataIdx;
245    for (int i = 0; i < nVertex; ++i)
246    {
247      data[dataIdx] = bounds_lon_src(i,idx); ++dataIdx;
248    }
249    for (int i = 0; i < nVertex; ++i)
250    {
251      data[dataIdx] = bounds_lat_src(i,idx); ++dataIdx;
252    }
253    data[dataIdx] = mask_1d_src(idx) ? 1.0 : -1; ++dataIdx;
254    data[dataIdx] = data_i_index_src_full(idx);
255  }
256
257  CClientClientDHTDouble dhtData(localData,client->intraComm);
258  CArray<size_t,1> neighborInd(nbNeighbor);
259  for (int idx = 0; idx < nbNeighbor; ++idx)
260    neighborInd(idx) = neighborsDomainSrc(0,idx);
261
262  // Compute local data on other domains
263  dhtData.computeIndexInfoMapping(neighborInd);
264  CClientClientDHTDouble::Index2VectorInfoTypeMap& neighborData = dhtData.getInfoIndexMap();
265  CClientClientDHTDouble::Index2VectorInfoTypeMap::iterator ite = neighborData.end(), it;
266  // Ok get neighbor data
267  size_t nIdx;
268  int nbUnMaskedPointOnExtendedPart = 0;
269  for (int idx = 0; idx < nbNeighbor; ++idx)
270  {
271    nIdx  = neighborInd(idx);
272    it = neighborData.find(nIdx);
273    if (ite != it)
274    {
275      index = idx + oldNbLocal;
276      std::vector<double>& data = it->second;
277      // Unpack data
278      int dataIdx = 0;
279      lon_dst(index) = data[dataIdx]; ++dataIdx;
280      lat_dst(index) = data[dataIdx]; ++dataIdx;
281      for (int i = 0; i < nVertex; ++i)
282      {
283        bounds_lon_dst(i,index) = data[dataIdx]; ++dataIdx;
284      }
285      for (int i = 0; i < nVertex; ++i)
286      {
287        bounds_lat_dst(i,index) = data[dataIdx]; ++dataIdx;
288      }
289      mask_1d_dst(index) = (1.0 == data[dataIdx]) ? true : false; ++dataIdx;
290      data_i_index_dst_full(index) = (int)(data[dataIdx]);
291      if (0 <= data_i_index_dst_full(index))
292      {
293        data_i_index_dst_full(index) = index;
294        ++nbUnMaskedPointOnExtendedPart;
295      }
296    }
297  }
298
299
300  // Finally, update data_i_index
301  int nbUnMaskedPointOnNewDstDomain = (nbUnMaskedPointOnExtendedPart + nbUnMaskedPointOnLocalDomain);
302  int count = 0, dataIdx;
303  for (int idx = 0; idx < newNbLocalDst; ++idx)
304  {
305    dataIdx = data_i_index_dst_full(idx);
306    if ((0 <= dataIdx) && (dataIdx < newNbLocalDst))
307    {
308      ++count;
309    }
310  }
311
312  data_i_index_dst.resize(count);
313  data_j_index_dst.resize(count);
314  data_j_index_dst = 0;
315
316  count = 0;
317  for (int idx = 0; idx < newNbLocalDst; ++idx)
318  {
319    dataIdx = data_i_index_dst_full(idx);
320    if ((0 <= dataIdx) && (dataIdx < newNbLocalDst))
321    {
322      data_i_index_dst(count) = dataIdx;
323      ++count;
324    }
325  }
326
327  // Update ni
328  domainDestination->ni.setValue(newNbLocalDst);
329
330}
331
332
333/*!
334  Compute the index mapping between domain on grid source and one on grid destination
335*/
336void CDomainAlgorithmExpand::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs)
337{
338
339}
340
341}
Note: See TracBrowser for help on using the repository browser.