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

Open-Sn / opensn / 18054039948

26 Sep 2025 11:32AM UTC coverage: 75.055% (+0.4%) from 74.703%
18054039948

push

github

web-flow
Merge pull request #769 from andrsd/fix-format

Fixing comment formatting

17797 of 23712 relevant lines covered (75.05%)

47290541.02 hits per line

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

80.68
/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/runtime.h"
6

7
namespace opensn
8
{
9

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

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

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

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

47
void
48
PETScLinearSolver::SetOptions()
287✔
49
{
50
}
287✔
51

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

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

64
void
65
PETScLinearSolver::SetMonitor()
287✔
66
{
67
}
287✔
68

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

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

79
void
80
PETScLinearSolver::Setup()
1,148✔
81
{
82
  if (IsSystemSet())
1,148✔
83
    return;
861✔
84

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

105
  system_set_ = true;
287✔
106
}
287✔
107

108
void
109
PETScLinearSolver::PreSolveCallback()
×
110
{
111
}
×
112

113
void
114
PETScLinearSolver::PostSolveCallback()
×
115
{
116
}
×
117

118
void
119
PETScLinearSolver::Solve()
1,148✔
120
{
121
  PreSolveCallback();
1,148✔
122
  SetInitialGuess();
1,148✔
123
  SetRHS();
1,148✔
124
  if (not suppress_kspsolve_)
1,148✔
125
    KSPSolve(ksp_, b_, x_);
1,036✔
126
  PostSolveCallback();
1,148✔
127
}
1,148✔
128

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

147
int
148
PETScLinearSolver::LinearSolverMatrixAction(Mat matrix, Vec vector, Vec action)
16,299✔
149
{
150
  LinearSystemContext* context;
16,299✔
151
  MatShellGetContext(matrix, &context);
16,299✔
152

153
  context->MatrixAction(matrix, vector, action);
16,299✔
154

155
  return 0;
16,299✔
156
}
157

158
} // 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