Author SHA1 Message Date
csendlinger 88dcafee03 fix mu=-1 bug 2025-03-18 12:15:24 +01:00
csendlinger 51877687b1 Energy split drawing implemented;
Debugging output still present, energy split hard coded
2025-02-28 17:13:34 +01:00
11 changed files with 487 additions and 23 deletions
+9 -2
View File
@@ -31,6 +31,7 @@ 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 density(const Coordinate& position, double energy_split, bool above);
bool changedPositionBin(const Coordinate& old_position, bool changedPositionBin(const Coordinate& old_position,
const Coordinate& new_position) const; const Coordinate& new_position) const;
@@ -48,6 +49,11 @@ 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 stored for energies higher than E_thresh
// (above = true) or for energies below (above = false)
// The energy bin, in which E_thresh is located, will be included in the
// 'above=true' case (and excluded in the other case)
double particleNumber(double E_thresh, bool above);
// 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
@@ -59,9 +65,10 @@ class DynamicDensityField : public DensityField {
blitz::Array<long int, 5> deletedParticlesArray() const; blitz::Array<long int, 5> deletedParticlesArray() const;
blitz::Array<double, 3> binVolumeArray() const; blitz::Array<double, 3> binVolumeArray() const;
blitz::Array<double, 3> densityArray(double energy_split, bool above);
// return an array of the density values at the Coordinate position (only energy // return an array of the density values at the Coordinate position (only energy
// direction left); over and underflow bin are removed // direction left); overbin is removed
// (might not be good, since then the underflow particles are lost...)
blitz::Array<double, 1> sliceSpectrumArray(const Coordinate& position) const; blitz::Array<double, 1> sliceSpectrumArray(const Coordinate& position) const;
// return an array of the time in bin at the Coordinate position and the energy // return an array of the time in bin at the Coordinate position and the energy
@@ -46,15 +46,21 @@ 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);
CylinderCoordinate drawPosition(std::mt19937* engine, double energy_split, bool above);
// 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
void setMaximumDensity(); void setMaximumDensity();
void setMaximumDensityAboveAndBelow(double energy_split);
private: private:
// variable storing the maximum value of the density // variable storing the maximum value of the density
double maximum_density_ = 1.; double maximum_density_ = 1.;
double maximum_density_below_ = 1.;
double maximum_density_above_ = 1.;
std::shared_ptr<DynamicDensityField> density_; std::shared_ptr<DynamicDensityField> density_;
// distributions for the spatial directions and for the density // distributions for the spatial directions and for the density
+13
View File
@@ -60,9 +60,15 @@ class Injector {
double timePassed() const; double timePassed() const;
void fillParticleNumbers(std::shared_ptr<species::TrackedSpecie> specie);
double energySplit() const;
private: private:
std::vector<InjectorInfo> injector_infos_; std::vector<InjectorInfo> injector_infos_;
const double activity_; const double activity_;
double energySplit_;
std::map<std::string, std::vector<double>> particleNumbersMap_;
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_; std::uniform_real_distribution<double> injection_time_distribution_;
@@ -74,6 +80,13 @@ class Injector {
std::shared_ptr<VelocityDistribution> creation_velocity_distribution, std::shared_ptr<VelocityDistribution> creation_velocity_distribution,
const double time_birth, const double time_birth,
const double weight); const double weight);
particle::Particle drawNewDensityParticleAboveOrBelow(std::mt19937* engine,
std::shared_ptr<species::TrackedSpecie> specie,
std::shared_ptr<MagneticField> field,
const double weight,
const double injection_time,
const bool above);
}; };
#endif /* INJECTOR_H */ #endif /* INJECTOR_H */
@@ -55,6 +55,23 @@ 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);
// returns a velocity vector drawn from the energy and pitch angle distributions
// use the given magnetic field at the provided position to determine the
// direction of velocity from the pitch angle
// the direction of the perpendicular part of the velocity vector is drawn isotropically
// draw from the distribution below the threshold energy if above = false
// and from above otherwise
CartesianVector rollVelocity(
const Coordinate& position, std::mt19937* engine,
std::shared_ptr<MagneticField> magnetic_field,
const int E_th_bin,
const bool above);
CartesianVector rollVelocity(
const Coordinate& position, std::mt19937* engine,
const int E_th_bin,
const bool above);
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_;
+6
View File
@@ -10,6 +10,7 @@
#include "poisson_solver/poisson_solver.h" #include "poisson_solver/poisson_solver.h"
#include "simulation/timer.h" #include "simulation/timer.h"
#include "simulation/event_position_distribution.h" #include "simulation/event_position_distribution.h"
#include "constants/ionization_constants.h"
// Note: error handling is a bit messy. Sometimes it's dealt with in the // Note: error handling is a bit messy. Sometimes it's dealt with in the
// called reset function (e.g. in the density field) and sometimes it's // called reset function (e.g. in the density field) and sometimes it's
@@ -40,6 +41,11 @@ void Restart::restart(std::map<std::string, std::shared_ptr<species::TrackedSpec
restart_identifier_; restart_identifier_;
restartDrawingDensityField(pair.second->densityForDrawing(), path); restartDrawingDensityField(pair.second->densityForDrawing(), path);
pair.second->positionDistributionFromDensity()->setMaximumDensity(); pair.second->positionDistributionFromDensity()->setMaximumDensity();
// ist nicht gut so, weils von der injector version entkoppelt ist...
// muss noch irgendwie geändert werden
pair.second->positionDistributionFromDensity()
->setMaximumDensityAboveAndBelow(
ionization::ionization_energy_janev_atomic);
} }
} }
+93
View File
@@ -161,6 +161,36 @@ double DynamicDensityField::density(const Coordinate& position,
bin_volume_m3_(position_bin(0), position_bin(1), position_bin(2)); bin_volume_m3_(position_bin(0), position_bin(1), position_bin(2));
} }
double DynamicDensityField::density(const Coordinate& position,
double energy_split,
bool above) {
const blitz::TinyVector<int, 3> position_bin =
position_binning_.bin(position.cylinderEntries());
const int energy_split_bin = energy_binning_.bin(energy_split);
blitz::Range range;
if(above) {
range = blitz::Range(energy_split_bin, energy_binning_.binIndexMax());
} else {
if(energy_split_bin == energy_binning_.binIndexMin())
return 0;
range = blitz::Range(energy_binning_.binIndexMin(), energy_split_bin-1);
}
const double t = timer_->getDeltaT();
const double time_in_bin = blitz::sum(time_in_bin_s_(
position_bin(0), position_bin(1), position_bin(2),
blitz::Range::all(), range));
const long deleted_particles = blitz::sum(deleted_particles_(
position_bin(0), position_bin(1), position_bin(2),
blitz::Range::all(), range));
return (time_in_bin / t - deleted_particles) /
bin_volume_m3_(position_bin(0), position_bin(1), position_bin(2));
}
bool DynamicDensityField::changedPositionBin( bool DynamicDensityField::changedPositionBin(
const Coordinate& old_position, const Coordinate& old_position,
const Coordinate& new_position) const { const Coordinate& new_position) const {
@@ -242,6 +272,40 @@ blitz::Array<double, 3> DynamicDensityField::densityArray() {
return density.copy(); return density.copy();
} }
blitz::Array<double, 3> DynamicDensityField::densityArray(
double energy_split, bool above) {
DEBUG_LEVEL_3("recalc Density array");
recalculateDensityArray();
const int energy_split_bin = energy_binning_.bin(energy_split);
blitz::TinyVector<int,3> lowerbound(density_per_m3_.lbound(0),
density_per_m3_.lbound(1),
density_per_m3_.lbound(2));
blitz::TinyVector<int,3> extent(density_per_m3_.extent(0),
density_per_m3_.extent(1),
density_per_m3_.extent(2));
blitz::Array<double, 3> density(lowerbound, extent);
density = 0;
blitz::Range range;
if(above) {
range = blitz::Range(energy_split_bin, energy_binning_.binIndexMax());
} else {
if(energy_split_bin == energy_binning_.binIndexMin())
return density;
range = blitz::Range(energy_binning_.binIndexMin(), energy_split_bin-1);
}
blitz::Range all = blitz::Range::all();
blitz::fourthIndex iE;
DEBUG_LEVEL_3("density sum");
density = blitz::sum(density_per_m3_(all, all, all, range), iE);
return density.copy();
}
blitz::Array<double, 4> DynamicDensityField::spectrumArray() { blitz::Array<double, 4> DynamicDensityField::spectrumArray() {
recalculateDensityArray(); recalculateDensityArray();
return density_per_m3_; return density_per_m3_;
@@ -307,6 +371,35 @@ double DynamicDensityField::particleNumber() {
return particleNumber; return particleNumber;
} }
double DynamicDensityField::particleNumber(double E_thresh, bool above) {
const int energy_bin = energy_binning_.bin(E_thresh);
double time_in_bin = 0;
double deleted_particles = 0;
blitz::Range range;
if(above) {
range = blitz::Range(energy_bin, energy_binning_.binIndexMax());
} else {
if(energy_bin == energy_binning_.binIndexMin())
return 0;
range = blitz::Range(energy_binning_.binIndexMin(), energy_bin-1);
}
time_in_bin = blitz::sum(time_in_bin_s_(
blitz::Range::all(), blitz::Range::all(), blitz::Range::all(),
blitz::Range::all(), range));
deleted_particles = blitz::sum(deleted_particles_(
blitz::Range::all(), blitz::Range::all(), blitz::Range::all(),
blitz::Range::all(), range));
const double t = timer_->getDeltaT();
return (time_in_bin / t - deleted_particles);
}
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 =
+7
View File
@@ -70,6 +70,13 @@ int PitchAngleBinning::bin(double pitch_angle) const {
auto const it = std::lower_bound(pitch_angle_bin_borders_.begin(), auto const it = std::lower_bound(pitch_angle_bin_borders_.begin(),
pitch_angle_bin_borders_.end(), pitch_angle_bin_borders_.end(),
pitch_angle); pitch_angle);
// otherwise the valid mu-value of -1 is assigned to the underflow
// bin which can lead to a segfault in the drawing of the pitch
// angle in "velocity_distribution.cpp"
if(!(pitch_angle < pitch_angle_bin_borders_.front()) &&
(it == pitch_angle_bin_borders_.begin())) {
return (it - pitch_angle_bin_borders_.begin());
}
return (it - pitch_angle_bin_borders_.begin()) - 1; return (it - pitch_angle_bin_borders_.begin()) - 1;
} }
+45 -1
View File
@@ -1,4 +1,5 @@
#include "simulation/event_position_distribution.h" #include "simulation/event_position_distribution.h"
#include "simulation/errors.h"
EventPositionDistribution::EventPositionDistribution( EventPositionDistribution::EventPositionDistribution(
std::shared_ptr<ScalarField> mother_density, std::shared_ptr<ScalarField> mother_density,
@@ -67,6 +68,14 @@ void PositionDistributionFromDensity::setMaximumDensity() {
maximum_density_ = blitz::max(density_->densityArray()); maximum_density_ = blitz::max(density_->densityArray());
} }
void PositionDistributionFromDensity::setMaximumDensityAboveAndBelow(
double energy_split) {
maximum_density_below_ = blitz::max(
density_->densityArray(energy_split, false));
maximum_density_above_ = blitz::max(
density_->densityArray(energy_split, true));
}
CylinderCoordinate PositionDistributionFromDensity::drawPosition(std::mt19937* engine) { CylinderCoordinate PositionDistributionFromDensity::drawPosition(std::mt19937* engine) {
if(isEqual(maximum_density_, 0.)) { if(isEqual(maximum_density_, 0.)) {
// abuse the phi coordinate as a check if the density was zero // abuse the phi coordinate as a check if the density was zero
@@ -80,10 +89,45 @@ CylinderCoordinate PositionDistributionFromDensity::drawPosition(std::mt19937* e
random_r = std::sqrt(r_values(*engine)); random_r = std::sqrt(r_values(*engine));
random_phi = phi_values(*engine); random_phi = phi_values(*engine);
random_z = z_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(...) // might be bad that the value gets recalculated for each call of density_->density(...)
} while (random_n*maximum_density_ >= density_->density(CylinderCoordinate( } while (random_n*maximum_density_ > density_->density(CylinderCoordinate(
blitz::TinyVector<double,3>(random_r, random_phi, random_z)))); blitz::TinyVector<double,3>(random_r, random_phi, random_z))));
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, double energy_split, bool above) {
const double max_density = above ? maximum_density_above_ : maximum_density_below_;
if(isEqual(max_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<double,3>(0,-1,0));
}
double random_r, random_phi, random_z, random_n;
double density_at_position;
// std::stringstream out;
// above ? out << "Above: " : out << "Below: ";
// out << "densities: ";
do {
random_r = std::sqrt(r_values(*engine));
random_phi = phi_values(*engine);
random_z = z_values(*engine);
random_n = density_values(*engine);
density_at_position = density_->density(CylinderCoordinate(
blitz::TinyVector<double,3>(random_r, random_phi, random_z)),
energy_split, above);
// out << density_at_position << "\n";
// might be bad that the value gets recalculated for each call of density_->density(...)
} while (random_n*max_density > density_at_position);
// DEBUG_LEVEL_2(out.str());
return CylinderCoordinate(
blitz::TinyVector<double, 3>(random_r, random_phi, random_z));
}
+123 -9
View File
@@ -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 "constants/ionization_constants.h"
#include <sstream> #include <sstream>
Injector::Injector( Injector::Injector(
@@ -19,7 +20,8 @@ Injector::Injector(
: injector_infos_(injector_info), : injector_infos_(injector_info),
activity_(activity), activity_(activity),
position_distribution_(position_distribution), position_distribution_(position_distribution),
mother_drift_velocity_(mother_drift_velocity) { mother_drift_velocity_(mother_drift_velocity),
energySplit_(ionization::ionization_energy_janev_atomic) {
ASSERT_POSITIVE(&activity_, "Injector: Activity must be positive"); ASSERT_POSITIVE(&activity_, "Injector: Activity must be positive");
ASSERT_EXISTS(&position_distribution_, ASSERT_EXISTS(&position_distribution_,
"Injector: The position distribution is null."); "Injector: The position distribution is null.");
@@ -59,10 +61,6 @@ void Injector::initInjectionTimeDistribution(double 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 double beta_particle_weight) { const double beta_particle_weight) {
// pull drawing from the position distribution into the "multiplicity" loop?
// (would improve resolution stuff in space as well as just in velocity direction
// especially for slow particles, i.e. ions)
std::vector<particle::Particle> new_particles; std::vector<particle::Particle> new_particles;
const double time_birth = current_time - injection_time_distribution_(*engine); const double time_birth = current_time - injection_time_distribution_(*engine);
@@ -73,12 +71,10 @@ std::vector<particle::Particle> Injector::rollNewParticles(
DEBUG_LEVEL_3("Injected z position: " + DEBUG_LEVEL_3("Injected z position: " +
std::to_string(position.cylinderEntries()(2))); std::to_string(position.cylinderEntries()(2)));
// 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,
injector_info.creation_velocity_distribution, injector_info.creation_velocity_distribution,
time_birth, beta_particle_weight)); time_birth, beta_particle_weight));
// }
} }
return new_particles; return new_particles;
} }
@@ -87,8 +83,23 @@ 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) {
if(specie->type() == species::ImplementedSpecies::electron) {
if(!(particleNumbersMap_[specie->name()][1] == 0))
queue.push_back(drawNewDensityParticleAboveOrBelow(engine, specie,
magnetic_field, weight_factor, injection_time, true));
if(!(particleNumbersMap_[specie->name()][0] == 0))
queue.push_back(drawNewDensityParticleAboveOrBelow(engine, specie,
magnetic_field, weight_factor, injection_time, false));
return;
}
if(particleNumbersMap_[specie->name()][0] == 0) {
return;
}
// draw the position // draw the position
CylinderCoordinate position = CylinderCoordinate position =
specie->positionDistributionFromDensity()->drawPosition(engine); specie->positionDistributionFromDensity()->drawPosition(engine);
@@ -119,7 +130,7 @@ void Injector::drawNewParticleFromDensityField(std::mt19937* engine,
} }
particle::Particle drawn_particle(specie, position, particle::Particle drawn_particle(specie, position,
velocity, injection_time, weight); velocity, injection_time, weight_factor*particleNumbersMap_[specie->name()][0]);
queue.push_back(drawn_particle); queue.push_back(drawn_particle);
} }
@@ -127,3 +138,106 @@ void Injector::drawNewParticleFromDensityField(std::mt19937* engine,
double Injector::timePassed() const { double Injector::timePassed() const {
return 1 / activity_; return 1 / activity_;
} }
void Injector::fillParticleNumbers(std::shared_ptr<species::TrackedSpecie> specie) {
// std::stringstream out;
// out << specie->name() << ":";
std::vector<double> numbers;
double nTot = 0;
if(specie->type() == species::ImplementedSpecies::electron) {
double numberBelow = specie->densityForDrawing()->particleNumber(
energySplit_, false);
numbers.push_back(numberBelow);
double numberAbove = specie->densityForDrawing()->particleNumber(
energySplit_, true);
numbers.push_back(numberAbove);
// out << "\n\tN( E < " << energySplit_<< ")"
// << " = " << numberBelow;
// out << "\n\tN( E > " << energySplit_<< ")"
// << " = " << numberAbove;
nTot = numberBelow + numberAbove;
} else {
numbers.push_back(specie->densityForDrawing()->particleNumber());
// out << numbers[0];
nTot = numbers[0];
}
particleNumbersMap_[specie->name()] = numbers;
// out << "\n\t N_total = " << nTot;
// DEBUG_LEVEL_2(out.str());
}
particle::Particle Injector::drawNewDensityParticleAboveOrBelow(
std::mt19937* engine,
std::shared_ptr<species::TrackedSpecie> specie,
std::shared_ptr<MagneticField> magnetic_field,
const double weight_factor,
const double injection_time,
const bool above) {
std::string above_string = above ? "Above" : "Below";
// std::stringstream out;
// // draw the position
// out << above_string << "\n";
// out << "\tPosition: ";
CylinderCoordinate position =
specie->positionDistributionFromDensity()
->drawPosition(engine, energySplit_, above);
// out << position.cylinderEntries();
double den_at_pos_below =
specie->densityForDrawing()->density(position, energySplit_, false);
// out << "\n\tDensity At position below: " << den_at_pos_below;
// check if density was empty (i.e. phi value lower than 0)
if (position.cylinderEntries()(1) < 0) {
ERROR(specie->name() + ": has zero density");
}
// DEBUG_LEVEL_3("Injected z position: " +
// std::to_string(position.cylinderEntries()(2)));
// DEBUG_LEVEL_3("Injected r position: " +
// std::to_string(position.cylinderEntries()(0)));
// draw the velocity
CartesianVector velocity(blitz::TinyVector<double, 3>(0,0,0));
const int energy_split_bin =
specie->dynamicDensityField()->energyBinning()->bin(energySplit_);
// out << "\n\tEnergy split bin: " << energy_split_bin;
// 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()->
rollVelocity(position, engine, energy_split_bin, above);
} else {
velocity = specie->velocityDistributionFromDensityField()->
rollVelocity(position, engine, magnetic_field,
energy_split_bin,
above);
}
// out << "\n\tVelocity: " << velocity.cartesianEntries();
int index = above ? 1 : 0;
// out << "\n\tWeight: " << weight_factor*particleNumbersMap_[specie->name()][index];
// out << "particleNumbersMap: ";
// for(auto element : particleNumbersMap_[specie->name()])
// out << element << " ";
particle::Particle drawn_particle(specie, position,
velocity, injection_time, weight_factor*particleNumbersMap_[specie->name()][index]);
// DEBUG_LEVEL_1(out.str());
return drawn_particle;
}
double Injector::energySplit() const {
return energySplit_;
}
+12 -7
View File
@@ -169,6 +169,8 @@ void Simulation::start() {
timer_->setDeltaT(delta_t); timer_->setDeltaT(delta_t);
injector_->initInjectionTimeDistribution(delta_t); injector_->initInjectionTimeDistribution(delta_t);
for(auto element: tracked_species_)
injector_->fillParticleNumbers(element.second);
int num_cpu_syncs = 0; int num_cpu_syncs = 0;
bool reset = true; bool reset = true;
@@ -251,6 +253,10 @@ void Simulation::start() {
// the density from which new particles are drawn is only updated after a timestep // the density from which new particles are drawn is only updated after a timestep
pair.second->positionDistributionFromDensity()->setMaximumDensity(); pair.second->positionDistributionFromDensity()->setMaximumDensity();
pair.second->positionDistributionFromDensity()
->setMaximumDensityAboveAndBelow(injector_->energySplit());
injector_->fillParticleNumbers(pair.second);
} }
leftover_events_time_resolution = beta_decay_events; leftover_events_time_resolution = beta_decay_events;
@@ -328,6 +334,7 @@ void Simulation::processDensityEvents(int events, const double weight_factor) {
std::map<std::string, double> particleNumbers; std::map<std::string, double> particleNumbers;
for(auto& pair: tracked_species_) { for(auto& pair: tracked_species_) {
particleNumbers[pair.first] = (pair.second->densityForDrawing()->particleNumber()); particleNumbers[pair.first] = (pair.second->densityForDrawing()->particleNumber());
// injector_->fillParticleNumbers(pair.second);
} }
while ((current_event < events) || (!queue_.empty())) { while ((current_event < events) || (!queue_.empty())) {
@@ -550,21 +557,19 @@ std::vector<particle::Particle> Simulation::determineInjectedDensityParticles(
continue; continue;
} }
#if defined debug_level3 #if defined debug_level2
if (current == 0) { // if (current == 0) {
std::stringstream out; std::stringstream out;
out << pair.second->name(); out << pair.second->name();
out << "particle number from density: "; out << "particle number from density: ";
out << particleNumbers[pair.first]; out << particleNumbers[pair.first];
DEBUG_LEVEL_3(out.str()); DEBUG_LEVEL_2(out.str());
} // }
#endif #endif
double weight = particleNumbers[pair.first]*weight_factor;
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);
} }
return new_particles; return new_particles;
} }
+152
View File
@@ -5,6 +5,7 @@
#include "simulation/velocity_distribution_function.h" #include "simulation/velocity_distribution_function.h"
#include "simulation/density_field.h" #include "simulation/density_field.h"
#include "simulation/electromagnetic_field.h" #include "simulation/electromagnetic_field.h"
#include "simulation/errors.h"
// utility function returning a unit vector perpendicular to the given // utility function returning a unit vector perpendicular to the given
// (cartesian) TinyVector // (cartesian) TinyVector
@@ -141,3 +142,154 @@ CartesianVector VelocityDistributionFromDensityField::rollVelocity(
return CartesianVector(v_direction * vMagnitude); return CartesianVector(v_direction * vMagnitude);
} }
/*
Note: right now assumes that the density (either above or below)
the threshold value is non-zero, i.e. in the case that
that is not the case will create no sensible result
*/
CartesianVector VelocityDistributionFromDensityField::rollVelocity(
const Coordinate& position, std::mt19937* engine,
std::shared_ptr<MagneticField> magnetic_field,
const int E_th_bin, const bool above) {
// draw energy from DynamicDensityField
blitz::Array<double, 1> energyValues = density_field_->
sliceSpectrumArray(position);
auto bordersBeginIterator = energyBinBorders_.begin();
auto bordersEndIterator = energyBinBorders_.end();
auto valuesBeginIterator = energyValues.begin();
if(above) {
bordersBeginIterator += E_th_bin - density_field_->energyBinning()->binIndexMin();
valuesBeginIterator = energyValues(blitz::Range(E_th_bin, blitz::toEnd)).begin();
} else {
if(E_th_bin == density_field_->energyBinning()->binIndexMin()) {
return CartesianVector(blitz::TinyVector<double, 3>(0,0,0));
}
bordersEndIterator = bordersBeginIterator + E_th_bin
- density_field_->energyBinning()->binIndexMin() + 1;
}
std::piecewise_constant_distribution<double> energy_dist(
bordersBeginIterator,
bordersEndIterator,
valuesBeginIterator);
double energy_eV = energy_dist(*engine);
// draw pitch angle from DynamicDensityField
blitz::Array<double, 1> pitchAngleValues = density_field_->
sliceTimeInBinArray(position, energy_eV);
std::piecewise_constant_distribution<double> pitchAngle_dist(
pitchAngleBinBorders_.begin(),
pitchAngleBinBorders_.end(),
pitchAngleValues.begin());
double pitchAngle = pitchAngle_dist(*engine);
#ifdef debug
if((std::isnan(pitchAngle)) || (std::isnan(energy_eV))) {
std::stringstream out;
above ? out << "Above" : out << "Below";
out << " threshold bin of " << E_th_bin;
out << "\npitchAngle: " << 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());
}
#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);
}
CartesianVector VelocityDistributionFromDensityField::rollVelocity(
const Coordinate& position, std::mt19937* engine,
const int E_th_bin, const bool above) {
// draw energy from DynamicDensityField
blitz::Array<double, 1> energyValues = density_field_->
sliceSpectrumArray(position);
auto bordersBeginIterator = energyBinBorders_.begin();
auto bordersEndIterator = energyBinBorders_.end();
auto valuesBeginIterator = energyValues.begin();
if(above) {
bordersBeginIterator += E_th_bin - density_field_->energyBinning()->binIndexMin();
valuesBeginIterator = energyValues(blitz::Range(E_th_bin, blitz::toEnd)).begin();
} else {
if(E_th_bin == density_field_->energyBinning()->binIndexMin()) {
return CartesianVector(blitz::TinyVector<double, 3>(0,0,0));
}
bordersEndIterator = bordersBeginIterator +
E_th_bin - density_field_->energyBinning()->binIndexMin();
}
std::piecewise_constant_distribution<double> energy_dist(
bordersBeginIterator,
bordersEndIterator,
valuesBeginIterator);
double energy_eV = energy_dist(*engine);
// draw pitch angle from DynamicDensityField
blitz::Array<double, 1> pitchAngleValues = density_field_->
sliceTimeInBinArray(position, energy_eV);
std::piecewise_constant_distribution<double> pitchAngle_dist(
pitchAngleBinBorders_.begin(),
pitchAngleBinBorders_.end(),
pitchAngleValues.begin());
double pitchAngle = pitchAngle_dist(*engine);
// construct the velocity
const double theta = theta_distribution_(*engine)*2*M_PI;
blitz::TinyVector<double, 3> cartesianB_values(0,0,1);
const double Bmagnitude = kinematic::norm(cartesianB_values);
const double vMagnitude = kinematic::velocity(energy_eV, mass_kg_);
blitz::TinyVector<double, 3> v_perp(1,0,0);
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);
}