#include "MeshPartition.hpp" double GetUniformMeshElementSize(Mesh * mesh) { int dim = mesh->Dimension(); int nrelem = mesh->GetNE(); DenseMatrix J(dim); double hmin, hmax; hmin = infinity(); hmax = -infinity(); Vector attr(nrelem); for (int iel=0; ielGetElementBaseGeometry(iel); ElementTransformation *T = mesh->GetElementTransformation(iel); T->SetIntPoint(&Geometries.GetCenter(geom)); Geometries.JacToPerfJac(geom, T->Jacobian(), J); attr(iel) = J.Det(); attr(iel) = pow(abs(attr(iel)), 1.0/double(dim)); hmin = min(hmin, attr(iel)); hmax = max(hmax, attr(iel)); } // cout << "hmin , hmax = " << hmin << ", " << hmax << endl; if (nrelem) { MFEM_VERIFY(abs(hmin-hmax) < 1e-12, "Case not supported yet") } else { hmin = hmax = 0.0; } // cout << hmax << endl; #ifdef MFEM_USE_MPI ParMesh * pmesh = dynamic_cast(mesh); if (pmesh) MPI_Allreduce(MPI_IN_PLACE, &hmax,1,MPI_DOUBLE,MPI_MAX,pmesh->GetComm()); #endif return hmax; } Mesh * ExtendMesh(Mesh * mesh, const Array & directions) { // extrute on one dimension // flag = 1 +x, -1 -x, 2 +y, -2 +y , 3 +z, -3, -z // copy the original mesh; Mesh * mesh_orig = new Mesh(*mesh); if (!directions.Size()) return mesh_orig; int dim = mesh_orig->Dimension(); Mesh * mesh_ext=nullptr; for (int j=0; jGetNV() << endl; // cout << "nrvertices : " << nrvertices << endl; // cout << "nrelements : " << nrelements << endl; mesh_ext = new Mesh(dim, nrvertices, nrelements); // Add existing vertices Array vmap(mesh_orig->GetNV()); vmap = 0; k = mesh_orig->GetNV(); for (int i=0; iGetNV(); ++i) { double * vert = mesh_orig->GetVertex(i); mesh_ext->AddVertex(vert); switch (abs(d)) { case 1: if (vert[0] == val) { vmap[i] = k; k++; } break; case 2: if (vert[1] == val) { vmap[i] = k; k++; } break; case 3: if (vert[2] == val) { vmap[i] = k; k++; } break; } } // Add existing elements for (int i=0; iGetNE(); ++i) { Arrayind; mesh_orig->GetElementVertices(i,ind); if (dim == 2) { mesh_ext->AddQuad(ind); } else if (dim == 3) { mesh_ext->AddHex(ind); } } // Add new vertices for (int i=0; iGetNV(); ++i) { double * vert = mesh_orig->GetVertex(i); switch (abs(d)) { case 1: if (vert[0] == val) { double coords[dim]; coords[0] = vert[0] + h; coords[1] = vert[1]; if (dim == 3) coords[2] = vert[2]; mesh_ext->AddVertex(coords); } break; case 2: if (vert[1] == val) { double coords[dim]; coords[0] = vert[0]; coords[1] = vert[1] + h; if (dim == 3) coords[2] = vert[2]; mesh_ext->AddVertex(coords); } break; case 3: if (vert[2] == val) { double coords[dim]; coords[0] = vert[0]; coords[1] = vert[1]; coords[2] = vert[2] + h; mesh_ext->AddVertex(coords); } break; } } // loop through boundary elements and extend in the given direction for (int i=0; iGetNBE(); ++i) { Array vertices; mesh_orig->GetBdrElementVertices(i,vertices); if (dim == 2) { int ind[4]; if (vmap[vertices[0]] && vmap[vertices[1]]) { ind[0] = vertices[1]; ind[1] = vertices[0]; ind[2] = vmap[vertices[0]]; ind[3] = vmap[vertices[1]]; mesh_ext->AddQuad(ind); } } else if (dim == 3) { int ind[8]; if (vmap[vertices[0]] && vmap[vertices[1]] && vmap[vertices[2]] && vmap[vertices[3]]) { ind[0] = vertices[0]; ind[1] = vertices[1]; ind[2] = vertices[2]; ind[3] = vertices[3]; ind[4] = vmap[vertices[0]]; ind[5] = vmap[vertices[1]]; ind[6] = vmap[vertices[2]]; ind[7] = vmap[vertices[3]]; mesh_ext->AddHex(ind); } } } mesh_ext->FinalizeTopology(); if (jDimension(); int n = pow(mesh->GetNE(), 1.0/(double)dim); if (nx > n) { nx = n; MFEM_WARNING("Changed partition in the x direction to nx = " << n << endl); } if (ny > n) { ny = n; MFEM_WARNING("Changed partition in the y direction to ny = " << n << endl); } if (nz > n) { nz = n; MFEM_WARNING("Changed partition in the z direction to nz = " << n << endl); } if (dim == 2) nz = 1; subdomains.SetSize(nx,ny,nz); nxyz[0] = nx; nxyz[1]=ny; nxyz[2] = nz; nrpatch = nx*ny*nz; Vector pmin, pmax; mesh->GetBoundingBox(pmin, pmax); double h = GetUniformMeshElementSize(mesh); MeshSize = h; element_map.resize(nrpatch); double ppt[dim]; Vector pt(ppt, dim); int nrelem = mesh->GetNE(); for (int el = 0; el < nrelem; el++) { mesh->GetElementTransformation(el)->Transform( Geometries.GetCenter(mesh->GetElementBaseGeometry(el)), pt); // Given the center coordinates determine the patches that this element contributes to Array idx0(dim); Array idx1(dim); Array idx2(dim); vector> idx(3); if (dim == 2) idx[2].Append(0); for (int i = 0; i= nxyz[i]) idx0[i] = nxyz[i]-1; if (idx1[i] < 0) idx1[i] = 0; if (idx1[i] >= nxyz[i]) idx1[i] = nxyz[i]-1; if (idx2[i] < 0) idx2[i] = 0; if (idx2[i] >= nxyz[i]) idx2[i] = nxyz[i]-1; // convenient to put in one list idx[i].Append(idx0[i]); if (idx1[i] != idx0[i]) idx[i].Append(idx1[i]); if (idx2[i] != idx0[i] && idx2[i] != idx1[i]) idx[i].Append(idx2[i]); } // Now loop through all the combinations according to the idx above // in case of dim = 2 then kk = 0 for (int k=0; kDimension(); // int n = pow(mesh->GetNE(), 1.0/(double)dim); // if (nx > n) // { // nx = n; // MFEM_WARNING("Changed partition in the x direction to nx = " << n << endl); // } // if (ny > n) // { // ny = n; // MFEM_WARNING("Changed partition in the y direction to ny = " << n << endl); // } // if (nz > n) // { // nz = n; // MFEM_WARNING("Changed partition in the z direction to nz = " << n << endl); // } if (dim == 2) nz = 1; subdomains.SetSize(nx,ny,nz); nxyz[0] = nx; nxyz[1]=ny; nxyz[2] = nz; nrpatch = nx*ny*nz; Vector pmin, pmax; mesh->GetBoundingBox(pmin, pmax); double h = GetUniformMeshElementSize(mesh); MeshSize = h; cout << "h = " << h << endl; // Check that ovlp_size does not exit subdomain size MFEM_VERIFY((pmax[0]-pmin[0])/nx >= h*ovlp_nlayers, "Check ovlp size in partition"); cout << "pmax[0]-pmin[0])/nx = " << (pmax[0]-pmin[0])/nx << endl; cout << "ovlp_nlayers = " << ovlp_nlayers << endl; cout << "h*ovlp_nlayers = " << h*ovlp_nlayers << endl; MFEM_VERIFY((pmax[1]-pmin[1])/ny >= h*ovlp_nlayers, "Check ovlp size in partition"); if (dim == 3) { MFEM_VERIFY((pmax[2]-pmin[2])/nz >= h*ovlp_nlayers, "Check ovlp size in partition"); } element_map.resize(nrpatch); double ppt[dim]; Vector pt(ppt, dim); int nrelem = mesh->GetNE(); for (int el = 0; el < nrelem; el++) { mesh->GetElementTransformation(el)->Transform( Geometries.GetCenter(mesh->GetElementBaseGeometry(el)), pt); // Given the center coordinates determine the patches that this element contributes to Array idx0(dim); Array idx1(dim); Array idx2(dim); vector> idx(3); if (dim == 2) idx[2].Append(0); for (int i = 0; i= nxyz[i]) idx0[i] = nxyz[i]-1; if (idx1[i] < 0) idx1[i] = 0; if (idx1[i] >= nxyz[i]) idx1[i] = nxyz[i]-1; if (idx2[i] < 0) idx2[i] = 0; if (idx2[i] >= nxyz[i]) idx2[i] = nxyz[i]-1; // convenient to put in one list idx[i].Append(idx0[i]); if (idx1[i] != idx0[i]) idx[i].Append(idx1[i]); if (idx2[i] != idx0[i] && idx2[i] != idx1[i]) idx[i].Append(idx2[i]); } // Now loop through all the combinations according to the idx above // in case of dim = 2 then kk = 0 for (int k=0; kDimension(); nxyz[0] = nx; nxyz[1] = ny; nxyz[2] = nz; nrpatch = nx*ny*nz; subdomains.SetSize(nx,ny,nz); Vector pmin, pmax; mesh->GetBoundingBox(pmin, pmax); int nrelem = mesh->GetNE(); int partitioning[nrelem]; // determine the partitioning using the centers of the elements double ppt[dim]; Vector pt(ppt, dim); for (int el = 0; el < nrelem; el++) { mesh->GetElementTransformation(el)->Transform( Geometries.GetCenter(mesh->GetElementBaseGeometry(el)), pt); int part = 0; for (int i = dim-1; i >= 0; i--) { int idx = (int)floor(nxyz[i]*((pt(i) - pmin[i])/(pmax[i] - pmin[i]))); if (idx < 0) { idx = 0; } if (idx >= nxyz[i]) { idx = nxyz[i]-1; } part = part * nxyz[i] + idx; } partitioning[el] = part; } element_map.resize(nrpatch); for (int iel = 0; iel < nrelem; iel++) { int ip = partitioning[iel]; element_map[ip].Append(iel); } for (int k = 0; kDimension(); nx = 5; ny = 1; nz = 1; int nxyz[3] = {nx,ny,nz}; // nrpatch = nx*ny*nz; Vector pmin, pmax; mesh->GetBoundingBox(pmin, pmax); int nrelem = mesh->GetNE(); int partitioning[nrelem]; // determine the partitioning using the centers of the elements double ppt[dim]; Vector pt(ppt, dim); for (int el = 0; el < nrelem; el++) { mesh->GetElementTransformation(el)->Transform( Geometries.GetCenter(mesh->GetElementBaseGeometry(el)), pt); int part = 0; for (int i = dim-1; i >= 0; i--) { int idx = (int)floor(nxyz[i]*((pt(i) - pmin[i])/(pmax[i] - pmin[i]))); if (idx < 0) { idx = 0; } if (idx >= nxyz[i]) { idx = nxyz[i]-1; } part = part * nxyz[i] + idx; } partitioning[el] = part; } std::vector> elem_map; int npatch = nx*ny*nz; elem_map.resize(npatch); for (int iel = 0; iel < nrelem; iel++) { int ip = partitioning[iel]; elem_map[ip].Append(iel); } // Append the next subdomain to the previous nrpatch = nx*ny*nz-1; element_map.resize(nrpatch); for (int ip = 0; ipDimension(); patch_mesh.SetSize(nrpatch); for (int ip = 0; ip patch_vertices; for (int iel=0; iel elem_vertices; int iel_idx = element_map[ip][iel]; mesh->GetElementVertices(iel_idx,elem_vertices); patch_vertices.Append(elem_vertices); } patch_vertices.Sort(); patch_vertices.Unique(); int patch_nrvertices = patch_vertices.Size(); // create the mesh patch_mesh[ip] = new Mesh(dim,patch_nrvertices,patch_nrelems); // Add the vertices for (int iv = 0; ivAddVertex(mesh->GetVertex(vert_idx)); } // Add the elements (for now search through all the vertices in the patch is needed) for (int iel=0; iel elem_vertices; int iel_idx = element_map[ip][iel]; mesh->GetElementVertices(iel_idx,elem_vertices); int nrvert = elem_vertices.Size(); int ind[nrvert]; for (int iv = 0; ivGetElementType(element_map[ip][iel]); AddElementToMesh(patch_mesh[ip],elem_type,ind); } patch_mesh[ip]->FinalizeTopology(); } } void MeshPartition::AddElementToMesh(Mesh * mesh,mfem::Element::Type elem_type, int * ind) { switch (elem_type) { case Element::QUADRILATERAL: mesh->AddQuad(ind); break; case Element::TRIANGLE : mesh->AddTri(ind); break; case Element::HEXAHEDRON : mesh->AddHex(ind); break; case Element::TETRAHEDRON : mesh->AddTet(ind); break; default: MFEM_ABORT("Unknown element type"); break; } } void MeshPartition::PrintElementMap() { mfem::out << "Element map" << endl; for (int ip = 0; ip meshes, string mfilename, string sfilename) { int nrmeshes = meshes.Size(); for (int ip = 0; ipPrint(mesh_ofs); L2_FECollection L2fec(1,meshes[ip]->Dimension()); FiniteElementSpace L2fes(meshes[ip], &L2fec); GridFunction x(&L2fes); ConstantCoefficient alpha((double)ip); x.ProjectCoefficient(alpha); ostringstream sol_name; sol_name << sfilename << setfill('0') << setw(6) << ip; ofstream sol_ofs(sol_name.str().c_str()); x.Save(sol_ofs); } } void SaveMesh(Mesh * mesh, string mfilename) { cout << "saving global mesh " << endl; ostringstream mesh_name; mesh_name << mfilename; ofstream mesh_ofs(mesh_name.str().c_str()); mesh_ofs.precision(8); mesh->Print(mesh_ofs); } MeshPartition::~MeshPartition() { for (int ip = 0; ipGetComm(); MPI_Comm_size(comm, &num_procs); MPI_Comm_rank(comm, &myid); int dim = pmesh->Dimension(); if (dim == 2 ) nz = 1; subdomains.SetSize(nx,ny,nz); nxyz[0] = nx; nxyz[1]=ny; nxyz[2] = nz; nrsubdomains = nx*ny*nz; cout << "nrsubdomains = " << nrsubdomains << endl; Vector pmin, pmax; pmesh->GetBoundingBox(pmin, pmax); double h = pmesh->GetNE() ? pmesh->GetElementSize(0) : 0.0; MeshSize = h; // Check that ovlp_size does not exit subdomain size for (int d = 0; d= h*ovlp_nlayers, "Check ovlp size in partition"); } local_element_map.resize(nrsubdomains); double ppt[dim]; Vector pt(ppt, dim); int mynrelem = pmesh->GetNE(); int myelem_offset; MPI_Scan(&mynrelem, &myelem_offset, 1, MPI_INT, MPI_SUM, comm); myelem_offset -= mynrelem; for (int el = 0; el < mynrelem; el++) { pmesh->GetElementTransformation(el)->Transform( Geometries.GetCenter(pmesh->GetElementBaseGeometry(el)), pt); // Given the center coordinates determine the patches that this element contributes to Array idx0(dim); Array idx1(dim); Array idx2(dim); vector> idx(3); if (dim == 2) idx[2].Append(0); for (int i = 0; i= nxyz[i]) idx0[i] = nxyz[i]-1; if (idx1[i] < 0) idx1[i] = 0; if (idx1[i] >= nxyz[i]) idx1[i] = nxyz[i]-1; if (idx2[i] < 0) idx2[i] = 0; if (idx2[i] >= nxyz[i]) idx2[i] = nxyz[i]-1; // convenient to put in one list idx[i].Append(idx0[i]); if (idx1[i] != idx0[i]) idx[i].Append(idx1[i]); if (idx2[i] != idx0[i] && idx2[i] != idx1[i]) idx[i].Append(idx2[i]); } // Now loop through all the combinations according to the idx above // in case of dim = 2 then kk = 0 for (int k=0; ksubdomain_size(nrsubdomains); for (int ip = 0; ip < nrsubdomains; ++ip) { subdomain_size[ip] = local_element_map[ip].Size(); } Arraysubdomain_ranks(nrsubdomains*num_procs); MPI_Allgather(subdomain_size, nrsubdomains, MPI_INT, subdomain_ranks, nrsubdomains, MPI_INT, comm); Array max(nrsubdomains); max = -1; subdomain_rank.SetSize(nrsubdomains); subdomain_rank = -1; // loop through the patches and determine the rank with the max number of elements for (int irank = 0; irank < num_procs; ++irank) { int offset = irank*nrsubdomains; for (int ip = 0; ip= max[ip]) { max[ip] = subdomain_ranks[ip+offset]; subdomain_rank[ip] = irank; } } } subdomains.SetSize(nx,ny,nz); for (int k = 0; kGetComm(); MPI_Comm_size(comm, &num_procs); MPI_Comm_rank(comm, &myid); int dim = pmesh->Dimension(); CartesianParMeshPartition partition(pmesh,nx, ny, nz,nrlayers); local_element_map = partition.local_element_map; subdomains = partition.subdomains; nxyz[0] = partition.nxyz[0]; nxyz[1] = partition.nxyz[1]; nxyz[2] = partition.nxyz[2]; MeshSize = partition.MeshSize; subdomain_rank = partition.subdomain_rank; nrsubdomains = partition.local_element_map.size(); int mynrelem = pmesh->GetNE(); MPI_Scan(&mynrelem, &myelem_offset, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); myelem_offset -= mynrelem; // communicate the element map to every processor that is involved element_map.resize(nrsubdomains); // Alternative way to construct element_map on the host rank // Each contributing process sends the contents of local_element_map // to the host rank Array send_count(num_procs); send_count = 0; Array send_displ(num_procs); send_displ = 0; Array recv_count(num_procs); recv_count = 0; Array recv_displ(num_procs); recv_displ = 0; for (int ip = 0; ip < nrsubdomains; ++ip) { // a) subdomain number // b) subdomain size (local) number of elements // c) the list of elements if (local_element_map[ip].Size()) { send_count[subdomain_rank[ip]] += 1+1+local_element_map[ip].Size(); } } MPI_Alltoall(send_count,1,MPI_INT,recv_count,1,MPI_INT,comm); for (int k=0; k sendbuf(sbuff_size); sendbuf = 0; Array soffs(num_procs); soffs = 0; for (int ip = 0; ip < nrsubdomains; ++ip) { // a) subdomain number // b) subdomain size (local) number of elements // c) the list of elements if (local_element_map[ip].Size()) { int j = send_displ[subdomain_rank[ip]] + soffs[subdomain_rank[ip]]; sendbuf[j] = ip; sendbuf[j+1] = local_element_map[ip].Size(); for (int i=0; i recvbuf(rbuff_size); MPI_Alltoallv(sendbuf, send_count, send_displ, MPI_INT, recvbuf, recv_count, recv_displ, MPI_INT, comm); // recv_count.Print(); // Extract from recvbuff int k=0; while (kPrint(mesh_ofs); } } } ParMeshPartition::~ParMeshPartition() { for (int ip = 0; ip