From 79da936956d6831548e1b7438a9a7fafd40fb460 Mon Sep 17 00:00:00 2001 From: Cory Date: Mon, 23 Oct 2023 10:52:38 +0200 Subject: [PATCH] Update formatting --- include/Particle.hpp | 8 ++++--- include/constants.hpp | 20 ++++++++++++----- include/typedefs.hpp | 4 ++-- include/utils.hpp | 50 +++++++++++++++++-------------------------- src/PenningTrap.cpp | 48 +++++++++++++++++++---------------------- src/main.cpp | 32 +++++++++++++-------------- src/test_suite.cpp | 34 +++++++++++++---------------- src/utils.cpp | 15 ++++++------- 8 files changed, 102 insertions(+), 109 deletions(-) diff --git a/include/Particle.hpp b/include/Particle.hpp index fb26a78..682859c 100644 --- a/include/Particle.hpp +++ b/include/Particle.hpp @@ -14,12 +14,13 @@ #include -#include "typedefs.hpp" #include "constants.hpp" +#include "typedefs.hpp" /** @brief A class that holds attributes of a particle * */ -class Particle { +class Particle +{ private: vec_3d r_vec; ///< position vec_3d v_vec; ///< velocity @@ -37,7 +38,8 @@ public: * @param r_vec The initial position of the particle * @param v_vec The initial velocity of the particle * */ - Particle(vec_3d r_vec, vec_3d v_vec, double q = CA_CHARGE, double m = CA_MASS); + Particle(vec_3d r_vec, vec_3d v_vec, double q = CA_CHARGE, + double m = CA_MASS); /** @brief Make private attributes available for PenningTrap. * */ diff --git a/include/constants.hpp b/include/constants.hpp index bc8694a..0546a4f 100644 --- a/include/constants.hpp +++ b/include/constants.hpp @@ -12,14 +12,24 @@ #ifndef __CONST__ #define __CONST__ -#define K_E 1.38935333*1e5 ///< Coulomb constant. unit: \f$\frac{u(\mu m)^3}{(\mu s)^2 e^2}\f$ +/** @brief Coulomb constant. unit: \f$\frac{u(\mu m)^3}{(\mu s)^2 e^2}\f$ + * */ +#define K_E 1.38935333 * 1e5 -#define T 9.64852558*1e1 ///< 1 Tesla. unit: \f$ \frac{u}{(\mu s) e} \f$ +/** @brief 1 Tesla. unit: \f$ \frac{u}{(\mu s) e} \f$ + * */ +#define T 9.64852558 * 1e1 -#define V 9.64852558*1e7 ///< 1 Volt. unit: \f$ \frac{u (\mu m)^2}{(\mu s)^2 e} \f$ +/** @brief 1 Volt. unit: \f$ \frac{u (\mu m)^2}{(\mu s)^2 e} \f$ + * */ +#define V 9.64852558 * 1e7 -#define CA_MASS 40.078 ///< Mass of a single calcium ion. unit: amu +/** @brief Mass of a single calcium ion. unit: amu + * */ +#define CA_MASS 40.078 -#define CA_CHARGE 1. ///< Charge of a singly charged calcium ion. unit: e +/** @brief Charge of a singly charged calcium ion. unit: e + * */ +#define CA_CHARGE 1. #endif diff --git a/include/typedefs.hpp b/include/typedefs.hpp index a5154b8..64234e8 100644 --- a/include/typedefs.hpp +++ b/include/typedefs.hpp @@ -7,7 +7,7 @@ * * @brief Useful typedefs for cleaner code. * - * @details These typedefs make the code more readable and easy to follow + * @details These typedefs make the code more readable and easy to follow * along. * * @bug No known bugs @@ -18,7 +18,7 @@ #include #include -/** @brief Typedef for the column of the result vector from simulating +/** @brief Typedef for the column of the result vector from simulating * particles. * */ typedef std::vector> sim_cols; diff --git a/include/utils.hpp b/include/utils.hpp index c8469c9..e2e28c2 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -27,14 +27,14 @@ * * This macro writes a debug message that includes the filename, * line number, and a custom message. The function is wrapped in an ifdef - * that checks if DBG is defined, so one can choose to display the debug + * that checks if DBG is defined, so one can choose to display the debug * messages by adding the -DDBG flag when compiling. * */ #ifdef DBG - #define DEBUG(msg) std::cout << __FILE__ << " " << __LINE__ << ": " \ - << msg << std::endl +#define DEBUG(msg) \ + std::cout << __FILE__ << " " << __LINE__ << ": " << msg << std::endl #else - #define DEBUG(msg) +#define DEBUG(msg) #endif /** @def ASSERT(expr) @@ -43,15 +43,14 @@ * This macro calls the m_assert function which is a more informative * assertion function than the regular assert function from cassert. * */ -#define ASSERT(expr, msg) m_assert(expr, #expr, __METHOD_NAME__, __FILE__, \ - __LINE__, msg) +#define ASSERT(expr, msg) \ + m_assert(expr, #expr, __METHOD_NAME__, __FILE__, __LINE__, msg) /** @def __METHOD_NAME__ * @brief Get the name of the current method/function without the return type. * */ #define __METHOD_NAME__ methodName(__PRETTY_FUNCTION__) - /** @brief Turns a double into a string written in scientific format. * * @details The code is stolen from https://github.com/anderkve/FYS3150. @@ -62,11 +61,11 @@ * * @return std::string * */ -std::string scientific_format(double d, int width=20, int prec=10); +std::string scientific_format(double d, int width = 20, int prec = 10); - -/** @brief Turns a vector of doubles into a string written in scientific format. - * +/** @brief Turns a vector of doubles into a string written in scientific + * format. + * * @details The code is stolen from https://github.com/anderkve/FYS3150. * * @param v The vector to stringify @@ -75,14 +74,12 @@ std::string scientific_format(double d, int width=20, int prec=10); * * @return std::string * */ -std::string scientific_format(const std::vector& v, - int width=20, - int prec=10); - +std::string scientific_format(const std::vector &v, int width = 20, + int prec = 10); /** @brief Test an expression, confirm that test is ok, or abort execution. * - * @details This function takes in an expression and prints an OK message if + * @details This function takes in an expression and prints an OK message if * it's true, or it prints a fail message and aborts execution if it fails. * * @param expr The expression to be evaluated @@ -92,17 +89,12 @@ std::string scientific_format(const std::vector& v, * @param line The line number where this function is called from * @param msg The message to be displayed * */ -void m_assert(bool expr, - std::string expr_str, - std::string func, - std::string file, - int line, - std::string msg); - +void m_assert(bool expr, std::string expr_str, std::string func, + std::string file, int line, std::string msg); /** @brief Test if two armadillo vectors are close to each other. * - * @details This function takes in 2 vectors and checks if they are + * @details This function takes in 2 vectors and checks if they are * approximately equal to each other given a tolerance. * * @param a Vector a @@ -111,8 +103,7 @@ void m_assert(bool expr, * * @return bool * */ -bool close_to(arma::vec &a, arma::vec &b, double tol=1e-8); - +bool close_to(arma::vec &a, arma::vec &b, double tol = 1e-8); /** @brief Takes in the __PRETTY_FUNCTION__ string and removes the return type. * @@ -124,16 +115,15 @@ bool close_to(arma::vec &a, arma::vec &b, double tol=1e-8); * * @return std::string * */ -static inline std::string methodName(const std::string& pretty_function) +static inline std::string methodName(const std::string &pretty_function) { size_t colons = pretty_function.find("::"); - size_t begin = pretty_function.substr(0,colons).rfind(" ") + 1; + size_t begin = pretty_function.substr(0, colons).rfind(" ") + 1; size_t end = pretty_function.rfind("(") - begin; - return pretty_function.substr(begin,end) + "()"; + return pretty_function.substr(begin, end) + "()"; } - /** @brief Make path given. * * @details This tries to be the equivalent to "mkdir -p" and creates a new diff --git a/src/PenningTrap.cpp b/src/PenningTrap.cpp index 6ccf12d..dbf7991 100644 --- a/src/PenningTrap.cpp +++ b/src/PenningTrap.cpp @@ -45,8 +45,9 @@ PenningTrap::PenningTrap(std::vector particles, double B_0, void PenningTrap::set_pertubation(double f, double omega_V) { - this->perturbation - = [f, omega_V](double t) { return f * std::cos(omega_V * t); }; + this->perturbation = [f, omega_V](double t) { + return f * std::cos(omega_V * t); + }; } void PenningTrap::reinitialize(double f, double omega_V, double t) @@ -167,9 +168,9 @@ void PenningTrap::evolve_RK4(double dt, bool particle_interaction) std::vector original_particles = this->particles; std::vector tmp_particles = this->particles; - vec_3d (PenningTrap::*force)(uint) - = particle_interaction ? &PenningTrap::total_force - : &PenningTrap::total_force_external; + vec_3d (PenningTrap::*force)(uint) = + particle_interaction ? &PenningTrap::total_force + : &PenningTrap::total_force_external; size_t size = this->particles.size(); @@ -188,10 +189,10 @@ void PenningTrap::evolve_RK4(double dt, bool particle_interaction) this->k_v[i][j] = (this->*force)(j) / this->particles[j].m; this->k_r[i][j] = this->particles[j].v_vec; - tmp_particles[j].v_vec - = original_particles[j].v_vec + this->v_func(i, j, dt); - tmp_particles[j].r_vec - = original_particles[j].r_vec + this->r_func(i, j, dt); + tmp_particles[j].v_vec = + original_particles[j].v_vec + this->v_func(i, j, dt); + tmp_particles[j].r_vec = + original_particles[j].r_vec + this->r_func(i, j, dt); } this->particles = tmp_particles; } @@ -205,9 +206,9 @@ void PenningTrap::evolve_forward_euler(double dt, bool particle_interaction) vec_3d force_res[size]; Particle *p; - vec_3d (PenningTrap::*force)(uint) - = particle_interaction ? &PenningTrap::total_force - : &PenningTrap::total_force_external; + vec_3d (PenningTrap::*force)(uint) = + particle_interaction ? &PenningTrap::total_force + : &PenningTrap::total_force_external; // Calculating the force for each particle is independent and therefore // a good candidate for parallel execution @@ -220,9 +221,8 @@ void PenningTrap::evolve_forward_euler(double dt, bool particle_interaction) // this as well #pragma omp parallel for private(p) for (size_t i = 0; i < size; i++) { - p = &this->particles[i]; - p->r_vec += dt * p->v_vec; - p->v_vec += dt * force_res[i] / p->m; + p->r_vec += dt * this->particles[i].v_vec; + p->v_vec += dt * force_res[i] / this->particles[i].m; } this->t += dt; @@ -241,11 +241,9 @@ simulation_t PenningTrap::simulate(double time, uint steps, std::string method, void (PenningTrap::*func)(double, bool); if (method == "rk4") { func = &PenningTrap::evolve_RK4; - } - else if (method == "euler") { + } else if (method == "euler") { func = &PenningTrap::evolve_forward_euler; - } - else { + } else { std::cout << "Not a valid method!" << std::endl; abort(); } @@ -273,8 +271,8 @@ void PenningTrap::write_simulation_to_dir(std::string path, double time, abort(); } - simulation_t res - = this->simulate(time, steps, method, particle_interaction); + simulation_t res = + this->simulate(time, steps, method, particle_interaction); std::ofstream ofile; @@ -308,11 +306,9 @@ double PenningTrap::fraction_of_particles_left(double time, uint steps, void (PenningTrap::*func)(double, bool); if (method == "rk4") { func = &PenningTrap::evolve_RK4; - } - else if (method == "euler") { + } else if (method == "euler") { func = &PenningTrap::evolve_forward_euler; - } - else { + } else { std::cout << "Not a valid method!" << std::endl; abort(); } @@ -323,7 +319,7 @@ double PenningTrap::fraction_of_particles_left(double time, uint steps, int particles_left = 0; -// A reduction is perfect here + // A reduction is perfect here #pragma omp parallel for reduction(+ : particles_left) for (size_t i = 0; i < this->particles.size(); i++) { if (arma::norm(this->particles[i].r_vec) < this->d) { diff --git a/src/main.cpp b/src/main.cpp index 76286d1..8c5b300 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,9 +42,9 @@ vec_3d analytical_solution_particle_1(double t) double w_n = (w_0 - std::sqrt(w_0 * w_0 - 2. * w_z2)) / 2.; double A_p = (25. + w_n * 20.) / (w_n - w_p); double A_n = -(25. + w_p * 20.) / (w_n - w_p); - std::complex f - = A_p * std::exp(std::complex(0., -w_p * t)) - + A_n * std::exp(std::complex(0., -w_n * t)); + std::complex f = + A_p * std::exp(std::complex(0., -w_p * t)) + + A_n * std::exp(std::complex(0., -w_n * t)); vec_3d res{std::real(f), std::imag(f), 20. * std::cos(std::sqrt(w_z2) * t)}; return res; @@ -156,8 +156,8 @@ void potential_resonance_wide_sweep() double freq_start = .2; double freq_end = 2.5; double freq_increment = .02; - size_t freq_iterations - = (size_t)((freq_end - freq_start) / freq_increment) + 1; + size_t freq_iterations = + (size_t)((freq_end - freq_start) / freq_increment) + 1; double res[4][freq_iterations]; @@ -176,14 +176,14 @@ void potential_resonance_wide_sweep() { // Each thread creates a PenningTrap instance and reuses it throughout // the sweep. - PenningTrap trap((unsigned int)100); + PenningTrap trap((uint)100); #pragma omp for collapse(2) for (size_t i = 0; i < 3; i++) { for (size_t j = 0; j < freq_iterations; j++) { // Reset particles and give new time dependent potential. trap.reinitialize(amplitudes[i], res[0][j]); - res[i + 1][j] - = trap.fraction_of_particles_left(time, N, "rk4", false); + res[i + 1][j] = + trap.fraction_of_particles_left(time, N, "rk4", false); } } } @@ -213,8 +213,8 @@ void potential_resonance_narrow_sweep() double freq_start = 1.; double freq_end = 1.7; double freq_increment = .002; - size_t freq_iterations - = (size_t)((freq_end - freq_start) / freq_increment); + size_t freq_iterations = + (size_t)((freq_end - freq_start) / freq_increment); double res[4][freq_iterations]; @@ -233,14 +233,14 @@ void potential_resonance_narrow_sweep() { // Each thread creates a PenningTrap instance and reuses it throughout // the sweep. - PenningTrap trap((unsigned int)100); + PenningTrap trap((uint)100); #pragma omp for collapse(2) for (size_t i = 0; i < 3; i++) { for (size_t j = 0; j < freq_iterations; j++) { // Reset particles and give new time dependent potential. trap.reinitialize(amplitudes[i], res[0][j]); - res[i + 1][j] - = trap.fraction_of_particles_left(time, N, "rk4", false); + res[i + 1][j] = + trap.fraction_of_particles_left(time, N, "rk4", false); } } } @@ -270,8 +270,8 @@ void potential_resonance_narrow_sweep_interaction() double freq_start = 1.; double freq_end = 1.7; double freq_increment = .002; - size_t freq_iterations - = (size_t)((freq_end - freq_start) / freq_increment); + size_t freq_iterations = + (size_t)((freq_end - freq_start) / freq_increment); double res[4][freq_iterations]; @@ -289,7 +289,7 @@ void potential_resonance_narrow_sweep_interaction() { // Each thread creates a PenningTrap instance and reuses it throughout // the sweep. - PenningTrap trap((unsigned int)100); + PenningTrap trap((uint)100); #pragma omp for collapse(2) for (size_t i = 0; i < 3; i++) { for (size_t j = 0; j < freq_iterations; j++) { diff --git a/src/test_suite.cpp b/src/test_suite.cpp index 16c3f53..3828541 100644 --- a/src/test_suite.cpp +++ b/src/test_suite.cpp @@ -20,7 +20,8 @@ /** @brief Test class for the Penning trap. * */ -class PenningTrapTest { +class PenningTrapTest +{ public: /** @brief Test that the external E field gives correct values. * */ @@ -34,17 +35,17 @@ public: tests.push_back( std::make_pair(vec_3d{0., 0., 0.}, vec_3d{0., 0., 0.})); - tests.push_back(std::make_pair(vec_3d{10., 0., 0.}, - vec_3d{96.4852558, 0., 0.})); + tests.push_back( + std::make_pair(vec_3d{10., 0., 0.}, vec_3d{96.4852558, 0., 0.})); - tests.push_back(std::make_pair(vec_3d{10., 0., 0.}, - vec_3d{96.4852558, 0., 0.})); + tests.push_back( + std::make_pair(vec_3d{10., 0., 0.}, vec_3d{96.4852558, 0., 0.})); - tests.push_back(std::make_pair(vec_3d{0., 10., 0.}, - vec_3d{0., 96.4852558, 0.})); + tests.push_back( + std::make_pair(vec_3d{0., 10., 0.}, vec_3d{0., 96.4852558, 0.})); - tests.push_back(std::make_pair(vec_3d{0., 0., 10.}, - vec_3d{0., 0., -192.9705116})); + tests.push_back( + std::make_pair(vec_3d{0., 0., 10.}, vec_3d{0., 0., -192.9705116})); vec_3d result; vec_3d v; @@ -106,8 +107,7 @@ public: static void test_total_force_external() { PenningTrap trap; - trap.add_particle( - Particle(vec_3d{1., 2., 3.}, vec_3d{3., 4., 5.})); + trap.add_particle(Particle(vec_3d{1., 2., 3.}, vec_3d{3., 4., 5.})); vec_3d expected{395.58954878, -270.15871624, -57.89115348}; vec_3d result = trap.total_force_external(0); @@ -122,8 +122,7 @@ public: static void test_total_force_particles() { PenningTrap trap; - trap.add_particle( - Particle(vec_3d{0., 0., 0.}, vec_3d{0., 0., 0.})); + trap.add_particle(Particle(vec_3d{0., 0., 0.}, vec_3d{0., 0., 0.})); vec_3d expected{0., 0., 0.}; vec_3d result = trap.total_force_particles(0); @@ -131,12 +130,9 @@ public: "Testing the total force of all particles on particle 0 " "with only a single particle"); - trap.add_particle( - Particle(vec_3d{1., 0., 0.}, vec_3d{0., 0., 0.})); - trap.add_particle( - Particle(vec_3d{0., 1., 0.}, vec_3d{0., 0., 0.})); - trap.add_particle( - Particle(vec_3d{0., 0., 1.}, vec_3d{0., 0., 0.})); + trap.add_particle(Particle(vec_3d{1., 0., 0.}, vec_3d{0., 0., 0.})); + trap.add_particle(Particle(vec_3d{0., 1., 0.}, vec_3d{0., 0., 0.})); + trap.add_particle(Particle(vec_3d{0., 0., 1.}, vec_3d{0., 0., 0.})); expected = vec_3d().fill(-138935.333); result = trap.total_force_particles(0); diff --git a/src/utils.cpp b/src/utils.cpp index 83e67a8..a7a1507 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -19,7 +19,8 @@ std::string scientific_format(double d, int width, int prec) return ss.str(); } -std::string scientific_format(const std::vector &v, int width, int prec) +std::string scientific_format(const std::vector &v, int width, + int prec) { std::stringstream ss; for (double elem : v) { @@ -32,8 +33,7 @@ static void print_message(std::string msg) { if (msg.size() > 0) { std::cout << "message: " << msg << "\n\n"; - } - else { + } else { std::cout << "\n"; } } @@ -47,8 +47,7 @@ void m_assert(bool expr, std::string expr_str, std::string f, std::string file, if (expr) { std::cout << "\x1B[32mOK\033[0m\n"; print_message(msg); - } - else { + } else { std::cout << "\x1B[31mFAIL\033[0m\n"; print_message(msg); std::cout << file << " " << line << ": Assertion \"" << expr_str @@ -85,11 +84,11 @@ bool mkpath(std::string path, int mode) pos = path.find('/', pos); if (pos != std::string::npos) { cur_dir = path.substr(0, pos); - if (mkdir(cur_dir.c_str(), mode) != 0 && stat(cur_dir.c_str(), &buf) != 0) { + if (mkdir(cur_dir.c_str(), mode) != 0 + && stat(cur_dir.c_str(), &buf) != 0) { return -1; } - } - else { + } else { break; } }