Files
karl/src/simulation/mover.cpp
T
2025-05-20 16:47:30 +02:00

473 lines
19 KiB
C++
Executable File

#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 "constants/sim_constants.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.");
}
// TBD: include F_g (simply add to electric force)
void BorisMover::move(particle::Particle* particle,
double collision_time_s,
ElectricField* electric_field,
MagneticField* magnetic_field,
GravityField* gravity_field) {
// only useful if the timescale defined by the gyro frequency allows fast particles
// to move too far
// keep hard coded for now; has to be decided to be either kept (new field for Boris
// mover in json file) or removed again
const double max_distance = 1e-4;
const double delta_timeresolution = particle->specie()->
dynamicDensityField()->timeResolution();
// in order to try to minimize the overshoot in the time domain (i.e. if particle
// can only life a time dt < min(distance/v, maximal_timestep), which might even be
// a lot larger than dt, which would increase the influence of the particle on the
// density)
double max_time_alive = particle->specie()->dynamicDensityField()->physicalTime() -
particle->timeAlive();
double leftover_time = collision_time_s;
auto old_position = particle->position();
double old_kinetic_energy = particle->kineticEnergy_eV();
blitz::TinyVector<double, 3> B_old =
magnetic_field->value(old_position).cartesianEntries(old_position);
double old_mu;
// if magnetic field is 0 use e_z as a reference for the pitch angle
// calculation (don't know if best approach, probably not...)
if (kinematic::norm(B_old) == 0) {
old_mu = kinematic::cosPitchAngle(
blitz::TinyVector<double, 3>(0,0,1),
particle->velocity().cartesianEntries(old_position));
} else {
old_mu = kinematic::cosPitchAngle(
magnetic_field->value(old_position).cartesianEntries(old_position),
particle->velocity().cartesianEntries(old_position));
}
double timestep = 0;
// if (old_kinetic_energy < 1E-9) {
// // Guard against small enegies taking up much time without contributing to
// // density
// SIM_INFO("particle skipped due to energy.");
// return;
// }
bool changed_position_bin = false;
bool changed_energy_bin = false;
bool left_simulation_domain = false;
bool changed_mu_bin = false;
bool died_in_bin = false;
while (
!(changed_position_bin || changed_energy_bin || left_simulation_domain ||
changed_mu_bin || died_in_bin) && (leftover_time > 0.)) {
old_position = particle->position();
old_kinetic_energy = particle->kineticEnergy_eV();
double old_velocity = particle->absoluteVelocity();
timestep = std::min(maximal_time_step_s_, leftover_time);
timestep = std::min(timestep, max_distance / old_velocity);
timestep = std::min(timestep, max_time_alive);
// can probably be removed
// 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);
const blitz::TinyVector<double, 3> g =
gravity_field->value(old_position).cartesianEntries(old_position);
movementStep(particle, E, B, g, timestep);
particle->increaseTimeAlive(timestep);
// particle->increaseTimeInBin(timestep);
auto new_position = particle->position();
auto new_velocity = particle->velocity();
const blitz::TinyVector<double, 3> B_new =
magnetic_field->value(new_position).cartesianEntries(new_position);
double new_mu;
if (kinematic::norm(B_new) == 0) {
new_mu = kinematic::cosPitchAngle(
blitz::TinyVector<double, 3>(0,0,1),
new_velocity.cartesianEntries(new_position));
} else {
new_mu = kinematic::cosPitchAngle(
magnetic_field->value(new_position).cartesianEntries(new_position),
new_velocity.cartesianEntries(new_position));
}
changed_mu_bin =
particle->specie()->dynamicDensityField()->changedPitchAngleBin(
old_mu, new_mu);
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);
died_in_bin =
particle->specie()->terminator()->reachedMaxTimeAlive(particle);
leftover_time -= timestep;
// if max_time_alive == 0 but the difference between the time alive and
// the endtime > 0 (think can happen due to numerical accuracy)
// an infinite loop is created. Therefore catch that case and
// allow for a small overshoot in the timedomain (see comment in drift mover)
max_time_alive = ((max_time_alive - timestep) <= 0)
? delta_timeresolution*SimConstants::compare_epsilon
: (max_time_alive - timestep);
}
if(died_in_bin)
particle->specie()->dynamicDensityField()->logMovement(
old_position, old_kinetic_energy, old_mu, particle->weight());
// 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,
const blitz::TinyVector<double, 3>& gravity_field,
// double& moved_distance,
double timestep) 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) * timestep) / (2. * m)
+ (gravity_field(0) * timestep) / 2.;
const double u_minus_y =
v(1) + (q * electric_field(1) * timestep) / (2. * m)
+ (gravity_field(1) * timestep) / 2.;
const double u_minus_z =
v(2) + (q * electric_field(2) * timestep) / (2. * m)
+ (gravity_field(2) * timestep) / 2.;
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) * timestep) / (2 * m * gamma);
const double temp_1_y =
(q * magnetic_field(1) * timestep) / (2 * m * gamma);
const double temp_1_z =
(q * magnetic_field(2) * timestep) / (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) * timestep) / (2. * m)
+ (gravity_field(0) * timestep) / 2.;
const double u_new_y =
u_plus_y + (q * electric_field(1) * timestep) / (2. * m)
+ (gravity_field(1) * timestep) / 2.;
const double u_new_z =
u_plus_z + (q * electric_field(2) * timestep) / (2. * m)
+ (gravity_field(2) * timestep) / 2.;
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 * timestep);
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 collision_time_s,
ElectricField* electric_field,
MagneticField* magnetic_field,
GravityField* gravity_field) {
double leftover_time = collision_time_s;
auto old_position = particle->position();
auto old_velocity = particle->velocity();
double old_kinetic_energy = particle->kineticEnergy_eV();
double old_mu = kinematic::cosPitchAngle(
magnetic_field->value(old_position).cartesianEntries(old_position),
old_velocity.cartesianEntries(old_position));
bool changed_position_bin = false;
bool changed_energy_bin = false;
bool changed_mu_bin = false;
bool left_simulation_domain = false;
// if particle too slow -> timestep > maximum time the particle lives
// -> add maximal timestep to time in bin and stop moving the particle
bool died_in_bin = false;
double max_time_alive =
particle->specie()->dynamicDensityField()->physicalTime() -
particle->timeAlive();
while (
!(changed_position_bin || changed_energy_bin || changed_mu_bin
|| left_simulation_domain) &&
(leftover_time > 0.) && !(died_in_bin)) {
old_position = particle->position();
old_velocity = particle->velocity();
old_kinetic_energy = particle->kineticEnergy_eV();
blitz::TinyVector<double, 3> force_over_charge =
electric_field->value(old_position).cartesianEntries(old_position) +
gravity_field->value(old_position).cartesianEntries(old_position) *
particle->mass_kg() / particle->charge_C();
const blitz::TinyVector<double, 3> B =
magnetic_field->value(old_position).cartesianEntries(old_position);
const double B_norm = kinematic::norm(B);
const blitz::TinyVector<double, 3> gradB =
magnetic_field->gradientValue(old_position).cartesianEntries(old_position);
// 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);
// magnetic moment
const double magnetic_moment =
(particle->mass_kg()*(v_perp_norm*v_perp_norm)/2)/B_norm;
force_over_charge += -(magnetic_moment / particle->charge_C() * gradB);
// curvature force
force_over_charge += -(particle->mass_kg()/particle->charge_C() *
(v_parallel_norm*v_parallel_norm) *
kinematic::perpendicularVector(gradB, B) /
B_norm);
const blitz::TinyVector<double, 3> v_drift =
driftVelocity(force_over_charge, B);
const double v_drift_norm = kinematic::norm(v_drift);
// const double stepsize = std::min(leftover_distance, maximal_distance_m_);
// guard against very large timesteps i.e. v ~ 0 -> timestep = Inf
// should be a very rare case (but has happend)
double timestep = std::min(
maximal_distance_m_ / std::sqrt(v_parallel_norm*v_parallel_norm +
v_perp_norm*v_perp_norm +
v_drift_norm*v_drift_norm),
max_time_alive);
timestep = std::min(timestep, collision_time_s);
const blitz::TinyVector<double, 3> movement_parallel =
v_parallel * timestep;
const blitz::TinyVector<double, 3> movement_drift = v_drift * timestep;
const blitz::TinyVector<double, 3> force_over_charge_parallel =
kinematic::parallelVector(force_over_charge, B);
const blitz::TinyVector<double, 3> dv_electric = timestep *
particle->charge_C() * force_over_charge_parallel / particle->mass_kg();
if (v_perp_norm/v_parallel_norm > SimConstants::compare_epsilon) {
const double gyro_radius = particle->mass_kg() * v_perp_norm /
(B_norm * particle->charge_C());
const blitz::TinyVector<double, 3> velocity_normal =
(-1) * blitz::cross(B, v_perp);
const blitz::TinyVector<double, 3> 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<double, 3> movement_around_gyro_center =
offset_to_gyro_center -
kinematic::rotateVector(offset_to_gyro_center, B,
total_rotation_angle);
const blitz::TinyVector<double, 3> v_perp_rotated =
kinematic::rotateVector(v_perp, B, total_rotation_angle);
const double v_parallel_norm_new =
kinematic::norm(v_parallel + dv_electric);
// might be better to do after the actual movement and then just extract
// the position again from the particle
const blitz::TinyVector<double, 3> new_position =
old_position.cartesianEntries() + movement_parallel
+ movement_drift + movement_around_gyro_center;
const blitz::TinyVector<double, 3> B_new =
magnetic_field->value(CartesianCoordinate(new_position)).
cartesianEntries(CartesianCoordinate(new_position));
const double v_perp_norm_new =
sqrt(2*magnetic_moment*kinematic::norm(B_new)/particle->mass_kg());
particle->move(CartesianCoordinate(movement_parallel + movement_drift +
movement_around_gyro_center));
const blitz::TinyVector<double, 3> v_parallel_new =
kinematic::parallelVector(v_parallel + dv_electric, B_new);
const blitz::TinyVector<double, 3> v_perp_new =
kinematic::perpendicularVector(v_perp_rotated, B_new);
// calculation might be an issue if v_parallel_norm = 0
// using just B does not work, since then the information about the velocity
// being parallel or anti-parallel is lost
const blitz::TinyVector<double, 3> v_new =
v_parallel_new/kinematic::norm(v_parallel_new)*v_parallel_norm_new +
v_perp_new/kinematic::norm(v_perp_new)*v_perp_norm_new;
particle->velocity(CartesianVector(v_new));
} else {
particle->move(CartesianCoordinate(movement_parallel + movement_drift));
particle->velocity(CartesianVector(v_parallel + dv_electric));
}
particle->increaseTimeAlive(timestep);
// particle->increaseTimeInBin(timestep);
// if instead of eps*deltaT 0.0 is used the integral would be conserved
// if no particles are lost at a boundary. however in that case the terminator does not
// return the correct value if the time the particle is alive is identical
// to the physical time... -> increases computation time alot
// The version below means that each timestep the density can grow by
// (at most) a factor of (1 + eps) (if no particles are lost)
// TODO:
// - check if it's not better to change the terminator such that is uses the
// compare function from /include/simulation/compare.h
max_time_alive = ((max_time_alive - timestep) <= 0)
? particle->specie()->dynamicDensityField()
->timeResolution()*SimConstants::compare_epsilon
: (max_time_alive - timestep);
if (max_time_alive == 0) {
DEBUG_LEVEL_3("Mover: max_time_alive is 0.");
DEBUG_LEVEL_3("Mover: particle time alive: " +
std::to_string(particle->timeAlive()));
}
const auto new_position = particle->position();
died_in_bin =
particle->specie()->terminator()->reachedMaxTimeAlive(particle);
if(died_in_bin) {
DEBUG_LEVEL_3("Mover: particle died in bin.");
}
changed_position_bin =
particle->specie()->dynamicDensityField()->changedPositionBin(
old_position, new_position);
changed_energy_bin =
particle->specie()->dynamicDensityField()->changedEnergyBin(
old_kinetic_energy, particle->kineticEnergy_eV());
const double new_mu = kinematic::cosPitchAngle(magnetic_field
->value(new_position).cartesianEntries(new_position),
particle->velocity().cartesianEntries(new_position));
changed_mu_bin = particle->specie()->dynamicDensityField()
->changedPitchAngleBin(old_mu, new_mu);
if(changed_mu_bin) {
DEBUG_LEVEL_3("cos(pitch old): " + std::to_string(old_mu));
DEBUG_LEVEL_3("cos(pitch new): " + std::to_string(new_mu));
}
left_simulation_domain =
particle->specie()->terminator()->leftSimulationDomain(particle);
leftover_time -= timestep;
}
// only log a particle in the bin where its movement ends, i.e. if
// the particle reached the end of the timestep
if(died_in_bin)
particle->specie()->dynamicDensityField()->logMovement(
old_position, old_kinetic_energy, old_mu, particle->weight());
particle->specie()->currentCollector()->collect(particle, old_position);
}
blitz::TinyVector<double, 3> DriftMover::driftVelocity(
const blitz::TinyVector<double, 3>& force_field_over_charge,
const blitz::TinyVector<double, 3>& magnetic_field) const {
return blitz::cross(force_field_over_charge, magnetic_field) /
blitz::dot(magnetic_field, magnetic_field);
}