40 #ifdef MOAB_HAVE_TEMPESTREMAP
42 #include "FiniteElementTools.h"
43 #include "GaussLobattoQuadrature.h"
56 if( initialize_fsets )
76 MPI_Initialized( &flagInit );
79 assert( m_pcomm !=
nullptr );
80 rank = m_pcomm->rank();
81 size = m_pcomm->size();
86 AnnounceOnlyOutputOnRankZero();
154 std::string inputFilename,
174 MB_CHK_SET_ERR( MB_FAILURE,
"Invalid IntersectionContext context provided" );
181 if( outputEnabled ) std::cout <<
"\nLoading TempestRemap Mesh object from file = " << inputFilename <<
" ...\n";
184 NcError
error( NcError::silent_nonfatal );
189 if( outputEnabled ) std::cout <<
"Loading mesh ...\n";
190 Mesh* mesh =
new Mesh( inputFilename );
191 mesh->RemoveZeroEdges();
192 if( outputEnabled ) std::cout <<
"----------------\n";
197 if( outputEnabled ) std::cout <<
"Validating mesh ...\n";
199 if( outputEnabled ) std::cout <<
"-------------------\n";
205 if( outputEnabled ) std::cout <<
"Constructing edge map on mesh ...\n";
206 mesh->ConstructEdgeMap(
false );
207 if( outputEnabled ) std::cout <<
"---------------------------------\n";
210 if( tempest_mesh ) *tempest_mesh = mesh;
212 catch( Exception& e )
214 std::cout <<
"TempestRemap ERROR: " << e.ToString() <<
"\n";
232 if( outputEnabled ) std::cout <<
"Converting (source) TempestRemap Mesh object to MOAB representation ...\n";
238 if( outputEnabled ) std::cout <<
"Converting (target) TempestRemap Mesh object to MOAB representation ...\n";
244 if( outputEnabled ) std::cout <<
"Converting (overlap) TempestRemap Mesh object to MOAB representation ...\n";
249 MB_CHK_SET_ERR( MB_FAILURE,
"Invalid IntersectionContext context provided" );
253 #define NEW_CONVERT_LOGIC
255 #ifdef NEW_CONVERT_LOGIC
264 const NodeVector& nodes = mesh->nodes;
265 const FaceVector& faces = mesh->faces;
276 std::vector< double* > arrays;
277 std::vector< int > gidsv( nodes.size() );
279 rval =
iface->get_node_coords( 3, nodes.size(), 0, startv, arrays );
MB_CHK_SET_ERR( rval,
"Can't get node coords" );
280 for(
unsigned iverts = 0; iverts < nodes.size(); ++iverts )
282 const Node& node = nodes[iverts];
283 arrays[0][iverts] = node.x;
284 arrays[1][iverts] = node.y;
285 arrays[2][iverts] = node.z;
286 gidsv[iverts] = iverts + 1;
288 Range mbverts( startv, startv + nodes.size() - 1 );
295 Tag srcParentTag, tgtParentTag;
296 std::vector< int > srcParent( faces.size(), -1 ), tgtParent( faces.size(), -1 );
297 std::vector< int > gidse( faces.size(), -1 );
298 bool storeParentInfo = ( mesh->vecSourceFaceIx.size() > 0 );
300 if( storeParentInfo )
318 dbgprint.printf( 0,
"..Mesh size: Nodes [%zu] Elements [%zu].\n", nodes.size(), faces.size() );
319 std::vector< EntityHandle > mbcells( faces.size() );
320 unsigned ntris = 0, nquads = 0, npolys = 0;
321 std::vector< EntityHandle > conn( 16 );
323 for(
unsigned ifaces = 0; ifaces < faces.size(); ++ifaces )
325 const Face&
face = faces[ifaces];
326 const unsigned num_v_per_elem =
face.edges.size();
328 for(
unsigned iedges = 0; iedges < num_v_per_elem; ++iedges )
330 conn[iedges] = startv +
face.edges[iedges].node[0];
333 switch( num_v_per_elem )
356 gidse[ifaces] = ifaces + 1;
358 if( storeParentInfo )
360 srcParent[ifaces] = mesh->vecSourceFaceIx[ifaces] + 1;
361 tgtParent[ifaces] = mesh->vecTargetFaceIx[ifaces] + 1;
365 if( ntris )
dbgprint.printf( 0,
"....Triangular Elements [%u].\n", ntris );
366 if( nquads )
dbgprint.printf( 0,
"....Quadrangular Elements [%u].\n", nquads );
367 if( npolys )
dbgprint.printf( 0,
"....Polygonal Elements [%u].\n", npolys );
372 if( storeParentInfo )
382 if( vertices ) *vertices = mbverts;
397 const NodeVector& nodes = mesh->nodes;
398 const FaceVector& faces = mesh->faces;
401 dbgprint.set_prefix(
"[TempestToMOAB]: " );
409 std::vector< double* > arrays;
410 std::vector< int > gidsv( nodes.size() );
412 rval =
iface->get_node_coords( 3, nodes.size(), 0, startv, arrays );
MB_CHK_SET_ERR( rval,
"Can't get node coords" );
413 for(
unsigned iverts = 0; iverts < nodes.size(); ++iverts )
415 const Node& node = nodes[iverts];
416 arrays[0][iverts] = node.x;
417 arrays[1][iverts] = node.y;
418 arrays[2][iverts] = node.z;
419 gidsv[iverts] = iverts + 1;
421 Range mbverts( startv, startv + nodes.size() - 1 );
428 Tag srcParentTag, tgtParentTag;
429 std::vector< int > srcParent, tgtParent;
430 bool storeParentInfo = ( mesh->vecSourceFaceIx.size() > 0 );
432 if( storeParentInfo )
450 dbgprint.printf( 0,
"..Mesh size: Nodes [%zu] Elements [%zu].\n", nodes.size(), faces.size() );
451 const int NMAXPOLYEDGES = 15;
452 std::vector< unsigned > nPolys( NMAXPOLYEDGES, 0 );
453 std::vector< std::vector< int > > typeNSeqs( NMAXPOLYEDGES );
454 for(
unsigned ifaces = 0; ifaces < faces.size(); ++ifaces )
456 const int iType = faces[ifaces].edges.size();
458 typeNSeqs[iType].push_back( ifaces );
461 for(
unsigned iType = 0; iType < NMAXPOLYEDGES; ++iType )
463 if( !nPolys[iType] )
continue;
465 const unsigned num_v_per_elem = iType;
470 switch( num_v_per_elem )
474 dbgprint.printf( 0,
"....Block %d: Triangular Elements [%u].\n", iBlock++, nPolys[iType] );
475 rval =
iface->get_element_connect( nPolys[iType], num_v_per_elem,
MBTRI, 0, starte, conn );
MB_CHK_SET_ERR( rval,
"Can't get element connectivity" );
479 dbgprint.printf( 0,
"....Block %d: Quadrilateral Elements [%u].\n", iBlock++, nPolys[iType] );
480 rval =
iface->get_element_connect( nPolys[iType], num_v_per_elem,
MBQUAD, 0, starte, conn );
MB_CHK_SET_ERR( rval,
"Can't get element connectivity" );
484 dbgprint.printf( 0,
"....Block %d: Polygonal [%u] Elements [%u].\n", iBlock++, iType,
486 rval =
iface->get_element_connect( nPolys[iType], num_v_per_elem,
MBPOLYGON, 0, starte, conn );
MB_CHK_SET_ERR( rval,
"Can't get element connectivity" );
490 Range mbcells( starte, starte + nPolys[iType] - 1 );
493 if( storeParentInfo )
495 srcParent.resize( mbcells.size(), -1 );
496 tgtParent.resize( mbcells.size(), -1 );
499 std::vector< int > gids( typeNSeqs[iType].
size() );
500 for(
unsigned ifaces = 0, offset = 0; ifaces < typeNSeqs[iType].size(); ++ifaces )
502 const int fIndex = typeNSeqs[iType][ifaces];
503 const Face&
face = faces[fIndex];
505 for(
unsigned iedges = 0; iedges <
face.edges.size(); ++iedges )
507 conn[offset++] = startv +
face.edges[iedges].node[0];
510 if( storeParentInfo )
512 srcParent[ifaces] = mesh->vecSourceFaceIx[fIndex] + 1;
513 tgtParent[ifaces] = mesh->vecTargetFaceIx[fIndex] + 1;
516 gids[ifaces] = typeNSeqs[iType][ifaces] + 1;
523 rval =
iface->update_adjacencies( starte, nPolys[iType], num_v_per_elem, conn );
MB_CHK_SET_ERR( rval,
"Can't update adjacencies" );
529 if( storeParentInfo )
538 if( vertices ) *vertices = mbverts;
557 if( outputEnabled )
dbgprint.printf( 0,
"Converting (source) MOAB to TempestRemap Mesh representation ...\n" );
567 if( outputEnabled )
dbgprint.printf( 0,
"Converting (target) MOAB to TempestRemap Mesh representation ...\n" );
574 if( outputEnabled )
dbgprint.printf( 0,
"Converting (overlap) MOAB to TempestRemap Mesh representation ...\n" );
579 MB_CHK_SET_ERR( MB_FAILURE,
"Invalid IntersectionContext context provided" );
593 NodeVector& nodes = mesh->nodes;
594 FaceVector& faces = mesh->faces;
599 const size_t nelems = elems.
size();
602 faces.resize( nelems );
606 if( verts.
size() == 0 )
612 std::map< EntityHandle, int > indxMap;
613 bool useRange =
true;
622 std::vector< int > globIds( nelems );
625 std::vector< size_t > sortedIdx;
628 sortedIdx.resize( nelems );
630 std::iota( sortedIdx.begin(), sortedIdx.end(), 0 );
633 std::sort( sortedIdx.begin(), sortedIdx.end(),
634 [&globIds](
size_t i1,
size_t i2 ) { return globIds[i1] < globIds[i2]; } );
647 while( connectface[nnodesf - 2] == connectface[nnodesf - 1] && nnodesf > 3 )
650 face.edges.resize( nnodesf );
651 for(
int iverts = 0; iverts < nnodesf; ++iverts )
653 int indx = ( useRange ? verts.
index( connectface[iverts] ) : indxMap[connectface[iverts]] );
655 face.SetNode( iverts, indx );
659 unsigned nnodes = verts.
size();
660 nodes.resize( nnodes );
663 std::vector< double > coordx( nnodes ), coordy( nnodes ), coordz( nnodes );
665 for(
unsigned inode = 0; inode < nnodes; ++inode )
667 Node& node = nodes[inode];
668 node.x = coordx[inode];
669 node.y = coordy[inode];
670 node.z = coordz[inode];
676 mesh->RemoveCoincidentNodes();
677 mesh->RemoveZeroEdges();
699 if( std::get< 1 >( a ) == std::get< 1 >( b ) )
700 return std::get< 2 >( a ) < std::get< 2 >( b );
702 return std::get< 1 >( a ) < std::get< 1 >( b );
707 sharedGhostEntities.
clear();
712 if( is_parallel &&
size > 1 )
715 rval = m_interface->get_entities_by_dimension( m_overlap_set, 2, allents );
MB_CHK_SET_ERR( rval,
"Getting entities dim 2 failed" );
719 std::vector< int > ghFlags( allents.
size() );
720 rval = m_interface->tag_get_handle(
"ORIG_PROC", ghostTag );
MB_CHK_ERR( rval );
721 rval = m_interface->tag_get_data( ghostTag, allents, &ghFlags[0] );
MB_CHK_ERR( rval );
722 for(
unsigned i = 0; i < allents.
size(); ++i )
723 if( ghFlags[i] >= 0 )
724 sharedents.
insert( allents[i] );
726 allents =
subtract( allents, sharedents );
731 rval = m_interface->get_connectivity( allents, ownedverts );
MB_CHK_SET_ERR( rval,
"Deleting entities dim 0 failed" );
732 rval = m_interface->get_connectivity( sharedents, sharedverts );
MB_CHK_SET_ERR( rval,
"Deleting entities dim 0 failed" );
733 sharedverts =
subtract( sharedverts, ownedverts );
738 sharedGhostEntities.
merge( sharedents );
757 std::vector< std::array< int, 3 > > sorted_overlap_order( n_overlap_entitites,
758 std::array< int, 3 >( { -1, -1, -1 } ) );
760 Tag srcParentTag, tgtParentTag;
764 m_overlap->vecTargetFaceIx.resize( n_overlap_entitites );
765 m_overlap->vecSourceFaceIx.resize( n_overlap_entitites );
768 std::vector< int > rbids_src( n_overlap_entitites ), rbids_tgt( n_overlap_entitites );
771 for(
size_t ix = 0; ix < n_overlap_entitites; ++ix )
773 std::get< 0 >( sorted_overlap_order[ix] ) = ix;
774 std::get< 1 >( sorted_overlap_order[ix] ) =
776 std::get< 2 >( sorted_overlap_order[ix] ) =
779 std::sort( sorted_overlap_order.begin(), sorted_overlap_order.end(),
IntPairComparator );
783 std::vector< int > ghFlags;
787 ghFlags.resize( n_overlap_entitites );
791 for(
unsigned ie = 0; ie < n_overlap_entitites; ++ie )
793 int ix = std::get< 0 >( sorted_overlap_order[ie] );
794 m_overlap->vecSourceFaceIx[ie] = std::get< 1 >( sorted_overlap_order[ie] );
798 m_overlap->vecTargetFaceIx[ie] = std::get< 2 >( sorted_overlap_order[ie] );
803 faces.resize( n_overlap_entitites );
811 std::map< EntityHandle, int > indxMap;
820 const unsigned iface = std::get< 0 >( sorted_overlap_order[ifac] );
821 Face&
face = faces[ifac];
829 face.edges.resize( nnodesf );
830 for(
int iverts = 0; iverts < nnodesf; ++iverts )
832 int indx = indxMap[connectface[iverts]];
834 face.SetNode( iverts, indx );
839 sorted_overlap_order.clear();
841 unsigned nnodes = verts.
size();
843 nodes.resize( nnodes );
846 std::vector< double > coordx( nnodes ), coordy( nnodes ), coordz( nnodes );
848 for(
unsigned inode = 0; inode < nnodes; ++inode )
850 Node& node = nodes[inode];
851 node.x = coordx[inode];
852 node.y = coordy[inode];
853 node.z = coordz[inode];
885 m_target->Write( std::string(
"target_TR_p" + std::to_string(
rank ) +
".g" ) );
897 std::vector< int > gids;
908 for(
unsigned ie = 0; ie < gids.size(); ++ie )
924 for(
unsigned ie = 0; ie < gids.size(); ++ie )
940 for(
unsigned ie = 0; ie < gids.size(); ++ie )
953 const bool fAllParallel,
954 const bool fInputConcave,
955 const bool fOutputConcave )
960 if( is_root &&
size == 1 )
962 this->m_source->CalculateFaceAreas( fInputConcave );
963 this->m_target->CalculateFaceAreas( fOutputConcave );
964 this->m_overlap->Write( strOutputFileName.c_str(), NcFile::Netcdf4 );
972 this->m_source->CalculateFaceAreas( fInputConcave );
973 this->m_covering_source->CalculateFaceAreas( fInputConcave );
974 this->m_target->CalculateFaceAreas( fOutputConcave );
975 this->m_overlap->Write( strOutputFileName.c_str(), NcFile::Netcdf4 );
980 this->m_source->CalculateFaceAreas( fInputConcave );
981 this->m_target->CalculateFaceAreas( fOutputConcave );
982 this->m_overlap->Write( strOutputFileName.c_str(), NcFile::Netcdf4 );
1025 #ifndef MOAB_HAVE_MPI
1028 const int dimension,
1029 const int start_id )
1039 int idoffset = start_id;
1040 std::vector< int > gid(
entities.size() );
1041 for(
unsigned i = 0; i <
entities.size(); ++i )
1042 gid[i] = idoffset++;
1055 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 );
1061 const std::string& dofTagName,
1068 const int csResolution = std::sqrt( ntot_elements / 6.0 );
1070 if( csResolution * csResolution * 6 != ntot_elements )
return MB_INVALID_SIZE;
1074 err = GenerateCSMesh( csMesh, csResolution,
"",
"NetCDF4" );
MB_CHK_SET_ERR( err ? MB_FAILURE :
MB_SUCCESS,
"Failed to generate CS mesh through TempestRemap" );
1082 const int ntot_elements,
1085 const std::string dofTagName,
1091 bool created =
false;
1096 int nElements =
static_cast< int >( csMesh.faces.size() );
1101 DataArray3D< int > dataGLLnodes;
1102 dataGLLnodes.Allocate( nP, nP, nElements );
1104 std::map< Node, int > mapNodes;
1105 std::map< Node, moab::EntityHandle > mapLocalMBNodes;
1108 DataArray1D< double > dG;
1109 DataArray1D< double > dW;
1110 GaussLobattoQuadrature::GetPoints( nP, 0.0, 1.0, dG, dW );
1113 if( secondary_ents )
entities.insert( secondary_ents->
begin(), secondary_ents->
end() );
1115 for(
unsigned iel = 0; iel <
entities.size(); ++iel )
1119 Node elCentroid( elcoords[0], elcoords[1], elcoords[2] );
1120 mapLocalMBNodes.insert( std::pair< Node, moab::EntityHandle >( elCentroid, eh ) );
1129 int* dofIDs =
new int[nP * nP];
1132 for(
int k = 0; k < nElements; k++ )
1134 const Face&
face = csMesh.faces[k];
1135 const NodeVector& nodes = csMesh.nodes;
1137 if(
face.edges.size() != 4 )
1139 _EXCEPTIONT(
"Mesh must only contain quadrilateral elements" );
1143 centroid.x = centroid.y = centroid.z = 0.0;
1144 for(
unsigned l = 0; l <
face.edges.size(); ++l )
1146 centroid.x += nodes[
face[l]].x;
1147 centroid.y += nodes[
face[l]].y;
1148 centroid.z += nodes[
face[l]].z;
1150 const double factor = 1.0 /
face.edges.size();
1151 centroid.x *= factor;
1152 centroid.y *= factor;
1153 centroid.z *= factor;
1155 bool locElem =
false;
1157 if( mapLocalMBNodes.find( centroid ) != mapLocalMBNodes.end() )
1160 current_eh = mapLocalMBNodes[centroid];
1163 for(
int j = 0; j < nP; j++ )
1165 for(
int i = 0; i < nP; i++ )
1180 const double& dAlpha = dG[i];
1181 const double& dBeta = dG[j];
1184 double dXc = nodes[
face[0]].x * ( 1.0 - dAlpha ) * ( 1.0 - dBeta ) +
1185 nodes[
face[1]].x * dAlpha * ( 1.0 - dBeta ) + nodes[
face[2]].x * dAlpha * dBeta +
1186 nodes[
face[3]].x * ( 1.0 - dAlpha ) * dBeta;
1188 double dYc = nodes[
face[0]].y * ( 1.0 - dAlpha ) * ( 1.0 - dBeta ) +
1189 nodes[
face[1]].y * dAlpha * ( 1.0 - dBeta ) + nodes[
face[2]].y * dAlpha * dBeta +
1190 nodes[
face[3]].y * ( 1.0 - dAlpha ) * dBeta;
1192 double dZc = nodes[
face[0]].z * ( 1.0 - dAlpha ) * ( 1.0 - dBeta ) +
1193 nodes[
face[1]].z * dAlpha * ( 1.0 - dBeta ) + nodes[
face[2]].z * dAlpha * dBeta +
1194 nodes[
face[3]].z * ( 1.0 - dAlpha ) * dBeta;
1196 double dR = sqrt( dXc * dXc + dYc * dYc + dZc * dZc );
1199 nodeGLL.x = dXc / dR;
1200 nodeGLL.y = dYc / dR;
1201 nodeGLL.z = dZc / dR;
1204 std::map< Node, int >::const_iterator iter = mapNodes.find( nodeGLL );
1205 if( iter == mapNodes.end() )
1208 int ixNode =
static_cast< int >( mapNodes.size() );
1209 mapNodes.insert( std::pair< Node, int >( nodeGLL, ixNode ) );
1210 dataGLLnodes[j][i][k] = ixNode + 1;
1214 dataGLLnodes[j][i][k] = iter->second + 1;
1217 dofIDs[j * nP + i] = dataGLLnodes[j][i][k];
1229 mapLocalMBNodes.clear();
1258 #ifdef MOAB_HAVE_MPI
1259 mbintx->set_parallel_comm( m_pcomm );
1269 #ifdef MOAB_HAVE_MPI
1278 std::stringstream filename;
1279 filename <<
"covering" <<
rank <<
".h5m";
1281 std::stringstream targetFile;
1282 targetFile <<
"target" <<
rank <<
".h5m";
1293 double tolerance = 1e-6, btolerance = 1e-3;
1301 for(
unsigned ie = 0; ie < targetVerts.
size(); ++ie )
1318 std::vector< moab::EntityHandle > leaf_elems;
1323 if( !leaf_elems.size() )
1331 std::vector< double > centroids( leaf_elems.size() * 3 );
1336 for(
size_t il = 0; il < leaf_elems.size(); ++il )
1338 const double* centroid = ¢roids[il * 3];
1339 const double locdist = std::pow( point[0] - centroid[0], 2 ) +
1340 std::pow( point[1] - centroid[1], 2 ) +
1341 std::pow( point[2] - centroid[2], 2 );
1343 if( locdist < dist )
1354 <<
": [Error] - Could not find a minimum distance within the leaf "
1356 << dist << std::endl;
1372 #ifdef MOAB_HAVE_MPI
1382 const bool outputEnabled = ( this->
rank == 0 );
1384 dbgprint.
set_prefix(
"[ComputeOverlapMesh]: " );
1406 bool concaveMeshA =
false, concaveMeshB =
false;
1408 "exact", concaveMeshA, concaveMeshB,
false );
1411 MB_CHK_SET_ERR( MB_FAILURE,
"TempestRemap: Can't compute the intersection of meshes on the sphere" );
1419 if( outputEnabled )
dbgprint.printf( 0,
"Computing intersection mesh with the Kd-tree search algorithm" );
1425 dbgprint.printf( 0,
"Computing intersection mesh with the advancing-front propagation algorithm" );
1429 #ifdef MOAB_HAVE_MPI
1433 std::stringstream ffc, fft, ffo;
1434 ffc <<
"cover_" <<
rank <<
".h5m";
1435 fft <<
"target_" <<
rank <<
".h5m";
1436 ffo <<
"intx_" <<
rank <<
".h5m";
1449 std::map< int, int > loc_gid_to_lid_covsrc;
1450 std::vector< int > gids( covEnts.
size(), -1 );
1455 for(
unsigned ie = 0; ie < gids.size(); ++ie )
1457 assert( gids[ie] > 0 );
1458 loc_gid_to_lid_covsrc[gids[ie]] = ie;
1461 Range intxCov, intxCells;
1471 assert( srcParent >= 0 );
1472 intxCov.
insert( covEnts[loc_gid_to_lid_covsrc[srcParent]] );
1476 if( !intxCov.
empty() )
1479 Range extraCovCells;
1482 intxCov.
merge( extraCovCells );
1506 rval =
mb->get_connectivity( eh, conn, num_nodes );
MB_CHK_ERR( rval );
1519 rval =
mb->get_connectivity( *it, conn, len,
false );
MB_CHK_ERR( rval );
1520 for(
int ie = 0; ie < len; ++ie )
1522 std::vector< EntityHandle > adjacent_entities;
1524 for(
auto ent : adjacent_entities )
1525 notNeededCovCells.
erase( ent );
1530 std::string(
"sourcecoveragemesh_p" + std::to_string(
rank ) +
".h5m" ).c_str(),
1541 std::cout <<
" total participating elements in the covering set: " << intxCov.
size() <<
"\n";
1542 std::cout <<
" remove from coverage set elements that are not intersected: " << notNeededCovCells.
size()
1584 #ifdef MOAB_HAVE_MPI
1607 Range targetCells, boundaryEdges;
1610 rval = skinner.find_skin( 0, targetCells,
false, boundaryEdges );
MB_CHK_ERR( rval );
1613 Range boundaryCells;
1615 boundaryCells =
intersect( boundaryCells, targetCells );
1622 std::stringstream ffs;
1623 ffs <<
"boundaryCells_0" <<
rank <<
".h5m";
1630 std::set< int > targetBoundaryIds;
1636 if( tid < 0 ) std::cout <<
" incorrect id for a target cell\n";
1637 targetBoundaryIds.insert( tid );
1643 std::set< int > affectedSourceCellsIds;
1644 Tag targetParentTag, sourceParentTag;
1647 for(
Range::iterator it = overlapCells.begin(); it != overlapCells.end(); it++ )
1650 int targetParentID, sourceParentID;
1652 if( targetBoundaryIds.find( targetParentID ) != targetBoundaryIds.end() )
1656 affectedSourceCellsIds.insert( sourceParentID );
1662 std::map< int, EntityHandle > affectedCovCellFromID;
1668 std::set< EntityHandle > affectedCovCells;
1675 for(
Range::iterator it = covCells.begin(); it != covCells.end(); it++ )
1680 if( affectedSourceCellsIds.find( covID ) != affectedSourceCellsIds.end() )
1683 affectedCovCellFromID[covID] = covCell;
1684 affectedCovCells.insert( covCell );
1695 std::map< int, std::set< EntityHandle > > overlapCellsForTask;
1698 std::set< EntityHandle > overlapCellsToSend;
1700 for(
Range::iterator it = overlapCells.begin(); it != overlapCells.end(); it++ )
1705 if( affectedSourceCellsIds.find( sourceParentID ) != affectedSourceCellsIds.end() )
1707 EntityHandle covCell = affectedCovCellFromID[sourceParentID];
1710 overlapCellsForTask[orgTask].insert(
1712 overlapCellsToSend.insert( intxCell );
1722 for( std::set< EntityHandle >::iterator it = overlapCellsToSend.begin(); it != overlapCellsToSend.end(); it++ )
1728 if( maxEdges < nnodes ) maxEdges = nnodes;
1734 MPI_Allreduce( &maxEdges, &globalMaxEdges, 1, MPI_INT, MPI_MAX, m_pcomm->comm() );
1736 globalMaxEdges = maxEdges;
1739 if(
is_root ) std::cout <<
"maximum number of edges for polygons to send is " << globalMaxEdges <<
"\n";
1746 for( std::set< EntityHandle >::iterator it = overlapCellsToSend.begin(); it != overlapCellsToSend.end(); it++ )
1751 for( std::set< EntityHandle >::iterator it = affectedCovCells.begin(); it != affectedCovCells.end(); it++ )
1756 std::stringstream ffs2;
1758 ffs2 <<
"affectedCells_" << m_pcomm->rank() <<
".h5m";
1771 std::map< int, std::set< EntityHandle > > verticesOverlapForTask;
1773 std::set< EntityHandle > allVerticesToSend;
1774 std::map< EntityHandle, int > allVerticesToSendMap;
1776 int numOverlapCells = 0;
1777 for( std::map<
int, std::set< EntityHandle > >::iterator it = overlapCellsForTask.begin();
1778 it != overlapCellsForTask.end(); it++ )
1780 int sendToProc = it->first;
1781 std::set< EntityHandle >& overlapCellsToSend2 = it->second;
1783 std::set< EntityHandle > vertices;
1784 for( std::set< EntityHandle >::iterator set_it = overlapCellsToSend2.begin();
1785 set_it != overlapCellsToSend2.end(); ++set_it )
1787 int nnodes_local = 0;
1790 for(
int k = 0; k < nnodes_local; k++ )
1791 vertices.insert( conn1[k] );
1793 verticesOverlapForTask[sendToProc] = vertices;
1794 numVerts += (int)vertices.size();
1795 numOverlapCells += (int)overlapCellsToSend2.size();
1796 allVerticesToSend.insert( vertices.begin(), vertices.end() );
1800 for( std::set< EntityHandle >::iterator vert_it = allVerticesToSend.begin(); vert_it != allVerticesToSend.end();
1804 allVerticesToSendMap[vert] = j;
1810 TLv.initialize( 2, 0, 0, 3, numVerts );
1811 TLv.enableWriteAccess();
1813 for( std::map<
int, std::set< EntityHandle > >::iterator it = verticesOverlapForTask.begin();
1814 it != verticesOverlapForTask.end(); it++ )
1816 int sendToProc = it->first;
1817 std::set< EntityHandle >& vertices = it->second;
1819 for( std::set< EntityHandle >::iterator it2 = vertices.begin(); it2 != vertices.end(); it2++, i++ )
1821 int n = TLv.get_n();
1822 TLv.vi_wr[2 * n] = sendToProc;
1824 int indexInAllVert = allVerticesToSendMap[v];
1825 TLv.vi_wr[2 * n + 1] = indexInAllVert;
1829 TLv.vr_wr[3 * n] = coords[0];
1830 TLv.vr_wr[3 * n + 1] = coords[1];
1831 TLv.vr_wr[3 * n + 2] = coords[2];
1837 int sizeTuple = 4 + globalMaxEdges;
1839 TLc.initialize( sizeTuple, 0, 0, 0,
1842 TLc.enableWriteAccess();
1844 for( std::map<
int, std::set< EntityHandle > >::iterator it = overlapCellsForTask.begin();
1845 it != overlapCellsForTask.end(); it++ )
1847 int sendToProc = it->first;
1848 std::set< EntityHandle >& overlapCellsToSend2 = it->second;
1850 for( std::set< EntityHandle >::iterator it2 = overlapCellsToSend2.begin(); it2 != overlapCellsToSend2.end();
1854 int sourceParentID, targetParentID;
1857 int n = TLc.get_n();
1858 TLc.vi_wr[sizeTuple * n] = sendToProc;
1859 TLc.vi_wr[sizeTuple * n + 1] = sourceParentID;
1860 TLc.vi_wr[sizeTuple * n + 2] = targetParentID;
1864 TLc.vi_wr[sizeTuple * n + 3] = nnodes;
1865 for(
int i = 0; i < nnodes; i++ )
1867 int indexVertex = allVerticesToSendMap[conn[i]];
1869 if( -1 == indexVertex )
MB_CHK_SET_ERR( MB_FAILURE,
"Can't find vertex in range of vertices to send" );
1870 TLc.vi_wr[sizeTuple * n + 4 + i] = indexVertex;
1873 for(
int i = nnodes; i < globalMaxEdges; i++ )
1874 TLc.vi_wr[sizeTuple * n + 4 + i] = 0;
1883 std::stringstream ff1;
1884 ff1 <<
"TLc_" <<
rank <<
".txt";
1885 TLc.print_to_file( ff1.str().c_str() );
1886 std::stringstream ffv;
1887 ffv <<
"TLv_" <<
rank <<
".txt";
1888 TLv.print_to_file( ffv.str().c_str() );
1890 ( m_pcomm->proc_config().crystal_router() )->gs_transfer( 1, TLv, 0 );
1891 ( m_pcomm->proc_config().crystal_router() )->gs_transfer( 1, TLc, 0 );
1894 TLc.print_to_file( ff1.str().c_str() );
1895 TLv.print_to_file( ffv.str().c_str() );
1901 buffer.buffer_init( sizeTuple * TLc.get_n() * 2 );
1904 TLc.print_to_file( ff1.str().c_str() );
1915 std::map< int, std::map< int, int > > availVertexIndicesPerProcessor;
1916 int nv = TLv.get_n();
1917 for(
int i = 0; i < nv; i++ )
1920 int orgProc = TLv.vi_rd[2 * i];
1921 int indexVert = TLv.vi_rd[2 * i + 1];
1922 availVertexIndicesPerProcessor[orgProc][indexVert] = i;
1932 int n = TLc.get_n();
1933 std::map< int, int > currentProcsCount;
1937 std::map< int, std::set< int > > sourcesForTasks;
1941 int currentSourceID = TLc.vi_rd[sizeTuple * 0 + 1];
1942 int proc0 = TLc.vi_rd[sizeTuple * 0];
1943 currentProcsCount[proc0] = 1;
1945 for(
int i = 1; i < n; i++ )
1947 int proc = TLc.vi_rd[sizeTuple * i];
1948 int sourceID = TLc.vi_rd[sizeTuple * i + 1];
1949 if( sourceID == currentSourceID )
1951 if( currentProcsCount.find( proc ) == currentProcsCount.end() )
1953 currentProcsCount[proc] = 1;
1956 currentProcsCount[proc]++;
1958 if( sourceID != currentSourceID || ( ( n - 1 ) == i ) )
1962 if( currentProcsCount.size() > 1 )
1965 std::cout <<
" source element " << currentSourceID <<
" intersects with "
1966 << currentProcsCount.size() <<
" target partitions\n";
1967 for( std::map< int, int >::iterator it = currentProcsCount.begin(); it != currentProcsCount.end();
1970 int procID = it->first;
1971 int numOverCells = it->second;
1972 std::cout <<
" task:" << procID <<
" " << numOverCells <<
" cells\n";
1977 for( std::map< int, int >::iterator it1 = currentProcsCount.begin(); it1 != currentProcsCount.end();
1980 int proc1 = it1->first;
1981 sourcesForTasks[currentSourceID].insert( proc1 );
1982 for( std::map< int, int >::iterator it2 = currentProcsCount.begin();
1983 it2 != currentProcsCount.end(); it2++ )
1985 int proc2 = it2->first;
1986 if( proc1 != proc2 ) sizeOfTLc2 += it2->second;
1991 if( sourceID != currentSourceID )
1993 currentSourceID = sourceID;
1994 currentProcsCount.clear();
1995 currentProcsCount[proc] = 1;
2004 std::cout <<
" need to initialize TLc2 with " << sizeOfTLc2 <<
" cells\n ";
2008 int sizeTuple2 = 5 + globalMaxEdges;
2011 TLc2.initialize( sizeTuple2, 0, 0, 0, sizeOfTLc2 );
2012 TLc2.enableWriteAccess();
2015 std::map< int, std::set< int > > verticesToSendForProc;
2017 for(
int i = 0; i < n; i++ )
2019 int sourceID = TLc.vi_rd[sizeTuple * i + 1];
2020 if( sourcesForTasks.find( sourceID ) != sourcesForTasks.end() )
2023 std::set< int > procs = sourcesForTasks[sourceID];
2024 if( procs.size() < 2 )
MB_CHK_SET_ERR( MB_FAILURE,
" not enough processes involved with a sourceID cell" );
2026 int orgProc = TLc.vi_rd[sizeTuple * i];
2029 std::map< int, int >& availableVerticesFromThisProc = availVertexIndicesPerProcessor[orgProc];
2030 for( std::set< int >::iterator setIt = procs.begin(); setIt != procs.end(); setIt++ )
2032 int procID = *setIt;
2035 if( procID != orgProc )
2038 int n2 = TLc2.get_n();
2039 if( n2 >= sizeOfTLc2 )
MB_CHK_SET_ERR( MB_FAILURE,
" memory overflow" );
2041 std::set< int >& indexVerticesInTLv = verticesToSendForProc[procID];
2042 TLc2.vi_wr[n2 * sizeTuple2] = procID;
2043 TLc2.vi_wr[n2 * sizeTuple2 + 1] = orgProc;
2044 TLc2.vi_wr[n2 * sizeTuple2 + 2] = sourceID;
2045 TLc2.vi_wr[n2 * sizeTuple2 + 3] = TLc.vi_rd[sizeTuple * i + 2];
2047 int nvert = TLc.vi_rd[sizeTuple * i + 3];
2048 TLc2.vi_wr[n2 * sizeTuple2 + 4] = nvert;
2053 for(
int j = 0; j < nvert; j++ )
2055 int vertexIndex = TLc.vi_rd[i * sizeTuple + 4 + j];
2057 if( availableVerticesFromThisProc.find( vertexIndex ) == availableVerticesFromThisProc.end() )
2059 MB_CHK_SET_ERR( MB_FAILURE,
" vertex index not available from processor" );
2061 TLc2.vi_wr[n2 * sizeTuple2 + 5 + j] = vertexIndex;
2062 int indexInTLv = availVertexIndicesPerProcessor[orgProc][vertexIndex];
2063 indexVerticesInTLv.insert( indexInTLv );
2066 for(
int j = nvert; j < globalMaxEdges; j++ )
2068 TLc2.vi_wr[n2 * sizeTuple2 + 5 + j] = 0;
2082 for( std::map<
int, std::set< int > >::iterator it = verticesToSendForProc.begin();
2083 it != verticesToSendForProc.end(); it++ )
2085 std::set< int >& indexInTLvSet = it->second;
2086 numVerts2 += (int)indexInTLvSet.size();
2088 TLv2.initialize( 3, 0, 0, 3,
2090 TLv2.enableWriteAccess();
2091 for( std::map<
int, std::set< int > >::iterator it = verticesToSendForProc.begin();
2092 it != verticesToSendForProc.end(); it++ )
2094 int sendToProc = it->first;
2095 std::set< int >& indexInTLvSet = it->second;
2097 for( std::set< int >::iterator itSet = indexInTLvSet.begin(); itSet != indexInTLvSet.end(); itSet++ )
2099 int indexInTLv = *itSet;
2100 int orgProc = TLv.vi_rd[2 * indexInTLv];
2101 int indexVertexInOrgProc = TLv.vi_rd[2 * indexInTLv + 1];
2102 int nv2 = TLv2.get_n();
2103 TLv2.vi_wr[3 * nv2] = sendToProc;
2104 TLv2.vi_wr[3 * nv2 + 1] = orgProc;
2105 TLv2.vi_wr[3 * nv2 + 2] = indexVertexInOrgProc;
2106 for(
int j = 0; j < 3; j++ )
2107 TLv2.vr_wr[3 * nv2 + j] =
2108 TLv.vr_rd[3 * indexInTLv + j];
2113 ( m_pcomm->proc_config().crystal_router() )->gs_transfer( 1, TLv2, 0 );
2114 ( m_pcomm->proc_config().crystal_router() )->gs_transfer( 1, TLc2, 0 );
2118 std::stringstream ff2;
2119 ff2 <<
"TLc2_" <<
rank <<
".txt";
2120 TLc2.print_to_file( ff2.str().c_str() );
2121 std::stringstream ffv2;
2122 ffv2 <<
"TLv2_" <<
rank <<
".txt";
2123 TLv2.print_to_file( ffv2.str().c_str() );
2132 int nvNew = TLv2.get_n();
2140 std::map< int, std::map< int, EntityHandle > > vertexPerProcAndIndex;
2141 for(
int i = 0; i < nvNew; i++ )
2143 int orgProc = TLv2.vi_rd[3 * i + 1];
2144 int indexInVert = TLv2.vi_rd[3 * i + 2];
2145 vertexPerProcAndIndex[orgProc][indexInVert] = newVerts[i];
2153 int ne = TLc2.get_n();
2154 for(
int i = 0; i < ne; i++ )
2156 int orgProc = TLc2.vi_rd[i * sizeTuple2 + 1];
2157 int sourceID = TLc2.vi_rd[i * sizeTuple2 + 2];
2158 int targetID = TLc2.vi_wr[i * sizeTuple2 + 3];
2159 int nve = TLc2.vi_wr[i * sizeTuple2 + 4];
2160 std::vector< EntityHandle > conn;
2162 for(
int j = 0; j < nve; j++ )
2164 int indexV = TLc2.vi_wr[i * sizeTuple2 + 5 + j];
2165 EntityHandle vh = vertexPerProcAndIndex[orgProc][indexV];
2170 newPolygons.insert( polyNew );
2182 std::stringstream ffs4;
2183 ffs4 <<
"extraIntxCells" <<
rank <<
".h5m";