Ejemplo n.º 1
0
 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()
Ejemplo n.º 2
0
    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()