State¶
- class hoomd.State(simulation, snapshot, domain_decomposition)¶
The state of a
Simulationobject.Note
This object cannot be directly instantiated. Use
Simulation.create_state_from_gsdandSimulation.create_state_from_snapshotto instantiate aStateobject as part of a simulation.Overview
Statestores the data that describes the thermodynamic microstate of aSimulationobject. This data consists of the box, particles, bonds, angles, dihedrals, impropers, special pairs, and constraints.Box
The simulation
boxdescribes the space that contains the particles as aBoxobject.Particles
The state contains
N_particlesparticles. Each particle has a position, orientation, type id, body, mass, moment of inertia, charge, diameter, velocity, angular momentum, image, and tag:: position - X,Y,Z cartesian coordinates defining the position of the particle in the box.
: orientation - Unit quaternion defining the rotation from the particle’s local reference frame to the box reference frame. The four components are in the order , , , for the in complex notation .
particle_typeid: type id - An integer in the interval[0,len(particle_types)) that identifies the particle’s type.particle_typesmaps type ids to names with:name = particle_types[particle_typeid].particle_body: body id - An integer that identifies the particle’s rigid body. A value of-1indicates that this particle does not belong to a body. A positive value indicates that the particle belongs to the bodyparticle_body. This particle is the central particle of a body when the body id is equal to the tag . (used byhoomd.md.constrain.Rigid): mass - The particle’s mass.
: moment of inertia - , , elements of the diagonal moment of inertia tensor in the particle’s local reference frame. The off-diagonal elements are 0.
: charge - The particle’s charge.
: diameter - Deprecated in v3.0.0. HOOMD-blue reads and writes particle diameters, but does not use them in any computations.
: velocity - X,Y,Z components of the particle’s velocity in the box’s reference frame.
: angular momentum - Quaternion defining the particle’s angular momentum (see note).
: image - Integers x,y,z that record how many times the particle has crossed each of the periodic box boundaries.
particle_tag: tag - An integer that uniquely identifies a given particle. The particles are stored in tag order when writing and initializing to/from a GSD file or snapshot: . When accessing data in local snapshots, particles may be in any order.
Note
HOOMD stores angular momentum as a quaternion because that is the form used when integrating the equations of motion (see Kamberaj 2005). The angular momentum quaternion is defined with respect to the orientation quaternion of the particle and the vector angular momentum of the particle, lifted into pure imaginary quaternion form as:
. Following this, the angular momentum vector in the particle’s local reference frame is:
Bonded groups
The state contains
N_bondsbonds,N_anglesangles,N_dihedralsdihedrals,N_impropersimpropers, andN_special_pairsspecial pairs. Each of these data structures is similar, differing in the number of particles in the group and what operations use them. Bonds, angles, dihedrals, and impropers contain 2, 3, 4, and 4 particles per group respectively. Bonds specify the toplogy used when computing energies and forces inmd.bond, angles define the same formd.angle, dihedrals formd.dihedraland impropers formd.improper. These collectively implement bonding potentials used in molecular dynamics force fields. Like bonds, special pairs define connections between two particles, but special pairs are intended to adjust the 1-4 pairwise interactions in some molecular dynamics force fields: seemd.special_pair. Each bonded group is defined by a type id, the group members, and a tag.bond_typeid: type id - An integer in the interval[0,len(bond_types))that identifies the bond’s type.bond_typesmaps type ids to names with:name = bond_types[bond_typeid]. Similarly,angle_typeslists the angle types,dihedral_typeslists the dihedral types,improper_typeslists the improper types, andspecial_pair_typeslists the special pair types.bond_group: A list of integers in the interval that defines the tags of the particles in the bond (2), angle inangle_group(3), dihedral indihedral_group(4), improper inimproper_group(4), or special pair inpair_group(2).bond_tag: tag - An integer that uniquely identifies a given bond. The bonds are in tag order when writing and initializing to/from a GSD file or snapshot . When accessing data in local snapshots, bonds may be in any order. The same applies to angles withangle_tag, dihedrals withdihedral_tag, impropers withimproper_tag, and special pairs withpair_tag.
Constraints
The state contains
N_constraintsdistance constraints between particles. These constraints are used byhoomd.md.constrain.Distance. Each distance constraint consists of a distance value and the group members.constraint_group: A list of 2 integers in the interval that identifies the tags of the particles in the constraint.: constraint value - The distance between particles in the constraint.
MPI domain decomposition
When running in serial or on 1 MPI rank, the entire simulation state is stored in that process. When using more than 1 MPI rank, HOOMD-blue employs a domain decomposition approach to split the simulation box an integer number of times in the x, y, and z directions (
domain_decomposition). Each MPI rank stores and operates on the particles local to that rank. Local particles are those contained within the region defined by the split planes (domain_decomposition_split_fractions). Each MPI rank communicates with its neighbors to obtain the properties of particles near the boundary between ranks (ghost particles) so that it can compute interactions across the boundary.Accessing Data
Two complementary APIs provide access to the state data: local snapshots that access data directly available on the local MPI rank (including the local and ghost particles) and global snapshots that collect the entire state on rank 0. See
State.cpu_local_snapshot,State.gpu_local_snapshot,get_snapshot, andset_snapshotfor information about these data access patterns.See also
To write the simulation to disk, use
write.GSD.- property N_angles¶
The number of angles in the simulation state.
Example:
N_angles = simulation.state.N_angles
- Type:
- property N_bonds¶
The number of bonds in the simulation state.
Example:
N_bonds = simulation.state.N_bonds
- Type:
- property N_constraints¶
The number of constraints in the simulation state.
Example:
N_constraints = simulation.state.N_constraints
- Type:
- property N_dihedrals¶
The number of dihedrals in the simulation state.
Example:
N_dihedrals = simulation.state.N_dihedrals
- Type:
- property N_impropers¶
The number of impropers in the simulation state.
Example:
N_impropers = simulation.state.N_impropers
- Type:
- property N_particles¶
The number of particles in the simulation state.
Example:
N_particles = simulation.state.N_particles
- Type:
- property angle_types¶
List of all angle types in the simulation state.
Example:
angle_types = simulation.state.angle_types
- property bond_types¶
List of all bond types in the simulation state.
Example:
bond_types = simulation.state.bond_types
- property box¶
A copy of the current simulation box.
Example:
box = simulation.state.box
- Type:
- property cpu_local_snapshot¶
Expose simulation data on the CPU.
Provides access directly to the system state’s particle, bond, angle, dihedral, improper, constraint, and pair data through a context manager. Data in
State.cpu_local_snapshotis MPI rank local, and thehoomd.data.LocalSnapshotobject is only usable within a context manager (i.e.with sim.state.cpu_local_snapshot as data:). Attempts to assess data outside the context manager will result in errors. The local snapshot interface is similar to that ofSnapshot.The
hoomd.data.LocalSnapshotdata access is mediated throughhoomd.data.array.HOOMDArrayobjects. This lets us ensure memory safety when directly accessing HOOMD-blue’s data. The interface provides zero-copy access (zero-copy is guaranteed on CPU, access may be zero-copy if running on GPU).Changing the data in the buffers exposed by the local snapshot will change the data across the HOOMD-blue simulation. For a trivial example, this example would set all particle z-axis positions to 0.
with simulation.state.cpu_local_snapshot as local_snapshot: local_snapshot.particles.position[:, 2] = 0
Note
The state’s box and the number of particles, bonds, angles, dihedrals, impropers, constraints, and pairs cannot change within the context manager.
Note
Getting a local snapshot object is order and setting a single value is of order .
- Type:
- property dihedral_types¶
List of all dihedral types in the simulation state.
Example:
angle_types = simulation.state.angle_types
- property domain_decomposition¶
Number of domains in the x, y, and z directions.
- property domain_decomposition_split_fractions¶
Box fractions of the domain split planes in the x, y, and z directions.
- get_snapshot()¶
Make a copy of the simulation current state.
State.get_snapshotmakes a copy of the simulation state and makes it available in a single object.set_snapshotresets the internal state to that in the given snapshot. Use these methods to implement techniques like hybrid MD/MC or umbrella sampling where entire system configurations need to be reset to a previous one after a rejected move.Note
Data across all MPI ranks and from GPUs is gathered on the root MPI rank’s memory. When accessing data in MPI simulations, use a
if snapshot.communicator.rank == 0:conditional to access data arrays only on the root rank.Note
State.get_snapshotis an order operation.See also
- Returns:
The current simulation state
- Return type:
Example:
snapshot = simulation.state.get_snapshot()
- property gpu_local_snapshot¶
Expose simulation data on the GPU.
Provides access directly to the system state’s particle, bond, angle, dihedral, improper, constraint, and pair data through a context manager. Data in
State.gpu_local_snapshotis GPU local, and thehoomd.data.LocalSnapshotGPUobject is only usable within a context manager (i.e.with sim.state.gpu_local_snapshot as data:). Attempts to assess data outside the context manager will result in errors. The local snapshot interface is similar to that ofSnapshot.The
hoomd.data.LocalSnapshotGPUdata access is mediated throughhoomd.data.array.HOOMDGPUArrayobjects. This helps us maintain memory safety when directly accessing HOOMD-blue’s data. The interface provides zero-copy access on the GPU (assuming data was last accessed on the GPU).Changing the data in the buffers exposed by the local snapshot will change the data across the HOOMD-blue simulation. For a trivial example, this example would set all particle z-axis positions to 0.
with simulation.state.gpu_local_snapshot as local_snapshot: local_snapshot.particles.position[:, 2] = 0
Warning
This property is only available when running on a GPU (or multiple GPUs).
Note
The state’s box and the number of particles, bonds, angles, dihedrals, impropers, constraints, and pairs cannot change within the context manager.
Note
Getting a local snapshot object is order and setting a single value is of order .
- property improper_types¶
List of all improper types in the simulation state.
Example:
improper_types = simulation.state.improper_types
- property particle_types¶
List of all particle types in the simulation state.
Example:
particle_types = simulation.state.particle_types
- replicate(nx, ny, nz=1)¶
Replicate the state of the system along the periodic box directions.
- Parameters:
replicatemakes the system statenx * ny * nztimes larger. In each of the new periodic box images, it places a copy of the initial state with the particle positions offset to locate them in the image and the bond, angle, dihedral, improper, and pair group tags offset to apply to the copied particles. All other particle properties (mass, typeid, velocity, charge, …) are copied to the new particles without change.After placing the particles,
replicateexpands the simulation box by a factor ofnx,ny, andnzin the direction of the first, second, and third box lattice vectors respectively and adjusts the particle positions to center them in the new box.Example:
simulation.state.replicate(nx=2, ny=2, nz=2)
- set_box(box)¶
Set a new simulation box.
- Parameters:
box (hoomd.box.box_like) – New simulation box.
Note
All particles must be inside the new box.
set_boxdoes not change any particle properties.See also
Example:
simulation.state.set_box(box)
- set_snapshot(snapshot)¶
Restore the state of the simulation from a snapshot.
Also calls
update_group_dofto count the number of degrees in the system with the new state.- Parameters:
snapshot (Snapshot) – Snapshot of the system from
get_snapshot
Warning
set_snapshotcan only make limited changes to the simulation state. While it can change the number of particles/bonds/etc… or their properties, it cannot change the number or names of the particle/bond/etc.. types.Note
set_snapshotis an order operation and is very expensive when the simulation device is a GPU.Example:
simulation.state.set_snapshot(snapshot)
- property special_pair_types¶
List of all special pair types in the simulation state.
Example:
special_pair_types = simulation.state.special_pair_types
- thermalize_particle_momenta(filter, kT)¶
Assign random values to particle momenta.
- Parameters:
filter (hoomd.filter.filter_like) – Particles to modify
kT (float) – Thermal energy to set
thermalize_particle_momentaassigns the selected particle’s velocities and angular momentum to random values drawn from a Gaussian distribution consistent with the given thermal energy kT.Velocity
thermalize_particle_momentaassigns random velocities to the x and y components of each particle’s velocity. When the simulation box is 3D, it also assigns a random velocity to the z component. When the simulation box is 2D, it sets the z component to 0. Finally, sets the center of mass velocity of the selected particles to 0.Angular momentum
thermalize_particle_momentaassigns random angular momenta to each rotational degree of freedom that has a non-zero moment of inertia. Each particle can have 0, 1, 2, or 3 rotational degrees of freedom as determine by its moment of inertia.Example:
simulation.state.thermalize_particle_momenta( filter=hoomd.filter.All(), kT=1.5 )
- property types¶
dictionary of all types in the state.
Combines the data from
particle_types,bond_types,angle_types,dihedral_types,improper_types, andspecial_pair_typesinto a dictionary with keys matching the property names.
- update_group_dof()¶
Schedule an update to the number of degrees of freedom in each group.
update_group_dofrequests thatSimulationupdate the degrees of freedom provided to each group by the Integrator.Simulationwill perform this update at the start ofSimulation.runor at the start of the next timestep during an ongoing call toSimulation.run.This method is called automatically when:
An Integrator is assigned to the
Simulation’s operations.The
hoomd.md.Integrator.integrate_rotational_dofparameter is set.set_snapshotis called.On timesteps where a
hoomd.update.FilterUpdatertriggers.
Call
update_group_dofmanually to force an update, such as when you modify particle moments of inertia withcpu_local_snapshot.Example:
box = simulation.state.update_group_dof()