• 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

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

4
#include "framework/math/math.h"
5
#include "framework/runtime.h"
6
#include <cassert>
7

8
namespace opensn
9
{
10

11
double
12
Factorial(const int x)
30,220,016✔
13
{
14
  double factorial_value = 1.0;
30,220,016✔
15
  for (int i = 2; i <= x; ++i)
54,191,640✔
16
    factorial_value *= i;
23,971,624✔
17

18
  return factorial_value;
30,220,016✔
19
}
20

21
std::pair<double, double>
22
OmegaToPhiThetaSafe(const Vector3& omega)
839,857✔
23
{
24
  // Notes: asin maps [-1,+1] to [-pi/2,+pi/2]
25
  //        acos maps [-1,+1] to [0,pi]
26
  // This mapping requires some logic for determining the azimuthal angle.
27
  //
28
  const auto omega_hat = omega.Normalized();
839,857✔
29

30
  double mu = omega_hat.z;
839,857✔
31
  mu = std::min(mu, 1.0);
839,857✔
32
  mu = std::max(mu, -1.0);
839,857✔
33

34
  double theta = acos(mu);
839,857✔
35

36
  // Handling omega aligned to k_hat
37
  if (std::fabs(omega_hat.z) < 1.0e-16)
839,857✔
38
    return {0.0, theta};
×
39

40
  double cos_phi = omega_hat.x / sin(theta);
839,857✔
41
  cos_phi = std::min(cos_phi, 1.0);
839,857✔
42
  cos_phi = std::max(cos_phi, -1.0);
839,857✔
43

44
  // Computing varphi for NE and NW quadrant
45
  if (omega_hat.y >= 0.0)
839,857✔
46
    return {acos(cos_phi), theta};
421,089✔
47
  // Computing varphi for SE and SW quadrant
48
  else
49
    return {2.0 * M_PI - acos(cos_phi), theta};
418,768✔
50
}
51

52
void
53
PrintVector(const std::vector<double>& x)
×
54
{
55
  for (const auto& xi : x)
×
56
    std::cout << xi << ' ';
×
57
  std::cout << std::endl;
×
58
}
×
59

60
void
61
Scale(std::vector<double>& x, const double& val)
6,906✔
62
{
63
  for (double& xi : x)
487,989,022✔
64
    xi *= val;
487,982,116✔
65
}
6,906✔
66

67
void
68
Set(std::vector<double>& x, const double& val)
9,282✔
69
{
70
  for (double& xi : x)
533,111,690✔
71
    xi = val;
533,102,408✔
72
}
9,282✔
73

74
double
75
Dot(const std::vector<double>& x, const std::vector<double>& y)
×
76
{
77
  // Error Checks
NEW
78
  assert(!x.empty());
×
NEW
79
  assert(!y.empty());
×
UNCOV
80
  assert(x.size() == y.size());
×
81
  // Local Variables
82
  size_t n = x.size();
×
83
  double val = 0.0;
×
84

85
  for (size_t i = 0; i != n; ++i)
×
86
    val += x[i] * y[i];
×
87

88
  return val;
×
89
}
90

91
std::vector<double>
92
Mult(const std::vector<double>& x, const double& val)
×
93
{
94
  size_t n = x.size();
×
95
  std::vector<double> y(n);
×
96

97
  for (size_t i = 0; i != n; ++i)
×
98
    y[i] = val * x[i];
×
99

100
  return y;
×
101
}
102

103
double
104
L1Norm(const std::vector<double>& x)
×
105
{
106
  // Local Variables
107
  size_t n = x.size();
×
108
  double val = 0.0;
×
109

110
  for (size_t i = 0; i != n; ++i)
×
111
    val += std::fabs(x[i]);
×
112

113
  return val;
×
114
}
115

116
double
117
L2Norm(const std::vector<double>& x)
×
118
{
119
  // Local Variables
120
  size_t n = x.size();
×
121
  double val = 0.0;
×
122

123
  for (size_t i = 0; i != n; ++i)
×
124
    val += x[i] * x[i];
×
125

126
  return std::sqrt(val);
×
127
}
128

129
double
130
LInfNorm(const std::vector<double>& x)
×
131
{
132
  // Local Variables
133
  size_t n = x.size();
×
134
  double val = 0.0;
×
135

136
  for (size_t i = 0; i != n; ++i)
×
137
    val += std::max(std::fabs(x[i]), val);
×
138

139
  return val;
×
140
}
141

142
double
143
LpNorm(const std::vector<double>& x, const double& p)
×
144
{
145
  // Local Variables
146
  size_t n = x.size();
×
147
  double val = 0.0;
×
148

149
  for (size_t i = 0; i != n; ++i)
×
150
    val += std::pow(std::fabs(x[i]), p);
×
151

152
  return std::pow(val, 1.0 / p);
×
153
}
154

155
std::vector<double>
156
operator+(const std::vector<double>& a, const std::vector<double>& b)
9,868✔
157
{
158
  assert(a.size() == b.size());
9,868✔
159
  std::vector<double> result(a.size(), 0.0);
9,868✔
160

161
  for (size_t i = 0; i < a.size(); ++i)
36,144,268✔
162
    result[i] = a[i] + b[i];
36,134,400✔
163

164
  return result;
9,868✔
165
}
166

167
std::vector<double>
168
operator-(const std::vector<double>& a, const std::vector<double>& b)
2,532✔
169
{
170
  assert(a.size() == b.size());
2,532✔
171
  std::vector<double> result(a.size(), 0.0);
2,532✔
172

173
  for (size_t i = 0; i < a.size(); ++i)
8,104,932✔
174
    result[i] = a[i] - b[i];
8,102,400✔
175

176
  return result;
2,532✔
177
}
178

179
double
180
ComputeL2Change(std::vector<double>& x, std::vector<double>& y)
×
181
{
182
  assert(x.size() == y.size());
×
183

184
  double norm = 0.0;
×
185
  for (auto i = 0; i < x.size(); ++i)
×
186
  {
187
    double val = x[i] - y[i];
×
188
    norm += val * val;
×
189
  }
190

191
  double global_norm = 0.0;
×
192
  mpi_comm.all_reduce<double>(norm, global_norm, mpi::op::sum<double>());
×
193

194
  return std::sqrt(global_norm);
×
195
}
196

197
double
198
ComputePointwiseChange(std::vector<double>& x, std::vector<double>& y)
5,268✔
199
{
200
  assert(x.size() == y.size());
5,268✔
201

202
  double pw_change = 0.0;
5,268✔
203
  for (auto i = 0; i < x.size(); ++i)
109,284,000✔
204
  {
205
    double max = std::max(x[i], y[i]);
109,278,732✔
206
    double delta = std::fabs(x[i] - y[i]);
109,278,732✔
207
    if (max >= std::numeric_limits<double>::min())
109,278,732✔
208
      pw_change = std::max(delta / max, pw_change);
82,527,698✔
209
    else
210
      pw_change = std::max(delta, pw_change);
26,751,034✔
211
  }
212

213
  double global_pw_change = 0.0;
5,268✔
214
  mpi_comm.all_reduce<double>(pw_change, global_pw_change, mpi::op::max<double>());
5,268✔
215

216
  return global_pw_change;
5,268✔
217
}
218

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