species (dynamic density field of previous timestep); some optimization changes
258 lines
11 KiB
C++
Executable File
258 lines
11 KiB
C++
Executable File
#include "interactions/recombination.h"
|
||
|
||
#include "simulation/density_field.h"
|
||
#include "simulation/logger.h"
|
||
#include "simulation/species.h"
|
||
#include "simulation/kinematic.h"
|
||
#include "constants/physical_constants.h"
|
||
#include "simulation/drift_velocity.h"
|
||
#include "simulation/electromagnetic_field.h"
|
||
#include "simulation/velocity_distribution.h"
|
||
|
||
namespace interaction {
|
||
|
||
Recombination::Recombination(std::function<double(double)> cross_section,
|
||
std::shared_ptr<species::TrackedSpecie> target)
|
||
: Interaction(cross_section, target), recombination_target_(target) {}
|
||
|
||
InteractionOutput Recombination::performInteraction(
|
||
particle::Particle& particle,
|
||
RandomNumbers* generator) {
|
||
#ifdef debug_level3
|
||
DEBUG_LEVEL_3("Perform recombination.");
|
||
#endif
|
||
|
||
// std::vector<double> macroscopic_bin_cross_sections_m;
|
||
// const int number_energy_bins = recombination_target_->dynamicDensityField()
|
||
// ->energyBinning()->binIndexMax();
|
||
// std::vector<double> energyBinBorders = recombination_target_->
|
||
// dynamicDensityField()->energyBinning()->binBorders();
|
||
|
||
// if(recombination_target_->type() == species::ImplementedSpecies::electron) {
|
||
// for (int bin = 0; bin < number_energy_bins - 1; ++bin) {
|
||
// macroscopic_bin_cross_sections_m.push_back(
|
||
// macroscopicCrossSection(particle.position(), bin));
|
||
// }
|
||
// } else {
|
||
// const blitz::TinyVector<double, 3> v_particle =
|
||
// particle.velocity().cartesianEntries();
|
||
|
||
// if(!recombination_target_->driftVelocity()) {
|
||
// ERROR(recombination_target_->name() + ": does not have a drift velocity!");
|
||
// }
|
||
|
||
// const blitz::TinyVector<double, 3> v_partner =
|
||
// recombination_target_->driftVelocity()
|
||
// ->driftVelocity(particle.position()).cartesianEntries();
|
||
|
||
// const blitz::TinyVector<double, 3> velocity_target_fixed =
|
||
// kinematic::velocityTargetFixed(v_particle, v_partner, particle.mass_kg(),
|
||
// recombination_target_->mass_kg());
|
||
|
||
// const double kinetic_energy_eV =
|
||
// kinematic::kineticEnergy(velocity_target_fixed, particle.mass_kg()) *
|
||
// UnitConversion::JouleToeV;
|
||
|
||
|
||
// double energy_eV_bin_center;
|
||
// for (int bin = 0; bin < number_energy_bins - 1; ++bin) {
|
||
// energy_eV_bin_center = (energyBinBorders[bin] + energyBinBorders[bin+1])/2.;
|
||
// macroscopic_bin_cross_sections_m.push_back(
|
||
// microscopicCrossSection(kinetic_energy_eV) *
|
||
// recombination_target_->dynamicDensityField()
|
||
// ->density(particle.position(), energy_eV_bin_center));
|
||
// }
|
||
// }
|
||
|
||
// const double total_macroscopic_cross_section_m =
|
||
// std::accumulate(macroscopic_bin_cross_sections_m.begin(),
|
||
// macroscopic_bin_cross_sections_m.end(), 0.0);
|
||
|
||
// if(total_macroscopic_cross_section_m <= 0.) {
|
||
// ERROR("Total cross section is " +
|
||
// std::to_string(total_macroscopic_cross_section_m) +
|
||
// ". But recombination was chosen.");
|
||
// }
|
||
|
||
// std::discrete_distribution<int> energy_bin_decider(
|
||
// macroscopic_bin_cross_sections_m.begin(),
|
||
// macroscopic_bin_cross_sections_m.end());
|
||
|
||
// const int energy_bin = energy_bin_decider(*(generator->engine()));
|
||
// const double energy_eV = (energyBinBorders[energy_bin] +
|
||
// energyBinBorders[energy_bin+1]) / 2.;
|
||
|
||
// blitz::TinyVector<double, 3> position_bins = recombination_target_->dynamicDensityField()
|
||
// ->positionBinning()->bin(particle.position().cylinderEntries());
|
||
|
||
// const blitz::Array<double, 1> mu_distribution =
|
||
// recombination_target_->dynamicDensityField()
|
||
// ->timeInBinArray()(position_bins(0),
|
||
// position_bins(1),
|
||
// position_bins(2),
|
||
// blitz::Range::all(),
|
||
// energy_bin);
|
||
|
||
// std::vector<double> mu_borders = recombination_target_->dynamicDensityField()
|
||
// ->pitchAngleBinning()->binBorders();
|
||
|
||
// std::discrete_distribution<int> mu_bin_decider(mu_distribution.begin(),
|
||
// mu_distribution.end());
|
||
|
||
// const int mu_bin = mu_bin_decider(*(generator->engine()));
|
||
|
||
// if((mu_bin == 0) || (mu_bin == mu_distribution.size()-1)) {
|
||
// ERROR("Recombination: drawn mu is from over- or underflow bin!");
|
||
// std::stringstream out;
|
||
// out << "mu distribution values: n";
|
||
// out << mu_distribution;
|
||
// DEBUG_LEVEL_3(out.str());
|
||
// }
|
||
// const double mu = (mu_borders[mu_bin - 1] + mu_borders[mu_bin])/2.;
|
||
|
||
// recombination_target_->dynamicDensityField()->logDeletion(particle.position(),
|
||
// energy_eV, mu);
|
||
|
||
InteractionOutput output;
|
||
output.deleteCurrentParticle(true);
|
||
|
||
if (particle.specie()->deletionLogger()) {
|
||
particle.specie()->deletionLogger()->logDeletion(
|
||
particle.cartesianPosition(), DeletionType::recombination);
|
||
}
|
||
|
||
return output;
|
||
}
|
||
|
||
double Recombination::macroscopicCrossSection(particle::Particle& particle) {
|
||
if(recombination_target_->type() == species::ImplementedSpecies::electron) {
|
||
const int numBins = recombination_target_->dynamicDensityField()->
|
||
energyBinning()->binIndexMax();
|
||
double macroscopic_cross_section = 0.;
|
||
const auto position = particle.cylinderPosition();
|
||
for (int bin = 0; bin < numBins - 1; ++bin) {
|
||
macroscopic_cross_section += macroscopicCrossSection(position, bin);
|
||
}
|
||
return macroscopic_cross_section;
|
||
}
|
||
// if target is not an electron -> cross section independent of Energy of
|
||
// the target
|
||
return Interaction::macroscopicCrossSection(particle);
|
||
}
|
||
// implicitly assumes that the bin index > 0 and < maxBinIndex
|
||
// calculates ∫dE_t σ(E_t) dn_t/dE_t (i.e. only applicable if
|
||
// cross section is independent of E_i); i: incident, t: target
|
||
double Recombination::macroscopicCrossSection(const Coordinate& position,
|
||
const int energy_bin_index) {
|
||
if (energy_bin_index > recombination_target_->dynamicDensityField()
|
||
->energyBinning()->binIndexMax()) {
|
||
return 0.;
|
||
}
|
||
const double lower_bound = recombination_target_->dynamicDensityField()
|
||
->energyBinning()->binBorders()[energy_bin_index];
|
||
const double upper_bound = recombination_target_->dynamicDensityField()
|
||
->energyBinning()->binBorders()[energy_bin_index + 1];
|
||
const double intermediate_value = (upper_bound + lower_bound)/2.;
|
||
const double differential_density_value = recombination_target_->
|
||
dynamicDensityField()->density(position, intermediate_value) /
|
||
(upper_bound - lower_bound);
|
||
//Simpson's rule
|
||
const double integrated_microscopic_cross_section =
|
||
(upper_bound - lower_bound)/6. *
|
||
(microscopicCrossSection(upper_bound, position) +
|
||
4*microscopicCrossSection(intermediate_value, position) +
|
||
microscopicCrossSection(lower_bound, position));
|
||
// assumption, that the differential density is constant within a cell
|
||
return differential_density_value * integrated_microscopic_cross_section;
|
||
}
|
||
|
||
double Recombination::drawCollisionTime(particle::Particle& particle,
|
||
RandomNumbers* generator) {
|
||
CartesianVector particle_velocity = particle.velocity();
|
||
// const double density = target_->densityField()->density(particle.cylinderPosition());
|
||
// Use the density for drawing field, since it contains the density of the earlier
|
||
// timestep
|
||
const double density = recombination_target_->densityForDrawing()
|
||
->density(particle.cylinderPosition());
|
||
|
||
// recombination_target_->densityForDrawing()->recalculateDensityArray();
|
||
// if(density != recombination_target_->densityForDrawing()
|
||
// ->density(particle.cylinderPosition())) {
|
||
// ERROR("density changes after recalculation of density array!");
|
||
// }
|
||
// density effectively 0
|
||
if(density <= SimConstants::compare_epsilon) {
|
||
return std::numeric_limits<double>::max();
|
||
}
|
||
|
||
// Assumes that B || z, which is obviously not necessarily the case !!!
|
||
target_velocity_ = recombination_target_->velocityDistributionFromDensityField()
|
||
->rollVelocity(particle.cylinderPosition(), generator->engine());
|
||
|
||
// for testing purposes
|
||
// target_velocity_ = CartesianVector(blitz::TinyVector<double, 3>(0.,0.,0.));
|
||
|
||
// masses are irrelevant for the transformation
|
||
const blitz::TinyVector<double, 3> velocity_target_fixed =
|
||
kinematic::velocityTargetFixed(particle_velocity.cartesianEntries(),
|
||
target_velocity_.cartesianEntries(), 0, 0);
|
||
|
||
const double relative_velocity = kinematic::norm(velocity_target_fixed);
|
||
|
||
// check if the relative velocity is smaller than an epsilon compared to the larger
|
||
// of the two present velocities (to avoid division by 0 if either is 0) probably
|
||
// a better version possible
|
||
if(relative_velocity/std::max(
|
||
kinematic::norm(particle_velocity.cartesianEntries()),
|
||
kinematic::norm(target_velocity_.cartesianEntries())
|
||
) < SimConstants::compare_epsilon) {
|
||
return std::numeric_limits<double>::max();
|
||
}
|
||
|
||
const double energy_eV =
|
||
(recombination_target_->type() == species::ImplementedSpecies::electron) ?
|
||
kinematic::kineticEnergy(velocity_target_fixed, recombination_target_->mass_kg())*
|
||
UnitConversion::JouleToeV :
|
||
kinematic::kineticEnergy(velocity_target_fixed, particle.mass_kg())*
|
||
UnitConversion::JouleToeV;
|
||
|
||
const double crosssection = microscopicCrossSection(energy_eV,
|
||
particle.cylinderPosition());
|
||
|
||
#if defined(debug)
|
||
if(density > 0 && std::isnan(crosssection)) {
|
||
std::stringstream msg;
|
||
msg << particle.specie()->name() << particle.cartesianPosition().cartesianEntries();
|
||
msg << " " << recombination_target_->densityForDrawing()->positionBinning()->bin(particle.cylinderPosition().cylinderEntries());
|
||
msg << ": Recombination: density is not nan but the cross section is ";
|
||
msg << density << ", " << crosssection;
|
||
msg << recombination_target_->densityForDrawing()->sliceSpectrumArray(particle.cylinderPosition());
|
||
ERROR(msg.str());
|
||
}
|
||
#endif
|
||
|
||
const double random_number = generator->rollUniform(0.,1.);
|
||
if (random_number == 0) {
|
||
return std::numeric_limits<double>::max();
|
||
}
|
||
|
||
const double distance_target_fixed =
|
||
- 1./(crosssection * density) * log(random_number);
|
||
|
||
const double time = distance_target_fixed / relative_velocity;
|
||
|
||
|
||
#ifdef debug_level2
|
||
std::stringstream out;
|
||
out << "particle energy: " << particle.kineticEnergy_eV();
|
||
out << "\ncross section: " << crosssection;
|
||
out << "\ndensity: " << density;
|
||
out << "\ntime: " << time;
|
||
if(density > 0)
|
||
DEBUG_LEVEL_2(out.str());
|
||
#endif
|
||
|
||
return time;
|
||
}
|
||
|
||
}; // namespace interaction
|