Action

class hoomd.custom.Action

Base class for user-defined actions.

To implement a custom operation in Python, subclass hoomd.custom.Action and implement the act() method to perform the desired action. To include the action in the simulation run loop, pass an instance of the action to hoomd.update.CustomUpdater, hoomd.write.CustomWriter, or hoomd.tune.CustomTuner.

Examples:

class ExampleAction(hoomd.custom.Action):
    def act(self, timestep):
        snapshot = self._state.get_snapshot()
        if snapshot.communicator.rank == 0:
            self.com = snapshot.particles.position.mean(axis=0)

To request that HOOMD-blue compute virials, pressure, the rotational kinetic energy, or the external field virial, set the flags attribute with the appropriate flags from the internal Action.Flags enumeration:

class ExampleAction(hoomd.custom.Action):
    flags = [
        Action.Flags.ROTATIONAL_KINETIC_ENERGY,
        Action.Flags.PRESSURE_TENSOR,
        Action.Flags.EXTERNAL_FIELD_VIRIAL,
    ]

    def act(self, timestep):
        pass

Use the hoomd.logging.log decorator to define loggable properties:

class ExampleAction(hoomd.custom.Action):
    @hoomd.logging.log
    def answer(self):
        return 42

    def act(self, timestep):
        pass


example_action = ExampleAction()
logger.add(example_action, quantities=["answer"])
flags

List of flags from the Action.Flags. Used to tell the integrator if specific quantities are needed for the action.

Type:

list[Action.Flags]

class Flags(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)

Flags to indictate the integrator should calculate quantities.

  • PRESSURE_TENSOR = 0

  • ROTATIONAL_KINETIC_ENERGY = 1

  • EXTERNAL_FIELD_VIRIAL = 2

abstract act(timestep)

Performs whatever action a subclass implements.

Parameters:

timestep (int) – The current timestep in a simulation.

Note

Use self._state to access the simulation state via hoomd.State when using the base class attach.

attach(simulation)

Attaches the Action to the hoomd.Simulation.

Parameters:

simulation (hoomd.Simulation) – The simulation to attach the action to.

Stores the simulation state in self._state. Override this in derived classes to implement other behaviors.

detach()

Detaches the Action from the hoomd.Simulation.

property loggables

Name, category mapping of loggable quantities.

Type:

dict[str, str]