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

Open-Sn / opensn / 20185909776

12 Dec 2025 06:55PM UTC coverage: 74.333% (+0.3%) from 74.037%
20185909776

push

github

web-flow
Merge pull request #859 from wdhawkins/td_source_driver

Adding time-dependent solver and time-dependent sources

367 of 398 new or added lines in 23 files covered. (92.21%)

113 existing lines in 28 files now uncovered.

18610 of 25036 relevant lines covered (74.33%)

68947552.69 hits per line

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

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

4
#pragma once
5

6
#include "framework/math/math.h"
7

8
namespace opensn
9
{
10

11
/**
12
 * Sparse matrix utility. This is a basic CSR type sparse matrix which allows efficient matrix
13
 * storage and multiplication. It is not intended for solving linear systems (use PETSc for that
14
 * instead). It was originally developed for the transfer matrices of transport cross sections.
15
 */
16
class SparseMatrix
17
{
18
private:
19
  /// Maximum number of rows for this matrix
20
  size_t row_size_;
21
  /// Maximum number of columns for this matrix
22
  size_t col_size_;
23

24
public:
25
  /// rowI_indices[i] is a vector indices j for the non-zero columns.
26
  std::vector<std::vector<size_t>> rowI_indices;
27
  /// rowI_values[i] corresponds to column indices and contains the non-zero value.
28
  std::vector<std::vector<double>> rowI_values;
29

30
public:
31
  /// Constructor with number of rows and columns constructor.
32
  SparseMatrix(size_t num_rows, size_t num_cols);
33
  /// Copy constructor.
34
  SparseMatrix(const SparseMatrix& matrix);
35

36
  virtual ~SparseMatrix() = default;
6,226✔
37

38
  size_t GetNumRows() const { return row_size_; }
5,154,732✔
39
  size_t GetNumCols() const { return col_size_; }
40

41
  /// Inserts a value into the matrix.
42
  void Insert(size_t i, size_t j, double value);
43

44
  /// Inserts-Adds a value into the matrix with duplicate check.
45
  void InsertAdd(size_t i, size_t j, double value);
46

47
  /**
48
   * Returns the value in the matrix at the given location. This is a rather inefficient routine.
49
   * Use the columns and values rather than directly this function.
50
   */
51
  double GetValueIJ(size_t i, size_t j) const;
52

53
  /// Sets the diagonal of the matrix using a vector.
54
  void SetDiagonal(const std::vector<double>& diag);
55

56
  /// Sorts the column indices of each row for faster lookup.
57
  void Compress();
58

59
  /// Prints the sparse matrix to string.
60
  std::string PrintStr() const;
61

62
private:
63
  /// Constructor with number of rows constructor.
64
  void CheckInitialized() const;
65

66
public:
67
  struct EntryReference
68
  {
69
    const size_t& row_index;
70
    const size_t& column_index;
71
    double& value;
72

73
    EntryReference(const size_t& row_id, const size_t& column_id, double& value)
3,767✔
74
      : row_index(row_id), column_index(column_id), value(value)
3,770✔
75
    {
76
    }
77
  };
78

79
  struct ConstEntryReference
80
  {
81
  public:
82
    const size_t& row_index;
83
    const size_t& column_index;
84
    const double& value;
85

86
    ConstEntryReference(const size_t& row_id, const size_t& column_id, const double& value)
87
      : row_index(row_id), column_index(column_id), value(value)
88
    {
89
    }
90
  };
91

92
  class RowIteratorContext
93
  {
94
  private:
95
    const std::vector<size_t>& ref_col_ids_;
96
    std::vector<double>& ref_col_vals_;
97
    const size_t ref_row_;
98

99
  public:
100
    RowIteratorContext(SparseMatrix& matrix, size_t ref_row)
101
      : ref_col_ids_(matrix.rowI_indices[ref_row]),
102
        ref_col_vals_(matrix.rowI_values[ref_row]),
103
        ref_row_(ref_row)
104
    {
105
    }
106

107
    class RowIterator
108
    {
109
    private:
110
      RowIteratorContext& context_;
111
      size_t ref_entry_;
112

113
    public:
114
      RowIterator(RowIteratorContext& context, size_t ref_entry)
349✔
115
        : context_{context}, ref_entry_{ref_entry}
116
      {
117
      }
118

119
      RowIterator operator++()
3,773✔
120
      {
121
        RowIterator i = *this;
3,773✔
122
        ref_entry_++;
3,773✔
123
        return i;
3,773✔
124
      }
125
      RowIterator operator++(int)
126
      {
127
        ref_entry_++;
128
        return *this;
129
      }
130

131
      EntryReference operator*()
3,773✔
132
      {
133
        return {
3,773✔
134
          context_.ref_row_, context_.ref_col_ids_[ref_entry_], context_.ref_col_vals_[ref_entry_]};
3,773✔
135
      }
136

137
      bool operator==(const RowIterator& rhs) const { return ref_entry_ == rhs.ref_entry_; }
138
      bool operator!=(const RowIterator& rhs) const { return ref_entry_ != rhs.ref_entry_; }
139
    };
140

141
    RowIterator begin() { return {*this, 0}; }
349✔
142
    RowIterator end() { return {*this, ref_col_vals_.size()}; }
349✔
143
  };
5,132✔
144

349✔
145
  RowIteratorContext Row(size_t row_id);
349✔
146

349✔
147
  class ConstRowIteratorContext
349✔
148
  {
149
  private:
150
    const std::vector<size_t>& ref_col_ids_;
151
    const std::vector<double>& ref_col_vals_;
152
    const size_t ref_row_;
153

154
  public:
155
    ConstRowIteratorContext(const SparseMatrix& matrix, size_t ref_row)
2,147,483,647✔
156
      : ref_col_ids_(matrix.rowI_indices[ref_row]),
2,147,483,647✔
157
        ref_col_vals_(matrix.rowI_values[ref_row]),
2,147,483,647✔
158
        ref_row_(ref_row)
2,147,483,647✔
159
    {
160
    }
161

162
    class ConstRowIterator
163
    {
164
    private:
165
      const ConstRowIteratorContext& context_;
166
      size_t ref_entry_;
167

168
    public:
169
      ConstRowIterator(const ConstRowIteratorContext& context, size_t ref_entry)
170
        : context_(context), ref_entry_{ref_entry}
171
      {
172
      }
173

174
      ConstRowIterator operator++()
175
      {
176
        ConstRowIterator i = *this;
177
        ref_entry_++;
178
        return i;
179
      }
180
      ConstRowIterator operator++(int)
181
      {
182
        ref_entry_++;
183
        return *this;
184
      }
185

186
      ConstEntryReference operator*()
187
      {
188
        return {
189
          context_.ref_row_, context_.ref_col_ids_[ref_entry_], context_.ref_col_vals_[ref_entry_]};
190
      }
191

192
      bool operator==(const ConstRowIterator& rhs) const { return ref_entry_ == rhs.ref_entry_; }
193
      bool operator!=(const ConstRowIterator& rhs) const { return ref_entry_ != rhs.ref_entry_; }
194
    };
195

196
    ConstRowIterator begin() const { return {*this, 0}; }
197
    ConstRowIterator end() const { return {*this, ref_col_vals_.size()}; }
198
  };
199

200
  ConstRowIteratorContext Row(size_t row_id) const;
201

202
  /// Iterator to loop over all matrix entries.
203
  class EntriesIterator
204
  {
205
  private:
206
    SparseMatrix& sp_matrix;
207
    size_t ref_row_;
208
    size_t ref_col_;
209

210
  public:
211
    explicit EntriesIterator(SparseMatrix& context, size_t row)
2✔
212
      : sp_matrix{context}, ref_row_{row}, ref_col_(0)
2✔
213
    {
2,147,483,647✔
214
    }
2,147,483,647✔
215

2,147,483,647✔
216
    void Advance()
2,147,483,647✔
217
    {
2,147,483,647✔
218
      ref_col_++;
2,147,483,647✔
219
      if (ref_col_ >= sp_matrix.rowI_indices[ref_row_].size())
2,147,483,647✔
220
      {
2,147,483,647✔
221
        ref_row_++;
2,147,483,647✔
222
        ref_col_ = 0;
2,147,483,647✔
223
        while ((ref_row_ < sp_matrix.row_size_) and (sp_matrix.rowI_indices[ref_row_].empty()))
2,147,483,647✔
224
          ref_row_++;
2,147,483,647✔
225
      }
12✔
226
    }
12✔
227

12✔
228
    EntriesIterator operator++()
4✔
229
    {
4✔
230
      EntriesIterator i = *this;
4✔
UNCOV
231
      Advance();
×
232
      return i;
12✔
233
    }
12✔
234
    EntriesIterator operator++(int)
12✔
235
    {
12✔
236
      Advance();
12✔
237
      return *this;
238
    }
239

240
    EntryReference operator*()
12✔
241
    {
242
      return {ref_row_,
12✔
243
              sp_matrix.rowI_indices[ref_row_][ref_col_],
12✔
244
              sp_matrix.rowI_values[ref_row_][ref_col_]};
12✔
245
    }
246
    bool operator==(const EntriesIterator& rhs) const
247
    {
248
      return (ref_row_ == rhs.ref_row_) and (ref_col_ == rhs.ref_col_);
249
    }
250
    bool operator!=(const EntriesIterator& rhs) const
13✔
251
    {
252
      return (ref_row_ != rhs.ref_row_) or (ref_col_ != rhs.ref_col_);
13✔
253
    }
254
  };
255

256
  EntriesIterator begin();
257
  EntriesIterator end();
258
};
259

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