25 return .5 * dt * this->
k_v[0][j];
27 return .5 * dt * this->k_v[1][j];
29 return dt * this->k_v[2][j];
32 * (this->k_v[0][j] + 2. * this->k_v[1][j]
33 + 2. * this->k_v[2][j] + this->k_v[3][j]));
35 std::cout <<
"Not valid!" << std::endl;
44 return .5 * dt * this->
k_r[0][j];
46 return .5 * dt * this->k_r[1][j];
48 return dt * this->k_r[2][j];
51 * (this->k_r[0][j] + 2. * this->k_r[1][j]
52 + 2. * this->k_r[2][j] + this->k_r[3][j]));
54 std::cout <<
"Not valid!" << std::endl;
78 double norm = arma::norm(res, 2);
80 return vec3((this->
particles[j].q / (norm * norm * norm)) * res);
88 * (this->external_E_field(p->
r_vec)
89 + arma::cross(p->
v_vec, this->external_B_field(p->
r_vec))));
96 for (
size_t j = 0; j < this->
particles.size(); j++) {
106 if (arma::norm(this->
particles[i].r_vec) > this->
d) {
107 return vec3{0., 0., 0.};
114 if (arma::norm(this->
particles[i].r_vec) > this->
d) {
115 return vec3{0., 0., 0.};
132 for (
size_t j = 0; j < i; j++) {
135 vec3(
vec3().randn() * .1 * this->d)));
140 double V_0,
double d,
double t)
149 return 1 + f * std::cos(omega_V *
t);
159 for (
size_t i = 0; i < this->
particles.size(); i++) {
176 std::vector<Particle> original_particles = this->
particles;
177 std::vector<Particle> tmp_particles = this->
particles;
186 if (this->
k_v.size() != 4 || this->
k_r.size() != 4
187 || this->
k_v[0].size() != size || this->
k_r[0].size() != size) {
193 for (
size_t i = 0; i < 4; i++) {
195#pragma omp parallel for private(p)
196 for (
size_t j = 0; j < size; j++) {
197 this->
k_v[i][j] = (this->*force)(j) / this->
particles[j].m;
200 p = &tmp_particles[j];
202 p->
v_vec = original_particles[j].v_vec + this->
v_func(i, j, dt);
203 p->
r_vec = original_particles[j].r_vec + this->
r_func(i, j, dt);
214 vec3 force_res[size];
223#pragma omp parallel for
224 for (
size_t i = 0; i < size; i++) {
225 force_res[i] = (this->*force)(i);
230#pragma omp parallel for
231 for (
size_t i = 0; i < size; i++) {
234 p->
v_vec += dt * force_res[i] / p->
m;
241 bool particle_interaction)
244 double dt = time / (double)steps;
252 if (method ==
"rk4") {
254 }
else if (method ==
"euler") {
257 std::cout <<
"Not a valid method!" << std::endl;
261 for (
size_t j = 0; j < steps; j++) {
262 for (
size_t i = 0; i < size; i++) {
264 res.r_vecs[i][j] = p->
r_vec;
265 res.v_vecs[i][j] = p->
v_vec;
267 (this->*func)(dt, particle_interaction);
274 uint steps, std::string method,
275 bool particle_interaction)
277 if (path.back() !=
'/') {
280 if (
mkpath(path, 0777) != 0) {
281 std::cout <<
"Failed to make path" << std::endl;
286 this->
simulate(time, steps, method, particle_interaction);
292#pragma omp parallel for private(ofile)
293 for (
size_t i = 0; i < this->
particles.size(); i++) {
294 ofile.open(path +
"particle_" + std::to_string(i) +
"_r.txt");
295 for (
vec3 &vec : res.r_vecs[i]) {
301 ofile.open(path +
"particle_" + std::to_string(i) +
"_v.txt");
302 for (
vec3 &vec : res.v_vecs[i]) {
313 bool particle_interaction)
315 double dt = time / (double)steps;
318 if (method ==
"rk4") {
320 }
else if (method ==
"euler") {
323 std::cout <<
"Not a valid method!" << std::endl;
327 for (
size_t j = 0; j < steps; j++) {
328 (this->*func)(dt, particle_interaction);
331 int particles_left = 0;
334#pragma omp parallel for reduction(+ : particles_left)
335 for (
size_t i = 0; i < this->
particles.size(); i++) {
336 if (arma::norm(this->
particles[i].r_vec) < this->
d) {
341 return (
double)particles_left / (double)this->
particles.size();
A class for simulating a Penning trap.
A class that holds attributes of a particle.
A class that simulates a Penning trap.
std::vector< Particle > particles
The particles in the Penning trap.
double B_0
Magnetic field strength.
vec3 total_force_external(uint i)
Calculate the total external force on a particle.
sim_arr k_r
A 2D vector containing all where is the index of a particle.
vec3 total_force_particles(uint i)
Calculate the total force on a particle p_i from other particles.
vec3 external_B_field(vec3 r)
Calculate B at point r.
void evolve_RK4(double dt, bool particle_interaction=true)
Go forward one timestep using the RK4 method.
vec3 v_func(uint i, uint j, double dt)
Helper for evolve_RK4 when calculating values.
vec3 external_E_field(vec3 r)
Calculate E at point r.
PenningTrap(double B_0=T, double V_0=(25. *V)/1000., double d=500., double t=0.)
Constructor for the PenningTrap class.
vec3 total_force_no_interaction(uint i)
calculate the total force on a particle p_i without interaction
double d
Characteristic dimension.
void add_particle(Particle particle)
Add a particle to the system.
double V_0
Applied potential.
simulation_t simulate(double time, uint steps, std::string method="rk4", bool particle_interaction=true)
Simulate the particle system inside the Penning trap over a certain amount of time.
vec3 force_on_particle(uint i, uint j)
Calculate the force between 2 particles.
vec3 r_func(uint i, uint j, double dt)
Helper for evolve_RK4 when calculating values.
vec3 total_force(uint i)
calculate the total force on a particle p_i.
void set_pertubation(double f, double omega_V)
Time dependent perturbation to V_0.
void reinitialize(double f, double omega_V, double t=0.)
Give all particles new positions and velocities, and change t and V_0.
void evolve_forward_euler(double dt, bool particle_interaction=true)
Go forward one timestep using the forward Euler method.
double fraction_of_particles_left(double time, uint steps, std::string method="rk4", bool particle_interaction=true)
Simulate and calculate what fraction of particles are still left inside the Penning trap after the si...
void write_simulation_to_dir(std::string path, double time, uint steps, std::string method="rk4", bool particle_interaction=true)
Simulate and write the displacement of all particles to files.
std::function< double(double)> perturbation
Time-dependent perturbation.
sim_arr k_v
A 2D vector containing all where is the index of a particle.
#define K_E
Coulomb constant. unit: .
Typedef for PenningTrap::simulation return value.
Useful typedefs for cleaner code.
arma::vec::fixed< 3 > vec3
Typedef for a fixed 3d arma vector.
std::vector< vec3 > sim_cols
Typedef for the column of the result vector from simulating particles.
std::vector< sim_cols > sim_arr
Typedef for the result of the simulate method.
bool mkpath(std::string path, int mode=0777)
Make path given.
std::string scientific_format(double d, int width=20, int prec=10)
Turns a double into a string written in scientific format.