Skip to content

Commit

Permalink
API adjustements for tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
fwitte committed Sep 25, 2023
1 parent 22733ce commit 191d2e8
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 80 deletions.
17 changes: 11 additions & 6 deletions src/tespy/networks/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,7 @@ def init_offdesign_params(self):
)
logger.debug(msg)
df = pd.read_csv(path, sep=';', decimal='.', index_col=0)
df.index = df.index.astype(str)

# iter through all components of this type and set data
_individual_design_paths = {}
Expand All @@ -1400,6 +1401,9 @@ def init_offdesign_params(self):
_individual_design_paths[path_c] = pd.read_csv(
path_c, sep=';', decimal='.', index_col=0
)
_individual_design_paths[path_c].index = (
_individual_design_paths[path_c].index.astype(str)
)
data = _individual_design_paths[path_c].loc[comp.label]

else:
Expand Down Expand Up @@ -1480,14 +1484,14 @@ def init_conn_design_params(self, c, df):
"""
# match connection (source, source_id, target, target_id) on
# connection objects of design file
df.index = df.index.astype(str)
if c.label not in df.index:
# no matches in the connections of the network and the design files
msg = (
'Could not find connection %s in design case. '
'Please, make sure no connections have been modified or '
'components have been relabeled for your offdesign '
'calculation.'
).format(c.label)
f"Could not find connection {c.label} in design case. Please "
"make sure no connections have been modified or components "
"havebeen relabeled for your offdesign calculation."
)
logger.exception(msg)
raise hlp.TESPyNetworkError(msg)

Expand All @@ -1514,9 +1518,10 @@ def init_conn_params_from_path(self, c, df):
"""
# match connection (source, source_id, target, target_id) on
# connection objects of design file
df.index = df.index.astype(str)
if c.label not in df.index:
# no matches in the connections of the network and the design files
msg = ('Could not find connection %s in init path file.').format(c.label)
msg = f"Could not find connection {c.label} in init path file."
logger.debug(msg)
return

Expand Down
29 changes: 10 additions & 19 deletions tutorial/advanced/starting_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
wf = "NH3"

# network
nw = Network(
fluids=["water", wf],
T_unit="C", p_unit="bar", h_unit="kJ / kg", m_unit="kg / s"
)
nw = Network(T_unit="C", p_unit="bar", h_unit="kJ / kg", m_unit="kg / s")

# components
cycle_closer = CycleCloser("Refrigerant Cycle Closer")
Expand Down Expand Up @@ -50,10 +47,7 @@
c5 = Connection(int_heatex, "out1", valve, "in1", label="5")
c6 = Connection(valve, "out1", cycle_closer, "in1", label="6")

nw.add_conns(
c0, c1, c2, c3, c4,
c5, c6
)
nw.add_conns(c0, c1, c2, c3, c4, c5, c6)

# heat source
c11 = Connection(heatsource_feedflow, "out1", heatsource_pump, "in1", label="11")
Expand All @@ -78,15 +72,15 @@
T_cons_ff = 90

# consumer cycle
c23.set_attr(T=T_cons_ff, p=10, fluid={"water": 1, wf: 0})
c23.set_attr(T=T_cons_ff, p=10, fluid={"water": 1})
c24.set_attr(T=T_cons_bf)

# heat source cycle
c11.set_attr(T=T_hs_ff, p=1, fluid={"water": 1, wf: 0})
c11.set_attr(T=T_hs_ff, p=1, fluid={"water": 1})
c13.set_attr(T=T_hs_bf, p=1)

# evaporation to fully saturated gas
c1.set_attr(x=1, fluid={"water": 0, wf: 1})
c1.set_attr(x=1, fluid={wf: 1})
# degree of overheating after internal heat exchanger (evaporation side)
c2.set_attr(Td_bp=10)

Expand All @@ -113,6 +107,7 @@
nw.solve("design")
except ValueError as e:
print(e)
nw._reset_topology_reduction_specifications()
# %%[sec_4]
import CoolProp.CoolProp as CP

