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

Open-Sn / opensn / 25204617443

01 May 2026 05:11AM UTC coverage: 75.417% (+0.3%) from 75.068%
25204617443

push

github

web-flow
Merge pull request #1045 from wdhawkins/upscatter_ags_update

Removing verbose_ags flag from keigenvalue_transport_3d_5g_upscatter_solvers.py

21819 of 28931 relevant lines covered (75.42%)

65656484.82 hits per line

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

86.98
/modules/linear_boltzmann_solvers/lbs_problem/vecops/lbs_vecops.cc
1
// SPDX-FileCopyrightText: 2024 The OpenSn Authors <https://open-sn.github.io/opensn/>
2
// SPDX-License-Identifier: MIT
3

4
#include "modules/linear_boltzmann_solvers/lbs_problem/vecops/lbs_vecops.h"
5
#include "modules/linear_boltzmann_solvers/lbs_problem/lbs_problem.h"
6
#include "framework/math/petsc_utils/petsc_utils.h"
7

8
namespace opensn
9
{
10

11
template <typename Functor>
12
int64_t
13
LBSVecOps::GroupsetScopedCopy(LBSProblem& lbs_problem,
206,128✔
14
                              unsigned int gsi,
15
                              unsigned int gss,
16
                              Functor func)
17
{
18
  CALI_CXX_MARK_SCOPE("LBSVecOps::GroupsetScopedCopy");
206,128✔
19

20
  const auto& grid = lbs_problem.GetGrid();
206,128✔
21
  const auto& cell_transport_views = lbs_problem.GetCellTransportViews();
206,128✔
22
  auto num_moments = lbs_problem.GetNumMoments();
206,128✔
23

24
  int64_t idx = -1;
206,128✔
25
  for (const auto& cell : grid->local_cells)
116,943,467✔
26
  {
27
    const auto& transport_view = cell_transport_views[cell.local_id];
116,737,339✔
28
    const auto num_nodes = static_cast<std::size_t>(transport_view.GetNumNodes());
116,737,339✔
29
    for (std::size_t i = 0; i < num_nodes; ++i)
626,726,371✔
30
    {
31
      for (unsigned int m = 0; m < num_moments; ++m)
2,147,483,647✔
32
      {
33
        auto mapped_idx = transport_view.MapDOF(i, m, gsi);
1,723,868,984✔
34
        for (unsigned int g = 0; g < gss; ++g)
2,147,483,647✔
35
        {
36
          ++idx;
2,147,483,647✔
37
          func(idx, mapped_idx + g);
2,147,483,647✔
38
        }
39
      }
40
    }
41
  }
42
  return idx;
412,256✔
43
}
206,128✔
44

45
void
46
LBSVecOps::SetPhiVectorScalarValues(LBSProblem& lbs_problem, PhiSTLOption phi_opt, double value)
202✔
47
{
48
  CALI_CXX_MARK_SCOPE("LBSVecOps::SetPhiVectorScalarValues");
202✔
49

50
  auto& phi = (phi_opt == PhiSTLOption::PHI_NEW) ? lbs_problem.GetPhiNewLocal()
202✔
51
                                                 : lbs_problem.GetPhiOldLocal();
202✔
52
  const auto& grid = lbs_problem.GetGrid();
202✔
53
  const auto& sdm = lbs_problem.GetSpatialDiscretization();
202✔
54
  const auto& unknown_manager = lbs_problem.GetUnknownManager();
202✔
55

56
  const auto first_grp = 0;
202✔
57
  const long final_grp = static_cast<long>(lbs_problem.GetNumGroups() - 1);
202✔
58

59
  for (const auto& cell : grid->local_cells)
19,814✔
60
  {
61
    const auto& cell_mapping = sdm.GetCellMapping(cell);
19,612✔
62
    const size_t num_nodes = cell_mapping.GetNumNodes();
19,612✔
63

64
    for (size_t i = 0; i < num_nodes; ++i)
102,128✔
65
    {
66
      const auto dof_map = static_cast<long>(sdm.MapDOFLocal(cell, i, unknown_manager, 0, 0));
82,516✔
67
      std::fill(phi.begin() + dof_map + first_grp, phi.begin() + dof_map + final_grp + 1, value);
165,032✔
68
    }
69
  }
70
}
202✔
71

72
void
73
LBSVecOps::ScalePhiVector(LBSProblem& lbs_problem, PhiSTLOption phi_opt, double value)
90✔
74
{
75
  CALI_CXX_MARK_SCOPE("LBSVecOps::ScalePhiVector");
90✔
76

77
  const auto& groupsets = lbs_problem.GetGroupsets();
90✔
78

79
  if (phi_opt == PhiSTLOption::PHI_NEW)
90✔
80
    lbs_problem.ScalePhiNew(value);
49✔
81
  else
82
    lbs_problem.ScalePhiOld(value);
41✔
83

84
  for (const auto& groupset : groupsets)
212✔
85
  {
86
    if (groupset.angle_agg)
122✔
87
    {
88
      if (phi_opt == PhiSTLOption::PHI_NEW)
122✔
89
      {
90
        auto psi = groupset.angle_agg->GetNewDelayedAngularDOFsAsSTLVector();
65✔
91
        Scale(psi, value);
65✔
92
        groupset.angle_agg->SetNewDelayedAngularDOFsFromSTLVector(psi);
65✔
93
      }
65✔
94
      else if (phi_opt == PhiSTLOption::PHI_OLD)
57✔
95
      {
96
        auto psi = groupset.angle_agg->GetOldDelayedAngularDOFsAsSTLVector();
57✔
97
        Scale(psi, value);
57✔
98
        groupset.angle_agg->SetOldDelayedAngularDOFsFromSTLVector(psi);
57✔
99
      }
57✔
100
    }
101
  }
102
}
90✔
103

104
void
105
LBSVecOps::SetGSPETScVecFromPrimarySTLvector(
89,355✔
106
  LBSProblem& lbs_problem, const LBSGroupset& groupset, Vec dest, PhiSTLOption src, int64_t offset)
107
{
108
  const auto& src_phi =
89,355✔
109
    (src == PhiSTLOption::PHI_NEW) ? lbs_problem.GetPhiNewLocal() : lbs_problem.GetPhiOldLocal();
89,355✔
110
  PetscScalar* petsc_dest = nullptr;
89,355✔
111
  OpenSnPETScCall(VecGetArray(dest, &petsc_dest));
89,355✔
112
  int64_t index = offset + GroupsetScopedCopy(lbs_problem,
89,355✔
113
                                              groupset.first_group,
89,355✔
114
                                              groupset.GetNumGroups(),
115
                                              [&](int64_t idx, size_t mapped_idx)
2,147,483,647✔
116
                                              { petsc_dest[offset + idx] = src_phi[mapped_idx]; });
2,147,483,647✔
117
  if (groupset.angle_agg)
89,355✔
118
  {
119
    if (src == PhiSTLOption::PHI_NEW)
89,355✔
120
      groupset.angle_agg->AppendNewDelayedAngularDOFsToArray(index, petsc_dest);
84,229✔
121
    else if (src == PhiSTLOption::PHI_OLD)
5,126✔
122
      groupset.angle_agg->AppendOldDelayedAngularDOFsToArray(index, petsc_dest);
5,126✔
123
  }
124
  OpenSnPETScCall(VecRestoreArray(dest, &petsc_dest));
89,355✔
125
}
89,355✔
126

127
void
128
LBSVecOps::SetPrimarySTLvectorFromGSPETScVec(
95,494✔
129
  LBSProblem& lbs_problem, const LBSGroupset& groupset, Vec src, PhiSTLOption dest, int64_t offset)
130
{
131
  auto& dest_phi =
95,494✔
132
    (dest == PhiSTLOption::PHI_NEW) ? lbs_problem.GetPhiNewLocal() : lbs_problem.GetPhiOldLocal();
95,494✔
133
  const PetscScalar* petsc_src = nullptr;
95,494✔
134
  OpenSnPETScCall(VecGetArrayRead(src, &petsc_src));
95,494✔
135
  int64_t index = offset + GroupsetScopedCopy(lbs_problem,
95,494✔
136
                                              groupset.first_group,
95,494✔
137
                                              groupset.GetNumGroups(),
138
                                              [&](int64_t idx, size_t mapped_idx)
2,147,483,647✔
139
                                              { dest_phi[mapped_idx] = petsc_src[offset + idx]; });
2,147,483,647✔
140
  if (groupset.angle_agg)
95,494✔
141
  {
142
    if (dest == PhiSTLOption::PHI_NEW)
95,494✔
143
      groupset.angle_agg->SetNewDelayedAngularDOFsFromArray(index, petsc_src);
8,320✔
144
    else if (dest == PhiSTLOption::PHI_OLD)
87,174✔
145
      groupset.angle_agg->SetOldDelayedAngularDOFsFromArray(index, petsc_src);
87,174✔
146
  }
147
  OpenSnPETScCall(VecRestoreArrayRead(src, &petsc_src));
95,494✔
148
}
95,494✔
149

150
void
151
LBSVecOps::SetGroupScopedPETScVecFromPrimarySTLvector(LBSProblem& lbs_problem,
×
152
                                                      unsigned int first_group_id,
153
                                                      unsigned int last_group_id,
154
                                                      Vec dest,
155
                                                      const std::vector<double>& src)
156
{
157
  PetscScalar* petsc_dest = nullptr;
×
158
  OpenSnPETScCall(VecGetArray(dest, &petsc_dest));
×
159
  GroupsetScopedCopy(lbs_problem,
×
160
                     first_group_id,
161
                     last_group_id - first_group_id + 1,
×
162
                     [&](int64_t idx, size_t mapped_idx) { petsc_dest[idx] = src[mapped_idx]; });
×
163
  OpenSnPETScCall(VecRestoreArray(dest, &petsc_dest));
×
164
}
×
165

166
void
167
LBSVecOps::SetPrimarySTLvectorFromGroupScopedPETScVec(LBSProblem& lbs_problem,
×
168
                                                      unsigned int first_group_id,
169
                                                      unsigned int last_group_id,
170
                                                      Vec src,
171
                                                      PhiSTLOption dest)
172
{
173
  auto& dest_phi =
×
174
    (dest == PhiSTLOption::PHI_NEW) ? lbs_problem.GetPhiNewLocal() : lbs_problem.GetPhiOldLocal();
×
175
  const PetscScalar* petsc_src = nullptr;
×
176
  OpenSnPETScCall(VecGetArrayRead(src, &petsc_src));
×
177
  GroupsetScopedCopy(lbs_problem,
×
178
                     first_group_id,
179
                     last_group_id - first_group_id + 1,
×
180
                     [&](int64_t idx, size_t mapped_idx)
×
181
                     { dest_phi[mapped_idx] = petsc_src[idx]; });
×
182
  OpenSnPETScCall(VecRestoreArrayRead(src, &petsc_src));
×
183
}
×
184

185
void
186
LBSVecOps::GSScopedCopyPrimarySTLvectors(LBSProblem& lbs_problem,
70✔
187
                                         const LBSGroupset& groupset,
188
                                         const std::vector<double>& src,
189
                                         std::vector<double>& dest)
190
{
191
  GroupsetScopedCopy(lbs_problem,
70✔
192
                     groupset.first_group,
70✔
193
                     groupset.GetNumGroups(),
194
                     [&](int64_t idx, size_t mapped_idx) { dest[mapped_idx] = src[mapped_idx]; });
930,880✔
195
}
70✔
196

197
void
198
LBSVecOps::GSScopedCopyPrimarySTLvectors(LBSProblem& lbs_problem,
21,038✔
199
                                         const LBSGroupset& groupset,
200
                                         PhiSTLOption src,
201
                                         PhiSTLOption dest)
202
{
203
  const auto& src_phi =
21,038✔
204
    (src == PhiSTLOption::PHI_NEW) ? lbs_problem.GetPhiNewLocal() : lbs_problem.GetPhiOldLocal();
21,038✔
205
  auto& dest_phi =
21,038✔
206
    (dest == PhiSTLOption::PHI_NEW) ? lbs_problem.GetPhiNewLocal() : lbs_problem.GetPhiOldLocal();
21,038✔
207
  GroupsetScopedCopy(lbs_problem,
21,038✔
208
                     groupset.first_group,
21,038✔
209
                     groupset.GetNumGroups(),
210
                     [&](int64_t idx, size_t mapped_idx)
2,147,483,647✔
211
                     { dest_phi[mapped_idx] = src_phi[mapped_idx]; });
2,147,483,647✔
212
  if (groupset.angle_agg)
21,038✔
213
  {
214
    if (src == PhiSTLOption::PHI_NEW and dest == PhiSTLOption::PHI_OLD)
21,038✔
215
      groupset.angle_agg->SetDelayedPsiNew2Old();
21,038✔
216
    else if (src == PhiSTLOption::PHI_OLD and dest == PhiSTLOption::PHI_NEW)
×
217
      groupset.angle_agg->SetDelayedPsiOld2New();
×
218
  }
219
}
21,038✔
220

221
void
222
LBSVecOps::SetMultiGSPETScVecFromPrimarySTLvector(LBSProblem& lbs_problem,
41✔
223
                                                  const std::vector<unsigned int>& groupset_ids,
224
                                                  Vec x,
225
                                                  PhiSTLOption which_phi)
226
{
227
  auto& y = (which_phi == PhiSTLOption::PHI_NEW) ? lbs_problem.GetPhiNewLocal()
41✔
228
                                                 : lbs_problem.GetPhiOldLocal();
41✔
229
  const auto& groupsets = lbs_problem.GetGroupsets();
41✔
230
  PetscScalar* x_ref = nullptr;
41✔
231
  OpenSnPETScCall(VecGetArray(x, &x_ref));
41✔
232

233
  int64_t offset = 0;
41✔
234
  for (auto gs_id : groupset_ids)
98✔
235
  {
236
    const auto& groupset = groupsets.at(gs_id);
57✔
237
    auto gsi = groupset.first_group;
57✔
238
    auto gsf = groupset.last_group;
57✔
239
    auto gss = gsf - gsi + 1;
57✔
240

241
    int64_t index = offset + GroupsetScopedCopy(lbs_problem,
57✔
242
                                                gsi,
243
                                                gss,
244
                                                [&](int64_t idx, size_t mapped_idx)
3,594,220✔
245
                                                { x_ref[offset + idx] = y[mapped_idx]; });
3,594,277✔
246
    if (groupset.angle_agg)
57✔
247
    {
248
      if (which_phi == PhiSTLOption::PHI_NEW)
57✔
249
        groupset.angle_agg->AppendNewDelayedAngularDOFsToArray(index, x_ref);
×
250
      else if (which_phi == PhiSTLOption::PHI_OLD)
57✔
251
        groupset.angle_agg->AppendOldDelayedAngularDOFsToArray(index, x_ref);
57✔
252
    }
253
    offset = index + 1;
57✔
254
  }
255

256
  OpenSnPETScCall(VecRestoreArray(x, &x_ref));
41✔
257
}
41✔
258

259
void
260
LBSVecOps::SetPrimarySTLvectorFromMultiGSPETScVec(LBSProblem& lbs_problem,
82✔
261
                                                  const std::vector<unsigned int>& groupset_ids,
262
                                                  Vec x,
263
                                                  PhiSTLOption which_phi)
264
{
265
  auto& y = (which_phi == PhiSTLOption::PHI_NEW) ? lbs_problem.GetPhiNewLocal()
82✔
266
                                                 : lbs_problem.GetPhiOldLocal();
41✔
267
  const auto& groupsets = lbs_problem.GetGroupsets();
82✔
268
  const PetscScalar* x_ref = nullptr;
82✔
269
  OpenSnPETScCall(VecGetArrayRead(x, &x_ref));
82✔
270

271
  int64_t offset = 0;
82✔
272
  for (auto gs_id : groupset_ids)
196✔
273
  {
274
    const auto& groupset = groupsets.at(gs_id);
114✔
275
    auto gsi = groupset.first_group;
114✔
276
    auto gsf = groupset.last_group;
114✔
277
    auto gss = gsf - gsi + 1;
114✔
278

279
    int64_t index = offset + GroupsetScopedCopy(lbs_problem,
114✔
280
                                                gsi,
281
                                                gss,
282
                                                [&](int64_t idx, size_t mapped_idx)
7,188,440✔
283
                                                { y[mapped_idx] = x_ref[offset + idx]; });
7,188,554✔
284
    if (groupset.angle_agg)
114✔
285
    {
286
      if (which_phi == PhiSTLOption::PHI_NEW)
114✔
287
        groupset.angle_agg->SetNewDelayedAngularDOFsFromArray(index, x_ref);
57✔
288
      else if (which_phi == PhiSTLOption::PHI_OLD)
57✔
289
        groupset.angle_agg->SetOldDelayedAngularDOFsFromArray(index, x_ref);
57✔
290
    }
291
    offset = index + 1;
114✔
292
  }
293

294
  OpenSnPETScCall(VecRestoreArrayRead(x, &x_ref));
82✔
295
}
82✔
296

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