hoomd.context

Overview

hoomd.context.SimulationContext

Simulation context

hoomd.context.initialize

Initialize the execution context

Details

Manage execution contexts.

Every hoomd simulation needs an execution context that describes what hardware it should execute on, the MPI configuration for the job, etc…

class hoomd.context.SimulationContext(device=None)

Simulation context

Parameters

device (hoomd.device) – the device to use for the simulation

Store all of the context related to a single simulation, including the system state, forces, updaters, integration methods, and all other commands specified on this simulation. All such commands in hoomd apply to the currently active simulation context. You swap between simulation contexts by using this class as a context manager:

sim1 = context.SimulationContext();
sim2 = context.SimulationContext();
with sim1:
  init.read_xml('init1.xml');
  lj = pair.lj(...)
  ...

with sim2:
  init.read_xml('init2.xml');
  gauss = pair.gauss(...)
  ...

# run simulation 1 for a bit
with sim1:
   run(100)

# run simulation 2 for a bit
with sim2:
   run(100)

# set_current sets the current context without needing to use with
sim1.set_current()
run(100)

If you do not need to maintain multiple contexts, you can call context.initialize() to initialize a new context and erase the existing one:

context.initialize()
init.read_xml('init1.xml');
lj = pair.lj(...)
...
run(100);

context.initialize()
init.read_xml('init2.xml');
gauss = pair.gauss(...)
...
run(100)
sorter

Global particle sorter.

Type

hoomd.update.sort

system_definition

System definition.

Type

hoomd.data.system_data

The attributes are global to the context. User scripts may access documented attributes to control settings, access particle data, etc… See the linked documentation of each attribute for more details. For example, to disable the global sorter:

c = context.initialize();
c.sorter.disable();
set_current()

Force this to be the current context

hoomd.context.initialize(args=None, device=None)

Initialize the execution context

Parameters
  • args (str) – Arguments to parse. When None, parse the arguments passed on the command line.

  • device (hoomd.device) – device to use for running the simulations

hoomd.context.initialize() parses the command line arguments given, sets the options and initializes MPI and GPU execution (if any). By default, hoomd.context.initialize() reads arguments given on the command line. Provide a string to hoomd.context.initialize() to set the launch configuration within the job script.

hoomd.context.initialize() can be called more than once in a script. However, the execution parameters are fixed on the first call and args is ignored. Subsequent calls to hoomd.context.initialize() create a new SimulationContext and set it current. This behavior is primarily to support use of hoomd in jupyter notebooks, so that a new clean simulation context is set when rerunning the notebook within an existing kernel.

Example:

from hoomd import *
context.initialize();
context.initialize("--mode=gpu --nrank=64");
context.initialize("--mode=cpu --nthreads=64");

world = MPI.COMM_WORLD
comm = world.Split(world.Get_rank(), 0)
c = comm.Communicator(mpi_comm=comm)
hoomd.context.initialize(device=device.GPU(communicator=c))