Penning Trap Simulation
Simulate particle behavior inside a Penning Trap
Loading...
Searching...
No Matches
PenningTrap.cpp
Go to the documentation of this file.
1
16#include "utils.hpp"
17#include "PenningTrap.hpp"
18#include "constants.hpp"
19#include <algorithm>
20#include <stdexcept>
21#include <omp.h>
22
23#pragma omp declare reduction( + : arma::vec : omp_out += omp_in ) \
24 initializer( omp_priv = omp_orig )
25
26PenningTrap::PenningTrap(double B_0, double V_0, double d)
27{
28 this->B_0 = B_0;
29 this->V_0 = V_0;
30 this->d = d;
31}
32
34{
35 this->particles.push_back(particle);
36}
37
38arma::vec PenningTrap::external_E_field(arma::vec r)
39{
40 arma::vec::fixed<3> res;
41 double f = this->V_0/(this->d*this->d);
42 res(0) = r(0);
43 res(1) = r(1);
44 res(2) = -2.*r(2);
45
46 return f*res;
47}
48
49arma::vec PenningTrap::external_B_field(arma::vec r)
50{
51 arma::vec::fixed<3> res;
52 res(0) = 0.;
53 res(1) = 0.;
54 res(2) = this->B_0;
55
56 return res;
57}
58
59arma::vec PenningTrap::force_on_particle(int i, int j)
60{
61 // Calculate the difference between the particles' position
62 arma::vec::fixed<3> res = this->particles.at(i).r_vec
63 - this->particles.at(j).r_vec;
64
65 // Get the distance between the particles
66 double norm = arma::norm(res);
67
68 // Multiply res with p_j's charge divided by the norm cubed
69 res *= this->particles.at(j).q/(norm*norm*norm);
70
71 return res;
72}
73
75{
76 Particle p = this->particles.at(i);
77
78 arma::vec::fixed<3> v_cross_B;
79
80 arma::vec::fixed<3> B = this->external_B_field(p.r_vec);
81
82 v_cross_B(0) = p.v_vec(1)*B(2) - p.v_vec(2)*B(1);
83 v_cross_B(1) = p.v_vec(2)*B(0) - p.v_vec(0)*B(2);
84 v_cross_B(2) = p.v_vec(0)*B(1) - p.v_vec(1)*B(0);
85
86 arma::vec force = p.q
87 *(this->external_E_field(p.r_vec) + v_cross_B);
88
89 return force;
90}
91
93{
94 Particle p = this->particles.at(i);
95
96 arma::vec res(3);
97
98 for (int j=0; j < this->particles.size(); j++) {
99 if (i == j) {
100 continue;
101 }
102
103 res += this->force_on_particle(i, j);
104 }
105
106 res *= K_E*(p.q/p.m);
107
108 return res;
109}
110
112{
113 return this->total_force_external(i) - this->total_force_particles(i);
114}
115
117{
118
119}
120
122{
123 std::vector<Particle> new_state = this->particles;
124
125 Particle *p;
126
127 #pragma omp parallel for private(p)
128 for (int i=0; i < this->particles.size(); i++) {
129 p = &new_state.at(i);
130 p->v_vec += dt*this->total_force(i)/new_state.at(i).m;
131 p->r_vec += dt*this->particles.at(i).v_vec;
132 }
133
134 this->particles = new_state;
135}
136
137arma::vec PenningTrap::get_particle(int i)
138{
139 return this->particles.at(i).r_vec;
140}
141
142double PenningTrap::get_d()
143{
144 return this->d;
145}
A class for simulating a Penning trap.
A class that holds attributes of a particle.
Definition: Particle.hpp:19
arma::vec::fixed< 3 > v_vec
velocity
Definition: Particle.hpp:24
double q
Charge.
Definition: Particle.hpp:21
arma::vec::fixed< 3 > r_vec
position
Definition: Particle.hpp:23
double m
Mass.
Definition: Particle.hpp:22
std::vector< Particle > particles
The particles in the Penning trap.
Definition: PenningTrap.hpp:30
arma::vec total_force_external(int i)
Calculate the total external force on a particle.
Definition: PenningTrap.cpp:74
double B_0
Magnetic field strength.
Definition: PenningTrap.hpp:27
arma::vec total_force_particles(int i)
Calculate the total force on a particle from other particles.
Definition: PenningTrap.cpp:92
arma::vec external_B_field(arma::vec r)
Calculate B at point r.
Definition: PenningTrap.cpp:49
arma::vec force_on_particle(int i, int j)
Calculate the force between 2 particles.
Definition: PenningTrap.cpp:59
void evolve_forward_euler(double dt)
Go forward one timestep using the forward Euler method.
double d
Characteristic dimension.
Definition: PenningTrap.hpp:29
void add_particle(Particle particle)
Add a particle to the system.
Definition: PenningTrap.cpp:33
double V_0
Applied potential.
Definition: PenningTrap.hpp:28
PenningTrap(double B_0=T, double V_0=25.*V/1000., double d=500.)
Set B_0, V_0 and d.
Definition: PenningTrap.cpp:26
arma::vec total_force(int i)
calculate the total force on a particle.
arma::vec external_E_field(arma::vec r)
Calculate E at point r.
Definition: PenningTrap.cpp:38
void evolve_RK4(double dt)
Go forward one timestep using the RK4 method.
Library of constants.
#define K_E
Coulomb constant. unit: .
Definition: constants.hpp:15
Function prototypes and macros that are useful.