Removed possibility of Nphi > 1

This commit is contained in:
2025-10-30 16:19:18 +01:00
parent 5c39fb1745
commit 5cb560e727
21 changed files with 403 additions and 524 deletions
+128 -203
View File
@@ -22,73 +22,66 @@ PoissonSolver::PoissonSolver(GlobalBoundary bottom_boundary,
hull_boundary_(hull_boundary),
geometry_(geometry),
density_binning_(density_binning),
potential_(blitz::Array<PotentialGridPoint, 3>(
potential_(blitz::Array<PotentialGridPoint, 2>(
blitz::Range(density_binning.binIndexMin()(0),
density_binning.binIndexMax()(0)),
blitz::Range(density_binning.binIndexMin()(1),
density_binning.binIndexMax()(1)), // the azimuthal direction is periodic
blitz::Range(density_binning.binIndexMin()(2) + 1,
density_binning.binIndexMax()(2)))),
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, 3> stepsize = density_binning_.stepsize();
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) {
for (int k = potential_.lbound(2); k <= potential_.ubound(2);
++k) {
CylinderCoordinate position(blitz::TinyVector<double, 3>(
i * stepsize(0), j * stepsize(1), k * stepsize(2)));
CylinderCoordinate position(blitz::TinyVector<double, 3>(
i * stepsize(0), 0, j * stepsize(1)));
switch(geometry_->isWhere(position)) {
case GeometryPositioning::bottom:
potential_(i,j,k) = {bottom_boundary_.type, bottom_boundary_.boundary_value};
break;
case GeometryPositioning::top:
potential_(i,j,k) = {top_boundary_.type, top_boundary_.boundary_value};
break;
case GeometryPositioning::hull:
potential_(i,j,k) = {hull_boundary_.type, hull_boundary_.boundary_value};
break;
case GeometryPositioning::inside:
potential_(i,j,k) = {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,k).type = BoundaryType::dirichlet;
potential_(i,j,k).value = electrode->electrodePotential();
}
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, 3> zero_chargedensity(
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)),
blitz::Range(density_binning.binIndexMin()(2),
density_binning.binIndexMax()(2)));
density_binning.binIndexMax()(1)));
zero_chargedensity = 0.;
solvePotential(zero_chargedensity);
}
void PoissonSolver::solvePotential(
const blitz::Array<double, 3> charge_density) {
const blitz::Array<double, 2> charge_density) {
if (!(do_solver_)) {
return;
}
#ifdef debug_level3
DEBUG_LEVEL_3("Interpolating chargedensity.");
#endif
blitz::Array<double, 3> interpolated_density =
blitz::Array<double, 2> interpolated_density =
interpolateDensityToPotentialGrid(charge_density);
int it = 1;
do {
@@ -106,61 +99,45 @@ void PoissonSolver::solvePotential(
}
void PoissonSolver::solverIteration(
const blitz::Array<double, 3>& interpolated_density) {
blitz::TinyVector<double, 3> stepsize = density_binning_.stepsize();
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 dth_sq = stepsize(1)*stepsize(1); // shorthand
const double dz_sq = stepsize(2)*stepsize(2); // 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) {
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) {
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 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) *
((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);
//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 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 / (i*i * dr_sq * dth_sq);
} else {
const double scaling_factor = 2 / dr_sq + 2 / dz_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 / (i*i * 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).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);
}
}
}
@@ -169,11 +146,10 @@ void PoissonSolver::solverIteration(
//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, 3>& interpolated_density) {
blitz::TinyVector<double, 3> stepsize = density_binning_.stepsize();
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 dth_sq = stepsize(1)*stepsize(1); // shorthand
const double dz_sq = stepsize(2)*stepsize(2); // shorthand
const double dz_sq = stepsize(1)*stepsize(1); // shorthand
double cumulated_quadradic_difference = 0.;
double normalized_difference = 0.;
@@ -181,64 +157,44 @@ bool PoissonSolver::converged(
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(potential_(i,j,k).type != BoundaryType::no_boundary) {
continue;
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);
}
if (i == 0) {
// special calculation for r=0
//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 += 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;
} 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
cumulated_quadradic_difference += difference*difference;
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)/(i*i*dr_sq*dth_sq) +
(potential_(i,j,k+1).value - 2*potential_(i,j,k).value +
potential_(i,j,k-1).value)/dz_sq;
cumulated_quadradic_difference += difference*difference;
if(fabs(difference) > maximum_difference) {
maximum_difference = fabs(difference);
}
if(fabs(difference) > maximum_difference) {
maximum_difference = fabs(difference);
}
}
}
}
const int nr = potential_.extent(0) - 1;
const int nth = potential_.extent(1);
const int nz = potential_.extent(2) - 2;
const int nz = potential_.extent(1) - 2;
normalized_difference =
std::sqrt(cumulated_quadradic_difference / (nr * nth * nz));
std::sqrt(cumulated_quadradic_difference / (nr * nz));
#ifdef debug
DEBUG_LEVEL_0("Poisson solver reached a normalized difference of: " +
std::to_string(normalized_difference));
@@ -254,37 +210,33 @@ bool PoissonSolver::converged(
return normalized_difference < target_accuracy_;
}
blitz::Array<double, 3> PoissonSolver::potential() {
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, 3> potentialValues(potential_.lbound(), potential_.extent());
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) {
for(int k = potential_.lbound(2); k <= potential_.ubound(2); ++k) {
potentialValues(i,j,k) = potential_(i,j,k).value;
}
potentialValues(i,j) = potential_(i,j).value;
}
}
return potentialValues;
}
blitz::Array<double, 3> PoissonSolver::interpolateDensityToPotentialGrid(
const blitz::Array<double, 3> density) {
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, 3> interpolatedDensity(potential_.lbound(), potential_.extent());
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) {
for(int k = potential_.lbound(2); k <= potential_.ubound(2); ++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.;
}
interpolatedDensity(i, j) = (
density(i-1, j-1) + density(i, j-1) +
density(i-1, j ) + density(i, j ))/4.;
}
}
@@ -292,17 +244,12 @@ blitz::Array<double, 3> PoissonSolver::interpolateDensityToPotentialGrid(
//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.;
}
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.;
}
//might not work that way (if not needs a loop)
interpolatedDensity(fixedIndex, blitz::Range::all(), blitz::Range::all()) /=
potential_.extent(1);
#ifdef debug_level3
DEBUG_LEVEL_3("Max Value in interpolated density array: " +
std::to_string(blitz::max(interpolatedDensity)));
@@ -313,60 +260,41 @@ blitz::Array<double, 3> PoissonSolver::interpolateDensityToPotentialGrid(
}
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);
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 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));
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));
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));
}
}
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(2)*stepsize(2)));
Eextent(0)*stepsize(0), Eextent(1)*stepsize(1)));
}
const CylinderBinning* PoissonSolver::densityBinning() {
return &density_binning_;
}
void PoissonSolver::setPotential(blitz::Array<double, 3>& potential) {
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())) {
@@ -381,13 +309,10 @@ void PoissonSolver::setPotential(blitz::Array<double, 3>& potential) {
// 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);
const int k_offset = potential.lbound(2) - potential_.lbound(2);
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) {
potential_(i,j,k).value = potential(i + i_offset,j + j_offset,k + k_offset);
}
potential_(i,j).value = potential(i + i_offset,j + j_offset);
}
}
}