Skip to content
153 changes: 153 additions & 0 deletions src/tespy/components/subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ class is the base class for custom subsystems.
from tespy.components import SubsystemInterface
from tespy.tools import logger
from tespy.tools.helpers import TESPyComponentError
from tespy.tools.units import Units
import tespy
import importlib
#from tespy.components.component import component_registry
#from tespy.connections.connection import connection_registry
#from tespy.tools import helpers as hlp


class Subsystem:
Expand Down Expand Up @@ -255,3 +261,150 @@ def create_network(self):
"Subsystem class and overwrite this method."
)
raise NotImplementedError(msg)

#def from_network(nw: tespy.networks.Network):
# nw_dict=nw.export()
# nw_import=tespy.networks.Network().from_dict(nw_dict)
@classmethod
def from_dict(cls, subsystem_data):
# create network
# get method to ensure compatibility with old style export
units = Units.from_json(subsystem_data["Network"].get("units", {}))
subsystem_data["Network"]["units"] = units
nw = tespy.networks.Network(**subsystem_data["Network"])
sub = cls.__new__(cls)
sub.label = subsystem_data['Subsystem']['label']
# load components
comps = {}
module_name = "tespy.components"
_ = importlib.import_module(module_name)
sub.num_in = subsystem_data['Subsystem']['num_in']
sub.num_out = subsystem_data['Subsystem']['num_out']
sub.num_heat_in = subsystem_data['Subsystem']['num_heat_in']
sub.num_heat_out = subsystem_data['Subsystem']['num_heat_out']
sub.num_power_in = subsystem_data['Subsystem']['num_power_in']
sub.num_power_out = subsystem_data['Subsystem']['num_power_out']

sub.conns={}
sub.comps={}

for component, data in subsystem_data["Component"].items():
if component not in tespy.components.component.component_registry.items:
msg = (
f"A class {component} is not available through the "
"tespy.components.component.component_registry decorator. "
"If you are using a custom component make sure to "
"decorate the class."
)
##logger.error(msg)
raise hlp.TESPyNetworkError(msg)

target_class = tespy.components.component.component_registry.items[component]
comps.update(tespy.networks.network._construct_components(target_class, data, nw))
sub.inlet= comps[f'{sub.label}_inlet']
sub.outlet= comps[f'{sub.label}_outlet']
msg = 'Created network components.'
##logger.info(msg)

conns = {}
# load connections
if "Connection" not in subsystem_data["Connection"]:
# v0.8 compatibility
target_class = tespy.connections.connection.connection_registry.items["Connection"]
conns.update(tespy.networks.network._construct_connections(
target_class, subsystem_data["Connection"], comps)
)
else:
for connection, data in subsystem_data["Connection"].items():
if connection not in tespy.connections.connection.connection_registry.items:
msg = (
f"A class {connection} is not available through the "
"tespy.connections.connection.connection_registry "
"decorator. If you are using a custom connection make "
"sure to decorate the class."
)
##logger.error(msg)
raise hlp.TESPyNetworkError(msg)

target_class = tespy.connections.connection.connection_registry.items[connection]
conns.update(tespy.networks.network._construct_connections(target_class, data, comps))

# add connections to network
for c in conns.values():
sub.add_conns(c)

msg = 'Created connections.'
logger.info(msg)

msg = 'Created Subsystem.'
logger.info(msg)


return sub

@classmethod
def from_network(cls, label, nw, interface_exceptions=[]):


nw_dict=nw.export()
nw_dict['Component']['SubsystemInterface']={}
nw_dict['Component']['SubsystemInterface'].update(
SubsystemInterface(f'{label}_inlet')._serialize())
nw_dict['Component']['SubsystemInterface'].update(
SubsystemInterface(f'{label}_outlet')._serialize())


nw_dict['Subsystem']={}
for attr in ['num_in', 'num_out', 'num_heat_in', 'num_heat_out', 'num_power_in','num_power_out']:
nw_dict['Subsystem'][attr]=0

