Penning Trap Simulation Simulate particle behavior inside a Penning Trap

Loading...
Searching...
No Matches
test_suite.cpp
Go to the documentation of this file.
1
13#include <iomanip>
14#include <sstream>
15#include <string>
16
17#include "PenningTrap.hpp"
18#include "utils.hpp"
19
22 class PenningTrapTest {
23 public:
26 static void test_external_E_field()
27 {
28 PenningTrap trap;
29
30 // Vector containing inputs and expected results
31 std::vector<std::pair<vec_3d, vec_3d>> tests;
32
33 tests.push_back(
34 std::make_pair(vec_3d{0., 0., 0.}, vec_3d{0., 0., 0.}));
35
36 tests.push_back(std::make_pair(vec_3d{10., 0., 0.},
37 vec_3d{96.4852558, 0., 0.}));
38
39 tests.push_back(std::make_pair(vec_3d{10., 0., 0.},
40 vec_3d{96.4852558, 0., 0.}));
41
42 tests.push_back(std::make_pair(vec_3d{0., 10., 0.},
43 vec_3d{0., 96.4852558, 0.}));
44
45 tests.push_back(std::make_pair(vec_3d{0., 0., 10.},
46 vec_3d{0., 0., -192.9705116}));
47
48 vec_3d result;
49 vec_3d v;
50 std::stringstream msg;
51 for (size_t i = 0; i < tests.size(); i++) {
52 v = tests.at(i).first;
53 result = trap.external_E_field(v);
54
55 msg.str("");
56 msg << "Testing the external E field at (" << std::setprecision(2)
57 << v(0) << "," << v(1) << "," << v(2) << ").";
58
59 ASSERT(close_to(result, tests.at(i).second), msg.str());
60 }
61 }
62
65 static void test_external_B_field()
66 {
67 // No point in testing at different points since it's not dependent
68 // on position.
69 PenningTrap trap;
70 vec_3d expected{0., 0., T};
71 vec_3d result = trap.external_B_field(vec_3d{0., 0., 0.});
72 ASSERT(close_to(expected, result),
73 "Testing the external B field at (0,0,0)");
74 }
75
78 static void test_force_on_particle()
79 {
80 PenningTrap trap;
81 vec_3d v{0., 0., 0.};
82
83 // Add particles to test
84 trap.add_particle(Particle(1., 40., vec_3d{0., 0., 0.}, v));
85 trap.add_particle(Particle(1., 40., vec_3d{1., 0., 0.}, v));
86 trap.add_particle(Particle(1., 40., vec_3d{0., 3., 4.}, v));
87
88 // Test p0 and p1
89 vec_3d expected{-1., 0., 0.};
90 vec_3d result = trap.force_on_particle(0, 1);
91 ASSERT(close_to(expected, result),
92 "Testing the force on a particle at (0,0,0) from a "
93 "particle at (1,0,0).");
94
95 // Test p0 and p2
96 expected = vec_3d{0, -.024, -.032};
97 result = trap.force_on_particle(0, 2);
98 ASSERT(close_to(expected, result),
99 "Testing the force on a particle at (0,0,0) from a "
100 "particle at (0,3,4).");
101 }
102
105 static void test_total_force_external()
106 {
107 PenningTrap trap;
108 trap.add_particle(
109 Particle(1., 40., vec_3d{1., 2., 3.}, vec_3d{3., 4., 5.}));
110
111 vec_3d expected{395.58954878, -270.15871624, -57.89115348};
112 vec_3d result = trap.total_force_external(0);
113 ASSERT(close_to(expected, result),
114 "Testing the total external force on a particle at "
115 "(1,2,3) with velocity (3,4,5)");
116 }
117
121 static void test_total_force_particles()
122 {
123 PenningTrap trap;
124 trap.add_particle(
125 Particle(1., 40., vec_3d{0., 0., 0.}, vec_3d{0., 0., 0.}));
126
127 vec_3d expected{0., 0., 0.};
128 vec_3d result = trap.total_force_particles(0);
129 ASSERT(close_to(expected, result),
130 "Testing the total force of all particles on particle 0 "
131 "with only a single particle");
132
133 trap.add_particle(
134 Particle(1., 40., vec_3d{1., 0., 0.}, vec_3d{0., 0., 0.}));
135 trap.add_particle(
136 Particle(1., 40., vec_3d{0., 1., 0.}, vec_3d{0., 0., 0.}));
137 trap.add_particle(
138 Particle(1., 40., vec_3d{0., 0., 1.}, vec_3d{0., 0., 0.}));
139
140 expected = vec_3d().fill(-3473.383325);
141 result = trap.total_force_particles(0);
142 ASSERT(close_to(expected, result),
143 "Testing the total force of all particles on particle 0 "
144 "with 3 other particles.");
145 }
146};
147
148 int main()
149{
150 PenningTrapTest::test_external_E_field();
151 PenningTrapTest::test_external_B_field();
152 PenningTrapTest::test_force_on_particle();
153 PenningTrapTest::test_total_force_external();
154 PenningTrapTest::test_total_force_particles();
155 return 0;
156}
PenningTrap.hpp
A class for simulating a Penning trap.
Particle
A class that holds attributes of a particle.
Definition: Particle.hpp:21
PenningTrapTest
Test class for the Penning trap.
Definition: test_suite.cpp:22
PenningTrapTest::test_total_force_particles
static void test_total_force_particles()
Test that the total force of all particles on a single particle returns expected results.
Definition: test_suite.cpp:121
PenningTrapTest::test_external_E_field
static void test_external_E_field()
Test that the external E field gives correct values.
Definition: test_suite.cpp:26
PenningTrapTest::test_external_B_field
static void test_external_B_field()
Test that the external B field gives correct values.
Definition: test_suite.cpp:65
PenningTrapTest::test_force_on_particle
static void test_force_on_particle()
Test that the force between particles gives expected results.
Definition: test_suite.cpp:78
PenningTrapTest::test_total_force_external
static void test_total_force_external()
Test that the total external force returns expected results.
Definition: test_suite.cpp:105
PenningTrap
A class that simulates a Penning trap.
Definition: PenningTrap.hpp:31
PenningTrap::external_E_field
vec_3d external_E_field(vec_3d r)
Calculate E at point r.
Definition: PenningTrap.cpp:84
PenningTrap::total_force_particles
vec_3d total_force_particles(unsigned int i)
Calculate the total force on a particle p_i from other particles.
Definition: PenningTrap.cpp:123
PenningTrap::add_particle
void add_particle(Particle particle)
Add a particle to the system.
Definition: PenningTrap.cpp:79
PenningTrap::force_on_particle
vec_3d force_on_particle(unsigned int i, unsigned int j)
Calculate the force between 2 particles.
Definition: PenningTrap.cpp:97
PenningTrap::external_B_field
vec_3d external_B_field(vec_3d r)
Calculate B at point r.
Definition: PenningTrap.cpp:92
PenningTrap::total_force_external
vec_3d total_force_external(unsigned int i)
Calculate the total external force on a particle.
Definition: PenningTrap.cpp:108
T
#define T
1 Tesla. unit:
Definition: constants.hpp:17
vec_3d
arma::vec::fixed< 3 > vec_3d
Typedef for a fixed 3d arma vector.
Definition: typedefs.hpp:36
utils.hpp
Function prototypes and macros that are useful.
ASSERT
#define ASSERT(expr, msg)
A prettier assertion function.
Definition: utils.hpp:46
close_to
bool close_to(arma::vec &a, arma::vec &b, double tol=1e-8)
Test if two armadillo vectors are close to each other.
Definition: utils.cpp:60