24 return .5 * dt * this->
k_v[0][j];
26 return .5 * dt * this->k_v[1][j];
28 return dt * this->k_v[2][j];
31 * (this->k_v[0][j] + 2. * this->k_v[1][j]
32 + 2. * this->k_v[2][j] + this->k_v[3][j]));
34 std::cout <<
"Not valid!" << std::endl;
43 return .5 * dt * this->
k_r[0][j];
45 return .5 * dt * this->k_r[1][j];
47 return dt * this->k_r[2][j];
50 * (this->k_r[0][j] + 2. * this->k_r[1][j]
51 + 2. * this->k_r[2][j] + this->k_r[3][j]));
53 std::cout <<
"Not valid!" << std::endl;
77 double norm = arma::norm(res, 2);
79 return vec3((this->
particles[j].q / (norm * norm * norm)) * res);
86 if (arma::norm(p->
r_vec) > this->d) {
87 return vec3{0., 0., 0.};
91 * (this->external_E_field(p->
r_vec)
92 + arma::cross(p->
v_vec, this->external_B_field(p->
r_vec))));
99 for (
size_t j = 0; j < this->
particles.size(); j++) {
109 if (arma::norm(this->
particles[i].r_vec) > this->
d) {
110 return vec3{0., 0., 0.};
127 for (
size_t j = 0; j < i; j++) {
130 vec3(
vec3().randn() * .1 * this->d)));
135 double V_0,
double d,
double t)
144 return 1 + f * std::cos(omega_V *
t);
153 for (
size_t i = 0; i < this->
particles.size(); i++) {
166 std::vector<Particle> original_particles = this->
particles;
167 std::vector<Particle> tmp_particles = this->
particles;
176 if (this->
k_v.size() != 4 || this->
k_r.size() != 4
177 || this->
k_v[0].size() != size || this->
k_r[0].size() != size) {
183 for (
size_t i = 0; i < 4; i++) {
185#pragma omp parallel for
186 for (
size_t j = 0; j < size; j++) {
187 this->
k_v[i][j] = (this->*force)(j) / this->
particles[j].m;
190 tmp_particles[j].v_vec =
191 original_particles[j].v_vec + this->
v_func(i, j, dt);
192 tmp_particles[j].r_vec =
193 original_particles[j].r_vec + this->
r_func(i, j, dt);
204 vec3 force_res[size];
212#pragma omp parallel for
213 for (
size_t i = 0; i < size; i++) {
214 force_res[i] = (this->*force)(i);
219#pragma omp parallel for
220 for (
size_t i = 0; i < size; i++) {
229 bool particle_interaction)
231 double dt = time / (double)steps;
239 if (method ==
"rk4") {
241 }
else if (method ==
"euler") {
244 std::cout <<
"Not a valid method!" << std::endl;
248 for (
size_t j = 0; j < steps; j++) {
249 for (
size_t i = 0; i < size; i++) {
250 res.r_vecs[i][j] = this->
particles[i].r_vec;
251 res.v_vecs[i][j] = this->
particles[i].v_vec;
253 (this->*func)(dt, particle_interaction);
260 uint steps, std::string method,
261 bool particle_interaction)
263 if (path.back() !=
'/') {
266 if (
mkpath(path, 0777) != 0) {
267 std::cout <<
"Failed to make path" << std::endl;
272 this->
simulate(time, steps, method, particle_interaction);
278#pragma omp parallel for private(ofile)
279 for (
size_t i = 0; i < this->
particles.size(); i++) {
280 ofile.open(path +
"particle_" + std::to_string(i) +
"_r.txt");
281 for (
vec3 &vec : res.r_vecs[i]) {
287 ofile.open(path +
"particle_" + std::to_string(i) +
"_v.txt");
288 for (
vec3 &vec : res.v_vecs[i]) {
299 bool particle_interaction)
301 double dt = time / (double)steps;
304 if (method ==
"rk4") {
306 }
else if (method ==
"euler") {
309 std::cout <<
"Not a valid method!" << std::endl;
313 for (
size_t j = 0; j < steps; j++) {
314 (this->*func)(dt, particle_interaction);
317 int particles_left = 0;
320#pragma omp parallel for reduction(+ : particles_left)
321 for (
size_t i = 0; i < this->
particles.size(); i++) {
322 if (arma::norm(this->
particles[i].r_vec) < this->
d) {
327 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.
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.