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++ )
155 std::cout <<
"gnomonic plane: " <<
plane <<
"\n";
156 std::cout <<
" target src\n";
157 for(
int j = 0; j < nsTgt; j++ )
161 for(
int j = 0; j < nsBlue; j++ )
173 if( extraPoints >= 1 )
175 for(
int k = 0; k < nsBlue; k++ )
184 markb[( k + nsBlue - 1 ) % nsBlue] =
196 if( extraPoints >= 1 )
198 for(
int k = 0; k < nsTgt; k++ )
204 markr[( k + nsTgt - 1 ) % nsTgt] =
221 for(
int k = 1; k < nP - 1; k++ )
223 #ifdef CHECK_CONVEXITY
225 for(
int k = 0; k < nP; k++ )
227 int k1 = ( k + 1 ) % nP;
228 int k2 = ( k1 + 1 ) % nP;
230 if( orientedArea < 0 )
232 std::cout <<
" oriented area is negative: " << orientedArea <<
" k:" << k <<
" target, src:" << tgt
233 <<
" " << src <<
" \n";
256 for(
int n = 0; n < nP; n++ )
257 std::cout <<
" \t" << iP[2 * n] <<
"\t" << iP[2 * n + 1] <<
"\n";
271 std::vector< EntityHandle > foundIds;
272 foundIds.resize( nP );
273 #ifdef CHECK_CONVEXITY
279 for(
int i = 0; i < nP; i++ )
281 double* pp = &iP[2 * i];
290 for( j = 0; j < nsTgt && !found; j++ )
299 #ifdef CHECK_CONVEXITY
311 for( j = 0; j < nsBlue && !found; j++ )
321 #ifdef CHECK_CONVEXITY
336 double minArea = 1.e+38;
338 for( j = 0; j < nsTgt; j++ )
340 int j1 = ( j + 1 ) % nsTgt;
347 if( area < minArea && checkx < 2 *
epsilon_1 )
354 assert( index_min >= 0 );
364 std::cerr <<
" error in adjacent target edge: " <<
mb->
id_from_handle( adjTgtEdges[index_min] )
372 int nbExtraNodesSoFar = expts->size();
373 if( nbExtraNodesSoFar > 0 )
375 std::vector< CartVect > coords1;
376 coords1.resize( nbExtraNodesSoFar );
377 mb->
get_coords( &( *expts )[0], nbExtraNodesSoFar, &( coords1[0][0] ) );
379 for(
int k = 0; k < nbExtraNodesSoFar && !found; k++ )
382 double d2 = ( pos - coords1[k] ).
length();
386 foundIds[i] = ( *expts )[k];
387 #ifdef CHECK_CONVEXITY
401 ( *expts ).push_back( outNode );
404 foundIds[i] = outNode;
411 std::cout <<
" target quad: ";
412 for(
int j1 = 0; j1 < nsTgt; j1++ )
416 std::cout <<
" a point pp is not on a target quad " << *pp <<
" " << pp[1] <<
" target quad "
424 std::cout <<
" candidate polygon: nP" << nP <<
" plane: " <<
plane <<
"\n";
425 for(
int i1 = 0; i1 < nP; i1++ )
426 std::cout << iP[2 * i1] <<
" " << iP[2 * i1 + 1] <<
" " << foundIds[i1] <<
"\n";
433 #ifdef CHECK_CONVEXITY
464 #ifdef CHECK_CONVEXITY
466 std::vector< double > coords;
467 coords.resize( 3 * nP );
469 std::vector< CartVect > posi( nP );
472 for(
int k = 0; k < nP; k++ )
474 int k1 = ( k + 1 ) % nP;
475 int k2 = ( k1 + 1 ) % nP;
476 double orientedArea = areaAdaptor. area_spherical_triangle( &coords[3 * k], &coords[3 * k1], &coords[3 * k2],
Rdest );
477 if( orientedArea < 0 )
479 std::cout <<
" np before 1 , 2, current " << npBefore1 <<
" " << npBefore2 <<
" " << nP <<
"\n";
480 for(
int i = 0; i < nP; i++ )
482 int nexti = ( i + 1 ) % nP;
483 double lengthEdge = ( posi[i] - posi[nexti] ).
length();
484 std::cout <<
" " << foundIds[i] <<
" edge en:" << lengthEdge <<
"\n";
486 std::cout <<
" old verts: " << oldNodes <<
" other intx:" << otherIntx <<
"\n";
488 std::cout <<
"rank:" <<
my_rank <<
" oriented area in 3d is negative: " << orientedArea <<
" k:" << k
489 <<
" target, src:" << tgt <<
" " << src <<
" \n";
497 std::cout <<
"Counting: " <<
counting <<
"\n";
498 std::cout <<
" polygon " <<
mb->
id_from_handle( polyNew ) <<
" nodes: " << nP <<
" :";
499 for(
int i1 = 0; i1 < nP; i1++ )
501 std::cout <<
" plane: " <<
plane <<
"\n";
502 std::vector< CartVect > posi( nP );
504 for(
int i1 = 0; i1 < nP; i1++ )
505 std::cout << foundIds[i1] <<
" " << posi[i1] <<
"\n";
507 std::stringstream fff;
508 fff <<
"file0" <<
counting <<
".vtk";
543 if( numTracers < 1 )
MB_CHK_SET_ERR( MB_FAILURE,
"no tracers data" );
545 std::vector< double > currentVals(
rs2.
size() * numTracers );
552 int n = remote_cells->get_n();
555 remote_cells_with_tracers =
new TupleList();
556 remote_cells_with_tracers->initialize( 2, 0, 1, numTracers,
558 remote_cells_with_tracers->enableWriteAccess();
559 for(
int i = 0; i < n; i++ )
561 remote_cells_with_tracers->vi_wr[2 * i] = remote_cells->vi_wr[2 * i];
562 remote_cells_with_tracers->vi_wr[2 * i + 1] = remote_cells->vi_wr[2 * i + 1];
564 remote_cells_with_tracers->vul_wr[i] =
565 remote_cells->vul_wr[i];
566 for(
int k = 0; k < numTracers; k++ )
567 remote_cells_with_tracers->vr_wr[numTracers * i + k] = 0;
568 remote_cells_with_tracers->inc_n();
577 std::vector< double > newValues(
rs2.
size() * numTracers,
581 double check_intx_area = 0.;
586 int srcIndex, tgtIndex;
596 double radius =
Rsrc;
598 check_intx_area += areap;
607 if( !remote_cells_with_tracers )
MB_CHK_SET_ERR( MB_FAILURE,
"no remote cells, failure\n" );
612 int index_in_remote = remote_cells_with_tracers->find( 1, global_id_src );
613 if( index_in_remote == -1 )
614 MB_CHK_SET_ERR( MB_FAILURE,
"can't find the global id element in remote cells\n" );
615 for(
int k = 0; k < numTracers; k++ )
616 remote_cells_with_tracers->vr_wr[index_in_remote * numTracers + k] +=
617 currentVals[numTracers * ( tgtIndex - 1 ) + k] * areap;
622 int arrTgtIndex =
rs2.
index( tgtArr );
623 if( -1 == arrTgtIndex )
MB_CHK_SET_ERR( MB_FAILURE,
"can't find the target arrival index" );
624 for(
int k = 0; k < numTracers; k++ )
625 newValues[numTracers * arrTgtIndex + k] += currentVals[( tgtIndex - 1 ) * numTracers + k] * areap;
629 MB_CHK_SET_ERR( rval,
"can't get arrival target for corresponding " );
634 if( remote_cells_with_tracers )
638 ( parcomm->proc_config().crystal_router() )->gs_transfer( 1, *remote_cells_with_tracers, 0 );
642 int n = remote_cells_with_tracers->get_n();
643 for(
int j = 0; j < n; j++ )
645 EntityHandle tgtCell = remote_cells_with_tracers->vul_rd[j];
646 int arrTgtIndex =
rs2.
index( tgtCell );
647 if( -1 == arrTgtIndex )
MB_CHK_SET_ERR( MB_FAILURE,
"can't find the target arrival index" );
648 for(
int k = 0; k < numTracers; k++ )
649 newValues[arrTgtIndex * numTracers + k] += remote_cells_with_tracers->vr_rd[j * numTracers + k];
658 std::vector< double > total_mass_local( numTracers, 0. );
659 while( iter !=
rs2.
end() )
662 double* ptrArea = (
double*)data;
663 for(
int i = 0; i < count; i++, ++iter, j++, ptrArea++ )
665 for(
int k = 0; k < numTracers; k++ )
667 total_mass_local[k] += newValues[j * numTracers + k];
668 newValues[j * numTracers + k] /= ( *ptrArea );
675 std::vector< double > total_mass( numTracers, 0. );
676 double total_intx_area = 0;
678 MPI_Reduce( &total_mass_local[0], &total_mass[0], numTracers, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD );
679 if( MPI_SUCCESS != mpi_err )
return MB_FAILURE;
681 mpi_err = MPI_Reduce( &check_intx_area, &total_intx_area, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD );
682 if( MPI_SUCCESS != mpi_err )
return MB_FAILURE;
685 for(
int k = 0; k < numTracers; k++ )
686 std::cout <<
"total mass now tracer k=" << k + 1 <<
" " << total_mass[k] <<
"\n";
687 std::cout <<
"check: total intersection area: (4 * M_PI * R^2): " << 4 * M_PI *
Rsrc *
Rsrc <<
" "
688 << total_intx_area <<
"\n";
691 if( remote_cells_with_tracers )
693 delete remote_cells_with_tracers;
694 remote_cells_with_tracers = NULL;
697 for(
int k = 0; k < numTracers; k++ )
698 std::cout <<
"total mass now tracer k=" << k + 1 <<
" " << total_mass_local[k] <<
"\n";
699 std::cout <<
"check: total intersection area: (4 * M_PI * R^2): " << 4 * M_PI *
Rsrc *
Rsrc <<
" "
700 << check_intx_area <<
"\n";
710 return Intx2Mesh::build_processor_euler_boxes( euler_set, local_verts, gnomonic );
716 int num_local_verts = (int)local_verts.
size();
718 assert( parcomm != NULL );
720 if( num_local_verts == 0 )
724 num_local_verts = (int)local_verts.
size();
730 for(
int i = 0; i < 6; i++ )
732 gnom_box[4 * i] = gnom_box[4 * i + 1] = DBL_MAX;
733 gnom_box[4 * i + 2] = gnom_box[4 * i + 3] = -DBL_MAX;
739 std::vector< double > coords( 3 * num_local_verts );
743 std::vector< int > gnplane;
744 gnplane.resize( num_local_verts );
745 for(
int i = 0; i < num_local_verts; i++ )
747 CartVect pos( &coords[3 * i] );
772 std::vector< double > elco( 3 * nnodes );
773 std::set< int > planes;
774 for(
int i = 0; i < nnodes; i++ )
776 int ix = local_verts.
index( conn[i] );
777 planes.insert( gnplane[ix] );
778 for(
int j = 0; j < 3; j++ )
780 elco[3 * i + j] = coords[3 * ix + j];
784 for( std::set< int >::iterator st = planes.begin(); st != planes.end(); st++ )
787 for(
int i = 0; i < nnodes; i++ )
789 CartVect pos( &elco[3 * i] );
794 for(
int k = 0; k < 2; k++ )
797 if( val < gnom_box[4 * ( pl - 1 ) + k] ) gnom_box[4 * ( pl - 1 ) + k] = val;
798 if( val > gnom_box[4 * ( pl - 1 ) + 2 + k] )
799 gnom_box[4 * ( pl - 1 ) + 2 + k] = val;
805 int numprocs = parcomm->proc_config().proc_size();
808 my_rank = parcomm->proc_config().proc_rank();
809 for(
int k = 0; k < 24; k++ )
814 #if( MPI_VERSION >= 2 )
816 mpi_err = MPI_Allgather( MPI_IN_PLACE, 0, MPI_DATATYPE_NULL, &
allBoxes[0], 24, MPI_DOUBLE,
817 parcomm->proc_config().proc_comm() );
820 std::vector< double > allBoxes_tmp( 24 * parcomm->proc_config().proc_size() );
821 mpi_err = MPI_Allgather( &
allBoxes[24 *
my_rank], 6, MPI_DOUBLE, &allBoxes_tmp[0], 24, MPI_DOUBLE,
822 parcomm->proc_config().proc_comm() );
826 if( MPI_SUCCESS != mpi_err )
return MB_FAILURE;
831 std::cout <<
" maximum number of vertices per cell are " <<
max_edges_1 <<
" on first mesh and " <<
max_edges_2
832 <<
" on second mesh \n";
833 for(
int i = 0; i < numprocs; i++ )
835 std::cout <<
"task: " << i <<
" \n";
836 for(
int pl = 1; pl <= 6; pl++ )
838 std::cout <<
" plane " << pl <<
" min: \t" <<
allBoxes[24 * i + 4 * ( pl - 1 )] <<
" \t"
839 <<
allBoxes[24 * i + 4 * ( pl - 1 ) + 1] <<
"\n";
840 std::cout <<
" \t max: \t" <<
allBoxes[24 * i + 4 * ( pl - 1 ) + 2] <<
" \t"
841 <<
allBoxes[24 * i + 4 * ( pl - 1 ) + 3] <<
"\n";
865 assert( parcomm != NULL );
869 bool extraWork = (order >= 2);
870 if( 1 == parcomm->proc_config().proc_size() )
888 &defaultInt );
MB_CHK_SET_ERR( rval,
"can't create sending processor tag" );
894 int orig_sender = -1;
899 int size_gdofs_tag = 0;
900 std::vector< int > valsDOFs;
904 if( meshCells.size() > 0 )
906 oneCell = meshCells[0];
919 valsDOFs.resize( lenTag );
925 size_gdofs_tag = lenTag;
938 int local_int_array[2], global_int_array[2];
939 local_int_array[0] = orig_sender;
940 local_int_array[1] = size_gdofs_tag;
943 MPI_Allreduce( local_int_array, global_int_array, 2, MPI_INT, MPI_MAX, parcomm->proc_config().proc_comm() );
944 if( MPI_SUCCESS != mpi_err )
return MB_FAILURE;
945 orig_sender = global_int_array[0];
946 size_gdofs_tag = global_int_array[1];
948 std::cout <<
"proc: " <<
my_rank <<
" size_gdofs_tag:" << size_gdofs_tag <<
"\n";
950 valsDOFs.resize( size_gdofs_tag );
954 int migrated_mesh = 0;
955 if( orig_sender != -1 ) migrated_mesh = 1;
965 size_t num_mesh_verts = mesh_verts.size();
968 std::vector< double > coords_mesh( 3 * num_mesh_verts );
972 std::vector< int > gnplane;
975 gnplane.resize( num_mesh_verts );
976 for(
size_t i = 0; i < num_mesh_verts; i++ )
978 CartVect pos( &coords_mesh[3 * i] );
985 std::vector< int > gids( num_mesh_verts );
990 std::map< int, Range > Rto;
991 int numprocs = parcomm->proc_config().proc_size();
993 for(
Range::iterator eit = meshCells.begin(); eit != meshCells.end(); ++eit )
1001 std::set< int > planes;
1002 std::vector< double > elco( 3 * num_nodes );
1003 for(
int i = 0; i < num_nodes; i++ )
1006 int index = mesh_verts.index( v );
1007 if( gnomonic ) planes.insert( gnplane[index] );
1008 for(
int j = 0; j < 3; j++ )
1010 elco[3 * i + j] = coords_mesh[3 * index + j];
1016 for( std::set< int >::iterator st = planes.begin(); st != planes.end(); st++ )
1019 double qmin[2] = { DBL_MAX, DBL_MAX };
1020 double qmax[2] = { -DBL_MAX, -DBL_MAX };
1021 for(
int i = 0; i < num_nodes; i++ )
1023 CartVect dp( &elco[3 * i] );
1028 for(
int j = 0; j < 2; j++ )
1030 if( qmin[j] > c2[j] ) qmin[j] = c2[j];
1031 if( qmax[j] < c2[j] ) qmax[j] = c2[j];
1036 for(
int p = 0; p < numprocs; p++ )
1038 double procMin1 =
allBoxes[24 * p + 4 * ( pl - 1 )];
1040 if( procMin1 >= DBL_MAX )
1042 double procMin2 =
allBoxes[24 * p + 4 * ( pl - 1 ) + 1];
1043 double procMax1 =
allBoxes[24 * p + 4 * ( pl - 1 ) + 2];
1044 double procMax2 =
allBoxes[24 * p + 4 * ( pl - 1 ) + 3];
1055 for(
int p = 0; p < numprocs; p++ )
1058 bool insert =
false;
1059 for(
int i = 0; i < num_nodes; i++ )
1061 if( box.contains_point( &elco[3 * i],
box_error ) )
1067 if( insert ) Rto[p].insert( q );
1078 for (
int p = 0; p < numprocs; p++ )
1080 Range originalSend = Rto[p];
1083 if( originalSend.empty() )
continue;
1085 rval = MeshTopoUtil(
mb ).get_bridge_adjacencies( originalSend, 0, 2, extraCells, order - 1 );
MB_CHK_SET_ERR( rval,
"Failed to get bridge adjacencies" );
1088 extraCells =
intersect(extraCells, meshCells);
1089 Rto[p].merge(extraCells);
1103 for(
int p = 0; p < numprocs; p++ )
1105 if( p == (
int)
my_rank )
continue;
1106 Range& range_to_P = Rto[p];
1108 if( range_to_P.empty() )
continue;
1110 std::cout <<
" proc : " <<
my_rank <<
" to proc " << p <<
" send " << range_to_P.size() <<
" cells "
1111 <<
" psize: " << range_to_P.psize() <<
"\n";
1115 numq = numq + range_to_P.size();
1116 numv = numv + vertsToP.size();
1117 range_to_P.merge( vertsToP );
1122 TLv.initialize( 2, 0, 0, 3, numv );
1123 TLv.enableWriteAccess();
1127 2 +
max_edges_1 + migrated_mesh + size_gdofs_tag + ghost_info;
1128 TLq.initialize( sizeTuple, 0, 0, 0,
1136 TLq.enableWriteAccess();
1138 std::cout <<
"from proc " <<
my_rank <<
" send " << numv <<
" vertices and " << numq <<
" elements\n";
1141 for(
int to_proc = 0; to_proc < numprocs; to_proc++ )
1143 if( to_proc == (
int)
my_rank )
continue;
1144 Range& range_to_P = Rto[to_proc];
1145 Range V = range_to_P.subset_by_type(
MBVERTEX );
1150 int index = mesh_verts.index( v );
1151 assert( -1 != index );
1152 int n = TLv.get_n();
1153 TLv.vi_wr[2 * n] = to_proc;
1154 TLv.vi_wr[2 * n + 1] = gids[index];
1155 TLv.vr_wr[3 * n] = coords_mesh[3 * index];
1156 TLv.vr_wr[3 * n + 1] = coords_mesh[3 * index + 1];
1157 TLv.vr_wr[3 * n + 2] = coords_mesh[3 * index + 2];
1161 Range Q = range_to_P.subset_by_dimension( 2 );
1167 int n = TLq.get_n();
1168 TLq.vi_wr[sizeTuple * n] = to_proc;
1169 TLq.vi_wr[sizeTuple * n + 1] =
1178 for(
int i = 0; i < num_nodes; i++ )
1181 int index = mesh_verts.index( v );
1182 assert( -1 != index );
1183 TLq.vi_wr[sizeTuple * n + 2 + i] = gids[index];
1187 TLq.vi_wr[sizeTuple * n + 2 + k] =
1196 TLq.vi_wr[sizeTuple * n + currentIndexIntTuple] = orig_sender;
1197 currentIndexIntTuple++;
1204 rval = parcomm->get_owner(q, owner);
MB_CHK_SET_ERR( rval,
"can't get owner for cell" );
1205 TLq.vi_wr[sizeTuple * n + currentIndexIntTuple] = owner;
1206 currentIndexIntTuple++;
1209 if( size_gdofs_tag )
1212 for(
int i = 0; i < size_gdofs_tag; i++ )
1214 TLq.vi_wr[sizeTuple * n + currentIndexIntTuple + i] =
1225 ( parcomm->proc_config().crystal_router() )->gs_transfer( 1, TLv, 0 );
1226 ( parcomm->proc_config().crystal_router() )->gs_transfer( 1, TLq, 0 );
1232 std::map< int, EntityHandle > globalID_to_vertex_handle;
1238 for(
Range::iterator vit = mesh_verts.begin(); vit != mesh_verts.end(); ++vit, k++ )
1240 globalID_to_vertex_handle[gids[k]] = *vit;
1243 globalID_to_eh.clear();
1246 int n = TLv.get_n();
1247 for(
int i = 0; i < n; i++ )
1249 int globalId = TLv.vi_rd[2 * i + 1];
1250 if( globalID_to_vertex_handle.find( globalId ) ==
1251 globalID_to_vertex_handle.end() )
1255 double dp_pos[3] = { TLv.vr_wr[3 * i], TLv.vr_wr[3 * i + 1], TLv.vr_wr[3 * i + 2] };
1257 globalID_to_vertex_handle[globalId] = new_vert;
1268 Range local_q = local.subset_by_dimension( 2 );
1270 for(
Range::iterator it = local_q.begin(); it != local_q.end(); ++it )
1275 assert( gid_el >= 0 );
1276 globalID_to_eh[gid_el] = q;
1281 rval = parcomm->get_owner(q, owner);
MB_CHK_SET_ERR( rval,
"can't get owner for cell" );
1295 for(
int i = 0; i < n; i++ )
1297 int globalIdEl = TLq.vi_rd[sizeTuple * i + 1];
1302 if (globalID_to_eh.find(globalIdEl) != globalID_to_eh.end())
1309 int vgid = TLq.vi_rd[sizeTuple * i + 2 + j];
1315 assert( globalID_to_vertex_handle.find( vgid ) != globalID_to_vertex_handle.end() );
1316 new_conn[j] = globalID_to_vertex_handle[vgid];
1322 EntityType entType =
MBQUAD;
1324 if( nnodes < 4 ) entType =
MBTRI;
1327 globalID_to_eh[globalIdEl] = new_element;
1328 local_q.insert( new_element );
1333 orig_sender = TLq.vi_wr[sizeTuple * i + currentIndexIntTuple];
1335 currentIndexIntTuple++;
1338 int from_proc = TLq.vi_rd[sizeTuple * i];
1342 from_proc = TLq.vi_wr[sizeTuple * i + currentIndexIntTuple];
1343 currentIndexIntTuple++;
1348 if( size_gdofs_tag )
1350 for(
int j = 0; j < size_gdofs_tag; j++ )
1352 valsDOFs[j] = TLq.vi_wr[sizeTuple * i + currentIndexIntTuple + j];
1354 rval =
mb->
tag_set_data( gdsTag, &new_element, 1, &valsDOFs[0] );
MB_CHK_SET_ERR( rval,
"can't set GLOBAL_DOFS data on coverage mesh" );
1362 std::cout <<
" proc " <<
my_rank <<
" add " << local_q.size() <<
" cells to covering set \n";
1369 #undef CHECK_CONVEXITY