691 assert(ELEMENT_DIM == 1);
696 EXCEPTION(
"There aren't enough nodes to make parallelisation worthwhile");
700 if (mPartitioning == DistributedTetrahedralMeshPartitionType::GEOMETRIC)
704 EXCEPTION(
"Space region not set for GEOMETRIC partition of DistributedTetrahedralMesh");
722 this->ConstructFromMeshReader(mesh_reader);
727 mPartitioning=DistributedTetrahedralMeshPartitionType::DUMB;
728 mTotalNumNodes=width+1;
729 mTotalNumBoundaryElements=2u;
730 mTotalNumElements=width;
733 assert(!this->mpDistributedVectorFactory);
735 if (this->mpDistributedVectorFactory->GetLocalOwnership() == 0)
746 bool am_top_most = (this->mpDistributedVectorFactory->GetHigh() == mTotalNumNodes);
748 unsigned lo_node=this->mpDistributedVectorFactory->GetLow();
749 unsigned hi_node=this->mpDistributedVectorFactory->GetHigh();
761 for (
unsigned node_index=lo_node; node_index<hi_node; node_index++)
765 if (node_index<this->mpDistributedVectorFactory->GetLow() ||
766 node_index==this->mpDistributedVectorFactory->GetHigh() )
769 RegisterHaloNode(node_index);
770 mHaloNodes.push_back(p_node);
774 RegisterNode(node_index);
775 this->mNodes.push_back(p_node);
781 this->mBoundaryNodes.push_back(p_node);
782 RegisterBoundaryElement(0);
785 if (node_index==width)
787 this->mBoundaryNodes.push_back(p_node);
788 RegisterBoundaryElement(1);
792 if (node_index>lo_node)
794 std::vector<Node<SPACE_DIM>*> nodes;
795 nodes.push_back(p_old_node);
796 nodes.push_back(p_node);
797 RegisterElement(node_index-1);
809 assert(SPACE_DIM == 2);
810 assert(ELEMENT_DIM == 2);
814 EXCEPTION(
"There aren't enough nodes to make parallelisation worthwhile");
818 if (mPartitioning == DistributedTetrahedralMeshPartitionType::GEOMETRIC)
822 EXCEPTION(
"Space region not set for GEOMETRIC partition of DistributedTetrahedralMesh");
840 this->ConstructFromMeshReader(mesh_reader);
845 mPartitioning=DistributedTetrahedralMeshPartitionType::DUMB;
847 mTotalNumNodes=(width+1)*(height+1);
848 mTotalNumBoundaryElements=(width+height)*2;
849 mTotalNumElements=width*height*2;
853 unsigned lo_y = y_partition.
GetLow();
854 unsigned hi_y = y_partition.
GetHigh();
856 assert(!this->mpDistributedVectorFactory);
858 if (this->mpDistributedVectorFactory->GetLocalOwnership() == 0)
869 bool am_top_most = (this->mpDistributedVectorFactory->GetHigh() == mTotalNumNodes);
884 for (
unsigned j=lo_y; j<hi_y; j++)
886 for (
unsigned i=0; i<width+1; i++)
888 bool is_boundary=
false;
889 if (i==0 || j==0 || i==width || j==height)
893 unsigned global_node_index=((width+1)*(j) + i);
898 RegisterHaloNode(global_node_index);
899 mHaloNodes.push_back(p_node);
903 RegisterNode(global_node_index);
904 this->mNodes.push_back(p_node);
908 this->mBoundaryNodes.push_back(p_node);
914 unsigned belem_index;
918 for (
unsigned i=0; i<width; i++)
920 std::vector<Node<SPACE_DIM>*> nodes;
921 nodes.push_back(GetNodeOrHaloNode( height*(width+1)+i+1 ));
922 nodes.push_back(GetNodeOrHaloNode( height*(width+1)+i ));
924 RegisterBoundaryElement(belem_index);
930 for (
unsigned j=lo_y+1; j<hi_y; j++)
932 std::vector<Node<SPACE_DIM>*> nodes;
933 nodes.push_back(GetNodeOrHaloNode( (width+1)*j-1 ));
934 nodes.push_back(GetNodeOrHaloNode( (width+1)*(j+1)-1 ));
935 belem_index=width+j-1;
936 RegisterBoundaryElement(belem_index);
943 for (
unsigned i=0; i<width; i++)
945 std::vector<Node<SPACE_DIM>*> nodes;
946 nodes.push_back(GetNodeOrHaloNode( i ));
947 nodes.push_back(GetNodeOrHaloNode( i+1 ));
948 belem_index=width+height+i;
949 RegisterBoundaryElement(belem_index);
955 for (
unsigned j=lo_y; j<hi_y-1; j++)
957 std::vector<Node<SPACE_DIM>*> nodes;
958 nodes.push_back(GetNodeOrHaloNode( (width+1)*(j+1) ));
959 nodes.push_back(GetNodeOrHaloNode( (width+1)*(j) ));
960 belem_index=2*width+height+j;
961 RegisterBoundaryElement(belem_index);
968 for (
unsigned j=lo_y; j<hi_y-1; j++)
970 for (
unsigned i=0; i<width; i++)
972 unsigned parity=(i+(height-j))%2;
973 unsigned nw=(j+1)*(width+1)+i;
974 unsigned sw=(j)*(width+1)+i;
975 std::vector<Node<SPACE_DIM>*> upper_nodes;
976 upper_nodes.push_back(GetNodeOrHaloNode( nw ));
977 upper_nodes.push_back(GetNodeOrHaloNode( nw+1 ));
978 if (stagger==
false || parity == 1)
980 upper_nodes.push_back(GetNodeOrHaloNode( sw+1 ));
984 upper_nodes.push_back(GetNodeOrHaloNode( sw ));
986 elem_index=2*(j*width+i);
987 RegisterElement(elem_index);
989 std::vector<Node<SPACE_DIM>*> lower_nodes;
990 lower_nodes.push_back(GetNodeOrHaloNode( sw+1 ));
991 lower_nodes.push_back(GetNodeOrHaloNode( sw ));
992 if (stagger==
false ||parity == 1)
994 lower_nodes.push_back(GetNodeOrHaloNode( nw ));
998 lower_nodes.push_back(GetNodeOrHaloNode( nw+1 ));
1001 RegisterElement(elem_index);
1014 assert(SPACE_DIM == 3);
1015 assert(ELEMENT_DIM == 3);
1019 EXCEPTION(
"There aren't enough nodes to make parallelisation worthwhile");
1023 if (mPartitioning == DistributedTetrahedralMeshPartitionType::GEOMETRIC)
1027 EXCEPTION(
"Space region not set for GEOMETRIC partition of DistributedTetrahedralMesh");
1045 this->ConstructFromMeshReader(mesh_reader);
1050 mPartitioning=DistributedTetrahedralMeshPartitionType::DUMB;
1052 mTotalNumNodes=(width+1)*(height+1)*(depth+1);
1053 mTotalNumBoundaryElements=((width*height)+(width*depth)+(height*depth))*4;
1054 mTotalNumElements=width*height*depth*6;
1058 unsigned lo_z = z_partition.
GetLow();
1059 unsigned hi_z = z_partition.
GetHigh();
1062 assert(!this->mpDistributedVectorFactory);
1064 if (this->mpDistributedVectorFactory->GetLocalOwnership() == 0)
1075 bool am_top_most = (this->mpDistributedVectorFactory->GetHigh() == mTotalNumNodes);
1089 unsigned global_node_index;
1090 for (
unsigned k=lo_z; k<hi_z; k++)
1092 for (
unsigned j=0; j<height+1; j++)
1094 for (
unsigned i=0; i<width+1; i++)
1096 bool is_boundary =
false;
1097 if (i==0 || j==0 || k==0 || i==width || j==height || k==depth)
1101 global_node_index = (k*(height+1)+j)*(width+1)+i;
1108 RegisterHaloNode(global_node_index);
1109 mHaloNodes.push_back(p_node);
1113 RegisterNode(global_node_index);
1114 this->mNodes.push_back(p_node);
1119 this->mBoundaryNodes.push_back(p_node);
1127 unsigned element_nodes[6][4] = {{0, 1, 5, 7}, {0, 1, 3, 7},
1128 {0, 2, 3, 7}, {0, 2, 6, 7},
1129 {0, 4, 6, 7}, {0, 4, 5, 7}};
1130 std::vector<Node<SPACE_DIM>*> tetrahedra_nodes;
1132 for (
unsigned k=lo_z; k<hi_z-1; k++)
1134 unsigned belem_index = 0;
1138 belem_index = 2*(height*width+k*2*(height+width));
1141 for (
unsigned j=0; j<height; j++)
1143 for (
unsigned i=0; i<width; i++)
1146 unsigned global_node_indices[8];
1147 unsigned local_node_index = 0;
1149 for (
unsigned z = 0; z < 2; z++)
1151 for (
unsigned y = 0; y < 2; y++)
1153 for (
unsigned x = 0; x < 2; x++)
1155 global_node_indices[local_node_index] = i+x+(width+1)*(j+y+(height+1)*(k+z));
1162 for (
unsigned m = 0; m < 6; m++)
1166 tetrahedra_nodes.clear();
1168 for (
unsigned n = 0; n < 4; n++)
1170 tetrahedra_nodes.push_back(GetNodeOrHaloNode( global_node_indices[element_nodes[m][n]] ));
1172 unsigned elem_index = 6 * ((k*height+j)*width+i)+m;
1173 RegisterElement(elem_index);
1178 std::vector<Node<SPACE_DIM>*> triangle_nodes;
1182 triangle_nodes.clear();
1183 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[0] ));
1184 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[2] ));
1185 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[6] ));
1186 RegisterBoundaryElement(belem_index);
1188 triangle_nodes.clear();
1189 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[0] ));
1190 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[6] ));
1191 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[4] ));
1192 RegisterBoundaryElement(belem_index);
1197 triangle_nodes.clear();
1198 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[1] ));
1199 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[5] ));
1200 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[7] ));
1201 RegisterBoundaryElement(belem_index);
1203 triangle_nodes.clear();
1204 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[1] ));
1205 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[7] ));
1206 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[3] ));
1207 RegisterBoundaryElement(belem_index);
1212 triangle_nodes.clear();
1213 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[0] ));
1214 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[5] ));
1215 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[1] ));
1216 RegisterBoundaryElement(belem_index);
1218 triangle_nodes.clear();
1219 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[0] ));
1220 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[4] ));
1221 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[5] ));
1222 RegisterBoundaryElement(belem_index);
1227 triangle_nodes.clear();
1228 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[2] ));
1229 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[3] ));
1230 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[7] ));
1231 RegisterBoundaryElement(belem_index);
1233 triangle_nodes.clear();
1234 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[2] ));
1235 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[7] ));
1236 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[6] ));
1237 RegisterBoundaryElement(belem_index);
1242 triangle_nodes.clear();
1243 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[0] ));
1244 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[3] ));
1245 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[2] ));
1246 RegisterBoundaryElement(belem_index);
1248 triangle_nodes.clear();
1249 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[0] ));
1250 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[1] ));
1251 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[3] ));
1252 RegisterBoundaryElement(belem_index);
1257 triangle_nodes.clear();
1258 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[4] ));
1259 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[7] ));
1260 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[5] ));
1261 RegisterBoundaryElement(belem_index);
1263 triangle_nodes.clear();
1264 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[4] ));
1265 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[6] ));
1266 triangle_nodes.push_back(GetNodeOrHaloNode( global_node_indices[7] ));
1267 RegisterBoundaryElement(belem_index);
1302 std::set<unsigned>& rElementsOwned,
1303 std::set<unsigned>& rNodesOwned,
1304 std::set<unsigned>& rHaloNodesOwned,
1305 std::vector<unsigned>& rProcessorsOffset)
1308 assert(ELEMENT_DIM==2 || ELEMENT_DIM==3);
1311 const unsigned num_nodes = rMeshReader.
GetNumNodes();
1318 boost::scoped_array<idx_t> element_distribution(
new idx_t[num_procs+1]);
1319 boost::scoped_array<int> element_counts(
new int[num_procs]);
1321 element_distribution[0] = 0;
1323 for (
unsigned proc_index=1; proc_index<num_procs; proc_index++)
1325 element_distribution[proc_index] = element_distribution[proc_index-1] + num_elements/num_procs;
1326 element_counts[proc_index-1] = element_distribution[proc_index] - element_distribution[proc_index-1];
1329 element_distribution[num_procs] = num_elements;
1330 element_counts[num_procs-1] = element_distribution[num_procs] - element_distribution[num_procs-1];
1335 idx_t first_local_element = element_distribution[local_proc_index];
1336 idx_t last_plus_one_element = element_distribution[local_proc_index+1];
1337 idx_t num_local_elements = last_plus_one_element - first_local_element;
1339 boost::scoped_array<idx_t> eind(
new idx_t[num_local_elements*(ELEMENT_DIM+1)]);
1340 boost::scoped_array<idx_t> eptr(
new idx_t[num_local_elements+1]);
1350 for (idx_t element_index = 0; element_index < first_local_element; element_index++)
1356#ifdef CHASTE_HOMEMADE_MESH_TO_DUAL
1359 Mat element_node_matrix;
1360 PetscTools::SetupMat(element_node_matrix, num_elements, num_nodes, ELEMENT_DIM+1, num_local_elements);
1363 unsigned counter = 0;
1364 for (idx_t element_index = 0; element_index < num_local_elements; element_index++)
1370 eptr[element_index] = counter;
1371 for (
unsigned i=0; i<ELEMENT_DIM+1; i++)
1373#ifdef CHASTE_HOMEMADE_MESH_TO_DUAL
1380 eptr[num_local_elements] = counter;
1382 rMeshReader.
Reset();
1385 MPI_Comm communicator = PETSC_COMM_WORLD;
1391#ifdef CHASTE_HOMEMADE_MESH_TO_DUAL
1393 std::vector<idx_t> my_xadj;
1394 std::vector<idx_t> my_adjncy;
1407 Mat node_element_matrix;
1408 MatTranspose(element_node_matrix, MAT_INITIAL_MATRIX, &node_element_matrix);
1409 Mat element_element_matrix;
1410 MatMatMult(element_node_matrix, node_element_matrix, MAT_INITIAL_MATRIX, PETSC_DETERMINE, &element_element_matrix);
1411 my_xadj.push_back(0);
1414 const PetscScalar *vals;
1415 for (
PetscInt el_index=first_local_element; el_index<last_plus_one_element; el_index++)
1417 MatGetRow(element_element_matrix, el_index, &ncols, &cols, &vals);
1420 if (std::lround(vals[i])==ELEMENT_DIM)
1423 my_adjncy.push_back(cols[i]);
1426 MatRestoreRow(element_element_matrix, el_index, &ncols, &cols, NULL);
1428 my_xadj.push_back(my_adjncy.size());
1430 MatDestroy(&element_node_matrix);
1431 MatDestroy(&node_element_matrix);
1432 MatDestroy(&element_element_matrix);
1434 adjncy = &my_adjncy[0];
1441 idx_t ncommonnodes = 3;
1442 if (ELEMENT_DIM == 2)
1446 ParMETIS_V3_Mesh2Dual(element_distribution.get(), eptr.get(), eind.get(),
1447 &numflag, &ncommonnodes, &xadj, &adjncy, &communicator);
1454 idx_t weight_flag = 0;
1455 idx_t n_constraints = 1;
1460 boost::scoped_array<real_t> tpwgts(
new real_t[n_subdomains]);
1461 real_t ubvec_value = (real_t)1.05;
1464 tpwgts[proc] = ((real_t)1.0)/n_subdomains;
1466 boost::scoped_array<idx_t> local_partition(
new idx_t[num_local_elements]);
1482 ParMETIS_V3_PartKway(element_distribution.get(), xadj, adjncy,
nullptr,
nullptr, &weight_flag, &numflag,
1483 &n_constraints, &n_subdomains, tpwgts.get(), &ubvec_value,
1484 options, &edgecut, local_partition.get(), &communicator);
1488 boost::scoped_array<idx_t> global_element_partition(
new idx_t[num_elements]);
1491 MPI_Datatype mpi_idx_t = MPI_LONG_LONG_INT;
1492 if (
sizeof(idx_t) ==
sizeof(
int))
1494 mpi_idx_t = MPI_INT;
1496 boost::scoped_array<int> int_element_distribution(
new int[num_procs+1]);
1497 for (
unsigned i=0; i<num_procs+1; ++i)
1499 int_element_distribution[i] = element_distribution[i];
1501 MPI_Allgatherv(local_partition.get(), num_local_elements, mpi_idx_t,
1502 global_element_partition.get(), element_counts.get(), int_element_distribution.get(), mpi_idx_t, PETSC_COMM_WORLD);
1504 local_partition.reset();
1506 for (
unsigned elem_index=0; elem_index<num_elements; elem_index++)
1508 if ((
unsigned) global_element_partition[elem_index] == local_proc_index)
1510 rElementsOwned.insert(elem_index);
1514 rMeshReader.
Reset();
1515#ifdef CHASTE_HOMEMADE_MESH_TO_DUAL
1525 std::vector<unsigned> global_node_partition(num_nodes, UNASSIGNED_NODE);
1527 assert(rProcessorsOffset.size() == 0);
1553 for (
unsigned element_number = 0; element_number < mTotalNumElements; element_number++)
1555 unsigned element_owner = global_element_partition[element_number];
1561 for (std::vector<unsigned>::const_iterator node_it = element_data.
NodeIndices.begin();
1569 if (global_node_partition[*node_it] == UNASSIGNED_NODE)
1571 if (element_owner == local_proc_index)
1573 rNodesOwned.insert(*node_it);
1576 global_node_partition[*node_it] = element_owner;
1583 rProcessorsOffset[proc]++;
1588 if (element_owner == local_proc_index)
1591 if (global_node_partition[*node_it] != local_proc_index)
1593 rHaloNodesOwned.insert(*node_it);
1613 rElementsOwned.clear();
1614 rMeshReader.
Reset();
1615 for (
unsigned element_number = 0; element_number < mTotalNumElements; element_number++)
1619 bool element_owned =
false;
1620 std::set<unsigned> temp_halo_nodes;
1622 for (std::vector<unsigned>::const_iterator node_it = element_data.
NodeIndices.begin();
1626 if (rNodesOwned.find(*node_it) != rNodesOwned.end())
1628 element_owned =
true;
1629 rElementsOwned.insert(element_number);
1633 temp_halo_nodes.insert(*node_it);
1639 rHaloNodesOwned.insert(temp_halo_nodes.begin(), temp_halo_nodes.end());
1643 rMeshReader.
Reset();
1650 this->mNodePermutation.resize(this->GetNumNodes());
1652 for (
unsigned node_index=0; node_index<this->GetNumNodes(); node_index++)
1654 unsigned partition = global_node_partition[node_index];
1655 assert(partition != UNASSIGNED_NODE);
1657 this->mNodePermutation[node_index] = rProcessorsOffset[partition] + local_index[partition];
1659 local_index[partition]++;