Skip to content

Commit

Permalink
Validate normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
kbarros committed Dec 19, 2023
1 parent 24e8f7c commit de27f94
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/System/Interactions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ end
The total system energy. See also [`energy_per_site`](@ref).
"""
function energy(sys::System{N}) where N
validate_normalization(sys)
E = 0.0

# Zeeman coupling to external field
Expand Down
15 changes: 14 additions & 1 deletion src/System/System.jl
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,18 @@ 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.")
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.")
end
end

"""
randomize_spins!(sys::System)
Expand All @@ -433,9 +445,10 @@ represent states with decreasing angular momentum along this axis (``m = S, S-1,
…, -S``).
"""
function set_coherent!(sys::System{N}, Z, site) where N
site = to_cartesian(site)
length(Z) != N && error("Length of coherent state does not match system.")
iszero(N) && error("Cannot set zero-length coherent state.")
setspin!(sys, coherent_state(sys, site, Z), to_cartesian(site))
setspin!(sys, coherent_state(sys, site, Z), site)
end


Expand Down

0 comments on commit de27f94

Please sign in to comment.