SimulationDynamics

class narupatools.core.dynamics.SimulationDynamics(*, playback_interval: float)

Base class for an implementation of dynamics driven by Narupa.

This implements all the common commands for controlling a simulation, as well as running the simulation in both blocking and non-blocking modes. Subclasses of this only have to override _step_internal and _reset_internal to implement stepping forward a single step in the simulation and reseting the entire simulation back to its initial state.

Inheritance

Inheritance diagram of SimulationDynamics

Methods

__init__

Create a SimulationDynamics object.

check_task

Check if a task has thrown an exception, and reraise it if so.

create_from_object

Attempt to convert an arbitrary object to simulation dynamics.

get_frame

Get the current state of the system as a Narupa FrameData.

health_check

Check background tasks to ensure they have not encountered an exception.

pause

Pause this object if it is running.

play

Play the given object.

reset

Reset the simulation.

reset_time

Reset the timer such that the elapsed time and steps is zero.

restart

Restart this back to its initial state.

run

Run the dynamics.

start

An alias for run().

step

Step forward by one step, and then pause the simulation.

stop

Stop the object if it is running.

__init__(*, playback_interval: float)

Create a SimulationDynamics object.

Should be called by subclasses, and not called directly.

Parameters

playback_interval – Interval at which dynamics will be run, in seconds.

classmethod check_task(task: Optional[concurrent.futures._base.Future])None

Check if a task has thrown an exception, and reraise it if so.

Parameters

task – Background task to check for an exception.

static create_from_object(obj: Any)Optional[narupatools.core.dynamics.SimulationDynamics]

Attempt to convert an arbitrary object to simulation dynamics.

get_frame(fields: infinite_sets.infinite_sets.InfiniteSet[str] = everything(), existing: Optional[narupatools.frame._patch.FrameData] = None)narupatools.frame._patch.FrameData

Get the current state of the system as a Narupa FrameData.

Parameters

fields – Collection of keys to include in the FrameData

Returns

Narupa FrameData populated with requested fields.

health_check()None

Check background tasks to ensure they have not encountered an exception.

Calling this allows exceptions raised on background threads to be thrown on the main thread. It is a useful tool for checking if an object has crashed silently in the background.

It is recommended to call this periodically from the main thread to ensure an object is healthy. If no exceptions have occurred, this method will do nothing.

pause(*, wait: bool = True)None

Pause this object if it is running.

This will not cancel the current run if present, merely suspend it until a subsequent call to play() is made.

Parameters

wait – Wait until the pause has actually occurred.

play()None

Play the given object.

If this is not running, run this on a background thread. Else, unpause if this is paused.

reset()None

Reset the simulation.

The behaviour of this depends on the implementation of the dynamics, but commonly an implementation of SimulationDynamics should record the initial state of the simulation on initialization, and use this to reset the simulation.

This method is called whenever a client runs the reset command, described in frame_server.

reset_time()None

Reset the timer such that the elapsed time and steps is zero.

restart()None

Restart this back to its initial state.

run(steps: Optional[int] = None, *, block: Optional[bool] = None)Union[bool, concurrent.futures._base.Future[bool]]

Run the dynamics.

Raises

ValueError – The number of steps was negative or 0.

Parameters
  • steps – The number of steps to run for, or None to run indefinitely. If provided, must be larger than 0.

  • block – Should this be run in this thread (block=True) or in a background thread (block=False)

Returns

If run in blocking mode, returns True if the dynamics completed and False if it was interrupted. If not run in blocking mode, returns a Future with the same result.

start(*, block: bool = True)None

An alias for run().

Parameters

block – Should this block?

step()None

Step forward by one step, and then pause the simulation.

stop(*, wait: bool = True)None

Stop the object if it is running.

If this is running, this object will stop executing at the next available point.

Parameters

wait – Should this call wait until this object has stopped running before returning?

Attributes

angular_momenta

Angular momentum of each particle abouts its center of mass.

angular_velocities

Angular velocity of each particle abouts its center of mass.

dynamic_fields

