Update formatting
This commit is contained in:
@@ -45,8 +45,9 @@ PenningTrap::PenningTrap(std::vector<Particle> particles, double B_0,
|
||||
|
||||
void PenningTrap::set_pertubation(double f, double omega_V)
|
||||
{
|
||||
this->perturbation
|
||||
= [f, omega_V](double t) { return f * std::cos(omega_V * t); };
|
||||
this->perturbation = [f, omega_V](double t) {
|
||||
return f * std::cos(omega_V * t);
|
||||
};
|
||||
}
|
||||
|
||||
void PenningTrap::reinitialize(double f, double omega_V, double t)
|
||||
@@ -167,9 +168,9 @@ void PenningTrap::evolve_RK4(double dt, bool particle_interaction)
|
||||
std::vector<Particle> original_particles = this->particles;
|
||||
std::vector<Particle> tmp_particles = this->particles;
|
||||
|
||||
vec_3d (PenningTrap::*force)(uint)
|
||||
= particle_interaction ? &PenningTrap::total_force
|
||||
: &PenningTrap::total_force_external;
|
||||
vec_3d (PenningTrap::*force)(uint) =
|
||||
particle_interaction ? &PenningTrap::total_force
|
||||
: &PenningTrap::total_force_external;
|
||||
|
||||
size_t size = this->particles.size();
|
||||
|
||||
@@ -188,10 +189,10 @@ void PenningTrap::evolve_RK4(double dt, bool particle_interaction)
|
||||
this->k_v[i][j] = (this->*force)(j) / this->particles[j].m;
|
||||
this->k_r[i][j] = this->particles[j].v_vec;
|
||||
|
||||
tmp_particles[j].v_vec
|
||||
= original_particles[j].v_vec + this->v_func(i, j, dt);
|
||||
tmp_particles[j].r_vec
|
||||
= original_particles[j].r_vec + this->r_func(i, j, dt);
|
||||
tmp_particles[j].v_vec =
|
||||
original_particles[j].v_vec + this->v_func(i, j, dt);
|
||||
tmp_particles[j].r_vec =
|
||||
original_particles[j].r_vec + this->r_func(i, j, dt);
|
||||
}
|
||||
this->particles = tmp_particles;
|
||||
}
|
||||
@@ -205,9 +206,9 @@ void PenningTrap::evolve_forward_euler(double dt, bool particle_interaction)
|
||||
vec_3d force_res[size];
|
||||
Particle *p;
|
||||
|
||||
vec_3d (PenningTrap::*force)(uint)
|
||||
= particle_interaction ? &PenningTrap::total_force
|
||||
: &PenningTrap::total_force_external;
|
||||
vec_3d (PenningTrap::*force)(uint) =
|
||||
particle_interaction ? &PenningTrap::total_force
|
||||
: &PenningTrap::total_force_external;
|
||||
|
||||
// Calculating the force for each particle is independent and therefore
|
||||
// a good candidate for parallel execution
|
||||
@@ -220,9 +221,8 @@ void PenningTrap::evolve_forward_euler(double dt, bool particle_interaction)
|
||||
// this as well
|
||||
#pragma omp parallel for private(p)
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
p = &this->particles[i];
|
||||
p->r_vec += dt * p->v_vec;
|
||||
p->v_vec += dt * force_res[i] / p->m;
|
||||
p->r_vec += dt * this->particles[i].v_vec;
|
||||
p->v_vec += dt * force_res[i] / this->particles[i].m;
|
||||
}
|
||||
|
||||
this->t += dt;
|
||||
@@ -241,11 +241,9 @@ simulation_t PenningTrap::simulate(double time, uint steps, std::string method,
|
||||
void (PenningTrap::*func)(double, bool);
|
||||
if (method == "rk4") {
|
||||
func = &PenningTrap::evolve_RK4;
|
||||
}
|
||||
else if (method == "euler") {
|
||||
} else if (method == "euler") {
|
||||
func = &PenningTrap::evolve_forward_euler;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
std::cout << "Not a valid method!" << std::endl;
|
||||
abort();
|
||||
}
|
||||
@@ -273,8 +271,8 @@ void PenningTrap::write_simulation_to_dir(std::string path, double time,
|
||||
abort();
|
||||
}
|
||||
|
||||
simulation_t res
|
||||
= this->simulate(time, steps, method, particle_interaction);
|
||||
simulation_t res =
|
||||
this->simulate(time, steps, method, particle_interaction);
|
||||
|
||||
std::ofstream ofile;
|
||||
|
||||
@@ -308,11 +306,9 @@ double PenningTrap::fraction_of_particles_left(double time, uint steps,
|
||||
void (PenningTrap::*func)(double, bool);
|
||||
if (method == "rk4") {
|
||||
func = &PenningTrap::evolve_RK4;
|
||||
}
|
||||
else if (method == "euler") {
|
||||
} else if (method == "euler") {
|
||||
func = &PenningTrap::evolve_forward_euler;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
std::cout << "Not a valid method!" << std::endl;
|
||||
abort();
|
||||
}
|
||||
@@ -323,7 +319,7 @@ double PenningTrap::fraction_of_particles_left(double time, uint steps,
|
||||
|
||||
int particles_left = 0;
|
||||
|
||||
// A reduction is perfect here
|
||||
// A reduction is perfect here
|
||||
#pragma omp parallel for reduction(+ : particles_left)
|
||||
for (size_t i = 0; i < this->particles.size(); i++) {
|
||||
if (arma::norm(this->particles[i].r_vec) < this->d) {
|
||||
|
||||
32
src/main.cpp
32
src/main.cpp
@@ -42,9 +42,9 @@ 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));
|
||||
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;
|
||||
@@ -156,8 +156,8 @@ void potential_resonance_wide_sweep()
|
||||
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) + 1;
|
||||
size_t freq_iterations =
|
||||
(size_t)((freq_end - freq_start) / freq_increment) + 1;
|
||||
|
||||
double res[4][freq_iterations];
|
||||
|
||||
@@ -176,14 +176,14 @@ void potential_resonance_wide_sweep()
|
||||
{
|
||||
// Each thread creates a PenningTrap instance and reuses it throughout
|
||||
// the sweep.
|
||||
PenningTrap trap((unsigned int)100);
|
||||
PenningTrap trap((uint)100);
|
||||
#pragma omp for collapse(2)
|
||||
for (size_t i = 0; i < 3; i++) {
|
||||
for (size_t j = 0; j < freq_iterations; j++) {
|
||||
// Reset particles and give new time dependent potential.
|
||||
trap.reinitialize(amplitudes[i], res[0][j]);
|
||||
res[i + 1][j]
|
||||
= trap.fraction_of_particles_left(time, N, "rk4", false);
|
||||
res[i + 1][j] =
|
||||
trap.fraction_of_particles_left(time, N, "rk4", false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -213,8 +213,8 @@ void potential_resonance_narrow_sweep()
|
||||
double freq_start = 1.;
|
||||
double freq_end = 1.7;
|
||||
double freq_increment = .002;
|
||||
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];
|
||||
|
||||
@@ -233,14 +233,14 @@ void potential_resonance_narrow_sweep()
|
||||
{
|
||||
// Each thread creates a PenningTrap instance and reuses it throughout
|
||||
// the sweep.
|
||||
PenningTrap trap((unsigned int)100);
|
||||
PenningTrap trap((uint)100);
|
||||
#pragma omp for collapse(2)
|
||||
for (size_t i = 0; i < 3; i++) {
|
||||
for (size_t j = 0; j < freq_iterations; j++) {
|
||||
// Reset particles and give new time dependent potential.
|
||||
trap.reinitialize(amplitudes[i], res[0][j]);
|
||||
res[i + 1][j]
|
||||
= trap.fraction_of_particles_left(time, N, "rk4", false);
|
||||
res[i + 1][j] =
|
||||
trap.fraction_of_particles_left(time, N, "rk4", false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -270,8 +270,8 @@ void potential_resonance_narrow_sweep_interaction()
|
||||
double freq_start = 1.;
|
||||
double freq_end = 1.7;
|
||||
double freq_increment = .002;
|
||||
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];
|
||||
|
||||
@@ -289,7 +289,7 @@ void potential_resonance_narrow_sweep_interaction()
|
||||
{
|
||||
// Each thread creates a PenningTrap instance and reuses it throughout
|
||||
// the sweep.
|
||||
PenningTrap trap((unsigned int)100);
|
||||
PenningTrap trap((uint)100);
|
||||
#pragma omp for collapse(2)
|
||||
for (size_t i = 0; i < 3; i++) {
|
||||
for (size_t j = 0; j < freq_iterations; j++) {
|
||||
|
||||
@@ -20,7 +20,8 @@
|
||||
|
||||
/** @brief Test class for the Penning trap.
|
||||
* */
|
||||
class PenningTrapTest {
|
||||
class PenningTrapTest
|
||||
{
|
||||
public:
|
||||
/** @brief Test that the external E field gives correct values.
|
||||
* */
|
||||
@@ -34,17 +35,17 @@ public:
|
||||
tests.push_back(
|
||||
std::make_pair(vec_3d{0., 0., 0.}, vec_3d{0., 0., 0.}));
|
||||
|
||||
tests.push_back(std::make_pair(vec_3d{10., 0., 0.},
|
||||
vec_3d{96.4852558, 0., 0.}));
|
||||
tests.push_back(
|
||||
std::make_pair(vec_3d{10., 0., 0.}, vec_3d{96.4852558, 0., 0.}));
|
||||
|
||||
tests.push_back(std::make_pair(vec_3d{10., 0., 0.},
|
||||
vec_3d{96.4852558, 0., 0.}));
|
||||
tests.push_back(
|
||||
std::make_pair(vec_3d{10., 0., 0.}, vec_3d{96.4852558, 0., 0.}));
|
||||
|
||||
tests.push_back(std::make_pair(vec_3d{0., 10., 0.},
|
||||
vec_3d{0., 96.4852558, 0.}));
|
||||
tests.push_back(
|
||||
std::make_pair(vec_3d{0., 10., 0.}, vec_3d{0., 96.4852558, 0.}));
|
||||
|
||||
tests.push_back(std::make_pair(vec_3d{0., 0., 10.},
|
||||
vec_3d{0., 0., -192.9705116}));
|
||||
tests.push_back(
|
||||
std::make_pair(vec_3d{0., 0., 10.}, vec_3d{0., 0., -192.9705116}));
|
||||
|
||||
vec_3d result;
|
||||
vec_3d v;
|
||||
@@ -106,8 +107,7 @@ public:
|
||||
static void test_total_force_external()
|
||||
{
|
||||
PenningTrap trap;
|
||||
trap.add_particle(
|
||||
Particle(vec_3d{1., 2., 3.}, vec_3d{3., 4., 5.}));
|
||||
trap.add_particle(Particle(vec_3d{1., 2., 3.}, vec_3d{3., 4., 5.}));
|
||||
|
||||
vec_3d expected{395.58954878, -270.15871624, -57.89115348};
|
||||
vec_3d result = trap.total_force_external(0);
|
||||
@@ -122,8 +122,7 @@ public:
|
||||
static void test_total_force_particles()
|
||||
{
|
||||
PenningTrap trap;
|
||||
trap.add_particle(
|
||||
Particle(vec_3d{0., 0., 0.}, vec_3d{0., 0., 0.}));
|
||||
trap.add_particle(Particle(vec_3d{0., 0., 0.}, vec_3d{0., 0., 0.}));
|
||||
|
||||
vec_3d expected{0., 0., 0.};
|
||||
vec_3d result = trap.total_force_particles(0);
|
||||
@@ -131,12 +130,9 @@ public:
|
||||
"Testing the total force of all particles on particle 0 "
|
||||
"with only a single particle");
|
||||
|
||||
trap.add_particle(
|
||||
Particle(vec_3d{1., 0., 0.}, vec_3d{0., 0., 0.}));
|
||||
trap.add_particle(
|
||||
Particle(vec_3d{0., 1., 0.}, vec_3d{0., 0., 0.}));
|
||||
trap.add_particle(
|
||||
Particle(vec_3d{0., 0., 1.}, vec_3d{0., 0., 0.}));
|
||||
trap.add_particle(Particle(vec_3d{1., 0., 0.}, vec_3d{0., 0., 0.}));
|
||||
trap.add_particle(Particle(vec_3d{0., 1., 0.}, vec_3d{0., 0., 0.}));
|
||||
trap.add_particle(Particle(vec_3d{0., 0., 1.}, vec_3d{0., 0., 0.}));
|
||||
|
||||
expected = vec_3d().fill(-138935.333);
|
||||
result = trap.total_force_particles(0);
|
||||
|
||||
@@ -19,7 +19,8 @@ std::string scientific_format(double d, int width, int prec)
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string scientific_format(const std::vector<double> &v, int width, int prec)
|
||||
std::string scientific_format(const std::vector<double> &v, int width,
|
||||
int prec)
|
||||
{
|
||||
std::stringstream ss;
|
||||
for (double elem : v) {
|
||||
@@ -32,8 +33,7 @@ static void print_message(std::string msg)
|
||||
{
|
||||
if (msg.size() > 0) {
|
||||
std::cout << "message: " << msg << "\n\n";
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
std::cout << "\n";
|
||||
}
|
||||
}
|
||||
@@ -47,8 +47,7 @@ void m_assert(bool expr, std::string expr_str, std::string f, std::string file,
|
||||
if (expr) {
|
||||
std::cout << "\x1B[32mOK\033[0m\n";
|
||||
print_message(msg);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
std::cout << "\x1B[31mFAIL\033[0m\n";
|
||||
print_message(msg);
|
||||
std::cout << file << " " << line << ": Assertion \"" << expr_str
|
||||
@@ -85,11 +84,11 @@ bool mkpath(std::string path, int mode)
|
||||
pos = path.find('/', pos);
|
||||
if (pos != std::string::npos) {
|
||||
cur_dir = path.substr(0, pos);
|
||||
if (mkdir(cur_dir.c_str(), mode) != 0 && stat(cur_dir.c_str(), &buf) != 0) {
|
||||
if (mkdir(cur_dir.c_str(), mode) != 0
|
||||
&& stat(cur_dir.c_str(), &buf) != 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user