Files
karl/src/poisson_solver/poisson_solver.cpp
T
csendlinger 1f52d850d8 included generator files for the poisson solver
Adjusted binning in poisson_solver.* to fit with
the generator
2024-01-23 17:28:53 +01:00

383 lines
17 KiB
C++
Executable File

#include "poisson_solver/poisson_solver.h"
#include "simulation/errors.h"
#include "simulation/geometry.h"
#include "simulation/position_binning.h"
#include "constants/physical_constants.h"
#include "simulation/vector_field.h"
#include <memory>
#include <string>
PoissonSolver::PoissonSolver(GlobalBoundary bottom_boundary,
GlobalBoundary top_boundary,
GlobalBoundary hull_boundary,
std::shared_ptr<Geometry> geometry,
CylinderBinning& density_binning,
std::vector<std::shared_ptr<Electrode>> 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<PotentialGridPoint, 3>(
blitz::Range(density_binning.binIndexMin()(0),
density_binning.binIndexMax()(0)+1),
blitz::Range(density_binning.binIndexMin()(1),
density_binning.binIndexMax()(1)), // the azimuthal direction is periodic
blitz::Range(density_binning.binIndexMin()(2),
density_binning.binIndexMax()(2)+1))),
electrodes_(electrodes),
target_accuracy_(target_accuracy),
num_cycles_(num_cycles) {
// set inner values
blitz::TinyVector<double, 3> stepsize = density_binning_.stepsize();
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).type = BoundaryType::no_boundary;
potential_(i,j,k).value = 0.;
CylinderCoordinate position(blitz::TinyVector<double, 3>(
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).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)) =
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;
}
void PoissonSolver::solverIteration(
const blitz::Array<double, 3>& interpolated_density) {
blitz::TinyVector<double, 3> stepsize = density_binning_.stepsize();
const double dr_sq = std::pow(stepsize(0), 2); // shorthand
const double dth_sq = std::pow(stepsize(1), 2); // shorthand
const double dz_sq = std::pow(stepsize(2), 2); // shorthand
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).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).value
+ hull_boundary_.boundary_value * stepsize(0);
} else if (i == 0) {
// special calculation for r=0
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) +
interpolated_density(i, j, k)/SI::eps0);
} else {
const int jp1 = j + 1 > potential_.ubound(1)
? potential_.lbound(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);
potential_(i, j, k).value =
(1 / scaling_factor) *
((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).value) +
(1 / dz_sq) * (potential_(i, j, k + 1).value +
potential_(i, j, k - 1).value) +
interpolated_density(i, j, k)/SI::eps0);
}
}
}
}
}
//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<double, 3>& interpolated_density) {
blitz::TinyVector<double, 3> stepsize = density_binning_.stepsize();
const double dr_sq = std::pow(stepsize(0), 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_.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 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) +
interpolated_density(i, j, k)/SI::eps0) -
potential_(i, j, k).value;
cumulated_quadradic_difference += std::pow(difference, 2);
} else {
const int jp1 = j + 1 > potential_.ubound(1)
? potential_.lbound(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 difference =
(1 / scaling_factor) *
((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).value) +
(1 / dz_sq) * (potential_(i, j, k + 1).value +
potential_(i, j, k - 1).value) +
interpolated_density(i, j, k)/SI::eps0) -
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: " +
std::to_string(normalized_difference));
return normalized_difference < target_accuracy_;
}
void PoissonSolver::solvePotential(
const blitz::Array<double, 3> charge_density) {
blitz::Array<double, 3> 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_);
}
blitz::Array<double, 3> PoissonSolver::potential() {
// there might be a better way to do this... (if one can do array.memberValue without
// explicitely looping)
blitz::Array<double, 3> 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<double, 3> PoissonSolver::interpolateDensityToPotentialGrid(const blitz::Array<double, 3> 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 lowerbound 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<double, 3> 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) {
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, j , k ) + density(i, j , k ))/4.;
}
}
}
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) {
interpolatedDensity(i, j, fixedIndex) = (
density(i-1, j , fixedIndex-1) +
density(i , j , 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. it 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;
}
//TODO: -interpolation to the correct position in the gridpoint
// (probably already during the calculation of the values
// otherwise need to adjust the size of the E-arrays
// since e.g. Ez exists for all r Values in potential_
std::shared_ptr<VectorField> PoissonSolver::staticElectricField() {
blitz::TinyVector<double, 3> stepsize = density_binning_.stepsize();
blitz::TinyVector<int,3> Eextent = potential_.extent() -
blitz::TinyVector<int,3>(1,0,1);
blitz::Array<double, 3> Er(potential_.lbound(), Eextent);
blitz::Array<double, 3> Ephi(potential_.lbound(), Eextent);
blitz::Array<double, 3> Ez(potential_.lbound(), Eextent);
Ephi = 0.;
//do calculation of E from the potential and interpolation to
//cell center at the same time
for(int j = potential_.lbound(1); j <= potential_.ubound(1); ++j) {
int jp = (j+1 > potential_.ubound(1)) ? potential_.lbound(1) : j+1;
int jm = (j-1 < potential_.lbound(1)) ? potential_.ubound(1) : j-1;
for(int i = potential_.lbound(0); i < potential_.ubound(0); ++i) {
for(int k = potential_.lbound(2); k < potential_.ubound(2); ++k) {
Er(i,j,k) = (potential_(i+1,j,k ).value -
potential_(i ,j,k ).value +
potential_(i+1,j,k+1).value -
potential_(i ,j,k+1).value)
/(2*stepsize(0));
Ez(i,j,k) = (potential_(i ,j,k+1).value -
potential_(i ,j,k ).value +
potential_(i+1,j,k+1).value -
potential_(i+1,j,k ).value)
/(2*stepsize(2));
if(i==potential_.lbound(0)) {
Ephi(i,j,k) = (
potential_(i+1,jp,k ).value -
potential_(i+1,jm,k ).value +
potential_(i+1,jp,k+1).value -
potential_(i+1,jm,k+1).value)
/((i+1)*stepsize(0)*stepsize(2))/4.;
} else {
Ephi(i,j,k) = (
(potential_(i ,jp,k ).value -
potential_(i ,jm,k ).value +
potential_(i ,jp,k+1).value -
potential_(i ,jm,k+1).value)
/(i*stepsize(0)) +
(potential_(i+1,jp,k ).value -
potential_(i+1,jm,k ).value +
potential_(i+1,jp,k+1).value -
potential_(i+1,jm,k+1).value)
/((i+1)*stepsize(0))
)/(4.*stepsize(1));
}
}
}
}
// if the VectorField object uses the E-field
//at the correct position is not certain
return std::shared_ptr<VectorField>(
new VectorFieldFromArray(Er, Ephi, Ez,
Eextent(0)*stepsize(0), Eextent(2)*stepsize(2)));
}
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:
// look for a function that calculates the E-Field from the potential (if it doesn't exist, one has to be written)
// get the "main" code to also output the potential if specified(for testing purposes)
// same for the electric field
// make sure .lbound(0) always makes sense as a replacement of r=0