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

Open-Sn / opensn / 17170369288

22 Aug 2025 06:44PM UTC coverage: 74.605% (+0.2%) from 74.434%
17170369288

push

github

web-flow
Merge pull request #721 from andrsd/issue/557

Renaming `SteadyStateSolver` `SteadyStateSourceSolver`

13 of 14 new or added lines in 3 files covered. (92.86%)

239 existing lines in 24 files now uncovered.

17550 of 23524 relevant lines covered (74.6%)

45237948.15 hits per line

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

95.51
/framework/data_types/vector.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/data_types/ndarray.h"
7
#include <sstream>
8
#include <cmath>
9

10
namespace opensn
11
{
12

13
template <typename TYPE>
14
class Vector : public NDArray<TYPE, 1>
893,707,805✔
15
{
16
public:
17
  /// Create an empty dense (column) vector
18
  Vector() : NDArray<TYPE, 1>() {}
19

20
  /// Create a dense (column) vector with specified number of rows
21
  Vector(unsigned int rows) : NDArray<TYPE, 1>({rows}) {}
31,445,904✔
22

23
  /// Create a dense (column) vector with specified number of rows and initialize the elements
24
  Vector(unsigned int rows, TYPE intitial_value) : NDArray<TYPE, 1>({rows}, intitial_value) {}
25

26
  Vector(const std::vector<TYPE>& in) : NDArray<TYPE, 1>({in.size()})
27
  {
28
    for (auto i = 0; i < in.size(); ++i)
29
      (*this)(i) = in[i];
30
  }
31

32
  /// Return the number of rows
33
  unsigned int Rows() const
15,018,134✔
34
  {
35
    if (this->empty())
15,018,134✔
36
      return 0;
37
    else
38
      return this->dimension()[0];
13,338,415✔
39
  }
40

41
  /// Resize the vector
42
  void Resize(unsigned int new_size) { this->resize({new_size}); }
1✔
43

44
  /// Resize the vector and initialize the new elements with a value
45
  void Resize(unsigned int new_size, const TYPE& value)
46
  {
47
    auto old_size = Rows();
48
    this->resize({new_size});
49
    if (Rows() > old_size)
50
    {
51
      for (auto i = old_size; i < Rows(); ++i)
52
        (*this)(i) = value;
53
    }
54
  }
55

56
  /// Set the elements of the vector to a specified value
57
  void Set(TYPE val) { this->set(val); }
267,599,054✔
58

59
  /// Scale the vector with a constant value
60
  void Scale(TYPE alpha)
1✔
61
  {
62
    for (auto i = 0; i < Rows(); ++i)
4✔
63
      (*this)(i) *= alpha;
3✔
64
  }
1✔
65

66
  /// Return this vector scaled
67
  Vector<TYPE> Scaled(TYPE alpha)
68
  {
69
    Vector<TYPE> res(Rows());
70
    for (auto i = 0; i < Rows(); ++i)
71
      res(i) = (*this)(i)*alpha;
72
    return res;
73
  }
74

75
  /// Normalizes the vector in-place.
76
  void Normalize()
77
  {
78
    TYPE mag = this->Magnitude();
79
    for (auto i = 0; i < Rows(); ++i)
80
      (*this)(i) /= mag;
81
  }
82

83
  /// Return this vector normalized
84
  Vector<TYPE> Normalized() const
1✔
85
  {
86
    TYPE mag = this->Magnitude();
1✔
87
    Vector<TYPE> res(Rows());
1✔
88
    for (auto i = 0; i < Rows(); ++i)
3✔
89
      res(i) = (*this)(i) / mag;
2✔
90
    return res;
1✔
UNCOV
91
  }
×
92

93
  /// Computes the L2-norm of the vector. Otherwise known as the length of a 3D vector.
94
  TYPE Magnitude() const
1✔
95
  {
96
    TYPE value = 0.0;
1✔
97
    for (auto i = 0; i < Rows(); ++i)
3✔
98
      value += (*this)(i) * (*this)(i);
2✔
99
    value = sqrt(value);
1✔
100
    return value;
1✔
101
  }
102

103
  void Add(const Vector<TYPE>& other)
1✔
104
  {
105
    assert(Rows() == other.Rows());
1✔
106
    for (auto i = 0; i < Rows(); ++i)
3✔
107
      (*this)(i) += other(i);
2✔
108
  }
1✔
109

110
  void Subtract(const Vector<TYPE>& other)
1✔
111
  {
112
    assert(Rows() == other.Rows());
1✔
113
    for (auto i = 0; i < Rows(); ++i)
3✔
114
      (*this)(i) -= other(i);
2✔
115
  }
1✔
116

117
  TYPE Dot(const Vector<TYPE>& other)
1✔
118
  {
119
    assert(Rows() == other.Rows());
1✔
120
    TYPE prod = 0.;
121
    for (auto i = 0; i < Rows(); ++i)
3✔
122
      prod += (*this)(i)*other(i);
2✔
123
    return prod;
1✔
124
  }
125

126
  /// Prints the vector to a string and then returns the string.
127
  std::string PrintStr() const
128
  {
129
    std::stringstream out;
130
    out << "[";
131
    for (int i = 0; i < (Rows() - 1); ++i)
132
      out << (*this)(i) << " ";
133
    out << (*this)(Rows() - 1) << "]";
134

135
    return out.str();
136
  }
137

138
  std::vector<TYPE> ToStdVector() const
139
  {
140
    std::vector<TYPE> res(Rows());
141
    for (auto i = 0; i < Rows(); ++i)
142
      res[i] = (*this)(i);
143
    return res;
144
  }
145
};
146

147
/// Scale the vector with a constant value
148
template <typename TYPE>
149
void
150
Scale(Vector<TYPE>& a, TYPE alpha)
1✔
151
{
152
  for (auto i = 0; i < a.Rows(); ++i)
3✔
153
    a(i) *= alpha;
2✔
154
}
1✔
155

156
template <typename TYPE>
157
Vector<TYPE>
158
Scaled(const Vector<TYPE>& a, TYPE alpha)
3,622✔
159
{
160
  Vector<TYPE> res(a.Rows());
3,622✔
161
  for (auto i = 0; i < a.Rows(); ++i)
611,952✔
162
    res(i) = a(i) * alpha;
608,330✔
163
  return res;
3,622✔
UNCOV
164
}
×
165

166
/// Add vector to this vector
167
template <typename TYPE>
168
Vector<TYPE>
169
Add(const Vector<TYPE>& a, const Vector<TYPE>& b)
1✔
170
{
171
  assert(a.Rows() == b.Rows());
1✔
172
  Vector<TYPE> res(a.Rows());
1✔
173
  for (auto i = 0; i < a.Rows(); ++i)
3✔
174
    res(i) = a(i) + b(i);
2✔
175
  return res;
1✔
UNCOV
176
}
×
177

178
/// Subtract two vectors
179
template <typename TYPE>
180
Vector<TYPE>
181
Subtract(const Vector<TYPE>& a, const Vector<TYPE>& b)
1✔
182
{
183
  assert(a.Rows() == b.Rows());
1✔
184
  Vector<TYPE> res(a.Rows());
1✔
185
  for (auto i = 0; i < a.Rows(); ++i)
3✔
186
    res(i) = a(i) - b(i);
2✔
187
  return res;
1✔
UNCOV
188
}
×
189

190
template <typename TYPE>
191
double
192
Vec2Norm(const Vector<TYPE>& x)
3,588✔
193
{
194
  auto n = x.Rows();
3,588✔
195
  double val = 0.0;
3,588✔
196
  for (auto i = 0; i != n; ++i)
606,206✔
197
    val += x(i) * x(i);
602,618✔
198
  return std::sqrt(val);
3,588✔
199
}
200

201
template <typename TYPE>
202
double
203
Dot(const Vector<TYPE>& x, const Vector<TYPE>& y)
3,588✔
204
{
205
  assert(x.Rows() > 0);
3,588✔
206
  assert(y.Rows() > 0);
3,588✔
207
  assert(x.Rows() == y.Rows());
3,588✔
208
  double val = 0.0;
209
  for (auto i = 0; i < x.Rows(); ++i)
606,206✔
210
    val += x(i) * y(i);
602,618✔
211
  return val;
3,588✔
212
}
2,849,678✔
213

12,826,621✔
214
} // namespace opensn
8,860✔
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