Coverage for bim2sim/export/modelica/standardlibrary.py: 71%
70 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-12 17:09 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-12 17:09 +0000
1"""Modul containing model representations from the Modelica Standard Library"""
2from dataclasses import dataclass
3from typing import Union
5import bim2sim.elements.aggregation.hvac_aggregations
6from bim2sim.export import modelica
7from bim2sim.elements import hvac_elements as hvac
8from bim2sim.elements.mapping.units import ureg
9from bim2sim.export.modelica import ModelicaParameter, check_numeric
11MEDIUM_WATER = 'Modelica.Media.Water.ConstantPropertyLiquidWater'
14@dataclass(frozen=True)
15class Parameter:
16 ifc_attribute_name: str
17 modelica_name: str
20class StandardLibrary(modelica.ModelicaElement):
21 """Base class for Modelica Standard Library"""
22 library = "Modelica Standard Library"
25class StaticPipe(StandardLibrary):
26 path = "Modelica.Fluid.Pipes.StaticPipe"
27 represents = [hvac.Pipe, hvac.PipeFitting,
28 bim2sim.elements.aggregation.hvac_aggregations.PipeStrand]
30 ID_PARAMETER_LENGTH = 'length'
32 mappser ={
33 ID_PARAMETER_LENGTH: 'length'
34 }
36 def __init__(self, element: Union[hvac.Pipe]):
37 super().__init__(element)
38 self._set_parameter(name='redeclare package Medium',
39 unit=None,
40 required=False,
41 value=MEDIUM_WATER)
42 self._set_parameter(name='length',
43 unit=ureg.meter,
44 required=True,
45 check=check_numeric(min_value=0 * ureg.meter),
46 attributes=['length'])
47 self._set_parameter(name='diameter',
48 unit=ureg.meter,
49 required=True,
50 check=check_numeric(min_value=0 * ureg.meter),
51 attributes=['diameter'])
53 def get_port_name(self, port):
54 if port.verbose_flow_direction == 'SINK':
55 return 'port_a'
56 if port.verbose_flow_direction == 'SOURCE':
57 return 'port_b'
58 else:
59 return super().get_port_name(port)
62class Valve(StandardLibrary):
63 path = "Modelica.Fluid.Valves.ValveIncompressible"
64 represents = [hvac.Valve]
66 def __init__(self, element):
67 super().__init__(element)
68 self._set_parameter(name='redeclare package Medium',
69 unit=None,
70 required=False,
71 value=MEDIUM_WATER)
72 self._set_parameter(name='dp_nominal',
73 unit=ureg.bar,
74 required=True,
75 check=check_numeric(min_value=0 * ureg.bar),
76 attributes=['nominal_pressure_difference'],)
77 self._set_parameter(name='m_flow_nominal',
78 unit=ureg.kg / ureg.s,
79 required=True,
80 check=check_numeric(min_value=0 * ureg.kg / ureg.s),
81 attributes=['nominal_mass_flow_rate'])
83 def get_port_name(self, port):
84 if port.verbose_flow_direction == 'SINK':
85 return 'port_a'
86 if port.verbose_flow_direction == 'SOURCE':
87 return 'port_b'
88 else:
89 return super().get_port_name(port)
92class ClosedVolume(StandardLibrary):
93 path = "Modelica.Fluid.Vessels.ClosedVolume"
94 represents = [hvac.Storage]
96 def __init__(self, element):
97 super().__init__(element)
98 self._set_parameter(name='redeclare package Medium',
99 unit=None,
100 required=False,
101 value=MEDIUM_WATER)
102 self._set_parameter(name='V',
103 unit=ureg.meter ** 3,
104 required=True,
105 check=check_numeric(min_value=0 * ureg.meter ** 3),
106 attributes=['volume'])
108 def get_port_name(self, port):
109 try:
110 index = self.element.ports.index(port)
111 except ValueError:
112 return super().get_port_name(port)
113 else:
114 return "ports[%d]" % index
117class TeeJunctionVolume(StandardLibrary):
118 path = "Modelica.Fluid.Fittings.TeeJunctionVolume"
119 represents = [hvac.Junction]
121 def __init__(self, element):
122 super().__init__(element)
123 self._set_parameter(name='redeclare package Medium',
124 unit=None,
125 required=False,
126 value=MEDIUM_WATER)
127 self._set_parameter(name='V',
128 unit=ureg.meter ** 3,
129 required=True,
130 check=check_numeric(min_value=0 * ureg.meter ** 3),
131 attributes=['volume'])
133 def get_port_name(self, port):
134 try:
135 index = self.element.ports.index(port)
136 except ValueError:
137 return super().get_port_name(port)
138 else:
139 return "port_%d" % (index + 1)
140 # TODO: name ports by flow direction?