def testReportersPBC(self): """ Test NetCDF and ASCII restart and trajectory reporters (w/ PBC) """ system = amber_solv.createSystem(nonbondedMethod=app.PME, nonbondedCutoff=8*u.angstroms) integrator = mm.LangevinIntegrator(300*u.kelvin, 5.0/u.picoseconds, 1.0*u.femtoseconds) sim = app.Simulation(amber_solv.topology, system, integrator) sim.context.setPositions(amber_solv.positions) sim.reporters.extend([ NetCDFReporter(get_fn('traj.nc', written=True), 1, vels=True, frcs=True), MdcrdReporter(get_fn('traj.mdcrd', written=True), 1), RestartReporter(get_fn('restart.ncrst', written=True), 1, netcdf=True), RestartReporter(get_fn('restart.rst7', written=True), 1) ]) sim.step(5) for reporter in sim.reporters: reporter.finalize() ntraj = NetCDFTraj.open_old(get_fn('traj.nc', written=True)) atraj = AmberMdcrd(get_fn('traj.mdcrd', written=True), amber_solv.ptr('natom'), True, mode='r') nrst = NetCDFRestart.open_old(get_fn('restart.ncrst', written=True)) arst = AmberAsciiRestart(get_fn('restart.rst7', written=True), 'r') self.assertEqual(ntraj.frame, 5) self.assertEqual(atraj.frame, 5) self.assertTrue(ntraj.hasvels) self.assertTrue(ntraj.hasfrcs) for i in range(ntraj.frame): for x1, x2 in zip(ntraj.box[i], atraj.box[i]): self.assertAlmostEqual(x1, x2, places=3) self.assertEqual(len(nrst.box), 6) self.assertEqual(len(arst.box), 6)
def get_rst_from_nc(self, frame_number, shift, parm): from parmed.amber import AmberParm, Rst7, NetCDFTraj import os traj = NetCDFTraj.open_old(MD._run_dir + "/run" + MD._pattern % self.current_step + ".nc") new_rst7 = Rst7(natom=traj.atom) new_rst7.coordinates = traj.coordinates[frame_number] new_rst7.vels = traj.velocities[frame_number] new_rst7.box = traj.box[frame_number] parm.load_rst7(new_rst7) for r in parm.residues: if r.number > 56 and r.number <= 67: for a in r: a.xx += shift[0] a.xy += shift[1] a.xz += shift[2] parm.write_rst7(MD._run_dir + "/run" + MD._pattern % self.current_step + "mod.rst") parm.save(MD._run_dir + "/run" + MD._pattern % self.current_step + "mod.pdb") if not os.path.isdir(MD._run_dir + "/lock/"): os.mkdir(MD._run_dir + "/lock/") parm.save(MD._run_dir + "/lock/lock_begin.pdb") self._restart_filename = MD._run_dir + "/run" + MD._pattern % self.current_step + "mod.rst"
def test_reporters_pbc(self): """ Test NetCDF and ASCII restart and trajectory reporters (w/ PBC) """ systemsolv = load_file(self.get_fn('ildn.solv.top'), xyz=self.get_fn('ildn.solv.gro')) system = systemsolv.createSystem(nonbondedMethod=app.PME, nonbondedCutoff=8 * u.angstroms) integrator = mm.LangevinIntegrator(300 * u.kelvin, 5.0 / u.picoseconds, 1.0 * u.femtoseconds) sim = app.Simulation(systemsolv.topology, system, integrator, CPU) sim.context.setPositions(systemsolv.positions) sim.reporters.extend([ NetCDFReporter(self.get_fn('traj.nc', written=True), 1, vels=True, frcs=True), MdcrdReporter(self.get_fn('traj.mdcrd', written=True), 1), RestartReporter(self.get_fn('restart.ncrst', written=True), 1, netcdf=True), RestartReporter(self.get_fn('restart.rst7', written=True), 1), StateDataReporter(self.get_fn('state.o', written=True), 1, volume=True, density=True, systemMass=1) ]) sim.step(5) for reporter in sim.reporters: reporter.finalize() ntraj = NetCDFTraj.open_old(self.get_fn('traj.nc', written=True)) atraj = AmberMdcrd(self.get_fn('traj.mdcrd', written=True), len(systemsolv.atoms), True, mode='r') nrst = NetCDFRestart.open_old( self.get_fn('restart.ncrst', written=True)) arst = AmberAsciiRestart(self.get_fn('restart.rst7', written=True), 'r') self.assertEqual(ntraj.frame, 5) self.assertEqual(atraj.frame, 5) self.assertTrue(ntraj.hasvels) self.assertTrue(ntraj.hasfrcs) for i in range(ntraj.frame): for x1, x2 in zip(ntraj.box[i], atraj.box[i]): self.assertAlmostEqual(x1, x2, places=3) self.assertEqual(len(nrst.box), 6) self.assertEqual(len(arst.box), 6) # Make sure the EnergyMinimizerReporter does not fail f = StringIO() rep = EnergyMinimizerReporter(f, volume=True) rep.report(sim) rep.finalize()
def test_reporters_pbc(self): """ Test NetCDF and ASCII restart and trajectory reporters (w/ PBC) """ systemsolv = load_file(get_fn('ildn.solv.top'), xyz=get_fn('ildn.solv.gro')) system = systemsolv.createSystem(nonbondedMethod=app.PME, nonbondedCutoff=8*u.angstroms) integrator = mm.LangevinIntegrator(300*u.kelvin, 5.0/u.picoseconds, 1.0*u.femtoseconds) sim = app.Simulation(systemsolv.topology, system, integrator, CPU) sim.context.setPositions(systemsolv.positions) sim.reporters.extend([ NetCDFReporter(get_fn('traj.nc', written=True), 1, vels=True, frcs=True), MdcrdReporter(get_fn('traj.mdcrd', written=True), 1), RestartReporter(get_fn('restart.ncrst', written=True), 1, netcdf=True), RestartReporter(get_fn('restart.rst7', written=True), 1), StateDataReporter(get_fn('state.o', written=True), 1, volume=True, density=True, systemMass=1) ]) sim.step(5) for reporter in sim.reporters: reporter.finalize() ntraj = NetCDFTraj.open_old(get_fn('traj.nc', written=True)) atraj = AmberMdcrd(get_fn('traj.mdcrd', written=True), len(systemsolv.atoms), True, mode='r') nrst = NetCDFRestart.open_old(get_fn('restart.ncrst', written=True)) arst = AmberAsciiRestart(get_fn('restart.rst7', written=True), 'r') self.assertEqual(ntraj.frame, 5) self.assertEqual(atraj.frame, 5) self.assertTrue(ntraj.hasvels) self.assertTrue(ntraj.hasfrcs) for i in range(ntraj.frame): for x1, x2 in zip(ntraj.box[i], atraj.box[i]): self.assertAlmostEqual(x1, x2, places=3) self.assertEqual(len(nrst.box), 6) self.assertEqual(len(arst.box), 6) # Make sure the EnergyMinimizerReporter does not fail f = StringIO() rep = EnergyMinimizerReporter(f, volume=True) rep.report(sim) rep.finalize()
def parse2new(self, source, ref, traj=None): """ Replace content of a new Trajectory from the source. Args: source (str): file name or other input object ref (str or PDBModel): reference structure instance or file traj (Biskit.md.Trajectory): existing instance to be updated Returns: Biskit.Trajectory: new Trajectory instance """ r = traj if traj is None: import biskit.md r = biskit.md.Trajectory() src = NetCDFTraj.open_old(str(source)) r.frames = src.coordinates r.setRef(B.PDBModel(ref)) r.resIndex = r.ref.resMap() assert N.shape(r.frames) == (src.frame, src.atom, 3) assert r.lenAtoms() == src.atom r.profiles['time'] = src.time if src.hasvels: r.profiles['velocities'] = src.velocities if src.hasfrcs: r.profiles['forces'] = src.forces r.ref.info['application'] = src.application r.ref.info['program'] = src.program return r
def testReporters(self): """ Test NetCDF and ASCII restart and trajectory reporters (no PBC) """ system = amber_gas.createSystem() integrator = mm.LangevinIntegrator(300*u.kelvin, 5.0/u.picoseconds, 1.0*u.femtoseconds) sim = app.Simulation(amber_gas.topology, system, integrator, platform=mm.Platform.getPlatformByName('CPU')) sim.context.setPositions(amber_gas.positions) sim.reporters.extend([ NetCDFReporter(get_fn('traj1.nc', written=True), 10), NetCDFReporter(get_fn('traj2.nc', written=True), 10, vels=True), NetCDFReporter(get_fn('traj3.nc', written=True), 10, frcs=True), NetCDFReporter(get_fn('traj4.nc', written=True), 10, vels=True, frcs=True), NetCDFReporter(get_fn('traj5.nc', written=True), 10, crds=False, vels=True), NetCDFReporter(get_fn('traj6.nc', written=True), 10, crds=False, frcs=True), NetCDFReporter(get_fn('traj7.nc', written=True), 10, crds=False, vels=True, frcs=True), MdcrdReporter(get_fn('traj1.mdcrd', written=True), 10), MdcrdReporter(get_fn('traj2.mdcrd', written=True), 10, crds=False, vels=True), MdcrdReporter(get_fn('traj3.mdcrd', written=True), 10, crds=False, frcs=True), RestartReporter(get_fn('restart.ncrst', written=True), 10, write_multiple=True, netcdf=True), RestartReporter(get_fn('restart.rst7', written=True), 10) ]) sim.step(500) for reporter in sim.reporters: reporter.finalize() self.assertEqual(len(os.listdir(get_fn('writes'))), 61) ntraj = [NetCDFTraj.open_old(get_fn('traj1.nc', written=True)), NetCDFTraj.open_old(get_fn('traj2.nc', written=True)), NetCDFTraj.open_old(get_fn('traj3.nc', written=True)), NetCDFTraj.open_old(get_fn('traj4.nc', written=True)), NetCDFTraj.open_old(get_fn('traj5.nc', written=True)), NetCDFTraj.open_old(get_fn('traj6.nc', written=True)), NetCDFTraj.open_old(get_fn('traj7.nc', written=True))] atraj = [AmberMdcrd(get_fn('traj1.mdcrd', written=True), amber_gas.ptr('natom'), hasbox=False, mode='r'), AmberMdcrd(get_fn('traj2.mdcrd', written=True), amber_gas.ptr('natom'), hasbox=False, mode='r'), AmberMdcrd(get_fn('traj3.mdcrd', written=True), amber_gas.ptr('natom'), hasbox=False, mode='r')] for traj in ntraj: self.assertEqual(traj.frame, 50) self.assertEqual(traj.Conventions, 'AMBER') self.assertEqual(traj.ConventionVersion, '1.0') self.assertEqual(traj.application, 'AmberTools') self.assertEqual(traj.program, 'ParmEd') self.assertFalse(traj.hasbox) self.assertTrue(ntraj[0].hascrds) self.assertFalse(ntraj[0].hasvels) self.assertFalse(ntraj[0].hasfrcs) self.assertTrue(ntraj[1].hascrds) self.assertTrue(ntraj[1].hasvels) self.assertFalse(ntraj[1].hasfrcs) self.assertTrue(ntraj[2].hascrds) self.assertFalse(ntraj[2].hasvels) self.assertTrue(ntraj[2].hasfrcs) self.assertTrue(ntraj[3].hascrds) self.assertTrue(ntraj[3].hasvels) self.assertTrue(ntraj[3].hasfrcs) self.assertFalse(ntraj[4].hascrds) self.assertTrue(ntraj[4].hasvels) self.assertFalse(ntraj[4].hasfrcs) self.assertFalse(ntraj[5].hascrds) self.assertFalse(ntraj[5].hasvels) self.assertTrue(ntraj[5].hasfrcs) self.assertFalse(ntraj[6].hascrds) self.assertTrue(ntraj[6].hasvels) self.assertTrue(ntraj[6].hasfrcs) for i in (0, 2, 3, 4, 5, 6): ntraj[i].close() # still need the 2nd for traj in atraj: traj.close() # Now test the NetCDF restart files fn = get_fn('restart.ncrst.%d', written=True) for i, j in enumerate(range(10, 501, 10)): ncrst = NetCDFRestart.open_old(fn % j) self.assertEqual(ncrst.coordinates.shape, (1, 25, 3)) self.assertEqual(ncrst.velocities.shape, (1, 25, 3)) np.testing.assert_allclose(ncrst.coordinates[0], ntraj[1].coordinates[i]) np.testing.assert_allclose(ncrst.velocities[0], ntraj[1].velocities[i], rtol=1e-6) # Now test the ASCII restart file f = AmberAsciiRestart(get_fn('restart.rst7', written=True), 'r') # Compare to ncrst and make sure it's the same data np.testing.assert_allclose(ncrst.coordinates, f.coordinates, rtol=1e-4) np.testing.assert_allclose(ncrst.velocities, f.velocities, rtol=1e-4)
def test_reporters(self): """ Test NetCDF and ASCII restart and trajectory reporters (no PBC) """ with self.assertRaises(ValueError): NetCDFReporter(self.get_fn('blah', written=True), 1, crds=False) with self.assertRaises(ValueError): MdcrdReporter(self.get_fn('blah', written=True), 1, crds=False) with self.assertRaises(ValueError): MdcrdReporter(self.get_fn('blah', written=True), 1, crds=True, vels=True) system = self.amber_gas.createSystem() integrator = mm.LangevinIntegrator(300 * u.kelvin, 5.0 / u.picoseconds, 1.0 * u.femtoseconds) sim = app.Simulation(self.amber_gas.topology, system, integrator, platform=CPU) sim.context.setPositions(self.amber_gas.positions) sim.reporters.extend([ NetCDFReporter(self.get_fn('traj1.nc', written=True), 10), NetCDFReporter(self.get_fn('traj2.nc', written=True), 10, vels=True), NetCDFReporter(self.get_fn('traj3.nc', written=True), 10, frcs=True), NetCDFReporter(self.get_fn('traj4.nc', written=True), 10, vels=True, frcs=True), NetCDFReporter(self.get_fn('traj5.nc', written=True), 10, crds=False, vels=True), NetCDFReporter(self.get_fn('traj6.nc', written=True), 10, crds=False, frcs=True), NetCDFReporter(self.get_fn('traj7.nc', written=True), 10, crds=False, vels=True, frcs=True), MdcrdReporter(self.get_fn('traj1.mdcrd', written=True), 10), MdcrdReporter(self.get_fn('traj2.mdcrd', written=True), 10, crds=False, vels=True), MdcrdReporter(self.get_fn('traj3.mdcrd', written=True), 10, crds=False, frcs=True), RestartReporter(self.get_fn('restart.ncrst', written=True), 10, write_multiple=True, netcdf=True), RestartReporter(self.get_fn('restart.rst7', written=True), 10), ]) sim.step(500) for reporter in sim.reporters: reporter.finalize() self.assertEqual(len(os.listdir(self._temporary_directory.name)), 61) ntraj = [ NetCDFTraj.open_old(self.get_fn('traj1.nc', written=True)), NetCDFTraj.open_old(self.get_fn('traj2.nc', written=True)), NetCDFTraj.open_old(self.get_fn('traj3.nc', written=True)), NetCDFTraj.open_old(self.get_fn('traj4.nc', written=True)), NetCDFTraj.open_old(self.get_fn('traj5.nc', written=True)), NetCDFTraj.open_old(self.get_fn('traj6.nc', written=True)), NetCDFTraj.open_old(self.get_fn('traj7.nc', written=True)) ] atraj = [ AmberMdcrd(self.get_fn('traj1.mdcrd', written=True), self.amber_gas.ptr('natom'), hasbox=False, mode='r'), AmberMdcrd(self.get_fn('traj2.mdcrd', written=True), self.amber_gas.ptr('natom'), hasbox=False, mode='r'), AmberMdcrd(self.get_fn('traj3.mdcrd', written=True), self.amber_gas.ptr('natom'), hasbox=False, mode='r'), ] for traj in ntraj: self.assertEqual(traj.frame, 50) self.assertEqual(traj.Conventions, 'AMBER') self.assertEqual(traj.ConventionVersion, '1.0') self.assertEqual(traj.application, 'AmberTools') self.assertEqual(traj.program, 'ParmEd') self.assertFalse(traj.hasbox) self.assertTrue(ntraj[0].hascrds) self.assertFalse(ntraj[0].hasvels) self.assertFalse(ntraj[0].hasfrcs) self.assertTrue(ntraj[1].hascrds) self.assertTrue(ntraj[1].hasvels) self.assertFalse(ntraj[1].hasfrcs) self.assertTrue(ntraj[2].hascrds) self.assertFalse(ntraj[2].hasvels) self.assertTrue(ntraj[2].hasfrcs) self.assertTrue(ntraj[3].hascrds) self.assertTrue(ntraj[3].hasvels) self.assertTrue(ntraj[3].hasfrcs) self.assertFalse(ntraj[4].hascrds) self.assertTrue(ntraj[4].hasvels) self.assertFalse(ntraj[4].hasfrcs) self.assertFalse(ntraj[5].hascrds) self.assertFalse(ntraj[5].hasvels) self.assertTrue(ntraj[5].hasfrcs) self.assertFalse(ntraj[6].hascrds) self.assertTrue(ntraj[6].hasvels) self.assertTrue(ntraj[6].hasfrcs) for i in (0, 2, 3, 4, 5, 6): ntraj[i].close() # still need the 2nd for traj in atraj: traj.close() # Now test the NetCDF restart files fn = self.get_fn('restart.ncrst.%d', written=True) for i, j in enumerate(range(10, 501, 10)): ncrst = NetCDFRestart.open_old(fn % j) self.assertEqual(ncrst.coordinates.shape, (1, 25, 3)) self.assertEqual(ncrst.velocities.shape, (1, 25, 3)) np.testing.assert_allclose(ncrst.coordinates[0], ntraj[1].coordinates[i]) np.testing.assert_allclose(ncrst.velocities[0], ntraj[1].velocities[i], rtol=1e-6) # Now test the ASCII restart file f = AmberAsciiRestart(self.get_fn('restart.rst7', written=True), 'r') # Compare to ncrst and make sure it's the same data np.testing.assert_allclose(ncrst.coordinates, f.coordinates, atol=1e-3) np.testing.assert_allclose(ncrst.velocities, f.velocities, rtol=1e-3) # Make sure the EnergyMinimizerReporter does not fail f = StringIO() rep = EnergyMinimizerReporter(f) rep.report(sim, frame=10) rep.finalize()
def run_continue(self): import random from pyxmol import aName, rName, aId, rId from pyxmol.geometry import distance, angle_deg from pyxmol import readPdb import shutil import subprocess import numpy as np import os from parmed.amber import AmberParm, Rst7, NetCDFTraj #self.keep_netcdf = False self.log("Continue > ") self.log("") self.log("Unconstrained simulation") self.log("") ats = self.restricted_structure.asAtoms bond = None step_limit = 5000 self.log("TRAJECTORY_PART_01_FIRST_STEP={}".format(1)) while self.current_step < 500: self.do_md_step() self.put_frame(ats, -1, -1) ats.writeAsPdb( open(MD._run_dir + "/run%05d.pdb" % self.current_step, "w")) os.remove(MD._run_dir + "/run%05d.nc" % self.current_step) self.run_parameters["ntwv"] = -1 parm = AmberParm(MD._build_dir + "/box.prmtop") while self.current_step < step_limit: self.do_md_step() self.put_frame(ats, -1, -1) ats.writeAsPdb( open(MD._run_dir + "/run%05d.pdb" % self.current_step, "w")) if bond is None: best_frame = None best_distance = 40 SG = (ats >> ((aName == "SG") & (rName == "CYM")))[0] C2 = (ats >> ((aName == "C2") & (rName == "LYC")))[0] CL = (ats >> ((aName == "CL") & (rName == "LYC")))[0] O = (ats >> ((aName == "O") & (rName == "PRO") & (rId == 64)))[0] N = (ats >> ((aName == "NE1") & (rName == "TRP") & (rId == 36)))[0] traj = NetCDFTraj.open_old(MD._run_dir + "/run" + MD._pattern % self.current_step + ".nc") for frame in range(1000): rand = 10 self.put_frame(ats, -1, frame) check_dist = distance(N, O) current_dist = distance(SG, C2) shift = np.array([0, 0, 0]) self.log("Check distance: dist = %f" % (check_dist), MD.INFO) self.log("SG-C2 distance: dist = %f" % (current_dist), MD.INFO) if check_dist > 5: check_dist, current_dist, shift = self.get_shift_from_nc_2( frame, 36, "NE1", 64, "O", parm, traj, 32, "SG", 68, "C2") self.log( "Corrected check distance: dist = %f" % (check_dist), MD.INFO) self.log( "Shift: ax = %f, ay = %f, az = %f" % (shift[0], shift[1], shift[2]), MD.INFO) self.log( "Corrected SC-C2 distance: dist = %f" % (current_dist), MD.INFO) if current_dist < 3.3: self.log("Throw the dice...") rand = random.random() if rand < 0.1: self.log("You win. Random is {} ".format(rand), MD.INFO) best_distance = current_dist best_frame = frame best_shift = shift break if best_frame is not None: bond = SG, C2 self.log("best_frame = {}".format(best_frame), MD.INFO) self.log("Bond found: dist = %f" % (best_distance), MD.INFO) else: self.log("Bond isn't found") if bond is not None: self.log("Start gentle lock...") self.log("TRAJECTORY_PART_01_LAST_STEP={}".format( self.current_step - 1)) self.log("TRAJECTORY_PART_02_FIRST_STEP={}".format( self.current_step + 1)) self.write_mod_topology( outname="1_build/box_before_lock.prmtop") lock_steps = 200 lock_nstlim_per_step = 5000 self.gentle_lock(best_frame, lock_steps, lock_nstlim_per_step, best_shift) self.log("Finish gentle lock.") shutil.copyfile(MD._build_dir + "/final_box.mod.prmtop", MD._build_dir + "/box.mod.prmtop") p = subprocess.Popen([ "ambpdb", "-p", MD._build_dir + "/box.mod.prmtop", "-c", self._restart_filename ], stdin=subprocess.PIPE, stdout=subprocess.PIPE) stdout, stderr = p.communicate() with open(self.tleaprc.pdb_output_name + ".pdb", "w") as fout: fout.write(stdout) self.restricted_structure = readPdb( self.tleaprc.pdb_output_name + ".pdb")[0].asResidues >> (rId <= (self.n_residues + 1)) ats = self.restricted_structure.asAtoms self.log("TRAJECTORY_PART_02_LAST_STEP={}".format( self.current_step)) self.log("TRAJECTORY_PART_03_FIRST_STEP={}".format( self.current_step + 1)) step_limit = self.current_step + 500 os.remove(MD._run_dir + "/run%05d.nc" % self.current_step) while self.current_step < 500: self.run_parameters["ig"] = random.randint(1, 100000) self.log("Random seed is %d " % self.run_parameters["ig"]) self.do_md_step() self.put_frame(ats, -1, -1) ats.writeAsPdb( open(MD._run_dir + "/run%05d.pdb" % self.current_step, "w")) os.remove(MD._run_dir + "/run%05d.nc" % self.current_step) self.log("TRAJECTORY_PART_03_LAST_STEP={}".format(self.current_step)) self.save_state("all_done.pickle") self.log("Continue < ")