def test_netcdf_trajectory(): command_refine = [ 'phenix.refine', get_fn('vAla3/vAla3.pdb'), get_fn('vAla3/vAla3.cif'), get_fn('vAla3/vAla3.mtz'), 'topology_file_name={}'.format(get_fn('vAla3/vAla3.prmtop')), 'coordinate_file_name={}'.format(get_fn('vAla3/vAla3.rst7')), 'use_amber=True', 'wxc_scale=0.025', '--overwrite', 'refinement.main.number_of_macro_cycles=1', 'amber.netcdf_trajectory_file_name=hello.nc' ] n_frames = 59 parm = pmd.load_file(get_fn('vAla3/vAla3.prmtop')) expected_boxes = np.array(n_frames * [ [30., 30., 30., 90., 90., 90.], ], dtype='f8') with tempfolder(): output = subprocess.check_output(command_refine) traj = NetCDFTraj.open_old('hello.nc') print(traj.coordinates.shape) aa_eq(traj.box, expected_boxes) assert traj.coordinates.shape == (n_frames, len(parm.atoms), 3)
def testScientificPython(self): """ Test ScientificPython parsing """ import parmed.amber as amber if utils.has_scientific(): amber.use("Scientific") from parmed.amber.netcdffiles import NetCDFTraj, NetCDFRestart traj = NetCDFTraj.open_old(get_fn("tz2.truncoct.nc")) self._check_traj(traj) rst = NetCDFRestart.open_old(get_fn("ncinpcrd.rst7")) self._check_rst(rst) else: self.assertRaises(ImportError, lambda: amber.use("Scientific"))
def testRemdFiles(self): """ Test proper reading and writing of NetCDF files with REMD info """ rstfn = get_fn('restart.ncrst', written=True) trjfn = get_fn('traj.nc', written=True) # Do the restart with T-REMD first traj = NetCDFRestart.open_new( rstfn, 100, True, True, 'Restart w/ REMD', remd='Temperature', temp=300, ) crd = np.random.rand(100, 3) vel = np.random.rand(100, 3) traj.coordinates = crd traj.velocities = vel traj.box = [40, 40, 40, 90, 90, 90] traj.close() traj = NetCDFRestart.open_old(rstfn) self.assertEqual(traj.temp0, 300) np.testing.assert_allclose(crd, traj.coordinates.squeeze(), atol=1e-5) np.testing.assert_allclose(vel, traj.velocities.squeeze(), atol=1e-5) traj = NetCDFRestart.open_new( rstfn, 100, False, True, 'Restart w/ REMD', remd='Multi', remd_dimtypes=[1, 3, 3, 3], ) crd = np.random.rand(100, 3) vel = np.random.rand(100, 3) traj.coordinates = crd traj.velocities = vel remd_indices = np.random.choice(np.arange(20), size=4, replace=True) traj.remd_indices = remd_indices traj.close() traj = NetCDFRestart.open_old(rstfn) np.testing.assert_allclose(crd, traj.coordinates.squeeze(), atol=1e-5) np.testing.assert_allclose(vel, traj.velocities.squeeze(), atol=1e-5) np.testing.assert_equal(remd_indices, traj.remd_indices) np.testing.assert_equal([1, 3, 3, 3], traj.remd_dimtype) # Do the restart with T-REMD first traj = NetCDFTraj.open_new( trjfn, 100, True, True, True, title='Traj w/ REMD', remd='Temperature', ) crd = np.random.rand(100, 3) vel = np.random.rand(100, 3) traj.add_coordinates(crd) traj.add_velocities(vel) traj.add_temp0(300) traj.add_box([40, 40, 40, 90, 90, 90]) traj.close() traj = NetCDFTraj.open_old(trjfn) np.testing.assert_equal(traj.temp0, [300]) np.testing.assert_allclose(crd, traj.coordinates.squeeze(), atol=1e-5) np.testing.assert_allclose(vel, traj.velocities.squeeze(), atol=1e-5) traj = NetCDFTraj.open_new( trjfn, 100, False, True, title='Traj w/ REMD', remd='Multi', remd_dimension=4, vels=True, frcs=True, ) crd = np.random.rand(100, 3) vel = np.random.rand(100, 3) frc = np.random.rand(100, 3) traj.remd_dimtype = [1, 3, 3, 3] traj.add_coordinates(crd * u.angstroms) traj.add_velocities(vel * u.angstroms / u.picosecond) traj.add_forces(frc * u.kilocalories_per_mole / u.angstrom) remd_indices = np.random.choice(np.arange(20), size=4, replace=True) traj.add_remd_indices(remd_indices) traj.close() traj = NetCDFTraj.open_old(trjfn) np.testing.assert_allclose(crd, traj.coordinates.squeeze(), atol=1e-5) np.testing.assert_allclose(vel, traj.velocities.squeeze(), atol=1e-5) np.testing.assert_allclose(frc, traj.forces.squeeze(), atol=1e-5) np.testing.assert_equal(remd_indices, traj.remd_indices.squeeze()) np.testing.assert_equal([1, 3, 3, 3], traj.remd_dimtype)
def testNetCDF(self): """ Test scipy NetCDF parsing """ traj = NetCDFTraj.open_old(get_fn('tz2.truncoct.nc')) self._check_traj(traj) rst = NetCDFRestart.open_old(get_fn('ncinpcrd.rst7')) self._check_rst(rst) # Now try writing files ntraj = NetCDFTraj.open_new(get_fn('test.nc', written=True), traj.atom, box=True) for crd in traj.coordinates: ntraj.add_coordinates(crd) for box in traj.box: ntraj.add_box(box) ntraj.close() traj = NetCDFTraj.open_old(get_fn('test.nc', written=True)) self._check_traj(traj, written=True) nrst = NetCDFRestart.open_new(get_fn('test.ncrst', written=True), rst.atom, rst.hasbox, rst.hasvels, title=rst.title) nrst.coordinates = rst.coordinates nrst.velocities = rst.velocities nrst.time = rst.time nrst.box = rst.box nrst.close() rst = NetCDFRestart.open_old(get_fn('test.ncrst', written=True)) self._check_rst(rst, written=True) # Now write NetCDF trajectory without any optional attributes/variables # and check proper reading natom = randint(100, 1000) nframe = randint(5, 20) coords = np.random.rand(nframe, natom, 3) * 20 - 10 coords = np.array(coords, dtype='f') nctraj = NetCDFFile(get_fn('test2.nc', written=True), 'w', mmap=False) nctraj.Conventions = 'AMBER' nctraj.ConventionVersion = '1.0' nctraj.program = 'ParmEd' nctraj.programVersion = __version__ nctraj.createDimension('frame', None) nctraj.createDimension('spatial', 3) nctraj.createDimension('atom', natom) v = nctraj.createVariable('spatial', 'c', ('spatial', )) v[:] = np.asarray(list('xyz')) v = nctraj.createVariable('coordinates', 'f', ('frame', 'atom', 'spatial')) v[:] = coords del v nctraj.close() traj2 = NetCDFTraj.open_old(get_fn('test2.nc', written=True)) np.testing.assert_allclose(traj2.coordinates, coords) # Now write NetCDF restart without any optional attributes/variables and # check proper reading natom = randint(100, 1000) nframe = randint(5, 20) coords = np.random.rand(natom, 3) * 20 - 10 nctraj = NetCDFFile(get_fn('test2.ncrst', written=True), 'w', mmap=False) nctraj.Conventions = 'AMBERRESTART' nctraj.ConventionVersion = '1.0' nctraj.program = 'ParmEd' nctraj.programVersion = __version__ nctraj.createDimension('frame', None) nctraj.createDimension('spatial', 3) nctraj.createDimension('atom', natom) v = nctraj.createVariable('spatial', 'c', ('spatial', )) v[:] = np.asarray(list('xyz')) v = nctraj.createVariable('coordinates', 'd', ('atom', 'spatial')) v[:] = coords del v nctraj.close() traj2 = NetCDFRestart.open_old(get_fn('test2.ncrst', written=True)) np.testing.assert_allclose(traj2.coordinates[0], coords)
def testRemdFiles(self): """ Test proper reading and writing of NetCDF files with REMD info """ rstfn = get_fn('restart.ncrst', written=True) trjfn = get_fn('traj.nc', written=True) # Do the restart with T-REMD first traj = NetCDFRestart.open_new( rstfn, 100, True, True, 'Restart w/ REMD', remd='Temperature', temp=300, ) crd = np.random.rand(100, 3) vel = np.random.rand(100, 3) traj.coordinates = crd traj.velocities = vel traj.box = [40, 40, 40, 90, 90, 90] traj.close() traj = NetCDFRestart.open_old(rstfn) self.assertEqual(traj.temp0, 300) np.testing.assert_allclose(crd, traj.coordinates.squeeze(), atol=1e-5) np.testing.assert_allclose(vel, traj.velocities.squeeze(), atol=1e-5) traj = NetCDFRestart.open_new( rstfn, 100, False, True, 'Restart w/ REMD', remd='Multi', remd_dimtypes=[1, 3, 3, 3], ) crd = np.random.rand(100, 3) vel = np.random.rand(100, 3) traj.coordinates = crd traj.velocities = vel remd_indices = np.random.choice(np.arange(20), size=4, replace=True) traj.remd_indices = remd_indices traj.close() traj = NetCDFRestart.open_old(rstfn) np.testing.assert_allclose(crd, traj.coordinates.squeeze(), atol=1e-5) np.testing.assert_allclose(vel, traj.velocities.squeeze(), atol=1e-5) np.testing.assert_equal(remd_indices, traj.remd_indices) np.testing.assert_equal([1, 3, 3, 3], traj.remd_dimtype) # Do the restart with T-REMD first traj = NetCDFTraj.open_new( trjfn, 100, True, True, True, title='Traj w/ REMD', remd='Temperature', ) crd = np.random.rand(100, 3) vel = np.random.rand(100, 3) traj.add_coordinates(crd) traj.add_velocities(vel) traj.add_temp0(300) traj.add_box([40, 40, 40, 90, 90, 90]) traj.close() traj = NetCDFTraj.open_old(trjfn) np.testing.assert_equal(traj.temp0, [300]) np.testing.assert_allclose(crd, traj.coordinates.squeeze(), atol=1e-5) np.testing.assert_allclose(vel, traj.velocities.squeeze(), atol=1e-5) traj = NetCDFTraj.open_new( trjfn, 100, False, True, title='Traj w/ REMD', remd='Multi', remd_dimension=4, vels=True, frcs=True, ) crd = np.random.rand(100, 3) vel = np.random.rand(100, 3) frc = np.random.rand(100, 3) traj.remd_dimtype = [1, 3, 3, 3] traj.add_coordinates(crd*u.angstroms) traj.add_velocities(vel*u.angstroms/u.picosecond) traj.add_forces(frc*u.kilocalories_per_mole/u.angstrom) remd_indices = np.random.choice(np.arange(20), size=4, replace=True) traj.add_remd_indices(remd_indices) traj.close() traj = NetCDFTraj.open_old(trjfn) np.testing.assert_allclose(crd, traj.coordinates.squeeze(), atol=1e-5) np.testing.assert_allclose(vel, traj.velocities.squeeze(), atol=1e-5) np.testing.assert_allclose(frc, traj.forces.squeeze(), atol=1e-5) np.testing.assert_equal(remd_indices, traj.remd_indices.squeeze()) np.testing.assert_equal([1, 3, 3, 3], traj.remd_dimtype)
def testNetCDF(self): """ Test scipy NetCDF parsing """ traj = NetCDFTraj.open_old(get_fn('tz2.truncoct.nc')) self._check_traj(traj) rst = NetCDFRestart.open_old(get_fn('ncinpcrd.rst7')) self._check_rst(rst) # Now try writing files ntraj = NetCDFTraj.open_new(get_fn('test.nc', written=True), traj.atom, box=True) for crd in traj.coordinates: ntraj.add_coordinates(crd) for box in traj.box: ntraj.add_box(box) ntraj.close() traj = NetCDFTraj.open_old(get_fn('test.nc', written=True)) self._check_traj(traj, written=True) nrst = NetCDFRestart.open_new(get_fn('test.ncrst', written=True), rst.atom, rst.hasbox, rst.hasvels, title=rst.title) nrst.coordinates = rst.coordinates nrst.velocities = rst.velocities nrst.time = rst.time nrst.box = rst.box nrst.close() rst = NetCDFRestart.open_old(get_fn('test.ncrst', written=True)) self._check_rst(rst, written=True) # Now write NetCDF trajectory without any optional attributes/variables # and check proper reading natom = randint(100, 1000) nframe = randint(5, 20) coords = np.random.rand(nframe, natom, 3) * 20 - 10 coords = np.array(coords, dtype='f') nctraj = NetCDFFile(get_fn('test2.nc', written=True), 'w', mmap=False) nctraj.Conventions = 'AMBER' nctraj.ConventionVersion = '1.0' nctraj.program = 'ParmEd' nctraj.programVersion = __version__ nctraj.createDimension('frame', None) nctraj.createDimension('spatial', 3) nctraj.createDimension('atom', natom) v = nctraj.createVariable('spatial', 'c', ('spatial',)) v[:] = np.asarray(list('xyz')) v = nctraj.createVariable('coordinates', 'f', ('frame', 'atom', 'spatial')) v[:] = coords del v nctraj.close() traj2 = NetCDFTraj.open_old(get_fn('test2.nc', written=True)) np.testing.assert_allclose(traj2.coordinates, coords) # Now write NetCDF restart without any optional attributes/variables and # check proper reading natom = randint(100, 1000) nframe = randint(5, 20) coords = np.random.rand(natom, 3) * 20 - 10 nctraj = NetCDFFile(get_fn('test2.ncrst', written=True), 'w', mmap=False) nctraj.Conventions = 'AMBERRESTART' nctraj.ConventionVersion = '1.0' nctraj.program = 'ParmEd' nctraj.programVersion = __version__ nctraj.createDimension('frame', None) nctraj.createDimension('spatial', 3) nctraj.createDimension('atom', natom) v = nctraj.createVariable('spatial', 'c', ('spatial',)) v[:] = np.asarray(list('xyz')) v = nctraj.createVariable('coordinates', 'd', ('atom', 'spatial')) v[:] = coords del v nctraj.close() traj2 = NetCDFRestart.open_old(get_fn('test2.ncrst', written=True)) np.testing.assert_allclose(traj2.coordinates[0], coords)