Files
karl/src/generators/species_generator.cpp
T

310 lines
10 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},
{"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::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_;
}
void SpeciesBuilder::initTimer() {
DEBUG_LEVEL_2("Init timer.");
timer_ = std::shared_ptr<Timer>(new Timer());
}
void SpeciesBuilder::initBackgroundSpecies(Parameter& parameter) {
DEBUG_LEVEL_2("Init background species info.");
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) {
DEBUG_LEVEL_2("Init tracked species info.");
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) {
DEBUG_LEVEL_2("Fill background species type.");
for (auto& b : background_species_info) {
std::string type = json_string(parameter.jsonObjectFromBase(
json_path_append(b.second.path_in_json, {"ParticleType"})));
DEBUG_LEVEL_3("Type: " + type);
if (SpeciesAlias::isAlias(type)) {
b.second.type = SpeciesAlias::alias(type);
} else {
ERROR("This specie type is not implemented: " + type);
}
}
}
void SpeciesBuilder::fillTrackedSpecieType(Parameter& parameter) {
DEBUG_LEVEL_2("Fill tracked species type.");
for (auto& b : tracked_species_info) {
std::string type = json_string(parameter.jsonObjectFromBase(
json_path_append(b.second.path_in_json, {"ParticleType"})));
DEBUG_LEVEL_3("Type: " + type);
if (SpeciesAlias::isAlias(type)) {
b.second.type = SpeciesAlias::alias(type);
} else {
ERROR("This specie type is not implemented: " + type);
}
}
}
void SpeciesBuilder::fillBackgroundDensity(Parameter& parameter) {
DEBUG_LEVEL_2("Fill background density.");
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) {
DEBUG_LEVEL_2("Fill velocity distributions.");
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) {
DEBUG_LEVEL_2("Fill drift velocity.");
for (auto& b : background_species_info) {
b.second.drift_velocity = buildDriftVelocity(
parameter, json_path_append(b.second.path_in_json, {"DriftVelocity"}));
}
for (auto& b : tracked_species_info) {
b.second.drift_velocity = buildDriftVelocity(
parameter, json_path_append(b.second.path_in_json, {"DriftVelocity"}));
}
}
void SpeciesBuilder::fillMass() {
DEBUG_LEVEL_2("Fill masses.");
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() {
DEBUG_LEVEL_2("Fill charges");
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) {
DEBUG_LEVEL_2("Fill movers.");
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) {
DEBUG_LEVEL_2("Fill terminators.");
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) {
DEBUG_LEVEL_2("Fill reflectors.");
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"}));
}
}
}
void SpeciesBuilder::fillTrackedDensity(Parameter& parameter) {
DEBUG_LEVEL_2("Fill tracked density.");
for (auto& b : tracked_species_info) {
b.second.density_field = buildDynamicDensityField(
parameter,
json_path_append(b.second.path_in_json,
{"DensityField", "DynamicDensityField"}),
timer_);
}
}
void SpeciesBuilder::buildSpecies() {
DEBUG_LEVEL_2("Build species from info.");
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));
}
}
void SpeciesBuilder::fillCurrentCollector(Parameter& parameter) {
DEBUG_LEVEL_2("Fill current collectors.");
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) {
DEBUG_LEVEL_2("Fill loggers.");
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) {
DEBUG_LEVEL_2("Fill interactions.");
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