for typ, con in nw_dict['Connection'].get('Connection',{}).items():
if con['source'] in nw_dict['Component'].get('Source', {}).keys() and con['source'] not in interface_exceptions:
del nw_dict['Component']['Source'][con['source']]
con['source']= f'{label}_inlet'
nw_dict['Subsystem']['num_in'] +=1
con['source_id']=f'out{nw_dict['Subsystem']['num_in']}'

if con['target'] in nw_dict['Component'].get('Sink', {}).keys() and con['target'] not in interface_exceptions:
del nw_dict['Component']['Sink'][con['target']]
con['target']= f'{label}_outlet'
nw_dict['Subsystem']['num_out'] +=1
con['target_id']=f'in{nw_dict['Subsystem']['num_out'] }'

for typ, con in nw_dict['Connection'].get('HeatConnection',{}).items():
if con['source'] in nw_dict['Component'].get('HeatSource',{}).keys() and con['source'] not in interface_exceptions:
del nw_dict['Component']['HeatSource'][con['source']]
con['source']= f'{label}_inlet'
nw_dict['Subsystem']['num_heat_in'] +=1
con['source_id']=f'heat_out{nw_dict['Subsystem']['num_heat_in']}'

if con['target'] in nw_dict['Component'].get('HeatSink',{}).keys() and con['target'] not in interface_exceptions:
del nw_dict['Component']['HeatSink'][con['target']]
con['target']= f'{label}_outlet'
nw_dict['Subsystem']['num_heat_out'] +=1
con['target_id']=f'heat_in{nw_dict['Subsystem']['num_heat_out'] }'

for typ, con in nw_dict['Connection'].get('PowerConnection', {}).items():
if con['source'] in nw_dict['Component'].get('PowerSource',{}).keys() and con['source'] not in interface_exceptions:
del nw_dict['Component']['PowerSource'][con['source']]
con['source']= f'{label}_inlet'
nw_dict['Subsystem']['num_power_in'] +=1
con['source_id']=f'power_out{nw_dict['Subsystem']['num_power_in']}'

if con['target'] in nw_dict['Component'].get('PowerSink',{}).keys() and con['target'] not in interface_exceptions:
del nw_dict['Component']['PowerSink'][con['target']]
con['target']= f'{label}_outlet'
nw_dict['Subsystem']['num_power_out'] +=1
con['target_id']=f'power_in{nw_dict['Subsystem']['num_power_out'] }'

nw_dict['Component']['SubsystemInterface'][f'{label}_outlet']['num_inter']={'val':nw_dict['Subsystem']['num_out'], 'is_set':True}
nw_dict['Component']['SubsystemInterface'][f'{label}_inlet']['num_inter']={'val':nw_dict['Subsystem']['num_in'], 'is_set':True}
nw_dict['Component']['SubsystemInterface'][f'{label}_outlet']['num_heat_inter']={'val':nw_dict['Subsystem']['num_heat_out'], 'is_set':True}
nw_dict['Component']['SubsystemInterface'][f'{label}_inlet']['num_heat_inter']={'val':nw_dict['Subsystem']['num_heat_in'], 'is_set':True}
nw_dict['Component']['SubsystemInterface'][f'{label}_outlet']['num_power_inter']={'val':nw_dict['Subsystem']['num_power_out'], 'is_set':True}
nw_dict['Component']['SubsystemInterface'][f'{label}_inlet']['num_power_inter']={'val':nw_dict['Subsystem']['num_power_in'], 'is_set':True}
nw_dict['Subsystem']['label']= label
sub_network= cls.from_dict(nw_dict)


return sub_network
2 changes: 1 addition & 1 deletion src/tespy/networks/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from tabulate import tabulate

from tespy.components.component import component_registry

from tespy.connections.connection import ConnectionBase
from tespy.connections.connection import connection_registry
from tespy.solver import Problem
Expand Down Expand Up @@ -3400,7 +3401,6 @@ def _export_components(self):

return components


def _construct_components(target_class, data, nw):
r"""
Create TESPy component from class name and set parameters.
Expand Down
Loading