From 9cd7db4e05098331ec4b640bb4b0ff84a1d78fd2 Mon Sep 17 00:00:00 2001 From: csendlinger Date: Tue, 18 Feb 2025 11:17:24 +0100 Subject: [PATCH] Bug fixes and formatting --- src/IO/restart.cpp | 6 ++-- src/simulation/density_field.cpp | 29 ++++++++--------- .../event_position_distribution.cpp | 3 +- src/simulation/mover.cpp | 31 ++++++++++++++----- src/simulation/simulation.cpp | 5 ++- src/simulation/velocity_distribution.cpp | 5 ++- 6 files changed, 48 insertions(+), 31 deletions(-) diff --git a/src/IO/restart.cpp b/src/IO/restart.cpp index 49ee7c5..3ce0e5c 100644 --- a/src/IO/restart.cpp +++ b/src/IO/restart.cpp @@ -27,12 +27,12 @@ void Restart::restart(std::mapsecond.get()); for(const auto& pair: species) { diff --git a/src/simulation/density_field.cpp b/src/simulation/density_field.cpp index 1417076..57f6279 100755 --- a/src/simulation/density_field.cpp +++ b/src/simulation/density_field.cpp @@ -174,17 +174,15 @@ bool DynamicDensityField::changedPositionBin( (old_bin(2) == new_bin(2))); } -bool DynamicDensityField::changedEnergyBin( - double old_energy_eV, - double new_energy_eV) const { +bool DynamicDensityField::changedEnergyBin(double old_energy_eV, + double new_energy_eV) const { const int old_bin = energy_binning_.bin(old_energy_eV); const int new_bin = energy_binning_.bin(new_energy_eV); return !(old_bin == new_bin); } -bool DynamicDensityField::changedPitchAngleBin( - double old_mu, - double new_mu) const { +bool DynamicDensityField::changedPitchAngleBin(double old_mu, + double new_mu) const { const int old_bin = pitch_angle_binning_.bin(old_mu); const int new_bin = pitch_angle_binning_.bin(new_mu); @@ -198,11 +196,10 @@ bool DynamicDensityField::changedPitchAngleBin( return !(old_bin == new_bin); } -void DynamicDensityField::logMovement( - const Coordinate& position, - double energy_eV, - double pitch_angle, - double time_in_bin_s) { +void DynamicDensityField::logMovement(const Coordinate& position, + double energy_eV, + double pitch_angle, + double time_in_bin_s) { const blitz::TinyVector position_bin = position_binning_.bin(position.cylinderEntries()); @@ -213,10 +210,9 @@ void DynamicDensityField::logMovement( pitch_angle_bin, energy_bin) += time_in_bin_s; } -void DynamicDensityField::logDeletion( - const Coordinate& position, - double energy_eV, - double pitch_angle) { +void DynamicDensityField::logDeletion(const Coordinate& position, + double energy_eV, + double pitch_angle) { const blitz::TinyVector position_bin = position_binning_.bin(position.cylinderEntries()); @@ -317,7 +313,8 @@ blitz::Array DynamicDensityField:: position_binning_.bin(position.cylinderEntries()); return density_per_m3_(position_bin(0), position_bin(1), position_bin(2), - blitz::Range(0, energy_binning_.binIndexMax()-1)).copy(); + blitz::Range(energy_binning_.binIndexMin(), + energy_binning_.binIndexMax()-1)).copy(); } blitz::Array DynamicDensityField:: diff --git a/src/simulation/event_position_distribution.cpp b/src/simulation/event_position_distribution.cpp index 7c0c136..eeca600 100755 --- a/src/simulation/event_position_distribution.cpp +++ b/src/simulation/event_position_distribution.cpp @@ -84,5 +84,6 @@ CylinderCoordinate PositionDistributionFromDensity::drawPosition(std::mt19937* e } while (random_n*maximum_density_ >= density_->density(CylinderCoordinate( blitz::TinyVector(random_r, random_phi, random_z)))); - return CylinderCoordinate(blitz::TinyVector(random_r, random_phi, random_z)); + return CylinderCoordinate( + blitz::TinyVector(random_r, random_phi, random_z)); } \ No newline at end of file diff --git a/src/simulation/mover.cpp b/src/simulation/mover.cpp index 0023c8c..64d23e0 100755 --- a/src/simulation/mover.cpp +++ b/src/simulation/mover.cpp @@ -31,6 +31,14 @@ void BorisMover::move(particle::Particle* particle, // 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_distance = mfp_m; auto old_position = particle->position(); @@ -54,12 +62,12 @@ void BorisMover::move(particle::Particle* particle, 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; - } +// 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; @@ -77,6 +85,7 @@ void BorisMover::move(particle::Particle* particle, timestep = std::min(maximal_time_step_s_, (leftover_distance + SimConstants::random_lower_limit) / old_velocity); timestep = std::min(timestep, max_distance / old_velocity); + timestep = std::min(timestep, max_time_alive); double moved_distance = 0.; @@ -127,6 +136,14 @@ void BorisMover::move(particle::Particle* particle, particle->specie()->terminator()->reachedMaxTimeAlive(particle); leftover_distance -= moved_distance; + + // 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); } particle->specie()->dynamicDensityField()->logMovement( @@ -386,7 +403,7 @@ 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 + // 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 diff --git a/src/simulation/simulation.cpp b/src/simulation/simulation.cpp index 62a6ea1..d8e07d3 100755 --- a/src/simulation/simulation.cpp +++ b/src/simulation/simulation.cpp @@ -138,9 +138,8 @@ void Simulation::start() { // calculate a 'stride' for the density events s.t. the same number of syncs // would be performed - const int density_stride = std::ceil(static_cast( - density_events * event_stride)/beta_decay_events); - + const int density_stride = std::ceil(density_events * + (static_cast(event_stride)/beta_decay_events)); // calculate the scaling factor for the particle weight for particles // drawn from the density const double density_weight_factor = diff --git a/src/simulation/velocity_distribution.cpp b/src/simulation/velocity_distribution.cpp index c2f8f2a..12e53b8 100755 --- a/src/simulation/velocity_distribution.cpp +++ b/src/simulation/velocity_distribution.cpp @@ -35,7 +35,10 @@ VelocityDistributionFromDensityField::VelocityDistributionFromDensityField( energyBinBorders_(density_field->energyBinning()->binBorders()), pitchAngleBinBorders_(density_field->pitchAngleBinning()->binBorders()), theta_distribution_(std::uniform_real_distribution(0.,1.)), - mass_kg_(mass_kg) {} + mass_kg_(mass_kg) { + // necessary to include the underflow bin in the energy domain + energyBinBorders_.insert(energyBinBorders_.begin(), 0); + } // not important as of now since it's meant for drawing a velocity from the // pitch angle distribution and not to be used in an interaction context in which