Expand Down Expand Up @@ -157,7 +152,6 @@
def generate_network_with_starting_values(wf):
# network
nw = Network(
fluids=["water", wf],
T_unit="C", p_unit="bar", h_unit="kJ / kg", m_unit="kg / s",
iterinfo=False
)
Expand Down Expand Up @@ -196,10 +190,7 @@ def generate_network_with_starting_values(wf):
c5 = Connection(int_heatex, "out1", valve, "in1", label="5")
c6 = Connection(valve, "out1", cycle_closer, "in1", label="6")

nw.add_conns(
c0, c1, c2, c3, c4,
c5, c6
)
nw.add_conns(c0, c1, c2, c3, c4, c5, c6)

# heat source
c11 = Connection(heatsource_feedflow, "out1", heatsource_pump, "in1", label="11")
Expand All @@ -223,15 +214,15 @@ def generate_network_with_starting_values(wf):
T_cons_ff = 90

# consumer cycle
c23.set_attr(T=T_cons_ff, p=10, fluid={"water": 1, wf: 0})
c23.set_attr(T=T_cons_ff, p=10, fluid={"water": 1})
c24.set_attr(T=T_cons_bf)

# heat source cycle
c11.set_attr(T=T_hs_ff, p=1, fluid={"water": 1, wf: 0})
c11.set_attr(T=T_hs_ff, p=1, fluid={"water": 1})
c13.set_attr(T=T_hs_bf, p=1)

# evaporation to fully saturated gas
c1.set_attr(x=1, fluid={"water": 0, wf: 1})
c1.set_attr(x=1, fluid={wf: 1})

# parametrization components
# isentropic efficiency
Expand Down
10 changes: 5 additions & 5 deletions tutorial/advanced/stepwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
# %%[sec_5]
from CoolProp.CoolProp import PropsSI as PSI
p_cond = PSI("P", "Q", 1, "T", 273.15 + 95, working_fluid) / 1e5
c0.set_attr(T=170, p=p_cond, fluid={"water": 0, working_fluid: 1})
c20.set_attr(T=60, p=2, fluid={"water": 1, working_fluid: 0})
c0.set_attr(T=170, p=p_cond, fluid={working_fluid: 1})
c20.set_attr(T=60, p=2, fluid={"water": 1})
c22.set_attr(T=90)

# key design paramter
Expand Down Expand Up @@ -96,7 +96,7 @@
c6.set_attr(h=h_sat)

# evaporator system hot side
c17.set_attr(T=15, fluid={"water": 1, working_fluid: 0})
c17.set_attr(T=15, fluid={"water": 1})
c19.set_attr(T=9, p=1.013)
# %%[sec_11]
nw.solve("design")
Expand Down Expand Up @@ -140,15 +140,15 @@
ic.set_attr(pr1=0.99, pr2=0.98)
hsp.set_attr(eta_s=0.75)
# %%[sec_15]
c0.set_attr(p=p_cond, fluid={"water": 0, working_fluid: 1})
c0.set_attr(p=p_cond, fluid={working_fluid: 1})

c6.set_attr(h=c5.h.val + 10)
c8.set_attr(h=c5.h.val + 10)

c7.set_attr(h=c5.h.val * 1.2)
c9.set_attr(h=c5.h.val * 1.2)

c11.set_attr(p=1.013, T=15, fluid={"water": 1, working_fluid: 0})
c11.set_attr(p=1.013, T=15, fluid={"water": 1})
c14.set_attr(T=30)
# %% [sec_16]
nw.solve("design")
Expand Down
5 changes: 2 additions & 3 deletions tutorial/basics/district_heating.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
)
from tespy.connections import Connection

fluid_list = ['INCOMP::Water']
nw = Network(fluids=fluid_list)
nw = Network()
nw.set_attr(T_unit='C', p_unit='bar', h_unit='kJ / kg')

