• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Open-Sn / opensn / 22249939997

20 Feb 2026 06:11PM UTC coverage: 74.156% (-0.2%) from 74.335%
22249939997

push

github

web-flow
Merge pull request #937 from andrsd/issue/90-num-precursors

Number of precursors is `unsigned int`

4 of 7 new or added lines in 5 files covered. (57.14%)

527 existing lines in 19 files now uncovered.

19997 of 26966 relevant lines covered (74.16%)

67202602.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.46
/framework/math/linear_solver/petsc_linear_system_solver.cc
1
// SPDX-FileCopyrightText: 2024 The OpenSn Authors <https://open-sn.github.io/opensn/>
2
// SPDX-License-Identifier: MIT
3

4
#include "framework/math/linear_solver/petsc_linear_system_solver.h"
5
#include "framework/math/petsc_utils/petsc_utils.h"
6
#include "framework/runtime.h"
7

8
namespace opensn
9
{
10

11
PETScLinearSolver::PETScLinearSolver(IterativeMethod method,
638✔
12
                                     std::shared_ptr<LinearSystemContext> context_ptr)
638✔
13
  : LinearSystemSolver(method, context_ptr),
14
    A_(nullptr),
638✔
15
    b_(nullptr),
638✔
16
    x_(nullptr),
638✔
17
    ksp_(nullptr),
638✔
18
    num_local_dofs_(0),
638✔
19
    num_global_dofs_(0),
638✔
20
    system_set_(false),
638✔
21
    suppress_kspsolve_(false)
638✔
22
{
23
}
638✔
24

25
PETScLinearSolver::~PETScLinearSolver()
626✔
26
{
27
  OpenSnPETScCall(KSPDestroy(&ksp_));
28
  OpenSnPETScCall(MatDestroy(&A_));
29
  OpenSnPETScCall(VecDestroy(&x_));
30
  OpenSnPETScCall(VecDestroy(&b_));
31
}
626✔
32

626✔
33
void
626✔
34
PETScLinearSolver::ApplyToleranceOptions()
626✔
35
{
626✔
36
  OpenSnPETScCall(KSPSetTolerances(ksp_,
558✔
37
                                   tolerance_options.residual_relative,
558✔
38
                                   tolerance_options.residual_absolute,
39
                                   tolerance_options.residual_divergence,
40
                                   tolerance_options.maximum_iterations));
41
}
558✔
42

43
void
UNCOV
44
PETScLinearSolver::PreSetupCallback()
×
45
{
UNCOV
46
}
×
47

48
void
49
PETScLinearSolver::SetOptions()
558✔
50
{
51
}
558✔
52

53
void
54
PETScLinearSolver::SetSolverContext()
558✔
55
{
56
  OpenSnPETScCall(KSPSetApplicationContext(ksp_, &(*context_ptr_)));
558✔
57
}
558✔
58

59
void
UNCOV
60
PETScLinearSolver::SetConvergenceTest()
×
61
{
62
  OpenSnPETScCall(KSPSetConvergenceTest(ksp_, &KSPConvergedDefault, nullptr, nullptr));
×
UNCOV
63
}
×
64

65
void
66
PETScLinearSolver::SetMonitor()
558✔
67
{
68
}
558✔
69

70
void
UNCOV
71
PETScLinearSolver::SetPreconditioner()
×
72
{
UNCOV
73
}
×
74

75
void
UNCOV
76
PETScLinearSolver::PostSetupCallback()
×
77
{
UNCOV
78
}
×
79

80
void
81
PETScLinearSolver::Setup()
3,775✔
82
{
83
  if (IsSystemSet())
3,775✔
84
    return;
3,217✔
85

86
  PreSetupCallback();
558✔
87
  OpenSnPETScCall(KSPCreate(opensn::mpi_comm, &ksp_));
558✔
88
  const auto petsc_iterative_method = PETScIterativeMethodName();
558✔
89
  OpenSnPETScCall(KSPSetType(ksp_, petsc_iterative_method.c_str()));
558✔
90
  ApplyToleranceOptions();
558✔
91
  if (method_ == IterativeMethod::PETSC_GMRES)
558✔
92
  {
93
    OpenSnPETScCall(KSPGMRESSetRestart(ksp_, tolerance_options.gmres_restart_interval));
406✔
94
    OpenSnPETScCall(
406✔
95
      KSPGMRESSetBreakdownTolerance(ksp_, tolerance_options.gmres_breakdown_tolerance));
96
  }
97
  OpenSnPETScCall(KSPSetInitialGuessNonzero(ksp_, PETSC_FALSE));
558✔
98
  SetOptions();
558✔
99
  SetSolverContext();
558✔
100
  SetConvergenceTest();
558✔
101
  SetMonitor();
558✔
102
  SetSystemSize();
558✔
103
  SetSystem();
558✔
104
  SetPreconditioner();
558✔
105
  PostSetupCallback();
558✔
106

107
  system_set_ = true;
558✔
108
}
558✔
109

110
void
111
PETScLinearSolver::PreSolveCallback()
×
112
{
UNCOV
113
}
×
114

115
void
116
PETScLinearSolver::PostSolveCallback()
×
117
{
UNCOV
118
}
×
119

120
void
121
PETScLinearSolver::Solve()
3,775✔
122
{
123
  PreSolveCallback();
3,775✔
124
  SetInitialGuess();
3,775✔
125
  SetRHS();
3,775✔
126
  if (not suppress_kspsolve_)
3,775✔
127
    OpenSnPETScCall(KSPSolve(ksp_, b_, x_));
3,663✔
128
  PostSolveCallback();
3,775✔
129
}
3,775✔
130

131
std::string
132
PETScLinearSolver::PETScIterativeMethodName()
558✔
133
{
134
  switch (method_)
558✔
135
  {
UNCOV
136
    case IterativeMethod::NONE:
×
UNCOV
137
      return "preonly";
×
138
    case IterativeMethod::PETSC_RICHARDSON:
148✔
139
      return "richardson";
148✔
140
    case IterativeMethod::PETSC_GMRES:
406✔
141
      return "gmres";
406✔
142
    case IterativeMethod::PETSC_BICGSTAB:
4✔
143
      return "bcgs";
4✔
UNCOV
144
    default:
×
UNCOV
145
      throw std::runtime_error("Unsupported PETSc iterative method.");
×
146
  }
147
}
148

149
PetscErrorCode
150
PETScLinearSolver::LinearSolverMatrixAction(Mat matrix, Vec vector, Vec action)
49,498✔
151
{
152
  LinearSystemContext* context = nullptr;
49,498✔
153
  const PetscErrorCode ierr = MatShellGetContext(matrix, static_cast<void*>(&context));
49,498✔
154
  if (ierr != PETSC_SUCCESS)
49,498✔
155
    return ierr;
156

157
  context->MatrixAction(matrix, vector, action);
49,498✔
158

159
  return PETSC_SUCCESS;
160
}
161

162
} // namespace opensn
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc