Skip to content

Instantly share code, notes, and snippets.

@mattwthompson
Last active June 6, 2023 17:38
Show Gist options
  • Save mattwthompson/49a3f20a4d5a5d2a91f8bcd39927f39b to your computer and use it in GitHub Desktop.
Save mattwthompson/49a3f20a4d5a5d2a91f8bcd39927f39b to your computer and use it in GitHub Desktop.
issue 620
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Periodic: True, Combine: True\n",
"\t[('NonbondedForce', 'PME')]\n",
"Periodic: True, Combine: False\n",
"\t[('CustomNonbondedForce', 'CutoffPeriodic'), ('NonbondedForce', 'PME')]\n",
"Periodic: False, Combine: True\n",
"\t[('NonbondedForce', 'NoCutoff')]\n",
"Periodic: False, Combine: False\n",
"\tWhen using `openmm.CustomNonbondedForce`, vdW and electrostatics cutoff methods must agree on whether or not periodic boundary conditions should be used.OpenMM will throw an error. Found vdw method 1, and electrostatics method 0, \n"
]
}
],
"source": [
"import openmm\n",
"from openff.toolkit import ForceField, Molecule, Topology\n",
"from openff.units import unit\n",
"\n",
"from openff.interchange import Interchange\n",
"from openff.interchange.exceptions import UnsupportedCutoffMethodError\n",
"\n",
"CUTOFFS = {\n",
" getattr(openmm.NonbondedForce, scheme): scheme\n",
" for scheme in [\"NoCutoff\", \"CutoffNonPeriodic\", \"CutoffPeriodic\", \"Ewald\", \"PME\"]\n",
"}\n",
"\n",
"\n",
"def get_topology(periodic: bool) -> Topology:\n",
" molecule = Molecule.from_smiles(\"CCO\")\n",
"\n",
" topology = molecule.to_topology()\n",
"\n",
" if periodic:\n",
" topology.box_vectors = unit.Quantity(\n",
" [4, 4, 4],\n",
" unit.nanometer,\n",
" )\n",
"\n",
" return topology\n",
"\n",
"\n",
"def get_nonbonded_methods(system: openmm.System) -> list[tuple[openmm.Force, str]]:\n",
" forces = list()\n",
"\n",
" for force in system.getForces():\n",
" if isinstance(force, (openmm.NonbondedForce, openmm.CustomNonbondedForce)):\n",
" forces.append(\n",
" (\n",
" force.__class__.__name__,\n",
" CUTOFFS[force.getNonbondedMethod()],\n",
" )\n",
" )\n",
"\n",
" return forces\n",
"\n",
"\n",
"force_field = ForceField(\"openff-2.1.0.offxml\")\n",
"\n",
"for periodic in [True, False]:\n",
" for combine in [True, False]:\n",
" print(f\"Periodic: {periodic}, Combine: {combine}\")\n",
" try:\n",
" system = Interchange.from_smirnoff(\n",
" topology=get_topology(periodic),\n",
" force_field=force_field,\n",
" ).to_openmm(combine_nonbonded_forces=combine)\n",
"\n",
" print(f\"\\t{get_nonbonded_methods(system)}\")\n",
" except UnsupportedCutoffMethodError as error:\n",
" print(f\"\\t{error}\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{0: 'NoCutoff',\n",
" 1: 'CutoffNonPeriodic',\n",
" 2: 'CutoffPeriodic',\n",
" 3: 'Ewald',\n",
" 4: 'PME'}"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"CUTOFFS"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "openff-interchange-env",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment