OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
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 USEFULFUNCTIONS
19#define USEFULFUNCTIONS
20
21#include "Algorithms/Vektor.h"
23
24#include <algorithm>
25#include <cmath>
26#include <cstring>
27#include <functional>
28#include <initializer_list>
29#include <limits>
30#include <sstream>
31#include <string>
32#include <type_traits>
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
45 double getGamma(Vector_t p) {
46 return std::sqrt(dot(p, p) + 1.0);
47 }
48
49 inline
51 return p / getGamma(p);
52 }
53
54 inline
55 double getKineticEnergy(Vector_t p, double mass) {
56 return (getGamma(p) - 1.0) * mass;
57 }
58
59 inline
60 double getBetaGamma(double Ekin, double mass) {
61 double value = std::sqrt(std::pow(Ekin / mass + 1.0, 2) - 1.0);
62 if (value < std::numeric_limits<double>::epsilon())
63 value = std::sqrt(2 * Ekin / mass);
64 return value;
65 }
66
67 inline
68 double convertMomentumEVoverCToBetaGamma(double p, double mass) {
69 return p / mass;
70 }
71
72 inline
73 std::string getTimeString(double time, unsigned int precision = 3) {
74 std::string timeUnit(" [ps]");
75
76 time *= 1e12;
77 if (std::abs(time) > 1000) {
78 time /= 1000;
79 timeUnit = std::string(" [ns]");
80
81 if (std::abs(time) > 1000) {
82 time /= 1000;
83 timeUnit = std::string(" [ms]");
84 }
85 } else if (std::abs(time) < 1.0) {
86 time *= 1000;
87 timeUnit = std::string(" [fs]");
88 }
89
90 std::stringstream timeOutput;
91 timeOutput << std::fixed << std::setw(precision + 2) << std::setprecision(precision) << time << timeUnit;
92 return timeOutput.str();
93 }
94
95 inline
96 std::string getLengthString(double spos, unsigned int precision = 3) {
97 std::string sposUnit(" [m]");
98
99 if (std::abs(spos) < 1.0) {
100 spos *= 1000.0;
101 sposUnit = std::string(" [mm]");
102 }
103
104 if (std::abs(spos) < 1.0) {
105 spos *= 1000.0;
106 sposUnit = std::string(" [um]");
107 }
108
109 std::stringstream positionOutput;
110 positionOutput << std::fixed << std::setw(precision + 2) << std::setprecision(precision) << spos << sposUnit;
111 return positionOutput.str();
112 }
113
114 inline
115 std::string getLengthString(Vector_t spos, unsigned int precision = 3) {
116 std::string sposUnit(" [m]");
117 double maxPos = std::abs(spos(0));
118 for (unsigned int i = 1; i < 3u; ++ i) {
119 maxPos = std::max(maxPos, std::abs(spos(i)));
120 }
121
122 std::stringstream positionOutput;
123
124 if (maxPos < 1.0) {
125 maxPos *= 1000.0;
126 spos *= 1000.0;
127 sposUnit = std::string(" [mm]");
128 }
129
130 if (maxPos < 1.0) {
131 maxPos *= 1000.0;
132 spos *= 1000.0;
133 sposUnit = std::string(" [um]");
134 }
135
136 positionOutput << std::fixed << std::setprecision(precision)
137 << "( "
138 << std::setw(precision + 7) << spos(0) << " , "
139 << std::setw(precision + 7) << spos(1) << " , "
140 << std::setw(precision + 7) << spos(2)
141 << " )" << sposUnit;
142 return positionOutput.str();
143 }
144
145 inline
146 std::string getEnergyString(double energyInMeV, unsigned int precision = 3) {
147 std::string energyUnit(" [MeV]");
148 double energy = energyInMeV;
149
150 if (energy > 1000.0) {
151 energy /= 1000.0;
152 energyUnit = std::string(" [GeV]");
153 } else if (energy < 1.0) {
154 energy *= 1000.0;
155 energyUnit = std::string(" [keV]");
156 if (energy < 1.0) {
157 energy *= 1000.0;
158 energyUnit = std::string(" [eV]");
159 }
160 }
161
162 std::stringstream energyOutput;
163 energyOutput << std::fixed << std::setw(precision + 2) << std::setprecision(precision) << energy << energyUnit;
164
165 return energyOutput.str();
166 }
167
168 inline
169 std::string getChargeString(double charge, unsigned int precision = 3) {
170 std::string chargeUnit(" [fC]");
171
172 charge *= 1e15;
173
174 if (std::abs(charge) > 1000.0) {
175 charge /= 1000.0;
176 chargeUnit = std::string(" [pC]");
177 }
178
179 if (std::abs(charge) > 1000.0) {
180 charge /= 1000.0;
181 chargeUnit = std::string(" [nC]");
182 }
183
184 if (std::abs(charge) > 1000.0) {
185 charge /= 1000.0;
186 chargeUnit = std::string(" [uC]");
187 }
188
189 std::stringstream chargeOutput;
190 chargeOutput << std::fixed << std::setw(precision + 2) << std::setprecision(precision) << charge << chargeUnit;
191
192 return chargeOutput.str();
193 }
194
195 Vector_t getTaitBryantAngles(Quaternion rotation, const std::string& elementName = "");
196
197 std::string toUpper(const std::string& str);
198
199 std::string boolToUpperString(const bool& b);
200
201 std::string boolVectorToUpperString(const std::vector<bool>& b);
202
203 std::string doubleVectorToString(const std::vector<double>& v);
204
205 std::string combineFilePath(std::initializer_list<std::string>);
206
207 template<class IteratorIn, class IteratorOut>
208 void toString(IteratorIn first, IteratorIn last, IteratorOut out);
209
210 template <typename T>
211 std::string toStringWithThousandSep(T value, char sep = '\'');
212
214 long double sum;
215 long double correction;
217
218 KahanAccumulation& operator+=(double value);
219 };
220
221 unsigned int rewindLinesSDDS(const std::string& fileName, double maxSPos, bool checkForTime = true);
222
223 std::string base64_encode(const std::string& string_to_encode);//unsigned char const* , unsigned int len);
224 std::string base64_decode(std::string const& s);
225
226 template<typename T, typename A>
227 T* c_data(std::vector<T,A>& v) { return v.empty() ? static_cast<T*>(0) : &(v[0]); }
228
229 template<typename T, typename A>
230 T const* c_data(std::vector<T,A> const& v) { return v.empty() ? static_cast<T const*>(0) : &(v[0]); }
231}
232
233template <typename T>
234std::string Util::toStringWithThousandSep(T value, char sep) {
235 static_assert(std::is_integral<T>::value, "Util::toStringWithThousandSep: T must be of integer type");
236
237 unsigned int powers = std::floor(std::max(0.0,
238 std::log(std::abs((double)value)) / std::log(10.0))
239 );
240 powers -= powers % 3u;
241
242 std::ostringstream ret;
243 unsigned int i = 0;
244 while (powers >= 3u) {
245 T multiplicator = std::pow(T(10), powers);
246 T pre = value / multiplicator;
247 if (i > 0) {
248 ret << std::setw(3) << std::setfill('0') << pre << sep;
249 } else {
250 ret << pre << sep;
251 }
252 value -= pre * multiplicator;
253
254 powers -= 3;
255 ++ i;
256 }
257
258 if (i > 0) {
259 ret << std::setw(3) << std::setfill('0') << value;
260 } else {
261 ret << value;
262 }
263
264 return ret.str();
265}
266
267template<class IteratorIn, class IteratorOut>
268void Util::toString(IteratorIn first, IteratorIn last, IteratorOut out) {
269 std::transform(first, last, out, [](auto d) {
270 std::ostringstream stm;
271 stm << d;
272 return stm.str();
273 } );
274}
275
276#endif
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
Definition: Vector3D.cpp:118
Tps< T > log(const Tps< T > &x)
Natural logarithm.
Definition: TpsMath.h:182
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
Definition: TpsMath.h:76
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:84
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition: PETE.h:733
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
Definition: Util.cpp:31
std::string combineFilePath(std::initializer_list< std::string > ilist)
Definition: Util.cpp:196
std::string doubleVectorToString(const std::vector< double > &v)
Definition: Util.cpp:175
std::string getChargeString(double charge, unsigned int precision=3)
Definition: Util.h:169
Vector_t getTaitBryantAngles(Quaternion rotation, const std::string &)
Definition: Util.cpp:116
std::string boolVectorToUpperString(const std::vector< bool > &b)
Definition: Util.cpp:160
double getKineticEnergy(Vector_t p, double mass)
Definition: Util.h:55
void toString(IteratorIn first, IteratorIn last, IteratorOut out)
Definition: Util.h:268
double convertMomentumEVoverCToBetaGamma(double p, double mass)
Definition: Util.h:68
double getBetaGamma(double Ekin, double mass)
Definition: Util.h:60
std::string toUpper(const std::string &str)
Definition: Util.cpp:146
double erfinv(double x)
Definition: Util.cpp:56
T * c_data(std::vector< T, A > &v)
Definition: Util.h:227
std::string base64_decode(std::string const &encoded_string)
Definition: Util.cpp:408
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:220
std::string getEnergyString(double energyInMeV, unsigned int precision=3)
Definition: Util.h:146
std::string getTimeString(double time, unsigned int precision=3)
Definition: Util.h:73
std::string getGitRevision()
Definition: Util.cpp:32
std::string base64_encode(const std::string &string_to_encode)
Definition: Util.cpp:364
Vector_t getBeta(Vector_t p)
Definition: Util.h:50
double getGamma(Vector_t p)
Definition: Util.h:45
std::string boolToUpperString(const bool &b)
Definition: Util.cpp:153
std::string getLengthString(double spos, unsigned int precision=3)
Definition: Util.h:96
std::string toStringWithThousandSep(T value, char sep='\'')
Definition: Util.h:234
long double sum
Definition: Util.h:214
long double correction
Definition: Util.h:215
KahanAccumulation & operator+=(double value)
Definition: Util.cpp:209