maverig.models¶
maverig.models.model
¶
-
maverig.models.model.
fast_deepcopy
(x)¶ Return a deepcopy of x.
x may be or contain the following types:
{dict, set, list, tuple, datetime, string, int, float, bool, NoneType}
-
class
maverig.models.model.
ElemPort
(elem_id, port)¶ Bases:
tuple
An element port instance (e.g.
ElemPort('CSV.House_1', '0')
) describes a specificport
(e.g.'0'
) of an element specified byelem_id
(e.g.'CSV.House_1'
), which may be docked to other element ports.Element ports are often abbreviated with
ep
.In all model methods, element ports can also be passed as list (e.g.
['CSV.House_1', '0']
and will be automatically converted to ElemPort when needed.-
elem_id
¶ Alias for field number 0
-
port
¶ Alias for field number 1
-
-
class
maverig.models.model.
ProgramMode
¶ Bases:
object
Represents the main program mode: composition, simulation, or simulation paused.
-
composition
= 'composition mode'¶ Composition mode, where scenario can be edited.
-
simulation
= 'simulation mode'¶ Simulation mode, where simulation or recorded simulation history “video” is running.
-
simulation_paused
= 'simulation paused'¶ Simulation paused mode, where simulation is paused.
-
-
class
maverig.models.model.
Mode
¶ Bases:
object
Represents the currently selected mode for the composition.
-
selection
= 'selection mode'¶ In selection mode, all elements may be moved and edited.
-
hand
= 'hand mode'¶ In hand mode, the complete scenario is movable and elements can’t be edited or created.
-
comp
= 'component mode'¶ In component mode, only elements of a the selected component in mode panel may be edited and created.
-
sim
= 'simulation mode'¶ In simulation mode, the scenario can only be watched and elements can’t be edited. Similar to hand mode but may differ in element visualizations.
-
-
class
maverig.models.model.
Model
¶ Bases:
object
Model manages the complete scenario state: - simulation start and end time - elements as component instances and their dockings - clipboard - selected elements - modi (comp_mode, selection_mode, hand_mode) and - events.
-
auto_update_components_languages
= None¶ Switch for automatic creation of *.po language entries for new component texts.
-
elements
= None¶ A dict mapping elem_id to element instance.” Elements instances represent a Power or Logic Unit (here a PQBus example) with customizable data for the following purposes:
link to component description by
model.get_component(elem_id)
ormodel.components[sim_model]
reference data (elem_id)
custom visualization (icon),
positioning for each port (pos),
dockings to/from other elements and their ports (out, in)
- data for simulation runtime and initialization:
parameters and their values (params)
attributes at simulation runtime with their static or listed dynamic values (attrs)
reference to mosaik entity (mosaik_full_id)
mosaik_full_id and attrs will be automatically filled during simulation time
Example elements dictionary with PyPower PQBus instance:
{ "PyPower.PQBus_4": { "sim_model": "PyPower.PQBus", "icon": "bus.svg", "elem_id": "PyPower.PQBus_4", "mosaik_full_id": "PyPower-0.0-PyPower.PQBus_4", "docking_ports": { "0": { "pos": [450.0,200.0] "out": [], "in": [["PyPower.Branch_3","1"], ["PyPower.Branch_5","0"], ["CSV.House_4","1"]], } }, "params": { "bus_type": "PQ", "base_kv": 0.23, "fbus": null, "tbus": null }, "attrs": { "Vl": 230.0, "Q": [0.0, 0.0, 0.0], "Vm": [230.14789278916751, 230.23588331696217, 230.22035839218473], "P": [381.21, 174.35, 116.43999999999998], "Va": [-0.17130258343689386, -0.17013456872934510, -0.16981416413842957] } }, "CSV.House_1": {...} }
-
history_undo
= None¶ The history list of scenarios that can be undone.
history_undo[-1]
is the nearest to the current scenario.
-
history_redo
= None¶ The history list of scenarios that can be redone.
history_redo[-1]
is the nearest to the current scenario.
-
tmp_scenario_copy
= None¶ A copy of the current scenario since the last history relevant change occured.
-
saved_scenario_copy
= None¶ A copy of the last saved scenario. Use
self.saved_scenario_copy == self.tmp_scenario_copy
comparison to check whether scenario has changed since last save.
-
uid
= None¶ A dict mapping sim_model to current id counter for creating elements.
-
clipboard_elements
= None¶ A dict mapping elem_id to element instance containing all the elements which are currently in clipboard.
-
graph
¶ The
maverig.models.modelGraph.ModelGraph
as networkx-Graph representation of the scenario.
-
simulation
¶ The
maverig.models.modelSimulation.SimulationServer
which manages the simulation.
-
components
¶ A dict mapping sim_model to component description. Component descriptions are read from JSON-Files in maverig/data/components.
-
simulators
¶ A dict mapping simulator name to simulator description. Simulator descriptions are read from JSON-Files in maverig/data/components/simulators.
-
sim_start
¶ The simulation start time as
datetime.datetime
.
-
sim_end
¶ The simulation end time as
datetime.datetime
.
-
sim_step_size
¶ The step size of the simulation in seconds. Standard is 1800 (= 30 minutes).
-
sim_index
¶ The current simulation time index, which is used to address the current timestamp and the current dynamic attribute value.
Setting it will result in the closest valid index if it exceeds the boundaries of collected simulation timestamps.
-
sim_end_index
¶ The last possible simulation time index of an completed simulation.
-
vid_speed
¶ Simulation visualization speed interval. The interval in milliseconds, how much time to pass until showing the next simulation timestep.
Setting this property will result in the closest supported speed interval.
Supported intervals (slow → fast):
2000, 1750, 1500, 1250, 1000, 750, 500, 250, 50
-
vid_speed_rel
¶ Simulation visualization speed factor. Note that this property does not represent an exact speed factor.
Setting this property will result in the closest supported speed factor.
Supported factors (slow → fast):
0.5, 0.625, 0.75, 0.875, 1, 1.5, 2, 4, 8
-
sim_progress
¶ The running simulation progress in percent (
0..100
).
-
sim_timestamp
¶ The current time stamp as
datetime.datetime
of the current simulation time index.Setting this property will result in the closest collected simulation time stamp.
-
language
¶ The currently active installed language for internationalization (e.g.
'en_EN'
).
-
program_mode
¶ The current program mode as
ProgramMode
.
-
switch_modes
(standard_mode, substitute_mode)¶ Switch current
mode
between standard_mode and substitute_mode asMode
. If currentmode
is none of these, it will be set to standard_mode.
-
raster_mode
¶ Raster visibility as
bool
.
-
comp_raster
¶ Raster visibility for composition mode as
bool
.Use this property to memorize
raster_mode
before simulation and set it back when switching back to composition mode.
-
raster_snap_mode
¶ bool
property whether elements snap to raster positions onmouse_release()
.
-
force_dragging
¶ bool
property whether scenario items are currently beeing dragged by an layout algorithm.
-
selection_dragging
¶ bool
property whether scenario items are currently beeing dragged by mouse selection.
-
selection
¶ A list of elem_ids of selected elements.
-
init_history
()¶ Reset all history entries and set an initial history entry as first scenario state.
-
add_history_point
()¶ Adds a new history point if there have been some changes and clears redo history list.
-
undo
()¶ Undo latest change.
-
redo
()¶ Redo latest undone change.
-
changes_count
()¶ Return an int value representing the current number of model state changes. This function is used for detecting history specific differences to the last temporary stored scenario.
History specific changes consider elements, positions, dockings, parameters and scenario simulation settings. Selection changes are not detected.
-
scenario
¶ Get or apply a JSON compatible scenario dict with the following keys:
changes_count()
: number of history specific scenario changes.uid
: id counters for element creation.sim_start
: simulation start time as string (e.g.'2014-10-20T00:00:00'
).sim_end
: simulation end time as string (e.g.'2014-10-24T23:59:59'
).sim_step_size
: the current simulation step size in seconds (e.g.1800
= 30 minutes).elements
: the elements dict of elem_id to element instance.selection
: the currently selected elements as dict from elem_id to element.
-
copy
()¶ Return a new flat copied model including ...
- scenario
- components and simulators descriptions
- no connected events.
-
copy_to_clipboard
(elem_ids)¶ Copy all elements of the elem_ids list to an internal clipboard.
-
paste_from_clipboard
()¶ Paste all elements from internal clipboard and return the newly inserted elem_ids. Only dockings inside of clipboard will be maintained.
-
create_element
(sim_model, pos)¶ Create a new element with a specific sim_model on an specific position as
QtCore.QPointF
in scenario.
-
delete_element
(elem_id)¶ Delete the given element.
-
get_component
(elem_id)¶ Return the component description dict of an elem_id by the elements sim_model.
-
get_simulator
(elem_id)¶ Return the simulator description dict of an element.
-
get_icon_color
(elem_id)¶ Return the color of the element icon. The icon will be scanned for the most valuable color only once. The icon colors are memorized locally.
Return a filtered list of published parameter names which are contained in each element of elem_ids.
-
param_is_multivalue
(elem_ids, param_name)¶ Return whether one specific parameter (param_name) of multiple elements (elem_ids) differ in their values.
-
get_param_value
(elem_id, param_name)¶ Get the value of a parameter in element (elem_id), Return
None
if element has no parameter param_name.
-
set_param_value
(elem_id, param_name, param_value)¶ Set value of parameter (param_name) in element (elem_id) if value is not
None
.
Return a filtered list of published attribute names which are contained in each element of elem_ids.
-
attr_is_multivalue
(elem_ids, attr_name)¶ Return whether current attribute values differ in elements of elem_ids.
-
get_attr_values
(elem_id, attr_name, from_time_index=0, to_time_index=None)¶ Get attribute values of attr_name in element (elem_id) in time interval [from_time_index, to_time_index] where to_time_index is
sim_timestamp
if set toNone
.
-
get_attr_value
(elem_id, attr_name, time_index=None)¶ Get current attribute value of attr_name in element (elem_id) at current time index. Return
None
if attribute does not exist.
-
get_u_heat_value
(elem_id)¶ Return the voltage dependent heat value of an element with component type PQBus or Branch.
-
get_i_heat_value
(elem_id)¶ Return the current dependent heat value of an element only with component type Branch.
-
get_p_level
(elem_id)¶ Return the power level of an element with component type House, PV, Transformer, CHP, WECS or EV.
-
get_state_of_charge
(elem_id)¶ Return the state of charge of an EV.
-
get_selected
(elem_id)¶ Return whether element (elem_id) is selected.
-
set_selected
(elem_id, value)¶ Set whether element (elem_id) is selected.
-
docking_port
(ep)¶ Return an element docking port content.
Example
docking_port(['CSV.House_1', '1'])
returns the port ‘1’ content of the element docking ports:{ 'pos': [220.0, 330.0], 'in': [], 'out': [['PyPower.PQBus_3', '0']] }
-
elem_ports
(elem_id)¶ Return a list of available element ports of an specific element (elem_id).
-
get_pos
(ep)¶ Get the position of an element port as
QtCore.QPointF
.
-
set_pos
(ep, pos)¶ Set a position of an element port as
QtCore.QPointF
.
-
dockings_out
(ep)¶ Return a list of outgoing dockings to other element ports from an element port ep.
-
dockings_in
(ep)¶ Return a list of ingoing dockings from other element ports to an element port ep.
-
docking_attrs
(from_elem_id, to_elem_id)¶ Return a set of valid attribute connection tuples from one element to another, e.g.
{('P_out','P')}
. This function is used for connecting elements in mosaik inmaverig.models.modelSimulation.SimulationProcess.start_simulation()
.Component attribute descriptions may contain
'out':[...]
or'in':[...]
entries indicating allowed connections to or from other attribute names.
-
can_dock
(from_ep, to_ep)¶ Check whether it is possible to dock from one port to another.
-
dock
(from_ep, to_ep)¶ Dock one port to another port.
-
undock
(from_ep, to_ep)¶ Undock two element ports. Only undock if elements exists, because undock might have been called by element deletion.
-
handle_scenario_error
(e)¶ Creates output and error events and selects elements in elem_ids for visual feedback of scenario errors.
-
validate_scenario
()¶ Validates the scenario.
-
update
()¶ Fires all events with pending demands.
-
update_all
()¶ Fires all events.
-
deselect_all_elems
()¶ Deselect all selected elements.
-
stop_simulation
()¶ Stop the simulation and switch the mode to selection and program_mode to composition.
-
maverig.models.modelGraph
¶
-
class
maverig.models.modelGraph.
ModelGraph
(model, data=None, **attr)¶ Bases:
networkx.Graph
A
networkx.Graph
representation of the model element ports as nodes and element internal lines between them as edges.This graph can be used for layout optimization algorithms.
Example connection scenario:
('House', '0') --- ('House', '1') -> ('PQBus_1', '0') ('PQBus_1', '0') <- ('Branch', '0') --- ('Branch', '1') -> ('PQBus_2', '0')
Where
---
are lines and<-
,->
are dockings.This scenario would be represented in
ModelGraph
as follows:('House', '0') --- ('PQBus_1', '0') --- ('PQBus_2', '0'))
Where
---
are edges and the element ports are nodes.Ports with outgoing dockings (endpoints in view) get represented by their docked Port as Node, because they share the same position.
-
set_pos
(ep, pos)¶ Change the position of an
ElemPort
node to pos (PySide.QtCore.QPointF
). This method gets called by model when a position is set there.
-
maverig.models.modelSimulation
¶
-
class
maverig.models.modelSimulation.
SimulationProcess
(sim_proxy, model)¶ Bases:
multiprocessing.context.Process
The Simulation Process instantiates all needed Mosaik models and simulators and runs them until simulation is finished.
-
run
()¶ Process entry point.
-
short_name
(name)¶ Strip name extensions after ‘-‘.
-
start_simulation
()¶ Start simulators and their elements in Mosaik and create and run Mosaik world.
The Simulation is beeing initiated in the following order:
- Prepare model and simulator specific parameters and configuration.
- Start the simulators with parameter values taken from element where possible.
- Start the element as Mosaik entity and optionally create needed parent entities
- Connect entities as defined by dockings and component attribute descriptions
- Create and connect
maverig.utils.visSimulator.VisSimulator
which constantly sends simulated data toSimulationServer
on each simulation step. - Run the Simulation and return when it is finished.
-
start_simulator
(world, elem)¶ Starts a mosaik_simulator with parameters specified in prepared element simulator parameter dict (
elem['simulator']['params']
).
-
start_element
(elem)¶ Create an element in Mosaik with needed params specified in simulator meta and collected from elem. Apply the mapping of mosaik element id (mosaik_full_id) to elem, so that the data pushed to SimulationServer by
maverig.utils.visSimulator.VisSimulator
can be mapped back to elem_id onSimulationServer.run_iteration()
side later on.
-
-
class
maverig.models.modelSimulation.
SimulationServer
(model)¶ Bases:
maverig.utils.processServer.ProcessServer
Manage a simulation process and serve proxy-access to registered functions.
-
start
()¶ Reset attributes and start class:SimulationProcess as Maverig subprocess.
-
stop
()¶ Stop the Simulation and terminate the simulation process if it is still active.
-
write
(text)¶ Proxy function. Write text and subprocess stdout to Maverig console output.
-
handle_process_scenario_error
(e)¶ Proxy function for error handling passed to
maverig.models.model.Model.handle_scenario_error()
.
-
map_elem_to_mosaik
(elem_id, mosaik_full_id)¶ Proxy function mapping mosaik_full_id to elem_id for data assignment in
run_iteration()
.
-
update_data
(timestamp, progress, data)¶ Proxy function for pushing collected from
maverig.utils.visSimulator.VisSimulator
. Apply the data later inrun_iteration()
by starting a timer. This may be faster than applying the data on each processed update_data call.
-
run_iteration
()¶ Apply simulated data on model.
-