diff --git a/include/generators/poisson_solver_generator.h b/include/generators/poisson_solver_generator.h new file mode 100644 index 0000000..972c071 --- /dev/null +++ b/include/generators/poisson_solver_generator.h @@ -0,0 +1,16 @@ +#ifndef POISSONSOLVER_GENERATOR_H +#define POISSONSOLVER_GENERATOR_H + +#include + +#include "poisson_solver/poisson_solver.h" +#include "simulation/jansson_additionals.h" +#include "simulation/parameter.h" + +namespace generator { + // no electrode stuff is included + std::shared_ptr buildPoissonSolver( + Parameter& parameter); +}; + +#endif /* POISSONSOLVER_GENERATOR_H */ \ No newline at end of file diff --git a/include/poisson_solver/poisson_solver.h b/include/poisson_solver/poisson_solver.h index d00829e..108acfc 100755 --- a/include/poisson_solver/poisson_solver.h +++ b/include/poisson_solver/poisson_solver.h @@ -52,7 +52,7 @@ class PoissonSolver { GlobalBoundary top_boundary, GlobalBoundary hull_boundary, std::shared_ptr geometry, - std::shared_ptr density_binning, + CylinderBinning& density_binning, std::vector> electrodes, double target_accuracy, int num_cycles); @@ -67,13 +67,13 @@ class PoissonSolver { GlobalBoundary hull_boundary_; std::shared_ptr geometry_; std::vector> electrodes_; - std::shared_ptr density_binning_; + CylinderBinning density_binning_; double target_accuracy_; int num_cycles_; -// the potential is located on the grid corners (such that -// it is defined on r=0) (note that the density_binning is taken -// to be defined in the cell centers) +// density binning defined on the cell centers, potential defined +// such that it exists on the cell edges so that the azimuthal +// position is the same as the one of the density binning blitz::Array potential_; //unit V blitz::Array interpolateDensityToPotentialGrid( diff --git a/src/generators/poisson_solver_generator.cpp b/src/generators/poisson_solver_generator.cpp new file mode 100644 index 0000000..4aeb834 --- /dev/null +++ b/src/generators/poisson_solver_generator.cpp @@ -0,0 +1,58 @@ +#include "generators/poisson_solver_generator.h" +#include "poisson_solver/poisson_solver.h" +#include "generators/geometry_generator.h" +#include "generators/binning_generator.h" +#include +#include "simulation/errors.h" + +namespace generator { + + std::shared_ptr buildPoissonSolver( + Parameter& parameter) { + + DEBUG_LEVEL_2("Generating Poisson solver."); + std::shared_ptr geometry; + + json_t* object = json_object_by_path( + parameter.jsonFile(), {"PoissonSolver"}); + //create geometry + if (json_object_by_path(object, {"BoundaryGeometry"})) { + geometry = buildGeometry(parameter, + json_path_append({"PoissonSolver"}, {"BoundaryGeometry"})); + } else { + ERROR("Cannot find boundary geometry for poisson solver."); + } + //create binning + json_path path_binning = json_path_append({"PoissonSolver"}, + {"PositionBinning"}); + CylinderBinning position_binning = + buildPositionBinningIncludingRedirection(parameter, path_binning); + //create top, bottom and hull boundary + // for now put them as fixed and not read from json file + // needs to be changed! (probably needs an own generator or smth) + GlobalBoundary bottom_boundary; + bottom_boundary.type = BoundaryType::dirichlet; + bottom_boundary.boundary_value = 0.; + + GlobalBoundary top_boundary; + top_boundary.type = BoundaryType::neumann_to_top; + top_boundary.boundary_value = 0.; + + GlobalBoundary hull_boundary; + hull_boundary.type = BoundaryType::dirichlet; + hull_boundary.boundary_value = 0.; + + std::vector> electrodes{}; + + double target_accuracy = parameter.doubleValue( + json_path_append({"PoissonSolver"}, {"TargetAccuracy"})); + + int num_cycles = parameter.intValue(json_path_append({"PoissonSolver"}, + {"NumCycles"})); + + return std::shared_ptr(new PoissonSolver(bottom_boundary, + top_boundary, hull_boundary, geometry, position_binning, electrodes, + target_accuracy, num_cycles)); + } + +}; \ No newline at end of file diff --git a/src/poisson_solver/poisson_solver.cpp b/src/poisson_solver/poisson_solver.cpp index 1595cf3..22bcace 100755 --- a/src/poisson_solver/poisson_solver.cpp +++ b/src/poisson_solver/poisson_solver.cpp @@ -11,7 +11,7 @@ PoissonSolver::PoissonSolver(GlobalBoundary bottom_boundary, GlobalBoundary top_boundary, GlobalBoundary hull_boundary, std::shared_ptr geometry, - std::shared_ptr density_binning, + CylinderBinning& density_binning, std::vector> electrodes, double target_accuracy, int num_cycles) @@ -21,17 +21,17 @@ PoissonSolver::PoissonSolver(GlobalBoundary bottom_boundary, geometry_(geometry), density_binning_(density_binning), potential_(blitz::Array( - blitz::Range(density_binning->binIndexMin()(0), - density_binning->binIndexMax()(0)+1), - blitz::Range(density_binning->binIndexMin()(1), - density_binning->binIndexMax()(1)), // the azimuthal direction is periodic - blitz::Range(density_binning->binIndexMin()(2), - density_binning->binIndexMax()(2)+1))), + blitz::Range(density_binning.binIndexMin()(0), + density_binning.binIndexMax()(0)+1), + blitz::Range(density_binning.binIndexMin()(1), + density_binning.binIndexMax()(1)), // the azimuthal direction is periodic + blitz::Range(density_binning.binIndexMin()(2), + density_binning.binIndexMax()(2)+1))), electrodes_(electrodes), target_accuracy_(target_accuracy), num_cycles_(num_cycles) { // set inner values - blitz::TinyVector stepsize = density_binning_->stepsize(); + blitz::TinyVector stepsize = density_binning_.stepsize(); 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; @@ -81,7 +81,7 @@ PoissonSolver::PoissonSolver(GlobalBoundary bottom_boundary, void PoissonSolver::solverIteration( const blitz::Array& interpolated_density) { - blitz::TinyVector stepsize = density_binning_->stepsize(); + 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 @@ -140,7 +140,7 @@ void PoissonSolver::solverIteration( //boundray, prbably needs to be added (or the "electrodes" removed) bool PoissonSolver::converged( const blitz::Array& interpolated_density) { - blitz::TinyVector stepsize = density_binning_->stepsize(); + 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 @@ -238,7 +238,7 @@ blitz::Array PoissonSolver::interpolateDensityToPotentialGrid(const b || (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 + //probably include a check for the lowerbound 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) @@ -248,13 +248,10 @@ blitz::Array PoissonSolver::interpolateDensityToPotentialGrid(const b //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.; + density(i-1, j , k ) + density(i, j , k ))/4.; } } } @@ -278,24 +275,21 @@ blitz::Array PoissonSolver::interpolateDensityToPotentialGrid(const b 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.; + density(i , j , 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)); + 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 + //however(!) right now. it 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; @@ -307,7 +301,7 @@ blitz::Array PoissonSolver::interpolateDensityToPotentialGrid(const b // otherwise need to adjust the size of the E-arrays // since e.g. Ez exists for all r Values in potential_ std::shared_ptr PoissonSolver::staticElectricField() { - blitz::TinyVector stepsize = density_binning_->stepsize(); + blitz::TinyVector stepsize = density_binning_.stepsize(); blitz::TinyVector Eextent = potential_.extent() - blitz::TinyVector(1,0,1); blitz::Array Er(potential_.lbound(), Eextent); @@ -315,24 +309,54 @@ std::shared_ptr PoissonSolver::staticElectricField() { 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)/stepsize(0); - Ez(i,j,k) = (potential_(i,j,k+1).value - - potential_(i,j,k).value)/stepsize(2); - if(i > 0) { - Ephi(i,j,k) = (potential_(i,jp,k).value - - potential_(i,j,k).value) / (i*stepsize(0) * stepsize(1)); + 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 VectorFieldConstant(CylinderVector(blitz::TinyVector(0,0,0)))); +// if the VectorField object uses the E-field +//at the correct position is not certain + return std::shared_ptr( + new VectorFieldFromArray(Er, Ephi, Ez, + Eextent(0)*stepsize(0), Eextent(2)*stepsize(2))); } Electrode::Electrode(double potential): potential_(potential) {}; @@ -355,4 +379,5 @@ bool RearWall::isOnElectrode(const Coordinate& position) { //TODO: // look for a function that calculates the E-Field from the potential (if it doesn't exist, one has to be written) // get the "main" code to also output the potential if specified(for testing purposes) -// same for the electric field \ No newline at end of file +// same for the electric field +// make sure .lbound(0) always makes sense as a replacement of r=0 \ No newline at end of file