Skip to content

Commit

Permalink
Add support for using no backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
lohedges committed Apr 22, 2024
1 parent 91dae48 commit bcec107
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
4 changes: 4 additions & 0 deletions bin/emle-server
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,10 @@ if args["log_file"] is None:
if args["save_settings"] is None:
args["save_settings"] = True

# Use the default backend if no backend is specified.
if args["backend"] is None:
args["backend"] = "torchani"

# Use the default backend if no external backend is specified.
if args["external_backend"] is None:
if args["backend"] is None:
Expand Down
44 changes: 26 additions & 18 deletions emle/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,9 @@ def __init__(
region.
backend: str
The backend to use to compute in vacuo energies and gradients.
The backend to use to compute in vacuo energies and gradients. If None,
then no backend will be used, allowing you to obtain the electrostatic
embedding energy and gradients only.
external_backend: str
The name of an external backend to use to compute in vacuo energies.
Expand Down Expand Up @@ -640,19 +642,17 @@ def __init__(
_logger.error(msg)
raise IOError(msg)

if backend is None:
backend = "torchani"

if not isinstance(backend, str):
msg = "'backend' must be of type 'str'"
_logger.error(msg)
raise TypeError(msg)
# Strip whitespace and convert to lower case.
backend = backend.lower().replace(" ", "")
if not backend in self._supported_backends:
msg = f"Unsupported backend '{backend}'. Options are: {', '.join(self._supported_backends)}"
_logger.error(msg)
raise ValueError(msg)
if backend is not None:
if not isinstance(backend, str):
msg = "'backend' must be of type 'str'"
_logger.error(msg)
raise TypeError(msg)
# Strip whitespace and convert to lower case.
backend = backend.lower().replace(" ", "")
if not backend in self._supported_backends:
msg = f"Unsupported backend '{backend}'. Options are: {', '.join(self._supported_backends)}"
_logger.error(msg)
raise ValueError(msg)
self._backend = backend

if external_backend is not None:
Expand Down Expand Up @@ -1316,7 +1316,7 @@ def run(self, path=None):
raise RuntimeError(msg)

# DeePMD.
if self._backend == "deepmd":
elif self._backend == "deepmd":
try:
E_vac, grad_vac = self._run_deepmd(xyz_qm, elements)
except Exception as e:
Expand Down Expand Up @@ -1368,6 +1368,10 @@ def run(self, path=None):
_logger.error(msg)
raise RuntimeError(msg)

# No backend.
else:
E_vac, grad_vac = 0.0, _np.zeros_like(xyz_qm)

# External backend.
else:
try:
Expand All @@ -1380,7 +1384,7 @@ def run(self, path=None):
raise RuntimeError(msg)

# Apply delta-learning corrections using Rascal.
if self._is_delta:
if self._is_delta and self._backend is not None:
try:
delta_E, delta_grad = self._run_rascal(atoms)
except Exception as e:
Expand Down Expand Up @@ -1700,7 +1704,7 @@ def _sire_callback(self, atomic_numbers, charges_mm, xyz_qm, xyz_mm):
raise RuntimeError(msg)

# DeePMD.
if self._backend == "deepmd":
elif self._backend == "deepmd":
try:
E_vac, grad_vac = self._run_deepmd(xyz_qm, elements)
except Exception as e:
Expand Down Expand Up @@ -1754,6 +1758,10 @@ def _sire_callback(self, atomic_numbers, charges_mm, xyz_qm, xyz_mm):
_logger.error(msg)
raise RuntimeError(msg)

# No backend.
else:
E_vac, grad_vac = 0.0, _np.zeros_like(xyz_qm)

# External backend.
else:
try:
Expand All @@ -1767,7 +1775,7 @@ def _sire_callback(self, atomic_numbers, charges_mm, xyz_qm, xyz_mm):
raise RuntimeError(msg)

# Apply delta-learning corrections using Rascal.
if self._is_delta:
if self._is_delta and self._backend is not None:
try:
if atoms is None:
atoms = _ase.Atoms(positions=xyz_qm, numbers=atomic_numbers)
Expand Down

0 comments on commit bcec107

Please sign in to comment.