Bin based drawing implemented (debug output not
yet removed)
This commit is contained in:
@@ -48,6 +48,8 @@ class DynamicDensityField : public DensityField {
|
|||||||
|
|
||||||
// return the particle number stored in the density of this object
|
// return the particle number stored in the density of this object
|
||||||
double particleNumber();
|
double particleNumber();
|
||||||
|
// return the particle number at the given index
|
||||||
|
double particleNumber(blitz::TinyVector<int, 5> index);
|
||||||
|
|
||||||
// returns true if the old pitch angle and the new pitch angle are in different bins
|
// returns true if the old pitch angle and the new pitch angle are in different bins
|
||||||
// of the pitch angle binning
|
// of the pitch angle binning
|
||||||
|
|||||||
@@ -46,6 +46,12 @@ class PositionDistributionFromDensity {
|
|||||||
|
|
||||||
// draw a position from the density provided at construction using the rejection method
|
// draw a position from the density provided at construction using the rejection method
|
||||||
CylinderCoordinate drawPosition(std::mt19937* engine);
|
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
|
// sets the maximum density value needed for the rejection method
|
||||||
// should not be called frequently, since during each call the density field is
|
// should not be called frequently, since during each call the density field is
|
||||||
// recalculated from scratch by the DynamicDensityField object
|
// recalculated from scratch by the DynamicDensityField object
|
||||||
|
|||||||
@@ -46,14 +46,14 @@ class Injector {
|
|||||||
// the new particles are then appened to the vector queue
|
// the new particles are then appened to the vector queue
|
||||||
// the magnetic field is necessary for the determination of the direction of the
|
// the magnetic field is necessary for the determination of the direction of the
|
||||||
// injected particles
|
// injected particles
|
||||||
// the weight scales the time a particle contributes to a certain bin
|
// 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
|
// and the injection time specifies the time the particle is injected (has to be
|
||||||
// between T, T+\DeltaT)
|
// between T, T+\DeltaT)
|
||||||
void drawNewParticleFromDensityField(std::mt19937* engine,
|
void drawNewParticleFromDensityField(std::mt19937* engine,
|
||||||
std::shared_ptr<species::TrackedSpecie> specie,
|
std::shared_ptr<species::TrackedSpecie> specie,
|
||||||
std::vector<particle::Particle>& queue,
|
std::vector<particle::Particle>& queue,
|
||||||
std::shared_ptr<MagneticField> field,
|
std::shared_ptr<MagneticField> field,
|
||||||
const double weight,
|
const double weight_factor,
|
||||||
const double injection_time);
|
const double injection_time);
|
||||||
|
|
||||||
void initInjectionTimeDistribution(double delta_t);
|
void initInjectionTimeDistribution(double delta_t);
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
|
||||||
|
#include "blitz/array.h"
|
||||||
|
|
||||||
class RandomNumbers {
|
class RandomNumbers {
|
||||||
public:
|
public:
|
||||||
RandomNumbers(int seed);
|
RandomNumbers(int seed);
|
||||||
@@ -19,4 +21,31 @@ class RandomNumbers {
|
|||||||
std::uniform_real_distribution<double> real_distribution_;
|
std::uniform_real_distribution<double> real_distribution_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// class to handle a random walk through a density field, where every
|
||||||
|
// bin is visited exactly once
|
||||||
|
class RandomIndex {
|
||||||
|
public:
|
||||||
|
// num_bins: contains the number of bins of all dimensions of a
|
||||||
|
// density binning (order: radial, azimuthal, longitudinal,
|
||||||
|
// pitch angle, energy)
|
||||||
|
RandomIndex(blitz::TinyVector<int, 5> num_bins);
|
||||||
|
|
||||||
|
// get the next index
|
||||||
|
blitz::TinyVector<int, 5> index();
|
||||||
|
|
||||||
|
// check if all bins have been visited
|
||||||
|
bool visited_all();
|
||||||
|
|
||||||
|
// create a new random order and reset the check if all bins have been visited
|
||||||
|
void reshuffle(std::mt19937* engine);
|
||||||
|
|
||||||
|
private:
|
||||||
|
const blitz::TinyVector<int, 5> num_bins_;
|
||||||
|
|
||||||
|
std::vector<size_t> one_dimensional_indices_;
|
||||||
|
|
||||||
|
size_t total_bin_number_;
|
||||||
|
size_t current_index_in_vector_;
|
||||||
|
};
|
||||||
|
|
||||||
#endif /* RANDOM_NUMBERS_H */
|
#endif /* RANDOM_NUMBERS_H */
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ class Simulation {
|
|||||||
// at the 0th timestep
|
// at the 0th timestep
|
||||||
void processEvents(int events, const double beta_particle_weight);
|
void processEvents(int events, const double beta_particle_weight);
|
||||||
|
|
||||||
void processDensityEvents(int events, const double weight_factor);
|
void processDensityEvents(int particles_per_bin, const double weight_factor);
|
||||||
void processUntilTermination(particle::Particle& particle);
|
void processUntilTermination(particle::Particle& particle);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -48,7 +48,8 @@ class Simulation {
|
|||||||
RandomNumbers* random,
|
RandomNumbers* random,
|
||||||
const double weight_factor,
|
const double weight_factor,
|
||||||
const int current,
|
const int current,
|
||||||
std::map<std::string, double>& particleNumbers);
|
std::map<std::string, double>& particleNumbers,
|
||||||
|
bool& all_bins_done);
|
||||||
|
|
||||||
double meanFreePath(particle::Particle& particle, RandomNumbers* random,
|
double meanFreePath(particle::Particle& particle, RandomNumbers* random,
|
||||||
std::vector<double>& macroscopic_cross_sections_m);
|
std::vector<double>& macroscopic_cross_sections_m);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include "simulation/parameter.h"
|
#include "simulation/parameter.h"
|
||||||
|
|
||||||
|
class RandomIndex;
|
||||||
class Mover;
|
class Mover;
|
||||||
class Terminator;
|
class Terminator;
|
||||||
class Reflector;
|
class Reflector;
|
||||||
@@ -91,6 +92,9 @@ class TrackedSpecie : public Specie {
|
|||||||
// getter for the raw pointer to the VelocityDistributionFromDensityField object
|
// getter for the raw pointer to the VelocityDistributionFromDensityField object
|
||||||
VelocityDistributionFromDensityField* velocityDistributionFromDensityField();
|
VelocityDistributionFromDensityField* velocityDistributionFromDensityField();
|
||||||
|
|
||||||
|
// getter for the raw pointer to the RandomIndex object
|
||||||
|
RandomIndex* randomIndex();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<DynamicDensityField> density_field_;
|
std::shared_ptr<DynamicDensityField> density_field_;
|
||||||
std::shared_ptr<VelocityDistribution> velocity_distribution_;
|
std::shared_ptr<VelocityDistribution> velocity_distribution_;
|
||||||
@@ -109,6 +113,8 @@ class TrackedSpecie : public Specie {
|
|||||||
position_distribution_from_density_;
|
position_distribution_from_density_;
|
||||||
std::shared_ptr<VelocityDistributionFromDensityField>
|
std::shared_ptr<VelocityDistributionFromDensityField>
|
||||||
velocity_distribution_from_density_;
|
velocity_distribution_from_density_;
|
||||||
|
|
||||||
|
std::shared_ptr<RandomIndex> random_index_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class BackgroundSpecie : public Specie {
|
class BackgroundSpecie : public Specie {
|
||||||
|
|||||||
@@ -55,6 +55,15 @@ class VelocityDistributionFromDensityField : public VelocityDistribution {
|
|||||||
CartesianVector rollVelocity(
|
CartesianVector rollVelocity(
|
||||||
const Coordinate& position, std::mt19937* engine,
|
const Coordinate& position, std::mt19937* engine,
|
||||||
std::shared_ptr<MagneticField> magnetic_field);
|
std::shared_ptr<MagneticField> magnetic_field);
|
||||||
|
|
||||||
|
// return a velocity vector drawn uniformly in the bin specifed by index
|
||||||
|
CartesianVector drawVelocity(const blitz::TinyVector<int, 5> index,
|
||||||
|
std::mt19937* engine);
|
||||||
|
CartesianVector drawVelocity(const blitz::TinyVector<int, 5> index,
|
||||||
|
std::mt19937* engine,
|
||||||
|
const Coordinate& position,
|
||||||
|
std::shared_ptr<MagneticField> magnetic_field);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// mass of the species of which the density field is a part of
|
// mass of the species of which the density field is a part of
|
||||||
const double mass_kg_;
|
const double mass_kg_;
|
||||||
|
|||||||
@@ -307,6 +307,10 @@ double DynamicDensityField::particleNumber() {
|
|||||||
return particleNumber;
|
return particleNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double DynamicDensityField::particleNumber(blitz::TinyVector<int, 5> index) {
|
||||||
|
return time_in_bin_s_(index);
|
||||||
|
}
|
||||||
|
|
||||||
blitz::Array<double, 1> DynamicDensityField::
|
blitz::Array<double, 1> DynamicDensityField::
|
||||||
sliceSpectrumArray(const Coordinate& position) const {
|
sliceSpectrumArray(const Coordinate& position) const {
|
||||||
blitz::TinyVector<int, 3> position_bin =
|
blitz::TinyVector<int, 3> position_bin =
|
||||||
|
|||||||
@@ -88,3 +88,26 @@ CylinderCoordinate PositionDistributionFromDensity::drawPosition(std::mt19937* e
|
|||||||
return CylinderCoordinate(
|
return CylinderCoordinate(
|
||||||
blitz::TinyVector<double, 3>(random_r, random_phi, random_z));
|
blitz::TinyVector<double, 3>(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 = density_values(*engine);
|
||||||
|
phi_rndm = density_values(*engine);
|
||||||
|
z_rndm = density_values(*engine);
|
||||||
|
|
||||||
|
return CylinderCoordinate(blitz::TinyVector<double, 3>
|
||||||
|
(left_bin_border_radial + stepsize(0)*r_rndm,
|
||||||
|
left_bin_border_azimuthal + stepsize(1) * phi_rndm,
|
||||||
|
left_bin_border_longitudinal + stepsize(2) * z_rndm));
|
||||||
|
}
|
||||||
+24
-10
@@ -9,6 +9,7 @@
|
|||||||
#include "simulation/density_field.h"
|
#include "simulation/density_field.h"
|
||||||
#include "simulation/velocity_distribution.h"
|
#include "simulation/velocity_distribution.h"
|
||||||
#include "simulation/electromagnetic_field.h"
|
#include "simulation/electromagnetic_field.h"
|
||||||
|
#include "simulation/random_numbers.h"
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
Injector::Injector(
|
Injector::Injector(
|
||||||
@@ -87,18 +88,27 @@ void Injector::drawNewParticleFromDensityField(std::mt19937* engine,
|
|||||||
std::shared_ptr<species::TrackedSpecie> specie,
|
std::shared_ptr<species::TrackedSpecie> specie,
|
||||||
std::vector<particle::Particle>& queue,
|
std::vector<particle::Particle>& queue,
|
||||||
std::shared_ptr<MagneticField> magnetic_field,
|
std::shared_ptr<MagneticField> magnetic_field,
|
||||||
const double weight,
|
const double weight_factor,
|
||||||
const double injection_time) {
|
const double injection_time) {
|
||||||
// draw the position
|
blitz::TinyVector<int, 5> index = specie->randomIndex()->index();
|
||||||
CylinderCoordinate position =
|
|
||||||
specie->positionDistributionFromDensity()->drawPosition(engine);
|
|
||||||
|
|
||||||
// check if density was empty (i.e. phi value lower than 0)
|
std::stringstream out;
|
||||||
if (position.cylinderEntries()(1) < 0) {
|
out << "drawn index: " << index;
|
||||||
DEBUG_LEVEL_3(specie->name() + ": has zero density");
|
// DEBUG_LEVEL_1(out.str());
|
||||||
|
const double num_part_in_bin = specie->densityForDrawing()->particleNumber(index);
|
||||||
|
out << "\nnumber of particles (" << specie->name() << ") at index: " << num_part_in_bin;
|
||||||
|
|
||||||
|
if(num_part_in_bin == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// draw the position
|
||||||
|
CylinderCoordinate position =
|
||||||
|
specie->positionDistributionFromDensity()->drawPosition(engine,
|
||||||
|
index(0), index(1), index(2));
|
||||||
|
|
||||||
|
out << "\ndrawn position: " << position.cylinderEntries();
|
||||||
|
|
||||||
DEBUG_LEVEL_3("Injected z position: " +
|
DEBUG_LEVEL_3("Injected z position: " +
|
||||||
std::to_string(position.cylinderEntries()(2)));
|
std::to_string(position.cylinderEntries()(2)));
|
||||||
DEBUG_LEVEL_3("Injected r position: " +
|
DEBUG_LEVEL_3("Injected r position: " +
|
||||||
@@ -112,16 +122,20 @@ void Injector::drawNewParticleFromDensityField(std::mt19937* engine,
|
|||||||
if(kinematic::norm(magnetic_field->value(position).
|
if(kinematic::norm(magnetic_field->value(position).
|
||||||
cartesianEntries(position)) == 0) {
|
cartesianEntries(position)) == 0) {
|
||||||
velocity = specie->velocityDistributionFromDensityField()->
|
velocity = specie->velocityDistributionFromDensityField()->
|
||||||
rollVelocity(position, engine);
|
drawVelocity(index, engine);
|
||||||
} else {
|
} else {
|
||||||
velocity = specie->velocityDistributionFromDensityField()->
|
velocity = specie->velocityDistributionFromDensityField()->
|
||||||
rollVelocity(position, engine, magnetic_field);
|
drawVelocity(index, engine, position, magnetic_field);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
out << "\ndrawn velocity: " << velocity.cartesianEntries();
|
||||||
|
DEBUG_LEVEL_2(out.str());
|
||||||
|
|
||||||
particle::Particle drawn_particle(specie, position,
|
particle::Particle drawn_particle(specie, position,
|
||||||
velocity, injection_time, weight);
|
velocity, injection_time, weight_factor*num_part_in_bin);
|
||||||
|
|
||||||
queue.push_back(drawn_particle);
|
queue.push_back(drawn_particle);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
double Injector::timePassed() const {
|
double Injector::timePassed() const {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "simulation/random_numbers.h"
|
#include "simulation/random_numbers.h"
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
RandomNumbers::RandomNumbers(int seed)
|
RandomNumbers::RandomNumbers(int seed)
|
||||||
: engine_(std::make_shared<std::mt19937>(seed)),
|
: engine_(std::make_shared<std::mt19937>(seed)),
|
||||||
@@ -15,3 +16,55 @@ double RandomNumbers::rollUniform(double min, double max) {
|
|||||||
std::mt19937* RandomNumbers::engine() {
|
std::mt19937* RandomNumbers::engine() {
|
||||||
return engine_.get();
|
return engine_.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// does NOT shuffle the indices!
|
||||||
|
RandomIndex::RandomIndex(blitz::TinyVector<int, 5> num_bins) :
|
||||||
|
num_bins_(num_bins){
|
||||||
|
|
||||||
|
total_bin_number_ = blitz::product(num_bins_);
|
||||||
|
|
||||||
|
one_dimensional_indices_.reserve(total_bin_number_);
|
||||||
|
|
||||||
|
for(size_t i = 0; i < total_bin_number_; ++i) {
|
||||||
|
one_dimensional_indices_.push_back(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
current_index_in_vector_ = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RandomIndex::reshuffle(std::mt19937* engine) {
|
||||||
|
current_index_in_vector_ = 0;
|
||||||
|
std::shuffle(std::begin(one_dimensional_indices_),
|
||||||
|
std::end(one_dimensional_indices_), *engine);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RandomIndex::visited_all() {
|
||||||
|
return current_index_in_vector_ == total_bin_number_ - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the next index in the shuffled index vector. Returns the last
|
||||||
|
// index if all bins have been visited. Reshuffling resets this.
|
||||||
|
blitz::TinyVector<int, 5> RandomIndex::index() {
|
||||||
|
size_t tmp_one_dimensional_idx = one_dimensional_indices_[current_index_in_vector_];
|
||||||
|
|
||||||
|
blitz::TinyVector<int, 5> return_index{0,0,0,0,0};
|
||||||
|
|
||||||
|
int tmp = 0;
|
||||||
|
|
||||||
|
size_t current_prod = total_bin_number_;
|
||||||
|
|
||||||
|
for(int d = 4; d >= 0; d--) {
|
||||||
|
current_prod /= num_bins_(d);
|
||||||
|
|
||||||
|
tmp = tmp_one_dimensional_idx / current_prod;
|
||||||
|
|
||||||
|
return_index(d) = tmp;
|
||||||
|
|
||||||
|
tmp_one_dimensional_idx -= tmp * current_prod;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!visited_all())
|
||||||
|
current_index_in_vector_++;
|
||||||
|
|
||||||
|
return return_index;
|
||||||
|
}
|
||||||
@@ -61,6 +61,10 @@ void Simulation::init(Parameter& parameter) {
|
|||||||
DEBUG_LEVEL_0("Init random numbers.");
|
DEBUG_LEVEL_0("Init random numbers.");
|
||||||
random_ = generator::buildRandomNumbers(parameter);
|
random_ = generator::buildRandomNumbers(parameter);
|
||||||
|
|
||||||
|
for(auto& tracked_specie : tracked_species_) {
|
||||||
|
tracked_specie.second->randomIndex()->reshuffle(random_->engine());
|
||||||
|
}
|
||||||
|
|
||||||
DEBUG_LEVEL_0("Init poisson solver.");
|
DEBUG_LEVEL_0("Init poisson solver.");
|
||||||
poisson_solver_ = generator::buildPoissonSolver(parameter);
|
poisson_solver_ = generator::buildPoissonSolver(parameter);
|
||||||
electric_field_->updateDynamicField(poisson_solver_->staticElectricField());
|
electric_field_->updateDynamicField(poisson_solver_->staticElectricField());
|
||||||
@@ -134,25 +138,26 @@ void Simulation::start() {
|
|||||||
static_cast<double>(beta_decay_events*MpiCommunication::mpiSize());
|
static_cast<double>(beta_decay_events*MpiCommunication::mpiSize());
|
||||||
|
|
||||||
// number of density events per timestep and CPU
|
// number of density events per timestep and CPU
|
||||||
const int density_events =
|
const int particles_per_bin =
|
||||||
parameter_.intValue(json_path({"Simulation", "DensityEventsPerCPU"}), 0);
|
parameter_.intValue(json_path({"Simulation",
|
||||||
|
"NumberDensityParticlesPerBinPerCPU"}), 0);
|
||||||
|
|
||||||
// calculate a 'stride' for the density events s.t. the same number of syncs
|
// calculate a 'stride' for the density events s.t. the same number of syncs
|
||||||
// would be performed
|
// would be performed
|
||||||
const int density_stride = std::ceil(density_events *
|
const int density_stride = std::ceil(particles_per_bin *
|
||||||
(static_cast<double>(event_stride)/beta_decay_events));
|
(static_cast<double>(event_stride)/beta_decay_events));
|
||||||
// calculate the scaling factor for the particle weight for particles
|
// calculate the scaling factor for the particle weight for particles
|
||||||
// drawn from the density
|
// drawn from the density
|
||||||
const double density_weight_factor =
|
const double density_weight_factor =
|
||||||
1./(MpiCommunication::mpiSize()*density_events);
|
1./(MpiCommunication::mpiSize()*particles_per_bin);
|
||||||
|
|
||||||
DEBUG_LEVEL_0("Density events per cpu: " + std::to_string(density_events));
|
DEBUG_LEVEL_0("Number of particles per bin per CPU: " + std::to_string(particles_per_bin));
|
||||||
|
|
||||||
SIM_INFO("Number of Timesteps: " + std::to_string(number_timesteps));
|
SIM_INFO("Number of Timesteps: " + std::to_string(number_timesteps));
|
||||||
SIM_INFO("Simulated events before cpu sync: " + std::to_string(event_stride));
|
SIM_INFO("Simulated events before cpu sync: " + std::to_string(event_stride));
|
||||||
|
|
||||||
int leftover_events_time_resolution = beta_decay_events;
|
int leftover_events_time_resolution = beta_decay_events;
|
||||||
int leftover_density_events = density_events;
|
int leftover_density_events = particles_per_bin;
|
||||||
|
|
||||||
const double delta_t = time_resolution_events*injector_->timePassed();
|
const double delta_t = time_resolution_events*injector_->timePassed();
|
||||||
|
|
||||||
@@ -203,7 +208,7 @@ void Simulation::start() {
|
|||||||
std::to_string(leftover_density_events));
|
std::to_string(leftover_density_events));
|
||||||
|
|
||||||
// process both density and beta events before a cpu synchronization
|
// process both density and beta events before a cpu synchronization
|
||||||
processDensityEvents(density_events_of_cycle, density_weight_factor);
|
processDensityEvents(particles_per_bin, density_weight_factor);
|
||||||
processEvents(events_of_cycle, beta_particle_weight);
|
processEvents(events_of_cycle, beta_particle_weight);
|
||||||
|
|
||||||
DEBUG_LEVEL_1("Processing timestep: " + std::to_string(timesteps));
|
DEBUG_LEVEL_1("Processing timestep: " + std::to_string(timesteps));
|
||||||
@@ -255,7 +260,7 @@ void Simulation::start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
leftover_events_time_resolution = beta_decay_events;
|
leftover_events_time_resolution = beta_decay_events;
|
||||||
leftover_density_events = density_events;
|
leftover_density_events = particles_per_bin;
|
||||||
|
|
||||||
timer_->increaseTimestep(delta_t);
|
timer_->increaseTimestep(delta_t);
|
||||||
timesteps++;
|
timesteps++;
|
||||||
@@ -318,9 +323,12 @@ void Simulation::processEvents(int events, const double beta_particle_weight) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Simulation::processDensityEvents(int events, const double weight_factor) {
|
void Simulation::processDensityEvents(int particles_per_bin, const double weight_factor) {
|
||||||
DEBUG_LEVEL_0("Processing density events.");
|
DEBUG_LEVEL_0("Processing density events.");
|
||||||
|
// number of full grid traversions
|
||||||
int current_event = 0;
|
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
|
// calculate the number of particles currently contained within the densities
|
||||||
// of the different tracked species before injection in order to avoid
|
// of the different tracked species before injection in order to avoid
|
||||||
@@ -331,7 +339,7 @@ void Simulation::processDensityEvents(int events, const double weight_factor) {
|
|||||||
particleNumbers[pair.first] = (pair.second->densityForDrawing()->particleNumber());
|
particleNumbers[pair.first] = (pair.second->densityForDrawing()->particleNumber());
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((current_event < events) || (!queue_.empty())) {
|
while ((current_event < particles_per_bin) || (!queue_.empty())) {
|
||||||
DEBUG_LEVEL_2("Current event: " + std::to_string(current_event));
|
DEBUG_LEVEL_2("Current event: " + std::to_string(current_event));
|
||||||
|
|
||||||
if (queue_.empty()) {
|
if (queue_.empty()) {
|
||||||
@@ -341,7 +349,8 @@ void Simulation::processDensityEvents(int events, const double weight_factor) {
|
|||||||
|
|
||||||
SimulationTimer::eventStartPoint(std::string("DensityInjection"));
|
SimulationTimer::eventStartPoint(std::string("DensityInjection"));
|
||||||
new_particles = determineInjectedDensityParticles(random_.get(),
|
new_particles = determineInjectedDensityParticles(random_.get(),
|
||||||
weight_factor, current_event, particleNumbers);
|
weight_factor, current_event, particleNumbers,
|
||||||
|
all_bins_done);
|
||||||
SimulationTimer::eventStopPoint(std::string("DensityInjection"));
|
SimulationTimer::eventStopPoint(std::string("DensityInjection"));
|
||||||
|
|
||||||
#if defined debug_level3
|
#if defined debug_level3
|
||||||
@@ -357,17 +366,25 @@ void Simulation::processDensityEvents(int events, const double weight_factor) {
|
|||||||
// if no new particles could be injected from the density, it is not possible
|
// if no new particles could be injected from the density, it is not possible
|
||||||
// that during another iteration cycle new particles will be injected
|
// that during another iteration cycle new particles will be injected
|
||||||
// (drawing field only changes after timesteps)
|
// (drawing field only changes after timesteps)
|
||||||
if(new_particles.empty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
current_event += 1;
|
|
||||||
|
|
||||||
queue_.insert(queue_.end(), new_particles.begin(), new_particles.end());
|
if(all_bins_done) {
|
||||||
|
current_event += 1;
|
||||||
|
for(auto& element : tracked_species_) {
|
||||||
|
element.second->randomIndex()->reshuffle(random_->engine());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!new_particles.empty()) {
|
||||||
|
queue_.insert(queue_.end(), new_particles.begin(), new_particles.end());
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
DEBUG_LEVEL_2("Particles in queue: " +
|
DEBUG_LEVEL_2("Particles in queue: " +
|
||||||
std::to_string(queue_.size()));
|
std::to_string(queue_.size()));
|
||||||
const int particle_number =
|
const int particle_number =
|
||||||
particleNumberFromQueue(queue_.size(), random_.get());
|
particleNumberFromQueue(queue_.size(), random_.get());
|
||||||
|
|
||||||
// extract particle from queue here, so that an extension of the queue does
|
// extract particle from queue here, so that an extension of the queue does
|
||||||
// not mess up the reference
|
// not mess up the reference
|
||||||
@@ -573,7 +590,8 @@ std::vector<particle::Particle> Simulation::determineInjectedParticles(
|
|||||||
// weight_factor has to be such that particleNumber * weight_factor = weight
|
// weight_factor has to be such that particleNumber * weight_factor = weight
|
||||||
std::vector<particle::Particle> Simulation::determineInjectedDensityParticles(
|
std::vector<particle::Particle> Simulation::determineInjectedDensityParticles(
|
||||||
RandomNumbers* random, const double weight_factor, const int current,
|
RandomNumbers* random, const double weight_factor, const int current,
|
||||||
std::map<std::string, double>& particleNumbers) {
|
std::map<std::string, double>& particleNumbers, bool& all_bins_done) {
|
||||||
|
|
||||||
std::vector<particle::Particle> new_particles{};
|
std::vector<particle::Particle> new_particles{};
|
||||||
|
|
||||||
if(!new_particles.empty()) {
|
if(!new_particles.empty()) {
|
||||||
@@ -583,8 +601,15 @@ std::vector<particle::Particle> Simulation::determineInjectedDensityParticles(
|
|||||||
|
|
||||||
const double injection_time = timer_->time() - timer_->getDeltaT();
|
const double injection_time = timer_->time() - timer_->getDeltaT();
|
||||||
|
|
||||||
|
bool tmp_done = false;
|
||||||
|
all_bins_done = true;
|
||||||
|
|
||||||
for(auto& pair: tracked_species_) {
|
for(auto& pair: tracked_species_) {
|
||||||
|
|
||||||
|
tmp_done = pair.second->randomIndex()->visited_all();
|
||||||
|
|
||||||
if (particleNumbers[pair.first] == 0) {
|
if (particleNumbers[pair.first] == 0) {
|
||||||
|
all_bins_done = all_bins_done && true;
|
||||||
DEBUG_LEVEL_3("No particles in density of species: " + pair.second->name());
|
DEBUG_LEVEL_3("No particles in density of species: " + pair.second->name());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -599,11 +624,14 @@ std::vector<particle::Particle> Simulation::determineInjectedDensityParticles(
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
double weight = particleNumbers[pair.first]*weight_factor;
|
if(!tmp_done) {
|
||||||
|
injector_->drawNewParticleFromDensityField(random->engine(),
|
||||||
injector_->drawNewParticleFromDensityField(random->engine(),
|
|
||||||
pair.second, new_particles, magnetic_field_,
|
pair.second, new_particles, magnetic_field_,
|
||||||
weight, injection_time);
|
weight_factor, injection_time);
|
||||||
|
}
|
||||||
|
|
||||||
|
// if species is not done tmp_done && ... = false
|
||||||
|
all_bins_done = all_bins_done && tmp_done;
|
||||||
}
|
}
|
||||||
return new_particles;
|
return new_particles;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
#include "simulation/terminator.h"
|
#include "simulation/terminator.h"
|
||||||
#include "simulation/velocity_distribution.h"
|
#include "simulation/velocity_distribution.h"
|
||||||
#include "simulation/event_position_distribution.h"
|
#include "simulation/event_position_distribution.h"
|
||||||
|
#include "simulation/random_numbers.h"
|
||||||
|
|
||||||
namespace species {
|
namespace species {
|
||||||
|
|
||||||
@@ -58,7 +59,20 @@ TrackedSpecie::TrackedSpecie(
|
|||||||
std::make_shared<PositionDistributionFromDensity>(density_for_drawing)),
|
std::make_shared<PositionDistributionFromDensity>(density_for_drawing)),
|
||||||
velocity_distribution_from_density_(
|
velocity_distribution_from_density_(
|
||||||
std::make_shared<VelocityDistributionFromDensityField>(
|
std::make_shared<VelocityDistributionFromDensityField>(
|
||||||
density_for_drawing, mass_kg)) {}
|
density_for_drawing, mass_kg)) {
|
||||||
|
blitz::TinyVector<int, 5> 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();
|
||||||
|
num_bins(4) = density_field->energyBinning()->numBins();
|
||||||
|
|
||||||
|
random_index_ = std::make_shared<RandomIndex> (RandomIndex(num_bins));
|
||||||
|
}
|
||||||
|
|
||||||
|
RandomIndex* TrackedSpecie::randomIndex() {
|
||||||
|
return random_index_.get();
|
||||||
|
}
|
||||||
|
|
||||||
DensityField* TrackedSpecie::densityField() {
|
DensityField* TrackedSpecie::densityField() {
|
||||||
return density_field_.get();
|
return density_field_.get();
|
||||||
|
|||||||
@@ -95,6 +95,7 @@ CartesianVector VelocityDistributionFromDensityField::rollVelocity(
|
|||||||
return CartesianVector(v_direction * vMagnitude);
|
return CartesianVector(v_direction * vMagnitude);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// can NOT be removed, needed for drawing of velocity of interaction partners
|
||||||
CartesianVector VelocityDistributionFromDensityField::rollVelocity(
|
CartesianVector VelocityDistributionFromDensityField::rollVelocity(
|
||||||
const Coordinate& position, std::mt19937* engine,
|
const Coordinate& position, std::mt19937* engine,
|
||||||
std::shared_ptr<MagneticField> magnetic_field) {
|
std::shared_ptr<MagneticField> magnetic_field) {
|
||||||
@@ -159,3 +160,105 @@ CartesianVector VelocityDistributionFromDensityField::rollVelocity(
|
|||||||
|
|
||||||
return CartesianVector(v_direction * vMagnitude);
|
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<int, 5> 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<double, 3> zDirection(0,0,1);
|
||||||
|
|
||||||
|
const double vMagnitude = kinematic::velocity(energy_eV, mass_kg_);
|
||||||
|
|
||||||
|
// vector perpendicular to e_z
|
||||||
|
blitz::TinyVector<double, 3> v_perp(1,0,0);
|
||||||
|
|
||||||
|
blitz::TinyVector<double, 3> 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<int, 5> index, std::mt19937* engine,
|
||||||
|
const Coordinate& position,
|
||||||
|
std::shared_ptr<MagneticField> 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 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 << "\nenergy values: " << energyValues;
|
||||||
|
// out << "\npitch angle values: " << pitchAngleValues;
|
||||||
|
out << "\nenergy bin borders: [ ";
|
||||||
|
for(auto element : energyBinBorders_) {
|
||||||
|
out << element << " ";
|
||||||
|
}
|
||||||
|
out << "]";
|
||||||
|
|
||||||
|
ERROR(out.str());
|
||||||
|
}
|
||||||
|
|
||||||
|
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<double, 3> 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<double, 3> v_perp =
|
||||||
|
perpendicularNormalVector(cartesianB_values);
|
||||||
|
|
||||||
|
blitz::TinyVector<double, 3> v_direction = pitchAngle
|
||||||
|
* cartesianB_values/Bmagnitude
|
||||||
|
+ std::sqrt(1 - pitchAngle*pitchAngle)
|
||||||
|
* kinematic::rotateVector(v_perp, cartesianB_values, theta);
|
||||||
|
|
||||||
|
return CartesianVector(v_direction * vMagnitude);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user