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

Open-Sn / opensn / 18928565313

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

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%)

53868061.69 hits per line

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

50.51
/framework/math/petsc_utils/petsc_utils.cc
1
// SPDX-FileCopyrightText: 2024 The OpenSn Authors <https://open-sn.github.io/opensn/>
2
// SPDX-License-Identifier: MIT
3

4
#include "framework/math/petsc_utils/petsc_utils.h"
5
#include "framework/data_types/parallel_vector/parallel_vector.h"
6
#include "framework/runtime.h"
7
#include "framework/logging/log.h"
8
#include <iomanip>
9

10
namespace opensn
11
{
12

13
Vec
14
CreateVector(int64_t local_size, int64_t global_size)
513✔
15
{
16
  Vec x = nullptr;
513✔
17
  VecCreate(opensn::mpi_comm, &x);
513✔
18
  VecSetType(x, VECMPI);
513✔
19
  VecSetSizes(x, local_size, global_size);
513✔
20
  VecSetOption(x, VEC_IGNORE_NEGATIVE_INDICES, PETSC_TRUE);
513✔
21

22
  return x;
513✔
23
}
24

25
void
26
CreateVector(Vec& x, int64_t local_size, int64_t global_size)
×
27
{
28
  VecCreate(opensn::mpi_comm, &x);
×
29
  VecSetType(x, VECMPI);
×
30
  VecSetSizes(x, local_size, global_size);
×
31
  VecSetOption(x, VEC_IGNORE_NEGATIVE_INDICES, PETSC_TRUE);
×
32
}
×
33

34
Vec
35
CreateVectorWithGhosts(int64_t local_size,
5✔
36
                       int64_t global_size,
37
                       int64_t nghosts,
38
                       const std::vector<PetscInt>& ghost_indices)
39
{
40
  Vec x = nullptr;
5✔
41
  VecCreateGhost(opensn::mpi_comm,
5✔
42
                 local_size,
43
                 global_size,
44
                 nghosts,
45
                 (ghost_indices.empty()) ? NULL : ghost_indices.data(),
5✔
46
                 &x);
47

48
  VecSetOption(x, VEC_IGNORE_NEGATIVE_INDICES, PETSC_TRUE);
5✔
49

50
  return x;
5✔
51
}
52

53
Mat
54
CreateSquareMatrix(int64_t local_size, int64_t global_size)
134✔
55
{
56
  Mat A = nullptr;
134✔
57
  MatCreate(opensn::mpi_comm, &A);
134✔
58
  MatSetType(A, MATMPIAIJ);
134✔
59
  MatSetSizes(A, local_size, local_size, global_size, global_size);
134✔
60

61
  MatMPIAIJSetPreallocation(A, 1, nullptr, 0, nullptr);
134✔
62
  MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
134✔
63
  MatSetOption(A, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);
134✔
64

65
  return A;
134✔
66
}
67

68
void
69
CreateSquareMatrix(Mat& A, int64_t local_size, int64_t global_size)
×
70
{
71
  MatCreate(opensn::mpi_comm, &A);
×
72
  MatSetType(A, MATMPIAIJ);
×
73
  MatSetSizes(A, local_size, local_size, global_size, global_size);
×
74

75
  MatMPIAIJSetPreallocation(A, 1, nullptr, 0, nullptr);
×
76
  MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
×
77
  MatSetOption(A, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);
×
78
}
×
79

80
void
81
InitMatrixSparsity(Mat& A,
134✔
82
                   const std::vector<int64_t>& nodal_nnz_in_diag,
83
                   const std::vector<int64_t>& nodal_nnz_off_diag)
84
{
85
  MatMPIAIJSetPreallocation(A, 0, nodal_nnz_in_diag.data(), 0, nodal_nnz_off_diag.data());
134✔
86
  MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
134✔
87
  MatSetOption(A, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);
134✔
88
  MatSetUp(A);
134✔
89
}
134✔
90

91
void
92
InitMatrixSparsity(Mat& A, int64_t nodal_nnz_in_diag, int64_t nodal_nnz_off_diag)
×
93
{
94
  MatMPIAIJSetPreallocation(A, nodal_nnz_in_diag, nullptr, nodal_nnz_off_diag, nullptr);
×
95
  MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
×
96
  MatSetOption(A, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);
×
97
}
×
98

99
PETScSolverSetup
100
CreateCommonKrylovSolverSetup(Mat matrix,
65✔
101
                              const std::string& solver_name,
102
                              const std::string& solver_type,
103
                              const std::string& preconditioner_type,
104
                              double rel_tol,
105
                              double abs_tol,
106
                              int64_t maximum_iterations)
107
{
108
  PETScSolverSetup setup;
65✔
109

110
  KSPCreate(opensn::mpi_comm, &setup.ksp);
65✔
111
  KSPSetOperators(setup.ksp, matrix, matrix);
65✔
112
  KSPSetType(setup.ksp, solver_type.c_str());
65✔
113

114
  KSPSetOptionsPrefix(setup.ksp, solver_name.c_str());
65✔
115

116
  KSPGetPC(setup.ksp, &setup.pc);
65✔
117
  PCSetType(setup.pc, preconditioner_type.c_str());
65✔
118

119
  KSPSetTolerances(setup.ksp, rel_tol, abs_tol, 1.0e50, maximum_iterations);
65✔
120
  KSPSetInitialGuessNonzero(setup.ksp, PETSC_TRUE);
65✔
121

122
  KSPSetFromOptions(setup.ksp);
65✔
123

124
  KSPMonitorSet(setup.ksp, &KSPMonitorRelativeToRHS, nullptr, nullptr);
65✔
125

126
  return setup;
65✔
127
}
×
128

129
PetscErrorCode
130
KSPMonitorRelativeToRHS(KSP ksp, PetscInt n, PetscReal rnorm, void* /* context */)
2,198✔
131
{
132
  Vec Rhs = nullptr;
2,198✔
133
  KSPGetRhs(ksp, &Rhs);
2,198✔
134
  double rhs_norm = 0.0;
2,198✔
135
  VecNorm(Rhs, NORM_2, &rhs_norm);
2,198✔
136
  if (rhs_norm < 1.0e-12)
2,198✔
137
    rhs_norm = 1.0;
×
138

139
  // Get solver name
140
  const char* ksp_name = nullptr;
2,198✔
141
  KSPGetOptionsPrefix(ksp, &ksp_name);
2,198✔
142

143
  // Default to this if ksp_name is NULL
144
  const char NONAME_SOLVER[] = "NoName-Solver\0";
2,198✔
145

146
  if (ksp_name == nullptr)
2,198✔
147
    ksp_name = NONAME_SOLVER;
×
148

149
  // Print message
150
  std::stringstream buff;
2,198✔
151
  buff << ksp_name << " iteration " << std::setw(4) << n << " - Residual " << std::scientific
2,198✔
152
       << std::setprecision(7) << rnorm / rhs_norm << std::endl;
2,198✔
153

154
  log.Log() << buff.str();
6,594✔
155

156
  return 0;
2,198✔
157
}
2,198✔
158

159
void
160
CopyVecToSTLvector(Vec x, std::vector<double>& data, size_t N, bool resize_STL)
9,033✔
161
{
162
  if (resize_STL)
9,033✔
163
  {
164
    data.clear();
9,033✔
165
    data.assign(N, 0.0);
9,033✔
166
  }
167
  else
168
    OpenSnLogicalErrorIf(data.size() < N,
×
169
                         "data.size() < N, " + std::to_string(data.size()) + " < " +
170
                           std::to_string(N));
171

172
  const double* x_ref = nullptr;
9,033✔
173
  VecGetArrayRead(x, &x_ref);
9,033✔
174

175
  std::copy(x_ref, x_ref + N, data.begin());
9,033✔
176

177
  VecRestoreArrayRead(x, &x_ref);
9,033✔
178
}
9,033✔
179

180
void
181
CopyVecToSTLvectorWithGhosts(Vec x, std::vector<double>& data, size_t N, bool resize_STL)
130✔
182
{
183
  if (resize_STL)
130✔
184
  {
185
    data.clear();
130✔
186
    data.assign(N, 0.0);
130✔
187
  }
188
  else
189
    OpenSnLogicalErrorIf(data.size() != N,
×
190
                         "data.size() != N, " + std::to_string(data.size()) + " < " +
191
                           std::to_string(N));
192

193
  auto info = GetGhostVectorLocalViewRead(x);
130✔
194
  const double* x_ref = info.x_localized_raw;
130✔
195

196
  std::copy(x_ref, x_ref + N, data.begin());
130✔
197

198
  RestoreGhostVectorLocalViewRead(x, info);
130✔
199
}
130✔
200

201
void
202
CopySTLvectorToVec(const std::vector<double>& data, Vec x, size_t N)
×
203
{
NEW
204
  double* x_ref = nullptr;
×
205
  VecGetArray(x, &x_ref);
×
206

207
  std::copy(data.begin(), data.end(), x_ref);
×
208

209
  VecRestoreArray(x, &x_ref);
×
210
}
×
211

212
void
213
CopyParallelVectorToVec(const ParallelVector& y, Vec x)
×
214
{
215
  const double* y_data = y.GetData();
×
NEW
216
  double* x_data = nullptr;
×
217
  VecGetArray(x, &x_data);
×
218
  std::copy(y_data, y_data + y.GetLocalSize(), x_data);
×
219
  VecRestoreArray(x, &x_data);
×
220
}
×
221

222
void
223
CopyGlobalVecToSTLvector(Vec x,
×
224
                         const std::vector<int64_t>& global_indices,
225
                         std::vector<double>& data)
226
{
227
  // Populating local indices
228
  auto N = static_cast<PetscInt>(global_indices.size());
×
229
  std::vector<int64_t> local_indices(N, 0);
×
230
  for (PetscInt counter = 0; counter < N; ++counter)
×
231
    local_indices[counter] = counter;
×
232

233
  // Creating PETSc vector
NEW
234
  Vec local_vec = nullptr;
×
235
  VecCreateSeq(PETSC_COMM_SELF, N + 1, &local_vec);
×
236
  VecSet(local_vec, 0.0);
×
237

238
  // Create and transfer index sets
NEW
239
  IS global_set = nullptr;
×
NEW
240
  IS local_set = nullptr;
×
241
  ISCreateGeneral(PETSC_COMM_SELF, N, global_indices.data(), PETSC_COPY_VALUES, &global_set);
×
242
  ISCreateGeneral(PETSC_COMM_SELF, N, local_indices.data(), PETSC_COPY_VALUES, &local_set);
×
NEW
243
  VecScatter scat = nullptr;
×
244
  VecScatterCreate(x, global_set, local_vec, local_set, &scat);
×
245
  VecScatterBegin(scat, x, local_vec, INSERT_VALUES, SCATTER_FORWARD);
×
246
  VecScatterEnd(scat, x, local_vec, INSERT_VALUES, SCATTER_FORWARD);
×
247

248
  // Copy to STL
249
  data.clear();
×
250
  data.resize(N, 0.0);
×
NEW
251
  const double* x_ref = nullptr;
×
252
  VecGetArrayRead(local_vec, &x_ref);
×
253

254
  std::copy(x_ref, x_ref + N, data.begin());
×
255

256
  VecRestoreArrayRead(x, &x_ref);
×
257

258
  // Cleanup
259
  ISDestroy(&global_set);
×
260
  ISDestroy(&local_set);
×
261

262
  VecDestroy(&local_vec);
×
263
}
×
264

265
void
266
CommunicateGhostEntries(Vec x)
130✔
267
{
268
  VecGhostUpdateBegin(x, INSERT_VALUES, SCATTER_FORWARD);
130✔
269
  VecGhostUpdateEnd(x, INSERT_VALUES, SCATTER_FORWARD);
130✔
270
}
130✔
271

272
GhostVecLocalRaw
273
GetGhostVectorLocalViewRead(Vec x)
130✔
274
{
275
  Vec x_localized = nullptr;
130✔
276
  VecGhostGetLocalForm(x, &x_localized);
130✔
277
  const double* x_localized_raw = nullptr;
130✔
278

279
  VecGetArrayRead(x_localized, &x_localized_raw);
130✔
280

281
  GhostVecLocalRaw local_data;
130✔
282
  local_data.x_localized = x_localized;
130✔
283
  local_data.x_localized_raw = (double*)x_localized_raw;
130✔
284

285
  return local_data;
130✔
286
}
287

288
void
289
RestoreGhostVectorLocalViewRead(Vec x, GhostVecLocalRaw& local_data)
130✔
290
{
291
  VecRestoreArrayRead(local_data.x_localized, (const double**)&local_data.x_localized_raw);
130✔
292
  VecGhostRestoreLocalForm(x, &local_data.x_localized);
130✔
293
}
130✔
294

295
std::string
296
GetPETScConvergedReasonstring(KSPConvergedReason reason)
132✔
297
{
298
  std::stringstream ostr;
132✔
299
  switch (reason)
132✔
300
  {
301
    case KSP_CONVERGED_RTOL_NORMAL:
×
302
      ostr << "KSP_CONVERGED_RTOL_NORMAL";
×
303
      break;
304
    case KSP_CONVERGED_ATOL_NORMAL:
×
305
      ostr << "KSP_CONVERGED_ATOL_NORMAL";
×
306
      break;
307
    case KSP_CONVERGED_RTOL:
132✔
308
      ostr << "KSP_CONVERGED_RTOL";
132✔
309
      break;
310
    case KSP_CONVERGED_ATOL:
×
311
      ostr << "KSP_CONVERGED_ATOL";
×
312
      break;
313
    case KSP_CONVERGED_ITS:
×
314
      ostr << "KSP_CONVERGED_ITS";
×
315
      break;
316
#if PETSC_VERSION_LT(3, 19, 0)
317
    case KSP_CONVERGED_CG_NEG_CURVE:
318
      ostr << "KSP_CONVERGED_CG_NEG_CURVE";
319
      break;
320
#else
321
    case KSP_CONVERGED_NEG_CURVE:
×
322
      ostr << "KSP_CONVERGED_NEG_CURVE";
×
323
      break;
324
#endif
325
#if PETSC_VERSION_LT(3, 19, 0)
326
    case KSP_CONVERGED_CG_CONSTRAINED:
327
      ostr << "KSP_CONVERGED_CG_CONSTRAINED";
328
      break;
329
#endif
330
    case KSP_CONVERGED_STEP_LENGTH:
×
331
      ostr << "KSP_CONVERGED_STEP_LENGTH";
×
332
      break;
333
    case KSP_CONVERGED_HAPPY_BREAKDOWN:
×
334
      ostr << "KSP_CONVERGED_HAPPY_BREAKDOWN";
×
335
      break;
336
      /* diverged */
337
    case KSP_DIVERGED_NULL:
×
338
      ostr << "KSP_DIVERGED_NULL";
×
339
      break;
340
    case KSP_DIVERGED_ITS:
×
341
      ostr << "KSP_DIVERGED_ITS";
×
342
      break;
343
    case KSP_DIVERGED_DTOL:
×
344
      ostr << "KSP_DIVERGED_DTOL";
×
345
      break;
346
    case KSP_DIVERGED_BREAKDOWN:
×
347
      ostr << "KSP_DIVERGED_BREAKDOWN";
×
348
      break;
349
    case KSP_DIVERGED_BREAKDOWN_BICG:
×
350
      ostr << "KSP_DIVERGED_BREAKDOWN_BICG";
×
351
      break;
352
    case KSP_DIVERGED_NONSYMMETRIC:
×
353
      ostr << "KSP_DIVERGED_NONSYMMETRIC";
×
354
      break;
355
    case KSP_DIVERGED_INDEFINITE_PC:
×
356
      ostr << "KSP_DIVERGED_INDEFINITE_PC";
×
357
      break;
358
    case KSP_DIVERGED_NANORINF:
×
359
      ostr << "KSP_DIVERGED_NANORINF";
×
360
      break;
361
    case KSP_DIVERGED_INDEFINITE_MAT:
×
362
      ostr << "KSP_DIVERGED_INDEFINITE_MAT";
×
363
      break;
364

365
    default:
×
366
      ostr << "Unknown convergence reason.";
×
367
  }
368

369
  return ostr.str();
264✔
370
}
132✔
371

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