Updated Boris mover to include gravity fields and
in case B=0 use z-direction for mu calculation; correction of units in analysis script
This commit is contained in:
@@ -256,7 +256,7 @@ def plotLongitudinalParticleCurrentVsZ(filename, path_to_species, plot_axis, out
|
||||
z_bins = zBinsDensityField(filename, path_to_species)
|
||||
plot_axis.plot(z_bins, current_in_z, label=str(path_to_species))
|
||||
plot_axis.set_xlabel(r"Distance to rear wall $z$ (m)")
|
||||
plot_axis.set_ylabel(r"Particle current $j$ ($m^{-3}s^{-1}$)")
|
||||
plot_axis.set_ylabel(r"Particle current $j$ ($m^{-2}s^{-1}$)")
|
||||
|
||||
|
||||
def plotAllLongitudinalParticleCurrentVsZ(filename, output_period, specie_keys, output_folder):
|
||||
|
||||
@@ -29,7 +29,9 @@ class BorisMover : public Mover {
|
||||
void 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>& gravity_field,
|
||||
double& moved_distance,
|
||||
double timestep) const;
|
||||
};
|
||||
|
||||
class DriftMover : public Mover {
|
||||
|
||||
+107
-35
@@ -19,44 +19,98 @@ BorisMover::BorisMover(double 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 mfp_m,
|
||||
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;
|
||||
double leftover_distance = mfp_m;
|
||||
|
||||
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) &&
|
||||
(leftover_distance > 0.)) {
|
||||
!(changed_position_bin || changed_energy_bin || left_simulation_domain ||
|
||||
changed_mu_bin || died_in_bin) && (leftover_distance > 0.)) {
|
||||
old_position = particle->position();
|
||||
old_kinetic_energy = particle->kineticEnergy_eV();
|
||||
|
||||
double old_velocity = particle->absoluteVelocity();
|
||||
timestep = std::min(maximal_time_step_s_,
|
||||
(leftover_distance + SimConstants::random_lower_limit) / old_velocity);
|
||||
timestep = std::min(timestep, max_distance / old_velocity);
|
||||
|
||||
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, moved_distance);
|
||||
particle->increaseTimeAlive(maximal_time_step_s_);
|
||||
particle->increaseTimeInBin(maximal_time_step_s_);
|
||||
movementStep(particle, E, B, g, moved_distance, 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(
|
||||
@@ -69,12 +123,14 @@ void BorisMover::move(particle::Particle* particle,
|
||||
left_simulation_domain =
|
||||
particle->specie()->terminator()->leftSimulationDomain(particle);
|
||||
|
||||
died_in_bin =
|
||||
particle->specie()->terminator()->reachedMaxTimeAlive(particle);
|
||||
|
||||
leftover_distance -= moved_distance;
|
||||
}
|
||||
|
||||
// todo: pitchangle stuff in Boris mover
|
||||
particle->specie()->dynamicDensityField()->logMovement(
|
||||
old_position, old_kinetic_energy, 0, particle->timeInBin());
|
||||
old_position, old_kinetic_energy, old_mu, particle->timeInBin());
|
||||
particle->resetTimeInBin();
|
||||
particle->specie()->currentCollector()->collect(particle, old_position);
|
||||
}
|
||||
@@ -83,28 +139,33 @@ 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>& 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) * maximal_time_step_s_) / (2. * m);
|
||||
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) * maximal_time_step_s_) / (2. * m);
|
||||
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) * maximal_time_step_s_) / (2. * m);
|
||||
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) * maximal_time_step_s_) / (2 * m * gamma);
|
||||
(q * magnetic_field(0) * timestep) / (2 * m * gamma);
|
||||
const double temp_1_y =
|
||||
(q * magnetic_field(1) * maximal_time_step_s_) / (2 * m * gamma);
|
||||
(q * magnetic_field(1) * timestep) / (2 * m * gamma);
|
||||
const double temp_1_z =
|
||||
(q * magnetic_field(2) * maximal_time_step_s_) / (2 * m * gamma);
|
||||
(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);
|
||||
@@ -131,11 +192,14 @@ void BorisMover::movementStep(
|
||||
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);
|
||||
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) * maximal_time_step_s_) / (2. * m);
|
||||
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) * maximal_time_step_s_) / (2. * m);
|
||||
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);
|
||||
|
||||
/*
|
||||
@@ -157,7 +221,7 @@ void BorisMover::movementStep(
|
||||
|
||||
particle->velocity(CartesianVector(u_new));
|
||||
|
||||
const CartesianCoordinate distance(u_new * maximal_time_step_s_);
|
||||
const CartesianCoordinate distance(u_new * timestep);
|
||||
particle->move(distance);
|
||||
|
||||
moved_distance += std::sqrt(
|
||||
@@ -192,7 +256,6 @@ void DriftMover::move(particle::Particle* particle,
|
||||
// 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();
|
||||
@@ -229,7 +292,8 @@ void DriftMover::move(particle::Particle* particle,
|
||||
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;
|
||||
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);
|
||||
|
||||
@@ -239,14 +303,17 @@ void DriftMover::move(particle::Particle* particle,
|
||||
kinematic::perpendicularVector(gradB, B) /
|
||||
B_norm);
|
||||
|
||||
const blitz::TinyVector<double, 3> v_drift = driftVelocity(force_over_charge, B);
|
||||
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),
|
||||
stepsize / std::sqrt(v_parallel_norm*v_parallel_norm +
|
||||
v_perp_norm*v_perp_norm +
|
||||
v_drift_norm*v_drift_norm),
|
||||
max_time_alive);
|
||||
|
||||
const blitz::TinyVector<double, 3> movement_parallel =
|
||||
@@ -256,8 +323,8 @@ void DriftMover::move(particle::Particle* particle,
|
||||
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();
|
||||
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 /
|
||||
@@ -280,13 +347,13 @@ void DriftMover::move(particle::Particle* particle,
|
||||
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);
|
||||
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> 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)).
|
||||
@@ -303,6 +370,9 @@ void DriftMover::move(particle::Particle* particle,
|
||||
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;
|
||||
@@ -315,11 +385,12 @@ void DriftMover::move(particle::Particle* particle,
|
||||
|
||||
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)
|
||||
// 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
|
||||
@@ -329,7 +400,8 @@ void DriftMover::move(particle::Particle* particle,
|
||||
: (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()));
|
||||
DEBUG_LEVEL_3("Mover: particle time alive: " +
|
||||
std::to_string(particle->timeAlive()));
|
||||
}
|
||||
|
||||
const auto new_position = particle->position();
|
||||
|
||||
Reference in New Issue
Block a user