204 lines
8.9 KiB
C++
Executable File
204 lines
8.9 KiB
C++
Executable File
#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> geometry,
|
|
std::shared_ptr<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)),
|
|
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<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;
|
|
++k) {
|
|
potential_(i, j, k) = PotentialGridPoint(BoundaryType::no_boundary, 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.);
|
|
}
|
|
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<double, 3> 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 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<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 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<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_);
|
|
}
|