Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
#include <cmath>
#include <complex>
#include <fstream>
#include <omp.h>
#include <string>
#include <vector>
#include "PenningTrap.hpp"
#include "constants.hpp"
#include "utils.hpp"
#define PARTICLES 100
#define N 40000
// Particles used for testing
Particle p1(vec3{20., 0., 20.}, vec3{0., 25., 0.});
Particle p2(vec3{25., 25., 0.}, vec3{0., 40., 5.});
vec3 analytical_solution_particle_1(double t)
{
double w_0 = T / CA_MASS;
double w_z2 = (50. * V / 1000.) / (CA_MASS * 500. * 500.);
double w_p = (w_0 + std::sqrt(w_0 * w_0 - 2. * w_z2)) / 2.;
double w_n = (w_0 - std::sqrt(w_0 * w_0 - 2. * w_z2)) / 2.;
double A_p = (25. + w_n * 20.) / (w_n - w_p);
double A_n = -(25. + w_p * 20.) / (w_n - w_p);
std::complex<double> f =
A_p * std::exp(std::complex<double>(0., -w_p * t))
+ A_n * std::exp(std::complex<double>(0., -w_n * t));
vec3 res{std::real(f), std::imag(f), 20. * std::cos(std::sqrt(w_z2) * t)};
return res;
}
void simulate_single_particle()
{
// Initialize trap with particle 1
PenningTrap trap(std::vector<Particle>{p1});
double time = 50.; // microseconds
// Simulate and write results to file
trap.write_simulation_to_dir("output/simulate_single_particle", time, N,
"rk4", false);
}
void simulate_two_particles()
{
// Initialize traps with particles
PenningTrap trap_no_interaction(std::vector<Particle>{p1, p2});
PenningTrap trap_with_interaction(std::vector<Particle>{p1, p2});
double time = 50.; // microseconds
// Simulate and write results to files
trap_no_interaction.write_simulation_to_dir(
"output/simulate_2_particles/no_interaction", time, N, "rk4", false);
trap_with_interaction.write_simulation_to_dir(
"output/simulate_2_particles/with_interaction", time, N);
}
void simulate_single_particle_with_different_steps()
{
double time = 50.; // microseconds
std::ofstream ofile;
// Calculate relative error for RK4
std::string path = "output/relative_error/RK4/";
mkpath(path);
#pragma omp parallel for
for (int i = 0; i < 4; i++) {
int steps = 4000 * std::pow(2, i);
double dt = time / (double)steps;
ofile.open(path + std::to_string(steps) + "_steps.txt");
PenningTrap trap(std::vector<Particle>{p1});
simulation_t res = trap.simulate(time, steps, "rk4", false);
for (int i = 0; i < steps; i++) {
ofile << arma::norm(res.r_vecs[0][i]
- analytical_solution_particle_1(dt * i))
<< '\n';
}
ofile.close();
}
// Calculate relative error for forward Euler
path = "output/relative_error/euler/";
mkpath(path);
#pragma omp parallel for
for (int i = 0; i < 4; i++) {
int steps = 4000 * std::pow(2, i);
double dt = time / (double)steps;
ofile.open(path + std::to_string(steps) + "_steps.txt");
PenningTrap trap(std::vector<Particle>{p1});
simulation_t res = trap.simulate(time, steps, "euler", false);
for (int i = 0; i < steps; i++) {
ofile << arma::norm(res.r_vecs[0][i]
- analytical_solution_particle_1(dt * i))
<< '\n';
}
ofile.close();
}
}
void simulate_100_particles()
{
PenningTrap trap((unsigned)100);
double time = 50.; // microseconds
trap.write_simulation_to_dir("output/simulate_100_particles", time, N,
"rk4", false);
}
void potential_resonance_wide_sweep()
{
double time = 500.;
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) + 1;
double res[4][freq_iterations];
std::string path = "output/time_dependent_potential/";
mkpath(path);
std::ofstream ofile;
#pragma omp parallel for
// Insert frequencies
for (size_t i = 0; i < freq_iterations; i++) {
res[0][i] = freq_start + freq_increment * i;
}
#pragma omp parallel
{
// Each thread creates a PenningTrap instance and reuses it throughout
// the sweep.
PenningTrap trap((uint)100);
#pragma omp for collapse(2)
for (size_t i = 0; i < 3; i++) {
for (size_t j = 0; j < freq_iterations; j++) {
// Reset particles and give new time dependent potential.
trap.reinitialize(amplitudes[i], res[0][j]);
res[i + 1][j] =
trap.fraction_of_particles_left(time, N, "rk4", false);
}
}
}
// Write results to file
ofile.open(path + "wide_sweep.txt");
for (size_t i = 0; i < freq_iterations; i++) {
ofile << res[0][i] << ',' << res[1][i] << ',' << res[2][i] << ','
<< res[3][i] << '\n';
}
ofile.close();
}
void potential_resonance_narrow_sweep()
{
double time = 500.;
double amplitudes[]{.1, .4, .7};
double freq_start = 1.;
double freq_end = 1.7;
double freq_increment = .002;
size_t freq_iterations = (size_t)((freq_end - freq_start) / freq_increment);
double res[4][freq_iterations];
std::string path = "output/time_dependent_potential/";
mkpath(path);
std::ofstream ofile;
#pragma omp parallel for
// Insert frequencies
for (size_t i = 0; i < freq_iterations; i++) {
res[0][i] = freq_start + freq_increment * i;
}
#pragma omp parallel
{
// Each thread creates a PenningTrap instance and reuses it throughout
// the sweep.
PenningTrap trap((uint)100);
#pragma omp for collapse(2)
for (size_t i = 0; i < 3; i++) {
for (size_t j = 0; j < freq_iterations; j++) {
// Reset particles and give new time dependent potential.
trap.reinitialize(amplitudes[i], res[0][j]);
res[i + 1][j] =
trap.fraction_of_particles_left(time, N, "rk4", false);
}
}
}
// Write results to file
ofile.open(path + "narrow_sweep.txt");
for (size_t i = 0; i < freq_iterations; i++) {
ofile << res[0][i] << ',' << res[1][i] << ',' << res[2][i] << ','
<< res[3][i] << '\n';
}
ofile.close();
}
void potential_resonance_narrow_sweep_interaction()
{
double time = 500.;
double amplitudes[]{.1, .4, .7};
double freq_start = 1.;
double freq_end = 1.7;
double freq_increment = .002;
size_t freq_iterations = (size_t)((freq_end - freq_start) / freq_increment);
double res[4][freq_iterations];
std::string path = "output/time_dependent_potential/";
mkpath(path);
std::ofstream ofile;
#pragma omp parallel for
for (size_t i = 0; i < freq_iterations; i++) {
res[0][i] = freq_start + freq_increment * i;
}
#pragma omp parallel
{
// Each thread creates a PenningTrap instance and reuses it throughout
// the sweep.
PenningTrap trap((uint)100);
#pragma omp for collapse(2)
for (size_t i = 0; i < 3; i++) {
for (size_t j = 0; j < freq_iterations; j++) {
// Reset particles and give new time dependent potential.
trap.reinitialize(amplitudes[i], res[0][j]);
res[i + 1][j] = trap.fraction_of_particles_left(time, N);
}
}
}
// Write results to file
ofile.open(path + "narrow_sweep_interactions.txt");
for (size_t i = 0; i < freq_iterations; i++) {
ofile << res[0][i] << ',' << res[1][i] << ',' << res[2][i] << ','
<< res[3][i] << '\n';
}
ofile.close();
}
int main()
{
double start, end, t1, t2;
start = omp_get_wtime();
simulate_single_particle();
simulate_two_particles();
simulate_single_particle_with_different_steps();
t2 = omp_get_wtime();
std::cout << "Time single and double : " << (t2 - start)
<< " seconds" << std::endl;
t1 = omp_get_wtime();
simulate_100_particles();
t2 = omp_get_wtime();
std::cout << "Time 100 particles : " << (t2 - t1)
<< " seconds" << std::endl;
t1 = omp_get_wtime();
potential_resonance_wide_sweep();
t2 = omp_get_wtime();
std::cout << "Time wide sweep : " << (t2 - t1)
<< " seconds" << std::endl;
t1 = omp_get_wtime();
potential_resonance_narrow_sweep();
t2 = omp_get_wtime();
std::cout << "Time narrow sweep no interaction : " << (t2 - t1)
<< " seconds" << std::endl;
t1 = omp_get_wtime();
potential_resonance_narrow_sweep_interaction();
t2 = omp_get_wtime();
std::cout << "Time narrow sweep with interaction: " << (t2 - t1)
<< " seconds" << std::endl;
end = omp_get_wtime();
std::cout << "Time : " << (end - start)
<< " seconds" << std::endl;
return 0;
}
PenningTrap.hpp
A class for simulating a Penning trap.
Particle
A class that holds attributes of a particle.
Definition: Particle.hpp:23
PenningTrap
A class that simulates a Penning trap.
Definition: PenningTrap.hpp:32
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::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
constants.hpp
Library of constants.
T
#define T
1 Tesla. unit:
Definition: constants.hpp:21
CA_MASS
#define CA_MASS
Mass of a single calcium ion. unit: amu.
Definition: constants.hpp:29
V
#define V
1 Volt. unit:
Definition: constants.hpp:25
simulate_100_particles
void simulate_100_particles()
Simulate 100 particles over 50 .
Definition: main.cpp:132
p1
Particle p1(vec3{20., 0., 20.}, vec3{0., 25., 0.})
Particle 1.
potential_resonance_narrow_sweep
void potential_resonance_narrow_sweep()
Simulate 100 particles over 500 using a time dependent potential.
Definition: main.cpp:206
potential_resonance_wide_sweep
void potential_resonance_wide_sweep()
Simulate 100 particles over 500 using a time dependent potential.
Definition: main.cpp:149
simulate_two_particles
void simulate_two_particles()
Simulate 2 particles over the period of 50 with and without particle interactions.
Definition: main.cpp:69
analytical_solution_particle_1
vec3 analytical_solution_particle_1(double t)
The analytical solution for particle p1.
Definition: main.cpp:37
simulate_single_particle
void simulate_single_particle()
Simulate a single particle over the period of 50 .
Definition: main.cpp:54
potential_resonance_narrow_sweep_interaction
void potential_resonance_narrow_sweep_interaction()
Simulate 100 particles over 500 using a time dependent potential.
Definition: main.cpp:262
simulate_single_particle_with_different_steps
void simulate_single_particle_with_different_steps()
Simulate a single particle over 50 using different amount of steps and different methods.
Definition: main.cpp:87
p2
Particle p2(vec3{25., 25., 0.}, vec3{0., 40., 5.})
Particle 2.
simulation
Typedef for PenningTrap::simulation return value.
Definition: typedefs.hpp:40
vec3
arma::vec::fixed< 3 > vec3
Typedef for a fixed 3d arma vector.
Definition: typedefs.hpp:23
utils.hpp
Function prototypes and macros that are useful.
mkpath
bool mkpath(std::string path, int mode=0777)
Make path given.
Definition: utils.cpp:72