Basic neccessities to draw from a density field;
does not yet work correctly since the neccessary density field is reset after a time resolution step
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#include "constants/sim_constants.h"
|
#include "constants/sim_constants.h"
|
||||||
|
|
||||||
bool isEqual(double a, double b) {
|
inline bool isEqual(double a, double b) {
|
||||||
return std::abs(a - b) < SimConstants::compare_epsilon;
|
return std::abs(a - b) < SimConstants::compare_epsilon;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,9 @@
|
|||||||
#define DENSITY_FIELD_H
|
#define DENSITY_FIELD_H
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
|
#include "simulation/compare.h"
|
||||||
#include "simulation/coordinates.h"
|
#include "simulation/coordinates.h"
|
||||||
#include "simulation/cpu_synchronizer.h"
|
#include "simulation/cpu_synchronizer.h"
|
||||||
#include "simulation/energy_binning.h"
|
#include "simulation/energy_binning.h"
|
||||||
@@ -38,6 +40,10 @@ class DynamicDensityField : public DensityField {
|
|||||||
double density(const Coordinate& position);
|
double density(const Coordinate& position);
|
||||||
double density(const Coordinate& position, double energy_eV);
|
double density(const Coordinate& position, double energy_eV);
|
||||||
|
|
||||||
|
double particleNumber();
|
||||||
|
blitz::TinyVector<double, 4> drawPositionAndEnergy(std::mt19937* engine);
|
||||||
|
void setDensityDistribution();
|
||||||
|
|
||||||
void resetDynamicDensityField();
|
void resetDynamicDensityField();
|
||||||
|
|
||||||
blitz::Array<double, 3> densityArray();
|
blitz::Array<double, 3> densityArray();
|
||||||
@@ -69,12 +75,19 @@ class DynamicDensityField : public DensityField {
|
|||||||
// num_bin_z: z >= z_max
|
// num_bin_z: z >= z_max
|
||||||
BinCoordinate densityBin(const CylinderCoordinate& position) const;
|
BinCoordinate densityBin(const CylinderCoordinate& position) const;
|
||||||
void recalculateDensityArray();
|
void recalculateDensityArray();
|
||||||
|
void initPositionDistributions();
|
||||||
|
|
||||||
blitz::Array<double, 4> time_in_bin_s_;
|
blitz::Array<double, 4> time_in_bin_s_;
|
||||||
blitz::Array<long int, 4> deleted_particles_;
|
blitz::Array<long int, 4> deleted_particles_;
|
||||||
blitz::Array<double, 4> density_per_m3_;
|
blitz::Array<double, 4> density_per_m3_;
|
||||||
blitz::Array<double, 3> bin_volume_m3_;
|
blitz::Array<double, 3> bin_volume_m3_;
|
||||||
|
|
||||||
|
std::uniform_real_distribution<double> r_dist_;
|
||||||
|
std::uniform_real_distribution<double> phi_dist_;
|
||||||
|
std::uniform_real_distribution<double> z_dist_;
|
||||||
|
std::piecewise_constant_distribution<double> E_dist_;
|
||||||
|
std::uniform_real_distribution<double> density_dist_;
|
||||||
|
|
||||||
const CylinderBinning position_binning_;
|
const CylinderBinning position_binning_;
|
||||||
const EnergyBinning energy_binning_;
|
const EnergyBinning energy_binning_;
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
class DriftVelocity;
|
class DriftVelocity;
|
||||||
class VelocityDistribution;
|
class VelocityDistribution;
|
||||||
|
class DynamicDensityField;
|
||||||
|
|
||||||
namespace particle {
|
namespace particle {
|
||||||
class Particle;
|
class Particle;
|
||||||
@@ -37,8 +38,16 @@ class Injector {
|
|||||||
std::shared_ptr<DriftVelocity> mother_drift_velocity);
|
std::shared_ptr<DriftVelocity> mother_drift_velocity);
|
||||||
|
|
||||||
std::vector<particle::Particle> rollNewParticles(std::mt19937* engine,
|
std::vector<particle::Particle> rollNewParticles(std::mt19937* engine,
|
||||||
const double current_time,
|
const double current_time);
|
||||||
const int time_resolution_events);
|
|
||||||
|
void rollNewParticleFromDensityField(std::mt19937* engine,
|
||||||
|
std::shared_ptr<species::TrackedSpecie> specie,
|
||||||
|
std::vector<particle::Particle>& queue,
|
||||||
|
const double weight,
|
||||||
|
const double injection_time);
|
||||||
|
|
||||||
|
void initInjectionTimeDistribution(double delta_t);
|
||||||
|
|
||||||
double timePassed() const;
|
double timePassed() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -46,6 +55,7 @@ class Injector {
|
|||||||
const double activity_;
|
const double activity_;
|
||||||
std::shared_ptr<EventPositionDistribution> position_distribution_;
|
std::shared_ptr<EventPositionDistribution> position_distribution_;
|
||||||
std::shared_ptr<DriftVelocity> mother_drift_velocity_;
|
std::shared_ptr<DriftVelocity> mother_drift_velocity_;
|
||||||
|
std::uniform_real_distribution<double> injection_time_distribution_;
|
||||||
|
|
||||||
particle::Particle rollNewParticle(
|
particle::Particle rollNewParticle(
|
||||||
std::mt19937* engine,
|
std::mt19937* engine,
|
||||||
|
|||||||
@@ -34,12 +34,14 @@ class Simulation {
|
|||||||
void init(Parameter& parameter);
|
void init(Parameter& parameter);
|
||||||
void start();
|
void start();
|
||||||
void finalize();
|
void finalize();
|
||||||
void processEvents(int events, const int time_resolution_events);
|
void processEvents(int events, int density_events);
|
||||||
void processUntilTermination(particle::Particle& particle);
|
void processUntilTermination(particle::Particle& particle);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<particle::Particle> determineInjectedParticles(
|
std::vector<particle::Particle> determineInjectedParticles(RandomNumbers* random);
|
||||||
RandomNumbers* random, const int time_resolution_events);
|
std::vector<particle::Particle>
|
||||||
|
determineInjectedDensityParticles(RandomNumbers* random,
|
||||||
|
const int numberParticlesToDraw);
|
||||||
|
|
||||||
double meanFreePath(particle::Particle& particle, RandomNumbers* random);
|
double meanFreePath(particle::Particle& particle, RandomNumbers* random);
|
||||||
|
|
||||||
@@ -69,9 +71,7 @@ class Simulation {
|
|||||||
std::shared_ptr<SimulationOutput> output_;
|
std::shared_ptr<SimulationOutput> output_;
|
||||||
std::shared_ptr<CpuSynchronizer> cpu_synchronizer_;
|
std::shared_ptr<CpuSynchronizer> cpu_synchronizer_;
|
||||||
|
|
||||||
std::vector<particle::Particle> queues_[2];
|
std::vector<particle::Particle> queue_;
|
||||||
int active_queue_;
|
|
||||||
int non_active_queue_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* SIMULATION_H */
|
#endif /* SIMULATION_H */
|
||||||
|
|||||||
@@ -83,6 +83,7 @@ DynamicDensityField::DynamicDensityField(
|
|||||||
if (!timer) {
|
if (!timer) {
|
||||||
ERROR("Timer is necessary for calculation of density field");
|
ERROR("Timer is necessary for calculation of density field");
|
||||||
}
|
}
|
||||||
|
initPositionDistributions();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DynamicDensityField::changedPositionBin(
|
bool DynamicDensityField::changedPositionBin(
|
||||||
@@ -166,6 +167,88 @@ blitz::Array<double, 3> DynamicDensityField::densityArray() {
|
|||||||
return density.copy();
|
return density.copy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double DynamicDensityField::particleNumber() {
|
||||||
|
blitz::Array<double, 3> density = densityArray();
|
||||||
|
double particleNumber = blitz::sum(density * bin_volume_m3_);
|
||||||
|
DEBUG_LEVEL_3("particle number: " + std::to_string(particleNumber));
|
||||||
|
return particleNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DynamicDensityField::initPositionDistributions() {
|
||||||
|
double rmin, rmax;
|
||||||
|
double phimin, phimax;
|
||||||
|
double zmin, zmax;
|
||||||
|
|
||||||
|
auto rbins = position_binning_.radial_binning.binBorders();
|
||||||
|
auto zbins = position_binning_.longitudinal_binning.binBorders();
|
||||||
|
auto phibins = position_binning_.azimuth_binning.binBorders();
|
||||||
|
|
||||||
|
rmin = *(rbins.begin());
|
||||||
|
rmax = *(rbins.end());
|
||||||
|
|
||||||
|
zmin = *(zbins.begin());
|
||||||
|
zmax = *(zbins.end());
|
||||||
|
|
||||||
|
phimin = *(phibins.begin());
|
||||||
|
phimax = *(phibins.end());
|
||||||
|
|
||||||
|
r_dist_ = std::uniform_real_distribution<double>(rmin*rmin, rmax*rmax);
|
||||||
|
phi_dist_ = std::uniform_real_distribution<double>(phimin, phimax);
|
||||||
|
z_dist_ = std::uniform_real_distribution<double>(zmin, zmax);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DynamicDensityField::setDensityDistribution() {
|
||||||
|
density_dist_ =
|
||||||
|
std::uniform_real_distribution<double>(0., blitz::max(densityArray()));
|
||||||
|
}
|
||||||
|
|
||||||
|
blitz::TinyVector<double, 4> DynamicDensityField::drawPositionAndEnergy(
|
||||||
|
std::mt19937* engine) {
|
||||||
|
// if the density is empty, then return a TinyVector filled with 0s as
|
||||||
|
// an indiction of that (probably not the best solution)
|
||||||
|
if (isEqual(blitz::max(density_per_m3_), 0.)) {
|
||||||
|
DEBUG_LEVEL_3("Density is empty");
|
||||||
|
return blitz::TinyVector<double, 4> (0,0,0,0);
|
||||||
|
}
|
||||||
|
double random_r, random_z, random_phi, random_E, random_n;
|
||||||
|
double density_in_random_cell;
|
||||||
|
|
||||||
|
// random_r = 0.01;
|
||||||
|
// random_phi = 0.;
|
||||||
|
// random_z = 1.;
|
||||||
|
// draw random position (independent of the Energy)
|
||||||
|
do {
|
||||||
|
DEBUG_LEVEL_3("Missed.");
|
||||||
|
random_r = std::sqrt(r_dist_(*engine));
|
||||||
|
random_phi = phi_dist_(*engine);
|
||||||
|
random_z = z_dist_(*engine);
|
||||||
|
random_n = density_dist_(*engine);
|
||||||
|
|
||||||
|
density_in_random_cell = density(CylinderCoordinate(
|
||||||
|
blitz::TinyVector<double, 3> (random_r, random_phi, random_z)));
|
||||||
|
} while (random_n >= density_in_random_cell);
|
||||||
|
|
||||||
|
const blitz::TinyVector<int, 3> position_bin =
|
||||||
|
position_binning_.bin(CylinderCoordinate(
|
||||||
|
blitz::TinyVector<double, 3> (random_r, random_phi, random_z))
|
||||||
|
.cylinderEntries());
|
||||||
|
|
||||||
|
std::vector<double> energyBins = energy_binning_.binBorders();
|
||||||
|
energyBins.insert(energyBins.begin(), 0.);
|
||||||
|
blitz::Array<double, 1> densityAtPosition = density_per_m3_(position_bin(0),
|
||||||
|
position_bin(1),
|
||||||
|
position_bin(2),
|
||||||
|
blitz::Range::all());
|
||||||
|
|
||||||
|
E_dist_ = std::piecewise_constant_distribution<double>(energyBins.begin(),
|
||||||
|
energyBins.end(),
|
||||||
|
densityAtPosition.begin());
|
||||||
|
|
||||||
|
random_E = E_dist_(*engine);
|
||||||
|
|
||||||
|
return blitz::TinyVector<double, 4> (random_r, random_phi, random_z, random_E);
|
||||||
|
}
|
||||||
|
|
||||||
blitz::Array<double, 4> DynamicDensityField::spectrumArray() {
|
blitz::Array<double, 4> DynamicDensityField::spectrumArray() {
|
||||||
recalculateDensityArray();
|
recalculateDensityArray();
|
||||||
return density_per_m3_;
|
return density_per_m3_;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "simulation/kinematic.h"
|
#include "simulation/kinematic.h"
|
||||||
#include "simulation/particle.h"
|
#include "simulation/particle.h"
|
||||||
#include "simulation/species.h"
|
#include "simulation/species.h"
|
||||||
|
#include "simulation/density_field.h"
|
||||||
#include "simulation/velocity_distribution.h"
|
#include "simulation/velocity_distribution.h"
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
@@ -45,19 +46,23 @@ particle::Particle Injector::rollNewParticle(
|
|||||||
CartesianVector(velocity), time_birth, weight);
|
CartesianVector(velocity), time_birth, weight);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Injector::initInjectionTimeDistribution(double delta_t) {
|
||||||
|
injection_time_distribution_ =
|
||||||
|
std::uniform_real_distribution<double>(0., delta_t);
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<particle::Particle> Injector::rollNewParticles(
|
std::vector<particle::Particle> Injector::rollNewParticles(
|
||||||
std::mt19937* engine, const double current_time,
|
std::mt19937* engine, const double current_time) {
|
||||||
const int time_resolution_events) {
|
|
||||||
CylinderCoordinate position = position_distribution_->rollPosition(engine);
|
CylinderCoordinate position = position_distribution_->rollPosition(engine);
|
||||||
DEBUG_LEVEL_3("Injected z position: " +
|
DEBUG_LEVEL_3("Injected z position: " +
|
||||||
std::to_string(position.cylinderEntries()(2)));
|
std::to_string(position.cylinderEntries()(2)));
|
||||||
std::vector<particle::Particle> new_particles;
|
std::vector<particle::Particle> new_particles;
|
||||||
const double delta_t = time_resolution_events*timePassed();
|
|
||||||
std::uniform_real_distribution<> injection_time(current_time - delta_t,
|
const double time_birth = current_time - injection_time_distribution_(*engine);
|
||||||
current_time);
|
|
||||||
const double time_birth = injection_time(*engine);
|
|
||||||
for (auto& injector_info : injector_infos_) {
|
for (auto& injector_info : injector_infos_) {
|
||||||
const double weight = 1./injector_info.multiplicity;
|
const double weight = 1./injector_info.multiplicity;
|
||||||
|
|
||||||
for (int i = 0; i < injector_info.multiplicity; i++) {
|
for (int i = 0; i < injector_info.multiplicity; i++) {
|
||||||
new_particles.push_back(
|
new_particles.push_back(
|
||||||
rollNewParticle(engine, position, injector_info.created_specie,
|
rollNewParticle(engine, position, injector_info.created_specie,
|
||||||
@@ -68,6 +73,44 @@ std::vector<particle::Particle> Injector::rollNewParticles(
|
|||||||
return new_particles;
|
return new_particles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Injector::rollNewParticleFromDensityField(std::mt19937* engine,
|
||||||
|
std::shared_ptr<species::TrackedSpecie> specie,
|
||||||
|
std::vector<particle::Particle>& queue,
|
||||||
|
const double weight,
|
||||||
|
const double injection_time) {
|
||||||
|
blitz::TinyVector<double, 4> particle_specs =
|
||||||
|
specie->dynamicDensityField()->drawPositionAndEnergy(engine);
|
||||||
|
// blitz::TinyVector<double, 4> particle_specs(0.01, 0, 1, 1);
|
||||||
|
|
||||||
|
// check if density was empty (i.e. Energy component is 0)
|
||||||
|
if (particle_specs[3] == 0) {
|
||||||
|
DEBUG_LEVEL_3(specie->name() + ": has zero density");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
DEBUG_LEVEL_3("Injected z position: " + std::to_string(particle_specs(2)));
|
||||||
|
|
||||||
|
const double v_abs = kinematic::velocity(particle_specs[3], specie->mass_kg());
|
||||||
|
|
||||||
|
std::uniform_real_distribution<double> distribution(0.0, 1.0);
|
||||||
|
const double theta = distribution(*engine) * 2 * M_PI;
|
||||||
|
const double phi = std::acos(1. - 2.*distribution(*engine));
|
||||||
|
|
||||||
|
// for now draw the particles with an isotropic velocity distribution,
|
||||||
|
// is not realistic but as of now there is no information about the
|
||||||
|
// velocity vector distribution in the respective cell
|
||||||
|
blitz::TinyVector<double, 3> v_normal(std::cos(theta) * std::sin(phi),
|
||||||
|
std::sin(theta) * std::sin(phi),
|
||||||
|
std::cos(phi));
|
||||||
|
|
||||||
|
particle::Particle drawn_particle(specie, CylinderCoordinate(
|
||||||
|
blitz::TinyVector<double, 3>(particle_specs[0],
|
||||||
|
particle_specs[1], particle_specs[2])),
|
||||||
|
CartesianVector(v_normal*v_abs), injection_time, weight);
|
||||||
|
|
||||||
|
queue.push_back(drawn_particle);
|
||||||
|
}
|
||||||
|
|
||||||
double Injector::timePassed() const {
|
double Injector::timePassed() const {
|
||||||
return 1 / activity_;
|
return 1 / activity_;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,9 +69,6 @@ void Simulation::init(Parameter& parameter) {
|
|||||||
DEBUG_LEVEL_0("Init cpu synchronizer.");
|
DEBUG_LEVEL_0("Init cpu synchronizer.");
|
||||||
cpu_synchronizer_ = std::make_shared<CpuSynchronizer>();
|
cpu_synchronizer_ = std::make_shared<CpuSynchronizer>();
|
||||||
DEBUG_LEVEL_3("Init done.");
|
DEBUG_LEVEL_3("Init done.");
|
||||||
|
|
||||||
active_queue_ = 0;
|
|
||||||
non_active_queue_ = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int eventsThisCpu(int events_all_cpus) {
|
int eventsThisCpu(int events_all_cpus) {
|
||||||
@@ -91,11 +88,6 @@ int eventsOfCycle(int stride, int leftover_events) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void switch_active(int& active, int& non_active) {
|
|
||||||
non_active = active;
|
|
||||||
active = (active + 1)%2;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Simulation::finalize() {
|
void Simulation::finalize() {
|
||||||
SIM_INFO("Simulation ended.");
|
SIM_INFO("Simulation ended.");
|
||||||
}
|
}
|
||||||
@@ -110,6 +102,8 @@ void Simulation::start() {
|
|||||||
const int time_resolution_events =
|
const int time_resolution_events =
|
||||||
parameter_.intValue(json_path({"Simulation", "TimeResolutionEvents"}),
|
parameter_.intValue(json_path({"Simulation", "TimeResolutionEvents"}),
|
||||||
total_simulated_events);
|
total_simulated_events);
|
||||||
|
const int density_events =
|
||||||
|
parameter_.intValue(json_path({"Simulation", "DensityEventsPerCPU"}), 0);
|
||||||
|
|
||||||
SIM_INFO("Total simulated events: " + std::to_string(total_simulated_events));
|
SIM_INFO("Total simulated events: " + std::to_string(total_simulated_events));
|
||||||
SIM_INFO("Simulated events before cpu sync: " + std::to_string(event_stride));
|
SIM_INFO("Simulated events before cpu sync: " + std::to_string(event_stride));
|
||||||
@@ -121,27 +115,24 @@ void Simulation::start() {
|
|||||||
|
|
||||||
const double delta_t = time_resolution_events*injector_->timePassed();
|
const double delta_t = time_resolution_events*injector_->timePassed();
|
||||||
timer_->setDeltaT(delta_t);
|
timer_->setDeltaT(delta_t);
|
||||||
|
injector_->initInjectionTimeDistribution(delta_t);
|
||||||
|
|
||||||
int num_cpu_syncs = 0;
|
int num_cpu_syncs = 0;
|
||||||
bool reset = true;
|
bool reset = true;
|
||||||
|
|
||||||
queues_[0].reserve(events_this_cpu);
|
queue_.reserve(100);
|
||||||
queues_[1].reserve(events_this_cpu);
|
|
||||||
// bring some order into the reset checks
|
|
||||||
|
|
||||||
timer_->increaseTimestep(delta_t);
|
timer_->increaseTimestep(delta_t);
|
||||||
while (leftover_events > 0) {
|
while (leftover_events > 0) {
|
||||||
std::stringstream out;
|
std::stringstream out;
|
||||||
out << "Timeinterval: " << timer_->time() << "s.";
|
out << "Timeinterval: " << timer_->time() << "s.";
|
||||||
out << "\n size of particle: " << sizeof(particle::Particle);
|
// out << "\n size of particle: " << sizeof(particle::Particle);
|
||||||
DEBUG_LEVEL_3(out.str());
|
DEBUG_LEVEL_3(out.str());
|
||||||
|
|
||||||
int events_of_cylcle = eventsOfCycle(event_stride, leftover_events_time_resolution);
|
int events_of_cylcle = eventsOfCycle(event_stride, leftover_events_time_resolution);
|
||||||
SIM_INFO("Leftover events on first cpu: " +
|
SIM_INFO("Leftover events on first cpu: " +
|
||||||
std::to_string(leftover_events));
|
std::to_string(leftover_events));
|
||||||
processEvents(events_of_cylcle, time_resolution_events);
|
processEvents(events_of_cylcle, density_events);
|
||||||
DEBUG_LEVEL_3("Size of backup queue: " +
|
|
||||||
std::to_string(queues_[non_active_queue_].size()));
|
|
||||||
|
|
||||||
DEBUG_LEVEL_1("Leftover events: " + std::to_string(leftover_events));
|
DEBUG_LEVEL_1("Leftover events: " + std::to_string(leftover_events));
|
||||||
leftover_events -= events_of_cylcle;
|
leftover_events -= events_of_cylcle;
|
||||||
@@ -186,7 +177,6 @@ void Simulation::start() {
|
|||||||
// pair.second->lastSyncDeletionLogger()->resetDeletionLogger();
|
// pair.second->lastSyncDeletionLogger()->resetDeletionLogger();
|
||||||
}
|
}
|
||||||
leftover_events_time_resolution = time_resolution_this_cpu;
|
leftover_events_time_resolution = time_resolution_this_cpu;
|
||||||
switch_active(active_queue_, non_active_queue_);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -195,45 +185,57 @@ int particleNumberFromQueue(int queue_length, RandomNumbers* random) {
|
|||||||
return random->rollUniform(0, queue_length);
|
return random->rollUniform(0, queue_length);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Simulation::processEvents(int events, const int time_resolution_events) {
|
void Simulation::processEvents(int events, int density_events) {
|
||||||
DEBUG_LEVEL_0("Processing events.");
|
DEBUG_LEVEL_0("Processing events.");
|
||||||
int current_event = 0;
|
int current_event = 0;
|
||||||
while ((current_event < events) || (!queues_[active_queue_].empty())) {
|
int current_density_event = 0;
|
||||||
|
while ((current_event < events) || (!queue_.empty())) {
|
||||||
DEBUG_LEVEL_2("Current event: " + std::to_string(current_event));
|
DEBUG_LEVEL_2("Current event: " + std::to_string(current_event));
|
||||||
|
|
||||||
if (queues_[active_queue_].empty()) {
|
if (queue_.empty()) {
|
||||||
current_event += 1;
|
std::vector<particle::Particle> new_particles;
|
||||||
DEBUG_LEVEL_2("Inject particles to queue.");
|
if(current_density_event == density_events) {
|
||||||
SimulationTimer::eventStartPoint(std::string("ParticleInjection"));
|
current_event += 1;
|
||||||
std::vector<particle::Particle> new_particles =
|
DEBUG_LEVEL_2("Inject particles to queue.");
|
||||||
determineInjectedParticles(random_.get(), time_resolution_events);
|
SimulationTimer::eventStartPoint(std::string("ParticleInjection"));
|
||||||
|
new_particles = determineInjectedParticles(random_.get());
|
||||||
#if defined debug_level3
|
#if defined debug_level3
|
||||||
for(auto& particle : new_particles) {
|
for(auto& particle : new_particles) {
|
||||||
std::stringstream output_ss;
|
std::stringstream output_ss;
|
||||||
output_ss << particle.specie()->name() << " with velocity (cart): ";
|
output_ss << particle.specie()->name() << " with velocity (cart): ";
|
||||||
output_ss << particle.velocity().cartesianEntries();
|
output_ss << particle.velocity().cartesianEntries();
|
||||||
output_ss << " and position (cart)";
|
output_ss << " and position (cart)";
|
||||||
output_ss << particle.position().cartesianEntries();
|
output_ss << particle.position().cartesianEntries();
|
||||||
DEBUG_LEVEL_3(output_ss.str());
|
DEBUG_LEVEL_3(output_ss.str());
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
SimulationTimer::eventStopPoint(std::string("ParticleInjection"));
|
SimulationTimer::eventStopPoint(std::string("ParticleInjection"));
|
||||||
DEBUG_LEVEL_2("Injected particles to queue.");
|
DEBUG_LEVEL_2("Injected particles to queue.");
|
||||||
|
|
||||||
queues_[active_queue_].insert(queues_[active_queue_].end(), new_particles.begin(),
|
} else {
|
||||||
|
current_density_event += 1;
|
||||||
|
DEBUG_LEVEL_2("Current density event: " +
|
||||||
|
std::to_string(current_density_event));
|
||||||
|
//maybe use a different event name?
|
||||||
|
SimulationTimer::eventStartPoint(std::string("DensityParticleInjection"));
|
||||||
|
new_particles = determineInjectedDensityParticles(random_.get(), density_events);
|
||||||
|
SimulationTimer::eventStopPoint(std::string("DensityParticleInjection"));
|
||||||
|
}
|
||||||
|
if(new_particles.empty())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
queue_.insert(queue_.end(), new_particles.begin(),
|
||||||
new_particles.end());
|
new_particles.end());
|
||||||
}
|
}
|
||||||
DEBUG_LEVEL_2("Particles in queue: " +
|
DEBUG_LEVEL_2("Particles in queue: " +
|
||||||
std::to_string(queues_[active_queue_].size()));
|
std::to_string(queue_.size()));
|
||||||
DEBUG_LEVEL_2("Particles in backup queue: " +
|
|
||||||
std::to_string(queues_[non_active_queue_].size()));
|
|
||||||
const int particle_number =
|
const int particle_number =
|
||||||
particleNumberFromQueue(queues_[active_queue_].size(), random_.get());
|
particleNumberFromQueue(queue_.size(), random_.get());
|
||||||
|
|
||||||
// extract particle here from queue, so that an extension of the queue does
|
// extract particle here from queue, so that an extension of the queue does
|
||||||
// not mess up the reference
|
// not mess up the reference
|
||||||
particle::Particle particle = queues_[active_queue_].at(particle_number);
|
particle::Particle particle = queue_.at(particle_number);
|
||||||
queues_[active_queue_].erase(queues_[active_queue_].begin() + particle_number);
|
queue_.erase(queue_.begin() + particle_number);
|
||||||
|
|
||||||
processUntilTermination(particle);
|
processUntilTermination(particle);
|
||||||
}
|
}
|
||||||
@@ -242,6 +244,7 @@ void Simulation::processEvents(int events, const int time_resolution_events) {
|
|||||||
void Simulation::processUntilTermination(
|
void Simulation::processUntilTermination(
|
||||||
particle::Particle& particle) {
|
particle::Particle& particle) {
|
||||||
DEBUG_LEVEL_3("Process particle until termination");
|
DEBUG_LEVEL_3("Process particle until termination");
|
||||||
|
DEBUG_LEVEL_3("Weight of the particle: " + std::to_string(particle.weight()));
|
||||||
while (particle.specie()->terminator()->isAlive(&particle)) {
|
while (particle.specie()->terminator()->isAlive(&particle)) {
|
||||||
auto old_position = particle.position();
|
auto old_position = particle.position();
|
||||||
double old_energy = particle.kineticEnergy_eV();
|
double old_energy = particle.kineticEnergy_eV();
|
||||||
@@ -267,9 +270,8 @@ void Simulation::processUntilTermination(
|
|||||||
|
|
||||||
if (particle.specie()->terminator()->reachedMaxTimeAlive(
|
if (particle.specie()->terminator()->reachedMaxTimeAlive(
|
||||||
&particle)) {
|
&particle)) {
|
||||||
DEBUG_LEVEL_3("Move particle " + particle.specie()->name()
|
DEBUG_LEVEL_3("Terminate particle " + particle.specie()->name() +
|
||||||
+" to backup queue.");
|
". Reached timeresolution.");
|
||||||
queues_[non_active_queue_].push_back(particle);
|
|
||||||
break;
|
break;
|
||||||
} else if (particle.specie()->terminator()->leftSimulationDomain(
|
} else if (particle.specie()->terminator()->leftSimulationDomain(
|
||||||
&particle)) {
|
&particle)) {
|
||||||
@@ -308,7 +310,7 @@ void Simulation::processUntilTermination(
|
|||||||
particle.increaseInteractionCounter();
|
particle.increaseInteractionCounter();
|
||||||
|
|
||||||
for (auto& new_particle : output.newParticles()) {
|
for (auto& new_particle : output.newParticles()) {
|
||||||
queues_[active_queue_].push_back(new_particle);
|
queue_.push_back(new_particle);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (output.deleteCurrentParticle()) {
|
if (output.deleteCurrentParticle()) {
|
||||||
@@ -377,10 +379,33 @@ interaction::InteractionOutput Simulation::interact(
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::vector<particle::Particle> Simulation::determineInjectedParticles(
|
std::vector<particle::Particle> Simulation::determineInjectedParticles(
|
||||||
RandomNumbers* random, const int time_resolution_events) {
|
RandomNumbers* random) {
|
||||||
// timer_->increaseTimestep(injector_->timePassed());
|
// timer_->increaseTimestep(injector_->timePassed());
|
||||||
return injector_->rollNewParticles(random->engine(),
|
return injector_->rollNewParticles(random->engine(), timer_->time());
|
||||||
timer_->time(), time_resolution_events);
|
}
|
||||||
|
|
||||||
|
std::vector<particle::Particle> Simulation::determineInjectedDensityParticles(
|
||||||
|
RandomNumbers* random, const int numberParticlesToDraw) {
|
||||||
|
std::vector<particle::Particle> new_particles{};
|
||||||
|
if(!new_particles.empty()) {
|
||||||
|
ERROR("In drawing particles from the density: the new particles vector is not empty at initialization");
|
||||||
|
}
|
||||||
|
const double injection_time = timer_->time() - timer_->getDeltaT();
|
||||||
|
for(auto& pair: tracked_species_) {
|
||||||
|
const double particleNumberDensity =
|
||||||
|
pair.second->dynamicDensityField()->particleNumber();
|
||||||
|
if (particleNumberDensity == 0) {
|
||||||
|
DEBUG_LEVEL_3("No particles in density of species: " + pair.second->name());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
DEBUG_LEVEL_3("particle number " + pair.second->name() + ": " + std::to_string(particleNumberDensity));
|
||||||
|
injector_->rollNewParticleFromDensityField(random->engine(),
|
||||||
|
pair.second,
|
||||||
|
new_particles,
|
||||||
|
numberParticlesToDraw/particleNumberDensity,
|
||||||
|
injection_time);
|
||||||
|
}
|
||||||
|
return new_particles;
|
||||||
}
|
}
|
||||||
|
|
||||||
//maybe put that into the poisson solver
|
//maybe put that into the poisson solver
|
||||||
|
|||||||
Reference in New Issue
Block a user