|
| 1 | +module ReinitializationExt |
| 2 | + |
| 3 | +import LevelSetMethods as LSM |
| 4 | +using Interpolations |
| 5 | +using NearestNeighbors |
| 6 | +using LinearAlgebra |
| 7 | +using StaticArrays |
| 8 | + |
| 9 | +function __init__() |
| 10 | + return @info "Loading extension for Newton reinitialization" |
| 11 | +end |
| 12 | + |
| 13 | +function LSM.reinitialize!(eq::LSM.LevelSetEquation; kwargs...) |
| 14 | + LSM.reinitialize!(LSM.current_state(eq); kwargs...) |
| 15 | + return eq |
| 16 | +end |
| 17 | + |
| 18 | +function LSM.reinitialize!( |
| 19 | + ϕ::LSM.LevelSet; |
| 20 | + upsample::Int = 4, |
| 21 | + maxiters::Int = 10, |
| 22 | + xtol::Float64 = 1.0e-8, |
| 23 | + ftol::Float64 = 1.0e-8, |
| 24 | + ) |
| 25 | + grid = LSM.mesh(ϕ) |
| 26 | + vals = ϕ.vals |
| 27 | + itp = Interpolations.interpolate(ϕ) |
| 28 | + f(x) = itp(x...) |
| 29 | + ∇f(x) = Interpolations.gradient(itp, x...) |
| 30 | + ∇²f(x) = Interpolations.hessian(itp, x...) |
| 31 | + |
| 32 | + # Sample the interface |
| 33 | + pts = _sample_interface(grid, f, ∇f, upsample, maxiters, ftol) |
| 34 | + tree = KDTree(pts) |
| 35 | + |
| 36 | + for I in eachindex(grid) |
| 37 | + x = grid[I] |
| 38 | + # Find closest point in the cloud |
| 39 | + idx, dist = nn(tree, x) |
| 40 | + x0 = pts[idx] |
| 41 | + # Refine with Newton's method |
| 42 | + cp = _closest_point(f, ∇f, ∇²f, x, x0, maxiters, xtol, ftol) |
| 43 | + vals[I] = sign(vals[I]) * norm(x - cp) |
| 44 | + end |
| 45 | + return ϕ |
| 46 | +end |
| 47 | + |
| 48 | +function _sample_interface(grid, f, ∇f, upsample, maxiter, ftol) |
| 49 | + pts = Vector{SVector{LSM.dimension(grid), Float64}}() |
| 50 | + for I in CartesianIndices(LSM.size(grid) .- 1) |
| 51 | + Ip = CartesianIndex(Tuple(I) .+ 1) |
| 52 | + lc, hc = grid[I], grid[Ip] |
| 53 | + samples = (lc .+ (hc .- lc) .* (SVector(j, k) .+ 0.5) ./ upsample for j in 0:(upsample - 1), k in 0:(upsample - 1)) |
| 54 | + # Skip cells where all samples have the same sign |
| 55 | + all(x -> f(x) > 0, samples) || all(x -> f(x) < 0, samples) && continue |
| 56 | + # Go over samples and push them to the interface |
| 57 | + for x in samples |
| 58 | + pt = _project_to_interface(f, ∇f, x, maxiter, ftol) |
| 59 | + push!(pts, pt) |
| 60 | + end |
| 61 | + end |
| 62 | + return pts |
| 63 | +end |
| 64 | + |
| 65 | +function _project_to_interface(f, ∇f, x0, maxiter, ftol) |
| 66 | + x = x0 |
| 67 | + for _ in 1:maxiter |
| 68 | + val = f(x) |
| 69 | + val < ftol && break # close enough to the interface |
| 70 | + grad = ∇f(x) |
| 71 | + norm_grad = norm(grad) |
| 72 | + iszero(norm_grad) && break |
| 73 | + δx = val * grad / norm_grad^2 |
| 74 | + x = x - δx |
| 75 | + end |
| 76 | + f(x) > ftol && @warn "projection to interface did not converge to $ftol at x=$x" |
| 77 | + return x |
| 78 | +end |
| 79 | + |
| 80 | +function _closest_point(f, ∇f, ∇²f, xq::SVector, x0::SVector, maxiters, xtol, ftol) |
| 81 | + x = x0 |
| 82 | + ∇p_x0 = ∇f(x0) |
| 83 | + λ = dot(xq - x0, ∇p_x0) / dot(∇p_x0, ∇p_x0) |
| 84 | + |
| 85 | + converged = false |
| 86 | + for _ in 1:maxiters |
| 87 | + px = f(x) |
| 88 | + ∇p = ∇f(x) |
| 89 | + ∇²p = ∇²f(x) |
| 90 | + |
| 91 | + # System for Newton's method |
| 92 | + # ∇L = [ x - xq + λ∇p ] = 0 |
| 93 | + # [ p(x) ] |
| 94 | + grad_L = vcat(x - xq + λ * ∇p, px) |
| 95 | + |
| 96 | + # Hessian of the Lagrangian |
| 97 | + # H_L = [ I + λ∇²p ∇p ] |
| 98 | + # [ ∇p' 0 ] |
| 99 | + hess_L = hcat(vcat(I + λ * ∇²p, ∇p'), vcat(∇p, 0)) |
| 100 | +
|
| 101 | + # Solve for the update |
| 102 | + δ = -hess_L \ grad_L |
| 103 | + δx = δ[1:(end - 1)] |
| 104 | + δλ = δ[end] |
| 105 | +
|
| 106 | + # Update variables |
| 107 | + α = 1.0 # TODO: reduce step size if not diverging? |
| 108 | + x = x + α * δx |
| 109 | + λ = λ + α * δλ |
| 110 | +
|
| 111 | + # Check for convergence |
| 112 | + if norm(δx) < xtol && norm(f(x)) < ftol |
| 113 | + converged = true |
| 114 | + break |
| 115 | + end |
| 116 | + end |
| 117 | +
|
| 118 | + converged || @warn "closest point search did not converge at xq=$xq" |
| 119 | +
|
| 120 | + return x |
| 121 | +end |
| 122 | +
|
| 123 | +end |
0 commit comments