From f79172b454cd8cf3cfe213f32d0878b94f456da8 Mon Sep 17 00:00:00 2001 From: Jonas Kellerer Date: Tue, 22 Nov 2022 18:12:10 +0100 Subject: [PATCH] Add guard against zero gyro radius in drift mover --- src/simulation/mover.cpp | 54 +++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/src/simulation/mover.cpp b/src/simulation/mover.cpp index 5a62b16..46e0fc7 100755 --- a/src/simulation/mover.cpp +++ b/src/simulation/mover.cpp @@ -188,15 +188,6 @@ void DriftMover::move(particle::Particle* particle, 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 velocity_normal = - (-1) * blitz::cross(v_parallel, v_perp); - - const blitz::TinyVector offset_to_gyro_center = - gyro_radius * velocity_normal / kinematic::norm(velocity_normal); - const blitz::TinyVector v_drift = driftVelocity(B, E); const double v_drift_norm = kinematic::norm(v_drift); @@ -206,30 +197,43 @@ void DriftMover::move(particle::Particle* particle, const blitz::TinyVector movement_parallel = v_parallel * timestep; - const blitz::TinyVector 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 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 E_parallel = kinematic::parallelVector(E, B); const blitz::TinyVector dv_electric = timestep * particle->charge_C() * E_parallel / particle->mass_kg(); - const blitz::TinyVector v_perp_rotated = - kinematic::rotateVector(v_perp, v_parallel, total_rotation_angle); + if (v_perp_norm > 0.) { + const double gyro_radius = particle->mass_kg() * v_perp_norm / + (B_norm * std::abs(particle->charge_C())); - particle->velocity( - CartesianVector(v_parallel + v_perp_rotated + dv_electric)); + const blitz::TinyVector velocity_normal = + (-1) * blitz::cross(B, v_perp); + + const blitz::TinyVector offset_to_gyro_center = + gyro_radius * velocity_normal / kinematic::norm(velocity_normal); + + const double gyro_frequency = + particle->charge_C() * B_norm / particle->mass_kg(); + const double total_rotation_angle = gyro_frequency * timestep; + const blitz::TinyVector movement_around_gyro_center = + kinematic::rotateVector(offset_to_gyro_center, B, + total_rotation_angle) - + offset_to_gyro_center; + + const blitz::TinyVector v_perp_rotated = + kinematic::rotateVector(v_perp, B, total_rotation_angle); + + particle->move(CartesianCoordinate(movement_parallel + movement_drift + + movement_around_gyro_center)); + + particle->velocity( + CartesianVector(v_parallel + v_perp_rotated + dv_electric)); + } else { + particle->move(CartesianCoordinate(movement_parallel + movement_drift)); + particle->velocity(CartesianVector(v_parallel + dv_electric)); + } particle->increaseTimeAlive(timestep); particle->increaseTimeInBin(timestep);