Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
TempestRemapper.cpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: TempestRemapper.hpp
5  *
6  * Description: Interface to the TempestRemap library to enable intersection and
7  * high-order conservative remapping of climate solution from
8  * arbitrary resolution of source and target grids on the sphere.
9  *
10  * Author: Vijay S. Mahadevan (vijaysm), [email protected]
11  *
12  * =====================================================================================
13  */
14 
15 #include <string>
16 #include <iostream>
17 #include <cassert>
18 #include <array>
19 #include <numeric> // std::iota
20 #include <algorithm> // std::sort, std::stable_sort
21 
22 #include "DebugOutput.hpp"
24 #include "moab/ReadUtilIface.hpp"
25 // needed for higher order mapping, retrieve additional layers of cells with bridge methods
26 #include "moab/MeshTopoUtil.hpp"
27 #include "AEntityFactory.hpp"
28 
29 // Intersection includes
32 
33 #include "moab/AdaptiveKDTree.hpp"
34 #include "moab/SpatialLocator.hpp"
35 
36 // skinner for augmenting overlap mesh to complete coverage
37 #include "moab/Skinner.hpp"
38 #include "MBParallelConventions.h"
39 
40 #ifdef MOAB_HAVE_TEMPESTREMAP
41 #include "Announce.h"
42 #include "FiniteElementTools.h"
43 #include "GaussLobattoQuadrature.h"
44 #endif
45 
46 // #define VERBOSE
47 
48 namespace moab
49 {
50 
51 ///////////////////////////////////////////////////////////////////////////////////
52 
53 ErrorCode TempestRemapper::initialize( bool initialize_fsets )
54 {
55  if( initialize_fsets )
56  {
60  }
61  else
62  {
63  m_source_set = 0;
64  m_target_set = 0;
65  m_overlap_set = 0;
66  }
67 
68  is_parallel = false;
69  is_root = true;
70  rank = 0;
71  size = 1;
72 #ifdef MOAB_HAVE_MPI
73  int flagInit;
74  MPI_Initialized( &flagInit );
75  if( flagInit )
76  {
77  assert( m_pcomm != nullptr );
78  rank = m_pcomm->rank();
79  size = m_pcomm->size();
80  is_root = ( rank == 0 );
81  is_parallel = ( size > 1 );
82  // is_parallel = true;
83  }
84  AnnounceOnlyOutputOnRankZero();
85 #endif
86 
87  m_source = nullptr;
88  m_target = nullptr;
89  m_overlap = nullptr;
90  m_covering_source = nullptr;
91 
92  point_cloud_source = false;
93  point_cloud_target = false;
94 
95  return MB_SUCCESS;
96 }
97 
98 ///////////////////////////////////////////////////////////////////////////////////
99 
101 {
102  this->clear();
103 }
104 
106 {
107  // destroy all meshes
108  if( m_source )
109  {
110  delete m_source;
111  m_source = nullptr;
112  }
113  if( m_target )
114  {
115  delete m_target;
116  m_target = nullptr;
117  }
118  if( m_overlap )
119  {
120  delete m_overlap;
121  m_overlap = nullptr;
122  }
123  if( m_covering_source && size > 1 )
124  {
125  delete m_covering_source;
126  m_covering_source = nullptr;
127  }
128 
129  point_cloud_source = false;
130  point_cloud_target = false;
131 
139  // gid_to_lid_src.clear();
140  // gid_to_lid_tgt.clear();
141  // gid_to_lid_covsrc.clear();
142  // lid_to_gid_src.clear();
143  // lid_to_gid_tgt.clear();
144  // lid_to_gid_covsrc.clear();
145 
146  return MB_SUCCESS;
147 }
148 
149 ///////////////////////////////////////////////////////////////////////////////////
150 
152  std::string inputFilename,
153  TempestMeshType type )
154 {
155  if( ctx == Remapper::SourceMesh )
156  {
157  m_source_type = type;
158  return load_tempest_mesh_private( inputFilename, &m_source );
159  }
160  else if( ctx == Remapper::TargetMesh )
161  {
162  m_target_type = type;
163  return load_tempest_mesh_private( inputFilename, &m_target );
164  }
165  else if( ctx != Remapper::DEFAULT )
166  {
167  m_overlap_type = type;
168  return load_tempest_mesh_private( inputFilename, &m_overlap );
169  }
170  else
171  {
172  MB_CHK_SET_ERR( MB_FAILURE, "Invalid IntersectionContext context provided" );
173  }
174 }
175 
176 ErrorCode TempestRemapper::load_tempest_mesh_private( std::string inputFilename, Mesh** tempest_mesh )
177 {
178  const bool outputEnabled = ( TempestRemapper::verbose && is_root );
179  if( outputEnabled ) std::cout << "\nLoading TempestRemap Mesh object from file = " << inputFilename << " ...\n";
180 
181  {
182  NcError error( NcError::silent_nonfatal );
183 
184  try
185  {
186  // Load input mesh
187  if( outputEnabled ) std::cout << "Loading mesh ...\n";
188  Mesh* mesh = new Mesh( inputFilename );
189  mesh->RemoveZeroEdges();
190  if( outputEnabled ) std::cout << "----------------\n";
191 
192  // Validate mesh
193  if( meshValidate )
194  {
195  if( outputEnabled ) std::cout << "Validating mesh ...\n";
196  mesh->Validate();
197  if( outputEnabled ) std::cout << "-------------------\n";
198  }
199 
200  // Construct the edge map on the mesh
201  if( constructEdgeMap )
202  {
203  if( outputEnabled ) std::cout << "Constructing edge map on mesh ...\n";
204  mesh->ConstructEdgeMap( false );
205  if( outputEnabled ) std::cout << "---------------------------------\n";
206  }
207 
208  if( tempest_mesh ) *tempest_mesh = mesh;
209  }
210  catch( Exception& e )
211  {
212  std::cout << "TempestRemap ERROR: " << e.ToString() << "\n";
213  return MB_FAILURE;
214  }
215  catch( ... )
216  {
217  return MB_FAILURE;
218  }
219  }
220  return MB_SUCCESS;
221 }
222 
223 ///////////////////////////////////////////////////////////////////////////////////
224 
226 {
227  const bool outputEnabled = ( TempestRemapper::verbose && is_root );
228  if( ctx == Remapper::SourceMesh )
229  {
230  if( outputEnabled ) std::cout << "Converting (source) TempestRemap Mesh object to MOAB representation ...\n";
233  }
234  else if( ctx == Remapper::TargetMesh )
235  {
236  if( outputEnabled ) std::cout << "Converting (target) TempestRemap Mesh object to MOAB representation ...\n";
239  }
240  else if( ctx != Remapper::DEFAULT )
241  {
242  if( outputEnabled ) std::cout << "Converting (overlap) TempestRemap Mesh object to MOAB representation ...\n";
244  }
245  else
246  {
247  MB_CHK_SET_ERR( MB_FAILURE, "Invalid IntersectionContext context provided" );
248  }
249 }
250 
251 #define NEW_CONVERT_LOGIC
252 
253 #ifdef NEW_CONVERT_LOGIC
255  Mesh* mesh,
256  EntityHandle& mesh_set,
257  Range& entities,
258  Range* vertices )
259 {
260  const bool outputEnabled = ( TempestRemapper::verbose && is_root );
261  const NodeVector& nodes = mesh->nodes;
262  const FaceVector& faces = mesh->faces;
263 
264  moab::DebugOutput dbgprint( std::cout, this->rank, 0 );
265  dbgprint.set_prefix( "[TempestToMOAB]: " );
266 
268  MB_CHK_SET_ERR( m_interface->query_interface( iface ), "Can't get reader interface" );
269 
270  Tag gidTag = m_interface->globalId_tag();
271 
272  // Set the data for the vertices
273  std::vector< double* > arrays;
274  std::vector< int > gidsv( nodes.size() );
275  EntityHandle startv;
276  MB_CHK_SET_ERR( iface->get_node_coords( 3, nodes.size(), 0, startv, arrays ), "Can't get node coords" );
277  for( unsigned iverts = 0; iverts < nodes.size(); ++iverts )
278  {
279  const Node& node = nodes[iverts];
280  arrays[0][iverts] = node.x;
281  arrays[1][iverts] = node.y;
282  arrays[2][iverts] = node.z;
283  gidsv[iverts] = iverts + 1;
284  }
285  Range mbverts( startv, startv + nodes.size() - 1 );
286  MB_CHK_SET_ERR( m_interface->add_entities( mesh_set, mbverts ), "Can't add entities" );
287  MB_CHK_SET_ERR( m_interface->tag_set_data( gidTag, mbverts, &gidsv[0] ), "Can't set global_id tag" );
288 
289  gidsv.clear();
290  entities.clear();
291 
292  Tag srcParentTag, tgtParentTag;
293  std::vector< int > srcParent( faces.size(), -1 ), tgtParent( faces.size(), -1 );
294  std::vector< int > gidse( faces.size(), -1 );
295  bool storeParentInfo = ( mesh->vecSourceFaceIx.size() > 0 );
296 
297  if( storeParentInfo )
298  {
299  int defaultInt = -1;
300  MB_CHK_SET_ERR( m_interface->tag_get_handle( "TargetParent", 1, MB_TYPE_INTEGER, tgtParentTag,
301  MB_TAG_DENSE | MB_TAG_CREAT, &defaultInt ),
302  "can't create positive tag" );
303 
304  MB_CHK_SET_ERR( m_interface->tag_get_handle( "SourceParent", 1, MB_TYPE_INTEGER, srcParentTag,
305  MB_TAG_DENSE | MB_TAG_CREAT, &defaultInt ),
306  "can't create negative tag" );
307  }
308 
309  // Let us first perform a full pass assuming arbitrary polygons. This is especially true for
310  // overlap meshes.
311  // 1. We do a first pass over faces, decipher edge size and group into categories based on
312  // element type
313  // 2. Next we loop over type, and add blocks of elements into MOAB
314  // 3. For each block within the loop, also update the connectivity of elements.
315  {
316  if( outputEnabled )
317  dbgprint.printf( 0, "..Mesh size: Nodes [%zu] Elements [%zu].\n", nodes.size(), faces.size() );
318  std::vector< EntityHandle > mbcells( faces.size() );
319  unsigned ntris = 0, nquads = 0, npolys = 0;
320  std::vector< EntityHandle > conn( 16 );
321 
322  for( unsigned ifaces = 0; ifaces < faces.size(); ++ifaces )
323  {
324  const Face& face = faces[ifaces];
325  const unsigned num_v_per_elem = face.edges.size();
326 
327  for( unsigned iedges = 0; iedges < num_v_per_elem; ++iedges )
328  {
329  conn[iedges] = startv + face.edges[iedges].node[0];
330  }
331 
332  switch( num_v_per_elem )
333  {
334  case 3:
335  // if( outputEnabled )
336  // dbgprint.printf( 0, "....Block %d: Triangular Elements [%u].\n", iBlock++, nPolys[iType] );
337  MB_CHK_SET_ERR( m_interface->create_element( MBTRI, &conn[0], num_v_per_elem, mbcells[ifaces] ),
338  "Can't get element connectivity" );
339  ntris++;
340  break;
341  case 4:
342  // if( outputEnabled )
343  // dbgprint.printf( 0, "....Block %d: Quadrilateral Elements [%u].\n", iBlock++, nPolys[iType] );
344  MB_CHK_SET_ERR( m_interface->create_element( MBQUAD, &conn[0], num_v_per_elem, mbcells[ifaces] ),
345  "Can't get element connectivity" );
346  nquads++;
347  break;
348  default:
349  // if( outputEnabled )
350  // dbgprint.printf( 0, "....Block %d: Polygonal [%u] Elements [%u].\n", iBlock++, iType,
351  // nPolys[iType] );
352  MB_CHK_SET_ERR( m_interface->create_element( MBPOLYGON, &conn[0], num_v_per_elem, mbcells[ifaces] ),
353  "Can't get element connectivity" );
354  npolys++;
355  break;
356  }
357 
358  gidse[ifaces] = ifaces + 1;
359 
360  if( storeParentInfo )
361  {
362  srcParent[ifaces] = mesh->vecSourceFaceIx[ifaces] + 1;
363  tgtParent[ifaces] = mesh->vecTargetFaceIx[ifaces] + 1;
364  }
365  }
366 
367  if( ntris ) dbgprint.printf( 0, "....Triangular Elements [%u].\n", ntris );
368  if( nquads ) dbgprint.printf( 0, "....Quadrangular Elements [%u].\n", nquads );
369  if( npolys ) dbgprint.printf( 0, "....Polygonal Elements [%u].\n", npolys );
370 
371  MB_CHK_SET_ERR( m_interface->add_entities( mesh_set, &mbcells[0], mbcells.size() ), "Could not add entities" );
372 
373  MB_CHK_SET_ERR( m_interface->tag_set_data( gidTag, &mbcells[0], mbcells.size(), &gidse[0] ),
374  "Can't set global_id tag" );
375  if( storeParentInfo )
376  {
377  MB_CHK_SET_ERR( m_interface->tag_set_data( srcParentTag, &mbcells[0], mbcells.size(), &srcParent[0] ),
378  "Can't set tag data" );
379  MB_CHK_SET_ERR( m_interface->tag_set_data( tgtParentTag, &mbcells[0], mbcells.size(), &tgtParent[0] ),
380  "Can't set tag data" );
381  }
382 
383  // insert from mbcells to entities to preserve ordering
384  std::copy( mbcells.begin(), mbcells.end(), range_inserter( entities ) );
385  }
386 
387  if( vertices ) *vertices = mbverts;
388 
389  return MB_SUCCESS;
390 }
391 
392 #else
394  Mesh* mesh,
395  EntityHandle& mesh_set,
396  Range& entities,
397  Range* vertices )
398 {
399  ErrorCode rval;
400 
401  const bool outputEnabled = ( TempestRemapper::verbose && is_root );
402  const NodeVector& nodes = mesh->nodes;
403  const FaceVector& faces = mesh->faces;
404 
405  moab::DebugOutput dbgprint( std::cout, this->rank, 0 );
406  dbgprint.set_prefix( "[TempestToMOAB]: " );
407 
409  MB_CHK_SET_ERR( m_interface->query_interface( iface ), "Can't get reader interface" );
410 
411  Tag gidTag = m_interface->globalId_tag();
412 
413  // Set the data for the vertices
414  std::vector< double* > arrays;
415  std::vector< int > gidsv( nodes.size() );
416  EntityHandle startv;
417  MB_CHK_SET_ERR( iface->get_node_coords( 3, nodes.size(), 0, startv, arrays ), "Can't get node coords" );
418  for( unsigned iverts = 0; iverts < nodes.size(); ++iverts )
419  {
420  const Node& node = nodes[iverts];
421  arrays[0][iverts] = node.x;
422  arrays[1][iverts] = node.y;
423  arrays[2][iverts] = node.z;
424  gidsv[iverts] = iverts + 1;
425  }
426  Range mbverts( startv, startv + nodes.size() - 1 );
427  MB_CHK_SET_ERR( m_interface->add_entities( mesh_set, mbverts ), "Can't add entities" );
428  MB_CHK_SET_ERR( m_interface->tag_set_data( gidTag, mbverts, &gidsv[0] ), "Can't set global_id tag" );
429 
430  gidsv.clear();
431  entities.clear();
432 
433  Tag srcParentTag, tgtParentTag;
434  std::vector< int > srcParent, tgtParent;
435  bool storeParentInfo = ( mesh->vecSourceFaceIx.size() > 0 );
436 
437  if( storeParentInfo )
438  {
439  int defaultInt = -1;
440  MB_CHK_SET_ERR( m_interface->tag_get_handle( "TargetParent", 1, MB_TYPE_INTEGER, tgtParentTag,
441  MB_TAG_DENSE | MB_TAG_CREAT, &defaultInt ),
442  "can't create positive tag" );
443 
444  MB_CHK_SET_ERR( m_interface->tag_get_handle( "SourceParent", 1, MB_TYPE_INTEGER, srcParentTag,
445  MB_TAG_DENSE | MB_TAG_CREAT, &defaultInt ),
446  "can't create negative tag" );
447  }
448 
449  // Let us first perform a full pass assuming arbitrary polygons. This is especially true for
450  // overlap meshes.
451  // 1. We do a first pass over faces, decipher edge size and group into categories based on
452  // element type
453  // 2. Next we loop over type, and add blocks of elements into MOAB
454  // 3. For each block within the loop, also update the connectivity of elements.
455  {
456  if( outputEnabled )
457  dbgprint.printf( 0, "..Mesh size: Nodes [%zu] Elements [%zu].\n", nodes.size(), faces.size() );
458  const int NMAXPOLYEDGES = 15;
459  std::vector< unsigned > nPolys( NMAXPOLYEDGES, 0 );
460  std::vector< std::vector< int > > typeNSeqs( NMAXPOLYEDGES );
461  for( unsigned ifaces = 0; ifaces < faces.size(); ++ifaces )
462  {
463  const int iType = faces[ifaces].edges.size();
464  nPolys[iType]++;
465  typeNSeqs[iType].push_back( ifaces );
466  }
467  int iBlock = 0;
468  for( unsigned iType = 0; iType < NMAXPOLYEDGES; ++iType )
469  {
470  if( !nPolys[iType] ) continue; // Nothing to do
471 
472  const unsigned num_v_per_elem = iType;
473  EntityHandle starte; // Connectivity
474  EntityHandle* conn;
475 
476  // Allocate the connectivity array, depending on the element type
477  switch( num_v_per_elem )
478  {
479  case 3:
480  if( outputEnabled )
481  dbgprint.printf( 0, "....Block %d: Triangular Elements [%u].\n", iBlock++, nPolys[iType] );
482  MB_CHK_SET_ERR( iface->get_element_connect( nPolys[iType], num_v_per_elem, MBTRI, 0, starte, conn ),
483  "Can't get element connectivity" );
484  break;
485  case 4:
486  if( outputEnabled )
487  dbgprint.printf( 0, "....Block %d: Quadrilateral Elements [%u].\n", iBlock++, nPolys[iType] );
488  MB_CHK_SET_ERR( iface->get_element_connect( nPolys[iType], num_v_per_elem, MBQUAD, 0, starte,
489  conn ),
490  "Can't get element connectivity" );
491  break;
492  default:
493  if( outputEnabled )
494  dbgprint.printf( 0, "....Block %d: Polygonal [%u] Elements [%u].\n", iBlock++, iType,
495  nPolys[iType] );
496  MB_CHK_SET_ERR( iface->get_element_connect( nPolys[iType], num_v_per_elem, MBPOLYGON, 0, starte,
497  conn ),
498  "Can't get element connectivity" );
499  break;
500  }
501 
502  Range mbcells( starte, starte + nPolys[iType] - 1 );
503  m_interface->add_entities( mesh_set, mbcells );
504 
505  if( storeParentInfo )
506  {
507  srcParent.resize( mbcells.size(), -1 );
508  tgtParent.resize( mbcells.size(), -1 );
509  }
510 
511  std::vector< int > gids( typeNSeqs[iType].size() );
512  for( unsigned ifaces = 0, offset = 0; ifaces < typeNSeqs[iType].size(); ++ifaces )
513  {
514  const int fIndex = typeNSeqs[iType][ifaces];
515  const Face& face = faces[fIndex];
516  // conn[offset++] = startv + face.edges[0].node[0];
517  for( unsigned iedges = 0; iedges < face.edges.size(); ++iedges )
518  {
519  conn[offset++] = startv + face.edges[iedges].node[0];
520  }
521 
522  if( storeParentInfo )
523  {
524  srcParent[ifaces] = mesh->vecSourceFaceIx[fIndex] + 1;
525  tgtParent[ifaces] = mesh->vecTargetFaceIx[fIndex] + 1;
526  }
527 
528  gids[ifaces] = typeNSeqs[iType][ifaces] + 1;
529  }
530  MB_CHK_SET_ERR( m_interface->tag_set_data( gidTag, mbcells, &gids[0] ), "Can't set global_id tag" );
531 
532  if( meshType == OVERLAP_FILES )
533  {
534  // Now let us update the adjacency data, because some elements are new
535  MB_CHK_SET_ERR( iface->update_adjacencies( starte, nPolys[iType], num_v_per_elem, conn ),
536  "Can't update adjacencies" );
537  // Generate all adj entities dimension 1 and 2 (edges and faces/ tri or qua)
538  Range edges;
539  MB_CHK_SET_ERR( m_interface->get_adjacencies( mbcells, 1, true, edges, Interface::UNION ),
540  "Can't get edges" );
541  }
542 
543  if( storeParentInfo )
544  {
545  MB_CHK_SET_ERR( m_interface->tag_set_data( srcParentTag, mbcells, &srcParent[0] ),
546  "Can't set tag data" );
547  MB_CHK_SET_ERR( m_interface->tag_set_data( tgtParentTag, mbcells, &tgtParent[0] ),
548  "Can't set tag data" );
549  }
550  entities.merge( mbcells );
551  }
552  }
553 
554  if( vertices ) *vertices = mbverts;
555 
556  return MB_SUCCESS;
557 }
558 #endif
559 
560 ///////////////////////////////////////////////////////////////////////////////////
561 
563 {
564  // now, let us ensure we have a valid source and target mesh objects
565  // if not, convert the source and target meshes to TempestRemap format
566  if( this->m_source == nullptr )
567  {
568  // Convert the covering mesh to TempestRemap format
570  "Can't convert source mesh to TempestRemap format" );
571  }
572  if( this->m_covering_source == nullptr )
573  {
574  // Convert the covering mesh to TempestRemap format
576  "Can't convert source coverage mesh to TempestRemap format" );
577  }
578  if( this->m_target == nullptr )
579  {
580  // Convert the covering mesh to TempestRemap format
582  "Can't convert target mesh to TempestRemap format" );
583  }
584 
585  return moab::MB_SUCCESS;
586 }
587 
589 {
590  const bool outputEnabled = ( TempestRemapper::verbose && is_root );
591 
592  // create a debug output object and set a prefix
593  moab::DebugOutput dbgprint( std::cout, this->rank, 0 );
594  dbgprint.set_prefix( "[MOABToTempest]: " );
595 
596  if( ctx == Remapper::SourceMesh )
597  {
598  if( !m_source ) m_source = new Mesh();
599  if( outputEnabled ) dbgprint.printf( 0, "Converting (source) MOAB to TempestRemap Mesh representation ...\n" );
602  "Can't convert source mesh to Tempest" );
603  if( m_source_entities.size() == 0 && m_source_vertices.size() != 0 )
604  {
605  this->point_cloud_source = true;
606  }
607  }
608  else if( ctx == Remapper::CoveringMesh )
609  {
610  if( !m_covering_source ) m_covering_source = new Mesh();
611  if( outputEnabled )
612  dbgprint.printf( 0, "Converting (covering source) MOAB to TempestRemap Mesh representation ...\n" );
615  "Can't convert convering source mesh to TempestRemap format" );
616  }
617  else if( ctx == Remapper::TargetMesh )
618  {
619  if( !m_target ) m_target = new Mesh();
620  if( outputEnabled ) dbgprint.printf( 0, "Converting (target) MOAB to TempestRemap Mesh representation ...\n" );
623  "Can't convert target mesh to Tempest" );
624  if( m_target_entities.size() == 0 && m_target_vertices.size() != 0 ) this->point_cloud_target = true;
625  }
626  else if( ctx == Remapper::OverlapMesh ) // Overlap mesh
627  {
628  if( !m_overlap ) m_overlap = new Mesh();
629  if( outputEnabled ) dbgprint.printf( 0, "Converting (overlap) MOAB to TempestRemap Mesh representation ...\n" );
630  MB_CHK_SET_ERR( ConvertOverlapMeshSourceOrdered(), "Can't convert overlap mesh to Tempest" );
631  }
632  else
633  {
634  MB_CHK_SET_ERR( MB_FAILURE, "Invalid IntersectionContext context provided" );
635  }
636 
637  return moab::MB_SUCCESS;
638 }
639 
641  EntityHandle mesh_set,
642  moab::Range& elems,
643  moab::Range* pverts )
644 {
645  NodeVector& nodes = mesh->nodes;
646  FaceVector& faces = mesh->faces;
647 
648  elems.clear();
649  MB_CHK_ERR( m_interface->get_entities_by_dimension( mesh_set, 2, elems ) );
650 
651  const size_t nelems = elems.size();
652 
653  // resize the number of elements in Tempest mesh
654  faces.resize( nelems );
655 
656  // let us now get the vertices from all the elements
657  Range verts;
658  MB_CHK_ERR( m_interface->get_connectivity( elems, verts ) );
659  if( verts.size() == 0 )
660  {
661  MB_CHK_ERR( m_interface->get_entities_by_dimension( mesh_set, 0, verts ) );
662  }
663  // assert(verts.size() > 0); // If not, this may be an invalid mesh ! possible for unbalanced loads
664 
665  std::map< EntityHandle, int > indxMap;
666  bool useRange = true;
667  if( verts.compactness() > 0.01 )
668  {
669  int j = 0;
670  for( Range::iterator it = verts.begin(); it != verts.end(); it++ )
671  indxMap[*it] = j++;
672  useRange = false;
673  }
674 
675  std::vector< int > globIds( nelems );
677  MB_CHK_ERR( m_interface->tag_get_data( gid, elems, &globIds[0] ) );
678  std::vector< size_t > sortedIdx;
679  if( offlineWorkflow )
680  {
681  sortedIdx.resize( nelems );
682  // initialize original index locations
683  std::iota( sortedIdx.begin(), sortedIdx.end(), 0 );
684  // sort indexes based on comparing values in v, using std::stable_sort instead of std::sort
685  // to avoid unnecessary index re-orderings when v contains elements of equal values
686  std::sort( sortedIdx.begin(), sortedIdx.end(),
687  [&globIds]( size_t i1, size_t i2 ) { return globIds[i1] < globIds[i2]; } );
688  }
689 
690  for( unsigned iface = 0; iface < nelems; ++iface )
691  {
692  Face& face = faces[iface];
693  EntityHandle ehandle = ( offlineWorkflow ? elems[sortedIdx[iface]] : elems[iface] );
694 
695  // get the connectivity for each edge
696  const EntityHandle* connectface;
697  int nnodesf;
698  MB_CHK_ERR( m_interface->get_connectivity( ehandle, connectface, nnodesf ) );
699  // account for padded polygons
700  while( connectface[nnodesf - 2] == connectface[nnodesf - 1] && nnodesf > 3 )
701  nnodesf--;
702 
703  face.edges.resize( nnodesf );
704  for( int iverts = 0; iverts < nnodesf; ++iverts )
705  {
706  int indx = ( useRange ? verts.index( connectface[iverts] ) : indxMap[connectface[iverts]] );
707  assert( indx >= 0 );
708  face.SetNode( iverts, indx );
709  }
710  }
711 
712  unsigned nnodes = verts.size();
713  nodes.resize( nnodes );
714 
715  // Set the data for the vertices
716  std::vector< double > coordx( nnodes ), coordy( nnodes ), coordz( nnodes );
717  MB_CHK_ERR( m_interface->get_coords( verts, &coordx[0], &coordy[0], &coordz[0] ) );
718  for( unsigned inode = 0; inode < nnodes; ++inode )
719  {
720  Node& node = nodes[inode];
721  node.x = coordx[inode];
722  node.y = coordy[inode];
723  node.z = coordz[inode];
724  }
725  coordx.clear();
726  coordy.clear();
727  coordz.clear();
728 
729  mesh->RemoveCoincidentNodes();
730  mesh->RemoveZeroEdges();
731 
732  // Generate reverse node array and edge map
733  if( constructEdgeMap ) mesh->ConstructEdgeMap( false );
734  // mesh->ConstructReverseNodeArray();
735 
736  // mesh->Validate();
737 
738  if( pverts )
739  {
740  pverts->clear();
741  *pverts = verts;
742  }
743  verts.clear();
744 
745  return MB_SUCCESS;
746 }
747 
748 ///////////////////////////////////////////////////////////////////////////////////
749 
750 bool IntPairComparator( const std::array< int, 3 >& a, const std::array< int, 3 >& b )
751 {
752  if( std::get< 1 >( a ) == std::get< 1 >( b ) )
753  return std::get< 2 >( a ) < std::get< 2 >( b );
754  else
755  return std::get< 1 >( a ) < std::get< 1 >( b );
756 }
757 
759 {
760  sharedGhostEntities.clear();
761 #ifdef MOAB_HAVE_MPI
762 
763  // Remove entities in the intersection mesh that are part of the ghosted overlap
764  if( is_parallel )
765  {
766  moab::Range allents;
767  MB_CHK_SET_ERR( m_interface->get_entities_by_dimension( m_overlap_set, 2, allents ),
768  "Getting entities dim 2 failed" );
769 
770  moab::Range sharedents;
771  moab::Tag ghostTag;
772  std::vector< int > ghFlags( allents.size() );
773  MB_CHK_ERR( m_interface->tag_get_handle( "ORIG_PROC", ghostTag ) );
774  MB_CHK_ERR( m_interface->tag_get_data( ghostTag, allents, &ghFlags[0] ) );
775  for( unsigned i = 0; i < allents.size(); ++i )
776  if( ghFlags[i] >= 0 ) // it means it is a ghost overlap element
777  sharedents.insert( allents[i] ); // this should not participate in smat!
778 
779  allents = subtract( allents, sharedents );
780 
781  // Get connectivity from all ghosted elements and filter out
782  // the vertices that are not owned
783  moab::Range ownedverts, sharedverts;
784  MB_CHK_SET_ERR( m_interface->get_connectivity( allents, ownedverts ), "Deleting entities dim 0 failed" );
785  MB_CHK_SET_ERR( m_interface->get_connectivity( sharedents, sharedverts ), "Deleting entities dim 0 failed" );
786  sharedverts = subtract( sharedverts, ownedverts );
787  // MB_CHK_SET_ERR( m_interface->remove_entities(m_overlap_set, sharedents), // "Deleting entities dim 2 failed" ); MB_CHK_SET_ERR( m_interface->remove_entities(m_overlap_set,
788  // sharedverts), "Deleting entities dim 0 failed" );
789 
790  sharedGhostEntities.merge( sharedents );
791  // sharedGhostEntities.merge(sharedverts);
792  }
793 #endif
794  return moab::MB_SUCCESS;
795 }
796 
798 {
801  size_t n_overlap_entitites = m_overlap_entities.size();
802 
803  // Allocate for the overlap mesh
804  if( !m_overlap ) m_overlap = new Mesh();
805 
806  std::vector< std::array< int, 3 > > sorted_overlap_order( n_overlap_entitites,
807  std::array< int, 3 >( { -1, -1, -1 } ) );
808  {
809  Tag srcParentTag, tgtParentTag;
810  MB_CHK_ERR( m_interface->tag_get_handle( "SourceParent", srcParentTag ) );
811  MB_CHK_ERR( m_interface->tag_get_handle( "TargetParent", tgtParentTag ) );
812  // Overlap mesh: resize the source and target connection arrays
813  m_overlap->vecTargetFaceIx.resize( n_overlap_entitites );
814  m_overlap->vecSourceFaceIx.resize( n_overlap_entitites );
815 
816  // We need a global to local numbering
817  Tag gidtag = m_interface->globalId_tag();
818  std::vector< int > gids_src( m_covering_source_entities.size(), -1 ), gids_tgt( m_target_entities.size(), -1 );
819  MB_CHK_ERR( m_interface->tag_get_data( gidtag, m_covering_source_entities, gids_src.data() ) );
820  MB_CHK_ERR( m_interface->tag_get_data( gidtag, m_target_entities, gids_tgt.data() ) );
821 
822 // #define USE_SORTED_GIDS
823 #ifdef USE_SORTED_GIDS
824  // let us sort the global indices so that we always have a consistent ordering
825  std::sort( gids_src.begin(), gids_src.end() );
826  std::sort( gids_tgt.begin(), gids_tgt.end() );
827 
828  auto find_lid = []( std::vector< int >& gids, int gid ) -> int {
829  // auto it = std::equal_range( gids.begin(), gids.end(), gid );
830  // return ( ( it.first != it.second ) ? std::distance( gids.begin(), it.first ) : -1 );
831 
832  auto it = std::lower_bound( gids.begin(), gids.end(), gid );
833  return ( it != gids.end() ? std::distance( gids.begin(), it ) : -1 );
834  };
835 #else
836  auto find_lid = []( std::vector< int >& gids, int gid ) -> int {
837  auto it = std::find( gids.begin(), gids.end(), gid );
838  return ( it != gids.end() ? std::distance( gids.begin(), it ) : -1 );
839  };
840 #endif
841 
842  std::vector< int > ghFlags;
843  if( is_parallel )
844  {
845  Tag ghostTag;
846  ghFlags.resize( n_overlap_entitites );
847  MB_CHK_SET_ERR( m_interface->tag_get_handle( "ORIG_PROC", ghostTag ), "Could not find ORIG_PROC tag" );
848  MB_CHK_ERR( m_interface->tag_get_data( ghostTag, m_overlap_entities, ghFlags.data() ) );
849  }
850 
851  // Overlap mesh: resize the source and target connection arrays
852  std::vector< int > rbids_src( n_overlap_entitites ), rbids_tgt( n_overlap_entitites );
853  MB_CHK_ERR( m_interface->tag_get_data( srcParentTag, m_overlap_entities, rbids_src.data() ) );
854  MB_CHK_ERR( m_interface->tag_get_data( tgtParentTag, m_overlap_entities, rbids_tgt.data() ) );
855 
856  for( size_t ix = 0; ix < n_overlap_entitites; ++ix )
857  {
858  std::get< 0 >( sorted_overlap_order[ix] ) = ix;
859  std::get< 1 >( sorted_overlap_order[ix] ) = find_lid( gids_src, rbids_src[ix] );
860  assert( std::get< 1 >( sorted_overlap_order[ix] ) >= 0 );
861  if( is_parallel && ghFlags[ix] >= 0 ) // it means it is a ghost overlap element
862  std::get< 2 >( sorted_overlap_order[ix] ) = -1; // this should not participate in the map!
863  else
864  std::get< 2 >( sorted_overlap_order[ix] ) = find_lid( gids_tgt, rbids_tgt[ix] );
865  }
866  // now sort the overlap elements such that they are ordered by source parent first
867  // and then target parent next
868  std::sort( sorted_overlap_order.begin(), sorted_overlap_order.end(), IntPairComparator );
869 
870  for( unsigned ie = 0; ie < n_overlap_entitites; ++ie )
871  {
872  // int ix = std::get< 0 >( sorted_overlap_order[ie] ); // original index of the element
873  m_overlap->vecSourceFaceIx[ie] = std::get< 1 >( sorted_overlap_order[ie] );
874  m_overlap->vecTargetFaceIx[ie] = std::get< 2 >( sorted_overlap_order[ie] );
875  }
876  }
877 
878  FaceVector& faces = m_overlap->faces;
879  faces.resize( n_overlap_entitites );
880 
881  Range verts;
882  // let us now get the vertices from all the elements
884 
885  std::map< EntityHandle, int > indxMap;
886  {
887  int j = 0;
888  for( Range::iterator it = verts.begin(); it != verts.end(); ++it )
889  indxMap[*it] = j++;
890  }
891 
892  for( unsigned ifac = 0; ifac < m_overlap_entities.size(); ++ifac )
893  {
894  const unsigned iface = std::get< 0 >( sorted_overlap_order[ifac] );
895  Face& face = faces[ifac];
897 
898  // get the connectivity for each edge
899  const EntityHandle* connectface;
900  int nnodesf;
901  MB_CHK_ERR( m_interface->get_connectivity( ehandle, connectface, nnodesf ) );
902 
903  face.edges.resize( nnodesf );
904  for( int iverts = 0; iverts < nnodesf; ++iverts )
905  {
906  int indx = indxMap[connectface[iverts]];
907  assert( indx >= 0 );
908  face.SetNode( iverts, indx );
909  }
910  }
911  indxMap.clear();
912  sorted_overlap_order.clear();
913 
914  unsigned nnodes = verts.size();
915  NodeVector& nodes = m_overlap->nodes;
916  nodes.resize( nnodes );
917 
918  // Set the data for the vertices
919  std::vector< double > coordx( nnodes ), coordy( nnodes ), coordz( nnodes );
920  MB_CHK_ERR( m_interface->get_coords( verts, &coordx[0], &coordy[0], &coordz[0] ) );
921  for( unsigned inode = 0; inode < nnodes; ++inode )
922  {
923  Node& node = nodes[inode];
924  node.x = coordx[inode];
925  node.y = coordy[inode];
926  node.z = coordz[inode];
927  }
928  coordx.clear();
929  coordy.clear();
930  coordz.clear();
931  verts.clear();
932 
933  // m_overlap->RemoveZeroEdges();
934  // m_overlap->RemoveCoincidentNodes( false );
935 
936  // Generate reverse node array and edge map
937  // if ( constructEdgeMap ) m_overlap->ConstructEdgeMap(false);
938  // m_overlap->ConstructReverseNodeArray();
939 
940  m_overlap->Validate();
941  return MB_SUCCESS;
942 }
943 
944 ///////////////////////////////////////////////////////////////////////////////////
945 
947  const bool fAllParallel,
948  const bool fInputConcave,
949  const bool fOutputConcave )
950 {
951  // Let us alos write out the TempestRemap equivalent so that we can do some verification checks
952  if( fAllParallel )
953  {
954  if( is_root && size == 1 )
955  {
956  this->m_source->CalculateFaceAreas( fInputConcave );
957  this->m_target->CalculateFaceAreas( fOutputConcave );
958  this->m_overlap->Write( strOutputFileName.c_str(), NcFile::Netcdf4 );
959  }
960  else
961  {
962  // Perform reduction and write from root processor
963  // if ( is_root )
964  // std::cout << "--- PARALLEL IMPLEMENTATION is NOT AVAILABLE yet ---\n";
965 
966  this->m_source->CalculateFaceAreas( fInputConcave );
967  this->m_covering_source->CalculateFaceAreas( fInputConcave );
968  this->m_target->CalculateFaceAreas( fOutputConcave );
969  this->m_overlap->Write( strOutputFileName.c_str(), NcFile::Netcdf4 );
970  }
971  }
972  else
973  {
974  this->m_source->CalculateFaceAreas( fInputConcave );
975  this->m_target->CalculateFaceAreas( fOutputConcave );
976  this->m_overlap->Write( strOutputFileName.c_str(), NcFile::Netcdf4 );
977  }
978 
979  return moab::MB_SUCCESS;
980 }
981 
982 void TempestRemapper::SetMeshSet( Remapper::IntersectionContext ctx /* Remapper::CoveringMesh*/,
983  moab::EntityHandle mset,
984  moab::Range* entities )
985 {
986 
987  if( ctx == Remapper::SourceMesh ) // should not be used
988  {
989  m_source_set = mset;
990  if( entities ) m_source_entities = *entities;
991  }
992  else if( ctx == Remapper::TargetMesh )
993  {
994  m_target_set = mset;
995  if( entities ) m_target_entities = *entities;
996  }
997  else if( ctx == Remapper::CoveringMesh )
998  {
999  m_covering_source_set = mset;
1000  if( entities ) m_covering_source_entities = *entities;
1001  }
1002  else
1003  {
1004  // nothing to do really..
1005  return;
1006  }
1007 }
1008 
1009 ///////////////////////////////////////////////////////////////////////////////////
1010 
1011 #ifndef MOAB_HAVE_MPI
1013  EntityHandle this_set,
1014  const int dimension,
1015  const int start_id )
1016 {
1017  assert( idtag );
1018 
1019  ErrorCode rval;
1020  Range entities;
1021  MB_CHK_SET_ERR( m_interface->get_entities_by_dimension( this_set, dimension, entities ), "Failed to get entities" );
1022 
1023  if( entities.size() == 0 ) return moab::MB_SUCCESS;
1024 
1025  int idoffset = start_id;
1026  std::vector< int > gid( entities.size() );
1027  for( unsigned i = 0; i < entities.size(); ++i )
1028  gid[i] = idoffset++;
1029 
1030  MB_CHK_ERR( m_interface->tag_set_data( idtag, entities, &gid[0] ) );
1031 
1032  return moab::MB_SUCCESS;
1033 }
1034 #endif
1035 
1036 ///////////////////////////////////////////////////////////////////////////////
1037 
1038 // Create a custom comparator for Nodes
1039 bool operator<( Node const& lhs, Node const& rhs )
1040 {
1041  return std::pow( lhs.x - rhs.x, 2.0 ) + std::pow( lhs.y - rhs.y, 2.0 ) + std::pow( lhs.z - rhs.z, 2.0 );
1042 }
1043 
1045  moab::Range& ents,
1046  moab::Range* secondary_ents,
1047  const std::string& dofTagName,
1048  int nP )
1049 {
1050  const int csResolution = std::sqrt( ntot_elements / 6.0 );
1051  if( csResolution * csResolution * 6 != ntot_elements ) return MB_INVALID_SIZE;
1052 
1053  // Create a temporary Cubed-Sphere mesh
1054  // NOTE: This will not work for RRM grids. Need to run HOMME for that case anyway
1055  Mesh csMesh;
1056  if( GenerateCSMesh( csMesh, csResolution, "", "NetCDF4" ) )
1057  MB_CHK_SET_ERR( moab::MB_FAILURE, // unsuccessful call
1058  "Failed to generate CS mesh through TempestRemap" );
1059 
1060  // let us now generate the mesh metadata
1061  if( this->GenerateMeshMetadata( csMesh, ntot_elements, ents, secondary_ents, dofTagName, nP ) )
1062  MB_CHK_SET_ERR( moab::MB_FAILURE, "Failed in call to GenerateMeshMetadata" ); // unsuccessful call
1063 
1064  return moab::MB_SUCCESS;
1065 }
1066 
1068  const int ntot_elements,
1069  moab::Range& ents,
1070  moab::Range* secondary_ents,
1071  const std::string dofTagName,
1072  int nP )
1073 {
1074  Tag dofTag;
1075  bool created = false;
1076  MB_CHK_SET_ERR( m_interface->tag_get_handle( dofTagName.c_str(), nP * nP, MB_TYPE_INTEGER, dofTag,
1077  MB_TAG_DENSE | MB_TAG_CREAT, 0, &created ),
1078  "Failed creating DoF tag" );
1079 
1080  // Number of Faces
1081  int nElements = static_cast< int >( csMesh.faces.size() );
1082 
1083  if( nElements != ntot_elements ) return MB_INVALID_SIZE;
1084 
1085  // Initialize data structures
1086  DataArray3D< int > dataGLLnodes;
1087  dataGLLnodes.Allocate( nP, nP, nElements );
1088 
1089  std::map< Node, int > mapNodes;
1090  std::map< Node, moab::EntityHandle > mapLocalMBNodes;
1091 
1092  // GLL Quadrature nodes
1093  DataArray1D< double > dG;
1094  DataArray1D< double > dW;
1095  GaussLobattoQuadrature::GetPoints( nP, 0.0, 1.0, dG, dW );
1096 
1097  moab::Range entities( ents );
1098  if( secondary_ents ) entities.insert( secondary_ents->begin(), secondary_ents->end() );
1099  double elcoords[3];
1100  for( unsigned iel = 0; iel < entities.size(); ++iel )
1101  {
1102  EntityHandle eh = entities[iel];
1103  MB_CHK_SET_ERR( m_interface->get_coords( &eh, 1, elcoords ), "failed to get element coordinates" );
1104  Node elCentroid( elcoords[0], elcoords[1], elcoords[2] );
1105  mapLocalMBNodes.insert( std::pair< Node, moab::EntityHandle >( elCentroid, eh ) );
1106  }
1107 
1108  // Build a Kd-tree for local mesh (nearest neighbor searches)
1109  // Loop over all elements in CS-Mesh
1110  // Then find if current centroid is in an element
1111  // If yes - then let us compute the DoF numbering and set to tag data
1112  // If no - then compute DoF numbering BUT DO NOT SET to tag data
1113  // continue
1114  int* dofIDs = new int[nP * nP];
1115 
1116  // Write metadata
1117  for( int k = 0; k < nElements; k++ )
1118  {
1119  const Face& face = csMesh.faces[k];
1120  const NodeVector& nodes = csMesh.nodes;
1121 
1122  if( face.edges.size() != 4 )
1123  {
1124  _EXCEPTIONT( "Mesh must only contain quadrilateral elements" );
1125  }
1126 
1127  Node centroid;
1128  centroid.x = centroid.y = centroid.z = 0.0;
1129  for( unsigned l = 0; l < face.edges.size(); ++l )
1130  {
1131  centroid.x += nodes[face[l]].x;
1132  centroid.y += nodes[face[l]].y;
1133  centroid.z += nodes[face[l]].z;
1134  }
1135  const double factor = 1.0 / face.edges.size();
1136  centroid.x *= factor;
1137  centroid.y *= factor;
1138  centroid.z *= factor;
1139 
1140  bool locElem = false;
1141  EntityHandle current_eh;
1142  if( mapLocalMBNodes.find( centroid ) != mapLocalMBNodes.end() )
1143  {
1144  locElem = true;
1145  current_eh = mapLocalMBNodes[centroid];
1146  }
1147 
1148  for( int j = 0; j < nP; j++ )
1149  {
1150  for( int i = 0; i < nP; i++ )
1151  {
1152  // Get local map vectors
1153  Node nodeGLL;
1154  Node dDx1G;
1155  Node dDx2G;
1156 
1157  // ApplyLocalMap(
1158  // face,
1159  // nodevec,
1160  // dG[i],
1161  // dG[j],
1162  // nodeGLL,
1163  // dDx1G,
1164  // dDx2G);
1165  const double& dAlpha = dG[i];
1166  const double& dBeta = dG[j];
1167 
1168  // Calculate nodal locations on the plane
1169  double dXc = nodes[face[0]].x * ( 1.0 - dAlpha ) * ( 1.0 - dBeta ) +
1170  nodes[face[1]].x * dAlpha * ( 1.0 - dBeta ) + nodes[face[2]].x * dAlpha * dBeta +
1171  nodes[face[3]].x * ( 1.0 - dAlpha ) * dBeta;
1172 
1173  double dYc = nodes[face[0]].y * ( 1.0 - dAlpha ) * ( 1.0 - dBeta ) +
1174  nodes[face[1]].y * dAlpha * ( 1.0 - dBeta ) + nodes[face[2]].y * dAlpha * dBeta +
1175  nodes[face[3]].y * ( 1.0 - dAlpha ) * dBeta;
1176 
1177  double dZc = nodes[face[0]].z * ( 1.0 - dAlpha ) * ( 1.0 - dBeta ) +
1178  nodes[face[1]].z * dAlpha * ( 1.0 - dBeta ) + nodes[face[2]].z * dAlpha * dBeta +
1179  nodes[face[3]].z * ( 1.0 - dAlpha ) * dBeta;
1180 
1181  double dR = sqrt( dXc * dXc + dYc * dYc + dZc * dZc );
1182 
1183  // Mapped node location
1184  nodeGLL.x = dXc / dR;
1185  nodeGLL.y = dYc / dR;
1186  nodeGLL.z = dZc / dR;
1187 
1188  // Determine if this is a unique Node
1189  std::map< Node, int >::const_iterator iter = mapNodes.find( nodeGLL );
1190  if( iter == mapNodes.end() )
1191  {
1192  // Insert new unique node into map
1193  int ixNode = static_cast< int >( mapNodes.size() );
1194  mapNodes.insert( std::pair< Node, int >( nodeGLL, ixNode ) );
1195  dataGLLnodes[j][i][k] = ixNode + 1;
1196  }
1197  else
1198  {
1199  dataGLLnodes[j][i][k] = iter->second + 1;
1200  }
1201 
1202  dofIDs[j * nP + i] = dataGLLnodes[j][i][k];
1203  }
1204  }
1205 
1206  if( locElem )
1207  {
1208  MB_CHK_SET_ERR( m_interface->tag_set_data( dofTag, &current_eh, 1, dofIDs ),
1209  "Failed to tag_set_data for DoFs" );
1210  }
1211  }
1212 
1213  // clear memory
1214  delete[] dofIDs;
1215  mapLocalMBNodes.clear();
1216  mapNodes.clear();
1217 
1218  return moab::MB_SUCCESS;
1219 }
1220 
1221 ///////////////////////////////////////////////////////////////////////////////////
1222 
1223 //#define MOAB_DBG
1225  double radius_src,
1226  double radius_tgt,
1227  double boxeps,
1228  bool regional_mesh,
1229  bool gnomonic,
1230  int nb_ghost_layers )
1231 {
1232  if( nb_ghost_layers >= 1 ) gnomonic = false;
1233  rrmgrids = regional_mesh;
1234  moab::Range local_verts;
1235 
1236  // Initialize intersection context
1238 
1240  mbintx->set_radius_source_mesh( radius_src );
1241  mbintx->set_radius_destination_mesh( radius_tgt );
1242  mbintx->set_box_error( boxeps );
1243 #ifdef MOAB_HAVE_MPI
1244  mbintx->set_parallel_comm( m_pcomm );
1245 #endif
1246 
1247  // compute the maxiumum edges in elements comprising source and target mesh
1249 
1252 
1253  // Note: lots of communication possible, if mesh is distributed very differently
1254 #ifdef MOAB_HAVE_MPI
1255  if( is_parallel )
1256  {
1257  MB_CHK_ERR( mbintx->build_processor_euler_boxes( m_target_set, local_verts, gnomonic ) );
1258 
1260  "Can't create new set" );
1261 
1262  MB_CHK_ERR( mbintx->construct_covering_set( m_source_set, m_covering_source_set, gnomonic, nb_ghost_layers ) );
1263 #ifdef MOAB_DBG
1264  std::stringstream filename;
1265  filename << "covering" << rank << ".h5m";
1266  MB_CHK_ERR( m_interface->write_file( filename.str().c_str(), 0, 0, &m_covering_source_set, 1 ) );
1267  std::stringstream targetFile;
1268  targetFile << "target" << rank << ".h5m";
1269  MB_CHK_ERR( m_interface->write_file( targetFile.str().c_str(), 0, 0, &m_target_set, 1 ) );
1270 #endif
1271  }
1272  else
1273  {
1274 #endif
1275  if( rrmgrids )
1276  {
1278  "Can't create new set" );
1279 
1280  double tolerance = 1e-6, btolerance = 1e-3;
1282  moab::Range targetVerts;
1283 
1284  MB_CHK_ERR( m_interface->get_connectivity( m_target_entities, targetVerts, true ) );
1285 
1287 
1288  for( unsigned ie = 0; ie < targetVerts.size(); ++ie )
1289  {
1290  EntityHandle el = targetVerts[ie], leaf;
1291  double point[3];
1292 
1293  // Get the element centroid to be queried
1294  MB_CHK_ERR( m_interface->get_coords( &el, 1, point ) );
1295 
1296  // Search for the closest source element in the master mesh corresponding
1297  // to the target element centroid in the slave mesh
1298  MB_CHK_ERR( tree.point_search( point, leaf, tolerance, btolerance ) );
1299 
1300  if( leaf == 0 )
1301  {
1302  leaf = m_source_set; // no hint
1303  }
1304 
1305  std::vector< moab::EntityHandle > leaf_elems;
1306  // We only care about the dimension that the user specified.
1307  // MOAB partitions are ordered by elements anyway.
1308  MB_CHK_ERR( m_interface->get_entities_by_dimension( leaf, 2, leaf_elems ) );
1309 
1310  if( !leaf_elems.size() )
1311  {
1312  // std::cout << ie << ": " << " No leaf elements found." << std::endl;
1313  continue;
1314  }
1315 
1316  // Now get the master element centroids so that we can compute
1317  // the minimum distance to the target point
1318  std::vector< double > centroids( leaf_elems.size() * 3 );
1319  MB_CHK_ERR( m_interface->get_coords( &leaf_elems[0], leaf_elems.size(), &centroids[0] ) );
1320 
1321  double dist = 1e5;
1322  int pinelem = -1;
1323  for( size_t il = 0; il < leaf_elems.size(); ++il )
1324  {
1325  const double* centroid = &centroids[il * 3];
1326  const double locdist = std::pow( point[0] - centroid[0], 2 ) +
1327  std::pow( point[1] - centroid[1], 2 ) +
1328  std::pow( point[2] - centroid[2], 2 );
1329 
1330  if( locdist < dist )
1331  {
1332  dist = locdist;
1333  pinelem = il;
1334  m_covering_source_entities.insert( leaf_elems[il] );
1335  }
1336  }
1337 
1338  if( pinelem < 0 )
1339  {
1340  std::cout << ie
1341  << ": [Error] - Could not find a minimum distance within the leaf "
1342  "nodes. Dist = "
1343  << dist << std::endl;
1344  }
1345  }
1346  // MB_CHK_ERR( tree.reset_tree() );
1347  std::cout << "[INFO] - Total covering source entities = " << m_covering_source_entities.size() << std::endl;
1349  }
1350  else
1351  {
1354  m_covering_source_entities = m_source_entities; // this is a tempest mesh object; careful about
1355  // incrementing the reference?
1356  m_covering_source_vertices = m_source_vertices; // this is a tempest mesh object; careful about
1357  // incrementing the reference?
1358  }
1359 #ifdef MOAB_HAVE_MPI
1360  }
1361 #endif
1362 
1363  // Convert the source, target and coverage meshes to TempestRemap format
1365 
1366  return moab::MB_SUCCESS;
1367 }
1368 #undef MOAB_DBG
1369 //#define MOAB_DBG
1370 
1371 ErrorCode TempestRemapper::ComputeOverlapMesh( bool kdtree_search, bool use_tempest )
1372 {
1373  const bool outputEnabled = ( this->rank == 0 );
1374  moab::DebugOutput dbgprint( std::cout, this->rank, 0 );
1375  dbgprint.set_prefix( "[ComputeOverlapMesh]: " );
1376 
1377  //
1378  // Create the intersection on the sphere object and set up necessary parameters
1379  //
1380  // First, split based on whether to use TempestRemap or MOAB for intersection
1381  // If TempestRemap,
1382  // 1) Check for valid Mesh and pointers to objects for source/target
1383  // 2) Invoke GenerateOverlapWithMeshes routine from Tempest library
1384  // If MOAB,
1385  // 1) Check for valid source and target meshsets (and entities)
1386  // 2) Build processor bounding boxes and construct a covering set
1387  // 3) Perform intersection between the source (covering) and target entities
1388  if( use_tempest )
1389  {
1390  // Now let us construct the overlap mesh, by calling TempestRemap interface directly
1391  // For the overlap method, choose between: "fuzzy", "exact" or "mixed"
1392  assert( m_covering_source != nullptr );
1393  assert( m_target != nullptr );
1394  if( m_overlap != nullptr ) delete m_overlap;
1395  bool concaveMeshA = false, concaveMeshB = false;
1396  // we have reset the overlap mesh - allocate now
1397  m_overlap = new Mesh();
1398  // Generate the overlap mesh using TempestRemap
1399  if( GenerateOverlapWithMeshes( *m_covering_source, *m_target, *m_overlap, "" /*outFilename*/, "Netcdf4",
1400  "exact", concaveMeshA, concaveMeshB, true, false ) )
1401  MB_CHK_SET_ERR( MB_FAILURE, "TempestRemap: cannot compute the intersection of meshes on the sphere" );
1402  }
1403  else
1404  {
1405  // Now perform the actual parallel intersection between the source and the target meshes
1406  if( kdtree_search )
1407  {
1408  if( outputEnabled ) dbgprint.printf( 0, "Computing intersection mesh with the Kd-tree search algorithm" );
1410  "Can't compute the intersection of meshes on the sphere with brute-force" );
1411  }
1412  else
1413  {
1414  if( outputEnabled )
1415  dbgprint.printf( 0, "Computing intersection mesh with the advancing-front propagation algorithm" );
1417  "Can't compute the intersection of meshes on the sphere" );
1418  }
1419 
1420 #ifdef MOAB_HAVE_MPI
1421  if( is_parallel || rrmgrids )
1422  {
1423 #ifdef VERBOSE
1424  std::stringstream ffc, fft, ffo;
1425  ffc << "cover_" << rank << ".h5m";
1426  fft << "target_" << rank << ".h5m";
1427  ffo << "intx_" << rank << ".h5m";
1428  MB_CHK_ERR( m_interface->write_mesh( ffc.str().c_str(), &m_covering_source_set, 1 ) );
1429  MB_CHK_ERR( m_interface->write_mesh( fft.str().c_str(), &m_target_set, 1 ) );
1430  MB_CHK_ERR( m_interface->write_mesh( ffo.str().c_str(), &m_overlap_set, 1 ) );
1431 #endif
1432  // because we do not want to work with elements in coverage set that do not participate
1433  // in intersection, remove them from the coverage set we will not delete them yet, just
1434  // remove from the set !
1435  if( !point_cloud_target )
1436  {
1437  Range covEnts;
1439 
1440  std::map< int, int > loc_gid_to_lid_covsrc;
1441  std::vector< int > gids( covEnts.size(), -1 );
1442 
1443  Tag gidtag = m_interface->globalId_tag();
1444  MB_CHK_ERR( m_interface->tag_get_data( gidtag, covEnts, gids.data() ) );
1445 
1446  for( unsigned ie = 0; ie < gids.size(); ++ie )
1447  {
1448  assert( gids[ie] > 0 );
1449  loc_gid_to_lid_covsrc[gids[ie]] = ie;
1450  }
1451 
1452  Range intxCov, intxCells;
1453  Tag srcParentTag;
1454  MB_CHK_ERR( m_interface->tag_get_handle( "SourceParent", srcParentTag ) );
1456  for( Range::iterator it = intxCells.begin(); it != intxCells.end(); it++ )
1457  {
1458  EntityHandle intxCell = *it;
1459  int srcParent = -1;
1460  MB_CHK_ERR( m_interface->tag_get_data( srcParentTag, &intxCell, 1, &srcParent ) );
1461 
1462  assert( srcParent >= 0 );
1463  intxCov.insert( covEnts[loc_gid_to_lid_covsrc[srcParent]] );
1464  }
1465 
1466  Range notNeededCovCells = moab::subtract( covEnts, intxCov );
1467 
1468  // now let us get only the covering entities that participate in intersection mesh
1469  covEnts = moab::subtract( covEnts, notNeededCovCells );
1470 
1471  // in order for getting 1-ring neighborhood, we need to be sure that the adjacencies are updated (created)
1472  if( false )
1473  {
1474  // update all adjacency list
1475  Core* mb = dynamic_cast< Core* >( m_interface );
1476  AEntityFactory* adj_fact = mb->a_entity_factory();
1477  if( !adj_fact->vert_elem_adjacencies() )
1478  adj_fact->create_vert_elem_adjacencies();
1479  else
1480  {
1481  for( Range::iterator it = covEnts.begin(); it != covEnts.end(); ++it )
1482  {
1483  EntityHandle eh = *it;
1484  const EntityHandle* conn = nullptr;
1485  int num_nodes = 0;
1486  MB_CHK_ERR( mb->get_connectivity( eh, conn, num_nodes ) );
1487  adj_fact->notify_create_entity( eh, conn, num_nodes );
1488  }
1489  }
1490 
1491  // next, for elements on the edge of the partition, get one ring adjacencies
1492  Skinner skinner( mb );
1493  Range skin;
1494  MB_CHK_SET_ERR( skinner.find_skin( m_covering_source_set, covEnts, false, skin ),
1495  "Unable to find skin" );
1496  for( Range::iterator it = skin.begin(); it != skin.end(); ++it )
1497  {
1498  const EntityHandle* conn = nullptr;
1499  int len = 0;
1500  MB_CHK_ERR( mb->get_connectivity( *it, conn, len, false ) );
1501  for( int ie = 0; ie < len; ++ie )
1502  {
1503  std::vector< EntityHandle > adjacent_entities;
1504  MB_CHK_ERR( adj_fact->get_adjacencies( conn[ie], 2, false, adjacent_entities ) );
1505  for( auto ent : adjacent_entities )
1506  notNeededCovCells.erase( ent ); // ent is part of the 1-ring neighborhood
1507  }
1508  }
1509 
1511  std::string( "sourcecoveragemesh_p" + std::to_string( rank ) + ".h5m" ).c_str(),
1512  &m_covering_source_set, 1 ) );
1513  }
1514 
1515  // remove now from coverage set the cells that are not needed
1516  // MB_CHK_ERR( m_interface->remove_entities( m_covering_source_set, notNeededCovCells ) );
1517 
1518  // Need to loop over covEnts now and ensure at least N-rings are available dependign on whether bilinear (1) or
1519  // high order FV (p) methods are being used for map generation. For bilinear/FV(1): need 1 ring, and for FV(p)
1520  // need p=ring neighborhood to recover exact conservation and consistency wrt serial/parallel.
1521 #ifdef VERBOSE
1522  std::cout << " total participating elements in the covering set: " << intxCov.size() << "\n";
1523  std::cout << " remove from coverage set elements that are not intersected: " << notNeededCovCells.size()
1524  << "\n";
1525 #endif
1526  if( size > 1 )
1527  {
1528  // some source elements cover multiple target partitions; the conservation logic
1529  // requires to know all overlap elements for a source element; they need to be
1530  // communicated from the other target partitions
1531  //
1532  // so first we have to identify source (coverage) elements that cover multiple
1533  // target partitions
1534  //
1535  // we will then mark the source, we will need to migrate the overlap elements
1536  // that cover this to the original source for the source element; then
1537  // distribute the overlap elements to all processors that have the coverage mesh
1538  // used
1540  }
1541  }
1542  }
1543 #endif
1544 
1545  // Fix any inconsistencies in the overlap mesh
1546  {
1547  IntxAreaUtils areaAdaptor;
1549  MB_CHK_ERR( areaAdaptor.positive_orientation( m_interface, m_overlap_set, 1.0 /*radius*/ ) );
1550  }
1551 
1552  // free the memory
1553  delete mbintx;
1554  }
1555 
1556  // Now, let us convert the overlap mesh to MOAB format so that we have a consistent interface
1557  MB_CHK_SET_ERR( ConvertOverlapMeshSourceOrdered(), "Can't convert overlap TempestRemap mesh to MOAB format" );
1558 
1559  return MB_SUCCESS;
1560 }
1561 
1562 #ifdef MOAB_HAVE_MPI
1563 
1564 // this function is called only in parallel
1565 ///////////////////////////////////////////////////////////////////////////////////
1567 {
1568  /*
1569  * overall strategy:
1570  *
1571  * 1) collect all boundary target cells on the current task, affected by the partition boundary;
1572  * note: not only partition boundary, we need all boundary (all coastal lines) and partition
1573  * boundary targetBoundaryIds is the set of target boundary cell IDs
1574  *
1575  * 2) collect all source cells that are intersecting boundary cells (call them
1576  * affectedSourceCellsIds)
1577  *
1578  * 3) collect overlap, that is accumulate all overlap cells that have source target in
1579  * affectedSourceCellsIds
1580  */
1581  // first, get all edges on the partition boundary, on the target mesh, then all the target
1582  // elements that border the partition boundary
1583  Skinner skinner( m_interface );
1584 
1585  // now let us find all the boundary edges
1586  Range boundaryEdges;
1587  MB_CHK_ERR( skinner.find_skin( 0, this->m_target_entities, false, boundaryEdges ) );
1588 
1589  // filter boundary edges that are on partition boundary, not on boundary
1590  // find all cells adjacent to these boundary edges, from target set
1591  Range boundaryCells; // these will be filtered from target_set
1592  MB_CHK_ERR( m_interface->get_adjacencies( boundaryEdges, 2, false, boundaryCells, Interface::UNION ) );
1593  boundaryCells = intersect( boundaryCells, this->m_target_entities );
1594 
1595 #ifdef MOAB_DBG
1596  EntityHandle tmpSet;
1597  MB_CHK_SET_ERR( m_interface->create_meshset( MESHSET_SET, tmpSet ), "Can't create temporary set" );
1598  // add the boundary set and edges, and save it to a file
1599  MB_CHK_SET_ERR( m_interface->add_entities( tmpSet, boundaryCells ), "Can't add entities" );
1600  MB_CHK_SET_ERR( m_interface->add_entities( tmpSet, boundaryEdges ), "Can't add edges" );
1601  std::stringstream ffs;
1602  ffs << "boundaryCells_0" << rank << ".h5m";
1603  MB_CHK_ERR( m_interface->write_mesh( ffs.str().c_str(), &tmpSet, 1 ) );
1604 #endif
1605 
1606  // now that we have the boundary cells, see which overlap polys have these as parents;
1607  // find the ids of the boundary cells;
1608  Tag gid = m_interface->globalId_tag();
1609  std::set< int > targetBoundaryIds;
1610  for( Range::iterator it = boundaryCells.begin(); it != boundaryCells.end(); it++ )
1611  {
1612  int tid;
1613  EntityHandle targetCell = *it;
1614  MB_CHK_SET_ERR( m_interface->tag_get_data( gid, &targetCell, 1, &tid ),
1615  "Can't get global id tag on target cell" );
1616  if( tid < 0 ) std::cout << " incorrect id for a target cell\n";
1617  targetBoundaryIds.insert( tid );
1618  }
1619 
1620  Range overlapCells;
1622 
1623  std::set< int > affectedSourceCellsIds;
1624  Tag targetParentTag, sourceParentTag; // do not use blue/red, as it is more confusing
1625  MB_CHK_ERR( m_interface->tag_get_handle( "TargetParent", targetParentTag ) );
1626  MB_CHK_ERR( m_interface->tag_get_handle( "SourceParent", sourceParentTag ) );
1627  for( Range::iterator it = overlapCells.begin(); it != overlapCells.end(); it++ )
1628  {
1629  EntityHandle intxCell = *it;
1630  int targetParentID, sourceParentID;
1631  MB_CHK_ERR( m_interface->tag_get_data( targetParentTag, &intxCell, 1, &targetParentID ) );
1632  if( targetBoundaryIds.find( targetParentID ) != targetBoundaryIds.end() )
1633  {
1634  // this means that the source element is affected
1635  MB_CHK_ERR( m_interface->tag_get_data( sourceParentTag, &intxCell, 1, &sourceParentID ) );
1636  affectedSourceCellsIds.insert( sourceParentID );
1637  }
1638  }
1639 
1640  // Now find all source cells affected, based on their id;
1641  // ( we do not yet have a global to local mapping for covering source mesh )
1642  // So create a map from source cell id to the eh;
1643  // it is needed to find out the original owning processor
1644  // this one came from, so to know where to send the overlap elements
1645  std::map< int, EntityHandle > affectedCovCellFromID;
1646 
1647  // use std::set<EntityHandle> instead of moab::Range for collecting cells,
1648  // either on coverage or target or intersection cells
1649  // their overlap cells will be sent to their original task, then distributed to all
1650  // other processes that might need them to compute conservation
1651  std::set< EntityHandle > affectedCovCells;
1652 
1653  Range covCells;
1655  // loop thru all cov cells, to find the ones with global ids in affectedSourceCellsIds
1656  for( Range::iterator it = covCells.begin(); it != covCells.end(); it++ )
1657  {
1658  EntityHandle covCell = *it; //
1659  int covID;
1660  MB_CHK_ERR( m_interface->tag_get_data( gid, &covCell, 1, &covID ) );
1661  if( affectedSourceCellsIds.find( covID ) != affectedSourceCellsIds.end() )
1662  {
1663  // this source cell is affected;
1664  affectedCovCellFromID[covID] = covCell;
1665  affectedCovCells.insert( covCell );
1666  }
1667  }
1668 
1669  // now loop again over all overlap cells, to see if their source parent is "affected"
1670  // store in ranges the overlap cells that need to be sent to original task of the source cell
1671  // from there, they will be redistributed to the tasks that need that coverage cell
1672  Tag sendProcTag;
1673  MB_CHK_ERR( m_interface->tag_get_handle( "sending_processor", 1, MB_TYPE_INTEGER, sendProcTag ) );
1674 
1675  // basically a map from original processor task to the set of overlap cells to be sent there
1676  std::map< int, std::set< EntityHandle > > overlapCellsForTask;
1677  // this set will contain all intx cells that will need to be sent ( a union of above sets ,
1678  // that are organized per task on the above map )
1679  std::set< EntityHandle > overlapCellsToSend;
1680 
1681  for( Range::iterator it = overlapCells.begin(); it != overlapCells.end(); it++ )
1682  {
1683  EntityHandle intxCell = *it;
1684  int sourceParentID;
1685  MB_CHK_ERR( m_interface->tag_get_data( sourceParentTag, &intxCell, 1, &sourceParentID ) );
1686 
1687  if( affectedSourceCellsIds.find( sourceParentID ) != affectedSourceCellsIds.end() )
1688  {
1689  int orgTask = -1; // the original task that this source cell came from
1690  EntityHandle covCell = affectedCovCellFromID[sourceParentID];
1691  MB_CHK_ERR( m_interface->tag_get_data( sendProcTag, &covCell, 1, &orgTask ) );
1692  // put the overlap cell in corresponding range (set<EntityHandle>)
1693  overlapCellsForTask[orgTask].insert( intxCell );
1694  // also put it in this range, for debugging mostly
1695  overlapCellsToSend.insert( intxCell );
1696  }
1697  }
1698 
1699  // now prepare to send; will use crystal router, as the buffers in ParComm are prepared only
1700  // for neighbors; coverage mesh was also migrated with crystal router, so here we go again :(
1701  // find out the maximum number of edges of the polygons needed to be sent
1702  // we could we conservative and use a big number, or the number from intx, if we store it then?
1703  int maxEdges = 0;
1704  for( std::set< EntityHandle >::iterator it = overlapCellsToSend.begin(); it != overlapCellsToSend.end(); it++ )
1705  {
1706  EntityHandle intxCell = *it;
1707  int nnodes;
1708  const EntityHandle* conn;
1709  MB_CHK_ERR( m_interface->get_connectivity( intxCell, conn, nnodes ) );
1710  if( maxEdges < nnodes ) maxEdges = nnodes;
1711  }
1712 
1713  // find the maximum among processes in intersection
1714  int globalMaxEdges;
1715  if( m_pcomm )
1716  MPI_Allreduce( &maxEdges, &globalMaxEdges, 1, MPI_INT, MPI_MAX, m_pcomm->comm() );
1717  else
1718  globalMaxEdges = maxEdges;
1719 
1720 #ifdef MOAB_DBG
1721  if( is_root ) std::cout << "maximum number of edges for polygons to send is " << globalMaxEdges << "\n";
1722 #endif
1723 
1724 #ifdef MOAB_DBG
1725  EntityHandle tmpSet2;
1726  MB_CHK_SET_ERR( m_interface->create_meshset( MESHSET_SET, tmpSet2 ), "Can't create temporary set2" );
1727  // add the affected source and overlap elements
1728  for( std::set< EntityHandle >::iterator it = overlapCellsToSend.begin(); it != overlapCellsToSend.end(); it++ )
1729  {
1730  EntityHandle intxCell = *it;
1731  MB_CHK_SET_ERR( m_interface->add_entities( tmpSet2, &intxCell, 1 ), "Can't add entities" );
1732  }
1733  for( std::set< EntityHandle >::iterator it = affectedCovCells.begin(); it != affectedCovCells.end(); it++ )
1734  {
1735  EntityHandle covCell = *it;
1736  MB_CHK_SET_ERR( m_interface->add_entities( tmpSet2, &covCell, 1 ), "Can't add entities" );
1737  }
1738  std::stringstream ffs2;
1739  // these will contain coverage cells and intx cells on the boundary
1740  ffs2 << "affectedCells_" << m_pcomm->rank() << ".h5m";
1741  MB_CHK_ERR( m_interface->write_mesh( ffs2.str().c_str(), &tmpSet2, 1 ) );
1742 #endif
1743  // form tuple lists to send vertices and cells;
1744  // the problem is that the lists of vertices will need to have other information, like the
1745  // processor it comes from, and its index in that list; we may have to duplicate vertices, but
1746  // we do not care much; we will not duplicate overlap elements, just the vertices, as they may
1747  // come from different cells and different processes each vertex will have a local index and a
1748  // processor task it is coming from
1749 
1750  // look through the std::set's to be sent to other processes, and form the vertex tuples and
1751  // cell tuples
1752  //
1753  std::map< int, std::set< EntityHandle > > verticesOverlapForTask;
1754  // Range allVerticesToSend;
1755  std::set< EntityHandle > allVerticesToSend;
1756  std::map< EntityHandle, int > allVerticesToSendMap;
1757  int numVerts = 0;
1758  int numOverlapCells = 0;
1759  for( std::map< int, std::set< EntityHandle > >::iterator it = overlapCellsForTask.begin();
1760  it != overlapCellsForTask.end(); it++ )
1761  {
1762  int sendToProc = it->first;
1763  std::set< EntityHandle >& overlapCellsToSend2 = it->second; // organize vertices in std::set per processor
1764  // Range vertices;
1765  std::set< EntityHandle > vertices; // collect all vertices connected to overlapCellsToSend2
1766  for( std::set< EntityHandle >::iterator set_it = overlapCellsToSend2.begin();
1767  set_it != overlapCellsToSend2.end(); ++set_it )
1768  {
1769  int nnodes_local = 0;
1770  const EntityHandle* conn1 = nullptr;
1771  MB_CHK_ERR( m_interface->get_connectivity( *set_it, conn1, nnodes_local ) );
1772  for( int k = 0; k < nnodes_local; k++ )
1773  vertices.insert( conn1[k] );
1774  }
1775  verticesOverlapForTask[sendToProc] = vertices;
1776  numVerts += (int)vertices.size();
1777  numOverlapCells += (int)overlapCellsToSend2.size();
1778  allVerticesToSend.insert( vertices.begin(), vertices.end() );
1779  }
1780  // build the index map, from entity handle to index in all vert set
1781  int j = 0;
1782  for( std::set< EntityHandle >::iterator vert_it = allVerticesToSend.begin(); vert_it != allVerticesToSend.end();
1783  vert_it++, j++ )
1784  {
1785  EntityHandle vert = *vert_it;
1786  allVerticesToSendMap[vert] = j;
1787  }
1788 
1789  // first send vertices in a tuple list, then send overlap cells, according to requests
1790  // overlap cells need to send info about the blue and red parent tags, too
1791  TupleList TLv; //
1792  TLv.initialize( 2, 0, 0, 3, numVerts ); // to proc, index in all range, DP points
1793  TLv.enableWriteAccess();
1794 
1795  for( std::map< int, std::set< EntityHandle > >::iterator it = verticesOverlapForTask.begin();
1796  it != verticesOverlapForTask.end(); it++ )
1797  {
1798  int sendToProc = it->first;
1799  std::set< EntityHandle >& vertices = it->second;
1800  int i = 0;
1801  for( std::set< EntityHandle >::iterator it2 = vertices.begin(); it2 != vertices.end(); it2++, i++ )
1802  {
1803  int n = TLv.get_n();
1804  TLv.vi_wr[2 * n] = sendToProc; // send to processor
1805  EntityHandle v = *it2;
1806  int indexInAllVert = allVerticesToSendMap[v];
1807  TLv.vi_wr[2 * n + 1] = indexInAllVert; // will be orgProc, to differentiate indices
1808  // of vertices sent to "sentToProc"
1809  double coords[3];
1810  MB_CHK_ERR( m_interface->get_coords( &v, 1, coords ) );
1811  TLv.vr_wr[3 * n] = coords[0]; // departure position, of the node local_verts[i]
1812  TLv.vr_wr[3 * n + 1] = coords[1];
1813  TLv.vr_wr[3 * n + 2] = coords[2];
1814  TLv.inc_n();
1815  }
1816  }
1817 
1818  TupleList TLc;
1819  int sizeTuple = 4 + globalMaxEdges;
1820  // total number of overlap cells to send
1821  TLc.initialize( sizeTuple, 0, 0, 0,
1822  numOverlapCells ); // to proc, blue parent ID, red parent ID, nvert,
1823  // connectivity[globalMaxEdges] (global ID v), local eh)
1824  TLc.enableWriteAccess();
1825 
1826  for( std::map< int, std::set< EntityHandle > >::iterator it = overlapCellsForTask.begin();
1827  it != overlapCellsForTask.end(); it++ )
1828  {
1829  int sendToProc = it->first;
1830  std::set< EntityHandle >& overlapCellsToSend2 = it->second;
1831  // send also the target and source parents for these overlap cells
1832  for( std::set< EntityHandle >::iterator it2 = overlapCellsToSend2.begin(); it2 != overlapCellsToSend2.end();
1833  it2++ )
1834  {
1835  EntityHandle intxCell = *it2;
1836  int sourceParentID, targetParentID;
1837  MB_CHK_ERR( m_interface->tag_get_data( targetParentTag, &intxCell, 1, &targetParentID ) );
1838  MB_CHK_ERR( m_interface->tag_get_data( sourceParentTag, &intxCell, 1, &sourceParentID ) );
1839  int n = TLc.get_n();
1840  TLc.vi_wr[sizeTuple * n] = sendToProc;
1841  TLc.vi_wr[sizeTuple * n + 1] = sourceParentID;
1842  TLc.vi_wr[sizeTuple * n + 2] = targetParentID;
1843  int nnodes;
1844  const EntityHandle* conn = nullptr;
1845  MB_CHK_ERR( m_interface->get_connectivity( intxCell, conn, nnodes ) );
1846  TLc.vi_wr[sizeTuple * n + 3] = nnodes;
1847  for( int i = 0; i < nnodes; i++ )
1848  {
1849  int indexVertex = allVerticesToSendMap[conn[i]];
1850  ; // the vertex index will be now unique per original proc
1851  if( -1 == indexVertex ) MB_CHK_SET_ERR( MB_FAILURE, "Can't find vertex in range of vertices to send" );
1852  TLc.vi_wr[sizeTuple * n + 4 + i] = indexVertex;
1853  }
1854  // fill the rest with 0, just because we do not like uninitialized data
1855  for( int i = nnodes; i < globalMaxEdges; i++ )
1856  TLc.vi_wr[sizeTuple * n + 4 + i] = 0;
1857 
1858  TLc.inc_n();
1859  }
1860  }
1861 
1862  // send first the vertices and overlap cells to original task for coverage cells
1863  // now we are done populating the tuples; route them to the appropriate processors
1864 #ifdef MOAB_DBG
1865  std::stringstream ff1;
1866  ff1 << "TLc_" << rank << ".txt";
1867  TLc.print_to_file( ff1.str().c_str() );
1868  std::stringstream ffv;
1869  ffv << "TLv_" << rank << ".txt";
1870  TLv.print_to_file( ffv.str().c_str() );
1871 #endif
1872  ( m_pcomm->proc_config().crystal_router() )->gs_transfer( 1, TLv, 0 );
1873  ( m_pcomm->proc_config().crystal_router() )->gs_transfer( 1, TLc, 0 );
1874 
1875 #ifdef MOAB_DBG
1876  TLc.print_to_file( ff1.str().c_str() ); // will append to existing file
1877  TLv.print_to_file( ffv.str().c_str() );
1878 #endif
1879  // first phase of transfer complete
1880  // now look at TLc, and sort by the source parent (index 1)
1881 
1883  buffer.buffer_init( sizeTuple * TLc.get_n() * 2 ); // allocate memory for sorting !! double
1884  TLc.sort( 1, &buffer );
1885 #ifdef MOAB_DBG
1886  TLc.print_to_file( ff1.str().c_str() );
1887 #endif
1888 
1889  // will keep a map with vertices per processor that will need to be used in TLv2;
1890  // so, availVertexIndicesPerProcessor[proc] is a map from vertex indices that are available from
1891  // this processor to the index in the local TLv; the used vertices will have to be sent to the
1892  // tasks that need them
1893 
1894  // connectivity of a cell is given by sending proc and index in original list of vertices from
1895  // that proc
1896 
1897  std::map< int, std::map< int, int > > availVertexIndicesPerProcessor;
1898  int nv = TLv.get_n();
1899  for( int i = 0; i < nv; i++ )
1900  {
1901  // int proc=TLv.vi_rd[3*i]; // it is coming from this processor
1902  int orgProc = TLv.vi_rd[2 * i]; // this is the original processor, for index vertex consideration
1903  int indexVert = TLv.vi_rd[2 * i + 1];
1904  availVertexIndicesPerProcessor[orgProc][indexVert] = i;
1905  }
1906 
1907  // now we have sorted the incoming overlap elements by the source element;
1908  // if we have overlap elements for one source coming from 2 or more processes, we need to send
1909  // back to the processes that do not have that overlap cell;
1910 
1911  // form new TLc2, TLv2, that will be distributed to necessary processes
1912  // first count source elements that are "spread" over multiple processes
1913  // TLc is ordered now by source ID; loop over them
1914  int n = TLc.get_n(); // total number of overlap elements received on current task;
1915  std::map< int, int > currentProcsCount;
1916  // form a map from proc to sets of vertex indices that will be sent using TLv2
1917  // will form a map between a source cell ID and tasks/targets that are partially overlapped by
1918  // these sources
1919  std::map< int, std::set< int > > sourcesForTasks;
1920  int sizeOfTLc2 = 0; // only increase when we will have to send data
1921  if( n > 0 )
1922  {
1923  int currentSourceID = TLc.vi_rd[sizeTuple * 0 + 1]; // we have written sizeTuple*0 for "clarity"
1924  int proc0 = TLc.vi_rd[sizeTuple * 0];
1925  currentProcsCount[proc0] = 1; //
1926 
1927  for( int i = 1; i < n; i++ )
1928  {
1929  int proc = TLc.vi_rd[sizeTuple * i];
1930  int sourceID = TLc.vi_rd[sizeTuple * i + 1];
1931  if( sourceID == currentSourceID )
1932  {
1933  if( currentProcsCount.find( proc ) == currentProcsCount.end() )
1934  {
1935  currentProcsCount[proc] = 1;
1936  }
1937  else
1938  currentProcsCount[proc]++;
1939  }
1940  if( sourceID != currentSourceID || ( ( n - 1 ) == i ) ) // we study the current source if we reach the last
1941  {
1942  // we have found a new source id, need to reset the proc counts, and establish if we
1943  // need to send data
1944  if( currentProcsCount.size() > 1 )
1945  {
1946 #ifdef VERBOSE
1947  std::cout << " source element " << currentSourceID << " intersects with "
1948  << currentProcsCount.size() << " target partitions\n";
1949  for( std::map< int, int >::iterator it = currentProcsCount.begin(); it != currentProcsCount.end();
1950  it++ )
1951  {
1952  int procID = it->first;
1953  int numOverCells = it->second;
1954  std::cout << " task:" << procID << " " << numOverCells << " cells\n";
1955  }
1956 
1957 #endif
1958  // estimate what we need to send
1959  for( std::map< int, int >::iterator it1 = currentProcsCount.begin(); it1 != currentProcsCount.end();
1960  it1++ )
1961  {
1962  int proc1 = it1->first;
1963  sourcesForTasks[currentSourceID].insert( proc1 );
1964  for( std::map< int, int >::iterator it2 = currentProcsCount.begin();
1965  it2 != currentProcsCount.end(); it2++ )
1966  {
1967  int proc2 = it2->first;
1968  if( proc1 != proc2 ) sizeOfTLc2 += it2->second;
1969  }
1970  }
1971  // mark vertices in TLv tuple that need to be sent
1972  }
1973  if( sourceID != currentSourceID ) // maybe we are not at the end, so continue on
1974  {
1975  currentSourceID = sourceID;
1976  currentProcsCount.clear();
1977  currentProcsCount[proc] = 1;
1978  }
1979  }
1980  }
1981  }
1982  // begin a loop to send the needed cells to the processes; also mark the vertices that need to
1983  // be sent, put them in a set
1984 
1985 #ifdef MOAB_DBG
1986  std::cout << " need to initialize TLc2 with " << sizeOfTLc2 << " cells\n ";
1987 #endif
1988 
1989  TupleList TLc2;
1990  int sizeTuple2 = 5 + globalMaxEdges; // send to, original proc for intx cell, source parent id,
1991  // target parent id,
1992  // number of vertices, then connectivity in terms of indices in vertex lists from original proc
1993  TLc2.initialize( sizeTuple2, 0, 0, 0, sizeOfTLc2 );
1994  TLc2.enableWriteAccess();
1995  // loop again through TLc, and select intx cells that have the problem sources;
1996 
1997  std::map< int, std::set< int > > verticesToSendForProc; // will look at indices in the TLv list
1998  // will form for each processor, the index list from TLv
1999  for( int i = 0; i < n; i++ )
2000  {
2001  int sourceID = TLc.vi_rd[sizeTuple * i + 1];
2002  if( sourcesForTasks.find( sourceID ) != sourcesForTasks.end() )
2003  {
2004  // it means this intx cell needs to be sent to any proc that is not "original" to it
2005  std::set< int > procs = sourcesForTasks[sourceID]; // set of processors involved with this source
2006  if( procs.size() < 2 ) MB_CHK_SET_ERR( MB_FAILURE, " not enough processes involved with a sourceID cell" );
2007 
2008  int orgProc = TLc.vi_rd[sizeTuple * i]; // this intx cell was sent from this orgProc, originally
2009  // will need to be sent to all other procs from above set; also, need to mark the vertex
2010  // indices for that proc, and check that they are available to populate TLv2
2011  std::map< int, int >& availableVerticesFromThisProc = availVertexIndicesPerProcessor[orgProc];
2012  for( std::set< int >::iterator setIt = procs.begin(); setIt != procs.end(); setIt++ )
2013  {
2014  int procID = *setIt;
2015  // send this cell to the other processors, not to orgProc this cell is coming from
2016 
2017  if( procID != orgProc )
2018  {
2019  // send the cell to this processor;
2020  int n2 = TLc2.get_n();
2021  if( n2 >= sizeOfTLc2 ) MB_CHK_SET_ERR( MB_FAILURE, " memory overflow" );
2022  //
2023  std::set< int >& indexVerticesInTLv = verticesToSendForProc[procID];
2024  TLc2.vi_wr[n2 * sizeTuple2] = procID; // send to
2025  TLc2.vi_wr[n2 * sizeTuple2 + 1] = orgProc; // this cell is coming from here
2026  TLc2.vi_wr[n2 * sizeTuple2 + 2] = sourceID; // source parent of the intx cell
2027  TLc2.vi_wr[n2 * sizeTuple2 + 3] = TLc.vi_rd[sizeTuple * i + 2]; // target parent of the intx cell
2028  // number of vertices of the intx cell
2029  int nvert = TLc.vi_rd[sizeTuple * i + 3];
2030  TLc2.vi_wr[n2 * sizeTuple2 + 4] = nvert;
2031  // now loop through the connectivity, and make sure the vertices are available;
2032  // mark them, to populate later the TLv2 tuple list
2033 
2034  // just copy the vertices, including 0 ones
2035  for( int j = 0; j < nvert; j++ )
2036  {
2037  int vertexIndex = TLc.vi_rd[i * sizeTuple + 4 + j];
2038  // is this vertex available from org proc?
2039  if( availableVerticesFromThisProc.find( vertexIndex ) == availableVerticesFromThisProc.end() )
2040  {
2041  MB_CHK_SET_ERR( MB_FAILURE, " vertex index not available from processor" );
2042  }
2043  TLc2.vi_wr[n2 * sizeTuple2 + 5 + j] = vertexIndex;
2044  int indexInTLv = availVertexIndicesPerProcessor[orgProc][vertexIndex];
2045  indexVerticesInTLv.insert( indexInTLv );
2046  }
2047 
2048  for( int j = nvert; j < globalMaxEdges; j++ )
2049  {
2050  TLc2.vi_wr[n2 * sizeTuple2 + 5 + j] = 0; // or mark them 0
2051  }
2052  TLc2.inc_n();
2053  }
2054  }
2055  }
2056  }
2057 
2058  // now we have to populate TLv2, with original source proc, index of vertex, and coordinates
2059  // from TLv use the verticesToSendForProc sets from above, and the map from index in proc to the
2060  // index in TLv
2061  TupleList TLv2;
2062  int numVerts2 = 0;
2063  // how many vertices to send?
2064  for( std::map< int, std::set< int > >::iterator it = verticesToSendForProc.begin();
2065  it != verticesToSendForProc.end(); it++ )
2066  {
2067  std::set< int >& indexInTLvSet = it->second;
2068  numVerts2 += (int)indexInTLvSet.size();
2069  }
2070  TLv2.initialize( 3, 0, 0, 3,
2071  numVerts2 ); // send to, original proc, index in original proc, and 3 coords
2072  TLv2.enableWriteAccess();
2073  for( std::map< int, std::set< int > >::iterator it = verticesToSendForProc.begin();
2074  it != verticesToSendForProc.end(); it++ )
2075  {
2076  int sendToProc = it->first;
2077  std::set< int >& indexInTLvSet = it->second;
2078  // now, look at indices in TLv, to find out the original proc, and the index in that list
2079  for( std::set< int >::iterator itSet = indexInTLvSet.begin(); itSet != indexInTLvSet.end(); itSet++ )
2080  {
2081  int indexInTLv = *itSet;
2082  int orgProc = TLv.vi_rd[2 * indexInTLv];
2083  int indexVertexInOrgProc = TLv.vi_rd[2 * indexInTLv + 1];
2084  int nv2 = TLv2.get_n();
2085  TLv2.vi_wr[3 * nv2] = sendToProc;
2086  TLv2.vi_wr[3 * nv2 + 1] = orgProc;
2087  TLv2.vi_wr[3 * nv2 + 2] = indexVertexInOrgProc;
2088  for( int j = 0; j < 3; j++ )
2089  TLv2.vr_wr[3 * nv2 + j] =
2090  TLv.vr_rd[3 * indexInTLv + j]; // departure position, of the node local_verts[i]
2091  TLv2.inc_n();
2092  }
2093  }
2094  // now, finally, transfer the vertices and the intx cells;
2095  ( m_pcomm->proc_config().crystal_router() )->gs_transfer( 1, TLv2, 0 );
2096  ( m_pcomm->proc_config().crystal_router() )->gs_transfer( 1, TLc2, 0 );
2097  // now, look at vertices from TLv2, and create them
2098  // we should have in TLv2 only vertices with orgProc different from current task
2099 #ifdef MOAB_DBG
2100  std::stringstream ff2;
2101  ff2 << "TLc2_" << rank << ".txt";
2102  TLc2.print_to_file( ff2.str().c_str() );
2103  std::stringstream ffv2;
2104  ffv2 << "TLv2_" << rank << ".txt";
2105  TLv2.print_to_file( ffv2.str().c_str() );
2106 #endif
2107  // first create vertices, and make a map from origin processor, and index, to entity handle
2108  // (index in TLv2 )
2109  Tag ghostTag;
2110  int orig_proc = -1;
2112  &orig_proc ) );
2113 
2114  int nvNew = TLv2.get_n();
2115  // if number of vertices to be created is 0, it means there is no need of ghost intx cells,
2116  // because everything matched perfectly (it can happen in manufactured cases)
2117  if( 0 == nvNew ) return MB_SUCCESS;
2118  // create a vertex h for each coordinate
2119  Range newVerts;
2120  MB_CHK_ERR( m_interface->create_vertices( &( TLv2.vr_rd[0] ), nvNew, newVerts ) );
2121  // now create a map from index , org proc, to actual entity handle corresponding to it
2122  std::map< int, std::map< int, EntityHandle > > vertexPerProcAndIndex;
2123  for( int i = 0; i < nvNew; i++ )
2124  {
2125  int orgProc = TLv2.vi_rd[3 * i + 1];
2126  int indexInVert = TLv2.vi_rd[3 * i + 2];
2127  vertexPerProcAndIndex[orgProc][indexInVert] = newVerts[i];
2128  }
2129 
2130  // new polygons will receive a dense tag, with default value -1, with the processor task they
2131  // originally belonged to
2132 
2133  // now form the needed cells, in order
2134  Range newPolygons;
2135  int ne = TLc2.get_n();
2136  for( int i = 0; i < ne; i++ )
2137  {
2138  int orgProc = TLc2.vi_rd[i * sizeTuple2 + 1]; // this cell is coming from here, originally
2139  int sourceID = TLc2.vi_rd[i * sizeTuple2 + 2]; // source parent of the intx cell
2140  int targetID = TLc2.vi_wr[i * sizeTuple2 + 3]; // target parent of intx cell
2141  int nve = TLc2.vi_wr[i * sizeTuple2 + 4]; // number of vertices for the polygon
2142  std::vector< EntityHandle > conn;
2143  conn.resize( nve );
2144  for( int j = 0; j < nve; j++ )
2145  {
2146  int indexV = TLc2.vi_wr[i * sizeTuple2 + 5 + j];
2147  EntityHandle vh = vertexPerProcAndIndex[orgProc][indexV];
2148  conn[j] = vh;
2149  }
2150  EntityHandle polyNew;
2151  MB_CHK_ERR( m_interface->create_element( MBPOLYGON, &conn[0], nve, polyNew ) );
2152  newPolygons.insert( polyNew );
2153  MB_CHK_ERR( m_interface->tag_set_data( targetParentTag, &polyNew, 1, &targetID ) );
2154  MB_CHK_ERR( m_interface->tag_set_data( sourceParentTag, &polyNew, 1, &sourceID ) );
2155  MB_CHK_ERR( m_interface->tag_set_data( ghostTag, &polyNew, 1, &orgProc ) );
2156  }
2157 
2158 #ifdef MOAB_DBG
2159  EntityHandle tmpSet3;
2160  MB_CHK_SET_ERR( m_interface->create_meshset( MESHSET_SET, tmpSet3 ), "Can't create temporary set3" );
2161  // add the boundary set and edges, and save it to a file
2162  MB_CHK_SET_ERR( m_interface->add_entities( tmpSet3, newPolygons ), "Can't add entities" );
2163 
2164  std::stringstream ffs4;
2165  ffs4 << "extraIntxCells" << rank << ".h5m";
2166  MB_CHK_ERR( m_interface->write_mesh( ffs4.str().c_str(), &tmpSet3, 1 ) );
2167 #endif
2168 
2169  // add the new polygons to the overlap set
2170  // these will be ghosted, so will participate in conservation only
2171  MB_CHK_ERR( m_interface->add_entities( m_overlap_set, newPolygons ) );
2172  return MB_SUCCESS;
2173 }
2174 #endif
2175 
2176 #undef MOAB_DBG
2177 
2179 {
2180  Tag maskTag;
2181  // it should have been created already, if not, we might have a problem
2182  int def_val = 1;
2184  &def_val ),
2185  "Trouble creating GRID_IMASK tag" );
2186 
2187  switch( ctx )
2188  {
2189  case Remapper::SourceMesh: {
2190  if( point_cloud_source )
2191  {
2192  masks.resize( m_source_vertices.size() );
2193  MB_CHK_SET_ERR( m_interface->tag_get_data( maskTag, m_source_vertices, &masks[0] ),
2194  "Trouble getting GRID_IMASK tag" );
2195  }
2196  else
2197  {
2198  masks.resize( m_source_entities.size() );
2199  MB_CHK_SET_ERR( m_interface->tag_get_data( maskTag, m_source_entities, &masks[0] ),
2200  "Trouble getting GRID_IMASK tag" );
2201  }
2202  return MB_SUCCESS;
2203  }
2204  case Remapper::TargetMesh: {
2205  if( point_cloud_target )
2206  {
2207  masks.resize( m_target_vertices.size() );
2208  MB_CHK_SET_ERR( m_interface->tag_get_data( maskTag, m_target_vertices, &masks[0] ),
2209  "Trouble getting GRID_IMASK tag" );
2210  }
2211  else
2212  {
2213  masks.resize( m_target_entities.size() );
2214  MB_CHK_SET_ERR( m_interface->tag_get_data( maskTag, m_target_entities, &masks[0] ),
2215  "Trouble getting GRID_IMASK tag" );
2216  }
2217  return MB_SUCCESS;
2218  }
2220  case Remapper::OverlapMesh:
2221  default:
2222  return MB_SUCCESS;
2223  }
2224 }
2225 
2226 } // namespace moab