diff --git a/include/simulation/density_field.h b/include/simulation/density_field.h index e2d018c..9f70294 100755 --- a/include/simulation/density_field.h +++ b/include/simulation/density_field.h @@ -31,6 +31,7 @@ class DynamicDensityField : public DensityField { double density(const Coordinate& position); double density(const Coordinate& position, double energy_eV); + double density(const Coordinate& position, double energy_split, bool above); bool changedPositionBin(const Coordinate& old_position, const Coordinate& new_position) const; @@ -48,6 +49,11 @@ class DynamicDensityField : public DensityField { // return the particle number stored in the density of this object 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 // of the pitch angle binning @@ -59,9 +65,10 @@ class DynamicDensityField : public DensityField { blitz::Array deletedParticlesArray() const; blitz::Array binVolumeArray() const; + blitz::Array densityArray(double energy_split, bool above); + // return an array of the density values at the Coordinate position (only energy -// direction left); over and underflow bin are removed -// (might not be good, since then the underflow particles are lost...) +// direction left); overbin is removed blitz::Array sliceSpectrumArray(const Coordinate& position) const; // return an array of the time in bin at the Coordinate position and the energy diff --git a/include/simulation/event_position_distribution.h b/include/simulation/event_position_distribution.h index 78e469e..a274233 100755 --- a/include/simulation/event_position_distribution.h +++ b/include/simulation/event_position_distribution.h @@ -46,15 +46,21 @@ class PositionDistributionFromDensity { // draw a position from the density provided at construction using the rejection method 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 // should not be called frequently, since during each call the density field is // recalculated from scratch by the DynamicDensityField object void setMaximumDensity(); + void setMaximumDensityAboveAndBelow(double energy_split); private: // variable storing the maximum value of the density double maximum_density_ = 1.; + double maximum_density_below_ = 1.; + double maximum_density_above_ = 1.; + std::shared_ptr density_; // distributions for the spatial directions and for the density diff --git a/include/simulation/injector.h b/include/simulation/injector.h index 6159c3c..4d4b234 100755 --- a/include/simulation/injector.h +++ b/include/simulation/injector.h @@ -60,9 +60,15 @@ class Injector { double timePassed() const; + void fillParticleNumbers(std::shared_ptr specie); + + double energySplit() const; + private: std::vector injector_infos_; const double activity_; + double energySplit_; + std::map> particleNumbersMap_; std::shared_ptr position_distribution_; std::shared_ptr mother_drift_velocity_; std::uniform_real_distribution injection_time_distribution_; @@ -74,6 +80,13 @@ class Injector { std::shared_ptr creation_velocity_distribution, const double time_birth, const double weight); + + particle::Particle drawNewDensityParticleAboveOrBelow(std::mt19937* engine, + std::shared_ptr specie, + std::shared_ptr field, + const double weight, + const double injection_time, + const bool above); }; #endif /* INJECTOR_H */ diff --git a/include/simulation/velocity_distribution.h b/include/simulation/velocity_distribution.h index 0b9efca..09e574f 100755 --- a/include/simulation/velocity_distribution.h +++ b/include/simulation/velocity_distribution.h @@ -55,6 +55,23 @@ class VelocityDistributionFromDensityField : public VelocityDistribution { CartesianVector rollVelocity( const Coordinate& position, std::mt19937* engine, std::shared_ptr 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 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: // mass of the species of which the density field is a part of const double mass_kg_; diff --git a/src/IO/restart.cpp b/src/IO/restart.cpp index 3ce0e5c..424dbf1 100644 --- a/src/IO/restart.cpp +++ b/src/IO/restart.cpp @@ -10,6 +10,7 @@ #include "poisson_solver/poisson_solver.h" #include "simulation/timer.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 // called reset function (e.g. in the density field) and sometimes it's @@ -40,6 +41,11 @@ void Restart::restart(std::mapdensityForDrawing(), path); 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); } } diff --git a/src/simulation/density_field.cpp b/src/simulation/density_field.cpp index 57f6279..0946a43 100755 --- a/src/simulation/density_field.cpp +++ b/src/simulation/density_field.cpp @@ -161,6 +161,36 @@ double DynamicDensityField::density(const Coordinate& position, 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 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( const Coordinate& old_position, const Coordinate& new_position) const { @@ -242,6 +272,40 @@ blitz::Array DynamicDensityField::densityArray() { return density.copy(); } +blitz::Array 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 lowerbound(density_per_m3_.lbound(0), + density_per_m3_.lbound(1), + density_per_m3_.lbound(2)); + blitz::TinyVector extent(density_per_m3_.extent(0), + density_per_m3_.extent(1), + density_per_m3_.extent(2)); + blitz::Array 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 DynamicDensityField::spectrumArray() { recalculateDensityArray(); return density_per_m3_; @@ -307,6 +371,35 @@ double DynamicDensityField::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 DynamicDensityField:: sliceSpectrumArray(const Coordinate& position) const { blitz::TinyVector position_bin = diff --git a/src/simulation/event_position_distribution.cpp b/src/simulation/event_position_distribution.cpp index eeca600..7cab027 100755 --- a/src/simulation/event_position_distribution.cpp +++ b/src/simulation/event_position_distribution.cpp @@ -1,4 +1,5 @@ #include "simulation/event_position_distribution.h" +#include "simulation/errors.h" EventPositionDistribution::EventPositionDistribution( std::shared_ptr mother_density, @@ -67,6 +68,14 @@ void PositionDistributionFromDensity::setMaximumDensity() { 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) { if(isEqual(maximum_density_, 0.)) { // 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_phi = phi_values(*engine); random_z = z_values(*engine); + random_n = density_values(*engine); // might be bad that the value gets recalculated for each call of density_->density(...) - } while (random_n*maximum_density_ >= density_->density(CylinderCoordinate( + } while (random_n*maximum_density_ > density_->density(CylinderCoordinate( blitz::TinyVector(random_r, random_phi, random_z)))); + return CylinderCoordinate( + blitz::TinyVector(random_r, random_phi, random_z)); +} + +CylinderCoordinate PositionDistributionFromDensity::drawPosition( + std::mt19937* engine, 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(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(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(random_r, random_phi, random_z)); } \ No newline at end of file diff --git a/src/simulation/injector.cpp b/src/simulation/injector.cpp index 68f4267..9a60bb6 100755 --- a/src/simulation/injector.cpp +++ b/src/simulation/injector.cpp @@ -9,6 +9,7 @@ #include "simulation/density_field.h" #include "simulation/velocity_distribution.h" #include "simulation/electromagnetic_field.h" +#include "constants/ionization_constants.h" #include Injector::Injector( @@ -19,7 +20,8 @@ Injector::Injector( : injector_infos_(injector_info), activity_(activity), 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_EXISTS(&position_distribution_, "Injector: The position distribution is null."); @@ -59,10 +61,6 @@ void Injector::initInjectionTimeDistribution(double delta_t) { std::vector Injector::rollNewParticles( std::mt19937* engine, const double current_time, 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 new_particles; const double time_birth = current_time - injection_time_distribution_(*engine); @@ -73,12 +71,10 @@ std::vector Injector::rollNewParticles( DEBUG_LEVEL_3("Injected z position: " + std::to_string(position.cylinderEntries()(2))); - // for (int i = 0; i < injector_info.multiplicity; i++) { - new_particles.push_back( - rollNewParticle(engine, position, injector_info.created_specie, - injector_info.creation_velocity_distribution, - time_birth, beta_particle_weight)); - // } + new_particles.push_back( + rollNewParticle(engine, position, injector_info.created_specie, + injector_info.creation_velocity_distribution, + time_birth, beta_particle_weight)); } return new_particles; } @@ -87,8 +83,23 @@ void Injector::drawNewParticleFromDensityField(std::mt19937* engine, std::shared_ptr specie, std::vector& queue, std::shared_ptr magnetic_field, - const double weight, + const double weight_factor, 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 CylinderCoordinate position = specie->positionDistributionFromDensity()->drawPosition(engine); @@ -119,7 +130,7 @@ void Injector::drawNewParticleFromDensityField(std::mt19937* engine, } particle::Particle drawn_particle(specie, position, - velocity, injection_time, weight); + velocity, injection_time, weight_factor*particleNumbersMap_[specie->name()][0]); queue.push_back(drawn_particle); } @@ -127,3 +138,106 @@ void Injector::drawNewParticleFromDensityField(std::mt19937* engine, double Injector::timePassed() const { return 1 / activity_; } + +void Injector::fillParticleNumbers(std::shared_ptr specie) { + // std::stringstream out; + // out << specie->name() << ":"; + std::vector 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 specie, + std::shared_ptr 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(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_; +} \ No newline at end of file diff --git a/src/simulation/simulation.cpp b/src/simulation/simulation.cpp index d8e07d3..18bbda2 100755 --- a/src/simulation/simulation.cpp +++ b/src/simulation/simulation.cpp @@ -169,6 +169,8 @@ void Simulation::start() { timer_->setDeltaT(delta_t); injector_->initInjectionTimeDistribution(delta_t); + for(auto element: tracked_species_) + injector_->fillParticleNumbers(element.second); int num_cpu_syncs = 0; bool reset = true; @@ -251,6 +253,10 @@ void Simulation::start() { // the density from which new particles are drawn is only updated after a timestep pair.second->positionDistributionFromDensity()->setMaximumDensity(); + pair.second->positionDistributionFromDensity() + ->setMaximumDensityAboveAndBelow(injector_->energySplit()); + + injector_->fillParticleNumbers(pair.second); } leftover_events_time_resolution = beta_decay_events; @@ -328,6 +334,7 @@ void Simulation::processDensityEvents(int events, const double weight_factor) { std::map particleNumbers; for(auto& pair: tracked_species_) { particleNumbers[pair.first] = (pair.second->densityForDrawing()->particleNumber()); + // injector_->fillParticleNumbers(pair.second); } while ((current_event < events) || (!queue_.empty())) { @@ -550,21 +557,19 @@ std::vector Simulation::determineInjectedDensityParticles( continue; } -#if defined debug_level3 - if (current == 0) { +#if defined debug_level2 + // if (current == 0) { std::stringstream out; out << pair.second->name(); out << "particle number from density: "; out << particleNumbers[pair.first]; - DEBUG_LEVEL_3(out.str()); - } + DEBUG_LEVEL_2(out.str()); + // } #endif - double weight = particleNumbers[pair.first]*weight_factor; - injector_->drawNewParticleFromDensityField(random->engine(), pair.second, new_particles, magnetic_field_, - weight, injection_time); + weight_factor, injection_time); } return new_particles; } diff --git a/src/simulation/velocity_distribution.cpp b/src/simulation/velocity_distribution.cpp index 12e53b8..5ce2687 100755 --- a/src/simulation/velocity_distribution.cpp +++ b/src/simulation/velocity_distribution.cpp @@ -5,6 +5,7 @@ #include "simulation/velocity_distribution_function.h" #include "simulation/density_field.h" #include "simulation/electromagnetic_field.h" +#include "simulation/errors.h" // utility function returning a unit vector perpendicular to the given // (cartesian) TinyVector @@ -134,6 +135,159 @@ CartesianVector VelocityDistributionFromDensityField::rollVelocity( blitz::TinyVector v_perp = perpendicularNormalVector(cartesianB_values); + blitz::TinyVector v_direction = pitchAngle + * cartesianB_values/Bmagnitude + + std::sqrt(1 - pitchAngle*pitchAngle) + * kinematic::rotateVector(v_perp, cartesianB_values, theta); + + return CartesianVector(v_direction * vMagnitude); +} + + +/* + 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 magnetic_field, + const int E_th_bin, const bool above) { + + // draw energy from DynamicDensityField + blitz::Array 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(0,0,0)); + } + bordersEndIterator = bordersBeginIterator + E_th_bin + - density_field_->energyBinning()->binIndexMin() + 1; + } + + std::piecewise_constant_distribution energy_dist( + bordersBeginIterator, + bordersEndIterator, + valuesBeginIterator); + + double energy_eV = energy_dist(*engine); + + // std::stringstream out; + // if(above) { + // out << "Above: "; + // } else { + // out << "Below: "; + // } + // out << "\n\tEnergyValues: " << energyValues; + // out << "\n\tBinBorders: ["; + // for(auto bb : energyBinBorders_) + // out << bb << ", "; + // out << "]"; + // out << "\n\tdrawn energy: " << energy_eV; + // out << "\n\tlowerIndexEnergyValues: " << energy_values.lbound(); + // out << "\n\tBeginIterator: " << *bordersBeginIterator; + // out << "\n\tEndIterator: " << *(bordersEndIterator - 1); + // out << "\n\tValueBeginIterator: " << *valuesBeginIterator; + // DEBUG_LEVEL_2(out.str()); + + // if (isnan(energy_eV)) { + // ERROR("energy value is nan!"); + // } + + // draw pitch angle from DynamicDensityField + + blitz::Array pitchAngleValues = density_field_-> + sliceTimeInBinArray(position, energy_eV); + + std::piecewise_constant_distribution 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 cartesianB_values = + magnetic_field->value(position).cartesianEntries(position); + const double Bmagnitude = kinematic::norm(cartesianB_values); + + const double vMagnitude = kinematic::velocity(energy_eV, mass_kg_); + + blitz::TinyVector v_perp = + perpendicularNormalVector(cartesianB_values); + + blitz::TinyVector v_direction = pitchAngle + * cartesianB_values/Bmagnitude + + std::sqrt(1 - pitchAngle*pitchAngle) + * kinematic::rotateVector(v_perp, cartesianB_values, theta); + + return CartesianVector(v_direction * vMagnitude); +} + +CartesianVector VelocityDistributionFromDensityField::rollVelocity( + const Coordinate& position, std::mt19937* engine, + const int E_th_bin, const bool above) { + + // draw energy from DynamicDensityField + blitz::Array 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(0,0,0)); + } + bordersEndIterator = bordersBeginIterator + + E_th_bin - density_field_->energyBinning()->binIndexMin(); + } + + std::piecewise_constant_distribution energy_dist( + bordersBeginIterator, + bordersEndIterator, + valuesBeginIterator); + + double energy_eV = energy_dist(*engine); + + // draw pitch angle from DynamicDensityField + + blitz::Array pitchAngleValues = density_field_-> + sliceTimeInBinArray(position, energy_eV); + + std::piecewise_constant_distribution 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 cartesianB_values(0,0,1); + const double Bmagnitude = kinematic::norm(cartesianB_values); + + const double vMagnitude = kinematic::velocity(energy_eV, mass_kg_); + + blitz::TinyVector v_perp(1,0,0); + blitz::TinyVector v_direction = pitchAngle * cartesianB_values/Bmagnitude + std::sqrt(1 - pitchAngle*pitchAngle)