8 #define _USE_MATH_DEFINES
24 #define CHECK_CONVEXITY
29 :
Intx2Mesh( mbimpl ), areaMethod( amethod ), plane( 0 ), Rsrc( 0.0 ), Rdest( 0.0 )
56 for(
int i = 1; i < nsTgt; i++ )
58 middle = 1. / nsTgt * middle;
61 for(
int j = 0; j < nsTgt; j++ )
68 for(
int j = 1; j < nsTgt - 1; j++ )
87 bool check_boxes_first )
104 if( check_boxes_first )
114 if( !overlap3d && (
plane != planeb ) )
118 if( !overlap3d &&
plane == planeb )
120 for(
int j = 0; j < nsBlue; j++ )
133 for(
int j = 0; j < nsTgt; j++ )
138 for(
int j = 0; j < nsBlue; j++ )
147 for(
int j = 0; j < nsBlue; j++ )
156 std::cout <<
"gnomonic plane: " <<
plane <<
"\n";
157 std::cout <<
" target src\n";
158 for(
int j = 0; j < nsTgt; j++ )
162 for(
int j = 0; j < nsBlue; j++ )
174 if( extraPoints >= 1 )
176 for(
int k = 0; k < nsBlue; k++ )
185 markb[( k + nsBlue - 1 ) % nsBlue] =
197 if( extraPoints >= 1 )
199 for(
int k = 0; k < nsTgt; k++ )
205 markr[( k + nsTgt - 1 ) % nsTgt] =
222 for(
int k = 1; k < nP - 1; k++ )
224 #ifdef CHECK_CONVEXITY
226 for(
int k = 0; k < nP; k++ )
228 int k1 = ( k + 1 ) % nP;
229 int k2 = ( k1 + 1 ) % nP;
231 if( orientedArea < 0 )
233 std::cout <<
" oriented area is negative: " << orientedArea <<
" k:" << k <<
" target, src:" << tgt
234 <<
" " << src <<
" \n";
257 for(
int n = 0; n < nP; n++ )
258 std::cout <<
" \t" << iP[2 * n] <<
"\t" << iP[2 * n + 1] <<
"\n";
272 std::vector< EntityHandle > foundIds;
273 foundIds.resize( nP );
274 #ifdef CHECK_CONVEXITY
280 for(
int i = 0; i < nP; i++ )
282 double* pp = &iP[2 * i];
291 for( j = 0; j < nsTgt && !found; j++ )
300 #ifdef CHECK_CONVEXITY
312 for( j = 0; j < nsBlue && !found; j++ )
322 #ifdef CHECK_CONVEXITY
337 double minArea = 1.e+38;
339 for( j = 0; j < nsTgt; j++ )
341 int j1 = ( j + 1 ) % nsTgt;
348 if( area < minArea && checkx < 2 *
epsilon_1 )
355 assert( index_min >= 0 );
365 std::cerr <<
" error in adjacent target edge: " <<
mb->
id_from_handle( adjTgtEdges[index_min] )
373 int nbExtraNodesSoFar = expts->size();
374 if( nbExtraNodesSoFar > 0 )
376 std::vector< CartVect > coords1;
377 coords1.resize( nbExtraNodesSoFar );
378 mb->
get_coords( &( *expts )[0], nbExtraNodesSoFar, &( coords1[0][0] ) );
380 for(
int k = 0; k < nbExtraNodesSoFar && !found; k++ )
383 double d2 = ( pos - coords1[k] ).
length();
387 foundIds[i] = ( *expts )[k];
388 #ifdef CHECK_CONVEXITY
402 ( *expts ).push_back( outNode );
405 foundIds[i] = outNode;
412 std::cout <<
" target quad: ";
413 for(
int j1 = 0; j1 < nsTgt; j1++ )
417 std::cout <<
" a point pp is not on a target quad " << *pp <<
" " << pp[1] <<
" target quad "
425 std::cout <<
" candidate polygon: nP" << nP <<
" plane: " <<
plane <<
"\n";
426 for(
int i1 = 0; i1 < nP; i1++ )
427 std::cout << iP[2 * i1] <<
" " << iP[2 * i1 + 1] <<
" " << foundIds[i1] <<
"\n";
434 #ifdef CHECK_CONVEXITY
465 #ifdef CHECK_CONVEXITY
467 std::vector< double > coords;
468 coords.resize( 3 * nP );
470 std::vector< CartVect > posi( nP );
473 for(
int k = 0; k < nP; k++ )
475 int k1 = ( k + 1 ) % nP;
476 int k2 = ( k1 + 1 ) % nP;
477 double orientedArea =
479 if( orientedArea < 0 )
481 std::cout <<
" np before 1 , 2, current " << npBefore1 <<
" " << npBefore2 <<
" " << nP <<
"\n";
482 for(
int i = 0; i < nP; i++ )
484 int nexti = ( i + 1 ) % nP;
485 double lengthEdge = ( posi[i] - posi[nexti] ).
length();
486 std::cout <<
" " << foundIds[i] <<
" edge en:" << lengthEdge <<
"\n";
488 std::cout <<
" old verts: " << oldNodes <<
" other intx:" << otherIntx <<
"\n";
490 std::cout <<
"rank:" <<
my_rank <<
" oriented area in 3d is negative: " << orientedArea <<
" k:" << k
491 <<
" target, src:" << tgt <<
" " << src <<
" \n";
499 std::cout <<
"Counting: " <<
counting <<
"\n";
500 std::cout <<
" polygon " <<
mb->
id_from_handle( polyNew ) <<
" nodes: " << nP <<
" :";
501 for(
int i1 = 0; i1 < nP; i1++ )
503 std::cout <<
" plane: " <<
plane <<
"\n";
504 std::vector< CartVect > posi( nP );
506 for(
int i1 = 0; i1 < nP; i1++ )
507 std::cout << foundIds[i1] <<
" " << posi[i1] <<
"\n";
509 std::stringstream fff;
510 fff <<
"file0" <<
counting <<
".vtk";
545 if( numTracers < 1 )
MB_CHK_SET_ERR( MB_FAILURE,
"no tracers data" );
547 std::vector< double > currentVals(
rs2.
size() * numTracers );
554 int n = remote_cells->get_n();
557 remote_cells_with_tracers =
new TupleList();
558 remote_cells_with_tracers->initialize( 2, 0, 1, numTracers,
560 remote_cells_with_tracers->enableWriteAccess();
561 for(
int i = 0; i < n; i++ )
563 remote_cells_with_tracers->vi_wr[2 * i] = remote_cells->vi_wr[2 * i];
564 remote_cells_with_tracers->vi_wr[2 * i + 1] = remote_cells->vi_wr[2 * i + 1];
566 remote_cells_with_tracers->vul_wr[i] =
567 remote_cells->vul_wr[i];
568 for(
int k = 0; k < numTracers; k++ )
569 remote_cells_with_tracers->vr_wr[numTracers * i + k] = 0;
570 remote_cells_with_tracers->inc_n();
574 remote_cells =
nullptr;
579 std::vector< double > newValues(
rs2.
size() * numTracers,
583 double check_intx_area = 0.;
588 int srcIndex, tgtIndex;
600 check_intx_area += areap;
609 if( !remote_cells_with_tracers )
MB_CHK_SET_ERR( MB_FAILURE,
"no remote cells, failure\n" );
613 "can't get arrival target for corresponding source gid" );
615 int index_in_remote = remote_cells_with_tracers->find( 1, global_id_src );
616 if( index_in_remote == -1 )
617 MB_CHK_SET_ERR( MB_FAILURE,
"can't find the global id element in remote cells\n" );
618 for(
int k = 0; k < numTracers; k++ )
619 remote_cells_with_tracers->vr_wr[index_in_remote * numTracers + k] +=
620 currentVals[numTracers * ( tgtIndex - 1 ) + k] * areap;
625 int arrTgtIndex =
rs2.
index( tgtArr );
626 if( -1 == arrTgtIndex )
MB_CHK_SET_ERR( MB_FAILURE,
"can't find the target arrival index" );
627 for(
int k = 0; k < numTracers; k++ )
628 newValues[numTracers * arrTgtIndex + k] += currentVals[( tgtIndex - 1 ) * numTracers + k] * areap;
632 MB_CHK_SET_ERR( rval,
"can't get arrival target for corresponding " );
637 if( remote_cells_with_tracers )
641 ( parcomm->proc_config().crystal_router() )->gs_transfer( 1, *remote_cells_with_tracers, 0 );
645 int n = remote_cells_with_tracers->get_n();
646 for(
int j = 0; j < n; j++ )
648 EntityHandle tgtCell = remote_cells_with_tracers->vul_rd[j];
649 int arrTgtIndex =
rs2.
index( tgtCell );
650 if( -1 == arrTgtIndex )
MB_CHK_SET_ERR( MB_FAILURE,
"can't find the target arrival index" );
651 for(
int k = 0; k < numTracers; k++ )
652 newValues[arrTgtIndex * numTracers + k] += remote_cells_with_tracers->vr_rd[j * numTracers + k];
659 void* data =
nullptr;
661 std::vector< double > total_mass_local( numTracers, 0. );
662 while( iter !=
rs2.
end() )
665 double* ptrArea = (
double*)data;
666 for(
int i = 0; i < count; i++, ++iter, j++, ptrArea++ )
668 for(
int k = 0; k < numTracers; k++ )
670 total_mass_local[k] += newValues[j * numTracers + k];
671 newValues[j * numTracers + k] /= ( *ptrArea );
678 std::vector< double > total_mass( numTracers, 0. );
679 double total_intx_area = 0;
681 MPI_Reduce( &total_mass_local[0], &total_mass[0], numTracers, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD );
682 if( MPI_SUCCESS != mpi_err )
return MB_FAILURE;
684 mpi_err = MPI_Reduce( &check_intx_area, &total_intx_area, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD );
685 if( MPI_SUCCESS != mpi_err )
return MB_FAILURE;
688 for(
int k = 0; k < numTracers; k++ )
689 std::cout <<
"total mass now tracer k=" << k + 1 <<
" " << total_mass[k] <<
"\n";
690 std::cout <<
"check: total intersection area: (4 * M_PI * R^2): " << 4 * M_PI *
Rsrc *
Rsrc <<
" "
691 << total_intx_area <<
"\n";
694 if( remote_cells_with_tracers )
696 delete remote_cells_with_tracers;
697 remote_cells_with_tracers =
nullptr;
700 for(
int k = 0; k < numTracers; k++ )
701 std::cout <<
"total mass now tracer k=" << k + 1 <<
" " << total_mass_local[k] <<
"\n";
702 std::cout <<
"check: total intersection area: (4 * M_PI * R^2): " << 4 * M_PI *
Rsrc *
Rsrc <<
" "
703 << check_intx_area <<
"\n";
713 return Intx2Mesh::build_processor_euler_boxes( euler_set, local_verts, gnomonic );
720 int num_local_verts = (int)local_verts.
size();
722 assert( parcomm !=
nullptr );
724 if( num_local_verts == 0 )
728 "can't get local vertices from set" );
729 num_local_verts = (int)local_verts.
size();
735 for(
int i = 0; i < 6; i++ )
737 gnom_box[4 * i] = gnom_box[4 * i + 1] = DBL_MAX;
738 gnom_box[4 * i + 2] = gnom_box[4 * i + 3] = -DBL_MAX;
744 std::vector< double > coords( 3 * num_local_verts );
748 std::vector< int > gnplane;
749 gnplane.resize( num_local_verts );
750 for(
int i = 0; i < num_local_verts; i++ )
752 CartVect pos( &coords[3 * i] );
777 std::vector< double > elco( 3 * nnodes );
778 std::set< int > planes;
779 for(
int i = 0; i < nnodes; i++ )
781 int ix = local_verts.
index( conn[i] );
782 planes.insert( gnplane[ix] );
783 for(
int j = 0; j < 3; j++ )
785 elco[3 * i + j] = coords[3 * ix + j];
789 for( std::set< int >::iterator st = planes.begin(); st != planes.end(); st++ )
792 for(
int i = 0; i < nnodes; i++ )
794 CartVect pos( &elco[3 * i] );
799 for(
int k = 0; k < 2; k++ )
802 if( val < gnom_box[4 * ( pl - 1 ) + k] ) gnom_box[4 * ( pl - 1 ) + k] = val;
803 if( val > gnom_box[4 * ( pl - 1 ) + 2 + k] )
804 gnom_box[4 * ( pl - 1 ) + 2 + k] = val;
810 int numprocs = parcomm->proc_config().proc_size();
813 my_rank = parcomm->proc_config().proc_rank();
814 for(
int k = 0; k < 24; k++ )
819 #if ( MPI_VERSION >= 2 )
821 mpi_err = MPI_Allgather( MPI_IN_PLACE, 0, MPI_DATATYPE_NULL, &
allBoxes[0], 24, MPI_DOUBLE,
822 parcomm->proc_config().proc_comm() );
825 std::vector< double > allBoxes_tmp( 24 * parcomm->proc_config().proc_size() );
826 mpi_err = MPI_Allgather( &
allBoxes[24 *
my_rank], 6, MPI_DOUBLE, &allBoxes_tmp[0], 24, MPI_DOUBLE,
827 parcomm->proc_config().proc_comm() );
831 if( MPI_SUCCESS != mpi_err )
return MB_FAILURE;
836 std::cout <<
" maximum number of vertices per cell are " <<
max_edges_1 <<
" on first mesh and " <<
max_edges_2
837 <<
" on second mesh \n";
838 for(
int i = 0; i < numprocs; i++ )
840 std::cout <<
"task: " << i <<
" \n";
841 for(
int pl = 1; pl <= 6; pl++ )
843 std::cout <<
" plane " << pl <<
" min: \t" <<
allBoxes[24 * i + 4 * ( pl - 1 )] <<
" \t"
844 <<
allBoxes[24 * i + 4 * ( pl - 1 ) + 1] <<
"\n";
845 std::cout <<
" \t max: \t" <<
allBoxes[24 * i + 4 * ( pl - 1 ) + 2] <<
" \t"
846 <<
allBoxes[24 * i + 4 * ( pl - 1 ) + 3] <<
"\n";
873 "can't create original sending processor tag" );
875 assert( parcomm !=
nullptr );
878 "can't get cells by dimension from mesh set" );
880 if( 1 == parcomm->proc_config().proc_size() )
890 "can't get vertices from mesh set" );
900 "can't create sending processor tag" );
906 int orig_sender = -1;
911 int size_gdofs_tag = 0;
912 std::vector< int > valsDOFs;
913 Tag gdsTag =
nullptr;
917 if( meshCells.size() > 0 )
919 oneCell = meshCells[0];
921 "can't get original sending processor value" );
933 valsDOFs.resize( lenTag );
935 "can't get SE DoF tag data" );
936 if( valsDOFs[0] > 0 )
940 size_gdofs_tag = lenTag;
953 int local_int_array[2], global_int_array[2];
954 local_int_array[0] = orig_sender;
955 local_int_array[1] = size_gdofs_tag;
958 MPI_Allreduce( local_int_array, global_int_array, 2, MPI_INT, MPI_MAX, parcomm->proc_config().proc_comm() );
959 if( MPI_SUCCESS != mpi_err )
return MB_FAILURE;
960 orig_sender = global_int_array[0];
961 size_gdofs_tag = global_int_array[1];
963 std::cout <<
"proc: " <<
my_rank <<
" size_gdofs_tag:" << size_gdofs_tag <<
"\n";
965 valsDOFs.resize( size_gdofs_tag );
969 int migrated_mesh = 0;
970 if( orig_sender != -1 ) migrated_mesh = 1;
977 size_t num_mesh_verts = mesh_verts.size();
980 std::vector< double > coords_mesh( 3 * num_mesh_verts );
984 std::vector< int > gnplane;
987 gnplane.resize( num_mesh_verts );
988 for(
size_t i = 0; i < num_mesh_verts; i++ )
990 CartVect pos( &coords_mesh[3 * i] );
997 std::vector< int > gids( num_mesh_verts );
1002 std::map< int, Range > Rto;
1003 int numprocs = parcomm->proc_config().proc_size();
1009 if( nb_ghost_layers > 0 )
1014 double global_diag = 0;
1015 mpi_err = MPI_Allreduce( &diagonal, &global_diag, 1, MPI_DOUBLE, MPI_MAX, parcomm->proc_config().proc_comm() );
1016 if( MPI_SUCCESS != mpi_err )
return MB_FAILURE;
1017 double extra_thickness = global_diag * nb_ghost_layers;
1018 if( gnomonic ) extra_thickness *= sqrt( 3. );
1021 std::cout <<
"ghost_layers:" << nb_ghost_layers <<
" max diagonal:" << global_diag
1022 <<
" extra thickness:" << extra_thickness <<
" box_error:" <<
box_error <<
"\n";
1024 for(
Range::iterator eit = meshCells.begin(); eit != meshCells.end(); ++eit )
1032 std::set< int > planes;
1033 std::vector< double > elco( 3 * num_nodes );
1034 for(
int i = 0; i < num_nodes; i++ )
1037 int index = mesh_verts.index( v );
1038 if( gnomonic ) planes.insert( gnplane[
index] );
1039 for(
int j = 0; j < 3; j++ )
1041 elco[3 * i + j] = coords_mesh[3 *
index + j];
1047 for( std::set< int >::iterator st = planes.begin(); st != planes.end(); st++ )
1050 double qmin[2] = { DBL_MAX, DBL_MAX };
1051 double qmax[2] = { -DBL_MAX, -DBL_MAX };
1052 for(
int i = 0; i < num_nodes; i++ )
1054 CartVect dp( &elco[3 * i] );
1059 for(
int j = 0; j < 2; j++ )
1061 if( qmin[j] > c2[j] ) qmin[j] = c2[j];
1062 if( qmax[j] < c2[j] ) qmax[j] = c2[j];
1067 for(
int p = 0; p < numprocs; p++ )
1069 double procMin1 =
allBoxes[24 * p + 4 * ( pl - 1 )];
1071 if( procMin1 >= DBL_MAX )
1073 double procMin2 =
allBoxes[24 * p + 4 * ( pl - 1 ) + 1];
1074 double procMax1 =
allBoxes[24 * p + 4 * ( pl - 1 ) + 2];
1075 double procMax2 =
allBoxes[24 * p + 4 * ( pl - 1 ) + 3];
1086 for(
int p = 0; p < numprocs; p++ )
1089 bool insert =
false;
1090 for(
int i = 0; i < num_nodes; i++ )
1092 if( box.contains_point( &elco[3 * i],
box_error ) )
1098 if( insert ) Rto[p].insert( q );
1113 for(
int p = 0; p < numprocs; p++ )
1115 Range& range_to_P = Rto[p];
1120 "can't get edges" );
1121 numq = numq + edgesToP.size();
1122 range_to_P.merge( edgesToP );
1125 if( range_to_P.empty() )
continue;
1127 std::cout <<
" proc : " <<
my_rank <<
" to proc " << p <<
" send " << range_to_P.size() <<
" cells "
1128 <<
" psize: " << range_to_P.psize() <<
"\n";
1132 numq = numq + range_to_P.size();
1133 numv = numv + vertsToP.size();
1135 range_to_P.merge( vertsToP );
1140 TLv.initialize( 2, 0, 0, 3, numv );
1141 TLv.enableWriteAccess();
1146 TLq.initialize( sizeTuple, 0, 0, 0,
1154 TLq.enableWriteAccess();
1156 std::cout <<
"from proc " <<
my_rank <<
" send " << numv <<
" vertices and " << numq <<
" elements\n";
1159 for(
int to_proc = 0; to_proc < numprocs; to_proc++ )
1161 if( to_proc == (
int)
my_rank )
continue;
1162 Range& range_to_P = Rto[to_proc];
1163 Range V = range_to_P.subset_by_type(
MBVERTEX );
1168 int index = mesh_verts.index( v );
1169 assert( -1 !=
index );
1170 int n = TLv.get_n();
1171 TLv.vi_wr[2 * n] = to_proc;
1172 TLv.vi_wr[2 * n + 1] = gids[
index];
1173 TLv.vr_wr[3 * n] = coords_mesh[3 *
index];
1174 TLv.vr_wr[3 * n + 1] = coords_mesh[3 *
index + 1];
1175 TLv.vr_wr[3 * n + 2] = coords_mesh[3 *
index + 2];
1179 Range Q =
subtract( range_to_P, mesh_verts );
1185 int n = TLq.get_n();
1186 TLq.vi_wr[sizeTuple * n] = to_proc;
1187 TLq.vi_wr[sizeTuple * n + 1] =
1197 for(
int i = 0; i < num_nodes; i++ )
1200 int index = mesh_verts.index( v );
1201 assert( -1 !=
index );
1202 TLq.vi_wr[sizeTuple * n + 2 + i] = gids[
index];
1206 TLq.vi_wr[sizeTuple * n + 2 + k] =
1215 "can't get original sender for polygon, in migrate scenario" );
1216 TLq.vi_wr[sizeTuple * n + currentIndexIntTuple] = orig_sender;
1217 currentIndexIntTuple++;
1220 if( size_gdofs_tag )
1223 for(
int i = 0; i < size_gdofs_tag; i++ )
1225 TLq.vi_wr[sizeTuple * n + currentIndexIntTuple + i] =
1236 ( parcomm->proc_config().crystal_router() )->gs_transfer( 1, TLv, 0 );
1237 ( parcomm->proc_config().crystal_router() )->gs_transfer( 1, TLq, 0 );
1243 std::map< int, EntityHandle > globalID_to_vertex_handle;
1249 for(
Range::iterator vit = mesh_verts.begin(); vit != mesh_verts.end(); ++vit, k++ )
1251 globalID_to_vertex_handle[gids[k]] = *vit;
1254 globalID_to_eh.clear();
1255 globalID_to_edgeh.clear();
1258 int n = TLv.get_n();
1259 for(
int i = 0; i < n; i++ )
1261 int globalId = TLv.vi_rd[2 * i + 1];
1262 if( globalID_to_vertex_handle.find( globalId ) ==
1263 globalID_to_vertex_handle.end() )
1267 double dp_pos[3] = { TLv.vr_wr[3 * i], TLv.vr_wr[3 * i + 1], TLv.vr_wr[3 * i + 2] };
1269 globalID_to_vertex_handle[globalId] = new_vert;
1272 "can't set global ID tag on new vertex " );
1281 Range local_q = local.subset_by_dimension( 2 );
1282 Range local_e = local.subset_by_dimension( 1 );
1284 for(
Range::iterator it = local_q.begin(); it != local_q.end(); ++it )
1289 assert( gid_el >= 0 );
1290 globalID_to_eh[gid_el] = q;
1296 for(
Range::iterator it = local_e.begin(); it != local_e.end(); ++it )
1301 assert( gid_el >= 0 );
1302 globalID_to_edgeh[gid_el] = q;
1312 for(
int i = 0; i < n; i++ )
1315 TLq.vi_rd[sizeTuple * i + 4] == 0;
1316 int globalIdEl = TLq.vi_rd[sizeTuple * i + 1];
1321 if( ( globalID_to_eh.find( globalIdEl ) != globalID_to_eh.end() ) && ( !isEdge ) )
continue;
1322 if( ( globalID_to_edgeh.find( globalIdEl ) != globalID_to_edgeh.end() ) && ( isEdge ) )
continue;
1328 int vgid = TLq.vi_rd[sizeTuple * i + 2 + j];
1329 if( vgid == 0 ) new_conn[j] = 0;
1334 assert( globalID_to_vertex_handle.find( vgid ) != globalID_to_vertex_handle.end() );
1335 new_conn[j] = globalID_to_vertex_handle[vgid];
1341 EntityType entType =
MBEDGE;
1342 if( nnodes == 3 ) entType =
MBTRI;
1343 if( nnodes == 4 ) entType =
MBQUAD;
1346 "can't create new element for second mesh " );
1349 globalID_to_edgeh[globalIdEl] = new_element;
1351 globalID_to_eh[globalIdEl] = new_element;
1352 local_q.insert( new_element );
1359 orig_sender = TLq.vi_wr[sizeTuple * i + currentIndexIntTuple];
1361 "can't set original sender for cell, in migrate scenario" );
1362 currentIndexIntTuple++;
1366 int from_proc = TLq.vi_rd[sizeTuple * i];
1370 if( size_gdofs_tag )
1372 for(
int j = 0; j < size_gdofs_tag; j++ )
1374 valsDOFs[j] = TLq.vi_wr[sizeTuple * i + currentIndexIntTuple + j];
1377 "can't set GLOBAL_DOFS data on coverage mesh" );
1384 std::cout <<
" proc " <<
my_rank <<
" add " << local_q.size() <<
" cells to covering set \n";
1391 #undef CHECK_CONVEXITY