fixed errors, included missing memberfunctions

This commit is contained in:
2024-01-19 15:07:02 +01:00
parent 00eb561e92
commit 5f7f44b337
2 changed files with 228 additions and 86 deletions
+197 -67
View File
@@ -2,8 +2,11 @@
#include "simulation/errors.h"
#include "simulation/geometry.h"
#include "simulation/position_binning.h"
#include "simulation/vector_field.h"
#include <memory>
#include <string>
PoissonSolver(GlobalBoundary bottom_boundary,
PoissonSolver::PoissonSolver(GlobalBoundary bottom_boundary,
GlobalBoundary top_boundary,
GlobalBoundary hull_boundary,
std::shared_ptr<Geometry> geometry,
@@ -18,98 +21,112 @@ PoissonSolver(GlobalBoundary bottom_boundary,
density_binning_(density_binning),
potential_(blitz::Array<PotentialGridPoint, 3>(
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<double, 3> 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<double, 3>(
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<double, 3> PoissonSolver::solverIteration(
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(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<double, 3> 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<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(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<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 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<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) {
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<VectorField> 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