#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 #include #include PoissonSolver::PoissonSolver(GlobalBoundary bottom_boundary, GlobalBoundary top_boundary, GlobalBoundary hull_boundary, std::shared_ptr geometry, CylinderBinning& density_binning, std::vector> 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( 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)))), electrodes_(electrodes), target_accuracy_(target_accuracy), num_cycles_(num_cycles), do_solver_(do_solver) { blitz::TinyVector 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( i * stepsize(0), j * stepsize(1), k * stepsize(2))); 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(); } } } } } blitz::Array 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))); zero_chargedensity = 0.; solvePotential(zero_chargedensity, true); } void PoissonSolver::solvePotential( const blitz::Array charge_density, const bool solve) { if (!(do_solver_) || !(solve)) { return; } DEBUG_LEVEL_3("Interpolating chargedensity."); blitz::Array interpolated_density = interpolateDensityToPotentialGrid(charge_density); int it = 1; do { solverIteration(interpolated_density); if (it % 1000 == 0) { DEBUG_LEVEL_3("Poisson solver, iteration: " + std::to_string(it)); if (converged(interpolated_density)) { break; }; } ++it; } while (it < num_cycles_); } void PoissonSolver::solverIteration( const blitz::Array& interpolated_density) { blitz::TinyVector 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 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_.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 / (std::pow(i, 2) * dr_sq * dth_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 / (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); } } } } } //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& interpolated_density) { blitz::TinyVector 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 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) { for (int k = potential_.lbound(2) + 1; k <= potential_.ubound(2) - 1; ++k) { if(potential_(i,j,k).type != BoundaryType::no_boundary) { continue; } 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 maximum_difference) { maximum_difference = fabs(difference); } } 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 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; cumulated_quadradic_difference += std::pow(difference, 2); 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; normalized_difference = std::sqrt(cumulated_quadradic_difference / (nr * nth * nz)); DEBUG_LEVEL_0("Poisson solver reached a normalized difference of: " + std::to_string(normalized_difference)); double normalized_maximum = maximum_difference*SI::eps0/(fabs(blitz::max(interpolated_density)) + 1e-12); std::ostringstream message; message << "Maximum Difference normalized by order of magnitude: " << normalized_difference; DEBUG_LEVEL_0(message.str()); return normalized_difference < target_accuracy_; } blitz::Array PoissonSolver::potential() { // there might be a better way to do this... (if one can do array.memberValue without // explicitely looping) blitz::Array 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) { potentialValues(i,j,k) = potential_(i,j,k).value; } } } return potentialValues; } blitz::Array PoissonSolver::interpolateDensityToPotentialGrid( const blitz::Array density) { // adapted from the external poisson solver (SDS: \\lsdf02.urz.uni-heidelberg.de\SD19H002\karl\src\tools\analysis_jkellerer) blitz::Array 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.; } } } 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); DEBUG_LEVEL_3("Max Value in interpolated density array: " + std::to_string(blitz::max(interpolatedDensity))); //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 PoissonSolver::staticElectricField() { blitz::TinyVector stepsize = density_binning_.stepsize(); blitz::TinyVector Eextent = potential_.extent() - blitz::TinyVector(1,0,1); blitz::Array Er(potential_.lbound(), Eextent); blitz::Array Ephi(potential_.lbound(), Eextent); blitz::Array 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)); 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)); } } } } return std::shared_ptr( new VectorFieldFromArray(Er, Ephi, Ez, Eextent(0)*stepsize(0), Eextent(2)*stepsize(2))); } const CylinderBinning* PoissonSolver::densityBinning() { return &density_binning_; } 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; }