Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Getting ready for 0.7/1.0 #1

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ os:
- linux
- osx
julia:
- 0.6
- 0.7

notifications:
email: false
Expand Down
16 changes: 16 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name = "Crystals"
uuid = "2c71e7ee-d617-5e33-8739-0c480ea2ee56"

[deps]
# MicroLogging = "cecf6091-0e4e-54ba-8cba-f0e96a51f282"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
TranscodingStreams = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"
ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
CoordinateTransformations = "150eb455-5306-5404-9cee-2592286d6298"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[compat]
Unitful = "0.11.0"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ attached to it.

```julia
using Crystals
crystal = Crystal(eye(3)u"nm")
crystal = Crystal(Matrix(1.0I, 3, 3)u"nm")
@assert crystal.cell === [1 0 0; 0 1 0; 0 0 1]
```

Expand All @@ -24,7 +24,7 @@ properties:

```julia
using Crystals
crystal = Crystal(eye(2)u"km",
crystal = Crystal(Matrix(1.0I, 2, 2)u"km",
position=transpose([1 1; 2 3; 4 5])u"m",
species=["Al", "O", "O"],
label=[:+, :-, :-])
Expand Down
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
julia 0.6
julia 0.7
Unitful
DataFrames
CoordinateTransformations
Expand Down
15 changes: 9 additions & 6 deletions docs/src/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ attached to it.

```jldoctest
using Crystals
crystal = Crystal(eye(3)u"nm")
using LinearAlgebra
crystal = Crystal(Matrix(1.0I, 3, 3)u"nm")

