Skip to content

Commit

Permalink
add an accessor setting to get the basis setting of coordinates; ch…
Browse files Browse the repository at this point in the history
…ange name of field `basisenum` to `setting`

- bump to v0.4.2
  • Loading branch information
thchr committed Jul 26, 2021
1 parent b12525b commit 8fba6af
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Brillouin"
uuid = "23470ee3-d0df-4052-8b1a-8cbd6363e7f0"
authors = ["Thomas Christensen <[email protected]> and contributors"]
version = "0.4.1"
version = "0.4.2"

[deps]
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Expand Down
23 changes: 18 additions & 5 deletions src/Brillouin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,28 @@ const SHOWDIGITS = 6
include("CrystallineBravaisVendor.jl")
# ---------------------------------------------------------------------------------------- #
@enum BasisEnum CARTESIAN LATTICE

"""
setting(x::Union{KPath, KPathInterpolant, Cell})
Return the basis setting of coordinates in `x`. The returned value is a member of the
[`BasisEnum`](@ref) enum with member values `LATTICE` (i.e. coordinates in the basis of the
lattice vectors) or `CARTESIAN` (i.e. coordinates in the Cartesian basis).
By default, methods in Brillouin will return coordinates in the `LATTICE` setting.
"""
setting(x) = x.setting[]

set_setting!(x, new_setting) = (x.setting[] = new_setting)

# ---------------------------------------------------------------------------------------- #
function latticize! end
latticize(v::AVec{<:Real}, basismatrix::AbstractMatrix{<:Real}) = basismatrix\v
latticize(v::AVec{<:Real}, basis::AVec{<:AVec{<:Real}}) = latticize(v, hcat(basis...))
latticize(x) = (x.basisenum[] === CARTESIAN ? latticize!(deepcopy(x)) : deepcopy(x))
latticize(x) = (setting(x) === CARTESIAN ? latticize!(deepcopy(x)) : deepcopy(x))

function cartesianize! end
cartesianize(v::AVec{<:Real}, basis::AVec{<:AVec{<:Real}}) = v'basis
cartesianize(x) = (x.basisenum[] === LATTICE ? cartesianize!(deepcopy(x)) : deepcopy(x))
cartesianize(x) = (setting(x) === LATTICE ? cartesianize!(deepcopy(x)) : deepcopy(x))

"""
basis(x::Union{KPath, KPathInterpolant, Cell})
Expand All @@ -30,17 +43,17 @@ Return the (reciprocal or direct) lattice basis associated with `x`, in Cartesia
coordinates.
Methods in Brillouin will by default return points in the lattice basis, i.e., points are
referred to `basis(x)`. This corresponds to the setting `x.basisenum[] == LATTICE`.
referred to `basis(x)`. This corresponds to the `setting(x) == LATTICE`.
Coordinates may instead be referred to a Cartesian basis, corresponding to
`x.basisenum[] == CARTESIAN` by using [`cartesianize`](@ref). The result of `basis(x)`,
`setting(x) == CARTESIAN` by using [`cartesianize`](@ref). The result of `basis(x)`,
however, is invariant to this and always refers to the lattice basis in Cartesian
coordinates.
"""
basis(x) = x.basis

export latticize!, latticize,
cartesianize!, cartesianize,
basis
basis, setting
# ---------------------------------------------------------------------------------------- #

# MAIN FUNCTIONALITY
Expand Down
18 changes: 9 additions & 9 deletions src/KPaths.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using ..Brillouin:
AVec,
BasisLike,
SHOWDIGITS,
BasisEnum, CARTESIAN, LATTICE,
BasisEnum, CARTESIAN, LATTICE, setting, set_setting!,
cartesianize, latticize
import ..Brillouin:
latticize!, cartesianize!,
Expand All @@ -43,10 +43,10 @@ $(TYPEDEF)
$(TYPEDFIELDS)
"""
struct KPath{D} <: AbstractPath{Pair{Symbol, SVector{D, Float64}}}
points :: Dict{Symbol, SVector{D,Float64}}
paths :: Vector{Vector{Symbol}}
basis :: SVector{D, SVector{D, Float64}}
basisenum :: Ref{BasisEnum}
points :: Dict{Symbol, SVector{D,Float64}}
paths :: Vector{Vector{Symbol}}
basis :: SVector{D, SVector{D, Float64}}
setting :: Ref{BasisEnum}
end

"""
Expand Down Expand Up @@ -219,11 +219,11 @@ reciprocal basis vectors `basis`.
Modifies `kp` in-place.
"""
function cartesianize!(kp::KPath{D}) where D
kp.basisenum[] === CARTESIAN && return kp
setting(kp) === CARTESIAN && return kp
for (lab, kv) in points(kp)
@inbounds points(kp)[lab] = cartesianize(kv, kp.basis)
end
kp.basisenum[] = CARTESIAN
set_setting!(kp, CARTESIAN)
return kp
end

Expand All @@ -235,12 +235,12 @@ reciprocal lattice vectors `basis`.
Modifies `kp` in-place.
"""
function latticize!(kp::KPath{D}) where D
kp.basisenum[] === LATTICE && return kp
setting(kp) === LATTICE && return kp
basismatrix = hcat(kp.basis...)
for (lab, kv) in points(kp)
@inbounds points(kp)[lab] = latticize(kv, basismatrix)
end
kp.basisenum[] = LATTICE
set_setting!(kp, LATTICE)
return kp
end

