40 #ifdef MOAB_HAVE_TEMPESTREMAP
42 #include "FiniteElementTools.h"
43 #include "GaussLobattoQuadrature.h"
55 if( initialize_fsets )
74 MPI_Initialized( &flagInit );
77 assert( m_pcomm !=
nullptr );
78 rank = m_pcomm->rank();
79 size = m_pcomm->size();
84 AnnounceOnlyOutputOnRankZero();
152 std::string inputFilename,
172 MB_CHK_SET_ERR( MB_FAILURE,
"Invalid IntersectionContext context provided" );
179 if( outputEnabled ) std::cout <<
"\nLoading TempestRemap Mesh object from file = " << inputFilename <<
" ...\n";
182 NcError
error( NcError::silent_nonfatal );
187 if( outputEnabled ) std::cout <<
"Loading mesh ...\n";
188 Mesh* mesh =
new Mesh( inputFilename );
189 mesh->RemoveZeroEdges();
190 if( outputEnabled ) std::cout <<
"----------------\n";
195 if( outputEnabled ) std::cout <<
"Validating mesh ...\n";
197 if( outputEnabled ) std::cout <<
"-------------------\n";
203 if( outputEnabled ) std::cout <<
"Constructing edge map on mesh ...\n";
204 mesh->ConstructEdgeMap(
false );
205 if( outputEnabled ) std::cout <<
"---------------------------------\n";
208 if( tempest_mesh ) *tempest_mesh = mesh;
210 catch( Exception& e )
212 std::cout <<
"TempestRemap ERROR: " << e.ToString() <<
"\n";
230 if( outputEnabled ) std::cout <<
"Converting (source) TempestRemap Mesh object to MOAB representation ...\n";
236 if( outputEnabled ) std::cout <<
"Converting (target) TempestRemap Mesh object to MOAB representation ...\n";
242 if( outputEnabled ) std::cout <<
"Converting (overlap) TempestRemap Mesh object to MOAB representation ...\n";
247 MB_CHK_SET_ERR( MB_FAILURE,
"Invalid IntersectionContext context provided" );
251 #define NEW_CONVERT_LOGIC
253 #ifdef NEW_CONVERT_LOGIC
261 const NodeVector& nodes = mesh->nodes;
262 const FaceVector& faces = mesh->faces;
273 std::vector< double* > arrays;
274 std::vector< int > gidsv( nodes.size() );
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 )
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;
285 Range mbverts( startv, startv + nodes.size() - 1 );
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 );
297 if( storeParentInfo )
302 "can't create positive tag" );
306 "can't create negative tag" );
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 );
322 for(
unsigned ifaces = 0; ifaces < faces.size(); ++ifaces )
324 const Face&
face = faces[ifaces];
325 const unsigned num_v_per_elem =
face.edges.size();
327 for(
unsigned iedges = 0; iedges < num_v_per_elem; ++iedges )
329 conn[iedges] = startv +
face.edges[iedges].node[0];
332 switch( num_v_per_elem )
338 "Can't get element connectivity" );
345 "Can't get element connectivity" );
353 "Can't get element connectivity" );
358 gidse[ifaces] = ifaces + 1;
360 if( storeParentInfo )
362 srcParent[ifaces] = mesh->vecSourceFaceIx[ifaces] + 1;
363 tgtParent[ifaces] = mesh->vecTargetFaceIx[ifaces] + 1;
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 );
374 "Can't set global_id tag" );
375 if( storeParentInfo )
378 "Can't set tag data" );
380 "Can't set tag data" );
384 std::copy( mbcells.begin(), mbcells.end(),
range_inserter( entities ) );
387 if( vertices ) *vertices = mbverts;
402 const NodeVector& nodes = mesh->nodes;
403 const FaceVector& faces = mesh->faces;
406 dbgprint.set_prefix(
"[TempestToMOAB]: " );
414 std::vector< double* > arrays;
415 std::vector< int > gidsv( nodes.size() );
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 )
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;
426 Range mbverts( startv, startv + nodes.size() - 1 );
433 Tag srcParentTag, tgtParentTag;
434 std::vector< int > srcParent, tgtParent;
435 bool storeParentInfo = ( mesh->vecSourceFaceIx.size() > 0 );
437 if( storeParentInfo )
442 "can't create positive tag" );
446 "can't create negative tag" );
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 )
463 const int iType = faces[ifaces].edges.size();
465 typeNSeqs[iType].push_back( ifaces );
468 for(
unsigned iType = 0; iType < NMAXPOLYEDGES; ++iType )
470 if( !nPolys[iType] )
continue;
472 const unsigned num_v_per_elem = iType;
477 switch( num_v_per_elem )
481 dbgprint.printf( 0,
"....Block %d: Triangular Elements [%u].\n", iBlock++, nPolys[iType] );
483 "Can't get element connectivity" );
487 dbgprint.printf( 0,
"....Block %d: Quadrilateral Elements [%u].\n", iBlock++, nPolys[iType] );
490 "Can't get element connectivity" );
494 dbgprint.printf( 0,
"....Block %d: Polygonal [%u] Elements [%u].\n", iBlock++, iType,
498 "Can't get element connectivity" );
502 Range mbcells( starte, starte + nPolys[iType] - 1 );
505 if( storeParentInfo )
507 srcParent.resize( mbcells.size(), -1 );
508 tgtParent.resize( mbcells.size(), -1 );
511 std::vector< int > gids( typeNSeqs[iType].
size() );
512 for(
unsigned ifaces = 0, offset = 0; ifaces < typeNSeqs[iType].size(); ++ifaces )
514 const int fIndex = typeNSeqs[iType][ifaces];
515 const Face&
face = faces[fIndex];
517 for(
unsigned iedges = 0; iedges <
face.edges.size(); ++iedges )
519 conn[offset++] = startv +
face.edges[iedges].node[0];
522 if( storeParentInfo )
524 srcParent[ifaces] = mesh->vecSourceFaceIx[fIndex] + 1;
525 tgtParent[ifaces] = mesh->vecTargetFaceIx[fIndex] + 1;
528 gids[ifaces] = typeNSeqs[iType][ifaces] + 1;
536 "Can't update adjacencies" );
543 if( storeParentInfo )
546 "Can't set tag data" );
548 "Can't set tag data" );
550 entities.
merge( mbcells );
554 if( vertices ) *vertices = mbverts;
570 "Can't convert source mesh to TempestRemap format" );
576 "Can't convert source coverage mesh to TempestRemap format" );
582 "Can't convert target mesh to TempestRemap format" );
599 if( outputEnabled )
dbgprint.printf( 0,
"Converting (source) MOAB to TempestRemap Mesh representation ...\n" );
602 "Can't convert source mesh to Tempest" );
612 dbgprint.printf( 0,
"Converting (covering source) MOAB to TempestRemap Mesh representation ...\n" );
615 "Can't convert convering source mesh to TempestRemap format" );
620 if( outputEnabled )
dbgprint.printf( 0,
"Converting (target) MOAB to TempestRemap Mesh representation ...\n" );
623 "Can't convert target mesh to Tempest" );
629 if( outputEnabled )
dbgprint.printf( 0,
"Converting (overlap) MOAB to TempestRemap Mesh representation ...\n" );
634 MB_CHK_SET_ERR( MB_FAILURE,
"Invalid IntersectionContext context provided" );
645 NodeVector& nodes = mesh->nodes;
646 FaceVector& faces = mesh->faces;
651 const size_t nelems = elems.
size();
654 faces.resize( nelems );
659 if( verts.
size() == 0 )
665 std::map< EntityHandle, int > indxMap;
666 bool useRange =
true;
675 std::vector< int > globIds( nelems );
678 std::vector< size_t > sortedIdx;
681 sortedIdx.resize( nelems );
683 std::iota( sortedIdx.begin(), sortedIdx.end(), 0 );
686 std::sort( sortedIdx.begin(), sortedIdx.end(),
687 [&globIds](
size_t i1,
size_t i2 ) { return globIds[i1] < globIds[i2]; } );
700 while( connectface[nnodesf - 2] == connectface[nnodesf - 1] && nnodesf > 3 )
703 face.edges.resize( nnodesf );
704 for(
int iverts = 0; iverts < nnodesf; ++iverts )
706 int indx = ( useRange ? verts.
index( connectface[iverts] ) : indxMap[connectface[iverts]] );
708 face.SetNode( iverts, indx );
712 unsigned nnodes = verts.
size();
713 nodes.resize( nnodes );
716 std::vector< double > coordx( nnodes ), coordy( nnodes ), coordz( nnodes );
718 for(
unsigned inode = 0; inode < nnodes; ++inode )
720 Node& node = nodes[inode];
721 node.x = coordx[inode];
722 node.y = coordy[inode];
723 node.z = coordz[inode];
729 mesh->RemoveCoincidentNodes();
730 mesh->RemoveZeroEdges();
752 if( std::get< 1 >( a ) == std::get< 1 >( b ) )
753 return std::get< 2 >( a ) < std::get< 2 >( b );
755 return std::get< 1 >( a ) < std::get< 1 >( b );
760 sharedGhostEntities.
clear();
767 MB_CHK_SET_ERR( m_interface->get_entities_by_dimension( m_overlap_set, 2, allents ),
768 "Getting entities dim 2 failed" );
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 )
777 sharedents.
insert( allents[i] );
779 allents =
subtract( allents, sharedents );
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 );
790 sharedGhostEntities.
merge( sharedents );
806 std::vector< std::array< int, 3 > > sorted_overlap_order( n_overlap_entitites,
807 std::array< int, 3 >( { -1, -1, -1 } ) );
809 Tag srcParentTag, tgtParentTag;
813 m_overlap->vecTargetFaceIx.resize( n_overlap_entitites );
814 m_overlap->vecSourceFaceIx.resize( n_overlap_entitites );
823 #ifdef USE_SORTED_GIDS
825 std::sort( gids_src.begin(), gids_src.end() );
826 std::sort( gids_tgt.begin(), gids_tgt.end() );
828 auto find_lid = []( std::vector< int >& gids,
int gid ) ->
int {
832 auto it = std::lower_bound( gids.begin(), gids.end(), gid );
833 return ( it != gids.end() ? std::distance( gids.begin(), it ) : -1 );
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 );
842 std::vector< int > ghFlags;
846 ghFlags.resize( n_overlap_entitites );
852 std::vector< int > rbids_src( n_overlap_entitites ), rbids_tgt( n_overlap_entitites );
856 for(
size_t ix = 0; ix < n_overlap_entitites; ++ix )
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 );
862 std::get< 2 >( sorted_overlap_order[ix] ) = -1;
864 std::get< 2 >( sorted_overlap_order[ix] ) = find_lid( gids_tgt, rbids_tgt[ix] );
868 std::sort( sorted_overlap_order.begin(), sorted_overlap_order.end(),
IntPairComparator );
870 for(
unsigned ie = 0; ie < n_overlap_entitites; ++ie )
873 m_overlap->vecSourceFaceIx[ie] = std::get< 1 >( sorted_overlap_order[ie] );
874 m_overlap->vecTargetFaceIx[ie] = std::get< 2 >( sorted_overlap_order[ie] );
879 faces.resize( n_overlap_entitites );
885 std::map< EntityHandle, int > indxMap;
894 const unsigned iface = std::get< 0 >( sorted_overlap_order[ifac] );
895 Face&
face = faces[ifac];
903 face.edges.resize( nnodesf );
904 for(
int iverts = 0; iverts < nnodesf; ++iverts )
906 int indx = indxMap[connectface[iverts]];
908 face.SetNode( iverts, indx );
912 sorted_overlap_order.clear();
914 unsigned nnodes = verts.
size();
916 nodes.resize( nnodes );
919 std::vector< double > coordx( nnodes ), coordy( nnodes ), coordz( nnodes );
921 for(
unsigned inode = 0; inode < nnodes; ++inode )
923 Node& node = nodes[inode];
924 node.x = coordx[inode];
925 node.y = coordy[inode];
926 node.z = coordz[inode];
947 const bool fAllParallel,
948 const bool fInputConcave,
949 const bool fOutputConcave )
954 if( is_root && size == 1 )
956 this->m_source->CalculateFaceAreas( fInputConcave );
957 this->m_target->CalculateFaceAreas( fOutputConcave );
958 this->m_overlap->Write( strOutputFileName.c_str(), NcFile::Netcdf4 );
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 );
974 this->m_source->CalculateFaceAreas( fInputConcave );
975 this->m_target->CalculateFaceAreas( fOutputConcave );
976 this->m_overlap->Write( strOutputFileName.c_str(), NcFile::Netcdf4 );
1011 #ifndef MOAB_HAVE_MPI
1014 const int dimension,
1015 const int start_id )
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++;
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 );
1047 const std::string& dofTagName,
1050 const int csResolution = std::sqrt( ntot_elements / 6.0 );
1051 if( csResolution * csResolution * 6 != ntot_elements )
return MB_INVALID_SIZE;
1056 if( GenerateCSMesh( csMesh, csResolution,
"",
"NetCDF4" ) )
1058 "Failed to generate CS mesh through TempestRemap" );
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" );
1068 const int ntot_elements,
1071 const std::string dofTagName,
1075 bool created =
false;
1078 "Failed creating DoF tag" );
1081 int nElements =
static_cast< int >( csMesh.faces.size() );
1086 DataArray3D< int > dataGLLnodes;
1087 dataGLLnodes.Allocate( nP, nP, nElements );
1089 std::map< Node, int > mapNodes;
1090 std::map< Node, moab::EntityHandle > mapLocalMBNodes;
1093 DataArray1D< double > dG;
1094 DataArray1D< double > dW;
1095 GaussLobattoQuadrature::GetPoints( nP, 0.0, 1.0, dG, dW );
1098 if( secondary_ents ) entities.
insert( secondary_ents->
begin(), secondary_ents->
end() );
1100 for(
unsigned iel = 0; iel < entities.
size(); ++iel )
1104 Node elCentroid( elcoords[0], elcoords[1], elcoords[2] );
1105 mapLocalMBNodes.insert( std::pair< Node, moab::EntityHandle >( elCentroid, eh ) );
1114 int* dofIDs =
new int[nP * nP];
1117 for(
int k = 0; k < nElements; k++ )
1119 const Face&
face = csMesh.faces[k];
1120 const NodeVector& nodes = csMesh.nodes;
1122 if(
face.edges.size() != 4 )
1124 _EXCEPTIONT(
"Mesh must only contain quadrilateral elements" );
1128 centroid.x = centroid.y = centroid.z = 0.0;
1129 for(
unsigned l = 0; l <
face.edges.size(); ++l )
1131 centroid.x += nodes[
face[l]].x;
1132 centroid.y += nodes[
face[l]].y;
1133 centroid.z += nodes[
face[l]].z;
1135 const double factor = 1.0 /
face.edges.size();
1136 centroid.x *= factor;
1137 centroid.y *= factor;
1138 centroid.z *= factor;
1140 bool locElem =
false;
1142 if( mapLocalMBNodes.find( centroid ) != mapLocalMBNodes.end() )
1145 current_eh = mapLocalMBNodes[centroid];
1148 for(
int j = 0; j < nP; j++ )
1150 for(
int i = 0; i < nP; i++ )
1165 const double& dAlpha = dG[i];
1166 const double& dBeta = dG[j];
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;
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;
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;
1181 double dR = sqrt( dXc * dXc + dYc * dYc + dZc * dZc );
1184 nodeGLL.x = dXc / dR;
1185 nodeGLL.y = dYc / dR;
1186 nodeGLL.z = dZc / dR;
1189 std::map< Node, int >::const_iterator iter = mapNodes.find( nodeGLL );
1190 if( iter == mapNodes.end() )
1193 int ixNode =
static_cast< int >( mapNodes.size() );
1194 mapNodes.insert( std::pair< Node, int >( nodeGLL, ixNode ) );
1195 dataGLLnodes[j][i][k] = ixNode + 1;
1199 dataGLLnodes[j][i][k] = iter->second + 1;
1202 dofIDs[j * nP + i] = dataGLLnodes[j][i][k];
1209 "Failed to tag_set_data for DoFs" );
1215 mapLocalMBNodes.clear();
1230 int nb_ghost_layers )
1232 if( nb_ghost_layers >= 1 ) gnomonic =
false;
1243 #ifdef MOAB_HAVE_MPI
1244 mbintx->set_parallel_comm( m_pcomm );
1254 #ifdef MOAB_HAVE_MPI
1260 "Can't create new set" );
1264 std::stringstream filename;
1265 filename <<
"covering" <<
rank <<
".h5m";
1267 std::stringstream targetFile;
1268 targetFile <<
"target" <<
rank <<
".h5m";
1278 "Can't create new set" );
1280 double tolerance = 1e-6, btolerance = 1e-3;
1288 for(
unsigned ie = 0; ie < targetVerts.
size(); ++ie )
1305 std::vector< moab::EntityHandle > leaf_elems;
1310 if( !leaf_elems.size() )
1318 std::vector< double > centroids( leaf_elems.size() * 3 );
1323 for(
size_t il = 0; il < leaf_elems.size(); ++il )
1325 const double* centroid = ¢roids[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 );
1330 if( locdist < dist )
1341 <<
": [Error] - Could not find a minimum distance within the leaf "
1343 << dist << std::endl;
1359 #ifdef MOAB_HAVE_MPI
1373 const bool outputEnabled = ( this->
rank == 0 );
1375 dbgprint.
set_prefix(
"[ComputeOverlapMesh]: " );
1395 bool concaveMeshA =
false, concaveMeshB =
false;
1400 "exact", concaveMeshA, concaveMeshB,
true,
false ) )
1401 MB_CHK_SET_ERR( MB_FAILURE,
"TempestRemap: cannot compute the intersection of meshes on the sphere" );
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" );
1415 dbgprint.printf( 0,
"Computing intersection mesh with the advancing-front propagation algorithm" );
1417 "Can't compute the intersection of meshes on the sphere" );
1420 #ifdef MOAB_HAVE_MPI
1424 std::stringstream ffc, fft, ffo;
1425 ffc <<
"cover_" <<
rank <<
".h5m";
1426 fft <<
"target_" <<
rank <<
".h5m";
1427 ffo <<
"intx_" <<
rank <<
".h5m";
1440 std::map< int, int > loc_gid_to_lid_covsrc;
1441 std::vector< int > gids( covEnts.
size(), -1 );
1446 for(
unsigned ie = 0; ie < gids.size(); ++ie )
1448 assert( gids[ie] > 0 );
1449 loc_gid_to_lid_covsrc[gids[ie]] = ie;
1452 Range intxCov, intxCells;
1462 assert( srcParent >= 0 );
1463 intxCov.
insert( covEnts[loc_gid_to_lid_covsrc[srcParent]] );
1486 MB_CHK_ERR(
mb->get_connectivity( eh, conn, num_nodes ) );
1495 "Unable to find skin" );
1500 MB_CHK_ERR(
mb->get_connectivity( *it, conn, len,
false ) );
1501 for(
int ie = 0; ie < len; ++ie )
1503 std::vector< EntityHandle > adjacent_entities;
1505 for(
auto ent : adjacent_entities )
1506 notNeededCovCells.
erase( ent );
1511 std::string(
"sourcecoveragemesh_p" + std::to_string(
rank ) +
".h5m" ).c_str(),
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()
1562 #ifdef MOAB_HAVE_MPI
1586 Range boundaryEdges;
1587 MB_CHK_ERR( skinner.find_skin( 0, this->m_target_entities,
false, boundaryEdges ) );
1591 Range boundaryCells;
1601 std::stringstream ffs;
1602 ffs <<
"boundaryCells_0" <<
rank <<
".h5m";
1609 std::set< int > targetBoundaryIds;
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 );
1623 std::set< int > affectedSourceCellsIds;
1624 Tag targetParentTag, sourceParentTag;
1627 for(
Range::iterator it = overlapCells.begin(); it != overlapCells.end(); it++ )
1630 int targetParentID, sourceParentID;
1632 if( targetBoundaryIds.find( targetParentID ) != targetBoundaryIds.end() )
1636 affectedSourceCellsIds.insert( sourceParentID );
1645 std::map< int, EntityHandle > affectedCovCellFromID;
1651 std::set< EntityHandle > affectedCovCells;
1656 for(
Range::iterator it = covCells.begin(); it != covCells.end(); it++ )
1661 if( affectedSourceCellsIds.find( covID ) != affectedSourceCellsIds.end() )
1664 affectedCovCellFromID[covID] = covCell;
1665 affectedCovCells.insert( covCell );
1676 std::map< int, std::set< EntityHandle > > overlapCellsForTask;
1679 std::set< EntityHandle > overlapCellsToSend;
1681 for(
Range::iterator it = overlapCells.begin(); it != overlapCells.end(); it++ )
1687 if( affectedSourceCellsIds.find( sourceParentID ) != affectedSourceCellsIds.end() )
1690 EntityHandle covCell = affectedCovCellFromID[sourceParentID];
1693 overlapCellsForTask[orgTask].insert( intxCell );
1695 overlapCellsToSend.insert( intxCell );
1704 for( std::set< EntityHandle >::iterator it = overlapCellsToSend.begin(); it != overlapCellsToSend.end(); it++ )
1710 if( maxEdges < nnodes ) maxEdges = nnodes;
1716 MPI_Allreduce( &maxEdges, &globalMaxEdges, 1, MPI_INT, MPI_MAX, m_pcomm->comm() );
1718 globalMaxEdges = maxEdges;
1721 if(
is_root ) std::cout <<
"maximum number of edges for polygons to send is " << globalMaxEdges <<
"\n";
1728 for( std::set< EntityHandle >::iterator it = overlapCellsToSend.begin(); it != overlapCellsToSend.end(); it++ )
1733 for( std::set< EntityHandle >::iterator it = affectedCovCells.begin(); it != affectedCovCells.end(); it++ )
1738 std::stringstream ffs2;
1740 ffs2 <<
"affectedCells_" << m_pcomm->rank() <<
".h5m";
1753 std::map< int, std::set< EntityHandle > > verticesOverlapForTask;
1755 std::set< EntityHandle > allVerticesToSend;
1756 std::map< EntityHandle, int > allVerticesToSendMap;
1758 int numOverlapCells = 0;
1759 for( std::map<
int, std::set< EntityHandle > >::iterator it = overlapCellsForTask.begin();
1760 it != overlapCellsForTask.end(); it++ )
1762 int sendToProc = it->first;
1763 std::set< EntityHandle >& overlapCellsToSend2 = it->second;
1765 std::set< EntityHandle > vertices;
1766 for( std::set< EntityHandle >::iterator set_it = overlapCellsToSend2.begin();
1767 set_it != overlapCellsToSend2.end(); ++set_it )
1769 int nnodes_local = 0;
1772 for(
int k = 0; k < nnodes_local; k++ )
1773 vertices.insert( conn1[k] );
1775 verticesOverlapForTask[sendToProc] = vertices;
1776 numVerts += (int)vertices.size();
1777 numOverlapCells += (int)overlapCellsToSend2.size();
1778 allVerticesToSend.insert( vertices.begin(), vertices.end() );
1782 for( std::set< EntityHandle >::iterator vert_it = allVerticesToSend.begin(); vert_it != allVerticesToSend.end();
1786 allVerticesToSendMap[vert] = j;
1792 TLv.initialize( 2, 0, 0, 3, numVerts );
1793 TLv.enableWriteAccess();
1795 for( std::map<
int, std::set< EntityHandle > >::iterator it = verticesOverlapForTask.begin();
1796 it != verticesOverlapForTask.end(); it++ )
1798 int sendToProc = it->first;
1799 std::set< EntityHandle >& vertices = it->second;
1801 for( std::set< EntityHandle >::iterator it2 = vertices.begin(); it2 != vertices.end(); it2++, i++ )
1803 int n = TLv.get_n();
1804 TLv.vi_wr[2 * n] = sendToProc;
1806 int indexInAllVert = allVerticesToSendMap[v];
1807 TLv.vi_wr[2 * n + 1] = indexInAllVert;
1811 TLv.vr_wr[3 * n] = coords[0];
1812 TLv.vr_wr[3 * n + 1] = coords[1];
1813 TLv.vr_wr[3 * n + 2] = coords[2];
1819 int sizeTuple = 4 + globalMaxEdges;
1821 TLc.initialize( sizeTuple, 0, 0, 0,
1824 TLc.enableWriteAccess();
1826 for( std::map<
int, std::set< EntityHandle > >::iterator it = overlapCellsForTask.begin();
1827 it != overlapCellsForTask.end(); it++ )
1829 int sendToProc = it->first;
1830 std::set< EntityHandle >& overlapCellsToSend2 = it->second;
1832 for( std::set< EntityHandle >::iterator it2 = overlapCellsToSend2.begin(); it2 != overlapCellsToSend2.end();
1836 int sourceParentID, targetParentID;
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;
1846 TLc.vi_wr[sizeTuple * n + 3] = nnodes;
1847 for(
int i = 0; i < nnodes; i++ )
1849 int indexVertex = allVerticesToSendMap[conn[i]];
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;
1855 for(
int i = nnodes; i < globalMaxEdges; i++ )
1856 TLc.vi_wr[sizeTuple * n + 4 + i] = 0;
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() );
1872 ( m_pcomm->proc_config().crystal_router() )->gs_transfer( 1, TLv, 0 );
1873 ( m_pcomm->proc_config().crystal_router() )->gs_transfer( 1, TLc, 0 );
1876 TLc.print_to_file( ff1.str().c_str() );
1877 TLv.print_to_file( ffv.str().c_str() );
1883 buffer.buffer_init( sizeTuple * TLc.get_n() * 2 );
1886 TLc.print_to_file( ff1.str().c_str() );
1897 std::map< int, std::map< int, int > > availVertexIndicesPerProcessor;
1898 int nv = TLv.get_n();
1899 for(
int i = 0; i < nv; i++ )
1902 int orgProc = TLv.vi_rd[2 * i];
1903 int indexVert = TLv.vi_rd[2 * i + 1];
1904 availVertexIndicesPerProcessor[orgProc][indexVert] = i;
1914 int n = TLc.get_n();
1915 std::map< int, int > currentProcsCount;
1919 std::map< int, std::set< int > > sourcesForTasks;
1923 int currentSourceID = TLc.vi_rd[sizeTuple * 0 + 1];
1924 int proc0 = TLc.vi_rd[sizeTuple * 0];
1925 currentProcsCount[proc0] = 1;
1927 for(
int i = 1; i < n; i++ )
1929 int proc = TLc.vi_rd[sizeTuple * i];
1930 int sourceID = TLc.vi_rd[sizeTuple * i + 1];
1931 if( sourceID == currentSourceID )
1933 if( currentProcsCount.find( proc ) == currentProcsCount.end() )
1935 currentProcsCount[proc] = 1;
1938 currentProcsCount[proc]++;
1940 if( sourceID != currentSourceID || ( ( n - 1 ) == i ) )
1944 if( currentProcsCount.size() > 1 )
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();
1952 int procID = it->first;
1953 int numOverCells = it->second;
1954 std::cout <<
" task:" << procID <<
" " << numOverCells <<
" cells\n";
1959 for( std::map< int, int >::iterator it1 = currentProcsCount.begin(); it1 != currentProcsCount.end();
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++ )
1967 int proc2 = it2->first;
1968 if( proc1 != proc2 ) sizeOfTLc2 += it2->second;
1973 if( sourceID != currentSourceID )
1975 currentSourceID = sourceID;
1976 currentProcsCount.clear();
1977 currentProcsCount[proc] = 1;
1986 std::cout <<
" need to initialize TLc2 with " << sizeOfTLc2 <<
" cells\n ";
1990 int sizeTuple2 = 5 + globalMaxEdges;
1993 TLc2.initialize( sizeTuple2, 0, 0, 0, sizeOfTLc2 );
1994 TLc2.enableWriteAccess();
1997 std::map< int, std::set< int > > verticesToSendForProc;
1999 for(
int i = 0; i < n; i++ )
2001 int sourceID = TLc.vi_rd[sizeTuple * i + 1];
2002 if( sourcesForTasks.find( sourceID ) != sourcesForTasks.end() )
2005 std::set< int > procs = sourcesForTasks[sourceID];
2006 if( procs.size() < 2 )
MB_CHK_SET_ERR( MB_FAILURE,
" not enough processes involved with a sourceID cell" );
2008 int orgProc = TLc.vi_rd[sizeTuple * i];
2011 std::map< int, int >& availableVerticesFromThisProc = availVertexIndicesPerProcessor[orgProc];
2012 for( std::set< int >::iterator setIt = procs.begin(); setIt != procs.end(); setIt++ )
2014 int procID = *setIt;
2017 if( procID != orgProc )
2020 int n2 = TLc2.get_n();
2021 if( n2 >= sizeOfTLc2 )
MB_CHK_SET_ERR( MB_FAILURE,
" memory overflow" );
2023 std::set< int >& indexVerticesInTLv = verticesToSendForProc[procID];
2024 TLc2.vi_wr[n2 * sizeTuple2] = procID;
2025 TLc2.vi_wr[n2 * sizeTuple2 + 1] = orgProc;
2026 TLc2.vi_wr[n2 * sizeTuple2 + 2] = sourceID;
2027 TLc2.vi_wr[n2 * sizeTuple2 + 3] = TLc.vi_rd[sizeTuple * i + 2];
2029 int nvert = TLc.vi_rd[sizeTuple * i + 3];
2030 TLc2.vi_wr[n2 * sizeTuple2 + 4] = nvert;
2035 for(
int j = 0; j < nvert; j++ )
2037 int vertexIndex = TLc.vi_rd[i * sizeTuple + 4 + j];
2039 if( availableVerticesFromThisProc.find( vertexIndex ) == availableVerticesFromThisProc.end() )
2041 MB_CHK_SET_ERR( MB_FAILURE,
" vertex index not available from processor" );
2043 TLc2.vi_wr[n2 * sizeTuple2 + 5 + j] = vertexIndex;
2044 int indexInTLv = availVertexIndicesPerProcessor[orgProc][vertexIndex];
2045 indexVerticesInTLv.insert( indexInTLv );
2048 for(
int j = nvert; j < globalMaxEdges; j++ )
2050 TLc2.vi_wr[n2 * sizeTuple2 + 5 + j] = 0;
2064 for( std::map<
int, std::set< int > >::iterator it = verticesToSendForProc.begin();
2065 it != verticesToSendForProc.end(); it++ )
2067 std::set< int >& indexInTLvSet = it->second;
2068 numVerts2 += (int)indexInTLvSet.size();
2070 TLv2.initialize( 3, 0, 0, 3,
2072 TLv2.enableWriteAccess();
2073 for( std::map<
int, std::set< int > >::iterator it = verticesToSendForProc.begin();
2074 it != verticesToSendForProc.end(); it++ )
2076 int sendToProc = it->first;
2077 std::set< int >& indexInTLvSet = it->second;
2079 for( std::set< int >::iterator itSet = indexInTLvSet.begin(); itSet != indexInTLvSet.end(); itSet++ )
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];
2095 ( m_pcomm->proc_config().crystal_router() )->gs_transfer( 1, TLv2, 0 );
2096 ( m_pcomm->proc_config().crystal_router() )->gs_transfer( 1, TLc2, 0 );
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() );
2114 int nvNew = TLv2.get_n();
2122 std::map< int, std::map< int, EntityHandle > > vertexPerProcAndIndex;
2123 for(
int i = 0; i < nvNew; i++ )
2125 int orgProc = TLv2.vi_rd[3 * i + 1];
2126 int indexInVert = TLv2.vi_rd[3 * i + 2];
2127 vertexPerProcAndIndex[orgProc][indexInVert] = newVerts[i];
2135 int ne = TLc2.get_n();
2136 for(
int i = 0; i < ne; i++ )
2138 int orgProc = TLc2.vi_rd[i * sizeTuple2 + 1];
2139 int sourceID = TLc2.vi_rd[i * sizeTuple2 + 2];
2140 int targetID = TLc2.vi_wr[i * sizeTuple2 + 3];
2141 int nve = TLc2.vi_wr[i * sizeTuple2 + 4];
2142 std::vector< EntityHandle > conn;
2144 for(
int j = 0; j < nve; j++ )
2146 int indexV = TLc2.vi_wr[i * sizeTuple2 + 5 + j];
2147 EntityHandle vh = vertexPerProcAndIndex[orgProc][indexV];
2152 newPolygons.insert( polyNew );
2164 std::stringstream ffs4;
2165 ffs4 <<
"extraIntxCells" <<
rank <<
".h5m";
2185 "Trouble creating GRID_IMASK tag" );
2194 "Trouble getting GRID_IMASK tag" );
2200 "Trouble getting GRID_IMASK tag" );
2209 "Trouble getting GRID_IMASK tag" );
2215 "Trouble getting GRID_IMASK tag" );