The low-level API
The low-level API uses standard TCP sockets. If mosaik starts a simulator, that simulator needs to connect to mosaik. If mosaik connects to a running instance of a simulator, that simulator obviously needs to provide a server socket that mosaik can connect to.
Network messages consists of a four bytes long header and a payload of arbitrary length. The header is an unsigned integer (uint32) in network byte order (big-endian) and stores the number of bytes in the payload. The payload itself is a UTF-8 encoded JSON list containing the message type, a message ID and the actual content:
Messages send between mosaik and a simulator must follow the request-reply
pattern. That means, that
every request that one party makes must be responded by the other party.
Request use the message type 0
, replies uses 1
for success or 2
to
indicate a failure. The message ID is an integer that is unique for every
request that a network socket makes. Replies (no matter if successful or
failed) need to use the message ID of the corresponding request.
The content of a request roughly map to a Python function call:
[function, [arg0, arg1, ...], {kwarg0: val0, kwar1: val1}]
Thereby, function is always a string. The type of the arguments and keyword arguments may vary depending on the function.
The content of replies is either the return value of the request, or an error message or stack trace. Error messages and stack traces should always be strings. The return value for successful requests depends on the function.
Example
We want to perform the following function call on the remote site:
my_func('hello', 'world', times=23) --> 'the return value'
. This would map
to the following message payload:
[0, 1, ["my_func", ["hello", "world"], {"times": 23}]]
Our message is a request (message type 0
), the message ID is 1
and the
content is a JSON list containing the function name as well as its arguments
and keyword arguments.
The complete message sent via the network will be:
\x00\x00\x00\x36[0, 1, ["my_func", ["hello", "world"], {"times": 23}]]
In case of success, the reply’s payload to this request could look like this:
[1, 1, "the return value"]
In case of error, this could be the reply’s payload:
[2, 1, "Error in your code line 23: ..."]
The actual network messages would be:
\x00\x00\x00\x1a[1, 1, "the return value"]
\x00\x00\x00\x29[2, 1, "Error in your code line 23: ..."]
All commands that mosaik may send to a simulator are described in-depth in the next section. All asynchronous requests that a simulator may make are described in Asynchronous requests.
API calls:
Async. requests:
API calls
This section describes the API calls init()
, create()
, setup_done()
,
step()
, get_data()
and stop()
. In addition to these, a simulator
may optionally expose additional functions (referred to as extra methods).
These methods can be called at composition time (when you create your
scenario).
init
["init", [sim_id], {time_resolution=time_resolution, **sim_params}] -> meta
The init
call is made once to initialize the simulator. It has one
positional argument, the simulator ID, and time_resolution and an arbitrary
amount of further parameters (sim_params) as keyword arguments.
The return value meta is an object with meta data about the simulator:
{
"api_version": "x.y",
"type": "time-based"|"event-based"|"hybrid",
"models": {
"ModelName": {
"public": true|false,
"params": ["param_1", ...],
"attrs": ["attr_1", ...],
"any_inputs": true|false,
},
...
},
"extra_methods": [
"do_cool_stuff",
"set_static_data"
]
}
The api_version is a string that defines which version of the mosaik API the simulator implements. Since mosaik API version 2.2, the simulator’s major version (“x”, in the snippet above) has to be equal to mosaik’s. Mosaik will cancel the simulation if a version mismatch occurs.
The type defines how the simulator is stepped and for which time the output is valid. Time-based simulators only decide themselves on which points in time they want to be stepped (i.e. communicate with the other simulators). Their output is valid until the next step. Event-based simulators are always stepped when a predecessor provides new input, but they can also schedule steps for themselves. A more fine-grained behavior can be set for hybrid simulators. See the scheduler description for details.
models is an object describing the models provided by this simulator. The
entry public determines whether a model can be instantiated by a user
(true
) or if it is a sub-model that cannot be created directly (false
).
params is a list of parameter names that can be passed to the model when
creating it. attrs is a list of attribute names that can be accessed (reading
or writing). If the optional any_inputs flag is set to true
, any
attributes can be connected to the model, even if they are not attrs. This
may, for example, be useful for databases that don’t know in advance which
attributes of an entity they’ll receive.
extra_methods is an optional list of methods that a simulator provides in
addition to the standard API calls (init()
, create()
and so on). These
methods can be called while the scenario is being created and can be used for
operations that don’t really belong into init()
or create()
.
Example
Request:
["init", ["PowerGridSim-0"], {"time_resolution": 1., "step_size": 60}]
Reply:
{
"api_version": "3.0",
"type": "time-based",
"models": {
"Grid": {
"public": true,
"params": ["topology_file"],
"attrs": []
},
"Node": {
"public": false,
"params": [],
"attrs": ["P", "Q"]
},
"Branch": {
"public": false,
"params": [],
"attrs": ["I", "I_max"]
}
}
}
create
["create", [num, model], {**model_params}] -> entity_list
Create num instances of model using the provided model_params
num is an integer for the number of model instances to create.
model needs to be a public entry in the simulator’s meta['models']
(see
init).
model_params is an object mapping parameters (from
meta['models'][model]['params']
, see init) to their values.
Return a (nested) list of objects describing the created model instances (entities). The root list must contain exactly num elements. The number of objects in sub-lists is not constrained:
[
{
"eid": "eid_1",
"type": "model_name",
"rel": ["eid_2", ...],
"children": <entity_list>,
},
...
]
The entity ID (eid) of an object must be unique within a simulator instance.
For entities in the root list, type must be the same as the model
parameter. The type for objects in sub-lists may be anything that can be found
in meta['models']
(see init). rel is an optional list of
related entities; “related” means that two entities are somehow connect within
the simulator, either logically or via a real data-flow (e.g., grid
nodes are related to their adjacent branches). The children entry is optional
and may contain a sub-list of entities.
Example
Request:
["create", [1, "Grid"], {"topology_file": "data/grid.json"}]
Reply:
[
{
"eid": "Grid_1",
"type": "Grid",
"rel": [],
"children": [
{
"eid": "node_0",
"type": "Node",
},
{
"eid": "node_1",
"type": "Node",
},
{
"eid": "branch_0",
"type": "Branch",
"rel": ["node_0", "node_1"]
}
]
}
]
setup_done
["setup_done", [], {}] -> null
Callback that indicates that the scenario setup is done and the actual simulation is about to start.
At this point, all entities and all connections between them are know but no simulator has been stepped yet.
Implementing this method is optional.
Added in mosaik API version 2.2.
Example
Request:
["setup_done", [], {}]
Reply:
null
step
["step", [time, inputs, max_advance], {}] -> Optional[time_next_step]
Perform the next simulation step at time time using input values from inputs and return the new simulation time (the time at which step should be called again) or null if the simulator doesn’t need to step itself.
time, max_advance, and the time_next_step are integers (or null). Their unit is arbitrary, e.g. seconds (counted from simulation start), but has to be consistent among all simulators used in a scenario.
inputs is a dict of dicts mapping entity IDs to attributes and dicts of values (each simulator has to decide on its own how to reduce the values (e.g., as its sum, average or maximum):
{
"eid_1": {
"attr_1": {'src_full_id_1': val_1_1, 'src_full_id_2': val_1_2, ...},
"attr_2": {'src_full_id_1': val_2_1, 'src_full_id_2': val_2_2, ...},
...
},
...
}
max_advance tells the simulator how far it can advance its time without risking any causality error, i.e. it is guaranteed that no external step will be triggered before max_advance + 1, unless the simulator activates an output loop earlier than that. For time-based simulators (or hybrid ones without any triggering input) max_advance is always equal to the end of the simulation (until). See the description of the scheduler for more details.
Example
Request:
[
"step",
[
60,
{
"node_1": {"P": [20, 3.14], "Q": [3, -2.5]},
"node_2": {"P": [42], "Q": [-23.2]},
},
3600
],
{}
]
Reply:
120
get_data
["get_data", [outputs], {}] -> data
Return the data for the requested attributes in outputs
outputs is an object mapping entity IDs to lists of attribute names whose values are requested (connected to any other simulator):
{
"eid_1": ["attr_1", "attr_2", ...],
...
}
The return value needs to be an object of objects mapping entity IDs and attribute names to their values:
{
"eid_1: {
"attr_1": "val_1",
"attr_2": "val_2",
...
},
...
"time": *output_time* (optional)
}
Time-based simulators have set an entry for all requested attributes, whereas for event-based and hybrid simulators this is optional (e.g. if there’s no new event).
Event-based and hybrid simulators can optionally set a timing of their non-persistent output attributes via a time entry, which is valid for all given (non-persistent) attributes. If not given, it defaults to the current time of the step. Thus only one output time is possible per step. For further output times the simulator has to schedule another self-step (via the step’s return value).
Examples
Request:
["get_data", [{"branch_0": ["I"]}], {}]
Reply:
{
"branch_0": {
"I": 42.5
}
}
Request:
["get_data", [{"node_0": ["msg"], "node_1": ["msg"]}], {}]
Reply:
{
"node_1": {
"msg": "Hi"
},
"time": 140
}
stop
["stop", [], {}] -> null
Immediately stop the simulation and terminate.
This call has no parameters and no reply is required.
Example
Request:
["stop", [], {}]
Reply:
no reply required
Asynchronous requests
get_progress
["get_progress", [], {}] -> progress
Return the current overall simulation progress in percent.
Example
Request:
["get_progress", [], {}]
Reply:
23.42
get_data
["get_data", [attrs], {}] -> data
Get the data for the requested attributes in attrs.
attrs is an object of (fully qualified) entity IDs mapping to lists of attribute names:
{
"sim_id.eid_1": ["attr_1", "attr_2", ...],
...
}
The return value is an object mapping the input entity IDs to data objects mapping attribute names to there respective values:
{
"sim_id.eid_1: {
"attr_1": "val_1",
"attr_2": "val_2",
...
},
...
}
Example
Request:
["get_data", [{"grid_sim_0.branch_0": ["I"]}], {}]
Reply:
{
"grid_sim_0.branch_0": {
"I": 42.5
}
}
set_data
["set_data", [data], {}] -> null
Set data as input data for all affected simulators.
data is an object mapping source entity IDs to objects which in turn map
destination entity IDs to objects of attributes and values
({"src_full_id": {"dest_full_id": {"attr1": "val1", "attr2": "val2"}}}
)
Example
Request:
[
"set_data",
[{
"mas_0.agent_0": {"pvsim_0.pv_0": {"P_target": 20,
"Q_target": 3.14}},
"mas_0.agent_1": {"pvsim_0.pv_1": {"P_target": 21,
"Q_target": 2.718}}
}],
{}
]
Reply:
null
set_event
["set_event", [time], {}] -> null
Request an event/step for itself at time time.
Example
Request:
["set_event", [5], {}]
Reply:
null