def create_test_trj(uni, filename): """uses pyh5md library to write h5md file""" with pyh5md.File(filename, 'w', creator='create_h5md_data.py') as f: trajectory = f.particles_group('trajectory') obsv = f.require_group('observables') positions = pyh5md.element(trajectory, 'position', store='time', data=uni.trajectory.ts.positions, time=True) f['particles/trajectory/position/value'].attrs['unit'] = 'Angstrom' f['particles/trajectory/position/time'].attrs['unit'] = 'ps' velocities = pyh5md.element(trajectory, 'velocity', data=uni.trajectory.ts.velocities, step_from=positions, store='time', time=True) f['particles/trajectory/velocity/value'].attrs['unit'] = 'Angstrom ps-1' f['particles/trajectory/velocity/time'].attrs['unit'] = 'ps' forces = pyh5md.element(trajectory, 'force', data=uni.trajectory.ts.forces, step_from=positions, store='time', time=True) f['particles/trajectory/force/value'].attrs['unit'] = 'kJ mol-1 Angstrom-1' f['particles/trajectory/force/time'].attrs['unit'] = 'ps' data_step = pyh5md.element(obsv, 'step', data=uni.trajectory.ts.data['step'], step_from=positions, store='time', time=True) data_lambda = pyh5md.element(obsv, 'lambda', data=uni.trajectory.ts.data['lambda'], step_from=positions, store='time', time=True) trajectory.create_box(dimension=3, boundary=['periodic', 'periodic', 'periodic'], store='time', data=uni.trajectory.ts.triclinic_dimensions, step_from=positions) f['particles/trajectory/box/edges/value'].attrs['unit'] = 'Angstrom' for ts in uni.trajectory: trajectory.box.edges.append(uni.trajectory.ts.triclinic_dimensions, ts.frame, time=ts.time) positions.append(uni.trajectory.ts.positions, ts.frame, time=ts.time) velocities.append(uni.trajectory.ts.velocities, ts.frame, time=ts.time) forces.append(uni.trajectory.ts.forces, ts.frame, time=ts.time) data_step.append(uni.trajectory.ts.data['step'], ts.frame, time=ts.time) data_lambda.append(uni.trajectory.ts.data['lambda'], ts.frame, time=ts.time)
def test_pyh5md_bench(): u = mda.Universe(TPR_xvf, H5MD_xvf) ca = u.select_atoms("protein and name CA") indices = ca.indices with pyh5md.File(H5MD_xvf, 'r') as f: trajectory = f.particles_group('trajectory') for i in range(len(u.trajectory)): u.trajectory[i] assert_array_equal( ca.positions, pyh5md.element(trajectory, 'position').value[i, indices, :])
def create_test_trj(uni, filename): """uses pyh5md library to write h5md file""" with pyh5md.File(filename, 'w', creator='dcd_to_h5md.py') as f: trajectory = f.particles_group('trajectory') obsv = f.require_group('observables') ag = uni.select_atoms( "(resid 1:29 or resid 60:121 or resid 160:214) and name CA") indices = ag.indices positions = pyh5md.element(trajectory, 'position', store='time', data=uni.trajectory.ts.positions[indices], time=True) f['particles/trajectory/position/value'].attrs['unit'] = 'Angstrom' f['particles/trajectory/position/time'].attrs['unit'] = 'ps' data_step = pyh5md.element(obsv, 'step', data=uni.trajectory.ts.data['step'], step_from=positions, store='time', time=True) data_dt = pyh5md.element(obsv, 'dt', data=uni.trajectory.ts.data['dt'], step_from=positions, store='time', time=True) trajectory.create_box(dimension=3, boundary=['periodic', 'periodic', 'periodic'], store='time', data=uni.trajectory.ts.triclinic_dimensions, step_from=positions) f['particles/trajectory/box/edges/value'].attrs['unit'] = 'Angstrom' for ts in uni.trajectory: trajectory.box.edges.append(ts.triclinic_dimensions, ts.frame, time=ts.time) positions.append(ts.positions[indices], ts.frame, time=ts.time) data_step.append(ts.data['step'], ts.frame, time=ts.time) data_dt.append(ts.data['dt'], ts.frame, time=ts.time)
def benchmark_pyh5md_rmsd(filename): with pyh5md.File(filename, 'r') as f: u = mda.Universe("testfiles/cobrotoxin.tpr", filename) indices = u.select_atoms("protein and name CA").indices trajectory = f.particles_group('trajectory') observables = f.require_group('observables') pos0 = pyh5md.element(trajectory, 'position').value[0, indices, :] x_ref = pos0.copy() n_frames = pyh5md.element(trajectory, 'position').value.shape[0] total_io = 0 total_rmsd = 0 total_loop = -time.time() for frame in range(n_frames): start_io = time.time() positions = pyh5md.element(trajectory, 'position').value[frame, indices] velocities = pyh5md.element(trajectory, 'velocity').value[frame, indices] forces = pyh5md.element(trajectory, 'force').value[frame, indices] box = pyh5md.element(trajectory, 'box/edges').value[frame] data_step = pyh5md.element(observables, 'step').value[frame] data_lambda = pyh5md.element(observables, 'lambda').value[frame] data_time = pyh5md.element(trajectory, 'position').time[frame] total_io += time.time() - start_io start_rmsd = time.time() result = rmsd(positions, x_ref, superposition=True) total_rmsd += time.time() - start_rmsd total_loop += time.time() return {"Loop": total_loop, "Loop_per_frame": (total_loop/n_frames), "I/O": total_io, "I/O_per_frame": (total_io/n_frames), "RMSD": total_rmsd, "RMSD_per_frame": (total_rmsd/n_frames), "Overhead": (total_loop - (total_io + total_rmsd)), "Overhead_per_frame": ((total_loop - (total_io + total_rmsd))/n_frames)}
import pyh5md import MDAnalysis as mda from MDAnalysis.tests.datafiles import TRR # Open an H5MD file to read with pyh5md.File('TRR3.h5', 'r') as f: # extract particles group particles = f.particles_group('particles') # extract particle postitions and velocities with pyh5md.element function part_pos = pyh5md.element(particles, 'position') part_vel = pyh5md.element(particles, 'velocity') # extract observables data from f group com = pyh5md.element(f, 'observables/center_of_mass') mean_vel = pyh5md.element(f, 'observables/mean_velocity')
def __init__(self, system, filename, store_position=True, store_species=True, store_state=False, store_velocity=False, store_force=False, store_charge=False, store_lambda=False, store_res_id=True, store_mass=True, static_box=True, is_adress=False, group_name='atoms', author='xxx', email='xxx', chunk_size=256, is_single_prec=False, do_sort=True): """ Args: system: The system object. filename: The name of hdf file name. group_name: The name of atom groups. (default: 'atoms'). store_position: If set to True then position will be stored. (default: True) store_species: If set to True then species will be stored. (default: True) store_state: If set to True then state will be stored. (default: False) store_velocity: If set to True then velocity will be stored. (default: False) store_force: If set to True then force will be stored. (default: False) store_charge: If set to True then charge will be stored. (default: False) store_lambda: If set to True then lambda (AdResS) will be stored. (default: False) store_res_id: If set to True then store res_id. (default: False) store_mass: If set to True then store mass. (default: True) static_box: If set to True then box is static (like in NVT ensemble) (default: True) is_adress: If set to True then AdResS particles will be save instead of coarse-grained. author: The name of author of the file. (default: xxx) email: The e-mail to author of that file. (default: xxx) chunk_size: The size of data chunk. (default: 128) is_single_prec: Use single precision instead of double. do_sort: If set to True then HDF5 will be sorted on close. """ if not pmi.workerIsActive(): return cxxinit(self, io_DumpH5MD, system, is_adress) self.filename = filename self.group_name = group_name self.store_position = store_position self.store_species = store_species self.store_state = store_state self.store_velocity = store_velocity self.store_force = store_force self.store_charge = store_charge self.store_lambda = store_lambda self.store_res_id = store_res_id self.store_mass = store_mass self.static_box = static_box self.chunk_size = chunk_size self.do_sort = do_sort self.single_prec = is_single_prec self.system = system if pmi.isController: if os.path.exists(filename): basename = os.path.basename(filename) dirname = os.path.dirname(filename) new_filename = '{}/{}_{}'.format(dirname, int(py_time.time()), os.path.basename(filename)) os.rename(filename, new_filename) print('File {} exists, moved to {}'.format( filename, new_filename)) try: self.file = pyh5md.File( filename, 'w', creator='espressopp', creator_version=espressopp.VersionLocal().info(), author=author, author_email=email, driver='mpio', comm=MPI.COMM_WORLD) except AttributeError: self.file = pyh5md.File( filename, 'w', creator='espressopp', creator_version=espressopp.VersionLocal().info(), author=author, author_email=email) self._system_data() self.float_type = np.float32 if is_single_prec else np.float64 self.int_type = np.int32 if is_single_prec else np.int part = self.file.particles_group(self.group_name) self.particle_group = part if self.static_box: self.box = part.create_box( dimension=3, boundary=['periodic', 'periodic', 'periodic'], store='fixed', data=np.array([ed_i for ed_i in self.system.bc.boxL], dtype=self.float_type)) else: self.box = part.box(dimension=3, boundary=['periodic', 'periodic', 'periodic'], store='time', time=True, data=np.zeros(3, dtype=self.float_type)) self.id_e = pyh5md.element(part, 'id', store='time', time=True, shape=(self.chunk_size, ), maxshape=(None, ), dtype=self.int_type, fillvalue=-1) if store_mass: self.mass = pyh5md.element(part, 'mass', store='time', time=True, maxshape=(None, ), shape=(self.chunk_size, ), dtype=self.float_type, fillvalue=-1) if self.store_position: self.position = pyh5md.element(part, 'position', store='time', time=True, maxshape=(None, 3), shape=(self.chunk_size, 3), dtype=self.float_type) self.image = pyh5md.element(part, 'image', store='time', time=True, maxshape=(None, 3), shape=(self.chunk_size, 3), dtype=self.float_type) if self.store_species: self.species = pyh5md.element(part, 'species', store='time', time=True, maxshape=(None, ), shape=(self.chunk_size, ), dtype=self.int_type, fillvalue=-1) if self.store_state: self.state = pyh5md.element(part, 'state', store='time', time=True, maxshape=(None, ), shape=(self.chunk_size, ), dtype=self.int_type, fillvalue=-1) if self.store_velocity: self.velocity = pyh5md.element(part, 'velocity', store='time', time=True, maxshape=(None, 3), shape=(self.chunk_size, 3), dtype=self.float_type) if self.store_force: self.force = pyh5md.element(part, 'force', store='time', time=True, maxshape=(None, 3), shape=(self.chunk_size, 3), dtype=self.float_type) if self.store_charge: self.charge = pyh5md.element(part, 'charge', store='time', time=True, maxshape=(None, ), shape=(self.chunk_size, ), dtype=self.float_type, fillvalue=-1) if self.store_lambda: self.lambda_adr = pyh5md.element(part, 'lambda_adr', store='time', time=True, maxshape=(None, ), shape=(self.chunk_size, ), dtype=self.float_type, fillvalue=-1) if self.store_res_id: self.res_id = pyh5md.element(part, 'res_id', store='time', time=True, maxshape=(None, ), shape=(self.chunk_size, ), dtype=self.int_type, fillvalue=-1) self._system_data() self.commTimer = 0.0 self.updateTimer = 0.0 self.writeTimer = 0.0 self.flushTimer = 0.0 self.closeTimer = 0.0 self.resizeCounter = 0
import numpy as np import h5py import pyh5md import MDAnalysis as mda from MDAnalysis.tests.datafiles import TPR_xvf, TRR_xvf u = mda.Universe(TPR_xvf, TRR_xvf) # open h5 file: f = pyh5md.File('cobrotoxin.h5md', 'r') # extract particles group atoms = f.particles_group('trajectory') # extract datasets from .h5 file with pyh5md.element function box = pyh5md.element(atoms['box'], 'edges').value[:] positions = pyh5md.element(atoms, 'positions').value[:] velocities = pyh5md.element(atoms, 'velocities').value[:] forces = pyh5md.element(atoms, 'forces').value[:] n_atoms = pyh5md.element(atoms, 'n_atoms').value[()] #TESTS print('Box Dimensions test (True=Pass False=Fail):') for ts, x in zip(u.trajectory, box): print('Timestep', u.trajectory.ts.frame, np.allclose(u.trajectory.ts.triclinic_dimensions, x)) print('Positions test (True=Pass False=Fail):') for ts, x in zip(u.trajectory, positions): print('Timestep', u.trajectory.ts.frame, np.allclose(u.atoms.positions, x))
parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('file', type=str, help='H5MD datafile') parser.add_argument('--plot', action='store_true', help='Display the result graphically') parser.add_argument('--no-q', action='store_true', help='Do not use quaternion data') args = parser.parse_args() import numpy as np import matplotlib.pyplot as plt from transforms3d import quaternions import pyh5md with pyh5md.File(args.file, 'r') as f: obs = f['observables'] u = pyh5md.element(obs, 'u').value[:] if 'q' in obs: q = pyh5md.element(obs, 'q').value[:] else: q = None janus_vel = pyh5md.element(obs, 'janus_vel') dt = janus_vel.time janus_vel = janus_vel.value[:] one_z = np.array([0., 0., 1.]) # Change of quaternion storage convention if q is not None and not args.no_q: tmp_s = q[:, 3].copy()
import pyh5md import MDAnalysis as mda from MDAnalysis.tests.datafiles import TPR_xvf, TRR_xvf u = mda.Universe(TPR_xvf, TRR_xvf) # Open a H5MD file to write with pyh5md.File('cobrotoxin.h5md', 'w', creator='write_h5md.py') as f: # Add a trajectory group into particles group trajectory = f.particles_group('trajectory') # Add the positions, velocities, forces, masses, n_atoms groups into the trajectory group trajectory_positions = pyh5md.element(trajectory, 'positions', store='time', data=u.trajectory.ts.positions, time=True) trajectory_velocities = pyh5md.element(trajectory, 'velocities', data=u.trajectory.ts.velocities, step_from=trajectory_positions, store='time', time=True) trajectory_forces = pyh5md.element(trajectory, 'forces', data=u.trajectory.ts.forces, step_from=trajectory_positions, store='time', time=True) trajectory_n_atoms = pyh5md.element(trajectory,
import numpy as np import pyh5md import MDAnalysis as mda from MDAnalysis.tests.datafiles import TRR # Create the trajectory data u = mda.Universe(TRR) positions = u.atoms.positions # Open a H5MD file to write with pyh5md.File('TRR3.h5', 'w', author='Edis') as f: ts = u.trajectory.ts step_from = mda.lib.util.Namespace(step=ts.frame, time=ts.time) # Add a trajectory group particles = f.particles_group('particles') # Add the position data element in the trajectory group part_pos = pyh5md.element(particles, 'position', step_from=step_from, store='time', shape=positions.shape, dtype=positions.dtype, time=True) # Add the velocity data element into the trajectory group part_vel = pyh5md.element(particles, 'velocity', step_from=step_from, store='time',
import numpy as np import pyh5md import MDAnalysis as mda from MDAnalysis.tests.datafiles import TRR # initialize stock TRR example: u = mda.Universe(TRR) # open h5 file: f = pyh5md.File('TRR3.h5', 'r') # extract particles group particles = f.particles_group('particles') # extract particle postitions and velocities with pyh5md.element function part_pos = pyh5md.element(particles, 'position').value[:] part_vel = pyh5md.element(particles, 'velocity').value[:] # extract observables data from f group com = pyh5md.element(f, 'observables/center_of_mass').value[:] mean_vel = pyh5md.element(f, 'observables/mean_velocity').value[:] f.close() #TEST POSITION print('positions tests:') for ts in range(len(u.trajectory)): diff = u.trajectory[ts].positions - part_pos[ts, :, :] if np.all(diff) == np.all(np.zeros(u.atoms.positions.shape)): print('Timestep:', ts, 'Pass') else: print('Fail')