Skip to content

Commit

Permalink
Rename reshape_supercell and resize_supercell
Browse files Browse the repository at this point in the history
These replace `reshape_geometry` and `resize_periodically`.
  • Loading branch information
kbarros committed Jul 30, 2023
1 parent 48c0562 commit 5db4e25
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 36 deletions.
13 changes: 8 additions & 5 deletions docs/src/versions.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ Major refactors and enhancements to intensity calculations. This new interface
allows unification between LSWT and classical spin dynamics calculations. This
interface allows: Custom observables as local quantum operators, better support
for linebroadening, and automatic binning to facilitate comparison with
experimental data. See [`intensity_formula`](@ref) for documentation.

Experimental neturon scattering data can now be loaded using [`load_nxs`](@ref).
experimental data. See [`intensity_formula`](@ref) for documentation. Use
[`load_nxs`](@ref) to load experimental neutron scattering data.

**Breaking changes**.

Expand Down Expand Up @@ -40,6 +39,10 @@ Rename `polarize_spin!` to [`set_dipole!`](@ref) for consistency with
[`set_coherent!`](@ref). The behavior of the former function is unchanged: the
spin at a given site will still be polarized along the provided direction.

Rename `reshape_geometry` to [`reshape_supercell`](@ref), which is the
fundamental reshaping function. Rename `resize_periodically` to
[`resize_supercell`](@ref).

The constructor [`SpinInfo`](@ref) now requires a ``g``-factor or tensor as a
named argument.

Expand Down Expand Up @@ -156,8 +159,8 @@ interface, see the [Structure Factor Calculations](@ref) page.

* [`repeat_periodically`](@ref) replaces `extend_periodically`.

Additional related functions include [`resize_periodically`](@ref) and
[`reshape_geometry`](@ref), the latter being fundamental.
Additional related functions include `resize_periodically` and
`reshape_geometry`, the latter being fundamental.

* [`print_symmetry_table`](@ref) replaces `print_bond_table()`.

Expand Down
4 changes: 2 additions & 2 deletions examples/fei2_tutorial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,11 @@ print_wrapped_intensities(sys)

suggest_magnetic_supercell([[0, -1/4, 1/4]], sys.latsize)

# The function [`reshape_geometry`](@ref) allows an arbitrary reshaping of the
# The function [`reshape_supercell`](@ref) allows an arbitrary reshaping of the
# system. After selecting the supercell geometry, it becomes much easier to find
# the energy-minimizing spin configuration.

sys_supercell = reshape_geometry(sys, [1 0 0; 0 1 -2; 0 1 2])
sys_supercell = reshape_supercell(sys, [1 0 0; 0 1 -2; 0 1 2])

langevin.kT = 0
for i in 1:10_000
Expand Down
2 changes: 1 addition & 1 deletion src/Plotting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ function plot_spins(sys::System; linecolor=:grey, arrowcolor=:red,
)

if !isnothing(sys.origin)
sys_origin = resize_periodically(sys,sys.origin.latsize)
sys_origin = resize_supercell(sys,sys.origin.latsize)

# Translate to a correct lattice site in the middle of the bigger system
center_origin = (sys_origin.crystal.latvecs * Vec3(sys_origin.latsize))/2
Expand Down
18 changes: 9 additions & 9 deletions src/Reshaping.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

"""
reshape_geometry(sys::System, A)
reshape_supercell(sys::System, A)
Maps an existing [`System`](@ref) to a new one that has the shape and
periodicity of a requested supercell. The columns of the ``3×3`` integer matrix
Expand All @@ -14,13 +14,13 @@ operations will be unavailable for this system, e.g., setting interactions by
symmetry propagation. In practice, one can set all interactions using the
original system, and then reshape as a final step.
"""
function reshape_geometry(sys::System{N}, A) where N
function reshape_supercell(sys::System{N}, A) where N
# latsize for new system
new_latsize = NTuple{3, Int}(gcd.(eachcol(A)))
# Unit cell for new system, in units of original unit cell. Obtained by
# dividing each column of A by corresponding new_latsize component.
new_cell_size = Int.(A / diagm(collect(new_latsize)))
return reshape_geometry_aux(sys, new_latsize, new_cell_size)
return reshape_supercell_aux(sys, new_latsize, new_cell_size)
end


Expand All @@ -46,7 +46,7 @@ function set_interactions_from_origin!(sys::System{N}) where N
end


