Added Terminator type, which detects the crossing across two surfaces (very rudamentary check)

This commit is contained in:
2025-11-11 09:19:34 +01:00
parent af2ea430f4
commit 5d72083a25
5 changed files with 159 additions and 23 deletions
+79 -11
View File
@@ -8,22 +8,90 @@ std::shared_ptr<Terminator> buildTerminator(
Parameter& parameter,
const json_path& path_to_terminator) {
#ifdef debug_level2
DEBUG_LEVEL_2("Creating terminator.");
DEBUG_LEVEL_2("Creating terminator.");
#endif
long int max_interactions = static_cast<long int>(parameter.doubleValue(
json_path_append(path_to_terminator, {"MaximumInteractions"})));
double max_time_alive = parameter.doubleValue(
json_path_append(path_to_terminator, {"MaxTimeAlive", "value"}));
if (parameter.jsonObjectFromBase(
json_path_append(path_to_terminator, {"Cylinder"}))) {
long int max_interactions = static_cast<long int>(parameter.doubleValue(
json_path_append(path_to_terminator, {"Cylinder", "MaximumInteractions"})));
double max_time_alive = parameter.doubleValue(
json_path_append(path_to_terminator, {"Cylinder", "MaxTimeAlive", "value"}));
#ifdef debug_level3
DEBUG_LEVEL_3("with max interactions: " + std::to_string(max_interactions));
DEBUG_LEVEL_3("with max time alive: " + std::to_string(max_time_alive));
DEBUG_LEVEL_3("with max interactions: " + std::to_string(max_interactions));
DEBUG_LEVEL_3("with max time alive: " + std::to_string(max_time_alive));
#endif
std::shared_ptr<Geometry> geometry = buildGeometryIncludingRedirection(
parameter, json_path_append(path_to_terminator, {"BoundaryGeometry"}));
std::shared_ptr<Geometry> geometry = buildGeometryIncludingRedirection(
parameter, json_path_append(path_to_terminator, {"Cylinder", "BoundaryGeometry"}));
return std::shared_ptr<Terminator>(
new Terminator(max_interactions, max_time_alive, geometry));
return std::shared_ptr<Terminator>(
new Terminator(max_interactions, max_time_alive, geometry));
} else if (parameter.jsonObjectFromBase(json_path_append(path_to_terminator,
{"CylinderWithPlates"}))) {
long int max_interactions = static_cast<long int>(parameter.doubleValue(
json_path_append(path_to_terminator, {"CylinderWithPlates", "MaximumInteractions"})));
double max_time_alive = parameter.doubleValue(
json_path_append(path_to_terminator, {"CylinderWithPlates", "MaxTimeAlive", "value"}));
#ifdef debug_level3
DEBUG_LEVEL_3("with max interactions: " + std::to_string(max_interactions));
DEBUG_LEVEL_3("with max time alive: " + std::to_string(max_time_alive));
#endif
std::shared_ptr<Geometry> geometry = buildGeometryIncludingRedirection(
parameter, json_path_append(path_to_terminator, {"CylinderWithPlates", "BoundaryGeometry"}));
double zpos1 = json_read_length(
parameter.jsonObjectFromBase(
json_path_append(path_to_terminator,
{"CylinderWithPlates", "Plate1", "zPosition"})));
double rInner1 = json_read_length(
parameter.jsonObjectFromBase(
json_path_append(path_to_terminator,
{"CylinderWithPlates", "Plate1", "innerRadius"})));
double rOuter1 = json_read_length(
parameter.jsonObjectFromBase(
json_path_append(path_to_terminator,
{"CylinderWithPlates", "Plate1", "outerRadius"})));
double zpos2 = json_read_length(
parameter.jsonObjectFromBase(
json_path_append(path_to_terminator,
{"CylinderWithPlates", "Plate2", "zPosition"})));
double rInner2 = json_read_length(
parameter.jsonObjectFromBase(
json_path_append(path_to_terminator,
{"CylinderWithPlates", "Plate2", "innerRadius"})));
double rOuter2 = json_read_length(
parameter.jsonObjectFromBase(
json_path_append(path_to_terminator,
{"CylinderWithPlates", "Plate2", "outerRadius"})));
if(MpiCommunication::mpi_rank == 0) {
std::stringstream out;
out << "Specifications of Plate 1:\n";
out << "\tz position: " << zpos1 << " m\n\t";
out << "inner radius: " << rInner1 << " m\n\t";
out << "outer radius: " << rOuter1 << " m\n";
out << "Specifications of Plate 2:\n";
out << "\tz position: " << zpos2 << " m\n\t";
out << "inner radius: " << rInner2 << " m\n\t";
out << "outer radius: " << rOuter2 << " m\n";
SIM_INFO(out.str());
}
return std::shared_ptr<TerminatorWithPlates>(
new TerminatorWithPlates(max_interactions, max_time_alive, geometry,
zpos1, zpos2, rInner1, rInner2, rOuter1, rOuter2));
} else {
ERROR("Unknown Terminator type");
return nullptr;
}
}
}; // namespace generator
+2 -2
View File
@@ -116,7 +116,7 @@ void BorisMover::move(particle::Particle* particle,
old_kinetic_energy, particle->kineticEnergy_eV());
left_simulation_domain =
particle->specie()->terminator()->leftSimulationDomain(particle);
particle->specie()->terminator()->leftSimulationDomain(particle, old_position_cyl);
died_in_bin =
particle->specie()->terminator()->reachedMaxTimeAlive(particle);
@@ -491,7 +491,7 @@ void DriftMover::move(particle::Particle* particle,
#endif
left_simulation_domain =
particle->specie()->terminator()->leftSimulationDomain(particle);
particle->specie()->terminator()->leftSimulationDomain(particle, old_position_cyl);
leftover_time -= timestep;
particle->reduceTimeUntilCollision(timestep);
+5 -3
View File
@@ -376,9 +376,11 @@ void Simulation::processUntilTermination(particle::Particle& particle) {
DEBUG_LEVEL_3("Process particle until termination");
DEBUG_LEVEL_3("Weight of the particle: " + std::to_string(particle.weight()));
#endif
while (particle.specie()->terminator()->isAlive(&particle)) {
auto old_position = particle.cylinderPosition();
double old_energy = particle.kineticEnergy_eV();
while (particle.specie()->terminator()->isAlive(&particle, old_position)) {
old_position = particle.cylinderPosition();
old_energy = particle.kineticEnergy_eV();
if(!particle.isNextInteractionValid()) {
// DEBUG_LEVEL_1("Calculate time until collision");
@@ -416,7 +418,7 @@ void Simulation::processUntilTermination(particle::Particle& particle) {
if (particle.specie()->terminator()->reachedMaxTimeAlive(
&particle) &&
!particle.specie()->terminator()->leftSimulationDomain(
&particle)) {
&particle, old_position)) {
// particle.invalidateNextInteraction();
#ifdef debug_level3
DEBUG_LEVEL_3("Terminate particle " + particle.specie()->name() +
@@ -443,7 +445,7 @@ void Simulation::processUntilTermination(particle::Particle& particle) {
}
break;
} else if (particle.specie()->terminator()->leftSimulationDomain(
&particle)) {
&particle, old_position)) {
#if defined(debug)
DEBUG("Terminate particle " + particle.specie()->name()
+ ". Left simulation domain.");
+44 -1
View File
@@ -11,7 +11,7 @@ Terminator::Terminator(long int max_interactions,
max_time_alive_(max_time_alive),
geometry_(geometry) {
if (!geometry_) {
ERROR("No particle lives forever. Give a geometry to terminatior.\n");
ERROR("No particle lives forever. Give a geometry to terminator.\n");
}
ASSERT_POSITIVE(
&max_interactions_,
@@ -27,6 +27,12 @@ bool Terminator::isAlive(const particle::Particle* particle) const {
!leftSimulationDomain(particle);
}
bool Terminator::isAlive(const particle::Particle* particle,
const Coordinate& old_position) const {
return !reachedMaxInteractions(particle) && !reachedMaxTimeAlive(particle) &&
!leftSimulationDomain(particle, old_position);
}
bool Terminator::reachedMaxInteractions(
const particle::Particle* particle) const {
return particle->interactionCounter() > max_interactions_;
@@ -41,6 +47,43 @@ bool Terminator::leftSimulationDomain(
return !(geometry_->isInside(particle->cylinderPosition()));
}
bool Terminator::leftSimulationDomain(
const particle::Particle* particle, const Coordinate& old_position) const {
return !(geometry_->isInside(particle->cylinderPosition()));
}
double Terminator::maxTimeAlive() const {
return max_time_alive_;
}
TerminatorWithPlates::TerminatorWithPlates(long int max_interactions, double max_time_alive,
std::shared_ptr<Geometry> geometry,
double zpos1, double zpos2, double innerRadius1,
double innerRadius2, double outerRadius1,
double outerRadius2) :
zpos1_(zpos1), zpos2_(zpos2),
innerRadius1_(innerRadius1), innerRadius2_(innerRadius2),
outerRadius1_(outerRadius1), outerRadius2_(outerRadius2),
Terminator(max_interactions, max_time_alive, geometry) {}
bool TerminatorWithPlates::leftSimulationDomain(const particle::Particle* particle,
const Coordinate& old_position) const {
blitz::TinyVector<double, 3> cyl_pos_new = particle->cylinderPosition().cylinderEntries();
blitz::TinyVector<double, 3> cyl_pos_old = old_position.cylinderEntries();
bool outerbounds = Terminator::leftSimulationDomain(particle);
// crossing of zpos1_ plane
bool firstPlate = ((cyl_pos_new(2)-zpos1_)*(cyl_pos_old(2)-zpos1_) < 0) &&
(cyl_pos_new(0) > innerRadius1_) &&
(cyl_pos_new(0) < outerRadius1_);
bool secondPlate = ((cyl_pos_new(2)-zpos2_)*(cyl_pos_old(2)-zpos2_) < 0) &&
(cyl_pos_new(0) > innerRadius2_) &&
(cyl_pos_new(0) < outerRadius2_);
return outerbounds || firstPlate || secondPlate;
}
bool TerminatorWithPlates::leftSimulationDomain(const particle::Particle* particle) const {
ERROR("TerminatorWithPlates: \"leftSimulationDomain\" cannot be called with only a single set of coordinates!");
return true;
}