259 lines
9.5 KiB
C++
Executable File
259 lines
9.5 KiB
C++
Executable File
#include "simulation/velocity_distribution.h"
|
|
|
|
#include "simulation/drift_velocity.h"
|
|
#include "simulation/kinematic.h"
|
|
#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
|
|
blitz::TinyVector<double, 3> perpendicularNormalVector(
|
|
blitz::TinyVector<double, 3> vector) {
|
|
blitz::TinyVector<double, 3> result(0,0,0);
|
|
result[0] = std::copysign(vector[2], vector[0]);
|
|
result[1] = std::copysign(vector[2], vector[1]);
|
|
result[2] = -std::copysign(std::fabs(vector[0] + std::fabs(vector[1])),
|
|
vector[2]);
|
|
|
|
return result / kinematic::norm(result);
|
|
}
|
|
|
|
VelocityDistributionFromFunction::VelocityDistributionFromFunction(
|
|
std::shared_ptr<VelocityDistributionFunction> fv)
|
|
: fv_(fv) {}
|
|
|
|
CartesianVector VelocityDistributionFromFunction::rollVelocity(
|
|
const Coordinate& position,
|
|
std::mt19937* engine) {
|
|
return fv_->rollVelocity(engine);
|
|
}
|
|
|
|
double drawPiecewiseConstant(const std::vector<double>& intervals, const blitz::Array<double, 1>& weights,
|
|
std::mt19937* engine) {
|
|
std::uniform_real_distribution<> dist(0.,1.);
|
|
// std::stringstream out;
|
|
// out << "intervals: [ ";
|
|
// out << "]\n";
|
|
|
|
// out << "\nweights: " << weights;
|
|
// out << "\ninterval sizes: " << interval_sizes;
|
|
double normalization = blitz::sum(weights);
|
|
#if defined(debug)
|
|
if(intervals.size() != weights.length(0) + 1) {
|
|
std::stringstream msg;
|
|
msg << "Interval size ( " << intervals.size() << " ) are not compatible ";
|
|
msg << "with the weight size ( " << weights.length(0) << " )";
|
|
ERROR(msg.str());
|
|
}
|
|
#endif
|
|
|
|
// out << "\nnormalization: " << normalization;
|
|
// if there are not particles in the considered interval return NaN as an
|
|
// indicator for that
|
|
if(normalization == 0) {
|
|
// SIM_INFO(out.str());
|
|
return NAN;
|
|
}
|
|
|
|
double sum = 0;
|
|
int interval = -1;
|
|
double check = dist(*engine)*normalization;
|
|
|
|
for(int i = weights.lbound(0); i <= weights.ubound(0); i++) {
|
|
sum += weights(i);
|
|
if(check < sum) {
|
|
interval = i-weights.lbound(0);
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(interval==-1) {
|
|
std::stringstream msg;
|
|
msg << "no interval found: " << weights << " [ ";
|
|
for(auto value : intervals) {
|
|
msg << value << " ";
|
|
}
|
|
msg << "]";
|
|
ERROR(msg.str());
|
|
}
|
|
|
|
// SIM_INFO(out.str());
|
|
|
|
return (intervals[interval+1] - intervals[interval])*dist(*engine) + intervals[interval];
|
|
}
|
|
|
|
VelocityDistributionFromDensityField::VelocityDistributionFromDensityField(
|
|
std::shared_ptr<DynamicDensityField> density_field, double mass_kg):
|
|
density_field_(density_field),
|
|
energyBinBorders_(density_field->energyBinning()->binBorders()),
|
|
pitchAngleBinBorders_(density_field->pitchAngleBinning()->binBorders()),
|
|
theta_distribution_(std::uniform_real_distribution<double>(0.,1.)),
|
|
mass_kg_(mass_kg) {
|
|
// necessary to include the underflow bin in the energy domain
|
|
energyBinBorders_.insert(energyBinBorders_.begin(), 0);
|
|
// energy_values.resize(density_field->energyBinning()->numBins() + 1);
|
|
// mu_values.resize(density_field->pitchAngleBinning()->numBins());
|
|
}
|
|
|
|
// not important as of now since it's meant for drawing a velocity from the
|
|
// pitch angle distribution and not to be used in an interaction context in which
|
|
// the drift velocity is necessary for the determination of the cross section
|
|
// (might be possible to extract from the pitch angle dist or from the current)
|
|
// returns a 0 CartesianVector
|
|
CartesianVector VelocityDistributionFromDensityField::driftVelocity_ms(
|
|
const CylinderVector& position) {
|
|
return CartesianVector(blitz::TinyVector<double, 3> (0,0,0));
|
|
}
|
|
|
|
CartesianVector VelocityDistributionFromDensityField::rollVelocity(
|
|
const Coordinate& position, std::mt19937* engine) {
|
|
// SIM_INFO("set energy values");
|
|
// draw energy from DynamicDensityField
|
|
// energy_values = density_field_->sliceSpectrumArray(position);
|
|
const blitz::Array<double, 1> energy_values = density_field_->sliceSpectrumArray(position);
|
|
|
|
// std::piecewise_constant_distribution<double> energy_dist(
|
|
// energyBinBorders_.begin(),
|
|
// energyBinBorders_.end(),
|
|
// energy_values.begin());
|
|
|
|
// SIM_INFO("draw energy value");
|
|
|
|
double energy_eV = drawPiecewiseConstant(energyBinBorders_, energy_values, engine);//energy_dist(*engine); //
|
|
|
|
// if(std::isnan(energy_eV)) {
|
|
// std::stringstream msg;
|
|
// msg << "\nenergy is nan: " << position.cartesianEntries();
|
|
// msg << energy_values;
|
|
// density_field_->recalculateDensityArray();
|
|
// msg << "recalculated: " << density_field_->sliceSpectrumArray(position);
|
|
// msg << "density in bin: " << density_field_->density(position);
|
|
// msg << "\nbin: " << density_field_->positionBinning()->bin(position.cylinderEntries());
|
|
// msg << "\n";
|
|
// std::cerr << msg.str();
|
|
// }
|
|
// draw pitch angle from DynamicDensityField
|
|
|
|
// SIM_INFO("set mu values");
|
|
|
|
const blitz::Array<double, 1> mu_values = density_field_->sliceNumberInBinArray(position, energy_eV);
|
|
|
|
// std::cout << mu_values;
|
|
|
|
// SIM_INFO("\nset mu dist");
|
|
|
|
// std::piecewise_constant_distribution<double> pitchAngle_dist(
|
|
// pitchAngleBinBorders_.begin(),
|
|
// pitchAngleBinBorders_.end(),
|
|
// mu_values.begin());
|
|
|
|
// SIM_INFO("draw mu value");
|
|
|
|
double pitchAngle = drawPiecewiseConstant(pitchAngleBinBorders_, mu_values, engine);//pitchAngle_dist(*engine); //
|
|
// if(std::isnan(pitchAngle)) {
|
|
// std::stringstream msg;
|
|
// msg << "\npitchangle is nan: " << position.cartesianEntries();
|
|
// msg << mu_values;
|
|
// density_field_->recalculateDensityArray();
|
|
// msg << "recalculated: " << density_field_->sliceTimeInBinArray(position, energy_eV);
|
|
// msg << "density in bin: " << density_field_->density(position);
|
|
// msg << "\nbin: " << density_field_->positionBinning()->bin(position.cylinderEntries());
|
|
// msg << "\nenergy value: " << energy_eV;
|
|
// msg << "\nenergy bin: " << density_field_->energyBinning()->bin(energy_eV);
|
|
// // msg << "\nenergy bin borders: [ ";
|
|
// // for(auto value : energyBinBorders_) {
|
|
// // msg << " " << value;
|
|
// // }
|
|
// // msg << "]";
|
|
// std::cerr << msg.str();
|
|
// }
|
|
#ifdef debug
|
|
if(!isnan(energy_eV) && isnan(pitchAngle)) {
|
|
ERROR("Energy is not nan, but pitchangle is.");
|
|
}
|
|
#endif
|
|
|
|
|
|
// 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_);
|
|
|
|
blitz::TinyVector<double, 3> v_direction = pitchAngle * zDirection
|
|
+ std::sqrt(1 - pitchAngle*pitchAngle)
|
|
* kinematic::rotateVector(blitz::TinyVector<double,3>(1,0,0),
|
|
zDirection, theta);
|
|
|
|
return CartesianVector(v_direction * vMagnitude);
|
|
}
|
|
|
|
// can NOT be removed, needed for drawing of velocity of interaction partners
|
|
CartesianVector VelocityDistributionFromDensityField::rollVelocity(
|
|
const Coordinate& position, std::mt19937* engine,
|
|
std::shared_ptr<MagneticField> magnetic_field) {
|
|
|
|
// draw energy from DynamicDensityField
|
|
blitz::Array<double, 1> energyValues = density_field_->
|
|
sliceSpectrumArray(position);
|
|
|
|
std::piecewise_constant_distribution<double> energy_dist(
|
|
energyBinBorders_.begin(),
|
|
energyBinBorders_.end(),
|
|
energyValues.begin());
|
|
|
|
double energy_eV = energy_dist(*engine);
|
|
|
|
// draw pitch angle from DynamicDensityField
|
|
|
|
blitz::Array<double, 1> pitchAngleValues = density_field_->
|
|
sliceNumberInBinArray(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;
|
|
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());
|
|
}
|
|
#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);
|
|
} |