function reshape_geometry_aux(sys::System{N}, new_latsize::NTuple{3, Int}, new_cell_size::Matrix{Int}) where N
function reshape_supercell_aux(sys::System{N}, new_latsize::NTuple{3, Int}, new_cell_size::Matrix{Int}) where N
is_homogeneous(sys) || error("Cannot reshape system with inhomogeneous interactions.")

# `origin` describes the unit cell of the original system. For sequential
Expand Down Expand Up @@ -126,16 +126,16 @@ function cell_dimensions(sys)
end

"""
resize_periodically(sys::System{N}, latsize) where N
resize_supercell(sys::System{N}, latsize) where N
Creates a [`System`](@ref) identical to `sys` but enlarged to a given number of
unit cells in each lattice vector direction.
An error will be thrown if `sys` is incommensurate with `latsize`. Use
[`reshape_geometry`](@ref) instead to reduce the volume, or to perform an
[`reshape_supercell`](@ref) instead to reduce the volume, or to perform an
incommensurate reshaping.
"""
function resize_periodically(sys::System{N}, latsize::NTuple{3,Int}) where N
function resize_supercell(sys::System{N}, latsize::NTuple{3,Int}) where N
# Shape of the original system, in multiples of the original unit cell.
sysdims = cell_dimensions(sys) * diagm(collect(sys.latsize))
# Proposed system shape, given in fractional coordinates of original system
Expand All @@ -145,7 +145,7 @@ function resize_periodically(sys::System{N}, latsize::NTuple{3,Int}) where N
if norm(A - round.(A)) > 1e-12
error("Incommensurate system size.")
end
return reshape_geometry(sys, diagm(collect(latsize)))
return reshape_supercell(sys, diagm(collect(latsize)))
end

