Skip to content

Commit

Permalink
Separate notebooks for exact answer, steps in fitting (#14)
Browse files Browse the repository at this point in the history
* Add utility for making calculator

Avoid copying code between notebooks

* Separate exact, sampling and approximation

* Accept "None" as an option
  • Loading branch information
WardLT authored Oct 3, 2023
1 parent 0263fb2 commit 93dc2ab
Show file tree
Hide file tree
Showing 15 changed files with 1,757 additions and 1,359 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 not (basis is None or basis == "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)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
},
"outputs": [],
"source": [
"from ase.calculators.mopac import MOPAC\n",
"from ase.calculators.psi4 import Psi4\n",
"from jitterbug.utils import make_calculator\n",
"from ase.io import write, read\n",
"from ase.db import connect\n",
"from ase import Atoms\n",
Expand All @@ -40,21 +39,17 @@
{
"cell_type": "code",
"execution_count": 2,
"id": "c6be56c5-a460-4acd-9b89-8c3d9c812f5f",
"id": "52252ee2-315c-48bb-8cba-07620e6e2faa",
"metadata": {
"tags": [
"parameters"
]
},
"outputs": [],
"source": [
"molecule_name = 'caffeine'\n",
"method = 'pm7'\n",
"mopac_methods = ['pm7'] # Use MOPAC for these methods\n",
"basis = None # Set to None for MOPAC methods\n",
"starting_geometry = '../data/exact/water_b3lyp_def2-svpd.xyz'\n",
"threads = min(os.cpu_count(), 12)\n",
"step_size: float = 0.01 # Perturbation amount, used as maximum L2 norm\n",
"assert (method in mopac_methods) == (basis is None), 'Use a basis of None for MOPAC computations'"
"step_size: float = 0.005 # Perturbation amount, used as maximum L2 norm"
]
},
{
Expand All @@ -72,7 +67,8 @@
"metadata": {},
"outputs": [],
"source": [
"run_name = f'{molecule_name}_{method}_{basis}'"
"run_name = Path(starting_geometry).name[:-4]\n",
"name, method, basis = run_name.split(\"_\")"
]
},
{
Expand All @@ -95,7 +91,7 @@
{
"data": {
"text/plain": [
"Atoms(symbols='O2N4C8H10', pbc=False, forces=..., calculator=SinglePointCalculator(...))"
"Atoms(symbols='OH2', pbc=False, forces=..., calculator=SinglePointCalculator(...))"
]
},
"execution_count": 4,
Expand All @@ -104,7 +100,7 @@
}
],
"source": [
"atoms = read(Path('data') / 'exact' / f'{run_name}.xyz')\n",
"atoms = read(starting_geometry)\n",
"atoms"
]
},
Expand Down Expand Up @@ -134,8 +130,8 @@
},
"outputs": [],
"source": [
"out_dir = Path('data') / 'approx'\n",
"out_dir.mkdir(exist_ok=True)"
"out_dir = Path('data') / 'random-dir-same-dist'\n",
"out_dir.mkdir(exist_ok=True, parents=True)"
]
},
{
Expand All @@ -147,7 +143,7 @@
},
"outputs": [],
"source": [
"db_path = out_dir / f'{run_name}-random-d={step_size:.2e}.db'"
"db_path = out_dir / f'{run_name}_d={step_size:.2e}.db'"
]
},
{
Expand Down Expand Up @@ -187,12 +183,17 @@
"metadata": {
"tags": []
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Threads set to 1 by Python driver.\n"
]
}
],
"source": [
"if method not in mopac_methods:\n",
" calc = Psi4(method=method, basis=basis, num_threads=threads, memory='4096MB')\n",
"else:\n",
" calc = MOPAC(method=method, command='mopac PREFIX.mop > /dev/null')"
"calc = make_calculator(method, basis, threads=threads)"
]
},
{
Expand All @@ -215,7 +216,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Need to run 2701 calculations for full accuracy.\n"
"Need to run 55 calculations for full accuracy.\n"
]
}
],
Expand All @@ -237,7 +238,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Already done 1. 2700 left to do.\n"
"Already done 1. 54 left to do.\n"
]
}
],
Expand All @@ -257,7 +258,21 @@
"name": "stderr",
"output_type": "stream",
"text": [
"100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2701/2701 [03:10<00:00, 19.27it/s]"
" 0%| | 0/55 [00:00<?, ?it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" Threads set to 1 by Python driver.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 55/55 [00:40<00:00, 1.30it/s]"
]
}
],
Expand Down
Loading

0 comments on commit 93dc2ab

Please sign in to comment.