334 lines
13 KiB
C++
Executable File
334 lines
13 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>
|
|
#include <sstream>
|
|
|
|
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,
|
|
bool do_solver)
|
|
: bottom_boundary_(bottom_boundary),
|
|
top_boundary_(top_boundary),
|
|
hull_boundary_(hull_boundary),
|
|
geometry_(geometry),
|
|
density_binning_(density_binning),
|
|
potential_(blitz::Array<PotentialGridPoint, 2>(
|
|
blitz::Range(density_binning.binIndexMin()(0),
|
|
density_binning.binIndexMax()(0)),
|
|
blitz::Range(density_binning.binIndexMin()(1) + 1,
|
|
density_binning.binIndexMax()(1)))),
|
|
electrodes_(electrodes),
|
|
target_accuracy_(target_accuracy),
|
|
num_cycles_(num_cycles),
|
|
do_solver_(do_solver) {
|
|
|
|
blitz::TinyVector<double, 2> stepsize = density_binning_.stepsize();
|
|
for (int i = potential_.lbound(0); i <= potential_.ubound(0); ++i) {
|
|
for (int j = potential_.lbound(1); j <= potential_.ubound(1); ++j) {
|
|
CylinderCoordinate position(blitz::TinyVector<double, 3>(
|
|
i * stepsize(0), 0, j * stepsize(1)));
|
|
|
|
switch(geometry_->isWhere(position)) {
|
|
case GeometryPositioning::bottom:
|
|
potential_(i,j) = {bottom_boundary_.type, bottom_boundary_.boundary_value};
|
|
break;
|
|
case GeometryPositioning::top:
|
|
potential_(i,j) = {top_boundary_.type, top_boundary_.boundary_value};
|
|
break;
|
|
case GeometryPositioning::hull:
|
|
potential_(i,j) = {hull_boundary_.type, hull_boundary_.boundary_value};
|
|
break;
|
|
case GeometryPositioning::inside:
|
|
potential_(i,j) = {BoundaryType::no_boundary, 0.};
|
|
break;
|
|
default:
|
|
// include position in output
|
|
ERROR("Something went terribly wrong. Position is nowhere.");
|
|
break;
|
|
}
|
|
for (auto& electrode : electrodes_) {
|
|
if (electrode->isOnElectrode(position)) {
|
|
potential_(i,j).type = BoundaryType::dirichlet;
|
|
potential_(i,j).value = electrode->electrodePotential();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
blitz::Array<double, 2> zero_chargedensity(
|
|
blitz::Range(density_binning.binIndexMin()(0),
|
|
density_binning.binIndexMax()(0)),
|
|
blitz::Range(density_binning.binIndexMin()(1),
|
|
density_binning.binIndexMax()(1)));
|
|
zero_chargedensity = 0.;
|
|
solvePotential(zero_chargedensity);
|
|
}
|
|
|
|
void PoissonSolver::solvePotential(
|
|
const blitz::Array<double, 2> charge_density) {
|
|
if (!(do_solver_)) {
|
|
return;
|
|
}
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3("Interpolating chargedensity.");
|
|
#endif
|
|
blitz::Array<double, 2> interpolated_density =
|
|
interpolateDensityToPotentialGrid(charge_density);
|
|
int it = 1;
|
|
do {
|
|
solverIteration(interpolated_density);
|
|
if (it % 1000 == 0) {
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3("Poisson solver, iteration: " + std::to_string(it));
|
|
#endif
|
|
if (converged(interpolated_density)) {
|
|
break;
|
|
};
|
|
}
|
|
++it;
|
|
} while (it < num_cycles_);
|
|
}
|
|
|
|
void PoissonSolver::solverIteration(
|
|
const blitz::Array<double, 2>& interpolated_density) {
|
|
blitz::TinyVector<double, 2> stepsize = density_binning_.stepsize();
|
|
const double dr_sq = stepsize(0)*stepsize(0); // shorthand
|
|
const double dz_sq = stepsize(1)*stepsize(1); // shorthand
|
|
|
|
for (int i = potential_.lbound(0); i <= potential_.ubound(0); ++i) {
|
|
for (int j = potential_.lbound(1); j <= potential_.ubound(1); ++j) {
|
|
//rewrite with a switch-case
|
|
if (potential_(i, j).type == BoundaryType::dirichlet) {
|
|
potential_(i, j).value = potential_(i, j).value;
|
|
} else if (potential_(i, j).type == BoundaryType::neumann_to_top) {
|
|
potential_(i, j).value = potential_(i, j-1).value
|
|
+ top_boundary_.boundary_value * stepsize(1);
|
|
} else if (potential_(i, j).type == BoundaryType::neumann_to_bottom) {
|
|
potential_(i, j).value = potential_(i, j + 1).value
|
|
- bottom_boundary_.boundary_value * stepsize(1);
|
|
} else if (potential_(i, j).type == BoundaryType::neumann_to_hull) {
|
|
potential_(i, j).value = potential_(i - 1, j).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).value =
|
|
(1 / scaling_factor) *
|
|
((4 / dr_sq) * (potential_(i + 1, j).value) +
|
|
(1 / dz_sq) * (potential_(i, j + 1).value +
|
|
potential_(i, j - 1).value) +
|
|
interpolated_density(i, j)/SI::eps0);
|
|
|
|
} else {
|
|
const double scaling_factor = 2 / dr_sq + 2 / dz_sq;
|
|
|
|
potential_(i, j).value =
|
|
(1 / scaling_factor) *
|
|
((1 / dr_sq * (1 + 1. / (2. * i))) * potential_(i + 1, j).value +
|
|
(1 / dr_sq * (1 - 1. / (2. * i))) * potential_(i - 1, j).value +
|
|
(1 / dz_sq) * (potential_(i, j + 1).value +
|
|
potential_(i, j - 1).value) +
|
|
interpolated_density(i, j)/SI::eps0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//as of now does not check for inner cells with dirichlet or neumann
|
|
//boundary, probably needs to be added (or the "electrodes" removed)
|
|
bool PoissonSolver::converged(
|
|
const blitz::Array<double, 2>& interpolated_density) {
|
|
blitz::TinyVector<double, 2> stepsize = density_binning_.stepsize();
|
|
const double dr_sq = stepsize(0)*stepsize(0); // shorthand
|
|
const double dz_sq = stepsize(1)*stepsize(1); // shorthand
|
|
|
|
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) {
|
|
if(potential_(i,j).type != BoundaryType::no_boundary) {
|
|
continue;
|
|
}
|
|
if (i == 0) {
|
|
// special calculation for r=0
|
|
const double difference = interpolated_density(i,j)/SI::eps0 +
|
|
4*(potential_(i+1,j).value - potential_(i,j).value)/dr_sq +
|
|
(potential_(i,j+1).value - 2*potential_(i,j).value +
|
|
potential_(i,j-1).value)/dz_sq;
|
|
|
|
cumulated_quadradic_difference += difference*difference;
|
|
|
|
if(fabs(difference) > maximum_difference) {
|
|
maximum_difference = fabs(difference);
|
|
}
|
|
|
|
} else {
|
|
const double difference = interpolated_density(i,j)/SI::eps0 +
|
|
(potential_(i+1,j).value - 2*potential_(i,j).value +
|
|
potential_(i-1,j).value)/dr_sq +
|
|
(potential_(i+1,j).value - potential_(i-1,j).value)/
|
|
(2*i*dr_sq) +
|
|
(potential_(i,j+1).value - 2*potential_(i,j).value +
|
|
potential_(i,j-1).value)/dz_sq;
|
|
|
|
cumulated_quadradic_difference += difference*difference;
|
|
|
|
if(fabs(difference) > maximum_difference) {
|
|
maximum_difference = fabs(difference);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
const int nr = potential_.extent(0) - 1;
|
|
const int nz = potential_.extent(1) - 2;
|
|
|
|
normalized_difference =
|
|
std::sqrt(cumulated_quadradic_difference / (nr * nz));
|
|
#ifdef debug
|
|
DEBUG_LEVEL_0("Poisson solver reached a normalized difference of: " +
|
|
std::to_string(normalized_difference));
|
|
#endif
|
|
double normalized_maximum =
|
|
maximum_difference*SI::eps0/(fabs(blitz::max(interpolated_density)) + 1e-12);
|
|
#ifdef debug_level1
|
|
std::ostringstream message;
|
|
message << "Maximum Difference normalized by order of magnitude: "
|
|
<< normalized_difference;
|
|
DEBUG_LEVEL_1(message.str());
|
|
#endif
|
|
return normalized_difference < target_accuracy_;
|
|
}
|
|
|
|
blitz::Array<double, 2> PoissonSolver::potential() {
|
|
// there might be a better way to do this... (if one can do array.memberValue without
|
|
// explicitely looping)
|
|
blitz::Array<double, 2> potentialValues(potential_.lbound(), potential_.extent());
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3("Extracting potential values");
|
|
#endif
|
|
for(int i = potential_.lbound(0); i <= potential_.ubound(0); ++i) {
|
|
for(int j = potential_.lbound(1); j <= potential_.ubound(1); ++j) {
|
|
potentialValues(i,j) = potential_(i,j).value;
|
|
}
|
|
}
|
|
return potentialValues;
|
|
}
|
|
|
|
blitz::Array<double, 2> PoissonSolver::interpolateDensityToPotentialGrid(
|
|
const blitz::Array<double, 2> density) {
|
|
// adapted from the external poisson solver (SDS: \\lsdf02.urz.uni-heidelberg.de\SD19H002\karl\src\tools\analysis_jkellerer)
|
|
blitz::Array<double, 2> interpolatedDensity(potential_.lbound(), potential_.extent());
|
|
interpolatedDensity = 0.;
|
|
|
|
//center values
|
|
for(int i = potential_.lbound(0)+1; i <= potential_.ubound(0); ++i) {
|
|
for(int j = potential_.lbound(1); j <= potential_.ubound(1); ++j) {
|
|
interpolatedDensity(i, j) = (
|
|
density(i-1, j-1) + density(i, j-1) +
|
|
density(i-1, j ) + density(i, j ))/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 k = potential_.lbound(1)+1; k <= potential_.ubound(1)-1; ++k) {
|
|
//might not work that way, would need a dummy variable otherwise
|
|
interpolatedDensity(potential_.lbound(0), k) =
|
|
(density(fixedIndex, k-1) + density(fixedIndex, k))/2.;
|
|
}
|
|
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3("Max Value in interpolated density array: " +
|
|
std::to_string(blitz::max(interpolatedDensity)));
|
|
#endif
|
|
//for r=r_max, k=k_min and k=k_max do not use an interpolation,
|
|
//since no boundary cell needs the information about its density value
|
|
return interpolatedDensity;
|
|
}
|
|
|
|
std::shared_ptr<VectorField> PoissonSolver::staticElectricField() {
|
|
blitz::TinyVector<double, 2> stepsize = density_binning_.stepsize();
|
|
blitz::TinyVector<int,2> Eextent = potential_.extent() -
|
|
blitz::TinyVector<int,2>(1,1);
|
|
blitz::Array<double, 2> Er(potential_.lbound(), Eextent);
|
|
blitz::Array<double, 2> Ephi(potential_.lbound(), Eextent);
|
|
blitz::Array<double, 2> Ez(potential_.lbound(), Eextent);
|
|
|
|
Ephi = 0.;
|
|
//do calculation of E from the potential and interpolation to
|
|
//cell center at the same time
|
|
|
|
for(int i = potential_.lbound(0); i < potential_.ubound(0); ++i) {
|
|
for(int k = potential_.lbound(1); k < potential_.ubound(1); ++k) {
|
|
Er(i,k) = -(potential_(i+1,k ).value -
|
|
potential_(i ,k ).value +
|
|
potential_(i+1,k+1).value -
|
|
potential_(i ,k+1).value ) /(2*stepsize(0));
|
|
|
|
Ez(i,k) = -(potential_(i ,k+1).value -
|
|
potential_(i ,k ).value +
|
|
potential_(i+1,k+1).value -
|
|
potential_(i+1,k ).value )/(2*stepsize(1));
|
|
}
|
|
}
|
|
|
|
return std::shared_ptr<VectorField>(
|
|
new VectorFieldFromArray(Er, Ephi, Ez,
|
|
Eextent(0)*stepsize(0), Eextent(1)*stepsize(1)));
|
|
}
|
|
|
|
const CylinderBinning* PoissonSolver::densityBinning() {
|
|
return &density_binning_;
|
|
}
|
|
|
|
void PoissonSolver::setPotential(blitz::Array<double, 2>& potential) {
|
|
// check if the loaded potential has the same size as the potential
|
|
// specified by the json file
|
|
if(!blitz::all(potential.length() == potential_.length())) {
|
|
std::stringstream out;
|
|
out << "The size of the potential is " << potential.length();
|
|
out << ". But expected a size of " << potential_.length();
|
|
ERROR(out.str());
|
|
}
|
|
|
|
// necessary since it's not guaranteed (or even likely) that both arrays
|
|
// are indexed the same (i.e. potential_ is most likely starting from -1,
|
|
// while the parameter array is most likely starting from 0)
|
|
const int i_offset = potential.lbound(0) - potential_.lbound(0);
|
|
const int j_offset = potential.lbound(1) - potential_.lbound(1);
|
|
|
|
for(int i = potential_.lbound(0); i <= potential_.ubound(0); ++i) {
|
|
for(int j = potential_.lbound(1); j <= potential_.ubound(1); ++j) {
|
|
potential_(i,j).value = potential(i + i_offset,j + j_offset);
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|