From dfbb67a960e1ee8f3f98a62b0cdd210580d269fc Mon Sep 17 00:00:00 2001 From: Jonas Kellerer Date: Fri, 11 Nov 2022 13:43:36 +0100 Subject: [PATCH] Start of solver, missing connection, density interpolation, cpu sync --- include/poisson_solver/poisson_solver.h | 76 +++++++++ src/poisson_solver/poisson_solver.cpp | 203 ++++++++++++++++++++++++ 2 files changed, 279 insertions(+) create mode 100755 include/poisson_solver/poisson_solver.h create mode 100755 src/poisson_solver/poisson_solver.cpp diff --git a/include/poisson_solver/poisson_solver.h b/include/poisson_solver/poisson_solver.h new file mode 100755 index 0000000..40a5df2 --- /dev/null +++ b/include/poisson_solver/poisson_solver.h @@ -0,0 +1,76 @@ +#ifndef POISSON_SOLVER_H +#define POISSON_SOLVER_H + +#include "blitz/array.h" + +class Geometry; +class CylinderBinning; + +enum class BoundaryType { + no_boundary, + dirichlet, + neumann_to_top, + neumann_to_bottom, + neumann_to_hull +}; + +struct PotentialGridPoint { + BoundaryType type; + double value; +}; + +struct GlobalBoundary { + double boundary_value; + BoundaryType type +}; + +class PoissonSolver { + public: + PoissonSolver(GlobalBoundary bottom_boundary, + GlobalBoundary top_boundary, + GlobalBoundary hull_boundary, + std::shared_ptr geometry, + std::shared_ptr density_binning, + std::vector> electrodes, + double target_accuracy, + int num_cycles); + + void solvePotential(const blitz::Array charge_density); + blitz::Array potential(); + + private: + Boundary bottom_boundary_; + Boundary top_boundary_; + Boundary hull_boundary_; + std::shared_ptr geometry_; + std::vector> electrodes_; + std::shared_ptr density_binning_; + double target_accuracy_; + int num_cycles_; + + blitz::Array potential_; + + blitz::Array interpolateDensityToPotentialGrid( + const blitz::Array charge_density); + + void solverIteration(const blitz::Array& interpolated_density); + bool converged(const blitz::Array& interpolated_density); +}; + +class Electrode { + public: + Electrode(double potential); + virtual bool isOnElectrode(Coordinate* position) = 0; + double electrodePotential(); + + private: + const double potential_; +}; + +class RearWall : public Electrode { + public: + RearWall(double potential); + bool isOnElectrode(Coordinate* position); +}; + +#endif /* POISSON_SOLVER_H */ diff --git a/src/poisson_solver/poisson_solver.cpp b/src/poisson_solver/poisson_solver.cpp new file mode 100755 index 0000000..4368bf2 --- /dev/null +++ b/src/poisson_solver/poisson_solver.cpp @@ -0,0 +1,203 @@ +#include "poisson_solver/poisson_solver.h" +#include "simulation/errors.h" +#include "simulation/geometry.h" +#include "simulation/position_binning.h" + +PoissonSolver(GlobalBoundary bottom_boundary, + GlobalBoundary top_boundary, + GlobalBoundary hull_boundary, + std::shared_ptr geometry, + std::shared_ptr density_binning, + std::vector> electrodes, + double target_accuracy, + int num_cycles) + : bottom_boundary_(bottom_boundary), + top_boundary_(top_boundary), + hull_boundary_(hull_boundary), + geometry_(geometry), + density_binning_(density_binning), + potential_(blitz::Array( + blitz::Range(density_binning->binIndexMin()(0), + density_binning->binIndexMax()(0)), + blitz::Range(density_binning->binIndexMin()(1), + density_binning->binIndexMax()(1)), + blitz::Range(density_binning->binIndexMin()(2), + density_binning->binIndexMax()(2)))), + electrodes_(electrodes), + target_accuracy_(target_accuracy), + num_cycles_(num_cycles) { + // set inner values + blitz::TinyVector stepsize = density_binning_->stepsize(); + for (int i = potential_.lbound(0); i <= potential_.rbound(0) - 1; ++i) { + for (int j = potential_.lbound(1); j <= potential_.rbound(1); ++j) { + for (int k = potential_.lbound(2) + 1; k <= potential_.rbound(2) - 1; + ++k) { + potential_(i, j, k) = PotentialGridPoint(BoundaryType::no_boundary, 0.); + + CylinderCoordinate position(blitz::TinyVector( + i * stepsize(0), j * stepsize(1), k * stepsize)); + if (!geometry_->isInside(position)) { + potential_(i, j, k) = PotentialGridPoint(BoundaryType::dirichlet, 0.); + } + for (auto& electrode : electrodes_) { + if (electrode->isOnElectrode(position)) { + potential_(i, j, k) = PotentialGridPoint( + BoundaryType::dirichlet, electrode->electrodePotential()); + } + } + } + } + } + + // set global boundary + potential_(blitz::Range::all(), blitz::Range::all(), potential_.lbound(2)) = + PotentialGridPoint(bottom_boundary_->type, + bottom_boundary_->boundary_value); + potential_(blitz::Range::all(), blitz::Range::all(), potential_.rbound(2)) = + PotentialGridPoint(top_boundary_->type, top_boundary_->boundary_value); + potential_(potential_.rbound(0), blitz::Range::all(), blitz::Range::all()) = + PotentialGridPoint(hull_boundary_->type, hull_boundary_->boundary_value); +} + +blitz::Array PoissonSolver::solverIteration( + const blitz::Array& interpolated_density) { + blitz::TinyVector stepsize = density_binning_->stepsize(); + const double dr_sq = std::pow(stepsize(0), 2); // shorthand + const double dth_sq = std::pow(stezsize(1), 2); // shorthand + const double dz_sq = std::pow(stepsize(2), 2); // shorthand + + for (int i = potential_.lbound(0); i <= potential_.rbound(0); ++i) { + for (int j = potential_.lbound(1); j <= potential_.rbound(1); ++j) { + for (int k = potential_.lbound(2); k <= potential_.rbound(2); ++k) { + if (potential_(i, j, k).type == BoundaryType::dirichlet) { + potential_(i, j, k).value = potential_(i, j, k).value; + } else if (potential_(i, j, k).type == BoundaryType::neumann_to_top) { + potential_(i, j, k).value = + potential_(i, j, k - 1) + potential_(i, j, k).value * stepsize(2); + } else if (potential_(i, j, k).type == BoundaryType::neumann_to_bot) { + potential_(i, j, k).value = + potential_(i, j, k + 1) - potential_(i, j, k).value * stepsize(2); + } else if (potential_(i, j, k).type == BoundaryType::neumann_to_hull) { + potential_(i, j, k).value = + potential_(i - 1, j, k) + potential_(i, j, k).value; + } else if (i == 0) { + // special calculation for r=0 + const unit_factor = 1.; + // todo: change this factor to the correct value for SI + + const double scaling_factor = 4 / dr_sq + 2 / dz_sq; + potential_(i, j, k).value = + (1 / scaling_factor) * + ((4 / dr_sq) * potential_(i + 1, j, k).value + + (1 / dz_sq) * (potential_(i, j, k + 1).value + + potential_(i, j, k - 1).value) - + 4 * M_PI * interpolated_density(i, j, k) * unit_factor); + + } else { + const int jp1 = j + 1 > potential_.rbound(1) + ? 0 + : j + 1; // j+1 with periodic guard + const int jm1 = j - 1 < 0 ? potential_.rbound(1) + : j - 1; // j-1 with periodic guard + + const double scaling_factor = 2 / dr_sq + 1 / (2 * i * dr_sq) + + 2 / (std::pow(i, 2) * dr_sq * dth_sq) + + 2 / dz_sq; + const unit_factor = 1.; + potential_(i, j, k).value = + (1 / scaling_factor) * + ((1 / ((1 + 2 * i) * dr_sq)) * potential_(i + 1, j, k).value + + (1 / dr_sq) * potential_(i - 1, j, k).value + + (1 / (std::pow(i, 2) * dr_sq * dth_sq)) * + (potential_(i, jp1, k).value + potential_(i, jm1, k)) + + (1 / dz_sq) * (potential_(i, j, k + 1).value + + potential_(i, j, k - 1).value) + + 4 * M_PI * interpolated_density(i, j, k) * unit_factor); + } + } + } + } +} + +bool PoissonSolver::converged( + const blitz::Array& interpolated_density) { + blitz::TinyVector stepsize = density_binning_->stepsize(); + const double dr_sq = std::pow(stepsize(0), 2); // shorthand + const double dth_sq = std::pow(stezsize(1), 2); // shorthand + const double dz_sq = std::pow(stepsize(2), 2); // shorthand + + double cumulated_quadradic_difference = 0.; + double normalized_difference = 0.; + + for (int i = potential_.lbound(0); i < potential_.rbound(0); ++i) { + for (int j = potential_.lbound(1); j <= potential_.rbound(1); ++j) { + for (int k = potential_.lbound(2) + 1; k <= potential_.rbound(2) - 1; + ++k) { + if (i == 0) { + // special calculation for r=0 + const unit_factor = 1.; + // todo: change this factor to the correct value for SI + + const double scaling_factor = 4 / dr_sq + 2 / dz_sq; + const double difference = + (1 / scaling_factor) * + ((4 / dr_sq) * potential_(i + 1, j, k).value + + (1 / dz_sq) * (potential_(i, j, k + 1).value + + potential_(i, j, k - 1).value) - + 4 * M_PI * interpolated_density(i, j, k) * unit_factor) - + potential_(i, j, k).value; + cumulated_quadradic_difference += std::pow(difference, 2); + + } else { + const int jp1 = j + 1 > potential_.rbound(1) + ? 0 + : j + 1; // j+1 with periodic guard + const int jm1 = j - 1 < 0 ? potential_.rbound(1) + : j - 1; // j-1 with periodic guard + + const double scaling_factor = 2 / dr_sq + 1 / (2 * i * dr_sq) + + 2 / (std::pow(i, 2) * dr_sq * dth_sq) + + 2 / dz_sq; + const unit_factor = 1.; + const double difference = + (1 / scaling_factor) * + ((1 / ((1 + 2 * i) * dr_sq)) * potential_(i + 1, j, k).value + + (1 / dr_sq) * potential_(i - 1, j, k).value + + (1 / (std::pow(i, 2) * dr_sq * dth_sq)) * + (potential_(i, jp1, k).value + potential_(i, jm1, k)) + + (1 / dz_sq) * (potential_(i, j, k + 1).value + + potential_(i, j, k - 1).value) + + 4 * M_PI * interpolated_density(i, j, k) * unit_factor) - + potential_(i, j, k).value; + cumulated_quadradic_difference += std::pow(difference, 2); + } + } + } + } + const int nr = potential_.extent(0) - 1; + const int nth = potential_.extent(1); + const int nz = potential_.extent(2) - 2; + + normalized_difference = + std::sqrt(cumulated_quadradic_difference / (nr * nth * nz)); + DEBUG_LEVEL_0("Poisson solver reached a normalized difference of: " + + normalized_difference); + return normalized_difference < target_accuracy_; +} + +void PoissonSolver::solvePotential( + const blitz::Array charge_density) { + blitz::Array interpolated_density = + interpolateDensityToPotentialGrid(charge_density); + int it = 1; + do { + DEBUG_LEVEL_3("Poisson solver, iteraction: " + it); + solverIteration(interpolated_density); + if (it % 1000 == 0) { + if (converged(interpolated_density)) { + break; + }; + } + ++it; + } while (it < num_cycles_); +}