libMesh
Loading...
Searching...
No Matches
petsc_auto_fieldsplit.C
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18#include "libmesh/petsc_auto_fieldsplit.h"
19
20#ifdef LIBMESH_HAVE_PETSC
21
22#include <petscksp.h>
23
24// Local includes
25#include "libmesh/dof_map.h"
26#include "libmesh/system.h"
27
28namespace {
29using namespace libMesh;
30
31void indices_to_fieldsplit (const Parallel::Communicator & comm,
32 const std::vector<dof_id_type> & indices,
33 PC my_pc,
34 const std::string & field_name)
35{
36 const PetscInt * idx = LIBMESH_PETSC_NULLPTR;
37 if (!indices.empty())
38 idx = reinterpret_cast<const PetscInt *>(indices.data());
39
40 IS is;
41 LibmeshPetscCall2(comm, ISCreateGeneral(comm.get(), cast_int<PetscInt>(indices.size()),
42 idx, PETSC_COPY_VALUES, &is));
43
44 LibmeshPetscCall2(comm, PCFieldSplitSetIS(my_pc, field_name.c_str(), is));
45}
46
47}
48
49namespace libMesh
50{
51
52void petsc_auto_fieldsplit (PC my_pc,
53 const System & sys)
54{
55 std::string sys_prefix = "--solver_group_";
56
57 if (libMesh::on_command_line("--solver-system-names"))
58 {
59 sys_prefix = sys_prefix + sys.name() + "_";
60 }
61
62 std::map<std::string, std::vector<dof_id_type>> group_indices;
63
64 if (libMesh::on_command_line("--solver-variable-names"))
65 {
66 for (auto v : make_range(sys.n_vars()))
67 {
68 const std::string & var_name = sys.variable_name(v);
69
70 std::vector<dof_id_type> var_idx;
72 (var_idx, sys.get_mesh(), v);
73
74 std::string group_command = sys_prefix + var_name;
75
76 const std::string empty_string;
77
78 std::string group_name = libMesh::command_line_value
79 (group_command, empty_string);
80
81 if (group_name != empty_string)
82 {
83 std::vector<dof_id_type> & indices =
84 group_indices[group_name];
85 const bool prior_indices = !indices.empty();
86 indices.insert(indices.end(), var_idx.begin(),
87 var_idx.end());
88 if (prior_indices)
89 std::sort(indices.begin(), indices.end());
90 }
91 else
92 {
93 indices_to_fieldsplit (sys.comm(), var_idx, my_pc, var_name);
94 }
95 }
96 }
97
98 for (const auto & [field_name, indices] : group_indices)
99 indices_to_fieldsplit(sys.comm(), indices, my_pc, field_name);
100}
101
102} // namespace libMesh
103
104
105#endif // #ifdef LIBMESH_HAVE_PETSC
void local_variable_indices(T &idx, const MeshBase &mesh, unsigned int var_num) const
If T == dof_id_type, counts, if T == std::vector<dof_id_type>, fills an array of, those dof indices w...
Definition dof_map.C:1122
const Parallel::Communicator & comm() const
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition system.h:100
const std::string & name() const
Definition system.h:2385
const std::string & variable_name(const unsigned int i) const
Definition system.C:2679
unsigned int n_vars() const
Definition system.C:2674
const DofMap & get_dof_map() const
Definition system.h:2417
const MeshBase & get_mesh() const
Definition system.h:2401
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)
A useful inline function which replaces the macros used previously.
The libMesh namespace provides an interface to certain functionality in the library.
T command_line_value(const std::string &, T)
Definition libmesh.C:971
PetscErrorCode PetscInt const PetscInt IS * is
void petsc_auto_fieldsplit(PC my_pc, const System &sys)
bool on_command_line(std::string arg)
Definition libmesh.C:934
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...
Definition int_range.h:176