Updates Driftmover; slight improvements Boris push

This commit is contained in:
2025-10-01 09:37:49 +02:00
parent 80b46f70e6
commit f87bb0d9f0
+43 -40
View File
@@ -29,49 +29,40 @@ void BorisMover::move(particle::Particle* particle,
// 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 max_distance = 1e-3;
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 = delta_timeresolution -//particle->specie()->dynamicDensityField()->physicalTime() -
particle->timeAlive();
double max_time_alive = delta_timeresolution - particle->timeAlive();
double leftover_time = collision_time_s;
// auto old_position = particle->position();
CartesianCoordinate old_position_cart = particle->cartesianPosition();
CylinderCoordinate old_position_cyl = particle->cylinderPosition();
double old_kinetic_energy = particle->kineticEnergy_eV();
blitz::TinyVector<double, 3> B_old =
blitz::TinyVector<double, 3> B =
magnetic_field->value(old_position_cyl).cartesianEntries(old_position_cart);
double old_mu;
double new_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) {
if (kinematic::norm(B) == 0) {
old_mu = kinematic::cosPitchAngle(
blitz::TinyVector<double, 3>(0,0,1),
particle->velocity().cartesianEntries(old_position_cart));
} else {
old_mu = kinematic::cosPitchAngle(
B_old, particle->velocity().cartesianEntries(old_position_cart));
B, particle->velocity().cartesianEntries(old_position_cart));
}
new_mu = old_mu;
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;
@@ -90,33 +81,26 @@ void BorisMover::move(particle::Particle* particle,
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_cyl).cartesianEntries(old_position_cart);
const blitz::TinyVector<double, 3> B =
magnetic_field->value(old_position_cyl).cartesianEntries(old_position_cart);
const blitz::TinyVector<double, 3> g =
gravity_field->value(old_position_cyl).cartesianEntries(old_position_cart);
movementStep(particle, E, B, g, timestep);
particle->increaseTimeAlive(timestep);
// particle->increaseTimeInBin(timestep);
auto new_position_cart = particle->cartesianPosition();
auto new_velocity = particle->velocity();
const blitz::TinyVector<double, 3> B_new =
magnetic_field->value(particle->cylinderPosition()).
B = magnetic_field->value(particle->cylinderPosition()).
cartesianEntries(new_position_cart);
if (kinematic::norm(B_new) == 0) {
if (kinematic::norm(B) == 0) {
new_mu = kinematic::cosPitchAngle(
blitz::TinyVector<double, 3>(0,0,1), new_velocity.cartesianEntries());
} else {
new_mu = kinematic::cosPitchAngle(
B_new, new_velocity.cartesianEntries());
new_mu = kinematic::cosPitchAngle(B, new_velocity.cartesianEntries());
}
changed_mu_bin =
@@ -148,9 +132,8 @@ void BorisMover::move(particle::Particle* particle,
? delta_timeresolution*SimConstants::compare_epsilon
: (max_time_alive - timestep);
}
if(died_in_bin && !left_simulation_domain) {
// && (leftover_time > SimConstants::compare_epsilon ||
// changed_energy_bin || changed_mu_bin || changed_position_bin)) {
if(died_in_bin && !left_simulation_domain) {
// use new values, since that is necessary if died_in_bin and a change in
// bin can occur at the same time
particle->specie()->dynamicDensityField()->logMovement(
@@ -158,7 +141,28 @@ if(died_in_bin && !left_simulation_domain) {
particle->weight());
particle->deposited();
}
// particle->resetTimeInBin();
#ifdef debug_level3
if((changed_energy_bin || changed_position_bin || changed_mu_bin) && !left_simulation_domain && !died_in_bin && (leftover_time > 0)) {
std::stringstream out;
out << "move(...) interrupted by change in ";
if(changed_energy_bin){
out << "energy bin: ";
out << old_kinetic_energy << " (old), " << particle->kineticEnergy_eV();
} else if (changed_position_bin) {
out << "position bin: ";
out << old_position_cyl.cylinderEntries() << " (old), " << particle->cylinderPosition().cylinderEntries();
} else if(changed_mu_bin) {
out << "mu bin: ";
out << old_mu << " (old), " << new_mu;
} else {
out <<"unknown bin... " << changed_energy_bin << changed_position_bin << changed_mu_bin << left_simulation_domain << died_in_bin << leftover_time;
}
out << "\n(gyroradius = " << particle->mass_kg()*std::sqrt(1-new_mu*new_mu)*particle->absoluteVelocity()/(particle->specie()->charge_C()*kinematic::norm(B)) << " m";
out << " energy: " << particle->kineticEnergy_eV() <<" eV)";
DEBUG(out.str());
}
#endif
particle->specie()->currentCollector()->collect(particle, old_position_cyl);
}
@@ -167,8 +171,8 @@ void BorisMover::movementStep(
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();
@@ -237,9 +241,8 @@ void BorisMover::movementStep(
Might be slower, but essentially the same code */
const blitz::TinyVector<double, 3> u_minus =
v + (acceleration * timestep) / (2.);
const double gamma = kinematic::relativisticGamma(u_minus);;
const blitz::TinyVector<double, 3> temp_1 =
(q * magnetic_field * timestep) / (2. * m * gamma);
(q * magnetic_field * timestep) / (2. * m * kinematic::relativisticGamma(u_minus));
const blitz::TinyVector<double, 3> u_prime =
u_minus + (blitz::cross(u_minus, temp_1));
const blitz::TinyVector<double, 3> temp_2 =
@@ -253,9 +256,6 @@ void BorisMover::movementStep(
const CartesianCoordinate distance(u_new * timestep);
particle->move(distance);
// moved_distance += std::sqrt(
// blitz::dot(distance.cartesianEntries(), distance.cartesianEntries()));
}
////////////////////////////////////////////////////////////////////////////////
@@ -278,6 +278,7 @@ void DriftMover::move(particle::Particle* particle,
double old_mu = kinematic::cosPitchAngle(
magnetic_field->value(old_position).cartesianEntries(old_position),
old_velocity.cartesianEntries(old_position));
double new_mu = old_mu;
bool changed_position_bin = false;
bool changed_energy_bin = false;
@@ -287,7 +288,7 @@ void DriftMover::move(particle::Particle* particle,
// -> 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->specie()->dynamicDensityField()->timeResolution() -
particle->timeAlive();
while (
@@ -345,7 +346,7 @@ void DriftMover::move(particle::Particle* particle,
v_perp_norm*v_perp_norm +
v_drift_norm*v_drift_norm),
max_time_alive);
timestep = std::min(timestep, collision_time_s);
timestep = std::min(timestep, leftover_time);
const blitz::TinyVector<double, 3> movement_parallel =
v_parallel * timestep;
@@ -455,7 +456,7 @@ void DriftMover::move(particle::Particle* particle,
particle->specie()->dynamicDensityField()->changedEnergyBin(
old_kinetic_energy, particle->kineticEnergy_eV());
const double new_mu = kinematic::cosPitchAngle(magnetic_field
new_mu = kinematic::cosPitchAngle(magnetic_field
->value(new_position).cartesianEntries(new_position),
particle->velocity().cartesianEntries(new_position));
@@ -473,13 +474,15 @@ void DriftMover::move(particle::Particle* particle,
particle->specie()->terminator()->leftSimulationDomain(particle);
leftover_time -= timestep;
particle->reduceTimeUntilCollision(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->cylinderPosition(), particle->kineticEnergy_eV(), new_mu,
particle->weight());
particle->specie()->currentCollector()->collect(particle, old_position);
}