Last active
December 3, 2021 14:14
-
-
Save amrhamedp/cd56f192fa0aa38b507874c3f98fe2ae to your computer and use it in GitHub Desktop.
desmond_openmm.py
This file contains hidden or 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
| from __future__ import print_function | |
| from simtk.openmm import app | |
| import simtk.openmm as mm | |
| from simtk import unit | |
| from sys import stdout | |
| import parmed | |
| import sys | |
| import simtk.unit as units | |
| from MDAnalysis.coordinates.DCD import DCDFile | |
| #https://github.com/jeff231li/random-scripts/blob/f4088c855dbe900341b950a9c217e0e575e8c585/python/extract_colvar_openmm.py | |
| #https://github.com/ajkluber/simulation/blob/6ac4ff03da04b13dfc79b3ae80810e1273196ab0/openmm/additional_reporters.py | |
| import simtk.unit as unit | |
| class ForceReporter(object): | |
| def __init__(self, filename, reportInterval): | |
| self._out = open(filename, 'w') | |
| self._reportInterval = reportInterval | |
| def __del__(self): | |
| self._out.close() | |
| def describeNextReport(self, simulation): | |
| steps = self._reportInterval - simulation.currentStep%self._reportInterval | |
| return (steps, False, False, True, False, None) | |
| def report(self, simulation, state): | |
| forces = state.getForces().value_in_unit(unit.kilojoules/unit.mole/unit.nanometer) | |
| f_string = "" | |
| for f in forces: | |
| #f_string += '%g %g %g ' % (f[0], f[1], f[2]) | |
| f_string += '{:>10.2f} {:>10.3f} {:>10.3f} '.format(f[0], f[1], f[2]) | |
| f_string = f_string[:-1] + '\n' | |
| self._out.write(f_string) | |
| sys.stdout.flush() | |
| dms = app.DesmondDMSFile('out.dms') | |
| system = dms.createSystem(nonbondedMethod=app.LJPME, | |
| nonbondedCutoff=9.0*unit.angstroms,OPLS=True,removeCMMotion=False) | |
| import simtk.openmm as openmm | |
| system.addForce(openmm.MonteCarloBarostat(1 * units.bar, 300*unit.kelvin)) | |
| for i, f in enumerate(system.getForces()): | |
| f.setForceGroup(i) | |
| integrator = mm.LangevinIntegrator(300*unit.kelvin, 1.0/unit.picoseconds, | |
| 2.0*unit.femtoseconds) | |
| integrator.setConstraintTolerance(0.00001) | |
| platform = mm.Platform.getPlatformByName('CUDA') | |
| properties = {'CudaPrecision': 'single'} | |
| simulation = app.Simulation(dms.topology, system, integrator, platform, | |
| properties) | |
| import mdtraj as md | |
| a=md.load('a.dcd', top='a.pdb') | |
| index1=0 | |
| import numpy as np | |
| aa=md.load('/desmond_md_job_1_trj/clickme.dtr',top='a.pdb') | |
| with DCDFile("a.dcd") as dcd: | |
| header = dcd.header | |
| for frame in dcd.readframes()[0]: | |
| simulation.context.setPeriodicBoxVectors(a=a.unitcell_vectors[index1][0],b=a.unitcell_vectors[index1][1],c=a.unitcell_vectors[index1][2]) | |
| #simulation.context.setPositions(frame * unit.angstrom)# | |
| simulation.context.setPositions(aa[index1].xyz[0]*unit.nanometer) | |
| state = simulation.context.getState(getForces=True,getEnergy=True) | |
| positions = state.getForces(asNumpy=True) | |
| print(state.getPeriodicBoxVectors()) | |
| print(state.getPotentialEnergy()/unit.kilocalories_per_mole) | |
| index1=index1+1 | |
| for i, f in enumerate(system.getForces()): | |
| state = simulation.context.getState(getEnergy=True, groups={i}) | |
| print(f.__class__, state.getPotentialEnergy()/unit.kilocalories_per_mole) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment