narupa.ase.imd module

Interactive molecular dynamics server for use with an ASE molecular dynamics simulation.

class narupa.ase.imd.NarupaASEDynamics(narupa_imd_app: narupa.app.imd_app.NarupaImdApplication, dynamics: ase.md.md.MolecularDynamics, frame_method: Optional[Callable] = None, frame_interval=1)

Bases: object

Interactive molecular dynamics adaptor for use with ASE.

Parameters:
  • narupa_imd_app – A NarupaImdApplication to pass frames to and read forces from.
  • dynamics – A prepared ASE molecular dynamics object to run, with IMD attached.
  • frame_interval – Interval, in steps, at which to publish frames.
  • frame_method – Method to use to generate frames, given the the ASE Atoms and a FramePublisher. The signature of the callback is expected to be frame_method(ase_atoms, frame_publisher).
>>> from ase.calculators.emt import EMT
>>> from ase.lattice.cubic import FaceCenteredCubic
>>> atoms = FaceCenteredCubic(directions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]], symbol="Cu", size=(2, 2, 2), pbc=True)
>>> atoms.calc = EMT()
>>> ase_dynamics = Langevin(atoms, timestep=0.5, temperature_K=300, friction=1.0)
>>> with NarupaASEDynamics.basic_imd(ase_dynamics) as sim: # run basic Narupa server
...
...     with NarupaImdClient.autoconnect() as client: # connect an iMD client.
...         sim.run(10) # run some dynamics
...         client.first_frame.particle_count # the client will have received some MD data!
32
address

The address of the Narupa server.

atoms

The atoms in the MD system.

Returns:ASE atoms.
classmethod basic_imd(dynamics: ase.md.md.MolecularDynamics, address: Optional[str] = None, port: Optional[int] = None, **kwargs)

Initialises basic interactive molecular dynamics running a Narupa server at the given address and port.

Parameters:
  • dynamics – Molecular dynamics object to attach the server to.
  • address – Address to run the server at.
  • port – Port to run the server on.
  • kwargs – Key-word arguments to pass to the constructor of :class:~NarupaASEDynamics
Returns:

Instantiation of a :class:~NarupaASEDynamics configured with the given server parameters and dynamics.

cancel_run(wait=False)

Cancel molecular dynamics that is running on a background thread.

Parameters:wait – Whether to block and wait for the molecular dynamics to halt before returning.
close()

Cancels the molecular dynamics if it is running.

dynamics_interval

Minimum interval, in seconds, between frames sent to the frame publisher.

internal_calculator

The internal calculator being used to compute internal energy and forces.

Returns:ASE internal calculator.
is_running

Whether or not the molecular dynamics is currently running on a background thread or not. :return: True, if molecular dynamics is running, False otherwise.

pause()

Pause the simulation, by cancelling any current run.

This method is called whenever a client runs the pause command, described in :mod:narupa.trajectory.frame_server.

play()

Run the simulation indefinitely

Cancels any current run and then begins running the simulation on a background thread.

This method is called whenever a client runs the play command, described in :mod:narupa.trajectory.frame_server.

port

The port of the Narupa server. :return:

reset()

Reset the positions, velocities, and box to their initial values.

When this happens, the “on_reset” event is triggered and all the callbacks listed in the on_reset_listeners are called. These callbacks are called without arguments and no return value is stored.

Note

Only the positions, the velocities, and the simulation box are reset to their initial values. If a simulation needs any other state to be reset or updated, one should register a callback in the on_reset_listeners list. The callbacks are executed in the order of the list, after the positions, velocities, and box are reset.

Such callbacks also allow to modify the simulation at each reset. They would allow, for instance, to draw new velocities, or to place molecules differently.

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

run(steps: Optional[int] = None, block: Optional[bool] = None, reset_energy: Optional[float] = None)

Runs the molecular dynamics.

Parameters:
  • steps – If passed, will run the given number of steps, otherwise will run forever on a background thread and immediately return.
  • block – If False, run in a separate thread. By default, “block” is None, which means it is automatically set to True if a number of steps is provided and to False otherwise.
  • reset_energy – Threshold of total energy in kJ/mol above which the simulation is reset to its initial conditions. If a value is provided, the simulation is reset if the total energy is greater than this value, or if the total energy is nan or infinite. If None is provided instead, then the simulation will not be automatically reset.
step()

Take a single step of the simulation and stop.

This method is called whenever a client runs the step command, described in :mod:narupa.trajectory.frame_server.