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

Open-Sn / opensn / 18300593117

06 Oct 2025 10:47PM UTC coverage: 74.862% (-0.2%) from 75.031%
18300593117

push

github

web-flow
Merge pull request #759 from wdhawkins/performance

Sweep performance optimizations

294 of 302 new or added lines in 15 files covered. (97.35%)

334 existing lines in 80 files now uncovered.

17788 of 23761 relevant lines covered (74.86%)

61852783.95 hits per line

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

93.5
/framework/data_types/vector3.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 <vector>
7
#include <array>
8
#include <iostream>
9
#include <cmath>
10
#include <sstream>
11
#include <memory>
12

13
namespace opensn
14
{
15

16
struct TensorRank2Dim3;
17

18
/// General 3-element vector structure.
19
struct Vector3
20
{
21
  /// X-component of the vector
22
  double x{0.0};
23
  /// Y-component of the vector
24
  double y{0.0};
25
  /// Z-component of the vector
26
  double z{0.0};
27

28
  /// Default constructor: initializes to (0, 0, 0).
29
  Vector3() = default;
1,385,614,783✔
30

31
  /// Constructor where \f$ \vec{x}=[a,b,c] \f$.
32
  explicit Vector3(double a, double b = 0.0, double c = 0.0) : x(a), y(b), z(c) {}
2,147,483,647✔
33

34
  /// Constructor where \f$ \vec{x}=\{a,b,c\} \f$.
35
  Vector3(std::initializer_list<double> list)
2,147,483,647✔
36
  {
2,147,483,647✔
37
    const auto* it = list.begin();
2,147,483,647✔
38
    if (list.size() > 0)
2,147,483,647✔
39
      x = *it++;
2,147,483,647✔
40
    if (list.size() > 1)
2,147,483,647✔
41
      y = *it++;
2,147,483,647✔
42
    if (list.size() > 2)
2,147,483,647✔
43
      z = *it;
2,147,483,647✔
44
  }
2,147,483,647✔
45

46
  /// Constructor where \f$ \vec{x}=\{a,b,c\} \f$.
47
  explicit Vector3(const std::vector<double>& list)
13✔
48
  {
13✔
49
    if (!list.empty())
13✔
50
      x = list[0];
12✔
51
    if (list.size() > 1)
13✔
52
      y = list[1];
11✔
53
    if (list.size() > 2)
13✔
54
      z = list[2];
10✔
55
  }
13✔
56

57
  /// Component-wise addition of two vectors. \f$ \vec{w} = \vec{x} + \vec{y} \f$
58
  Vector3 operator+(const Vector3& other) const { return {x + other.x, y + other.y, z + other.z}; }
990,390,677✔
59

60
  /// In-place component-wise addition of two vectors. \f$ \vec{x} = \vec{x} + \vec{y} \f$
61
  Vector3& operator+=(const Vector3& that)
2,147,483,647✔
62
  {
63
    x += that.x;
2,147,483,647✔
64
    y += that.y;
2,147,483,647✔
65
    z += that.z;
2,147,483,647✔
66
    return *this;
2,147,483,647✔
67
  }
68

69
  /// Component-wise shift by scalar-value. \f$ \vec{w} = \vec{x} + \alpha \f$
70
  Vector3 Shifted(double value) const { return Vector3{x + value, y + value, z + value}; }
1✔
71

72
  /// In-place component-wise shift by scalar value. \f$ \vec{x} = \vec{x} + \alpha \f$
73
  Vector3& Shift(double value)
1✔
74
  {
75
    x += value;
1✔
76
    y += value;
1✔
77
    z += value;
1✔
78
    return *this;
1✔
79
  }
80

81
  /// Component-wise subtraction of two vectors. \f$ \vec{w} = \vec{x} - \vec{y} \f$
82
  Vector3 operator-(const Vector3& other) const { return {x - other.x, y - other.y, z - other.z}; }
725,321,093✔
83

84
  /// In-place component-wise subtraction. \f$ \vec{x} = \vec{x} - \vec{y} \f$
85
  Vector3& operator-=(const Vector3& that)
1✔
86
  {
87
    x -= that.x;
1✔
88
    y -= that.y;
1✔
89
    z -= that.z;
1✔
90
    return *this;
1✔
91
  }
92

93
  /// Vector component-wise multiplication by scalar. \f$ \vec{w} = \vec{x} \alpha \f$
94
  Vector3 operator*(double value) const { return Vector3{x * value, y * value, z * value}; }
2,147,483,647✔
95

96
  /// Vector in-place component-wise multiplication by scalar. \f$ \vec{x} = \vec{x} \alpha \f$
97
  Vector3& operator*=(double value)
1,046,154✔
98
  {
99
    x *= value;
1,046,154✔
100
    y *= value;
1,046,154✔
101
    z *= value;
1,046,154✔
102
    return *this;
1,046,154✔
103
  }
104

105
  /// Vector component-wise multiplication. \f$ w_i = x_i y_i \f$
106
  Vector3 operator*(const Vector3& that) const
107
  {
108
    return Vector3{x * that.x, y * that.y, z * that.z};
109
  }
110

111
  /// Vector in-place component-wise multiplication. \f$ x_i = x_i y_i \f$
112
  Vector3& operator*=(const Vector3& that)
113
  {
114
    x *= that.x;
115
    y *= that.y;
116
    z *= that.z;
117
    return *this;
118
  }
119

120
  /// Vector component-wise division by scalar. \f$ w_i = \frac{x_i}{\alpha} \f$
121
  Vector3 operator/(double value) const
53,575,545✔
122
  {
123
    if (value == 0.0)
53,575,545✔
124
      throw std::runtime_error("Division by zero in Vector3 operator/");
×
125
    return Vector3{x / value, y / value, z / value};
53,575,545✔
126
  }
127

128
  /// Vector in-place component-wise division by scalar. \f$ x_i = \frac{x_i}{\alpha} \f$
129
  Vector3& operator/=(double value)
23,532,846✔
130
  {
131
    if (value == 0.0)
23,532,846✔
132
      throw std::runtime_error("Division by zero in Vector3 operator/=");
×
133
    x /= value;
23,532,846✔
134
    y /= value;
23,532,846✔
135
    z /= value;
23,532,846✔
136
    return *this;
23,532,846✔
137
  }
138

139
  /// Vector component-wise division. \f$ w_i = \frac{x_i}{y_i} \f$
140
  Vector3 operator/(const Vector3& that) const
1✔
141
  {
142
    if (that.x == 0.0 || that.y == 0.0 || that.z == 0.0)
1✔
143
      throw std::runtime_error("Division by zero in Vector3 operator/ (component-wise)");
×
144
    return Vector3{x / that.x, y / that.y, z / that.z};
1✔
145
  }
146

147
  /// Vector in-place component-wise division. \f$ x_i = \frac{x_i}{y_i} \f$
148
  Vector3& operator/=(const Vector3& that)
1✔
149
  {
150
    if (that.x == 0.0 || that.y == 0.0 || that.z == 0.0)
1✔
151
      throw std::runtime_error("Division by zero in Vector3 operator/= (component-wise)");
×
152
    x /= that.x;
1✔
153
    y /= that.y;
1✔
154
    z /= that.z;
1✔
155
    return *this;
1✔
156
  }
157

158
  /// Returns a copy of the value at the given index.
159
  double operator[](size_t i) const
160
  {
161
    if (i > 2)
162
      throw std::out_of_range("Index out of range in Vector3 operator[]");
163
    return i == 0 ? x : (i == 1 ? y : z);
164
  }
165

166
  /// Returns a reference of the value at the given index.
167
  double& operator()(size_t i)
6,146,053✔
168
  {
169
    if (i > 2)
6,146,053✔
170
      throw std::out_of_range("Index out of range in Vector3 operator()");
×
171
    return i == 0 ? x : (i == 1 ? y : z);
6,146,053✔
172
  }
173

174
  /**
175
   * Vector cross-product.
176
   * \f$ \vec{w} = \vec{x} \times \vec{y} \f$
177
   */
178
  Vector3 Cross(const Vector3& that) const
114,192,749✔
179
  {
180
    Vector3 newVector;
114,192,749✔
181
    newVector.x = this->y * that.z - this->z * that.y;
114,192,749✔
182
    newVector.y = this->z * that.x - this->x * that.z;
114,192,749✔
183
    newVector.z = this->x * that.y - this->y * that.x;
114,192,749✔
184

185
    return newVector;
114,192,749✔
186
  }
187

188
  /// Vector dot-product. \f$ \vec{w} = \vec{x} \bullet \vec{y} \f$
189
  double Dot(const Vector3& that) const { return x * that.x + y * that.y + z * that.z; }
2,147,483,647✔
190

191
  /// Computes the L2-norm of the vector. Otherwise known as the length of a 3D vector.
192
  double Norm() const { return std::sqrt(NormSquare()); }
241,802,846✔
193

194
  /// Computes the square of the L2-norm of the vector. Less expensive than a proper L2-norm.
195
  double NormSquare() const { return x * x + y * y + z * z; }
510,778,455✔
196

197
  /// Normalizes the vector in-place. \f$ \vec{x} = \frac{\vec{x}}{||x||_2} \f$
198
  void Normalize()
5,630,236✔
199
  {
200
    double norm = Norm();
5,630,236✔
201
    if (norm == 0.0)
5,630,236✔
202
      throw std::runtime_error("Cannot normalize a zero-length vector.");
×
203
    x /= norm;
5,630,236✔
204
    y /= norm;
5,630,236✔
205
    z /= norm;
5,630,236✔
206
  }
5,630,236✔
207

208
  /// Returns a normalized version of the vector. \f$ \vec{w} = \frac{\vec{x}}{||x||_2} \f$
209
  Vector3 Normalized() const
107,212,335✔
210
  {
211
    double norm = Norm();
107,212,335✔
212
    if (norm == 0.0)
107,212,335✔
213
      throw std::runtime_error("Cannot normalize a zero-length vector.");
×
214
    return Vector3{x / norm, y / norm, z / norm};
107,212,335✔
215
  }
216

217
  /**
218
   * Returns a vector v^* where each element is inverted provided that it is greater than the given
219
   * tolerance, otherwise the offending entry is set to 0.0. \f$ w_i = \frac{1.0}{x_i} \f$
220
   */
221
  /// Inverse components, setting zero if below tolerance.
222
  Vector3 InverseZeroIfSmaller(double tol) const
3✔
223
  {
224
    return Vector3{std::fabs(x) > tol ? 1.0 / x : 0.0,
3✔
225
                   std::fabs(y) > tol ? 1.0 / y : 0.0,
3✔
226
                   std::fabs(z) > tol ? 1.0 / z : 0.0};
6✔
227
  }
228

229
  /// Inverse components, setting one if below tolerance.
230
  Vector3 InverseOneIfSmaller(double tol) const
3✔
231
  {
232
    return Vector3{std::fabs(x) > tol ? 1.0 / x : 1.0,
3✔
233
                   std::fabs(y) > tol ? 1.0 / y : 1.0,
3✔
234
                   std::fabs(z) > tol ? 1.0 / z : 1.0};
6✔
235
  }
236

237
  /// Inverts each component without checking for division by zero.
238
  Vector3 Inverse() const
2✔
239
  {
240
    if (x == 0.0 || y == 0.0 || z == 0.0)
2✔
241
      throw std::runtime_error("Division by zero in Vector3::Inverse.");
1✔
242
    return Vector3{1.0 / x, 1.0 / y, 1.0 / z};
1✔
243
  }
244

245
  /// Prints the vector to std::cout.
246
  void Print() const { std::cout << "[" << x << " " << y << " " << z << "]"; }
247

248
  /// Returns the vector as a string.
249
  std::string PrintStr() const
250
  {
251
    std::ostringstream out;
252
    out << "[" << x << " " << y << " " << z << "]";
253
    return out.str();
254
  }
255

256
  /// Absolute equality check with another point.
257
  bool AbsoluteEquals(const Vector3& other, const double tol = 1.e-6) const
1,014✔
258
  {
259
    if (std::abs(other.x - x) > tol)
1,014✔
260
      return false;
490✔
261
    if (std::abs(other.y - y) > tol)
524✔
262
      return false;
249✔
263
    return std::abs(other.z - z) < tol;
275✔
264
  }
265

266
  static constexpr size_t Size() { return 3; }
9✔
267
};
141,564✔
268

141,564✔
269
/// Scalar multiplication from the left. \f$ \vec{w} = \alpha \vec{x}\f$
175,674,670✔
270
Vector3 operator*(double value, const Vector3& vec);
175,674,670✔
UNCOV
271

×
272
} // namespace opensn
175,674,670✔
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