From 0b8b5f88a60720ca74369efb83fd00446faf7dea Mon Sep 17 00:00:00 2001 From: Cory Date: Tue, 3 Oct 2023 14:25:40 +0200 Subject: [PATCH 01/10] Add testing function and macro --- include/utils.hpp | 4 ++++ src/utils.cpp | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include/utils.hpp b/include/utils.hpp index c8d3e4a..5c2926f 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -35,6 +35,8 @@ #define DEBUG(msg) #endif +#define ASSERT(expr) m_assert(expr, #expr, __FUNCTION__, __FILE__, __LINE__) + /** Code stolen from https://github.com/anderkve/FYS3150 * Header: https://github.com/anderkve/FYS3150/blob/master/code_examples/compilation_linking/example_1/include/utils.hpp * Source: https://github.com/anderkve/FYS3150/blob/master/code_examples/compilation_linking/example_1/src/utils.cpp @@ -62,4 +64,6 @@ std::string scientific_format(const std::vector& v, int width=20, int prec=10); +void m_assert(bool expr, const char* expr_str, const char* func, const char* file, int line); + #endif diff --git a/src/utils.cpp b/src/utils.cpp index 0e93a31..3c35a9d 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -10,6 +10,8 @@ * @bug No known bugs * */ #include "utils.hpp" +#include +#include std::string scientific_format(double d, int width, int prec) { @@ -26,3 +28,20 @@ std::string scientific_format(const std::vector& v, int width, int prec) } return ss.str(); } + +void m_assert(bool expr, + const char* expr_str, + const char* f, + const char* file, + int line) +{ + printf("%s: ", f); + if (expr) { + printf("\x1B[32mOK\033[0m\n\n"); + } + else { + printf("\x1B[31mFAIL\033[0m\n\n"); + printf("%s %d: Assertion (%s) Failed\n\n", file, line, expr_str); + abort(); + } +} -- 2.20.1 From 6112e36c22f09f64b432b9c9e4e0dfe3048d4b00 Mon Sep 17 00:00:00 2001 From: Cory Date: Tue, 3 Oct 2023 15:06:29 +0200 Subject: [PATCH 02/10] Fix Assert functions --- include/utils.hpp | 30 +++++++++++++++++++++++++++--- src/utils.cpp | 28 +++++++++++++++++++++++----- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/include/utils.hpp b/include/utils.hpp index 5c2926f..a41ce7b 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -23,7 +23,7 @@ /** @def DEBUG(msg) * @brief Writes a debug message * - * This function writes a debug message that includes the filename, + * 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 * messages by adding the -DDBG flag when compiling. @@ -35,7 +35,14 @@ #define DEBUG(msg) #endif -#define ASSERT(expr) m_assert(expr, #expr, __FUNCTION__, __FILE__, __LINE__) +/** @def ASSERT(expr) + * @brief A prettier assertion function. + * + * 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, __FUNCTION__, __FILE__, \ + __LINE__, msg) /** Code stolen from https://github.com/anderkve/FYS3150 * Header: https://github.com/anderkve/FYS3150/blob/master/code_examples/compilation_linking/example_1/include/utils.hpp @@ -64,6 +71,23 @@ std::string scientific_format(const std::vector& v, int width=20, int prec=10); -void m_assert(bool expr, const char* expr_str, const char* func, const char* file, int line); +/** @brief Test an expression, confirm that test is ok, or abort execution. + * + * 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 + * @param expr_str The stringified version of the expression + * @param func The function name of the caller + * @param file The file of the caller + * @param line The line number where this function is called from + * @param msg The message to be displayed + * */ +void m_assert(bool expr, + const char* expr_str, + const char* func, + const char* file, + int line, + const char* msg); #endif diff --git a/src/utils.cpp b/src/utils.cpp index 3c35a9d..f326b19 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -11,7 +11,9 @@ * */ #include "utils.hpp" #include +#include #include +#include std::string scientific_format(double d, int width, int prec) { @@ -29,19 +31,35 @@ std::string scientific_format(const std::vector& v, int width, int prec) return ss.str(); } +static void print_message(const char* msg) +{ + if (strlen(msg) > 0) { + std::cout << "message: " << msg << "\n\n"; + } + else { + std::cout << "\n"; + } +} + void m_assert(bool expr, const char* expr_str, const char* f, const char* file, - int line) + int line, + const char* msg) { - printf("%s: ", f); + std::string new_assert(strlen(f) + (expr ? 4 : 6), '-'); + std::cout << "\x1B[36m" << new_assert << "\033[0m\n"; + std::cout << f << ": "; if (expr) { - printf("\x1B[32mOK\033[0m\n\n"); + std::cout << "\x1B[32mOK\033[0m\n"; + print_message(msg); } else { - printf("\x1B[31mFAIL\033[0m\n\n"); - printf("%s %d: Assertion (%s) Failed\n\n", file, line, expr_str); + std::cout << "\x1B[31mFAIL\033[0m\n"; + print_message(msg); + std::cout << file << " " << line << ": Assertion \"" + << expr_str << "\" Failed\n\n"; abort(); } } -- 2.20.1 From 07197c1902bbe1ed1ffd25843b1fd4d5f694d403 Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 4 Oct 2023 12:56:47 +0200 Subject: [PATCH 03/10] Fix mistake --- src/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Makefile b/src/Makefile index f0cd401..52c16b7 100644 --- a/src/Makefile +++ b/src/Makefile @@ -27,7 +27,7 @@ all: test_suite main main: main.o $(LIBOBJS) $(CLASSOBJS) $(CC) $^ -o $@ $(CFLAGS) $(DBGFLAG) -I$(INCLUDE) $(OPENMP) -test_suite: test_suite.o $(LIBOBJS) +test_suite: test_suite.o $(LIBOBJS) $(CLASSOBJS) $(CC) $^ -o $@ $(CFLAGS) $(DBGFLAG) -I$(INCLUDE) $(OPENMP) # Rule for object files -- 2.20.1 From 6307256edcfa8fb0f22e96c6df502d3e6201b6f9 Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 4 Oct 2023 12:57:36 +0200 Subject: [PATCH 04/10] Use string instead of char* --- include/utils.hpp | 27 +++++++++++++++++++++------ src/utils.cpp | 32 +++++++++++++++++++++----------- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/include/utils.hpp b/include/utils.hpp index a41ce7b..95657ba 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -15,10 +15,11 @@ #ifndef __UTILS__ #define __UTILS__ -#include -#include +#include #include #include +#include +#include /** @def DEBUG(msg) * @brief Writes a debug message @@ -84,10 +85,24 @@ std::string scientific_format(const std::vector& v, * @param msg The message to be displayed * */ void m_assert(bool expr, - const char* expr_str, - const char* func, - const char* file, + std::string expr_str, + std::string func, + std::string file, int line, - const char* msg); + std::string msg); + + +/** @brief Test if two armadillo vectors are close to each other. + * + * This function takes in 2 vectors and checks if they are approximately + * equal to each other given a tolerance. + * + * @param a Vector a + * @param b Vector b + * @param tol The tolerance + * + * @return Boolean + * */ +bool arma_vector_close_to(arma::vec &a, arma::vec &b, double tol=1e-8); #endif diff --git a/src/utils.cpp b/src/utils.cpp index f326b19..9694610 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -10,10 +10,6 @@ * @bug No known bugs * */ #include "utils.hpp" -#include -#include -#include -#include std::string scientific_format(double d, int width, int prec) { @@ -31,9 +27,9 @@ std::string scientific_format(const std::vector& v, int width, int prec) return ss.str(); } -static void print_message(const char* msg) +static void print_message(std::string msg) { - if (strlen(msg) > 0) { + if (msg.size() > 0) { std::cout << "message: " << msg << "\n\n"; } else { @@ -42,13 +38,13 @@ static void print_message(const char* msg) } void m_assert(bool expr, - const char* expr_str, - const char* f, - const char* file, + std::string expr_str, + std::string f, + std::string file, int line, - const char* msg) + std::string msg) { - std::string new_assert(strlen(f) + (expr ? 4 : 6), '-'); + std::string new_assert(f.size() + (expr ? 4 : 6), '-'); std::cout << "\x1B[36m" << new_assert << "\033[0m\n"; std::cout << f << ": "; if (expr) { @@ -63,3 +59,17 @@ void m_assert(bool expr, abort(); } } + +bool arma_vector_close_to(arma::vec &a, arma::vec &b, double tol) +{ + if (a.n_elem != b.n_elem) { + return false; + } + + for (int i=0; i < a.n_elem; i++) { + if (std::abs(a(i) - b(i)) >= tol) { + return false; + } + } + return true; +} -- 2.20.1 From 21b94acbd8e5ee530dd4563e18e367b1dee11fdd Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 4 Oct 2023 12:58:16 +0200 Subject: [PATCH 05/10] Make small changes --- include/PenningTrap.hpp | 6 +++++- src/PenningTrap.cpp | 8 +------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/include/PenningTrap.hpp b/include/PenningTrap.hpp index 1b1752e..3601634 100644 --- a/include/PenningTrap.hpp +++ b/include/PenningTrap.hpp @@ -13,9 +13,13 @@ #define __PENNING_TRAP__ #include +#include -#include "constants.hpp" #include "Particle.hpp" +#include "constants.hpp" + +#pragma omp declare reduction( + : arma::vec : omp_out += omp_in ) \ + initializer( omp_priv = omp_orig ) /** @brief A class that simulates a Penning trap. * diff --git a/src/PenningTrap.cpp b/src/PenningTrap.cpp index 59be75c..eeb0c0d 100644 --- a/src/PenningTrap.cpp +++ b/src/PenningTrap.cpp @@ -13,15 +13,9 @@ * @todo Implement evolve_forward_euler * */ -#include "utils.hpp" #include "PenningTrap.hpp" #include "constants.hpp" -#include -#include -#include - -#pragma omp declare reduction( + : arma::vec : omp_out += omp_in ) \ - initializer( omp_priv = omp_orig ) +#include "utils.hpp" PenningTrap::PenningTrap(double B_0, double V_0, double d) { -- 2.20.1 From b1b7c1fdd077e31ef1ad65aa4147541db6510e2a Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 4 Oct 2023 12:58:44 +0200 Subject: [PATCH 06/10] Remove unnecessary includes --- src/main.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index b479649..14e8fa7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,9 +10,7 @@ * @bug No known bugs * */ -#include #include -#include #include #include -- 2.20.1 From 709d0c54119aa5f40a5487b908c0e73e56d4e745 Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 4 Oct 2023 12:58:59 +0200 Subject: [PATCH 07/10] Implement tests --- src/test_suite.cpp | 127 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/src/test_suite.cpp b/src/test_suite.cpp index 41f8b9c..626aaba 100644 --- a/src/test_suite.cpp +++ b/src/test_suite.cpp @@ -9,7 +9,134 @@ * * @bug No known bugs * */ + +#include "PenningTrap.hpp" +#include "utils.hpp" + +#include +#include +#include + +void test_PenningTrap_external_E_field() +{ + PenningTrap trap; + + // Vector containing inputs and expected results + std::vector> tests; + + tests.push_back(std::make_pair(arma::vec{0.,0.,0.}, + arma::vec{0.,0.,0.})); + + tests.push_back(std::make_pair(arma::vec{10.,0.,0.}, + arma::vec{96.4852558,0.,0.})); + + tests.push_back(std::make_pair(arma::vec{10.,0.,0.}, + arma::vec{96.4852558,0.,0.})); + + tests.push_back(std::make_pair(arma::vec{0.,10.,0.}, + arma::vec{0.,96.4852558,0.})); + + tests.push_back(std::make_pair(arma::vec{0.,0.,10.}, + arma::vec{0.,0.,-192.9705116})); + + + arma::vec result; + arma::vec v; + std::stringstream msg; + for (int i=0; i < tests.size(); i++) { + v = tests.at(i).first; + result = trap.external_E_field(v); + + msg.str(""); + msg << "Testing the external E field at (" << std::setprecision(2) + << v(0) << "," << v(1) << "," << v(2) << ")."; + + ASSERT(arma_vector_close_to(result, tests.at(i).second), msg.str()); + } +} + + +void test_PenningTrap_external_B_field() +{ + // No point in testing at different points since it's not dependent on + // position. + PenningTrap trap; + arma::vec expected{0.,0.,T}; + arma::vec result = trap.external_B_field(arma::vec{0.,0.,0.}); + ASSERT(arma_vector_close_to(expected, result), + "Testing the external B field at (0,0,0)"); +} + +void test_PenningTrap_force_on_particle() +{ + PenningTrap trap; + arma::vec v{0.,0.,0.}; + + // Add particles to test + trap.add_particle(Particle(1.,40.,arma::vec{0.,0.,0.},v)); + trap.add_particle(Particle(1.,40.,arma::vec{1.,0.,0.},v)); + trap.add_particle(Particle(1.,40.,arma::vec{0.,3.,4.},v)); + + // Test p0 and p1 + arma::vec expected{-1.,0.,0.}; + arma::vec result = trap.force_on_particle(0, 1); + ASSERT(arma_vector_close_to(expected, result), + "Testing the force on a particle at (0,0,0) from a " + "particle at (1,0,0)."); + + // Test p0 and p2 + expected = arma::vec{0, -.024, -.032}; + result = trap.force_on_particle(0, 2); + ASSERT(arma_vector_close_to(expected, result), + "Testing the force on a particle at (0,0,0) from a " + "particle at (0,3,4)."); +} + +void test_PenningTrap_total_force_external() +{ + PenningTrap trap; + trap.add_particle(Particle(1.,40.,arma::vec{1.,2.,3.}, + arma::vec{3.,4.,5.})); + + arma::vec expected{395.58954878,-270.15871624,-57.89115348}; + arma::vec result = trap.total_force_external(0); + ASSERT(arma_vector_close_to(expected, result), + "Testing the total external force on a particle at " + "(1,2,3) with velocity (3,4,5)"); +} + +void test_PenningTrap_total_force_particles() +{ + PenningTrap trap; + trap.add_particle(Particle(1.,40.,arma::vec{0.,0.,0.}, + arma::vec{0.,0.,0.})); + + arma::vec expected{0.,0.,0.}; + arma::vec result = trap.total_force_particles(0); + ASSERT(arma_vector_close_to(expected, result), + "Testing the total force of all particles on particle 0 " + "with only a single particle"); + + trap.add_particle(Particle(1.,40.,arma::vec{1.,0.,0.}, + arma::vec{0.,0.,0.})); + trap.add_particle(Particle(1.,40.,arma::vec{0.,1.,0.}, + arma::vec{0.,0.,0.})); + trap.add_particle(Particle(1.,40.,arma::vec{0.,0.,1.}, + arma::vec{0.,0.,0.})); + + expected = arma::vec(3,arma::fill::value(-3473.383325)); + result = trap.total_force_particles(0); + ASSERT(arma_vector_close_to(expected, result), + "Testing the total force of all particles on particle 0 " + "with 3 other particles."); +} + int main() { + test_PenningTrap_external_E_field(); + test_PenningTrap_external_B_field(); + test_PenningTrap_force_on_particle(); + test_PenningTrap_total_force_external(); + test_PenningTrap_total_force_particles(); return 0; } -- 2.20.1 From a24635ede0bd596f9112e874f302983d868cba35 Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 4 Oct 2023 17:46:10 +0200 Subject: [PATCH 08/10] Make some changes --- include/utils.hpp | 15 ++- src/test_suite.cpp | 225 +++++++++++++++++++++++---------------------- 2 files changed, 129 insertions(+), 111 deletions(-) diff --git a/include/utils.hpp b/include/utils.hpp index 95657ba..7a03149 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -42,9 +42,12 @@ * 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, __FUNCTION__, __FILE__, \ +#define ASSERT(expr, msg) m_assert(expr, #expr, __METHOD_NAME__, __FILE__, \ __LINE__, msg) +#define __METHOD_NAME__ methodName(__PRETTY_FUNCTION__) + + /** Code stolen from https://github.com/anderkve/FYS3150 * Header: https://github.com/anderkve/FYS3150/blob/master/code_examples/compilation_linking/example_1/include/utils.hpp * Source: https://github.com/anderkve/FYS3150/blob/master/code_examples/compilation_linking/example_1/src/utils.cpp @@ -105,4 +108,14 @@ void m_assert(bool expr, * */ bool arma_vector_close_to(arma::vec &a, arma::vec &b, double tol=1e-8); + +static inline std::string methodName(const std::string& prettyFunction) +{ + size_t colons = prettyFunction.find("::"); + size_t begin = prettyFunction.substr(0,colons).rfind(" ") + 1; + size_t end = prettyFunction.rfind("(") - begin; + + return prettyFunction.substr(begin,end) + "()"; +} + #endif diff --git a/src/test_suite.cpp b/src/test_suite.cpp index 626aaba..17f9c0e 100644 --- a/src/test_suite.cpp +++ b/src/test_suite.cpp @@ -17,126 +17,131 @@ #include #include -void test_PenningTrap_external_E_field() -{ - PenningTrap trap; +class PenningTrapTest { +public: + static void test_external_E_field() + { + PenningTrap trap; - // Vector containing inputs and expected results - std::vector> tests; + // Vector containing inputs and expected results + std::vector> tests; - tests.push_back(std::make_pair(arma::vec{0.,0.,0.}, + tests.push_back(std::make_pair(arma::vec{0.,0.,0.}, + arma::vec{0.,0.,0.})); + + tests.push_back(std::make_pair(arma::vec{10.,0.,0.}, + arma::vec{96.4852558,0.,0.})); + + tests.push_back(std::make_pair(arma::vec{10.,0.,0.}, + arma::vec{96.4852558,0.,0.})); + + tests.push_back(std::make_pair(arma::vec{0.,10.,0.}, + arma::vec{0.,96.4852558,0.})); + + tests.push_back(std::make_pair(arma::vec{0.,0.,10.}, + arma::vec{0.,0.,-192.9705116})); + + + arma::vec result; + arma::vec v; + std::stringstream msg; + for (int i=0; i < tests.size(); i++) { + v = tests.at(i).first; + result = trap.external_E_field(v); + + msg.str(""); + msg << "Testing the external E field at (" << std::setprecision(2) + << v(0) << "," << v(1) << "," << v(2) << ")."; + + ASSERT(arma_vector_close_to(result, tests.at(i).second), msg.str()); + } + } + + + static void test_external_B_field() + { + // No point in testing at different points since it's not dependent on + // position. + PenningTrap trap; + arma::vec expected{0.,0.,T}; + arma::vec result = trap.external_B_field(arma::vec{0.,0.,0.}); + ASSERT(arma_vector_close_to(expected, result), + "Testing the external B field at (0,0,0)"); + } + + + static void test_force_on_particle() + { + PenningTrap trap; + arma::vec v{0.,0.,0.}; + + // Add particles to test + trap.add_particle(Particle(1.,40.,arma::vec{0.,0.,0.},v)); + trap.add_particle(Particle(1.,40.,arma::vec{1.,0.,0.},v)); + trap.add_particle(Particle(1.,40.,arma::vec{0.,3.,4.},v)); + + // Test p0 and p1 + arma::vec expected{-1.,0.,0.}; + arma::vec result = trap.force_on_particle(0, 1); + ASSERT(arma_vector_close_to(expected, result), + "Testing the force on a particle at (0,0,0) from a " + "particle at (1,0,0)."); + + // Test p0 and p2 + expected = arma::vec{0, -.024, -.032}; + result = trap.force_on_particle(0, 2); + ASSERT(arma_vector_close_to(expected, result), + "Testing the force on a particle at (0,0,0) from a " + "particle at (0,3,4)."); + } + + static void test_total_force_external() + { + PenningTrap trap; + trap.add_particle(Particle(1.,40.,arma::vec{1.,2.,3.}, + arma::vec{3.,4.,5.})); + + arma::vec expected{395.58954878,-270.15871624,-57.89115348}; + arma::vec result = trap.total_force_external(0); + ASSERT(arma_vector_close_to(expected, result), + "Testing the total external force on a particle at " + "(1,2,3) with velocity (3,4,5)"); + } + + static void test_total_force_particles() + { + PenningTrap trap; + trap.add_particle(Particle(1.,40.,arma::vec{0.,0.,0.}, arma::vec{0.,0.,0.})); - tests.push_back(std::make_pair(arma::vec{10.,0.,0.}, - arma::vec{96.4852558,0.,0.})); + arma::vec expected{0.,0.,0.}; + arma::vec result = trap.total_force_particles(0); + ASSERT(arma_vector_close_to(expected, result), + "Testing the total force of all particles on particle 0 " + "with only a single particle"); - tests.push_back(std::make_pair(arma::vec{10.,0.,0.}, - arma::vec{96.4852558,0.,0.})); + trap.add_particle(Particle(1.,40.,arma::vec{1.,0.,0.}, + arma::vec{0.,0.,0.})); + trap.add_particle(Particle(1.,40.,arma::vec{0.,1.,0.}, + arma::vec{0.,0.,0.})); + trap.add_particle(Particle(1.,40.,arma::vec{0.,0.,1.}, + arma::vec{0.,0.,0.})); - tests.push_back(std::make_pair(arma::vec{0.,10.,0.}, - arma::vec{0.,96.4852558,0.})); - - tests.push_back(std::make_pair(arma::vec{0.,0.,10.}, - arma::vec{0.,0.,-192.9705116})); - - - arma::vec result; - arma::vec v; - std::stringstream msg; - for (int i=0; i < tests.size(); i++) { - v = tests.at(i).first; - result = trap.external_E_field(v); - - msg.str(""); - msg << "Testing the external E field at (" << std::setprecision(2) - << v(0) << "," << v(1) << "," << v(2) << ")."; - - ASSERT(arma_vector_close_to(result, tests.at(i).second), msg.str()); + expected = arma::vec(3,arma::fill::value(-3473.383325)); + result = trap.total_force_particles(0); + ASSERT(arma_vector_close_to(expected, result), + "Testing the total force of all particles on particle 0 " + "with 3 other particles."); } -} +}; -void test_PenningTrap_external_B_field() -{ - // No point in testing at different points since it's not dependent on - // position. - PenningTrap trap; - arma::vec expected{0.,0.,T}; - arma::vec result = trap.external_B_field(arma::vec{0.,0.,0.}); - ASSERT(arma_vector_close_to(expected, result), - "Testing the external B field at (0,0,0)"); -} - -void test_PenningTrap_force_on_particle() -{ - PenningTrap trap; - arma::vec v{0.,0.,0.}; - - // Add particles to test - trap.add_particle(Particle(1.,40.,arma::vec{0.,0.,0.},v)); - trap.add_particle(Particle(1.,40.,arma::vec{1.,0.,0.},v)); - trap.add_particle(Particle(1.,40.,arma::vec{0.,3.,4.},v)); - - // Test p0 and p1 - arma::vec expected{-1.,0.,0.}; - arma::vec result = trap.force_on_particle(0, 1); - ASSERT(arma_vector_close_to(expected, result), - "Testing the force on a particle at (0,0,0) from a " - "particle at (1,0,0)."); - - // Test p0 and p2 - expected = arma::vec{0, -.024, -.032}; - result = trap.force_on_particle(0, 2); - ASSERT(arma_vector_close_to(expected, result), - "Testing the force on a particle at (0,0,0) from a " - "particle at (0,3,4)."); -} - -void test_PenningTrap_total_force_external() -{ - PenningTrap trap; - trap.add_particle(Particle(1.,40.,arma::vec{1.,2.,3.}, - arma::vec{3.,4.,5.})); - - arma::vec expected{395.58954878,-270.15871624,-57.89115348}; - arma::vec result = trap.total_force_external(0); - ASSERT(arma_vector_close_to(expected, result), - "Testing the total external force on a particle at " - "(1,2,3) with velocity (3,4,5)"); -} - -void test_PenningTrap_total_force_particles() -{ - PenningTrap trap; - trap.add_particle(Particle(1.,40.,arma::vec{0.,0.,0.}, - arma::vec{0.,0.,0.})); - - arma::vec expected{0.,0.,0.}; - arma::vec result = trap.total_force_particles(0); - ASSERT(arma_vector_close_to(expected, result), - "Testing the total force of all particles on particle 0 " - "with only a single particle"); - - trap.add_particle(Particle(1.,40.,arma::vec{1.,0.,0.}, - arma::vec{0.,0.,0.})); - trap.add_particle(Particle(1.,40.,arma::vec{0.,1.,0.}, - arma::vec{0.,0.,0.})); - trap.add_particle(Particle(1.,40.,arma::vec{0.,0.,1.}, - arma::vec{0.,0.,0.})); - - expected = arma::vec(3,arma::fill::value(-3473.383325)); - result = trap.total_force_particles(0); - ASSERT(arma_vector_close_to(expected, result), - "Testing the total force of all particles on particle 0 " - "with 3 other particles."); -} - int main() { - test_PenningTrap_external_E_field(); - test_PenningTrap_external_B_field(); - test_PenningTrap_force_on_particle(); - test_PenningTrap_total_force_external(); - test_PenningTrap_total_force_particles(); + PenningTrapTest::test_external_E_field(); + PenningTrapTest::test_external_B_field(); + PenningTrapTest::test_force_on_particle(); + PenningTrapTest::test_total_force_external(); + PenningTrapTest::test_total_force_particles(); return 0; } -- 2.20.1 From 8a2100a3349b7249e50a843d9e9668b2b9061c3b Mon Sep 17 00:00:00 2001 From: Cory Date: Sat, 7 Oct 2023 21:59:37 +0200 Subject: [PATCH 09/10] Format file --- src/PenningTrap.cpp | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/src/PenningTrap.cpp b/src/PenningTrap.cpp index eeb0c0d..b4b2dd8 100644 --- a/src/PenningTrap.cpp +++ b/src/PenningTrap.cpp @@ -32,20 +32,17 @@ void PenningTrap::add_particle(Particle particle) arma::vec PenningTrap::external_E_field(arma::vec r) { arma::vec::fixed<3> res; - double f = this->V_0/(this->d*this->d); + double f = this->V_0 / (this->d * this->d); res(0) = r(0); res(1) = r(1); - res(2) = -2.*r(2); + res(2) = -2. * r(2); - return f*res; + return f * res; } arma::vec PenningTrap::external_B_field(arma::vec r) { - arma::vec::fixed<3> res; - res(0) = 0.; - res(1) = 0.; - res(2) = this->B_0; + arma::vec::fixed<3> res{0., 0., this->B_0}; return res; } @@ -53,14 +50,14 @@ arma::vec PenningTrap::external_B_field(arma::vec r) arma::vec PenningTrap::force_on_particle(int i, int j) { // Calculate the difference between the particles' position - arma::vec::fixed<3> res = this->particles.at(i).r_vec - - this->particles.at(j).r_vec; + arma::vec::fixed<3> res = + this->particles.at(i).r_vec - this->particles.at(j).r_vec; // Get the distance between the particles double norm = arma::norm(res); // Multiply res with p_j's charge divided by the norm cubed - res *= this->particles.at(j).q/(norm*norm*norm); + res *= this->particles.at(j).q / (norm * norm * norm); return res; } @@ -69,16 +66,13 @@ arma::vec PenningTrap::total_force_external(int i) { Particle p = this->particles.at(i); - arma::vec::fixed<3> v_cross_B; - arma::vec::fixed<3> B = this->external_B_field(p.r_vec); - v_cross_B(0) = p.v_vec(1)*B(2) - p.v_vec(2)*B(1); - v_cross_B(1) = p.v_vec(2)*B(0) - p.v_vec(0)*B(2); - v_cross_B(2) = p.v_vec(0)*B(1) - p.v_vec(1)*B(0); + arma::vec::fixed<3> v_cross_B{p.v_vec(1) * B(2) - p.v_vec(2) * B(1), + p.v_vec(2) * B(0) - p.v_vec(0) * B(2), + p.v_vec(0) * B(1) - p.v_vec(1) * B(0)}; - arma::vec force = p.q - *(this->external_E_field(p.r_vec) + v_cross_B); + arma::vec force = p.q * (this->external_E_field(p.r_vec) + v_cross_B); return force; } @@ -89,7 +83,7 @@ arma::vec PenningTrap::total_force_particles(int i) arma::vec res(3); - for (int j=0; j < this->particles.size(); j++) { + for (int j = 0; j < this->particles.size(); j++) { if (i == j) { continue; } @@ -97,7 +91,7 @@ arma::vec PenningTrap::total_force_particles(int i) res += this->force_on_particle(i, j); } - res *= K_E*(p.q/p.m); + res *= K_E * (p.q / p.m); return res; } @@ -109,7 +103,6 @@ arma::vec PenningTrap::total_force(int i) void PenningTrap::evolve_RK4(double dt) { - } void PenningTrap::evolve_forward_euler(double dt) @@ -118,11 +111,11 @@ void PenningTrap::evolve_forward_euler(double dt) Particle *p; - #pragma omp parallel for private(p) - for (int i=0; i < this->particles.size(); i++) { +#pragma omp parallel for private(p) + for (int i = 0; i < this->particles.size(); i++) { p = &new_state.at(i); - p->v_vec += dt*this->total_force(i)/new_state.at(i).m; - p->r_vec += dt*this->particles.at(i).v_vec; + p->v_vec += dt * this->total_force(i) / new_state.at(i).m; + p->r_vec += dt * this->particles.at(i).v_vec; } this->particles = new_state; -- 2.20.1 From 0e82f8cac134069fc5e8f121e0f3e964aedc5aab Mon Sep 17 00:00:00 2001 From: Cory Date: Sat, 7 Oct 2023 22:00:05 +0200 Subject: [PATCH 10/10] Add custom formatting options for clang-format --- .clang-format | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..2298bfb --- /dev/null +++ b/.clang-format @@ -0,0 +1,6 @@ +IndentWidth: 4 +AllowShortFunctionsOnASingleLine: false +BreakBeforeBraces: Custom +BraceWrapping: + AfterFunction: true + BeforeElse: true -- 2.20.1