Adjusted convergence criterion, included

json file with examplary use of the poisson solver
This commit is contained in:
2024-01-26 15:45:02 +01:00
parent ffc65f8ef9
commit 3a802d8f8d
3 changed files with 686 additions and 50 deletions
+58 -48
View File
@@ -6,6 +6,7 @@
#include "simulation/vector_field.h"
#include <memory>
#include <string>
#include <sstream>
PoissonSolver::PoissonSolver(GlobalBoundary bottom_boundary,
GlobalBoundary top_boundary,
@@ -36,7 +37,6 @@ PoissonSolver::PoissonSolver(GlobalBoundary bottom_boundary,
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.;
@@ -46,7 +46,8 @@ PoissonSolver::PoissonSolver(GlobalBoundary bottom_boundary,
// 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;
//DEBUG_LEVEL_0("Binning is larger than the Geometry.");
potential_(i,j,k).type = BoundaryType::no_boundary;
potential_(i,j,k).value = 0.;
}
for (auto& electrode : electrodes_) {
@@ -89,6 +90,7 @@ void PoissonSolver::solverIteration(
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) {
//rewrite with a switch-case
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) {
@@ -102,10 +104,13 @@ void PoissonSolver::solverIteration(
+ hull_boundary_.boundary_value * stepsize(0);
} else if (i == 0) {
// special calculation for r=0
const int jmirror = (j<potential_.extent(1)/2) ? j+potential_.extent(1)/2
: j-potential_.extent(1)/2;
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 +
((2 / dr_sq) * (potential_(i + 1, j, k).value +
potential_(i + 1, jmirror, k).value) +
(1 / dz_sq) * (potential_(i, j, k + 1).value +
potential_(i, j, k - 1).value) +
interpolated_density(i, j, k)/SI::eps0);
@@ -137,17 +142,17 @@ void 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)
//boundary, probably 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.;
double maximum_difference = 0.;
for (int i = potential_.lbound(0); i < potential_.ubound(0); ++i) {
for (int j = potential_.lbound(1); j <= potential_.ubound(1); ++j) {
@@ -155,16 +160,24 @@ bool PoissonSolver::converged(
++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;
//Note: the value \phi_{i,j+Nj/2,k} as the replacement of
// \phi_{i-1,j,k} is only correct if Nj is even otherwise the mirrored point
// lies inbetween two known values; additionally not sure if this calculation
// is correct at all (would definitely be fine for cylindrical symmetric calc.)
const int jmirror = (j<potential_.extent(1)/2) ? j+potential_.extent(1)/2
: j-potential_.extent(1)/2;
const double difference = interpolated_density(i,j,k)/SI::eps0 +
2*(potential_(i+1,j,k).value - 2*potential_(i,j,k).value +
potential_(i+1,jmirror,k).value)/dr_sq +
(potential_(i,j,k+1).value - 2*potential_(i,j,k).value +
potential_(i,j,k-1).value)/dz_sq;
cumulated_quadradic_difference += std::pow(difference, 2);
if(fabs(difference) > maximum_difference) {
maximum_difference = fabs(difference);
}
} else {
const int jp1 = j + 1 > potential_.ubound(1)
? potential_.lbound(1)
@@ -173,20 +186,21 @@ bool PoissonSolver::converged(
? 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 = interpolated_density(i,j,k)/SI::eps0 +
(potential_(i+1,j,k).value - 2*potential_(i,j,k).value +
potential_(i-1,j,k).value)/dr_sq +
(potential_(i+1,j,k).value - potential_(i-1,j,k).value)/
(2*i*dr_sq) +
(potential_(i,jp1,k).value - 2*potential_(i,j,k).value +
potential_(i,jm1,k).value)/(std::pow(i,2)*dr_sq*dth_sq) +
(potential_(i,j,k+1).value - 2*potential_(i,j,k).value +
potential_(i,j,k-1).value)/dz_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);
if(fabs(difference) > maximum_difference) {
maximum_difference = fabs(difference);
}
}
}
}
@@ -199,18 +213,23 @@ bool PoissonSolver::converged(
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_;
std::ostringstream message;
message << "Maximum Differenece normalized by order of magnitude: "
<< maximum_difference*SI::eps0/(fabs(blitz::max(interpolated_density)) + 1e-12);
DEBUG_LEVEL_0(message.str());
return maximum_difference*SI::eps0/(fabs(blitz::max(interpolated_density)) + 1e-12) < target_accuracy_;
}
void PoissonSolver::solvePotential(
const blitz::Array<double, 3> charge_density) {
DEBUG_LEVEL_3("Interpolating chargedensity.");
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) {
DEBUG_LEVEL_3("Poisson solver, iteration: " + std::to_string(it));
if (converged(interpolated_density)) {
break;
};
@@ -223,6 +242,7 @@ 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());
DEBUG_LEVEL_3("Extracting potential values");
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) {
@@ -287,6 +307,8 @@ blitz::Array<double, 3> PoissonSolver::interpolateDensityToPotentialGrid(const b
(density(potential_.lbound(0), j , fixedIndex-1))/(potential_.extent(1));
}
DEBUG_LEVEL_3("Max Value in interpolated density array: " +
std::to_string(blitz::max(interpolatedDensity)));
//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
@@ -295,11 +317,6 @@ blitz::Array<double, 3> PoissonSolver::interpolateDensityToPotentialGrid(const b
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() -
@@ -330,30 +347,23 @@ std::shared_ptr<VectorField> PoissonSolver::staticElectricField() {
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)
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)
(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));
(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
// whether 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)));