Skip to content

Commit

Permalink
allow some whitespace in parsing of SymOperations (fixes #29)
Browse files Browse the repository at this point in the history
  • Loading branch information
thchr committed Jun 23, 2021
1 parent 5a72fd5 commit 44b2b4e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Crystalline"
uuid = "ae5e2be0-a263-11e9-351e-f94dad1eb351"
authors = ["Thomas Christensen <tchr@mit.edu>"]
version = "0.3.9"
version = "0.3.10"

[deps]
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
Expand Down
27 changes: 20 additions & 7 deletions src/symops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,35 @@ function xyzt2components(s::AbstractString, ::Val{D}) where D
return SMatrix(W), SVector(w)
end


const IDX2XYZ = ('x', 'y', 'z')

@inline function xyzt2components!(W::MMatrix{D,D,T}, w::MVector{D,T},
xyzts::AbstractVector{<:AbstractString}) where {D,T<:Real}


chars = D == 3 ? ('x','y','z') : D == 2 ? ('x','y') : ('x',)
@inbounds for (i,s) in enumerate(xyzts)
# rotation/inversion/reflection part
firstidx = nextidx = firstindex(s)
while (idx = findnext(c -> c=='x' || c=='y' || c=='z', s, nextidx)) !== nothing
while (idx = findnext(c -> c chars, s, nextidx)) !== nothing
c = s[idx]
j = c=='x' ? 1 : (c=='y' ? 2 : 3)

previdx = prevind(s, idx)
if idx == firstidx || s[previdx] == '+'
if idx == firstidx
W[i,j] = one(T)
elseif s[previdx] == '-'
W[i,j] = -one(T)
else
previdx = prevind(s, idx)
while (c′=s[previdx]; isspace(s[previdx]))
previdx = prevind(s, previdx)
previdx firstidx && break
end
if c′ == '+' || isspace(c′)
W[i,j] = one(T)
elseif c′ == '-'
W[i,j] = -one(T)
else
throw(ArgumentError("failed to parse provided string representation"))
end
end
nextidx = nextind(s, idx)
end
Expand All @@ -131,7 +145,6 @@ end
return W, w
end

const IDX2XYZ = ('x', 'y', 'z')

function matrix2xyzt(O::AbstractMatrix{<:Real})
D = size(O,1)
Expand Down
13 changes: 13 additions & 0 deletions test/symops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ using Crystalline, Test
@test op == SymOperation(Matrix(op)) # Matrix
end

@testset "Parsing cornercases" begin
# allow some forms of whitespace in xyzt string parsing (issue #29)
@test SymOperation("x,y,z") == SymOperation("x, y, z")
@test SymOperation("x,-y,+z") == SymOperation(" x, - y, +z")
@test SymOperation("x,-y+x,+z-1/2") == SymOperation("x, - y + x, +z - 1/2")
@test SymOperation(" x,-y+x+1/3,+z-1/2") == SymOperation(" x, - y + x + 1/3, +z - 1/2")
end

@testset "Conversion between xyzt and matrix forms" begin
for D = 1:3
Dᵛ = Val(D)
Expand Down Expand Up @@ -104,6 +112,11 @@ using Crystalline, Test
@test_throws DomainError spacegroup(-1, 2)
@test_throws DomainError spacegroup(2, 0)
@test_throws DomainError spacegroup(41, 5)
@test_throws ArgumentError SymOperation{2}("x,z")
@test_throws ArgumentError SymOperation("x,z")
@test_throws ArgumentError SymOperation("x,÷z")
@test_throws ArgumentError SymOperation("x ,z") # don't allow spaces *after* entries
@test_throws DimensionMismatch SymOperation{3}("x,y+z")
end

@testset "Checking symmorphic space groups" begin
Expand Down

2 comments on commit 44b2b4e

@thchr
Copy link
Owner Author

@thchr thchr commented on 44b2b4e Jun 23, 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/39494

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.3.10 -m "<description of version>" 44b2b4e0f7987f3a74d059952df802b2b2c3d7e3
git push origin v0.3.10

Please sign in to comment.