-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from deepmodeling/devel
Merge devel into master
- Loading branch information
Showing
9 changed files
with
626 additions
and
425 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -778,4 +778,7 @@ FodyWeavers.xsd | |
.vscode/** | ||
|
||
# acpype cache | ||
*.acpype/ | ||
*.acpype/ | ||
|
||
*/_date.py | ||
*/_version.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Classical Force Field in DMFF" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"DMFF implements classcial molecular mechanics force fields with the following forms:\n", | ||
"\n", | ||
"$$\\begin{align*}\n", | ||
" V(\\mathbf{R}) &= V_{\\mathrm{bond}} + V_{\\mathrm{angle}} + V_\\mathrm{torsion} + V_\\mathrm{vdW} + V_\\mathrm{Coulomb} \\\\\n", | ||
" &= \\sum_{\\mathrm{bonds}}\\frac{1}{2}k_b(r - r_0)^2 + \\sum_{\\mathrm{angles}}\\frac{1}{2}k_\\theta (\\theta -\\theta_0)^2 + \\sum_{\\mathrm{torsion}}\\sum_{n=1}^4 V_n[1+\\cos(n\\phi - \\phi_s)] \\\\\n", | ||
" &\\quad+ \\sum_{ij}4\\varepsilon_{ij}\\left[\\left(\\frac{\\sigma_{ij}}{r_{ij}}\\right)^{12} - \\left(\\frac{\\sigma_{ij}}{r_{ij}}\\right)^6\\right] + \\sum_{ij}\\frac{q_iq_j}{4\\pi\\varepsilon_0r_{ij}}\n", | ||
"\\end{align*}$$" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Import necessary packages" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import jax\n", | ||
"import jax.numpy as jnp\n", | ||
"import openmm.app as app\n", | ||
"import openmm.unit as unit\n", | ||
"from dmff import Hamiltonian, NeighborList" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Compute energy" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"DMFF uses **OpenMM** to parse input files, including coordinates files, topology specification files. Class `Hamiltonian` inherited from `openmm.ForceField` will be initialized and used to parse force field parameters in XML format. Take parametrzing an organic moleclue with GAFF2 force field as an example.\n", | ||
"\n", | ||
"- `lig_top.xml`: Define bond connections (topology). Not necessary if such information is specified in pdb with `CONNECT` keyword.\n", | ||
"- `gaff-2.11.xml`: GAFF2 force field parameters: bonds, angles, torsions and vdW params\n", | ||
"- `lig-prm.xml`: Atomic charges" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"app.Topology.loadBondDefinitions(\"lig-top.xml\")\n", | ||
"pdb = app.PDBFile(\"lig.pdb\")\n", | ||
"ff = Hamiltonian(\"gaff-2.11.xml\", \"lig-prm.xml\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The method `Hamiltonian.createPotential` will be called to create differentiable potential energy functions for different energy terms. " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"potentials = ff.createPotential(\n", | ||
" pdb.topology,\n", | ||
" nonbondedMethod=app.NoCutoff\n", | ||
")\n", | ||
"for pot in potentials:\n", | ||
" print(pot)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The force field parameters are stored as a Python dict in the `param` attribute of force generators." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"nbparam = ff.getGenerators()[3].params\n", | ||
"nbparam[\"charge\"] # also \"epsilon\", \"sigma\" etc. keys" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Each generated function will read **coordinates, box, pairs** and force field parameters as inputs. `pairs` is a integer array in which each row specifying atoms condsidered as neighbors within rcut. This can be calculated with `dmff.NeighborList` class which is supported by `jax_md`.\n", | ||
"\n", | ||
"The potential energy function will give energy (a scalar, in kJ/mol) as output:\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"positions = jnp.array(pdb.getPositions(asNumpy=True).value_in_unit(unit.nanometer))\n", | ||
"box = jnp.array([\n", | ||
" [10.0, 0.0, 0.0], \n", | ||
" [0.0, 10.0, 0.0],\n", | ||
" [0.0, 0.0, 10.0]\n", | ||
"])\n", | ||
"nbList = NeighborList(box, rc=4)\n", | ||
"nbList.allocate(positions)\n", | ||
"pairs = nbList.pairs\n", | ||
"nbfunc = potentials[-1]\n", | ||
"energy = nbfunc(positions, box, pairs, ff.getGenerators()[-1].params)\n", | ||
"print(energy)\n", | ||
"print(pairs)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"You can also obtain the whole potential energy function and force field parameter set, instead of seperated functions for different energy terms." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"efunc = ff.getPotentialFunc()\n", | ||
"params = ff.getParameters()\n", | ||
"totene = efunc(positions, box, pairs, params)\n", | ||
"totene" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Compute forces and parametric gradients" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Use `jax.grad` to compute forces and parametric gradients automatically" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"pos_grad_func = jax.grad(efunc, argnums=0)\n", | ||
"force = -pos_grad_func(positions, box, pairs, params)\n", | ||
"force.shape" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"param_grad_func = jax.grad(nbfunc, argnums=-1)\n", | ||
"pgrad = param_grad_func(positions, box, pairs, nbparam)\n", | ||
"pgrad[\"charge\"]" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"interpreter": { | ||
"hash": "44fe82502fda871be637af1aa98d2b3ddaac01204dd30f1519cbec4e95000815" | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"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.7.12" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
Oops, something went wrong.