From 52a580ac63197110f7d262effaac7299d44fa13c Mon Sep 17 00:00:00 2001 From: Christian Sendlinger Date: Thu, 28 Mar 2024 15:57:04 +0100 Subject: [PATCH] Restructuring of poisson solver offset; inclusion of stride for poisson solver --- include/poisson_solver/poisson_solver.h | 13 +++-- src/generators/poisson_solver_generator.cpp | 16 +++++- src/poisson_solver/poisson_solver.cpp | 59 ++++++++++++--------- src/simulation/simulation.cpp | 11 ++-- 4 files changed, 60 insertions(+), 39 deletions(-) diff --git a/include/poisson_solver/poisson_solver.h b/include/poisson_solver/poisson_solver.h index 0586e4d..a739f74 100755 --- a/include/poisson_solver/poisson_solver.h +++ b/include/poisson_solver/poisson_solver.h @@ -54,9 +54,12 @@ class PoissonSolver { CylinderBinning& density_binning, std::vector> electrodes, double target_accuracy, - int num_cycles); + int num_cycles, + int solver_intervall, + int solver_offset); - void solvePotential(const blitz::Array charge_density); + void solvePotential(const blitz::Array charge_density, + int cpu_sync); blitz::Array potential(); std::shared_ptr staticElectricField(); const CylinderBinning* densityBinning(); @@ -68,8 +71,10 @@ class PoissonSolver { std::shared_ptr geometry_; std::vector> electrodes_; const CylinderBinning density_binning_; - double target_accuracy_; - int num_cycles_; + const double target_accuracy_; + const int num_cycles_; + const int solver_intervall_; + const int solver_offset_; // density binning defined on the cell centers, potential defined // such that it exists on the cell edges so that the azimuthal diff --git a/src/generators/poisson_solver_generator.cpp b/src/generators/poisson_solver_generator.cpp index a786b7c..299c66b 100644 --- a/src/generators/poisson_solver_generator.cpp +++ b/src/generators/poisson_solver_generator.cpp @@ -30,6 +30,20 @@ namespace generator { auto boundaries = buildBoundaries(parameter, {"PoissonSolver", "Boundary"}); + int solver_intervall = json_int(json_object_by_path(object, {"Intervall"})); + if (solver_intervall == 0 && + json_string(json_object_by_path( + object, {"Intervall"})) == "EachSync") { + solver_intervall = 1; + } + ASSERT_POSITIVE(&solver_intervall, + "The intervall for poisson solver stride must be positive" + " or 'EachSync'"); + + int solver_offset = json_int(json_object_by_path(object, {"Offset"})); + ASSERT_NOTNEGATIVE(&solver_offset, + "The offset for poisson solver has to be non-negative"); + std::vector> electrodes{}; double target_accuracy = parameter.doubleValue( @@ -40,7 +54,7 @@ namespace generator { return std::shared_ptr(new PoissonSolver(boundaries.at("Bottom"), boundaries.at("Top"), boundaries.at("Hull"), geometry, position_binning, - electrodes, target_accuracy, num_cycles)); + electrodes, target_accuracy, num_cycles, solver_intervall, solver_offset)); } std::map buildBoundaries(Parameter& parameter, diff --git a/src/poisson_solver/poisson_solver.cpp b/src/poisson_solver/poisson_solver.cpp index 445b726..68748ca 100755 --- a/src/poisson_solver/poisson_solver.cpp +++ b/src/poisson_solver/poisson_solver.cpp @@ -15,7 +15,9 @@ PoissonSolver::PoissonSolver(GlobalBoundary bottom_boundary, CylinderBinning& density_binning, std::vector> electrodes, double target_accuracy, - int num_cycles) + int num_cycles, + int solver_intervall, + int solver_offset) : bottom_boundary_(bottom_boundary), top_boundary_(top_boundary), hull_boundary_(hull_boundary), @@ -30,7 +32,9 @@ PoissonSolver::PoissonSolver(GlobalBoundary bottom_boundary, density_binning.binIndexMax()(2)))), electrodes_(electrodes), target_accuracy_(target_accuracy), - num_cycles_(num_cycles) { + num_cycles_(num_cycles), + solver_intervall_(solver_intervall), + solver_offset_(solver_offset) { blitz::TinyVector stepsize = density_binning_.stepsize(); for (int i = potential_.lbound(0); i <= potential_.ubound(0); ++i) { @@ -75,7 +79,29 @@ PoissonSolver::PoissonSolver(GlobalBoundary bottom_boundary, blitz::Range(density_binning.binIndexMin()(2), density_binning.binIndexMax()(2))); zero_chargedensity = 0.; - solvePotential(zero_chargedensity); + solvePotential(zero_chargedensity, 0); +} + +void PoissonSolver::solvePotential( + const blitz::Array charge_density, int cpu_sync) { + if ((cpu_sync % solver_intervall_ != 0) || ((cpu_sync <= solver_offset_) + && (cpu_sync > 0))) { + return; + } + DEBUG_LEVEL_3("Interpolating chargedensity."); + blitz::Array interpolated_density = + interpolateDensityToPotentialGrid(charge_density); + int it = 1; + do { + solverIteration(interpolated_density); + if (it % 1000 == 0) { + DEBUG_LEVEL_3("Poisson solver, iteration: " + std::to_string(it)); + if (converged(interpolated_density)) { + break; + }; + } + ++it; + } while (it < num_cycles_); } void PoissonSolver::solverIteration( @@ -223,24 +249,6 @@ bool PoissonSolver::converged( return normalized_difference < target_accuracy_; } -void PoissonSolver::solvePotential( - const blitz::Array charge_density) { - DEBUG_LEVEL_3("Interpolating chargedensity."); - blitz::Array interpolated_density = - interpolateDensityToPotentialGrid(charge_density); - int it = 1; - do { - solverIteration(interpolated_density); - if (it % 1000 == 0) { - DEBUG_LEVEL_3("Poisson solver, iteration: " + std::to_string(it)); - if (converged(interpolated_density)) { - break; - }; - } - ++it; - } while (it < num_cycles_); -} - blitz::Array PoissonSolver::potential() { // there might be a better way to do this... (if one can do array.memberValue without // explicitely looping) @@ -346,6 +354,10 @@ std::shared_ptr PoissonSolver::staticElectricField() { Eextent(0)*stepsize(0), Eextent(2)*stepsize(2))); } +const CylinderBinning* PoissonSolver::densityBinning() { + return &density_binning_; +} + Electrode::Electrode(double potential): potential_(potential) {}; double Electrode::electrodePotential() { @@ -360,9 +372,4 @@ RearWall::RearWall(double potential): Electrode(potential) {}; bool RearWall::isOnElectrode(const Coordinate& position) { return false; } - -const CylinderBinning* PoissonSolver::densityBinning() { - return &density_binning_; -} -//PotentialGridPoint::PotentialGridPoint(BoundaryType _type, double _value): type(_type), value(_value) {}; \ No newline at end of file diff --git a/src/simulation/simulation.cpp b/src/simulation/simulation.cpp index 65c0b2d..d427bfc 100755 --- a/src/simulation/simulation.cpp +++ b/src/simulation/simulation.cpp @@ -97,10 +97,6 @@ void Simulation::start() { parameter_.intValue(json_path({"Simulation", "EventStride"}), 1); const int total_simulated_events = parameter_.intValue(json_path({"Simulation", "SimulatedEvents"}), 1); - // Minimum number of cpu syncs before the potential is done the first time - // afterwards the potential is recalculated every cpu sync (maybe change to a stride) - const int poisson_offset = - parameter_.intValue(json_path({"Simulation", "PoissonOffset"}), 0); SIM_INFO("Total simulated events: " + std::to_string(total_simulated_events)); SIM_INFO("Simulated events before cpu sync: " + std::to_string(event_stride)); const int events_this_cpu = eventsThisCpu(total_simulated_events); @@ -125,10 +121,9 @@ void Simulation::start() { SIM_INFO("Poisson step."); SimulationTimer::eventStartPoint(std::string("PoissonStep")); - if(num_cpu_syncs > poisson_offset) { - poisson_solver_->solvePotential(chargedensity()); - electric_field_->updateDynamicField(poisson_solver_->staticElectricField()); - } + poisson_solver_->solvePotential(chargedensity(), num_cpu_syncs); + electric_field_->updateDynamicField(poisson_solver_->staticElectricField()); + SimulationTimer::eventStopPoint(std::string("PoissonStep")); SIM_INFO("Perform output.");