Skip to content

Commit

Permalink
Various cleanup
Browse files Browse the repository at this point in the history
Improved error message on combiner isbits check
  • Loading branch information
kbarros committed Sep 10, 2024
1 parent d259cb0 commit 769d60e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 32 deletions.
15 changes: 8 additions & 7 deletions examples/spinw_tutorials/SW15_Ba3NbFe3Si2O14.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

using Sunny, GLMakie

# Build a [`Crystal`](@ref) for Ba₃NbFe₃Si₂O₁₄ using the crystal
# structure from [Marty et al., Phys. Rev. Lett. **101**, 247201
# Specify the Ba₃NbFe₃Si₂O₁₄ [`Crystal`](@ref) cell following [Marty et al.,
# Phys. Rev. Lett. **101**, 247201
# (2008)](http://dx.doi.org/10.1103/PhysRevLett.101.247201).

units = Units(:meV, :angstrom)
Expand All @@ -23,8 +23,8 @@ langasite = Crystal(latvecs, positions, 150; types)
cryst = subcrystal(langasite, "Fe")
view_crystal(cryst)

# Create a [`System`](@ref) and set Ba₃NbFe₃Si₂O₁₄ exchange interactions as
# parametrized in [Loire et al., Phys. Rev. Lett. **106**, 207201
# Create a [`System`](@ref) and set exchange interactions as parametrized in
# [Loire et al., Phys. Rev. Lett. **106**, 207201
# (2011)](http://dx.doi.org/10.1103/PhysRevLett.106.207201).

sys = System(cryst, [1 => Moment(s=5/2, g=2)], :dipole; seed=0)
Expand Down Expand Up @@ -54,8 +54,8 @@ end
# This compound is known to have a spiral order with approximate propagation
# wavevector ``𝐤 ≈ [0, 0, 1/7]``. Search for this magnetic order with
# [`minimize_spiral_energy!`](@ref). Due to reflection symmetry, one of two
# possible propagation wavevectors may appear, ``𝐤 = ± [0, 0, 0.1426...]``.
# Note that ``k_z = 0.1426...`` is very close to ``1/7 = 0.1428...``.
# possible propagation wavevectors may appear, ``𝐤 = ± [0, 0, 0.1426]``.
# Note that ``k_z = 0.1426`` is very close to ``1/7 = 0.1428``.

axis = [0, 0, 1]
randomize_spins!(sys)
Expand Down Expand Up @@ -95,4 +95,5 @@ measure = ssf_custom_bm(sys; u=[0, 1, 0], v=[0, 0, 1]) do q, ssf
end
swt = SpinWaveTheorySpiral(sys; measure, k, axis)
res = intensities(swt, path; energies, kernel=gaussian(fwhm=0.25))
plot_intensities(res; units, saturation=0.8, allpositive=false, title="Im[S²³(q, ω) - S³²(q, ω)]")
plot_intensities(res; units, saturation=0.8, allpositive=false,
title="Im[S²³(q, ω) - S³²(q, ω)]")
12 changes: 6 additions & 6 deletions ext/PlottingExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1013,17 +1013,17 @@ function colorrange_from_data(; data, saturation, sensitivity, allpositive)
cmax = Statistics.quantile(vec(maximum(data; dims=1)), saturation)
cmin = Statistics.quantile(vec(minimum(data; dims=1)), 1 - saturation)

# The returned (lo, hi) should satisfy lo < hi in all cases to allow for a
# meaningful Makie.Colorbar.
# The returned pair (lo, hi) should be strictly ordered, lo < hi, for use in
# Makie.Colorbar
if allpositive
# Intensities are supposed to be non-negative in this branch, but might
# not be due to any of: User error, very small roundative around
# negative zero, or Gibbs ringing in a KPM calculation.
# not be due to any of: User error, very small round-off around negative
# zero, or Gibbs ringing in a KPM calculation.
@assert 0 <= sensitivity < 1
if iszero(abs(cmax))
if cmax <= 0
return (0, 1e-15)
else
(sensitivity, 1) .* abs(cmax)
(sensitivity, 1) .* cmax
end
else
cscale = max(abs(cmax), abs(cmin))
Expand Down
2 changes: 1 addition & 1 deletion src/Measurements/MeasureSpec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct MeasureSpec{Op <: Union{Vec3, HermitianC64}, F, Ret}
function MeasureSpec(observables::Array{Op, 5}, corr_pairs, combiner::F, formfactors; offsets=nothing) where {Op, F}
# Lift return type of combiner function to type-level
Ret = only(Base.return_types(combiner, (Vec3, Vector{ComplexF64})))
@assert isbitstype(Ret)
isbitstype(Ret) || error("Inferred data type $Ret is not `isbits`")
# Create inner `nobs` dimension, if missing
if isone(ndims(formfactors))
formfactors = [ff for _ in axes(observables, 1), ff in formfactors]
Expand Down
20 changes: 4 additions & 16 deletions src/Symmetry/Crystal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ end
# Constructs a crystal from the complete list of atom positions `positions`,
# representing fractions (between 0 and 1) of the lattice vectors `latvecs`.
# All symmetry information is automatically inferred.
function Crystal(latvecs, positions; types::Union{Nothing,Vector{String}}=nothing, symprec=1e-5)
function Crystal(latvecs, positions; types::Union{Nothing, Vector{String}}=nothing, symprec=1e-5)
print_crystal_warnings(latvecs, positions)
latvecs = convert(Mat3, latvecs)
positions = [convert(Vec3, p) for p in positions]
Expand All @@ -96,28 +96,16 @@ end


# Builds a crystal by applying the symmetry operators for a given spacegroup
# symbol.
function Crystal(latvecs, positions, symbol::String; types::Union{Nothing,Vector{String}}=nothing, setting=nothing, symprec=1e-5)
# (number or symbol).
function Crystal(latvecs, positions, symbol::Union{Int, String}; types::Union{Nothing, Vector{String}}=nothing, setting=nothing, symprec=1e-5)
print_crystal_warnings(latvecs, positions)
latvecs = convert(Mat3, latvecs)
positions = [convert(Vec3, p) for p in positions]
if isnothing(types)
types = fill("", length(positions))
end
crystal_from_symbol(latvecs, positions, types, symbol; setting, symprec)
end

# Builds a crystal by applying symmetry operators for a given international
# spacegroup number.
function Crystal(latvecs, positions, spacegroup_number::Int; types::Union{Nothing,Vector{String}}=nothing, setting=nothing, symprec=1e-5)
print_crystal_warnings(latvecs, positions)
latvecs = convert(Mat3, latvecs)
positions = [convert(Vec3, p) for p in positions]
if isnothing(types)
types = fill("", length(positions))
end
symbol = string(spacegroup_number)
crystal_from_symbol(latvecs, positions, types, symbol; setting, symprec)
crystal_from_symbol(latvecs, positions, types, string(symbol); setting, symprec)
end

function print_crystal_warnings(latvecs, positions)
Expand Down
5 changes: 3 additions & 2 deletions test/test_intensities_setup.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# TODO: Optimize performance, perhaps via `unitary_irrep_for_rotation`. Runtime
# is 1.1s on Sunny 0.7.
# TODO: Investigate TestItemRunner slowdown. Runtime is 1.1s on Sunny 0.7,
# mainly due to type inference. But the same code, compiled in a function from
# the terminal, is a small fraction of a second.
@testitem "Magnetization Observables" begin
using LinearAlgebra

Expand Down

0 comments on commit 769d60e

Please sign in to comment.