Skip to content

Commit 2f17929

Browse files
authored
Better error message for bad time step inputs (#112)
1 parent 64a7dcb commit 2f17929

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/solvers/tdvp.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ function time_step_and_nsteps(t, time_step, nsteps::Nothing)
3434
if nsteps_float nsteps_rounded
3535
return error("`t / time_step = $t / $time_step = $(t / time_step)` must be an integer.")
3636
end
37+
if real(nsteps_rounded) < 0
38+
return error(
39+
"computed number of steps is negative ($nsteps_rounded), check that total time ($t) and time step ($time_step) signs agree",
40+
)
41+
end
3742
return time_step, Int(nsteps_rounded)
3843
end
3944

test/base/test_solvers/test_tdvp.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
3838
@test norm(ψ2) 1 rtol = eps(real(elt)) * 10
3939
# Should rotate back to original state:
4040
@test abs(inner(ψ0, ψ2)) > 0.99
41+
42+
@testset "Failure mode tests" begin
43+
total_time = elt(0.1) * im
44+
# matching total time and time step directions
45+
@test_throws "signs agree" tdvp(H, -total_time, ψ0; time_step)
46+
@test_throws "signs agree" tdvp(H, total_time, ψ0; time_step=-time_step)
47+
# total time is an integer of time steps
48+
@test_throws "integer" tdvp(H, total_time * 1.5, ψ0; time_step)
49+
end
4150
end
4251

4352
@testset "TDVP: Sum of Hamiltonians (eltype=$elt)" for elt in elts

0 commit comments

Comments
 (0)