Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some additional information to the 01 module #1

Merged
merged 1 commit into from
Aug 14, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 71 additions & 23 deletions 01 - Creating and importing molecular systems in OpenMM.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"metadata": {
"collapsed": true
},
Expand Down Expand Up @@ -60,7 +60,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 4,
"metadata": {
"collapsed": true
},
Expand All @@ -77,6 +77,8 @@
"# Create a system and add particles to it\n",
"system = openmm.System()\n",
"for index in range(nparticles):\n",
" # Particles are added one at a time\n",
" # Their indices in the System will correspond with their indices in the Force objects we will add later\n",
" system.addParticle(mass)\n",
" \n",
"# Set the periodic box vectors\n",
Expand All @@ -89,7 +91,8 @@
"# Add Lennard-Jones interactions using a NonbondedForce\n",
"force = openmm.NonbondedForce()\n",
"force.setNonbondedMethod(openmm.NonbondedForce.CutoffPeriodic)\n",
"for index in range(nparticles): # all particles must have parameters assigned\n",
"for index in range(nparticles): # all particles must have parameters assigned for the NonbondedForce\n",
" # Particles are assigned properties in the same order as they appear in the System object\n",
" force.addParticle(charge, sigma, epsilon)\n",
"force.setCutoffDistance(3.0 * sigma) # set cutoff (truncation) distance at 3*sigma\n",
"force.setUseSwitchingFunction(True) # use a smooth switching function to avoid force discontinuities at cutoff\n",
Expand Down Expand Up @@ -118,7 +121,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -163,7 +166,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"metadata": {
"collapsed": true
},
Expand All @@ -187,7 +190,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 7,
"metadata": {
"collapsed": true
},
Expand All @@ -205,7 +208,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also specify which platform we want. OpenMM comes with the following platforms:\n",
"We will look at [`Integrator`](http://docs.openmm.org/7.1.0/userguide/application.html#integrators) objects more in depth in [Module 02 - Integrators and Sampling](https://github.com/choderalab/openmm-tutorials/blob/master/02%20-%20Integrators%20and%20sampling.ipynb)\n",
"\n",
"We can also optionally specify which platform we want. OpenMM comes with the following [`Platforms:`](http://docs.openmm.org/7.1.0/userguide/application.html#platforms)\n",
"* [`Reference`](http://docs.openmm.org/7.1.0/userguide/library.html#platforms) - A slow double-precision single-threaded CPU-only implementation intended for comparing fast implementations with a \"verifiably correct\" one\n",
"* [`CPU`](http://docs.openmm.org/7.1.0/userguide/library.html#cpu-platform) - A fast multithreaded mixed-precision CPU-only implementation\n",
"* [`OpenCL`](http://docs.openmm.org/7.1.0/userguide/library.html#opencl-platform) - A highly portable OpenCL implementation that can be used with GPU or CPU OpenCL drivers; supports multiple precision models (single, mixed, double)\n",
Expand All @@ -214,7 +219,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 8,
"metadata": {
"collapsed": true
},
Expand All @@ -227,24 +232,64 @@
"context = openmm.Context(system, integrator, platform)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"The `System` object can be used by multiple `Contexts` because the `System` is copied and converted to the efficient `Platform` specific kernels inside the `Context` object. Changing parameters or adding new `Force`'s on the Python `System` itself will not update the representation inside the `Context`."
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[Quantity(value=(14.766431834276288, 0.0, 0.0), unit=nanometer), Quantity(value=(0.0, 14.766431834276288, 0.0), unit=nanometer), Quantity(value=(0.0, 0.0, 14.766431834276288), unit=nanometer)]\n",
"[Quantity(value=(7.383215917138144, 0.0, 0.0), unit=nanometer), Quantity(value=(0.0, 7.383215917138144, 0.0), unit=nanometer), Quantity(value=(0.0, 0.0, 7.383215917138144), unit=nanometer)]\n"
]
}
],
"source": [
"from copy import deepcopy\n",
"# Create a new integrator we can throw away\n",
"throwaway_integrator = openmm.VerletIntegrator(timestep)\n",
"# Copy the System object for this example\n",
"copy_system = deepcopy(system)\n",
"throwaway_context = openmm.Context(copy_system, throwaway_integrator)\n",
"# Change something in the Python System, e.g. double the box size\n",
"copy_system.setDefaultPeriodicBoxVectors(*box_vectors*2)\n",
"# Observe how the box size in the Context and Python System are different\n",
"print([vector for i, vector in enumerate(copy_system.getDefaultPeriodicBoxVectors())])\n",
"print([vector for i, vector in enumerate(throwaway_context.getState().getPeriodicBoxVectors())])\n",
"# Cleanup\n",
"del throwaway_integrator, throwaway_context"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are ways to update paramters in an existing `Context`, but that topic will be covered in later tutorials.\n",
"\n",
"Once we have a [`Context`](http://docs.openmm.org/7.1.0/api-python/generated/simtk.openmm.openmm.Context.html#simtk.openmm.openmm.Context), we need to tell OpenMM we want to retrieve the energy and forces for a specific set of particle positions. This is done by retrieving a [`State`](http://docs.openmm.org/7.1.0/api-python/generated/simtk.openmm.openmm.State.html#simtk.openmm.openmm.State) object from the [`Context`](http://docs.openmm.org/7.1.0/api-python/generated/simtk.openmm.openmm.Context.html#simtk.openmm.openmm.Context) that contains *only* the information we want to retrieve so as to minimize the amount of data that needs to be sent over the bus from a GPU:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Potential energy: 1.448514147588133e+19 kJ/mol\n",
"Forces: [(-2794733174784.0, -1432761401344.0, 10279611006976.0), (-118015672320.0, 54708154368.0, 54223556608.0), (-56166918914048.0, 39987085049856.0, -1126801604608.0), (253162520576.0, -532917649408.0, 742085033984.0), (146564198694912.0, 104002121367552.0, -27263051497472.0)] kJ/(nm mol)\n"
"Potential energy: 3.2946350692471962e+22 kJ/mol\n",
"Forces: [(13941287682048.0, -182473044525056.0, -150239113641984.0), (-20440577736704.0, 379253497528320.0, 468020203880448.0), (-25137057792.0, 28844783616.0, 37093617664.0), (-580928077824.0, -595612925952.0, -420115972096.0), (719020425216.0, -396371623936.0, -84854677504.0)] kJ/(nm mol)\n"
]
}
],
Expand All @@ -268,20 +313,20 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Forces: [[ -2.79473317e+12 -1.43276140e+12 1.02796110e+13]\n",
" [ -1.18015672e+11 5.47081544e+10 5.42235566e+10]\n",
" [ -5.61669189e+13 3.99870850e+13 -1.12680160e+12]\n",
"Forces: [[ 1.39412877e+13 -1.82473045e+14 -1.50239114e+14]\n",
" [ -2.04405777e+13 3.79253498e+14 4.68020204e+14]\n",
" [ -2.51370578e+10 2.88447836e+10 3.70936177e+10]\n",
" ..., \n",
" [ -4.52482625e+18 7.33504393e+16 1.47632292e+18]\n",
" [ -6.31034282e+11 1.49568052e+13 3.18327960e+12]\n",
" [ -5.04012063e+10 2.29258805e+11 -1.66995001e+11]] kJ/(nm mol)\n"
" [ -5.20011678e+11 8.17781277e+11 -6.13594694e+11]\n",
" [ 4.94299146e+13 1.57116698e+13 3.21396576e+13]\n",
" [ -2.12560333e+09 7.25265715e+09 -5.14150963e+09]] kJ/(nm mol)\n"
]
}
],
Expand All @@ -302,15 +347,15 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Potential energy: 1.448514147588133e+19 kJ/mol\n",
"Force magnitude: 2.142979567352318e+22 kJ/(nm mol)\n"
"Potential energy: 3.2946350692471954e+22 kJ/mol\n",
"Force magnitude: 1.09759694767392e+26 kJ/(nm mol)\n"
]
}
],
Expand Down Expand Up @@ -501,6 +546,7 @@
"# Load in a gromacs system\n",
"from simtk.openmm import app\n",
"gro = app.GromacsGroFile('resources/input.gro')\n",
"# Make sure you change this to your appropriate gromacs include directory!\n",
"gromacs_include_filepath = '/Users/choderaj/miniconda3/share/gromacs/top/'\n",
"top = app.GromacsTopFile('resources/input.top', periodicBoxVectors=gro.getPeriodicBoxVectors(), includeDir=gromacs_include_filepath)\n",
"system = top.createSystem(nonbondedMethod=app.PME, nonbondedCutoff=1*unit.nanometer, constraints=app.HBonds)\n",
Expand Down Expand Up @@ -546,7 +592,9 @@
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Write out a PDB file\n",
Expand Down Expand Up @@ -6956,7 +7004,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
"version": "3.5.0"
}
},
"nbformat": 4,
Expand Down