Load, analyze, and plot results from Modelica simulations.
Shortcuts to the “get” methods in SimRes
Alias for SimRes.get_FV()
Alias for SimRes.get_IV()
Alias for SimRes.get_description()
Alias for SimRes.get_displayUnit()
Alias for SimRes.get_indices_wi_times()
Alias for SimRes.get_times()
Alias for SimRes.get_unit()
Alias for SimRes.get_values()
Alias for SimRes.get_description()
Bases: object
Base class for Modelica-based simulation results and methods to analyze those results
On initialization, load Modelica simulation results from a MATLAB® file in Dymola® format.
Arguments:
fname: Name of the file (may include the path)
The file extension (‘.mat’) is optional.
constants_only: True, if only the variables from the first data table should be loaded
The first data table typically contains all of the constants, parameters, and variables that don’t vary. If only that information is needed, it will save some time and memory to set constants_only to True.
Example:
>>> from modelicares import SimRes
>>> sim = SimRes('examples/ChuaCircuit.mat')
Upon a call to an instance of SimRes, call a method on variable(s) given their name(s)
Arguments:
names: String or (possibly nested) list of strings of the variable names
action: Method for retrieving information about the variable(s)
The default is get_values(). action may be a list or tuple, in which case the return value is a list or tuple.
*args, **kwargs: Additional arguments for action
Examples:
>>> from modelicares.simres import SimRes, Info
>>> sim = SimRes('examples/ChuaCircuit.mat')
# Values of a single variable
>>> sim('L.v')
array([ 0.00000000e+00, ... -2.53528625e-01], dtype=float32)
>>> # This is equivalent to:
>>> sim.get_values('L.v')
array([ 0.00000000e+00, ... -2.53528625e-01], dtype=float32)
# Values of a list of variables
>>> sim(['L.L', 'C1.C'], SimRes.get_description)
['Inductance', 'Capacitance']
>>> # This is equivalent to:
>>> sim.get_description(['L.L', 'C1.C'])
['Inductance', 'Capacitance']
# Other attributes
>>> print("The final value of %s is %.3f %s." %
... sim('L.i', (Info.description, Info.FV, Info.unit)))
The final value of Current flowing from pin p to pin n is 2.049 A.
Test if a variable is present in the simulation results.
Arguments:
Example:
>>> from modelicares import SimRes
>>> sim = SimRes('examples/ChuaCircuit.mat')
>>> # 'L.v' is a valid variable name:
>>> 'L.v' in sim
True
>>> # but 'x' is not:
>>> 'x' not in sim
True
Upon accessing a variable name within an instance of SimRes, return its values
Arguments:
Examples:
>>> from modelicares import SimRes
>>> sim = SimRes('examples/ChuaCircuit.mat')
>>> sim['L.v']
array([ 0.00000000e+00, ... -2.53528625e-01], dtype=float32)
>>> # This is equivalent to:
>>> sim.get_values('L.v')
array([ 0.00000000e+00, ... -2.53528625e-01], dtype=float32)
Return the number of variables in the simulation.
Example:
>>> from modelicares import SimRes
>>> sim = SimRes('examples/ChuaCircuit.mat')
>>> print("There are %i variables in the %s simulation." %
... (len(sim), sim.fbase))
There are 62 variables in the ChuaCircuit simulation.
Return a formal description of the SimRes instance.
Example:
>>> from modelicares import SimRes
>>> sim = SimRes('examples/ChuaCircuit.mat')
>>> sim
SimRes('...ChuaCircuit.mat')
Return an informal description of the SimRes instance.
Example:
>>> from modelicares import SimRes
>>> sim = SimRes('examples/ChuaCircuit.mat')
>>> print(sim)
Modelica simulation results from "...ChuaCircuit.mat"
Launch a variable browser.
When a variable is selected, the right panel shows its attributes and a simple plot of the variable over time. Variable names can be dragged and dropped into a text editor.
There are no arguments or return values.
Example:
>>> from modelicares import SimRes
>>> sim = SimRes('examples/ChuaCircuit.mat')
>>> sim.browse()
Return the final value(s) of variable(s).
Arguments:
If names is a string, then the output will be an array of values. If names is a (optionally nested) list of strings, then the output will be a (nested) list of arrays.
Example:
>>> from modelicares import SimRes
>>> sim = SimRes('examples/ChuaCircuit.mat')
>>> sim.get_FV('L.v')
-0.25352862
Return the initial values of variable(s).
Arguments:
If names is a string, then the output will be a single value. If names is a (optionally nested) list of strings, then the output will be a (nested) list of values.
Example:
>>> from modelicares import SimRes
>>> sim = SimRes('examples/ChuaCircuit.mat')
>>> sim.get_IV('L.v')
0.0
Return the description(s) of trajectory variable(s).
Arguments:
names: Name(s) of the variable(s) from which to get the description(s)
This may be a string or (possibly nested) list of strings representing the names of the variables.
If names is a string, then the output will be a single description. If names is a (optionally nested) list of strings, then the output will be a (nested) list of descriptions.
Example:
>>> from modelicares import SimRes
>>> sim = SimRes('examples/ChuaCircuit.mat')
>>> sim.get_description('L.v')
'Voltage drop between the two pins (= p.v - n.v)'
Return the Modelica displayUnit attribute(s) of trajectory variable(s).
Arguments:
names: Name(s) of the variable(s) from which to get the display unit(s)
This may be a string or (possibly nested) list of strings representing the names of the variables.
If names is a string, then the output will be a single display unit. If names is a (optionally nested) list of strings, then the output will be a (nested) list of display units.
Example:
>>> from modelicares import SimRes
>>> sim = SimRes('examples/ChuaCircuit.mat')
>>> sim.get_displayUnit('G.T_heatPort')
'degC'
Return the widest index pair(s) for which the time of signal(s) is within given limits.
Arguments:
If names is a string, then the output will be an array of values. If names is a (optionally nested) list of strings, then the output will be a (nested) list of arrays.
Example:
>>> from modelicares import SimRes
>>> sim = SimRes('examples/ChuaCircuit.mat')
>>> sim.get_indices_wi_times('L.v', t_1=500, t_2=2000)
(104, 412)
Return vector(s) of the sample times of variable(s).
Arguments:
names: String or (possibly nested) list of strings of the variable names
i: Index (-1 for last), list of indices, or slice of the time(s) to return
By default, all times are returned.
f: Function that should be applied to all times (default is identity)
If names is a string, then the output will be an array of times. If names is a (optionally nested) list of strings, then the output will be a (nested) list of arrays.
Example:
>>> from modelicares import SimRes
>>> sim = SimRes('examples/ChuaCircuit.mat')
>>> sim.get_times('L.v')
array([ 0. , ... 2500. ], dtype=float32)
Return the unit attribute(s) of trajectory variable(s).
Arguments:
names: Name(s) of the variable(s) from which to get the unit(s)
This may be a string or (possibly nested) list of strings representing the names of the variables.
If names is a string, then the output will be a single unit. If names is a (optionally nested) list of strings, then the output will be a (nested) list of units.
Example:
>>> from modelicares import SimRes
>>> sim = SimRes('examples/ChuaCircuit.mat')
>>> sim.get_unit('L.v')
'V'
Return vector(s) of the values of the samples of variable(s).
If there is only a single value, then it is repeated (to support plotting).
Arguments:
names: String or (possibly nested) list of strings of the variable names
i: Index (-1 for last), list of indices, or slice of the values(s) to return
By default, all values are returned.
f: Function that should be applied to all values (default is identity)
If names is a string, then the output will be an array of values. If names is a (optionally nested) list of strings, then the output will be a (nested) list of arrays.
Example:
>>> from modelicares import SimRes
>>> sim = SimRes('examples/ChuaCircuit.mat')
>>> sim.get_values('L.v')
array([ 0.00000000e+00, ... -2.53528625e-01], dtype=float32)
If the variable cannot be found, then possible matches are listed:
>>> sim.get_values(['L.vv'])
"L.vv" is not a valid variable name.
Did you mean one of the these?
L.v
L.p.v
L.n.v
The other get_* methods also give this message when a variable cannot be found.
Return vector(s) of the values of the samples of variable(s) (at optionally given times).
If names is a string, then the output will be an array of values. If names is a (optionally nested) list of strings, then the output will be a (nested) list of arrays.
If times is not provided, all of the samples will be returned. If necessary, the values will be interpolated over time. The function f is applied before interpolation.
Example:
>>> from modelicares import SimRes
>>> sim = SimRes('examples/ChuaCircuit.mat')
>>> sim.get_values_at_times('L.v', [0, 2000])
array([ 0. , 0.15459341])
Return a list of all variable names.
Example:
>>> from modelicares import SimRes
>>> sim = SimRes('examples/ChuaCircuit.mat')
>>> sim.keys()
[u'L.p.i', u'Ro.alpha', ... u'Ro.LossPower']
Return a list of all variable names.
Example:
>>> from modelicares import SimRes
>>> sim = SimRes('examples/ChuaCircuit.mat')
>>> sim.names()
[u'L.p.i', u'Ro.alpha', ... u'Ro.LossPower']
Return a tree of all variable names with respect to the path names.
The tree represents the structure of the Modelica model. It is returned as a nested dictionary. The keys are the path elements and the values are sub-dictionaries or variable names.
There are no arguments.
Example:
>>> from modelicares import SimRes
>>> sim = SimRes('examples/ChuaCircuit.mat')
>>> sim.nametree()
{u'G': {u'G': u'G.G', ... u'n': {u'i': u'G.n.i', u'v': u'G.n.v'}, ...}, u'L': {...}, ...}
Plot data as points and/or curves in 2D Cartesian coordinates.
Arguments:
ynames1: Names of variables for the primary y axis
If any names are invalid, then they will be skipped.
ylabel1: Label for the primary y axis
If ylabel1 is None (default) and all of the variables have the same Modelica description string, then the common description will be used. Use ‘’ for no label.
legends1: List of legend entries for variables assigned to the primary y axis
If legends1 is an empty list ([]), ynames1 will be used. If legends1 is None and all of the variables on the primary axis have the same unit, then no legend will be shown.
leg1_kwargs: Dictionary of keyword arguments for the primary legend
ax1: Primary y axes
If ax1 is not provided, then axes will be created in a new figure.
ynames2, ylabel2, legends2, leg2_kwargs, and ax2: Similar to ynames1, ylabel1, legends1, leg1_kwargs, and ax1 but for the secondary y axis
xname: Name of the x-axis data
xlabel: Label for the x axis
If xlabel is None (default), the variable’s Modelica description string will be applied. Use ‘’ for no label.
title: Title for the figure
If title is None (default), then the title will be the base filename. Use ‘’ for no title.
label: Label for the figure (ignored if ax is provided)
This will be used as a base filename if the figure is saved.
incl_prefix: If True, prefix the legend strings with the base filename of the class.
suffix: String that will be added at the end of the legend entries
use_paren: Add parentheses around the suffix
**kwargs: Propagated to base.plot() (and thus to matplotlib.pyplot.plot())
If both y axes are used (primary and secondary), then the dashes argument is ignored. The curves on the primary axis will be solid and the curves on the secondary axis will be dotted.
Returns:
Example:
>>> from modelicares import SimRes, saveall
>>> sim = SimRes('examples/ChuaCircuit.mat')
>>> sim.plot(ynames1='L.i', ylabel1="Current",
... ynames2='L.der(i)', ylabel2="Derivative of current",
... title="Chua Circuit", label='examples/ChuaCircuit')
(<matplotlib.axes._subplots.AxesSubplot object at 0x...>, <matplotlib.axes._subplots.AxesSubplot object at 0x...>)
>>> saveall()
Saved examples/ChuaCircuit.pdf
Saved examples/ChuaCircuit.png
Create a figure with Sankey diagram(s).
Arguments:
names: List of names of the flow variables
times: List of times at which the data should be sampled
If multiple times are given, then subfigures will be generated, each with a Sankey diagram.
n_rows: Number of rows of (sub)plots
title: Title for the figure
If title is None (default), then the title will be “Sankey Diagram of fbase”, where fbase is the base filename of the data. Use ‘’ for no title.
subtitles: List of subtitles (i.e., titles for each subplot)
If not provided, “t = xx s” will be used, where xx is the time of each entry. “(initial)” or “(final)” is appended if appropriate.
label: Label for the figure
This will be used as the base filename if the figure is saved.
margin_left: Left margin
margin_right: Right margin
margin_bottom: Bottom margin
margin_top: Top margin
wspace: The amount of width reserved for blank space between subplots
hspace: The amount of height reserved for white space between subplots
**kwargs: Additional arguments for matplotlib.sankey.Sankey
Returns:
Example:
>>> from modelicares import SimRes, saveall
>>> sim = SimRes('examples/ThreeTanks')
>>> sankeys = sim.sankey(label='examples/ThreeTanks',
... title="Sankey Diagrams of Modelica.Fluid.Examples.Tanks.ThreeTanks",
... times=[0, 50, 100, 150], n_rows=2, format='%.1f ',
... names=['tank1.ports[1].m_flow', 'tank2.ports[1].m_flow',
... 'tank3.ports[1].m_flow'],
... labels=['Tank 1', 'Tank 2', 'Tank 3'],
... orientations=[-1, 0, 1],
... scale=0.100, margin=6, offset=1.5,
... pathlengths=2, trunklength=10)
>>> saveall()
Saved examples/ThreeTanks.pdf
Saved examples/ThreeTanks.png
Merge a list of multiple time vectors into one vector.
Example:
>>> from modelicares.simres import SimRes, merge_times
>>> sim = SimRes('examples/ChuaCircuit.mat')
>>> times_list = sim.get_times(['L.v', 'G.T_heatPort'])
>>> merge_times(times_list)
array([ 0. , ... 2500. ], dtype=float32)