25 std::function<
double(
double)> V_0,
double d,
double t)
29 for (
size_t j = 0; j < i; j++) {
30 r =
vec_3d().randn() * .1 * this->
d;
31 v =
vec_3d().randn() * .1 * this->
d;
37 std::function<
double(
double)> V_0,
double d,
double t)
47 return .5 * dt * this->
k_v[0][j];
49 return .5 * dt * this->k_v[1][j];
51 return dt * this->k_v[2][j];
53 return (dt / 6.) * (this->k_v[0][j] + 2. * this->k_v[1][j] +
54 2. * this->k_v[2][j] + this->k_v[3][j]);
56 std::cout <<
"Not valid!" << std::endl;
65 return .5 * dt * this->
k_r[0][j];
67 return .5 * dt * this->k_r[1][j];
69 return dt * this->k_r[2][j];
71 return (dt / 6.) * (this->k_r[0][j] + 2. * this->k_r[1][j] +
72 2. * this->k_r[2][j] + this->k_r[3][j]);
74 std::cout <<
"Not valid!" << std::endl;
81 this->particles.push_back(particle);
87 double f = this->
V_0(this->
t) / (this->
d * this->
d);
94 return vec_3d{0., 0., this->B_0};
100 vec_3d res = this->particles[i].r_vec - this->particles[j].r_vec;
103 double norm = arma::norm(res, 2);
105 return vec_3d(res * this->particles[j].q / (norm * norm * norm));
112 if (arma::norm(p.
r_vec) > this->d) {
113 return vec_3d{0., 0., 0.};
118 arma::cross(p.
v_vec, this->external_B_field(p.
r_vec)));
129 for (
size_t j = 0; j < this->particles.size(); j++) {
142 if (arma::norm(this->particles[i].r_vec) > this->
d) {
143 return vec_3d{0., 0., 0.};
151 std::vector<Particle> original_particles = this->
particles;
152 std::vector<Particle> tmp_particles = this->
particles;
155 if (particle_interaction) {
162 size_t size = this->particles.size();
167 for (
size_t i = 0; i < 4; i++) {
168#pragma omp parallel for
169 for (
size_t j = 0; j < this->particles.size(); j++) {
170 this->
k_v[i][j] = (this->*force)(j) / this->particles[j].m;
171 this->
k_r[i][j] = this->particles[j].v_vec;
175 p->
v_vec = original_particles[j].v_vec + this->
v_func(i, j, dt);
176 p->
r_vec = original_particles[j].r_vec + this->
r_func(i, j, dt);
178 this->particles.swap(tmp_particles);
185 size_t size = this->particles.size();
190 if (particle_interaction) {
197#pragma omp parallel for
198 for (
size_t i = 0; i < size; i++) {
199 force_res[i] = (this->*force)(i);
202#pragma omp parallel for private(p)
203 for (
size_t i = 0; i < size; i++) {
204 p = &this->particles[i];
206 p->
v_vec += dt * force_res[i] / p->
m;
214 bool particle_interaction)
216 double dt = time / (double)steps;
218 unsigned int size = this->particles.size();
224 if (method ==
"rk4") {
227 else if (method ==
"euler") {
231 std::cout <<
"Not a valid method!" << std::endl;
235 for (
size_t j = 0; j < steps; j++) {
236 for (
size_t i = 0; i < size; i++) {
237 res.r_vecs[i][j] = this->particles[i].r_vec;
238 res.v_vecs[i][j] = this->particles[i].v_vec;
240 (this->*func)(dt, particle_interaction);
249 bool particle_interaction)
251 if (path.back() !=
'/') {
254 if (
mkpath(path, 0777) != 0) {
255 std::cout <<
"Hello" << std::endl;
260 this->
simulate(time, steps, method, particle_interaction);
264#pragma omp parallel for private(ofile)
265 for (
size_t i = 0; i < this->particles.size(); i++) {
266 ofile.open(path +
"particle_" + std::to_string(i) +
"_r.txt");
267 for (
vec_3d &vec : res.r_vecs[i]) {
268 ofile << vec(0) <<
"," << vec(1) <<
"," << vec(2) <<
"\n";
271 ofile.open(path +
"particle_" + std::to_string(i) +
"_v.txt");
272 for (
vec_3d &vec : res.v_vecs[i]) {
283 bool particle_interaction)
286 this->
simulate(time, steps, method, particle_interaction);
288 int particles_left = 0;
290 for (
Particle p : this->particles) {
291 if (arma::norm(p.r_vec) < this->d) {
296 return (
double)particles_left / (double)this->particles.size();
299vec_3d PenningTrap::get_r(
int i)
301 return this->particles[i].r_vec;
304double PenningTrap::get_t()
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 fraction_of_particles_left(double time, unsigned int 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...
double B_0
Magnetic field strength.
vec_3d external_E_field(vec_3d r)
Calculate E at point r.
vec_3d total_force(unsigned int i)
calculate the total force on a particle p_i.
void evolve_RK4(double dt, bool particle_interaction=true)
Go forward one timestep using the RK4 method.
vec_3d r_func(unsigned int i, unsigned int j, double dt)
Helper for evolve_RK4 when calculating values.
vec_3d total_force_particles(unsigned int i)
Calculate the total force on a particle p_i from other particles.
double d
Characteristic dimension.
void add_particle(Particle particle)
Add a particle to the system.
vec_3d force_on_particle(unsigned int i, unsigned int j)
Calculate the force between 2 particles.
std::function< double(double)> V_0
Applied potential.
void evolve_forward_euler(double dt, bool particle_interaction=true)
Go forward one timestep using the forward Euler method.
void write_simulation_to_dir(std::string path, double time, unsigned int steps, std::string method="rk4", bool particle_interaction=true)
Simulate and write the displacement of all particles to files.
vec_3d external_B_field(vec_3d r)
Calculate B at point r.
simulation_t simulate(double time, unsigned int steps, std::string method="rk4", bool particle_interaction=true)
Simulate the particle system inside the Penning trap over a certain amount of time.
PenningTrap(double B_0=T, std::function< double(double)> V_0=[](double t) { return 25. *V/1000.;}, double d=500., double t=0.)
Constructor for the PenningTrap class.
vec_3d total_force_external(unsigned int i)
Calculate the total external force on a particle.
vec_3d v_func(unsigned int i, unsigned int j, double dt)
Helper for evolve_RK4 when calculating values.
#define K_E
Coulomb constant. unit: .
Typedef for PenningTrap::simulation return value.
std::vector< arma::vec::fixed< 3 > > sim_cols
Typedef for the column of the result vector from simulating particles.
arma::vec::fixed< 3 > vec_3d
Typedef for a fixed 3d arma vector.
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.