Penning Trap Simulation
Simulate particle behavior inside a Penning Trap
Loading...
Searching...
No Matches
utils.cpp
Go to the documentation of this file.
1
12#include "utils.hpp"
13
14std::string scientific_format(double d, int width, int prec)
15{
16 std::stringstream ss;
17 ss << std::setw(width) << std::setprecision(prec) << std::scientific << d;
18 return ss.str();
19}
20
21std::string scientific_format(const std::vector<double> &v, int width, int prec)
22{
23 std::stringstream ss;
24 for (double elem : v) {
25 ss << scientific_format(elem, width, prec);
26 }
27 return ss.str();
28}
29
30static void print_message(std::string msg)
31{
32 if (msg.size() > 0) {
33 std::cout << "message: " << msg << "\n\n";
34 }
35 else {
36 std::cout << "\n";
37 }
38}
39
40void m_assert(bool expr, std::string expr_str, std::string f, std::string file,
41 int line, std::string msg)
42{
43 std::string new_assert(f.size() + (expr ? 4 : 6), '-');
44 std::cout << "\x1B[36m" << new_assert << "\033[0m\n";
45 std::cout << f << ": ";
46 if (expr) {
47 std::cout << "\x1B[32mOK\033[0m\n";
48 print_message(msg);
49 }
50 else {
51 std::cout << "\x1B[31mFAIL\033[0m\n";
52 print_message(msg);
53 std::cout << file << " " << line << ": Assertion \"" << expr_str
54 << "\" Failed\n\n";
55 abort();
56 }
57}
58
59bool arma_vector_close_to(arma::vec &a, arma::vec &b, double tol)
60{
61 if (a.n_elem != b.n_elem) {
62 return false;
63 }
64
65 for (int i = 0; i < a.n_elem; i++) {
66 if (std::abs(a(i) - b(i)) >= tol) {
67 return false;
68 }
69 }
70 return true;
71}
bool arma_vector_close_to(arma::vec &a, arma::vec &b, double tol)
Test if two armadillo vectors are close to each other.
Definition: utils.cpp:59
std::string scientific_format(double d, int width, int prec)
Turns a double into a string written in scientific format.
Definition: utils.cpp:14
void m_assert(bool expr, std::string expr_str, std::string f, std::string file, int line, std::string msg)
Test an expression, confirm that test is ok, or abort execution.
Definition: utils.cpp:40
Function prototypes and macros that are useful.