Skip to content

Commit 4e0d1cd

Browse files
Test and document external calculators (#383)
* Add docs for external calculators * Test Lennard Jones calculator
1 parent d4d4b32 commit 4e0d1cd

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

docs/source/user_guide/python.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,30 @@ will return
5757
If running calculations with multiple MLIPs, ``arch`` and ``mlip_model`` will be overwritten with the most recent MLIP information.
5858
Results labelled by the architecture (e.g. ``mace_mp_energy``) will be saved between MLIPs,
5959
unless the same ``arch`` is chosen, in which case these values will also be overwritten.
60+
61+
62+
Additional Calculators
63+
======================
64+
65+
Although ``janus-core`` only directly supports the MLIP calculators listed in :doc:`Getting started </getting_started/getting_started>`,
66+
any valid `ASE calculator <https://wiki.fysik.dtu.dk/ase/ase/calculators/calculators.html>`_
67+
can be attached to a structure, including calculators for currently unsupported MLIPs.
68+
69+
This structure can then be passed to ``janus-core`` calculations, which can be run as usual.
70+
71+
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:
72+
73+
.. code-block:: python
74+
75+
from janus_core.calculations.geom_opt import GeomOpt
76+
from ase.calculators.lj import LennardJones
77+
from ase.io import read
78+
79+
struct = read("tests/data/NaCl-deformed.cif")
80+
struct.calc = LennardJones()
81+
82+
geom_opt = GeomOpt(
83+
struct=struct,
84+
fmax=0.001,
85+
)
86+
geom_opt.run()

tests/test_ase_calculators.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Test external ASE calculators."""
2+
3+
from __future__ import annotations
4+
5+
from pathlib import Path
6+
7+
from ase.calculators.lj import LennardJones
8+
from ase.io import read
9+
import numpy as np
10+
import pytest
11+
12+
from janus_core.calculations.geom_opt import GeomOpt
13+
from janus_core.calculations.single_point import SinglePoint
14+
15+
DATA_PATH = Path(__file__).parent / "data"
16+
17+
18+
def test_single_point():
19+
"""Test single point calculation using LJ calculator."""
20+
struct = read(DATA_PATH / "NaCl.cif")
21+
struct.calc = LennardJones()
22+
single_point = SinglePoint(struct=struct, properties="energy")
23+
results = single_point.run()
24+
25+
assert isinstance(struct.calc, LennardJones)
26+
assert results["energy"] == pytest.approx(-0.05900093815771919)
27+
28+
29+
def test_geom_opt():
30+
"""Test geometry optimisation using LJ calculator."""
31+
struct = read(DATA_PATH / "NaCl-deformed.cif")
32+
struct.calc = LennardJones()
33+
geom_opt = GeomOpt(struct=struct, fmax=0.01)
34+
geom_opt.run()
35+
36+
assert isinstance(geom_opt.struct.calc, LennardJones)
37+
assert struct.get_forces() == pytest.approx(np.zeros((8, 3)))

0 commit comments

Comments
 (0)