# central heating plant
Expand Down Expand Up @@ -39,7 +38,7 @@
pipe_feed.set_attr(Q=-250, pr=0.98)
pipe_return.set_attr(Q=-200, pr=0.98)

c1.set_attr(T=90, p=10, fluid={'Water': 1})
c1.set_attr(T=90, p=10, fluid={'INCOMP::Water': 1})
c2.set_attr(p=13)
c4.set_attr(T=65)

Expand Down
1 change: 0 additions & 1 deletion tutorial/basics/heat_pump.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from tespy.networks import Network

# create a network object with R134a as fluid
fluid_list = ['R134a']
my_plant = Network()
# %%[sec_2]
# set the unitsystem for temperatures to °C and for pressure to bar
Expand Down
3 changes: 1 addition & 2 deletions tutorial/basics/rankine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
from tespy.networks import Network

# create a network object with R134a as fluid
fluid_list = ['water']
my_plant = Network(fluids=fluid_list)
my_plant = Network()
my_plant.set_attr(T_unit='C', p_unit='bar', h_unit='kJ / kg')
# %%[sec_2]
from tespy.components import (
Expand Down
21 changes: 10 additions & 11 deletions tutorial/heat_pump_exergy/NH3.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
from tespy.tools.characteristics import load_default_char as ldc
import numpy as np

# %% network
pamb = 1.013 # ambient pressure
Tamb = 2.8 # ambient temperature

# mean geothermal temperature (mean value of ground feed and return flow)
Tgeo = 9.5

nw = Network(fluids=['water', 'NH3'], T_unit='C', p_unit='bar',
h_unit='kJ / kg', m_unit='kg / s')
nw = Network(T_unit='C', p_unit='bar', h_unit='kJ / kg', m_unit='kg / s')

# %% components

Expand Down Expand Up @@ -90,15 +88,15 @@
# %% connection parametrization

# heat pump system
cc_cd.set_attr(fluid={'water': 0, 'NH3': 1})
cc_cd.set_attr(fluid={'NH3': 1})
ev_cp.set_attr(Td_bp=3)

# geothermal heat collector
gh_in_ghp.set_attr(T=Tgeo + 1.5, p=1.5, fluid={'water': 1, 'NH3': 0})
gh_in_ghp.set_attr(T=Tgeo + 1.5, p=1.5, fluid={'water': 1})
ev_gh_out.set_attr(T=Tgeo - 1.5, p=1.5)

# heating system
cd_hs_feed.set_attr(T=40, p=2, fluid={'water': 1, 'NH3': 0})
cd_hs_feed.set_attr(T=40, p=2, fluid={'water': 1})
hs_ret_hsp.set_attr(T=35, p=2)

# starting values
Expand All @@ -114,18 +112,19 @@
# power bus
char = CharLine(x=x, y=y)
power = Bus('power input')
power.add_comps({'comp': cp, 'char': char, 'base': 'bus'},
{'comp': ghp, 'char': char, 'base': 'bus'},
{'comp': hsp, 'char': char, 'base': 'bus'})
power.add_comps(
{'comp': cp, 'char': char, 'base': 'bus'},
{'comp': ghp, 'char': char, 'base': 'bus'},
{'comp': hsp, 'char': char, 'base': 'bus'}
)

# consumer heat bus
heat_cons = Bus('heating system')
heat_cons.add_comps({'comp': hs_ret, 'base': 'bus'}, {'comp': hs_feed})

# geothermal heat bus
heat_geo = Bus('geothermal heat')
heat_geo.add_comps({'comp': gh_in, 'base': 'bus'},
{'comp': gh_out})
heat_geo.add_comps({'comp': gh_in, 'base': 'bus'}, {'comp': gh_out})


nw.add_busses(power, heat_cons, heat_geo)
Expand Down
15 changes: 7 additions & 8 deletions tutorial/heat_pump_exergy/NH3_calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,19 @@
# power bus
char = CharLine(x=x, y=y)
power = Bus('power input')
power.add_comps({'comp': cp, 'char': char, 'base': 'bus'},
{'comp': ghp, 'char': char, 'base': 'bus'},
{'comp': hsp, 'char': char, 'base': 'bus'})
power.add_comps(
{'comp': cp, 'char': char, 'base': 'bus'},
{'comp': ghp, 'char': char, 'base': 'bus'},
{'comp': hsp, 'char': char, 'base': 'bus'}
)

# consumer heat bus
heat_cons = Bus('heating system')
heat_cons.add_comps({'comp': hs_ret, 'base': 'bus'}, {'comp': hs_feed})

# geothermal heat bus
heat_geo = Bus('geothermal heat')
heat_geo.add_comps({'comp': gh_in, 'base': 'bus'},
{'comp': gh_out})
heat_geo.add_comps({'comp': gh_in, 'base': 'bus'}, {'comp': gh_out})


nw.add_busses(power, heat_cons, heat_geo)
Expand Down Expand Up @@ -178,9 +179,7 @@

# %% exergy analysis

ean = ExergyAnalysis(network=nw,
E_F=[power, heat_geo],
E_P=[heat_cons])
ean = ExergyAnalysis(network=nw, E_F=[power, heat_geo], E_P=[heat_cons])
ean.analyse(pamb, Tamb)
print("\n##### EXERGY ANALYSIS #####\n")
ean.print_results()
Expand Down
20 changes: 10 additions & 10 deletions tutorial/heat_pump_exergy/R410A.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
# mean geothermal temperature (mean value of ground feed and return flow)
Tgeo = 9.5

nw = Network(fluids=['water', 'R410A'], T_unit='C', p_unit='bar',
h_unit='kJ / kg', m_unit='kg / s')
nw = Network(T_unit='C', p_unit='bar', h_unit='kJ / kg', m_unit='kg / s')

# %% components

Expand Down Expand Up @@ -90,15 +89,15 @@
# %% connection parametrization

# heat pump system
cc_cd.set_attr(fluid={'water': 0, 'R410A': 1})
cc_cd.set_attr(fluid={'R410A': 1})
ev_cp.set_attr(Td_bp=3)

# geothermal heat collector
gh_in_ghp.set_attr(T=Tgeo + 1.5, p=1.5, fluid={'water': 1, 'R410A': 0})
gh_in_ghp.set_attr(T=Tgeo + 1.5, p=1.5, fluid={'water': 1})
ev_gh_out.set_attr(T=Tgeo - 1.5, p=1.5)

# heating system
cd_hs_feed.set_attr(T=40, p=2, fluid={'water': 1, 'R410A': 0})
cd_hs_feed.set_attr(T=40, p=2, fluid={'water': 1})
hs_ret_hsp.set_attr(T=35, p=2)

# starting values
Expand All @@ -114,18 +113,19 @@
# power bus
char = CharLine(x=x, y=y)
power = Bus('power input')
power.add_comps({'comp': cp, 'char': char, 'base': 'bus'},
{'comp': ghp, 'char': char, 'base': 'bus'},
{'comp': hsp, 'char': char, 'base': 'bus'})
power.add_comps(
{'comp': cp, 'char': char, 'base': 'bus'},
{'comp': ghp, 'char': char, 'base': 'bus'},
{'comp': hsp, 'char': char, 'base': 'bus'}
)

# consumer heat bus
heat_cons = Bus('heating system')
heat_cons.add_comps({'comp': hs_ret, 'base': 'bus'}, {'comp': hs_feed})

# geothermal heat bus
heat_geo = Bus('geothermal heat')
heat_geo.add_comps({'comp': gh_in, 'base': 'bus'},
{'comp': gh_out})
heat_geo.add_comps({'comp': gh_in, 'base': 'bus'}, {'comp': gh_out})


nw.add_busses(power, heat_cons, heat_geo)
Expand Down
Loading

0 comments on commit 191d2e8

Please sign in to comment.