Penning Trap Simulation
Simulate particle behavior inside a Penning Trap
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1
13#include <cmath>
14#include <fstream>
15#include <omp.h>
16#include <string>
17#include <sys/stat.h>
18#include <vector>
19
20#include "PenningTrap.hpp"
21#include "utils.hpp"
22
23#define PARTICLES 100
24#define N 10000
25#define CHARGE 1.
26#define MASS 40. // unit: amu
27
28Particle p1(CHARGE, MASS, vec_3d{20., 0., 20.}, vec_3d{0., 25., 0.});
29Particle p2(CHARGE, MASS, vec_3d{25., 25., 0.}, vec_3d{0., 40., 5.});
30
31void simulate_single_particle()
32{
33 DEBUG("Inside single particle sim");
34 PenningTrap trap(std::vector<Particle>{p1});
35
36 double time = 50.; // microseconds
37
38 DEBUG("Write to dir");
39 trap.write_simulation_to_dir("output/simulate_single_particle", time, N);
40}
41
42void simulate_two_particles()
43{
44 PenningTrap trap_no_interaction(std::vector<Particle>{p1, p2});
45 PenningTrap trap_with_interaction(std::vector<Particle>{p1, p2});
46
47 double time = 50.; // microseconds
48
49 trap_no_interaction.write_simulation_to_dir(
50 "output/simulate_2_particles/no_interaction", time, N, "rk4", false);
51 trap_with_interaction.write_simulation_to_dir(
52 "output/simulate_2_particles/with_interaction", time, N);
53}
54
55void simulate_single_particle_with_different_steps()
56{
57
58 double time = 50; // microseconds
59
60 for (int i = 0; i < 4; i++) {
61 int steps = 4000 * (i + 1);
62 PenningTrap trap(std::vector<Particle>{p1});
63 trap.write_simulation_to_dir("output/N_steps/RK4/" +
64 std::to_string(steps) + "_steps",
65 time, steps, "rk4", false);
66 }
67
68 for (int i = 0; i < 4; i++) {
69 int steps = 4000 * (i + 1);
70 PenningTrap trap(std::vector<Particle>{p1});
71 trap.write_simulation_to_dir("output/N_steps/euler/" +
72 std::to_string(steps) + "_steps",
73 time, steps, "euler", false);
74 }
75}
76
77void simulate_100_particles()
78{
79 PenningTrap trap((unsigned)100, T,
80 [](double t) {
81 return 25. * V / 1000. *
82 (1. + .4 * std::cos(1.5 * t));
83 },
84 500., 0);
85
86 double time = 500.; // microseconds
87
88 trap.write_simulation_to_dir("output/simulate_100_particles", time, N*5);
89}
90
91void simulate_100_particles_with_time_potential()
92{
93 double amplitudes[]{.1, .4, .7};
94
95 double freq_start = .2;
96 double freq_end = 2.5;
97 double freq_increment = .02;
98 size_t freq_iterations = (size_t) ((freq_end - freq_start) / freq_increment);
99
100 std::string path = "output/time_dependent_potential/";
101 mkpath(path);
102
103 std::ofstream ofile;
104
105 for (double f : amplitudes) {
106 ofile.open(path + "f_" + std::to_string(f) + ".txt");
107 #pragma omp parallel for ordered schedule(static, 1)
108 for (size_t i=0; i < freq_iterations; i++) {
109 double freq = freq_start + i*freq_increment;
110 PenningTrap trap((unsigned)100, T,
111 [f, freq](double t) {
112 return (25. * V / 1000.) *
113 (1. + f * std::cos(freq * t));
114 },
115 500., 0.);
116 double res = trap.fraction_of_particles_left(500., 40000, "rk4", true);
117 #pragma omp ordered
118 ofile << freq << "," << res << "\n";
119 }
120 ofile.close();
121 }
122}
123
124int main()
125{
126
127 simulate_single_particle();
128
129 simulate_two_particles();
130
131 simulate_single_particle_with_different_steps();
132
133 double start = omp_get_wtime();
134
135 //simulate_100_particles();
136
137 simulate_100_particles_with_time_potential();
138
139 double end = omp_get_wtime();
140
141 std::cout << "Time: " << end - start << " seconds" << std::endl;
142
143 return 0;
144}
A class for simulating a Penning trap.
A class that holds attributes of a particle.
Definition: Particle.hpp:21
A class that simulates a Penning trap.
Definition: PenningTrap.hpp:30
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...
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.
#define T
1 Tesla. unit:
Definition: constants.hpp:17
#define V
1 Volt. unit:
Definition: constants.hpp:19
arma::vec::fixed< 3 > vec_3d
Typedef for a fixed 3d arma vector.
Definition: typedefs.hpp:36
Function prototypes and macros that are useful.
bool mkpath(std::string path, int mode=0777)
Make path given.
Definition: utils.cpp:76
#define DEBUG(msg)
Writes a debug message.
Definition: utils.hpp:36