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 <fstream>
14#include <omp.h>
15#include <sys/stat.h>
16
17#include "PenningTrap.hpp"
18
19#define PARTICLES 100
20#define N 10000
21#define CHARGE 1.
22#define MASS 40. // unit: amu
23
24void simulate_100_particles()
25{
26 PenningTrap trap;
27
28 // Add particles inside trap
29 for (int i = 0; i < PARTICLES; i++) {
30 arma::vec r = arma::vec(3).randn() * 0.1 *
31 trap.get_d(); // random initial position
32 arma::vec v = arma::vec(3).randn() * 0.1 *
33 trap.get_d(); // random initial velocity
34 trap.add_particle(Particle(CHARGE, MASS, r, v));
35 }
36
37 double time = 50.; // microseconds
38 double dt = time / (double)N;
39
40 auto res = new arma::vec::fixed<3>[PARTICLES][N];
41
42 int counter = 0;
43
44 // Get the path of all particles
45 for (int j = 0; j < N; j++) {
46#pragma omp parallel for
47 for (int i = 0; i < PARTICLES; i++) {
48 res[i][j] = trap.get_particle(i);
49 }
50 trap.evolve_RK4(dt);
51 }
52
53 std::cout << counter << std::endl;
54
55 arma::vec::fixed<3> *cur_row;
56 arma::vec::fixed<3> cur_elem;
57
58 mkdir("output", 0777);
59 mkdir("output/simulate_100_particles", 0777);
60
61 std::ofstream ofile;
62
63// Write particle paths to file
64#pragma omp parallel for private(cur_row, cur_elem, ofile)
65 for (int i = 0; i < PARTICLES; i++) {
66 cur_row = res[i];
67 ofile.open("output/simulate_100_particles/p" + std::to_string(i) + ".txt");
68 for (int j = 0; j < N; j++) {
69 cur_elem = cur_row[j];
70 ofile << cur_elem(0) << "," << cur_elem(1) << "," << cur_elem(2)
71 << "\n";
72 }
73 ofile.close();
74 }
75}
76
77int main()
78{
79 double start = omp_get_wtime();
80
81 simulate_100_particles();
82
83 double end = omp_get_wtime();
84
85 std::cout << "Time: " << end - start << " seconds" << std::endl;
86
87 return 0;
88}
A class for simulating a Penning trap.
A class that holds attributes of a particle.
Definition: Particle.hpp:19
A class that simulates a Penning trap.
Definition: PenningTrap.hpp:29
void add_particle(Particle particle)
Add a particle to the system.
Definition: PenningTrap.cpp:27
void evolve_RK4(double dt)
Go forward one timestep using the RK4 method.