Version 2, position dependent E/B, kinetic ions, boris mover, terminator
This commit is contained in:
Executable
+283
@@ -0,0 +1,283 @@
|
||||
#include "simulation/mover.h"
|
||||
|
||||
#include "constants/physical_constants.h"
|
||||
#include "simulation/coordinates.h"
|
||||
#include "simulation/current_collector.h"
|
||||
#include "simulation/density_field.h"
|
||||
#include "simulation/errors.h"
|
||||
#include "simulation/kinematic.h"
|
||||
#include "simulation/particle.h"
|
||||
#include "simulation/reflector.h"
|
||||
#include "simulation/terminator.h"
|
||||
|
||||
#include "tools/simulation_time.h"
|
||||
|
||||
BorisMover::BorisMover(double maximal_time_step_s)
|
||||
: maximal_time_step_s_(maximal_time_step_s) {
|
||||
ASSERT_POSITIVE(&maximal_time_step_s_,
|
||||
"The maximal timestep for boris push must be positive.");
|
||||
}
|
||||
|
||||
void BorisMover::move(particle::Particle* particle,
|
||||
double mfp_m,
|
||||
ElectricField* electric_field,
|
||||
MagneticField* magnetic_field) {
|
||||
double leftover_distance = mfp_m;
|
||||
|
||||
auto old_position = particle->position();
|
||||
double old_kinetic_energy = particle->kineticEnergy_eV();
|
||||
|
||||
if (old_kinetic_energy < 1E-9) {
|
||||
// Guard against small enegies taking up much time without contributing to
|
||||
// density
|
||||
return;
|
||||
}
|
||||
|
||||
bool changed_position_bin = false;
|
||||
bool changed_energy_bin = false;
|
||||
bool left_simulation_domain = false;
|
||||
|
||||
while (
|
||||
!(changed_position_bin || changed_energy_bin || left_simulation_domain) &&
|
||||
(leftover_distance > 0.)) {
|
||||
old_position = particle->position();
|
||||
old_kinetic_energy = particle->kineticEnergy_eV();
|
||||
|
||||
double moved_distance = 0.;
|
||||
|
||||
const blitz::TinyVector<double, 3> E =
|
||||
electric_field->value(old_position).cartesianEntries(old_position);
|
||||
const blitz::TinyVector<double, 3> B =
|
||||
magnetic_field->value(old_position).cartesianEntries(old_position);
|
||||
|
||||
movementStep(particle, E, B, moved_distance);
|
||||
particle->increaseTimeAlive(maximal_time_step_s_);
|
||||
particle->increaseTimeInBin(maximal_time_step_s_);
|
||||
|
||||
auto new_position = particle->position();
|
||||
|
||||
changed_position_bin =
|
||||
particle->specie()->dynamicDensityField()->changedPositionBin(
|
||||
old_position, new_position);
|
||||
|
||||
changed_energy_bin =
|
||||
particle->specie()->dynamicDensityField()->changedEnergyBin(
|
||||
old_kinetic_energy, particle->kineticEnergy_eV());
|
||||
|
||||
left_simulation_domain =
|
||||
particle->specie()->terminator()->leftSimulationDomain(particle);
|
||||
|
||||
leftover_distance -= moved_distance;
|
||||
}
|
||||
|
||||
particle->specie()->dynamicDensityField()->logMovement(
|
||||
old_position, old_kinetic_energy, particle->timeInBin());
|
||||
particle->resetTimeInBin();
|
||||
particle->specie()->currentCollector()->collect(particle, old_position);
|
||||
}
|
||||
|
||||
void BorisMover::movementStep(
|
||||
particle::Particle* particle,
|
||||
const blitz::TinyVector<double, 3>& electric_field,
|
||||
const blitz::TinyVector<double, 3>& magnetic_field,
|
||||
double& moved_distance) const {
|
||||
const blitz::TinyVector<double, 3> v =
|
||||
particle->velocity().cartesianEntries();
|
||||
const double m = particle->mass_kg();
|
||||
const double q = particle->charge_C();
|
||||
|
||||
const double u_minus_x =
|
||||
v(0) + (q * electric_field(0) * maximal_time_step_s_) / (2. * m);
|
||||
const double u_minus_y =
|
||||
v(1) + (q * electric_field(1) * maximal_time_step_s_) / (2. * m);
|
||||
const double u_minus_z =
|
||||
v(2) + (q * electric_field(2) * maximal_time_step_s_) / (2. * m);
|
||||
|
||||
const double gamma = kinematic::relativisticGamma(
|
||||
blitz::TinyVector<double, 3>(u_minus_x, u_minus_y, u_minus_z));
|
||||
|
||||
const double temp_1_x =
|
||||
(q * magnetic_field(0) * maximal_time_step_s_) / (2 * m * gamma);
|
||||
const double temp_1_y =
|
||||
(q * magnetic_field(1) * maximal_time_step_s_) / (2 * m * gamma);
|
||||
const double temp_1_z =
|
||||
(q * magnetic_field(2) * maximal_time_step_s_) / (2 * m * gamma);
|
||||
|
||||
const double u_prime_x =
|
||||
u_minus_x + (u_minus_y * temp_1_z - u_minus_z * temp_1_y);
|
||||
const double u_prime_y =
|
||||
u_minus_y + (u_minus_z * temp_1_x - u_minus_x * temp_1_z);
|
||||
const double u_prime_z =
|
||||
u_minus_z + (u_minus_x * temp_1_y - u_minus_y * temp_1_x);
|
||||
|
||||
const double temp_2_x =
|
||||
(2. * temp_1_x) /
|
||||
(1 + temp_1_x * temp_1_x + temp_1_y * temp_1_y + temp_1_z * temp_1_z);
|
||||
const double temp_2_y =
|
||||
(2. * temp_1_y) /
|
||||
(1 + temp_1_x * temp_1_x + temp_1_y * temp_1_y + temp_1_z * temp_1_z);
|
||||
const double temp_2_z =
|
||||
(2. * temp_1_z) /
|
||||
(1 + temp_1_x * temp_1_x + temp_1_y * temp_1_y + temp_1_z * temp_1_z);
|
||||
|
||||
const double u_plus_x =
|
||||
u_minus_x + (u_prime_y * temp_2_z - u_prime_z * temp_2_y);
|
||||
const double u_plus_y =
|
||||
u_minus_y + (u_prime_z * temp_2_x - u_prime_x * temp_2_z);
|
||||
const double u_plus_z =
|
||||
u_minus_z + (u_prime_x * temp_2_y - u_prime_y * temp_2_x);
|
||||
|
||||
const double u_new_x =
|
||||
u_plus_x + (q * electric_field(0) * maximal_time_step_s_) / (2. * m);
|
||||
const double u_new_y =
|
||||
u_plus_y + (q * electric_field(1) * maximal_time_step_s_) / (2. * m);
|
||||
const double u_new_z =
|
||||
u_plus_z + (q * electric_field(2) * maximal_time_step_s_) / (2. * m);
|
||||
const blitz::TinyVector<double, 3> u_new(u_new_x, u_new_y, u_new_z);
|
||||
|
||||
/*
|
||||
Might be slower, but essentially the same code
|
||||
const blitz::TinyVector<double, 3> u_minus =
|
||||
v + (q * electric_field * maximal_time_step_s_) / (2. * m);
|
||||
const double gamma = kinematic::relativisticGamma(u_minus);;
|
||||
const blitz::TinyVector<double, 3> temp_1 =
|
||||
(q * magnetic_field * maximal_time_step_s_) / (2. * m * gamma);
|
||||
const blitz::TinyVector<double, 3> u_prime =
|
||||
u_minus + (blitz::cross(u_minus, temp_1));
|
||||
const blitz::TinyVector<double, 3> temp_2 =
|
||||
(2. * temp_1) / (1. + (blitz::dot(temp_1, temp_1)));
|
||||
const blitz::TinyVector<double, 3> u_plus =
|
||||
u_minus + (blitz::cross(u_prime, temp_2));
|
||||
const blitz::TinyVector<double, 3> u_new =
|
||||
u_plus + (q * electric_field * maximal_time_step_s_) / (2. * m);
|
||||
*/
|
||||
|
||||
particle->velocity(CartesianVector(u_new));
|
||||
|
||||
const CartesianCoordinate distance(u_new * maximal_time_step_s_);
|
||||
particle->move(distance);
|
||||
|
||||
moved_distance += std::sqrt(
|
||||
blitz::dot(distance.cartesianEntries(), distance.cartesianEntries()));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
DriftMover::DriftMover(double maximal_distance)
|
||||
: maximal_distance_m_(maximal_distance){};
|
||||
|
||||
void DriftMover::move(particle::Particle* particle,
|
||||
double mfp_m,
|
||||
ElectricField* electric_field,
|
||||
MagneticField* magnetic_field) {
|
||||
double leftover_distance = mfp_m;
|
||||
|
||||
auto old_position = particle->position();
|
||||
auto old_velocity = particle->velocity();
|
||||
double old_kinetic_energy = particle->kineticEnergy_eV();
|
||||
|
||||
bool changed_position_bin = false;
|
||||
bool changed_energy_bin = false;
|
||||
bool left_simulation_domain = false;
|
||||
|
||||
while (
|
||||
!(changed_position_bin || changed_energy_bin || left_simulation_domain) &&
|
||||
(leftover_distance > 0.)) {
|
||||
old_position = particle->position();
|
||||
old_velocity = particle->velocity();
|
||||
|
||||
old_kinetic_energy = particle->kineticEnergy_eV();
|
||||
|
||||
const blitz::TinyVector<double, 3> E =
|
||||
electric_field->value(old_position).cartesianEntries(old_position);
|
||||
|
||||
const blitz::TinyVector<double, 3> B =
|
||||
magnetic_field->value(old_position).cartesianEntries(old_position);
|
||||
const double B_norm = kinematic::norm(B);
|
||||
// Todo: add calculation of divB here
|
||||
// use Kassiopeia paper for reference
|
||||
|
||||
const blitz::TinyVector<double, 3> v_parallel =
|
||||
kinematic::parallelVector(old_velocity.cartesianEntries(), B);
|
||||
const double v_parallel_norm = kinematic::norm(v_parallel);
|
||||
|
||||
const blitz::TinyVector<double, 3> v_perp =
|
||||
kinematic::perpendicularVector(old_velocity.cartesianEntries(), B);
|
||||
const double v_perp_norm = kinematic::norm(v_perp);
|
||||
|
||||
const double gyro_radius = particle->mass_kg() * v_perp_norm /
|
||||
(B_norm * std::abs(particle->charge_C()));
|
||||
|
||||
const blitz::TinyVector<double, 3> velocity_normal =
|
||||
(-1) * blitz::cross(v_parallel, v_perp);
|
||||
|
||||
const blitz::TinyVector<double, 3> offset_to_gyro_center =
|
||||
gyro_radius * velocity_normal / kinematic::norm(velocity_normal);
|
||||
|
||||
const blitz::TinyVector<double, 3> v_drift = driftVelocity(B, E);
|
||||
const double v_drift_norm = kinematic::norm(v_drift);
|
||||
|
||||
const double stepsize = std::min(leftover_distance, maximal_distance_m_);
|
||||
const double timestep =
|
||||
stepsize / (v_parallel_norm + v_perp_norm + v_drift_norm);
|
||||
|
||||
const blitz::TinyVector<double, 3> movement_parallel =
|
||||
v_parallel * timestep;
|
||||
|
||||
const blitz::TinyVector<double, 3> movement_drift = v_drift * timestep;
|
||||
|
||||
const double gyro_frequency =
|
||||
particle->charge_C() * B_norm / particle->mass_kg();
|
||||
const double total_rotation_angle = gyro_frequency * timestep;
|
||||
const blitz::TinyVector<double, 3> movement_around_gyro_center =
|
||||
kinematic::rotateVector(offset_to_gyro_center, v_parallel,
|
||||
total_rotation_angle) -
|
||||
offset_to_gyro_center;
|
||||
|
||||
particle->move(CartesianCoordinate(movement_parallel + movement_drift +
|
||||
movement_around_gyro_center));
|
||||
|
||||
const blitz::TinyVector<double, 3> E_parallel =
|
||||
kinematic::parallelVector(E, B);
|
||||
const blitz::TinyVector<double, 3> dv_electric =
|
||||
timestep * particle->charge_C() * E_parallel / particle->mass_kg();
|
||||
|
||||
const blitz::TinyVector<double, 3> v_perp_rotated =
|
||||
kinematic::rotateVector(v_perp, v_parallel, total_rotation_angle);
|
||||
|
||||
particle->velocity(
|
||||
CartesianVector(v_parallel + v_perp_rotated + dv_electric));
|
||||
|
||||
particle->increaseTimeAlive(timestep);
|
||||
particle->increaseTimeInBin(timestep);
|
||||
|
||||
const auto new_position = particle->position();
|
||||
|
||||
changed_position_bin =
|
||||
particle->specie()->dynamicDensityField()->changedPositionBin(
|
||||
old_position, new_position);
|
||||
|
||||
changed_energy_bin =
|
||||
particle->specie()->dynamicDensityField()->changedEnergyBin(
|
||||
old_kinetic_energy, particle->kineticEnergy_eV());
|
||||
|
||||
left_simulation_domain =
|
||||
particle->specie()->terminator()->leftSimulationDomain(particle);
|
||||
|
||||
leftover_distance -= stepsize;
|
||||
}
|
||||
|
||||
particle->specie()->dynamicDensityField()->logMovement(
|
||||
old_position, old_kinetic_energy, particle->timeInBin());
|
||||
particle->resetTimeInBin();
|
||||
particle->specie()->currentCollector()->collect(particle, old_position);
|
||||
}
|
||||
|
||||
blitz::TinyVector<double, 3> DriftMover::driftVelocity(
|
||||
const blitz::TinyVector<double, 3>& magnetic_field,
|
||||
const blitz::TinyVector<double, 3>& electric_field) const {
|
||||
return blitz::cross(magnetic_field, electric_field) /
|
||||
blitz::dot(magnetic_field, magnetic_field);
|
||||
}
|
||||
Reference in New Issue
Block a user