OGS
ComputeSparsityPattern.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
5
6#include <numeric>
7#include <range/v3/range/conversion.hpp>
8#include <range/v3/view/transform.hpp>
9
11#include "MeshLib/Location.h"
13
14#ifdef USE_PETSC
16
18 NumLib::LocalToGlobalIndexMap const& dof_table, MeshLib::Mesh const& mesh)
19{
20 assert(dynamic_cast<MeshLib::NodePartitionedMesh const*>(&mesh));
21 auto const& npmesh =
22 *static_cast<MeshLib::NodePartitionedMesh const*>(&mesh);
23
24 auto const max_nonzeroes = dof_table.getNumberOfGlobalComponents() *
25 npmesh.getMaximumNConnectedNodesToNode();
26
27 // The sparsity pattern is misused here in the sense that it will only
28 // contain a single value.
29 return GlobalSparsityPattern(1, max_nonzeroes);
30}
31#else
32GlobalSparsityPattern computeSparsityPatternNonPETSc(
33 NumLib::LocalToGlobalIndexMap const& dof_table, MeshLib::Mesh const& mesh)
34{
35 MeshLib::NodeAdjacencyTable const node_adjacency_table(mesh);
36
37 // A mapping mesh node id -> global indices
38 // It acts as a cache for dof table queries.
39 auto const global_idcs =
41 ranges::views::transform([&](auto&& l)
42 { return dof_table.getGlobalIndices(l); }) |
43 ranges::to<std::vector>();
44
45 GlobalSparsityPattern sparsity_pattern(dof_table.dofSizeWithGhosts());
46
47 // Map adjacent mesh nodes to "adjacent global indices".
48 for (std::size_t n = 0; n < mesh.getNumberOfNodes(); ++n)
49 {
50 auto const& an = node_adjacency_table.getAdjacentNodes(n);
51 auto const n_self_dof = global_idcs[n].size();
52 auto const n_connected_dof =
53 std::accumulate(cbegin(an), cend(an), 0,
54 [&](auto const result, auto const i)
55 { return result + global_idcs[i].size(); });
56 auto const n_dof = n_self_dof + n_connected_dof;
57 for (auto global_index : global_idcs[n])
58 {
59 sparsity_pattern[global_index] = n_dof;
60 }
61 }
62
63 return sparsity_pattern;
64}
65#endif
66
67namespace NumLib
68{
70 LocalToGlobalIndexMap const& dof_table, MeshLib::Mesh const& mesh)
71{
72#ifdef USE_PETSC
73 return computeSparsityPatternPETSc(dof_table, mesh);
74#else
75 return computeSparsityPatternNonPETSc(dof_table, mesh);
76#endif
77}
78
79} // namespace NumLib
GlobalSparsityPattern computeSparsityPatternPETSc(NumLib::LocalToGlobalIndexMap const &dof_table, MeshLib::Mesh const &mesh)
MathLib::SparsityPattern< GlobalIndexType > GlobalSparsityPattern
std::size_t getNumberOfNodes() const
Get the number of nodes.
Definition Mesh.h:91
std::vector< GlobalIndexType > getGlobalIndices(const MeshLib::Location &l) const
Forwards the respective method from MeshComponentMap.
auto meshLocations(Mesh const &mesh, MeshItemType const item_type)
Definition Mesh.h:227
GlobalSparsityPattern computeSparsityPattern(LocalToGlobalIndexMap const &dof_table, MeshLib::Mesh const &mesh)
Computes a sparsity pattern for the given inputs.