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

Open-Sn / opensn / 18812107179

22 Oct 2025 07:55PM UTC coverage: 74.771%. Remained the same
18812107179

push

github

web-flow
Merge pull request #807 from wdhawkins/clang-tidy-init-variables

Fixing clang-tidy init-variables warnings

132 of 179 new or added lines in 52 files covered. (73.74%)

184 existing lines in 3 files now uncovered.

18203 of 24345 relevant lines covered (74.77%)

53789631.27 hits per line

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

74.43
/modules/diffusion/diffusion.cc
1
// SPDX-FileCopyrightText: 2024 The OpenSn Authors <https://open-sn.github.io/opensn/>
2
// SPDX-License-Identifier: MIT
3

4
#include "modules/diffusion/diffusion.h"
5
#include "framework/math/spatial_discretization/spatial_discretization.h"
6
#include "framework/mesh/mesh_continuum/mesh_continuum.h"
7
#include "framework/math/petsc_utils/petsc_utils.h"
8
#include "framework/logging/log.h"
9
#include "framework/runtime.h"
10

11
namespace opensn
12
{
13

14
DiffusionSolver::DiffusionSolver(std::string name,
69✔
15
                                 const opensn::SpatialDiscretization& sdm,
16
                                 const UnknownManager& uk_man,
17
                                 std::map<uint64_t, BoundaryCondition> bcs,
18
                                 MatID2XSMap map_mat_id_2_xs,
19
                                 const std::vector<UnitCellMatrices>& unit_cell_matrices,
20
                                 const bool suppress_bcs,
21
                                 const bool requires_ghosts,
22
                                 const bool verbose)
69✔
23
  : name_(std::move(name)),
×
24
    grid_(sdm.GetGrid()),
69✔
25
    sdm_(sdm),
69✔
26
    uk_man_(uk_man),
69✔
27
    bcs_(std::move(bcs)),
69✔
28
    mat_id_2_xs_map_(std::move(map_mat_id_2_xs)),
69✔
29
    unit_cell_matrices_(unit_cell_matrices),
69✔
30
    num_local_dofs_(static_cast<int64_t>(sdm_.GetNumLocalDOFs(uk_man_))),
69✔
31
    num_global_dofs_(static_cast<int64_t>(sdm_.GetNumGlobalDOFs(uk_man_))),
69✔
32
    A_(nullptr),
69✔
33
    rhs_(nullptr),
69✔
34
    ksp_(nullptr),
69✔
35
    requires_ghosts_(requires_ghosts),
69✔
36
    suppress_bcs_(suppress_bcs)
138✔
37
{
38
  options.verbose = verbose;
69✔
39
}
69✔
40

41
DiffusionSolver::~DiffusionSolver()
414✔
42
{
43
  MatDestroy(&A_);
44
  VecDestroy(&rhs_);
45
  KSPDestroy(&ksp_);
46
}
138✔
47

69✔
48
std::string
69✔
49
DiffusionSolver::GetName() const
69✔
50
{
×
51
  return name_;
×
52
}
53

54
const Vec&
55
DiffusionSolver::GetRHS() const
×
56
{
57
  return rhs_;
×
58
}
59

60
const UnknownManager&
61
DiffusionSolver::GetUnknownStructure() const
21,316✔
62
{
63
  return uk_man_;
21,316✔
64
}
65

66
const SpatialDiscretization&
67
DiffusionSolver::GetSpatialDiscretization() const
13,628✔
68
{
69
  return sdm_;
13,628✔
70
}
71

72
std::pair<size_t, size_t>
73
DiffusionSolver::GetNumPhiIterativeUnknowns()
×
74
{
75
  return {sdm_.GetNumLocalDOFs(uk_man_), sdm_.GetNumGlobalDOFs(uk_man_)};
×
76
}
77

78
void
79
DiffusionSolver::AddToRHS(const std::vector<double>& values)
128✔
80
{
81
  const auto num_local_dofs = sdm_.GetNumLocalDOFs(uk_man_);
128✔
82
  if (num_local_dofs != values.size())
128✔
83
    throw std::invalid_argument("Vector size mismatch.");
×
84

85
  PetscScalar* rhs_ptr = nullptr;
128✔
86
  VecGetArray(rhs_, &rhs_ptr);
128✔
87
  for (size_t i = 0; i < num_local_dofs; ++i)
2,688,128✔
88
    rhs_ptr[i] += values[i];
2,688,000✔
89
  VecRestoreArray(rhs_, &rhs_ptr);
128✔
90
}
128✔
91

92
void
93
DiffusionSolver::AddToMatrix(const std::vector<PetscInt>& rows,
4✔
94
                             const std::vector<PetscInt>& cols,
95
                             const std::vector<double>& vals)
96
{
97
  if (rows.size() != cols.size() or rows.size() != vals.size())
4✔
98
    throw std::invalid_argument("The number of row entries, column entries, and value "
×
99
                                "entries do not agree.");
×
100
  for (int i = 0; i < vals.size(); ++i)
4✔
101
    MatSetValue(A_, rows[i], cols[i], vals[i], ADD_VALUES);
×
102
  MatAssemblyBegin(A_, MAT_FLUSH_ASSEMBLY);
4✔
103
  MatAssemblyEnd(A_, MAT_FLUSH_ASSEMBLY);
4✔
104
}
4✔
105

106
void
107
DiffusionSolver::Initialize()
69✔
108
{
109
  if (options.verbose)
69✔
110
    log.Log() << name_ << ": Initializing PETSc items";
12✔
111

112
  if (options.verbose)
69✔
113
    log.Log() << name_ << ": Global number of DOFs=" << num_global_dofs_;
12✔
114

115
  opensn::mpi_comm.barrier();
69✔
116
  log.Log() << "Sparsity pattern";
138✔
117
  opensn::mpi_comm.barrier();
69✔
118
  // Create Matrix
119
  std::vector<int64_t> nodal_nnz_in_diag;
69✔
120
  std::vector<int64_t> nodal_nnz_off_diag;
69✔
121
  sdm_.BuildSparsityPattern(nodal_nnz_in_diag, nodal_nnz_off_diag, uk_man_);
69✔
122
  opensn::mpi_comm.barrier();
69✔
123
  log.Log() << "Done Sparsity pattern";
138✔
124
  opensn::mpi_comm.barrier();
69✔
125
  A_ = CreateSquareMatrix(num_local_dofs_, num_global_dofs_);
69✔
126
  InitMatrixSparsity(A_, nodal_nnz_in_diag, nodal_nnz_off_diag);
69✔
127
  opensn::mpi_comm.barrier();
69✔
128
  log.Log() << "Done matrix creation";
138✔
129
  opensn::mpi_comm.barrier();
69✔
130

131
  // Create RHS
132
  if (not requires_ghosts_)
69✔
133
    rhs_ = CreateVector(num_local_dofs_, num_global_dofs_);
64✔
134
  else
135
  {
136
    auto ghost_ids = sdm_.GetGhostDOFIndices(uk_man_);
5✔
137
    std::vector<int64_t> ghids(ghost_ids.begin(), ghost_ids.end());
5✔
138
    rhs_ = CreateVectorWithGhosts(num_local_dofs_,
5✔
139
                                  num_global_dofs_,
5✔
140
                                  static_cast<int64_t>(sdm_.GetNumGhostDOFs(uk_man_)),
5✔
141
                                  ghids);
142
  }
5✔
143

144
  opensn::mpi_comm.barrier();
69✔
145
  log.Log() << "Done vector creation";
138✔
146
  opensn::mpi_comm.barrier();
69✔
147

148
  // Create KSP
149
  KSPCreate(opensn::mpi_comm, &ksp_);
69✔
150
  KSPSetOptionsPrefix(ksp_, name_.c_str());
69✔
151
  KSPSetType(ksp_, KSPCG);
69✔
152

153
  KSPSetTolerances(ksp_, 1.0e-50, options.residual_tolerance, 1.0e50, options.max_iters);
69✔
154

155
  // Set Pre-conditioner
156
  PC pc = nullptr;
69✔
157
  KSPGetPC(ksp_, &pc);
69✔
158
  //  PCSetType(pc, PCGAMG);
159
  PCSetType(pc, PCHYPRE);
69✔
160

161
  PCHYPRESetType(pc, "boomeramg");
69✔
162
  std::vector<std::string> pc_options = {"pc_hypre_boomeramg_agg_nl 1",
69✔
163
                                         "pc_hypre_boomeramg_P_max 4",
164
                                         "pc_hypre_boomeramg_grid_sweeps_coarse 1",
165
                                         "pc_hypre_boomeramg_max_levels 25",
166
                                         "pc_hypre_boomeramg_relax_type_all symmetric-SOR/Jacobi",
167
                                         "pc_hypre_boomeramg_coarsen_type HMIS",
168
                                         "pc_hypre_boomeramg_interp_type ext+i"};
69✔
169

170
  if (grid_->GetDimension() == 2)
69✔
171
    pc_options.emplace_back("pc_hypre_boomeramg_strong_threshold 0.6");
38✔
172
  else if (grid_->GetDimension() == 3)
31✔
173
    pc_options.emplace_back("pc_hypre_boomeramg_strong_threshold 0.8");
16✔
174

175
  for (const auto& option : pc_options)
606✔
176
    PetscOptionsInsertString(nullptr, ("-" + name_ + option).c_str());
1,074✔
177

178
  PetscOptionsInsertString(nullptr, options.additional_options_string.c_str());
69✔
179

180
  PCSetFromOptions(pc);
69✔
181
  KSPSetFromOptions(ksp_);
69✔
182
}
69✔
183

184
void
185
DiffusionSolver::Solve(std::vector<double>& solution, bool use_initial_guess)
9,098✔
186
{
187
  const std::string fname = "acceleration::DiffusionMIPSolver::Solve";
9,098✔
188
  Vec x = nullptr;
9,098✔
189
  VecDuplicate(rhs_, &x);
9,098✔
190
  VecSet(x, 0.0);
9,098✔
191

192
  if (not use_initial_guess)
9,098✔
193
    KSPSetInitialGuessNonzero(ksp_, PETSC_FALSE);
6,610✔
194
  else
195
    KSPSetInitialGuessNonzero(ksp_, PETSC_TRUE);
2,488✔
196

197
  KSPSetTolerances(
9,098✔
198
    ksp_, options.residual_tolerance, options.residual_tolerance, 1.0e50, options.max_iters);
9,098✔
199

200
  if (options.perform_symmetry_check)
9,098✔
201
  {
202
    PetscBool symmetry = PETSC_FALSE;
4✔
203
    MatIsSymmetric(A_, 1.0e-6, &symmetry);
4✔
204
    if (symmetry == PETSC_FALSE)
4✔
205
      throw std::logic_error(fname + ":Symmetry check failed");
×
206
  }
207

208
  if (options.verbose)
9,098✔
209
  {
210
    KSPMonitorSet(ksp_, &KSPMonitorRelativeToRHS, nullptr, nullptr);
132✔
211

212
    double rhs_norm = 0.0;
132✔
213
    VecNorm(rhs_, NORM_2, &rhs_norm);
132✔
214
    log.Log() << "RHS-norm " << rhs_norm;
396✔
215
  }
216

217
  if (use_initial_guess)
9,098✔
218
  {
219
    double* x_raw = nullptr;
2,488✔
220
    VecGetArray(x, &x_raw);
2,488✔
221
    size_t k = 0;
2,488✔
222
    for (const auto& value : solution)
10,242,488✔
223
      x_raw[k++] = value;
10,240,000✔
224
    VecRestoreArray(x, &x_raw);
2,488✔
225
  }
226

227
  // Solve
228
  KSPSolve(ksp_, rhs_, x);
9,098✔
229

230
  // Print convergence info
231
  if (options.verbose)
9,098✔
232
  {
233
    double sol_norm = 0.0;
132✔
234
    VecNorm(x, NORM_2, &sol_norm);
132✔
235
    log.Log() << "Solution-norm " << sol_norm;
396✔
236

237
    KSPConvergedReason reason = KSP_CONVERGED_ITERATING;
132✔
238
    KSPGetConvergedReason(ksp_, &reason);
132✔
239

240
    log.Log() << "Convergence Reason: " << GetPETScConvergedReasonstring(reason);
396✔
241
  }
242

243
  // Transfer petsc solution to vector
244
  if (requires_ghosts_)
9,098✔
245
  {
246
    CommunicateGhostEntries(x);
130✔
247
    sdm_.LocalizePETScVectorWithGhosts(x, solution, uk_man_);
130✔
248
  }
249
  else
250
    sdm_.LocalizePETScVector(x, solution, uk_man_);
8,968✔
251

252
  // Cleanup x
253
  VecDestroy(&x);
9,098✔
254
}
9,098✔
255

256
void
257
DiffusionSolver::Solve(Vec petsc_solution, bool use_initial_guess)
×
258
{
259
  const std::string fname = "acceleration::DiffusionMIPSolver::Solve";
×
NEW
260
  Vec x = nullptr;
×
261
  VecDuplicate(rhs_, &x);
×
262
  VecSet(x, 0.0);
×
263

264
  if (not use_initial_guess)
×
265
    KSPSetInitialGuessNonzero(ksp_, PETSC_FALSE);
×
266
  else
267
    KSPSetInitialGuessNonzero(ksp_, PETSC_TRUE);
×
268

269
  KSPSetTolerances(
×
270
    ksp_, options.residual_tolerance, options.residual_tolerance, 1.0e50, options.max_iters);
×
271

272
  if (options.perform_symmetry_check)
×
273
  {
274
    PetscBool symmetry = PETSC_FALSE;
×
275
    MatIsSymmetric(A_, 1.0e-6, &symmetry);
×
276
    if (symmetry == PETSC_FALSE)
×
277
      throw std::logic_error(fname + ":Symmetry check failed");
×
278
  }
279

280
  if (options.verbose)
×
281
  {
282
    KSPMonitorSet(ksp_, &KSPMonitorRelativeToRHS, nullptr, nullptr);
×
283

NEW
284
    double rhs_norm = 0.0;
×
285
    VecNorm(rhs_, NORM_2, &rhs_norm);
×
286
    log.Log() << "RHS-norm " << rhs_norm;
×
287
  }
288

289
  if (use_initial_guess)
×
290
  {
291
    VecCopy(petsc_solution, x);
×
292
  }
293

294
  // Solve
295
  KSPSolve(ksp_, rhs_, x);
×
296

297
  // Print convergence info
298
  if (options.verbose)
×
299
  {
NEW
300
    double sol_norm = 0.0;
×
301
    VecNorm(x, NORM_2, &sol_norm);
×
302
    log.Log() << "Solution-norm " << sol_norm;
×
303

NEW
304
    KSPConvergedReason reason = KSP_CONVERGED_ITERATING;
×
305
    KSPGetConvergedReason(ksp_, &reason);
×
306

307
    log.Log() << "Convergence Reason: " << GetPETScConvergedReasonstring(reason);
×
308
  }
309

310
  // Transfer petsc solution to vector
311
  VecCopy(x, petsc_solution);
×
312

313
  // Cleanup x
314
  VecDestroy(&x);
×
315
}
×
316

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