def testBadNetCDFFiles(self): """ Tests error checking for bad usage of NetCDF files """ self.assertRaises(ValueError, lambda: NetCDFRestart.open_new(get_fn('test.ncrst', written=True), natom=10, box=True, vels=True, remd='Temperature') ) self.assertRaises(ValueError, lambda: NetCDFRestart.open_new(get_fn('test.ncrst', written=True), natom=10, box=True, vels=True, remd='Multi') ) self.assertRaises(ValueError, lambda: NetCDFRestart.open_new(get_fn('test.ncrst', written=True), natom=10, box=True, vels=True, remd='Multi', remd_dimtypes=[1, 3, 2]) ) self.assertRaises(ValueError, lambda: NetCDFRestart.open_new(get_fn('test.ncrst', written=True), natom=10, box=True, vels=True, remd='Illegal') ) self.assertRaises(ValueError, lambda: NetCDFTraj.open_new(get_fn('test.nc', written=True), natom=10, box=True, vels=False, remd='Multidim') ) self.assertRaises(ValueError, lambda: NetCDFTraj.open_new(get_fn('test.nc', written=True), natom=10, box=True, vels=False, remd='Illegal') )
def testBadNetCDFFiles(self): """ Tests error checking for bad usage of NetCDF files """ self.assertRaises( ValueError, lambda: NetCDFRestart.open_new(get_fn('test.ncrst', written=True), natom=10, box=True, vels=True, remd='Temperature')) self.assertRaises( ValueError, lambda: NetCDFRestart.open_new(get_fn('test.ncrst', written=True), natom=10, box=True, vels=True, remd='Multi')) self.assertRaises( ValueError, lambda: NetCDFRestart.open_new(get_fn('test.ncrst', written=True), natom=10, box=True, vels=True, remd='Multi', remd_dimtypes=[1, 3, 2])) self.assertRaises( ValueError, lambda: NetCDFRestart.open_new(get_fn('test.ncrst', written=True), natom=10, box=True, vels=True, remd='Illegal')) self.assertRaises( ValueError, lambda: NetCDFTraj.open_new(get_fn('test.nc', written=True), natom=10, box=True, vels=False, remd='Multidim')) self.assertRaises( ValueError, lambda: NetCDFTraj.open_new(get_fn('test.nc', written=True), natom=10, box=True, vels=False, remd='Illegal'))
def report(self, simulation, state): """Generate a report. Parameters ---------- simulation : :class:`app.Simulation` The Simulation to generate a report for state : :class:`mm.State` The current state of the simulation """ global VELUNIT, FRCUNIT if self.crds: crds = state.getPositions().value_in_unit(u.angstrom) if self.vels: vels = state.getVelocities().value_in_unit(VELUNIT) if self.frcs: frcs = state.getForces().value_in_unit(FRCUNIT) if self._out is None: # This must be the first frame, so set up the trajectory now if self.crds: atom = len(crds) elif self.vels: atom = len(vels) elif self.frcs: atom = len(frcs) self.uses_pbc = simulation.topology.getUnitCellDimensions() is not None self._out = NetCDFTraj.open_new( self.fname, atom, self.uses_pbc, self.crds, self.vels, self.frcs, title="ParmEd-created trajectory using OpenMM" ) if self.uses_pbc: vecs = state.getPeriodicBoxVectors() lengths, angles = box_vectors_to_lengths_and_angles(*vecs) self._out.add_cell_lengths_angles(lengths.value_in_unit(u.angstrom), angles.value_in_unit(u.degree)) # Add the coordinates, velocities, and/or forces as needed if self.crds: self._out.add_coordinates(crds) if self.vels: # The velocities get scaled right before writing self._out.add_velocities(vels) if self.frcs: self._out.add_forces(frcs) # Now it's time to add the time. self._out.add_time(state.getTime().value_in_unit(u.picosecond))
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)
def get_amber_struct_object(params): amber_params = params.amber ridingH = True if getattr(params, "hydrogens", False): # ensemble refinement does not have this if params.hydrogens.refine in ['riding', 'Auto']: ridingH = True elif params.hydrogens.refine in ['individual']: ridingH = False else: raise Sorry("Hydrogens.refine parameter '%s' unknown!" % params.hydrogens.refine) amber_structs = amber_adaptbx.SanderStruct( parm_file_name=amber_params.topology_file_name, rst_file_name=amber_params.coordinate_file_name, ridingH=ridingH, ) if amber_params.bellymask: try: amber_structs.inp.ibelly = 1 amber_structs.inp.bellymask = amber_params.bellymask except AttributeError: raise AttributeError( 'Setting bellymask for pysander does not work with AmberTools <= 16') if amber_params.restraint_wt > 0.: try: amber_structs.inp.ntr = 1 amber_structs.inp.restraint_wt = amber_params.restraint_wt amber_structs.inp.restraintmask = amber_params.restraintmask if amber_params.reference_file_name: amber_structs.inp.refc = amber_params.reference_file_name else: amber_structs.inp.refc = amber_params.coordinate_file_name except AttributeError as e: raise AttributeError( 'Setting amber restraint for pysander does not work with AmberTools <= 16') amber_structs.qm_inp=amber_structs.sander_engine.QmInputOptions() if amber_params.qmmask: amber_structs.inp.ifqnt=1 amber_structs.qm_inp.qm_theory = 'PM6' amber_structs.qm_inp.qmmask = amber_params.qmmask amber_structs.qm_inp.qmcut = 8.0 amber_structs.qm_inp.qmcharge= amber_params.qmcharge amber_structs.qm_inp.diag_routine=1 else: amber_structs.inp.ifqnt=0 if not amber_structs.sander_engine.is_setup(): amber_structs.sander_engine.setup( amber_structs.parm, amber_structs.parm.coordinates, amber_structs.parm.box, amber_structs.inp, amber_structs.qm_inp ) if amber_params.order_file_name is not None: order_map_file_name = amber_params.order_file_name amber_structs.order_map_file_name = order_map_file_name mapped_arr = np.loadtxt(order_map_file_name, dtype='i4').transpose() amber_structs.order_converter = dict(a2p=mapped_arr[0], p2a=mapped_arr[1]) if amber_params.netcdf_trajectory_file_name: n_atoms = len(amber_structs.parm.atoms) amber_structs.writer = NetCDFTraj.open_new(amber_params.netcdf_trajectory_file_name, n_atoms, box=True, crds=True, frcs=False) return amber_structs, amber_structs.sander_engine