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

Open-Sn / opensn / 16766938693

05 Aug 2025 03:26PM UTC coverage: 73.386% (+0.1%) from 73.282%
16766938693

push

github

web-flow
Merge pull request #693 from andrsd/move-bnds

Moving sweep boundaries into `DiscreteOrdinatesProblem`

247 of 311 new or added lines in 19 files covered. (79.42%)

625 existing lines in 49 files now uncovered.

18320 of 24964 relevant lines covered (73.39%)

43106214.05 hits per line

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

83.96
/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/runtime.h"
6
#include "framework/logging/log.h"
7
#include "framework/logging/stringstream_color.h"
8

9
namespace opensn
10
{
11

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

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

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

57
void
UNCOV
58
PETScNonLinearSolver::PreSetupCallback()
×
59
{
UNCOV
60
}
×
61

62
void
63
PETScNonLinearSolver::SetOptions()
56✔
64
{
65
}
56✔
66

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

73
void
74
PETScNonLinearSolver::SetConvergenceTest()
28✔
75
{
76
}
28✔
77

78
void
UNCOV
79
PETScNonLinearSolver::SetMonitor()
×
80
{
UNCOV
81
}
×
82

83
void
84
PETScNonLinearSolver::SetPreconditioner()
28✔
85
{
86
}
28✔
87

88
void
89
PETScNonLinearSolver::PostSetupCallback()
28✔
90
{
91
}
28✔
92

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

100
  SNESCreate(mpi_comm, &nl_solver_);
28✔
101

102
  SNESSetOptionsPrefix(nl_solver_, solver_name_.c_str());
28✔
103

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

111
  KSP ksp;
28✔
112
  SNESGetKSP(nl_solver_, &ksp);
28✔
113
  KSPSetType(ksp, options_.l_method.c_str());
28✔
114

115
  KSPSetOptionsPrefix(ksp, solver_name_.c_str());
28✔
116

117
  ApplyToleranceOptions();
28✔
118

119
  SetOptions();
28✔
120

121
  SetSolverContext();
28✔
122
  SetOptions();
28✔
123

124
  SetSolverContext();
28✔
125
  SetConvergenceTest();
28✔
126
  SetMonitor();
28✔
127

128
  SetSystemSize();
28✔
129
  SetSystem();
28✔
130

131
  SetFunction();
28✔
132
  SetJacobian();
28✔
133

134
  SetPreconditioner();
28✔
135

136
  PostSetupCallback();
28✔
137
  system_set_ = true;
28✔
138
}
139

140
void
141
PETScNonLinearSolver::PreSolveCallback()
28✔
142
{
143
}
28✔
144

145
void
UNCOV
146
PETScNonLinearSolver::PostSolveCallback()
×
147
{
UNCOV
148
}
×
149

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

158
  SNESSolve(nl_solver_, nullptr, x_);
28✔
159
  PostSolveCallback();
28✔
160

161
  SNESConvergedReason conv_reason;
28✔
162
  SNESGetConvergedReason(nl_solver_, &conv_reason);
28✔
163

164
  if (conv_reason > 0)
28✔
165
    converged_ = true;
28✔
166

167
  const char* strreason;
28✔
168
  SNESGetConvergedReasonString(nl_solver_, &strreason);
28✔
169

170
  converged_reason_string_ = std::string(strreason);
28✔
171
}
28✔
172

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

184
  return outstr.str();
×
UNCOV
185
}
×
186

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