Skip to content

Commit

Permalink
Allow bailout with solution return if handle_exceptions is true (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
j-fu authored Jun 27, 2023
1 parent ada3fa4 commit 649eaf7
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "VoronoiFVM"
uuid = "82b139dc-5afc-11e9-35da-9b9bdfd336f3"
authors = ["Jürgen Fuhrmann <juergen.fuhrmann@wias-berlin.de>", "Dilara Abdel", "Jan Weidner", "Alexander Seiler", "Patricio Farrell", "Matthias Liero"]
version = "1.8.0"
version = "1.9.0"

[deps]
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
Expand Down
6 changes: 6 additions & 0 deletions docs/src/changes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# Changes

## v1.9.0 June 27, 2023
- With `control.handle_exceptions=true`, in case of a failing step,
time stepping and embeding now returns the solution calculated so far instead of
throwing an error

## v1.8.0 June 20, 2023
- LinearSolve 2.x

Expand Down
100 changes: 59 additions & 41 deletions src/vfvm_solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,9 @@ function CommonSolve.solve(
println("[e]volution: start in $(extrema(lambdas))")
end

λ0 = 0
istep = 0
solved = false
t1 = @elapsed for i = 1:(length(lambdas)-1)
Δλ = max(Δλ, Δλ_min(control, transient))
λstart = lambdas[i]
Expand Down Expand Up @@ -402,58 +404,74 @@ function CommonSolve.solve(
if !solved
# reduce time step and retry solution
Δλ = Δλ * 0.5
rd(x)=round(x,sigdigits=5)
if Δλ < Δλ_min(control, transient)
if !(control.force_first_step && istep == 0)
throw(
EmbeddingError(
" Δ$(λstr)_min=$(Δλ_min(control,transient)) reached while Δu=$(Δu) >> Δu_opt=$(control.Δu_opt) ",
),
)
err="At $(λstr)=$(λ|>rd): Δ$(λstr)_min=$(Δλ_min(control,transient)|>rd) reached while Δu=$(Δu|>rd) and Δu_opt=$(control.Δu_opt|>rd) "
if control.handle_exceptions
@warn err
else
throw(DomainError(err))
end
break
else
solved = true
solved=true
end
end
if doprint(control, 'e')
@printf("[e]volution: Δu=%.3e => retry: Δ%s=%.3e\n", Δu, λstr, Δλ)
end
end
end
istep = istep + 1
if doprint(control, 'e')
@printf(
"[e]volution: step=%d %s=%.3e Δ%s=%.3e Δu=%.3e\n",
istep,
λstr,
λ,
λstr,
Δλ,
Δu
)
end
if control.log
push!(allhistory, system.history)
push!(allhistory.updates, Δu)
push!(allhistory.times, λ)
end
if control.store_all
append!(tsol, λ, solution)
end
control.post(solution, oldsolution, λ, Δλ)
oldsolution .= solution
if λ < λend
Δλ = min(
Δλ_max(control, transient),
Δλ * Δλ_grow(control, transient),
Δλ * control.Δu_opt / (Δu + 1.0e-14),
λend - λ,
)
end
end # while !solved

if solved
istep = istep + 1
if doprint(control, 'e')
@printf(
"[e]volution: step=%d %s=%.3e Δ%s=%.3e Δu=%.3e\n",
istep,
λstr,
λ,
λstr,
Δλ,
Δu
)
end
if control.log
push!(allhistory, system.history)
push!(allhistory.updates, Δu)
push!(allhistory.times, λ)
end
if control.store_all
append!(tsol, λ, solution)
end
control.post(solution, oldsolution, λ, Δλ)
oldsolution .= solution
if λ < λend
Δλ = min(
Δλ_max(control, transient),
Δλ * Δλ_grow(control, transient),
Δλ * control.Δu_opt / (Δu + 1.0e-14),
λend - λ,
)
end
else
break
end # if solved
end # while λ<λ_end

if !control.store_all # store last solutionobtained
append!(tsol, λ0, solution)
end
if !control.store_all
append!(tsol, lambdas[i+1], solution)
control.sample(solution, λ0)
if solved
if !lambdas[i+1])
@warn "λ=$(λ), lambdas[i+1]=$(lambdas[i+1])"
end
else
break
end
control.sample(solution, lambdas[i+1])
end
end # for i = 1:(length(lambdas)-1)

if doprint(control, 'e')
println("[e]volution: $(round(t0+t1,sigdigits=3)) seconds")
Expand Down
5 changes: 4 additions & 1 deletion src/vfvm_solvercontrol.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,11 @@ Base.@kwdef mutable struct SolverControl
"""
Handle exceptions during transient solver and parameter embedding.
If `true`, exceptions in Newton solves are caught, the embedding resp. time step is lowered,
and solution is retried.
and solution is retried. Moreover, if embedding or time stepping fails (e.g. due to reaching
minimal step size), a warning is issued, and a solution is returned with all steps calculated
so far.
Otherwise (by default) errors are thrown.
"""
handle_exceptions::Bool = false

Expand Down

2 comments on commit 649eaf7

@j-fu
Copy link
Member Author

@j-fu j-fu commented on 649eaf7 Jun 27, 2023

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/86361

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.9.0 -m "<description of version>" 649eaf7d8dabc746d99cefeb3e2a5adf886a6d03
git push origin v1.9.0

Please sign in to comment.