Skip to content

Commit

Permalink
Added registry of common unit cell vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
timbernat committed Apr 19, 2024
1 parent b900df2 commit b04ce5b
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions polymerist/maths/lattices.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ..genutils.typetools.numpytypes import Shape, N


# INTEGER LATTICES (UNIT BASIS SUPERCELLS)
def generate_int_lattice(*dims : Iterable[int]) -> np.ndarray[Shape[N, 3], int]:
'''Generate all N-D coordinates of points on a integer lattice with the sizes of all D dimensions given'''
return np.fromiter(
Expand All @@ -19,8 +20,38 @@ def generate_int_lattice(*dims : Iterable[int]) -> np.ndarray[Shape[N, 3], int]:
dtype=np.dtype((int, len(dims)))
)


# LATTICE VECTORS AND PARAMETERS
COMMON_LATTICE_VECTORS : dict[str, np.ndarray[Shape[3, 3], float]] = {
'UNIT_CUBE' : np.asarray([
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
]),
'RHOMBIC_DODECAHEDRON_XY_SQR' : np.array([
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[1/2, 1/2, np.sqrt(2.0) / 2.0],
]),
'RHOMBIC_DODECAHEDRON_XY_HEX' : np.array([
[1.0, 0.0, 0.0],
[1/2, np.sqrt(3.0) / 2.0, 0.0],
[1/2, np.sqrt(3.0) / 6.0, np.sqrt(6.0) / 3.0],
]),
'TRUNCATED_OCTAHEDRON' : np.array([
[ 1.0, 0.0, 0.0],
[ 1/3, 2.0 * np.sqrt(2.0) / 3.0, 0.0],
[-1/3, np.sqrt(2.0) / 3.0, np.sqrt(6.0) / 3.0],
]),
} # adapted from https://manual.gromacs.org/current/reference-manual/algorithms/periodic-boundary-conditions.html

for lattice_name, lattice_vectors in COMMON_LATTICE_VECTORS.items():
globals()[lattice_name] = lattice_vectors # dynamically register common lattice vectors at module level


## REPRESENTATION / CONVERSION CLASS
@dataclass
class LatticeParameters: # TODO : add support for OpenMM units
class LatticeParameters: # TODO : add support for OpenMM units, add compat w/ lammpstools.lammpseval
'''For storing the lengths of and the angles between the 3 lattice vectors of a crystallographic unit cell'''
a : float
b : float
Expand Down Expand Up @@ -72,6 +103,11 @@ def axial_angles(self, in_degrees : bool=False) -> np.ndarray[Shape[3], float]:
if in_degrees:
return np.rad2deg(self.angles)
return self.angles

@property
def volume(self) -> float:
'''The volume of the unit cell (in arbitrary units)'''
return np.linalg.det(self.lattice_vectors)

# LATTICE VECTOR METHODS
@classmethod
Expand Down Expand Up @@ -111,8 +147,4 @@ def to_lattice_vectors(self) -> np.ndarray[Shape[3,3], float]:
@property
def lattice_vectors(self) ->np.ndarray[Shape[3,3], float]:
'''Property alias of to_lattice_vectors() method for convenience'''
return self.to_lattice_vectors()

def volume(self) -> float:
'''The volume of the unit cell (in arbitrary units)'''
return np.linalg.det(self.lattice_vectors)
return self.to_lattice_vectors()

0 comments on commit b04ce5b

Please sign in to comment.