Skip to content

Commit 50ef74f

Browse files
Preserve FIRK nested solve type boundaries
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
1 parent 0dd5ce7 commit 50ef74f

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

lib/BoundaryValueDiffEqFIRK/src/collocation.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ function Φ!(residual, cache::FIRKCacheNested, y, u, trait, constraint)
1212
)
1313
end
1414

15+
@inline function __nested_stage_parameter(cache::FIRKCacheNested, p)
16+
length(cache.bcresid_prototype) < cache.M && return nodual_value(p)
17+
return p
18+
end
19+
1520
@views function Φ!(
1621
residual, fᵢ_cache, k_discrete, f!, TU::FIRKTableau{false}, y, u, p,
1722
mesh, mesh_dt, stage::Int, f_prototype, singular_term, ::DiffCacheNeeded, ::Val{true}
@@ -195,7 +200,7 @@ end
195200

196201
K = get_tmp(k_discrete[i], u)
197202

198-
_nestprob = remake(nest_prob, p = nodual_value(nestprob_p))
203+
_nestprob = remake(nest_prob, p = __nested_stage_parameter(cache, nestprob_p))
199204
nestsol = __solve(_nestprob, nest_nlsolve_alg; alg.nested_nlsolve_kwargs...)
200205
@. K = nestsol.u
201206
@. residᵢ = yᵢ₊₁ - yᵢ
@@ -227,7 +232,7 @@ end
227232

228233
K = get_tmp(k_discrete[i], u)
229234

230-
_nestprob = remake(nest_prob, p = nodual_value(nestprob_p))
235+
_nestprob = remake(nest_prob, p = __nested_stage_parameter(cache, nestprob_p))
231236
nestsol = __solve(_nestprob, nest_nlsolve_alg; alg.nested_nlsolve_kwargs...)
232237
@. K = nestsol.u
233238
@. residᵢ = yᵢ₊₁ - yᵢ
@@ -385,7 +390,7 @@ end
385390
nestprob_p[2] = T(mesh_dt[i])
386391
nestprob_p[3:end] = yᵢ
387392

388-
_nestprob = remake(nest_prob, p = nestprob_p)
393+
_nestprob = remake(nest_prob, p = __nested_stage_parameter(cache, nestprob_p))
389394
nestsol = __solve(_nestprob, nest_nlsolve_alg; alg.nested_nlsolve_kwargs...)
390395

391396
@. residᵢ = yᵢ₊₁ - yᵢ

lib/BoundaryValueDiffEqFIRK/src/firk.jl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,15 +1393,23 @@ function __construct_problem(
13931393
)
13941394
end
13951395

1396+
@inline function __firk_eval_sol!(eval_sol::EvalSol, y_, cache)
1397+
u = __restructure_sol(y_, cache.in_size)
1398+
if eltype(first(u)) <: eltype(first(eval_sol.u))
1399+
eval_sol.u[1:end] .= u
1400+
return eval_sol
1401+
end
1402+
return EvalSol(u, eval_sol.t, cache)
1403+
end
1404+
13961405
@views function __firk_loss!(
13971406
resid, u, p, y, pt::StandardBVProblem, bc!::BC, residual, mesh,
13981407
cache, eval_sol, trait::DiffCacheNeeded, constraint
13991408
) where {BC}
14001409
y_ = recursive_unflatten!(y, u)
14011410
resids = [get_tmp(r, u) for r in residual]
14021411
Φ!(resids[2:end], cache, y_, u, trait, constraint)
1403-
eval_sol.u[1:end] .= y_
1404-
eval_bc_residual!(resids[1], pt, bc!, eval_sol, p, mesh)
1412+
eval_bc_residual!(resids[1], pt, bc!, __firk_eval_sol!(eval_sol, y_, cache), p, mesh)
14051413
recursive_flatten!(resid, resids)
14061414
return nothing
14071415
end
@@ -1413,8 +1421,7 @@ end
14131421
y_ = recursive_unflatten!(y, u)
14141422
resids = [r for r in residual]
14151423
Φ!(resids[2:end], cache, y_, u, trait, constraint)
1416-
eval_sol.u[1:end] .= y_
1417-
eval_bc_residual!(resids[1], pt, bc!, eval_sol, p, mesh)
1424+
eval_bc_residual!(resids[1], pt, bc!, __firk_eval_sol!(eval_sol, y_, cache), p, mesh)
14181425
recursive_flatten!(resid, resids)
14191426
return nothing
14201427
end
@@ -1473,8 +1480,7 @@ end
14731480
u, p, y, pt::StandardBVProblem, bc::BC, mesh, cache, eval_sol, trait
14741481
) where {BC}
14751482
y_ = recursive_unflatten!(y, u)
1476-
eval_sol.u[1:end] .= y_
1477-
resid_bc = eval_bc_residual(pt, bc, eval_sol, p, mesh)
1483+
resid_bc = eval_bc_residual(pt, bc, __firk_eval_sol!(eval_sol, y_, cache), p, mesh)
14781484
resid_co = Φ(cache, y_, u, trait)
14791485
return vcat(resid_bc, mapreduce(vec, vcat, resid_co))
14801486
end

0 commit comments

Comments
 (0)