Skip to content

Commit

Permalink
Allow use of MOPAC in proof of concept
Browse files Browse the repository at this point in the history
  • Loading branch information
WardLT committed Oct 2, 2023
1 parent b7d6b6a commit 202308d
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 192 deletions.
4 changes: 3 additions & 1 deletion envs/environment-cpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ dependencies:
- scikit-learn

# Quantum chemistry
- psi4==1.8.*
- psi4==1.8.* # Remove for MacOS systems
- mopac

# Interatomic forcefields
- dscribe==2.1.*
Expand All @@ -29,4 +30,5 @@ dependencies:

- pip
- pip:
- git+https://gitlab.com/ase/ase.git # Needed for MOPAC
- -e ..[test]
182 changes: 45 additions & 137 deletions notebooks/proof-of-concept/0_get-exact-answer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
"source": [
"from ase.thermochemistry import IdealGasThermo\n",
"from ase.vibrations import VibrationsData, Vibrations\n",
"from ase.calculators.mopac import MOPAC\n",
"from ase.calculators.psi4 import Psi4\n",
"from ase.optimize import QuasiNewton\n",
"from ase import Atoms, units\n",
"from ase.io import write\n",
"from contextlib import redirect_stderr\n",
"from time import perf_counter\n",
"from platform import node\n",
"from pathlib import Path\n",
"from os import devnull\n",
"import numpy as np\n",
"import shutil\n",
"import json\n",
Expand All @@ -53,10 +56,12 @@
},
"outputs": [],
"source": [
"molecule_name = 'caffeine'\n",
"method = 'hf'\n",
"basis = 'def2-svpd'\n",
"threads = min(os.cpu_count(), 12)"
"molecule_name = 'water'\n",
"method = 'pm7'\n",
"mopac_methods = ['pm7'] # Use MOPAC for these methods\n",
"basis = None # Set to None for MOPAC methods\n",
"threads = min(os.cpu_count(), 12)\n",
"assert (method in mopac_methods) == (basis is None), 'Use a basis of None for MOPAC computations'"
]
},
{
Expand Down Expand Up @@ -149,19 +154,12 @@
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" Memory set to 3.815 GiB by Python driver.\n",
" Threads set to 12 by Python driver.\n"
]
}
],
"outputs": [],
"source": [
"calc = Psi4(method=method, basis=basis, num_threads=threads, memory='4096MB')"
"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')"
]
},
{
Expand All @@ -174,58 +172,24 @@
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" Memory set to 3.815 GiB by Python driver.\n",
" Threads set to 12 by Python driver.\n",
" Step[ FC] Time Energy fmax\n",
"BFGSLineSearch: 0[ 0] 15:25:31 -18390.794139 4.1661\n",
"BFGSLineSearch: 1[ 2] 15:26:05 -18391.020663 1.9419\n",
"BFGSLineSearch: 2[ 4] 15:26:41 -18391.134456 1.3043\n",
"BFGSLineSearch: 3[ 6] 15:27:16 -18391.190769 1.0587\n",
"BFGSLineSearch: 4[ 8] 15:27:51 -18391.215384 0.5746\n",
"BFGSLineSearch: 5[ 10] 15:28:27 -18391.226240 0.3911\n",
"BFGSLineSearch: 6[ 12] 15:29:02 -18391.233268 0.3498\n",
"BFGSLineSearch: 7[ 14] 15:29:37 -18391.239126 0.2511\n",
"BFGSLineSearch: 8[ 16] 15:30:12 -18391.244270 0.2340\n",
"BFGSLineSearch: 9[ 18] 15:30:46 -18391.247254 0.1998\n",
"BFGSLineSearch: 10[ 20] 15:31:21 -18391.249083 0.1269\n",
"BFGSLineSearch: 11[ 22] 15:31:56 -18391.249935 0.1215\n",
"BFGSLineSearch: 12[ 24] 15:32:31 -18391.251072 0.1065\n",
"BFGSLineSearch: 13[ 26] 15:33:06 -18391.251900 0.1094\n",
"BFGSLineSearch: 14[ 28] 15:33:41 -18391.252777 0.0838\n",
"BFGSLineSearch: 15[ 30] 15:34:15 -18391.253605 0.1189\n",
"BFGSLineSearch: 16[ 32] 15:34:50 -18391.254853 0.0854\n",
"BFGSLineSearch: 17[ 34] 15:35:24 -18391.255313 0.0749\n",
"BFGSLineSearch: 18[ 36] 15:35:59 -18391.256046 0.0943\n",
"BFGSLineSearch: 19[ 38] 15:36:34 -18391.256326 0.0431\n",
"BFGSLineSearch: 20[ 40] 15:37:09 -18391.256438 0.0387\n",
"BFGSLineSearch: 21[ 42] 15:37:44 -18391.256503 0.0240\n",
"BFGSLineSearch: 22[ 44] 15:38:18 -18391.256571 0.0296\n",
"BFGSLineSearch: 23[ 46] 15:38:53 -18391.256651 0.0230\n",
"BFGSLineSearch: 24[ 48] 15:39:28 -18391.256700 0.0238\n",
"BFGSLineSearch: 25[ 50] 15:40:02 -18391.256791 0.0237\n",
"BFGSLineSearch: 26[ 52] 15:40:37 -18391.256819 0.0183\n",
"BFGSLineSearch: 27[ 54] 15:41:12 -18391.256859 0.0086\n",
"CPU times: user 2h 50min 12s, sys: 5min 56s, total: 2h 56min 8s\n",
"Wall time: 15min 59s\n"
"*Force-consistent energies used in optimization.\n",
"BFGSLineSearch: 0[ 0] 13:34:20 3.054451* 39.6081\n",
"BFGSLineSearch: 1[ 1] 13:34:20 -2.442459* 1.4363\n",
"BFGSLineSearch: 2[ 3] 13:34:20 -2.504294* 0.3803\n",
"BFGSLineSearch: 3[ 5] 13:34:20 -2.506296* 0.0708\n",
"BFGSLineSearch: 4[ 6] 13:34:20 -2.506439* 0.0029\n",
"CPU times: user 35.5 ms, sys: 20.8 ms, total: 56.3 ms\n",
"Wall time: 342 ms\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%time\n",
"atoms.calc = calc\n",
"dyn = QuasiNewton(atoms)\n",
"dyn.run(fmax=0.01)"
"with redirect_stderr(devnull):\n",
" dyn.run(fmax=0.01)"
]
},
{
Expand Down Expand Up @@ -295,8 +259,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 4h 37min 32s, sys: 9min 54s, total: 4h 47min 27s\n",
"Wall time: 26min 18s\n"
"CPU times: user 26.8 ms, sys: 42.5 ms, total: 69.4 ms\n",
"Wall time: 782 ms\n"
]
}
],
Expand Down Expand Up @@ -349,7 +313,7 @@
{
"data": {
"text/plain": [
"5.5067174465850215"
"0.4429860245936006"
]
},
"execution_count": 13,
Expand All @@ -372,83 +336,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "577cd427-7829-4f7b-8457-a6c709c3ea80",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"analytic_time = perf_counter()\n",
"calc.set_psi4(atoms)\n",
"hess = calc.psi4.hessian(f'{method}/{basis}')\n",
"analytic_time = perf_counter() - analytic_time"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0d611176-e94d-44de-a57b-6750625463a1",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"analytic_hess = hess.to_array() * units.Hartree / units.Bohr / units.Bohr"
]
},
{
"cell_type": "markdown",
"id": "9567ec44-fda4-4052-87dc-c6c257adbc4e",
"metadata": {},
"source": [
"Convert it to an ASE object and save"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8a767054-2b20-4a87-8f51-5e715d71d539",
"execution_count": 14,
"id": "da5a4aea-134d-4950-8dbd-df83338983a8",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"vib_data = VibrationsData.from_2d(atoms, analytic_hess)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b5053fa4-e301-472a-8982-1b02c7727d90",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"vib_data = vib.get_vibrations()\n",
"with (out_dir / f'{run_name}-psi4.json').open('w') as fp:\n",
" vib_data.write(fp)"
]
},
{
"cell_type": "markdown",
"id": "36d8ad22-2ad6-45c0-ba9d-0aeed39cb41f",
"metadata": {},
"source": [
"Print the ZPE for reference"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9e9422b7-a62d-49b7-af4a-b433d24804ae",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"vib_data.get_zero_point_energy()"
"if isinstance(calc, Psi4):\n",
" # Compute\n",
" analytic_time = perf_counter()\n",
" calc.set_psi4(atoms)\n",
" hess = calc.psi4.hessian(f'{method}/{basis}')\n",
" analytic_time = perf_counter() - analytic_time\n",
"\n",
" # Convert to ASE format\n",
" analytic_hess = hess.to_array() * units.Hartree / units.Bohr / units.Bohr\n",
" vib_data = VibrationsData.from_2d(atoms, analytic_hess)\n",
" with (out_dir / f'{run_name}-psi4.json').open('w') as fp:\n",
" vib_data.write(fp)\n",
"else:\n",
" analytic_time = None"
]
},
{
Expand All @@ -461,7 +369,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"id": "273b5116-06ec-4098-8b20-537d1f9d7e84",
"metadata": {
"tags": []
Expand Down Expand Up @@ -501,7 +409,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.9.17"
}
},
"nbformat": 4,
Expand Down
48 changes: 14 additions & 34 deletions notebooks/proof-of-concept/1_compute-random-offsets.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"outputs": [],
"source": [
"from ase.calculators.mopac import MOPAC\n",
"from ase.calculators.psi4 import Psi4\n",
"from ase.io import write, read\n",
"from ase.db import connect\n",
Expand Down Expand Up @@ -48,10 +49,12 @@
"outputs": [],
"source": [
"molecule_name = 'caffeine'\n",
"method = 'hf'\n",
"basis = 'def2-svpd'\n",
"method = 'pm7'\n",
"mopac_methods = ['pm7'] # Use MOPAC for these methods\n",
"basis = None # Set to None for MOPAC methods\n",
"threads = min(os.cpu_count(), 12)\n",
"step_size: float = 0.01 # Perturbation amount, used as maximum L2 norm"
"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'"
]
},
{
Expand Down Expand Up @@ -184,19 +187,12 @@
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" Memory set to 3.815 GiB by Python driver.\n",
" Threads set to 12 by Python driver.\n"
]
}
],
"outputs": [],
"source": [
"calc = Psi4(method=method, basis=basis, num_threads=threads, memory='4096MB')"
"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')"
]
},
{
Expand Down Expand Up @@ -241,7 +237,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Already done 427. 2274 left to do.\n"
"Already done 1. 2700 left to do.\n"
]
}
],
Expand All @@ -253,31 +249,15 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"id": "a6fa1b33-defc-4b35-895d-052eb64453fb",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" 0%| | 0/2701 [00:00<?, ?it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" Memory set to 3.815 GiB by Python driver.\n",
" Threads set to 12 by Python driver.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 59%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▍ | 1600/2701 [2:06:01<2:33:24, 8.36s/it]"
"100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2701/2701 [03:10<00:00, 19.27it/s]"
]
}
],
Expand Down
4 changes: 2 additions & 2 deletions notebooks/proof-of-concept/2_approximate-hessians.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
"outputs": [],
"source": [
"molecule_name = 'caffeine'\n",
"method = 'hf'\n",
"basis = 'def2-svpd'\n",
"method = 'pm7'\n",
"basis = None\n",
"step_size: float = 0.005 # Perturbation amount, used as maximum L2 norm\n",
"regressor = LinearRegression"
]
Expand Down
Loading

0 comments on commit 202308d

Please sign in to comment.