reduction of calls to atan2, sin and cos

This commit is contained in:
2025-06-04 10:43:39 +02:00
parent 2439f4fca9
commit 532bdf7979
17 changed files with 94 additions and 33 deletions
+13
View File
@@ -41,6 +41,13 @@ class Coordinate {
// return the value of the stored coordinate transformed to Cylinder space
virtual blitz::TinyVector<double, 3> cylinderEntries() const = 0;
// return cos(phi) with phi the second variable of the coordinate in cylinder
// coordinates
virtual double cosAzimuth() const = 0;
// return cos(phi) with phi the second variable of the coordinate in cylinder
// coordinates
virtual double sinAzimuth() const = 0;
// add the values of the parameter coordinate to the value stored in this variable
virtual void add(const Coordinate& coordinate) = 0;
};
@@ -58,6 +65,9 @@ class CartesianCoordinate : public Coordinate {
// return the value of the stored coordinate transformed to Cylider space
blitz::TinyVector<double, 3> cylinderEntries() const;
double cosAzimuth() const;
double sinAzimuth() const;
// add the values of the parameter coordinate to the value stored in this variable
void add(const Coordinate& coordinate);
@@ -77,6 +87,9 @@ class CylinderCoordinate : public Coordinate {
// return the value of the stored coordinate transformed to Cylinder space
blitz::TinyVector<double, 3> cylinderEntries() const;
double cosAzimuth() const;
double sinAzimuth() const;
// add the values of the parameter coordinate to the value stored in this variable
void add(const Coordinate& coordinate);
+3
View File
@@ -14,6 +14,9 @@ class DriftVelocity {
// returns the drift velocity value at the Coordinate position as a CartesianVector
CartesianVector driftVelocity(const Coordinate& position);
// overload using both the cartesian and cylinder coordinate version of the position
CartesianVector driftVelocity(const CartesianCoordinate& cartpos,
const CylinderCoordinate& cylpos);
private:
std::shared_ptr<VectorField> drift_velocity_;
+4 -1
View File
@@ -19,8 +19,10 @@ class Particle {
// set the position of the particle to the provided Coordinate new_position
void position(const Coordinate& new_position);
// return the position of the particle as a CartesianVector
// return the position of the particle as a CartesianCoordinate
CartesianCoordinate position() const;
// return the cylinder position of the particle
CylinderCoordinate cylinderPosition() const;
// set the velocity of the particle to the provided CartesianVector new_velocity
void velocity(const CartesianVector& new_velocity);
@@ -77,6 +79,7 @@ class Particle {
private:
CartesianVector velocity_;
CartesianCoordinate position_;
CylinderCoordinate position_cyl_;
double time_alive_s_;
double time_in_bin_s_;
+3 -1
View File
@@ -45,8 +45,10 @@ LIBS += $(HDF5P_LIBRARIES)
CXXFLAGS += $(JANSSON_INCLUDES)
LIBS += $(JANSSON_LIBRARIES)
CXXFLAGS += -g
ifeq ($(DO_DEBUGGING),true)
CXXFLAGS += -ftemplate-depth-30 -g -DBZ_DEBUG
CXXFLAGS += -ftemplate-depth-30 #-DBZ_DEBUG
CXXFLAGS += -Ddebug
CXXFLAGS += -Ddebug_level$(DEBUG_LEVEL)
endif
+2 -2
View File
@@ -20,8 +20,8 @@ InteractionOutput ChargeExchange::performInteraction(
const auto x_impact = particle.position();
// draw velocity of the target specie
const blitz::TinyVector<double, 3> v_target =
target_->driftVelocity()->driftVelocity(x_impact).cartesianEntries() +
target_->velocityDistibution()
target_->driftVelocity()->driftVelocity(x_impact, particle.cylinderPosition())
.cartesianEntries() + target_->velocityDistibution()
->rollVelocity(x_impact, generator->engine())
.cartesianEntries();
+2 -2
View File
@@ -23,8 +23,8 @@ InteractionOutput ClusterFormation::performInteraction(
#endif
const auto x_impact = particle.position();
const blitz::TinyVector<double, 3> v_target =
target_->driftVelocity()->driftVelocity(x_impact).cartesianEntries() +
target_->velocityDistibution()
target_->driftVelocity()->driftVelocity(x_impact, particle.cylinderPosition())
.cartesianEntries() + target_->velocityDistibution()
->rollVelocity(x_impact, generator->engine())
.cartesianEntries();
// kinetic energy of the target particle
+2 -2
View File
@@ -27,8 +27,8 @@ InteractionOutput ElasticScattering::performInteraction(
const auto x_impact = particle.position();
// draw the velocity of the target particle
const blitz::TinyVector<double, 3> v_target =
target_->driftVelocity()->driftVelocity(x_impact).cartesianEntries() +
target_->velocityDistibution()
target_->driftVelocity()->driftVelocity(x_impact, particle.cylinderPosition())
.cartesianEntries() + target_->velocityDistibution()
->rollVelocity(x_impact, generator->engine())
.cartesianEntries();
+2 -2
View File
@@ -47,8 +47,8 @@ InteractionOutput ElectronIonization::performInteraction(
double E_impact;
do {
v_target =
target_->driftVelocity()->driftVelocity(x_impact).cartesianEntries() +
target_->velocityDistibution()
target_->driftVelocity()->driftVelocity(x_impact, particle.cylinderPosition())
.cartesianEntries() + target_->velocityDistibution()
->rollVelocity(x_impact, generator->engine())
.cartesianEntries();
+6 -5
View File
@@ -30,7 +30,7 @@ InteractionOutput Recombination::performInteraction(
if(recombination_target_->type() == species::ImplementedSpecies::electron) {
for (int bin = 0; bin < number_energy_bins - 1; ++bin) {
macroscopic_bin_cross_sections_m.push_back(
macroscopicCrossSection(particle.position(), bin));
macroscopicCrossSection(particle.cylinderPosition(), bin));
}
} else {
const blitz::TinyVector<double, 3> v_particle =
@@ -45,7 +45,8 @@ InteractionOutput Recombination::performInteraction(
// drift velocity of the target particle
const blitz::TinyVector<double, 3> v_partner =
recombination_target_->driftVelocity()
->driftVelocity(particle.position()).cartesianEntries();
->driftVelocity(particle.position(),
particle.cylinderPosition()).cartesianEntries();
// velocity of incident particle in the drift frame of the target species
const blitz::TinyVector<double, 3> velocity_target_fixed =
@@ -63,9 +64,9 @@ InteractionOutput Recombination::performInteraction(
for (int bin = 0; bin < number_energy_bins - 1; ++bin) {
energy_eV_bin_center = (energyBinBorders[bin] + energyBinBorders[bin+1])/2.;
macroscopic_bin_cross_sections_m.push_back(
microscopicCrossSection(kinetic_energy_eV, particle.position()) *
microscopicCrossSection(kinetic_energy_eV, particle.cylinderPosition()) *
recombination_target_->dynamicDensityField()
->density(particle.position(), energy_eV_bin_center));
->density(particle.cylinderPosition(), energy_eV_bin_center));
}
}
@@ -110,7 +111,7 @@ double Recombination::macroscopicCrossSection(particle::Particle& particle) {
const int numBins = recombination_target_->dynamicDensityField()->
energyBinning()->binIndexMax();
double macroscopic_cross_section = 0.;
const auto position = particle.position();
const auto position = particle.cylinderPosition();
for (int bin = 0; bin < numBins - 1; ++bin) {
macroscopic_cross_section += macroscopicCrossSection(position, bin);
}
+26 -4
View File
@@ -56,6 +56,18 @@ blitz::TinyVector<double, 3> CartesianCoordinate::cartesianEntries() const {
return cartesian_position;
}
double CartesianCoordinate::cosAzimuth() const {
double rad = std::sqrt(cartesian_position(0)*cartesian_position(0) +
cartesian_position(1)*cartesian_position(1));
return cartesian_position(0)/rad;
}
double CartesianCoordinate::sinAzimuth() const {
double rad = std::sqrt(cartesian_position(0)*cartesian_position(0) +
cartesian_position(1)*cartesian_position(1));
return cartesian_position(1)/rad;
}
blitz::TinyVector<double, 3> CartesianCoordinate::cylinderEntries() const {
const double radius = std::sqrt(std::pow(cartesian_position(0), 2) +
std::pow(cartesian_position(1), 2));
@@ -104,6 +116,14 @@ void CylinderCoordinate::limitPhi() {
}
}
double CylinderCoordinate::cosAzimuth() const {
return std::cos(cylinder_position(1));
}
double CylinderCoordinate::sinAzimuth() const {
return std::sin(cylinder_position(1));
}
void CylinderCoordinate::add(const Coordinate& coordinate) {
const blitz::TinyVector<double, 3> new_cart_position =
cartesianEntries() + coordinate.cartesianEntries();
@@ -134,12 +154,14 @@ CylinderVector CylinderVector::operator+(const CylinderVector& vector) {
blitz::TinyVector<double, 3> CylinderVector::cartesianEntries(
const Coordinate& coordinate) const {
double sinPhi = coordinate.sinAzimuth();
double cosPhi = coordinate.cosAzimuth();
const double A_x =
std::cos(coordinate.cylinderEntries()(1)) * cylinder_vector(0) -
std::sin(coordinate.cylinderEntries()(1)) * cylinder_vector(1);
cosPhi * cylinder_vector(0) -
sinPhi * cylinder_vector(1);
const double A_y =
std::sin(coordinate.cylinderEntries()(1)) * cylinder_vector(0) +
std::cos(coordinate.cylinderEntries()(1)) * cylinder_vector(1);
sinPhi * cylinder_vector(0) +
cosPhi * cylinder_vector(1);
const double A_z = cylinder_vector(2);
return blitz::TinyVector<double, 3>(A_x, A_y, A_z);
}
+1 -1
View File
@@ -130,7 +130,7 @@ void CurrentCollector::collect(particle::Particle* particle,
blitz::TinyVector<int, 3> bin_old =
position_binning_.bin(old_position.cylinderEntries());
blitz::TinyVector<int, 3> bin_new =
position_binning_.bin(particle->position().cylinderEntries());
position_binning_.bin(particle->cylinderPosition().cylinderEntries());
collectRadialMovement(bin_old, bin_new);
collectLongitudinalMovement(bin_old, bin_new);
+5
View File
@@ -8,3 +8,8 @@ DriftVelocity::DriftVelocity(std::shared_ptr<VectorField> drift_velocity)
CartesianVector DriftVelocity::driftVelocity(const Coordinate& position) {
return CartesianVector(drift_velocity_->value(position), position);
}
CartesianVector DriftVelocity::driftVelocity(const CartesianCoordinate& cartpos,
const CylinderCoordinate& cylpos) {
return CartesianVector(drift_velocity_->value(cylpos), cartpos);
}
+3 -2
View File
@@ -32,7 +32,7 @@ Interaction::Interaction(std::function<double(double)> cross_section,
: cross_section_(cross_section), target_(target) {}
double Interaction::macroscopicCrossSection(particle::Particle& particle) {
const auto x_particle = particle.position();
const auto x_particle = particle.cylinderPosition();
const blitz::TinyVector<double, 3> v_particle =
particle.velocity().cartesianEntries();
if(!target_->driftVelocity()) {
@@ -40,7 +40,8 @@ double Interaction::macroscopicCrossSection(particle::Particle& particle) {
}
const blitz::TinyVector<double, 3> v_partner =
target_->driftVelocity()->driftVelocity(x_particle).cartesianEntries();
target_->driftVelocity()->driftVelocity(x_particle, particle.cylinderPosition())
.cartesianEntries();
const blitz::TinyVector<double, 3> velocity_target_fixed =
kinematic::velocityTargetFixed(v_particle, v_partner, particle.mass_kg(),
+11 -7
View File
@@ -165,7 +165,8 @@ void DriftMover::move(particle::Particle* particle,
MagneticField* magnetic_field) {
double leftover_distance = mfp_m;
auto old_position = particle->position();
CartesianCoordinate old_position = particle->position();
CylinderCoordinate old_position_cyl = particle->cylinderPosition();
auto old_velocity = particle->velocity();
double old_kinetic_energy = particle->kineticEnergy_eV();
@@ -181,15 +182,18 @@ void DriftMover::move(particle::Particle* particle,
!(changed_position_bin || changed_energy_bin || left_simulation_domain) &&
(leftover_distance > 0.) && !(died_in_bin)) {
old_position = particle->position();
old_position_cyl = particle->cylinderPosition();
old_velocity = particle->velocity();
old_kinetic_energy = particle->kineticEnergy_eV();
const blitz::TinyVector<double, 3> E =
electric_field->value(old_position).cartesianEntries(old_position);
electric_field->value(particle->cylinderPosition())
.cartesianEntries(old_position);
const blitz::TinyVector<double, 3> B =
magnetic_field->value(old_position).cartesianEntries(old_position);
magnetic_field->value(particle->cylinderPosition())
.cartesianEntries(old_position);
const double B_norm = kinematic::norm(B);
// calculate component of the velocity parallel to the magnetic field
@@ -266,14 +270,14 @@ void DriftMover::move(particle::Particle* particle,
particle->increaseTimeAlive(timestep);
particle->increaseTimeInBin(timestep);
const auto new_position = particle->position();
// const auto new_position = particle->position();
died_in_bin =
particle->specie()->terminator()->reachedMaxTimeAlive(particle);
changed_position_bin =
particle->specie()->dynamicDensityField()->changedPositionBin(
old_position, new_position);
old_position_cyl, particle->cylinderPosition());
changed_energy_bin =
particle->specie()->dynamicDensityField()->changedEnergyBin(
@@ -286,9 +290,9 @@ void DriftMover::move(particle::Particle* particle,
}
particle->specie()->dynamicDensityField()->logMovement(
old_position, old_kinetic_energy, particle->timeInBin());
old_position_cyl, old_kinetic_energy, particle->timeInBin());
particle->resetTimeInBin();
particle->specie()->currentCollector()->collect(particle, old_position);
particle->specie()->currentCollector()->collect(particle, old_position_cyl);
}
blitz::TinyVector<double, 3> DriftMover::driftVelocity(
+7
View File
@@ -9,6 +9,7 @@ Particle::Particle(std::shared_ptr<species::TrackedSpecie> specie,
const Vector& velocity)
: specie_(specie),
position_(position),
position_cyl_(position),
velocity_(velocity, position),
id_(this->last_id_),
time_alive_s_(0.),
@@ -54,8 +55,14 @@ CartesianCoordinate Particle::position() const {
return position_;
}
CylinderCoordinate Particle::cylinderPosition() const {
return position_cyl_;
}
void Particle::move(const Coordinate& distance) {
position_.add(distance);
// update the cylinder entries only after movement
position_cyl_ = CylinderCoordinate(position_);
}
double Particle::kineticEnergy_eV() const {
+3 -3
View File
@@ -215,7 +215,7 @@ void Simulation::processUntilTermination(
DEBUG_LEVEL_3("Process particle until termination");
#endif
while (particle.specie()->terminator()->isAlive(&particle)) {
auto old_position = particle.position();
CylinderCoordinate old_position = particle.cylinderPosition();
double old_energy = particle.kineticEnergy_eV();
std::vector<double> macroscopic_cross_sections_m;
@@ -236,7 +236,7 @@ void Simulation::processUntilTermination(
#ifdef debug_level3
DEBUG_LEVEL_3("Reflect particle.");
#endif
old_position = particle.position();
old_position = particle.cylinderPosition();
particle.specie()->reflector()->reflectBackToDomain(&particle);
particle.specie()->currentCollector()->collect(&particle, old_position);
was_reflected = true;
@@ -269,7 +269,7 @@ void Simulation::processUntilTermination(
}
if (!(particle.specie()->dynamicDensityField()->changedPositionBin(
old_position, particle.position())) &&
old_position, particle.cylinderPosition())) &&
!(particle.specie()->dynamicDensityField()->changedEnergyBin(
old_energy, particle.kineticEnergy_eV())) &&
!was_reflected) {
+1 -1
View File
@@ -36,7 +36,7 @@ bool Terminator::reachedMaxTimeAlive(const particle::Particle* particle) const {
bool Terminator::leftSimulationDomain(
const particle::Particle* particle) const {
return !(geometry_->isInside(particle->position()));
return !(geometry_->isInside(particle->cylinderPosition()));
}
double Terminator::maxTimeAlive() const {