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 <filesystem>
14#include <fstream>
15#include <string>
16#include <omp.h>
17#include <sys/stat.h>
18
19#include "PenningTrap.hpp"
20
21#define PARTICLES 100
22#define N 10000
23#define CHARGE 1.
24#define MASS 40. // unit: amu
25
26void euler_100_particles()
27{
28 PenningTrap trap;
29
30 // Add particles inside trap
31 for (int i=0; i < PARTICLES; i++) {
32 arma::vec r = arma::vec(3).randn() * 0.1 * trap.get_d(); // random initial position
33 arma::vec v = arma::vec(3).randn() * 0.1 * 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_forward_euler(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
60 std::ofstream ofile;
61
62 // Write particle paths to file
63 #pragma omp parallel for private(cur_row, cur_elem, ofile)
64 for (int i=0; i < PARTICLES; i++) {
65 cur_row = res[i];
66 ofile.open("output/p" + std::to_string(i) + ".txt");
67 for (int j=0; j < N; j++) {
68 cur_elem = cur_row[j];
69 ofile << cur_elem(0) << ","
70 << cur_elem(1) << ","
71 << cur_elem(2) << "\n";
72 }
73 ofile.close();
74 }
75}
76
77int main()
78{
79 double start = omp_get_wtime();
80
81 euler_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:25
void evolve_forward_euler(double dt)
Go forward one timestep using the forward Euler method.
void add_particle(Particle particle)
Add a particle to the system.
Definition: PenningTrap.cpp:33