Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CoordinateVector to represent coordinates in real/reciprocal space #210

Merged
merged 36 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
d373643
Added `AbstractCoordinateVector` and `CoordinateVector` types
brainandforce Dec 6, 2023
2c8eb7c
Made `ShiftVector` an `AbstractCoordinateVector`
brainandforce Dec 6, 2023
64cc206
Exported `AbstractCoordinateVector`, `CoordinateVector`, and aliases
brainandforce Dec 6, 2023
f931916
Implemented StaticArrays API for `CoordinateVector`
brainandforce Dec 6, 2023
f4a2a04
Restricted element type of `AbstractCoordinateVector` to `Real`
brainandforce Dec 6, 2023
dd572f0
Added docstrings for `CoordinateVector` aliases
brainandforce Dec 6, 2023
88a1fe0
Reworked constructors for `CoordinateVector`
brainandforce Dec 6, 2023
85badd4
Reworked constructors for `ShiftVector`
brainandforce Dec 6, 2023
4a54bdb
Changed ordering of some constructors
brainandforce Dec 6, 2023
ee57d47
Removed tests for cases that no longer throw errors
brainandforce Dec 6, 2023
f9a1e2b
Added documentation for new exported names/features
brainandforce Dec 6, 2023
93cdf25
Turned lattice documentation into more general geometry documentation
brainandforce Dec 6, 2023
dfde79f
Moved `test/kpoints.jl` to `test/coordinates.jl`
brainandforce Dec 6, 2023
18fda12
Added better definitions for common mathematical operations
brainandforce Dec 6, 2023
f5d866e
Added more tests for new coordinate types
brainandforce Dec 7, 2023
cecff84
Removed `AbstractCoordinateVector` from the type tree
brainandforce Dec 8, 2023
e53d102
Created stable equivalents to `Base.promote_typeof` and `Base.promote…
brainandforce Dec 8, 2023
917d6b7
Added more tests for conversion
brainandforce Dec 8, 2023
40d2df2
Reworked construction of `CoordinateVector`
brainandforce Dec 8, 2023
c24774e
Implemented `StaticArrays.similar_type` for `CoordinateVector`
brainandforce Dec 10, 2023
221f4ff
Restricted construction of `CoordinateVector` from another `Coordinat…
brainandforce Dec 10, 2023
dd7cc07
Fixed broken mathematical operations on `CoordinateVector`
brainandforce Dec 10, 2023
60e1104
Fixed some broken tuple constructors
brainandforce Dec 10, 2023
c7b0d21
Fixed accidental assignment in test
brainandforce Dec 10, 2023
7f71a61
Implemented conversion restrictions/errors for `CoordinateVector` and…
brainandforce Dec 10, 2023
c30848f
Adding a bare vector to a `CoordinateVector` should propagate the tra…
brainandforce Dec 10, 2023
24d1ff0
Multiplying `CoordinateVector` with scalars should return `SVector`
brainandforce Dec 10, 2023
af21033
Fixed misspelling in `@docs` block
brainandforce Dec 10, 2023
2c7a330
Removed rational division methods (matches other vector types)
brainandforce Dec 10, 2023
c4b71ba
Removed `@inline` annotations not compatible with Julia <1.8
brainandforce Dec 10, 2023
0feacde
Added tests for uncovered lines
brainandforce Dec 10, 2023
8a2561e
Removed redundant method for subtraction
brainandforce Dec 10, 2023
f3f677f
Added final set of tests
brainandforce Dec 10, 2023
21a8493
Fixed show methods for `CoordinateVector` and `ShiftVector`
brainandforce Dec 10, 2023
01e89ff
Allowed for extra type parameters in `ShiftVector` varargs constructor
brainandforce Dec 10, 2023
9fd767e
Removed duplicated `show` method for `ShiftVector`
brainandforce Dec 10, 2023
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
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- `StateDensity{T}` type which combines `EnergyOccupancy{T}` with a density of states value at the
energy provided.
- `ByCoordinate` traits: `ByCartesianCoordinate` and `ByFractionalCoordinate`.
- `ShiftVector{S,D,T} <: StaticVector{D,T}` type describing the shift of a lattice or data defined
on it with respect to the origin, along with an optional weight parameter.
- `ShiftVector{S,D,T} <: AbstractCoordinateVector{S,ByFractionalCoordinate,D,T}`, representing a
potentially weighted vector which shifts a data grid.
- `require_same_space`, `require_dual_space`, and `require_same_coordinate` functions for checking
whether `BySpace` and `ByCoordinate` traits are compatible in an operation.
- `AbstractCoordinateVector{S<:Electrum.BySpace,C<:Electrum.ByCoordinate,D,T}` type for coordinate
vectors in real or reciprocal space with either fractional or Cartesian coordinates.
- `CoordinateVector{S,C,D,T}` and aliases:
+ `RealCartesianCoordinate`
+ `RealFractionalCoordinate`
+ `ReciprocalCartesianCoordinate`
+ `ReciprocalFractionalCoordinate`

