OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
Util.h
Go to the documentation of this file.
1//
2// Namespace Util
3// This namespace contains useful global methods.
4//
5// Copyright (c) 200x - 2022, Paul Scherrer Institut, Villigen PSI, Switzerland
6// All rights reserved
7//
8// This file is part of OPAL.
9//
10// OPAL is free software: you can redistribute it and/or modify
11// it under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// You should have received a copy of the GNU General Public License
16// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
17//
18#ifndef UTIL
19#define UTIL
20
22
23#include <algorithm>
24#include <cmath>
25#include <cstring>
26#include <functional>
27#include <initializer_list>
28#include <limits>
29#include <sstream>
30#include <string>
31#include <type_traits>
32#include "OPALTypes.h"
33
34// ------- DON'T DELETE: start --------
35#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
36#define __DBGMSG__ __FILENAME__ << ": " << __LINE__ << "\t"
37// ------- DON'T DELETE: end --------
38
39namespace Util {
40 std::string getGitRevision();
41
42 double erfinv(double x);
43
44 inline double getGamma(ippl::Vector<double, 3> p) {
45 double dotP = 0.0; // \todo dot(p, p); // .apply();
46 for (unsigned i = 0; i < 3; i++)
47 dotP += p(i) * p(i);
48 return std::sqrt(dotP + 1.0);
49 }
50
51 inline ippl::Vector<double, 3> getBeta(ippl::Vector<double, 3> p) { return p / getGamma(p); }
52
53 inline double getKineticEnergy(ippl::Vector<double, 3> p, double mass) {
54 return (getGamma(p) - 1.0) * mass;
55 }
56
57 inline double getBetaGamma(double Ekin, double mass) {
58 double value = std::sqrt(std::pow(Ekin / mass + 1.0, 2) - 1.0);
59 if (value < std::numeric_limits<double>::epsilon()) value = std::sqrt(2 * Ekin / mass);
60 return value;
61 }
62
63 inline double convertMomentumEVoverCToBetaGamma(double p, double mass) { return p / mass; }
64
65 inline std::string getTimeString(double time, unsigned int precision = 3) {
66 std::string timeUnit(" [ps]");
67
68 time *= 1e12;
69 if (std::abs(time) > 1000) {
70 time /= 1000;
71 timeUnit = std::string(" [ns]");
72
73 if (std::abs(time) > 1000) {
74 time /= 1000;
75 timeUnit = std::string(" [ms]");
76 }
77 } else if (std::abs(time) < 1.0) {
78 time *= 1000;
79 timeUnit = std::string(" [fs]");
80 }
81
82 std::stringstream timeOutput;
83 timeOutput << std::fixed << std::setw(precision + 2) << std::setprecision(precision) << time
84 << timeUnit;
85 return timeOutput.str();
86 }
87
88 inline std::string getLengthString(double spos, unsigned int precision = 3) {
89 std::string sposUnit(" [m]");
90
91 if (std::abs(spos) < 1.0) {
92 spos *= 1000.0;
93 sposUnit = std::string(" [mm]");
94 }
95
96 if (std::abs(spos) < 1.0) {
97 spos *= 1000.0;
98 sposUnit = std::string(" [um]");
99 }
100
101 std::stringstream positionOutput;
102 positionOutput << std::fixed << std::setw(precision + 2) << std::setprecision(precision)
103 << spos << sposUnit;
104 return positionOutput.str();
105 }
106
107 inline std::string getLengthString(ippl::Vector<double, 3> spos, unsigned int precision = 3) {
108 std::string sposUnit(" [m]");
109 double maxPos = std::abs(spos(0));
110 for (unsigned int i = 1; i < 3u; ++i) {
111 maxPos = std::max(maxPos, std::abs(spos(i)));
112 }
113
114 std::stringstream positionOutput;
115
116 if (maxPos < 1.0) {
117 maxPos *= 1000.0;
118 spos = spos * 1000.0;
119 sposUnit = std::string(" [mm]");
120 }
121
122 if (maxPos < 1.0) {
123 maxPos *= 1000.0;
124 spos = spos * 1000.0;
125 sposUnit = std::string(" [um]");
126 }
127
128 positionOutput << std::fixed << std::setprecision(precision) << "( "
129 << std::setw(precision + 7) << spos(0) << " , " << std::setw(precision + 7)
130 << spos(1) << " , " << std::setw(precision + 7) << spos(2) << " )"
131 << sposUnit;
132 return positionOutput.str();
133 }
134
135 inline std::string getEnergyString(double energyInMeV, unsigned int precision = 3) {
136 std::string energyUnit(" [MeV]");
137 double energy = energyInMeV;
138
139 if (energy > 1000.0) {
140 energy /= 1000.0;
141 energyUnit = std::string(" [GeV]");
142 } else if (energy < 1.0) {
143 energy *= 1000.0;
144 energyUnit = std::string(" [keV]");
145 if (energy < 1.0) {
146 energy *= 1000.0;
147 energyUnit = std::string(" [eV]");
148 }
149 }
150
151 std::stringstream energyOutput;
152 energyOutput << std::fixed << std::setw(precision + 2) << std::setprecision(precision)
153 << energy << energyUnit;
154
155 return energyOutput.str();
156 }
157
158 inline std::string getChargeString(double charge, unsigned int precision = 3) {
159 std::string chargeUnit(" [fC]");
160
161 charge *= 1e15;
162
163 if (std::abs(charge) > 1000.0) {
164 charge /= 1000.0;
165 chargeUnit = std::string(" [pC]");
166 }
167
168 if (std::abs(charge) > 1000.0) {
169 charge /= 1000.0;
170 chargeUnit = std::string(" [nC]");
171 }
172
173 if (std::abs(charge) > 1000.0) {
174 charge /= 1000.0;
175 chargeUnit = std::string(" [uC]");
176 }
177
178 std::stringstream chargeOutput;
179 chargeOutput << std::fixed << std::setw(precision + 2) << std::setprecision(precision)
180 << charge << chargeUnit;
181
182 return chargeOutput.str();
183 }
184
185 ippl::Vector<double, 3> getTaitBryantAngles(
186 Quaternion rotation, const std::string& elementName = "");
187
188 std::string toUpper(const std::string& str);
189
190 std::string boolToUpperString(const bool& b);
191
192 std::string boolVectorToUpperString(const std::vector<bool>& b);
193
194 std::string doubleVectorToString(const std::vector<double>& v);
195
196 std::string combineFilePath(std::initializer_list<std::string>);
197
198 template <class IteratorIn, class IteratorOut>
199 void toString(IteratorIn first, IteratorIn last, IteratorOut out);
200
201 template <typename T>
202 std::string toStringWithThousandSep(T value, char sep = '\'');
203
205 long double sum;
206 long double correction;
208
209 KahanAccumulation& operator+=(double value);
210 };
211
212 unsigned int rewindLinesSDDS(
213 const std::string& fileName, double maxSPos, bool checkForTime = true);
214
215 std::string base64_encode(
216 const std::string& string_to_encode); // unsigned char const* , unsigned int len);
217 std::string base64_decode(std::string const& s);
218
219 template <typename T, typename A>
220 T* c_data(std::vector<T, A>& v) {
221 return v.empty() ? static_cast<T*>(0) : &(v[0]);
222 }
223
224 template <typename T, typename A>
225 T const* c_data(std::vector<T, A> const& v) {
226 return v.empty() ? static_cast<T const*>(0) : &(v[0]);
227 }
228} // namespace Util
229
230template <typename T>
231std::string Util::toStringWithThousandSep(T value, char sep) {
232 static_assert(
233 std::is_integral<T>::value, "Util::toStringWithThousandSep: T must be of integer type");
234
235 unsigned int powers =
236 std::floor(std::max(0.0, std::log(std::abs((double)value)) / std::log(10.0)));
237 powers -= powers % 3u;
238
239 std::ostringstream ret;
240 unsigned int i = 0;
241 while (powers >= 3u) {
242 T multiplicator = std::pow(T(10), powers);
243 T pre = value / multiplicator;
244 if (i > 0) {
245 ret << std::setw(3) << std::setfill('0') << pre << sep;
246 } else {
247 ret << pre << sep;
248 }
249 value -= pre * multiplicator;
250
251 powers -= 3;
252 ++i;
253 }
254
255 if (i > 0) {
256 ret << std::setw(3) << std::setfill('0') << value;
257 } else {
258 ret << value;
259 }
260
261 return ret.str();
262}
263
264template <class IteratorIn, class IteratorOut>
265void Util::toString(IteratorIn first, IteratorIn last, IteratorOut out) {
266 std::transform(first, last, out, [](auto d) {
267 std::ostringstream stm;
268 stm << d;
269 return stm.str();
270 });
271}
272
273#endif
double T
Definition OPALTypes.h:8
Quaternion storage and rotation algebra used by OPALX geometry code.
Definition Util.cpp:31
std::string combineFilePath(std::initializer_list< std::string > ilist)
Definition Util.cpp:193
std::string doubleVectorToString(const std::vector< double > &v)
Definition Util.cpp:172
std::string getChargeString(double charge, unsigned int precision=3)
Definition Util.h:158
std::string boolVectorToUpperString(const std::vector< bool > &b)
Definition Util.cpp:157
void toString(IteratorIn first, IteratorIn last, IteratorOut out)
Definition Util.h:265
double convertMomentumEVoverCToBetaGamma(double p, double mass)
Definition Util.h:63
double getBetaGamma(double Ekin, double mass)
Definition Util.h:57
double getGamma(ippl::Vector< double, 3 > p)
Definition Util.h:44
std::string toUpper(const std::string &str)
Definition Util.cpp:141
double erfinv(double x)
Definition Util.cpp:54
T * c_data(std::vector< T, A > &v)
Definition Util.h:220
std::string base64_decode(std::string const &encoded_string)
Definition Util.cpp:401
unsigned int rewindLinesSDDS(const std::string &fileName, double maxSPos, bool checkForTime)
rewind the SDDS file such that the spos of the last step is less or equal to maxSPos
Definition Util.cpp:214
double getKineticEnergy(ippl::Vector< double, 3 > p, double mass)
Definition Util.h:53
std::string getEnergyString(double energyInMeV, unsigned int precision=3)
Definition Util.h:135
std::string getTimeString(double time, unsigned int precision=3)
Definition Util.h:65
std::string getGitRevision()
Definition Util.cpp:32
std::string base64_encode(const std::string &string_to_encode)
Definition Util.cpp:359
ippl::Vector< double, 3 > getBeta(ippl::Vector< double, 3 > p)
Definition Util.h:51
std::string boolToUpperString(const bool &b)
Definition Util.cpp:150
std::string getLengthString(double spos, unsigned int precision=3)
Definition Util.h:88
std::string toStringWithThousandSep(T value, char sep='\'')
Definition Util.h:231
Vector_t< double, 3 > getTaitBryantAngles(Quaternion rotation, const std::string &)
Definition Util.cpp:110
long double sum
Definition Util.h:205
long double correction
Definition Util.h:206
KahanAccumulation & operator+=(double value)
Definition Util.cpp:203