24#ifndef VULPES_DOFS_DOF_HANDLER_HPP_
25#define VULPES_DOFS_DOF_HANDLER_HPP_
37#include <boost/unordered/unordered_flat_map.hpp>
38#include <boost/unordered/unordered_flat_set.hpp>
71template <
typename MeshType>
75 using index_t =
typename MeshType::index_t;
76 inline static constexpr bool parallel = MeshType::parallel();
192 if (dof_id >= lo && dof_id < hi) {
226 template <
typename FeSpace>
242 template <
bool DofInteractionDistance>
271 std::size_t SubentityDim,
414 MeshType::parallel(),
415 boost::unordered_flat_map<index_t, index_t>,
416 std::vector<index_t>>;
524template <
typename MeshType>
539 index_t global_dof_id = dof_id;
540 if constexpr (MeshType::parallel()) {
549 const index_t n_dofs_per_entity =
551 const index_t owning_enty_id = entity_dof_id / n_dofs_per_entity;
552 const index_t enty_multiplicity = entity_dof_id % n_dofs_per_entity;
556 m_mesh, cell_id, owning_enty_id, enty_dim, enty_multiplicity);
561template <
typename MeshType>
569 for (std::size_t d = 0; d < FeSpace::dim + 1; ++d) {
571 const auto n_glob_entities =
m_mesh.nEntity(d);
572 auto n_loc_entities =
m_mesh.nLocEntity(d);
573 if constexpr (MeshType::parallel()) {
574 n_loc_entities +=
m_mesh.nGhostEntity(d);
583template <
typename MeshType>
585 std::size_t SubentityDim,
593 const index_t n_dofs_per_entity =
595 if (n_dofs_per_entity > 0) {
597 if constexpr (MeshType::parallel()) {
598 n_loc_entity +=
m_mesh.nGhostEntity(SubentityDim);
601 for (
index_t enty_id = 0; enty_id < n_loc_entity; ++enty_id) {
603 for (
index_t j = 0; j < n_dofs_per_entity; ++j) {
605 index_t global_enty_id = enty_id;
606 if constexpr (MeshType::parallel()) {
607 global_enty_id =
m_mesh.localToGlobal(SubentityDim)[enty_id];
610 global_enty_id * n_dofs_per_entity + j;
611 auto loc_dof_id = glob_dof_id;
612 if constexpr (MeshType::parallel()) {
619 if constexpr (SubentityDim == Dim) {
621 dof_to_cell[loc_dof_id].push_back(global_enty_id);
623 if constexpr (MeshType::parallel()) {
625 if (enty_id <
m_mesh.nLocEntity(SubentityDim)) {
634 const auto& enty_to_cell_map =
635 m_mesh.template getEntityToCellGlobalMap<SubentityDim>();
637 for (
const auto cell_id : enty_to_cell_map[enty_id]) {
640 if constexpr (SubentityDim == Dim - 1) {
641 if (cell_id ==
static_cast<decltype(cell_id)
>(-1)) {
645 dof_to_cell[loc_dof_id].push_back(cell_id);
647 if constexpr (MeshType::parallel()) {
650 const auto local_cell_id =
m_mesh.globalToLocal(Dim).at(cell_id);
651 if ((local_cell_id >=
m_mesh.nLocEntity(Dim)) &&
660 if constexpr (MeshType::parallel()) {
673template <
typename MeshType>
683 if constexpr (FeSpace::dim > 0) {
686 if constexpr (FeSpace::dim > 1) {
689 if constexpr (FeSpace::dim > 2) {
694template <
typename MeshType>
697 std::iota(idxs.begin(), idxs.end(), 0);
698 std::sort(idxs.begin(), idxs.end(), [
this](
auto a,
auto b) {
699 return m_shared_dofs[a] < m_shared_dofs[b];
703 for (std::size_t i = 0; i < idxs.size(); ++i) {
707 for (std::size_t i = 0; i < idxs.size(); ++i) {
713template <
typename MeshType>
716 constexpr std::size_t static_buffer_size =
719 auto n_loc_cell =
m_mesh.nLocEntity(MeshType::self_dim);
720 if constexpr (MeshType::parallel()) {
721 n_loc_cell +=
m_mesh.nGhostEntity(MeshType::self_dim);
723 cell_to_dof.
resize(n_loc_cell);
724 for (std::size_t dof_id = 0; dof_id < dof_to_cell.size(); ++dof_id) {
728 std::sort(dof_to_cell[dof_id].begin(), dof_to_cell[dof_id].end());
729 for (
const auto global_cell_id : dof_to_cell[dof_id]) {
730 auto loc_cell_id = global_cell_id;
731 if constexpr (MeshType::parallel()) {
733 m_mesh.globalToLocal(MeshType::self_dim).at(global_cell_id);
735 cell_to_dof[loc_cell_id].
push_back(dof_id);
743template <
typename MeshType>
745 const auto max_dof_idx =
748 boost::unordered_flat_set<index_t> owned_cells_dof(
749 data.begin(), data.begin() + max_dof_idx);
756 const auto it = std::lower_bound(
765template <
typename MeshType>
775 constexpr std::size_t static_row_size =
776 sizeof(std::vector<index_t>) /
sizeof(
index_t);
778 static_assert(FeSpace::dim == MeshType::self_dim);
784 if (MeshType::parallel()) {
794 if constexpr (MeshType::parallel()) {
800 if constexpr (MeshType::parallel()) {
804 const auto reminder = (p < (total_dofs %
mpi::size())) ? 1 : 0;
811template <
typename MeshType>
812template <
bool DofInteractionDistance>
813std::pair<typename MeshType::index_t, typename MeshType::index_t>
815 typename MeshType::index_t dof_id,
816 typename MeshType::index_t* buffer1,
817 typename MeshType::index_t* buffer2) {
818 static std::array<std::size_t, 2> buffers_size = {0, 0};
819 static std::size_t active_buffer = 0;
820 buffers_size = {0, 0};
822 std::array<index_t*, 2> buffers = {buffer1, buffer2};
828 const auto updateBuffer = [&](
auto dofs_view) {
829 const index_t* f1 = dofs_view.data();
830 const index_t* l1 = f1 + dofs_view.size();
831 const index_t* f2 = buffers[active_buffer];
832 const index_t* l2 = f2 + buffers_size[active_buffer];
833 index_t* o = buffers[1 - active_buffer];
835 buffers_size[1 - active_buffer] =
836 std::distance(o, std::set_union(f1, l1, f2, l2, o));
840 buffers_size[active_buffer] = 0;
841 active_buffer = 1 - active_buffer;
846 auto local_cell_id = global_cell_id;
847 if constexpr (MeshType::parallel()) {
849 m_mesh.globalToLocal(MeshType::self_dim).at(global_cell_id);
855 if constexpr (DofInteractionDistance) {
856 for (
index_t loc_neigh_cell_id :
m_mesh.getDualMap()[local_cell_id]) {
862 return {active_buffer, buffers_size[active_buffer]};
865template <
typename MeshType>
866template <
typename FeSpace>
868 static constexpr std::size_t n_max_dofs_per_row = 1048576;
869 static std::array<std::array<index_t, n_max_dofs_per_row>, 2> buffers;
875 constexpr index_t probe_size = 1000;
880 std::uniform_int_distribution<uint64_t> dis(0,
m_dof_to_cell.size() - 1);
881 for (
index_t i = 0; i < probe_size; ++i) {
882 const auto random_dof_id = dis(gen);
883 const auto [active_buffer, buffer_size] =
885 random_dof_id, buffers[0].data(), buffers[1].data());
891 <<
"Sparsity pattern probing suggested size: " + std::to_string(nnz) +
893 auto uncompressed_sparsity_pattern =
898 uncompressed_sparsity_pattern);
902 const auto [active_buffer, buffer_size] =
904 dof_id, buffers[0].data(), buffers[1].data());
905 const auto nnz_dofid = buffer_size;
908 [dof_id, nnz_dofid, active_buffer](
auto& map) {
910 buffers[active_buffer].data(),
911 buffers[active_buffer].data() + nnz_dofid);
913 uncompressed_sparsity_pattern);
918 [](
const auto& map) {
return map.template compress<index_t>(); },
919 uncompressed_sparsity_pattern);
922template <
typename MeshType>
927 std::vector<std::size_t> cum_dof_offset(size + 1, 0);
935 for (
int p = 1; p < size; ++p) {
937 cum_dof_offset[p + 1] = cum_dof_offset[p] + owned_dof_p;
947 std::vector<index_t> shared_renumbering;
951 std::vector<index_t> unq_shared_dof;
954 std::vector<index_t> bnd_dof_belonging_to_p;
957 std::vector<index_t> tmp_buffer;
959 for (
int p = 0; p < size - 1; ++p) {
964 unq_shared_dof.end(), std::back_inserter(bnd_dof_belonging_to_p));
966 for (
int i = p; i < size; ++i) {
967 cum_dof_offset[i + 1] += bnd_dof_belonging_to_p.size();
970 const auto offset_without_shared =
971 cum_dof_offset[p + 1] - bnd_dof_belonging_to_p.size();
972 for (std::size_t i = 0; i < bnd_dof_belonging_to_p.size(); ++i) {
974 offset_without_shared + i;
978 unq_shared_dof.begin(), unq_shared_dof.end(),
981 std::swap(unq_shared_dof, tmp_buffer);
982 bnd_dof_belonging_to_p.resize(0);
983 tmp_buffer.resize(0);
994 std::vector<index_t> tmp_own_global_old;
995 std::vector<index_t> tmp_own_global_new;
996 for (
int p = 1; p < size; ++p) {
999 for (std::size_t i = 0; i < tmp_own_global_old.size(); ++i) {
1001 tmp_own_global_new[i];
1006 for (
int p = 1; p < size; ++p) {
1009 shared_renumbering[i] =
1017 shared_renumbering[i] =
1023 std::vector<index_t> own_global_old;
1024 std::vector<index_t> own_global_new;
1026 for (std::size_t i = 0; i <
m_owned_dofs.size(); ++i) {
1031 own_global_old.push_back(glob_own_id);
1032 own_global_new.push_back(new_glob_id);
1057template <
typename MeshType>
1065 std::shared_ptr<SparsityPatternType> owned_sparsity_pattern;
1066 if (MeshType::parallel()) {
1067 owned_sparsity_pattern =
1069 sparsity_pattern_ptr = owned_sparsity_pattern.get();
1071 const auto [perm, perm_inv] =
1072 renumbering_func(*sparsity_pattern_ptr, reversed);
1073 assert(perm.size() == perm_inv.size());
1075 if constexpr (MeshType::parallel()) {
1078 std::vector<index_t> new_owned_dof_id(
m_owned_dofs.size());
1081 for (std::size_t i = 0; i < perm.size(); ++i) {
1084 new_owned_dof_id[i] =
1090 std::vector<index_t> new_shared_numbering;
1094 for (
int p = 0; p < size; ++p) {
1099 assert(new_owned_dof_id.size() == owned_dofs_p.size());
1100 for (std::size_t i = 0; i < new_owned_dof_id.size(); ++i) {
1106 for (
int p = 1; p < size; ++p) {
1109 new_shared_numbering[i] =
1149template <
typename MeshType>
1151 if (MeshType::parallel()) {
1154 std::vector<index_t> loc_owned_dofs_inv(n_local_dofs, -1);
1162 owned_map.indices().push_back(0);
1164 const auto row_start = indices[own_loc_dof_id];
1165 const index_t row_size = indices[own_loc_dof_id + 1] - row_start;
1167 for (
index_t j = 0; j < row_size; ++j) {
1168 const index_t new_glob_id = data[row_start + j];
1171 const auto it = std::lower_bound(
1176 owned_map.data().push_back(loc_owned_dofs_inv[loc_id]);
1180 owned_map.indices().push_back(real_size + owned_map.indices().back());
1188template <
typename MeshType>
1190 if constexpr (MeshType::parallel()) {
1211 std::vector<index_t> tmp_buffer;
1215 if (tmp_buffer.size() <
static_cast<std::size_t
>(row_size)) {
1216 tmp_buffer.resize(row_size);
1218 for (
index_t i = 0; i < row_size; ++i) {
1222 tmp_buffer.begin(), tmp_buffer.begin() + row_size,
1232 if constexpr (MeshType::parallel()) {
1242template <
typename MeshType>
1246 if constexpr (MeshType::parallel()) {
1247 const auto n_owned =
m_mesh.nLocEntity(MeshType::self_dim);
1248 const auto n_ghost =
m_mesh.nGhostEntity(MeshType::self_dim);
1256 for (
index_t cell_id = n_owned; cell_id < n_owned + n_ghost; ++cell_id) {
1258 if (dof_id >= lo && dof_id < hi) {
1271template <
typename MeshType>
1274 using LocToNewMap = std::vector<index_t>;
1276 std::vector<SpPatternType> sp_patterns(
mpi::size());
1277 std::vector<LocToNewMap> loc_to_new_maps(
mpi::size());
1278 std::vector<std::vector<index_t>> all_owned_dofs(
mpi::size());
1283 loc_to_new_maps[0] = map;
1300 for (
index_t loc_dof_id = 0; loc_dof_id < sp_patterns[p].size();
1302 const auto glob_dof_id = loc_to_new_maps[p][loc_dof_id];
1303 auto& glob_row = global_sp_pattern.
getRow(glob_dof_id);
1304 const auto loc_row = sp_patterns[p][loc_dof_id];
1305 std::sort(loc_row.begin(), loc_row.end());
1310 all_owned_dofs[p].begin(), all_owned_dofs[p].end(),
1311 loc_dof_id) != all_owned_dofs[p].end()) {
1312 if (glob_row.size()) {
1313 assert(glob_row.size() ==
static_cast<std::size_t
>(loc_row.size()));
1315 std::equal(glob_row.begin(), glob_row.end(), loc_row.begin()));
1317 glob_row.assign(loc_row.begin(), loc_row.end());
1323 return global_sp_pattern.template compress<index_t>();
A class that behaves like a vector of vectors, providing automatic row range checking.
Definition crs_map.hpp:758
constexpr void resize(std::size_t new_size)
Resizes the map to contain the specified number of SmallVector objects.
Definition crs_map.hpp:807
constexpr void push_back(const SmallVector< DataType, N > &value)
Appends a copy of a SmallVector to the map.
Definition crs_map.hpp:830
SmallVector< DataType, N > & getRow(std::size_t row_idx)
Access a row of the matrix as a mutable reference.
Definition crs_map.hpp:857
Definition crs_map.hpp:72
typename MeshType::index_t index_t
Definition dof_handler.hpp:75
std::vector< index_t > m_owned_cells_dofs
Definition dof_handler.hpp:482
void buildGhostDofs()
Builds m_ghost_dofs/m_ghost_dof_to_local from this rank's ghost cells (mesh-level) and m_cell_to_dof ...
Definition dof_handler.hpp:1243
index_t localGhostedIndex(index_t dof_id) const
Local index, within a Vector built by la::Vector::createGhosted() from this DofHandler,...
Definition dof_handler.hpp:188
void distributeSubentityDofs(const fe::FeSpaceBase< Dim, FloatType, DofPolicy, FeSpace > &fe_space, auto &dof_to_cell)
Distribute the dofs associated to a certain geometric entity.
Definition dof_handler.hpp:590
std::size_t m_owning_matrix_rows
Definition dof_handler.hpp:487
void buildCellToCellDof(const fe::FeSpaceBase< Dim, FloatType, DofPolicy, FeSpace > &fe_space)
Build the local enumeration of the basis functions for each cell.
Definition dof_handler.hpp:530
std::vector< index_t > m_partition_bnd_dofs
Definition dof_handler.hpp:493
std::array< index_t, MeshType::self_dim+2 > m_glob_cum_ndof_per_entity
Definition dof_handler.hpp:407
datastructure::CrsMap< index_t, index_t > m_cell_to_cell_dof
Definition dof_handler.hpp:397
const auto & getRefToCurrNumbering() const
Definition dof_handler.hpp:131
datastructure::CrsMap< index_t, index_t > m_cell_to_dof
Definition dof_handler.hpp:393
void invertDofToCellMappingAndCompress(auto &dof_to_cell)
Invert the dof to cell mapping.
Definition dof_handler.hpp:714
MeshType Mesh
Definition dof_handler.hpp:74
void computeNumDofs(const fe::FeSpaceBase< Dim, FloatType, DofPolicy, FeSpace > &fe_space)
Computes the total number of DoFs.
Definition dof_handler.hpp:567
void distributeDofs(const fe::FeSpaceBase< Dim, FloatType, DofPolicy, FeSpace > &fe_space)
Distribute the DoFs (that is create a cell to dof and dof to cell maps) and create a sparsity pattern...
Definition dof_handler.hpp:771
void renumberMpiSubdomainWise()
Renumber the DoFs in order to minimize the MPI communications.
Definition dof_handler.hpp:923
boost::unordered_flat_map< index_t, index_t > m_global_to_local_dof
Definition dof_handler.hpp:445
std::vector< std::size_t > m_all_dof_offset
Definition dof_handler.hpp:478
void computeSparsityPattern()
Compute the sparsity pattern of the global matrix.
Definition dof_handler.hpp:867
boost::unordered_flat_map< index_t, index_t > m_ghost_dof_to_local
Definition dof_handler.hpp:505
const auto & dofOffset() const
Get the dofs offset of each proc.
Definition dof_handler.hpp:116
std::vector< index_t > m_local_shared_dofs
Definition dof_handler.hpp:467
index_t nOwnedDofs() const
Number of dofs owned by this rank (all of them, for a serial mesh).
Definition dof_handler.hpp:167
std::size_t ndofs() const
Get the total number of dofs for this MPI proc.
Definition dof_handler.hpp:108
index_t ghostDofLocalIndex(index_t dof_id) const
Position of a ghost dof's global id within getGhostDofs(), i.e. its offset within a Vector::createGho...
Definition dof_handler.hpp:159
std::vector< index_t > m_local_to_global_dof
Definition dof_handler.hpp:441
datastructure::CrsMap< index_t, index_t > m_sparsity_pattern
Definition dof_handler.hpp:402
void applyRenumbering(const RenumberingFunctor< index_t > &renumbering_func, bool reversed)
Apply a renumbering to the DoFs to minimize the bandwidth.
Definition dof_handler.hpp:1058
std::vector< index_t > m_old_to_new_numbering
Definition dof_handler.hpp:421
bool hasRenumbering() const
Definition dof_handler.hpp:134
std::size_t ndofsGlobal() const
Get the total number of dofs.
Definition dof_handler.hpp:112
bool m_has_renumbering
Definition dof_handler.hpp:219
const auto & getCellToDof() const
Get the cell to dof map.
Definition dof_handler.hpp:127
std::vector< index_t > m_owned_dofs
Definition dof_handler.hpp:453
std::vector< index_t > m_local_owned_dofs
Definition dof_handler.hpp:463
const auto & getSparsityPattern() const
Get the CrsMap of the sparsity pattern.
Definition dof_handler.hpp:120
const auto & getCurrToRefNumbering() const
Definition dof_handler.hpp:130
std::array< index_t, MeshType::self_dim+2 > m_loc_cum_ndof_per_entity
Definition dof_handler.hpp:436
const auto & getCellToCellDof() const
Definition dof_handler.hpp:129
static constexpr bool parallel
Definition dof_handler.hpp:76
void computeOwnedCellDofsAndBndDofs()
Compute owned cell dofs and boundary dofs.
Definition dof_handler.hpp:744
const auto & getLocToGlobNumbering() const
Definition dof_handler.hpp:133
const auto & getGlobToLocNumbering() const
Definition dof_handler.hpp:132
const MeshType & m_mesh
Definition dof_handler.hpp:381
auto computeOwnedSparsityPattern()
Compute the slice of the sparsity pattern that belongs to this proc.
Definition dof_handler.hpp:1150
void finalize()
Definition dof_handler.hpp:1189
std::vector< std::vector< index_t > > m_all_partition_bnd_dofs
Definition dof_handler.hpp:521
void sortSharedDofs()
Sorts shared dofs following global (geometric) enumeration.
Definition dof_handler.hpp:695
MappingType m_new_to_old_numbering
Definition dof_handler.hpp:426
std::conditional_t< MeshType::parallel(), boost::unordered_flat_map< index_t, index_t >, std::vector< index_t > > MappingType
Definition dof_handler.hpp:413
std::vector< index_t > m_all_old_to_new_numbering
Definition dof_handler.hpp:513
std::vector< index_t > m_shared_dofs
Definition dof_handler.hpp:459
std::vector< index_t > m_ghost_dofs
Definition dof_handler.hpp:500
std::size_t m_dof_offset
Definition dof_handler.hpp:473
datastructure::CrsMap< index_t, index_t > m_dof_to_cell
Definition dof_handler.hpp:386
const std::vector< index_t > & getGhostDofs() const
Global dof indices of dofs living on this rank's ghost cells, i.e. dofs owned by another rank but nee...
Definition dof_handler.hpp:146
std::pair< index_t, index_t > computeSparsityForDof(index_t dof_id, index_t *buffer1, index_t *buffer2)
Compute the sparsity pattern of the global matrix for a certain dof.
DofHandler(const MeshType &mesh)
Definition dof_handler.hpp:78
void distributeAllSubentityDofs(const fe::FeSpaceBase< Dim, FloatType, DofPolicy, FeSpace > &fe_space, auto &dof_to_cell)
Distribute the dofs associated to a all geometric entities.
Definition dof_handler.hpp:679
auto gatherSparsityPatternRank0() const
Compute the global sparsity pattern scattered among MPI procs.
Definition dof_handler.hpp:1272
std::vector< std::vector< index_t > > m_all_shared_dof
Definition dof_handler.hpp:517
Class describing the template for a FeSpace.
Definition fe_space.hpp:77
std::size_t getNumDofsForSubdimEntity(std::size_t dim) const
number of DoFs associated with each geometric entity (vtx = 0, edges = 1, faces = 2 or volume cells =...
Definition fe_space.hpp:132
constexpr MeshType::index_t dofToCellDof(const MeshType &mesh, MeshType::index_t cell_id, MeshType::index_t owning_enty_id, MeshType::index_t enty_dim, MeshType::index_t enty_multiplicity) const
From the global descriptors of a dof, get the dof in local cell enumeration.
Definition fe_space.hpp:166
CrsMapUncompressedVariant< DataType > getUncompressedCrsMapWithStaticStorageGreaterThan(std::size_t static_size)
Function that builds an UncompressedCrsMap with at least a certain static storage,...
Definition crs_map.hpp:936
Definition dof_handler.hpp:50
std::function< Permutation< T >( const datastructure::CrsMap< T, T > &sparsity_pattern, bool reversed)> RenumberingFunctor
Type of a function that renumbers a CrsMap.
Definition numbering.hpp:52
void recv(T *data, int count, int source, MPI_Comm communicator=MPI_COMM_WORLD, MPI_Status *status=MPI_STATUS_IGNORE)
Receive count contiguous values of type T from a source rank.
Definition mpi_wrappers.hpp:311
static int rank()
Return the MPI rank in MPI_COMM_WORLD.
Definition mpi_wrappers.hpp:116
static int size()
Return the size of MPI_COMM_WORLD.
Definition mpi_wrappers.hpp:124
static bool root()
Return true if the rank in MPI_COMM_WORLD is zero.
Definition mpi_wrappers.hpp:132
void send(const T *data, int count, int destination, MPI_Comm communicator=MPI_COMM_WORLD)
Send count contiguous values of type T to a destination rank.
Definition mpi_wrappers.hpp:149
void bcast(T *data, int count, int root, MPI_Comm communicator=MPI_COMM_WORLD)
Broadcast count contiguous values of type T from root.
Definition mpi_wrappers.hpp:495
@ DEBUG
Definition pcout.hpp:37
constexpr U pow(U base, V exp, U result=U(1))
Computes the power of a number.
Definition vmath.hpp:171
Definition cell_manager.hpp:38
vulpes::utils::pcout_t & pcout
Definition pcout.hpp:160
#define PROFILE_INTERVAL(func)
Time a single execution of func under the label #func using Profiler::startInterval()/stopInterval().
Definition profiler.hpp:56