#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 Injector::Injector( std::vector& injector_info, double activity, std::shared_ptr position_distribution, std::shared_ptr 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 created_specie, std::shared_ptr creation_velocity_distribution, const double time_birth, const double weight) { blitz::TinyVector 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(0., 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); 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 specie, std::vector& queue, std::shared_ptr magnetic_field, const double weight_factor, const double injection_time) { blitz::TinyVector 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)); 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)); // 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_; }