Files
karl/src/simulation/injector.cpp
T

146 lines
5.2 KiB
C++
Executable File

#include "simulation/injector.h"
#include "simulation/drift_velocity.h"
#include "simulation/errors.h"
#include "simulation/event_position_distribution.h"
#include "simulation/kinematic.h"
#include "simulation/particle.h"
#include "simulation/species.h"
#include "simulation/density_field.h"
#include "simulation/velocity_distribution.h"
#include "simulation/electromagnetic_field.h"
#include "simulation/random_numbers.h"
#include <sstream>
Injector::Injector(
std::vector<InjectorInfo>& injector_info,
double activity,
std::shared_ptr<EventPositionDistribution> position_distribution,
std::shared_ptr<DriftVelocity> mother_drift_velocity)
: injector_infos_(injector_info),
activity_(activity),
position_distribution_(position_distribution),
mother_drift_velocity_(mother_drift_velocity) {
ASSERT_POSITIVE(&activity_, "Injector: Activity must be positive");
ASSERT_EXISTS(&position_distribution_,
"Injector: The position distribution is null.");
}
particle::Particle Injector::rollNewParticle(
std::mt19937* engine,
const Coordinate& position,
std::shared_ptr<species::TrackedSpecie> created_specie,
std::shared_ptr<VelocityDistribution> creation_velocity_distribution,
const double time_birth,
const double weight) {
blitz::TinyVector<double, 3> velocity =
creation_velocity_distribution->rollVelocity(position, engine)
.cartesianEntries();
if (mother_drift_velocity_) {
velocity = kinematic::relativisticBoost(
velocity,
mother_drift_velocity_->driftVelocity(position).cartesianEntries());
}
#if defined debug_level3
std::stringstream out;
out << created_specie->name() << " created at time: " << time_birth << "s.";
DEBUG_LEVEL_3(out.str());
#endif
return particle::Particle(created_specie, position,
CartesianVector(velocity), time_birth, weight);
}
void Injector::initInjectionTimeDistribution(double delta_t) {
injection_time_distribution_ =
std::uniform_real_distribution<double>(0., delta_t);
}
std::vector<particle::Particle> 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<particle::Particle> new_particles;
const double time_birth = current_time - injection_time_distribution_(*engine);
for (auto& injector_info : injector_infos_) {
CylinderCoordinate position = position_distribution_->rollPosition(engine);
#ifdef debug_level3
DEBUG_LEVEL_3("Injected z position: " +
std::to_string(position.cylinderEntries()(2)));
#endif
// 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));
// }
}
return new_particles;
}
void Injector::drawNewParticleFromDensityField(std::mt19937* engine,
std::shared_ptr<species::TrackedSpecie> specie,
std::vector<particle::Particle>& queue,
std::shared_ptr<MagneticField> magnetic_field,
const double weight_factor,
const double injection_time) {
blitz::TinyVector<int, 5> index = specie->randomIndex()->index();
const double num_part_in_bin = specie->densityForDrawing()->particleNumber(index);
if(num_part_in_bin == 0) {
return;
}
// draw the position
CylinderCoordinate position =
specie->positionDistributionFromDensity()->drawPosition(engine,
index(0), index(1), index(2));
#ifdef debug_level3
DEBUG_LEVEL_3("Injected z position: " +
std::to_string(position.cylinderEntries()(2)));
DEBUG_LEVEL_3("Injected r position: " +
std::to_string(position.cylinderEntries()(0)));
#endif
// draw the velocity
CartesianVector velocity(blitz::TinyVector<double, 3>(0,0,0));
// 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()->
drawVelocity(index, engine);
} else {
velocity = specie->velocityDistributionFromDensityField()->
drawVelocity(index, engine, position, magnetic_field);
}
#ifdef debug_level2
std::stringstream out;
out << "drawn index: " << index;
out << "\nnumber of particles (" << specie->name() << ") at index: " << num_part_in_bin;
out << "\ndrawn position: " << position.cylinderEntries();
out << "\ndrawn velocity: " << velocity.cartesianEntries();
DEBUG_LEVEL_2(out.str());
#endif
particle::Particle drawn_particle(specie, position,
velocity, injection_time, weight_factor*num_part_in_bin);
queue.push_back(drawn_particle);
}
double Injector::timePassed() const {
return 1 / activity_;
}