Version 2, position dependent E/B, kinetic ions, boris mover, terminator
This commit is contained in:
Executable
+201
@@ -0,0 +1,201 @@
|
||||
#include "simulation/coordinates.h"
|
||||
|
||||
BinCoordinate::BinCoordinate(const blitz::TinyVector<int, 3>& bin,
|
||||
const blitz::TinyVector<double, 3>& frac)
|
||||
: bin(bin), frac(frac){};
|
||||
|
||||
BinCoordinate::BinCoordinate(blitz::TinyVector<double, 3> position,
|
||||
blitz::TinyVector<double, 3> spacing) {
|
||||
bin = blitz::TinyVector<int, 3>(
|
||||
position(0) < 0 ? position(0) / spacing(0) - 1 : position(0) / spacing(0),
|
||||
position(1) < 0 ? position(1) / spacing(1) - 1 : position(1) / spacing(1),
|
||||
position(2) < 0 ? position(2) / spacing(2) - 1
|
||||
: position(2) / spacing(2));
|
||||
|
||||
frac = blitz::TinyVector<double, 3>(position(0) / spacing(0) - bin(0),
|
||||
position(1) / spacing(1) - bin(1),
|
||||
position(2) / spacing(2) - bin(2));
|
||||
}
|
||||
|
||||
blitz::TinyVector<double, 3> BinCoordinate::binFrac(
|
||||
blitz::TinyVector<double, 3> position,
|
||||
blitz::TinyVector<double, 3> spacing) {
|
||||
blitz::TinyVector<int, 3> bin = blitz::TinyVector<int, 3>(
|
||||
position(0) < 0 ? position(0) / spacing(0) - 1 : position(0) / spacing(0),
|
||||
position(1) < 0 ? position(1) / spacing(1) - 1 : position(1) / spacing(1),
|
||||
position(2) < 0 ? position(2) / spacing(2) - 1
|
||||
: position(2) / spacing(2));
|
||||
|
||||
return blitz::TinyVector<double, 3>(position(0) / spacing(0) - bin(0),
|
||||
position(1) / spacing(1) - bin(1),
|
||||
position(2) / spacing(2) - bin(2));
|
||||
}
|
||||
|
||||
blitz::TinyVector<int, 3> BinCoordinate::binValue(
|
||||
blitz::TinyVector<double, 3> position,
|
||||
blitz::TinyVector<double, 3> spacing) {
|
||||
return blitz::TinyVector<int, 3>(
|
||||
position(0) < 0 ? position(0) / spacing(0) - 1 : position(0) / spacing(0),
|
||||
position(1) < 0 ? position(1) / spacing(1) - 1 : position(1) / spacing(1),
|
||||
position(2) < 0 ? position(2) / spacing(2) - 1
|
||||
: position(2) / spacing(2));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
CartesianCoordinate::CartesianCoordinate(
|
||||
const blitz::TinyVector<double, 3>& cartesian_position)
|
||||
: cartesian_position(cartesian_position) {}
|
||||
|
||||
CartesianCoordinate::CartesianCoordinate(const Coordinate& coordinate)
|
||||
: cartesian_position(coordinate.cartesianEntries()) {}
|
||||
|
||||
blitz::TinyVector<double, 3> CartesianCoordinate::cartesianEntries() const {
|
||||
return cartesian_position;
|
||||
}
|
||||
|
||||
blitz::TinyVector<double, 3> CartesianCoordinate::cylinderEntries() const {
|
||||
const double radius = std::sqrt(std::pow(cartesian_position(0), 2) +
|
||||
std::pow(cartesian_position(1), 2));
|
||||
const double phi = std::atan2(cartesian_position(1), cartesian_position(0));
|
||||
const double z = cartesian_position(2);
|
||||
return blitz::TinyVector<double, 3>(radius, phi, z);
|
||||
}
|
||||
|
||||
void CartesianCoordinate::add(const Coordinate& coordinate) {
|
||||
cartesian_position += coordinate.cartesianEntries();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
CylinderCoordinate::CylinderCoordinate(
|
||||
const blitz::TinyVector<double, 3>& cylinder_position)
|
||||
: cylinder_position(cylinder_position) {
|
||||
// limitPhi();
|
||||
}
|
||||
|
||||
CylinderCoordinate::CylinderCoordinate(const Coordinate& coordinate)
|
||||
: cylinder_position(coordinate.cylinderEntries()) {
|
||||
// limitPhi();
|
||||
}
|
||||
|
||||
blitz::TinyVector<double, 3> CylinderCoordinate::cartesianEntries() const {
|
||||
return blitz::TinyVector<double, 3>(
|
||||
cylinder_position(0) * std::cos(cylinder_position(1)),
|
||||
cylinder_position(0) * std::sin(cylinder_position(1)),
|
||||
cylinder_position(2));
|
||||
}
|
||||
|
||||
blitz::TinyVector<double, 3> CylinderCoordinate::cylinderEntries() const {
|
||||
return cylinder_position;
|
||||
}
|
||||
|
||||
void CylinderCoordinate::limitPhi() {
|
||||
if (cylinder_position(1) < 0) {
|
||||
const int bin = cylinder_position(1) / (2 * M_PI) - 1;
|
||||
cylinder_position(1) = cylinder_position(1) - bin * 2 * M_PI;
|
||||
} else if (cylinder_position(1) > 2 * M_PI) {
|
||||
const int bin = cylinder_position(1) / (2 * M_PI);
|
||||
cylinder_position(1) = cylinder_position(1) - bin * 2 * M_PI;
|
||||
}
|
||||
}
|
||||
|
||||
void CylinderCoordinate::add(const Coordinate& coordinate) {
|
||||
const blitz::TinyVector<double, 3> new_cart_position =
|
||||
cartesianEntries() + coordinate.cartesianEntries();
|
||||
const CartesianCoordinate c_cart(new_cart_position);
|
||||
const CylinderCoordinate c_cyl(c_cart);
|
||||
cylinder_position = c_cyl.cylinderEntries();
|
||||
limitPhi();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
CylinderVector::CylinderVector(
|
||||
const blitz::TinyVector<double, 3> cylinder_vector)
|
||||
: cylinder_vector(cylinder_vector) {}
|
||||
|
||||
CylinderVector::CylinderVector(const CylinderVector& vector)
|
||||
: cylinder_vector(vector.cylinderEntries()) {}
|
||||
|
||||
CylinderVector::CylinderVector(const Vector& vector,
|
||||
const Coordinate& coordinate)
|
||||
: cylinder_vector(vector.cylinderEntries(coordinate)) {}
|
||||
|
||||
CylinderVector CylinderVector::operator+(const CylinderVector& vector) {
|
||||
return CylinderVector(this->cylinderEntries() + vector.cylinderEntries());
|
||||
}
|
||||
|
||||
blitz::TinyVector<double, 3> CylinderVector::cartesianEntries(
|
||||
const Coordinate& coordinate) const {
|
||||
const double A_x =
|
||||
std::cos(coordinate.cylinderEntries()(1)) * cylinder_vector(0) -
|
||||
std::sin(coordinate.cylinderEntries()(1)) * cylinder_vector(1);
|
||||
const double A_y =
|
||||
std::sin(coordinate.cylinderEntries()(1)) * cylinder_vector(0) +
|
||||
std::cos(coordinate.cylinderEntries()(1)) * cylinder_vector(1);
|
||||
const double A_z = cylinder_vector(2);
|
||||
return blitz::TinyVector<double, 3>(A_x, A_y, A_z);
|
||||
}
|
||||
|
||||
blitz::TinyVector<double, 3> CylinderVector::cylinderEntries(
|
||||
const Coordinate& coordinate) const {
|
||||
return cylinder_vector;
|
||||
}
|
||||
|
||||
blitz::TinyVector<double, 3> CylinderVector::cylinderEntries() const {
|
||||
return cylinder_vector;
|
||||
}
|
||||
|
||||
void CylinderVector::add(const Vector& vector, const Coordinate& coordinate) {
|
||||
cylinder_vector += vector.cylinderEntries(coordinate);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
CartesianVector::CartesianVector(
|
||||
const blitz::TinyVector<double, 3> cartesian_vector)
|
||||
: cartesian_vector(cartesian_vector){};
|
||||
|
||||
CartesianVector::CartesianVector(const Vector& vector,
|
||||
const Coordinate& coordinate)
|
||||
: cartesian_vector(vector.cartesianEntries(coordinate)) {}
|
||||
|
||||
CartesianVector::CartesianVector(const CartesianVector& vector)
|
||||
: cartesian_vector(vector.cartesianEntries()) {}
|
||||
|
||||
blitz::TinyVector<double, 3> CartesianVector::cartesianEntries(
|
||||
const Coordinate& coordinate) const {
|
||||
return cartesian_vector;
|
||||
}
|
||||
|
||||
blitz::TinyVector<double, 3> CartesianVector::cartesianEntries() const {
|
||||
return cartesian_vector;
|
||||
}
|
||||
|
||||
blitz::TinyVector<double, 3> CartesianVector::cylinderEntries(
|
||||
const Coordinate& coordinate) const {
|
||||
const blitz::TinyVector<double, 3> cylinder_vector =
|
||||
coordinate.cylinderEntries();
|
||||
|
||||
const double A_rho =
|
||||
std::cos(coordinate.cylinderEntries()(1)) * cylinder_vector(0) +
|
||||
std::sin(coordinate.cylinderEntries()(1)) * cylinder_vector(1);
|
||||
const double A_phi =
|
||||
(-1.) * std::sin(coordinate.cylinderEntries()(1)) * cylinder_vector(0) +
|
||||
std::cos(coordinate.cylinderEntries()(1)) * cylinder_vector(1);
|
||||
const double A_z = cylinder_vector(2);
|
||||
return blitz::TinyVector<double, 3>(A_rho, A_phi, A_z);
|
||||
}
|
||||
|
||||
void CartesianVector::add(const Vector& vector, const Coordinate& coordinate) {
|
||||
cartesian_vector += vector.cartesianEntries(coordinate);
|
||||
}
|
||||
Executable
+84
@@ -0,0 +1,84 @@
|
||||
#include "simulation/cpu_synchronizer.h"
|
||||
|
||||
#include <mpi.h>
|
||||
#include "simulation/current_collector.h"
|
||||
#include "simulation/density_field.h"
|
||||
#include "simulation/logger.h"
|
||||
#include "simulation/species.h"
|
||||
|
||||
void CpuSynchronizer::sumToAll(blitz::Array<double, 4>& array) {
|
||||
int size = array.size();
|
||||
MPI_Allreduce(MPI_IN_PLACE, array.data(), size, MPI_DOUBLE, MPI_SUM,
|
||||
MPI_COMM_WORLD);
|
||||
}
|
||||
|
||||
void CpuSynchronizer::sumToAll(blitz::Array<long int, 4>& array) {
|
||||
int size = array.size();
|
||||
MPI_Allreduce(MPI_IN_PLACE, array.data(), size, MPI_LONG, MPI_SUM,
|
||||
MPI_COMM_WORLD);
|
||||
}
|
||||
|
||||
void CpuSynchronizer::sumToAll(blitz::Array<double, 3>& array) {
|
||||
int size = array.size();
|
||||
MPI_Allreduce(MPI_IN_PLACE, array.data(), size, MPI_DOUBLE, MPI_SUM,
|
||||
MPI_COMM_WORLD);
|
||||
}
|
||||
|
||||
void CpuSynchronizer::sumToAll(double& value) {
|
||||
int size = 1;
|
||||
MPI_Allreduce(MPI_IN_PLACE, &value, size, MPI_DOUBLE, MPI_SUM,
|
||||
MPI_COMM_WORLD);
|
||||
}
|
||||
|
||||
void CpuSynchronizer::sumToAll(long int& value) {
|
||||
int size = 1;
|
||||
MPI_Allreduce(MPI_IN_PLACE, &value, size, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
|
||||
}
|
||||
|
||||
void CpuSynchronizer::synchronize(Timer* timer) {
|
||||
double time_s = timer->time();
|
||||
sumToAll(time_s);
|
||||
timer->setTime(time_s);
|
||||
}
|
||||
|
||||
void CpuSynchronizer::synchronize(DynamicDensityField* field) {
|
||||
if (field) {
|
||||
sumToAll(field->time_in_bin_s_);
|
||||
sumToAll(field->deleted_particles_);
|
||||
}
|
||||
}
|
||||
|
||||
void CpuSynchronizer::synchronize(CurrentCollector* collector) {
|
||||
if (collector) {
|
||||
sumToAll(collector->z_negative_crossings);
|
||||
sumToAll(collector->z_positive_crossings);
|
||||
sumToAll(collector->phi_negative_crossings);
|
||||
sumToAll(collector->phi_positive_crossings);
|
||||
sumToAll(collector->r_negative_crossings);
|
||||
sumToAll(collector->r_positive_crossings);
|
||||
}
|
||||
}
|
||||
|
||||
void CpuSynchronizer::synchronize(DeletionLogger* logger) {
|
||||
if (logger) {
|
||||
for (auto& pair : logger->num_deletions) {
|
||||
long int num_deletion = pair.second;
|
||||
sumToAll(num_deletion);
|
||||
pair.second = num_deletion;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CpuSynchronizer::synchronize(
|
||||
std::map<std::string, std::shared_ptr<species::TrackedSpecie>>&
|
||||
tracked_species) {
|
||||
MPI_Barrier(MPI_COMM_WORLD);
|
||||
auto it = tracked_species.begin();
|
||||
synchronize(it->second->dynamicDensityField()->timer_.get());
|
||||
|
||||
for (auto& pair : tracked_species) {
|
||||
synchronize(pair.second->dynamicDensityField());
|
||||
synchronize(pair.second->currentCollector());
|
||||
synchronize(pair.second->deletionLogger());
|
||||
}
|
||||
}
|
||||
Executable
+242
@@ -0,0 +1,242 @@
|
||||
#include "simulation/current_collector.h"
|
||||
|
||||
CurrentCollector::CurrentCollector(const CylinderBinning& position_binning,
|
||||
std::shared_ptr<Timer> timer)
|
||||
: position_binning_(position_binning),
|
||||
timer_(timer),
|
||||
z_negative_crossings(createCrossingsLongitudinal(position_binning)),
|
||||
z_positive_crossings(createCrossingsLongitudinal(position_binning)),
|
||||
collector_areas_z(createAreaLongitudinal(position_binning)),
|
||||
r_negative_crossings(createCrossingsRadial(position_binning)),
|
||||
r_positive_crossings(createCrossingsRadial(position_binning)),
|
||||
collector_areas_r(createAreaRadial(position_binning)),
|
||||
phi_negative_crossings(createCrossingsAzimuthal(position_binning)),
|
||||
phi_positive_crossings(createCrossingsAzimuthal(position_binning)),
|
||||
collector_areas_phi(createAreaAzimuthal(position_binning)) {}
|
||||
|
||||
blitz::Array<double, 3> CurrentCollector::createAreaLongitudinal(
|
||||
const CylinderBinning& position_binning) {
|
||||
const blitz::TinyVector<int, 3> bin_index_min =
|
||||
position_binning.binIndexMin();
|
||||
const blitz::TinyVector<int, 3> bin_index_max =
|
||||
position_binning.binIndexMax();
|
||||
|
||||
blitz::Array<double, 3> area_z(
|
||||
blitz::Range(bin_index_min(0), bin_index_max(0)),
|
||||
blitz::Range(bin_index_min(1), bin_index_max(1)),
|
||||
blitz::Range(0, bin_index_max(2)));
|
||||
|
||||
for (int ir = 0; ir <= area_z.ubound(0); ++ir) {
|
||||
const double dr = position_binning.stepsize()(0);
|
||||
const double r1 = ir * dr;
|
||||
const double r2 = (ir + 1) * dr;
|
||||
const double area_element =
|
||||
(std::pow(r2, 2) - std::pow(r1, 2)) * M_PI / (area_z.extent(1));
|
||||
area_z(ir, blitz::Range::all(), blitz::Range::all()) = area_element;
|
||||
}
|
||||
return area_z;
|
||||
}
|
||||
|
||||
blitz::Array<double, 3> CurrentCollector::createCrossingsLongitudinal(
|
||||
const CylinderBinning& position_binning) {
|
||||
const blitz::TinyVector<int, 3> bin_index_min =
|
||||
position_binning.binIndexMin();
|
||||
const blitz::TinyVector<int, 3> bin_index_max =
|
||||
position_binning.binIndexMax();
|
||||
|
||||
blitz::Array<double, 3> crossings_longitudinal(
|
||||
blitz::Range(bin_index_min(0), bin_index_max(0)),
|
||||
blitz::Range(bin_index_min(1), bin_index_max(1)),
|
||||
blitz::Range(0, bin_index_max(2)));
|
||||
crossings_longitudinal = 0.;
|
||||
return crossings_longitudinal;
|
||||
}
|
||||
|
||||
blitz::Array<double, 3> CurrentCollector::createAreaRadial(
|
||||
const CylinderBinning& position_binning) {
|
||||
const blitz::TinyVector<int, 3> bin_index_min =
|
||||
position_binning.binIndexMin();
|
||||
const blitz::TinyVector<int, 3> bin_index_max =
|
||||
position_binning.binIndexMax();
|
||||
|
||||
blitz::Array<double, 3> area_r(
|
||||
blitz::Range(bin_index_min(0), bin_index_max(0) - 1),
|
||||
blitz::Range(bin_index_min(1), bin_index_max(1)),
|
||||
blitz::Range(bin_index_min(2), bin_index_max(2)));
|
||||
|
||||
for (int ir = 0; ir <= area_r.ubound(0); ++ir) {
|
||||
const double dr = position_binning.stepsize()(0);
|
||||
const double dz = position_binning.stepsize()(2);
|
||||
const double r = (ir + 1) * dr;
|
||||
const double area_element = (2 * M_PI * r) / area_r.extent(1) * dz;
|
||||
|
||||
area_r(ir, blitz::Range::all(), blitz::Range::all()) = area_element;
|
||||
}
|
||||
return area_r;
|
||||
}
|
||||
|
||||
blitz::Array<double, 3> CurrentCollector::createCrossingsRadial(
|
||||
const CylinderBinning& position_binning) {
|
||||
const blitz::TinyVector<int, 3> bin_index_min =
|
||||
position_binning.binIndexMin();
|
||||
const blitz::TinyVector<int, 3> bin_index_max =
|
||||
position_binning.binIndexMax();
|
||||
|
||||
blitz::Array<double, 3> crossings_radial(
|
||||
blitz::Range(bin_index_min(0), bin_index_max(0) - 1),
|
||||
blitz::Range(bin_index_min(1), bin_index_max(1)),
|
||||
blitz::Range(bin_index_min(2), bin_index_max(2)));
|
||||
crossings_radial = 0.;
|
||||
return crossings_radial;
|
||||
}
|
||||
|
||||
blitz::Array<double, 3> CurrentCollector::createAreaAzimuthal(
|
||||
const CylinderBinning& position_binning) {
|
||||
const blitz::TinyVector<int, 3> bin_index_min =
|
||||
position_binning.binIndexMin();
|
||||
const blitz::TinyVector<int, 3> bin_index_max =
|
||||
position_binning.binIndexMax();
|
||||
|
||||
blitz::Array<double, 3> area_phi(
|
||||
blitz::Range(bin_index_min(0), bin_index_max(0)),
|
||||
blitz::Range(bin_index_min(1), bin_index_max(1)),
|
||||
blitz::Range(bin_index_min(2), bin_index_max(2)));
|
||||
|
||||
const double dr = position_binning.stepsize()(0);
|
||||
const double dz = position_binning.stepsize()(2);
|
||||
const double area_element = dr * dz;
|
||||
area_phi(blitz::Range::all(), blitz::Range::all(), blitz::Range::all()) =
|
||||
area_element;
|
||||
return area_phi;
|
||||
}
|
||||
|
||||
blitz::Array<double, 3> CurrentCollector::createCrossingsAzimuthal(
|
||||
const CylinderBinning& position_binning) {
|
||||
const blitz::TinyVector<int, 3> bin_index_min =
|
||||
position_binning.binIndexMin();
|
||||
const blitz::TinyVector<int, 3> bin_index_max =
|
||||
position_binning.binIndexMax();
|
||||
|
||||
blitz::Array<double, 3> crossings_azimuthal(
|
||||
blitz::Range(bin_index_min(0), bin_index_max(0)),
|
||||
blitz::Range(bin_index_min(1), bin_index_max(1)),
|
||||
blitz::Range(bin_index_min(2), bin_index_max(2)));
|
||||
crossings_azimuthal = 0;
|
||||
return crossings_azimuthal;
|
||||
}
|
||||
|
||||
void CurrentCollector::collect(particle::Particle* particle,
|
||||
const Coordinate& old_position) {
|
||||
blitz::TinyVector<int, 3> bin_old =
|
||||
position_binning_.bin(old_position.cylinderEntries());
|
||||
blitz::TinyVector<int, 3> bin_new =
|
||||
position_binning_.bin(particle->position().cylinderEntries());
|
||||
|
||||
collectRadialMovement(bin_old, bin_new);
|
||||
collectLongitudinalMovement(bin_old, bin_new);
|
||||
collectAzimuthalMovement(bin_old, bin_new);
|
||||
}
|
||||
|
||||
void CurrentCollector::collectRadialMovement(
|
||||
const blitz::TinyVector<int, 3>& bin_old,
|
||||
const blitz::TinyVector<int, 3>& bin_new) {
|
||||
if (bin_new(0) < bin_old(0)) {
|
||||
r_negative_crossings(bin_new(0), bin_old(1), bin_old(2)) += 1;
|
||||
} else if (bin_new(0) > bin_old(0)) {
|
||||
r_positive_crossings(bin_new(0) - 1, bin_old(1), bin_old(2)) += 1;
|
||||
}
|
||||
}
|
||||
void CurrentCollector::collectLongitudinalMovement(
|
||||
const blitz::TinyVector<int, 3>& bin_old,
|
||||
const blitz::TinyVector<int, 3>& bin_new) {
|
||||
if (bin_new(2) < bin_old(2)) {
|
||||
z_negative_crossings(bin_old(0), bin_old(1), bin_new(2) + 1) += 1;
|
||||
} else if (bin_new(2) > bin_old(2)) {
|
||||
z_positive_crossings(bin_old(0), bin_old(1), bin_new(2)) += 1;
|
||||
}
|
||||
}
|
||||
void CurrentCollector::collectAzimuthalMovement(
|
||||
const blitz::TinyVector<int, 3>& bin_old,
|
||||
const blitz::TinyVector<int, 3>& bin_new) {
|
||||
if ((bin_new(1) < bin_old(1)) ||
|
||||
((bin_old(1) == 0) &&
|
||||
(bin_new(1) == position_binning_.binIndexMax()(1)))) {
|
||||
phi_negative_crossings(bin_old(0), bin_new(1), bin_old(2)) += 1;
|
||||
} else if ((bin_new(1) > bin_old(1)) ||
|
||||
((bin_old(1) == position_binning_.binIndexMax()(1)) &&
|
||||
(bin_new(1) == 0))) {
|
||||
phi_positive_crossings(bin_old(0), bin_new(1) - 1, bin_old(2)) += 1;
|
||||
}
|
||||
}
|
||||
|
||||
blitz::Array<double, 3> CurrentCollector::negativeCrossingsInZ() const {
|
||||
return z_negative_crossings;
|
||||
}
|
||||
blitz::Array<double, 3> CurrentCollector::positiveCrossingsInZ() const {
|
||||
return z_positive_crossings;
|
||||
}
|
||||
|
||||
blitz::Array<double, 3> CurrentCollector::negativeCrossingsInPhi() const {
|
||||
return phi_negative_crossings;
|
||||
}
|
||||
blitz::Array<double, 3> CurrentCollector::positiveCrossingsInPhi() const {
|
||||
return phi_positive_crossings;
|
||||
}
|
||||
|
||||
blitz::Array<double, 3> CurrentCollector::negativeCrossingsInR() const {
|
||||
return r_negative_crossings;
|
||||
}
|
||||
blitz::Array<double, 3> CurrentCollector::positiveCrossingsInR() const {
|
||||
return r_positive_crossings;
|
||||
}
|
||||
|
||||
blitz::Array<double, 3> CurrentCollector::areaInR() const {
|
||||
return collector_areas_r;
|
||||
}
|
||||
blitz::Array<double, 3> CurrentCollector::areaInPhi() const {
|
||||
return collector_areas_phi;
|
||||
}
|
||||
blitz::Array<double, 3> CurrentCollector::areaInZ() const {
|
||||
return collector_areas_z;
|
||||
}
|
||||
|
||||
blitz::Array<double, 3> CurrentCollector::currentInZ() const {
|
||||
blitz::Array<double, 3> current_in_z = z_positive_crossings.copy();
|
||||
current_in_z(blitz::Range::all(), blitz::Range::all(), blitz::Range::all()) =
|
||||
(z_positive_crossings(blitz::Range::all(), blitz::Range::all(),
|
||||
blitz::Range::all()) -
|
||||
z_negative_crossings(blitz::Range::all(), blitz::Range::all(),
|
||||
blitz::Range::all())) /
|
||||
(physicalTime() * collector_areas_z(blitz::Range::all(),
|
||||
blitz::Range::all(),
|
||||
blitz::Range::all()));
|
||||
return current_in_z;
|
||||
}
|
||||
blitz::Array<double, 3> CurrentCollector::currentInPhi() const {
|
||||
blitz::Array<double, 3> current_in_phi = phi_positive_crossings.copy();
|
||||
current_in_phi(blitz::Range::all(), blitz::Range::all(),
|
||||
blitz::Range::all()) =
|
||||
(phi_positive_crossings(blitz::Range::all(), blitz::Range::all(),
|
||||
blitz::Range::all()) -
|
||||
phi_negative_crossings(blitz::Range::all(), blitz::Range::all(),
|
||||
blitz::Range::all())) /
|
||||
(physicalTime() * collector_areas_phi(blitz::Range::all(),
|
||||
blitz::Range::all(),
|
||||
blitz::Range::all()));
|
||||
return current_in_phi;
|
||||
}
|
||||
blitz::Array<double, 3> CurrentCollector::currentInR() const {
|
||||
blitz::Array<double, 3> current_in_r = r_positive_crossings.copy();
|
||||
current_in_r(blitz::Range::all(), blitz::Range::all(), blitz::Range::all()) =
|
||||
(r_positive_crossings(blitz::Range::all(), blitz::Range::all(),
|
||||
blitz::Range::all()) -
|
||||
r_negative_crossings(blitz::Range::all(), blitz::Range::all(),
|
||||
blitz::Range::all())) /
|
||||
(physicalTime() * collector_areas_r(blitz::Range::all(),
|
||||
blitz::Range::all(),
|
||||
blitz::Range::all()));
|
||||
return current_in_r;
|
||||
}
|
||||
double CurrentCollector::physicalTime() const {
|
||||
return timer_->time();
|
||||
}
|
||||
Executable
+223
@@ -0,0 +1,223 @@
|
||||
#include "simulation/density_field.h"
|
||||
|
||||
#include "simulation/coordinates.h"
|
||||
#include "simulation/errors.h"
|
||||
#include "simulation/event_position_distribution.h"
|
||||
#include "simulation/scalar_field.h"
|
||||
|
||||
template <class T>
|
||||
blitz::Array<T, 4> initiate4DArray(blitz::Range radial,
|
||||
blitz::Range phi,
|
||||
blitz::Range longitudinal,
|
||||
blitz::Range energy) {
|
||||
blitz::Array<T, 4> data(radial, phi, longitudinal, energy);
|
||||
data = static_cast<T>(0);
|
||||
return data.copy();
|
||||
}
|
||||
|
||||
blitz::Array<double, 3> initiateVolumeArray(
|
||||
blitz::Range radial,
|
||||
blitz::Range phi,
|
||||
blitz::Range longitudinal,
|
||||
const blitz::TinyVector<double, 3>& position_spacing) {
|
||||
blitz::Array<double, 3> volume(radial, phi, longitudinal);
|
||||
|
||||
for (int ir = 0; ir <= volume.ubound(0); ++ir) {
|
||||
const double r1 = ir * position_spacing(0);
|
||||
const double r2 = (ir + 1) * position_spacing(0);
|
||||
for (int iz = volume.lbound(2); iz <= volume.ubound(2); ++iz) {
|
||||
const double z1 = iz * position_spacing(2);
|
||||
const double z2 = (iz + 1) * position_spacing(2);
|
||||
|
||||
const double bin_volume =
|
||||
M_PI * (std::pow(r2, 2) - std::pow(r1, 2)) * (z2 - z1) / phi.length();
|
||||
if (ir == 0 && iz == -1) {
|
||||
}
|
||||
volume(ir, blitz::Range::all(), iz) = bin_volume;
|
||||
}
|
||||
}
|
||||
return volume.copy();
|
||||
}
|
||||
|
||||
DynamicDensityField::DynamicDensityField(
|
||||
const CylinderBinning& position_binning,
|
||||
const EnergyBinning& energy_binning,
|
||||
std::shared_ptr<Timer> timer)
|
||||
: position_binning_(position_binning),
|
||||
energy_binning_(energy_binning),
|
||||
timer_(timer),
|
||||
time_in_bin_s_(initiate4DArray<double>(
|
||||
blitz::Range(position_binning.radial_binning.binIndexMin(),
|
||||
position_binning.radial_binning.binIndexMax()),
|
||||
blitz::Range(position_binning.azimuth_binning.binIndexMin(),
|
||||
position_binning.azimuth_binning.binIndexMax()),
|
||||
blitz::Range(position_binning.longitudinal_binning.binIndexMin(),
|
||||
position_binning.longitudinal_binning.binIndexMax()),
|
||||
blitz::Range(energy_binning.binIndexMin(),
|
||||
energy_binning.binIndexMax()))),
|
||||
deleted_particles_(initiate4DArray<long int>(
|
||||
blitz::Range(position_binning.radial_binning.binIndexMin(),
|
||||
position_binning.radial_binning.binIndexMax()),
|
||||
blitz::Range(position_binning.azimuth_binning.binIndexMin(),
|
||||
position_binning.azimuth_binning.binIndexMax()),
|
||||
blitz::Range(position_binning.longitudinal_binning.binIndexMin(),
|
||||
position_binning.longitudinal_binning.binIndexMax()),
|
||||
blitz::Range(energy_binning.binIndexMin(),
|
||||
energy_binning.binIndexMax()))),
|
||||
density_per_m3_(initiate4DArray<double>(
|
||||
blitz::Range(position_binning.radial_binning.binIndexMin(),
|
||||
position_binning.radial_binning.binIndexMax()),
|
||||
blitz::Range(position_binning.azimuth_binning.binIndexMin(),
|
||||
position_binning.azimuth_binning.binIndexMax()),
|
||||
blitz::Range(position_binning.longitudinal_binning.binIndexMin(),
|
||||
position_binning.longitudinal_binning.binIndexMax()),
|
||||
blitz::Range(energy_binning.binIndexMin(),
|
||||
energy_binning.binIndexMax()))),
|
||||
bin_volume_m3_(initiateVolumeArray(
|
||||
blitz::Range(position_binning.radial_binning.binIndexMin(),
|
||||
position_binning.radial_binning.binIndexMax()),
|
||||
blitz::Range(position_binning.azimuth_binning.binIndexMin(),
|
||||
position_binning.azimuth_binning.binIndexMax()),
|
||||
blitz::Range(position_binning.longitudinal_binning.binIndexMin(),
|
||||
position_binning.longitudinal_binning.binIndexMax()),
|
||||
position_binning.stepsize())) {
|
||||
if (!timer) {
|
||||
ERROR("Timer is necessary for calculation of density field");
|
||||
}
|
||||
}
|
||||
|
||||
bool DynamicDensityField::changedPositionBin(
|
||||
const Coordinate& old_position,
|
||||
const Coordinate& new_position) const {
|
||||
const blitz::TinyVector<int, 3> old_bin =
|
||||
position_binning_.bin(old_position.cylinderEntries());
|
||||
|
||||
const blitz::TinyVector<int, 3> new_bin =
|
||||
position_binning_.bin(new_position.cylinderEntries());
|
||||
|
||||
return !((old_bin(0) == new_bin(0)) && (old_bin(1) == new_bin(1)) &&
|
||||
(old_bin(2) == new_bin(2)));
|
||||
}
|
||||
|
||||
bool DynamicDensityField::changedEnergyBin(double old_energy_eV,
|
||||
double new_energy_eV) const {
|
||||
const int old_bin = energy_binning_.bin(old_energy_eV);
|
||||
const int new_bin = energy_binning_.bin(new_energy_eV);
|
||||
return !(old_bin == new_bin);
|
||||
}
|
||||
|
||||
void DynamicDensityField::logMovement(const Coordinate& position,
|
||||
double energy_eV,
|
||||
double time_in_bin_s) {
|
||||
const blitz::TinyVector<int, 3> position_bin =
|
||||
position_binning_.bin(position.cylinderEntries());
|
||||
const int energy_bin = energy_binning_.bin(energy_eV);
|
||||
|
||||
time_in_bin_s_(position_bin(0), position_bin(1), position_bin(2),
|
||||
energy_bin) += time_in_bin_s;
|
||||
}
|
||||
|
||||
void DynamicDensityField::logDeletion(const Coordinate& position,
|
||||
double energy_eV) {
|
||||
const blitz::TinyVector<int, 3> position_bin =
|
||||
position_binning_.bin(position.cylinderEntries());
|
||||
const int energy_bin = energy_binning_.bin(energy_eV);
|
||||
|
||||
deleted_particles_(position_bin(0), position_bin(1), position_bin(2),
|
||||
energy_bin) += 1;
|
||||
}
|
||||
|
||||
double DynamicDensityField::density(const Coordinate& position) {
|
||||
const blitz::TinyVector<int, 3> position_bin =
|
||||
position_binning_.bin(position.cylinderEntries());
|
||||
|
||||
const double t = timer_->time();
|
||||
const double time_in_bin = blitz::sum(time_in_bin_s_(
|
||||
position_bin(0), position_bin(1), position_bin(2), blitz::Range::all()));
|
||||
const long int deleted_particles = blitz::sum(deleted_particles_(
|
||||
position_bin(0), position_bin(1), position_bin(2), blitz::Range::all()));
|
||||
|
||||
return (time_in_bin / t - deleted_particles) /
|
||||
bin_volume_m3_(position_bin(0), position_bin(1), position_bin(2));
|
||||
}
|
||||
|
||||
double DynamicDensityField::density(const Coordinate& position,
|
||||
double energy_eV) {
|
||||
const blitz::TinyVector<int, 3> position_bin =
|
||||
position_binning_.bin(position.cylinderEntries());
|
||||
|
||||
const int energy_bin = energy_binning_.bin(energy_eV);
|
||||
const double t = timer_->time();
|
||||
const double time_in_bin = time_in_bin_s_(position_bin(0), position_bin(1),
|
||||
position_bin(2), energy_bin);
|
||||
const long deleted_particles = deleted_particles_(
|
||||
position_bin(0), position_bin(1), position_bin(2), energy_bin);
|
||||
|
||||
return (time_in_bin / t - deleted_particles) /
|
||||
bin_volume_m3_(position_bin(0), position_bin(1), position_bin(2));
|
||||
}
|
||||
|
||||
blitz::Array<double, 3> DynamicDensityField::densityArray() {
|
||||
recalculateDensityArray();
|
||||
|
||||
blitz::Array<double, 3> density(density_per_m3_(
|
||||
blitz::Range::all(), blitz::Range::all(), blitz::Range::all(), 0));
|
||||
blitz::fourthIndex iE;
|
||||
density = blitz::sum(density_per_m3_, iE);
|
||||
return density.copy();
|
||||
}
|
||||
|
||||
blitz::Array<double, 4> DynamicDensityField::spectrumArray() {
|
||||
recalculateDensityArray();
|
||||
return density_per_m3_;
|
||||
}
|
||||
|
||||
void DynamicDensityField::recalculateDensityArray() {
|
||||
blitz::firstIndex ir;
|
||||
blitz::secondIndex iphi;
|
||||
blitz::thirdIndex iz;
|
||||
blitz::fourthIndex iE;
|
||||
|
||||
const double t = timer_->time();
|
||||
density_per_m3_ = (time_in_bin_s_(ir, iphi, iz, iE) / t -
|
||||
deleted_particles_(ir, iphi, iz, iE)) /
|
||||
bin_volume_m3_(ir, iphi, iz);
|
||||
}
|
||||
|
||||
blitz::Array<double, 4> DynamicDensityField::timeInBinArray() const {
|
||||
return time_in_bin_s_;
|
||||
}
|
||||
blitz::Array<long int, 4> DynamicDensityField::deletedParticlesArray() const {
|
||||
return deleted_particles_;
|
||||
}
|
||||
blitz::Array<double, 3> DynamicDensityField::binVolumeArray() const {
|
||||
return bin_volume_m3_;
|
||||
}
|
||||
|
||||
const EnergyBinning* DynamicDensityField::energyBinning() const {
|
||||
return &energy_binning_;
|
||||
}
|
||||
|
||||
const CylinderBinning* DynamicDensityField::positionBinning() const {
|
||||
return &position_binning_;
|
||||
}
|
||||
|
||||
double DynamicDensityField::physicalTime() const {
|
||||
return timer_->time();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
ConstantDensityField::ConstantDensityField(std::shared_ptr<ScalarField> density)
|
||||
: density_(density) {}
|
||||
|
||||
double ConstantDensityField::density(const Coordinate& position) {
|
||||
return density_->value(position);
|
||||
}
|
||||
|
||||
double ConstantDensityField::density(const Coordinate& position,
|
||||
double energy_eV) {
|
||||
return density_->value(position);
|
||||
}
|
||||
Executable
+10
@@ -0,0 +1,10 @@
|
||||
#include "simulation/drift_velocity.h"
|
||||
|
||||
#include "simulation/vector_field.h"
|
||||
|
||||
DriftVelocity::DriftVelocity(std::shared_ptr<VectorField> drift_velocity)
|
||||
: drift_velocity_(drift_velocity) {}
|
||||
|
||||
CartesianVector DriftVelocity::driftVelocity(const Coordinate& position) {
|
||||
return CartesianVector(drift_velocity_->value(position), position);
|
||||
}
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#include "simulation/electromagnetic_field.h"
|
||||
|
||||
ElectricField::ElectricField(std::shared_ptr<VectorField> background,
|
||||
std::shared_ptr<VectorField> dynamic)
|
||||
: background_(background), dynamic_(dynamic) {}
|
||||
|
||||
CylinderVector ElectricField::value(const Coordinate& position) const {
|
||||
return background_->value(position) + dynamic_->value(position);
|
||||
}
|
||||
|
||||
void ElectricField::updateDynamicField(std::shared_ptr<VectorField> field) {
|
||||
dynamic_ = field;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
MagneticField::MagneticField(std::shared_ptr<VectorField> field)
|
||||
: field_(field) {}
|
||||
|
||||
CylinderVector MagneticField::value(const Coordinate& position) const {
|
||||
return field_->value(position);
|
||||
}
|
||||
Executable
+47
@@ -0,0 +1,47 @@
|
||||
#include "simulation/energy_binning.h"
|
||||
#include <math.h>
|
||||
#include <algorithm>
|
||||
|
||||
std::vector<double> generateBinBorders(double bin_min,
|
||||
double bin_max,
|
||||
int num_bins) {
|
||||
const double min_bin_log = log10(bin_min);
|
||||
const double max_bin_log = log10(bin_max);
|
||||
|
||||
std::vector<double> bin_borders;
|
||||
for (int i = 0; i <= num_bins; ++i) {
|
||||
const double log_bin_border =
|
||||
(max_bin_log - min_bin_log) / num_bins * i + min_bin_log;
|
||||
bin_borders.push_back(pow(10, log_bin_border));
|
||||
}
|
||||
return bin_borders;
|
||||
}
|
||||
|
||||
EnergyBinning::EnergyBinning(double minimal_energy,
|
||||
double maximal_energy,
|
||||
int num_bins)
|
||||
: energy_bin_borders_(
|
||||
generateBinBorders(minimal_energy, maximal_energy, num_bins)) {}
|
||||
|
||||
EnergyBinning::EnergyBinning(std::vector<double> energy_bin_borders)
|
||||
: energy_bin_borders_(energy_bin_borders) {}
|
||||
|
||||
int EnergyBinning::bin(double energy_eV) const {
|
||||
auto const it = std::lower_bound(energy_bin_borders_.begin(),
|
||||
energy_bin_borders_.end(), energy_eV);
|
||||
return (it - energy_bin_borders_.begin()) - 1;
|
||||
}
|
||||
|
||||
int EnergyBinning::numBins() const {
|
||||
return energy_bin_borders_.size() - 1;
|
||||
}
|
||||
std::vector<double> EnergyBinning::binBorders() const {
|
||||
return energy_bin_borders_;
|
||||
}
|
||||
|
||||
int EnergyBinning::binIndexMin() const {
|
||||
return -1;
|
||||
}
|
||||
int EnergyBinning::binIndexMax() const {
|
||||
return numBins();
|
||||
}
|
||||
Executable
+49
@@ -0,0 +1,49 @@
|
||||
#include "simulation/energy_distribution.h"
|
||||
|
||||
#include "simulation/errors.h"
|
||||
|
||||
#include "constants/T2_beta_decay.h"
|
||||
#include "constants/physical_constants.h"
|
||||
#include "simulation/kinematic.h"
|
||||
#include "simulation/tritium_spectrum.h"
|
||||
|
||||
TritiumBetaDecay::TritiumBetaDecay()
|
||||
: x_distribution_(
|
||||
std::uniform_real_distribution<double>(0., T2BetaDecay::E0_T)),
|
||||
y_distribution_(std::uniform_real_distribution<double>(
|
||||
0.,
|
||||
T2BetaDecay::spectrum_maximum)),
|
||||
tritium_spectrum_(tritiumDecayEnergySpectrum(1.)) {}
|
||||
|
||||
double TritiumBetaDecay::rollEnergy(std::mt19937* engine) {
|
||||
double random_number_x;
|
||||
double random_number_y;
|
||||
double return_value;
|
||||
do {
|
||||
random_number_x = x_distribution_(*engine);
|
||||
random_number_y = y_distribution_(*engine);
|
||||
} while (random_number_y > tritium_spectrum_(random_number_x));
|
||||
|
||||
return random_number_x;
|
||||
}
|
||||
|
||||
MaxwellBoltzmann::MaxwellBoltzmann(double temperature_K)
|
||||
: distribution_(std::gamma_distribution<double>(
|
||||
3. / 2.,
|
||||
kinematic::thermalEnergy(temperature_K))) {
|
||||
ASSERT_POSITIVE(
|
||||
&temperature_K,
|
||||
"Temperature for MaxwellBoltzmann distribution must be positive.");
|
||||
}
|
||||
|
||||
double MaxwellBoltzmann::rollEnergy(std::mt19937* engine) {
|
||||
return distribution_(*engine);
|
||||
}
|
||||
|
||||
Constant::Constant(double constant_eV) : constant_eV_(constant_eV) {
|
||||
ASSERT_POSITIVE(&constant_eV,
|
||||
"The energy for the constant distribution must be positive.");
|
||||
}
|
||||
double Constant::rollEnergy(std::mt19937* engine) {
|
||||
return constant_eV_;
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
#include "simulation/event_position_distribution.h"
|
||||
|
||||
EventPositionDistribution::EventPositionDistribution(
|
||||
std::shared_ptr<ScalarField> mother_density,
|
||||
Limit density,
|
||||
Limit r_position,
|
||||
Limit phi_position,
|
||||
Limit z_position)
|
||||
: mother_density_(mother_density),
|
||||
r_values(std::uniform_real_distribution<double>(r_position.min,
|
||||
r_position.max)),
|
||||
phi_values(std::uniform_real_distribution<double>(phi_position.min,
|
||||
phi_position.max)),
|
||||
z_values(std::uniform_real_distribution<double>(z_position.min,
|
||||
z_position.max)),
|
||||
density_values(
|
||||
std::uniform_real_distribution<double>(density.min, density.max)) {}
|
||||
|
||||
CylinderCoordinate EventPositionDistribution::rollPosition(
|
||||
std::mt19937* engine) {
|
||||
double random_number_z;
|
||||
double random_numer_r;
|
||||
double random_number_phi;
|
||||
double random_number_n;
|
||||
|
||||
double return_value;
|
||||
|
||||
do {
|
||||
random_number_z = z_values(*engine);
|
||||
random_numer_r = r_values(*engine);
|
||||
random_number_phi = phi_values(*engine);
|
||||
random_number_n = density_values(*engine);
|
||||
|
||||
return_value =
|
||||
mother_density_->value(CylinderCoordinate(blitz::TinyVector<double, 3>(
|
||||
random_numer_r, random_number_phi, random_number_z)));
|
||||
} while (random_number_n > return_value);
|
||||
|
||||
return CylinderCoordinate(blitz::TinyVector<double, 3>(
|
||||
random_numer_r, random_number_phi, random_number_z));
|
||||
}
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
#include "simulation/geometry.h"
|
||||
|
||||
Cylinder::Cylinder(double radius, double height)
|
||||
: radius_(radius), height_(height) {}
|
||||
|
||||
bool Cylinder::isInside(const Coordinate& position) const {
|
||||
const blitz::TinyVector<double, 3> cylinder_position =
|
||||
position.cylinderEntries();
|
||||
return (cylinder_position(0) < radius_) && (cylinder_position(2) < height_) &&
|
||||
(cylinder_position(2) > 0);
|
||||
}
|
||||
Executable
+58
@@ -0,0 +1,58 @@
|
||||
#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/velocity_distribution.h"
|
||||
|
||||
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) {
|
||||
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());
|
||||
}
|
||||
return particle::Particle(created_specie, position,
|
||||
CartesianVector(velocity));
|
||||
}
|
||||
|
||||
std::vector<particle::Particle> Injector::rollNewParticles(
|
||||
std::mt19937* engine) {
|
||||
CylinderCoordinate position = position_distribution_->rollPosition(engine);
|
||||
DEBUG_LEVEL_3("Injected z position: " +
|
||||
std::to_string(position.cylinderEntries()(2)));
|
||||
std::vector<particle::Particle> new_particles;
|
||||
for (auto& injector_info : injector_infos_) {
|
||||
new_particles.push_back(
|
||||
rollNewParticle(engine, position, injector_info.created_specie,
|
||||
injector_info.creation_velocity_distribution));
|
||||
}
|
||||
return new_particles;
|
||||
}
|
||||
|
||||
double Injector::timePassed() const {
|
||||
return 1 / activity_;
|
||||
}
|
||||
Executable
+59
@@ -0,0 +1,59 @@
|
||||
#include "simulation/interaction.h"
|
||||
|
||||
#include "constants/physical_constants.h"
|
||||
#include "simulation/coordinates.h"
|
||||
#include "simulation/density_field.h"
|
||||
#include "simulation/drift_velocity.h"
|
||||
#include "simulation/kinematic.h"
|
||||
#include "simulation/velocity_distribution.h"
|
||||
|
||||
namespace interaction {
|
||||
|
||||
InteractionOutput::InteractionOutput() : delete_current_particle_(false){};
|
||||
|
||||
void InteractionOutput::deleteCurrentParticle(bool new_decider) {
|
||||
delete_current_particle_ = new_decider;
|
||||
}
|
||||
|
||||
bool InteractionOutput::deleteCurrentParticle() {
|
||||
return delete_current_particle_;
|
||||
}
|
||||
|
||||
void InteractionOutput::add(const particle::Particle& new_particle) {
|
||||
new_particles_.push_back(new_particle);
|
||||
}
|
||||
|
||||
std::vector<particle::Particle> InteractionOutput::newParticles() {
|
||||
return new_particles_;
|
||||
}
|
||||
|
||||
Interaction::Interaction(std::function<double(double)> cross_section,
|
||||
std::shared_ptr<species::Specie> target)
|
||||
: cross_section_(cross_section), target_(target) {}
|
||||
|
||||
double Interaction::macroscopicCrossSection(particle::Particle& particle) {
|
||||
const auto x_particle = particle.position();
|
||||
const blitz::TinyVector<double, 3> v_particle =
|
||||
particle.velocity().cartesianEntries();
|
||||
const blitz::TinyVector<double, 3> v_partner =
|
||||
target_->driftVelocity()->driftVelocity(x_particle).cartesianEntries();
|
||||
|
||||
const blitz::TinyVector<double, 3> velocity_target_fixed =
|
||||
kinematic::velocityTargetFixed(v_particle, v_partner, particle.mass_kg(),
|
||||
target_->mass_kg());
|
||||
|
||||
const double kinetic_energy_eV =
|
||||
kinematic::kineticEnergy(velocity_target_fixed, particle.mass_kg()) *
|
||||
UnitConversion::JouleToeV;
|
||||
|
||||
const double density = target_->densityField()->density(x_particle);
|
||||
const double cross_section = microscopicCrossSection(kinetic_energy_eV);
|
||||
|
||||
return cross_section * density;
|
||||
};
|
||||
|
||||
double Interaction::microscopicCrossSection(double energy_eV) {
|
||||
return cross_section_(energy_eV);
|
||||
}
|
||||
|
||||
}; // namespace interaction
|
||||
Executable
+169
@@ -0,0 +1,169 @@
|
||||
#include "simulation/kinematic.h"
|
||||
#include "blitz/array.h"
|
||||
#include "constants/physical_constants.h"
|
||||
|
||||
namespace kinematic {
|
||||
|
||||
blitz::TinyVector<double, 3> velocityOfCenterOfMass(
|
||||
const blitz::TinyVector<double, 3>& velocity_1,
|
||||
const blitz::TinyVector<double, 3>& velocity_2,
|
||||
double mass_1,
|
||||
double mass_2) {
|
||||
return blitz::TinyVector<double, 3>(
|
||||
(mass_1 * velocity_1 + mass_2 * velocity_2) / (mass_1 + mass_2));
|
||||
}
|
||||
|
||||
blitz::TinyVector<double, 3> velocityTargetFixed(
|
||||
const blitz::TinyVector<double, 3>& velocity_1,
|
||||
const blitz::TinyVector<double, 3>& velocity_2,
|
||||
double mass_1,
|
||||
double mass_2) {
|
||||
return blitz::TinyVector<double, 3>(velocity_1 - velocity_2);
|
||||
};
|
||||
|
||||
blitz::TinyVector<double, 3> rotateVector(const blitz::TinyVector<double, 3>& v,
|
||||
double theta,
|
||||
double phi) {
|
||||
const double norm_value(norm(v));
|
||||
if (norm_value == 0) {
|
||||
return v;
|
||||
}
|
||||
const blitz::TinyVector<double, 3> n = v / norm_value;
|
||||
const double s = std::sqrt(1 - n(2) * n(2));
|
||||
if (s != 0) {
|
||||
const double vx =
|
||||
-(n(0) * n(2) / s * std::cos(phi) - n(1) / s * std::sin(phi)) *
|
||||
std::sin(theta) +
|
||||
n(0) * std::cos(theta);
|
||||
const double vy =
|
||||
-(n(1) * n(2) / s * std::cos(phi) + n(0) / s * std::sin(phi)) *
|
||||
std::sin(theta) +
|
||||
n(1) * std::cos(theta);
|
||||
const double vz =
|
||||
s * (std::sin(theta) * std::cos(phi)) + n(2) * std::cos(theta);
|
||||
return blitz::TinyVector<double, 3>(vx, vy, vz) * norm_value;
|
||||
} else {
|
||||
const double vx = std::sin(theta) * std::cos(phi);
|
||||
const double vy = std::sin(theta) * std::sin(phi);
|
||||
const double vz = std::cos(theta);
|
||||
return blitz::TinyVector<double, 3>(vx, vy, vz) * norm_value;
|
||||
}
|
||||
}
|
||||
|
||||
blitz::TinyVector<double, 3> rotateVelocityInCenterOfMassFrame(
|
||||
const blitz::TinyVector<double, 3>& velocity_1,
|
||||
const blitz::TinyVector<double, 3>& velocity_2,
|
||||
double mass_1,
|
||||
double mass_2,
|
||||
double theta,
|
||||
double phi) {
|
||||
const blitz::TinyVector<double, 3> v_center_of_mass =
|
||||
velocityOfCenterOfMass(velocity_1, velocity_2, mass_1, mass_2);
|
||||
const blitz::TinyVector<double, 3> v1_in_center_of_mass_frame =
|
||||
velocity_1 - v_center_of_mass;
|
||||
const blitz::TinyVector<double, 3> v1_rot_in_center_of_mass_frame =
|
||||
rotateVector(v1_in_center_of_mass_frame, theta, phi);
|
||||
return v1_rot_in_center_of_mass_frame + v_center_of_mass;
|
||||
}
|
||||
|
||||
double norm(const blitz::TinyVector<double, 3>& vector) {
|
||||
return std::sqrt(blitz::dot(vector, vector));
|
||||
}
|
||||
|
||||
double distanceInCylinder(const blitz::TinyVector<double, 3>& position_1,
|
||||
const blitz::TinyVector<double, 3>& position_2) {
|
||||
return std::sqrt(std::pow(position_1(0), 2) + std::pow(position_2(0), 2) -
|
||||
2 * position_1(0) * position_2(0) *
|
||||
std::cos(position_1(1) - position_2(1)) +
|
||||
std::pow(position_1(2) - position_2(2), 2));
|
||||
}
|
||||
|
||||
double kineticEnergy(const blitz::TinyVector<double, 3>& velocity,
|
||||
double mass) {
|
||||
return blitz::dot(velocity, velocity) * mass / 2.;
|
||||
}
|
||||
|
||||
double thermalEnergy(double temperature_K) {
|
||||
return temperature_K * UnitConversion::KToeV;
|
||||
}
|
||||
|
||||
double velocity(double energy_eV, double mass_kg) {
|
||||
return std::sqrt(2 * energy_eV / (UnitConversion::JouleToeV * mass_kg));
|
||||
}
|
||||
|
||||
double relativisticGamma(const blitz::TinyVector<double, 3>& velocity) {
|
||||
return std::sqrt(1. + blitz::dot(velocity, velocity) / std::pow(SI::c, 2));
|
||||
}
|
||||
|
||||
double relativisticGamma(double kin_energy_eV, double mass_eV) {
|
||||
return 1 + kin_energy_eV / mass_eV;
|
||||
}
|
||||
|
||||
double relativisticBeta(double velocity_ms) {
|
||||
return velocity_ms / SI::c;
|
||||
}
|
||||
|
||||
double relativisticBeta(double kin_energy_eV, double mass_eV) {
|
||||
const double gamma = relativisticGamma(kin_energy_eV, mass_eV);
|
||||
return std::sqrt(1 - std::pow(1 / gamma, 2));
|
||||
}
|
||||
|
||||
double relativisticMomentum(double energy_eV, double mass_eV) {
|
||||
return std::sqrt(energy_eV * energy_eV + 2.0 * energy_eV * mass_eV);
|
||||
}
|
||||
|
||||
blitz::TinyVector<double, 3> velocityVector(
|
||||
double energy_eV,
|
||||
const blitz::TinyVector<double, 3>& random_unit_vector,
|
||||
double mass_kg) {
|
||||
if (energy_eV <= 0.) {
|
||||
return blitz::TinyVector<double, 3>(0., 0., 0.);
|
||||
} else {
|
||||
return random_unit_vector * kinematic::velocity(energy_eV, mass_kg);
|
||||
}
|
||||
}
|
||||
|
||||
blitz::TinyVector<double, 3> relativisticBoost(
|
||||
const blitz::TinyVector<double, 3>& v1,
|
||||
const blitz::TinyVector<double, 3>& v2) {
|
||||
const blitz::TinyVector<double, 3> v1_rel = v1 / SI::c;
|
||||
const blitz::TinyVector<double, 3> v2_rel = v2 / SI::c;
|
||||
|
||||
const double udotv = blitz::dot(v1_rel, v2_rel);
|
||||
const double vsqr = blitz::dot(v2_rel, v2_rel);
|
||||
|
||||
if (vsqr == 0) {
|
||||
return v1;
|
||||
}
|
||||
|
||||
const blitz::TinyVector<double, 3> u_parallel = udotv / vsqr * v2_rel;
|
||||
const blitz::TinyVector<double, 3> u_perpendicular = v1_rel - u_parallel;
|
||||
|
||||
return SI::c * (v2_rel + u_parallel + std::sqrt(1 - vsqr) * u_perpendicular) /
|
||||
(1 + udotv);
|
||||
}
|
||||
|
||||
blitz::TinyVector<double, 3> perpendicularVector(
|
||||
const blitz::TinyVector<double, 3>& vector,
|
||||
const blitz::TinyVector<double, 3>& direction) {
|
||||
return vector - parallelVector(vector, direction);
|
||||
}
|
||||
|
||||
blitz::TinyVector<double, 3> parallelVector(
|
||||
const blitz::TinyVector<double, 3>& vector,
|
||||
const blitz::TinyVector<double, 3>& direction) {
|
||||
return blitz::dot(vector, direction) * direction /
|
||||
blitz::dot(direction, direction);
|
||||
}
|
||||
|
||||
blitz::TinyVector<double, 3> rotateVector(
|
||||
const blitz::TinyVector<double, 3>& vector,
|
||||
const blitz::TinyVector<double, 3>& rotation_axis,
|
||||
double angle) {
|
||||
const blitz::TinyVector<double, 3> n = rotation_axis / norm(rotation_axis);
|
||||
const blitz::TinyVector<double, 3> cross = blitz::cross(n, vector);
|
||||
return n * blitz::dot(n, vector) + std::cos(angle) * blitz::cross(cross, n) +
|
||||
std::sin(angle) * cross;
|
||||
}
|
||||
|
||||
}; // namespace kinematic
|
||||
Executable
+32
@@ -0,0 +1,32 @@
|
||||
#include "simulation/logger.h"
|
||||
|
||||
DeletionLogger::DeletionLogger(std::vector<DeletionType> used_types) {
|
||||
for (auto& type : used_types) {
|
||||
num_deletions[type] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void DeletionLogger::logDeletion(const Coordinate& coordinate,
|
||||
DeletionType type) {
|
||||
if (num_deletions.count(type) > 0) {
|
||||
num_deletions.at(type) += 1;
|
||||
}
|
||||
}
|
||||
|
||||
std::map<DeletionType, long int> DeletionLogger::numDeletions() const {
|
||||
return num_deletions;
|
||||
}
|
||||
|
||||
void DeletionLogger::numDeletions(long int new_value, DeletionType type) {
|
||||
if (num_deletions.count(type)) {
|
||||
num_deletions.at(type) = new_value;
|
||||
}
|
||||
}
|
||||
|
||||
long int DeletionLogger::numDeletions(DeletionType type) const {
|
||||
if (num_deletions.count(type)) {
|
||||
return num_deletions.at(type);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Executable
+283
@@ -0,0 +1,283 @@
|
||||
#include "simulation/mover.h"
|
||||
|
||||
#include "constants/physical_constants.h"
|
||||
#include "simulation/coordinates.h"
|
||||
#include "simulation/current_collector.h"
|
||||
#include "simulation/density_field.h"
|
||||
#include "simulation/errors.h"
|
||||
#include "simulation/kinematic.h"
|
||||
#include "simulation/particle.h"
|
||||
#include "simulation/reflector.h"
|
||||
#include "simulation/terminator.h"
|
||||
|
||||
#include "tools/simulation_time.h"
|
||||
|
||||
BorisMover::BorisMover(double maximal_time_step_s)
|
||||
: maximal_time_step_s_(maximal_time_step_s) {
|
||||
ASSERT_POSITIVE(&maximal_time_step_s_,
|
||||
"The maximal timestep for boris push must be positive.");
|
||||
}
|
||||
|
||||
void BorisMover::move(particle::Particle* particle,
|
||||
double mfp_m,
|
||||
ElectricField* electric_field,
|
||||
MagneticField* magnetic_field) {
|
||||
double leftover_distance = mfp_m;
|
||||
|
||||
auto old_position = particle->position();
|
||||
double old_kinetic_energy = particle->kineticEnergy_eV();
|
||||
|
||||
if (old_kinetic_energy < 1E-9) {
|
||||
// Guard against small enegies taking up much time without contributing to
|
||||
// density
|
||||
return;
|
||||
}
|
||||
|
||||
bool changed_position_bin = false;
|
||||
bool changed_energy_bin = false;
|
||||
bool left_simulation_domain = false;
|
||||
|
||||
while (
|
||||
!(changed_position_bin || changed_energy_bin || left_simulation_domain) &&
|
||||
(leftover_distance > 0.)) {
|
||||
old_position = particle->position();
|
||||
old_kinetic_energy = particle->kineticEnergy_eV();
|
||||
|
||||
double moved_distance = 0.;
|
||||
|
||||
const blitz::TinyVector<double, 3> E =
|
||||
electric_field->value(old_position).cartesianEntries(old_position);
|
||||
const blitz::TinyVector<double, 3> B =
|
||||
magnetic_field->value(old_position).cartesianEntries(old_position);
|
||||
|
||||
movementStep(particle, E, B, moved_distance);
|
||||
particle->increaseTimeAlive(maximal_time_step_s_);
|
||||
particle->increaseTimeInBin(maximal_time_step_s_);
|
||||
|
||||
auto new_position = particle->position();
|
||||
|
||||
changed_position_bin =
|
||||
particle->specie()->dynamicDensityField()->changedPositionBin(
|
||||
old_position, new_position);
|
||||
|
||||
changed_energy_bin =
|
||||
particle->specie()->dynamicDensityField()->changedEnergyBin(
|
||||
old_kinetic_energy, particle->kineticEnergy_eV());
|
||||
|
||||
left_simulation_domain =
|
||||
particle->specie()->terminator()->leftSimulationDomain(particle);
|
||||
|
||||
leftover_distance -= moved_distance;
|
||||
}
|
||||
|
||||
particle->specie()->dynamicDensityField()->logMovement(
|
||||
old_position, old_kinetic_energy, particle->timeInBin());
|
||||
particle->resetTimeInBin();
|
||||
particle->specie()->currentCollector()->collect(particle, old_position);
|
||||
}
|
||||
|
||||
void BorisMover::movementStep(
|
||||
particle::Particle* particle,
|
||||
const blitz::TinyVector<double, 3>& electric_field,
|
||||
const blitz::TinyVector<double, 3>& magnetic_field,
|
||||
double& moved_distance) const {
|
||||
const blitz::TinyVector<double, 3> v =
|
||||
particle->velocity().cartesianEntries();
|
||||
const double m = particle->mass_kg();
|
||||
const double q = particle->charge_C();
|
||||
|
||||
const double u_minus_x =
|
||||
v(0) + (q * electric_field(0) * maximal_time_step_s_) / (2. * m);
|
||||
const double u_minus_y =
|
||||
v(1) + (q * electric_field(1) * maximal_time_step_s_) / (2. * m);
|
||||
const double u_minus_z =
|
||||
v(2) + (q * electric_field(2) * maximal_time_step_s_) / (2. * m);
|
||||
|
||||
const double gamma = kinematic::relativisticGamma(
|
||||
blitz::TinyVector<double, 3>(u_minus_x, u_minus_y, u_minus_z));
|
||||
|
||||
const double temp_1_x =
|
||||
(q * magnetic_field(0) * maximal_time_step_s_) / (2 * m * gamma);
|
||||
const double temp_1_y =
|
||||
(q * magnetic_field(1) * maximal_time_step_s_) / (2 * m * gamma);
|
||||
const double temp_1_z =
|
||||
(q * magnetic_field(2) * maximal_time_step_s_) / (2 * m * gamma);
|
||||
|
||||
const double u_prime_x =
|
||||
u_minus_x + (u_minus_y * temp_1_z - u_minus_z * temp_1_y);
|
||||
const double u_prime_y =
|
||||
u_minus_y + (u_minus_z * temp_1_x - u_minus_x * temp_1_z);
|
||||
const double u_prime_z =
|
||||
u_minus_z + (u_minus_x * temp_1_y - u_minus_y * temp_1_x);
|
||||
|
||||
const double temp_2_x =
|
||||
(2. * temp_1_x) /
|
||||
(1 + temp_1_x * temp_1_x + temp_1_y * temp_1_y + temp_1_z * temp_1_z);
|
||||
const double temp_2_y =
|
||||
(2. * temp_1_y) /
|
||||
(1 + temp_1_x * temp_1_x + temp_1_y * temp_1_y + temp_1_z * temp_1_z);
|
||||
const double temp_2_z =
|
||||
(2. * temp_1_z) /
|
||||
(1 + temp_1_x * temp_1_x + temp_1_y * temp_1_y + temp_1_z * temp_1_z);
|
||||
|
||||
const double u_plus_x =
|
||||
u_minus_x + (u_prime_y * temp_2_z - u_prime_z * temp_2_y);
|
||||
const double u_plus_y =
|
||||
u_minus_y + (u_prime_z * temp_2_x - u_prime_x * temp_2_z);
|
||||
const double u_plus_z =
|
||||
u_minus_z + (u_prime_x * temp_2_y - u_prime_y * temp_2_x);
|
||||
|
||||
const double u_new_x =
|
||||
u_plus_x + (q * electric_field(0) * maximal_time_step_s_) / (2. * m);
|
||||
const double u_new_y =
|
||||
u_plus_y + (q * electric_field(1) * maximal_time_step_s_) / (2. * m);
|
||||
const double u_new_z =
|
||||
u_plus_z + (q * electric_field(2) * maximal_time_step_s_) / (2. * m);
|
||||
const blitz::TinyVector<double, 3> u_new(u_new_x, u_new_y, u_new_z);
|
||||
|
||||
/*
|
||||
Might be slower, but essentially the same code
|
||||
const blitz::TinyVector<double, 3> u_minus =
|
||||
v + (q * electric_field * maximal_time_step_s_) / (2. * m);
|
||||
const double gamma = kinematic::relativisticGamma(u_minus);;
|
||||
const blitz::TinyVector<double, 3> temp_1 =
|
||||
(q * magnetic_field * maximal_time_step_s_) / (2. * m * gamma);
|
||||
const blitz::TinyVector<double, 3> u_prime =
|
||||
u_minus + (blitz::cross(u_minus, temp_1));
|
||||
const blitz::TinyVector<double, 3> temp_2 =
|
||||
(2. * temp_1) / (1. + (blitz::dot(temp_1, temp_1)));
|
||||
const blitz::TinyVector<double, 3> u_plus =
|
||||
u_minus + (blitz::cross(u_prime, temp_2));
|
||||
const blitz::TinyVector<double, 3> u_new =
|
||||
u_plus + (q * electric_field * maximal_time_step_s_) / (2. * m);
|
||||
*/
|
||||
|
||||
particle->velocity(CartesianVector(u_new));
|
||||
|
||||
const CartesianCoordinate distance(u_new * maximal_time_step_s_);
|
||||
particle->move(distance);
|
||||
|
||||
moved_distance += std::sqrt(
|
||||
blitz::dot(distance.cartesianEntries(), distance.cartesianEntries()));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
DriftMover::DriftMover(double maximal_distance)
|
||||
: maximal_distance_m_(maximal_distance){};
|
||||
|
||||
void DriftMover::move(particle::Particle* particle,
|
||||
double mfp_m,
|
||||
ElectricField* electric_field,
|
||||
MagneticField* magnetic_field) {
|
||||
double leftover_distance = mfp_m;
|
||||
|
||||
auto old_position = particle->position();
|
||||
auto old_velocity = particle->velocity();
|
||||
double old_kinetic_energy = particle->kineticEnergy_eV();
|
||||
|
||||
bool changed_position_bin = false;
|
||||
bool changed_energy_bin = false;
|
||||
bool left_simulation_domain = false;
|
||||
|
||||
while (
|
||||
!(changed_position_bin || changed_energy_bin || left_simulation_domain) &&
|
||||
(leftover_distance > 0.)) {
|
||||
old_position = particle->position();
|
||||
old_velocity = particle->velocity();
|
||||
|
||||
old_kinetic_energy = particle->kineticEnergy_eV();
|
||||
|
||||
const blitz::TinyVector<double, 3> E =
|
||||
electric_field->value(old_position).cartesianEntries(old_position);
|
||||
|
||||
const blitz::TinyVector<double, 3> B =
|
||||
magnetic_field->value(old_position).cartesianEntries(old_position);
|
||||
const double B_norm = kinematic::norm(B);
|
||||
// Todo: add calculation of divB here
|
||||
// use Kassiopeia paper for reference
|
||||
|
||||
const blitz::TinyVector<double, 3> v_parallel =
|
||||
kinematic::parallelVector(old_velocity.cartesianEntries(), B);
|
||||
const double v_parallel_norm = kinematic::norm(v_parallel);
|
||||
|
||||
const blitz::TinyVector<double, 3> v_perp =
|
||||
kinematic::perpendicularVector(old_velocity.cartesianEntries(), B);
|
||||
const double v_perp_norm = kinematic::norm(v_perp);
|
||||
|
||||
const double gyro_radius = particle->mass_kg() * v_perp_norm /
|
||||
(B_norm * std::abs(particle->charge_C()));
|
||||
|
||||
const blitz::TinyVector<double, 3> velocity_normal =
|
||||
(-1) * blitz::cross(v_parallel, v_perp);
|
||||
|
||||
const blitz::TinyVector<double, 3> offset_to_gyro_center =
|
||||
gyro_radius * velocity_normal / kinematic::norm(velocity_normal);
|
||||
|
||||
const blitz::TinyVector<double, 3> v_drift = driftVelocity(B, E);
|
||||
const double v_drift_norm = kinematic::norm(v_drift);
|
||||
|
||||
const double stepsize = std::min(leftover_distance, maximal_distance_m_);
|
||||
const double timestep =
|
||||
stepsize / (v_parallel_norm + v_perp_norm + v_drift_norm);
|
||||
|
||||
const blitz::TinyVector<double, 3> movement_parallel =
|
||||
v_parallel * timestep;
|
||||
|
||||
const blitz::TinyVector<double, 3> movement_drift = v_drift * timestep;
|
||||
|
||||
const double gyro_frequency =
|
||||
particle->charge_C() * B_norm / particle->mass_kg();
|
||||
const double total_rotation_angle = gyro_frequency * timestep;
|
||||
const blitz::TinyVector<double, 3> movement_around_gyro_center =
|
||||
kinematic::rotateVector(offset_to_gyro_center, v_parallel,
|
||||
total_rotation_angle) -
|
||||
offset_to_gyro_center;
|
||||
|
||||
particle->move(CartesianCoordinate(movement_parallel + movement_drift +
|
||||
movement_around_gyro_center));
|
||||
|
||||
const blitz::TinyVector<double, 3> E_parallel =
|
||||
kinematic::parallelVector(E, B);
|
||||
const blitz::TinyVector<double, 3> dv_electric =
|
||||
timestep * particle->charge_C() * E_parallel / particle->mass_kg();
|
||||
|
||||
const blitz::TinyVector<double, 3> v_perp_rotated =
|
||||
kinematic::rotateVector(v_perp, v_parallel, total_rotation_angle);
|
||||
|
||||
particle->velocity(
|
||||
CartesianVector(v_parallel + v_perp_rotated + dv_electric));
|
||||
|
||||
particle->increaseTimeAlive(timestep);
|
||||
particle->increaseTimeInBin(timestep);
|
||||
|
||||
const auto new_position = particle->position();
|
||||
|
||||
changed_position_bin =
|
||||
particle->specie()->dynamicDensityField()->changedPositionBin(
|
||||
old_position, new_position);
|
||||
|
||||
changed_energy_bin =
|
||||
particle->specie()->dynamicDensityField()->changedEnergyBin(
|
||||
old_kinetic_energy, particle->kineticEnergy_eV());
|
||||
|
||||
left_simulation_domain =
|
||||
particle->specie()->terminator()->leftSimulationDomain(particle);
|
||||
|
||||
leftover_distance -= stepsize;
|
||||
}
|
||||
|
||||
particle->specie()->dynamicDensityField()->logMovement(
|
||||
old_position, old_kinetic_energy, particle->timeInBin());
|
||||
particle->resetTimeInBin();
|
||||
particle->specie()->currentCollector()->collect(particle, old_position);
|
||||
}
|
||||
|
||||
blitz::TinyVector<double, 3> DriftMover::driftVelocity(
|
||||
const blitz::TinyVector<double, 3>& magnetic_field,
|
||||
const blitz::TinyVector<double, 3>& electric_field) const {
|
||||
return blitz::cross(magnetic_field, electric_field) /
|
||||
blitz::dot(magnetic_field, magnetic_field);
|
||||
}
|
||||
Executable
+28
@@ -0,0 +1,28 @@
|
||||
#include "simulation/mpi_communication.h"
|
||||
|
||||
#include "mpi.h"
|
||||
|
||||
int MpiCommunication::mpi_rank = 0;
|
||||
int MpiCommunication::mpi_size = 1;
|
||||
|
||||
int MpiCommunication::mpiSize() {
|
||||
int size;
|
||||
MPI_Comm_size(MPI_COMM_WORLD, &size);
|
||||
return size;
|
||||
}
|
||||
|
||||
int MpiCommunication::mpiRank() {
|
||||
int rank;
|
||||
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
|
||||
return rank;
|
||||
}
|
||||
|
||||
void MpiCommunication::init(int argc, char* argv[]) {
|
||||
MPI_Init(&argc, &argv);
|
||||
mpi_rank = mpiRank();
|
||||
mpi_size = mpiSize();
|
||||
}
|
||||
|
||||
void MpiCommunication::finalize() {
|
||||
MPI_Finalize();
|
||||
}
|
||||
Executable
+71
@@ -0,0 +1,71 @@
|
||||
#include "simulation/parameter.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <stdexcept>
|
||||
#include "jansson.h"
|
||||
|
||||
#include "simulation/errors.h"
|
||||
#include "simulation/jansson_additionals.h"
|
||||
|
||||
Parameter::Parameter(std::string filename)
|
||||
: j_error_(new json_error_t()), j_file_(openParameterFile(filename)) {}
|
||||
|
||||
Parameter::~Parameter() {
|
||||
delete j_error_;
|
||||
delete j_file_;
|
||||
}
|
||||
|
||||
json_t* Parameter::openParameterFile(const std::string& filename) const {
|
||||
json_t* j_file =
|
||||
json_load_file(filename.c_str(), JSON_DECODE_INT_AS_REAL, j_error_);
|
||||
|
||||
if (!j_file) {
|
||||
ERROR("Can not load parameter file: " + filename);
|
||||
}
|
||||
return j_file;
|
||||
}
|
||||
|
||||
double Parameter::doubleValue(const json_path& path) const {
|
||||
return json_double(json_object_by_path(j_file_, path));
|
||||
}
|
||||
|
||||
double Parameter::doubleValue(const json_path& path,
|
||||
double default_value) const {
|
||||
return json_double(json_object_by_path(j_file_, path), default_value);
|
||||
}
|
||||
|
||||
int Parameter::intValue(const json_path& path) const {
|
||||
return json_int(json_object_by_path(j_file_, path));
|
||||
}
|
||||
|
||||
int Parameter::intValue(const json_path& path, int default_value) const {
|
||||
return json_int(json_object_by_path(j_file_, path), default_value);
|
||||
}
|
||||
|
||||
std::string Parameter::stringValue(const json_path& path) const {
|
||||
return json_string(json_object_by_path(j_file_, path));
|
||||
}
|
||||
|
||||
std::string Parameter::stringValue(const json_path& path,
|
||||
const std::string& default_value) const {
|
||||
return json_string(json_object_by_path(j_file_, path), default_value);
|
||||
}
|
||||
|
||||
bool Parameter::boolValue(const json_path& path) const {
|
||||
return json_bool(json_object_by_path(j_file_, path));
|
||||
}
|
||||
|
||||
bool Parameter::boolValue(const json_path& path, bool default_value) const {
|
||||
return json_bool(json_object_by_path(j_file_, path), default_value);
|
||||
}
|
||||
|
||||
bool Parameter::pathExists(const json_path& path) const {
|
||||
return json_object_by_path(j_file_, path) != nullptr;
|
||||
}
|
||||
|
||||
json_t* Parameter::jsonFile() const {
|
||||
return j_file_;
|
||||
}
|
||||
json_t* Parameter::jsonObjectFromBase(const json_path& path) const {
|
||||
return json_object_by_path(j_file_, path);
|
||||
}
|
||||
Executable
+113
@@ -0,0 +1,113 @@
|
||||
#include "simulation/particle.h"
|
||||
#include "constants/physical_constants.h"
|
||||
#include "simulation/species.h"
|
||||
|
||||
namespace particle {
|
||||
/*
|
||||
Particle::Particle(std::shared_ptr<species::TrackedSpecie> specie,
|
||||
Coordinate* position,
|
||||
Vector* velocity)
|
||||
: specie_(specie),
|
||||
position_(CylinderCoordinate(*position)),
|
||||
velocity_(CartesianVector(*velocity, *position)),
|
||||
id_(this->last_id_),
|
||||
time_alive_s_(0.),
|
||||
time_in_bin_s_(0.),
|
||||
interaction_counter_(0) {
|
||||
last_id_++;
|
||||
}
|
||||
*/
|
||||
|
||||
Particle::Particle(std::shared_ptr<species::TrackedSpecie> specie,
|
||||
const Coordinate& position,
|
||||
const Vector& velocity)
|
||||
: specie_(specie),
|
||||
position_(position),
|
||||
velocity_(velocity, position),
|
||||
id_(this->last_id_),
|
||||
time_alive_s_(0.),
|
||||
time_in_bin_s_(0.),
|
||||
interaction_counter_(0) {
|
||||
last_id_++;
|
||||
}
|
||||
|
||||
long int Particle::last_id_ = 0;
|
||||
|
||||
double Particle::mass_kg() const {
|
||||
return specie_->mass_kg();
|
||||
}
|
||||
double Particle::charge_C() const {
|
||||
return specie_->charge_C();
|
||||
}
|
||||
|
||||
void Particle::lastId(long int id) {
|
||||
last_id_ = id;
|
||||
}
|
||||
|
||||
long int Particle::lastId() {
|
||||
return last_id_;
|
||||
}
|
||||
|
||||
void Particle::velocity(const CartesianVector& new_velocity) {
|
||||
velocity_ = new_velocity;
|
||||
}
|
||||
|
||||
CartesianVector Particle::velocity() const {
|
||||
return velocity_;
|
||||
}
|
||||
|
||||
species::TrackedSpecie* Particle::specie() const {
|
||||
return specie_.get();
|
||||
}
|
||||
|
||||
void Particle::position(const Coordinate& new_position) {
|
||||
position_ = CartesianCoordinate(new_position);
|
||||
}
|
||||
|
||||
CartesianCoordinate Particle::position() const {
|
||||
return position_;
|
||||
}
|
||||
|
||||
void Particle::move(const Coordinate& distance) {
|
||||
position_.add(distance);
|
||||
}
|
||||
|
||||
double Particle::kineticEnergy_eV() const {
|
||||
return mass_kg() * absoluteVelocitySquared() * UnitConversion::JouleToeV / 2.;
|
||||
}
|
||||
|
||||
double Particle::absoluteVelocity() const {
|
||||
return std::sqrt(absoluteVelocitySquared());
|
||||
}
|
||||
|
||||
double Particle::absoluteVelocitySquared() const {
|
||||
return blitz::dot(velocity_.cartesianEntries(), velocity_.cartesianEntries());
|
||||
}
|
||||
|
||||
void Particle::increaseInteractionCounter() {
|
||||
interaction_counter_++;
|
||||
}
|
||||
long int Particle::interactionCounter() const {
|
||||
return interaction_counter_;
|
||||
}
|
||||
|
||||
void Particle::increaseTimeAlive(double time_s) {
|
||||
time_alive_s_ += time_s;
|
||||
}
|
||||
double Particle::timeAlive() const {
|
||||
return time_alive_s_;
|
||||
}
|
||||
|
||||
void Particle::increaseTimeInBin(double time_s) {
|
||||
time_in_bin_s_ += time_s;
|
||||
}
|
||||
|
||||
void Particle::resetTimeInBin() {
|
||||
time_in_bin_s_ = 0.;
|
||||
}
|
||||
|
||||
double Particle::timeInBin() const {
|
||||
return time_in_bin_s_;
|
||||
}
|
||||
|
||||
} // namespace particle
|
||||
Executable
+162
@@ -0,0 +1,162 @@
|
||||
#include "simulation/position_binning.h"
|
||||
#include "simulation/coordinates.h"
|
||||
|
||||
LowerAndUpperBoundBinning::LowerAndUpperBoundBinning(int num_bins,
|
||||
double maximum)
|
||||
: num_bins_(num_bins), stepsize_(maximum / num_bins) {}
|
||||
|
||||
int LowerAndUpperBoundBinning::bin(double position) const {
|
||||
const int bin = position / stepsize_;
|
||||
return std::max(0, std::min(bin, num_bins_ - 1));
|
||||
}
|
||||
|
||||
double LowerAndUpperBoundBinning::stepsize() const {
|
||||
return stepsize_;
|
||||
}
|
||||
|
||||
int LowerAndUpperBoundBinning::numBins() const {
|
||||
return num_bins_;
|
||||
}
|
||||
|
||||
std::vector<double> LowerAndUpperBoundBinning::binBorders() const {
|
||||
std::vector<double> bin_borders;
|
||||
for (int i = 0; i <= num_bins_; ++i) {
|
||||
bin_borders.push_back(i * stepsize_);
|
||||
}
|
||||
return bin_borders;
|
||||
}
|
||||
|
||||
int LowerAndUpperBoundBinning::binIndexMin() const {
|
||||
return 0;
|
||||
}
|
||||
int LowerAndUpperBoundBinning::binIndexMax() const {
|
||||
return num_bins_ - 1;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
LowerBoundBinningWithOverflow::LowerBoundBinningWithOverflow(
|
||||
int num_bins_excluding_overflow,
|
||||
double maximum)
|
||||
: num_bins_(num_bins_excluding_overflow + 1),
|
||||
stepsize_(maximum / num_bins_excluding_overflow) {}
|
||||
|
||||
int LowerBoundBinningWithOverflow::bin(double position) const {
|
||||
const int bin = position / stepsize_;
|
||||
return std::max(0, std::min(bin, num_bins_ - 1));
|
||||
}
|
||||
|
||||
double LowerBoundBinningWithOverflow::stepsize() const {
|
||||
return stepsize_;
|
||||
}
|
||||
|
||||
int LowerBoundBinningWithOverflow::numBinsWithOverflow() const {
|
||||
return num_bins_;
|
||||
}
|
||||
int LowerBoundBinningWithOverflow::numBinsWithoutOverflow() const {
|
||||
return num_bins_ - 1;
|
||||
}
|
||||
std::vector<double> LowerBoundBinningWithOverflow::binBorders() const {
|
||||
std::vector<double> bin_borders;
|
||||
for (int i = 0; i <= num_bins_ - 1; ++i) {
|
||||
bin_borders.push_back(i * stepsize_);
|
||||
}
|
||||
return bin_borders;
|
||||
}
|
||||
|
||||
int LowerBoundBinningWithOverflow::binIndexMin() const {
|
||||
return 0;
|
||||
}
|
||||
int LowerBoundBinningWithOverflow::binIndexMax() const {
|
||||
return num_bins_ - 1;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
BinningWithOverAndUnderflow::BinningWithOverAndUnderflow(
|
||||
int num_bins_excluding_overunderflow,
|
||||
double maximum)
|
||||
: num_bins_(num_bins_excluding_overunderflow + 2),
|
||||
stepsize_(maximum / num_bins_excluding_overunderflow){};
|
||||
|
||||
int BinningWithOverAndUnderflow::bin(double position) const {
|
||||
const int bin =
|
||||
position < 0 ? position / stepsize_ - 1 : position / stepsize_;
|
||||
return std::max(-1, std::min(bin, num_bins_ - 2));
|
||||
}
|
||||
|
||||
double BinningWithOverAndUnderflow::stepsize() const {
|
||||
return stepsize_;
|
||||
}
|
||||
int BinningWithOverAndUnderflow::numBinsWithOverUnderflow() const {
|
||||
return num_bins_;
|
||||
}
|
||||
int BinningWithOverAndUnderflow::numBinsWithoutOverUnderflow() const {
|
||||
return num_bins_ - 2;
|
||||
}
|
||||
|
||||
std::vector<double> BinningWithOverAndUnderflow::binBorders() const {
|
||||
std::vector<double> bin_borders;
|
||||
for (int i = 0; i <= num_bins_ - 2; ++i) {
|
||||
bin_borders.push_back(i * stepsize_);
|
||||
}
|
||||
return bin_borders;
|
||||
}
|
||||
|
||||
int BinningWithOverAndUnderflow::binIndexMin() const {
|
||||
return -1;
|
||||
}
|
||||
int BinningWithOverAndUnderflow::binIndexMax() const {
|
||||
return num_bins_ - 2;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
CylinderBinning::CylinderBinning(int num_bins_radial,
|
||||
double radius,
|
||||
int num_bins_azimuthal,
|
||||
int num_bins_longitudinal,
|
||||
double height)
|
||||
: radial_binning(num_bins_radial, radius),
|
||||
azimuth_binning(num_bins_azimuthal, 2 * M_PI),
|
||||
longitudinal_binning(num_bins_longitudinal, height) {}
|
||||
|
||||
blitz::TinyVector<int, 3> CylinderBinning::bin(
|
||||
const blitz::TinyVector<double, 3>& cylinder_position) const {
|
||||
return blitz::TinyVector<int, 3>(
|
||||
radial_binning.bin(cylinder_position(0)),
|
||||
azimuth_binning.bin(cylinder_position(1)),
|
||||
longitudinal_binning.bin(cylinder_position(2)));
|
||||
}
|
||||
|
||||
blitz::TinyVector<int, 3> CylinderBinning::numBins() const {
|
||||
return blitz::TinyVector<int, 3>(
|
||||
radial_binning.numBinsWithoutOverflow(), azimuth_binning.numBins(),
|
||||
longitudinal_binning.numBinsWithoutOverUnderflow());
|
||||
}
|
||||
|
||||
blitz::TinyVector<double, 3> CylinderBinning::stepsize() const {
|
||||
return blitz::TinyVector<double, 3>(radial_binning.stepsize(),
|
||||
azimuth_binning.stepsize(),
|
||||
longitudinal_binning.stepsize());
|
||||
}
|
||||
|
||||
blitz::TinyVector<int, 3> CylinderBinning::numBinsIncludingOverUnderflow()
|
||||
const {
|
||||
return blitz::TinyVector<int, 3>(
|
||||
radial_binning.numBinsWithOverflow(), azimuth_binning.numBins(),
|
||||
longitudinal_binning.numBinsWithOverUnderflow());
|
||||
}
|
||||
blitz::TinyVector<int, 3> CylinderBinning::binIndexMin() const {
|
||||
return blitz::TinyVector<int, 3>(radial_binning.binIndexMin(),
|
||||
azimuth_binning.binIndexMin(),
|
||||
longitudinal_binning.binIndexMin());
|
||||
}
|
||||
blitz::TinyVector<int, 3> CylinderBinning::binIndexMax() const {
|
||||
return blitz::TinyVector<int, 3>(radial_binning.binIndexMax(),
|
||||
azimuth_binning.binIndexMax(),
|
||||
longitudinal_binning.binIndexMax());
|
||||
}
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
#include "simulation/random_numbers.h"
|
||||
|
||||
RandomNumbers::RandomNumbers(int seed)
|
||||
: engine_(std::make_shared<std::mt19937>(seed)),
|
||||
real_distribution_(0.0, 1.0) {}
|
||||
|
||||
int RandomNumbers::rollUniform(int min, int max) {
|
||||
return min + std::floor((max - min) * real_distribution_(*engine_));
|
||||
}
|
||||
|
||||
double RandomNumbers::rollUniform(double min, double max) {
|
||||
return min + real_distribution_(*engine_) * (max - min);
|
||||
}
|
||||
|
||||
std::mt19937* RandomNumbers::engine() {
|
||||
return engine_.get();
|
||||
}
|
||||
Executable
+28
@@ -0,0 +1,28 @@
|
||||
#include "simulation/reflector.h"
|
||||
|
||||
ReflectorDPS::ReflectorDPS(double z_position_dps)
|
||||
: z_position_dps_(z_position_dps) {}
|
||||
|
||||
bool ReflectorDPS::needsReflectionBackToDomain(
|
||||
const particle::Particle* particle) const {
|
||||
return particle->position().cylinderEntries()(2) > z_position_dps_;
|
||||
}
|
||||
|
||||
void ReflectorDPS::reflectBackToDomain(particle::Particle* particle) const {
|
||||
blitz::TinyVector<double, 3> old_position =
|
||||
particle->position().cylinderEntries();
|
||||
old_position(2) = 2 * z_position_dps_ - old_position(2);
|
||||
particle->position(CylinderCoordinate(old_position));
|
||||
blitz::TinyVector<double, 3> old_velocity =
|
||||
particle->velocity().cartesianEntries();
|
||||
old_velocity(2) *= -1;
|
||||
particle->velocity(CartesianVector(old_velocity));
|
||||
}
|
||||
|
||||
bool NoReflector::needsReflectionBackToDomain(
|
||||
const particle::Particle* particle) const {
|
||||
return false;
|
||||
}
|
||||
void NoReflector::reflectBackToDomain(particle::Particle* particle) const {
|
||||
// Do nothing
|
||||
}
|
||||
Executable
+12
@@ -0,0 +1,12 @@
|
||||
#include "simulation/scalar_field.h"
|
||||
|
||||
ScalarFieldFunctionInZ::ScalarFieldFunctionInZ(
|
||||
std::function<double(double, double, double)> F_z)
|
||||
: F_z_(F_z) {}
|
||||
|
||||
double ScalarFieldFunctionInZ::value(const Coordinate& coordinate) const {
|
||||
const blitz::TinyVector<double, 3> cylinder_coordinate =
|
||||
coordinate.cylinderEntries();
|
||||
return F_z_(cylinder_coordinate(0), cylinder_coordinate(1),
|
||||
cylinder_coordinate(2));
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
#include "simulation/scattering_angle_distribution.h"
|
||||
#include "simulation/homebase.h"
|
||||
|
||||
#include "IO/hdf5_reader.h"
|
||||
|
||||
IsotropicAngleDistribution::IsotropicAngleDistribution()
|
||||
: distribution_(std::uniform_real_distribution<double>(0, M_PI)) {}
|
||||
double IsotropicAngleDistribution::roll(std::mt19937* engine,
|
||||
double energy_eV) {
|
||||
return distribution_(*engine);
|
||||
}
|
||||
|
||||
std::vector<double> energyBinBorders(const blitz::Array<double, 1>& energies) {
|
||||
std::vector<double> energy_bin_borders;
|
||||
|
||||
energy_bin_borders.push_back(energies(0) - (energies(1) - energies(0)) / 2);
|
||||
for (int i = 0; i < energies.size() - 1; ++i) {
|
||||
energy_bin_borders.push_back(energies(i) +
|
||||
(energies(i + 1) - energies(i)) / 2);
|
||||
}
|
||||
energy_bin_borders.push_back(
|
||||
energies(energies.size() - 1) +
|
||||
(energies(energies.size() - 1) - energies(energies.size() - 2)) / 2);
|
||||
return energy_bin_borders;
|
||||
}
|
||||
|
||||
AngleDistributionFromFile::AngleDistributionFromFile(
|
||||
std::string absolute_filepath)
|
||||
: energy_binning_(nullptr) {
|
||||
Hdf5Reader reader(absolute_filepath);
|
||||
blitz::Array<double, 1> energies = reader.readD1Double("/energies(eV)");
|
||||
blitz::Array<double, 1> angles = reader.readD1Double("/angles(rad)");
|
||||
blitz::Array<double, 2> cross_sections =
|
||||
reader.readD2Double("/cross_sections(m-2)");
|
||||
|
||||
energy_binning_ = std::make_shared<EnergyBinning>(energyBinBorders(energies));
|
||||
|
||||
for (int i = 0; i < energies.size(); ++i) {
|
||||
blitz::Array<double, 1> cross_section_at_energy =
|
||||
cross_sections(i, blitz::Range::all());
|
||||
angle_distributions_.push_back(std::piecewise_linear_distribution<double>(
|
||||
angles.begin(), angles.end(), cross_section_at_energy.begin()));
|
||||
}
|
||||
}
|
||||
|
||||
double AngleDistributionFromFile::roll(std::mt19937* engine, double energy_eV) {
|
||||
const int energy_index = energy_binning_->bin(energy_eV);
|
||||
return angle_distributions_[energy_index](*engine);
|
||||
}
|
||||
|
||||
CompositeAngleDistribution::CompositeAngleDistribution(
|
||||
std::shared_ptr<ScatteringAngleDistribution> lower_distribution,
|
||||
std::shared_ptr<ScatteringAngleDistribution> higher_distribution,
|
||||
double decider_energy)
|
||||
: lower_distribution_(lower_distribution),
|
||||
higher_distribution_(higher_distribution),
|
||||
decider_energy_(decider_energy){};
|
||||
|
||||
double CompositeAngleDistribution::roll(std::mt19937* engine,
|
||||
double energy_eV) {
|
||||
if (energy_eV <= decider_energy_) {
|
||||
return lower_distribution_->roll(engine, energy_eV);
|
||||
} else {
|
||||
return higher_distribution_->roll(engine, energy_eV);
|
||||
}
|
||||
}
|
||||
Executable
+302
@@ -0,0 +1,302 @@
|
||||
#include "simulation/simulation.h"
|
||||
#include <numeric>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
#include "constants/sim_constants.h"
|
||||
#include "simulation/cpu_synchronizer.h"
|
||||
#include "simulation/current_collector.h"
|
||||
#include "simulation/density_field.h"
|
||||
#include "simulation/electromagnetic_field.h"
|
||||
#include "simulation/errors.h"
|
||||
#include "simulation/injector.h"
|
||||
#include "simulation/interaction.h"
|
||||
#include "simulation/logger.h"
|
||||
#include "simulation/mover.h"
|
||||
#include "simulation/mpi_communication.h"
|
||||
#include "simulation/particle.h"
|
||||
#include "simulation/random_numbers.h"
|
||||
#include "simulation/reflector.h"
|
||||
#include "simulation/terminator.h"
|
||||
|
||||
#include "IO/simulation_output.h"
|
||||
|
||||
#include "generators/electric_field_generator.h"
|
||||
#include "generators/injector_generator.h"
|
||||
#include "generators/magnetic_field_generator.h"
|
||||
#include "generators/random_numbers_generator.h"
|
||||
#include "generators/simulation_output_generator.h"
|
||||
#include "generators/species_generator.h"
|
||||
|
||||
#include "tools/simulation_time.h"
|
||||
|
||||
Simulation::Simulation(std::string parameter_filename)
|
||||
: parameter_(parameter_filename) {
|
||||
init(parameter_);
|
||||
}
|
||||
|
||||
void Simulation::init(Parameter& parameter) {
|
||||
magnetic_field_ = generator::magneticField(parameter);
|
||||
electric_field_ = generator::electricField(parameter);
|
||||
|
||||
DEBUG_LEVEL_0("Creating species.");
|
||||
generator::SpeciesBuilder builder(parameter);
|
||||
tracked_species_ = builder.trackedSpecies();
|
||||
background_species_ = builder.backgroundSpecies();
|
||||
timer_ = builder.timer();
|
||||
|
||||
DEBUG_LEVEL_0("Creating injector.");
|
||||
injector_ = generator::buildInjector(tracked_species_, background_species_,
|
||||
parameter);
|
||||
|
||||
DEBUG_LEVEL_0("Init random numbers.");
|
||||
random_ = generator::buildRandomNumbers(parameter);
|
||||
|
||||
DEBUG_LEVEL_0("Init output.");
|
||||
output_ = generator::buildSimulationOutput(
|
||||
parameter.jsonObjectFromBase({"Output"}), tracked_species_);
|
||||
|
||||
DEBUG_LEVEL_0("Output meta data.");
|
||||
output_->writeMetaData(tracked_species_);
|
||||
|
||||
DEBUG_LEVEL_0("Init cpu synchronizer.");
|
||||
cpu_synchronizer_ = std::make_shared<CpuSynchronizer>();
|
||||
}
|
||||
|
||||
int eventsThisCpu(int events_all_cpus) {
|
||||
int events = events_all_cpus / MpiCommunication::mpi_size;
|
||||
int overflow = events_all_cpus % MpiCommunication::mpi_size;
|
||||
if (MpiCommunication::mpi_rank < overflow) {
|
||||
events += 1;
|
||||
}
|
||||
return events;
|
||||
}
|
||||
|
||||
int eventsOfCycle(int stride, int leftover_events) {
|
||||
if ((leftover_events - stride) >= 0) {
|
||||
return stride;
|
||||
} else {
|
||||
return leftover_events;
|
||||
}
|
||||
}
|
||||
|
||||
void Simulation::finalize() {
|
||||
SIM_INFO("Simulation ended.");
|
||||
}
|
||||
|
||||
void Simulation::start() {
|
||||
SIM_INFO("Starting Simulation.");
|
||||
|
||||
const int event_stride =
|
||||
parameter_.intValue(json_path({"Simulation", "EventStride"}), 1);
|
||||
const int total_simulated_events =
|
||||
parameter_.intValue(json_path({"Simulation", "SimulatedEvents"}), 1);
|
||||
SIM_INFO("Total simulated events: " + std::to_string(total_simulated_events));
|
||||
SIM_INFO("Simulated events before cpu sync: " + std::to_string(event_stride));
|
||||
const int events_this_cpu = eventsThisCpu(total_simulated_events);
|
||||
SIM_INFO("Events per cpu (approx.): " + std::to_string(events_this_cpu));
|
||||
int leftover_events = events_this_cpu;
|
||||
|
||||
int num_cpu_syncs = 0;
|
||||
while (leftover_events > 0) {
|
||||
int events_of_cylcle = eventsOfCycle(event_stride, leftover_events);
|
||||
SIM_INFO("Leftover events on first cpu: " +
|
||||
std::to_string(leftover_events));
|
||||
processEvents(events_of_cylcle);
|
||||
|
||||
DEBUG_LEVEL_1("Leftover events: " + std::to_string(leftover_events));
|
||||
leftover_events -= events_of_cylcle;
|
||||
|
||||
SIM_INFO("Cpu sync.");
|
||||
SimulationTimer::eventStartPoint(std::string("CpuSync"));
|
||||
cpu_synchronizer_->synchronize(tracked_species_);
|
||||
num_cpu_syncs += 1;
|
||||
SimulationTimer::eventStopPoint(std::string("CpuSync"));
|
||||
|
||||
SIM_INFO("Poisson step.");
|
||||
SimulationTimer::eventStartPoint(std::string("PoissonStep"));
|
||||
SimulationTimer::eventStopPoint(std::string("PoissonStep"));
|
||||
|
||||
SIM_INFO("Perform output.");
|
||||
SimulationTimer::eventStartPoint(std::string("Output"));
|
||||
output_->writeSimulationData(num_cpu_syncs, tracked_species_);
|
||||
SimulationTimer::eventStopPoint(std::string("Output"));
|
||||
}
|
||||
}
|
||||
|
||||
int particleNumberFromQueue(int queue_length, RandomNumbers* random) {
|
||||
return random->rollUniform(0, queue_length);
|
||||
}
|
||||
|
||||
void Simulation::processEvents(int events) {
|
||||
DEBUG_LEVEL_0("Processing events.");
|
||||
std::vector<particle::Particle> particle_queue;
|
||||
particle_queue.reserve(100);
|
||||
int current_event = 0;
|
||||
while ((current_event < events) || (!particle_queue.empty())) {
|
||||
DEBUG_LEVEL_2("Current event: " + std::to_string(current_event));
|
||||
|
||||
if (particle_queue.empty()) {
|
||||
current_event += 1;
|
||||
DEBUG_LEVEL_2("Inject particles to queue.");
|
||||
SimulationTimer::eventStartPoint(std::string("ParticleInjection"));
|
||||
std::vector<particle::Particle> new_particles =
|
||||
determineInjectedParticles(random_.get());
|
||||
SimulationTimer::eventStopPoint(std::string("ParticleInjection"));
|
||||
DEBUG_LEVEL_2("Injected particles to queue.");
|
||||
|
||||
particle_queue.insert(particle_queue.end(), new_particles.begin(),
|
||||
new_particles.end());
|
||||
}
|
||||
DEBUG_LEVEL_2("Particles in queue: " + particle_queue.size());
|
||||
const int particle_number =
|
||||
particleNumberFromQueue(particle_queue.size(), random_.get());
|
||||
|
||||
// extract particle here from queue, so that an extension of the queue does
|
||||
// not mess up the reference
|
||||
particle::Particle particle = particle_queue.at(particle_number);
|
||||
particle_queue.erase(particle_queue.begin() + particle_number);
|
||||
|
||||
processUntilTermination(particle, particle_queue);
|
||||
}
|
||||
}
|
||||
|
||||
void Simulation::processUntilTermination(
|
||||
particle::Particle& particle,
|
||||
std::vector<particle::Particle>& particle_queue) {
|
||||
DEBUG_LEVEL_3("Process particle until termination");
|
||||
while (particle.specie()->terminator()->isAlive(&particle)) {
|
||||
auto old_position = particle.position();
|
||||
double old_energy = particle.kineticEnergy_eV();
|
||||
|
||||
SimulationTimer::eventStartPoint(std::string("MFP calculation"));
|
||||
const double mfp_m = meanFreePath(particle, random_.get());
|
||||
SimulationTimer::eventStopPoint(std::string("MFP calculation"));
|
||||
|
||||
SimulationTimer::eventStartPoint(std::string("Mover"));
|
||||
particle.specie()->mover()->move(&particle, mfp_m, electric_field_.get(),
|
||||
magnetic_field_.get());
|
||||
SimulationTimer::eventStopPoint(std::string("Mover"));
|
||||
|
||||
bool was_reflected = false;
|
||||
if (particle.specie()->reflector()->needsReflectionBackToDomain(
|
||||
&particle)) {
|
||||
DEBUG_LEVEL_3("Reflect particle.");
|
||||
old_position = particle.position();
|
||||
particle.specie()->reflector()->reflectBackToDomain(&particle);
|
||||
particle.specie()->currentCollector()->collect(&particle, old_position);
|
||||
was_reflected = true;
|
||||
}
|
||||
|
||||
if (particle.specie()->terminator()->reachedMaxInteractions(&particle)) {
|
||||
DEBUG_LEVEL_3("Terminate particle. Reached max interactions.");
|
||||
if (particle.specie()->deletionLogger()) {
|
||||
particle.specie()->deletionLogger()->logDeletion(
|
||||
particle.position(), DeletionType::max_interactions);
|
||||
}
|
||||
break;
|
||||
} else if (particle.specie()->terminator()->reachedMaxTimeAlive(
|
||||
&particle)) {
|
||||
DEBUG_LEVEL_3("Terminate particle. Reached max time alive.");
|
||||
if (particle.specie()->deletionLogger()) {
|
||||
particle.specie()->deletionLogger()->logDeletion(
|
||||
particle.position(), DeletionType::max_time_alive);
|
||||
}
|
||||
break;
|
||||
} else if (particle.specie()->terminator()->leftSimulationDomain(
|
||||
&particle)) {
|
||||
DEBUG_LEVEL_3("Terminate particle. Left simulation domain.");
|
||||
std::stringstream output_ss;
|
||||
output_ss << "Position (cyl): " << particle.position().cylinderEntries();
|
||||
output_ss << " (cart): " << particle.position().cartesianEntries();
|
||||
DEBUG_LEVEL_3(output_ss.str());
|
||||
if (particle.specie()->deletionLogger()) {
|
||||
particle.specie()->deletionLogger()->logDeletion(particle.position(),
|
||||
DeletionType::wall);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (!(particle.specie()->dynamicDensityField()->changedPositionBin(
|
||||
old_position, particle.position())) &&
|
||||
!(particle.specie()->dynamicDensityField()->changedEnergyBin(
|
||||
old_energy, particle.kineticEnergy_eV())) &&
|
||||
!was_reflected) {
|
||||
DEBUG_LEVEL_3("Perform interaction.");
|
||||
DEBUG_LEVEL_3("Number of Interactions: " +
|
||||
std::to_string(particle.interactionCounter()));
|
||||
DEBUG_LEVEL_3("Ekin before interact: " +
|
||||
std::to_string(particle.kineticEnergy_eV()));
|
||||
std::stringstream output_ss;
|
||||
output_ss << "Position (cyl): " << particle.position().cylinderEntries();
|
||||
// output_ss << "\n (cart): " << particle.position().cartesianEntries();
|
||||
DEBUG_LEVEL_3(output_ss.str());
|
||||
|
||||
SimulationTimer::eventStartPoint(std::string("Interaction"));
|
||||
interaction::InteractionOutput output = interact(particle, random_.get());
|
||||
SimulationTimer::eventStopPoint(std::string("Interaction"));
|
||||
|
||||
particle.increaseInteractionCounter();
|
||||
|
||||
for (auto& new_particle : output.newParticles()) {
|
||||
particle_queue.push_back(new_particle);
|
||||
}
|
||||
|
||||
if (output.deleteCurrentParticle()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
double Simulation::meanFreePath(particle::Particle& particle,
|
||||
RandomNumbers* random) {
|
||||
double total_macroscopic_cross_section_m = 0.;
|
||||
for (auto& interaction : particle.specie()->interactions()) {
|
||||
total_macroscopic_cross_section_m +=
|
||||
interaction->macroscopicCrossSection(particle);
|
||||
}
|
||||
|
||||
if (total_macroscopic_cross_section_m <= 0.) {
|
||||
return std::numeric_limits<double>::max();
|
||||
} else {
|
||||
const double rnd_number = random->rollUniform(0., 1.);
|
||||
if (rnd_number <= 0) {
|
||||
return std::numeric_limits<double>::max();
|
||||
} else {
|
||||
return 1. / total_macroscopic_cross_section_m * log(1. / rnd_number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interaction::InteractionOutput Simulation::interact(
|
||||
particle::Particle& particle,
|
||||
RandomNumbers* random) {
|
||||
std::vector<double> macroscopic_cross_sections_m;
|
||||
for (auto& interaction : particle.specie()->interactions()) {
|
||||
macroscopic_cross_sections_m.push_back(
|
||||
interaction->macroscopicCrossSection(particle));
|
||||
}
|
||||
const double total_macroscopic_cross_section_m =
|
||||
std::accumulate(macroscopic_cross_sections_m.begin(),
|
||||
macroscopic_cross_sections_m.end(), 0.0);
|
||||
|
||||
if (total_macroscopic_cross_section_m <= 0.) {
|
||||
// do nothing
|
||||
return interaction::InteractionOutput();
|
||||
} else {
|
||||
std::discrete_distribution<int> interaction_decider(
|
||||
macroscopic_cross_sections_m.begin(),
|
||||
macroscopic_cross_sections_m.end());
|
||||
const int interaction_index = interaction_decider(*(random->engine()));
|
||||
return particle.specie()
|
||||
->interactions()
|
||||
.at(interaction_index)
|
||||
->performInteraction(particle, random);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<particle::Particle> Simulation::determineInjectedParticles(
|
||||
RandomNumbers* random) {
|
||||
timer_->increaseTimestep(injector_->timePassed());
|
||||
return injector_->rollNewParticles(random->engine());
|
||||
}
|
||||
Executable
+131
@@ -0,0 +1,131 @@
|
||||
#include "simulation/species.h"
|
||||
|
||||
#include "simulation/current_collector.h"
|
||||
#include "simulation/density_field.h"
|
||||
#include "simulation/drift_velocity.h"
|
||||
#include "simulation/interaction.h"
|
||||
#include "simulation/logger.h"
|
||||
#include "simulation/mover.h"
|
||||
#include "simulation/parameter.h"
|
||||
#include "simulation/terminator.h"
|
||||
#include "simulation/velocity_distribution.h"
|
||||
|
||||
namespace species {
|
||||
|
||||
Specie::Specie(ImplementedSpecies type,
|
||||
std::string name,
|
||||
double mass_kg,
|
||||
double charge_C)
|
||||
: type_(type), name_(name), mass_kg_(mass_kg), charge_C_(charge_C) {}
|
||||
|
||||
double Specie::mass_kg() const {
|
||||
return mass_kg_;
|
||||
}
|
||||
double Specie::charge_C() const {
|
||||
return charge_C_;
|
||||
}
|
||||
ImplementedSpecies Specie::type() const {
|
||||
return type_;
|
||||
}
|
||||
std::string Specie::name() const {
|
||||
return name_;
|
||||
}
|
||||
|
||||
TrackedSpecie::TrackedSpecie(
|
||||
ImplementedSpecies type,
|
||||
std::string name,
|
||||
double mass_kg,
|
||||
double charge_C,
|
||||
std::shared_ptr<Mover> mover,
|
||||
std::shared_ptr<Terminator> terminator,
|
||||
std::shared_ptr<Reflector> reflector,
|
||||
std::shared_ptr<DynamicDensityField> density_field,
|
||||
std::shared_ptr<VelocityDistribution> velocity_distribution,
|
||||
std::shared_ptr<DriftVelocity> drift_velocity)
|
||||
: Specie(type, name, mass_kg, charge_C),
|
||||
mover_(mover),
|
||||
terminator_(terminator),
|
||||
reflector_(reflector),
|
||||
density_field_(density_field),
|
||||
deletion_logger_(std::shared_ptr<DeletionLogger>(nullptr)),
|
||||
current_collector_(std::shared_ptr<CurrentCollector>(nullptr)),
|
||||
velocity_distribution_(velocity_distribution),
|
||||
drift_velocity_(drift_velocity) {}
|
||||
|
||||
DensityField* TrackedSpecie::densityField() {
|
||||
return density_field_.get();
|
||||
}
|
||||
|
||||
VelocityDistribution* TrackedSpecie::velocityDistibution() {
|
||||
return velocity_distribution_.get();
|
||||
}
|
||||
DriftVelocity* TrackedSpecie::driftVelocity() {
|
||||
return drift_velocity_.get();
|
||||
}
|
||||
|
||||
DynamicDensityField* TrackedSpecie::dynamicDensityField() {
|
||||
return density_field_.get();
|
||||
}
|
||||
|
||||
Mover* TrackedSpecie::mover() {
|
||||
return mover_.get();
|
||||
}
|
||||
|
||||
Terminator* TrackedSpecie::terminator() {
|
||||
return terminator_.get();
|
||||
}
|
||||
|
||||
Reflector* TrackedSpecie::reflector() {
|
||||
return reflector_.get();
|
||||
}
|
||||
DeletionLogger* TrackedSpecie::deletionLogger() {
|
||||
return deletion_logger_.get();
|
||||
}
|
||||
|
||||
CurrentCollector* TrackedSpecie::currentCollector() {
|
||||
return current_collector_.get();
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<interaction::Interaction>>
|
||||
TrackedSpecie::interactions() {
|
||||
return interactions_;
|
||||
}
|
||||
|
||||
void TrackedSpecie ::addInteraction(
|
||||
std::shared_ptr<interaction::Interaction> interaction) {
|
||||
interactions_.push_back(interaction);
|
||||
}
|
||||
|
||||
void TrackedSpecie::deletionLogger(std::shared_ptr<DeletionLogger> new_logger) {
|
||||
deletion_logger_ = new_logger;
|
||||
}
|
||||
|
||||
void TrackedSpecie::currentCollector(
|
||||
std::shared_ptr<CurrentCollector> new_collector) {
|
||||
current_collector_ = new_collector;
|
||||
}
|
||||
|
||||
BackgroundSpecie::BackgroundSpecie(
|
||||
ImplementedSpecies type,
|
||||
std::string name,
|
||||
double mass_kg,
|
||||
double charge_C,
|
||||
std::shared_ptr<ConstantDensityField> density_field,
|
||||
std::shared_ptr<VelocityDistribution> velocity_distribution,
|
||||
std::shared_ptr<DriftVelocity> drift_velocity)
|
||||
: Specie(type, name, mass_kg, charge_C),
|
||||
density_field_(density_field),
|
||||
velocity_distribution_(velocity_distribution),
|
||||
drift_velocity_(drift_velocity) {}
|
||||
|
||||
DensityField* BackgroundSpecie::densityField() {
|
||||
return density_field_.get();
|
||||
}
|
||||
VelocityDistribution* BackgroundSpecie::velocityDistibution() {
|
||||
return velocity_distribution_.get();
|
||||
}
|
||||
DriftVelocity* BackgroundSpecie::driftVelocity() {
|
||||
return drift_velocity_.get();
|
||||
}
|
||||
|
||||
} // namespace species
|
||||
Executable
+40
@@ -0,0 +1,40 @@
|
||||
#include "simulation/terminator.h"
|
||||
|
||||
#include "simulation/errors.h"
|
||||
#include "simulation/geometry.h"
|
||||
|
||||
Terminator::Terminator(long int max_interactions,
|
||||
double max_time_alive,
|
||||
std::shared_ptr<Geometry> geometry)
|
||||
: max_interactions_(max_interactions),
|
||||
max_time_alive_(max_time_alive),
|
||||
geometry_(geometry) {
|
||||
if (!geometry_) {
|
||||
ERROR("No particle lives forever. Give a geometry to terminatior.\n");
|
||||
}
|
||||
ASSERT_POSITIVE(
|
||||
&max_interactions_,
|
||||
"No particle lives forever. Give a positive max_interactions to "
|
||||
"terminator.");
|
||||
ASSERT_POSITIVE(&max_time_alive_,
|
||||
"Times are positive. Give a positive "
|
||||
"max_time_alive to terminatior.");
|
||||
}
|
||||
|
||||
bool Terminator::isAlive(const particle::Particle* particle) const {
|
||||
return !reachedMaxInteractions(particle) && !reachedMaxTimeAlive(particle) &&
|
||||
!leftSimulationDomain(particle);
|
||||
}
|
||||
|
||||
bool Terminator::reachedMaxInteractions(
|
||||
const particle::Particle* particle) const {
|
||||
return particle->interactionCounter() > max_interactions_;
|
||||
}
|
||||
bool Terminator::reachedMaxTimeAlive(const particle::Particle* particle) const {
|
||||
return particle->timeAlive() > max_time_alive_;
|
||||
}
|
||||
|
||||
bool Terminator::leftSimulationDomain(
|
||||
const particle::Particle* particle) const {
|
||||
return !(geometry_->isInside(particle->position()));
|
||||
}
|
||||
Executable
+15
@@ -0,0 +1,15 @@
|
||||
#include "simulation/timer.h"
|
||||
|
||||
Timer::Timer() : current_time_s_(0.) {}
|
||||
|
||||
double Timer::time() const {
|
||||
return current_time_s_;
|
||||
}
|
||||
|
||||
void Timer::increaseTimestep(double time_s) {
|
||||
current_time_s_ += time_s;
|
||||
}
|
||||
|
||||
void Timer::setTime(double time_s) {
|
||||
current_time_s_ = time_s;
|
||||
}
|
||||
Executable
+32
@@ -0,0 +1,32 @@
|
||||
#include "simulation/tritium_spectrum.h"
|
||||
|
||||
#include "constants/T2_beta_decay.h"
|
||||
#include "constants/physical_constants.h"
|
||||
#include "simulation/errors.h"
|
||||
#include "simulation/kinematic.h"
|
||||
|
||||
std::function<double(double)> tritiumDecayEnergySpectrum(double prefactor) {
|
||||
if (prefactor <= 0.) {
|
||||
ERROR("Prefactor must be positive");
|
||||
}
|
||||
|
||||
return [=](double energy_eV) {
|
||||
if (energy_eV < 0.) {
|
||||
ERROR("Beta decay spectrum not defined for negative energies.");
|
||||
}
|
||||
|
||||
else if ((energy_eV == 0.) || (energy_eV > T2BetaDecay::E0_T)) {
|
||||
return 0.;
|
||||
}
|
||||
|
||||
const double Z = 2.0;
|
||||
const double beta = kinematic::relativisticBeta(energy_eV, HEPunits::me);
|
||||
const double p = kinematic::relativisticMomentum(energy_eV, HEPunits::me);
|
||||
const double eta = 2.0 * M_PI * Z / (137.0 * beta);
|
||||
const double fermi_function = eta / (1. - std::exp(-eta));
|
||||
|
||||
return T2BetaDecay::spectrum_prefactor * fermi_function *
|
||||
(energy_eV + HEPunits::me) * p *
|
||||
std::pow(T2BetaDecay::E0_T - energy_eV, 2) * prefactor;
|
||||
};
|
||||
}
|
||||
Executable
+48
@@ -0,0 +1,48 @@
|
||||
#include "simulation/unit_converter.h"
|
||||
#include "constants/physical_constants.h"
|
||||
#include "simulation/errors.h"
|
||||
|
||||
namespace unitConvertion {
|
||||
|
||||
double lengthPrefactor(const std::string& unit) {
|
||||
if (unit == "cm") {
|
||||
return 1E-2;
|
||||
} else if (unit == "m") {
|
||||
return 1;
|
||||
} else if (unit == "mm") {
|
||||
return 1E-3;
|
||||
} else {
|
||||
ERROR("I dont know this length unit.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
double length(double value,
|
||||
const std::string& unit_from,
|
||||
const std::string& unit_to) {
|
||||
double prefactor_from = lengthPrefactor(unit_from);
|
||||
double prefactor_to = lengthPrefactor(unit_to);
|
||||
|
||||
return value * prefactor_from / prefactor_to;
|
||||
}
|
||||
|
||||
double energyPrefactor(const std::string& unit) {
|
||||
if (unit == "eV") {
|
||||
return 1.;
|
||||
} else if (unit == "J") {
|
||||
return 1 / SI::e;
|
||||
} else {
|
||||
ERROR("I dont know this energy unit.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
double energy(double value,
|
||||
const std::string& unit_from,
|
||||
const std::string& unit_to) {
|
||||
double prefactor_from = energyPrefactor(unit_from);
|
||||
double prefactor_to = energyPrefactor(unit_to);
|
||||
return value * prefactor_from / prefactor_to;
|
||||
}
|
||||
|
||||
}; // namespace unitConvertion
|
||||
Executable
+62
@@ -0,0 +1,62 @@
|
||||
#include "simulation/vector_field.h"
|
||||
|
||||
#include "simulation/coordinates.h"
|
||||
|
||||
VectorFieldFromArray::VectorFieldFromArray(
|
||||
const blitz::Array<double, 3>& radial_component,
|
||||
const blitz::Array<double, 3>& azimuthal_component,
|
||||
const blitz::Array<double, 3>& longitudial_component,
|
||||
double radius,
|
||||
double length)
|
||||
: Fr_(radial_component.copy()),
|
||||
Fphi_(azimuthal_component.copy()),
|
||||
Fz_(longitudial_component.copy()),
|
||||
binning_(CylinderBinning(radial_component.extent(0),
|
||||
radius,
|
||||
radial_component.extent(1),
|
||||
radial_component.extent(2),
|
||||
length)) {}
|
||||
|
||||
CylinderVector VectorFieldFromArray::value(const Coordinate& coordinate) const {
|
||||
const blitz::TinyVector<int, 3> bin =
|
||||
binning_.bin(coordinate.cylinderEntries());
|
||||
return value(bin);
|
||||
}
|
||||
|
||||
CylinderVector VectorFieldFromArray::value(
|
||||
const blitz::TinyVector<int, 3>& bin) const {
|
||||
if (Fr_.isInRange(bin)) {
|
||||
return CylinderVector(
|
||||
blitz::TinyVector<double, 3>(Fr_(bin), Fphi_(bin), Fz_(bin)));
|
||||
} else {
|
||||
return CylinderVector(blitz::TinyVector<double, 3>(0., 0., 0.));
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
VectorFieldConstant::VectorFieldConstant(const CylinderVector& constant)
|
||||
: constant_(constant) {}
|
||||
|
||||
CylinderVector VectorFieldConstant::value(const Coordinate& coordinate) const {
|
||||
return constant_;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
VectorFieldFunctionInZ::VectorFieldFunctionInZ(
|
||||
std::function<double(double, double, double)> F_z)
|
||||
: F_z_(F_z) {}
|
||||
|
||||
CylinderVector VectorFieldFunctionInZ::value(
|
||||
const Coordinate& coordinate) const {
|
||||
const blitz::TinyVector<double, 3> cylinder_position =
|
||||
coordinate.cylinderEntries();
|
||||
return CylinderVector(blitz::TinyVector<double, 3>(
|
||||
0., 0.,
|
||||
F_z_(cylinder_position(0), cylinder_position(1), cylinder_position(2))));
|
||||
}
|
||||
Executable
+15
@@ -0,0 +1,15 @@
|
||||
#include "simulation/velocity_distribution.h"
|
||||
|
||||
#include "simulation/drift_velocity.h"
|
||||
#include "simulation/kinematic.h"
|
||||
#include "simulation/velocity_distribution_function.h"
|
||||
|
||||
VelocityDistributionFromFunction::VelocityDistributionFromFunction(
|
||||
std::shared_ptr<VelocityDistributionFunction> fv)
|
||||
: fv_(fv) {}
|
||||
|
||||
CartesianVector VelocityDistributionFromFunction::rollVelocity(
|
||||
const Coordinate& position,
|
||||
std::mt19937* engine) {
|
||||
return fv_->rollVelocity(engine);
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
#include "simulation/velocity_distribution_function.h"
|
||||
|
||||
#include "constants/physical_constants.h"
|
||||
#include "simulation/compare.h"
|
||||
#include "simulation/energy_distribution.h"
|
||||
#include "simulation/errors.h"
|
||||
#include "simulation/kinematic.h"
|
||||
|
||||
IsotropicDistribution::IsotropicDistribution(
|
||||
std::shared_ptr<EnergyDistributionFunction> f_E,
|
||||
double mass_kg)
|
||||
: f_E_(f_E), mass_kg_(mass_kg) {}
|
||||
|
||||
blitz::TinyVector<double, 3> rollUnitVector(std::mt19937* engine) {
|
||||
std::uniform_real_distribution<double> distribution(0.0, 1.0);
|
||||
const double theta = distribution(*engine) * 2 * M_PI;
|
||||
const double phi = distribution(*engine) * M_PI;
|
||||
return blitz::TinyVector<double, 3>(std::cos(theta) * std::sin(phi),
|
||||
std::sin(theta) * std::sin(phi),
|
||||
std::cos(phi));
|
||||
}
|
||||
|
||||
CartesianVector IsotropicDistribution::rollVelocity(std::mt19937* engine) {
|
||||
const double energy_eV = f_E_->rollEnergy(engine);
|
||||
const double absolute_velocity =
|
||||
std::sqrt(2 * energy_eV * UnitConversion::eVToJoule / mass_kg_);
|
||||
return CartesianVector(rollUnitVector(engine) * absolute_velocity);
|
||||
}
|
||||
|
||||
DirectedDistribution::DirectedDistribution(
|
||||
std::shared_ptr<EnergyDistributionFunction> f_E,
|
||||
double mass_kg,
|
||||
const CartesianVector& direction)
|
||||
: f_E_(f_E),
|
||||
mass_kg_(mass_kg),
|
||||
direction_normal_(direction.cartesianEntries() /
|
||||
kinematic::norm(direction.cartesianEntries())) {
|
||||
if (!isEqual(blitz::dot(direction_normal_, direction_normal_), 1)) {
|
||||
SIM_INFO("The direction normal seems to be not normalized.");
|
||||
}
|
||||
}
|
||||
|
||||
CartesianVector DirectedDistribution::rollVelocity(std::mt19937* engine) {
|
||||
const double absolute_velocity = std::sqrt(
|
||||
2 * f_E_->rollEnergy(engine) * UnitConversion::eVToJoule / mass_kg_);
|
||||
return CartesianVector(absolute_velocity * direction_normal_);
|
||||
}
|
||||
Reference in New Issue
Block a user