Skip to content

Commit 649eaf7

Browse files
authored
Allow bailout with solution return if handle_exceptions is true (#75)
1 parent ada3fa4 commit 649eaf7

File tree

4 files changed

+70
-43
lines changed

4 files changed

+70
-43
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "VoronoiFVM"
22
uuid = "82b139dc-5afc-11e9-35da-9b9bdfd336f3"
33
authors = ["Jürgen Fuhrmann <juergen.fuhrmann@wias-berlin.de>", "Dilara Abdel", "Jan Weidner", "Alexander Seiler", "Patricio Farrell", "Matthias Liero"]
4-
version = "1.8.0"
4+
version = "1.9.0"
55

66
[deps]
77
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"

docs/src/changes.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
# Changes
2+
3+
## v1.9.0 June 27, 2023
4+
- With `control.handle_exceptions=true`, in case of a failing step,
5+
time stepping and embeding now returns the solution calculated so far instead of
6+
throwing an error
7+
28
## v1.8.0 June 20, 2023
39
- LinearSolve 2.x
410

src/vfvm_solver.jl

+59-41
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,9 @@ function CommonSolve.solve(
343343
println("[e]volution: start in $(extrema(lambdas))")
344344
end
345345

346+
λ0 = 0
346347
istep = 0
348+
solved = false
347349
t1 = @elapsed for i = 1:(length(lambdas)-1)
348350
Δλ = max(Δλ, Δλ_min(control, transient))
349351
λstart = lambdas[i]
@@ -402,58 +404,74 @@ function CommonSolve.solve(
402404
if !solved
403405
# reduce time step and retry solution
404406
Δλ = Δλ * 0.5
407+
rd(x)=round(x,sigdigits=5)
405408
if Δλ < Δλ_min(control, transient)
406409
if !(control.force_first_step && istep == 0)
407-
throw(
408-
EmbeddingError(
409-
" Δ$(λstr)_min=$(Δλ_min(control,transient)) reached while Δu=$(Δu) >> Δu_opt=$(control.Δu_opt) ",
410-
),
411-
)
410+
err="At $(λstr)=$(λ|>rd): Δ$(λstr)_min=$(Δλ_min(control,transient)|>rd) reached while Δu=$(Δu|>rd) and Δu_opt=$(control.Δu_opt|>rd) "
411+
if control.handle_exceptions
412+
@warn err
413+
else
414+
throw(DomainError(err))
415+
end
416+
break
412417
else
413-
solved = true
418+
solved=true
414419
end
415420
end
416421
if doprint(control, 'e')
417422
@printf("[e]volution: Δu=%.3e => retry: Δ%s=%.3e\n", Δu, λstr, Δλ)
418423
end
419424
end
420-
end
421-
istep = istep + 1
422-
if doprint(control, 'e')
423-
@printf(
424-
"[e]volution: step=%d %s=%.3e Δ%s=%.3e Δu=%.3e\n",
425-
istep,
426-
λstr,
427-
λ,
428-
λstr,
429-
Δλ,
430-
Δu
431-
)
432-
end
433-
if control.log
434-
push!(allhistory, system.history)
435-
push!(allhistory.updates, Δu)
436-
push!(allhistory.times, λ)
437-
end
438-
if control.store_all
439-
append!(tsol, λ, solution)
440-
end
441-
control.post(solution, oldsolution, λ, Δλ)
442-
oldsolution .= solution
443-
if λ < λend
444-
Δλ = min(
445-
Δλ_max(control, transient),
446-
Δλ * Δλ_grow(control, transient),
447-
Δλ * control.Δu_opt / (Δu + 1.0e-14),
448-
λend - λ,
449-
)
450-
end
425+
end # while !solved
426+
427+
if solved
428+
istep = istep + 1
429+
if doprint(control, 'e')
430+
@printf(
431+
"[e]volution: step=%d %s=%.3e Δ%s=%.3e Δu=%.3e\n",
432+
istep,
433+
λstr,
434+
λ,
435+
λstr,
436+
Δλ,
437+
Δu
438+
)
439+
end
440+
if control.log
441+
push!(allhistory, system.history)
442+
push!(allhistory.updates, Δu)
443+
push!(allhistory.times, λ)
444+
end
445+
if control.store_all
446+
append!(tsol, λ, solution)
447+
end
448+
control.post(solution, oldsolution, λ, Δλ)
449+
oldsolution .= solution
450+
if λ < λend
451+
Δλ = min(
452+
Δλ_max(control, transient),
453+
Δλ * Δλ_grow(control, transient),
454+
Δλ * control.Δu_opt / (Δu + 1.0e-14),
455+
λend - λ,
456+
)
457+
end
458+
else
459+
break
460+
end # if solved
461+
end # while λ<λ_end
462+
463+
if !control.store_all # store last solutionobtained
464+
append!(tsol, λ0, solution)
451465
end
452-
if !control.store_all
453-
append!(tsol, lambdas[i+1], solution)
466+
control.sample(solution, λ0)
467+
if solved
468+
if !lambdas[i+1])
469+
@warn "λ=$(λ), lambdas[i+1]=$(lambdas[i+1])"
470+
end
471+
else
472+
break
454473
end
455-
control.sample(solution, lambdas[i+1])
456-
end
474+
end # for i = 1:(length(lambdas)-1)
457475

458476
if doprint(control, 'e')
459477
println("[e]volution: $(round(t0+t1,sigdigits=3)) seconds")

src/vfvm_solvercontrol.jl

+4-1
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,11 @@ Base.@kwdef mutable struct SolverControl
198198
"""
199199
Handle exceptions during transient solver and parameter embedding.
200200
If `true`, exceptions in Newton solves are caught, the embedding resp. time step is lowered,
201-
and solution is retried.
201+
and solution is retried. Moreover, if embedding or time stepping fails (e.g. due to reaching
202+
minimal step size), a warning is issued, and a solution is returned with all steps calculated
203+
so far.
202204
205+
Otherwise (by default) errors are thrown.
203206
"""
204207
handle_exceptions::Bool = false
205208

0 commit comments

Comments
 (0)