bim2sim.export.modelica package

Package for Modelica export

class bim2sim.export.modelica.Dummy(element: HVACProduct)

Bases: ModelicaElement

path: str = 'Path.to.Dummy'
represents

alias of Dummy

exception bim2sim.export.modelica.FactoryError

Bases: Exception

Error in Model factory

exception bim2sim.export.modelica.ModelError

Bases: Exception

Error occurring in model

class bim2sim.export.modelica.ModelicaElement(element: HVACProduct)

Bases: object

Modelica model element

This class represents an element of a Modelica model, which includes elements, parameters, connections, and other metadata.

Attributes:

library: The library the instance belongs to. version: The version of the library. path: The path of the model in the library. represents: The element or a container of elements that the instance

represents.

lookup: A dictionary mapping element types to instance types. dummy: A placeholder for an instance. _initialized: Indicates whether the instance has been initialized.

# TODO describe the total process

collect_params()

Collects the parameters of the instance.

dummy: Type[ModelicaElement] = None
static factory(element: HVACProduct)

Create model depending on ifc_element

get_comment() str

Returns comment string

get_full_port_name(port: HVACPort) str

Returns name of port including model name.

Args:

port: The HVACPort for which to get the full name.

Returns:

The full name of the port as string.

get_port_name(port: HVACPort) str

Get the name of port. Override this method in a subclass.

Args:

port: The HVACPort for which to get the name.

Returns:

The name of the port as string.

static init_factory(libraries: tuple)
Initializes the lookup dictionary for the factory with the provided

libraries.

Args:

libraries: A tuple of libraries to initialize the factory with.

Raises:
AssertionError: If a library is not defined or if there are

conflicts in models.

library: str = None
lookup: Dict[Type[Element], Type[ModelicaElement]] = {}
property modelica_parameters: dict

Converts and returns the instance parameters to Modelica parameters.

Returns:
A dictionary of Modelica parameters with key as name and value as

the parameter in Modelica code.

property path

Returns the model path in the library

represents: Element | Container[Element] = None
version = None
class bim2sim.export.modelica.ModelicaModel(name: str, comment: str, modelica_elements: List[ModelicaElement], connections: list)

Bases: object

Modelica model

code() str
Returns the Modelica code for the model.The mako template is used to

render the Modelica code based on the model’s elements, connections, and unknown parameters.

Returns

str: The Modelica code representation of the model.

save(path: str)

Save the model as Modelica file.

Args:

path (str): The path where the Modelica file should be saved.

set_positions(elements: list, connections: list) list
Sets the position of elements relative to min/max positions of

instance.element.position

Args:

elements: A list of elements whose positions are to be set. connections: A list of connections between the elements.

Returns:

A list of connections with positions.

unknown_params() list
Identifies unknown parameters in the model. Unknown parameters are

parameters with None value and that are required by the model.

Returns:

A list of unknown parameters in the model.

class bim2sim.export.modelica.ModelicaParameter(name: str, unit: Unit, required: bool, element: HVACProduct, **kwargs)

Bases: object

Represents a parameter in a Modelica model.

Attributes:

_decisions: Collection of decisions related to parameters. _answers: Dictionary to store answers for parameter decisions.

collect()

Collects the value of the parameter based on its source.

This method performs the following steps: 1. If the parameter has a function assigned:

  • Collects all function inputs, either as ModelicaParameter values

    or attribute values.

  • Calls the function with the collected inputs and converts the

    function output to the parameter’s value.

  1. If the parameter is required and has no attributes:
    • Sets the parameter value from the collected answers.

  2. If the parameter has attributes:
    • Retrieves the attribute value(s) and converts them to the

      parameter’s value.

  3. If the parameter already has a value, it retains the existing value.

  4. If none of the above conditions are met, sets the parameter value to

    None and logs a warning.

convert_parameter(parameter: Quantity | list) Quantity | list

Converts a parameter to its appropriate unit.

Args:

parameter: The parameter to convert.

Returns:

The converted parameter.

get_attribute_value() List[Quantity] | Quantity
Retrieves the value(s) of the parameter’s attributes from the

associated element.

Returns:

The attribute value(s) as a list of pint.Quantity objects if there are multiple attributes, or a single pint.Quantity object if there is only one attribute.

classmethod get_pending_parameter_decisions()

Yields pending parameter decisions.

Yields:

The decisions related to the parameters.

register()
Registers the parameter, requesting necessary element attributes or

creating decisions if necessary.

