Skip to content

Commit 631dcdd

Browse files
committed
addOperatorSparsity(J, mat) implementation
Applies the same connectivity in `mat` to all pairs of variables in `J`. Passes unit tests.
1 parent 9de5da3 commit 631dcdd

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

include/bout/petsc_jacobian.hxx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
/// @param in_var Column variable index in [0, nvars).
2828
void addOperatorSparsity(Mat Jfd, Mat sub, int out_var, int in_var);
2929

30+
/// @brief Insert the nonzero pattern of @p sub into every variable block of
31+
/// @p Jfd.
32+
///
33+
/// Equivalent to calling addOperatorSparsity(Jfd, sub, out_var, in_var) for
34+
/// every combination of @p out_var and @p in_var in [0, nvars), where
35+
/// @c nvars is inferred as @c Jfd_global / @c sub_global.
36+
///
3037
/// @param Jfd The Jacobian matrix to populate. Must be preallocated.
3138
/// @param sub Evolving-cell submatrix providing the nonzero pattern.
3239
void addOperatorSparsity(Mat Jfd, Mat sub);

src/mesh/petsc_jacobian.cxx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ void addOperatorSparsity(Mat Jfd, Mat sub, int out_var, int in_var) {
4545
}
4646
}
4747

48-
void addOperatorSparsity(Mat Jfd, Mat sub) {}
48+
void addOperatorSparsity(Mat Jfd, Mat sub) {
49+
PetscInt jfd_global{0}, sub_global{0};
50+
MatGetSize(Jfd, &jfd_global, nullptr);
51+
MatGetSize(sub, &sub_global, nullptr);
52+
53+
ASSERT1(sub_global > 0);
54+
ASSERT1(jfd_global % sub_global == 0);
55+
56+
const int nvars = static_cast<int>(jfd_global / sub_global);
57+
58+
for (int out_var = 0; out_var < nvars; ++out_var) {
59+
for (int in_var = 0; in_var < nvars; ++in_var) {
60+
addOperatorSparsity(Jfd, sub, out_var, in_var);
61+
}
62+
}
63+
}
4964

5065
#endif // BOUT_HAS_PETSC

0 commit comments

Comments
 (0)