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