Skip to content

Commit

Permalink
FrechetCellFilter class for variable cell relaxation is added
Browse files Browse the repository at this point in the history
  • Loading branch information
kenko911 committed Jun 21, 2024
1 parent 7eb55e5 commit 4171d7b
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/matgl/ext/ase.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from ase import Atoms, units
from ase.calculators.calculator import Calculator, all_changes
from ase.constraints import ExpCellFilter
from ase.filters import FrechetCellFilter
from ase.md import Langevin
from ase.md.andersen import Andersen
from ase.md.npt import NPT
Expand Down Expand Up @@ -253,7 +254,8 @@ def relax(
traj_file: str | None = None,
interval: int = 1,
verbose: bool = False,
params_expcellfilter: dict | None = None,
ase_cellfilter: Literal["Frechet", "Exp"] = "Frechet",
params_asecellfilter: dict | None = None,
**kwargs,
):
"""
Expand All @@ -267,27 +269,34 @@ def relax(
traj_file (str): the trajectory file for saving
interval (int): the step interval for saving the trajectories
verbose (bool): Whether to have verbose output.
params_expcellfilter (dict): Parameters to be passed to ExpCellFilter. Allows
ase_cellfilter (literal): which filter is used for variable cell relaxation. Default is Frechet.
params_asecellfilter (dict): Parameters to be passed to ExpCellFilter or FrechetCellFilter. Allows
setting of constant pressure or constant volume relaxations, for example. Refer to
https://wiki.fysik.dtu.dk/ase/ase/filters.html#the-expcellfilter-class for more information.
https://wiki.fysik.dtu.dk/ase/ase/filters.html#FrechetCellFilter for more information.
**kwargs: Kwargs pass-through to optimizer.
"""
if isinstance(atoms, (Structure, Molecule)):
atoms = self.ase_adaptor.get_atoms(atoms)
atoms.set_calculator(self.calculator)
stream = sys.stdout if verbose else io.StringIO()
params_expcellfilter = params_expcellfilter or {}
params_asecellfilter = params_asecellfilter or {}
with contextlib.redirect_stdout(stream):
obs = TrajectoryObserver(atoms)
if self.relax_cell:
atoms = ExpCellFilter(atoms, **params_expcellfilter)
atoms = (
FrechetCellFilter(atoms, **params_asecellfilter)
if ase_cellfilter == "Frechet"
else ExpCellFilter(atoms, **params_asecellfilter)
)

optimizer = self.optimizer(atoms, **kwargs)
optimizer.attach(obs, interval=interval)
optimizer.run(fmax=fmax, steps=steps)
obs()
if traj_file is not None:
obs.save(traj_file)
if isinstance(atoms, ExpCellFilter):

if isinstance(atoms, (FrechetCellFilter, ExpCellFilter)):
atoms = atoms.atoms

return {
Expand Down

0 comments on commit 4171d7b

Please sign in to comment.