Update documentation

This commit is contained in:
2023-10-14 03:12:23 +02:00
parent 22c8b9707d
commit 58c9212e5c
8 changed files with 322 additions and 128 deletions

View File

@@ -8,7 +8,7 @@ CLASSOBJS=$(CLASSSRCS:.cpp=.o)
INCLUDE=../include
CFLAGS=-Wall -larmadillo -llapack -std=c++11 -O3
CFLAGS=-Wall -larmadillo -lblas -llapack -std=c++11 -O3
OPENMP=-fopenmp
# Add a debug flag when compiling (For the DEBUG macro in utils.hpp)

View File

@@ -8,7 +8,6 @@
* @brief The implementation of the Particle class.
*
* @bug No known bugs
*
* */
#include "Particle.hpp"

View File

@@ -8,33 +8,28 @@
* @brief The implementation of the PenningTrap class.
*
* @bug No known bugs
*
* @todo Implement evolve_RK4
* @todo Implement evolve_forward_euler
* */
#include "PenningTrap.hpp"
#include "constants.hpp"
#include "typedefs.hpp"
#include "utils.hpp"
#include <cstdlib>
#include <functional>
#include <string>
#include <vector>
PenningTrap::PenningTrap(double B_0, double V_0, double d)
PenningTrap::PenningTrap(double B_0, std::function<double(double)> V_0,
double d, double t)
{
this->B_0 = B_0;
this->V_0 = V_0;
this->d = d;
this->t = t;
}
PenningTrap::PenningTrap(int i, double B_0, double V_0, double d)
PenningTrap::PenningTrap(unsigned int i, double B_0,
std::function<double(double)> V_0, double d, double t)
: PenningTrap::PenningTrap(B_0, V_0, d)
{
vec_3d r, v;
for (int j = 0; j < i; j++) {
for (size_t j = 0; j < i; j++) {
r = vec_3d().randn() * .1 * this->d;
v = vec_3d().randn() * .1 * this->d;
this->add_particle(Particle(1., 40., r, v));
@@ -42,13 +37,13 @@ PenningTrap::PenningTrap(int i, double B_0, double V_0, double d)
}
PenningTrap::PenningTrap(std::vector<Particle> particles, double B_0,
double V_0, double d)
std::function<double(double)> V_0, double d, double t)
: PenningTrap::PenningTrap(B_0, V_0, d)
{
this->particles = particles;
}
vec_3d PenningTrap::v_func(int i, int j, double dt)
vec_3d PenningTrap::v_func(unsigned int i, unsigned int j, double dt)
{
switch (i) {
case 0:
@@ -66,7 +61,7 @@ vec_3d PenningTrap::v_func(int i, int j, double dt)
}
}
vec_3d PenningTrap::r_func(int i, int j, double dt)
vec_3d PenningTrap::r_func(unsigned int i, unsigned int j, double dt)
{
switch (i) {
case 0:
@@ -92,7 +87,7 @@ void PenningTrap::add_particle(Particle particle)
vec_3d PenningTrap::external_E_field(vec_3d r)
{
r(2) *= -2.;
double f = this->V_0 / (this->d * this->d);
double f = this->V_0(this->t) / (this->d * this->d);
return f * r;
}
@@ -102,21 +97,21 @@ vec_3d PenningTrap::external_B_field(vec_3d r)
return vec_3d{0., 0., this->B_0};
}
vec_3d PenningTrap::force_on_particle(int i, int j)
vec_3d PenningTrap::force_on_particle(unsigned int i, unsigned int j)
{
Particle p_j = this->particles[j];
// Calculate the difference between the particles' position
vec_3d res = this->particles[i].r_vec - this->particles[j].r_vec;
vec_3d res = this->particles[i].r_vec - p_j.r_vec;
// Get the distance between the particles
double norm = arma::norm(res);
double norm = arma::norm(res, 2);
// Multiply res with p_j's charge divided by the norm cubed
res *= this->particles[j].q / (norm * norm * norm);
return res;
return vec_3d(res * p_j.q / (norm * norm * norm));
}
vec_3d PenningTrap::total_force_external(int i)
vec_3d PenningTrap::total_force_external(unsigned int i)
{
Particle p = this->particles[i];
@@ -124,13 +119,9 @@ vec_3d PenningTrap::total_force_external(int i)
return vec_3d{0., 0., 0.};
}
vec_3d B = this->external_B_field(p.r_vec);
vec_3d v_cross_B{p.v_vec(1) * B(2) - p.v_vec(2) * B(1),
p.v_vec(2) * B(0) - p.v_vec(0) * B(2),
p.v_vec(0) * B(1) - p.v_vec(1) * B(0)};
vec_3d force = p.q * (this->external_E_field(p.r_vec) + v_cross_B);
vec_3d force =
p.q * (this->external_E_field(p.r_vec) +
arma::cross(p.v_vec, this->external_B_field(p.r_vec)));
return force;
}
@@ -140,9 +131,8 @@ vec_3d PenningTrap::total_force_particles(unsigned int i)
Particle p = this->particles[i];
vec_3d res;
size_t size = this->particles.size();
for (size_t j = 0; j < size; j++) {
for (size_t j = 0; j < this->particles.size(); j++) {
if (i == j) {
continue;
}
@@ -150,12 +140,10 @@ vec_3d PenningTrap::total_force_particles(unsigned int i)
res += this->force_on_particle(i, j);
}
res *= K_E * (p.q / p.m);
return res;
return vec_3d(res * K_E * (p.q / p.m));
}
vec_3d PenningTrap::total_force(int i)
vec_3d PenningTrap::total_force(unsigned int i)
{
return this->total_force_external(i) - this->total_force_particles(i);
}
@@ -163,43 +151,36 @@ vec_3d PenningTrap::total_force(int i)
void PenningTrap::evolve_RK4(double dt, bool particle_interaction)
{
DEBUG("Inside RK4");
std::vector<Particle> original_particles = this->particles;
std::vector<Particle> tmp_particles = this->particles;
DEBUG("Choose force func");
std::function<vec_3d(int)> force;
vec_3d (PenningTrap::*force)(unsigned int);
if (particle_interaction) {
force = [this](int i) { return this->total_force(i); };
force = &PenningTrap::total_force;
}
else {
force = [this](int i) { return this->total_force_external(i); };
force = &PenningTrap::total_force_external;
}
DEBUG("Create k_v and k_r");
int size = this->particles.size();
size_t size = this->particles.size();
this->k_v = sim_arr(4, sim_cols(size));
this->k_r = sim_arr(4, sim_cols(size));
DEBUG("Loop");
for (int i = 0; i < 4; i++) {
DEBUG("Inside outer loop");
for (size_t i = 0; i < 4; i++) {
#pragma omp parallel for
for (int j = 0; j < size; j++) {
DEBUG("Set k_v and k_r");
this->k_v[i][j] = this->total_force(j) / this->particles[j].m;
for (size_t j = 0; j < this->particles.size(); j++) {
this->k_v[i][j] = (this->*force)(j) / this->particles[j].m;
this->k_r[i][j] = this->particles[j].v_vec;
Particle *p = &tmp_particles[j];
DEBUG("Update v and r");
p->v_vec = original_particles[j].v_vec + this->v_func(i, j, dt);
p->r_vec = original_particles[j].r_vec + this->r_func(i, j, dt);
}
DEBUG("After inner loop");
this->particles = tmp_particles;
}
this->t += dt;
}
void PenningTrap::evolve_forward_euler(double dt, bool particle_interaction)
@@ -208,58 +189,49 @@ void PenningTrap::evolve_forward_euler(double dt, bool particle_interaction)
Particle *p;
std::function<vec_3d(int)> force;
vec_3d (PenningTrap::*force)(unsigned int);
if (particle_interaction) {
force = [this](int i) { return this->total_force(i); };
force = &PenningTrap::total_force;
}
else {
force = [this](int i) { return this->total_force_external(i); };
force = &PenningTrap::total_force_external;
}
#pragma omp parallel for private(p)
for (size_t i = 0; i < this->particles.size(); i++) {
p = &new_state[i];
p->v_vec += dt * force(i) / p->m;
p->v_vec += dt * (this->*force)(i) / p->m;
p->r_vec += dt * this->particles[i].v_vec;
}
this->particles = new_state;
this->t += dt;
}
sim_arr PenningTrap::simulate(double time, unsigned int steps,
std::string method, bool particle_interaction)
{
DEBUG("Inside simulate");
double dt = time / (double)steps;
sim_arr res(this->particles.size(), sim_cols(steps));
DEBUG("Choose function to use");
std::function<void(double, bool)> func;
void (PenningTrap::*func)(double, bool);
if (method == "rk4") {
func = [this](double dt, bool particle_interaction) {
DEBUG("Inside RK4 lambda");
this->evolve_RK4(dt, particle_interaction);
};
func = &PenningTrap::evolve_RK4;
}
else if (method == "euler") {
func = [this](double dt, bool particle_interaction) {
this->evolve_forward_euler(dt, particle_interaction);
};
func = &PenningTrap::evolve_forward_euler;
}
else {
std::cout << "Not a valid method!" << std::endl;
abort();
}
DEBUG("Loop");
for (size_t j = 0; j < steps; j++) {
DEBUG("Inside outer loop");
for (size_t i = 0; i < this->particles.size(); i++) {
res[i][j] = this->particles[i].r_vec;
}
DEBUG("After inner loop");
func(dt, particle_interaction);
(this->*func)(dt, particle_interaction);
}
return res;
@@ -269,7 +241,6 @@ void PenningTrap::write_simulation_to_dir(std::string path, double time,
int steps, std::string method,
bool particle_interaction)
{
DEBUG("Inside write to dir");
if (path.back() != '/') {
path += '/';
}
@@ -278,12 +249,10 @@ void PenningTrap::write_simulation_to_dir(std::string path, double time,
return;
}
DEBUG("Create sim_arr");
sim_arr res = this->simulate(time, steps, method, particle_interaction);
std::ofstream ofile;
DEBUG("Loop");
#pragma omp parallel for private(ofile)
for (size_t i = 0; i < this->particles.size(); i++) {
ofile.open(path + "particle_" + std::to_string(i) + ".txt");
@@ -293,3 +262,19 @@ void PenningTrap::write_simulation_to_dir(std::string path, double time,
ofile.close();
}
}
double PenningTrap::fraction_of_particles_left(double time, unsigned int steps, std::string method, bool particle_interaction)
{
sim_arr res = this->simulate(time, steps, method, particle_interaction);
int particles_left = 0;
for (Particle p : this->particles) {
if (arma::norm(p.r_vec) < this->d) {
particles_left++;
}
}
return (double) particles_left / (double) this->particles.size();
}

View File

@@ -10,6 +10,7 @@
* @bug No known bugs
* */
#include <cmath>
#include <fstream>
#include <omp.h>
#include <string>
@@ -59,45 +60,81 @@ void simulate_single_particle_with_different_steps()
for (int i = 0; i < 4; i++) {
int steps = 4000 * (i + 1);
PenningTrap trap(std::vector<Particle>{p1});
trap.write_simulation_to_dir("output/N_steps/RK4/" + std::to_string(steps) +
"_steps",
trap.write_simulation_to_dir("output/N_steps/RK4/" +
std::to_string(steps) + "_steps",
time, steps, "rk4", false);
}
for (int i = 0; i < 4; i++) {
int steps = 4000 * (i + 1);
PenningTrap trap(std::vector<Particle>{p1});
trap.write_simulation_to_dir("output/N_steps/euler/" + std::to_string(steps) +
"_steps",
trap.write_simulation_to_dir("output/N_steps/euler/" +
std::to_string(steps) + "_steps",
time, steps, "euler", false);
}
}
void simulate_100_particles()
{
PenningTrap trap(100);
PenningTrap trap((unsigned)100, T,
[](double t) {
return 25. * V / 1000. *
(1. + .4 * std::cos(1.5 * t));
},
500., 0);
double time = 50.; // microseconds
double time = 500.; // microseconds
trap.write_simulation_to_dir("output/simulate_100_particles", time, N);
trap.write_simulation_to_dir("output/simulate_100_particles", time, N*5);
}
void simulate_100_particles_with_time_potential()
{
double amplitudes[]{.1, .4, .7};
double freq_start = .2;
double freq_end = 2.5;
double freq_increment = .02;
size_t freq_iterations = (size_t) ((freq_end - freq_start) / freq_increment);
std::string path = "output/time_dependent_potential/";
mkpath(path);
std::ofstream ofile;
for (double f : amplitudes) {
ofile.open(path + "f_" + std::to_string(f) + ".txt");
#pragma omp parallel for ordered schedule(static, 1)
for (size_t i=0; i < freq_iterations; i++) {
double freq = freq_start + i*freq_increment;
PenningTrap trap((unsigned)100, T,
[f, freq](double t) {
return (25. * V / 1000.) *
(1. + f * std::cos(freq * t));
},
500., 0.);
double res = trap.fraction_of_particles_left(500., 40000, "rk4", true);
#pragma omp ordered
ofile << freq << "," << res << "\n";
}
ofile.close();
}
}
int main()
{
DEBUG("Start");
double start = omp_get_wtime();
DEBUG("Single particle");
simulate_single_particle();
DEBUG("Two particles");
simulate_two_particles();
DEBUG("Single particle with different steps");
simulate_single_particle_with_different_steps();
DEBUG("100 particles");
simulate_100_particles();
double start = omp_get_wtime();
//simulate_100_particles();
simulate_100_particles_with_time_potential();
double end = omp_get_wtime();