Penning Trap Simulation
Simulate particle behavior inside a Penning Trap
Loading...
Searching...
No Matches
frequency_narrow_sweeps_long.cpp
Go to the documentation of this file.
1
13#include <cmath>
14#include <complex>
15#include <fstream>
16#include <omp.h>
17#include <string>
18#include <vector>
19
20#include "PenningTrap.hpp"
21#include "constants.hpp"
22#include "utils.hpp"
23
24#define PARTICLES 100
25#define N 40000
26
35{
36 double time = 500.;
37
38 double amplitudes[]{.1, .4, .7};
39
40 double freq_start = 1.1;
41 double freq_end = 1.7;
42 double freq_increment = .0005;
43 size_t freq_iterations =
44 (size_t)((freq_end - freq_start) / freq_increment) + 1;
45
46 double res[4][freq_iterations];
47
48 std::string path = "output/time_dependent_potential/";
49 mkpath(path);
50
51 std::ofstream ofile;
52
53#pragma omp parallel for
54 // Insert frequencies
55 for (size_t i = 0; i < freq_iterations; i++) {
56 res[0][i] = freq_start + freq_increment * i;
57 }
58
59#pragma omp parallel
60 {
61 // Each thread creates a PenningTrap instance and reuses it throughout
62 // the sweep.
63 PenningTrap trap((uint)PARTICLES);
64#pragma omp for collapse(2)
65 for (size_t i = 0; i < 3; i++) {
66 for (size_t j = 0; j < freq_iterations; j++) {
67 // Reset particles and give new time dependent potential.
68 trap.reinitialize(amplitudes[i], res[0][j]);
69 res[i + 1][j] =
70 trap.fraction_of_particles_left(time, N, "rk4", false);
71 }
72 }
73 }
74
75 // Write results to file
76 ofile.open(path + "narrow_sweep_fine.txt");
77 for (size_t i = 0; i < freq_iterations; i++) {
78 ofile << res[0][i] << ',' << res[1][i] << ',' << res[2][i] << ','
79 << res[3][i] << '\n';
80 }
81 ofile.close();
82}
83
92{
93 double time = 500.;
94
95 double amplitudes[]{.1, .4, .7};
96
97 double freq_start = 1.1;
98 double freq_end = 1.7;
99 double freq_increment = .0005;
100 size_t freq_iterations =
101 (size_t)((freq_end - freq_start) / freq_increment) + 1;
102
103 double res[4][freq_iterations];
104
105 std::string path = "output/time_dependent_potential/";
106 mkpath(path);
107
108 std::ofstream ofile;
109
110#pragma omp parallel for
111 for (size_t i = 0; i < freq_iterations; i++) {
112 res[0][i] = freq_start + freq_increment * i;
113 }
114
115#pragma omp parallel
116 {
117 // Each thread creates a PenningTrap instance and reuses it throughout
118 // the sweep.
119 PenningTrap trap((uint)PARTICLES);
120#pragma omp for collapse(2)
121 for (size_t i = 0; i < 3; i++) {
122 for (size_t j = 0; j < freq_iterations; j++) {
123 // Reset particles and give new time dependent potential.
124 trap.reinitialize(amplitudes[i], res[0][j]);
125 res[i + 1][j] = trap.fraction_of_particles_left(time, N);
126 }
127 }
128 }
129
130 // Write results to file
131 ofile.open(path + "narrow_sweep_interactions_fine.txt");
132 for (size_t i = 0; i < freq_iterations; i++) {
133 ofile << res[0][i] << ',' << res[1][i] << ',' << res[2][i] << ','
134 << res[3][i] << '\n';
135 }
136 ofile.close();
137}
138
139int main()
140{
141 double start, end;
142
143 start = omp_get_wtime();
144
146
148
149 end = omp_get_wtime();
150
151 std::cout << "Time: " << end - start << " seconds" << std::endl;
152
153 return 0;
154}
A class for simulating a Penning trap.
A class that simulates a Penning trap.
Definition: PenningTrap.hpp:32
void reinitialize(double f, double omega_V, double t=0.)
Give all particles new positions and velocities, and change t and V_0.
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...
Library of constants.
void potential_resonance_narrow_sweep()
Simulate 100 particles over 500 using a time dependent potential.
void potential_resonance_narrow_sweep_interaction()
Simulate 100 particles over 500 using a time dependent potential.
Function prototypes and macros that are useful.
bool mkpath(std::string path, int mode=0777)
Make path given.
Definition: utils.cpp:72