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

Open-Sn / opensn / 17934392714

23 Sep 2025 01:57AM UTC coverage: 74.703% (+0.08%) from 74.627%
17934392714

push

github

web-flow
Merge pull request #766 from wdhawkins/python_doc_updates

Updating Python interface documentation

17718 of 23718 relevant lines covered (74.7%)

44392337.32 hits per line

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

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

4
#include "framework/utils/utils.h"
5
#include "framework/logging/log_exceptions.h"
6
#include <fstream>
7
#include <cmath>
8
#include <iomanip>
9
#include <sstream>
10

11
namespace opensn
12
{
13

14
std::string
15
StringLTrim(const std::string& s)
163✔
16
{
17
  size_t start = s.find_first_not_of(WHITESPACE);
163✔
18
  return (start == std::string::npos) ? "" : s.substr(start);
163✔
19
}
20

21
std::string
22
StringRTrim(const std::string& s)
163✔
23
{
24
  size_t end = s.find_last_not_of(WHITESPACE);
163✔
25
  return (end == std::string::npos) ? "" : s.substr(0, end + 1);
163✔
26
}
27

28
std::string
29
StringTrim(const std::string& s)
163✔
30
{
31
  return StringRTrim(StringLTrim(s));
326✔
32
}
33

34
std::vector<std::string>
35
StringSplit(const std::string& input, const std::string& delim)
×
36
{
37
  constexpr size_t NPOS = std::string::npos;
×
38
  std::vector<std::string> output;
×
39

40
  std::string remainder = input;
×
41
  size_t first_scope = remainder.find_first_of(delim);
×
42

43
  while (first_scope != NPOS)
×
44
  {
45
    if (first_scope != 0)
×
46
      output.push_back(remainder.substr(0, first_scope));
×
47

48
    remainder = remainder.substr(first_scope + delim.size(), NPOS);
×
49
    first_scope = remainder.find_first_of(delim);
×
50
  }
51
  output.push_back(remainder);
×
52

53
  return output;
×
54
}
×
55

56
std::string
57
StringUpToFirstReverse(const std::string& input, const std::string& search_string)
×
58
{
59
  constexpr size_t NPOS = std::string::npos;
×
60
  std::string output = input;
×
61
  const size_t last_scope = input.find_last_of(search_string);
×
62
  if (last_scope != NPOS)
×
63
    output = input.substr(last_scope + search_string.size(), NPOS);
×
64

65
  return output;
×
66
}
×
67

68
std::string
69
LowerCase(const std::string& name)
354✔
70
{
71
  std::string lower(name);
354✔
72
  std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
354✔
73
  return lower;
354✔
74
}
75

76
std::string
77
UpperCase(const std::string& name)
×
78
{
79
  std::string upper(name);
×
80
  std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
×
81
  return upper;
×
82
}
83

84
void
85
AssertReadableFile(const std::string& file_name)
148✔
86
{
87
  std::ifstream file(file_name.c_str(), std::ifstream::in);
148✔
88
  OpenSnLogicalErrorIf(file.fail(),
148✔
89
                       "Failed to open file \"" + file_name +
90
                         "\"."
91
                         "Either the file does not exist or you do not have read permissions.");
92

93
  file.close();
148✔
94
}
148✔
95

96
std::string
97
PrintIterationProgress(const size_t current_iteration,
39✔
98
                       const size_t total_num_iterations,
99
                       const unsigned int num_intvls)
100
{
101
  // Creating shorthand symbols for arguments
102
  const auto& i = current_iteration;
39✔
103
  const auto& I = num_intvls;
39✔
104
  const auto& N = total_num_iterations;
39✔
105

106
  // If on the first iteration then do nothing
107
  if (i == 0)
39✔
108
    return {};
1✔
109

110
  // Prepare an output stream
111
  std::stringstream output;
38✔
112
  output << std::fixed << std::setprecision(2) << std::setw(7);
38✔
113

114
  // If at the end, just print 100 and quit
115
  if ((i + 1) == N)
38✔
116
  {
117
    output << 100.0;
1✔
118
    return output.str();
1✔
119
  }
120

121
  const double dI = std::ceil(double(N) / I); // Interval size
37✔
122

123
  // std::modf is used to get the integral part
124
  // of a real value
125
  double x1;
37✔
126
  std::modf(double(i - 1) / dI, &x1);
37✔
127
  double x2;
37✔
128
  std::modf(double(i) / dI, &x2);
37✔
129

130
  if (static_cast<unsigned int>(x2) != static_cast<unsigned int>(x1))
37✔
131
  {
132
    output << x2 * (100.0 / I);
3✔
133
    return output.str();
3✔
134
  }
135

136
  return {};
34✔
137
}
38✔
138

139
std::vector<SubSetInfo>
140
MakeSubSets(size_t num_items, size_t desired_num_subsets)
7,563✔
141
{
142
  std::vector<SubSetInfo> ss_infos;
7,563✔
143
  const std::size_t div =
7,563✔
144
    std::floor(static_cast<double>(num_items) / static_cast<double>(desired_num_subsets));
7,563✔
145
  const std::size_t rem = num_items % desired_num_subsets;
7,563✔
146

147
  ss_infos.reserve(desired_num_subsets);
7,563✔
148
  for (size_t i = 0; i < desired_num_subsets; ++i)
15,187✔
149
    ss_infos.push_back({0, 0, div});
7,624✔
150
  for (size_t j = 0; j < rem; ++j)
7,563✔
151
    ss_infos[j].ss_size += 1;
×
152

153
  size_t check_sum = 0;
154
  for (size_t i = 0; i < desired_num_subsets; ++i)
15,187✔
155
  {
156
    ss_infos[i].ss_begin = check_sum;
7,624✔
157
    check_sum += ss_infos[i].ss_size;
7,624✔
158
    ss_infos[i].ss_end = check_sum - 1;
7,624✔
159
  }
160

161
  return ss_infos;
7,563✔
162
}
×
163

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