From 5f7f44b3375f7688acd507439acff54987392407 Mon Sep 17 00:00:00 2001 From: Christian Sendlinger Date: Fri, 19 Jan 2024 15:07:02 +0100 Subject: [PATCH] fixed errors, included missing memberfunctions --- include/poisson_solver/poisson_solver.h | 50 +++-- src/poisson_solver/poisson_solver.cpp | 264 ++++++++++++++++++------ 2 files changed, 228 insertions(+), 86 deletions(-) diff --git a/include/poisson_solver/poisson_solver.h b/include/poisson_solver/poisson_solver.h index 40a5df2..e5d966e 100755 --- a/include/poisson_solver/poisson_solver.h +++ b/include/poisson_solver/poisson_solver.h @@ -2,6 +2,11 @@ #define POISSON_SOLVER_H #include "blitz/array.h" +#include "simulation/vector_field.h" +#include + +// for now put the electric field calculation into +// the poisson solver class Geometry; class CylinderBinning; @@ -15,13 +20,30 @@ enum class BoundaryType { }; struct PotentialGridPoint { + //PotentialGridPoint(BoundaryType _type, double _value); BoundaryType type; double value; }; struct GlobalBoundary { double boundary_value; - BoundaryType type + BoundaryType type; +}; + +class Electrode { + public: + Electrode(double potential); + virtual bool isOnElectrode(const Coordinate& position) = 0; + double electrodePotential(); + + protected: + const double potential_; +}; + +class RearWall : public Electrode { + public: + RearWall(double potential); + bool isOnElectrode(const Coordinate& position); }; class PoissonSolver { @@ -37,17 +59,21 @@ class PoissonSolver { void solvePotential(const blitz::Array charge_density); blitz::Array potential(); + // std::shared_ptr staticElectricField(); private: - Boundary bottom_boundary_; - Boundary top_boundary_; - Boundary hull_boundary_; + GlobalBoundary bottom_boundary_; + GlobalBoundary top_boundary_; + GlobalBoundary hull_boundary_; std::shared_ptr geometry_; std::vector> electrodes_; std::shared_ptr density_binning_; double target_accuracy_; int num_cycles_; +// the potential is located on the grid corners (such that +// it is defined on r=0) (note that the density_binning is taken +// to be defined in the cell centers) blitz::Array potential_; blitz::Array interpolateDensityToPotentialGrid( @@ -57,20 +83,6 @@ class PoissonSolver { 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); -}; +// Electrodes can only be dirichlet boundary conditions #endif /* POISSON_SOLVER_H */ diff --git a/src/poisson_solver/poisson_solver.cpp b/src/poisson_solver/poisson_solver.cpp index 4368bf2..2b3a85f 100755 --- a/src/poisson_solver/poisson_solver.cpp +++ b/src/poisson_solver/poisson_solver.cpp @@ -2,8 +2,11 @@ #include "simulation/errors.h" #include "simulation/geometry.h" #include "simulation/position_binning.h" +#include "simulation/vector_field.h" +#include +#include -PoissonSolver(GlobalBoundary bottom_boundary, +PoissonSolver::PoissonSolver(GlobalBoundary bottom_boundary, GlobalBoundary top_boundary, GlobalBoundary hull_boundary, std::shared_ptr geometry, @@ -18,98 +21,112 @@ PoissonSolver(GlobalBoundary bottom_boundary, density_binning_(density_binning), potential_(blitz::Array( blitz::Range(density_binning->binIndexMin()(0), - density_binning->binIndexMax()(0)), + density_binning->binIndexMax()(0)+1), blitz::Range(density_binning->binIndexMin()(1), - density_binning->binIndexMax()(1)), + density_binning->binIndexMax()(1)), // the azimuthal direction is periodic blitz::Range(density_binning->binIndexMin()(2), - density_binning->binIndexMax()(2)))), + density_binning->binIndexMax()(2)+1))), 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; + for (int i = potential_.lbound(0); i <= potential_.ubound(0) - 1; ++i) { + for (int j = potential_.lbound(1); j <= potential_.ubound(1); ++j) { + for (int k = potential_.lbound(2) + 1; k <= potential_.ubound(2) - 1; ++k) { - potential_(i, j, k) = PotentialGridPoint(BoundaryType::no_boundary, 0.); + //potential_(i, j, k) = PotentialGridPoint(BoundaryType::no_boundary, 0.); + potential_(i,j,k).type = BoundaryType::no_boundary; + potential_(i,j,k).value = 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.); + i * stepsize(0), j * stepsize(1), k * stepsize(2))); +// does not make sense, either resize the potential array or include +// check which boundary it is and use the global boundary condition +// or throw an error, because otherwise there should be an error with the density binning + if (!geometry_->isInside(position)) { + potential_(i,j,k).type = BoundaryType::dirichlet; + potential_(i,j,k).value = 0.; } for (auto& electrode : electrodes_) { if (electrode->isOnElectrode(position)) { - potential_(i, j, k) = PotentialGridPoint( - BoundaryType::dirichlet, electrode->electrodePotential()); + potential_(i,j,k).type = BoundaryType::dirichlet; + potential_(i,j,k).value = electrode->electrodePotential(); } } } } } + //does not yet make sense. Should be the boundaries for the boundary + // of the geometry, right now it's the boundary of the potential array // set global boundary + PotentialGridPoint tempGridPoint{}; + tempGridPoint.type = bottom_boundary_.type; + tempGridPoint.value = bottom_boundary_.boundary_value; 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); + tempGridPoint; //hopefully by value and not by reference... + + tempGridPoint.type = top_boundary_.type; + tempGridPoint.value = top_boundary_.boundary_value; + potential_(blitz::Range::all(), blitz::Range::all(), potential_.ubound(2)) = + tempGridPoint; + + tempGridPoint.type = hull_boundary_.type; + tempGridPoint.value = hull_boundary_.boundary_value; + potential_(potential_.ubound(0), blitz::Range::all(), blitz::Range::all()) = + tempGridPoint; } -blitz::Array PoissonSolver::solverIteration( +void 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 dth_sq = std::pow(stepsize(1), 2); // shorthand const double dz_sq = std::pow(stepsize(2), 2); // shorthand + const double unit_factor = 1.; // todo: change this factor to the correct value for SI - 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) { + for (int i = potential_.lbound(0); i <= potential_.ubound(0); ++i) { + for (int j = potential_.lbound(1); j <= potential_.ubound(1); ++j) { + for (int k = potential_.lbound(2); k <= potential_.ubound(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); + potential_(i, j, k).value = potential_(i, j, k - 1).value + + top_boundary_.boundary_value * stepsize(2); + } else if (potential_(i, j, k).type == BoundaryType::neumann_to_bottom) { + potential_(i, j, k).value = potential_(i, j, k + 1).value + - bottom_boundary_.boundary_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) { + potential_(i, j, k).value = potential_(i - 1, j, k).value + + hull_boundary_.boundary_value * stepsize(0); + } 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) - + 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 + const int jp1 = j + 1 > potential_.ubound(1) + ? potential_.lbound(1) : j + 1; // j+1 with periodic guard - const int jm1 = j - 1 < 0 ? potential_.rbound(1) - : j - 1; // j-1 with periodic guard + const int jm1 = j - 1 < potential_.lbound(1) + ? potential_.ubound(1) + : j - 1; // j-1 with periodic guard + + const double scaling_factor = 2 / dr_sq + 2 / dz_sq + + 2 / (std::pow(i, 2) * dr_sq * dth_sq); - 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 / dr_sq * (1 + 1. / (2. * i))) * potential_(i + 1, j, k).value + + (1 / dr_sq * (1 - 1. / (2. * i))) * potential_(i - 1, j, k).value + (1 / (std::pow(i, 2) * dr_sq * dth_sq)) * - (potential_(i, jp1, k).value + potential_(i, jm1, k)) + + (potential_(i, jp1, k).value + potential_(i, jm1, 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); @@ -119,52 +136,52 @@ blitz::Array PoissonSolver::solverIteration( } } +//as of now does not check for inner cells with dirichlet or neumann +//boundray, prbably needs to be added (or the "electrodes" removed) 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 dth_sq = std::pow(stepsize(1), 2); // shorthand const double dz_sq = std::pow(stepsize(2), 2); // shorthand + const double unit_factor = 1.; // todo: change this factor to the correct value for SI 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; + for (int i = potential_.lbound(0); i < potential_.ubound(0); ++i) { + for (int j = potential_.lbound(1); j <= potential_.ubound(1); ++j) { + for (int k = potential_.lbound(2) + 1; k <= potential_.ubound(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) - + 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 + const int jp1 = j + 1 > potential_.ubound(1) + ? potential_.lbound(1) : j + 1; // j+1 with periodic guard - const int jm1 = j - 1 < 0 ? potential_.rbound(1) - : j - 1; // j-1 with periodic guard + const int jm1 = j - 1 < potential_.lbound(1) + ? potential_.ubound(1) + : j - 1; // j-1 with periodic guard + + const double scaling_factor = 2 / dr_sq + 2 / dz_sq + + 2 / (std::pow(i, 2) * dr_sq * dth_sq); - 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 / dr_sq * (1 + 1. / (2. * i))) * potential_(i + 1, j, k).value + + (1 / dr_sq * (1 - 1. / (2. * i))) * potential_(i - 1, j, k).value + (1 / (std::pow(i, 2) * dr_sq * dth_sq)) * - (potential_(i, jp1, k).value + potential_(i, jm1, k)) + + (potential_(i, jp1, k).value + potential_(i, jm1, 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) - @@ -181,7 +198,7 @@ bool PoissonSolver::converged( normalized_difference = std::sqrt(cumulated_quadradic_difference / (nr * nth * nz)); DEBUG_LEVEL_0("Poisson solver reached a normalized difference of: " + - normalized_difference); + std::to_string(normalized_difference)); return normalized_difference < target_accuracy_; } @@ -201,3 +218,116 @@ void PoissonSolver::solvePotential( ++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) + blitz::Array potentialValues(potential_.lbound(), potential_.extent()); + for(int i = potential_.lbound(0); i <= potential_.ubound(0); ++i) { + for(int j = potential_.lbound(1); j <= potential_.ubound(1); ++j) { + for(int k = potential_.lbound(2); k <= potential_.ubound(2); ++k) { + potentialValues(i,j,k) = potential_(i,j,k).value; + } + } + } + return potentialValues; +} + +blitz::Array PoissonSolver::interpolateDensityToPotentialGrid(const blitz::Array density) { + if((potential_.extent(0) - 1 != density.extent(0)) || (potential_.extent(1) != density.extent(1)) + || (potential_.extent(2) - 1 != density.extent(2))) { + ERROR("Extent of the cylinder binning and the provided chargedensity do not match."); + } + //probably include a check for the loweubound of the provided chargedensity + //coincides with the one of the potential (and by that extent the cylinder binning) + + // adapted from the external poisson solver (SDS: \\lsdf02.urz.uni-heidelberg.de\SD19H002\karl\src\tools\analysis_jkellerer) + blitz::Array interpolatedDensity(potential_.lbound(), potential_.extent()); + interpolatedDensity = 0.; + + //center values + for(int i = potential_.lbound(0)+1; i <= potential_.ubound(0)-1; ++i) { + for(int j = potential_.lbound(1); j <= potential_.ubound(1); ++j) { + int jm = (j - 1) < potential_.lbound(1) ? potential_.ubound(1) : j-1; + for(int k = potential_.lbound(2)+1; k <= potential_.ubound(2)-1; ++k) { + interpolatedDensity(i, j, k) = ( + density(i-1, j , k-1) + density(i, j , k-1) + + density(i-1, jm, k-1) + density(i, jm, k-1) + + density(i-1, j , k ) + density(i, j , k ) + + density(i-1, jm, k ) + density(i, jm, k ) )/8.; + } + } + } + + int fixedIndex; + //values for r=0; charge density has to be single valued at r=0 + //therefore all elements in the j index have to be the same + fixedIndex = potential_.lbound(0); + for(int j = potential_.lbound(1); j <= potential_.ubound(1); ++j) { + for(int k = potential_.lbound(2)+1; k <= potential_.ubound(2)-1; ++k) { + //might not work that way, would need a dummy variable otherwise + interpolatedDensity(potential_.lbound(0), blitz::Range::all(), k) += + (density(fixedIndex, j, k-1) + density(fixedIndex, j, k))/2.; + } + } + + //might not work that way (if not needs a loop) + interpolatedDensity(fixedIndex, blitz::Range::all(), blitz::Range::all()) /= potential_.extent(1); + + //values for k = k_max (i.e. the open boundary) + fixedIndex = potential_.ubound(2); + for(int i = potential_.lbound(0)+1; i <= potential_.ubound(0)-1; ++i) { + for(int j = potential_.lbound(1); j <= potential_.ubound(1); ++j) { + int jm = (j - 1) < potential_.lbound(1) ? potential_.ubound(1) : j-1; + interpolatedDensity(i, j, fixedIndex) = ( + density(i-1, j , fixedIndex-1) + + density(i , j , fixedIndex-1) + + density(i-1, jm, fixedIndex-1) + + density(i , jm, fixedIndex-1) )/4.; + } + } + + //value where the axis and the open boundary meet + for(int j = potential_.lbound(1); j <= potential_.ubound(1); ++j) { + interpolatedDensity(potential_.lbound(0), blitz::Range::all(), fixedIndex) += ( + density(potential_.lbound(0), j , fixedIndex-1))/(potential_.extent(1)); + } + + //for r=r_max and k=k_min do not use an interpolation, since there's assumed + //to be an absorbing, metallic wall, which leads to those values being 0 always + //however(!) right now i would be possible that the global boundary at those + //positions is not a metallic(i.e. dirichlet) boundary --> would need to include + //a check for the type of boundary and adjust the interpolation accordingly + return interpolatedDensity; +} + +// std::shared_ptr staticElectricField() { + +// } + +Electrode::Electrode(double potential): potential_(potential) {}; + +double Electrode::electrodePotential() { + return potential_; +} + +// that whole structure has to be re-evaluated, because as of +// right now the Electrode object does not have any information +// about where it is located +RearWall::RearWall(double potential): Electrode(potential) {}; + +bool RearWall::isOnElectrode(const Coordinate& position) { + return false; +} + +//PotentialGridPoint::PotentialGridPoint(BoundaryType _type, double _value): type(_type), value(_value) {}; + +//TODO: write (or find): +// - interpolateDensityToPotentialGrid() <- need to understand how the potential grid +// is intended to differ from the density one +// - Electrode class +// - Rearwall class +// - constructor for Potentialgridpoint and probs for GlobalBoundary +// look for a function that calculates the E-Field from the potential (if it doesn't exist, one has to be written) +// Note: ubound() might not be an actual function in blitz, the correct one might be ubound() +// change the update formula to SI units