Skip to content
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

Catch errors during show(System) #234

Merged
merged 2 commits into from
Feb 7, 2024
Merged
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
23 changes: 15 additions & 8 deletions src/System/System.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ function lattice_to_str(sys::System)
end

function energy_to_str(sys::System)
return "Energy per site "*number_to_math_string(energy_per_site(sys))
if is_valid_normalization(sys)
"Energy per site "*number_to_math_string(energy_per_site(sys))
else
"[Incorrectly normalized spin state!]"
end
end

function Base.show(io::IO, sys::System{N}) where N
Expand Down Expand Up @@ -408,18 +412,21 @@ end
return
end

function validate_normalization(sys::System{0})
for (s, κ) in zip(sys.dipoles, sys.κs)
norm(s) ≈ κ || error("Detected non-normalized dipole. Consider using `set_dipole!` to automatically normalize.")
function is_valid_normalization(sys::System{0})
all(zip(sys.dipoles, sys.κs)) do (s, κ)
norm2(s) ≈ κ^2
end
end

function validate_normalization(sys::System{N}) where N
for (Z, κ) in zip(sys.coherents, sys.κs)
norm(Z) ≈ √κ || error("Detected non-normalized coherent state. Consider using `set_coherent!` to automatically normalize.")
function is_valid_normalization(sys::System{N}) where N
all(zip(sys.coherents, sys.κs)) do (Z, κ)
norm2(Z) ≈ κ
end
end

function validate_normalization(sys::System)
is_valid_normalization(sys) || error("Detected non-normalized spin state.")
end

"""
randomize_spins!(sys::System)

Expand Down
Loading