### H5MD Fixed-Interval Time Storage in Python Source: https://context7.com/pythonfz/h5md/llms.txt Demonstrates how to set up H5MD metadata and store particle position data at fixed time intervals using scalar step and time datasets. This method significantly reduces file size for regularly sampled data. ```python import h5py import numpy as np with h5py.File("fixed_interval.h5", "w", libver="18") as f: # Setup h5md metadata h5md = f.create_group("h5md") h5md.attrs["version"] = np.array([1, 1], dtype=np.int32) particles = f.create_group("particles/all") box = particles.create_group("box") box.attrs["dimension"] = 3 box.attrs["boundary"] = np.array(["periodic"] * 3, dtype="S8") # Fixed-interval position storage position = particles.create_group("position") # Scalar step: increment between samples step_ds = position.create_dataset("step", data=100) # every 100 steps step_ds.attrs["offset"] = 0 # starting step # Scalar time: time increment between samples time_ds = position.create_dataset("time", data=0.001) # 1 fs intervals time_ds.attrs["offset"] = 0.0 # starting time # Value dataset grows as simulation runs position.create_dataset("value", shape=(0, 100, 3), maxshape=(None, 100, 3), dtype=np.float64) ``` -------------------------------- ### Create HDF5 File with HDF5 Format Version 2 (C API) Source: https://context7.com/pythonfz/h5md/llms.txt Demonstrates how to create an HDF5 file using the C API, explicitly setting the library version bounds to H5F_LIBVER_18 to ensure compatibility with HDF5 file format version 2, which is recommended for H5MD files to enable automatic object time tracking. ```c #include /* Create HDF5 file with HDF5 file format version 2 */ hid_t fapl_id = H5Pcreate(H5P_FILE_ACCESS); H5Pset_libver_bounds(fapl_id, H5F_LIBVER_18, H5F_LIBVER_18); hid_t file_id = H5Fcreate("trajectory.h5", H5F_ACC_TRUNC, H5P_DEFAULT, fapl_id); /* Create h5md metadata group */ hid_t h5md_group = H5Gcreate(file_id, "h5md", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* Set version attribute [1, 1] for H5MD 1.1 */ hsize_t dims[1] = {2}; hid_t space_id = H5Screate_simple(1, dims, NULL); hid_t attr_id = H5Acreate(h5md_group, "version", H5T_NATIVE_INT, space_id, H5P_DEFAULT, H5P_DEFAULT); int version[2] = {1, 1}; H5Awrite(attr_id, H5T_NATIVE_INT, version); H5Aclose(attr_id); H5Sclose(space_id); H5Gclose(h5md_group); H5Fclose(file_id); ``` -------------------------------- ### Create Compact Datasets with HDF5 C API Source: https://context7.com/pythonfz/h5md/llms.txt Illustrates how to create HDF5 datasets with the H5D_COMPACT layout using the HDF5 C API. This layout is suitable for small, time-independent data as it stores data directly within the dataset header. ```c #include /* Create compact dataset for small time-independent data */ hid_t dcpl_id = H5Pcreate(H5P_DATASET_CREATE); H5Pset_layout(dcpl_id, H5D_COMPACT); /* Example: store particle masses (100 particles, ~800 bytes) */ hsize_t dims[1] = {100}; hid_t space_id = H5Screate_simple(1, dims, NULL); hid_t dset_id = H5Dcreate(particles_group, "mass", H5T_NATIVE_DOUBLE, space_id, H5P_DEFAULT, dcpl_id, H5P_DEFAULT); double masses[100]; for (int i = 0; i < 100; i++) masses[i] = 12.0; // Carbon mass H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, masses); H5Dclose(dset_id); H5Sclose(space_id); H5Pclose(dcpl_id); ``` -------------------------------- ### Create H5MD File with HDF5 Format Version 2 (Python API) Source: https://context7.com/pythonfz/h5md/llms.txt Illustrates how to create an H5MD file using the Python `h5py` library. It ensures proper file format version compatibility by using the `libver='18'` argument, which corresponds to HDF5 file format version 2, and sets the H5MD version attribute. ```python import h5py import numpy as np # Create H5MD file with format version 2 with h5py.File("trajectory.h5", "w", libver="18") as f: # Create h5md metadata group h5md = f.create_group("h5md") h5md.attrs["version"] = np.array([1, 1], dtype=np.int32) # Create author subgroup author = h5md.create_group("author") author.attrs["name"] = "Researcher Name" author.attrs["email"] = "researcher@example.org" # Create creator subgroup creator = h5md.create_group("creator") creator.attrs["name"] = "MySimulation" creator.attrs["version"] = "2.0.0" ``` -------------------------------- ### Query HDF5 Object Time Tracking with h5py Source: https://context7.com/pythonfz/h5md/llms.txt Demonstrates how to query object access, modification, change, and birth times from an HDF5 file using the low-level h5py API. This is valuable for provenance tracking in scientific workflows. ```python import h5py from datetime import datetime with h5py.File("trajectory.h5", "r") as f: # Get object info using low-level API info = h5py.h5o.get_info(f.id) print(f"Access time: {datetime.fromtimestamp(info.atime)}") print(f"Modification time: {datetime.fromtimestamp(info.mtime)}") print(f"Change time: {datetime.fromtimestamp(info.ctime)}") print(f"Birth time: {datetime.fromtimestamp(info.btime)}") # Example output: # Access time: 2024-01-15 14:30:22 # Modification time: 2024-01-15 14:25:10 # Change time: 2024-01-15 14:25:10 # Birth time: 2024-01-15 14:20:00 ``` -------------------------------- ### H5MD Thermodynamics Module: Storing Observables in Python Source: https://context7.com/pythonfz/h5md/llms.txt Shows how to implement the H5MD thermodynamics module (v1.0.0) for storing macroscopic properties like temperature, pressure, and energies. Data is stored as per-particle averages and time-dependent scalars. ```python import h5py import numpy as np with h5py.File("observables.h5", "w", libver="18") as f: # H5MD metadata h5md = f.create_group("h5md") h5md.attrs["version"] = np.array([1, 1], dtype=np.int32) # Register thermodynamics module modules = h5md.create_group("modules") thermo = modules.create_group("thermodynamics") thermo.attrs["version"] = np.array([1, 0], dtype=np.int32) # Create observables group obs = f.create_group("observables") obs.attrs["dimension"] = 3 n_frames = 1000 # Time-dependent particle number (scalar observable) pnum = obs.create_group("particle_number") pnum.create_dataset("step", data=np.arange(0, n_frames * 100, 100)) pnum.create_dataset("time", data=np.linspace(0, 10.0, n_frames)) pnum.create_dataset("value", data=np.full(n_frames, 1000, dtype=np.int32)) # Temperature (per-particle average) temp = obs.create_group("temperature") temp.create_dataset("step", data=np.arange(0, n_frames * 100, 100)) temp.create_dataset("time", data=np.linspace(0, 10.0, n_frames)) temp.create_dataset("value", data=300.0 + np.random.randn(n_frames) * 5.0) # Kinetic energy (per-particle average) ke = obs.create_group("kinetic_energy") ke.create_dataset("step", data=np.arange(0, n_frames * 100, 100)) ke.create_dataset("value", data=1.5 * 300.0 * 8.314e-3 + np.random.randn(n_frames) * 0.1) ``` -------------------------------- ### Attach SI Units to Datasets with h5py Source: https://context7.com/pythonfz/h5md/llms.txt Demonstrates how to attach SI unit strings to HDF5 datasets using the 'unit' attribute within the h5md framework. This is useful for ensuring physical consistency in scientific data. ```python import h5py import numpy as np with h5py.File("with_units.h5", "w", libver="18") as f: # H5MD metadata with units module h5md = f.create_group("h5md") h5md.attrs["version"] = np.array([1, 1], dtype=np.int32) modules = h5md.create_group("modules") units = modules.create_group("units") units.attrs["version"] = np.array([1, 0], dtype=np.int32) units.attrs["system"] = "SI" # Particles with units particles = f.create_group("particles/all") box = particles.create_group("box") box.attrs["dimension"] = 3 box.attrs["boundary"] = np.array(["periodic"] * 3, dtype="S8") position = particles.create_group("position") position.create_dataset("step", data=np.arange(100)) time_ds = position.create_dataset("time", data=np.arange(100) * 1e-12) time_ds.attrs["unit"] = "ps" # picoseconds value_ds = position.create_dataset("value", shape=(100, 50, 3), dtype=np.float64) value_ds.attrs["unit"] = "nm" # nanometers # Velocity with composite units velocity = particles.create_group("velocity") velocity.create_dataset("step", data=np.arange(100)) vel_value = velocity.create_dataset("value", shape=(100, 50, 3), dtype=np.float64) vel_value.attrs["unit"] = "nm ps-1" # nanometers per picosecond ``` -------------------------------- ### Reading H5MD Data at Specific Time Step in Python Source: https://context7.com/pythonfz/h5md/llms.txt Provides a Python function to read H5MD element values at a specific simulation step using binary search on the monotonically increasing 'step' dataset. This allows for efficient retrieval of data points. ```python import h5py import numpy as np def read_at_step(filename, group_path, target_step): """Returns value of H5MD element at given step.""" with h5py.File(filename, "r") as f: group = f[group_path] step_data = group["step"][:] # Binary search for step index index = np.searchsorted(step_data, target_step) if index >= len(step_data) or step_data[index] != target_step: raise ValueError(f"Step {target_step} not found in dataset") return group["value"][index] # Example usage positions = read_at_step("trajectory.h5", "particles/all/position", 5000) print(f"Positions at step 5000: shape {positions.shape}") # Output: Positions at step 5000: shape (100, 3) ``` -------------------------------- ### Store Time-Dependent Particle Positions (Python) Source: https://context7.com/pythonfz/h5md/llms.txt Shows how to store time-dependent particle trajectory data in an H5MD file using Python's `h5py`. This includes creating groups for particles, defining simulation box properties, and storing explicit `step`, `time`, and `value` datasets for positions. ```python import h5py import numpy as np with h5py.File("trajectory.h5", "a") as f: # Create particles group for 100 particles in 3D space particles = f.create_group("particles") all_particles = particles.create_group("all") # Create simulation box (required in each particles subgroup) box = all_particles.create_group("box") box.attrs["dimension"] = 3 box.attrs["boundary"] = np.array(["periodic", "periodic", "periodic"], dtype="S8") box.create_dataset("edges", data=np.array([10.0, 10.0, 10.0])) # Store position trajectory (1000 frames, 100 particles, 3D) position = all_particles.create_group("position") n_frames, n_particles, n_dims = 1000, 100, 3 # Explicit step and time storage position.create_dataset("step", data=np.arange(0, n_frames * 100, 100)) position.create_dataset("time", data=np.arange(0, n_frames * 0.001, 0.001)) position.create_dataset("value", shape=(n_frames, n_particles, n_dims), dtype=np.float64, chunks=(1, n_particles, n_dims)) # Write frame data for i in range(n_frames): position["value"][i] = np.random.random((n_particles, n_dims)) * 10.0 ``` -------------------------------- ### H5MD Species Storage with Enumerated Type in Python Source: https://context7.com/pythonfz/h5md/llms.txt Illustrates how to store particle species information in H5MD using HDF5 enumerated types. This maps chemical symbols to integer identifiers, optimizing storage while maintaining performance. ```python import h5py import numpy as np with h5py.File("species_example.h5", "w", libver="18") as f: # Create enum type for species species_enum = h5py.enum_dtype({ "H": 1, "C": 6, "N": 7, "O": 8 }, basetype=np.int32) particles = f.create_group("particles/water") box = particles.create_group("box") box.attrs["dimension"] = 3 box.attrs["boundary"] = np.array(["periodic"] * 3, dtype="S8") # 3 water molecules = 9 atoms (O, H, H per molecule) n_atoms = 9 species_data = np.array([8, 1, 1, 8, 1, 1, 8, 1, 1], dtype=np.int32) # O, H, H... particles.create_dataset("species", data=species_data, dtype=species_enum) ``` -------------------------------- ### Load Search Index with jQuery Source: https://github.com/pythonfz/h5md/blob/main/_theme/bootstrap/search.html This snippet uses jQuery to load the search index from a specified JavaScript file. It includes a fallback mechanism for cases where AJAX loading fails, such as when accessing local documents via Chrome. ```html jQuery(function() { Search.loadIndex("{{ pathto('searchindex.js', 1) }}"); }); {# this is used when loading the search index using $.ajax fails, such as on Chrome for documents on localhost #} ``` -------------------------------- ### Store Particle Connectivity Information with h5py Source: https://context7.com/pythonfz/h5md/llms.txt Shows how to store connectivity information, such as particle bonds, in an HDF5 file using h5py. It utilizes HDF5 object references to link connectivity data to the relevant particles group. ```python import h5py import numpy as np with h5py.File("connectivity.h5", "w", libver="18") as f: h5md = f.create_group("h5md") h5md.attrs["version"] = np.array([1, 1], dtype=np.int32) # Create particles group first particles = f.create_group("particles/polymer") box = particles.create_group("box") box.attrs["dimension"] = 3 box.attrs["boundary"] = np.array(["periodic"] * 3, dtype="S8") # Store particle positions for 100-monomer polymer chain n_monomers = 100 position = particles.create_group("position") position.create_dataset("step", data=np.array([0])) position.create_dataset("value", shape=(1, n_monomers, 3), dtype=np.float64) # Connectivity: bonds between consecutive monomers connectivity = f.create_group("connectivity") # Time-independent bond list (99 bonds for 100-monomer chain) bonds = np.array([[i, i+1] for i in range(n_monomers - 1)], dtype=np.int32) bond_ds = connectivity.create_dataset("bonds", data=bonds) bond_ds.attrs["particles_group"] = particles.ref # HDF5 object reference ``` -------------------------------- ### Display Search Results and Context (Jinja2) Source: https://github.com/pythonfz/h5md/blob/main/_theme/bootstrap/searchresults.html This snippet iterates through search results, displaying the caption, link to the document, and a snippet of the context where the search term appeared. It handles cases where no results are found. ```jinja2 {# basic/searchresults.html ~~~~~~~~~~~~~~~~~~~~~~~~ Template for the body of the search results page. :copyright: Copyright 2007-2011 by the Sphinx team, see AUTHORS. :license: BSD, see LICENSE for details. #} Search ====== From here you can search these documents. Enter your search words into the box below and click "search". {{ \_('search') }} {%- if search\_performed %} Search Results -------------- {%- if not search\_results %} Your search did not match any results. {%- endif %} {%- endif %} {%- if search\_results %} {% for href, caption, context in search\_results %}* [{{ caption }}]({{ docroot }}{{ href }}/?highlight={{ q }}) {{ context|e }} {% endfor %} {%- endif %} ``` -------------------------------- ### Display Search Results Source: https://github.com/pythonfz/h5md/blob/main/_theme/bootstrap/search.html This Jinja2 template code iterates through search results, displaying the caption, link, and context for each matching document. It handles cases where no results are found. ```html {% for href, caption, context in search_results %}* [{{ caption }}]({{ pathto(item.href) }}) {{ context|e }} {% endfor %} ``` -------------------------------- ### Hide Fallback Element Source: https://github.com/pythonfz/h5md/blob/main/_theme/bootstrap/search.html This JavaScript code snippet is used to hide an HTML element with the ID 'fallback'. This is typically done once the search functionality is initialized or confirmed to be working. ```javascript $('#fallback').hide(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.