Author SHA1 Message Date
csendlinger 2f68d8e3b1 Added more deletionLogger elements (for debugging) 2025-06-16 09:44:51 +02:00
csendlinger 9d170336b8 changes from double to float to check if
error is indeed cancellation error
2025-06-11 11:03:23 +02:00
20 changed files with 132 additions and 357 deletions
+1
View File
@@ -15,6 +15,7 @@ class Hdf5Writer {
void write(const blitz::Array<double, 3>& data, std::string path);
void write(const blitz::Array<double, 4>& data, std::string path);
void write(const blitz::Array<double, 5>& data, std::string path);
void write(const blitz::Array<float, 5>& data, std::string path);
void write(const blitz::Array<long int, 4>& data, std::string path);
void write(const blitz::Array<long int, 5>& data, std::string path);
void write(const blitz::TinyVector<double, 3>& data, std::string path);
+1 -2
View File
@@ -15,8 +15,7 @@ class ElasticScattering : public Interaction {
InteractionOutput performInteraction(particle::Particle& particle,
RandomNumbers* generator);
virtual double drawCollisionTime(particle::Particle& particle,
RandomNumbers* generator);
private:
std::shared_ptr<ScatteringAngleDistribution> theta_distrib_;
};
+2
View File
@@ -36,6 +36,8 @@ class CpuSynchronizer {
*/
void sumToAll(blitz::Array<double, 4>& array,
blitz::Array<double, 4>& last_sync_array);
void sumToAll(blitz::Array<float, 5>& array,
blitz::Array<float, 5>& last_sync_array);
void sumToAll(blitz::Array<double, 5>& array,
blitz::Array<double, 5>& last_sync_array);
void sumToAll(blitz::Array<double, 3>& array,
+4 -4
View File
@@ -57,7 +57,7 @@ class DynamicDensityField : public DensityField {
blitz::Array<double, 3> densityArray();
blitz::Array<double, 4> spectrumArray();
blitz::Array<double, 5> timeInBinArray() const;
blitz::Array<float, 5> timeInBinArray() const;
// blitz::Array<long int, 5> deletedParticlesArray() const;
blitz::Array<double, 3> binVolumeArray() const;
@@ -68,7 +68,7 @@ class DynamicDensityField : public DensityField {
// return an array of the time in bin at the Coordinate position and the energy
// energy_eV (only pitch angle direction left); over and underflow bin are removed
blitz::Array<double, 1> sliceTimeInBinArray(const Coordinate& position,
blitz::Array<float, 1> sliceTimeInBinArray(const Coordinate& position,
double energy_eV) const;
const EnergyBinning* energyBinning() const;
@@ -94,10 +94,10 @@ class DynamicDensityField : public DensityField {
// time in bin now contains the physical particle number per bin
// (not renamed, since that would require many, many changes due to
// renaming of the functions)
blitz::Array<double, 5> time_in_bin_s_;
blitz::Array<float, 5> time_in_bin_s_;
// blitz::Array<long int, 5> deleted_particles_;
blitz::Array<double, 5> sync_time_in_bin_s_;
blitz::Array<float, 5> sync_time_in_bin_s_;
// blitz::Array<long int, 5> sync_deleted_particles_;
blitz::Array<double, 4> density_per_m3_;
+8 -18
View File
@@ -3,34 +3,28 @@
#include <map>
#include <vector>
#include "simulation/particle.h"
#include "simulation/coordinates.h"
// to count numerical particle deletions
enum class DeletionType {
wall,
recombination,
max_interactions,
max_time_alive,
cluster_formation
cluster_formation,
creation_beta,
creation_density,
creation_interaction,
creation_above_2keV,
loss_above_2keV
};
// to count physical particle deletion
enum class PhysicalDeletionType {
wall
};
class Coordinate;
class DeletionLogger {
public:
DeletionLogger(std::vector<DeletionType> used_types,
std::vector<PhysicalDeletionType> used_phys_types);
DeletionLogger(std::vector<DeletionType> used_types);
// why a coordinate as a parameter -> not used in implementation
virtual void logDeletion(const Coordinate& coordinate, DeletionType type);
virtual void logDeletion(const particle::Particle& particle,
PhysicalDeletionType type);
std::map<DeletionType, long int> numDeletions() const;
std::map<PhysicalDeletionType, double> numPhysDeletions() const;
void numDeletions(long int new_value, DeletionType type);
long int numDeletions(DeletionType type) const;
@@ -41,10 +35,6 @@ class DeletionLogger {
private:
std::map<DeletionType, long int> num_deletions;
std::map<DeletionType, long int> sync_num_deletions;
std::map<PhysicalDeletionType, double> num_phys_deletions;
std::map<PhysicalDeletionType, double> sync_num_phys_deletions;
};
#endif /* LOGGER_H */
-5
View File
@@ -90,11 +90,6 @@ class Simulation {
std::shared_ptr<CpuSynchronizer> cpu_synchronizer_;
std::vector<particle::Particle> queue_;
#ifdef debug
double phyLossAtWallIon = 0;
double createdDensity = 0;
#endif
};
#endif /* SIMULATION_H */
@@ -67,8 +67,6 @@ class VelocityDistributionFromDensityField : public VelocityDistribution {
private:
// mass of the species of which the density field is a part of
const double mass_kg_;
double alpha_lower_limit;
double alpha_upper_limit;
std::shared_ptr<DynamicDensityField> density_field_;
+32
View File
@@ -197,6 +197,38 @@ void Hdf5Writer::write(const blitz::Array<double, 5>& data, std::string path) {
H5Fclose(file);
}
void Hdf5Writer::write(const blitz::Array<float, 5>& data, std::string path) {
hid_t file = H5Fopen(absolute_filename_.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
blitz::TinyVector<int, 5> size = data.extent();
blitz::TinyVector<int, 5> start(0, 0, 0, 0, 0);
hid_t filespace =
H5Screate_simple(5, blitz::TinyVector<hsize_t, 5>(size).data(), NULL);
hid_t lcpl = H5Pcreate(H5P_LINK_CREATE);
H5Pset_create_intermediate_group(lcpl, 1);
// turn of error report by hdf5 because of check H5Dopen2
H5Eset_auto(H5E_DEFAULT, NULL, NULL);
hid_t dataset;
dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT);
if (dataset < 0) {
dataset = H5Dcreate2(file, path.c_str(), H5T_NATIVE_FLOAT, filespace, lcpl,
H5P_DEFAULT, H5P_DEFAULT);
}
// turn error report back onby hdf5 because of check H5Dopen2
H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
H5Dwrite(dataset, H5T_NATIVE_FLOAT, H5S_ALL, filespace, H5P_DEFAULT,
data.data());
H5Dclose(dataset);
H5Pclose(lcpl);
H5Sclose(filespace);
H5Fclose(file);
}
void Hdf5Writer::write(const blitz::Array<long int, 4>& data,
std::string path) {
hid_t file = H5Fopen(absolute_filename_.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
+10 -7
View File
@@ -242,13 +242,16 @@ void SimulationOutput::writeNumDeletions(DeletionLogger* logger,
writer.write(pair.second, path_to_deletions + "/MaxTimeAlive");
} else if (pair.first == DeletionType::cluster_formation) {
writer.write(pair.second, path_to_deletions + "/ClusterFormation");
}
}
auto physDeletions = logger->numPhysDeletions();
for (auto& pair : physDeletions) {
if (pair.first == PhysicalDeletionType::wall) {
writer.write(pair.second, path_to_deletions + "/Wall(physical)");
} else if (pair.first == DeletionType::creation_beta) {
writer.write(pair.second, path_to_deletions + "/CreationBeta");
} else if (pair.first == DeletionType::creation_density) {
writer.write(pair.second, path_to_deletions + "/CreationDensity");
} else if (pair.first == DeletionType::creation_interaction) {
writer.write(pair.second, path_to_deletions + "/CreationInteraction");
} else if (pair.first == DeletionType::creation_above_2keV) {
writer.write(pair.second, path_to_deletions + "/CreationAbove2keV");
} else if (pair.first == DeletionType::loss_above_2keV) {
writer.write(pair.second, path_to_deletions + "/LossAbove2keV");
}
}
}
+9 -6
View File
@@ -7,7 +7,6 @@ namespace generator {
std::shared_ptr<DeletionLogger> buildDeletionLogger(json_t* object) {
std::vector<DeletionType> used_types;
std::vector<PhysicalDeletionType> used_phys_types;
json_t* type = json_object_by_path(object, {"Type"});
if (json_is_array(type)) {
@@ -25,9 +24,7 @@ std::shared_ptr<DeletionLogger> buildDeletionLogger(json_t* object) {
used_types.push_back(DeletionType::max_time_alive);
} else if (type_alias == "cluster_formation") {
used_types.push_back(DeletionType::cluster_formation);
} else if (type_alias == "wall_physical") {
used_phys_types.push_back(PhysicalDeletionType::wall);
}else {
} else {
ERROR("This deletion type " + type_alias + " is not known.");
}
}
@@ -38,11 +35,17 @@ std::shared_ptr<DeletionLogger> buildDeletionLogger(json_t* object) {
used_types.push_back(DeletionType::max_interactions);
used_types.push_back(DeletionType::max_time_alive);
used_types.push_back(DeletionType::cluster_formation);
used_phys_types.push_back(PhysicalDeletionType::wall);
// Debugging types, not yet able to be specified on their own, just
// in when "all" is used
used_types.push_back(DeletionType::creation_beta);
used_types.push_back(DeletionType::creation_density);
used_types.push_back(DeletionType::creation_interaction);
used_types.push_back(DeletionType::creation_above_2keV);
used_types.push_back(DeletionType::loss_above_2keV);
}
}
return std::make_shared<DeletionLogger>(used_types, used_phys_types);
return std::make_shared<DeletionLogger>(used_types);
}
std::shared_ptr<DeletionLogger> buildDeletionLogger(
Parameter& parameter,
+2
View File
@@ -46,6 +46,8 @@ InteractionOutput ClusterFormation::performInteraction(
x_impact, DeletionType::cluster_formation);
}
product_->deletionLogger()->logDeletion(x_impact, DeletionType::creation_interaction);
return output;
}
+2 -74
View File
@@ -6,8 +6,7 @@
#include "simulation/kinematic.h"
#include "simulation/scattering_angle_distribution.h"
#include "simulation/velocity_distribution.h"
#include "constants/sim_constants.h"
#include "simulation/density_field.h"
namespace interaction {
ElasticScattering::ElasticScattering(
@@ -49,80 +48,9 @@ InteractionOutput ElasticScattering::performInteraction(
target_->mass_kg(), theta, phi);
particle.velocity(CartesianVector(v_impact_new));
// no Gyrocentrum movement because boris push is used;
return InteractionOutput();
}
double ElasticScattering::drawCollisionTime(particle::Particle& particle,
RandomNumbers* generator) {
CartesianVector particle_velocity = particle.velocity();
target_velocity_ = target_->velocityDistribution()->
rollVelocity(particle.cylinderPosition(),
generator->engine());
// // if no drift velocity is present, then assume target at rest otherwise
// // use the drift velocity
if(target_->driftVelocity()) {
// auto tmp = target_->driftVelocity()->driftVelocity(particle.position());
target_velocity_.add(target_->driftVelocity()->driftVelocity(
particle.cartesianPosition(), particle.cylinderPosition()),
particle.cartesianPosition());
}
// masses are irrelevant for the transformation
const blitz::TinyVector<double, 3> velocity_target_fixed =
kinematic::velocityTargetFixed(particle_velocity.cartesianEntries(),
target_velocity_.cartesianEntries(), 0, 0);
const double relative_velocity = kinematic::norm(velocity_target_fixed);
// std::stringstream out;
// out << "particle "<< particle.specie()->name() <<" velocity: "
// << particle_velocity.cartesianEntries();
// out << "\ntarget "<< target_->name() <<" velocity: "
// << target_velocity_.cartesianEntries();
// out << "\nabsolute relative velocity: " << relative_velocity;
// check if the relative velocity is smaller than an epsilon compared to the larger
// of the two present velocities (to avoid division by 0 if either is 0) probably
// a better version possible
if(relative_velocity/std::max(
kinematic::norm(particle_velocity.cartesianEntries()),
kinematic::norm(target_velocity_.cartesianEntries())
) < SimConstants::compare_epsilon) {
return std::numeric_limits<double>::max();
}
const double energy_eV =
kinematic::kineticEnergy(velocity_target_fixed, particle.mass_kg())*
UnitConversion::JouleToeV;
// out << "energy collision time: " << energy_eV;
const double crosssection = microscopicCrossSection(energy_eV);
// out << "\ncross section: " << crosssection;
const double density = target_->densityField()->density(particle.cylinderPosition());
// out << "\ninverse mfp: " << crosssection*density << ", sigma: " << crosssection;
// out << ", density: " << density;
const double random_number = generator->rollUniform(0.,1.);
if (random_number == 0) {
return std::numeric_limits<double>::max();
}
const double distance_target_fixed =
- 1./(crosssection * density) * log(random_number);
// out << "\ndistance in target fixed frame: " << distance_target_fixed;
const double time = distance_target_fixed / relative_velocity;
// out << "\ntime before collision: " << time;
// if(density != 0)
// DEBUG_LEVEL_1(out.str());
return time;
}
}; // namespace interaction
+5 -3
View File
@@ -8,6 +8,7 @@
#include "simulation/kinematic.h"
#include "simulation/particle.h"
#include "simulation/velocity_distribution.h"
#include "simulation/logger.h"
namespace interaction {
@@ -45,9 +46,6 @@ InteractionOutput ElectronIonization::performInteraction(
->driftVelocity(x_impact, particle.cylinderPosition())
.cartesianEntries();
}
v_target *= std::sqrt(target_->mass_kg()/ion_->mass_kg());
blitz::TinyVector<double, 3> v_laboratory = kinematic::velocityTargetFixed(
v_impact, target_velocity_.cartesianEntries(), particle.mass_kg(), target_->mass_kg()
);
@@ -120,6 +118,10 @@ InteractionOutput ElectronIonization::performInteraction(
output.add(particle::Particle(ion_, x_impact, CartesianVector(v_target),
time_birth, weight_parent));
ion_->deletionLogger()->logDeletion(x_impact, DeletionType::creation_interaction);
electron_->deletionLogger()
->logDeletion(x_impact, DeletionType::creation_interaction);
return output;
}
+10 -9
View File
@@ -27,6 +27,16 @@ void CpuSynchronizer::sumToAll(blitz::Array<double, 5>& array,
last_sync_array += delta;
}
void CpuSynchronizer::sumToAll(blitz::Array<float, 5>& array,
blitz::Array<float, 5>& last_sync_array) {
int size = array.size();
blitz::Array<float, 5> delta(array.extent());
delta = array - last_sync_array;
MPI_Allreduce(MPI_IN_PLACE, delta.data(), size, MPI_FLOAT,
MPI_SUM, MPI_COMM_WORLD);
last_sync_array += delta;
}
void CpuSynchronizer::sumToAll(blitz::Array<long int, 4>& array,
blitz::Array<long int, 4>& last_sync_array) {
int size = array.size();
@@ -145,15 +155,6 @@ void CpuSynchronizer::synchronize(DeletionLogger* logger) {
pair.second = last_sync_num_deletion;
logger->sync_num_deletions[pair.first] = last_sync_num_deletion;
}
for (auto& pair : logger->num_phys_deletions) {
double num_deletion = pair.second;
double last_sync_num_deletion = logger->sync_num_phys_deletions[pair.first];
sumToAll(num_deletion, last_sync_num_deletion);
// last_sync_num_deletion now contains the total number of deletions
// across all cpus -> update the values in the objects
pair.second = last_sync_num_deletion;
logger->sync_num_phys_deletions[pair.first] = last_sync_num_deletion;
}
}
}
+5 -5
View File
@@ -61,7 +61,7 @@ DynamicDensityField::DynamicDensityField(
energy_binning_(energy_binning),
pitch_angle_binning_(pitch_angle_binning),
timer_(timer),
time_in_bin_s_(initiate5DArray<double>(
time_in_bin_s_(initiate5DArray<float>(
blitz::Range(position_binning.radial_binning.binIndexMin(),
position_binning.radial_binning.binIndexMax()),
blitz::Range(position_binning.azimuth_binning.binIndexMin(),
@@ -72,7 +72,7 @@ DynamicDensityField::DynamicDensityField(
pitch_angle_binning.binIndexMax()),
blitz::Range(energy_binning.binIndexMin(),
energy_binning.binIndexMax()))),
sync_time_in_bin_s_(initiate5DArray<double>(
sync_time_in_bin_s_(initiate5DArray<float>(
blitz::Range(position_binning.radial_binning.binIndexMin(),
position_binning.radial_binning.binIndexMax()),
blitz::Range(position_binning.azimuth_binning.binIndexMin(),
@@ -209,7 +209,7 @@ void DynamicDensityField::logMovement(const Coordinate& position,
const int pitch_angle_bin = pitch_angle_binning_.bin(pitch_angle);
time_in_bin_s_(position_bin(0), position_bin(1), position_bin(2),
pitch_angle_bin, energy_bin) += weight;
pitch_angle_bin, energy_bin) += static_cast<float>(weight);
}
// void DynamicDensityField::logDeletion(const Coordinate& position,
@@ -251,7 +251,7 @@ blitz::Array<double, 4> DynamicDensityField::spectrumArray() {
return density_per_m3_;
}
blitz::Array<double, 5> DynamicDensityField::timeInBinArray() const {
blitz::Array<float, 5> DynamicDensityField::timeInBinArray() const {
return time_in_bin_s_;
}
@@ -327,7 +327,7 @@ blitz::Array<double, 1> DynamicDensityField::
energy_binning_.binIndexMax()-1)).copy();
}
blitz::Array<double, 1> DynamicDensityField::
blitz::Array<float, 1> DynamicDensityField::
sliceTimeInBinArray(const Coordinate& position, double energy_eV) const {
blitz::TinyVector<int, 3> position_bin =
position_binning_.bin(position.cylinderEntries());
+13 -1
View File
@@ -10,6 +10,8 @@
#include "simulation/velocity_distribution.h"
#include "simulation/electromagnetic_field.h"
#include "simulation/random_numbers.h"
#include "simulation/logger.h"
#include "constants/physical_constants.h"
#include <sstream>
Injector::Injector(
@@ -48,6 +50,12 @@ particle::Particle Injector::rollNewParticle(
DEBUG_LEVEL_3(out.str());
#endif
created_specie->deletionLogger()->logDeletion(position, DeletionType::creation_beta);
if(kinematic::kineticEnergy(velocity, created_specie->mass_kg())
*UnitConversion::JouleToeV > 2000) {
created_specie->deletionLogger()->logDeletion(position, DeletionType::creation_above_2keV);
}
return particle::Particle(created_specie, position,
CartesianVector(velocity), time_birth, weight);
}
@@ -135,7 +143,11 @@ void Injector::drawNewParticleFromDensityField(std::mt19937* engine,
particle::Particle drawn_particle(specie, position,
velocity, injection_time, weight_factor*num_part_in_bin);
// debugging
specie->deletionLogger()->logDeletion(position, DeletionType::creation_density);
if(drawn_particle.kineticEnergy_eV() > 2000) {
specie->deletionLogger()->logDeletion(position, DeletionType::creation_above_2keV);
}
queue.push_back(drawn_particle);
}
+1 -24
View File
@@ -1,16 +1,10 @@
#include "simulation/logger.h"
#include "simulation/coordinates.h"
DeletionLogger::DeletionLogger(std::vector<DeletionType> used_types,
std::vector<PhysicalDeletionType> used_phys_types) {
DeletionLogger::DeletionLogger(std::vector<DeletionType> used_types) {
for (auto& type : used_types) {
num_deletions[type] = 0;
sync_num_deletions[type] = 0;
}
for (auto& type: used_phys_types) {
num_phys_deletions[type] = 0.;
sync_num_phys_deletions[type] = 0.;
}
}
void DeletionLogger::logDeletion(const Coordinate& coordinate,
@@ -20,21 +14,10 @@ void DeletionLogger::logDeletion(const Coordinate& coordinate,
}
}
void DeletionLogger::logDeletion(const particle::Particle& particle,
PhysicalDeletionType type) {
if (num_phys_deletions.count(type) > 0) {
num_phys_deletions.at(type) += particle.weight();
}
}
std::map<DeletionType, long int> DeletionLogger::numDeletions() const {
return num_deletions;
}
std::map<PhysicalDeletionType, double> DeletionLogger::numPhysDeletions() const {
return num_phys_deletions;
}
void DeletionLogger::numDeletions(long int new_value, DeletionType type) {
if (num_deletions.count(type)) {
num_deletions.at(type) = new_value;
@@ -56,10 +39,4 @@ void DeletionLogger::resetDeletionLogger() {
for (auto& pair : sync_num_deletions) {
pair.second = 0;
}
for(auto& pair : num_phys_deletions) {
pair.second = 0;
}
for (auto& pair : sync_num_phys_deletions) {
pair.second = 0;
}
}
+9 -26
View File
@@ -51,7 +51,6 @@ void BorisMover::move(particle::Particle* particle,
magnetic_field->value(old_position_cyl).cartesianEntries(old_position_cart);
double old_mu;
double new_mu;
// if magnetic field is 0 use e_z as a reference for the pitch angle
// calculation (don't know if best approach, probably not...)
if (kinematic::norm(B_old) == 0) {
@@ -62,7 +61,6 @@ void BorisMover::move(particle::Particle* particle,
old_mu = kinematic::cosPitchAngle(
B_old, particle->velocity().cartesianEntries(old_position_cart));
}
new_mu = old_mu;
double timestep = 0;
@@ -77,7 +75,7 @@ void BorisMover::move(particle::Particle* particle,
bool changed_energy_bin = false;
bool left_simulation_domain = false;
bool changed_mu_bin = false;
bool died_in_bin = particle->specie()->terminator()->reachedMaxTimeAlive(particle);
bool died_in_bin = false;
while (
!(changed_position_bin || changed_energy_bin || left_simulation_domain ||
@@ -112,6 +110,7 @@ void BorisMover::move(particle::Particle* particle,
magnetic_field->value(particle->cylinderPosition()).
cartesianEntries(new_position_cart);
double new_mu;
if (kinematic::norm(B_new) == 0) {
new_mu = kinematic::cosPitchAngle(
blitz::TinyVector<double, 3>(0,0,1), new_velocity.cartesianEntries());
@@ -131,19 +130,6 @@ void BorisMover::move(particle::Particle* particle,
changed_energy_bin =
particle->specie()->dynamicDensityField()->changedEnergyBin(
old_kinetic_energy, particle->kineticEnergy_eV());
// #ifdef debug
// if(changed_energy_bin &&
// particle->specie()->type() == species::ImplementedSpecies::Tplus) {
// std::stringstream out;
// out << "Changed energy bin from: " << particle->specie()->dynamicDensityField()
// ->energyBinning()->bin(old_kinetic_energy);
// out << " (" << old_kinetic_energy << ") to: ";
// out << particle->specie()->dynamicDensityField()->energyBinning()
// ->bin(particle->kineticEnergy_eV());
// out << " (" << particle->kineticEnergy_eV() << ")\n";
// DEBUG(out.str());
// }
// #endif
left_simulation_domain =
particle->specie()->terminator()->leftSimulationDomain(particle);
@@ -161,15 +147,12 @@ void BorisMover::move(particle::Particle* particle,
? delta_timeresolution*SimConstants::compare_epsilon
: (max_time_alive - timestep);
}
if(died_in_bin && !left_simulation_domain) {
// && (leftover_time > SimConstants::compare_epsilon ||
// changed_energy_bin || changed_mu_bin || changed_position_bin)) {
// use new values, since that is necessary if died_in_bin and a change in
// bin can occur at the same time
if(died_in_bin) {
particle->specie()->dynamicDensityField()->logMovement(
particle->cylinderPosition(), particle->kineticEnergy_eV(), new_mu,
particle->weight());
old_position_cyl, old_kinetic_energy, old_mu, particle->weight());
#ifdef debug
particle->deposited();
#endif
}
// particle->resetTimeInBin();
particle->specie()->currentCollector()->collect(particle, old_position_cyl);
@@ -249,10 +232,10 @@ void BorisMover::movementStep(
/*
Might be slower, but essentially the same code */
const blitz::TinyVector<double, 3> u_minus =
v + (acceleration * timestep) / (2.);
v + (acceleration * maximal_time_step_s_) / (2.);
const double gamma = kinematic::relativisticGamma(u_minus);;
const blitz::TinyVector<double, 3> temp_1 =
(q * magnetic_field * timestep) / (2. * m * gamma);
(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 =
@@ -260,7 +243,7 @@ void BorisMover::movementStep(
const blitz::TinyVector<double, 3> u_plus =
u_minus + (blitz::cross(u_prime, temp_2));
const blitz::TinyVector<double, 3> u_new =
u_plus + (acceleration * timestep) / (2.);
u_plus + (acceleration * maximal_time_step_s_) / (2.);
particle->velocity(CartesianVector(u_new));
+14 -62
View File
@@ -229,7 +229,8 @@ void Simulation::start() {
// process both density and beta events before a cpu synchronization
processDensityEvents(particles_per_bin, density_weight_factor);
processEvents(events_of_cycle, beta_particle_weight);
if(num_cpu_syncs == 0)
processEvents(events_of_cycle, beta_particle_weight);
#ifdef debug_level1
DEBUG_LEVEL_1("Processing timestep: " + std::to_string(timesteps));
@@ -270,21 +271,7 @@ void Simulation::start() {
SimulationTimer::eventStartPoint("Reset");
if(reset) {
SIM_INFO("Do resets and increase the timestep for timeresolution.");
#ifdef debug
std::stringstream out;
out << "Ions (T+) lost at wall (physical value): " << phyLossAtWallIon;
phyLossAtWallIon = 0.;
double total_tp = 0;
double total_tp_sync = 0.;
#endif
for(auto& pair : tracked_species_) {
#ifdef debug
if(pair.second->type() == species::ImplementedSpecies::Tplus) {
total_tp = blitz::sum(pair.second->dynamicDensityField()->timeInBinArray());
total_tp_sync = blitz::sum(
pair.second->densityForDrawing()->timeInBinArray());
}
#endif
pair.second->dynamicDensityField()->resetDynamicDensityField();
pair.second->currentCollector()->resetCurrentCollector();
@@ -294,17 +281,6 @@ void Simulation::start() {
// the density from which new particles are drawn is only updated after a timestep
pair.second->positionDistributionFromDensity()->setMaximumDensity();
}
#ifdef debug
out << "\n\ttotal number in tp: " << total_tp;
out << "\n\tdiff to expected: " << (timesteps+1)*0.02 - total_tp;
out << "\n\ttotal number created: " << createdDensity;
out << "\n\tdiff to expected: " << (timesteps)*0.02/MpiCommunication::mpiSize()
- createdDensity;
out << "\n\ttotal number in density drawing field: " << total_tp_sync;
out << "\n\tdiff to in density: " << total_tp_sync - total_tp;
DEBUG(out.str());
createdDensity = 0;
#endif
leftover_events_time_resolution = beta_decay_events;
leftover_density_events = particles_per_bin;
@@ -496,8 +472,6 @@ void Simulation::processUntilTermination(
}
if (particle.specie()->terminator()->reachedMaxTimeAlive(
&particle) &&
!particle.specie()->terminator()->leftSimulationDomain(
&particle)) {
#ifdef debug_level3
DEBUG_LEVEL_3("Terminate particle " + particle.specie()->name() +
@@ -506,6 +480,10 @@ void Simulation::processUntilTermination(
if(particle.specie()->deletionLogger()) {
particle.specie()->deletionLogger()->logDeletion(particle.cylinderPosition(),
DeletionType::max_time_alive);
if(particle.kineticEnergy_eV() > 2000) {
particle.specie()->deletionLogger()->logDeletion(particle.cartesianPosition(),
DeletionType::loss_above_2keV);
}
#ifdef debug
if(!particle.wasDeposited()) {
std::stringstream out;
@@ -530,13 +508,12 @@ void Simulation::processUntilTermination(
if (particle.specie()->deletionLogger()) {
particle.specie()->deletionLogger()->logDeletion(particle.cylinderPosition(),
DeletionType::wall);
particle.specie()->deletionLogger()->logDeletion(particle,
PhysicalDeletionType::wall);
if(particle.kineticEnergy_eV() > 2000) {
particle.specie()->deletionLogger()->logDeletion(particle.cylinderPosition(),
DeletionType::loss_above_2keV);
}
}
#ifdef debug
if(particle.specie()->type() == species::ImplementedSpecies::Tplus) {
phyLossAtWallIon += particle.weight();
}
if(particle.wasDeposited()) {
std::stringstream out;
out << "particle " << particle.specie()->name();
@@ -569,21 +546,6 @@ void Simulation::processUntilTermination(
// macroscopic_cross_sections_m);
SimulationTimer::eventStopPoint(std::string("Interaction"));
// particle has reached time resolution, but was not deposited due to
// the interaction taking place at the same time.
// If the particle is not destroyed in the interaction put it into the mover
// in order to log the position. (Not ideal, but otherwise it would be a bit
// more effort to calculate the pitchangle here again...)
// should work, since the check for died in bin should immediatly happen (hopefully)
// if(particle.specie()->terminator()->reachedMaxTimeAlive(&particle)
// && !particle.wasDeposited() && !output.deleteCurrentParticle()) {
// particle.specie()->mover()->move(&particle, 1,
// electric_field_.get(), magnetic_field_.get(), gravity_field_.get());
// if(!particle.wasDeposited()) {
// ERROR("particle was not deposited even though it should have been");
// }
// }
particle.increaseInteractionCounter();
for (auto& new_particle : output.newParticles()) {
@@ -605,6 +567,10 @@ void Simulation::processUntilTermination(
if (particle.specie()->deletionLogger()) {
particle.specie()->deletionLogger()->logDeletion(
particle.cylinderPosition(), DeletionType::max_interactions);
if(particle.kineticEnergy_eV() > 2000) {
particle.specie()->deletionLogger()->logDeletion(particle.cylinderPosition(),
DeletionType::loss_above_2keV);
}
}
break;
}
@@ -745,20 +711,6 @@ std::vector<particle::Particle> Simulation::determineInjectedDensityParticles(
injector_->drawNewParticleFromDensityField(random->engine(),
pair.second, new_particles, magnetic_field_,
weight_factor, injection_time);
#ifdef debug
if(!new_particles.empty()) {
if(new_particles.back().specie()->type() == species::ImplementedSpecies::Tplus
&& pair.second->type() == species::ImplementedSpecies::Tplus) {
// if(createdDensity + new_particles.back().weight() == createdDensity){
// std::stringstream out;
// out << "catastrophic cancellation!\ncreated density: " << createdDensity;
// out << "\n\tweight: " << new_particles.back().weight();
// DEBUG(out.str());
// }
createdDensity += new_particles.back().weight();
}
}
#endif
}
// if species is not done tmp_done && ... = false
+4 -109
View File
@@ -37,10 +37,6 @@ VelocityDistributionFromDensityField::VelocityDistributionFromDensityField(
pitchAngleBinBorders_(density_field->pitchAngleBinning()->binBorders()),
theta_distribution_(std::uniform_real_distribution<double>(0.,1.)),
mass_kg_(mass_kg) {
double maxAbsExponent = std::max(std::abs(std::log10(energyBinBorders_.front())),
std::abs(std::log10(energyBinBorders_.back())));
alpha_lower_limit = -300/maxAbsExponent;
alpha_upper_limit = 300/maxAbsExponent;
// necessary to include the underflow bin in the energy domain
energyBinBorders_.insert(energyBinBorders_.begin(), 0);
}
@@ -71,7 +67,7 @@ CartesianVector VelocityDistributionFromDensityField::rollVelocity(
// draw pitch angle from DynamicDensityField
blitz::Array<double, 1> pitchAngleValues = density_field_->
blitz::Array<float, 1> pitchAngleValues = density_field_->
sliceTimeInBinArray(position, energy_eV);
std::piecewise_constant_distribution<double> pitchAngle_dist(
@@ -117,7 +113,7 @@ CartesianVector VelocityDistributionFromDensityField::rollVelocity(
// draw pitch angle from DynamicDensityField
blitz::Array<double, 1> pitchAngleValues = density_field_->
blitz::Array<float, 1> pitchAngleValues = density_field_->
sliceTimeInBinArray(position, energy_eV);
std::piecewise_constant_distribution<double> pitchAngle_dist(
@@ -214,78 +210,8 @@ CartesianVector VelocityDistributionFromDensityField::drawVelocity(
const double pitch_angle_step = pitchAngleBinBorders_[index(3) + 1]
- pitchAngleBinBorders_[index(3)];
// double energy_eV = energyBinBorders_[index(4) + 1]
// + theta_distribution_(*engine)*energy_step;
double energy_eV;
double alpha = 1;
if(index(4) == -1) {
// linear drawing inbetween E=0, N(0)=0 to E=Er and N(Er)=Nr
energy_eV = energyBinBorders_[index(4) + 2]*
std::sqrt(theta_distribution_(*engine));
// #ifdef debug
// std::stringstream out;
// out << "underflow bin.\n\tdrawn energy: " << energy_eV;
// out << "\n\tenergy value left: " << energyBinBorders_[index(4)+1];
// out << "\n\tenergy value right: " << energyBinBorders_[index(4)+2];
// DEBUG(out.str());
// #endif
} else {
double nl = density_field_->particleNumber(index);
double nr = density_field_->particleNumber(index +
blitz::TinyVector<int, 5>(0,0,0,0,1));
// double alpha;
// no reason for specific values, just to avoid these cases
if(nl == 0) {
alpha = 1;
} else if (nr == 0) {
alpha = -1;
} else if (nr == 0 && nl == 0) {
alpha = 0;
} else {
alpha = std::log(nr/nl)/std::log(energyBinBorders_[index(4) + 2]/
energyBinBorders_[index(4) + 1]);
}
// alpha = std::abs(alpha) > 100 ?(alpha/std::abs(alpha))*100 : alpha;
alpha = std::min(std::max(alpha, alpha_lower_limit), alpha_upper_limit);
if(alpha == -1) {
energy_eV = std::pow(energyBinBorders_[index(4) + 2]
/ energyBinBorders_[index(4) + 1], theta_distribution_(*engine)) *
energyBinBorders_[index(4) + 1];
// energy_eV = (std::pow(2, theta_distribution_(*engine)) - 1)*energy_step
// + energyBinBorders_[index(4) + 1];
// #ifdef debug
// std::stringstream out;
// out << "alpha = -1\n\tdrawn energy: " << energy_eV;
// out << "\n\tenergy value left: " << energyBinBorders_[index(4)+1];
// out << "\n\tenergy value right: " << energyBinBorders_[index(4)+2];
// DEBUG(out.str());
// #endif
} else {
double xl_alphap1 = std::pow(energyBinBorders_[index(4) + 1], alpha + 1);
double xr_alphap1 = std::pow(energyBinBorders_[index(4) + 2], alpha + 1);
energy_eV = std::pow((xr_alphap1 - xl_alphap1)*
theta_distribution_(*engine) + xl_alphap1, 1/(alpha + 1));
// #ifdef debug
// if(std::abs(alpha) > 100) {
// std::stringstream out;
// out << "alpha: " << alpha;
// out << "\nxl_alphap1: " << xl_alphap1;
// out << "\nxr_alphap1: " << xr_alphap1;
// DEBUG(out.str());
// }
// #endif
// #ifdef debug
// std::stringstream out;
// out << "alpha = "<< alpha << "\n\tdrawn energy: " << energy_eV;
// out << "\n\tenergy value left: " << energyBinBorders_[index(4)+1];
// out << "\n\tenergy value right: " << energyBinBorders_[index(4)+2];
// DEBUG(out.str());
// #endif
}
}
double energy_eV = energyBinBorders_[index(4) + 1]
+ theta_distribution_(*engine)*energy_step;
double pitchAngle = pitchAngleBinBorders_[index(3)]
+ theta_distribution_(*engine)*pitch_angle_step;
@@ -295,15 +221,6 @@ CartesianVector VelocityDistributionFromDensityField::drawVelocity(
std::stringstream out;
out << "pitchAngle: " << pitchAngle;
out << "\nenergy value: " << energy_eV;
out << "\nalpha: " << alpha;
out << "\ncurrent index: " << index;
out << "\nNl: " << density_field_->particleNumber(index);
out << "\nNl: " << density_field_->particleNumber(index +
blitz::TinyVector<int, 5>(0,0,0,0,1));
out << "\nEl: " << energyBinBorders_[index(4) + 1];
out << "\nEr: " << energyBinBorders_[index(4) + 2];
out << "\nEl^(alpha+1): " << std::pow(energyBinBorders_[index(4) + 1], alpha + 1);
out << "\nEr^(alpha+1): " << std::pow(energyBinBorders_[index(4) + 2], alpha + 1);
// out << "\nenergy values: " << energyValues;
// out << "\npitch angle values: " << pitchAngleValues;
out << "\nenergy bin borders: [ ";
@@ -314,28 +231,6 @@ CartesianVector VelocityDistributionFromDensityField::drawVelocity(
ERROR(out.str());
}
// if(std::isinf(energy_eV)) {
// std::stringstream out;
// out << "pitchAngle: " << pitchAngle;
// out << "\nenergy value: " << energy_eV;
// out << "\ncurrent index: " << index;
// out << "\nalpha: " << alpha;
// out << "\nNl: " << density_field_->particleNumber(index);
// out << "\nNl: " << density_field_->particleNumber(index +
// blitz::TinyVector<int, 5>(0,0,0,0,1));
// out << "\nEl: " << energyBinBorders_[index(4) + 1];
// out << "\nEr: " << energyBinBorders_[index(4) + 2];
// // out << "\nenergy values: " << energyValues;
// // out << "\npitch angle values: " << pitchAngleValues;
// out << "\nenergy bin borders: [ ";
// for(auto element : energyBinBorders_) {
// out << element << " ";
// }
// out << "]";
// ERROR(out.str());
// }
#endif
#ifdef debug_level2