### Changed
- [BREAKING] `KPoint{D}` is now `KPoint{D,T}`, equivalent to `ShiftVector{ByReciprocalSpace,D,T}`.
- `BySpace` traits are now part of the public API.
- `BySpace{D}` and its subtypes have lost their dimension type parameter.

Expand Down
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ makedocs(
warnonly = is_ci_env,
pages = [
"Home" => "index.md",
"Lattices" => "lattices.md",
"Geometry" => "geometry.md",
"Atoms and crystals" => "atoms.md",
"Data grids" => "grids.md",
"File formats" => "filetypes.md",
Expand Down
7 changes: 6 additions & 1 deletion docs/src/api/geometry.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ eachvertex
maxHKLindex
```

## Shift vectors
## Coordinate vectors
```@docs
CoordinateVector
RealCartesianCoordinate
RealFractionalCoordinate
ReciprocalCartesianCoordinate
ReciprocalFractionalCoordinate
ShiftVector
KPoint
weight
Expand Down
92 changes: 89 additions & 3 deletions docs/src/lattices.md → docs/src/geometry.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,103 @@
# Lattices
# Geometry

The starting point for solid state structures is a periodic lattice. Electrum provides convenient
types and functions for working with lattices of arbitrary dimension, not just three dimensions.

## Real and reciprocal space traits
## Traits

Julia parametric types can be used to encode information about data types that are wrapped by a
new composite type: for instance, Julia's `Array{T,D}` type includes information about the types of
the elements in the type parameter `T`. However, type parameters can be used as tags that provide
additional information about data types without relying on inheritance from another abstract type.
One example of this is `Ptr{T}`: although a pointer is underlyingly a `UInt`, the type parameter
encodes the type of data pointed at.

Electrum uses several traits to encode information about numerical data, particularly vectors. If a
user is given an instance of `SVector{3,Float64}`, the data type itself does not encode important
information, like whether the data corresponds to a real or reciprocal space coordinate, or whether
the coordinates are fractional coordinate or Cartesian coordinates. Electrum's data types use these
traits in their declarations to convey this extra information.

### Conventions

Trait types are declared as singleton structs (no fields). If more information needs to be encoded,
use a type parameter. The names of traits generally tend to start with `By`.

If creating a new data type that uses one of the traits mentioned below, use the type itself as a
parameter, not the singleton instance.

### Real and reciprocal space

The `BySpace` supertype contains two types, `ByRealSpace` and `ByReciprocalSpace`. These types are
used to denote whether data is associated with real space (e.g. electron density) or reciprocal
space (e.g. the Fourier transform of the electron density). When working with lattices, it is
important to distinguish the two types of lattice: this is the primary reason why bare
`SMatrix{D,D,T}` instances are not used in this package.

## `Electrum.LatticeBasis` and methods
The units associated with data bearing the `Electrum.ByRealSpace` trait are bohr or powers of bohr,
whereas those associated with `Electrum.ByReciprocalSpace` are rad bohr⁻¹ or powers thereof.

```@docs; canonical=false
Electrum.BySpace
Electrum.ByRealSpace
Electrum.ByReciprocalSpace
```

### Coordinate system

The `Electrum.ByCoordinate{D}` supertype contains `Electrum.ByCartesianCoordinate{D}` and
`Electrum.ByFractionalCoordinate{D}`, corresponding to Cartesian or fractional coordinates in `D`
dimensions.

```@docs; canonical=false
Electrum.ByCoordinate
Electrum.ByCartesianCoordinate
Electrum.ByFractionalCoordinate
```

## Coordinate vectors

The traits above can be incorporated into new coordinate types that wrap a `SVector{D,T<:Real}`, and
retain information about what type of information is being stored by the coordinate.

### `CoordinateVector`

The `CoordinateVector{S,C,D,T}` type is the primary data type for working with spatial coordinates.
The `RealCartesianCoordinate`, `RealFractionalCoordinate`, `ReciprocalCartesianCoordinate`, and
`ReciprocalFractionalCoordinate` aliases correspond to the four possible combinations of `S` and `C`
type parameters, and are preferred for interactive use.

This type behaves almost identically to `SVector{D,T}`, but forbids nonsenical operations between
data types: for instance, adding a `RealCartesianCoordinate` to a `ReciprocalCartesianCoordinate`.

```@docs; canonical=false
CoordinateVector
RealCartesianCoordinate
RealFractionalCoordinate
ReciprocalCartesianCoordinate
ReciprocalFractionalCoordinate
```

### `ShiftVector`

A `ShiftVector` is almost identical to a `CoordinateVector`, and wraps one as a field, but includes
a weight parameter that defaults to 1.

The primary use for `ShiftVector` is to provide information about how data associated with a lattice
is shifted with respect to the origin. In particular, it forms the implementation of `KPoint{D,T}`,
which is simply an alias for `ShiftVector{ByReciprocalSpace,D,T}`. In many cases, lists of k-points
are symmetry-reduced, and the weight parameter is used to account for coordinates not present in the
list due to symmetry reduction.

To retain the

```@docs; canonical=false
ShiftVector
KPoint
weight
```

## Lattices

The `Electrum.LatticeBasis{S<:BySpace,D,T}` data type is a wrapper for an
`SMatrix{D,D,T,D^2}` which represents the real or reciprocal space basis vectors of a lattice.
Expand Down
24 changes: 23 additions & 1 deletion src/Electrum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,27 @@ Computes the dot product of a vector with itself.
"""
_selfdot(v) = dot(v,v)

"""
Electrum.promote_typeof(args...)

Calls `promote_type()` on the types of the arguments, equivalent to `promote_type(typeof.(x)...)`.

This function is implemented in Julia Base, but it is not part of the public API; a stable
implementation is provided here.
"""
promote_typeof(x...) = promote_type(typeof.(x)...)

"""
Electrum.promote_eltype(args...)

Calls `promote_type()` on the element types of the arguments, equivalent to
`promote_type(eltype.(x)...)`.

This function is implemented in Julia Base, but it is not part of the public API; a stable
implementation is provided here.
"""
promote_eltype(x...) = promote_type(eltype.(x)...)

# Methods used in array operations that go beyond what's available in LinearAlgebra
include("math.jl")
export FFTBins, FFTLength
Expand All @@ -118,7 +139,8 @@ export RealBasis, ReciprocalBasis, AbstractBasis
export eachvertex, basis, dual, dualbasis, lengths, volume, angles_cos, angles_rad, angles_deg,
gram, isdiag, qr, triangularize, maxHKLindex
include("vectors.jl")
export ShiftVector, KPoint
export CoordinateVector, RealCartesianCoordinate, RealFractionalCoordinate,
ReciprocalCartesianCoordinate, ReciprocalFractionalCoordinate, ShiftVector, KPoint
export weight
# Methods and structs for working with atomic positions
include("atoms.jl")
Expand Down
Loading
Loading