Skip to content

Commit

Permalink
Add utility for making calculator
Browse files Browse the repository at this point in the history
Avoid copying code between notebooks
  • Loading branch information
WardLT committed Oct 3, 2023
1 parent 0263fb2 commit f3e6406
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
36 changes: 36 additions & 0 deletions jitterbug/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Utility functions"""
from typing import Optional


from ase.calculators.calculator import Calculator
from ase.calculators.mopac import MOPAC
from ase.calculators.psi4 import Psi4

mopac_methods = ['pm7']
"""List of methods for which we will use MOPAC"""


def make_calculator(method: str, basis: Optional[str], **kwargs) -> Calculator:
"""Make an ASE calculator that implements a desired method.
This function will select the appropriate quantum chemistry code depending
on the method name using the following rules:
1. Use MOPAC if the method is PM7.
2. Use Psi4 otherwise
Any keyword arguments are passed to the calculator
Args:
method: Name of the quantum chemistry method
basis: Basis set name, if appropriate
Returns:
Calculator defined according to the user's settings
"""

if method in mopac_methods:
if basis is not None:
raise ValueError(f'Basis must be none for method: {method}')
return MOPAC(method=method, command='mopac PREFIX.mop > /dev/null')
else:
return Psi4(method=method, basis=basis, **kwargs)
11 changes: 11 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pytest import raises

from jitterbug.utils import make_calculator


def test_make_calculator():
assert make_calculator('b3lyp', 'def2_svpd').name == 'psi4'
assert make_calculator('pm7', None).name == 'mopac'

with raises(ValueError):
make_calculator('pm7', 'wrong')

0 comments on commit f3e6406

Please sign in to comment.