Expand Down
18 changes: 9 additions & 9 deletions src/WignerSeitz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module WignerSeitz
using ..Brillouin:
AVec,
SHOWDIGITS,
BasisEnum, CARTESIAN, LATTICE,
BasisEnum, CARTESIAN, LATTICE, setting, set_setting!,
cartesianize, latticize
import ..Brillouin:
latticize!, cartesianize!,
Expand Down Expand Up @@ -37,10 +37,10 @@ $(TYPEDEF)
$(TYPEDFIELDS)
"""
struct Cell{D} <: AVec{Vector{SVector{D, Float64}}} # over of polygons
verts :: Vector{SVector{D, Float64}}
faces :: Vector{Vector{Int}}
basis :: SVector{D, SVector{D, Float64}}
basisenum :: Ref{BasisEnum} # internal field
verts :: Vector{SVector{D, Float64}}
faces :: Vector{Vector{Int}}
basis :: SVector{D, SVector{D, Float64}}
setting :: Ref{BasisEnum} # internal field
end

faces(c::Cell) = c.faces
Expand Down Expand Up @@ -313,16 +313,16 @@ end


function latticize!(c::Cell)
c.basisenum[] === LATTICE && return c
setting(c) === LATTICE && return c
basismatrix = hcat(c.basis...)
c.verts .= latticize.(c.verts, Ref(basismatrix))
c.basisenum[] = LATTICE
set_setting!(c, LATTICE)
return c
end
function cartesianize!(c::Cell)
c.basisenum[] === CARTESIAN && return c
setting(c) === CARTESIAN && return c
c.verts .= cartesianize.(c.verts, Ref(c.basis))
c.basisenum[] = CARTESIAN
set_setting!(c, CARTESIAN)
return c
end

Expand Down
26 changes: 13 additions & 13 deletions src/interpolate-paths.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ $(TYPEDEF)
$(TYPEDFIELDS)
"""
struct KPathInterpolant{D} <: AbstractPath{SVector{D, Float64}}
kpaths :: Vector{Vector{SVector{D, Float64}}}
labels :: Vector{Dict{Int, Symbol}}
basis :: SVector{D, SVector{D, Float64}}
basisenum :: Ref{BasisEnum}
kpaths :: Vector{Vector{SVector{D, Float64}}}
labels :: Vector{Dict{Int, Symbol}}
basis :: SVector{D, SVector{D, Float64}}
setting :: Ref{BasisEnum}
end

size(kpi::KPathInterpolant) = (sum(length, kpi.kpaths),)
Expand Down Expand Up @@ -63,11 +63,11 @@ Transform an interpolated **k**-path `kpi` in a lattice basis to a Cartesian bas
Modifies `kpi` in-place.
"""
function cartesianize!(kpi::KPathInterpolant)
kpi.basisenum[] === CARTESIAN && return kp
setting(kpi) === CARTESIAN && return kp
for i in eachindex(kpi)
@inbounds kpi[i] = cartesianize(kpi[i], kpi.basis)
end
kpi.basisenum[] = CARTESIAN
set_setting!(kpi, CARTESIAN)
return kpi
end

Expand All @@ -79,12 +79,12 @@ Transform an interpolated **k**-path `kpi` in a Cartesian basis to a lattice bas
Modifies `kpi` in-place.
"""
function latticize!(kpi::KPathInterpolant)
kpi.basisenum[] === LATTICE && return kp
setting(kpi) === LATTICE && return kp
basismatrix = hcat(kpi.basis...)
for i in eachindex(kpi)
@inbounds kpi[i] = latticize(kpi[i], basismatrix)
end
kpi.basisenum[] = LATTICE
set_setting!(kpi, LATTICE)
return kpi
end

