What is the best way to debug a BVP

I have a BVP that I’m trying to solve with TwoPointBVProblem, and having a very hard time debugging it. is there a good way to get output detailing why a problem is unstable?

for example, I initially had a z′/r which is sometimes 0/0, so I had to replace it with the actual value using l’hopital when it’s too small; but it took me a very long time to even realize that that was even the problem.

what I’m actually trying to do is trying to write

\begin{gathered} r'' = -\frac{z'}{c_2} \left[ \frac{\rho}{\sigma}\Pi(r,z) + c - c_2\frac{z'}{r} \right], \\[6pt] z'' = \frac{r'}{c_2} \left[ \frac{\rho}{\sigma}\Pi(r,z) + c - c_2\frac{z'}{r} \right], \\[6pt] V' = 2\pi r r'z, \qquad c' = 0, \qquad c_2' = 0 \\[8pt] r(0)=0, \qquad r'(0)=\frac{1}{c_2}, \qquad z'(0)=0, \qquad V(0)=0, \\[6pt] z'(1) = r'(1)\cot\theta, \qquad V(1)=V_0, \qquad r(1) = R\!\left(z(1)\right). \end{gathered}

in julia, and what I have currently works only when Π is zero (e.g., interface.(0, 0, 1:179) all look like how I’d expect):

using BoundaryValueDiffEq
function capillary!(res, (r′, r, z′, z, V, c, c₂), (;ρ, ω, g, σ), ξ)
    Π    =  ω^2*r^2/2 - g*z
    z′_r = abs(r) < 1e-6 ? (ρ/σ*Π+c)/2c₂ : z′/r # l'hopital

    r′′  = -z′/c₂ * (ρ/σ*Π + c - c₂*z′_r)
    z′′  =  r′/c₂ * (ρ/σ*Π + c - c₂*z′_r)
    V′   = 2π*r*r′*z

    res .= [r′′, r′, z′′, z′, V′, 0., 0.]
end

function start!(res, (r′, r, z′, z, V, c, c₂), p)
    res[1] = r
    res[2] = r′ - 1/c₂
    res[3] = z′
    res[4] = V
end
function end!(res, (r′, r, z′, z, V, c, c₂), (;θ, V₀, R))
    res[1] = z′ - r′*cotd(θ)
    res[2] = V - V₀
    res[3] = r - R(z)
end

wall(z) = 1
function interface(g, ω, θ; V₀ = 1, R = wall, ρ = 903.5, σ = 0.028)
    u0 = [0., 0., 0., 0., V₀/2, -ρ/σ*(ω^2/6-g), 1.] # threw some random stuff in here to see if it would help, hasn't done anything yet but idk
    p = (;ρ, σ, g, ω, θ, V₀, R)
    prob = TwoPointBVProblem(capillary!, (start!, end!), u0, (0., 1.), p;
                             bcresid_prototype=(zeros(4), zeros(3)))
    solve(prob, MIRK4(); dt = 1 / 50)
end

What does the failure look like?

if I run like interface(.1, 0, 50) or interface(0, .01, 50), it uses between 50~100% of my cpu for a few hours and then returns an all-zero result with retcode: Unstable

Huh it should be stopping an unstable solve giving the final iteration, I’ll check what’s going on here.

the all zeros result may have been an earlier version; I stopped waiting the whole time since it makes my computer a bit slow while it’s chugging along, and successful results were finishing in under a second so it was pretty clear when I was doing something wrong. I started a run of interface(.1, 0, 50) when you first replied so whenever that stops I can edit the actual return of this iteration of the code into this reply

just returned! looks like roughly 3.5ish hours?

julia> function interface(g, ω, θ; V₀ = π, R = wall, ρ = 903.5, σ = 0.028)
           u0 = [0., 0., 0., 0., V₀/2, -ρ/σ*(ω^2/6-g), 1.]
           p = (;ρ, σ, g, ω, θ, V₀, R)
           prob = TwoPointBVProblem(capillary!, (start!, end!), u0, (0., 1.), p;
                                    bcresid_prototype=(zeros(4), zeros(3)))
           solve(prob, MIRK4(); dt = 1 / 50)
       end
interface (generic function with 1 method)

julia> sol = interface(0.1, 0, 50)
retcode: Unstable
Interpolation: MIRK Order 4 Interpolation
t: 1601-element Vector{Float64}:
 0.0
 0.000625
 0.00125
 0.001875
 0.0025
 0.003125
 0.00375
 0.004375
 0.005
 0.005625
 0.00625
 0.006875
 0.0075
 0.008125
 0.00875
 0.009375000000000001
 0.01
 0.010624999999999999
 0.01125
 0.011875
 0.0125
 0.013125000000000001
 ⋮
 0.9875
 0.988125
 0.98875
 0.989375
 0.99
 0.990625
 0.99125
 0.991875
 0.9924999999999999
 0.9931249999999999
 0.9937499999999999
 0.994375
 0.995
 0.995625
 0.9962500000000001
 0.9968750000000001
 0.9975
 0.998125
 0.99875
 0.999375
 1.0
u: 1601-element Vector{Vector{Float64}}:
 [0.0, 0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