This method performs the following steps: 1. If the parameter is required and does not have a function assigned:

  • Requests the specified attributes from the element.

  • If no attributes are specified, creates a decision for the

    parameter.

  1. If the parameter has a function assigned:
    • Processes the function inputs, which can be either

      ModelicaParameter instances or element attributes.

    • Raises an AttributeError if the function input is neither an

      attribute nor a ModelicaParameter.

to_modelica()
property value

Returns the current value of the parameter.

bim2sim.export.modelica.check_none()

Generates a function to check if a given value is not None.

bim2sim.export.modelica.check_numeric(min_value: Quantity | None = None, max_value: Quantity | None = None)
Generates a function to check if a given value falls within specified

numeric bounds.

This function creates and returns a checker function (inner_check) that validates whether a given value (a pint.Quantity) falls within the range defined by min_value and max_value.

Args:

min_value: The minimum value for the range check. max_value: The maximum value for the range check.

Raises:
AssertionError: If min_value or max_value is not a pint.Quantity

or None.

Returns:
A function (inner_check) that takes a single argument value` and

returns True if the value is within the specified bounds, otherwise False.

bim2sim.export.modelica.clean_string(string: str) str

Replace modelica invalid chars by underscore.

bim2sim.export.modelica.parse_to_modelica(name: str | None, value: Any) str | None

Converts a parameter to a Modelica-readable string.

Args:

name: The name of the parameter. value: The value of the parameter.

Returns:

The Modelica-readable string representation of the parameter.

The conversion handles different data types as follows: - bool: Converted to “true” or “false”. - ModelicaParameter: Recursively converts the parameter’s name and value. - pint.Quantity: Converts the magnitude of the quantity. - int, float, str: Directly converted to their string representation. - list, tuple, set: Converted to a comma-separated list enclosed in curly

braces.

  • dict: Converted to a Modelica record format, with each key-value pair

    converted recursively.

  • Path: Converts to a Modelica file resource load function call.

  • Other types: Logs a warning and converts to a string representation.

Submodules

bim2sim.export.modelica.standardlibrary module

Modul containing model representations from the Modelica Standard Library

class bim2sim.export.modelica.standardlibrary.ClosedVolume(element)

Bases: StandardLibrary

get_port_name(port)

Get the name of port. Override this method in a subclass.

Args:

port: The HVACPort for which to get the name.

Returns:

The name of the port as string.

path: str = 'Modelica.Fluid.Vessels.ClosedVolume'
represents: Element | Container[Element] = [<class 'bim2sim.elements.hvac_elements.Storage'>]
class bim2sim.export.modelica.standardlibrary.Parameter(ifc_attribute_name: str, modelica_name: str)

Bases: object

ifc_attribute_name: str
modelica_name: str
class bim2sim.export.modelica.standardlibrary.StandardLibrary(element: HVACProduct)

Bases: ModelicaElement

Base class for Modelica Standard Library

library: str = 'Modelica Standard Library'
class bim2sim.export.modelica.standardlibrary.StaticPipe(element: Pipe)

Bases: StandardLibrary

ID_PARAMETER_LENGTH = 'length'
get_port_name(port)

Get the name of port. Override this method in a subclass.

Args:

port: The HVACPort for which to get the name.

Returns:

The name of the port as string.

mappser = {'length': 'length'}
path: str = 'Modelica.Fluid.Pipes.StaticPipe'
represents: Element | Container[Element] = [<class 'bim2sim.elements.hvac_elements.Pipe'>, <class 'bim2sim.elements.hvac_elements.PipeFitting'>, <class 'bim2sim.elements.aggregation.hvac_aggregations.PipeStrand'>]
class bim2sim.export.modelica.standardlibrary.TeeJunctionVolume(element)

Bases: StandardLibrary

get_port_name(port)

Get the name of port. Override this method in a subclass.

Args:

port: The HVACPort for which to get the name.

Returns:

The name of the port as string.

path: str = 'Modelica.Fluid.Fittings.TeeJunctionVolume'
represents: Element | Container[Element] = [<class 'bim2sim.elements.hvac_elements.Junction'>]
class bim2sim.export.modelica.standardlibrary.Valve(element)

Bases: StandardLibrary

get_port_name(port)

Get the name of port. Override this method in a subclass.

Args:

port: The HVACPort for which to get the name.

Returns:

The name of the port as string.

path: str = 'Modelica.Fluid.Valves.ValveIncompressible'
represents: Element | Container[Element] = [<class 'bim2sim.elements.hvac_elements.Valve'>]