Refactor
- Change PenningTrap so that perturbation is its own field - Add Calcium mass and charge to constants
This commit is contained in:
@@ -14,45 +14,52 @@
|
||||
#include "typedefs.hpp"
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <sys/types.h>
|
||||
#include <vector>
|
||||
|
||||
PenningTrap::PenningTrap(double B_0, std::function<double(double)> V_0,
|
||||
double d, double t)
|
||||
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(unsigned int i, double B_0,
|
||||
std::function<double(double)> V_0, double d, double t)
|
||||
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(1., 40., vec_3d(vec_3d().randn() * .1 * this->d),
|
||||
vec_3d(vec_3d().randn() * .1 * this->d)));
|
||||
this->particles.push_back(
|
||||
Particle(1., 40., vec_3d(vec_3d().randn() * .1 * this->d),
|
||||
vec_3d(vec_3d().randn() * .1 * this->d)));
|
||||
}
|
||||
}
|
||||
|
||||
PenningTrap::PenningTrap(std::vector<Particle> particles, double B_0,
|
||||
std::function<double(double)> V_0, double d, double t)
|
||||
double V_0, double d, double t)
|
||||
: PenningTrap::PenningTrap(B_0, V_0, d)
|
||||
{
|
||||
this->particles = particles;
|
||||
}
|
||||
|
||||
void PenningTrap::reinitialize(std::function<double(double)> V_0, double t)
|
||||
void PenningTrap::set_pertubation(double f, double omega_V)
|
||||
{
|
||||
this->perturbation
|
||||
= [f, omega_V](double t) { return f * std::cos(omega_V * t); };
|
||||
}
|
||||
|
||||
void PenningTrap::reinitialize(double f, double omega_V, double t)
|
||||
{
|
||||
this->V_0 = V_0;
|
||||
this->t = t;
|
||||
this->set_pertubation(f, omega_V);
|
||||
|
||||
for (size_t i = 0; i < this->particles.size(); i++) {
|
||||
this->particles[i].r_vec = vec_3d().randn() * .1 * this->d;
|
||||
}
|
||||
}
|
||||
|
||||
vec_3d PenningTrap::v_func(unsigned int i, unsigned int j, double dt)
|
||||
vec_3d PenningTrap::v_func(uint i, uint j, double dt)
|
||||
{
|
||||
switch (i) {
|
||||
case 0:
|
||||
@@ -71,7 +78,7 @@ vec_3d PenningTrap::v_func(unsigned int i, unsigned int j, double dt)
|
||||
}
|
||||
}
|
||||
|
||||
vec_3d PenningTrap::r_func(unsigned int i, unsigned int j, double dt)
|
||||
vec_3d PenningTrap::r_func(uint i, uint j, double dt)
|
||||
{
|
||||
switch (i) {
|
||||
case 0:
|
||||
@@ -99,7 +106,8 @@ vec_3d PenningTrap::external_E_field(vec_3d r)
|
||||
{
|
||||
r(2) *= -2.;
|
||||
|
||||
return vec_3d((this->V_0(this->t) / (this->d * this->d)) * r);
|
||||
return vec_3d(
|
||||
(this->V_0 * this->perturbation(this->t) / (this->d * this->d)) * r);
|
||||
}
|
||||
|
||||
vec_3d PenningTrap::external_B_field(vec_3d r)
|
||||
@@ -107,7 +115,7 @@ vec_3d PenningTrap::external_B_field(vec_3d r)
|
||||
return vec_3d{0., 0., this->B_0};
|
||||
}
|
||||
|
||||
vec_3d PenningTrap::force_on_particle(unsigned int i, unsigned int j)
|
||||
vec_3d PenningTrap::force_on_particle(uint i, uint j)
|
||||
{
|
||||
// Calculate the difference between the particles' position
|
||||
vec_3d res = this->particles[i].r_vec - this->particles[j].r_vec;
|
||||
@@ -118,7 +126,7 @@ vec_3d PenningTrap::force_on_particle(unsigned int i, unsigned int j)
|
||||
return vec_3d((this->particles[j].q / (norm * norm * norm)) * res);
|
||||
}
|
||||
|
||||
vec_3d PenningTrap::total_force_external(unsigned int i)
|
||||
vec_3d PenningTrap::total_force_external(uint i)
|
||||
{
|
||||
Particle *p = &this->particles[i];
|
||||
|
||||
@@ -132,7 +140,7 @@ vec_3d PenningTrap::total_force_external(unsigned int i)
|
||||
+ arma::cross(p->v_vec, this->external_B_field(p->r_vec))));
|
||||
}
|
||||
|
||||
vec_3d PenningTrap::total_force_particles(unsigned int i)
|
||||
vec_3d PenningTrap::total_force_particles(uint i)
|
||||
{
|
||||
vec_3d res;
|
||||
|
||||
@@ -141,10 +149,10 @@ vec_3d PenningTrap::total_force_particles(unsigned int i)
|
||||
res += this->force_on_particle(i, j);
|
||||
}
|
||||
|
||||
return vec_3d(res * K_E * (this->particles[i].q));
|
||||
return vec_3d(res * (K_E * this->particles[i].q));
|
||||
}
|
||||
|
||||
vec_3d PenningTrap::total_force(unsigned int i)
|
||||
vec_3d PenningTrap::total_force(uint i)
|
||||
{
|
||||
if (arma::norm(this->particles[i].r_vec) > this->d) {
|
||||
return vec_3d{0., 0., 0.};
|
||||
@@ -159,7 +167,7 @@ void PenningTrap::evolve_RK4(double dt, bool particle_interaction)
|
||||
std::vector<Particle> original_particles = this->particles;
|
||||
std::vector<Particle> tmp_particles = this->particles;
|
||||
|
||||
vec_3d (PenningTrap::*force)(unsigned int)
|
||||
vec_3d (PenningTrap::*force)(uint)
|
||||
= particle_interaction ? &PenningTrap::total_force
|
||||
: &PenningTrap::total_force_external;
|
||||
|
||||
@@ -197,7 +205,7 @@ void PenningTrap::evolve_forward_euler(double dt, bool particle_interaction)
|
||||
vec_3d force_res[size];
|
||||
Particle *p;
|
||||
|
||||
vec_3d (PenningTrap::*force)(unsigned int)
|
||||
vec_3d (PenningTrap::*force)(uint)
|
||||
= particle_interaction ? &PenningTrap::total_force
|
||||
: &PenningTrap::total_force_external;
|
||||
|
||||
@@ -220,13 +228,12 @@ void PenningTrap::evolve_forward_euler(double dt, bool particle_interaction)
|
||||
this->t += dt;
|
||||
}
|
||||
|
||||
simulation_t PenningTrap::simulate(double time, unsigned int steps,
|
||||
std::string method,
|
||||
simulation_t PenningTrap::simulate(double time, uint steps, std::string method,
|
||||
bool particle_interaction)
|
||||
{
|
||||
double dt = time / (double)steps;
|
||||
|
||||
unsigned int size = this->particles.size();
|
||||
uint size = this->particles.size();
|
||||
|
||||
simulation_t res{sim_arr(size, sim_cols(steps)),
|
||||
sim_arr(size, sim_cols(steps))};
|
||||
@@ -255,8 +262,7 @@ simulation_t PenningTrap::simulate(double time, unsigned int steps,
|
||||
}
|
||||
|
||||
void PenningTrap::write_simulation_to_dir(std::string path, double time,
|
||||
unsigned int steps,
|
||||
std::string method,
|
||||
uint steps, std::string method,
|
||||
bool particle_interaction)
|
||||
{
|
||||
if (path.back() != '/') {
|
||||
@@ -293,7 +299,7 @@ void PenningTrap::write_simulation_to_dir(std::string path, double time,
|
||||
}
|
||||
}
|
||||
|
||||
double PenningTrap::fraction_of_particles_left(double time, unsigned int steps,
|
||||
double PenningTrap::fraction_of_particles_left(double time, uint steps,
|
||||
std::string method,
|
||||
bool particle_interaction)
|
||||
{
|
||||
@@ -317,9 +323,9 @@ double PenningTrap::fraction_of_particles_left(double time, unsigned int steps,
|
||||
|
||||
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++) {
|
||||
// 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++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user