# output
cell(nm):
Expand All @@ -30,10 +31,11 @@ properties:
```@meta
DocTestSetup = quote
using Crystals

end
```
```jldoctest
crystal = Crystal(eye(2)u"km",
using LinearAlgebra
crystal = Crystal(Matrix(1.0I, 2, 2)u"km",
position=transpose([1 1; 2 3; 4 5])u"m",
species=["Al", "O", "O"],
label=[:+, :-, :-])
Expand All @@ -58,7 +60,7 @@ one coordinate (and all atoms) at a time. Hence the transpose.


```@eval
using Base.Markdown
using Markdown
using Crystals
result = """
!!! note
Expand All @@ -68,7 +70,7 @@ result = """
the constructor.
"""
result *= " - :" * join(map(string, Crystals.Structures.RESERVED_COLUMNS), "\n - :")
Base.Markdown.parse(result)
Meta.parse(result)
```

## Accessing the cell and atomic sites
Expand All @@ -81,7 +83,8 @@ ways of doing this, more or less reflecting what can be done with a
```@meta
DocTestSetup = quote
using Crystals
crystal = Crystal(eye(2)u"km",
using LinearAlgebra
crystal = Crystal(Matrix(1.0I, 2, 2)u"km",
position=transpose([1 1; 2 3; 4 5])u"m",
species=["Al", "O", "O"],
label=[:+, :-, :-])
Expand Down
17 changes: 6 additions & 11 deletions docs/src/cartesian.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ end
```
```jldoctest
frac_crystal = Crystal([0 2.1 2.1; 2.1 0 2.1; 2.1 2.1 0]u"nm",
position=[0, 0, 0],
position=[0.25, 0.25, 0.25])
tposition=[0 0 0;0.25 0.25 0.25])
@assert frac_crystal[:position] === frac_crystal[:fractional]
@assert frac_crystal[:cartesian] ≈ frac_crystal.cell * frac_crystal[:fractional]
units = unit(eltype(frac_crystal[:cartesian]))
Expand All @@ -34,8 +33,7 @@ Cartesian coordinates instead -- by calling the constructor with positions that

```jldoctest
cart_crystal = Crystal([0 2.1 2.1; 2.1 0 2.1; 2.1 2.1 0]u"nm",
position=[0, 0, 0]u"nm",
position=[1.05, 1.05, 1.05]u"nm")
tposition=[0 0 0;1.05 1.05 1.05]u"nm")
@assert cart_crystal[:position] === cart_crystal[:cartesian]
@assert cart_crystal[:fractional] ≈ inv(cart_crystal.cell) * cart_crystal[:cartesian]
println(cart_crystal[:fractional])
Expand All @@ -53,8 +51,7 @@ of crystal from any other crystal, one can simply use the bracket operator:

```jldoctest
crystal = Crystal([0 2.1 2.1; 2.1 0 2.1; 2.1 2.1 0]u"nm",
position=[0, 0, 0]u"nm",
position=[1.05, 1.05, 1.05]u"nm",
tposition=[0 0 0;1.05 1.05 1.05]u"nm",
species=["Si", "Si"])
crystal[[:fractional, :species]]

Expand Down Expand Up @@ -83,8 +80,7 @@ crystal.

```jldoctest
frac_crystal = Crystal([0 2.1 2.1; 2.1 0 2.1; 2.1 2.1 0]u"nm",
position=[0, 0, 0],
position=[0.25, 0.25, 0.25])
tposition=[0 0 0;0.25 0.25 0.25])
frac_crystal[2, :position] = [1, 1, 1]u"nm"
frac_crystal

Expand All @@ -107,12 +103,11 @@ Apart from `:cartesian` and `:fractional`, there are three other special column
ease access to specific atomic properties. `:x`, `:y`, `:z` will return an array
representing the corresponding coordinate, in the same system -- Cartesian or fractional --
as the crystal (and as `:position`). `:x`, `:y`, `:z` can be used to set a specific
coordinate as well. Only three such special names are provided.
coordinate as well. Only three such special names are provided.

```jldoctest
crystal = Crystal([0 2.1 2.1; 2.1 0 2.1; 2.1 2.1 0]u"nm",
position=[0, 0, 0],
position=[0.25, 0.27, 0.25])
tposition=[0 0 0;0.25 0.27 0.25])

println("Y coordinate: ", crystal[2, :y])
crystal[2, :y] = 0.25
Expand Down
6 changes: 3 additions & 3 deletions src/Binary.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
" Rock-salt lattice "
rock_salt(T::Type=Float64; unit=u"nm") = Crystal(
eye(T, 3) * unit,
position=T[0.5, 0.5, 0.5] * unit,
position=T[0, 0, 0] * unit,
Matrix{T}(I, 3, 3) * unit,
T[0.5, 0.5, 0.5] * unit,
T[0, 0, 0] * unit,
species=['A', 'B']
)

Expand Down
15 changes: 7 additions & 8 deletions src/CrystalAtoms.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module CrystalAtoms
using Crystals.Structures: Crystal, position_for_crystal
import Crystals.Structures: Crystal, position_for_crystal
export eachatom
import DataFrames

using LinearAlgebra
""" Wrapper around a row/atom in a Crystal for iteration """
immutable CrystalAtom{PARENT <: Crystal}
struct CrystalAtom{PARENT <: Crystal}
""" Parent crystal """
parent::PARENT
""" Index in parent """
Expand All @@ -20,7 +20,7 @@ end
Base.names(a::CrystalAtom) = names(a.parent)

""" Wraps crystal for iteration purposes """
immutable AtomicIterator{PARENT <: Crystal}
struct AtomicIterator{PARENT <: Crystal}
""" Parent crystal structure """
parent::PARENT
end
Expand All @@ -30,11 +30,10 @@ eachatom(crystal::Crystal) = AtomicIterator(crystal)
""" Iterator over each atom in the crystal """
DataFrames.eachrow(crystal::Crystal) = eachatom(crystal)

Base.start(itr::AtomicIterator) = 1
Base.done(itr::AtomicIterator, i::Integer) = i > size(itr.parent, 1)
Base.next(itr::AtomicIterator, i::Integer) = (CrystalAtom(itr.parent, i), i + 1)
Base.iterate(itr::AtomicIterator, i::Integer=1) = i > size(itr.parent, 1) ? nothing : (CrystalAtom(itr.parent, i), i + 1)
Base.size(itr::AtomicIterator) = (length(itr.parent), )

Base.length(itr::AtomicIterator) = size(itr.parent, 1)
Base.getindex(itr::AtomicIterator, i::Any) = CrystalAtom(itr.parent, i)
Base.eltype{T <: Crystal}(::Type{AtomicIterator{T}}) = CrystalAtom{T}
Base.eltype(::Type{AtomicIterator{T}}) where {T <: Crystal} = CrystalAtom{T}
end
22 changes: 12 additions & 10 deletions src/Crystals.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Crystals
using Unitful
using MicroLogging
using LinearAlgebra
# using MicroLogging
using Markdown
export @u_str

export Crystal, is_fractional, volume, round!, are_compatible_lattices
Expand All @@ -12,13 +14,13 @@ export into_voronoi, supercell, cell_parameters
export point_group, inner_translations, is_primitive, primitive, space_group
export Lattices

if Pkg.installed("Unitful") ≤ v"0.0.4"
function Base.inv{T<:Quantity}(x::StridedMatrix{T})
m = inv(ustrip.(x))
iq = eltype(m)
reinterpret(Quantity{iq, typeof(inv(dimension(T))), typeof(inv(unit(T)))}, m)
end
end
# if Pkg.installed("Unitful") ≤ v"0.0.4"
# function Base.inv{T<:Quantity}(x::StridedMatrix{T})
# m = inv(ustrip.(x))
# iq = eltype(m)
# reinterpret(Quantity{iq, typeof(inv(dimension(T))), typeof(inv(unit(T)))}, m)
# end
# end

module Constants
const default_tolerance = 1e-8
Expand All @@ -45,7 +47,7 @@ using .SpaceGroup
module Lattices
using DocStringExtensions
using Unitful
using Crystals.Structures.Crystal
import Crystals.Structures: Crystal

@template DEFAULT =
"""
Expand All @@ -68,4 +70,4 @@ function _doc_pages()
end

end # module
@doc Markdown.readme("Crystals") -> Crystals
# @doc Markdown.readme("Crystals") -> Crystals
61 changes: 32 additions & 29 deletions src/Gruber.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
module Gruber
export gruber, niggly

