Skip to content

Reuse integrators in nlstep and NonlinearSolveAlg integrations #2760

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lib/OrdinaryDiffEqNonlinearSolve/src/newton.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function initialize!(nlsolver::NLSolver{<:NonlinearSolveAlg, true},
cache.invγdt = inv(dt * nlsolver.γ)
cache.tstep = integrator.t + nlsolver.c * dt

@unpack ustep, tstep, k, invγdt = cache
@unpack ustep, atmp, tstep, k, invγdt = cache

if DiffEqBase.has_stats(integrator)
integrator.stats.nf += cache.cache.stats.nf
Expand All @@ -66,25 +66,25 @@ function initialize!(nlsolver::NLSolver{<:NonlinearSolveAlg, true},

nlstep_data = f.nlstep_data
if nlstep_data !== nothing
atmp .= 0
if method === COEFFICIENT_MULTISTEP
nlstep_data.set_γ_c(nlstep_data.nlprob, (one(t), one(t), α * invγdt, tstep))
nlstep_data.set_inner_tmp(nlstep_data.nlprob, zero(z))
nlstep_data.set_inner_tmp(nlstep_data.nlprob, atmp)
nlstep_data.set_outer_tmp(nlstep_data.nlprob, tmp)
else
nlstep_data.set_γ_c(nlstep_data.nlprob, (dt, γ, one(t), tstep))
nlstep_data.set_inner_tmp(nlstep_data.nlprob, tmp)
nlstep_data.set_outer_tmp(nlstep_data.nlprob, zero(z))
nlstep_data.set_outer_tmp(nlstep_data.nlprob, atmp)
end
nlstep_data.nlprob.u0 .= @view z[nlstep_data.u0perm]
cache.cache = init(nlstep_data.nlprob, alg.alg)
SciMLBase.reinit!(cache.cache, nlstep_data.nlprob.u0, p=nlstep_data.nlprob.p)
else
if f isa DAEFunction
nlp_params = (tmp, ztmp, ustep, γ, α, tstep, k, invγdt, p, dt, f)
else
nlp_params = (tmp, ustep, γ, α, tstep, k, invγdt, method, p, dt, f)
end
new_prob = remake(cache.prob, p = nlp_params, u0 = z)
cache.cache = init(new_prob, alg.alg)
SciMLBase.reinit!(cache.cache, z, p=nlp_params)
end
nothing
end
Expand Down Expand Up @@ -127,7 +127,7 @@ end
nlcache.prob, nlcache.alg, nlcache.u, nlcache.fu;
nlcache.retcode, nlcache.stats, nlcache.trace
)
ztmp .= nlstep_data.nlprobmap(nlstepsol)
nlstep_data.nlprobmap(ztmp, nlstepsol)
ustep = compute_ustep!(ustep, tmp, γ, z, method)
calculate_residuals!(@view(atmp[nlstep_data.u0perm]), nlcache.fu,
@view(uprev[nlstep_data.u0perm]),
Expand Down
8 changes: 4 additions & 4 deletions test/modelingtoolkit/nlstep_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using Test
eqs = [D(y₁) ~ -k₁ * y₁ + k₃ * y₂ * y₃,
D(y₂) ~ k₁ * y₁ - k₂ * y₂^2 - k₃ * y₂ * y₃,
D(y₃) ~ k₂ * y₂^2]
@mtkbuild rober = ODESystem(eqs, t)
@mtkcompile rober = ODESystem(eqs, t)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mtkcompile rober = System(eqs, t)

prob = ODEProblem(rober, [[y₁, y₂, y₃] .=> [1.0; 0.0; 0.0]; [k₁, k₂, k₃] .=> (0.04, 3e7, 1e4)], (0.0, 1e5), jac = true)
prob2 = ODEProblem(rober, [[y₁, y₂, y₃] .=> [1.0; 0.0; 0.0]; [k₁, k₂, k₃] .=> (0.04, 3e7, 1e4)], (0.0, 1e5), jac = true, nlstep = true)

Expand All @@ -29,7 +29,7 @@ sol1 = solve(prob, TRBDF2(autodiff=AutoFiniteDiff(), nlsolve = nlalg));
sol2 = solve(prob2, TRBDF2(autodiff=AutoFiniteDiff(), nlsolve = nlalg));

@test sol1.t != sol2.t
@test sol1 != sol2
@test sol1.u != sol2.u
@test sol1(sol1.t) ≈ sol2(sol1.t) atol=1e-3

testprob = ODEProblem(rober, [[y₁, y₂, y₃] .=> [1.0; 0.0; 0.0]; [k₁, k₂, k₃] .=> (0.04, 3e7, 1e4)], (0.0, 1.0), nlstep = true)
Expand Down Expand Up @@ -60,15 +60,15 @@ sim = analyticless_test_convergence(dts, testprob, FBDF(autodiff=AutoFiniteDiff(
eqs_nonaut = [D(y₁) ~ -k₁ * y₁ + (t+1) * k₃ * y₂ * y₃,
D(y₂) ~ k₁ * y₁ - (t+1) * k₂ * y₂^2 - (t+1) * k₃ * y₂ * y₃,
D(y₃) ~ (t+1) * k₂ * y₂^2]
@mtkbuild rober_nonaut = ODESystem(eqs_nonaut, t)
@mtkcompile rober_nonaut = ODESystem(eqs_nonaut, t)
prob = ODEProblem(rober_nonaut, [[y₁, y₂, y₃] .=> [1.0; 0.0; 0.0]; [k₁, k₂, k₃] .=> (0.04, 3e7, 1e4)], (0.0, 1e5), jac = true)
prob2 = ODEProblem(rober_nonaut, [[y₁, y₂, y₃] .=> [1.0; 0.0; 0.0]; [k₁, k₂, k₃] .=> (0.04, 3e7, 1e4)], (0.0, 1e5), jac = true, nlstep = true)

sol1 = solve(prob, FBDF(autodiff=AutoFiniteDiff(), nlsolve = nlalg));
sol2 = solve(prob2, FBDF(autodiff=AutoFiniteDiff(), nlsolve = nlalg));

@test sol1.t != sol2.t
@test sol1 != sol2
@test sol1.u != sol2.u
@test sol1(sol1.t) ≈ sol2(sol1.t) atol=1e-3

sol1 = solve(prob, TRBDF2(autodiff=AutoFiniteDiff(), nlsolve = nlalg));
Expand Down
Loading