• 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

83.65
/framework/math/nonlinear_solver/petsc_nonlinear_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/nonlinear_solver/petsc_nonlinear_solver.h"
5
#include "framework/math/petsc_utils/petsc_utils.h"
6
#include "framework/runtime.h"
7
#include "framework/logging/log.h"
8
#include "framework/logging/stringstream_color.h"
9

10
namespace opensn
11
{
12

13
PETScNonLinearSolver::PETScNonLinearSolver(std::shared_ptr<NonLinearSolverContext> context_ptr,
28✔
14
                                           const InputParameters& params)
28✔
15
  : NonLinearSolver(params.GetParamValue<std::string>("name"), context_ptr),
56✔
16
    J_(nullptr),
28✔
17
    P_(nullptr),
28✔
18
    r_(nullptr),
28✔
19
    x_(nullptr),
28✔
20
    nl_solver_(nullptr),
28✔
21
    num_local_dofs_(0),
28✔
22
    num_global_dofs_(0),
28✔
23
    options_(params),
28✔
24
    system_set_(false),
28✔
25
    converged_(false)
112✔
26
{
27
}
28✔
28

29
PETScNonLinearSolver::~PETScNonLinearSolver()
28✔
30
{
31
  OpenSnPETScCall(SNESDestroy(&nl_solver_));
32
  OpenSnPETScCall(VecDestroy(&x_));
33
  OpenSnPETScCall(VecDestroy(&r_));
34
  OpenSnPETScCall(MatDestroy(&J_));
35
}
28✔
36

28✔
37
void
28✔
38
PETScNonLinearSolver::ApplyToleranceOptions()
28✔
39
{
28✔
40
  OpenSnPETScCall(SNESSetTolerances(nl_solver_,
28✔
41
                                    options_.nl_abs_tol,
28✔
42
                                    options_.nl_rel_tol,
43
                                    options_.nl_sol_tol,
44
                                    options_.nl_max_its,
45
                                    options_.nl_max_r_evaluations));
46
  OpenSnPETScCall(SNESSetMaxLinearSolveFailures(nl_solver_, options_.l_max_failed_iterations));
28✔
47
  KSP ksp = nullptr;
28✔
48
  OpenSnPETScCall(SNESGetKSP(nl_solver_, &ksp));
28✔
49
  OpenSnPETScCall(KSPSetTolerances(
28✔
50
    ksp, options_.l_rel_tol, options_.l_abs_tol, options_.l_div_tol, options_.l_max_its));
51
  if (options_.l_method == "gmres")
28✔
52
  {
53
    OpenSnPETScCall(KSPGMRESSetRestart(ksp, options_.l_gmres_restart_intvl));
28✔
54
    OpenSnPETScCall(KSPGMRESSetBreakdownTolerance(ksp, options_.l_gmres_breakdown_tol));
28✔
55
  }
56
  OpenSnPETScCall(KSPSetInitialGuessNonzero(ksp, PETSC_FALSE));
28✔
57
}
28✔
58

59
void
UNCOV
60
PETScNonLinearSolver::PreSetupCallback()
×
61
{
UNCOV
62
}
×
63

64
void
65
PETScNonLinearSolver::SetOptions()
56✔
66
{
67
}
56✔
68

69
void
70
PETScNonLinearSolver::SetSolverContext()
56✔
71
{
72
  OpenSnPETScCall(SNESSetApplicationContext(nl_solver_, &(*context_ptr_)));
56✔
73
}
56✔
74

75
void
76
PETScNonLinearSolver::SetConvergenceTest()
28✔
77
{
78
}
28✔
79

80
void
UNCOV
81
PETScNonLinearSolver::SetMonitor()
×
82
{
UNCOV
83
}
×
84

85
void
86
PETScNonLinearSolver::SetPreconditioner()
28✔
87
{
88
}
28✔
89

90
void
91
PETScNonLinearSolver::PostSetupCallback()
28✔
92
{
93
}
28✔
94

95
void
96
PETScNonLinearSolver::Setup()
28✔
97
{
98
  if (IsSystemSet())
28✔
UNCOV
99
    return;
×
100
  this->PreSetupCallback();
28✔
101

102
  OpenSnPETScCall(SNESCreate(mpi_comm, &nl_solver_));
28✔
103

104
  OpenSnPETScCall(SNESSetOptionsPrefix(nl_solver_, solver_name_.c_str()));
28✔
105

106
  OpenSnPETScCall(SNESSetType(nl_solver_, options_.petsc_snes_type.c_str()));
28✔
107
  if (options_.nl_method == "LINEAR")
28✔
UNCOV
108
    OpenSnPETScCall(SNESSetType(nl_solver_, SNESKSPONLY));
×
109
  SNESLineSearch linesearch = nullptr;
28✔
110
  OpenSnPETScCall(SNESGetLineSearch(nl_solver_, &linesearch));
28✔
111
  OpenSnPETScCall(SNESLineSearchSetType(linesearch, SNESLINESEARCHBT));
28✔
112

113
  KSP ksp = nullptr;
28✔
114
  OpenSnPETScCall(SNESGetKSP(nl_solver_, &ksp));
28✔
115
  OpenSnPETScCall(KSPSetType(ksp, options_.l_method.c_str()));
28✔
116

117
  OpenSnPETScCall(KSPSetOptionsPrefix(ksp, solver_name_.c_str()));
28✔
118

119
  ApplyToleranceOptions();
28✔
120

121
  SetOptions();
28✔
122

123
  SetSolverContext();
28✔
124
  SetOptions();
28✔
125

126
  SetSolverContext();
28✔
127
  SetConvergenceTest();
28✔
128
  SetMonitor();
28✔
129

130
  SetSystemSize();
28✔
131
  SetSystem();
28✔
132

133
  SetFunction();
28✔
134
  SetJacobian();
28✔
135

136
  SetPreconditioner();
28✔
137

138
  PostSetupCallback();
28✔
139
  system_set_ = true;
28✔
140
}
141

142
void
143
PETScNonLinearSolver::PreSolveCallback()
28✔
144
{
145
}
28✔
146

147
void
UNCOV
148
PETScNonLinearSolver::PostSolveCallback()
×
149
{
UNCOV
150
}
×
151

152
void
153
PETScNonLinearSolver::Solve()
28✔
154
{
155
  converged_ = false;
28✔
156
  converged_reason_string_ = "Reason not obtained";
28✔
157
  PreSolveCallback();
28✔
158
  SetInitialGuess();
28✔
159

160
  OpenSnPETScCall(SNESSolve(nl_solver_, nullptr, x_));
28✔
161
  PostSolveCallback();
28✔
162

163
  SNESConvergedReason conv_reason = SNES_CONVERGED_ITERATING;
28✔
164
  OpenSnPETScCall(SNESGetConvergedReason(nl_solver_, &conv_reason));
28✔
165

166
  if (conv_reason > 0)
28✔
167
    converged_ = true;
28✔
168

169
  const char* strreason = nullptr;
28✔
170
  OpenSnPETScCall(SNESGetConvergedReasonString(nl_solver_, &strreason));
28✔
171

172
  converged_reason_string_ = std::string(strreason);
28✔
173
}
28✔
174

175
std::string
UNCOV
176
PETScNonLinearSolver::GetConvergedReasonString() const
×
177
{
178
  std::stringstream outstr;
×
179
  if (converged_)
×
180
    outstr << StringStreamColor(FG_GREEN) << std::string(10, ' ') << "Converged "
×
UNCOV
181
           << converged_reason_string_ << StringStreamColor(RESET);
×
182
  else
183
    outstr << StringStreamColor(FG_RED) << std::string(10, ' ') << "Convergence failure "
×
UNCOV
184
           << converged_reason_string_ << StringStreamColor(RESET);
×
185

186
  return outstr.str();
×
UNCOV
187
}
×
188

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