[ANN] LightLattices.jl - lazy interface to lattices with arbitrary unit cells

Hi, everyone.

This is an announcement for the package LightLattices.jl. It provides the lazy interface to work with lattices. The lattices of arbitrary dimensionality and arbitrary unit cells are supported.

Overview

The package exports the type RegularLattice{D,T} and several types used to describe the unit cell of the lattice. All the exported types are subtypes of the abstract type AbstractNodeCollection{D,T}. Here, D refers to the dimensionality of space (number of coordinates), T refers to the type used to store the coordinates. For all the exported subtypes, the package defines the array interface

node_collection[I]

which allows to access the coordinate of the I-th node of the collection.
In addition to that, the package provides the function relative_coordinate:

relative_coordinate(node_collection::AbstractNodeCollection, I1, I2)

which returns the vector connecting the I2-th node with the I1-th node.
In the case of RegularLattice-s with periodic boundary conditions, relative_coordinate returns the shortest connecting vector.
(Actually, in the case of complex unit cell, there can be several “shortest” vectors. The problem is resolved by a simple heuristic, described in the docs).

The package also defines eachindex for all the exported types.

There are three available types to describe the unit cell: HomogeneousCell, TrivialCell and InhomogeneousCell.
HomogeneousCell refers to a homogeneous collection of nodes. TrivialCell behaves like HomogeneousCell with single node at the origin (zero coordinates). Finally, InhomogeneousCell is useful in the situation where one need to distinguish between several groups of nodes in the unit cell. For example, we can have several groupes of nodes corresponding to the different types of nuclei which occupy these nodes.

For the detailed account of exported types and the interface, please look at the manual section of the docs.

Example

As an example, let us create a diamond lattice with dimensions 11x11x11 and periodic boundary conditions:

using LightLattices, Unitful

const a = 3.567u"Å"# diamond lattice parameter

basis = 0.5a*hcat([0,1,1],[1,1,0],[1,0,1]) |> SMatrix{3,3}
cell = HomogeneousCell(a*[[0.0,0.0,0.0], [0.25,0.25,0.25]]; label = :diamond)
diamond_lattice = RegularLattice((11,11,11), basis, cell; periodic=true, label=:fcc)

Now, we can get the coordinate of first node in the unit cell with indices (2,5,7) using either

diamond_lattice[CartesianIndex(2,5,7), 1]

or

diamond_lattice[2,5,7,1]

Suppose we now want to find the shortest vector connecting the second node of the first cell with the node we just considered. One can use either

relative_coordinate(diamond_lattice, (CartesianIndex(2,5,7),1), (CartesianIndex(1,1,1), 1))

or simply

relative_coordinate(diamond_lattice, (), (2,5,7, 1), (1,1,1, 1))

For more examples, see README section.

Comparison with other packages

I am aware of two packages providing similar functionality: Lattices.jl and LatticePhysics.jl. The first seems to support only hypercubic lattices without complex unit cells. The second one is not registered and looks very complex. In contrast, I wanted to create a compact easy-to-understand package, hence “Light” in the name of the package.

Final words

Suggestions and ideas about the improvement of the package are welcome!!! I am open to the discussion.

Consider joining JuliaMolSim Slack?!

https://juliamolsim.github.io/

I would like to announce the version 0.4.0, which drastically extends the functionality of the package.

Physical Collections

I have built a new layer that supports static collections of arbitrary physical objects occupying the nodes of a lattice or some irregular cluster. These physical collections partially support the interface of AtomsBase.jl by providing species, position and getindex dispatches, as well as other convenience functions.

unstructured

Say, we want to describe a methane molecule. Four Hydrogen atoms of Methane form the vertices of a regular tetrahedron, whose center is occupied by a single Carbon atom. We can create it using a cluster convenience constructor.

using LightLattices, UnitfulGauss, IsotopeTable

a = 1.09ug"Å"
h_vecs = [[1,1,1],[1,-1,-1],[-1,1,-1],[-1,-1,1]].|>x->x*a/sqrt(3)

meth_mol = cluster(:C12=>zeros(3)ug"Å", :H1=>h_vecs; decoder=isotopes, label=:methane)

We can pass different groups of species as species_label=>set_of_positions. Then, the labels are decoded into species by applying the decoder function: species = decoder(species_label). In this example, we supplement isotopes function from IsotopeTable.jl, so species would be an Isotope struct containing all the isotope information.

In principle, species and their labels can be anything the user wants. The behavior can be changed in a very flexible manner by supplementing a custom decoder function. For example, if we don’t care about the isotopic information, we can instead use the elements from PeriodicTable.jl:

using PeriodicTable

meth_mol = cluster("carbon"=>zeros(3)ug"Å", "hydrogen"=>h_vecs; decoder=x->elements[x], label=:methane)

By default, decoder=identity, so we can pass the species directly

meth_mol = cluster(isotopes(:C12)=>zeros(3)ug"Å", isotopes(:H1)=>h_vecs; label=:methane)

lattices

The same idea applies to lattices, although here species_label=>set_of_positions specifies positions of some species type within the unit cell of a Bravais lattice.

As an example, say we want to describe a NaCl lattice of 11\times11\times11 unit cells with periodic boundary conditions. It has face cubic crystal structure with lattice parameter 5.64\ \AA:

using LightLattices, StaticArrays, UnitfulGauss, PeriodicTable

a = 5.64ug"Å"
fcc_primitive_vecs = 0.5a*hcat([0,1,1],[1,1,0],[1,0,1]) |> SMatrix{3,3}

salt_lattice = lattice((11,11,11), fcc_primitive_vecs, :Na=>zeros(3)ug"Å",
    :Cl=>ones(3)*a/4;decoder = x->elements[x], label=:fcc)

Direct access to different species

Although the physical collections also support scalar indexing for the purpose of interoperability with AtomsBase.jl, the indexing is designed around the groups, where a group roughly corresponds to a particular type of species.

For example, if we need the position of the third hydrogen atom in methane molecule, i.e. third element in the second group, we do

position(meth_mol, 3, 2)

Correspondingly, if we need to adress the natrium atom in the unit cell with indices (2,4,5), we must supplement index in the form unit_cell_index, index_within_cell, group_index:

position(salt_lattice, CartesianIndex(2,4,5), 1, 1)

If we had many species of the same type in the unit cell, index_within_cell would distinguish between them.

Since the species are tied to the groups, it is more convenient to access them using the group_species function. For example, group_species(salt_lattice, 2) returns the information about the chlorine element from PeriodicTable.jl.

In addition to that, there is group_iterator(pcollection, group_index), which returns an iterator over a single group.

This allows one to write the code in the form

for ig=1:num_of_groups(pcollection)
    s = group_species(pcollection, ig)
    for I in group_iterator(pcollection, ig)
        #compute stuff for single species type
    end
end

Physical Subollections and Composite Collections

If you need to specify a subsystem of a larger system, now you can use Subcollection-s. These function like views into underlying Physical collections, but provide the same interface as the physical collections: you can use position, species, group_species, group_iterator, etc.

Several physical collections or Subcollection-s can be merged into a single CompositeCollection, which also realizes the interface of physical collections. This can be convenient to describe some impurities or molecules added to a lattice, for example. For more details, look at the docs.