28 std::function<
double(
double)> V_0,
double d,
double t)
32 for (
size_t j = 0; j < i; j++) {
33 r =
vec_3d().randn() * .1 * this->
d;
34 v =
vec_3d().randn() * .1 * this->
d;
40 std::function<
double(
double)> V_0,
double d,
double t)
50 return .5 * dt * this->
k_v[0][j];
52 return .5 * dt * this->k_v[1][j];
54 return dt * this->k_v[2][j];
56 return (dt / 6.) * (this->k_v[0][j].eval() + this->k_v[1][j].eval() +
57 this->k_v[2][j].eval() + this->k_v[3][j].eval());
59 std::cout <<
"Not valid!" << std::endl;
68 return .5 * dt * this->
k_r[0][j];
70 return .5 * dt * this->k_r[1][j];
72 return dt * this->k_r[2][j];
74 return (dt / 6.) * (this->k_r[0][j].eval() + this->k_r[1][j].eval() +
75 this->k_r[2][j].eval() + this->k_r[3][j].eval());
77 std::cout <<
"Not valid!" << std::endl;
84 this->particles.push_back(particle);
90 double f = this->
V_0(this->
t) / (this->
d * this->
d);
97 return vec_3d{0., 0., this->B_0};
107 double norm = arma::norm(res, 2);
111 return vec_3d(res * p_j.
q / (norm * norm * norm));
118 if (arma::norm(p.
r_vec) > this->d) {
119 return vec_3d{0., 0., 0.};
124 arma::cross(p.
v_vec, this->external_B_field(p.
r_vec)));
135 for (
size_t j = 0; j < this->particles.size(); j++) {
154 std::vector<Particle> original_particles = this->
particles;
155 std::vector<Particle> tmp_particles = this->
particles;
158 if (particle_interaction) {
165 size_t size = this->particles.size();
170 for (
size_t i = 0; i < 4; i++) {
171#pragma omp parallel for
172 for (
size_t j = 0; j < this->particles.size(); j++) {
173 this->
k_v[i][j] = (this->*force)(j) / this->particles[j].m;
174 this->
k_r[i][j] = this->particles[j].v_vec;
178 p->
v_vec = original_particles[j].v_vec + this->
v_func(i, j, dt);
179 p->
r_vec = original_particles[j].r_vec + this->
r_func(i, j, dt);
181 this->particles = tmp_particles;
188 std::vector<Particle> new_state = this->
particles;
193 if (particle_interaction) {
200#pragma omp parallel for private(p)
201 for (
size_t i = 0; i < this->particles.size(); i++) {
203 p->
v_vec += dt * (this->*force)(i) / p->
m;
204 p->
r_vec += dt * this->particles[i].v_vec;
207 this->particles = new_state;
212 std::string method,
bool particle_interaction)
214 double dt = time / (double)steps;
219 if (method ==
"rk4") {
222 else if (method ==
"euler") {
226 std::cout <<
"Not a valid method!" << std::endl;
230 for (
size_t j = 0; j < steps; j++) {
231 for (
size_t i = 0; i < this->particles.size(); i++) {
232 res[i][j] = this->particles[i].r_vec;
234 (this->*func)(dt, particle_interaction);
241 int steps, std::string method,
242 bool particle_interaction)
244 if (path.back() !=
'/') {
247 if (
mkpath(path, 0777) != 0) {
248 std::cout <<
"Hello" << std::endl;
252 sim_arr res = this->
simulate(time, steps, method, particle_interaction);
256#pragma omp parallel for private(ofile)
257 for (
size_t i = 0; i < this->particles.size(); i++) {
258 ofile.open(path +
"particle_" + std::to_string(i) +
".txt");
259 for (
vec_3d &vec : res[i]) {
260 ofile << vec(0) <<
"," << vec(1) <<
"," << vec(2) <<
"\n";
268 sim_arr res = this->
simulate(time, steps, method, particle_interaction);
270 int particles_left = 0;
272 for (
Particle p : this->particles) {
273 if (arma::norm(p.r_vec) < this->d) {
278 return (
double) particles_left / (double) this->particles.size();
A class for simulating a Penning trap.
A class that holds attributes of a particle.
A class that simulates a Penning trap.
std::vector< Particle > particles
The particles in the Penning trap.
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...
double B_0
Magnetic field strength.
vec_3d external_E_field(vec_3d r)
Calculate E at point r.
vec_3d total_force(unsigned int i)
calculate the total force on a particle p_i.
void evolve_RK4(double dt, bool particle_interaction=true)
Go forward one timestep using the RK4 method.
vec_3d r_func(unsigned int i, unsigned int j, double dt)
Helper for evolve_RK4 when calculating values.
vec_3d total_force_particles(unsigned int i)
Calculate the total force on a particle p_i from other particles.
sim_arr simulate(double time, unsigned int steps, std::string method="rk4", bool particle_interaction=true)
Simulate the particle system inside the Penning trap over a certain amount of time.
double d
Characteristic dimension.
void add_particle(Particle particle)
Add a particle to the system.
vec_3d force_on_particle(unsigned int i, unsigned int j)
Calculate the force between 2 particles.
std::function< double(double)> V_0
Applied potential.
void evolve_forward_euler(double dt, bool particle_interaction=true)
Go forward one timestep using the forward Euler method.
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.
vec_3d external_B_field(vec_3d r)
Calculate B at point r.
PenningTrap(double B_0=T, std::function< double(double)> V_0=[](double t) { return 25. *V/1000.;}, double d=500., double t=0.)
Constructor for the PenningTrap class.
vec_3d total_force_external(unsigned int i)
Calculate the total external force on a particle.
vec_3d v_func(unsigned int i, unsigned int j, double dt)
Helper for evolve_RK4 when calculating values.
#define K_E
Coulomb constant. unit: .
Useful typedefs for cleaner code.
std::vector< arma::vec::fixed< 3 > > sim_cols
Typedef for the column of the result vector from simulating particles.
arma::vec::fixed< 3 > vec_3d
Typedef for a fixed 3d arma vector.
std::vector< sim_cols > sim_arr
Typedef for the result of the simulate method.
Function prototypes and macros that are useful.
bool mkpath(std::string path, int mode=0777)
Make path given.