Expand All @@ -105,7 +105,7 @@ a desired density per unit (reciprocal) length can be specified via the keyword
See also [`interpolate(::AbstractVector{::AbstractVector{<:Real}}, ::Integer)`](@ref).
"""
function interpolate(kp::KPath{D}, N::Integer) where D
kpᶜ = kp.basisenum[] === CARTESIAN ? kp : cartesianize(kp)
kpᶜ = setting(kp) === CARTESIAN ? kp : cartesianize(kp)
distss = map(paths(kpᶜ)) do path
map(1:length(path)-1) do i
norm(points(kpᶜ)[path[i]] - points(kpᶜ)[path[i+1]])
Expand All @@ -126,7 +126,7 @@ function interpolate(kp::KPath{D}, N::Integer) where D
push!(kipaths[j], points(kp)[last(path)])
end

return KPathInterpolant(kipaths, labels, basis(kp), kp.basisenum)
return KPathInterpolant(kipaths, labels, basis(kp), Ref(setting(kp)))
end

function interpolate(kp::KPath;
Expand All @@ -146,7 +146,7 @@ function interpolate(kp::KPath;
end

function interpolate_via_density(kp::KPath{D}, density::Real) where D
kpᶜ = kp.basisenum[] === CARTESIAN ? kp : cartesianize(kp)
kpᶜ = setting(kp) === CARTESIAN ? kp : cartesianize(kp)

kipaths = [Vector{SVector{D, Float64}}() for _ in 1:length(paths(kp))]
labels = [Dict{Int, Symbol}() for _ in 1:length(paths(kp))]
Expand All @@ -173,7 +173,7 @@ function interpolate_via_density(kp::KPath{D}, density::Real) where D
push!(kipaths[j], points(kp)[last(path)]) # add previously omitted end point
end

return KPathInterpolant(kipaths, labels, basis(kp), kp.basisenum)
return KPathInterpolant(kipaths, labels, basis(kp), Ref(setting(kp)))
end


Expand All @@ -198,7 +198,7 @@ function splice(kp::KPath{D}, N::Integer) where D
push!(kipaths[j], points(kp)[last(path)])
end

return KPathInterpolant(kipaths, labels, kp.basis, kp.basisenum)
return KPathInterpolant(kipaths, labels, kp.basis, Ref(setting(kp)))
end

# ---------------------------------------------------------------------------------------- #
Expand Down
4 changes: 2 additions & 2 deletions src/requires/plotlyjs_kpaths.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const KPATH_COL = Ref("rgb(95,39,205)") # "nasu purple" (https://flatuicolors.co

function plot(kp::KPath{3}, layout::Layout=DEFAULT_PLOTLY_LAYOUT_3D;
config::PlotConfig = PlotConfig(responsive=true, displaylogo=false))
kp.basisenum[] !== CARTESIAN && (kp = cartesianize(kp))
setting(kp) !== CARTESIAN && (kp = cartesianize(kp))

tpaths = Vector{GenericTrace{Dict{Symbol,Any}}}(undef, length(paths(kp)))
for (i,path) in enumerate(paths(kp))
Expand All @@ -33,7 +33,7 @@ end
# ---------------------------------------------------------------------------------------- #
function plot(kp::KPath{2}, layout::Layout=DEFAULT_PLOTLY_LAYOUT_2D;
config::PlotConfig = PlotConfig(responsive=true, displaylogo=false))
kp.basisenum[] !== CARTESIAN && (kp = cartesianize(kp))
setting(kp) !== CARTESIAN && (kp = cartesianize(kp))

tpaths = Vector{GenericTrace{Dict{Symbol,Any}}}(undef, length(paths(kp)))
for (i,path) in enumerate(paths(kp))
Expand Down
4 changes: 2 additions & 2 deletions src/requires/plotlyjs_wignerseitz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const AXIS_LIGHT_COL = Ref("rgb(242,215,208)") # 20% AXIS_COL, 80% white
function plot(c::Cell{3}, layout::Layout=DEFAULT_PLOTLY_LAYOUT_3D;
config::PlotConfig = PlotConfig(responsive=true, displaylogo=false))

c.basisenum[] !== CARTESIAN && (c = cartesianize(c))
setting(c) !== CARTESIAN && (c = cartesianize(c))
scale = maximum(norm, basis(c))

# BZ
Expand Down Expand Up @@ -151,7 +151,7 @@ function plot(c::Cell{2}, layout::Layout=DEFAULT_PLOTLY_LAYOUT_2D;
config::PlotConfig = PlotConfig(responsive=true, displaylogo=false))
layout = deepcopy(layout) # because we have to mutate to get arrows...

c.basisenum[] !== CARTESIAN && (c = cartesianize(c))
setting(c) !== CARTESIAN && (c = cartesianize(c))

scale = maximum(norm, basis(c))
max_x, max_y = maximum(v->abs(v[1]), basis(c)), maximum(v->abs(v[2]), basis(c))
Expand Down

2 comments on commit 8fba6af

@thchr
Copy link
Owner Author

@thchr thchr commented on 8fba6af Jul 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/41544

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.2 -m "<description of version>" 8fba6aff9ccf8e8556d22e62ee4084dd8f555cdb
git push origin v0.4.2

Please sign in to comment.