mosaik.simmanager — Management of external processes

The simulation manager is responsible for starting simulation processes and shutting them down. It also manages the communication between mosaik and the processes.

It is able to start pure Python simulators in-process (by importing and instantiating them), to start external simulation processes and to connect to already running simulators and manage access to them.

mosaik.simmanager.start(world, sim_name, sim_id, time_resolution, sim_params)

Start the simulator sim_name based on the configuration im world.sim_config, give it the ID sim_id and pass the time_resolution and the parameters of the dict sim_params to it.

The sim config is a dictionary with one entry for every simulator. The entry itself tells mosaik how to start the simulator:

{
    'ExampleSimA': {
        'python': 'example_sim.mosaik:ExampleSim',
    },
    'ExampleSimB': {
        'cmd': 'example_sim %(addr)s',
        'cwd': '.',
    },
    'ExampleSimC': {
        'connect': 'host:port',
    },
}

ExampleSimA is a pure Python simulator. Mosaik will import the module example_sim.mosaik and instantiate the class ExampleSim to start the simulator.

ExampleSimB would be started by executing the command example_sim and passing the network address of mosaik das command line argument. You can optionally specify a current working directory. It defaults to ..

ExampleSimC can not be started by mosaik, so mosaik tries to connect to it.

time_resolution (in seconds) is a global scenario parameter, which tells the simulators what the integer time step means in seconds. Its default value is 1., meaning one integer step corresponds to one second simulated time.

The function returns a mosaik_api.Simulator instance.

It raises a SimulationError if the simulator could not be started.

Return a SimProxy instance.

mosaik.simmanager.start_inproc(world, sim_name, sim_config, sim_id, time_resolution, sim_params)

Import and instantiate the Python simulator sim_name based on its config entry sim_config.

Return a LocalProcess instance.

Raise a ScenarioError if the simulator cannot be instantiated.

mosaik.simmanager.start_proc(world, sim_name, sim_config, sim_id, time_resolution, sim_params)

Start a new process for simulator sim_name based on its config entry sim_config.

Return a RemoteProcess instance.

Raise a ScenarioError if the simulator cannot be instantiated.

mosaik.simmanager.start_connect(world, sim_name, sim_config, sim_id, time_resolution, sim_params)

Connect to the already running simulator sim_name based on its config entry sim_config.

Return a RemoteProcess instance.

Raise a ScenarioError if the simulator cannot be instantiated.

class mosaik.simmanager.SimProxy(name, sid, meta, world)

Handler for an external simulator.

It stores its simulation state and own the proxy object to the external simulator.

stop()

Stop the simulator behind the proxy.

The default implementation does nothing.

class mosaik.simmanager.LocalProcess(name, sid, meta, inst, world)

Proxy for internal simulators.

stop()

Yield a triggered event but do nothing else.

class mosaik.simmanager.RemoteProcess(name, sid, meta, proc, rpc_con, world)

Proxy for external simulator processes.

stop()

Send a stop message to the process represented by this proxy and wait for it to terminate.

class mosaik.simmanager.MosaikRemote(world, sim_id)

This class provides an RPC interface for remote processes to query mosaik and other processes (simulators) for data while they are executing a step() command.

get_progress()

Return the current simulation progress from sim_progress.

Return information about the related entities of entities.

If entities omitted (or None), return the complete entity graph, e.g.:

{
    'nodes': {
        'sid_0.eid_0': {'type': 'A'},
        'sid_0.eid_1': {'type': 'B'},
        'sid_1.eid_0': {'type': 'C'},
    },
    'edges': [
        ['sid_0.eid_0', 'sid_1.eid0', {}],
        ['sid_0.eid_1', 'sid_1.eid0', {}],
    ],
}

If entities is a single string (e.g., sid_1.eid_0), return a dict containing all entities related to that entity:

{
    'sid_0.eid_0': {'type': 'A'},
    'sid_0.eid_1': {'type': 'B'},
}

If entities is a list of entity IDs (e.g., ['sid_0.eid_0', 'sid_0.eid_1']), return a dict mapping each entity to a dict of related entities:

{
    'sid_0.eid_0': {
        'sid_1.eid_0': {'type': 'B'},
    },
    'sid_0.eid_1': {
        'sid_1.eid_1': {'type': 'B'},
    },
}
get_data(attrs)

Return the data for the requested attributes attrs.

attrs is a dict of (fully qualified) entity IDs mapping to lists of attribute names ({'sid/eid': ['attr1', 'attr2']}).

The return value is a dictionary, which maps the input entity IDs to data dictionaries, which in turn map attribute names to their respective values: ({'sid/eid': {'attr1': val1, 'attr2': val2}}).

set_data(data)

Set data as input data for all affected simulators.

data is a dictionary mapping source entity IDs to destination entity IDs with dictionaries of attributes and values ({'src_full_id': {'dest_full_id': {'attr1': 'val1', 'attr2': 'val2'}}}).

set_event(event_time)

Schedules an event/step at simulation time event_time.