using Crystals.Constants: default_tolerance
using Crystals.Utilities: cell_parameters
using Crystals.Structures: Crystal, volume
using Unitful: unit, ustrip, Quantity
using MicroLogging
import Crystals.Constants: default_tolerance
import Crystals.Utilities: cell_parameters
import Crystals.Structures: Crystal, volume
import Unitful: unit, ustrip, Quantity
# using MicroLogging
using ArgCheck
using LinearAlgebra

function no_opt_change_test(new, last)
const m_new = 16e0 * new;
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 = 16e0 * new;
difference = new - last;
m_new_plus_diff = m_new + difference;
m_new_plus_diff_minus_m_new = m_new_plus_diff - m_new;
m_new_plus_diff_minus_m_new != 0;
end

Expand Down Expand Up @@ -49,18 +50,18 @@ function n2_action(params::Vector, rinv::Matrix)
end

function n3_action(params::Vector, rinv::Matrix; tolerance=default_tolerance)
const i = params[4] ≤ -tolerance ? -1 : 1
const j = params[5] ≤ -tolerance ? -1 : 1
const k = params[6] ≤ -tolerance ? -1 : 1
i = params[4] ≤ -tolerance ? -1 : 1
j = params[5] ≤ -tolerance ? -1 : 1
k = params[6] ≤ -tolerance ? -1 : 1
rinv[:, :] = rinv * [i 0 0; 0 j 0; 0 0 k]
params[4:end] = abs.(params[4:end])
end

function n4_action(params::Vector, rinv::Matrix; tolerance=default_tolerance)
const i = params[4] ≥ tolerance ? -1 : 1
const j = params[5] ≥ tolerance ? -1 : 1
const k = params[6] ≥ tolerance ? -1 : 1
update = diagm([i, j, k])
i = params[4] ≥ tolerance ? -1 : 1
j = params[5] ≥ tolerance ? -1 : 1
k = params[6] ≥ tolerance ? -1 : 1
update = diagm(0 => [i, j, k])
if i * j * k < 0
if k == 1 && params[6] > -tolerance
update[3, 3] = -1
Expand All @@ -77,23 +78,23 @@ function n4_action(params::Vector, rinv::Matrix; tolerance=default_tolerance)
end

function n5_action(params::Vector, rinv::Matrix; tolerance=default_tolerance)
const pos_or_neg = params[4] > tolerance ? -1 : 1
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 pos_or_neg = params[5] > tolerance ? -1 : 1
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 pos_or_neg = params[6] > tolerance ? -1 : 1
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]
Expand Down Expand Up @@ -129,9 +130,9 @@ the `niggly` algorithm.
* `max_no_change::Integer`: Maximum number of times to go through algorithm
without changes before bailing out
"""
function gruber{T <: Number}(cell::Matrix{T};
tolerance::Real=default_tolerance, itermax::Integer=50,
max_no_change::Integer=10)
function gruber(cell::Matrix{T};
tolerance::Real=default_tolerance, itermax::Integer=50,
max_no_change::Integer=10) where T <: Number
@argcheck size(cell, 1) == size(cell, 2)
@argcheck volume(cell) > tolerance
if itermax ≤ 0
Expand All @@ -141,13 +142,15 @@ function gruber{T <: Number}(cell::Matrix{T};
max_no_change = typemax(max_no_change)
end

const ε = tolerance
const metric = transpose(cell) * cell
ε = tolerance
metric = transpose(cell) * cell
params = vcat(diag(metric), [2metric[2, 3], 2metric[1, 3], 2metric[1, 2]])
rinv = eye(size(metric, 1))
no_change, previous = 0, -params[1:3]
iteration::Int = 0
totits = 0
for iteration in 1:itermax
totits += 1
condition0 =
(a, b, d, e) -> a ≥ b + ε || (abs(a - b) < ε && abs(d) ≥ abs(e) + ε)

Expand Down Expand Up @@ -179,21 +182,21 @@ function gruber{T <: Number}(cell::Matrix{T};
condition1(params[6], params[1], params[4], params[5]) &&
(n7_action(params, rinv; tolerance=ε); continue)

const sum_no_c = sum(params[1:2]) + sum(params[4:end])
sum_no_c = sum(params[1:2]) + sum(params[4:end])
(
sum_no_c ≤ -ε ||
(abs(params[6] - params[1]) < ε && (2params[4] ≤ params[5] - ε)) ||
(abs(sum_no_c) < ε && 2params[1] + 2params[5] + params[6] ≥ ε)
) || break
n8_action(params, rinv)
end
iteration == itermax && error("Reached end of iteration without converging")
totits == itermax && error("Reached end of iteration without converging")
cell * rinv
end

function gruber{T, D, U}(cell::Matrix{Quantity{T, D, U}};
tolerance::Real=default_tolerance, itermax::Integer=50,
max_no_change::Integer=10)
function gruber(cell::Matrix{Quantity{T, D, U}};
tolerance::Real=default_tolerance, itermax::Integer=50,
max_no_change::Integer=10) where {T, D, U}
gruber(ustrip.(cell);
tolerance=tolerance,
itermax=itermax,
Expand Down
Loading