Skip to content

Instantly share code, notes, and snippets.

@zhang-ivy
Created September 22, 2021 14:55
Show Gist options
  • Save zhang-ivy/db2b94cda201541edd05f5cf333efe76 to your computer and use it in GitHub Desktop.
Save zhang-ivy/db2b94cda201541edd05f5cf333efe76 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "970ed216",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Warning: importing 'simtk.openmm' is deprecated. Import 'openmm' instead.\n"
]
}
],
"source": [
"import time\n",
"import os\n",
"import pickle\n",
"\n",
"from simtk.openmm import XmlSerializer\n",
"from simtk import openmm\n",
"from simtk.openmm import app, unit\n",
"import openmmtools"
]
},
{
"cell_type": "markdown",
"id": "269645ac",
"metadata": {},
"source": [
"# Check computation time for setUseDispersionCorrection(True) for only the NonbondedForce that contains nonalchemical atoms, but not the CustomNonbondedForce that contains alchemical atoms"
]
},
{
"cell_type": "markdown",
"id": "378e7055",
"metadata": {},
"source": [
"Use long-range env"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "09632b1e",
"metadata": {},
"outputs": [],
"source": [
"with open('rbd_ace2_example/rbd_ace2_hybrid_system.xml', 'r') as f:\n",
" system = XmlSerializer.deserialize(f.read())\n",
"with open('rbd_ace2_example/rbd_ace2_state.xml', 'r') as f:\n",
" state = XmlSerializer.deserialize(f.read())"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "0848171e",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"[<openmm.openmm.MonteCarloBarostat; proxy of <Swig Object of type 'OpenMM::MonteCarloBarostat *' at 0x2b04dbf2eed0> >,\n",
" <openmm.openmm.CustomBondForce; proxy of <Swig Object of type 'OpenMM::CustomBondForce *' at 0x2b04dbf2e1b0> >,\n",
" <openmm.openmm.HarmonicBondForce; proxy of <Swig Object of type 'OpenMM::HarmonicBondForce *' at 0x2b04dbf2e0f0> >,\n",
" <openmm.openmm.CustomAngleForce; proxy of <Swig Object of type 'OpenMM::CustomAngleForce *' at 0x2b051d42dba0> >,\n",
" <openmm.openmm.HarmonicAngleForce; proxy of <Swig Object of type 'OpenMM::HarmonicAngleForce *' at 0x2b051d42de10> >,\n",
" <openmm.openmm.CustomTorsionForce; proxy of <Swig Object of type 'OpenMM::CustomTorsionForce *' at 0x2b051d42d8d0> >,\n",
" <openmm.openmm.PeriodicTorsionForce; proxy of <Swig Object of type 'OpenMM::PeriodicTorsionForce *' at 0x2b051d42dcf0> >,\n",
" <openmm.openmm.NonbondedForce; proxy of <Swig Object of type 'OpenMM::NonbondedForce *' at 0x2b051d42d120> >,\n",
" <openmm.openmm.CustomNonbondedForce; proxy of <Swig Object of type 'OpenMM::CustomNonbondedForce *' at 0x2b051d42da80> >,\n",
" <openmm.openmm.CustomBondForce; proxy of <Swig Object of type 'OpenMM::CustomBondForce *' at 0x2b051d42dc90> >]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"system.getForces()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "aacc7cd7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"system.getForce(7).getUseDispersionCorrection()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "a2eda49f",
"metadata": {},
"outputs": [],
"source": [
"system.getForce(8).setUseLongRangeCorrection(False)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "b48b609f",
"metadata": {},
"outputs": [],
"source": [
"# Set up integrator\n",
"integrator = openmmtools.integrators.LangevinIntegrator(temperature=300 * unit.kelvin, \n",
" collision_rate=1 / unit.picoseconds, \n",
" timestep=4 * unit.femtoseconds)\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "8e72cca6",
"metadata": {},
"outputs": [],
"source": [
"# Set up context\n",
"platform_name = 'CUDA'\n",
"platform = openmm.Platform.getPlatformByName(platform_name)\n",
"if platform_name in ['CUDA', 'OpenCL']:\n",
" platform.setPropertyDefaultValue('Precision', 'mixed')\n",
"if platform_name in ['CUDA']:\n",
" platform.setPropertyDefaultValue('DeterministicForces', 'true')\n",
"context = openmm.Context(system, integrator, platform)\n",
"context.setPeriodicBoxVectors(*state.getPeriodicBoxVectors())\n",
"context.setPositions(state.getPositions())\n",
"context.setVelocitiesToTemperature(300 * unit.kelvin)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "8381169b",
"metadata": {},
"outputs": [],
"source": [
"nsteps = 100\n",
"initial_time = time.time()\n",
"for step in range(nsteps):\n",
" context.setParameter('lambda_sterics_insert', (step + 1) * 0.01)\n",
" integrator.step(1)\n",
"elapsed_time = (time.time() - initial_time) * unit.seconds"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "6ec993d7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Quantity(value=8.10090708732605, unit=second)"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"elapsed_time"
]
},
{
"cell_type": "markdown",
"id": "8bbdd964",
"metadata": {},
"source": [
"# Check computation time for setLongRangeCorrection(True) for both CustomNonbondedForces"
]
},
{
"cell_type": "markdown",
"id": "277b2307",
"metadata": {},
"source": [
"Use long-range env"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "9e939bce",
"metadata": {},
"outputs": [],
"source": [
"with open('rbd_ace2_example/rbd_ace2_hybrid_system.xml', 'r') as f:\n",
" system = XmlSerializer.deserialize(f.read())\n",
"with open('rbd_ace2_example/rbd_ace2_state.xml', 'r') as f:\n",
" state = XmlSerializer.deserialize(f.read())"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "4f66d4b3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"system.getForce(7).getUseDispersionCorrection()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "0c8fc258",
"metadata": {},
"outputs": [],
"source": [
"system.getForce(8).setUseLongRangeCorrection(True)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "2bae6f98",
"metadata": {},
"outputs": [],
"source": [
"# Set up integrator\n",
"integrator = openmmtools.integrators.LangevinIntegrator(temperature=300 * unit.kelvin, \n",
" collision_rate=1 / unit.picoseconds, \n",
" timestep=4 * unit.femtoseconds)\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "881624d3",
"metadata": {},
"outputs": [],
"source": [
"# Set up context\n",
"platform_name = 'CUDA'\n",
"platform = openmm.Platform.getPlatformByName(platform_name)\n",
"if platform_name in ['CUDA', 'OpenCL']:\n",
" platform.setPropertyDefaultValue('Precision', 'mixed')\n",
"if platform_name in ['CUDA']:\n",
" platform.setPropertyDefaultValue('DeterministicForces', 'true')\n",
"context = openmm.Context(system, integrator, platform)\n",
"context.setPeriodicBoxVectors(*state.getPeriodicBoxVectors())\n",
"context.setPositions(state.getPositions())\n",
"context.setVelocitiesToTemperature(300 * unit.kelvin)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "aadbe82a",
"metadata": {},
"outputs": [],
"source": [
"nsteps = 100\n",
"initial_time = time.time()\n",
"for step in range(nsteps):\n",
" context.setParameter('lambda_sterics_insert', (step + 1) * 0.01)\n",
" integrator.step(1)\n",
"elapsed_time = (time.time() - initial_time) * unit.seconds"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "57ee6d31",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Quantity(value=36.05855202674866, unit=second)"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"elapsed_time"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8c22e1d5",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "e26b353e",
"metadata": {},
"source": [
"# Check computation time for setLongRangeCorrection(True) for both CustomNonbondedForces (and with Peter's fix)"
]
},
{
"cell_type": "markdown",
"id": "99e6326c",
"metadata": {},
"source": [
"Use long-range-peter env"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f3c7ac6d",
"metadata": {},
"outputs": [],
"source": [
"with open('rbd_ace2_example/rbd_ace2_hybrid_system.xml', 'r') as f:\n",
" system = XmlSerializer.deserialize(f.read())\n",
"with open('rbd_ace2_example/rbd_ace2_state.xml', 'r') as f:\n",
" state = XmlSerializer.deserialize(f.read())"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "64722b8a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"system.getForce(7).getUseDispersionCorrection()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "408abbbe",
"metadata": {},
"outputs": [],
"source": [
"system.getForce(8).setUseLongRangeCorrection(True)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "13c8b1ce",
"metadata": {},
"outputs": [],
"source": [
"# Set up integrator\n",
"integrator = openmmtools.integrators.LangevinIntegrator(temperature=300 * unit.kelvin, \n",
" collision_rate=1 / unit.picoseconds, \n",
" timestep=4 * unit.femtoseconds)\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "7d428b9f",
"metadata": {},
"outputs": [],
"source": [
"# Set up context\n",
"platform_name = 'CUDA'\n",
"platform = openmm.Platform.getPlatformByName(platform_name)\n",
"if platform_name in ['CUDA', 'OpenCL']:\n",
" platform.setPropertyDefaultValue('Precision', 'mixed')\n",
"if platform_name in ['CUDA']:\n",
" platform.setPropertyDefaultValue('DeterministicForces', 'true')\n",
"context = openmm.Context(system, integrator, platform)\n",
"context.setPeriodicBoxVectors(*state.getPeriodicBoxVectors())\n",
"context.setPositions(state.getPositions())\n",
"context.setVelocitiesToTemperature(300 * unit.kelvin)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "f542945d",
"metadata": {},
"outputs": [],
"source": [
"nsteps = 100\n",
"initial_time = time.time()\n",
"for step in range(nsteps):\n",
" context.setParameter('lambda_sterics_insert', (step + 1) * 0.01)\n",
" integrator.step(1)\n",
"elapsed_time = (time.time() - initial_time) * unit.seconds"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "23a2999c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Quantity(value=12.859097957611084, unit=second)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"elapsed_time"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "973923ff",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"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.8.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment