365 lines
11 KiB
C++
Executable File
365 lines
11 KiB
C++
Executable File
#include "generators/species_generator.h"
|
|
|
|
#include "constants/physical_constants.h"
|
|
#include "generators/current_collector_generator.h"
|
|
#include "generators/density_field_generator.h"
|
|
#include "generators/drift_velocity_generator.h"
|
|
#include "generators/logger_generator.h"
|
|
#include "generators/mass_generator.h"
|
|
#include "generators/mover_generator.h"
|
|
#include "generators/reflector_generator.h"
|
|
#include "generators/terminator_generator.h"
|
|
#include "generators/velocity_distribution_generator.h"
|
|
#include "simulation/errors.h"
|
|
|
|
#include "simulation/timer.h"
|
|
|
|
namespace generator {
|
|
|
|
std::map<std::string, species::ImplementedSpecies>
|
|
SpeciesAlias::specie_aliases = {
|
|
{"electron", species::ImplementedSpecies::electron},
|
|
{"T2+", species::ImplementedSpecies::T2plus},
|
|
{"T3+", species::ImplementedSpecies::T3plus},
|
|
{"T+", species::ImplementedSpecies::Tplus},
|
|
{"T", species::ImplementedSpecies::T},
|
|
{"T2", species::ImplementedSpecies::T2}};
|
|
|
|
species::ImplementedSpecies SpeciesAlias::alias(const std::string& name) {
|
|
if (isAlias(name)) {
|
|
return specie_aliases.at(name);
|
|
} else {
|
|
ERROR("This specie has no alias: " + name);
|
|
return specie_aliases.at(name);
|
|
}
|
|
}
|
|
|
|
std::string SpeciesAlias::name(species::ImplementedSpecies specie) {
|
|
for (auto& pair : specie_aliases) {
|
|
if (pair.second == specie) {
|
|
return pair.first;
|
|
}
|
|
}
|
|
return std::string("");
|
|
}
|
|
|
|
bool SpeciesAlias::isAlias(const std::string& name) {
|
|
return static_cast<bool>(specie_aliases.count(name));
|
|
}
|
|
|
|
std::map<species::ImplementedSpecies, double> SpeciesCharges::charge_ = {
|
|
{species::ImplementedSpecies::electron, -SI::e},
|
|
{species::ImplementedSpecies::T, 0.},
|
|
{species::ImplementedSpecies::T2, 0.},
|
|
{species::ImplementedSpecies::Tplus, SI::e},
|
|
{species::ImplementedSpecies::T2plus, SI::e},
|
|
{species::ImplementedSpecies::T3plus, SI::e}};
|
|
|
|
double SpeciesCharges::charge(species::ImplementedSpecies species) {
|
|
return charge_.at(species);
|
|
}
|
|
|
|
SpeciesBuilder::SpeciesBuilder(Parameter& parameter) {
|
|
initTimer();
|
|
|
|
initBackgroundSpecies(parameter);
|
|
initTrackedSpecies(parameter);
|
|
|
|
fillBackgroundSpecieType(parameter);
|
|
fillTrackedSpecieType(parameter);
|
|
|
|
// same for all
|
|
fillMass();
|
|
fillCharge();
|
|
fillVelocityDistribution(parameter);
|
|
fillDriftVelocity(parameter);
|
|
|
|
// only background
|
|
fillBackgroundDensity(parameter);
|
|
|
|
// only trackable
|
|
fillMover(parameter);
|
|
fillTerminator(parameter);
|
|
fillReflector(parameter);
|
|
fillTrackedDensity(parameter);
|
|
|
|
buildSpecies();
|
|
fillInteractions(parameter);
|
|
fillCurrentCollector(parameter);
|
|
fillLoggers(parameter);
|
|
};
|
|
|
|
std::map<std::string, std::shared_ptr<species::TrackedSpecie>>
|
|
SpeciesBuilder::trackedSpecies() {
|
|
return tracked_species;
|
|
}
|
|
std::map<std::string, std::shared_ptr<species::BackgroundSpecie>>
|
|
SpeciesBuilder::backgroundSpecies() {
|
|
return background_species;
|
|
}
|
|
|
|
std::shared_ptr<Timer> SpeciesBuilder::timer() {
|
|
return timer_;
|
|
}
|
|
|
|
std::shared_ptr<Timer> SpeciesBuilder::lastSyncTimer() {
|
|
return last_sync_timer_;
|
|
}
|
|
|
|
void SpeciesBuilder::initTimer() {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Init timer.");
|
|
#endif
|
|
timer_ = std::shared_ptr<Timer>(new Timer());
|
|
last_sync_timer_ = std::shared_ptr<Timer>(new Timer());
|
|
}
|
|
|
|
void SpeciesBuilder::initBackgroundSpecies(Parameter& parameter) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Init background species info.");
|
|
#endif
|
|
const char* key;
|
|
json_t* value;
|
|
json_object_foreach(parameter.jsonObjectFromBase({"Species", "Background"}),
|
|
key, value) {
|
|
background_species_info[std::string(key)] = BackgroundSpeciesBuilderInfo();
|
|
background_species_info[std::string(key)].name = std::string(key);
|
|
background_species_info[std::string(key)].path_in_json = {
|
|
"Species", "Background", std::string(key)};
|
|
}
|
|
}
|
|
|
|
void SpeciesBuilder::initTrackedSpecies(Parameter& parameter) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Init tracked species info.");
|
|
#endif
|
|
const char* key;
|
|
json_t* value;
|
|
json_object_foreach(parameter.jsonObjectFromBase({"Species", "Tracked"}), key,
|
|
value) {
|
|
tracked_species_info[std::string(key)] = TrackedSpeciesBuilderInfo();
|
|
tracked_species_info[std::string(key)].name = std::string(key);
|
|
tracked_species_info[std::string(key)].path_in_json = {"Species", "Tracked",
|
|
std::string(key)};
|
|
}
|
|
}
|
|
|
|
void SpeciesBuilder::fillBackgroundSpecieType(Parameter& parameter) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Fill background species type.");
|
|
#endif
|
|
for (auto& b : background_species_info) {
|
|
std::string type = json_string(parameter.jsonObjectFromBase(
|
|
json_path_append(b.second.path_in_json, {"ParticleType"})));
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3("Type: " + type);
|
|
#endif
|
|
if (SpeciesAlias::isAlias(type)) {
|
|
b.second.type = SpeciesAlias::alias(type);
|
|
} else {
|
|
ERROR("This specie type is not implemented: " + type);
|
|
}
|
|
}
|
|
}
|
|
|
|
void SpeciesBuilder::fillTrackedSpecieType(Parameter& parameter) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Fill tracked species type.");
|
|
#endif
|
|
for (auto& b : tracked_species_info) {
|
|
std::string type = json_string(parameter.jsonObjectFromBase(
|
|
json_path_append(b.second.path_in_json, {"ParticleType"})));
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3("Type: " + type);
|
|
#endif
|
|
if (SpeciesAlias::isAlias(type)) {
|
|
b.second.type = SpeciesAlias::alias(type);
|
|
} else {
|
|
ERROR("This specie type is not implemented: " + type);
|
|
}
|
|
}
|
|
}
|
|
|
|
void SpeciesBuilder::fillBackgroundDensity(Parameter& parameter) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Fill background density.");
|
|
#endif
|
|
for (auto& b : background_species_info) {
|
|
b.second.density = buildConstantDensityField(parameter.jsonObjectFromBase(
|
|
json_path_append(b.second.path_in_json, {"ConstantDensityField"})));
|
|
}
|
|
}
|
|
|
|
void SpeciesBuilder::fillVelocityDistribution(Parameter& parameter) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Fill velocity distributions.");
|
|
#endif
|
|
for (auto& b : background_species_info) {
|
|
b.second.velocity_distribution = buildVelocityDistribution(
|
|
parameter,
|
|
json_path_append(b.second.path_in_json, {"VelocityDistribution"}));
|
|
}
|
|
for (auto& b : tracked_species_info) {
|
|
b.second.velocity_distribution = buildVelocityDistribution(
|
|
parameter,
|
|
json_path_append(b.second.path_in_json, {"VelocityDistribution"}));
|
|
}
|
|
}
|
|
|
|
void SpeciesBuilder::fillDriftVelocity(Parameter& parameter) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Fill drift velocity.");
|
|
#endif
|
|
for (auto& b : background_species_info) {
|
|
b.second.drift_velocity = buildDriftVelocityIncludingRedirection(
|
|
parameter, json_path_append(b.second.path_in_json, {"DriftVelocity"}));
|
|
}
|
|
for (auto& b : tracked_species_info) {
|
|
b.second.drift_velocity = buildDriftVelocityIncludingRedirection(
|
|
parameter, json_path_append(b.second.path_in_json, {"DriftVelocity"}));
|
|
}
|
|
}
|
|
|
|
void SpeciesBuilder::fillMass() {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Fill masses.");
|
|
#endif
|
|
for (auto& b : background_species_info) {
|
|
b.second.mass_kg = SpeciesMasses::mass(b.second.type);
|
|
}
|
|
for (auto& b : tracked_species_info) {
|
|
b.second.mass_kg = SpeciesMasses::mass(b.second.type);
|
|
}
|
|
}
|
|
|
|
void SpeciesBuilder::fillCharge() {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Fill charges.");
|
|
#endif
|
|
for (auto& b : background_species_info) {
|
|
b.second.charge_C = SpeciesCharges::charge(b.second.type);
|
|
}
|
|
for (auto& b : tracked_species_info) {
|
|
b.second.charge_C = SpeciesCharges::charge(b.second.type);
|
|
}
|
|
}
|
|
|
|
void SpeciesBuilder::fillMover(Parameter& parameter) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Fill movers.");
|
|
#endif
|
|
for (auto& b : tracked_species_info) {
|
|
if ((b.second.mover == nullptr)) {
|
|
b.second.mover = buildMover(
|
|
parameter, json_path_append(b.second.path_in_json, {"Mover"}));
|
|
}
|
|
}
|
|
}
|
|
|
|
void SpeciesBuilder::fillTerminator(Parameter& parameter) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Fill terminators.");
|
|
#endif
|
|
for (auto& b : tracked_species_info) {
|
|
if ((b.second.terminator == nullptr)) {
|
|
b.second.terminator = buildTerminator(
|
|
parameter, json_path_append(b.second.path_in_json, {"Terminator"}));
|
|
}
|
|
}
|
|
}
|
|
|
|
void SpeciesBuilder::fillReflector(Parameter& parameter) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Fill reflectors.");
|
|
#endif
|
|
for (auto& b : tracked_species_info) {
|
|
if ((b.second.reflector == nullptr)) {
|
|
b.second.reflector = buildReflector(
|
|
parameter, json_path_append(b.second.path_in_json, {"Reflector"}));
|
|
}
|
|
}
|
|
}
|
|
|
|
// TBD: Fill and create dynamic density fields with pitch angle distribution
|
|
void SpeciesBuilder::fillTrackedDensity(Parameter& parameter) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Fill tracked density.");
|
|
#endif
|
|
for (auto& b : tracked_species_info) {
|
|
b.second.density_field = buildDynamicDensityField(
|
|
parameter,
|
|
json_path_append(b.second.path_in_json,
|
|
{"DensityField", "DynamicDensityField"}),
|
|
timer_);
|
|
|
|
// not the optimal solution! (has to be optimized)
|
|
b.second.density_drawing_field = buildDynamicDensityField(
|
|
parameter,
|
|
json_path_append(b.second.path_in_json,
|
|
{"DensityField", "DynamicDensityField"}),
|
|
timer_);
|
|
}
|
|
}
|
|
|
|
void SpeciesBuilder::buildSpecies() {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Build species from info.");
|
|
#endif
|
|
for (auto& b : background_species_info) {
|
|
background_species[b.first] = std::shared_ptr<species::BackgroundSpecie>(
|
|
new species::BackgroundSpecie(
|
|
b.second.type, b.second.name, b.second.mass_kg, b.second.charge_C,
|
|
b.second.density, b.second.velocity_distribution,
|
|
b.second.drift_velocity));
|
|
}
|
|
|
|
for (auto& b : tracked_species_info) {
|
|
tracked_species[b.first] =
|
|
std::shared_ptr<species::TrackedSpecie>(new species::TrackedSpecie(
|
|
b.second.type, b.second.name, b.second.mass_kg, b.second.charge_C,
|
|
b.second.mover, b.second.terminator, b.second.reflector,
|
|
b.second.density_field, b.second.velocity_distribution,
|
|
b.second.drift_velocity, b.second.density_drawing_field));
|
|
}
|
|
}
|
|
|
|
void SpeciesBuilder::fillCurrentCollector(Parameter& parameter) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Fill current collectors.");
|
|
#endif
|
|
for (auto& b : tracked_species) {
|
|
auto current_collector = buildCurrentCollector(
|
|
parameter,
|
|
json_path_append(tracked_species_info.at(b.first).path_in_json,
|
|
{"CurrentCollector"}),
|
|
timer_);
|
|
b.second->currentCollector(current_collector);
|
|
}
|
|
}
|
|
|
|
void SpeciesBuilder::fillLoggers(Parameter& parameter) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Fill loggers.");
|
|
#endif
|
|
for (auto& b : tracked_species) {
|
|
auto deletion_logger = buildDeletionLogger(
|
|
parameter,
|
|
json_path_append(tracked_species_info.at(b.first).path_in_json,
|
|
{"Loggers"}));
|
|
b.second->deletionLogger(deletion_logger);
|
|
}
|
|
}
|
|
|
|
void SpeciesBuilder::fillInteractions(Parameter& parameter) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Fill interactions.");
|
|
#endif
|
|
for (auto& b : tracked_species) {
|
|
json_path path = tracked_species_info[b.first].path_in_json;
|
|
path = json_path_append(path, {"Interactions"});
|
|
addInteractions(b.second, tracked_species, background_species,
|
|
parameter, path);
|
|
}
|
|
}
|
|
|
|
}; // namespace generator
|