md.force
Overview
Defines a force in HOOMD-blue. |
|
Custom forces implemented in python. |
|
Active force. |
|
Active force on a manifold. |
Details
Apply forces to particles.
- class hoomd.md.force.Force
Defines a force in HOOMD-blue.
Pair, angle, bond, and other forces are subclasses of this class.
Note
Force
is the base class for all loggable forces. Users should not instantiate this class directly.Initializes some loggable quantities.
- property additional_energy
Additional energy term not included in
energies
\([\mathrm{energy}]\).(
Loggable
: category=”scalar”)- Type
- property additional_virial
Additional virial tensor term not included in
virials
\([\mathrm{energy}]\).(
Loggable
: category=”sequence”)- Type
(1, 6)
numpy.ndarray
offloat
- property cpu_local_force_arrays
Expose force arrays on the CPU.
Provides direct access to the force, potential energy, torque, and virial data of the particles in the system on the cpu through a context manager. All data is MPI rank-local.
The
hoomd.md.data.ForceLocalAccess
object returned by this property has four arrays through which one can modify the force data:Examples:
with self.cpu_local_force_arrays as arrays: arrays.force[:] = ... arrays.potential_energy[:] = ... arrays.torque[:] = ... arrays.virial[:] = ...
- property energies
Energy contribution from each particle \([\mathrm{energy}]\).
Attention
In MPI parallel execution, the array is available on rank 0 only.
energies
isNone
on ranks >= 1.(
Loggable
: category=”particle”)- Type
(N_particles, )
numpy.ndarray
offloat
- property energy
Total contribution to the potential energy of the system \([\mathrm{energy}]\).
(
Loggable
: category=”scalar”)- Type
- property forces
The force applied to each particle \([\mathrm{force}]\).
Attention
In MPI parallel execution, the array is available on rank 0 only.
forces
isNone
on ranks >= 1.(
Loggable
: category=”particle”)- Type
(N_particles, 3)
numpy.ndarray
offloat
- property gpu_local_force_arrays
Expose force arrays on the GPU.
Provides direct access to the force, potential energy, torque, and virial data of the particles in the system on the gpu through a context manager. All data is MPI rank-local.
The
hoomd.md.data.ForceLocalAccessGPU
object returned by this property has four arrays through which one can modify the force data:Examples:
with self.gpu_local_force_arrays as arrays: arrays.force[:] = ... arrays.potential_energy[:] = ... arrays.torque[:] = ... arrays.virial[:] = ...
Note
GPU local force data is not available if the chosen device for the simulation is
hoomd.device.CPU
.
- property torques
The torque applied to each particle \([\mathrm{force} \cdot \mathrm{length}]\).
Attention
In MPI parallel execution, the array is available on rank 0 only.
torques
isNone
on ranks >= 1.(
Loggable
: category=”particle”)- Type
(N_particles, 3)
numpy.ndarray
offloat
- property virials
Virial tensor contribution from each particle \([\mathrm{energy}]\).
The 6 elements form the upper-triangular virial tensor in the order: xx, xy, xz, yy, yz, zz.
Attention
To improve performance
Force
objects only compute virials when needed. When not computed,virials
isNone
. Virials are computed on every step when using amd.methods.NPT
ormd.methods.NPH
integrator, on steps where a writer is triggered (such aswrite.GSD
which may log pressure or virials), or whenSimulation.always_compute_pressure
isTrue
.Attention
In MPI parallel execution, the array is available on rank 0 only.
virials
isNone
on ranks >= 1.(
Loggable
: category=”particle”)- Type
(N_particles, 6)
numpy.ndarray
offloat
- class hoomd.md.force.Custom(aniso=False)
Custom forces implemented in python.
Derive a custom force class from
Custom
, and override theset_forces
method to compute forces on particles. Users have direct, zero-copy access to the C++ managed buffers via either thecpu_local_force_arrays
orgpu_local_force_arrays
property. Choose the property that corresponds to the device you wish to alter the data on. In addition to zero-copy access to force buffers, custom forces have access to the local snapshot API via the_state.cpu_local_snapshot
or the_state.gpu_local_snapshot
property.See also
See the documentation in
hoomd.State
for more information on the local snapshot API.Examples:
class MyCustomForce(hoomd.force.Custom): def __init__(self): super().__init__(aniso=True) def set_forces(self, timestep): with self.cpu_local_force_arrays as arrays: arrays.force[:] = -5 arrays.torque[:] = 3 arrays.potential_energy[:] = 27 arrays.virial[:] = np.arange(6)[None, :]
In addition, since data is MPI rank-local, there may be ghost particle data associated with each rank. To access this read-only ghost data, access the property name with either the prefix
ghost_
of the suffix_with_ghost
.Note
Pass
aniso=True
to themd.force.Custom
constructor if your custom force produces non-zero torques on particles.Examples:
class MyCustomForce(hoomd.force.Custom): def __init__(self): super().__init__() def set_forces(self, timestep): with self.cpu_local_force_arrays as arrays: # access only the ghost particle forces ghost_force_data = arrays.ghost_force # access torque data on this rank and ghost torque data torque_data = arrays.torque_with_ghost
Note
When accessing the local force arrays, always use a context manager.
Note
The shape of the exposed arrays cannot change while in the context manager.
Note
All force data buffers are MPI rank local, so in simulations with MPI, only the data for a single rank is available.
Note
Access to the force buffers is constant (O(1)) time.
- class hoomd.md.force.Active(filter)
Bases:
hoomd.md.force.Force
Active force.
- Parameters
filter (
hoomd.filter
) – Subset of particles on which to apply active forces.
Active
specifies that an active force should be added to particles selected by the filter. particles. Obeys \(\delta {\bf r}_i = \delta t v_0 \hat{p}_i\), where \(v_0\) is the active velocity. In 2D \(\hat{p}_i = (\cos \theta_i, \sin \theta_i)\) is the active force vector for particle \(i\). The active force and the active torque vectors in the particle frame stay constant during the simulation. Hence, the active forces in the system frame are composed of the forces in particle frame and the current orientation of the particle.Note
To introduce rotational diffusion to the particle orientations, use
create_diffusion_updater
.Examples:
all = hoomd.filter.All() active = hoomd.md.force.Active( filter=hoomd.filter.All() ) active.active_force['A','B'] = (1,0,0) active.active_torque['A','B'] = (0,0,0) rotational_diffusion_updater = active.create_diffusion_updater( trigger=10) sim.operations += rotational_diffusion_updater
- filter
Subset of particles on which to apply active forces.
- Type
- active_force
Active force vector in the local reference frame of the particle \([\mathrm{force}]\). It is defined per particle type and stays constant during the simulation.
Type:
TypeParameter
[particle_type
,tuple
[float
,float
,float
]]
- active_torque
Active torque vector in the local reference frame of the particle \([\mathrm{force} \cdot \mathrm{length}]\). It is defined per particle type and stays constant during the simulation.
Type:
TypeParameter
[particle_type
,tuple
[float
,float
,float
]]
- create_diffusion_updater(trigger, rotational_diffusion)
Create a rotational diffusion updater for this active force.
- Parameters
trigger (hoomd.trigger.Trigger) – Select the timesteps to update rotational diffusion.
rotational_diffusion (hoomd.variant.Variant or float) – The rotational diffusion as a function of time or a constant.
- Returns
The rotational diffusion updater.
- Return type
- class hoomd.md.force.ActiveOnManifold(filter, manifold_constraint)
Bases:
hoomd.md.force.Active
Active force on a manifold.
- Parameters
filter (
hoomd.filter.ParticleFilter
) – Subset of particles on which to apply active forces.manifold_constraint (
hoomd.md.manifold.Manifold
) – Manifold constraint.
ActiveOnManifold
specifies that a constrained active force should be added to particles selected by the filter similar toActive
. The active force vector \(\hat{p}_i\) is restricted to the local tangent plane of the manifold constraint at point \({\bf r}_i\). For more information seeActive
.Hint
Use
ActiveOnManifold
with amd.methods.rattle
integration method with the same manifold constraint.Examples:
all = filter.All() sphere = hoomd.md.manifold.Sphere(r=10) active = hoomd.md.force.ActiveOnManifold( filter=hoomd.filter.All(), rotation_diff=0.01, manifold_constraint = sphere ) active.active_force['A','B'] = (1,0,0) active.active_torque['A','B'] = (0,0,0)
- filter
Subset of particles on which to apply active forces.
- manifold_constraint
Manifold constraint.
- active_force
Active force vector in the local reference frame of the particle \([\mathrm{force}]\). It is defined per particle type and stays constant during the simulation.
Type:
TypeParameter
[particle_type
,tuple
[float
,float
,float
]]
- active_torque
Active torque vector in local reference frame of the particle \([\mathrm{force} \cdot \mathrm{length}]\). It is defined per particle type and stays constant during the simulation.
Type:
TypeParameter
[particle_type
,tuple
[float
,float
,float
]]
- create_diffusion_updater(trigger, rotational_diffusion)
Create a rotational diffusion updater for this active force.
- Parameters
trigger (hoomd.trigger.Trigger) – Select the timesteps to update rotational diffusion.
rotational_diffusion (hoomd.variant.Variant or float) – The rotational diffusion as a function of time or a constant.
- Returns
The rotational diffusion updater.
- Return type