ebcpy.modelica package
This package aims to help manipulate simulation files (dsfinal.txt or dsin.txt) or to load simulation result files (.mat) efficiently into a pandas.DataFrame
- ebcpy.modelica.get_expressions(filepath_model: str, get_protected: bool = False, modelica_type: str | List = 'parameters', excludes: List | None = None)[source]
This function extracts specific expressions out of modelica models.
- Parameters:
filepath_model (str,os.path.normpath) – Full path of modelica model on the given os e.g. path_model = “C://MyLibrary//TestModel.mo”
modelica_type (str,list) – Type you want to have matched. “parameters” and “variables” have a special regex pattern. For other models, you can parse a string like: “replaceable package Medium” and it will yield all afflicted lines. You can also give a list of strings if multiple strings are relevant to you. Special cases: parameters: - include: [“parameter”] - excludes: [“final”, “in”, “of”, “replaceable”] variables: Note: The case for already imported SIUnits is not considered here. - include: [“Modelica.SIunits”, “Real”, “Boolean”, “Integer”] - excludes: [“parameter”, “import”, “constant”]
excludes (list) – List of strings to exclude from expression. Default is None.
get_protected (Boolean) – Whether to extract protected parameters or not. Default is false
- Returns:
list matches List with all lines matching the given expression.
- ebcpy.modelica.get_names_and_values_of_lines(lines: List[str]) dict [source]
All unnecessary code is deleted (annotations, doc). Only the name of the variable and the value is extracted.
- Parameters:
lines (List[str]) – List of strings with lines from a modelica file.
- Returns:
dict: Containing the names as key and values as value.
Example:
>>> lines = ['parameter Boolean my_boolean=true "Some description"', >>> 'parameter Real my_real=12.0 "Some description" annotation("Some annotation")'] >>> output = get_names_and_values_of_lines(lines=lines) >>> print(output) {'my_boolean': True, 'my_real': 12.0}
Submodules
ebcpy.modelica.manipulate_ds module
Functions to manipulate (or extract information of) the dsfinal.txt and dsin.txt files created by Modelica.
- ebcpy.modelica.manipulate_ds.convert_ds_file_to_dataframe(filename)[source]
Function to convert a given dsfinal or dsfin file to a DataFrame. The index is the name of the variable. Further, the following columns are used analog to the dsfinal: column 1: Type of initial value: = -2: special case: for continuing simulation (column 2 = value) = -1: fixed value (column 2 = fixed value) = 0: free value, i.e., no restriction (column 2 = initial value) > 0: desired value (column 1 = weight for optimization, column 2 = desired value) use weight=1, since automatic scaling usually leads to equally weighted terms column 2: fixed, free or desired value according to column 1. column 3: Minimum value (ignored, if Minimum >= Maximum). column 4: Maximum value (ignored, if Minimum >= Maximum). Minimum and maximum restrict the search range in initial value calculation. They might also be used for scaling. column 5: Category of variable: = 1: parameter. = 2: state. = 3: state derivative. = 4: output. = 5: input. = 6: auxiliary variable. column 6: Data type of variable and flags according to dsBaseType
- Parameters:
filename (str,os.path.normpath) – Filepath to the dsfinal or dsinto be loaded.
- Returns:
pd.DataFrame Converted DataFrame
- ebcpy.modelica.manipulate_ds.eliminate_parameters_from_ds_file(filename, savepath, exclude_paras, del_aux_paras=True)[source]
Create a new dsfinal file out of the given dsfinal.txt All parameters except those listed in exclude_paras will be eliminated from the dsfinal file. Used for continuing of simulation in calibration problems.
- Parameters:
ebcpy.modelica.simres module
Module based on the simres module of modelicares. As no new content is going to be merged upstream, this “fork” of the to_pandas() function is used.
Update 18.01.2021: As modelicares is no longer compatible with matplotlib > 3.3.2, we integrated all necessary functions from modelicares to still be able and use loadsim functions.
New in version 0.1.7.
- class ebcpy.modelica.simres.Samples(times, values, negated)
Bases:
tuple
- negated
Alias for field number 2
- times
Alias for field number 0
- values
Alias for field number 1
- class ebcpy.modelica.simres.Variable(samples, description, unit, displayUnit)[source]
Bases:
VariableNamedTuple
Special namedtuple to represent a variable in a simulation, with methods to retrieve and perform calculations on its values
This class is usually not instantiated directly by the user, but instances are returned when indexing a variable name from a simulation result (
SimRes
instance).
- ebcpy.modelica.simres.get_strings(str_arr)[source]
Return a list of strings from a character array.
Strip the whitespace from the right and recode it as utf-8.
- ebcpy.modelica.simres.loadsim(fname, constants_only=False)[source]
Load Dymola® or OpenModelica simulation results.
Arguments:
fname: Name of the results file, including the path
The file extension (‘.mat’) is optional.
constants_only: True to load only the variables from the first data matrix
The first data matrix usually contains all of the constants, parameters, and variables that don’t vary. If only that information is needed, it may save resources to set constants_only to True.
Returns: An instance of dict
- ebcpy.modelica.simres.mat_to_pandas(fname='dsres.mat', names=None, aliases=None, with_unit=True, constants_only=False)[source]
Return a pandas.DataFrame with values from selected variables for the given .mat file.
The index is time. The column headings indicate the variable names and units.
- Parameters:
fname (str) – The mat file to load.
names (list) – If None (default), then all variables are included.
aliases (dict) –
Dictionary of aliases for the variable names
The keys are the “official” variable names from the Modelica model and the values are the names as they should be included in the column headings. Any variables not in this list will not be aliased. Any unmatched aliases will not be used.
with_unit (bool) –
Boolean to determine format of keys. Default value is True.
If set to True, the unit will be added to the key. As not all modelica- result files export the unit information, using with_unit=True can lead to errors.
constants_only (bool) – The first data matrix usually contains all of the constants, parameters, and variables that don’t vary. If only that information is needed, it may save resources to set constants_only to True.
- ebcpy.modelica.simres.read(fname, constants_only=False)[source]
Read variables from a MATLAB® file with Dymola® or OpenModelica results.
Arguments:
fname: Name of the results file, including the path
This may be from a simulation or linearization.
constants_only: True to load only the variables from the first data matrix, if the result is from a simulation
Returns:
A dictionary of variables
A list of strings from the lines of the ‘Aclass’ matrix