40 #ifdef MOAB_HAVE_TEMPESTREMAP
42 #include "FiniteElementTools.h"
43 #include "GaussLobattoQuadrature.h"
56 if( initialize_fsets )
75 MPI_Initialized( &flagInit );
78 assert( m_pcomm !=
nullptr );
79 rank = m_pcomm->rank();
80 size = m_pcomm->size();
85 AnnounceOnlyOutputOnRankZero();
153 std::string inputFilename,
173 MB_CHK_SET_ERR( MB_FAILURE,
"Invalid IntersectionContext context provided" );
180 if( outputEnabled ) std::cout <<
"\nLoading TempestRemap Mesh object from file = " << inputFilename <<
" ...\n";
183 NcError
error( NcError::silent_nonfatal );
188 if( outputEnabled ) std::cout <<
"Loading mesh ...\n";
189 Mesh* mesh =
new Mesh( inputFilename );
190 mesh->RemoveZeroEdges();
191 if( outputEnabled ) std::cout <<
"----------------\n";
196 if( outputEnabled ) std::cout <<
"Validating mesh ...\n";
198 if( outputEnabled ) std::cout <<
"-------------------\n";
204 if( outputEnabled ) std::cout <<
"Constructing edge map on mesh ...\n";
205 mesh->ConstructEdgeMap(
false );
206 if( outputEnabled ) std::cout <<
"---------------------------------\n";
209 if( tempest_mesh ) *tempest_mesh = mesh;
211 catch( Exception& e )
213 std::cout <<
"TempestRemap ERROR: " << e.ToString() <<
"\n";
231 if( outputEnabled ) std::cout <<
"Converting (source) TempestRemap Mesh object to MOAB representation ...\n";
237 if( outputEnabled ) std::cout <<
"Converting (target) TempestRemap Mesh object to MOAB representation ...\n";
243 if( outputEnabled ) std::cout <<
"Converting (overlap) TempestRemap Mesh object to MOAB representation ...\n";
248 MB_CHK_SET_ERR( MB_FAILURE,
"Invalid IntersectionContext context provided" );
252 #define NEW_CONVERT_LOGIC
254 #ifdef NEW_CONVERT_LOGIC
263 const NodeVector& nodes = mesh->nodes;
264 const FaceVector& faces = mesh->faces;
275 std::vector< double* > arrays;
276 std::vector< int > gidsv( nodes.size() );
278 rval =
iface->get_node_coords( 3, nodes.size(), 0, startv, arrays );
MB_CHK_SET_ERR( rval,
"Can't get node coords" );
279 for(
unsigned iverts = 0; iverts < nodes.size(); ++iverts )
281 const Node& node = nodes[iverts];
282 arrays[0][iverts] = node.x;
283 arrays[1][iverts] = node.y;
284 arrays[2][iverts] = node.z;
285 gidsv[iverts] = iverts + 1;
287 Range mbverts( startv, startv + nodes.size() - 1 );
294 Tag srcParentTag, tgtParentTag;
295 std::vector< int > srcParent( faces.size(), -1 ), tgtParent( faces.size(), -1 );
296 std::vector< int > gidse( faces.size(), -1 );
297 bool storeParentInfo = ( mesh->vecSourceFaceIx.size() > 0 );
299 if( storeParentInfo )
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 )
355 gidse[ifaces] = ifaces + 1;
357 if( storeParentInfo )
359 srcParent[ifaces] = mesh->vecSourceFaceIx[ifaces] + 1;
360 tgtParent[ifaces] = mesh->vecTargetFaceIx[ifaces] + 1;
364 if( ntris )
dbgprint.printf( 0,
"....Triangular Elements [%u].\n", ntris );
365 if( nquads )
dbgprint.printf( 0,
"....Quadrangular Elements [%u].\n", nquads );
366 if( npolys )
dbgprint.printf( 0,
"....Polygonal Elements [%u].\n", npolys );
371 if( storeParentInfo )
381 if( vertices ) *vertices = mbverts;
396 const NodeVector& nodes = mesh->nodes;
397 const FaceVector& faces = mesh->faces;
400 dbgprint.set_prefix(
"[TempestToMOAB]: " );
408 std::vector< double* > arrays;
409 std::vector< int > gidsv( nodes.size() );
411 rval =
iface->get_node_coords( 3, nodes.size(), 0, startv, arrays );
MB_CHK_SET_ERR( rval,
"Can't get node coords" );
412 for(
unsigned iverts = 0; iverts < nodes.size(); ++iverts )
414 const Node& node = nodes[iverts];
415 arrays[0][iverts] = node.x;
416 arrays[1][iverts] = node.y;
417 arrays[2][iverts] = node.z;
418 gidsv[iverts] = iverts + 1;
420 Range mbverts( startv, startv + nodes.size() - 1 );
427 Tag srcParentTag, tgtParentTag;
428 std::vector< int > srcParent, tgtParent;
429 bool storeParentInfo = ( mesh->vecSourceFaceIx.size() > 0 );
431 if( storeParentInfo )
449 dbgprint.printf( 0,
"..Mesh size: Nodes [%zu] Elements [%zu].\n", nodes.size(), faces.size() );
450 const int NMAXPOLYEDGES = 15;
451 std::vector< unsigned > nPolys( NMAXPOLYEDGES, 0 );
452 std::vector< std::vector< int > > typeNSeqs( NMAXPOLYEDGES );
453 for(
unsigned ifaces = 0; ifaces < faces.size(); ++ifaces )
455 const int iType = faces[ifaces].edges.size();
457 typeNSeqs[iType].push_back( ifaces );
460 for(
unsigned iType = 0; iType < NMAXPOLYEDGES; ++iType )
462 if( !nPolys[iType] )
continue;
464 const unsigned num_v_per_elem = iType;
469 switch( num_v_per_elem )
473 dbgprint.printf( 0,
"....Block %d: Triangular Elements [%u].\n", iBlock++, nPolys[iType] );
474 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" );
478 dbgprint.printf( 0,
"....Block %d: Quadrilateral Elements [%u].\n", iBlock++, nPolys[iType] );
479 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" );
483 dbgprint.printf( 0,
"....Block %d: Polygonal [%u] Elements [%u].\n", iBlock++, iType,
485 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" );
489 Range mbcells( starte, starte + nPolys[iType] - 1 );
492 if( storeParentInfo )
494 srcParent.resize( mbcells.size(), -1 );
495 tgtParent.resize( mbcells.size(), -1 );
498 std::vector< int > gids( typeNSeqs[iType].
size() );
499 for(
unsigned ifaces = 0, offset = 0; ifaces < typeNSeqs[iType].size(); ++ifaces )
501 const int fIndex = typeNSeqs[iType][ifaces];
502 const Face&
face = faces[fIndex];
504 for(
unsigned iedges = 0; iedges <
face.edges.size(); ++iedges )
506 conn[offset++] = startv +
face.edges[iedges].node[0];
509 if( storeParentInfo )
511 srcParent[ifaces] = mesh->vecSourceFaceIx[fIndex] + 1;
512 tgtParent[ifaces] = mesh->vecTargetFaceIx[fIndex] + 1;
515 gids[ifaces] = typeNSeqs[iType][ifaces] + 1;
522 rval =
iface->update_adjacencies( starte, nPolys[iType], num_v_per_elem, conn );
MB_CHK_SET_ERR( rval,
"Can't update adjacencies" );
528 if( storeParentInfo )
537 if( vertices ) *vertices = mbverts;
553 "Can't convert source mesh to TempestRemap format" );
559 "Can't convert source coverage mesh to TempestRemap format" );
565 "Can't convert target mesh to TempestRemap format" );
582 if( outputEnabled )
dbgprint.printf( 0,
"Converting (source) MOAB to TempestRemap Mesh representation ...\n" );
585 "Can't convert source mesh to Tempest" );
595 dbgprint.printf( 0,
"Converting (covering source) MOAB to TempestRemap Mesh representation ...\n" );
598 "Can't convert convering source mesh to TempestRemap format" );
603 if( outputEnabled )
dbgprint.printf( 0,
"Converting (target) MOAB to TempestRemap Mesh representation ...\n" );
606 "Can't convert target mesh to Tempest" );
612 if( outputEnabled )
dbgprint.printf( 0,
"Converting (overlap) MOAB to TempestRemap Mesh representation ...\n" );
617 MB_CHK_SET_ERR( MB_FAILURE,
"Invalid IntersectionContext context provided" );
631 NodeVector& nodes = mesh->nodes;
632 FaceVector& faces = mesh->faces;
637 const size_t nelems = elems.
size();
640 faces.resize( nelems );
644 if( verts.
size() == 0 )
650 std::map< EntityHandle, int > indxMap;
651 bool useRange =
true;
660 std::vector< int > globIds( nelems );
663 std::vector< size_t > sortedIdx;
666 sortedIdx.resize( nelems );
668 std::iota( sortedIdx.begin(), sortedIdx.end(), 0 );
671 std::sort( sortedIdx.begin(), sortedIdx.end(),
672 [&globIds](
size_t i1,
size_t i2 ) { return globIds[i1] < globIds[i2]; } );
685 while( connectface[nnodesf - 2] == connectface[nnodesf - 1] && nnodesf > 3 )
688 face.edges.resize( nnodesf );
689 for(
int iverts = 0; iverts < nnodesf; ++iverts )
691 int indx = ( useRange ? verts.
index( connectface[iverts] ) : indxMap[connectface[iverts]] );
693 face.SetNode( iverts, indx );
697 unsigned nnodes = verts.
size();
698 nodes.resize( nnodes );
701 std::vector< double > coordx( nnodes ), coordy( nnodes ), coordz( nnodes );
703 for(
unsigned inode = 0; inode < nnodes; ++inode )
705 Node& node = nodes[inode];
706 node.x = coordx[inode];
707 node.y = coordy[inode];
708 node.z = coordz[inode];
714 mesh->RemoveCoincidentNodes();
715 mesh->RemoveZeroEdges();
737 if( std::get< 1 >( a ) == std::get< 1 >( b ) )
738 return std::get< 2 >( a ) < std::get< 2 >( b );
740 return std::get< 1 >( a ) < std::get< 1 >( b );
745 sharedGhostEntities.
clear();
753 rval = m_interface->get_entities_by_dimension( m_overlap_set, 2, allents );
MB_CHK_SET_ERR( rval,
"Getting entities dim 2 failed" );
757 std::vector< int > ghFlags( allents.
size() );
758 rval = m_interface->tag_get_handle(
"ORIG_PROC", ghostTag );
MB_CHK_ERR( rval );
759 rval = m_interface->tag_get_data( ghostTag, allents, &ghFlags[0] );
MB_CHK_ERR( rval );
760 for(
unsigned i = 0; i < allents.
size(); ++i )
761 if( ghFlags[i] >= 0 )
762 sharedents.
insert( allents[i] );
764 allents =
subtract( allents, sharedents );
769 rval = m_interface->get_connectivity( allents, ownedverts );
MB_CHK_SET_ERR( rval,
"Deleting entities dim 0 failed" );
770 rval = m_interface->get_connectivity( sharedents, sharedverts );
MB_CHK_SET_ERR( rval,
"Deleting entities dim 0 failed" );
771 sharedverts =
subtract( sharedverts, ownedverts );
776 sharedGhostEntities.
merge( sharedents );
794 std::vector< std::array< int, 3 > > sorted_overlap_order( n_overlap_entitites,
795 std::array< int, 3 >( { -1, -1, -1 } ) );
797 Tag srcParentTag, tgtParentTag;
801 m_overlap->vecTargetFaceIx.resize( n_overlap_entitites );
802 m_overlap->vecSourceFaceIx.resize( n_overlap_entitites );
811 #ifdef USE_SORTED_GIDS
813 std::sort( gids_src.begin(), gids_src.end() );
814 std::sort( gids_tgt.begin(), gids_tgt.end() );
816 auto find_lid = []( std::vector< int >& gids,
int gid ) ->
int {
820 auto it = std::lower_bound( gids.begin(), gids.end(), gid );
821 return ( it != gids.end() ? std::distance( gids.begin(), it ) : -1 );
824 auto find_lid = []( std::vector< int >& gids,
int gid ) ->
int {
825 auto it = std::find( gids.begin(), gids.end(), gid );
826 return ( it != gids.end() ? std::distance( gids.begin(), it ) : -1 );
830 std::vector< int > ghFlags;
834 ghFlags.resize( n_overlap_entitites );
840 std::vector< int > rbids_src( n_overlap_entitites ), rbids_tgt( n_overlap_entitites );
844 for(
size_t ix = 0; ix < n_overlap_entitites; ++ix )
846 std::get< 0 >( sorted_overlap_order[ix] ) = ix;
847 std::get< 1 >( sorted_overlap_order[ix] ) = find_lid( gids_src, rbids_src[ix] );
848 assert( std::get< 1 >( sorted_overlap_order[ix] ) >= 0 );
850 std::get< 2 >( sorted_overlap_order[ix] ) = -1;
852 std::get< 2 >( sorted_overlap_order[ix] ) = find_lid( gids_tgt, rbids_tgt[ix] );
856 std::sort( sorted_overlap_order.begin(), sorted_overlap_order.end(),
IntPairComparator );
858 for(
unsigned ie = 0; ie < n_overlap_entitites; ++ie )
861 m_overlap->vecSourceFaceIx[ie] = std::get< 1 >( sorted_overlap_order[ie] );
862 m_overlap->vecTargetFaceIx[ie] = std::get< 2 >( sorted_overlap_order[ie] );
867 faces.resize( n_overlap_entitites );
873 std::map< EntityHandle, int > indxMap;
882 const unsigned iface = std::get< 0 >( sorted_overlap_order[ifac] );
883 Face&
face = faces[ifac];
891 face.edges.resize( nnodesf );
892 for(
int iverts = 0; iverts < nnodesf; ++iverts )
894 int indx = indxMap[connectface[iverts]];
896 face.SetNode( iverts, indx );
900 sorted_overlap_order.clear();
902 unsigned nnodes = verts.
size();
904 nodes.resize( nnodes );
907 std::vector< double > coordx( nnodes ), coordy( nnodes ), coordz( nnodes );
909 for(
unsigned inode = 0; inode < nnodes; ++inode )
911 Node& node = nodes[inode];
912 node.x = coordx[inode];
913 node.y = coordy[inode];
914 node.z = coordz[inode];
935 const bool fAllParallel,
936 const bool fInputConcave,
937 const bool fOutputConcave )
942 if( is_root &&
size == 1 )
944 this->m_source->CalculateFaceAreas( fInputConcave );
945 this->m_target->CalculateFaceAreas( fOutputConcave );
946 this->m_overlap->Write( strOutputFileName.c_str(), NcFile::Netcdf4 );
954 this->m_source->CalculateFaceAreas( fInputConcave );
955 this->m_covering_source->CalculateFaceAreas( fInputConcave );
956 this->m_target->CalculateFaceAreas( fOutputConcave );
957 this->m_overlap->Write( strOutputFileName.c_str(), NcFile::Netcdf4 );
962 this->m_source->CalculateFaceAreas( fInputConcave );
963 this->m_target->CalculateFaceAreas( fOutputConcave );
964 this->m_overlap->Write( strOutputFileName.c_str(), NcFile::Netcdf4 );
999 #ifndef MOAB_HAVE_MPI
1002 const int dimension,
1003 const int start_id )
1013 int idoffset = start_id;
1014 std::vector< int > gid(
entities.size() );
1015 for(
unsigned i = 0; i <
entities.size(); ++i )
1016 gid[i] = idoffset++;
1029 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 );
1035 const std::string& dofTagName,
1038 const int csResolution = std::sqrt( ntot_elements / 6.0 );
1039 if( csResolution * csResolution * 6 != ntot_elements )
return MB_INVALID_SIZE;
1044 if( GenerateCSMesh( csMesh, csResolution,
"",
"NetCDF4" ) )
1046 "Failed to generate CS mesh through TempestRemap" );
1049 if( this->
GenerateMeshMetadata( csMesh, ntot_elements, ents, secondary_ents, dofTagName, nP ) )
1050 MB_CHK_SET_ERR( moab::MB_FAILURE,
"Failed in call to GenerateMeshMetadata" );
1056 const int ntot_elements,
1059 const std::string dofTagName,
1065 bool created =
false;
1070 int nElements =
static_cast< int >( csMesh.faces.size() );
1075 DataArray3D< int > dataGLLnodes;
1076 dataGLLnodes.Allocate( nP, nP, nElements );
1078 std::map< Node, int > mapNodes;
1079 std::map< Node, moab::EntityHandle > mapLocalMBNodes;
1082 DataArray1D< double > dG;
1083 DataArray1D< double > dW;
1084 GaussLobattoQuadrature::GetPoints( nP, 0.0, 1.0, dG, dW );
1087 if( secondary_ents )
entities.insert( secondary_ents->
begin(), secondary_ents->
end() );
1089 for(
unsigned iel = 0; iel <
entities.size(); ++iel )
1093 Node elCentroid( elcoords[0], elcoords[1], elcoords[2] );
1094 mapLocalMBNodes.insert( std::pair< Node, moab::EntityHandle >( elCentroid, eh ) );
1103 int* dofIDs =
new int[nP * nP];
1106 for(
int k = 0; k < nElements; k++ )
1108 const Face&
face = csMesh.faces[k];
1109 const NodeVector& nodes = csMesh.nodes;
1111 if(
face.edges.size() != 4 )
1113 _EXCEPTIONT(
"Mesh must only contain quadrilateral elements" );
1117 centroid.x = centroid.y = centroid.z = 0.0;
1118 for(
unsigned l = 0; l <
face.edges.size(); ++l )
1120 centroid.x += nodes[
face[l]].x;
1121 centroid.y += nodes[
face[l]].y;
1122 centroid.z += nodes[
face[l]].z;
1124 const double factor = 1.0 /
face.edges.size();
1125 centroid.x *= factor;
1126 centroid.y *= factor;
1127 centroid.z *= factor;
1129 bool locElem =
false;
1131 if( mapLocalMBNodes.find( centroid ) != mapLocalMBNodes.end() )
1134 current_eh = mapLocalMBNodes[centroid];
1137 for(
int j = 0; j < nP; j++ )
1139 for(
int i = 0; i < nP; i++ )
1154 const double& dAlpha = dG[i];
1155 const double& dBeta = dG[j];
1158 double dXc = nodes[
face[0]].x * ( 1.0 - dAlpha ) * ( 1.0 - dBeta ) +
1159 nodes[
face[1]].x * dAlpha * ( 1.0 - dBeta ) + nodes[
face[2]].x * dAlpha * dBeta +
1160 nodes[
face[3]].x * ( 1.0 - dAlpha ) * dBeta;
1162 double dYc = nodes[
face[0]].y * ( 1.0 - dAlpha ) * ( 1.0 - dBeta ) +
1163 nodes[
face[1]].y * dAlpha * ( 1.0 - dBeta ) + nodes[
face[2]].y * dAlpha * dBeta +
1164 nodes[
face[3]].y * ( 1.0 - dAlpha ) * dBeta;
1166 double dZc = nodes[
face[0]].z * ( 1.0 - dAlpha ) * ( 1.0 - dBeta ) +
1167 nodes[
face[1]].z * dAlpha * ( 1.0 - dBeta ) + nodes[
face[2]].z * dAlpha * dBeta +
1168 nodes[
face[3]].z * ( 1.0 - dAlpha ) * dBeta;
1170 double dR = sqrt( dXc * dXc + dYc * dYc + dZc * dZc );
1173 nodeGLL.x = dXc / dR;
1174 nodeGLL.y = dYc / dR;
1175 nodeGLL.z = dZc / dR;
1178 std::map< Node, int >::const_iterator iter = mapNodes.find( nodeGLL );
1179 if( iter == mapNodes.end() )
1182 int ixNode =
static_cast< int >( mapNodes.size() );
1183 mapNodes.insert( std::pair< Node, int >( nodeGLL, ixNode ) );
1184 dataGLLnodes[j][i][k] = ixNode + 1;
1188 dataGLLnodes[j][i][k] = iter->second + 1;
1191 dofIDs[j * nP + i] = dataGLLnodes[j][i][k];
1203 mapLocalMBNodes.clear();
1218 int nb_ghost_layers )
1222 if (nb_ghost_layers >= 1)
1234 #ifdef MOAB_HAVE_MPI
1235 mbintx->set_parallel_comm( m_pcomm );
1245 #ifdef MOAB_HAVE_MPI
1254 std::stringstream filename;
1255 filename <<
"covering" <<
rank <<
".h5m";
1257 std::stringstream targetFile;
1258 targetFile <<
"target" <<
rank <<
".h5m";
1269 double tolerance = 1e-6, btolerance = 1e-3;
1277 for(
unsigned ie = 0; ie < targetVerts.
size(); ++ie )
1294 std::vector< moab::EntityHandle > leaf_elems;
1299 if( !leaf_elems.size() )
1307 std::vector< double > centroids( leaf_elems.size() * 3 );
1312 for(
size_t il = 0; il < leaf_elems.size(); ++il )
1314 const double* centroid = ¢roids[il * 3];
1315 const double locdist = std::pow( point[0] - centroid[0], 2 ) +
1316 std::pow( point[1] - centroid[1], 2 ) +
1317 std::pow( point[2] - centroid[2], 2 );
1319 if( locdist < dist )
1330 <<
": [Error] - Could not find a minimum distance within the leaf "
1332 << dist << std::endl;
1348 #ifdef MOAB_HAVE_MPI
1363 const bool outputEnabled = ( this->
rank == 0 );
1365 dbgprint.
set_prefix(
"[ComputeOverlapMesh]: " );
1385 bool concaveMeshA =
false, concaveMeshB =
false;
1390 "exact", concaveMeshA, concaveMeshB,
true,
false ) )
1391 MB_CHK_SET_ERR( MB_FAILURE,
"TempestRemap: cannot compute the intersection of meshes on the sphere" );
1398 if( outputEnabled )
dbgprint.printf( 0,
"Computing intersection mesh with the Kd-tree search algorithm" );
1404 dbgprint.printf( 0,
"Computing intersection mesh with the advancing-front propagation algorithm" );
1408 #ifdef MOAB_HAVE_MPI
1412 std::stringstream ffc, fft, ffo;
1413 ffc <<
"cover_" <<
rank <<
".h5m";
1414 fft <<
"target_" <<
rank <<
".h5m";
1415 ffo <<
"intx_" <<
rank <<
".h5m";
1428 std::map< int, int > loc_gid_to_lid_covsrc;
1429 std::vector< int > gids( covEnts.
size(), -1 );
1434 for(
unsigned ie = 0; ie < gids.size(); ++ie )
1436 assert( gids[ie] > 0 );
1437 loc_gid_to_lid_covsrc[gids[ie]] = ie;
1440 Range intxCov, intxCells;
1450 assert( srcParent >= 0 );
1451 intxCov.
insert( covEnts[loc_gid_to_lid_covsrc[srcParent]] );
1474 rval =
mb->get_connectivity( eh, conn, num_nodes );
MB_CHK_ERR( rval );
1487 rval =
mb->get_connectivity( *it, conn, len,
false );
MB_CHK_ERR( rval );
1488 for(
int ie = 0; ie < len; ++ie )
1490 std::vector< EntityHandle > adjacent_entities;
1492 for(
auto ent : adjacent_entities )
1493 notNeededCovCells.
erase( ent );
1498 std::string(
"sourcecoveragemesh_p" + std::to_string(
rank ) +
".h5m" ).c_str(),
1509 std::cout <<
" total participating elements in the covering set: " << intxCov.
size() <<
"\n";
1510 std::cout <<
" remove from coverage set elements that are not intersected: " << notNeededCovCells.
size()
1549 #ifdef MOAB_HAVE_MPI
1575 Range boundaryEdges;
1576 MB_CHK_ERR( skinner.find_skin( 0, this->m_target_entities,
false, boundaryEdges ) );
1580 Range boundaryCells;
1590 std::stringstream ffs;
1591 ffs <<
"boundaryCells_0" <<
rank <<
".h5m";
1598 std::set< int > targetBoundaryIds;
1604 "Can't get global id tag on target cell" );
1605 if( tid < 0 ) std::cout <<
" incorrect id for a target cell\n";
1606 targetBoundaryIds.insert( tid );
1612 std::set< int > affectedSourceCellsIds;
1613 Tag targetParentTag, sourceParentTag;
1616 for(
Range::iterator it = overlapCells.begin(); it != overlapCells.end(); it++ )
1619 int targetParentID, sourceParentID;
1621 if( targetBoundaryIds.find( targetParentID ) != targetBoundaryIds.end() )
1625 affectedSourceCellsIds.insert( sourceParentID );
1634 std::map< int, EntityHandle > affectedCovCellFromID;
1640 std::set< EntityHandle > affectedCovCells;
1645 for(
Range::iterator it = covCells.begin(); it != covCells.end(); it++ )
1650 if( affectedSourceCellsIds.find( covID ) != affectedSourceCellsIds.end() )
1653 affectedCovCellFromID[covID] = covCell;
1654 affectedCovCells.insert( covCell );
1665 std::map< int, std::set< EntityHandle > > overlapCellsForTask;
1668 std::set< EntityHandle > overlapCellsToSend;
1670 for(
Range::iterator it = overlapCells.begin(); it != overlapCells.end(); it++ )
1676 if( affectedSourceCellsIds.find( sourceParentID ) != affectedSourceCellsIds.end() )
1679 EntityHandle covCell = affectedCovCellFromID[sourceParentID];
1682 overlapCellsForTask[orgTask].insert( intxCell );
1684 overlapCellsToSend.insert( intxCell );
1693 for( std::set< EntityHandle >::iterator it = overlapCellsToSend.begin(); it != overlapCellsToSend.end(); it++ )
1699 if( maxEdges < nnodes ) maxEdges = nnodes;
1705 MPI_Allreduce( &maxEdges, &globalMaxEdges, 1, MPI_INT, MPI_MAX, m_pcomm->comm() );
1707 globalMaxEdges = maxEdges;
1710 if(
is_root ) std::cout <<
"maximum number of edges for polygons to send is " << globalMaxEdges <<
"\n";
1717 for( std::set< EntityHandle >::iterator it = overlapCellsToSend.begin(); it != overlapCellsToSend.end(); it++ )
1722 for( std::set< EntityHandle >::iterator it = affectedCovCells.begin(); it != affectedCovCells.end(); it++ )
1727 std::stringstream ffs2;
1729 ffs2 <<
"affectedCells_" << m_pcomm->rank() <<
".h5m";
1742 std::map< int, std::set< EntityHandle > > verticesOverlapForTask;
1744 std::set< EntityHandle > allVerticesToSend;
1745 std::map< EntityHandle, int > allVerticesToSendMap;
1747 int numOverlapCells = 0;
1748 for( std::map<
int, std::set< EntityHandle > >::iterator it = overlapCellsForTask.begin();
1749 it != overlapCellsForTask.end(); it++ )
1751 int sendToProc = it->first;
1752 std::set< EntityHandle >& overlapCellsToSend2 = it->second;
1754 std::set< EntityHandle > vertices;
1755 for( std::set< EntityHandle >::iterator set_it = overlapCellsToSend2.begin();
1756 set_it != overlapCellsToSend2.end(); ++set_it )
1758 int nnodes_local = 0;
1761 for(
int k = 0; k < nnodes_local; k++ )
1762 vertices.insert( conn1[k] );
1764 verticesOverlapForTask[sendToProc] = vertices;
1765 numVerts += (int)vertices.size();
1766 numOverlapCells += (int)overlapCellsToSend2.size();
1767 allVerticesToSend.insert( vertices.begin(), vertices.end() );
1771 for( std::set< EntityHandle >::iterator vert_it = allVerticesToSend.begin(); vert_it != allVerticesToSend.end();
1775 allVerticesToSendMap[vert] = j;
1781 TLv.initialize( 2, 0, 0, 3, numVerts );
1782 TLv.enableWriteAccess();
1784 for( std::map<
int, std::set< EntityHandle > >::iterator it = verticesOverlapForTask.begin();
1785 it != verticesOverlapForTask.end(); it++ )
1787 int sendToProc = it->first;
1788 std::set< EntityHandle >& vertices = it->second;
1790 for( std::set< EntityHandle >::iterator it2 = vertices.begin(); it2 != vertices.end(); it2++, i++ )
1792 int n = TLv.get_n();
1793 TLv.vi_wr[2 * n] = sendToProc;
1795 int indexInAllVert = allVerticesToSendMap[v];
1796 TLv.vi_wr[2 * n + 1] = indexInAllVert;
1800 TLv.vr_wr[3 * n] = coords[0];
1801 TLv.vr_wr[3 * n + 1] = coords[1];
1802 TLv.vr_wr[3 * n + 2] = coords[2];
1808 int sizeTuple = 4 + globalMaxEdges;
1810 TLc.initialize( sizeTuple, 0, 0, 0,
1813 TLc.enableWriteAccess();
1815 for( std::map<
int, std::set< EntityHandle > >::iterator it = overlapCellsForTask.begin();
1816 it != overlapCellsForTask.end(); it++ )
1818 int sendToProc = it->first;
1819 std::set< EntityHandle >& overlapCellsToSend2 = it->second;
1821 for( std::set< EntityHandle >::iterator it2 = overlapCellsToSend2.begin(); it2 != overlapCellsToSend2.end();
1825 int sourceParentID, targetParentID;
1828 int n = TLc.get_n();
1829 TLc.vi_wr[sizeTuple * n] = sendToProc;
1830 TLc.vi_wr[sizeTuple * n + 1] = sourceParentID;
1831 TLc.vi_wr[sizeTuple * n + 2] = targetParentID;
1835 TLc.vi_wr[sizeTuple * n + 3] = nnodes;
1836 for(
int i = 0; i < nnodes; i++ )
1838 int indexVertex = allVerticesToSendMap[conn[i]];
1840 if( -1 == indexVertex )
MB_CHK_SET_ERR( MB_FAILURE,
"Can't find vertex in range of vertices to send" );
1841 TLc.vi_wr[sizeTuple * n + 4 + i] = indexVertex;
1844 for(
int i = nnodes; i < globalMaxEdges; i++ )
1845 TLc.vi_wr[sizeTuple * n + 4 + i] = 0;
1854 std::stringstream ff1;
1855 ff1 <<
"TLc_" <<
rank <<
".txt";
1856 TLc.print_to_file( ff1.str().c_str() );
1857 std::stringstream ffv;
1858 ffv <<
"TLv_" <<
rank <<
".txt";
1859 TLv.print_to_file( ffv.str().c_str() );
1861 ( m_pcomm->proc_config().crystal_router() )->gs_transfer( 1, TLv, 0 );
1862 ( m_pcomm->proc_config().crystal_router() )->gs_transfer( 1, TLc, 0 );
1865 TLc.print_to_file( ff1.str().c_str() );
1866 TLv.print_to_file( ffv.str().c_str() );
1872 buffer.buffer_init( sizeTuple * TLc.get_n() * 2 );
1875 TLc.print_to_file( ff1.str().c_str() );
1886 std::map< int, std::map< int, int > > availVertexIndicesPerProcessor;
1887 int nv = TLv.get_n();
1888 for(
int i = 0; i < nv; i++ )
1891 int orgProc = TLv.vi_rd[2 * i];
1892 int indexVert = TLv.vi_rd[2 * i + 1];
1893 availVertexIndicesPerProcessor[orgProc][indexVert] = i;
1903 int n = TLc.get_n();
1904 std::map< int, int > currentProcsCount;
1908 std::map< int, std::set< int > > sourcesForTasks;
1912 int currentSourceID = TLc.vi_rd[sizeTuple * 0 + 1];
1913 int proc0 = TLc.vi_rd[sizeTuple * 0];
1914 currentProcsCount[proc0] = 1;
1916 for(
int i = 1; i < n; i++ )
1918 int proc = TLc.vi_rd[sizeTuple * i];
1919 int sourceID = TLc.vi_rd[sizeTuple * i + 1];
1920 if( sourceID == currentSourceID )
1922 if( currentProcsCount.find( proc ) == currentProcsCount.end() )
1924 currentProcsCount[proc] = 1;
1927 currentProcsCount[proc]++;
1929 if( sourceID != currentSourceID || ( ( n - 1 ) == i ) )
1933 if( currentProcsCount.size() > 1 )
1936 std::cout <<
" source element " << currentSourceID <<
" intersects with "
1937 << currentProcsCount.size() <<
" target partitions\n";
1938 for( std::map< int, int >::iterator it = currentProcsCount.begin(); it != currentProcsCount.end();
1941 int procID = it->first;
1942 int numOverCells = it->second;
1943 std::cout <<
" task:" << procID <<
" " << numOverCells <<
" cells\n";
1948 for( std::map< int, int >::iterator it1 = currentProcsCount.begin(); it1 != currentProcsCount.end();
1951 int proc1 = it1->first;
1952 sourcesForTasks[currentSourceID].insert( proc1 );
1953 for( std::map< int, int >::iterator it2 = currentProcsCount.begin();
1954 it2 != currentProcsCount.end(); it2++ )
1956 int proc2 = it2->first;
1957 if( proc1 != proc2 ) sizeOfTLc2 += it2->second;
1962 if( sourceID != currentSourceID )
1964 currentSourceID = sourceID;
1965 currentProcsCount.clear();
1966 currentProcsCount[proc] = 1;
1975 std::cout <<
" need to initialize TLc2 with " << sizeOfTLc2 <<
" cells\n ";
1979 int sizeTuple2 = 5 + globalMaxEdges;
1982 TLc2.initialize( sizeTuple2, 0, 0, 0, sizeOfTLc2 );
1983 TLc2.enableWriteAccess();
1986 std::map< int, std::set< int > > verticesToSendForProc;
1988 for(
int i = 0; i < n; i++ )
1990 int sourceID = TLc.vi_rd[sizeTuple * i + 1];
1991 if( sourcesForTasks.find( sourceID ) != sourcesForTasks.end() )
1994 std::set< int > procs = sourcesForTasks[sourceID];
1995 if( procs.size() < 2 )
MB_CHK_SET_ERR( MB_FAILURE,
" not enough processes involved with a sourceID cell" );
1997 int orgProc = TLc.vi_rd[sizeTuple * i];
2000 std::map< int, int >& availableVerticesFromThisProc = availVertexIndicesPerProcessor[orgProc];
2001 for( std::set< int >::iterator setIt = procs.begin(); setIt != procs.end(); setIt++ )
2003 int procID = *setIt;
2006 if( procID != orgProc )
2009 int n2 = TLc2.get_n();
2010 if( n2 >= sizeOfTLc2 )
MB_CHK_SET_ERR( MB_FAILURE,
" memory overflow" );
2012 std::set< int >& indexVerticesInTLv = verticesToSendForProc[procID];
2013 TLc2.vi_wr[n2 * sizeTuple2] = procID;
2014 TLc2.vi_wr[n2 * sizeTuple2 + 1] = orgProc;
2015 TLc2.vi_wr[n2 * sizeTuple2 + 2] = sourceID;
2016 TLc2.vi_wr[n2 * sizeTuple2 + 3] = TLc.vi_rd[sizeTuple * i + 2];
2018 int nvert = TLc.vi_rd[sizeTuple * i + 3];
2019 TLc2.vi_wr[n2 * sizeTuple2 + 4] = nvert;
2024 for(
int j = 0; j < nvert; j++ )
2026 int vertexIndex = TLc.vi_rd[i * sizeTuple + 4 + j];
2028 if( availableVerticesFromThisProc.find( vertexIndex ) == availableVerticesFromThisProc.end() )
2030 MB_CHK_SET_ERR( MB_FAILURE,
" vertex index not available from processor" );
2032 TLc2.vi_wr[n2 * sizeTuple2 + 5 + j] = vertexIndex;
2033 int indexInTLv = availVertexIndicesPerProcessor[orgProc][vertexIndex];
2034 indexVerticesInTLv.insert( indexInTLv );
2037 for(
int j = nvert; j < globalMaxEdges; j++ )
2039 TLc2.vi_wr[n2 * sizeTuple2 + 5 + j] = 0;
2053 for( std::map<
int, std::set< int > >::iterator it = verticesToSendForProc.begin();
2054 it != verticesToSendForProc.end(); it++ )
2056 std::set< int >& indexInTLvSet = it->second;
2057 numVerts2 += (int)indexInTLvSet.size();
2059 TLv2.initialize( 3, 0, 0, 3,
2061 TLv2.enableWriteAccess();
2062 for( std::map<
int, std::set< int > >::iterator it = verticesToSendForProc.begin();
2063 it != verticesToSendForProc.end(); it++ )
2065 int sendToProc = it->first;
2066 std::set< int >& indexInTLvSet = it->second;
2068 for( std::set< int >::iterator itSet = indexInTLvSet.begin(); itSet != indexInTLvSet.end(); itSet++ )
2070 int indexInTLv = *itSet;
2071 int orgProc = TLv.vi_rd[2 * indexInTLv];
2072 int indexVertexInOrgProc = TLv.vi_rd[2 * indexInTLv + 1];
2073 int nv2 = TLv2.get_n();
2074 TLv2.vi_wr[3 * nv2] = sendToProc;
2075 TLv2.vi_wr[3 * nv2 + 1] = orgProc;
2076 TLv2.vi_wr[3 * nv2 + 2] = indexVertexInOrgProc;
2077 for(
int j = 0; j < 3; j++ )
2078 TLv2.vr_wr[3 * nv2 + j] =
2079 TLv.vr_rd[3 * indexInTLv + j];
2084 ( m_pcomm->proc_config().crystal_router() )->gs_transfer( 1, TLv2, 0 );
2085 ( m_pcomm->proc_config().crystal_router() )->gs_transfer( 1, TLc2, 0 );
2089 std::stringstream ff2;
2090 ff2 <<
"TLc2_" <<
rank <<
".txt";
2091 TLc2.print_to_file( ff2.str().c_str() );
2092 std::stringstream ffv2;
2093 ffv2 <<
"TLv2_" <<
rank <<
".txt";
2094 TLv2.print_to_file( ffv2.str().c_str() );
2103 int nvNew = TLv2.get_n();
2111 std::map< int, std::map< int, EntityHandle > > vertexPerProcAndIndex;
2112 for(
int i = 0; i < nvNew; i++ )
2114 int orgProc = TLv2.vi_rd[3 * i + 1];
2115 int indexInVert = TLv2.vi_rd[3 * i + 2];
2116 vertexPerProcAndIndex[orgProc][indexInVert] = newVerts[i];
2124 int ne = TLc2.get_n();
2125 for(
int i = 0; i < ne; i++ )
2127 int orgProc = TLc2.vi_rd[i * sizeTuple2 + 1];
2128 int sourceID = TLc2.vi_rd[i * sizeTuple2 + 2];
2129 int targetID = TLc2.vi_wr[i * sizeTuple2 + 3];
2130 int nve = TLc2.vi_wr[i * sizeTuple2 + 4];
2131 std::vector< EntityHandle > conn;
2133 for(
int j = 0; j < nve; j++ )
2135 int indexV = TLc2.vi_wr[i * sizeTuple2 + 5 + j];
2136 EntityHandle vh = vertexPerProcAndIndex[orgProc][indexV];
2141 newPolygons.insert( polyNew );
2153 std::stringstream ffs4;
2154 ffs4 <<
"extraIntxCells" <<
rank <<
".h5m";