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_state_data_reporter(self): """ Test StateDataReporter with various options """ 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) f = open(self.get_fn('akma5.dat', written=True), 'w') sim.reporters.extend([ StateDataReporter(self.get_fn('akma1.dat', written=True), 10), StateDataReporter(self.get_fn('akma2.dat', written=True), 10, time=False, potentialEnergy=False, kineticEnergy=False, totalEnergy=False, temperature=False), StateDataReporter(self.get_fn('akma3.dat', written=True), 10, volume=True, density=True), StateDataReporter(self.get_fn('akma4.dat', written=True), 10, separator='\t'), StateDataReporter(self.get_fn('units.dat', written=True), 10, volume=True, density=True, energyUnit=u.kilojoules_per_mole, volumeUnit=u.nanometers**3), StateDataReporter(f, 10) ]) sim.step(500) f.close() # Now open all of the reporters and check that the information in there # is what we expect it to be akma1 = open(self.get_fn('akma1.dat', written=True), 'r') akma2 = open(self.get_fn('akma2.dat', written=True), 'r') akma3 = open(self.get_fn('akma3.dat', written=True), 'r') akma4 = open(self.get_fn('akma4.dat', written=True), 'r') akma5 = open(self.get_fn('akma5.dat', written=True), 'r') units = open(self.get_fn('units.dat', written=True), 'r') # AKMA 1 first header = akma1.readline().strip()[1:].split(',') self.assertEqual(len(header), 6) for i, label in enumerate( ('Step', 'Time', 'Potential Energy', 'Kinetic Energy', 'Total Energy', 'Temperature')): self.assertTrue(label in header[i]) for i, line in enumerate(akma1): words = line.replace('\n', '').split(',') self.assertEqual(i * 10 + 10, int(words[0])) # step akma1.close() # AKMA 2 header = akma2.readline().strip()[1:].split(',') self.assertEqual(len(header), 1) self.assertTrue('Step' in header[0]) for i, line in enumerate(akma2): self.assertEqual(int(line.replace('\n', '').split(',')[0]), 10 * i + 10) akma2.close() # AKMA 3 -- save energies so we can compare to the file with different # units header = akma3.readline().strip()[1:].split(',') self.assertEqual(len(header), 8) for i, label in enumerate( ('Step', 'Time', 'Potential Energy', 'Kinetic Energy', 'Total Energy', 'Temperature', 'Box Volume', 'Density')): self.assertTrue(label in header[i]) akma_energies = [[0.0 for i in range(8)] for j in range(50)] for i, line in enumerate(akma3): words = line.replace('\n', '').split(',') akma_energies[i][0] = int(words[0]) for j in range(1, 8): akma_energies[i][j] = float(words[j]) akma3.close() # AKMA 4 -- tab delimiter header = akma4.readline().strip()[1:].split('\t') self.assertEqual(len(header), 6) for i, label in enumerate( ('Step', 'Time', 'Potential Energy', 'Kinetic Energy', 'Total Energy', 'Temperature')): self.assertTrue(label in header[i]) for i, line in enumerate(akma4): words = line.replace('\n', '').split('\t') self.assertEqual(i * 10 + 10, int(words[0])) # step akma4.close() # AKMA 5 -- write to open file handle header = akma5.readline().strip()[1:].split(',') self.assertEqual(len(header), 6) for i, label in enumerate( ('Step', 'Time', 'Potential Energy', 'Kinetic Energy', 'Total Energy', 'Temperature')): self.assertTrue(label in header[i]) for i, line in enumerate(akma5): words = line.replace('\n', '').split(',') self.assertEqual(i * 10 + 10, int(words[0])) # step akma5.close() # UNITS -- compare other units ene = u.kilojoule_per_mole.conversion_factor_to(u.kilocalorie_per_mole) volume = u.nanometers.conversion_factor_to(u.angstroms)**3 conversions = [1, 1, ene, ene, ene, 1, volume, 1] headers = units.readline().strip()[1:].split(',') self.assertEqual(len(headers), 8) for i, line in enumerate(units): words = line.replace('\n', '').split(',') self.assertEqual(int(words[0]), akma_energies[i][0]) for j in range(1, 8): self.assertAlmostEqual(float(words[j]) * conversions[j], akma_energies[i][j], places=5) units.close()
# Change to CUDA platform platform = mm.openmm.Platform.getPlatformByName('CUDA') properties = {'CudaDeviceIndex': '1'} # you can add other things like the precision here # Create our Simulation object with our system and chosen integrator sim = app.Simulation(om_solv.topology, system, integrator, platform, properties) # Set the positions from our PDB file sim.context.setPositions(om_crds.positions) # Minimize Energy print("Minimizing Energy") sim.minimizeEnergy() # Reporters print("Appending Reporters") sim.reporters.append(StateDataReporter(sys.stdout, round(steps/10), step=True, potentialEnergy=False, kineticEnergy=False, temperature=True, volume=True, density=False)) # Write out to DCD file sim.reporters.append(app.DCDReporter('tryps_ben_solv.dcd', report_steps)) # Run (100ns) sim.step(steps)