Still includes (and uses) the non-pitch angle resolved fields as well; have to be either removed completely or refactored s.t. one of the two options is chosen. Pitch angle distribution has a hardcoded number of bins for now, and exists in 'energy_binning.h'
388 lines
16 KiB
C++
Executable File
388 lines
16 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"
|
|
|
|
double cosAngleBetweenVectors(blitz::TinyVector<double, 3> vector_1,
|
|
blitz::TinyVector<double, 3> vector_2) {
|
|
const double divisor = kinematic::norm(vector_1) * kinematic::norm(vector_2);
|
|
return blitz::dot(vector_1, vector_2)/divisor;
|
|
}
|
|
|
|
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,
|
|
GravityField* gravity_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,
|
|
GravityField* gravity_field) {
|
|
double leftover_distance = mfp_m;
|
|
|
|
auto old_position = particle->position();
|
|
auto old_velocity = particle->velocity();
|
|
double old_kinetic_energy = particle->kineticEnergy_eV();
|
|
double old_mu = cosAngleBetweenVectors(
|
|
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;
|
|
// DEBUG_LEVEL_3("Mover: timealive before moving: " + std::to_string(particle->timeAlive()));
|
|
double max_time_alive =
|
|
particle->specie()->dynamicDensityField()->physicalTime() -
|
|
particle->timeAlive();
|
|
|
|
while (
|
|
!(changed_position_bin || changed_energy_bin || changed_mu_bin
|
|
|| left_simulation_domain) &&
|
|
(leftover_distance > 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)
|
|
const double timestep = std::min(
|
|
stepsize / (v_parallel_norm + v_perp_norm + v_drift_norm),
|
|
max_time_alive);
|
|
|
|
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 > 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);
|
|
|
|
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 were lost
|
|
// 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 = cosAngleBetweenVectors(magnetic_field
|
|
->value(new_position).cartesianEntries(new_position),
|
|
particle->velocity().cartesianEntries(new_position));
|
|
|
|
changed_mu_bin = particle->specie()->dynamicDensityFieldWithPitchAngle()
|
|
->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_distance -= stepsize;
|
|
}
|
|
|
|
particle->specie()->dynamicDensityField()->logMovement(
|
|
old_position, old_kinetic_energy, particle->timeInBin());
|
|
particle->specie()->dynamicDensityFieldWithPitchAngle()->logMovement(
|
|
old_position, old_kinetic_energy, old_mu, particle->timeInBin());
|
|
particle->resetTimeInBin();
|
|
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);
|
|
}
|