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

Open-Sn / opensn / 25149738188

27 Apr 2026 05:34PM UTC coverage: 75.068%. Remained the same
25149738188

push

github

web-flow
Merge pull request #1033 from wdhawkins/iteration_logging

Improvements for iteration logging

364 of 485 new or added lines in 29 files covered. (75.05%)

478 existing lines in 26 files now uncovered.

21700 of 28907 relevant lines covered (75.07%)

65083867.21 hits per line

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

91.01
/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

8
namespace opensn
9
{
10

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

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

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

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

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

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

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

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

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

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

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

100
  OpenSnPETScCall(SNESCreate(mpi_comm, &nl_solver_));
37✔
101

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

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

111
  KSP ksp = nullptr;
37✔
112
  OpenSnPETScCall(SNESGetKSP(nl_solver_, &ksp));
37✔
113
  OpenSnPETScCall(KSPSetType(ksp, options_.l_method.c_str()));
37✔
114

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

117
  ApplyToleranceOptions();
37✔
118

119
  SetOptions();
37✔
120
  SetSolverContext();
37✔
121
  SetConvergenceTest();
37✔
122
  SetMonitor();
37✔
123

124
  SetSystemSize();
37✔
125
  SetSystem();
37✔
126

127
  SetFunction();
37✔
128
  SetJacobian();
37✔
129

130
  SetPreconditioner();
37✔
131

132
  PostSetupCallback();
37✔
133
  system_set_ = true;
37✔
134
}
135

136
void
137
PETScNonLinearSolver::PreSolveCallback()
37✔
138
{
139
}
37✔
140

141
void
142
PETScNonLinearSolver::PostSolveCallback()
×
143
{
UNCOV
144
}
×
145

146
void
147
PETScNonLinearSolver::Solve()
37✔
148
{
149
  converged_ = false;
37✔
150
  PreSolveCallback();
37✔
151
  SetInitialGuess();
37✔
152

153
  OpenSnPETScCall(SNESSolve(nl_solver_, nullptr, x_));
37✔
154
  PostSolveCallback();
37✔
155

156
  SNESConvergedReason conv_reason = SNES_CONVERGED_ITERATING;
37✔
157
  OpenSnPETScCall(SNESGetConvergedReason(nl_solver_, &conv_reason));
37✔
158

159
  if (SNESReasonToPETScSolverStatus(conv_reason) == PETScSolverStatus::CONVERGED)
37✔
160
    converged_ = true;
37✔
161
}
37✔
162

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