From 3a802d8f8da8e81c1382eca071667d31fdfc6ef8 Mon Sep 17 00:00:00 2001 From: Christian Sendlinger Date: Fri, 26 Jan 2024 15:45:02 +0100 Subject: [PATCH] Adjusted convergence criterion, included json file with examplary use of the poisson solver --- simulation_config/test_poisson_parameter.json | 624 ++++++++++++++++++ src/poisson_solver/poisson_solver.cpp | 106 +-- src/simulation/simulation.cpp | 6 +- 3 files changed, 686 insertions(+), 50 deletions(-) create mode 100755 simulation_config/test_poisson_parameter.json diff --git a/simulation_config/test_poisson_parameter.json b/simulation_config/test_poisson_parameter.json new file mode 100755 index 0000000..dc686dc --- /dev/null +++ b/simulation_config/test_poisson_parameter.json @@ -0,0 +1,624 @@ +{ + "GlobalPositionBinning": { + "CylinderBinning": { + "Radial": { + "NumberOfBins": 15, + "MaxRadius": { + "value": 4.5, + "unit": "cm" + } + }, + "Longitudinal": { + "NumberOfBins": 160, + "MaxLength": { + "value": 1600, + "unit": "cm" + } + }, + "Azimuthal": { + "NumberOfBins": 1 + } + } + }, + "SourceGeometry": { + "Cylinder": { + "Radius": { + "value": 4.5, + "unit": "cm" + }, + "Height": { + "value": 1600, + "unit": "cm" + } + } + }, + "Species": { + "Background": { + "TritiumGas": { + "ParticleType": "T2", + "ConstantDensityField": { + "ScalarField": { + "ScalarFieldFunctionInZ": { + "zFunction": { + "Product": { + "fi": { + "Constant": { + "constant": 1 + } + }, + "fj": { + "Constant": { + "constant": 1 + } + }, + "fk": { + "PiecewiseLinear": { + "xValues": [ + 0, + 1.43, + 3, + 8, + 13, + 14.57, + 16 + ], + "yValues": [ + 5E-4, + 0.002, + 0.02, + 1, + 0.02, + 0.002, + 5E-4 + ], + "prefactor": 1E20 + } + } + } + } + } + } + }, + "VelocityDistribution": { + "VelocityDistributionFromFunction": { + "VelocityDistributionFunction": { + "IsotropicDistribution": { + "EnergyDistributionFunction": { + "MaxwellBoltzmann": { + "Temperature": { + "value": 80, + "unit": "K" + } + } + }, + "Mass": { + "FromParticleType": "T2" + } + } + } + } + }, + "DriftVelocity": { + "VectorField": { + "VectorFieldFunctionInZ": { + "zFunction": { + "Product": { + "fi": { + "Constant": { + "constant": 1 + } + }, + "fj": { + "Constant": { + "constant": 1 + } + }, + "fk": { + "PiecewiseLinear": { + "xValues": [ + 0, + 7.99, + 8.01, + 16 + ], + "yValues": [ + -100, + -100, + 100, + 100 + ], + "prefactor": 1 + } + } + } + } + } + } + } + } + }, + "Tracked": { + "e-": { + "ParticleType": "electron", + "Mover": { + "Drift": { + "MaximalMovedDistance": { + "value": 0.001, + "unit": "m" + } + } + }, + "Terminator": { + "MaximumInteractions": 1E2, + "MaxTimeAlive": { + "value": 1, + "unit": "s" + }, + "BoundaryGeometry": { + "Cylinder": { + "Radius": { + "value": 4.5, + "unit": "cm" + }, + "Height": { + "value": 1600, + "unit": "cm" + } + } + } + }, + "Reflector": { + "ReflectorDPS": { + "position_z": { + "value": 1600, + "unit": "cm" + } + } + }, + "DensityField": { + "DynamicDensityField": { + "PositionBinning": { + "CreateSameAs": [ + "GlobalPositionBinning" + ] + }, + "EnergyBinning": { + "Type": "Logarithmic", + "NumberOfBins": 100, + "MinEnergy": { + "value": 1E-6, + "unit": "eV" + }, + "MaxEnergy": { + "value": 20E3, + "unit": "eV" + } + } + } + }, + "VelocityDistribution": { + "null": {} + }, + "DriftVelocity": { + "null": {} + }, + "Loggers": { + "Deletion": { + "Type": "all" + } + }, + "CurrentCollector": { + "PositionBinning": { + "CreateSameAs": [ + "GlobalPositionBinning" + ] + } + }, + "Interactions": { + "elastic": { + "Type": "ElasticScattering", + "CrossSection": { + "PredefinedFunction": "eT2_elastic_scattering_constant" + }, + "Target": "TritiumGas", + "ScatteringAngleDistribution": { + "Isotropic": {} + } + }, + "janev_non_diss": { + "Type": "ElectronIonization", + "IonizationModel": "JanevNonDiss", + "Target": "TritiumGas", + "OutgoingElectron": "e-", + "OutgoingIon": "T2+" + }, + "janev_diss_to_ground": { + "Type": "ElectronIonization", + "IonizationModel": "JanevDissToGround", + "Target": "TritiumGas", + "OutgoingElectron": "e-", + "OutgoingIon": "T+" + }, + "janev_diss_to_first_excited": { + "Type": "ElectronIonization", + "IonizationModel": "JanevDissToFirstExcited", + "Target": "TritiumGas", + "OutgoingElectron": "e-", + "OutgoingIon": "T+" + } + } + }, + "T2+": { + "ParticleType": "T2+", + "Mover": { + "Drift": { + "MaximalMovedDistance": { + "value": 0.001, + "unit": "m" + } + } + }, + "Terminator": { + "MaximumInteractions": 1E2, + "MaxTimeAlive": { + "value": 1, + "unit": "s" + }, + "BoundaryGeometry": { + "Cylinder": { + "Radius": { + "value": 4.5, + "unit": "cm" + }, + "Height": { + "value": 1600, + "unit": "cm" + } + } + } + }, + "Reflector": { + "NoReflector": {} + }, + "DensityField": { + "DynamicDensityField": { + "PositionBinning": { + "CreateSameAs": [ + "GlobalPositionBinning" + ] + }, + "EnergyBinning": { + "Type": "Logarithmic", + "NumberOfBins": 100, + "MinEnergy": { + "value": 1E-6, + "unit": "eV" + }, + "MaxEnergy": { + "value": 1E3, + "unit": "eV" + } + } + } + }, + "VelocityDistribution": { + "null": {} + }, + "DriftVelocity": { + "null": {} + }, + "Loggers": { + "Deletion": { + "Type": "all" + } + }, + "CurrentCollector": { + "PositionBinning": { + "CreateSameAs": [ + "GlobalPositionBinning" + ] + } + }, + "Interactions": { + "name1": { + "Type": "FreeMovement" + } + } + }, + "T+": { + "ParticleType": "T+", + "Mover": { + "Drift":{ + "MaximalMovedDistance": { + "value":0.001, + "unit": "m" + } + } + }, + "Terminator": { + "MaximumInteractions": 1E2, + "MaxTimeAlive": { + "value": 1, + "unit": "s" + }, + "BoundaryGeometry": { + "Cylinder": { + "Radius": { + "value": 4.5, + "unit": "cm" + }, + "Height": { + "value": 1600, + "unit": "cm" + } + } + } + }, + "Reflector": { + "NoReflector": {} + }, + "DensityField": { + "DynamicDensityField": { + "PositionBinning": { + "CreateSameAs": [ + "GlobalPositionBinning" + ] + }, + "EnergyBinning": { + "Type": "Logarithmic", + "NumberOfBins": 100, + "MinEnergy": { + "value": 1E-6, + "unit": "eV" + }, + "MaxEnergy": { + "value": 1E3, + "unit": "eV" + } + } + } + }, + "VelocityDistribution": { + "null": {} + }, + "DriftVelocity": { + "null": {} + }, + "Loggers": { + "Deletion": { + "Type": "all" + } + }, + "CurrentCollector": { + "PositionBinning": { + "CreateSameAs": [ + "GlobalPositionBinning" + ] + } + }, + "Interactions": { + "name1": { + "Type": "FreeMovement" + } + } + } + } + }, + "Injector": { + "CreatedSpecies": { + "specie1": { + "TrackedSpecieName": "e-", + "CreationVelocityDistribution": { + "VelocityDistributionFromFunction": { + "VelocityDistributionFunction": { + "IsotropicDistribution": { + "EnergyDistributionFunction": { + "TritiumBetaDecay": {} + }, + "Mass": { + "FromParticleType": "electron" + } + } + } + } + } + }, + "specie2": { + "TrackedSpecieName": "T2+", + "CreationVelocityDistribution": { + "VelocityDistributionFromFunction": { + "VelocityDistributionFunction": { + "IsotropicDistribution": { + "EnergyDistributionFunction": { + "MaxwellBoltzmann": { + "Temperature": { + "value": 80, + "unit": "K" + } + } + }, + "Mass": { + "FromParticleType": "T2" + } + } + } + } + } + } + }, + "EventPositionDistribution": { + "ScalarField": { + "ScalarFieldFunctionInZ": { + "zFunction": { + "Product": { + "fi": { + "Constant": { + "constant": 1 + } + }, + "fj": { + "Constant": { + "constant": 1 + } + }, + "fk": { + "PiecewiseLinear": { + "xValues": [ + 0, + 1.43, + 3, + 8, + 13, + 14.57, + 16 + ], + "yValues": [ + 5E-4, + 0.002, + 0.02, + 1, + 0.02, + 0.002, + 5E-4 + ], + "prefactor": 1E15 + } + } + } + } + } + }, + "DensityLimit": { + "min": 0, + "max": 1E15 + }, + "PositionLimits": { + "rPosition": { + "min": 0, + "max": 0.045 + }, + "phiPosition": { + "min": 0, + "max": 6.28318530718 + }, + "zPosition": { + "min": 0, + "max": 1.60 + } + } + }, + "MotherDriftVelocity": { + "VectorField": { + "VectorFieldConstant": { + "constant": { + "vector": [ + 0, + 0, + 0 + ] + }, + "unit": "m/s" + } + } + }, + "Activity": { + "value": 1E15, + "unit": "Bq" + } + }, + "ElectricField": { + "Background": { + "VectorField": { + "VectorFieldConstant": { + "constant": { + "vector": [ + 0, + 0, + 0 + ] + }, + "unit": "V/m" + } + } + }, + "Dynamic": { + "VectorField": { + "VectorFieldConstant": { + "constant": { + "vector": [ + 0, + 0, + 0 + ] + }, + "unit": "V/m" + } + } + } + }, + "MagneticField": { + "VectorField": { + "VectorFieldFunctionInZ": { + "zFunction": { + "Constant3D": { + "constant": 2.5 + } + } + } + } + }, + "PoissonSolver": { + "BoundaryGeometry": { + "Cylinder": { + "Radius": { + "value": 4.5, + "unit": "cm" + }, + "Height": { + "value": 1600, + "unit": "cm" + } + } + }, + "PositionBinning": { + "CreateSameAs": [ + "GlobalPositionBinning" + ] + }, + "TargetAccuracy": 1e-7, + "NumCycles": 2e4 + }, + "Simulation": { + "SimulatedEvents": 1, + "EventStride": 1 + }, + "Output": { + "folder": "output", + "filename": "test", + "DensityFields": { + "Species": "all", + "Intervall": 1, + "OverwriteLast": "true" + }, + "CurrentBorders": { + "Species": [ + "e-", + "T2+" + ], + "Intervall": "EachSync", + "OverwriteLast": "true" + }, + "DeletionLogger": { + "Species": "all", + "Intervall": "EachSync", + "OverwriteLast": "true" + }, + "PoissonSolver": { + "Intervall": "EachSync", + "OverwriteLast": "true" + } + }, + "RandomNumbers": { + "Seed": 1337 + } +} \ No newline at end of file diff --git a/src/poisson_solver/poisson_solver.cpp b/src/poisson_solver/poisson_solver.cpp index 22bcace..b68e187 100755 --- a/src/poisson_solver/poisson_solver.cpp +++ b/src/poisson_solver/poisson_solver.cpp @@ -6,6 +6,7 @@ #include "simulation/vector_field.h" #include #include +#include PoissonSolver::PoissonSolver(GlobalBoundary bottom_boundary, GlobalBoundary top_boundary, @@ -36,7 +37,6 @@ PoissonSolver::PoissonSolver(GlobalBoundary bottom_boundary, for (int j = potential_.lbound(1); j <= potential_.ubound(1); ++j) { for (int k = potential_.lbound(2) + 1; k <= potential_.ubound(2) - 1; ++k) { - //potential_(i, j, k) = PotentialGridPoint(BoundaryType::no_boundary, 0.); potential_(i,j,k).type = BoundaryType::no_boundary; potential_(i,j,k).value = 0.; @@ -46,7 +46,8 @@ PoissonSolver::PoissonSolver(GlobalBoundary bottom_boundary, // check which boundary it is and use the global boundary condition // or throw an error, because otherwise there should be an error with the density binning if (!geometry_->isInside(position)) { - potential_(i,j,k).type = BoundaryType::dirichlet; + //DEBUG_LEVEL_0("Binning is larger than the Geometry."); + potential_(i,j,k).type = BoundaryType::no_boundary; potential_(i,j,k).value = 0.; } for (auto& electrode : electrodes_) { @@ -89,6 +90,7 @@ void PoissonSolver::solverIteration( 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) { @@ -102,10 +104,13 @@ void PoissonSolver::solverIteration( + hull_boundary_.boundary_value * stepsize(0); } else if (i == 0) { // special calculation for r=0 + const int jmirror = (j& 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 - const double unit_factor = 1.; // todo: change this factor to the correct value for SI 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) { @@ -155,16 +160,24 @@ bool PoissonSolver::converged( ++k) { if (i == 0) { // special calculation for r=0 - 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) + - interpolated_density(i, j, k)/SI::eps0) - - potential_(i, j, k).value; + //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) @@ -173,20 +186,21 @@ bool PoissonSolver::converged( ? 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); + 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; - const double difference = - (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) - - potential_(i, j, k).value; cumulated_quadradic_difference += std::pow(difference, 2); + + if(fabs(difference) > maximum_difference) { + maximum_difference = fabs(difference); + } } } } @@ -199,18 +213,23 @@ bool PoissonSolver::converged( std::sqrt(cumulated_quadradic_difference / (nr * nth * nz)); DEBUG_LEVEL_0("Poisson solver reached a normalized difference of: " + std::to_string(normalized_difference)); - return normalized_difference < target_accuracy_; + std::ostringstream message; + message << "Maximum Differenece normalized by order of magnitude: " + << maximum_difference*SI::eps0/(fabs(blitz::max(interpolated_density)) + 1e-12); + DEBUG_LEVEL_0(message.str()); + return maximum_difference*SI::eps0/(fabs(blitz::max(interpolated_density)) + 1e-12) < target_accuracy_; } void PoissonSolver::solvePotential( const blitz::Array charge_density) { + DEBUG_LEVEL_3("Interpolating chargedensity."); blitz::Array interpolated_density = interpolateDensityToPotentialGrid(charge_density); int it = 1; do { - DEBUG_LEVEL_3("Poisson solver, iteraction: " + it); solverIteration(interpolated_density); if (it % 1000 == 0) { + DEBUG_LEVEL_3("Poisson solver, iteration: " + std::to_string(it)); if (converged(interpolated_density)) { break; }; @@ -223,6 +242,7 @@ 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) { @@ -287,6 +307,8 @@ blitz::Array PoissonSolver::interpolateDensityToPotentialGrid(const b (density(potential_.lbound(0), j , fixedIndex-1))/(potential_.extent(1)); } + DEBUG_LEVEL_3("Max Value in interpolated density array: " + + std::to_string(blitz::max(interpolatedDensity))); //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. it would be possible that the global boundary at those @@ -295,11 +317,6 @@ blitz::Array PoissonSolver::interpolateDensityToPotentialGrid(const b return interpolatedDensity; } - -//TODO: -interpolation to the correct position in the gridpoint -// (probably already during the calculation of the values -// 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 Eextent = potential_.extent() - @@ -330,30 +347,23 @@ std::shared_ptr PoissonSolver::staticElectricField() { 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) + 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) + (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)); + (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)); } } } } -// if the VectorField object uses the E-field -//at the correct position is not certain +// whether 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))); diff --git a/src/simulation/simulation.cpp b/src/simulation/simulation.cpp index 5c4ab7d..9dc33bc 100755 --- a/src/simulation/simulation.cpp +++ b/src/simulation/simulation.cpp @@ -155,7 +155,7 @@ void Simulation::processEvents(int events) { particle_queue.insert(particle_queue.end(), new_particles.begin(), new_particles.end()); } - DEBUG_LEVEL_2("Particles in queue: " + particle_queue.size()); + DEBUG_LEVEL_2("Particles in queue: " + std::to_string(particle_queue.size())); const int particle_number = particleNumberFromQueue(particle_queue.size(), random_.get()); @@ -319,11 +319,13 @@ blitz::Array Simulation::chargedensity() { blitz::Array init = firstTracked->dynamicDensityField()->densityArray(); blitz::Array chargeden(init.lbound(), init.extent()); chargeden = 0.; + DEBUG_LEVEL_3("Collect chargedensity"); for (auto& pair : tracked_species_) { chargeden += pair.second->charge_C() * pair.second->dynamicDensityField()-> densityArray(); } - + DEBUG_LEVEL_3("Maximum value in the chargedensity: " + std::to_string(blitz::max(chargeden))); + return chargeden; }