Skip to content

Commit

Permalink
more AtomsBase compatibility (#151)
Browse files Browse the repository at this point in the history
* more AtomsBase compatibility

* change `chemical_formula` to `empirical_formula`
  • Loading branch information
eahenle authored Mar 25, 2022
1 parent 821d152 commit 35c1d16
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Xtals"
uuid = "ede5f01d-793e-4c47-9885-c447d1f18d6d"
authors = ["SimonEnsemble <cory.simon@oregonstate.edu>"]
version = "0.3.13"
version = "0.4.0"

[deps]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Expand Down
6 changes: 3 additions & 3 deletions docs/src/crystal.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ xtal.atoms.n, super_xtal.atoms.n
```julia
rho = crystal_density(xtal) # Crystal density of the crystal in kg/m^2
mw = molecular_weight(xtal) # The molecular weight of the unit cell in amu
formula = chemical_formula(xtal) # The irreducible chemical formula of the crystal
formula = empirical_formula(xtal) # The irreducible chemical formula of the crystal
```

## Assigning new charges
Expand All @@ -95,7 +95,7 @@ Bravais unit cell of a crystal.
# atoms = 424
# charges = 424
chemical formula: Dict(:Zn => 4, :H => 12, :O => 13, :C => 24)
chemical formula: C₁₉₂H₉₆O₁₀₄Zn₃₂
space Group: P1
symmetry Operations:
'x, y, z'
Expand Down Expand Up @@ -125,7 +125,7 @@ write_xyz(xtal, "my_new_xyz_file.xyz") # stored in the current directory
replicate
molecular_weight
crystal_density
chemical_formula
empirical_formula
assign_charges
write_cif
write_xyz
Expand Down
2 changes: 1 addition & 1 deletion src/Xtals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export
read_xyz, write_xyz, read_mol, write_mol2, assert_P1_symmetry, set_paths, view_crystal,

# crystal.jl
Crystal, strip_numbers_from_atom_labels!, assign_charges, chemical_formula, molecular_weight,
Crystal, strip_numbers_from_atom_labels!, assign_charges, empirical_formula, molecular_weight,
crystal_density, write_cif, has_charges, apply_symmetry_operations, write_cssr, rename,
# AtomsBase things also from crystal
position, velocity, bounding_box, boundary_conditions,
Expand Down
15 changes: 12 additions & 3 deletions src/crystal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ end
vtk_filename(crystal::Crystal) = replace(replace(crystal.name, ".cif" => ""), ".cssr" => "") * ".vtk"

"""
formula = chemical_formula(crystal, verbose=false)
formula = empirical_formula(crystal, verbose=false)
Find the irreducible chemical formula of a crystal structure.
Expand All @@ -638,7 +638,7 @@ Find the irreducible chemical formula of a crystal structure.
# Returns
- `formula::Dict{Symbol, Int}`: A dictionary with the irreducible chemical formula of a crystal structure
"""
function chemical_formula(crystal::Crystal; verbose::Bool=false)
function empirical_formula(crystal::Crystal; verbose::Bool=false)
unique_atoms = unique(crystal.atoms.species)
# use dictionary to count atom types
atom_counts = Dict{Symbol, Int}([a => 0 for a in unique_atoms])
Expand Down Expand Up @@ -1110,11 +1110,19 @@ function Base.:+(crystals::Crystal...; check_overlap::Bool=true, name::String="a
return crystal
end

# AtomsBase interface things...NB that the indexing/iteration aspects aren't included because they would conflict with how slicing is defined in this package
# AtomsBase interface things
import Base.position
import AtomsBase.velocity
import AtomsBase.bounding_box
import AtomsBase.boundary_conditions
import Base.length, Base.getindex
import AtomsBase.atomic_symbol

length(xtal::Crystal) = xtal.atoms.n

getindex(xtal::Crystal, index::Int) = xtal.atoms[index]

atomic_symbol(atom::Atoms) = atom.species[1]

function position(crystal::Crystal)
pos = Cart(crystal.atoms.coords, crystal.box).x
Expand All @@ -1124,4 +1132,5 @@ end
velocity(::Crystal) = missing

bounding_box(crystal::Crystal) = SVector{3}([SVector{3}(crystal.box.f_to_c[:,i]u"") for i in 1:3])

boundary_conditions(::Crystal) = SVector{3,BoundaryCondition}([Periodic(), Periodic(), Periodic()])
10 changes: 5 additions & 5 deletions test/crystal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ irmof1 = Crystal("IRMOF-1.cif")
@test isapprox(xtal2.charges, Charges([-2.0, 2.0], Frac([0.2 0.6; 0.5 0.3; 0.7 0.1])))
@test ! neutral(assign_charges(xtal, Dict(:Ca => 2.0, :O => 2.0), 100.0)) # not charge neutral
@test_throws ErrorException assign_charges(xtal, Dict(:Ca => 2.0, :O => 2.0)) # not charge neutral
@test chemical_formula(xtal) == Dict(:Ca => 1, :O => 1)
@test empirical_formula(xtal) == Dict(:Ca => 1, :O => 1)
@test molecular_weight(xtal) 15.9994 + 40.078
xtal3 = assign_charges(xtal2, Dict(:Ca => -2.0, :O => 2.0))
@test isapprox(xtal2.charges, xtal3.charges)
Expand All @@ -78,7 +78,7 @@ irmof1 = Crystal("IRMOF-1.cif")

@test isapprox(xtal.atoms, Atoms([:Ca, :Ca, :O, :C], f))
@test isapprox(net_charge(xtal), 2.0)
@test chemical_formula(xtal) == Dict(:Ca => 2, :O => 1, :C => 1)
@test empirical_formula(xtal) == Dict(:Ca => 2, :O => 1, :C => 1)
infer_bonds!(xtal, true)
@test_throws ErrorException Xtals.remove_duplicate_atoms_and_charges(xtal)

Expand Down Expand Up @@ -225,7 +225,7 @@ irmof1 = Crystal("IRMOF-1.cif")
@test Xtals.replication_factors(replicated_sbmof.box, 14.0) == (1, 1, 1)
@test isapprox(sbmof.atoms.coords.xf[:, 1] ./ repfactors, replicated_sbmof.atoms.coords.xf[:, 1])
@test isapprox(replicated_sbmof.box.reciprocal_lattice, 2 * π * inv(replicated_sbmof.box.f_to_c))
@test chemical_formula(sbmof) == chemical_formula(replicated_sbmof)
@test empirical_formula(sbmof) == empirical_formula(replicated_sbmof)
@test isapprox(crystal_density(sbmof), crystal_density(replicated_sbmof), atol=1e-7)

xtal = deepcopy(sbmof1)
Expand Down Expand Up @@ -355,8 +355,8 @@ irmof1 = Crystal("IRMOF-1.cif")
infer_bonds!(xtal1, true)
@test_throws ErrorException replicate(xtal1, (1,1,1))

# test verbose printing in chemical_formula
Xtals.chemical_formula(xtal1, verbose=true)
# test verbose printing in empirical_formula
empirical_formula(xtal1, verbose=true)
@test true

@test xtal1 == apply_symmetry_operations(xtal1)
Expand Down

0 comments on commit 35c1d16

Please sign in to comment.