Skip to content

Commit

Permalink
Test and document external calculators (stfc#383)
Browse files Browse the repository at this point in the history
* Add docs for external calculators

* Test Lennard Jones calculator
  • Loading branch information
ElliottKasoar authored Jan 17, 2025
1 parent d4d4b32 commit 4e0d1cd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/source/user_guide/python.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,30 @@ will return
If running calculations with multiple MLIPs, ``arch`` and ``mlip_model`` will be overwritten with the most recent MLIP information.
Results labelled by the architecture (e.g. ``mace_mp_energy``) will be saved between MLIPs,
unless the same ``arch`` is chosen, in which case these values will also be overwritten.


Additional Calculators
======================

Although ``janus-core`` only directly supports the MLIP calculators listed in :doc:`Getting started </getting_started/getting_started>`,
any valid `ASE calculator <https://wiki.fysik.dtu.dk/ase/ase/calculators/calculators.html>`_
can be attached to a structure, including calculators for currently unsupported MLIPs.

This structure can then be passed to ``janus-core`` calculations, which can be run as usual.

For example, performing geometry optimisation using the (`ASE built-in <https://wiki.fysik.dtu.dk/ase/ase/calculators/others.html#lennard-jones>`_) Lennard Jones potential calculator:

.. code-block:: python
from janus_core.calculations.geom_opt import GeomOpt
from ase.calculators.lj import LennardJones
from ase.io import read
struct = read("tests/data/NaCl-deformed.cif")
struct.calc = LennardJones()
geom_opt = GeomOpt(
struct=struct,
fmax=0.001,
)
geom_opt.run()
37 changes: 37 additions & 0 deletions tests/test_ase_calculators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Test external ASE calculators."""

from __future__ import annotations

from pathlib import Path

from ase.calculators.lj import LennardJones
from ase.io import read
import numpy as np
import pytest

from janus_core.calculations.geom_opt import GeomOpt
from janus_core.calculations.single_point import SinglePoint

DATA_PATH = Path(__file__).parent / "data"


def test_single_point():
"""Test single point calculation using LJ calculator."""
struct = read(DATA_PATH / "NaCl.cif")
struct.calc = LennardJones()
single_point = SinglePoint(struct=struct, properties="energy")
results = single_point.run()

assert isinstance(struct.calc, LennardJones)
assert results["energy"] == pytest.approx(-0.05900093815771919)


def test_geom_opt():
"""Test geometry optimisation using LJ calculator."""
struct = read(DATA_PATH / "NaCl-deformed.cif")
struct.calc = LennardJones()
geom_opt = GeomOpt(struct=struct, fmax=0.01)
geom_opt.run()

assert isinstance(geom_opt.struct.calc, LennardJones)
assert struct.get_forces() == pytest.approx(np.zeros((8, 3)))

0 comments on commit 4e0d1cd

Please sign in to comment.