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

Implement lattice transform to upper-triangular matrix for ASE Nose-Hoover thermostat #68

Merged
merged 9 commits into from
Sep 12, 2023
25 changes: 25 additions & 0 deletions chgnet/model/dynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ def __init__(
Nose-hoover (constant N, V, T) molecular dynamics.
ASE implementation currently only supports upper triangular lattice
"""
self.upper_triangular_cell()
self.dyn = NPT(
atoms=self.atoms,
timestep=timestep * units.fs,
Expand Down Expand Up @@ -551,6 +552,7 @@ def __init__(
see: https://gitlab.com/ase/ase/-/blob/master/ase/md/npt.py
ASE implementation currently only supports upper triangular lattice
"""
self.upper_triangular_cell()
ptime = taup * units.fs
self.dyn = NPT(
atoms=self.atoms,
Expand Down Expand Up @@ -651,6 +653,29 @@ def set_atoms(self, atoms: Atoms):
self.dyn.atoms = atoms
self.dyn.atoms.calc = calculator

def upper_triangular_cell(self):
"""Transform to upper triangular cell."""
tsihyoung marked this conversation as resolved.
Show resolved Hide resolved
if not NPT._isuppertriangular(self.atoms.get_cell()):
a, b, c, alpha, beta, gamma = self.atoms.cell.cellpar()
ang = np.radians((alpha, beta, gamma))
sina, sinb, sing = np.sin(ang)
cosa, cosb, cosg = np.cos(ang)
cosp = (cosg - cosa * cosb) / (sina * sinb)
if cosp > 1.0:
cosp = 1.0
elif cosp < -1.0:
cosp = -1.0
tsihyoung marked this conversation as resolved.
Show resolved Hide resolved
sinp = np.sqrt(1.0 - cosp**2.0)

new_basis = [
(a * sinb * sinp, a * sinb * cosp, a * cosb),
(0.0, b * sina, b * cosa),
(0.0, 0.0, c),
]

self.atoms.set_cell(new_basis, scale_atoms=True)
print("Transformed to upper triangular unit cell.", flush=True)
tsihyoung marked this conversation as resolved.
Show resolved Hide resolved


class EquationOfState:
"""Class to calculate equation of state."""
Expand Down