Skip to content

Commit

Permalink
Change sign of regularization, plus cleaner code design
Browse files Browse the repository at this point in the history
  • Loading branch information
glwagner committed Oct 18, 2024
1 parent c653542 commit 3f62872
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions src/Solvers/conjugate_gradient_poisson_solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ end

@kernel function laplacian!(∇²ϕ, grid, ϕ)
i, j, k = @index(Global, NTuple)
@inbounds ∇²ϕ[i, j, k] = ∇²ᶜᶜᶜ(i, j, k, grid, ϕ)
active = !inactive_cell(i, j, k, grid)
@inbounds ∇²ϕ[i, j, k] = ∇²ᶜᶜᶜ(i, j, k, grid, ϕ) * active
end

struct RegularizedLaplacian{D}
Expand All @@ -47,7 +48,10 @@ function (L::RegularizedLaplacian)(Lϕ, ϕ)
if !isnothing(L.δ)
# Add regularization
ϕ̄ = mean(ϕ)
parent(Lϕ) .+= L.δ * ϕ̄
ΔLϕ = L.δ * ϕ̄
grid = ϕ.grid
arch = architecture(grid)
launch!(arch, grid, :xyz, subtract_and_mask!, Lϕ, grid, ΔLϕ)
end

return nothing
Expand Down Expand Up @@ -136,15 +140,19 @@ const FFTBasedPreconditioner = RegularizedPreconditioner{<:SolverWithFFT}
function precondition!(p, regularized::FFTBasedPreconditioner, r, args...)
solver = regularized.preconditioner
compute_preconditioner_rhs!(solver, r)
p = solve!(p, solver)
solve!(p, solver)
regularize_poisson_solution!(p, regularized)
return p
end

function regularize_poisson_solution!(p, regularized)
δ = regularized.regularization
rhs = regularized.rhs
mean_p = mean(p)

if !isnothing(δ)
mean_rhs = mean(rhs)
Δp = mean_p - mean_rhs / δ
Δp = mean_p + mean_rhs / δ
else
Δp = mean_p
end
Expand Down Expand Up @@ -175,22 +183,7 @@ Base.summary(::AsymptoticPoissonPreconditioner) = "AsymptoticPoissonPrecondition
arch = architecture(p)
fill_halo_regions!(r)
launch!(arch, grid, :xyz, _asymptotic_poisson_precondition!, p, grid, r)

δ = preconditioner.regularization
rhs = preconditioner.rhs
mean_p = mean(p)

if !isnothing(δ)
mean_rhs = mean(rhs)
Δp = mean_p - mean_rhs / δ
else
Δp = mean_p
end

grid = p.grid
arch = architecture(grid)
launch!(arch, grid, :xyz, subtract_and_mask!, p, grid, Δp)

regularize_poisson_solution!(p, preconditioner)
return p
end

Expand Down

0 comments on commit 3f62872

Please sign in to comment.