the timestep; Bug with density drawing present (if CPU syncs don't line up with the timestep)
130 lines
4.7 KiB
C++
Executable File
130 lines
4.7 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 <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);
|
|
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));
|
|
// }
|
|
}
|
|
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,
|
|
const double injection_time) {
|
|
// draw the position
|
|
CylinderCoordinate position =
|
|
specie->positionDistributionFromDensity()->drawPosition(engine);
|
|
|
|
// check if density was empty (i.e. phi value lower than 0)
|
|
if (position.cylinderEntries()(1) < 0) {
|
|
DEBUG_LEVEL_3(specie->name() + ": has zero density");
|
|
return;
|
|
}
|
|
|
|
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));
|
|
|
|
// 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);
|
|
} else {
|
|
velocity = specie->velocityDistributionFromDensityField()->
|
|
rollVelocity(position, engine, magnetic_field);
|
|
}
|
|
|
|
particle::Particle drawn_particle(specie, position,
|
|
velocity, injection_time, weight);
|
|
|
|
queue.push_back(drawn_particle);
|
|
}
|
|
|
|
double Injector::timePassed() const {
|
|
return 1 / activity_;
|
|
}
|