Loading...
Searching...
No Matches
PenningTrap.cpp
Go to the documentation of this file.
#include "PenningTrap.hpp"
#include "typedefs.hpp"
#include <algorithm>
#include <functional>
#include <sys/types.h>
#include <vector>
vec3 PenningTrap::v_func(uint i, uint j, double dt)
{
switch (i) {
case 0:
return .5 * dt * this->k_v[0][j];
case 1:
return .5 * dt * this->k_v[1][j];
case 2:
return dt * this->k_v[2][j];
case 3:
return vec3((dt / 6.)
* (this->k_v[0][j] + 2. * this->k_v[1][j]
+ 2. * this->k_v[2][j] + this->k_v[3][j]));
default:
std::cout << "Not valid!" << std::endl;
abort();
}
}
vec3 PenningTrap::r_func(uint i, uint j, double dt)
{
switch (i) {
case 0:
return .5 * dt * this->k_r[0][j];
case 1:
return .5 * dt * this->k_r[1][j];
case 2:
return dt * this->k_r[2][j];
case 3:
return vec3((dt / 6.)
* (this->k_r[0][j] + 2. * this->k_r[1][j]
+ 2. * this->k_r[2][j] + this->k_r[3][j]));
default:
std::cout << "Not valid!" << std::endl;
abort();
}
}
vec3 PenningTrap::external_E_field(vec3 r)
{
r(2) *= -2.;
return vec3((this->V_0 * this->perturbation(this->t) / (this->d * this->d))
* r);
}
vec3 PenningTrap::external_B_field(vec3 r)
{
return vec3{0., 0., this->B_0};
}
vec3 PenningTrap::force_on_particle(uint i, uint j)
{
// Calculate the difference between the particles' position
vec3 res = this->particles[i].r_vec - this->particles[j].r_vec;
// Get the distance between the particles
double norm = arma::norm(res, 2);
return vec3((this->particles[j].q / (norm * norm * norm)) * res);
}
vec3 PenningTrap::total_force_external(uint i)
{
Particle *p = &this->particles[i];
if (arma::norm(p->r_vec) > this->d) {
return vec3{0., 0., 0.};
}
return vec3(p->q
* (this->external_E_field(p->r_vec)
+ arma::cross(p->v_vec, this->external_B_field(p->r_vec))));
}
vec3 PenningTrap::total_force_particles(uint i)
{
vec3 res;
for (size_t j = 0; j < this->particles.size(); j++) {
if (i != j)
res += this->force_on_particle(i, j);
}
return vec3(res * (K_E * this->particles[i].q));
}
vec3 PenningTrap::total_force(uint i)
{
if (arma::norm(this->particles[i].r_vec) > this->d) {
return vec3{0., 0., 0.};
}
return vec3(this->total_force_external(i) - this->total_force_particles(i));
}
PenningTrap::PenningTrap(double B_0, double V_0, double d, double t)
{
this->B_0 = B_0;
this->V_0 = V_0;
this->d = d;
this->t = t;
this->perturbation = [](double t) { return 1.; };
}
PenningTrap::PenningTrap(uint i, double B_0, double V_0, double d, double t)
: PenningTrap::PenningTrap(B_0, V_0, d)
{
for (size_t j = 0; j < i; j++) {
this->particles.push_back(
Particle(vec3(vec3().randn() * .1 * this->d),
vec3(vec3().randn() * .1 * this->d)));
}
}
PenningTrap::PenningTrap(std::vector<Particle> particles, double B_0,
double V_0, double d, double t)
: PenningTrap::PenningTrap(B_0, V_0, d)
{
this->particles = particles;
}
void PenningTrap::set_pertubation(double f, double omega_V)
{
this->perturbation = [f, omega_V](double t) {
return 1 + f * std::cos(omega_V * t);
};
}
void PenningTrap::reinitialize(double f, double omega_V, double t)
{
this->t = t;
this->set_pertubation(f, omega_V);
for (size_t i = 0; i < this->particles.size(); i++) {
this->particles[i].r_vec = vec3().randn() * .1 * this->d;
}
}
void PenningTrap::add_particle(Particle particle)
{
this->particles.push_back(particle);
}
void PenningTrap::evolve_RK4(double dt, bool particle_interaction)
{
std::vector<Particle> original_particles = this->particles;
std::vector<Particle> tmp_particles = this->particles;
vec3 (PenningTrap::*force)(uint) = particle_interaction
? &PenningTrap::total_force
: &PenningTrap::total_force_external;
size_t size = this->particles.size();
// Allocating takes a long time, so reuse sim_arr if possible
if (this->k_v.size() != 4 || this->k_r.size() != 4
|| this->k_v[0].size() != size || this->k_r[0].size() != size) {
this->k_v = sim_arr(4, sim_cols(size));
this->k_r = sim_arr(4, sim_cols(size));
}
// Each k_{i+1} is dependent on k_i, so outer loop is not parallelizable
for (size_t i = 0; i < 4; i++) {
// Inner loop is able to be parallelized
#pragma omp parallel for
for (size_t j = 0; j < size; j++) {
this->k_v[i][j] = (this->*force)(j) / this->particles[j].m;
this->k_r[i][j] = this->particles[j].v_vec;
tmp_particles[j].v_vec =
original_particles[j].v_vec + this->v_func(i, j, dt);
tmp_particles[j].r_vec =
original_particles[j].r_vec + this->r_func(i, j, dt);
}
this->particles = tmp_particles;
}
this->t += dt;
}
void PenningTrap::evolve_forward_euler(double dt, bool particle_interaction)
{
size_t size = this->particles.size();
vec3 force_res[size];
vec3 (PenningTrap::*force)(uint) = particle_interaction
? &PenningTrap::total_force
: &PenningTrap::total_force_external;
// Calculating the force for each particle is independent and therefore
// a good candidate for parallel execution
#pragma omp parallel for
for (size_t i = 0; i < size; i++) {
force_res[i] = (this->*force)(i);
}
// Updating the particles is also independent, so we can parallelize
// this as well
#pragma omp parallel for
for (size_t i = 0; i < size; i++) {
this->particles[i].r_vec += dt * this->particles[i].v_vec;
this->particles[i].v_vec += dt * force_res[i] / this->particles[i].m;
}
this->t += dt;
}
simulation_t PenningTrap::simulate(double time, uint steps, std::string method,
bool particle_interaction)
{
double dt = time / (double)steps;
uint size = this->particles.size();
simulation_t res{sim_arr(size, sim_cols(steps)),
sim_arr(size, sim_cols(steps))};
void (PenningTrap::*func)(double, bool);
if (method == "rk4") {
func = &PenningTrap::evolve_RK4;
} else if (method == "euler") {
func = &PenningTrap::evolve_forward_euler;
} else {
std::cout << "Not a valid method!" << std::endl;
abort();
}
for (size_t j = 0; j < steps; j++) {
for (size_t i = 0; i < size; i++) {
res.r_vecs[i][j] = this->particles[i].r_vec;
res.v_vecs[i][j] = this->particles[i].v_vec;
}
(this->*func)(dt, particle_interaction);
}
return res;
}
void PenningTrap::write_simulation_to_dir(std::string path, double time,
uint steps, std::string method,
bool particle_interaction)
{
if (path.back() != '/') {
path += '/';
}
if (mkpath(path, 0777) != 0) {
std::cout << "Failed to make path" << std::endl;
abort();
}
simulation_t res =
this->simulate(time, steps, method, particle_interaction);
std::ofstream ofile;
// Writing each particle to its own file is independent and can be run in
// parallel.
#pragma omp parallel for private(ofile)
for (size_t i = 0; i < this->particles.size(); i++) {
ofile.open(path + "particle_" + std::to_string(i) + "_r.txt");
for (vec3 &vec : res.r_vecs[i]) {
ofile << scientific_format(vec(0), 10, 8) << ','
<< scientific_format(vec(1), 10, 8) << ','
<< scientific_format(vec(2), 10, 8) << '\n';
}
ofile.close();
ofile.open(path + "particle_" + std::to_string(i) + "_v.txt");
for (vec3 &vec : res.v_vecs[i]) {
ofile << scientific_format(vec(0), 10, 8) << ','
<< scientific_format(vec(1), 10, 8) << ','
<< scientific_format(vec(2), 10, 8) << '\n';
}
ofile.close();
}
}
double PenningTrap::fraction_of_particles_left(double time, uint steps,
std::string method,
bool particle_interaction)
{
double dt = time / (double)steps;
void (PenningTrap::*func)(double, bool);
if (method == "rk4") {
func = &PenningTrap::evolve_RK4;
} else if (method == "euler") {
func = &PenningTrap::evolve_forward_euler;
} else {
std::cout << "Not a valid method!" << std::endl;
abort();
}
for (size_t j = 0; j < steps; j++) {
(this->*func)(dt, particle_interaction);
}
int particles_left = 0;
// A reduction is perfect here
#pragma omp parallel for reduction(+ : particles_left)
for (size_t i = 0; i < this->particles.size(); i++) {
if (arma::norm(this->particles[i].r_vec) < this->d) {
particles_left++;
}
}
return (double)particles_left / (double)this->particles.size();
}
PenningTrap.hpp
A class for simulating a Penning trap.
Particle
A class that holds attributes of a particle.
Definition: Particle.hpp:23
Particle::r_vec
vec3 r_vec
position
Definition: Particle.hpp:25
Particle::v_vec
vec3 v_vec
velocity
Definition: Particle.hpp:26
Particle::q
double q
Charge.
Definition: Particle.hpp:27
PenningTrap
A class that simulates a Penning trap.
Definition: PenningTrap.hpp:32
PenningTrap::particles
std::vector< Particle > particles
The particles in the Penning trap.
Definition: PenningTrap.hpp:42
PenningTrap::B_0
double B_0
Magnetic field strength.
Definition: PenningTrap.hpp:37
PenningTrap::total_force_external
vec3 total_force_external(uint i)
Calculate the total external force on a particle.
Definition: PenningTrap.cpp:82
PenningTrap::k_r
sim_arr k_r
A 2D vector containing all where is the index of a particle.
Definition: PenningTrap.hpp:52
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::evolve_RK4
void evolve_RK4(double dt, bool particle_interaction=true)
Go forward one timestep using the RK4 method.
Definition: PenningTrap.cpp:163
PenningTrap::v_func
vec3 v_func(uint i, uint j, double dt)
Helper for evolve_RK4 when calculating values.
Definition: PenningTrap.cpp:20
PenningTrap::external_E_field
vec3 external_E_field(vec3 r)
Calculate E at point r.
Definition: PenningTrap.cpp:58
PenningTrap::PenningTrap
PenningTrap(double B_0=T, double V_0=(25. *V)/1000., double d=500., double t=0.)
Constructor for the PenningTrap class.
Definition: PenningTrap.cpp:115
PenningTrap::d
double d
Characteristic dimension.
Definition: PenningTrap.hpp:40
PenningTrap::add_particle
void add_particle(Particle particle)
Add a particle to the system.
Definition: PenningTrap.cpp:158
PenningTrap::V_0
double V_0
Applied potential.
Definition: PenningTrap.hpp:38
PenningTrap::simulate
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.
Definition: PenningTrap.cpp:228
PenningTrap::force_on_particle
vec3 force_on_particle(uint i, uint j)
Calculate the force between 2 particles.
Definition: PenningTrap.cpp:71
PenningTrap::r_func
vec3 r_func(uint i, uint j, double dt)
Helper for evolve_RK4 when calculating values.
Definition: PenningTrap.cpp:39
PenningTrap::t
double t
Current time.
Definition: PenningTrap.hpp:41
PenningTrap::total_force
vec3 total_force(uint i)
calculate the total force on a particle p_i.
Definition: PenningTrap.cpp:107
PenningTrap::set_pertubation
void set_pertubation(double f, double omega_V)
Time dependent perturbation to V_0.
Definition: PenningTrap.cpp:141
PenningTrap::reinitialize
void reinitialize(double f, double omega_V, double t=0.)
Give all particles new positions and velocities, and change t and V_0.
Definition: PenningTrap.cpp:148
PenningTrap::evolve_forward_euler
void evolve_forward_euler(double dt, bool particle_interaction=true)
Go forward one timestep using the forward Euler method.
Definition: PenningTrap.cpp:201
PenningTrap::fraction_of_particles_left
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...
Definition: PenningTrap.cpp:297
PenningTrap::write_simulation_to_dir
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.
Definition: PenningTrap.cpp:259
PenningTrap::perturbation
std::function< double(double)> perturbation
Time-dependent perturbation.
Definition: PenningTrap.hpp:39
PenningTrap::k_v
sim_arr k_v
A 2D vector containing all where is the index of a particle.
Definition: PenningTrap.hpp:47
K_E
#define K_E
Coulomb constant. unit: .
Definition: constants.hpp:17
simulation
Typedef for PenningTrap::simulation return value.
Definition: typedefs.hpp:40
typedefs.hpp
Useful typedefs for cleaner code.
vec3
arma::vec::fixed< 3 > vec3
Typedef for a fixed 3d arma vector.
Definition: typedefs.hpp:23
sim_cols
std::vector< vec3 > sim_cols
Typedef for the column of the result vector from simulating particles.
Definition: typedefs.hpp:28
sim_arr
std::vector< sim_cols > sim_arr
Typedef for the result of the simulate method.
Definition: typedefs.hpp:36
mkpath
bool mkpath(std::string path, int mode=0777)
Make path given.
Definition: utils.cpp:72
scientific_format
std::string scientific_format(double d, int width=20, int prec=10)
Turns a double into a string written in scientific format.
Definition: utils.cpp:15