Optimizations

This commit is contained in:
2023-10-19 22:36:54 +02:00
parent 4551b233e9
commit 7269429a37
2 changed files with 66 additions and 63 deletions

View File

@@ -26,8 +26,10 @@
#define MASS 40. // unit: amu
// Particles used for testing
Particle p1(CHARGE, MASS, vec_3d{20., 0., 20.}, vec_3d{0., 25., 0.}); ///< Particle 1
Particle p2(CHARGE, MASS, vec_3d{25., 25., 0.}, vec_3d{0., 40., 5.}); ///< Particle 2
Particle p1(CHARGE, MASS, vec_3d{20., 0., 20.},
vec_3d{0., 25., 0.}); ///< Particle 1
Particle p2(CHARGE, MASS, vec_3d{25., 25., 0.},
vec_3d{0., 40., 5.}); ///< Particle 2
/** @brief The analytical solution for particle p1
*
@@ -43,10 +45,11 @@ vec_3d analytical_solution_particle_1(double t)
double w_n = (w_0 - std::sqrt(w_0 * w_0 - 2. * w_z2)) / 2.;
double A_p = (25. + w_n * 20.) / (w_n - w_p);
double A_n = -(25. + w_p * 20.) / (w_n - w_p);
std::complex<double> f =
A_p * std::exp(std::complex<double>(0., -w_p * t)) +
A_n * std::exp(std::complex<double>(0., -w_n * t));
vec_3d res{std::real(f), std::imag(f), 20. * std::cos(std::sqrt(w_z2) * t)};
std::complex<double> f
= A_p * std::exp(std::complex<double>(0., -w_p * t))
+ A_n * std::exp(std::complex<double>(0., -w_n * t));
vec_3d res{std::real(f), std::imag(f),
20. * std::cos(std::sqrt(w_z2) * t)};
return res;
}
@@ -101,8 +104,8 @@ void simulate_single_particle_with_different_steps()
PenningTrap trap(std::vector<Particle>{p1});
simulation_t res = trap.simulate(time, steps, "rk4", false);
for (int i = 0; i < steps; i++) {
ofile << arma::norm(res.r_vecs[0][i] -
analytical_solution_particle_1(dt*i))
ofile << arma::norm(res.r_vecs[0][i]
- analytical_solution_particle_1(dt * i))
<< "\n";
}
ofile.close();
@@ -118,8 +121,8 @@ void simulate_single_particle_with_different_steps()
PenningTrap trap(std::vector<Particle>{p1});
simulation_t res = trap.simulate(time, steps, "euler", false);
for (int i = 0; i < steps; i++) {
ofile << arma::norm(res.r_vecs[0][i] -
analytical_solution_particle_1(dt*i))
ofile << arma::norm(res.r_vecs[0][i]
- analytical_solution_particle_1(dt * i))
<< "\n";
}
ofile.close();
@@ -134,13 +137,15 @@ void simulate_100_particles()
double time = 50.; // microseconds
trap.write_simulation_to_dir("output/simulate_100_particles", time, N);
trap.write_simulation_to_dir("output/simulate_100_particles", time, N,
"rk4", false);
}
/** @brief Simulate 100 particles over 500 \f$ \mu s \f$ using a time
/** @brief Simulate 100 particles over 500 \f$ \mu s \f$ using a time
* dependent potential.
*
* @details The simulation sweeps over different frequencies in [0.2, 2.5] MHz.
* @details The simulation sweeps over different frequencies in [0.2, 2.5]
* MHz.
*
* */
void simulate_100_particles_with_time_potential()
@@ -152,7 +157,8 @@ void simulate_100_particles_with_time_potential()
double freq_start = .2;
double freq_end = 2.5;
double freq_increment = .02;
size_t freq_iterations = (size_t)((freq_end - freq_start) / freq_increment);
size_t freq_iterations
= (size_t)((freq_end - freq_start) / freq_increment);
double res[4][freq_iterations];
@@ -161,25 +167,24 @@ void simulate_100_particles_with_time_potential()
std::ofstream ofile;
double freq = freq_start;
#pragma omp parallel for
for (size_t i = 0; i < freq_iterations; i++) {
res[0][i] = freq;
freq += freq_increment;
res[0][i] = freq_start+ freq_increment*i;
}
#pragma omp parallel for collapse(2) num_threads(4)
// Using num_threads() is usually bad practice, but not having it
// causes a SIGKILL.
#pragma omp parallel for collapse(2) num_threads(4)
for (size_t i = 0; i < 3; i++) {
for (size_t j = 0; j < freq_iterations; j++) {
PenningTrap trap(
res[i+1][j] = PenningTrap(
(unsigned)100, T,
std::bind(
[](double f, double r, double t) {
return (25. * V / 1000.) * (1. + f * std::cos(r * t));
},
amplitudes[i], res[0][j], std::placeholders::_1),
500., 0.);
res[i + 1][j] =
trap.fraction_of_particles_left(time, N, "rk4", false);
500., 0.).fraction_of_particles_left(time, N, "rk4", false);
}
}
@@ -195,21 +200,19 @@ int main()
{
double t0 = omp_get_wtime();
// simulate_single_particle();
simulate_single_particle();
// simulate_two_particles();
simulate_two_particles();
simulate_single_particle_with_different_steps();
double t1 = omp_get_wtime();
simulate_single_particle_with_different_steps();
simulate_100_particles();
//simulate_100_particles_with_time_potential();
simulate_100_particles_with_time_potential();
double end = omp_get_wtime();
std::cout << "Time: " << (end - t1) << " seconds" << std::endl;
std::cout << "Time: " << (end - t0) << " seconds" << std::endl;
return 0;
}