Files
karl/src/generators/poisson_solver_generator.cpp
T

137 lines
5.2 KiB
C++

#include "generators/poisson_solver_generator.h"
#include "poisson_solver/poisson_solver.h"
#include "generators/geometry_generator.h"
#include "generators/binning_generator.h"
#include <memory>
#include "simulation/errors.h"
namespace generator {
std::shared_ptr<PoissonSolver> buildPoissonSolver(
Parameter& parameter) {
#ifdef debug_level2
DEBUG_LEVEL_2("Generating Poisson solver.");
#endif
std::shared_ptr<Geometry> geometry;
json_t* object = json_object_by_path(
parameter.jsonFile(), {"PoissonSolver"});
//create geometry
if (json_object_by_path(object, {"BoundaryGeometry"})) {
geometry = buildGeometryIncludingRedirection(parameter,
json_path_append({"PoissonSolver"}, {"BoundaryGeometry"}));
} else {
ERROR("Cannot find boundary geometry for poisson solver.");
}
//create binning
json_path path_binning = json_path_append({"PoissonSolver"},
{"PositionBinning"});
CylinderBinning position_binning =
buildPositionBinningIncludingRedirection(parameter, path_binning);
auto boundaries = buildBoundaries(parameter, {"PoissonSolver", "Boundary"});
// build electrodes, might be good to put into own function
std::vector<std::shared_ptr<Electrode>> electrodes{};
const char* key;
json_t* value;
json_t* array = parameter.jsonObjectFromBase({"PoissonSolver", "Electrodes"});
json_object_foreach(array, key, value) {
std::string type = json_string(json_object_by_path(value, {"Type"}));
if(type == "RingElectrode") {
double zpos = json_read_length(json_object_by_path(value, {"zPosition"}));
double rInner = json_read_length(json_object_by_path(value, {"InnerRadius"}));
double rOuter = json_read_length(json_object_by_path(value, {"OuterRadius"}));
double potentialValue = json_double(json_object_by_path(value, {"Potential", "value"}));
#ifdef debug
std::stringstream out;
out << "Electrode " << std::string(key) << ":\n\t";
out << "z position: " << zpos << " m\n\t";
out << "inner radius: " << rInner << " m\n\t";
out << "outer radius: " << rOuter << " m\n\t";
out << "potential: " << potentialValue << " V";
DEBUG(out.str());
#endif
electrodes.push_back(
std::shared_ptr<RingElectrode>(
new RingElectrode(potentialValue, zpos, rInner, rOuter,
position_binning.stepsize())));
} else {
ERROR("Unknown or not implemented electrode type: " + type + ". Available options: \"RingElectrode\"");
}
}
double target_accuracy = parameter.doubleValue(
json_path_append({"PoissonSolver"}, {"TargetAccuracy"}));
int num_cycles = parameter.intValue(json_path_append({"PoissonSolver"},
{"NumCycles"}));
bool do_solver = parameter.boolValue(
json_path_append({"PoissonSolver"}, {"DoSolver"}), false);
return std::shared_ptr<PoissonSolver>(new PoissonSolver(boundaries.at("Bottom"),
boundaries.at("Top"), boundaries.at("Hull"), geometry, position_binning,
electrodes, target_accuracy, num_cycles, do_solver));
}
std::map<std::string, GlobalBoundary> buildBoundaries(Parameter& parameter,
const json_path& path_to_boundaries) {
#ifdef debug_level3
DEBUG_LEVEL_3("Creating Boundaries.");
#endif
std::map<std::string, GlobalBoundary> boundary_map {
{"Bottom", {0., BoundaryType::dirichlet}},
{"Top", {0., BoundaryType::dirichlet}},
{"Hull", {0., BoundaryType::dirichlet}}};
const std::map<std::string, BoundaryType> neumann_location_map {
{"Bottom", BoundaryType::neumann_to_bottom},
{"Top", BoundaryType::neumann_to_top},
{"Hull", BoundaryType::neumann_to_hull}};
const char* key;
json_t* value;
json_object_foreach(parameter.jsonObjectFromBase(path_to_boundaries),
key, value) {
#ifdef debug_level3
DEBUG_LEVEL_3("Creating " + std::string(key) + ": ");
#endif
if(!(boundary_map.find(std::string(key)) == boundary_map.end())) {
double boundary_value =
json_double(json_object_by_path(value, {"value"}));
std::string boundary_type =
json_string(json_object_by_path(value, {"type"}));
if (boundary_type == "dirichlet") {
boundary_map[std::string(key)].type = BoundaryType::dirichlet;
boundary_map[std::string(key)].boundary_value = boundary_value;
} else if (boundary_type == "neumann") {
boundary_map[std::string(key)].type =
neumann_location_map.at(std::string(key));
boundary_map[std::string(key)].boundary_value = boundary_value;
} else {
SIM_INFO("Unknown boundary type:" + boundary_type +
". Using default boundary (dirichtlet, value 0)");
continue;
}
#ifdef debug_level3
DEBUG_LEVEL_3("with type: " + boundary_type);
DEBUG_LEVEL_3("with value: " + std::to_string(boundary_value));
#endif
} else {
SIM_INFO(std::string(key) + ": unknown location. Is ignored.");
}
}
return boundary_map;
}
};