Skip to content

Commit

Permalink
Show absolute errors in interface checks
Browse files Browse the repository at this point in the history
  • Loading branch information
goerz committed Feb 1, 2025
1 parent 9eb65f6 commit ff6d107
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 39 deletions.
20 changes: 12 additions & 8 deletions src/interfaces/operator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ function check_operator(
quiet || @error "$(px)`mul!(ϕ, op, state)` must return the resulting ϕ"
success = false
end
if norm- op * Ψ) > atol
quiet || @error "$(px)`mul!(ϕ, op, state)` must match `op * state`"
Δ = norm- op * Ψ)
if Δ > atol
quiet || @error "$(px)`mul!(ϕ, op, state)` must match `op * state`" Δ atol
success = false
end
catch exc
Expand All @@ -146,9 +147,10 @@ function check_operator(
@error "$(px)`mul!(ϕ, op, state, α, β)` must return the resulting ϕ"
success = false
end
if norm- (0.5 * ϕ0 + 0.5 * (op * Ψ))) > atol
Δ = norm- (0.5 * ϕ0 + 0.5 * (op * Ψ)))
if Δ > atol
quiet ||
@error "$(px)`mul!(ϕ, op, state, α, β)` must match β*ϕ + α*op*state"
@error "$(px)`mul!(ϕ, op, state, α, β)` must match β*ϕ + α*op*state" Δ atol
success = false
end
catch exc
Expand All @@ -170,17 +172,19 @@ function check_operator(
@error "$(px)`dot(state, op, state)` must return a number, not $typeof(val)"
success = false
end
if abs(dot(Ψ, op, Ψ) - dot(Ψ, op * Ψ)) > atol
Δ = abs(dot(Ψ, op, Ψ) - dot(Ψ, op * Ψ))
if Δ > atol
quiet ||
@error "$(px)`dot(state, op, state)` must match `dot(state, op * state)`"
@error "$(px)`dot(state, op, state)` must match `dot(state, op * state)`" Δ atol
success = false
end
if supports_inplace(state)
ϕ = similar(Ψ)
mul!(ϕ, op, Ψ)
if abs(dot(Ψ, op, Ψ) - dot(Ψ, ϕ)) > atol
Δ = abs(dot(Ψ, op, Ψ) - dot(Ψ, ϕ))
if Δ > atol
quiet ||
@error "$(px)`dot(state, op, state)` must match `dot(state, op * state)`"
@error "$(px)`dot(state, op, state)` must match `dot(state, op * state)`" Δ atol
success = false
end
end
Expand Down
23 changes: 14 additions & 9 deletions src/interfaces/propagator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function check_propagator(
end
if abs(Δ) > atol
quiet ||
@error "$(px)`prop_step!` must advance `propagator.t` forward or backward one step on the time grid (Δ=)"
@error "$(px)`prop_step!` must advance `propagator.t` forward or backward one step on the time grid" Δ atol
success = false
end
catch exc
Expand Down Expand Up @@ -234,9 +234,10 @@ function check_propagator(

try
Ψ = set_state!(propagator, Ψ₀)
if norm(propagator.state - Ψ₀) > atol
Δ = norm(propagator.state - Ψ₀)
if Δ > atol
quiet ||
@error "$(px)`set_state!(propagator, state)` must set `propagator.state`"
@error "$(px)`set_state!(propagator, state)` must set `propagator.state`" Δ atol
success = false
end
if inplace
Expand Down Expand Up @@ -281,14 +282,16 @@ function check_propagator(
try
reinit_prop!(propagator, Ψ₀)
t = backward ? propagator.tlist[end] : propagator.tlist[begin]
if abs(propagator.t - t) > atol
Δ = abs(propagator.t - t)
if Δ > atol
quiet ||
@error "$(px)`reinit_prop!(propagator, state)` must reset `propagator.t`"
@error "$(px)`reinit_prop!(propagator, state)` must reset `propagator.t`" Δ atol
success = false
end
if norm(propagator.state - Ψ₀) > atol
Δ = norm(propagator.state - Ψ₀)
if Δ > atol
quiet ||
@error "$(px)`reinit_prop!(propagator, state)` must reset `propagator.state`"
@error "$(px)`reinit_prop!(propagator, state)` must reset `propagator.state`" Δ atol
success = false
end
if propagator.backward backward
Expand All @@ -302,11 +305,13 @@ function check_propagator(
success = false
end
prop_step!(propagator)
if norm(propagator.state - Ψ₁) > atol # Ψ₁ from very first prop_step!
Δ = norm(propagator.state - Ψ₁)
if Δ > atol # Ψ₁ from very first prop_step!
# Without keyword arguments to reinit_prop!, the propagator should
# be in the exact original state and thus reproduce the exact same
# propagation
quiet || @error "$(px)`reinit_prop!(propagator, state)` must be idempotent"
quiet ||
@error "$(px)`reinit_prop!(propagator, state)` must be idempotent" Δ atol
success = false
end
if inplace
Expand Down
55 changes: 33 additions & 22 deletions src/interfaces/state.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function check_state(
r2 = sqrt(real(state state))
Δ = abs(r1 - r2)
if Δ > atol
quiet || @error "$(px)`norm(state)=$r1)` must match `√(state⋅state)=$r2` (Δ=)"
quiet || @error "$(px)`norm(state)=$r1)` must match `√(state⋅state)=$r2`" Δ atol
success = false
end
catch exc
Expand All @@ -117,7 +117,7 @@ function check_state(
try
Δ = norm(state - state)
if Δ > atol
quiet || @error "`$(px)state - state` must have norm 0"
quiet || @error "`$(px)state - state` must have norm 0" Δ atol
success = false
end
η = norm(state + state)
Expand All @@ -141,8 +141,9 @@ function check_state(
@error "$(px)`copy(state)::$(typeof(ϕ))` must have the same type as `state::$(typeof(state))`"
success = false
end
if norm- state) > atol
quiet || @error "$(px)`copy(state) - state` must have norm 0"
Δ = norm- state)
if Δ > atol
quiet || @error "$(px)`copy(state) - state` must have norm 0" Δ atol
success = false
end
catch exc
Expand All @@ -155,9 +156,10 @@ function check_state(

try
ϕ = 0.5 * state
if abs(norm(ϕ) - 0.5 * norm(state)) > atol
Δ = abs(norm(ϕ) - 0.5 * norm(state))
if Δ > atol
quiet ||
@error "$(px)`norm(state)` must have absolute homogeneity: `norm(s * state) = s * norm(state)`"
@error "$(px)`norm(state)` must have absolute homogeneity: `norm(s * state) = s * norm(state)`" Δ atol
success = false
end
catch exc
Expand All @@ -170,8 +172,9 @@ function check_state(

try
state_zero = zero(state)
if norm(state_zero) > atol
quiet || @error("$(px)`zero(state)` must produce a state with norm 0.")
Δ = norm(state_zero)
if Δ > atol
quiet || @error "$(px)`zero(state)` must produce a state with norm 0." Δ atol
end
catch exc
quiet || @error(
Expand All @@ -182,8 +185,9 @@ function check_state(
end

try
if norm(0.0 * state) > atol
quiet || @error "$(px)`0.0 * state` must produce a state with norm 0"
Δ = norm(0.0 * state)
if Δ > atol
quiet || @error "$(px)`0.0 * state` must produce a state with norm 0" Δ atol
success = false
end
catch exc
Expand Down Expand Up @@ -236,9 +240,10 @@ function check_state(
success = false
end
end
if norm- state) > atol
Δ = norm- state)
if Δ > atol
quiet ||
@error "$(px)`ϕ - state` must have norm 0, where `ϕ = similar(state); copyto!(ϕ, state)`"
@error "$(px)`ϕ - state` must have norm 0, where `ϕ = similar(state); copyto!(ϕ, state)`" Δ atol
success = false
end
end
Expand All @@ -259,8 +264,9 @@ function check_state(
quiet || @error "$(px)`fill!(state, 0.0)` must return the filled state"
success = false
end
if norm(state_zero) > atol
quiet || @error "$(px)`fill!(state, 0.0)` must have norm 0"
Δ = norm(state_zero)
if Δ > atol
quiet || @error "$(px)`fill!(state, 0.0)` must have norm 0" Δ atol
success = false
end
end
Expand All @@ -277,9 +283,10 @@ function check_state(
ϕ = similar(state)
copyto!(ϕ, state)
ϕ = lmul!(1im, ϕ)
if norm- 1im * state) > atol
Δ = norm- 1im * state)
if Δ > atol
quiet ||
@error "$(px)`norm(state)` must have absolute homogeneity: `norm(s * state) = s * norm(state)`"
@error "$(px)`norm(state)` must have absolute homogeneity: `norm(s * state) = s * norm(state)`" Δ atol
success = false
end
end
Expand All @@ -296,9 +303,10 @@ function check_state(
ϕ = similar(state)
copyto!(ϕ, state)
ϕ = lmul!(0.0, ϕ)
if norm(ϕ) > atol
Δ = norm(ϕ)
if Δ > atol
quiet ||
@error "$(px)`lmul!(0.0, state)` must produce a state with norm 0"
@error "$(px)`lmul!(0.0, state)` must produce a state with norm 0" Δ atol
success = false
end
end
Expand All @@ -315,8 +323,10 @@ function check_state(
ϕ = similar(state)
copyto!(ϕ, state)
ϕ = axpy!(1im, state, ϕ)
if norm- (state + 1im * state)) > atol
quiet || @error "$(px)`axpy!(a, state, ϕ)` must match `ϕ += a * state`"
Δ = norm- (state + 1im * state))
if Δ > atol
quiet ||
@error "$(px)`axpy!(a, state, ϕ)` must match `ϕ += a * state`" Δ atol
success = false
end
end
Expand All @@ -333,8 +343,9 @@ function check_state(
if normalized
try
η = norm(state)
if abs- 1) > atol
quiet || @error "$(px)`norm(state)` must be 1, not "
Δ = abs- 1)
if Δ > atol
quiet || @error "$(px)`norm(state)` must be 1, not " Δ atol
success = false
end
catch exc
Expand Down

0 comments on commit ff6d107

Please sign in to comment.