Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Poisson solve to accept arbitrary eigenvalues #26

Merged
merged 7 commits into from
Aug 29, 2024
2 changes: 1 addition & 1 deletion src/ParticleInCell.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ end
export step!

include("field_utils.jl")
export FiniteDifferenceToEdges, AverageEdgesToNodes
export FiniteDifferenceToEdges, AverageEdgesToNodes, ZeroField, MultiplyField

include("poisson.jl")
export PoissonSolveFFT
Expand Down
7 changes: 7 additions & 0 deletions src/field_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,10 @@ struct ZeroField{F} <: AbstractSimulationStep
end

step!(step::ZeroField) = step.field.values .= 0

struct MultiplyField{F,T} <: AbstractSimulationStep
field::F
x::T
end

step!(step::MultiplyField) = step.field.values .*= step.x
10 changes: 10 additions & 0 deletions src/interpolation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ struct BSplineChargeInterpolation{S,F,IF} <: AbstractSimulationStep
interp_func,
)
end

function BSplineChargeInterpolation(
species::S,
rho::Field{T,N,NodeOffset},
interp_width,
interp_func::F,
) where {T,N,S,F}

new{S,typeof(rho),F}(species, rho, -1, interp_width, interp_func)
end
end

function step!(step::BSplineChargeInterpolation)
Expand Down
2 changes: 1 addition & 1 deletion src/particle_updaters/electrostatic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function step!(step::ElectrostaticParticlePush)
)

for I in Is
grid_cell_coord = cell_index_to_cell_coords(step.E, I)
grid_cell_coord = cell_index_to_cell_coords(step.E, I, 1)
dist = Tuple(particle_cell_coord .- grid_cell_coord)
interp_weights = step.interpolation_function.(dist)
interp_weight = prod(interp_weights)
Expand Down
19 changes: 15 additions & 4 deletions src/poisson.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
field_solve_three_pt(k, dx) = k^2 * sinc(k * dx / 2 / pi)^2
field_solve_lagrange(k, dx) = k^2 * sinc(k * dx / 2 / pi)^2 * (2 + cos(k * dx)) / 3
field_solve_five_pt(k, dx) = -1 * k^2 * sinc(k * dx / 2 / pi)^2 * (-7 + cos(k * dx)) / 6

struct PoissonSolveFFT{T,D,G,P,F<:AbstractField} <: AbstractSimulationStep
rho::F
phi::F
Expand All @@ -7,7 +11,11 @@ struct PoissonSolveFFT{T,D,G,P,F<:AbstractField} <: AbstractSimulationStep

fft_plan::P

function PoissonSolveFFT(rho::F, phi::F) where {T,N,O,D,G,F<:AbstractField{T,N,O,D,G}}
function PoissonSolveFFT(
rho::F,
phi::F,
k2s = field_solve_three_pt,
) where {T,D,G,F<:AbstractField{T,D,G}}
# This restriction could possibly be relaxed to just require compatible grids...
@assert rho.grid === phi.grid
# Currently only supports periodic boundary conditions...
Expand All @@ -31,9 +39,12 @@ struct PoissonSolveFFT{T,D,G,P,F<:AbstractField} <: AbstractSimulationStep
continue
end

ks = 2π .* (It .- 1) ./ sim_lengths
grid_angles = ks .* cell_lengths ./ 2
inv_Ksqs = (cell_lengths ./ (2 .* sin.(grid_angles))) .^ 2 ./ epsilon_0
# ks = 2π .* (It .- 1) ./ sim_lengths
ks =
2π .*
ifelse.(It .<= size(Ksq_inv) ./ 2, It .- 1, It .- size(Ksq_inv) .- 1) ./
sim_lengths
inv_Ksqs = (k2s.(ks, cell_lengths)) .^ (-1) ./ epsilon_0

Ksq_inv[I] = prod(inv_Ksqs)
end
Expand Down
Loading