Set of fields which are marked as having changed after a dynamics step.

elapsed_steps

Elapsed number of steps of the simulation since initialization/last reset.

elapsed_time

Elapsed time of the simulation since initialization/last reset in picoseconds.

forces

Forces on particles in kilojoules per mole per nanometer.

is_paused

Is this currently running but paused?

is_playing

Is this currently playing and not paused?

is_running

Is this currently running?

kinetic_energy

Kinetic energy in kilojoules per mole.

masses

Masses of particles in daltons.

momenta

Momenta of particles in dalton nanometers per picosecond.

moments_of_inertia

Moments of inertia for each particle abouts its origin in its local frame.

on_fields_changed

on_post_step

Event triggered after each step of the dynamics is run.

on_pre_step

Event triggered before each step of the dynamics is run.

on_reset

Event triggered when dynamics is reset.

orientations

Orientations of each atom as unit quaternions.

playback_interval

Time in seconds between individual steps of this playable.

playback_rate

Number of steps this playable goes through in 1 second.

positions

Positions of particles in nanometers.

potential_energy

Potential energy in kilojoules per mole.

temperature

Current temperature of the dynamics in Kelvin.

timestep

Current time step of the simulation in picoseconds.

torques

Torques on each particle abouts its center of mass.

total_steps

Total number of steps of the simulation.

total_time

Total time of the simulation in picoseconds.

velocities

Velocities of particles in nanometers per picosecond.

angular_momenta

Angular momentum of each particle abouts its center of mass.

Raises

AttributeError – Angular momenta is not defined for this system.

Returns

Array of angular momenta in dalton nanometer squared per picosecond.

angular_velocities

Angular velocity of each particle abouts its center of mass.

Returns

Array of angular velocities in radians per picoseconds.

dynamic_fields = frozenset({'energy.kinetic', 'energy.potential', 'particle.forces', 'particle.positions', 'particle.velocities', 'simulation.elapsed_steps', 'simulation.elapsed_time', 'simulation.total_steps', 'simulation.total_time'})

Set of fields which are marked as having changed after a dynamics step.

elapsed_steps

Elapsed number of steps of the simulation since initialization/last reset.

When the system is reset, this value is reset to 0.

elapsed_time

Elapsed time of the simulation since initialization/last reset in picoseconds.

When the system is reset, this value is reset to 0.

forces

Forces on particles in kilojoules per mole per nanometer.

is_paused

Is this currently running but paused?

is_playing

Is this currently playing and not paused?

is_running

Is this currently running?

This is True even if the playback is paused.

kinetic_energy

Kinetic energy in kilojoules per mole.

masses

Masses of particles in daltons.

momenta

Momenta of particles in dalton nanometers per picosecond.

moments_of_inertia

Moments of inertia for each particle abouts its origin in its local frame.

Returns

Array of moments of inertia, either scalars (for symmetric shapes) or 3-vectors.

on_fields_changed
on_post_step

Event triggered after each step of the dynamics is run.

on_pre_step

Event triggered before each step of the dynamics is run.

on_reset

Event triggered when dynamics is reset.

This can be used when the behaviour of reset that the current dynamics performs is insufficient, or the user would like to perform a more detailed reset such as reinitializing velocities.

orientations

Orientations of each atom as unit quaternions.

playback_interval

Time in seconds between individual steps of this playable.

playback_rate

Number of steps this playable goes through in 1 second.

positions

Positions of particles in nanometers.

potential_energy

Potential energy in kilojoules per mole.

temperature

Current temperature of the dynamics in Kelvin.

Raises

AttributeError – Temperature is not defined for this dynamics.

timestep

Current time step of the simulation in picoseconds.

Raises

AttributeError – Cannot get the current time step for this dynamics.

torques

Torques on each particle abouts its center of mass.

Returns

Array of torques in kilojoules per mole.

total_steps

Total number of steps of the simulation.

This includes all times the simulation has been reset.

total_time

Total time of the simulation in picoseconds.

This includes all times the simulation has been reset.

velocities

Velocities of particles in nanometers per picosecond.