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 "constants.hpp"
19#include "utils.hpp"
20
23 class PenningTrapTest
24{
25 public:
28 void test_external_E_field()
29 {
30 PenningTrap trap;
31
32 // Vector containing inputs and expected results
33 std::vector<std::pair<vec3, vec3>> tests;
34
35 tests.push_back(std::make_pair(vec3{0., 0., 0.}, vec3{0., 0., 0.}));
36
37 tests.push_back(
38 std::make_pair(vec3{10., 0., 0.}, vec3{96.4852558, 0., 0.}));
39
40 tests.push_back(
41 std::make_pair(vec3{10., 0., 0.}, vec3{96.4852558, 0., 0.}));
42
43 tests.push_back(
44 std::make_pair(vec3{0., 10., 0.}, vec3{0., 96.4852558, 0.}));
45
46 tests.push_back(
47 std::make_pair(vec3{0., 0., 10.}, vec3{0., 0., -192.9705116}));
48
49 vec3 result;
50 vec3 v;
51 std::stringstream msg;
52 for (size_t i = 0; i < tests.size(); i++) {
53 v = tests.at(i).first;
54 result = trap.external_E_field(v);
55
56 msg.str("");
57 msg << "Testing the external E field at (" << std::setprecision(2)
58 << v(0) << "," << v(1) << "," << v(2) << ").";
59
60 ASSERT(close_to(result, tests.at(i).second), msg.str());
61 }
62 }
63
66 void test_external_B_field()
67 {
68 // No point in testing at different points since it's not dependent
69 // on position.
70 PenningTrap trap;
71 vec3 expected{0., 0., T};
72 vec3 result = trap.external_B_field(vec3{0., 0., 0.});
73 ASSERT(close_to(expected, result),
74 "Testing the external B field at (0,0,0)");
75 }
76
80 void test_force_on_particle()
81 {
82 PenningTrap trap;
83 vec3 v{0., 0., 0.};
84
85 // Add particles to test
86 trap.add_particle(Particle(vec3{0., 0., 0.}, v));
87 trap.add_particle(Particle(vec3{1., 0., 0.}, v));
88 trap.add_particle(Particle(vec3{0., 3., 4.}, v));
89
90 // Test p0 and p1
91 vec3 expected{-1., 0., 0.};
92 vec3 result = trap.force_on_particle(0, 1);
93 ASSERT(close_to(expected, result),
94 "Testing the force on a particle at (0,0,0) from a "
95 "particle at (1,0,0).");
96
97 // Test p0 and p2
98 expected = vec3{0, -.024, -.032};
99 result = trap.force_on_particle(0, 2);
100 ASSERT(close_to(expected, result),
101 "Testing the force on a particle at (0,0,0) from a "
102 "particle at (0,3,4).");
103 }
104
107 void test_total_force_external()
108 {
109 PenningTrap trap;
110 trap.add_particle(Particle(vec3{1., 2., 3.}, vec3{3., 4., 5.}));
111
112 vec3 expected{395.58954878, -270.15871624, -57.89115348};
113 vec3 result = trap.total_force_external(0);
114 ASSERT(close_to(expected, result),
115 "Testing the total external force on a particle at "
116 "(1,2,3) with velocity (3,4,5)");
117 }
118
122 void test_total_force_particles()
123 {
124 PenningTrap trap;
125 trap.add_particle(Particle(vec3{0., 0., 0.}, vec3{0., 0., 0.}));
126
127 vec3 expected{0., 0., 0.};
128 vec3 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(Particle(vec3{1., 0., 0.}, vec3{0., 0., 0.}));
134 trap.add_particle(Particle(vec3{0., 1., 0.}, vec3{0., 0., 0.}));
135 trap.add_particle(Particle(vec3{0., 0., 1.}, vec3{0., 0., 0.}));
136
137 expected = vec3().fill(-138935.333);
138 result = trap.total_force_particles(0);
139 ASSERT(close_to(expected, result),
140 "Testing the total force of all particles on particle 0 "
141 "with 3 other particles.");
142 }
143};
144
145 int main()
146{
147 PenningTrapTest test;
148 test.test_external_E_field();
149 test.test_external_B_field();
150 test.test_force_on_particle();
151 test.test_total_force_external();
152 test.test_total_force_particles();
153 return 0;
154}
PenningTrap.hpp
A class for simulating a Penning trap.
Particle
A class that holds attributes of a particle.
Definition: Particle.hpp:23
PenningTrapTest
Test class for the Penning trap.
Definition: test_suite.cpp:24
PenningTrapTest::test_force_on_particle
void test_force_on_particle()
Test that the force between particles gives expected results.
Definition: test_suite.cpp:80
PenningTrapTest::test_total_force_particles
void test_total_force_particles()
Test that the total force of all particles on a single particle returns expected results.
Definition: test_suite.cpp:122
PenningTrapTest::test_external_E_field
void test_external_E_field()
Test that the external E field gives correct values.
Definition: test_suite.cpp:28
PenningTrapTest::test_external_B_field
void test_external_B_field()
Test that the external B field gives correct values.
Definition: test_suite.cpp:66
PenningTrapTest::test_total_force_external
void test_total_force_external()
Test that the total external force returns expected results.
Definition: test_suite.cpp:107
PenningTrap
A class that simulates a Penning trap.
Definition: PenningTrap.hpp:32
PenningTrap::total_force_external
vec3 total_force_external(uint i)
Calculate the total external force on a particle.
Definition: PenningTrap.cpp:82
PenningTrap::total_force_particles
vec3 total_force_particles(uint i)
Calculate the total force on a particle p_i from other particles.
Definition: PenningTrap.cpp:95
PenningTrap::external_B_field
vec3 external_B_field(vec3 r)
Calculate B at point r.
Definition: PenningTrap.cpp:66
PenningTrap::external_E_field
vec3 external_E_field(vec3 r)
Calculate E at point r.
Definition: PenningTrap.cpp:58
PenningTrap::add_particle
void add_particle(Particle particle)
Add a particle to the system.
Definition: PenningTrap.cpp:158
PenningTrap::force_on_particle
vec3 force_on_particle(uint i, uint j)
Calculate the force between 2 particles.
Definition: PenningTrap.cpp:71
constants.hpp
Library of constants.
T
#define T
1 Tesla. unit:
Definition: constants.hpp:21
vec3
arma::vec::fixed< 3 > vec3
Typedef for a fixed 3d arma vector.
Definition: typedefs.hpp:23
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:58