20#include "OPALrevision.h"
39 return std::string(GIT_VERSION);
42#define erfinv_a3 -0.140543331
43#define erfinv_a2 0.914624893
44#define erfinv_a1 -1.645349621
45#define erfinv_a0 0.886226899
47#define erfinv_b4 0.012229801
48#define erfinv_b3 -0.329097515
49#define erfinv_b2 1.442710462
50#define erfinv_b1 -2.118377725
53#define erfinv_c3 1.641345311
54#define erfinv_c2 3.429567803
55#define erfinv_c1 -1.62490649
56#define erfinv_c0 -1.970840454
58#define erfinv_d2 1.637067800
59#define erfinv_d1 3.543889200
88 double y = std::sqrt (-std::log ((1 - x) / 2));
96 r -= (std::erf (r) - x) / (2 / std::sqrt (
Physics::pi) * std::exp (-r * r));
97 r -= (std::erf (r) - x) / (2 / std::sqrt (
Physics::pi) * std::exp (-r * r));
131 Quaternion rotTheta(std::cos(0.5 * theta), 0, std::sin(0.5 * theta), 0);
132 rotation = rotTheta.
conjugate() * rotation;
140 Quaternion rotPhi(std::cos(0.5 * phi), std::sin(0.5 * phi), 0, 0);
141 rotation = rotPhi.
conjugate() * rotation;
153 std::string output = str;
154 std::transform(output.begin(), output.end(), output.begin(),
155 [](
unsigned char c) {
156 return std::toupper(c);
162 std::ostringstream valueStream;
163 valueStream << std::boolalpha << b;
169 std::ostringstream output;
173 for (std::size_t i = 0; i < b.size(); ++i) {
176 (i < (b.size()-1)) ? (output <<
", ") : (output <<
")");
184 std::vector<std::string> stringVec;
185 stringVec.reserve(v.size());
186 Util::toString(std::begin(v), std::end(v), std::back_inserter(stringVec));
188 std::ostringstream output;
193 for (
auto& s: stringVec) {
197 (i < stringVec.size()) ? (output <<
", ") : (output <<
")");
205 std::filesystem::path path;
206 for (
auto entry : ilist) {
209 return path.string();
214 if (std::abs(std::floor(
real) -
real) > 2*tolerance) {
216 "Value for " +
name +
217 " should be an integer but a real value was found");
219 if (std::floor(
real) < 0.5) {
221 "Value for " +
name +
" should be 1 or more");
226 return std::all_of(str.begin(),
228 [](
char c) { return std::isdigit(c); });
232 const std::string& from,
233 const std::string& to) {
234 if (from.empty())
return str;
237 result.reserve(str.size());
239 std::size_t pos = 0, found;
240 while ((found = str.find(from, pos)) != std::string::npos) {
241 result.append(str, pos, found - pos);
243 pos = found + from.length();
245 result.append(str, pos, std::string::npos);
251 const std::string& delims,
253 std::vector<std::string> out;
256 if (delims.find(c) != std::string::npos) {
257 if (!token.empty() || !compress) {
258 out.push_back(token);
265 if (!token.empty() || !compress) {
266 out.push_back(token);
272 if (str.empty())
return {};
274 uLongf compressed_size = compressBound(str.size());
275 std::string out(compressed_size,
'\0');
277 int ret = compress2(
reinterpret_cast<Bytef*
>(&out[0]), &compressed_size,
278 reinterpret_cast<const Bytef*
>(str.data()),
279 str.size(), Z_BEST_COMPRESSION);
282 throw std::runtime_error(
"zlib compression failed");
285 out.resize(compressed_size);
296 long double t = this->
sum + y;
297 this->correction = (t - this->
sum) - y;
305 unsigned int rewindLinesSDDS(
const std::string& fileName,
double maxSPos,
bool checkForTime) {
308 std::fstream fs(fileName.c_str(), std::fstream::in);
309 if (!fs.is_open())
return 0;
312 std::queue<std::string> allLines;
313 unsigned int numParameters = 0;
314 unsigned int numColumns = 0;
315 unsigned int sposColumnNr = 0;
316 unsigned int timeColumnNr = 0;
317 double spos, time = 0.0;
318 double lastTime = -1.0;
320 std::regex parameters(
"¶meter");
321 std::regex column(
"&column");
322 std::regex data(
"&data");
323 std::regex
end(
"&end");
324 std::regex
name(
"name=([a-zA-Z0-9\\$_]+)");
327 std::istringstream linestream;
329 while (std::getline(fs, line)) {
334 fs.open (fileName.c_str(), std::fstream::out);
336 if (!fs.is_open())
return 0;
339 line = allLines.front();
342 if (std::regex_search(line, match, parameters)) {
344 while (!std::regex_search(line, match,
end)) {
345 line = allLines.front();
349 }
else if (std::regex_search(line, match, column)) {
351 while (!std::regex_search(line, match,
name)) {
352 line = allLines.front();
356 if (match[1] ==
"s") {
357 sposColumnNr = numColumns;
359 if (match[1] ==
"t") {
360 timeColumnNr = numColumns;
362 while (!std::regex_search(line, match,
end)) {
363 line = allLines.front();
368 }
while (!std::regex_search(line, match, data));
370 while (!std::regex_search(line, match,
end)) {
371 line = allLines.front();
376 for (
unsigned int i = 0; i < numParameters; ++ i) {
377 fs << allLines.front() <<
"\n";
381 while (!allLines.empty()) {
382 line = allLines.front();
384 linestream.str(line);
386 for (
unsigned int i = 0; i < timeColumnNr; ++ i) {
391 linestream.str(line);
392 for (
unsigned int i = 0; i < sposColumnNr; ++ i) {
396 if ((spos - maxSPos) > 1e-20 *
Physics::c)
break;
400 if (!checkForTime || (time - lastTime) > 1e-20)
408 if (!allLines.empty())
409 INFOMSG(
level2 <<
"rewind " + fileName +
" to " + std::to_string(maxSPos) <<
" m" <<
endl);
411 return allLines.size();
441 static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
442 "abcdefghijklmnopqrstuvwxyz"
445 static inline bool is_base64(
unsigned char c) {
446 return (std::isalnum(c) || (c ==
'+') || (c ==
'/'));
450 const char* bytes_to_encode = string_to_encode.c_str();
451 unsigned int in_len = string_to_encode.size();
455 unsigned char char_array_3[3];
456 unsigned char char_array_4[4];
459 char_array_3[i++] = *(bytes_to_encode++);
461 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
462 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
463 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
464 char_array_4[3] = char_array_3[2] & 0x3f;
466 for (i = 0; (i <4) ; i++)
467 ret += base64_chars[char_array_4[i]];
474 for (j = i; j < 3; j++)
475 char_array_3[j] =
'\0';
477 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
478 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
479 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
480 char_array_4[3] = char_array_3[2] & 0x3f;
482 for (j = 0; (j < i + 1); j++)
483 ret += base64_chars[char_array_4[j]];
494 int in_len = encoded_string.size();
498 unsigned char char_array_4[4], char_array_3[3];
501 while (in_len-- && ( encoded_string[in_] !=
'=') && is_base64(encoded_string[in_])) {
502 char_array_4[i++] = encoded_string[in_]; in_++;
504 for (i = 0; i <4; i++)
505 char_array_4[i] = base64_chars.find(char_array_4[i]);
507 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
508 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
509 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
511 for (i = 0; (i < 3); i++)
512 ret += char_array_3[i];
518 for (j = i; j <4; j++)
521 for (j = 0; j <4; j++)
522 char_array_4[j] = base64_chars.find(char_array_4[j]);
524 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
525 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
526 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
528 for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
FLieGenerator< T, N > real(const FLieGenerator< std::complex< T >, N > &)
Take real part of a complex generator.
T euclidean_norm(const Vector< T > &)
Euclidean norm.
Inform & level2(Inform &inf)
Inform & endl(Inform &inf)
T::PETE_Expr_t::PETE_Return_t sum(const PETE_Expr< T > &expr)
constexpr double two_pi
The value of.
constexpr double c
The velocity of light in m/s.
constexpr double pi
The value of.
void checkInt(double real, const std::string &name, double tolerance)
std::string combineFilePath(std::initializer_list< std::string > ilist)
std::string doubleVectorToString(const std::vector< double > &v)
Vector_t getTaitBryantAngles(Quaternion rotation, const std::string &)
std::string boolVectorToUpperString(const std::vector< bool > &b)
void toString(IteratorIn first, IteratorIn last, IteratorOut out)
std::string toUpper(const std::string &str)
std::vector< std::string > split_any_of(const std::string &s, const std::string &delims, bool compress)
std::string base64_decode(std::string const &encoded_string)
std::string replaceAll(const std::string &str, const std::string &from, const std::string &to)
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
std::string getGitRevision()
std::string compressString(const std::string &str)
std::string base64_encode(const std::string &string_to_encode)
std::string boolToUpperString(const bool &b)
bool isAllDigits(const std::string &str)
Vector_t rotate(const Vector_t &) const
Quaternion conjugate() const
KahanAccumulation & operator+=(double value)
The base class for all OPAL exceptions.
Vektor< double, 3 > Vector_t