From 324da20d4ab4e2a680097069aaf93e2fe3f7ca3c Mon Sep 17 00:00:00 2001 From: csendlinger Date: Fri, 20 Feb 2026 11:39:47 +0100 Subject: [PATCH] Added an energy resolved current at z = 0; hardcoded detection region --- include/IO/simulation_output.h | 5 ++ include/simulation/current_collector.h | 28 ++++++--- makefile | 6 ++ src/IO/simulation_output.cpp | 27 ++++++++- src/simulation/cpu_synchronizer.cpp | 5 ++ src/simulation/current_collector.cpp | 79 ++++++++++++++++++++------ src/simulation/simulation.cpp | 29 ++++++++-- 7 files changed, 148 insertions(+), 31 deletions(-) diff --git a/include/IO/simulation_output.h b/include/IO/simulation_output.h index 45b4bea..78f4169 100755 --- a/include/IO/simulation_output.h +++ b/include/IO/simulation_output.h @@ -52,6 +52,11 @@ class SimulationOutput { std::vector& storage_queue, int injectionGroupOffset); +#ifdef GENRICHCURRENT + void setResetCurrentCollector(int cpu_sync, + std::map>& species); +#endif + private: std::vector tasks_density_fields_; std::vector tasks_current_collector_; diff --git a/include/simulation/current_collector.h b/include/simulation/current_collector.h index 81426d4..8379fb1 100755 --- a/include/simulation/current_collector.h +++ b/include/simulation/current_collector.h @@ -29,6 +29,14 @@ class CurrentCollector { blitz::Array areaInZ() const; blitz::Array currentInZ() const; +#ifdef GENRICHCURRENT + blitz::Array sampleNegativeCrossings() const; + std::vector energyBinBorders() const; + void needsReset() { needs_reset = true; }; + bool getNeedsReset() const { return needs_reset; }; + double timeSinceReset() const {return time_since_reset;}; +#endif //GENRICHCURRENT + double physicalTime() const; void resetCurrentCollector(); @@ -41,6 +49,9 @@ class CurrentCollector { const double weight); void collectLongitudinalMovement(const blitz::TinyVector& bin_old, const blitz::TinyVector& bin_new, +#ifdef GENRICHCURRENT + const double energy_eV, +#endif const double weight); void collectAzimuthalMovement(const blitz::TinyVector& old_pos_cart, const blitz::TinyVector& new_pos_cart, @@ -71,19 +82,18 @@ class CurrentCollector { blitz::Array r_negative_crossings; blitz::Array r_positive_crossings; -// blitz::Array sync_z_negative_crossings; -// blitz::Array sync_z_positive_crossings; - -// blitz::Array sync_phi_negative_crossings; -// blitz::Array sync_phi_positive_crossings; - -// blitz::Array sync_r_negative_crossings; -// blitz::Array sync_r_positive_crossings; - blitz::Array collector_areas_r; blitz::Array collector_areas_phi; blitz::Array collector_areas_z; +#ifdef GENRICHCURRENT + blitz::Array sample_z_negative_crossings; + EnergyBinning energy_binning_; + + bool needs_reset = false; + double time_since_reset = 0; +#endif //GENRICHCURRENT + CylinderBinning position_binning_; std::shared_ptr timer_; }; diff --git a/makefile b/makefile index 3c1d61c..33023d6 100755 --- a/makefile +++ b/makefile @@ -35,6 +35,8 @@ LIBS =#-static CXXFLAGS += -DHOMEBASE=$(HOMEBASE) +#CXXFLAGS += -Ddebug_timedist + # Blitz CXXFLAGS += $(BLITZ_INCLUDES) LIBS += $(BLITZ_LIBS) @@ -55,6 +57,10 @@ ifeq ($(DO_DEBUGGING),true) CXXFLAGS += -Ddebug_level$(DEBUG_LEVEL) endif +ifeq ($(DO_GENRICHCURRENT),true) + CXXFLAGS += -DGENRICHCURRENT #-Ddebug_genrich +endif + # Catch2 CXXFLAGS += $(CATCH_INCLUDES) diff --git a/src/IO/simulation_output.cpp b/src/IO/simulation_output.cpp index c20bc29..759f3ac 100755 --- a/src/IO/simulation_output.cpp +++ b/src/IO/simulation_output.cpp @@ -81,7 +81,6 @@ void SimulationOutput::writeSimulationData( std::vector& storage_queue, int injectionGroupOffset) { if (MpiCommunication::mpi_rank == 0) { - #ifdef debug_level2 DEBUG_LEVEL_2("Write density field data."); #endif @@ -294,6 +293,11 @@ void SimulationOutput::writeCurrentCrossings( path + "/PositiveCrossingsInZ"); writer.write(current_collector->negativeCrossingsInZ(), path + "/NegativeCrossingsInZ"); + +#ifdef GENRICHCURRENT + writer.write(current_collector->sampleNegativeCrossings(), + path + "/SampleNegativeCrossings"); +#endif } void SimulationOutput::writeCurrent(CurrentCollector* current_collector, @@ -306,6 +310,10 @@ void SimulationOutput::writeCurrent(CurrentCollector* current_collector, void SimulationOutput::writePhysicalTime(CurrentCollector* current_collector, std::string path) { writer.write(current_collector->physicalTime(), path + "/PhysicalTime"); +#ifdef GENRICHCURRENT +// write the time over which the "sample" current has been accumulated during + writer.write(current_collector->timeSinceReset(), path + "/TimeSinceReset"); +#endif } void SimulationOutput::writeSpectrum(DynamicDensityField* density_field, @@ -428,6 +436,10 @@ void SimulationOutput::writeCollectorAreas(CurrentCollector* current_collector, writer.write(current_collector->areaInR(), path + "/AreaInR"); writer.write(current_collector->areaInPhi(), path + "/AreaInPhi"); writer.write(current_collector->areaInZ(), path + "/AreaInZ"); + +#ifdef GENRICHCURRENT + writer.write(current_collector->energyBinBorders(), path + "/EnergyBinBorders"); +#endif } void SimulationOutput::writeBinVolumes(DynamicDensityField* density_field, @@ -468,4 +480,15 @@ void SimulationOutput::writeParticles( int cpu_sync, double dt) { particle_writer.output_particles(particles, cpu_sync, dt); -} \ No newline at end of file +} + + +#ifdef GENRICHCURRENT +void SimulationOutput::setResetCurrentCollector(int cpu_sync, + std::map>& species) { + for(auto& task : tasks_current_collector_) { + if(cpu_sync % task.intervall == 0) + species.at(task.specie_name)->currentCollector()->needsReset(); + } +} +#endif \ No newline at end of file diff --git a/src/simulation/cpu_synchronizer.cpp b/src/simulation/cpu_synchronizer.cpp index e2a1ab0..285d891 100755 --- a/src/simulation/cpu_synchronizer.cpp +++ b/src/simulation/cpu_synchronizer.cpp @@ -157,6 +157,11 @@ void CpuSynchronizer::synchronize(CurrentCollector* collector) { // collector->r_positive_crossings = // collector->sync_r_positive_crossings; sumToRoot(collector->r_positive_crossings); + +#ifdef GENRICHCURRENT + if(collector->getNeedsReset()) + sumToRoot(collector->sample_z_negative_crossings); +#endif //GENRICHCURRENT } } diff --git a/src/simulation/current_collector.cpp b/src/simulation/current_collector.cpp index d4e9f98..286063e 100755 --- a/src/simulation/current_collector.cpp +++ b/src/simulation/current_collector.cpp @@ -12,14 +12,20 @@ CurrentCollector::CurrentCollector(const CylinderBinning& position_binning, collector_areas_r(createAreaRadial(position_binning)), phi_negative_crossings(createCrossingsAzimuthal(position_binning)), phi_positive_crossings(createCrossingsAzimuthal(position_binning)), - collector_areas_phi(createAreaAzimuthal(position_binning))//, - // sync_z_negative_crossings(createCrossingsLongitudinal(position_binning)), - // sync_z_positive_crossings(createCrossingsLongitudinal(position_binning)), - // sync_r_negative_crossings(createCrossingsRadial(position_binning)), - // sync_r_positive_crossings(createCrossingsRadial(position_binning)), - // sync_phi_negative_crossings(createCrossingsAzimuthal(position_binning)), - // sync_phi_positive_crossings(createCrossingsAzimuthal(position_binning)) - {} + collector_areas_phi(createAreaAzimuthal(position_binning)) +#ifdef GENRICHCURRENT +// Hard coded values, not ideal, but it didn't work using defines... + , energy_binning_(0.1, 5, 100) + , sample_z_negative_crossings( + blitz::Range(position_binning.binIndexMin()(0), + position_binning.binIndexMax()(0)), + blitz::Range(-1, 100)) +#endif //GENRICHCURRENT + { +#ifdef GENRICHCURRENT + sample_z_negative_crossings = 0; +#endif + } blitz::Array CurrentCollector::createAreaLongitudinal( const CylinderBinning& position_binning) { @@ -138,8 +144,24 @@ void CurrentCollector::collect(particle::Particle* particle, const double weight = particle->weight(); +#ifdef debug_genrich + if(particle->specie()->type() == species::ImplementedSpecies::Tplus && + bin_new(1) == -1) { + std::stringstream out; + out << "Energy " << particle->kineticEnergy_eV() << " \n\t"; + out << "Bin old " << bin_old << "; Bin new " << bin_new; + out << "; energy bin " << energy_binning_.bin(particle->kineticEnergy_eV()); + out << "\n"; + std::cout << out.str(); + } +#endif + collectRadialMovement(bin_old, bin_new, weight); - collectLongitudinalMovement(bin_old, bin_new, weight); + collectLongitudinalMovement(bin_old, bin_new, +#ifdef GENRICHCURRENT + particle->kineticEnergy_eV(), +#endif + weight); collectAzimuthalMovement(old_position.cartesianEntries(), particle->cartesianPosition().cartesianEntries(), bin_old, weight); @@ -158,9 +180,21 @@ void CurrentCollector::collectRadialMovement( void CurrentCollector::collectLongitudinalMovement( const blitz::TinyVector& bin_old, const blitz::TinyVector& bin_new, +#ifdef GENRICHCURRENT + const double energy_eV, +#endif const double weight) { if (bin_new(1) < bin_old(1)) { z_negative_crossings(bin_old(0), bin_new(1) + 1) += weight; +#ifdef GENRICHCURRENT + if(bin_new(1) == -1) { + int ebin = energy_binning_.bin(energy_eV); +// #ifdef debug_genrich +// // fprintf(stdout, "Energybin: %d, energy: %f\n", ebin, energy_eV); +// #endif + sample_z_negative_crossings(bin_old(0), ebin) += weight; + } +#endif } else if (bin_new(1) > bin_old(1)) { z_positive_crossings(bin_old(0), bin_new(1)) += weight; } @@ -257,12 +291,25 @@ void CurrentCollector::resetCurrentCollector() { r_negative_crossings = 0.; r_positive_crossings = 0.; - // sync_z_negative_crossings = 0.; - // sync_z_positive_crossings = 0.; +#ifdef GENRICHCURRENT + if(needs_reset) { + sample_z_negative_crossings = 0.; + needs_reset = false; + time_since_reset = timer_->getDeltaT(); + } else { + time_since_reset += timer_->getDeltaT(); + } +#endif +} - // sync_phi_negative_crossings = 0.; - // sync_phi_positive_crossings = 0.; +#ifdef GENRICHCURRENT - // sync_r_negative_crossings = 0.; - // sync_r_positive_crossings = 0.; -} \ No newline at end of file +blitz::Array CurrentCollector::sampleNegativeCrossings() const { + return sample_z_negative_crossings; +} + +std::vector CurrentCollector::energyBinBorders() const { + return energy_binning_.binBorders(); +} + +#endif \ No newline at end of file diff --git a/src/simulation/simulation.cpp b/src/simulation/simulation.cpp index 09a612a..82efb2b 100755 --- a/src/simulation/simulation.cpp +++ b/src/simulation/simulation.cpp @@ -38,6 +38,11 @@ #include "tools/simulation_time.h" +// #ifdef debug_timedist +// #include +// #include +// #endif + Simulation::Simulation(std::string parameter_filename) : parameter_(parameter_filename) { init(parameter_); @@ -192,6 +197,10 @@ void Simulation::start() { // std::to_string(leftover_events_time_resolution)); #endif +#ifdef GENRICHCURRENT + output_->setResetCurrentCollector(timestep + 1, tracked_species_); +#endif + #ifdef debug SIM_INFO("Cpu sync."); #endif @@ -404,8 +413,7 @@ void Simulation::processUntilTermination(particle::Particle& particle) { SimulationTimer::eventStopPoint(std::string("Mover")); bool was_reflected = false; - if (particle.specie()->reflector()->needsReflectionBackToDomain( - &particle)) { + if (particle.specie()->reflector()->needsReflectionBackToDomain(&particle)) { #ifdef debug_level3 DEBUG_LEVEL_3("Reflect particle."); #endif @@ -477,11 +485,11 @@ void Simulation::processUntilTermination(particle::Particle& particle) { break; } - if (!(particle.specie()->dynamicDensityField()->changedPositionBin( + if ((!(particle.specie()->dynamicDensityField()->changedPositionBin( old_position, particle.cylinderPosition())) && !(particle.specie()->dynamicDensityField()->changedEnergyBin( old_energy, particle.kineticEnergy_eV())) && - !was_reflected) { + !was_reflected) || particle.timeUntilCollision() <= 0.) { #ifdef debug_level3 DEBUG_LEVEL_3("Perform interaction."); DEBUG_LEVEL_3("Number of Interactions: " + @@ -581,6 +589,19 @@ void Simulation::collisionTime(particle::Particle& particle, RandomNumbers* rand interaction::InteractionOutput Simulation::interact( particle::Particle& particle, RandomNumbers* random) { +// #ifdef debug_timedist +// if(particle.kineticEnergy_eV() > 20 && particle.specie()->type() == species::ImplementedSpecies::electron) { +// std::ofstream file; +// std::stringstream filename; +// filename << MpiCommunication::mpi_rank << "_collisionTimeDistribution.dat"; +// file.open(filename.str(), std::ios_base::app); + +// file << std::setprecision(16) << particle.timeSinceCollision() << "\n"; +// file.close(); +// } +// particle.resetTimeSinceCollision(); +// #endif + return particle.specie()->interactions().at(particle.indexNextInteraction()) ->performInteraction(particle, random); }