Skip to content

Conversation

yantar92
Copy link
Contributor

@yantar92 yantar92 commented Dec 17, 2024

Summary

Major changes:

  • Implement full selective_dynamics support when converting to/from ASE atoms.

Todos

If this is work in progress, what else needs to be done?

  • I need directions to add tests.

Checklist

  • Google format doc strings added. Check with ruff.
  • Type annotations included. Check with mypy.
  • Tests added for new features/fixes.
  • If applicable, new classes/functions/modules have duecredit @due.dcite decorators to reference relevant papers by DOI (example)

Tip: Install pre-commit hooks to auto-check types and linting before every commit:

pip install -U pre-commit
pre-commit install

* src/pymatgen/io/ase.py (AseAtomsAdaptor.get_atoms): Use FixCartesian
constraint instead of FixAtoms.  FixCartesian can constrain individual
coordinates and directly map selective dynamics settings.
(AseAtomsAdaptor.get_structure): Add mapping from FixCartesian
constraint to selective dynamics.
@jmmshn
Copy link
Contributor

jmmshn commented Dec 26, 2024

Hi @yantar92,

I need directions to add tests.

You can use this example here:

def test_get_structure_dyn(select_dyn):
atoms = ase.io.read(f"{VASP_IN_DIR}/POSCAR")
atoms.set_constraint(ase.constraints.FixAtoms(mask=[True] * len(atoms)))
structure = AseAtomsAdaptor.get_structure(atoms)
assert structure.site_properties["selective_dynamics"][-1][0] is False
structure = Structure(
lattice=Lattice.cubic(5),
species=("Fe", "O"),
coords=((0, 0, 0), (0.5, 0.5, 0.5)),
site_properties={"selective_dynamics": select_dyn},
)
structure.sites[0].selective_dynamics = select_dyn
# mostly testing that this call doesn't raise
ase_atoms = AseAtomsAdaptor.get_atoms(structure)
assert len(ase_atoms) == len(structure)

Also can you detail how this is different than the existing set_constraint method? It looks like some ase classes being used there changed, does this change require an update to the ASE version?

@yantar92
Copy link
Contributor Author

yantar92 commented Dec 27, 2024

Also can you detail how this is different than the existing set_constraint method? It looks like some ase classes being used there changed, does this change require an update to the ASE version?

I replaced FixAtoms ASE constraint with FixCartesian. FixCartesian allows fixing individual coordinates while FixAtoms fixes everything.

FixCartesian: https://gitlab.com/ase/ase/-/blob/master/ase/constraints.py?ref_type=heads#L835
FixAtoms: https://gitlab.com/ase/ase/-/blob/master/ase/constraints.py?ref_type=heads#L208

FixCartesian thus provides everything needed to implement "selective dynamics" from VASP. It is a strict superset of FixAtoms.

FixCartesian is available in ASE since forever (introduced in e2e1799d5, 16 years ago). There should be no issues with compatibility.

* src/pymatgen/io/ase.py (AseAtomsAdaptor.get_atoms): Make sure that
old tests are not failing.  Never create empty constraints.  Prefer
using FixAtoms when possible (as in the past).
* tests/io/test_ase.py (test_get_atoms_from_structure_dyn)
(test_get_structure_dyn): Test mixed coordinate constraint.
@yantar92
Copy link
Contributor Author

yantar92 commented Jan 7, 2025

I added new tests now.

@shyuep shyuep merged commit 4be17da into materialsproject:master Jan 9, 2025
43 checks passed
@shyuep
Copy link
Member

shyuep commented Jan 9, 2025

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants