From e0559eeed08bcc86f8b7844f5b5d176dce42b03c Mon Sep 17 00:00:00 2001 From: Mayeul d'Avezac Date: Sun, 9 Jul 2017 14:04:22 +0100 Subject: [PATCH] Update to julia 0.6 Mostly broadcast stuff, and some changes forthe sake of Lint.jl --- REQUIRE | 4 +-- src/Gruber.jl | 47 ++++++++++++++++---------------- src/SNF.jl | 28 +++++++++---------- src/SpaceGroup.jl | 7 ++--- src/Structures.jl | 41 +++++++++++++++------------- src/utilities.jl | 43 +++++++++++++++-------------- test/Crystal.jl | 38 +++++++++++++++----------- test/Gruber.jl | 8 +++--- test/SpaceGroup.jl | 8 +++--- test/utilities.jl | 68 +++++++++++++++++++++++----------------------- 10 files changed, 150 insertions(+), 142 deletions(-) diff --git a/REQUIRE b/REQUIRE index ea04be9..cf5faae 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,5 +1,5 @@ -julia 0.5 -Unitful 0.0.4 +julia 0.6 +Unitful DataFrames CoordinateTransformations Lumberjack diff --git a/src/Gruber.jl b/src/Gruber.jl index aa75e5a..9f7c358 100644 --- a/src/Gruber.jl +++ b/src/Gruber.jl @@ -6,12 +6,11 @@ using Crystals.Utilities: cell_parameters using Crystals.Structures: Crystal, volume using Crystals: Log using Unitful: unit, ustrip, Quantity -const max_no_change = 2 function no_opt_change_test(new, last) const m_new = 16e0 * new; - const diff = new - last; - const m_new_plus_diff = m_new + diff; + const difference = new - last; + const m_new_plus_diff = m_new + difference; const m_new_plus_diff_minus_m_new = m_new_plus_diff - m_new; m_new_plus_diff_minus_m_new != 0; end @@ -31,8 +30,8 @@ function def_test(args; tolerance=default_tolerance) end function def_gt_0(args...; tolerance=default_tolerance) - zero, positive = def_test(args; tolerance=tolerance) - positive == 3 || (zero == 0 && positive == 1) + z₀, positive = def_test(args; tolerance=tolerance) + positive == 3 || (z₀ == 0 && positive == 1) end @@ -53,7 +52,7 @@ function n3_action(params::Vector, rinv::Matrix; tolerance=default_tolerance) const j = params[5] ≤ -tolerance ? -1 : 1 const k = params[6] ≤ -tolerance ? -1 : 1 rinv[:, :] = rinv * [i 0 0; 0 j 0; 0 0 k] - params[4:end] = abs(params[4:end]) + params[4:end] = abs.(params[4:end]) end function n4_action(params::Vector, rinv::Matrix; tolerance=default_tolerance) @@ -73,31 +72,31 @@ function n4_action(params::Vector, rinv::Matrix; tolerance=default_tolerance) end end rinv[:, :] = rinv * update - params[4:end] = -abs(params[4:end]) + params[4:end] = -abs.(params[4:end]) end function n5_action(params::Vector, rinv::Matrix; tolerance=default_tolerance) - const sign = params[4] > tolerance ? -1 : 1 - rinv[:, :] = rinv * [1 0 0; 0 1 sign; 0 0 1] - params[3] += params[2] + sign * params[4] - params[4] += 2sign * params[2] - params[5] += sign * params[6] + const pos_or_neg = params[4] > tolerance ? -1 : 1 + rinv[:, :] = rinv * [1 0 0; 0 1 pos_or_neg; 0 0 1] + params[3] += params[2] + pos_or_neg * params[4] + params[4] += 2pos_or_neg * params[2] + params[5] += pos_or_neg * params[6] end function n6_action(params::Vector, rinv::Matrix; tolerance=default_tolerance) - const sign = params[5] > tolerance ? -1 : 1 - rinv[:, :] = rinv * [1 0 sign; 0 1 0; 0 0 1] - params[3] += params[1] + sign * params[5] - params[4] += sign * params[6] - params[5] += 2sign * params[1] + const pos_or_neg = params[5] > tolerance ? -1 : 1 + rinv[:, :] = rinv * [1 0 pos_or_neg; 0 1 0; 0 0 1] + params[3] += params[1] + pos_or_neg * params[5] + params[4] += pos_or_neg * params[6] + params[5] += 2pos_or_neg * params[1] end function n7_action(params::Vector, rinv::Matrix; tolerance=default_tolerance) - const sign = params[6] > tolerance ? -1 : 1 - rinv[:, :] = rinv * [1 sign 0; 0 1 0; 0 0 1] - params[2] += params[1] + sign * params[6] - params[4] += sign * params[5] - params[6] += 2sign * params[1] + const pos_or_neg = params[6] > tolerance ? -1 : 1 + rinv[:, :] = rinv * [1 pos_or_neg 0; 0 1 0; 0 0 1] + params[2] += params[1] + pos_or_neg * params[6] + params[4] += pos_or_neg * params[5] + params[6] += 2pos_or_neg * params[1] end function n8_action(params::Vector, rinv::Matrix) @@ -145,7 +144,7 @@ function gruber{T <: Number}(cell::Matrix{T}; const metric = transpose(cell) * cell params = vcat(diag(metric), [2metric[2, 3], 2metric[1, 3], 2metric[1, 2]]) rinv = eye(size(metric, 1)) - nochange, previous = 0, -params[1:3] + no_change, previous = 0, -params[1:3] iteration::Int = 0 for iteration in 1:itermax condition0 = @@ -160,7 +159,7 @@ function gruber{T <: Number}(cell::Matrix{T}; n3_action(params, rinv; tolerance=ε) else n4_action(params, rinv; tolerance=ε) - if all(abs(previous - params[1:3]) .< ε) + if all(abs.(previous .- params[1:3]) .< ε) no_change += 1 else no_change = 0 diff --git a/src/SNF.jl b/src/SNF.jl index 72d70b8..7755b03 100644 --- a/src/SNF.jl +++ b/src/SNF.jl @@ -3,16 +3,16 @@ using Unitful: Quantity, ustrip export smith_normal_form function choose_pivot!{T <: Integer}(left::Matrix{T}, smith::Matrix{T}, - step::Integer, index::Integer=1) + istep::Integer, index::Integer=1) index > size(smith, 2) && return 0 while all(smith[:, index] .== 0) index += 1 index ≤ size(smith, 2) || return 0; end - if smith[step, index] == 0 + if smith[istep, index] == 0 k = findfirst(x -> x ≠ 0, smith[:, index]) - left[step, :], left[k, :] = deepcopy(left[k, :]), deepcopy(left[step, :]) - smith[step, :], smith[k, :] = deepcopy(smith[k, :]), deepcopy(smith[step, :]) + left[istep, :], left[k, :] = deepcopy(left[k, :]), deepcopy(left[istep, :]) + smith[istep, :], smith[k, :] = deepcopy(smith[k, :]), deepcopy(smith[istep, :]) end index end @@ -25,11 +25,11 @@ function improve_col_pivot!{T <: Integer}(left::Matrix{T}, smith::Matrix{T}, β, σ, τ = gcdx(smith[row, col], smith[k, col]) α = smith[row, col] / β - γ = smith[k, col] / β + γₒ = smith[k, col] / β Lp = eye(T, size(left, 1)) Lp[row, [row, k]] = [σ, τ] - Lp[k, [row, k]] = [-γ, α] + Lp[k, [row, k]] = [-γₒ, α] left[:] = Lp * left smith[:] = Lp * smith @@ -44,11 +44,11 @@ function improve_row_pivot!{T <: Integer}(smith::Matrix{T}, right::Matrix{T}, β, σ, τ = gcdx(smith[row, col], smith[row, k]) α = smith[row, col] / β - γ = smith[row, k] / β + γ₀ = smith[row, k] / β Rp = eye(T, size(right, 1)) Rp[[col, k], col] = [σ, τ] - Rp[[col, k], k] = [-γ, α] + Rp[[col, k], k] = [-γ₀, α] right[:] = right * Rp smith[:] = smith * Rp @@ -81,14 +81,14 @@ end function diagonalize_all_entries!{T <: Integer}(left::Matrix{T}, smith::Matrix{T}, right::Matrix{T}) @assert size(smith, 1) == size(smith, 2) - step = 1 - col = choose_pivot!(left, smith, step) + istep = 1 + col = choose_pivot!(left, smith, istep) while 0 < col ≤ size(smith, 2) - diagonalize_at_entry!(left, smith, right, step, col) + diagonalize_at_entry!(left, smith, right, istep, col) - step += 1 + istep += 1 col += 1 - col = choose_pivot!(left, smith, step, col) + col = choose_pivot!(left, smith, istep, col) end return smith, left, right end @@ -126,7 +126,7 @@ function smith_normal_form{T <: Integer}(matrix::Matrix{T}) move_zero_entries!(smith, right) make_divisible!(left, smith, right) left = convert(Matrix{T}, diagm([u ≥ 0 ? 1: -1 for u in diag(smith)])) * left - smith = abs(smith) + smith = abs.(smith) smith, left, right end diff --git a/src/SpaceGroup.jl b/src/SpaceGroup.jl index bd87874..ff75b63 100644 --- a/src/SpaceGroup.jl +++ b/src/SpaceGroup.jl @@ -55,7 +55,6 @@ Implementation taken from [ENUM](http://enum.sourceforge.net/). function point_group{T <: Number}(cell::AbstractMatrix{T}; tolerance::Real=default_tolerance) @assert size(cell, 1) == size(cell, 2) - const ndims = size(cell, 1) avecs, bvecs, cvecs = potential_equivalents(cell, tolerance=tolerance) result = Matrix{T}[eye(cell)] @@ -112,7 +111,7 @@ function inner_translations_impl(fractional::AbstractMatrix, species[site] ≠ atom_type && continue translation = into_voronoi(fractional[:, site] - atom_center, grubcell) - all(abs(translation) .< tolerance) && continue + all(abs.(translation) .< tolerance) && continue is_mapping = true for mapping ∈ eachindex(species) @@ -263,7 +262,7 @@ function primitive_impl(cell::AbstractMatrix, det(ustrip(trial)) < 0e0 && Log.error("Negative volume") int_cell = inv(trial) * cell - all(abs(int_cell - round(Integer, int_cell) .< 1e-8)) || continue + all(abs.(int_cell - round.(Integer, int_cell) .< 1e-8)) || continue new_cell = trial V = volume(trial) @@ -344,7 +343,7 @@ function space_group_impl(cell::AbstractMatrix, Log.info( "$(length(result)) symmetry operations found, with " * "$(count(result) do op - all(abs(ustrip(op(zeros(eltype(cartesian), size(translations, 1))))) .< 1e-8) + all(abs.(ustrip(op(zeros(eltype(cartesian), size(translations, 1))))) .< 1e-8) end) " * "pure symmetries." ) [u for u in result] diff --git a/src/Structures.jl b/src/Structures.jl index 744a371..dee3373 100644 --- a/src/Structures.jl +++ b/src/Structures.jl @@ -16,9 +16,9 @@ using Unitful: dimension, unit const RESERVED_COLUMNS = [:position, :fractional, :cartesian, :x, :y, :z] """ Top type node for Crystals """ -abstract AbstractCrystal +abstract type AbstractCrystal end -typealias RowIndices{T <: Integer} Union{T, AbstractVector{T}, Range{T}, Colon} +RowIndices{T <: Integer} = Union{T, AbstractVector{T}, Range{T}, Colon} """ Describe a crystalline structure """ type Crystal{T <: Number, D, U, P <: Number} <: AbstractCrystal @@ -65,8 +65,8 @@ function Crystal{C, D, U, P <: Number}(cell::Matrix{Quantity{C, D, U}}, end function Crystal{C, D, U}(cell::Matrix{Quantity{C, D, U}}; kwargs...) - const dpositions = filter(x -> x[1] ∈ (:position, :positions), kwargs) - const tpositions = filter(x -> x[1] ∈ (:tposition, :tpositions), kwargs) + const dpositions = collect(filter(x -> x[1] ∈ (:position, :positions), kwargs)) + const tpositions = collect(filter(x -> x[1] ∈ (:tposition, :tpositions), kwargs)) if length(dpositions) ≠ 0 && length(tpositions) ≠ 0 const positions = hcat((x[2] for x in dpositions)..., transpose(hcat([x[2] for x in tpositions]...))) @@ -75,12 +75,11 @@ function Crystal{C, D, U}(cell::Matrix{Quantity{C, D, U}}; kwargs...) elseif length(tpositions) ≠ 0 const positions = transpose(hcat([x[2] for x in tpositions]...)) else - const positions = reshape(Matrix{Quantity{C, D, U}}(), - (size(cell, 1), 0)) + const positions = Matrix{Quantity{C, D, U}}(size(cell, 1), 0) end - filter!(x -> x[1] ∉ (:position, :positions, :tposition, :tpositions), kwargs) - Crystal(cell, positions; kwargs...) + leftover = filter(x -> x[1] ∉ (:position, :positions, :tposition, :tpositions), kwargs) + Crystal(cell, positions; leftover...) end """ Underlying physical units of the crystal """ @@ -128,17 +127,17 @@ volume(crystal::Crystal) = volume(crystal.cell) function are_compatible_lattices(cell_a::Matrix, cell_b::Matrix; digits=12, rtol=default_tolerance, atol=default_tolerance) - isinteger(round(inv(cell_a) * cell_b, digits)) && + all(isinteger.(round.(inv(cell_a) * cell_b, digits))) && isapprox(ustrip(volume(cell_a)), ustrip(volume(cell_b)); rtol=rtol, atol=atol) end function are_compatible_lattices(crystal::Crystal, cell::Matrix; kwargs...) - are_compatible_lattices(crystal.cell, cell) + are_compatible_lattices(crystal.cell, cell; kwargs...) end function are_compatible_lattices(crystal_a::Crystal, crystal_b::Crystal; kwargs...) - are_compatible_lattices(crystal_a.cell, crystal_b.cell) + are_compatible_lattices(crystal_a.cell, crystal_b.cell; kwargs...) end function are_compatible_lattices(cell::Matrix, crystal::Crystal; kwargs...) - are_compatible_lattices(cell, crystal.cell) + are_compatible_lattices(cell, crystal.cell; kwargs...) end """ are_compatible_lattices(lattices...) @@ -201,7 +200,7 @@ end position_for_crystal(::Val{:fractional}, ::AbstractMatrix, pos::AbstractArray) = pos position_for_crystal(::Val{:cartesian}, cell::AbstractMatrix, p::AbstractArray) = cell * p function position_for_crystal{T <: Quantity}(::Val{:cartesian}, - cell::AbstractMatrix, + ::AbstractMatrix, positions::AbstractArray{T}) positions end @@ -383,7 +382,7 @@ function Base.getindex(crystal::Crystal, ::Colon, symbol::Symbol) end Base.getindex(crystal::Crystal, ::Colon, ::Colon) = copy(crystal) -Base.getindex(crystal::Crystal, row::Any, col::Colon) = Base.getindex(crystal, row) +Base.getindex(crystal::Crystal, row::Any, ::Colon) = Base.getindex(crystal, row) function Base.getindex(crystal::Crystal, index::RowIndices, symbols::AbstractVector{Symbol}) const specials = symbols ∩ RESERVED_COLUMNS @@ -428,7 +427,7 @@ end Base.names(crystal::Crystal) = push!(names(crystal.properties), :position) Base.size(crystal::Crystal) = (r = size(crystal.properties); (r[1], r[2] + 1)) Base.size(crystal::Crystal, i::Integer) = size(crystal)[i] -Base.ndims(crystal::Crystal) = 2 +Base.ndims(::Crystal) = 2 DataFrames.nrow(crystal::Crystal) = size(crystal.positions, 2) DataFrames.ncol(crystal::Crystal) = ncol(crystal.properties) + 1 Base.endof(crystal::Crystal) = nrow(crystal) @@ -588,6 +587,10 @@ function DataFrames.deleterows!(crystal::Crystal, row::Integer) crystal.positions = crystal.positions[:, rows] end +function DataFrames.deleterows!(crystal::Crystal, ::Colon) + deleterows!(crystal, 1:length(crystal)) +end + function DataFrames.deleterows!(crystal::Crystal, rows::RowIndices) deleterows!(crystal.properties, rows) prows = filter(i -> i ∉ rows, 1:nrow(crystal)) @@ -644,15 +647,15 @@ Rounds the cell and positions of a crystal. See `round` for possible parameters. """ function round!{T, D, U, TT, DD, UU}(crystal::Crystal{T, D, U, Quantity{TT, DD, UU}}, args...) - crystal.cell = round(reinterpret(T, crystal.cell), args...) * unit(Quantity{T, D, U}) + crystal.cell = round.(reinterpret(T, crystal.cell), args...) * unit(Quantity{T, D, U}) const punit = unit(Quantity{TT, DD, UU}) - crystal[:position] = round(reinterpret(TT, crystal[:position]), args...) * punit + crystal[:position] = round.(reinterpret(TT, crystal[:position]), args...) * punit crystal end function round!{T, D, U, TT}(crystal::Crystal{T, D, U, TT}, args...) - crystal.cell = round(reinterpret(T, crystal.cell), args...) * unit(Quantity{T, D, U}) - crystal[:position] = round(crystal[:position], args...) + crystal.cell = round.(reinterpret(T, crystal.cell), args...) * unit(Quantity{T, D, U}) + crystal[:position] = round.(crystal[:position], args...) crystal end diff --git a/src/utilities.jl b/src/utilities.jl index 864d7d4..b3d2e69 100644 --- a/src/utilities.jl +++ b/src/utilities.jl @@ -11,6 +11,7 @@ using NamedTuples: @NT using Unitful using Unitful: Dimensions, NoUnits using DocStringExtensions +using Base.Iterators: cycle, take """ eldimension(u::Any) @@ -29,7 +30,7 @@ is_unitful{T <: Number}(a::Type{T}) = Val{:unitless}() is_unitful{T, D, U}(a::Type{Quantity{T, D, U}}) = Val{:unitful}() """ Tuple holding Hart-Forcade transform """ -typealias HartForcadeTransform @NT(transform::Matrix, quotient::Vector) +const HartForcadeTransform = @NT(transform::Matrix, quotient::Vector) """ $(SIGNATURES) @@ -76,7 +77,7 @@ Positions in supercell: ``` """ function hart_forcade(lattice::AbstractMatrix, supercell::AbstractMatrix; digits::Integer=8) - fractional = convert(Matrix{Int64}, round(inv(lattice) * supercell, digits)) + fractional = convert(Matrix{Int64}, round.(inv(lattice) * supercell, digits)) snf, left, right = smith_normal_form(fractional) @@ -163,8 +164,8 @@ True if the positions are one-to-one periodic with respect to the input cell. """ function is_periodic(a::AbstractVector, b::AbstractVector, cell::AbstractMatrix; tolerance::Real=default_tolerance) - const result = abs( - mod(to_fractional(a, cell) .- to_fractional(b, cell) + 0.5, 1) - 0.5 + const result = abs.( + mod.(to_fractional(a, cell) .- to_fractional(b, cell) + 0.5, 1) - 0.5 ) .< tolerance all(result) end @@ -179,8 +180,8 @@ function is_periodic{T, D, U}(a::AbstractMatrix, b::AbstractVector, cell::AbstractMatrix{Quantity{T, D, U}}; tolerance::Real=default_tolerance) - const result = abs( - mod(to_fractional(a, cell) .- to_fractional(b, cell) + 0.5, 1) - 0.5 + const result = abs.( + mod.(to_fractional(a, cell) .- to_fractional(b, cell) + 0.5, 1) - 0.5 ) .< tolerance all(result, 1) end @@ -194,8 +195,8 @@ function is_periodic{T, D, U}(a::AbstractMatrix, b::AbstractMatrix, cell::AbstractMatrix{Quantity{T, D, U}}; tolerance::Real=default_tolerance) - const result = abs( - mod(to_fractional(a, cell) .- to_fractional(b, cell) + 0.5, 1) - 0.5 + const result = abs.( + mod.(to_fractional(a, cell) .- to_fractional(b, cell) + 0.5, 1) - 0.5 ) .< tolerance all(result, 1) end @@ -207,7 +208,7 @@ Folds periodic positions into cell """ function into_cell(pos::AbstractArray, cell::AbstractMatrix; tolerance::Real=default_tolerance) - frac = mod(to_fractional(pos, cell), 1) + frac = mod.(to_fractional(pos, cell), 1) if tolerance > 0 for i in 1:length(frac) if abs(frac[i] - 1e0) < tolerance @@ -227,7 +228,7 @@ Folds positions back to origin, such that each fractional component ``x_f`` is b result is also in Cartesian (fractional) coordinates. """ function origin_centered(pos::AbstractArray, cell::AbstractMatrix) - to_same_kind(mod(to_fractional(pos, cell) .+ 0.5, -1) .+ 0.5, pos, cell) + to_same_kind(mod.(to_fractional(pos, cell) .+ 0.5, -1) .+ 0.5, pos, cell) end """ @@ -241,7 +242,7 @@ the cell is quite pathological, then the result will not be within the Voronoi c function into_voronoi(pos::AbstractArray, cell::AbstractMatrix; extent::Integer=1) zcentered = to_cartesian(origin_centered(pos, cell), cell) result = deepcopy(zcentered) - norms = [norm(zcentered[:, i]) for i in 1:size(zcentered, 2)] + norms = [norm(zcentered[:, z]) for z in 1:size(zcentered, 2)] for n in eachindex(norms) for i = -extent:extent, j = -extent:extent, k = -extent:extent translation = cell * [i, j, k] @@ -296,27 +297,27 @@ function supercell(lattice::Crystal, supercell::AbstractMatrix; end """ Tuple holding cell parameters """ -typealias CellParameters @NT(a, b, c, α, β, γ) +const CellParameters = @NT(a, b, c, α, β, γ) """ cell_parameters(a::Quantity, b::Quantity, c::Quantity, α::Quantity=(π/2)u"rad", β::Quantity=(π/2)u"rad", - γ::Quantity=(π/2)u"rad") + γₒ::Quantity=(π/2)u"rad") Computes the cell matrix from the cell parameters [a, b, c, α, β, γ]. """ function cell_parameters(a::Quantity, b::Quantity, c::Quantity, α::Quantity=(π/2)u"rad", β::Quantity=(π/2)u"rad", - γ::Quantity=(π/2)u"rad") + γₒ::Quantity=(π/2)u"rad") cx = cos(β) - cy = (cos(α) - cos(β)cos(γ))/sin(γ) + cy = (cos(α) - cos(β)cos(γₒ))/sin(γₒ) units = unit(a) bb = uconvert(unit(a), b) cc = uconvert(unit(a), c) - zero = typeof(a)(0) - [a bb * cos(γ) cc * cx; - zero bb * sin(γ) cc * cy; - zero zero cc * √(1 - cx * cx - cy * cy)] + z₀ = typeof(a)(0) + [a bb * cos(γₒ) cc * cx; + z₀ bb * sin(γₒ) cc * cy; + z₀ z₀ cc * √(1 - cx * cx - cy * cy)] end cell_parameters(cp::CellParameters) = cell_parameters(cp...) @@ -331,8 +332,8 @@ function cell_parameters(cell::AbstractMatrix) a, b, c = sqrt.(diag(G)) α = acos(0.5(G[2, 3] + G[3, 2])/(c * b))u"rad" β = acos(0.5(G[3, 1] + G[1, 3])/(a * c))u"rad" - γ = acos(0.5(G[1, 2] + G[2, 1])/(a * b))u"rad" - CellParameters(a, b, c, α, β, γ) + γₒ = acos(0.5(G[1, 2] + G[2, 1])/(a * b))u"rad" + CellParameters(a, b, c, α, β, γₒ) end end diff --git a/test/Crystal.jl b/test/Crystal.jl index 516ce03..bb9d77f 100644 --- a/test/Crystal.jl +++ b/test/Crystal.jl @@ -41,10 +41,10 @@ @testset ">> Convert position to crystal's type" begin position_for_crystal = Crystals.Structures.position_for_crystal cell = [0 1 1; 1 0 1; 1 1 0]u"nm" - real = Crystal(cell) - @test !is_fractional(real) - @test position_for_crystal(real, [1, 1, 1]) == cell * [1, 1, 1] - @test position_for_crystal(real, [1, 1, 1]u"nm") == [1, 1, 1]u"nm" + realpos = Crystal(cell) + @test !is_fractional(realpos) + @test position_for_crystal(realpos, [1, 1, 1]) == cell * [1, 1, 1] + @test position_for_crystal(realpos, [1, 1, 1]u"nm") == [1, 1, 1]u"nm" frac = Crystal(Float64[0 1 1; 1 0 1; 1 1 0]u"nm", position=[0, 0, 0]) @test position_for_crystal(frac, [1, 1, 1]) == [1, 1, 1] @test position_for_crystal(frac, [1, 1, 1]u"nm") == inv(frac.cell) * [1, 1, 1]u"nm" @@ -303,10 +303,6 @@ end @test_throws ErrorException delete!(crystal, :position) - crystal = deepcopy(original) - delete!(crystal, :) - @test names(crystal) == [:position] - crystal = deepcopy(original) empty!(crystal) @test nrow(crystal) == 0 @@ -325,36 +321,46 @@ end @test nrow(crystal) == 1 @test crystal.properties == original.properties[2, :] @test crystal.positions == original.positions[:, 2:2] + + crystal = deepcopy(original) + deleterows!(crystal, :) + @test nrow(crystal) == 0 + @test ncol(crystal) == ncol(original) + + crystal = deepcopy(original) + delete!(crystal, :) + @test nrow(crystal) == 0 + @test ncol(crystal) == ncol(original) end end @testset "> vcat and append" begin - first = Crystal([0 5 5; 5 0 5; 5 5 0]u"nm", species=["Al", "O", "O"], + crysA = Crystal([0 5 5; 5 0 5; 5 5 0]u"nm", species=["Al", "O", "O"], position=[1, 1, 1]u"nm", position=[1, 2, 1]u"nm", position=[0, 0, 1]u"nm", label=[:+, :-, :-]) - second = Crystal([0 5 5; 5 0 5; 5 5 0]u"nm", + crysB = Crystal([0 5 5; 5 0 5; 5 5 0]u"nm", position=[2, -1, 1]u"nm", position=[0, 0, -1]u"nm", label=[:-, :a]) - crystal = vcat(first, second) + crystal = vcat(crysA, crysB) @test nrow(crystal) == 5 @test crystal.positions == transpose([1 1 1; 1 2 1; 0 0 1; 2 -1 1; 0 0 -1])u"nm" @test crystal[1:3, :species] == ["Al", "O", "O"] - @test all(isna(crystal[4:5, :species])) + @test all(isna.(crystal[4:5, :species])) @test crystal[:label] == [:+, :-, :-, :-, :a] - crystal = deepcopy(first[[:position, :label]]) - append!(crystal, second) + crystal = deepcopy(crysA[[:position, :label]]) + append!(crystal, crysB) @test nrow(crystal) == 5 @test crystal.positions == transpose([1 1 1; 1 2 1; 0 0 1; 2 -1 1; 0 0 -1])u"nm" @test crystal[:label] == [:+, :-, :-, :-, :a] # mismatching columns - crystal = deepcopy(first) - @test_throws ErrorException append!(crystal, second) + crystal = deepcopy(crysA) + @test_throws ErrorException append!(crystal, crysB) end @testset "> round crystal cell and positions" begin diff --git a/test/Gruber.jl b/test/Gruber.jl index 85a3ec2..a9098c8 100644 --- a/test/Gruber.jl +++ b/test/Gruber.jl @@ -9,8 +9,8 @@ g = gruber(cell * a) @test abs(det(cell)) > 1e-12 @test volume(cell * a) ≈ volume(g) - @test isinteger(inv(cell) * g) - @test isinteger(round(inv(g) * cell, 8)) + @test all(isinteger, inv(cell) * g) + @test all(isinteger, round.(inv(g) * cell, 8)) end end @@ -24,8 +24,8 @@ g = gruber(cell * a) @test abs(det(ustrip(cell))) > 1e-12 @test volume(cell * a) ≈ volume(g) - @test isinteger(inv(cell) * g) - @test isinteger(round(inv(g) * cell, 8)) + @test all(isinteger, inv(cell) * g) + @test all(isinteger, round.(inv(g) * cell, 8)) break end end diff --git a/test/SpaceGroup.jl b/test/SpaceGroup.jl index 193b124..c98a4af 100644 --- a/test/SpaceGroup.jl +++ b/test/SpaceGroup.jl @@ -1,5 +1,5 @@ function valid_super_cell(size::Integer=3) - cell = rand(-3:3, (3, 3)) + cell = rand(-3:3, (size, size)) while det(cell) ≤ 1 cell = rand(-3:3, (3, 3)) end @@ -40,14 +40,14 @@ end for op in ops @test size(op) == (3, 3) transformation = inv(cell) * op * cell - @test isinteger(round(transformation, 8)) + @test all(isinteger, round.(transformation, 8)) @test volume(transformation) ≈ 1.0 if numops != 48 found = 0 for op in allops transformation = inv(cell) * op * cell - isinteger(round(transformation, 8)) || continue + all(isinteger, round.(transformation, 8)) || continue isapprox(volume(transformation), 1, rtol=1e-8) || continue found += 1 end @@ -124,6 +124,6 @@ end b5 = Lattices.b5() ops = space_group(b5) @test length(ops) == 48 - @test count(op -> all(abs(ustrip(op([0, 0, 0]u"nm"))) .< 1e-12), ops) == 12 + @test count(o -> all(abs.(ustrip(o([0, 0, 0]u"nm"))) .< 1e-12), ops) == 12 end end diff --git a/test/utilities.jl b/test/utilities.jl index 52952f7..769cfc9 100644 --- a/test/utilities.jl +++ b/test/utilities.jl @@ -11,10 +11,10 @@ using Crystals.Utilities: eldimension, CellParameters hf = @inferred(hart_forcade(lattice.cell, crystal.cell)) @test hf.quotient == [1, 2, 2] for i in eachindex(crystal) - @test isinteger(hf.transform * crystal[i, :position]) + @test all(isinteger, hf.transform * crystal[i, :position]) end for i=1:hf.quotient[1], j=1:hf.quotient[2], k=1:hf.quotient[3] - @test isinteger(inv(lattice.cell) * (inv(hf.transform) * [i, j, k])) + @test all(isinteger, inv(lattice.cell) * (inv(hf.transform) * [i, j, k])) end end @@ -34,17 +34,17 @@ end # @testset "> to fractional" begin cell = [0 0.5 0.5; 0.5 0 0.5; 0.5 0.5 0]u"nm" - position = [0.1, 0.1, 0.2]u"nm" - @test @inferred(to_fractional(position, cell)) ≈ inv(cell) * position - uposition = ustrip(position) + pos = [0.1, 0.1, 0.2]u"nm" + @test @inferred(to_fractional(pos, cell)) ≈ inv(cell) * pos + uposition = ustrip(pos) @test @inferred(to_fractional(uposition, cell)) === uposition end @testset "> to cartesian" begin cell = [0 0.5 0.5; 0.5 0 0.5; 0.5 0.5 0]u"nm" - position = [0.1, 0.1, 0.2]u"nm" - @test @inferred(to_cartesian(position, cell)) === position - uposition = ustrip(position) + pos = [0.1, 0.1, 0.2]u"nm" + @test @inferred(to_cartesian(pos, cell)) === pos + uposition = ustrip(pos) @test @inferred(to_cartesian(uposition, cell)) ≈ cell * uposition end @@ -52,10 +52,10 @@ end cell = [0 0.5 0.5; 0.5 0 0.5; 0.5 0.5 0]u"nm" for i in 1:1 #10 - position = cell * rand(3) - image = position + cell * rand(Int8, (3, )) - @test @inferred(is_periodic(position, image, cell)) - @test !@inferred(is_periodic(position, image + (rand(3) * 1e-4) * u"nm", cell)) + pos = cell * rand(3) + image = pos + cell * rand(Int8, (3, )) + @test @inferred(is_periodic(pos, image, cell)) + @test !@inferred(is_periodic(pos, image + (rand(3) * 1e-4) * u"nm", cell)) positions = cell * rand((3, 10)) images = positions + cell * rand(Int8, (3, 10)) @@ -70,14 +70,14 @@ end cell = [0 0.5 0.5; 0.5 0 0.5; 0.5 0.5 0]u"nm" for i in 1:10 - position = cell * (rand(3) + rand(Int8, (3, ))) - folded = into_cell(position, cell) - @test @inferred(is_periodic(position, folded, cell)) + pos = cell * (rand(3) + rand(Int8, (3, ))) + folded = into_cell(pos, cell) + @test @inferred(is_periodic(pos, folded, cell)) @test all(0 .≤ inv(cell) * folded .< 1) - position = cell * (rand((3, 10)) + rand(Int8, (3, 10))) - folded = into_cell(position, cell) - @test all(@inferred(is_periodic(position, folded, cell))) + pos = cell * (rand((3, 10)) + rand(Int8, (3, 10))) + folded = into_cell(pos, cell) + @test all(@inferred(is_periodic(pos, folded, cell))) @test all(0 .≤ inv(cell) * folded .< 1) end end @@ -86,23 +86,23 @@ end cell = [0 0.5 0.5; 0.5 0 0.5; 0.5 0.5 0]u"nm" for i in 1:10 - position = cell * (rand(3) + rand(Int8, (3, ))) - folded = @inferred(origin_centered(position, cell)) - @test is_periodic(position, folded, cell) + pos = cell * (rand(3) + rand(Int8, (3, ))) + folded = @inferred(origin_centered(pos, cell)) + @test is_periodic(pos, folded, cell) @test all(-0.5 .< inv(cell) * folded .≤ 0.5) - position = cell * (rand((3, 10)) + rand(Int8, (3, 10))) - folded = @inferred(origin_centered(position, cell)) - @test all(is_periodic(position, folded, cell)) + pos = cell * (rand((3, 10)) + rand(Int8, (3, 10))) + folded = @inferred(origin_centered(pos, cell)) + @test all(is_periodic(pos, folded, cell)) @test all(-0.5 .< inv(cell) * folded .≤ 0.5) end end -function none_smaller(position::Vector, cell::Matrix) - const d = norm(position) +function none_smaller(pos::Vector, cell::Matrix) + const d = norm(pos) for i = -2:2, j = -2:2, k = -2:2 i == j == k == 0 && continue - norm(position + cell * [i, j, k]) < d * (1 - 1e-12) && return false + norm(pos + cell * [i, j, k]) < d * (1 - 1e-12) && return false end true end @@ -111,15 +111,15 @@ end cell = [0 0.5 0.5; 0.5 0 0.5; 0.5 0.5 0]u"nm" for i in 1:10 - position = cell * (rand(3) + rand(Int8, (3, ))) - folded = into_voronoi(position, cell) - @test is_periodic(position, folded, cell) + pos = cell * (rand(3) + rand(Int8, (3, ))) + folded = into_voronoi(pos, cell) + @test is_periodic(pos, folded, cell) @test none_smaller(folded, cell) - position = cell * (rand((3, 10)) + rand(Int8, (3, 10))) - folded = into_voronoi(position, cell) - @test all(is_periodic(position, folded, cell)) - for i = 1:size(position, 2) + pos = cell * (rand((3, 10)) + rand(Int8, (3, 10))) + folded = into_voronoi(pos, cell) + @test all(is_periodic(pos, folded, cell)) + for i = 1:size(pos, 2) @test none_smaller(folded[:, i], cell) end end