"""
Expand All @@ -158,7 +158,7 @@ function repeat_periodically(sys::System{N}, counts::NTuple{3,Int}) where N
counts = NTuple{3,Int}(counts)
@assert all(>=(1), counts)
# Scale each column by `counts` and reshape
return reshape_geometry_aux(sys, counts .* sys.latsize, Matrix(cell_dimensions(sys)))
return reshape_supercell_aux(sys, counts .* sys.latsize, Matrix(cell_dimensions(sys)))
end


Expand Down
2 changes: 1 addition & 1 deletion src/SpinWaveTheory/SpinWaveTheory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ end
function SpinWaveTheory(sys::System{N}; energy_ϵ::Float64=1e-8, energy_tol::Float64=1e-6) where N
# Reshape into single unit cell
cellsize_mag = cell_dimensions(sys) * diagm(collect(sys.latsize))
sys = reshape_geometry_aux(sys, (1,1,1), cellsize_mag)
sys = reshape_supercell_aux(sys, (1,1,1), cellsize_mag)

# Computes the Stevens operator in the local reference frame and the SO(3) rotation matrix from global to local frame
# (:dipole mode only)
Expand Down
2 changes: 1 addition & 1 deletion src/Sunny.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export SpinInfo, System, Site, all_sites, position_to_site,
symmetry_equivalent_bonds, set_exchange_at!, remove_periodicity!

include("Reshaping.jl")
export reshape_geometry, resize_periodically, repeat_periodically,
export reshape_supercell, resize_supercell, repeat_periodically,
print_wrapped_intensities, suggest_magnetic_supercell

include("Integrators.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/System/System.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ end
Converts a position `r` to four indices of a [`Site`](@ref). The coordinates of
`r` are given in units of the lattice vectors for the original crystal. This
function can be useful for working with systems that have been reshaped using
[`reshape_geometry`](@ref).
[`reshape_supercell`](@ref).
# Example
Expand Down
8 changes: 4 additions & 4 deletions test/test_lswt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ end

randomize_spins!(sys)
A = [1 1 1; -1 1 0; 0 0 1]
sys_swt = reshape_geometry(sys, A)
sys_swt = reshape_supercell(sys, A)

langevin.kT = 0
for i in 1:50_000
Expand Down Expand Up @@ -141,11 +141,11 @@ end
set_exchange!(sys_SUN, JL, Bond(1, 1, [1, 0, 0]); biquad=JQ)
set_exchange!(sys_dip, JL, Bond(1, 1, [1, 0, 0]); biquad=JQ)

sys_swt_SUN = reshape_geometry(sys_SUN, [1 1 1; -1 1 0; 0 0 1])
sys_swt_SUN = reshape_supercell(sys_SUN, [1 1 1; -1 1 0; 0 0 1])
set_dipole!(sys_swt_SUN, ( 1, 0, 0), position_to_site(sys_swt_SUN, (0, 0, 0)))
set_dipole!(sys_swt_SUN, (-1, 0, 0), position_to_site(sys_swt_SUN, (0, 1, 0)))

sys_swt_dip = reshape_geometry(sys_dip, [1 1 1; -1 1 0; 0 0 1])
sys_swt_dip = reshape_supercell(sys_dip, [1 1 1; -1 1 0; 0 0 1])
set_dipole!(sys_swt_dip, ( 1, 0, 0), position_to_site(sys_swt_dip, (0, 0, 0)))
set_dipole!(sys_swt_dip, (-1, 0, 0), position_to_site(sys_swt_dip, (0, 1, 0)))

Expand Down Expand Up @@ -198,7 +198,7 @@ end
set_onsite_coupling!(sys_dip, D*Sz^2, 1)

set_external_field!(sys_dip, [0, 0, h])
sys_swt_dip = reshape_geometry(sys_dip, [1 -1 0; 1 1 0; 0 0 1])
sys_swt_dip = reshape_supercell(sys_dip, [1 -1 0; 1 1 0; 0 0 1])
c₂ = 1 - 1/(2S)
θ = acos(h / (2S*(4J+D*c₂)))
set_dipole!(sys_swt_dip, ( sin(θ), 0, cos(θ)), position_to_site(sys_swt_dip, (0,0,0)))
Expand Down
24 changes: 12 additions & 12 deletions test/test_resize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@

A1 = [1 0 0; 0 2 0; 0 0 1]
A2 = [1 0 0; 1 2 0; 0 0 1]
newsys1 = reshape_geometry(sys, A1)
newsys2 = reshape_geometry(sys, A2)
newsys1 = reshape_supercell(sys, A1)
newsys2 = reshape_supercell(sys, A2)

@test energy(sys) / prod(sys.latsize) 2.55

newsys = reshape_geometry(sys, A1)
newsys = reshape_supercell(sys, A1)
@test energy(newsys) / prod(newsys.latsize) 2.55
newsys = reshape_geometry(sys, A2)
newsys = reshape_supercell(sys, A2)
@test energy(newsys) / prod(newsys.latsize) 2.55
newsys = reshape_geometry(sys, A1)
newsys = reshape_supercell(sys, A1)
@test energy(newsys) / prod(newsys.latsize) 2.55
end

Expand All @@ -59,15 +59,15 @@ end
randomize_spins!(sys)

# Reshape to sheared volume
sys2 = reshape_geometry(sys, [3 0 0; 2 3 0; 0 0 3])
sys2 = reshape_supercell(sys, [3 0 0; 2 3 0; 0 0 3])
# Reshape back to original volume
sys3 = reshape_geometry(sys2, diagm([3,3,3]))
sys3 = reshape_supercell(sys2, diagm([3,3,3]))
@test sys2.dipoles != sys.dipoles
@test sys3.dipoles == sys.dipoles

# Two equivalent ways of sizing up sys
sys2 = repeat_periodically(sys, (2, 2, 1))
sys3 = resize_periodically(sys, (6, 6, 3))
sys3 = resize_supercell(sys, (6, 6, 3))
@test sys2.dipoles == sys3.dipoles
end

Expand All @@ -80,7 +80,7 @@ end

# Commensurate shear that is specially designed to preserve the periodicity of
# the system volume
sys2 = reshape_geometry(sys, [3 3 0; 0 3 0; 0 0 3])
sys2 = reshape_supercell(sys, [3 3 0; 0 3 0; 0 0 3])

# Users always specify a bond using atom indices of the original unit cell,
# but `sys2.interactions_union` is internally reindexed.
Expand Down Expand Up @@ -111,7 +111,7 @@ end
0 latsize[2] 0
0 0 latsize[3]]
@test det(A) prod(latsize)
sys2 = reshape_geometry(sys, A)
sys2 = reshape_supercell(sys, A)
@test Sunny.natoms(sys2.crystal) == 2
@test energy(sys2) E0

Expand Down Expand Up @@ -195,8 +195,8 @@ end
sys.dipoles[:, j, k, 1] = circshift([s, s, -s, -s], mod(j+k, 4))
end

sys_supercell = reshape_geometry(sys, [2 0 1; -1 1 0; -1 -1 1])
sys2 = resize_periodically(sys_supercell, (4,4,4))
sys_supercell = reshape_supercell(sys, [2 0 1; -1 1 0; -1 -1 1])
sys2 = resize_supercell(sys_supercell, (4,4,4))

E0 = energy(sys) / length(sys.dipoles)
E1 = energy(sys_supercell) / length(sys_supercell.dipoles)
Expand Down

0 comments on commit 5db4e25

Please sign in to comment.