diff --git a/include/generators/species_generator.h b/include/generators/species_generator.h index e2b534c..6c2090e 100755 --- a/include/generators/species_generator.h +++ b/include/generators/species_generator.h @@ -110,7 +110,6 @@ class SpeciesBuilder { void fillTerminator(Parameter& parameter); void fillReflector(Parameter& parameter); void fillTrackedDensity(Parameter& parameter); - void fillDensityDrawingType(Parameter& parameter); void initTimer(); void buildSpecies(); diff --git a/include/simulation/density_field.h b/include/simulation/density_field.h index 9dd7ac5..acfbca1 100755 --- a/include/simulation/density_field.h +++ b/include/simulation/density_field.h @@ -42,15 +42,6 @@ class DynamicDensityField : public DensityField { double pitch_angle, double weight); - // void logDeletion(const Coordinate& position, - // double energy_eV, - // double pitch_angle); - -// return the particle number stored in the density of this object - double particleNumber(); -// return the particle number at the given index - double particleNumber(blitz::TinyVector index); - // returns true if the old pitch angle and the new pitch angle are in different bins // of the pitch angle binning bool changedPitchAngleBin(double old_pitch_angle, double new_pitch_angle) const; @@ -58,7 +49,6 @@ class DynamicDensityField : public DensityField { blitz::Array densityArray(); blitz::Array spectrumArray(); blitz::Array timeInBinArray() const; - // blitz::Array deletedParticlesArray() const; blitz::Array binVolumeArray() const; // return an array of the density values at the Coordinate position (only energy @@ -84,7 +74,6 @@ class DynamicDensityField : public DensityField { void restartTimer(double time, double delta_t); void restartTimeInBin(blitz::Array time_in_bin); - // void restartDeletedParticlesArray(blitz::Array deleted_particles); private: @@ -95,10 +84,7 @@ class DynamicDensityField : public DensityField { // (not renamed, since that would require many, many changes due to // renaming of the functions) blitz::Array time_in_bin_s_; - // blitz::Array deleted_particles_; - blitz::Array sync_time_in_bin_s_; - // blitz::Array sync_deleted_particles_; blitz::Array density_per_m3_; blitz::Array bin_volume_m3_; diff --git a/include/simulation/event_position_distribution.h b/include/simulation/event_position_distribution.h index 85c35ec..100f10b 100755 --- a/include/simulation/event_position_distribution.h +++ b/include/simulation/event_position_distribution.h @@ -37,38 +37,4 @@ class EventPositionDistribution { std::uniform_real_distribution density_values; }; -// Class to draw a position from the density provided by a pointer -// to a DynamicDensityField object -class PositionDistributionFromDensity { - public: - // Constructor taking a pointer to a DynamicDensityField object - PositionDistributionFromDensity(std::shared_ptr density); - -// // draw a position from the density provided at construction using the rejection method -// CylinderCoordinate drawPosition(std::mt19937* engine); - -// draw a position uniformly distributed within the bin of the given index -// (note that the r-dependence of the particle number in a uniform density distribution -// is not incorporated) - CylinderCoordinate drawPosition(std::mt19937* engine, int r_idx, int phi_idx, - int z_idx); -// // sets the maximum density value needed for the rejection method -// // should not be called frequently, since during each call the density field is -// // recalculated from scratch by the DynamicDensityField object -// void setMaximumDensity(); - - private: -// variable storing the maximum value of the density - // double maximum_density_ = 1.; - - std::shared_ptr density_; - -// // distributions for the spatial directions and for the density - // std::uniform_real_distribution r_values; - // std::uniform_real_distribution phi_values; - // std::uniform_real_distribution z_values; -// std::uniform_real_distribution density_values; - std::uniform_real_distribution std_unifrm; -}; - #endif /* EVENT_POSITION_DISTRIBUTION_H */ diff --git a/include/simulation/injector.h b/include/simulation/injector.h index cb4246b..11ea5f7 100755 --- a/include/simulation/injector.h +++ b/include/simulation/injector.h @@ -48,19 +48,6 @@ class Injector { // numerical_number: number of numerical beta particles to be injected on // a single process void setWeights(const double physical_number, const int numerical_number); - -// draw new particles from the DynamicDensityField of the provided specie, -// the new particles are then appened to the vector 'queue' -// the magnetic field is necessary for the determination of the direction of the -// injected particles -// the weight_factor scales the time a particle contributes to a certain bin -// and the injection time specifies the time the particle is injected (has to be -// between T, T+\DeltaT) - void drawNewParticleFromDensityField(std::mt19937* engine, - std::shared_ptr specie, - std::vector& queue, - std::shared_ptr field, - const double weight_factor); void initInjectionTimeDistribution(double delta_t); diff --git a/include/simulation/simulation.h b/include/simulation/simulation.h index aafdb6b..008a17b 100755 --- a/include/simulation/simulation.h +++ b/include/simulation/simulation.h @@ -36,16 +36,10 @@ class Simulation { void finalize(); void processEvents(int events); - void processDensityEvents(int particles_per_bin, const double weight_factor); + void processDensityEvents(); void processUntilTermination(particle::Particle& particle); private: - std::vector determineInjectedDensityParticles( - RandomNumbers* random, - const double weight_factor, - const int current, - std::map& particleNumbers, - bool& all_bins_done); // draw the individual collision times of the possible interactions and return the // minimum of them. Also sets the "nextInteractionIndex" in the particle to the diff --git a/include/simulation/species.h b/include/simulation/species.h index 561eaac..e4a1ec7 100755 --- a/include/simulation/species.h +++ b/include/simulation/species.h @@ -59,7 +59,6 @@ class TrackedSpecie : public Specie { std::string name, double mass_kg, double charge_C, - bool explicitly_stored, std::shared_ptr mover, std::shared_ptr terminator, std::shared_ptr reflector, @@ -88,19 +87,11 @@ class TrackedSpecie : public Specie { std::vector> interactions(); void addInteraction(std::shared_ptr interaction); -// getter for the raw pointer to the positionDistributionFromDensity object - PositionDistributionFromDensity* positionDistributionFromDensity(); // getter for the raw pointer to the VelocityDistributionFromDensityField object VelocityDistributionFromDensityField* velocityDistributionFromDensityField(); -// getter for the raw pointer to the RandomIndex object - RandomIndex* randomIndex(); - - bool explicitlyStored(); - private: std::shared_ptr density_field_; - std::shared_ptr velocity_distribution_; std::shared_ptr drift_velocity_; std::shared_ptr mover_; @@ -112,17 +103,9 @@ class TrackedSpecie : public Specie { std::vector> interactions_; std::shared_ptr density_for_drawing_; - std::shared_ptr - position_distribution_from_density_; +// necessary for interactions between TrackedSpecies std::shared_ptr velocity_distribution_from_density_; - - std::shared_ptr random_index_; - - // should work if it changes from true -> false (after a restart) - // however, it should not work in the opposite case (in which the - // old density would be lost) - const bool explicitly_stored_; }; class BackgroundSpecie : public Specie { diff --git a/include/simulation/velocity_distribution.h b/include/simulation/velocity_distribution.h index 10ad82c..2f9fe40 100755 --- a/include/simulation/velocity_distribution.h +++ b/include/simulation/velocity_distribution.h @@ -56,19 +56,9 @@ class VelocityDistributionFromDensityField : public VelocityDistribution { const Coordinate& position, std::mt19937* engine, std::shared_ptr magnetic_field); -// return a velocity vector drawn uniformly in the bin specifed by index - CartesianVector drawVelocity(const blitz::TinyVector index, - std::mt19937* engine); - CartesianVector drawVelocity(const blitz::TinyVector index, - std::mt19937* engine, - const Coordinate& position, - std::shared_ptr magnetic_field); - private: // mass of the species of which the density field is a part of const double mass_kg_; - double alpha_lower_limit; - double alpha_upper_limit; std::shared_ptr density_field_; diff --git a/src/generators/species_generator.cpp b/src/generators/species_generator.cpp index 535433c..7b7b7f6 100755 --- a/src/generators/species_generator.cpp +++ b/src/generators/species_generator.cpp @@ -82,7 +82,6 @@ SpeciesBuilder::SpeciesBuilder(Parameter& parameter) { fillTerminator(parameter); fillReflector(parameter); fillTrackedDensity(parameter); - fillDensityDrawingType(parameter); buildSpecies(); fillInteractions(parameter); @@ -307,13 +306,6 @@ void SpeciesBuilder::fillTrackedDensity(Parameter& parameter) { } } -void SpeciesBuilder::fillDensityDrawingType(Parameter& parameter) { - for(auto& b : tracked_species_info) { - b.second.explicitly_stored = parameter.boolValue( - json_path_append(b.second.path_in_json, {"StoreInQueue"}), false); - } -} - void SpeciesBuilder::buildSpecies() { #ifdef debug_level2 DEBUG_LEVEL_2("Build species from info."); @@ -327,15 +319,9 @@ void SpeciesBuilder::buildSpecies() { } for (auto& b : tracked_species_info) { -#ifdef debug - std::stringstream out; - out << b.second.name << ": stored in queue? " << (b.second.explicitly_stored ? "true": "false"); - DEBUG_LEVEL_1(out.str()); -#endif tracked_species[b.first] = std::shared_ptr(new species::TrackedSpecie( b.second.type, b.second.name, b.second.mass_kg, b.second.charge_C, - b.second.explicitly_stored, 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)); diff --git a/src/simulation/density_field.cpp b/src/simulation/density_field.cpp index d25ec01..0e086a4 100755 --- a/src/simulation/density_field.cpp +++ b/src/simulation/density_field.cpp @@ -83,28 +83,6 @@ DynamicDensityField::DynamicDensityField( pitch_angle_binning.binIndexMax()), blitz::Range(energy_binning.binIndexMin(), energy_binning.binIndexMax()))), - // deleted_particles_(initiate5DArray( - // blitz::Range(position_binning.radial_binning.binIndexMin(), - // position_binning.radial_binning.binIndexMax()), - // blitz::Range(position_binning.azimuth_binning.binIndexMin(), - // position_binning.azimuth_binning.binIndexMax()), - // blitz::Range(position_binning.longitudinal_binning.binIndexMin(), - // position_binning.longitudinal_binning.binIndexMax()), - // blitz::Range(pitch_angle_binning.binIndexMin(), - // pitch_angle_binning.binIndexMax()), - // blitz::Range(energy_binning.binIndexMin(), - // energy_binning.binIndexMax()))), - // sync_deleted_particles_(initiate5DArray( - // blitz::Range(position_binning.radial_binning.binIndexMin(), - // position_binning.radial_binning.binIndexMax()), - // blitz::Range(position_binning.azimuth_binning.binIndexMin(), - // position_binning.azimuth_binning.binIndexMax()), - // blitz::Range(position_binning.longitudinal_binning.binIndexMin(), - // position_binning.longitudinal_binning.binIndexMax()), - // blitz::Range(pitch_angle_binning.binIndexMin(), - // pitch_angle_binning.binIndexMax()), - // blitz::Range(energy_binning.binIndexMin(), - // energy_binning.binIndexMax()))), density_per_m3_(initiate4DArray( blitz::Range(position_binning.radial_binning.binIndexMin(), position_binning.radial_binning.binIndexMax()), @@ -304,19 +282,6 @@ void DynamicDensityField::recalculateDensityArray() { // SimulationTimer::eventStopPoint("recalcDensityArray"); } -double DynamicDensityField::particleNumber() { - blitz::Array density = densityArray(); - double particleNumber = blitz::sum(density * bin_volume_m3_); -#ifdef debug_level3 - DEBUG_LEVEL_3("particle number: " + std::to_string(particleNumber)); -#endif - return particleNumber; -} - -double DynamicDensityField::particleNumber(blitz::TinyVector index) { - return time_in_bin_s_(index); -} - blitz::Array DynamicDensityField:: sliceSpectrumArray(const Coordinate& position) const { blitz::TinyVector position_bin = @@ -341,10 +306,8 @@ blitz::Array DynamicDensityField:: void DynamicDensityField::resetDynamicDensityField() { //timer_->resetTime(); // time stays global time_in_bin_s_ = 0.; - // deleted_particles_ = 0.; density_per_m3_ = 0.; sync_time_in_bin_s_ = 0.; - // sync_deleted_particles_ = 0.; } void DynamicDensityField::restartTimer(double time, double delta_t) { diff --git a/src/simulation/event_position_distribution.cpp b/src/simulation/event_position_distribution.cpp index bd224ca..8f3cb19 100755 --- a/src/simulation/event_position_distribution.cpp +++ b/src/simulation/event_position_distribution.cpp @@ -16,8 +16,7 @@ EventPositionDistribution::EventPositionDistribution( density_values( std::uniform_real_distribution(density.min, density.max)) {} -CylinderCoordinate EventPositionDistribution::rollPosition( - std::mt19937* engine) { +CylinderCoordinate EventPositionDistribution::rollPosition(std::mt19937* engine) { double random_number_z; double random_numer_r; double random_number_phi; @@ -38,82 +37,4 @@ CylinderCoordinate EventPositionDistribution::rollPosition( return CylinderCoordinate(blitz::TinyVector( random_numer_r, random_number_phi, random_number_z)); -} - -PositionDistributionFromDensity::PositionDistributionFromDensity( - std::shared_ptr density): - density_(density), - std_unifrm(std::uniform_real_distribution(0.,1.)) { -/************************************************************************************ -// get the extent of the domain of the DynamicDensityField object to be used for -// the rejection method - double rmin = density->positionBinning()->radial_binning.binBorders().front(); - double rmax = density->positionBinning()->radial_binning.binBorders().back(); - - double phimin = 0; - double phimax = 2*M_PI; - - double zmin = density->positionBinning()->longitudinal_binning.binBorders().front(); - double zmax = density->positionBinning()->longitudinal_binning.binBorders().back(); - -// initialize the distributions - r_values = std::uniform_real_distribution(rmin*rmin, rmax*rmax); - phi_values = std::uniform_real_distribution(phimin, phimax); - z_values = std::uniform_real_distribution(zmin, zmax); -**************************************************************************************/ -} - -/************************************************************************** -void PositionDistributionFromDensity::setMaximumDensity() { - maximum_density_ = blitz::max(density_->densityArray()); -} - -CylinderCoordinate PositionDistributionFromDensity::drawPosition(std::mt19937* engine) { - if(isEqual(maximum_density_, 0.)) { - // abuse the phi coordinate as a check if the density was zero - // only works if phi \in (0, 2pi) - return CylinderCoordinate(blitz::TinyVector(0,-1,0)); - } - - double random_r, random_phi, random_z, random_n; - - do { - random_r = std::sqrt(r_values(*engine)); - random_phi = phi_values(*engine); - random_z = z_values(*engine); - random_n = density_values(*engine); -// might be bad that the value gets recalculated for each call of density_->density(...) - } 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)); -} - ***************************************************************************/ - -CylinderCoordinate PositionDistributionFromDensity::drawPosition(std::mt19937* engine, - int r_idx, int phi_idx, int z_idx) { - double left_bin_border_radial = density_->positionBinning() - ->radial_binning.binBorders()[r_idx]; - double left_bin_border_azimuthal = density_->positionBinning() - ->azimuth_binning.binBorders()[phi_idx]; - double left_bin_border_longitudinal = density_->positionBinning() - ->longitudinal_binning.binBorders()[z_idx]; - - auto stepsize = density_->positionBinning()->stepsize(); - - double r_rndm, phi_rndm, z_rndm; - - r_rndm = std_unifrm(*engine); - phi_rndm = std_unifrm(*engine); - z_rndm = std_unifrm(*engine); - - double radius = std::sqrt(left_bin_border_radial*left_bin_border_radial + - (2*left_bin_border_radial + stepsize(0)) - *stepsize(0)*r_rndm); - - return CylinderCoordinate(blitz::TinyVector - (radius, - left_bin_border_azimuthal + stepsize(1) * phi_rndm, - left_bin_border_longitudinal + stepsize(2) * z_rndm)); } \ No newline at end of file diff --git a/src/simulation/injector.cpp b/src/simulation/injector.cpp index a058818..a0ca173 100755 --- a/src/simulation/injector.cpp +++ b/src/simulation/injector.cpp @@ -113,60 +113,6 @@ void Injector::setWeights(const double physical_number, const int numerical_numb #endif } -void Injector::drawNewParticleFromDensityField(std::mt19937* engine, - std::shared_ptr specie, - std::vector& queue, - std::shared_ptr magnetic_field, - const double weight_factor) { - blitz::TinyVector index = specie->randomIndex()->index(); - - const double num_part_in_bin = specie->densityForDrawing()->particleNumber(index); - - if(num_part_in_bin == 0) { -#if defined(debug_level2) || defined(debug_level3) - std::stringstream out; - out << "index: "<< index <<" has no particles"; - // DEBUG_LEVEL_2(out.str()); -#endif - return; - } - -// draw the position - CylinderCoordinate position = - specie->positionDistributionFromDensity()->drawPosition(engine, - index(0), index(1), index(2)); -// draw the velocity - CartesianVector velocity(blitz::TinyVector(0,0,0)); - -// in case the magnetic field is 0 at the current position draw the velocity -// with respect to the z-axis otherwise use the magnetic field - if(kinematic::norm(magnetic_field->value(position). - cartesianEntries(position)) == 0) { - velocity = specie->velocityDistributionFromDensityField()-> - drawVelocity(index, engine); - } else { - velocity = specie->velocityDistributionFromDensityField()-> - drawVelocity(index, engine, position, magnetic_field); - } - -#if defined(debug_level2) || defined(debug_level3) - std::stringstream out; - out << specie->name() << ":\n"; - out << "\ndrawn index: " << index; - out << "\nnumber of particles (" << specie->name() << ") at index: " << num_part_in_bin; - out << "\ndrawn position: " << position.cylinderEntries(); - out << "\ndrawn velocity: " << velocity.cartesianEntries(); - if(num_part_in_bin > 0) - DEBUG_LEVEL_2(out.str()); -#endif - - particle::Particle drawn_particle(specie, position, - velocity, weight_factor*num_part_in_bin); - - queue.push_back(drawn_particle); - -} - double Injector::timePassed() const { return 1 / activity_; } diff --git a/src/simulation/mover.cpp b/src/simulation/mover.cpp index 38613b2..7a1d28a 100755 --- a/src/simulation/mover.cpp +++ b/src/simulation/mover.cpp @@ -150,6 +150,13 @@ void BorisMover::move(particle::Particle* particle, } #ifdef debug_level3 + double new_mu; + if (kinematic::norm(B) == 0) { + new_mu = kinematic::cosPitchAngle( + blitz::TinyVector(0,0,1), particle->velocity().cartesianEntries()); + } else { + new_mu = kinematic::cosPitchAngle(B, particle->velocity().cartesianEntries()); + } if((changed_energy_bin || changed_position_bin) && !left_simulation_domain && !died_in_bin && (leftover_time > 0)) { std::stringstream out; out << "move(...) interrupted by change in "; @@ -163,7 +170,7 @@ void BorisMover::move(particle::Particle* particle, // 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 <<"unknown bin... " << changed_energy_bin << changed_position_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)"; diff --git a/src/simulation/simulation.cpp b/src/simulation/simulation.cpp index 13f5e40..2f7506e 100755 --- a/src/simulation/simulation.cpp +++ b/src/simulation/simulation.cpp @@ -79,10 +79,6 @@ void Simulation::init(Parameter& parameter) { #endif random_ = generator::buildRandomNumbers(parameter); - for(auto& tracked_specie : tracked_species_) { - tracked_specie.second->randomIndex()->reshuffle(random_->engine()); - } - #ifdef debug DEBUG_LEVEL_0("Init poisson solver."); #endif @@ -164,32 +160,10 @@ void Simulation::start() { injector_->setWeights(time_resolution_events, beta_decay_events); -// number of density events per timestep and CPU - const int particles_per_bin = - parameter_.intValue(json_path({"Simulation", - "NumberDensityParticlesPerBinPerCPU"}), 0); - -// calculate a 'stride' for the density events s.t. the same number of syncs -// would be performed - int density_stride = particles_per_bin; - if(beta_decay_events != 0) { - density_stride = std::ceil(particles_per_bin * - (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 = - 1./(MpiCommunication::mpi_size*particles_per_bin); - -#ifdef debug - DEBUG_LEVEL_0("Number of particles per bin per CPU: " + std::to_string(particles_per_bin)); -#endif - 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; - int leftover_density_events = particles_per_bin; const double delta_t = time_resolution_events*injector_->timePassed(); @@ -211,8 +185,6 @@ void Simulation::start() { // cpu sync is done int events_of_cycle = eventsOfCycle(event_stride, leftover_events_time_resolution); - int density_events_of_cycle = - eventsOfCycle(density_stride, leftover_density_events); #ifdef debug_level1 DEBUG_LEVEL_1("Events of cycle: " + std::to_string(events_of_cycle)); @@ -225,12 +197,10 @@ void Simulation::start() { #ifdef debug SIM_INFO("Leftover events this timestep this CPU: " + std::to_string(leftover_events_time_resolution)); - SIM_INFO("Leftover density events this timestep this CPU: " + - std::to_string(leftover_density_events)); #endif // process both density and beta events before a cpu synchronization - processDensityEvents(particles_per_bin, density_weight_factor); + processDensityEvents(); processEvents(events_of_cycle); #if defined(debug_level1) || defined(debug_level2) || defined(debug_level3) @@ -240,7 +210,6 @@ void Simulation::start() { #endif leftover_events_time_resolution -= events_of_cycle; - leftover_density_events -= density_events_of_cycle; reset = !(leftover_events_time_resolution > 0); @@ -300,7 +269,6 @@ void Simulation::start() { } leftover_events_time_resolution = beta_decay_events; - leftover_density_events = particles_per_bin; timer_->increaseTimestep(delta_t); timestep++; @@ -379,71 +347,43 @@ void Simulation::processEvents(int events) { } } -void Simulation::processDensityEvents(int particles_per_bin, const double weight_factor) { +void Simulation::processDensityEvents() { #ifdef debug DEBUG_LEVEL_0("Processing density events."); #endif - // number of full grid traversions + // number of full grid traversions (legacy variable) int current_event = 0; - // set to true if all grids of all species have been visited - bool all_bins_done = false; - // calculate the number of particles currently contained within the densities - // of the different tracked species before injection in order to avoid - // the expensive recalculateDensityField funtion being calculated for every - // new injection event - std::map particleNumbers; - for(auto& pair: tracked_species_) { - particleNumbers[pair.first] = (pair.second->densityForDrawing()->particleNumber()); - } - - while ((current_event < particles_per_bin) || (!queue_.empty())) { + while ((!queue_.empty()) || (!storage_queue_[active_storage].empty())) { #ifdef debug_level2 DEBUG_LEVEL_2("Current event: " + std::to_string(current_event)); #endif if (queue_.empty()) { - std::vector new_particles; - #ifdef debug_level2 DEBUG_LEVEL_2("Current density event: " + std::to_string(current_event)); #endif SimulationTimer::eventStartPoint(std::string("DensityInjection")); - new_particles = determineInjectedDensityParticles(random_.get(), - weight_factor, current_event, particleNumbers, - all_bins_done); + particle::Particle newParticle(storage_queue_[active_storage].back(), + tracked_species_); + storage_queue_[active_storage].pop_back(); SimulationTimer::eventStopPoint(std::string("DensityInjection")); #if defined debug_level3 - for(auto& particle : new_particles) { - std::stringstream output_ss; - output_ss << particle.specie()->name() << " with velocity (cart): "; - output_ss << particle.velocity().cartesianEntries(); - output_ss << " and position (cart)"; - output_ss << particle.cartesianPosition().cartesianEntries(); - DEBUG_LEVEL_3(output_ss.str()); - } + std::stringstream output_ss; + output_ss << newParticle.specie()->name() << " with velocity (cart): "; + output_ss << newParticle.velocity().cartesianEntries(); + output_ss << " and position (cart)"; + output_ss << newParticle.cartesianPosition().cartesianEntries(); + DEBUG_LEVEL_3(output_ss.str()); #endif // if no new particles could be injected from the density, it is not possible // that during another iteration cycle new particles will be injected // (drawing field only changes after timesteps) - if(all_bins_done) { - current_event += 1; - for(auto& element : tracked_species_) { - if(element.second->explicitlyStored()) { - continue; - } - element.second->randomIndex()->reshuffle(random_->engine()); - } - } - - if(!new_particles.empty()) { - queue_.insert(queue_.end(), new_particles.begin(), new_particles.end()); - } else { - continue; - } + current_event += 1; + queue_.push_back(newParticle); } #ifdef debug_level2 DEBUG_LEVEL_2("Particles in queue: " + @@ -521,11 +461,11 @@ void Simulation::processUntilTermination(particle::Particle& particle) { ERROR(out.str()); } #endif - // particle terminated due to reaching time resolution alone -> needs - // to be stored (if it's an electron) - if(particle.specie()->explicitlyStored()) { - storage_queue_[inactive_storage].push_back(particle.serialize()); - } + + storage_queue_[inactive_storage].push_back(particle.serialize()); +#ifdef debug_level3 + DEBUG_LEVEL_3(particle.specie()->name() + " pushed back into storage."); +#endif if(particle.specie()->deletionLogger()) { particle.specie()->deletionLogger()->logDeletion(particle.cylinderPosition(), @@ -692,75 +632,6 @@ interaction::InteractionOutput Simulation::interact( } } -// weight_factor has to be such that particleNumber * weight_factor = weight -std::vector Simulation::determineInjectedDensityParticles( - RandomNumbers* random, const double weight_factor, const int current, - std::map& particleNumbers, bool& all_bins_done) { - - std::vector new_particles{}; - - if(!new_particles.empty()) { - ERROR("In drawing particles from the density:" - " the new particles vector is not empty at initialization"); - } - - bool tmp_done = false; - all_bins_done = true; - - for(auto& pair: tracked_species_) { -// Note: takes the next particle in the storage queue regardless if -// that particle is a member of the current tracked_specie - if(pair.second->explicitlyStored()) { - // if the storage queue does not contain any more particles - // no new particles can be drawn - if(storage_queue_[active_storage].empty()) { - all_bins_done = all_bins_done && true; - continue; - } else { - all_bins_done = false; - } - // take the last particle from the storage queue and extract it - particle::SerializedParticle new_sparticle = - storage_queue_[active_storage].back(); - storage_queue_[active_storage].pop_back(); - particle::Particle new_particle(new_sparticle, tracked_species_); - // "draw" the actual particle - new_particles.push_back(new_particle); - continue; - } - - tmp_done = pair.second->randomIndex()->visited_all(); - - if (particleNumbers[pair.first] == 0) { - all_bins_done = all_bins_done && true; -#ifdef debug_level3 - DEBUG_LEVEL_3("No particles in density of species: " + pair.second->name()); -#endif - continue; - } - -#if defined debug_level3 - if (current == 0) { - std::stringstream out; - out << pair.second->name(); - out << "particle number from density: "; - out << particleNumbers[pair.first]; - DEBUG_LEVEL_3(out.str()); - } -#endif - - if(!tmp_done) { - injector_->drawNewParticleFromDensityField(random->engine(), - pair.second, new_particles, magnetic_field_, - weight_factor); - } - - // if species is not done tmp_done && ... = false - all_bins_done = all_bins_done && tmp_done; - } - return new_particles; -} - //maybe put that into the poisson solver blitz::Array Simulation::chargedensity() { #ifdef debug_level3 diff --git a/src/simulation/species.cpp b/src/simulation/species.cpp index 449bdee..f2e647f 100755 --- a/src/simulation/species.cpp +++ b/src/simulation/species.cpp @@ -38,7 +38,6 @@ TrackedSpecie::TrackedSpecie( std::string name, double mass_kg, double charge_C, - bool explicitly_stored, std::shared_ptr mover, std::shared_ptr terminator, std::shared_ptr reflector, @@ -53,29 +52,11 @@ TrackedSpecie::TrackedSpecie( density_field_(density_field), deletion_logger_(std::shared_ptr(nullptr)), current_collector_(std::shared_ptr(nullptr)), - velocity_distribution_(velocity_distribution), drift_velocity_(drift_velocity), density_for_drawing_(density_for_drawing), - position_distribution_from_density_( - std::make_shared(density_for_drawing)), velocity_distribution_from_density_( std::make_shared( - density_for_drawing, mass_kg)), - explicitly_stored_(explicitly_stored) { - blitz::TinyVector num_bins; - auto tmp = density_field->positionBinning()->numBins(); - num_bins(0) = tmp(0); num_bins(1) = tmp(1); num_bins(2) = tmp(2); - - num_bins(3) = density_field->pitchAngleBinning()->numBins(); - // include underflow bin in the energy direction only - num_bins(4) = density_field->energyBinning()->numBins() + 1; - - random_index_ = std::make_shared (RandomIndex(num_bins)); -} - -RandomIndex* TrackedSpecie::randomIndex() { - return random_index_.get(); -} + density_for_drawing, mass_kg)) {} DensityField* TrackedSpecie::densityField() { return density_field_.get(); @@ -120,11 +101,6 @@ CurrentCollector* TrackedSpecie::currentCollector() { return current_collector_.get(); } -PositionDistributionFromDensity* - TrackedSpecie::positionDistributionFromDensity() { - return position_distribution_from_density_.get(); -} - VelocityDistributionFromDensityField* TrackedSpecie::velocityDistributionFromDensityField() { return velocity_distribution_from_density_.get(); @@ -149,10 +125,6 @@ void TrackedSpecie::currentCollector( current_collector_ = new_collector; } -bool TrackedSpecie::explicitlyStored() { - return explicitly_stored_; -} - BackgroundSpecie::BackgroundSpecie( ImplementedSpecies type, std::string name, diff --git a/src/simulation/velocity_distribution.cpp b/src/simulation/velocity_distribution.cpp index 82dde87..5342c14 100755 --- a/src/simulation/velocity_distribution.cpp +++ b/src/simulation/velocity_distribution.cpp @@ -37,10 +37,6 @@ VelocityDistributionFromDensityField::VelocityDistributionFromDensityField( pitchAngleBinBorders_(density_field->pitchAngleBinning()->binBorders()), theta_distribution_(std::uniform_real_distribution(0.,1.)), mass_kg_(mass_kg) { - double maxAbsExponent = std::max(std::abs(std::log10(energyBinBorders_.front())), - std::abs(std::log10(energyBinBorders_.back()))); - alpha_lower_limit = -300/maxAbsExponent; - alpha_upper_limit = 300/maxAbsExponent; // necessary to include the underflow bin in the energy domain energyBinBorders_.insert(energyBinBorders_.begin(), 0); } @@ -154,211 +150,6 @@ CartesianVector VelocityDistributionFromDensityField::rollVelocity( const double vMagnitude = kinematic::velocity(energy_eV, mass_kg_); - blitz::TinyVector v_perp = - perpendicularNormalVector(cartesianB_values); - - blitz::TinyVector v_direction = pitchAngle - * cartesianB_values/Bmagnitude - + std::sqrt(1 - pitchAngle*pitchAngle) - * kinematic::rotateVector(v_perp, cartesianB_values, theta); - - return CartesianVector(v_direction * vMagnitude); -} - -// if valid is false after function, the number of particles in the cell at the given index -// is 0 -CartesianVector VelocityDistributionFromDensityField::drawVelocity( - const blitz::TinyVector index, std::mt19937* engine) { - - // const double number_particles_in_bin = density_field_->particleNumber(index); - - const double energy_step = energyBinBorders_[index(4) + 1] - - energyBinBorders_[index(4)]; - - const double pitch_angle_step = pitchAngleBinBorders_[index(3) + 1] - - pitchAngleBinBorders_[index(3)]; - - double energy_eV = energyBinBorders_[index(4)] - + theta_distribution_(*engine)*energy_step; - - double pitchAngle = pitchAngleBinBorders_[index(3)] - + theta_distribution_(*engine)*pitch_angle_step; - - // construct the velocity - - const double theta = theta_distribution_(*engine)*2*M_PI; - - blitz::TinyVector zDirection(0,0,1); - - const double vMagnitude = kinematic::velocity(energy_eV, mass_kg_); - -// vector perpendicular to e_z - blitz::TinyVector v_perp(1,0,0); - - blitz::TinyVector v_direction = pitchAngle * zDirection - + std::sqrt(1 - pitchAngle*pitchAngle) - * kinematic::rotateVector(v_perp, zDirection, theta); - - return CartesianVector(v_direction * vMagnitude); -} - -CartesianVector VelocityDistributionFromDensityField::drawVelocity( - const blitz::TinyVector index, std::mt19937* engine, - const Coordinate& position, - std::shared_ptr magnetic_field) { - - // since a 0 is included in the bin borders for the energy - const double energy_step = energyBinBorders_[index(4) + 2] - - energyBinBorders_[index(4) + 1]; - - const double pitch_angle_step = pitchAngleBinBorders_[index(3) + 1] - - pitchAngleBinBorders_[index(3)]; - - // double energy_eV = energyBinBorders_[index(4) + 1] - // + theta_distribution_(*engine)*energy_step; - double energy_eV; - double alpha = 1; - if(index(4) == -1) { - // linear drawing inbetween E=0, N(0)=0 to E=Er and N(Er)=Nr - energy_eV = energyBinBorders_[index(4) + 2]* - std::sqrt(theta_distribution_(*engine)); -// #ifdef debug -// std::stringstream out; -// out << "underflow bin.\n\tdrawn energy: " << energy_eV; -// out << "\n\tenergy value left: " << energyBinBorders_[index(4)+1]; -// out << "\n\tenergy value right: " << energyBinBorders_[index(4)+2]; -// DEBUG(out.str()); -// #endif - } else { - double nl = density_field_->particleNumber(index); - double nr = density_field_->particleNumber(index + - blitz::TinyVector(0,0,0,0,1)); - - // double alpha; - // no reason for specific values, just to avoid these cases - if(nl == 0) { - alpha = 1; - } else if (nr == 0) { - alpha = -1; - } else if (nr == 0 && nl == 0) { - alpha = 0; - } else { - alpha = std::log(nr/nl)/std::log(energyBinBorders_[index(4) + 2]/ - energyBinBorders_[index(4) + 1]); - } - - // alpha = std::abs(alpha) > 100 ?(alpha/std::abs(alpha))*100 : alpha; - alpha = std::min(std::max(alpha, alpha_lower_limit), alpha_upper_limit); - - if(alpha == -1) { - energy_eV = std::pow(energyBinBorders_[index(4) + 2] - / energyBinBorders_[index(4) + 1], theta_distribution_(*engine)) * - energyBinBorders_[index(4) + 1]; - // energy_eV = (std::pow(2, theta_distribution_(*engine)) - 1)*energy_step - // + energyBinBorders_[index(4) + 1]; -// #ifdef debug -// std::stringstream out; -// out << "alpha = -1\n\tdrawn energy: " << energy_eV; -// out << "\n\tenergy value left: " << energyBinBorders_[index(4)+1]; -// out << "\n\tenergy value right: " << energyBinBorders_[index(4)+2]; -// DEBUG(out.str()); -// #endif - } else { - double xl_alphap1 = std::pow(energyBinBorders_[index(4) + 1], alpha + 1); - double xr_alphap1 = std::pow(energyBinBorders_[index(4) + 2], alpha + 1); - energy_eV = std::pow((xr_alphap1 - xl_alphap1)* - theta_distribution_(*engine) + xl_alphap1, 1/(alpha + 1)); -// #ifdef debug -// if(std::abs(alpha) > 100) { -// std::stringstream out; -// out << "alpha: " << alpha; -// out << "\nxl_alphap1: " << xl_alphap1; -// out << "\nxr_alphap1: " << xr_alphap1; -// DEBUG(out.str()); -// } -// #endif -// #ifdef debug -// std::stringstream out; -// out << "alpha = "<< alpha << "\n\tdrawn energy: " << energy_eV; -// out << "\n\tenergy value left: " << energyBinBorders_[index(4)+1]; -// out << "\n\tenergy value right: " << energyBinBorders_[index(4)+2]; -// DEBUG(out.str()); -// #endif - } - } - - double pitchAngle = pitchAngleBinBorders_[index(3)] - + theta_distribution_(*engine)*pitch_angle_step; - -#ifdef debug - if((std::isnan(pitchAngle)) || (std::isnan(energy_eV))) { - std::stringstream out; - out << "pitchAngle: " << pitchAngle; - out << "\nenergy value: " << energy_eV; - out << "\nalpha: " << alpha; - out << "\ncurrent index: " << index; - out << "\nNl: " << density_field_->particleNumber(index); - out << "\nNl: " << density_field_->particleNumber(index + - blitz::TinyVector(0,0,0,0,1)); - out << "\nEl: " << energyBinBorders_[index(4) + 1]; - out << "\nEr: " << energyBinBorders_[index(4) + 2]; - out << "\nEl^(alpha+1): " << std::pow(energyBinBorders_[index(4) + 1], alpha + 1); - out << "\nEr^(alpha+1): " << std::pow(energyBinBorders_[index(4) + 2], alpha + 1); - // out << "\nenergy values: " << energyValues; - // out << "\npitch angle values: " << pitchAngleValues; - out << "\nenergy bin borders: [ "; - for(auto element : energyBinBorders_) { - out << element << " "; - } - out << "]"; - - ERROR(out.str()); - } - - // if(std::isinf(energy_eV)) { - // std::stringstream out; - // out << "pitchAngle: " << pitchAngle; - // out << "\nenergy value: " << energy_eV; - // out << "\ncurrent index: " << index; - // out << "\nalpha: " << alpha; - // out << "\nNl: " << density_field_->particleNumber(index); - // out << "\nNl: " << density_field_->particleNumber(index + - // blitz::TinyVector(0,0,0,0,1)); - // out << "\nEl: " << energyBinBorders_[index(4) + 1]; - // out << "\nEr: " << energyBinBorders_[index(4) + 2]; - // // out << "\nenergy values: " << energyValues; - // // out << "\npitch angle values: " << pitchAngleValues; - // out << "\nenergy bin borders: [ "; - // for(auto element : energyBinBorders_) { - // out << element << " "; - // } - // out << "]"; - - // ERROR(out.str()); - // } -#endif - -#ifdef debug_level2 - std::stringstream out; - out << "left Pitchangle value: "<< pitchAngleBinBorders_[index(3)]; - out << "\nright Pitchangle value: " << pitchAngleBinBorders_[index(3)+1]; - out << "\npitchAngle: " << pitchAngle; - out << "\nleft energy value: "<< energyBinBorders_[index(4) + 1]; - out << "\nright energy value: " << energyBinBorders_[index(4)+2]; - out << "\nenergy value: " << energy_eV; - DEBUG_LEVEL_2(out.str()); -#endif - - // construct the velocity - - const double theta = theta_distribution_(*engine)*2*M_PI; - - blitz::TinyVector cartesianB_values = - magnetic_field->value(position).cartesianEntries(position); - const double Bmagnitude = kinematic::norm(cartesianB_values); - - const double vMagnitude = kinematic::velocity(energy_eV, mass_kg_); - blitz::TinyVector v_perp = perpendicularNormalVector(cartesianB_values);