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

Open-Sn / opensn / 22085707006

16 Feb 2026 06:17PM UTC coverage: 74.335% (-0.07%) from 74.409%
22085707006

push

github

web-flow
Merge pull request #934 from wdhawkins/keigen_fixes

Fixes for k-eigenvalue problems

148 of 219 new or added lines in 4 files covered. (67.58%)

9 existing lines in 2 files now uncovered.

19965 of 26858 relevant lines covered (74.34%)

67397443.76 hits per line

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

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

4
#include "framework/data_types/sparse_matrix/sparse_matrix.h"
5
#include "framework/logging/log.h"
6
#include "framework/runtime.h"
7
#include <iomanip>
8
#include <algorithm>
9
#include <sstream>
10

11
namespace opensn
12
{
13

14
SparseMatrix::SparseMatrix(size_t num_rows, size_t num_cols)
1,416✔
15
  : row_size_(num_rows), col_size_(num_cols)
1,416✔
16
{
17
  rowI_values.resize(num_rows, std::vector<double>());
1,416✔
18
  rowI_indices.resize(num_rows, std::vector<size_t>());
1,416✔
19
}
1,416✔
20

21
SparseMatrix::SparseMatrix(const SparseMatrix& matrix)
5,571✔
22
  : row_size_(matrix.GetNumRows()), col_size_(matrix.GetNumCols())
5,571✔
23
{
24
  rowI_values.resize(row_size_, std::vector<double>());
5,571✔
25
  rowI_indices.resize(row_size_, std::vector<size_t>());
5,571✔
26

27
  for (size_t i = 0; i < matrix.rowI_values.size(); ++i)
562,094✔
28
  {
29
    rowI_values[i] = (matrix.rowI_values[i]);
556,523✔
30
    rowI_indices[i] = (matrix.rowI_indices[i]);
556,523✔
31
  }
32
}
5,571✔
33

34
void
35
SparseMatrix::Insert(size_t i, size_t j, double value)
18,406,899✔
36
{
37
  CheckInitialized();
18,406,899✔
38

39
  if ((i < 0) or (i >= row_size_) or (j < 0) or (j >= col_size_))
18,406,899✔
40
  {
41
    std::ostringstream oss;
×
42
    oss << "SparseMatrix: Insert encountered out of bounds ("
×
43
        << "i = " << i << ", j = " << j << ", bounds = " << row_size_ << ", " << col_size_ << ")";
×
44
    throw std::runtime_error(oss.str());
×
45
  }
×
46

47
  auto relative_location = std::find(rowI_indices[i].begin(), rowI_indices[i].end(), j);
18,406,899✔
48
  bool already_there = (relative_location != rowI_indices[i].end());
18,406,899✔
49

50
  if (already_there)
18,406,899✔
51
  {
UNCOV
52
    size_t jr = relative_location - rowI_indices[i].begin();
×
UNCOV
53
    rowI_values[i][jr] = value;
×
54
  }
55
  else
56
  {
57
    rowI_values[i].push_back(value);
18,406,899✔
58
    rowI_indices[i].push_back(j);
18,406,899✔
59
  }
60
}
18,406,899✔
61

62
void
63
SparseMatrix::InsertAdd(size_t i, size_t j, double value)
35✔
64
{
65
  CheckInitialized();
35✔
66

67
  if ((i < 0) or (i >= row_size_) or (j < 0) or (j >= col_size_))
35✔
68
  {
69
    std::ostringstream oss;
×
70
    oss << "SparseMatrix: Insert encountered out of bounds ("
×
71
        << "i = " << i << ", j = " << j << ", bounds = " << row_size_ << ", " << col_size_ << ")";
×
72
    throw std::runtime_error(oss.str());
×
73
  }
×
74

75
  auto relative_location = std::find(rowI_indices[i].begin(), rowI_indices[i].end(), j);
35✔
76
  bool already_there = (relative_location != rowI_indices[i].end());
35✔
77

78
  if (already_there)
35✔
79
  {
80
    size_t jr = relative_location - rowI_indices[i].begin();
18✔
81
    rowI_values[i][jr] += value;
18✔
82
  }
83
  else
84
  {
85
    rowI_values[i].push_back(value);
17✔
86
    rowI_indices[i].push_back(j);
17✔
87
  }
88
}
35✔
89

90
void
91
SparseMatrix::SetDiagonal(const std::vector<double>& diag)
185✔
92
{
93
  CheckInitialized();
185✔
94

95
  size_t num_rows = rowI_values.size();
185✔
96
  // Check size
97
  if (diag.size() != rowI_values.size())
185✔
98
    throw std::runtime_error(
×
99
      "SparseMatrix: Incompatible matrix-vector size encountered in call to SetDiagonal");
×
100

101
  // Assign values
102
  for (size_t i = 0; i < num_rows; ++i)
372✔
103
  {
104
    auto relative_location = std::find(rowI_indices[i].begin(), rowI_indices[i].end(), i);
187✔
105
    bool already_there = (relative_location != rowI_indices[i].end());
187✔
106

107
    if (already_there)
187✔
108
    {
109
      size_t jr = relative_location - rowI_indices[i].begin();
×
110
      rowI_values[i][jr] = diag[i];
×
111
    }
112
    else
113
    {
114
      rowI_values[i].push_back(diag[i]);
187✔
115
      rowI_indices[i].push_back(i);
187✔
116
    }
117
  } // for i
118
}
185✔
119

120
double
121
SparseMatrix::GetValueIJ(size_t i, size_t j) const
16✔
122
{
123
  double retval = 0.0;
16✔
124
  if ((i < 0) or (i >= rowI_indices.size()))
16✔
125
  {
126
    std::ostringstream oss;
×
127
    oss << "SparseMatrix: Index i out of bounds in call to ValueIJ (i = " << i << ")";
×
128
    throw std::runtime_error(oss.str());
×
129
  }
×
130

131
  if (not rowI_indices[i].empty())
16✔
132
  {
133
    auto relative_location = std::find(rowI_indices[i].begin(), rowI_indices[i].end(), j);
16✔
134
    bool non_zero = (relative_location != rowI_indices[i].end());
16✔
135
    if (non_zero)
16✔
136
    {
137
      size_t jr = relative_location - rowI_indices[i].begin();
16✔
138
      retval = rowI_values[i][jr];
16✔
139
    }
140
  }
141
  return retval;
16✔
142
}
143

144
void
145
SparseMatrix::Compress()
×
146
{
147
  for (size_t i = 0; i < rowI_indices.size(); ++i)
×
148
  {
149
    auto& indices = rowI_indices[i];
×
150
    auto& values = rowI_values[i];
×
151

152
    // Copy row indexes and values into vector of pairs
153
    std::vector<std::pair<size_t, double>> target;
×
154
    target.reserve(indices.size());
×
155

156
    auto index = indices.begin();
×
157
    auto value = values.begin();
×
158
    for (; index != indices.end(); ++index, ++value)
×
159
    {
160
      target.emplace_back(*index, *value);
×
161
    }
162

163
    // Define compare operator
164
    struct
×
165
    {
166
      bool operator()(std::pair<size_t, double> a, std::pair<size_t, double> b)
×
167
      {
168
        return a.first < b.first;
×
169
      }
170
    } compare_index;
171

172
    // Sort
173
    std::stable_sort(target.begin(), target.end(), compare_index);
×
174

175
    // Copy back
176
    indices.clear();
×
177
    values.clear();
×
178
    for (auto& iv_pair : target)
×
179
    {
180
      indices.push_back(iv_pair.first);
×
181
      values.push_back(iv_pair.second);
×
182
    }
183
  }
×
184
}
×
185

186
std::string
187
SparseMatrix::PrintStr() const
×
188
{
189
  std::stringstream out;
×
190

191
  for (size_t i = 0; i < row_size_; ++i)
×
192
  {
193
    for (size_t j = 0; j < col_size_; ++j)
×
194
    {
195
      auto relative_location = std::find(rowI_indices[i].begin(), rowI_indices[i].end(), j);
×
196
      bool non_zero = (relative_location != rowI_indices[i].end());
×
197

198
      if (non_zero)
×
199
      {
200
        size_t jr = relative_location - rowI_indices[i].begin();
×
201
        out << std::setprecision(2) << std::scientific << std::setw(9) << rowI_values[i][jr] << " ";
×
202
      }
203
      else
204
      {
205
        out << std::setprecision(0) << std::fixed << std::setw(9) << 0.0 << " ";
×
206
      }
207
    }
208
    out << "\n";
×
209
  }
210

211
  return out.str();
×
212
}
×
213

214
void
215
SparseMatrix::CheckInitialized() const
18,407,119✔
216
{
217
  if (rowI_values.empty())
18,407,119✔
218
    throw std::runtime_error("SparseMatrix: Illegal call to uninitialized matrix");
×
219
}
18,407,119✔
220

221
//  Iterator routines
222

223
SparseMatrix::RowIteratorContext
224
SparseMatrix::Row(size_t row_id)
365✔
225
{
226
  return {*this, row_id};
365✔
227
}
228

229
SparseMatrix::ConstRowIteratorContext
230
SparseMatrix::Row(size_t row_id) const
2,147,483,647✔
231
{
232
  return {*this, row_id};
2,147,483,647✔
233
}
234

235
SparseMatrix::EntriesIterator
236
SparseMatrix::begin()
1✔
237
{
238
  // Find first non-empty row
239
  size_t nerow = row_size_; // nerow = non-empty row
1✔
240
  for (size_t r = 0; r < row_size_; ++r)
1✔
241
    if (not rowI_indices[r].empty())
1✔
242
    {
243
      nerow = r;
244
      break;
245
    }
246

247
  return EntriesIterator(*this, nerow);
1✔
248
}
249

250
SparseMatrix::EntriesIterator
251
SparseMatrix::end()
1✔
252
{
253
  return EntriesIterator(*this, row_size_);
1✔
254
}
255
} // 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