diff --git a/include/generators/species_generator.h b/include/generators/species_generator.h index 6c2090e..e28c3de 100755 --- a/include/generators/species_generator.h +++ b/include/generators/species_generator.h @@ -24,7 +24,6 @@ struct TrackedSpeciesBuilderInfo { std::shared_ptr velocity_distribution; std::shared_ptr drift_velocity; std::shared_ptr last_sync_density_field; - std::shared_ptr density_drawing_field; double mass_kg; double charge_C; diff --git a/include/poisson_solver/poisson_solver.h b/include/poisson_solver/poisson_solver.h index 3db2dee..90071e5 100755 --- a/include/poisson_solver/poisson_solver.h +++ b/include/poisson_solver/poisson_solver.h @@ -57,8 +57,7 @@ class PoissonSolver { int num_cycles, bool do_solver); - void solvePotential(const blitz::Array charge_density, - const bool solve); + void solvePotential(const blitz::Array charge_density); blitz::Array potential(); std::shared_ptr staticElectricField(); const CylinderBinning* densityBinning(); diff --git a/include/simulation/cpu_synchronizer.h b/include/simulation/cpu_synchronizer.h index 0fabda5..b8e004b 100755 --- a/include/simulation/cpu_synchronizer.h +++ b/include/simulation/cpu_synchronizer.h @@ -21,11 +21,9 @@ class CpuSynchronizer { void synchronize( std::map>& - tracked_species, bool reset); + tracked_species); - void synchronize(DynamicDensityField* field, - DynamicDensityField* density_for_drawing, - bool reset); + void synchronize(DynamicDensityField* field); void synchronize(CurrentCollector* collector); void synchronize(DeletionLogger* logger); void synchronize(Timer* timer, Timer* last_sync_timer); @@ -36,8 +34,6 @@ class CpuSynchronizer { */ void sumToAll(blitz::Array& array, blitz::Array& last_sync_array); - void sumToAll(blitz::Array& array, - blitz::Array& last_sync_array); void sumToAll(blitz::Array& array, blitz::Array& last_sync_array); void sumToAll(blitz::Array& array, @@ -46,6 +42,9 @@ class CpuSynchronizer { blitz::Array& last_sync_array); void sumToAll(double& value, double& last_sync_value); void sumToAll(long int& value, long int& last_sync_value); + +// array has to be the delta of the timestep + void sumToAll(blitz::Array& array); }; #endif /* CPU_SYNCHRONIZER_H */ diff --git a/include/simulation/species.h b/include/simulation/species.h index e4a1ec7..9dffafa 100755 --- a/include/simulation/species.h +++ b/include/simulation/species.h @@ -64,8 +64,7 @@ class TrackedSpecie : public Specie { std::shared_ptr reflector, std::shared_ptr density_field, std::shared_ptr velocity_distribution, - std::shared_ptr drift_velocity, - std::shared_ptr density_for_drawing); + std::shared_ptr drift_velocity); DensityField* densityField(); VelocityDistribution* velocityDistribution(); @@ -79,8 +78,6 @@ class TrackedSpecie : public Specie { DeletionLogger* deletionLogger(); void deletionLogger(std::shared_ptr new_logger); - DynamicDensityField* densityForDrawing(); - CurrentCollector* currentCollector(); void currentCollector(std::shared_ptr new_collector); @@ -102,7 +99,6 @@ class TrackedSpecie : public Specie { std::shared_ptr current_collector_; std::vector> interactions_; - std::shared_ptr density_for_drawing_; // necessary for interactions between TrackedSpecies std::shared_ptr velocity_distribution_from_density_; diff --git a/src/IO/restart.cpp b/src/IO/restart.cpp index cad6969..e770794 100644 --- a/src/IO/restart.cpp +++ b/src/IO/restart.cpp @@ -57,7 +57,7 @@ void Restart::restart( for(const auto& pair: species) { std::string path = pair.second->name() + "/DensityField/SimulationData/" + restart_identifier_; - restartDrawingDensityField(pair.second->densityForDrawing(), path); + restartDrawingDensityField(pair.second->dynamicDensityField(), path); // pair.second->positionDistributionFromDensity()->setMaximumDensity(); } diff --git a/src/generators/species_generator.cpp b/src/generators/species_generator.cpp index 7b7b7f6..3d3f7f8 100755 --- a/src/generators/species_generator.cpp +++ b/src/generators/species_generator.cpp @@ -296,13 +296,6 @@ void SpeciesBuilder::fillTrackedDensity(Parameter& parameter) { json_path_append(b.second.path_in_json, {"DensityField", "DynamicDensityField"}), timer_); - -// not the optimal solution! (has to be optimized) - b.second.density_drawing_field = buildDynamicDensityField( - parameter, - json_path_append(b.second.path_in_json, - {"DensityField", "DynamicDensityField"}), - timer_); } } @@ -324,7 +317,7 @@ void SpeciesBuilder::buildSpecies() { b.second.type, b.second.name, b.second.mass_kg, b.second.charge_C, b.second.mover, b.second.terminator, b.second.reflector, b.second.density_field, b.second.velocity_distribution, - b.second.drift_velocity, b.second.density_drawing_field)); + b.second.drift_velocity)); } } diff --git a/src/interactions/recombination.cpp b/src/interactions/recombination.cpp index de6eadd..416f95d 100755 --- a/src/interactions/recombination.cpp +++ b/src/interactions/recombination.cpp @@ -172,7 +172,7 @@ double Recombination::drawCollisionTime(particle::Particle& particle, // const double density = target_->densityField()->density(particle.cylinderPosition()); // Use the density for drawing field, since it contains the density of the earlier // timestep - const double density = recombination_target_->densityForDrawing() + const double density = recombination_target_->dynamicDensityField() ->density(particle.cylinderPosition()); // recombination_target_->densityForDrawing()->recalculateDensityArray(); @@ -223,10 +223,10 @@ double Recombination::drawCollisionTime(particle::Particle& particle, if(density > 0 && std::isnan(crosssection)) { std::stringstream msg; msg << particle.specie()->name() << particle.cartesianPosition().cartesianEntries(); - msg << " " << recombination_target_->densityForDrawing()->positionBinning()->bin(particle.cylinderPosition().cylinderEntries()); + msg << " " << recombination_target_->dynamicDensityField()->positionBinning()->bin(particle.cylinderPosition().cylinderEntries()); msg << ": Recombination: density is not nan but the cross section is "; msg << density << ", " << crosssection; - msg << recombination_target_->densityForDrawing()->sliceSpectrumArray(particle.cylinderPosition()); + msg << recombination_target_->dynamicDensityField()->sliceSpectrumArray(particle.cylinderPosition()); ERROR(msg.str()); } #endif diff --git a/src/poisson_solver/poisson_solver.cpp b/src/poisson_solver/poisson_solver.cpp index f9c7586..2456bf7 100755 --- a/src/poisson_solver/poisson_solver.cpp +++ b/src/poisson_solver/poisson_solver.cpp @@ -77,12 +77,12 @@ PoissonSolver::PoissonSolver(GlobalBoundary bottom_boundary, blitz::Range(density_binning.binIndexMin()(2), density_binning.binIndexMax()(2))); zero_chargedensity = 0.; - solvePotential(zero_chargedensity, true); + solvePotential(zero_chargedensity); } void PoissonSolver::solvePotential( - const blitz::Array charge_density, const bool solve) { - if (!(do_solver_) || !(solve)) { + const blitz::Array charge_density) { + if (!(do_solver_)) { return; } #ifdef debug_level3 diff --git a/src/simulation/cpu_synchronizer.cpp b/src/simulation/cpu_synchronizer.cpp index d9337b0..5522078 100755 --- a/src/simulation/cpu_synchronizer.cpp +++ b/src/simulation/cpu_synchronizer.cpp @@ -17,14 +17,10 @@ void CpuSynchronizer::sumToAll(blitz::Array& array, last_sync_array += delta; } -void CpuSynchronizer::sumToAll(blitz::Array& array, - blitz::Array& last_sync_array) { +void CpuSynchronizer::sumToAll(blitz::Array& array) { int size = array.size(); - blitz::Array delta(array.extent()); - delta = array - last_sync_array; - MPI_Allreduce(MPI_IN_PLACE, delta.data(), size, MPI_DOUBLE, + MPI_Allreduce(MPI_IN_PLACE, array.data(), size, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); - last_sync_array += delta; } void CpuSynchronizer::sumToAll(blitz::Array& array, @@ -84,19 +80,13 @@ void CpuSynchronizer::synchronize(Timer* timer, Timer* last_sync_timer) { last_sync_timer->setTime(last_sync_time_s); } -void CpuSynchronizer::synchronize(DynamicDensityField* field, - DynamicDensityField* density_for_drawing, - bool reset) { +// synchronize density field across cpus and set the +// array storing the previous timestep +void CpuSynchronizer::synchronize(DynamicDensityField* field) { if (field) { - sumToAll(field->time_in_bin_s_, field->sync_time_in_bin_s_); + sumToAll(field->sync_time_in_bin_s_); field->time_in_bin_s_ = field->sync_time_in_bin_s_; - - // has to be moved somewhere else i think - if (reset) { - field->recalculateDensityArray(); - density_for_drawing->time_in_bin_s_ = field->time_in_bin_s_; - density_for_drawing->density_per_m3_ = field->density_per_m3_; - } + field->recalculateDensityArray(); } } @@ -159,13 +149,12 @@ void CpuSynchronizer::synchronize(DeletionLogger* logger) { void CpuSynchronizer::synchronize( std::map>& - tracked_species, bool reset) { + tracked_species) { MPI_Barrier(MPI_COMM_WORLD); auto it = tracked_species.begin(); for (auto& pair : tracked_species) { - synchronize(pair.second->dynamicDensityField(), - pair.second->densityForDrawing(), reset); + synchronize(pair.second->dynamicDensityField()); synchronize(pair.second->currentCollector()); synchronize(pair.second->deletionLogger()); } diff --git a/src/simulation/density_field.cpp b/src/simulation/density_field.cpp index ff1aabf..66e218e 100755 --- a/src/simulation/density_field.cpp +++ b/src/simulation/density_field.cpp @@ -199,7 +199,7 @@ void DynamicDensityField::logMovement(const Coordinate& position, const int energy_bin = energy_binning_.bin(energy_eV); const int pitch_angle_bin = pitch_angle_binning_.bin(pitch_angle); - time_in_bin_s_(position_bin(0), position_bin(1), position_bin(2), + sync_time_in_bin_s_(position_bin(0), position_bin(1), position_bin(2), pitch_angle_bin, energy_bin) += weight; } @@ -308,10 +308,8 @@ blitz::Array DynamicDensityField::sliceTimeInBinArray( energy_bin); } +// resets the field that logs the new density void DynamicDensityField::resetDynamicDensityField() { - //timer_->resetTime(); // time stays global - time_in_bin_s_ = 0.; - density_per_m3_ = 0.; sync_time_in_bin_s_ = 0.; } diff --git a/src/simulation/simulation.cpp b/src/simulation/simulation.cpp index 2f7506e..1cfc927 100755 --- a/src/simulation/simulation.cpp +++ b/src/simulation/simulation.cpp @@ -139,10 +139,6 @@ void Simulation::finalize() { void Simulation::start() { SIM_INFO("Starting Simulation."); -// stride wrt the number of beta decay events per CPU - const int event_stride = - parameter_.intValue(json_path({"Simulation", "EventStride"}), 1); - // number of timesteps to be simulated const int number_timesteps = parameter_.intValue(json_path({"Simulation", "NumberTimesteps"}), 1); @@ -161,18 +157,13 @@ void Simulation::start() { injector_->setWeights(time_resolution_events, beta_decay_events); SIM_INFO("Number of Timesteps: " + std::to_string(number_timesteps)); - SIM_INFO("Simulated events before cpu sync: " + std::to_string(event_stride)); - - int leftover_events_time_resolution = beta_decay_events; + SIM_INFO("Number of injected particles per timestep: " + std::to_string(beta_decay_events)); const double delta_t = time_resolution_events*injector_->timePassed(); timer_->setDeltaT(delta_t); injector_->initInjectionTimeDistribution(delta_t); - int num_cpu_syncs = 0; - bool reset = true; - timer_->increaseTimestep(delta_t); while (timestep < number_timesteps) { @@ -181,10 +172,6 @@ void Simulation::start() { out << "Timeinterval: " << timer_->time() << "s."; DEBUG_LEVEL_3(out.str()); #endif -// calculate the number of beta events to be simulated before a -// cpu sync is done - int events_of_cycle = - eventsOfCycle(event_stride, leftover_events_time_resolution); #ifdef debug_level1 DEBUG_LEVEL_1("Events of cycle: " + std::to_string(events_of_cycle)); @@ -194,14 +181,10 @@ void Simulation::start() { if(timestep%(std::max(number_timesteps/100, 1)) == 0) SIM_INFO("Processing timestep: " + std::to_string(timestep)); -#ifdef debug - SIM_INFO("Leftover events this timestep this CPU: " + - std::to_string(leftover_events_time_resolution)); -#endif // process both density and beta events before a cpu synchronization processDensityEvents(); - processEvents(events_of_cycle); + processEvents(beta_decay_events); #if defined(debug_level1) || defined(debug_level2) || defined(debug_level3) DEBUG_LEVEL_1("Processing timestep: " + std::to_string(timestep)); @@ -209,10 +192,6 @@ void Simulation::start() { std::to_string(leftover_events_time_resolution)); #endif - leftover_events_time_resolution -= events_of_cycle; - - reset = !(leftover_events_time_resolution > 0); - #ifdef debug SIM_INFO("Cpu sync."); #endif @@ -221,58 +200,53 @@ void Simulation::start() { // otherwise it would be more difficult to set it... (cpu synchronizer // is a friend class of the density field, i.e. has access to the // private members) - cpu_synchronizer_->synchronize(tracked_species_, reset); - num_cpu_syncs += 1; + cpu_synchronizer_->synchronize(tracked_species_); + timer_->increaseTimestep(delta_t); + timestep++; SimulationTimer::eventStopPoint(std::string("CpuSync")); -// can probably be moved into the reset section and therefore -// the parameter 'reset' could probably be removed #ifdef debug SIM_INFO("Poisson step."); #endif SimulationTimer::eventStartPoint(std::string("PoissonStep")); -// only solves the potential anew if 'reset' is true - poisson_solver_->solvePotential(chargedensity(), reset); + + poisson_solver_->solvePotential(chargedensity()); electric_field_->updateDynamicField(poisson_solver_->staticElectricField()); + SimulationTimer::eventStopPoint(std::string("PoissonStep")); #ifdef debug SIM_INFO("Perform output."); #endif SimulationTimer::eventStartPoint(std::string("Output")); - // only write inactive queue, obvi not correct if written between timesteps - output_->writeSimulationData(num_cpu_syncs, tracked_species_, poisson_solver_.get(), + + output_->writeSimulationData(timestep, tracked_species_, poisson_solver_.get(), random_->engine(), storage_queue_[inactive_storage], - timestep + 1 + injectionGroupOffset); + timestep + injectionGroupOffset); + SimulationTimer::eventStopPoint(std::string("Output")); SimulationTimer::eventStartPoint("Reset"); - if(reset) { #if defined(debug_level2) || defined(debug_level3) - DEBUG("Number of particles in active storage queue: " - + std::to_string(storage_queue_[active_storage].size())); - DEBUG("Number of particles in inactive storage queue: " - + std::to_string(storage_queue_[inactive_storage].size())); + DEBUG("Number of particles in active storage queue: " + + std::to_string(storage_queue_[active_storage].size())); + DEBUG("Number of particles in inactive storage queue: " + + std::to_string(storage_queue_[inactive_storage].size())); #endif #ifdef debug - SIM_INFO("Do resets and increase the timestep for timeresolution."); + SIM_INFO("Do resets and increase the timestep for timeresolution."); #endif - active_storage = (active_storage + 1) % 2; - inactive_storage = (inactive_storage + 1) % 2; + active_storage = (active_storage + 1) % 2; + inactive_storage = (inactive_storage + 1) % 2; - for(auto& pair : tracked_species_) { - pair.second->dynamicDensityField()->resetDynamicDensityField(); + for(auto& pair : tracked_species_) { + pair.second->dynamicDensityField()->resetDynamicDensityField(); - pair.second->currentCollector()->resetCurrentCollector(); + pair.second->currentCollector()->resetCurrentCollector(); - pair.second->deletionLogger()->resetDeletionLogger(); - } - - leftover_events_time_resolution = beta_decay_events; - - timer_->increaseTimestep(delta_t); - timestep++; + pair.second->deletionLogger()->resetDeletionLogger(); } + SimulationTimer::eventStopPoint("Reset"); } } @@ -335,13 +309,8 @@ void Simulation::processEvents(int events) { DEBUG_LEVEL_2("Particles in queue: " + std::to_string(queue_.size())); #endif - const int particle_number = - particleNumberFromQueue(queue_.size(), random_.get()); - - // extract particle from queue here, so that an extension of the queue does - // not mess up the reference - particle::Particle particle = queue_.at(particle_number); - queue_.erase(queue_.begin() + particle_number); + particle::Particle particle = queue_.back(); + queue_.pop_back(); processUntilTermination(particle); } @@ -587,9 +556,10 @@ void Simulation::collisionTime(particle::Particle& particle, RandomNumbers* rand particle.indexNextInteration((min_iterator - collision_times.begin())); particle.timeUntilCollision(*min_iterator); -#ifdef debug_level3 +#ifdef debug std::stringstream out; - out << "collision times:\n[ "; + out << "collision times "; + out << particle.specie()->name() <<":\n[ "; for(auto& element : collision_times) { out << element << " "; @@ -598,7 +568,7 @@ void Simulation::collisionTime(particle::Particle& particle, RandomNumbers* rand out << "\nmin value: " << *min_iterator; out << "\nindexNextInteraction: " << particle.indexNextInteraction(); - DEBUG_LEVEL_3(out.str()); + DEBUG(out.str()); #endif } diff --git a/src/simulation/species.cpp b/src/simulation/species.cpp index b343458..2977bcc 100755 --- a/src/simulation/species.cpp +++ b/src/simulation/species.cpp @@ -43,8 +43,7 @@ TrackedSpecie::TrackedSpecie( std::shared_ptr reflector, std::shared_ptr density_field, std::shared_ptr velocity_distribution, - std::shared_ptr drift_velocity, - std::shared_ptr density_for_drawing) + std::shared_ptr drift_velocity) : Specie(type, name, mass_kg, charge_C), mover_(mover), terminator_(terminator), @@ -53,16 +52,15 @@ TrackedSpecie::TrackedSpecie( deletion_logger_(std::shared_ptr(nullptr)), current_collector_(std::shared_ptr(nullptr)), drift_velocity_(drift_velocity), - density_for_drawing_(density_for_drawing), velocity_distribution_from_density_( std::make_shared( - density_for_drawing, mass_kg)) {} + density_field, mass_kg)) {} DensityField* TrackedSpecie::densityField() { // return the density field storing the previous timestep, since // that density is the only one that makes sense for the calculation // of e.g. MFP for Tracked-Tracked interactions - return density_for_drawing_.get(); + return density_field_.get(); } VelocityDistribution* TrackedSpecie::velocityDistribution() { @@ -80,10 +78,6 @@ DynamicDensityField* TrackedSpecie::dynamicDensityField() { return density_field_.get(); } -DynamicDensityField* TrackedSpecie::densityForDrawing() { - return density_for_drawing_.get(); -} - Mover* TrackedSpecie::mover() { return mover_.get(); }