Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into props_norm
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/pandapipes/component_models/abstract_models/branch_w_internals_models.py
#	src/pandapipes/component_models/pipe_component.py
#	src/pandapipes/idx_branch.py
#	src/pandapipes/pf/build_system_matrix.py
#	src/pandapipes/pf/derivative_calculation.py
  • Loading branch information
SimonRubenDrauz committed Apr 8, 2024
2 parents 2968c25 + 2104e36 commit 155e9fe
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 44 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[metadata]
description-file = installation.rst
description_file = installation.rst
license_file = LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import numpy as np

from pandapipes.component_models.abstract_models.branch_models import BranchComponent
from pandapipes.component_models.component_toolbox import set_entry_check_repeat
from pandapipes.idx_branch import ACTIVE, FROM_NODE, FROM_NODE_T, TO_NODE, TO_NODE_T, TOUTINIT, ELEMENT_IDX
from pandapipes.idx_node import L, node_cols, TINIT as TINIT_NODE
from pandapipes.component_models.component_toolbox import set_entry_check_repeat, vinterp, \
p_correction_height_air
from pandapipes.idx_branch import ACTIVE, FROM_NODE, FROM_NODE_T, TO_NODE, TO_NODE_T, TOUTINIT, ELEMENT_IDX, TOUTINIT
from pandapipes.idx_node import (L, node_cols, TINIT as TINIT_NODE, HEIGHT, PINIT, PAMB,
ACTIVE as ACTIVE_ND)
from pandapipes.pf.pipeflow_setup import add_table_lookup, get_lookup, get_table_number

try:
Expand Down Expand Up @@ -157,6 +159,17 @@ def create_pit_node_entries(cls, net, node_pit):
junction_indices = get_lookup(net, "node", "index")[junction_table_name]
fj_nodes = junction_indices[from_junctions]
tj_nodes = junction_indices[to_junctions]

int_node_pit[:, HEIGHT] = vinterp(junction_pit[fj_nodes, HEIGHT],
junction_pit[tj_nodes, HEIGHT], int_node_number)
int_node_pit[:, PINIT] = vinterp(junction_pit[fj_nodes, PINIT],
junction_pit[tj_nodes, PINIT], int_node_number)
int_node_pit[:, TINIT_NODE] = vinterp(junction_pit[fj_nodes, TINIT_NODE],
junction_pit[tj_nodes, TINIT_NODE],
int_node_number)
int_node_pit[:, PAMB] = p_correction_height_air(int_node_pit[:, HEIGHT])
int_node_pit[:, ACTIVE_ND] = \
np.repeat(net[cls.table_name()][cls.active_identifier()].values, int_node_number)
return table_nr, int_node_number, int_node_pit, junction_pit, fj_nodes, tj_nodes

@classmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def create_pit_node_entries(cls, net, node_pit):
"""
circ_pump_tbl = net[cls.table_name()][net[cls.table_name()][cls.active_identifier()].values]

junction = net[cls.table_name()][cls.from_to_node_cols()[1]].values
junction = circ_pump_tbl[cls.from_to_node_cols()[1]].values

# TODO: there should be a warning, if any p_bar value is not given or any of the types does
# not contain "p", as this should not be allowed for this component
Expand Down
33 changes: 3 additions & 30 deletions src/pandapipes/component_models/pipe_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import matplotlib.pyplot as plt
import numpy as np
from numpy import dtype

from pandapipes.component_models.abstract_models import BranchWInternalsComponent
from pandapipes.component_models.component_toolbox import p_correction_height_air, \
vinterp, set_entry_check_repeat
from pandapipes.component_models.component_toolbox import set_entry_check_repeat
from pandapipes.component_models.junction_component import Junction
from pandapipes.constants import NORMAL_TEMPERATURE, NORMAL_PRESSURE
from pandapipes.idx_branch import FROM_NODE, TO_NODE, LENGTH, D, AREA, K, \
MDOTINIT, ALPHA, QEXT, TEXT, LOSS_COEFFICIENT as LC
from pandapipes.idx_node import PINIT, HEIGHT, TINIT as TINIT_NODE, PAMB, ACTIVE as ACTIVE_ND
from pandapipes.idx_node import PINIT, TINIT as TINIT_NODE, PAMB
from pandapipes.pf.pipeflow_setup import get_fluid, get_lookup
from pandapipes.pf.result_extraction import extract_branch_results_with_internals, \
extract_branch_results_without_internals
Expand Down Expand Up @@ -90,33 +90,6 @@ def create_node_lookups(cls, net, ft_lookups, table_lookup, idx_lookups, current

return end, current_table

@classmethod
def create_pit_node_entries(cls, net, node_pit):
"""
Function which creates pit node entries.
:param net: The pandapipes network
:type net: pandapipesNet
:param node_pit:
:type node_pit:
:return: No Output.
"""
table_nr, int_node_number, int_node_pit, junction_pit, fj_nodes, tj_nodes = \
super().create_pit_node_entries(net, node_pit)
if table_nr is None:
return
get_lookup(net, "node", "index")
int_node_pit[:, HEIGHT] = vinterp(junction_pit[fj_nodes, HEIGHT],
junction_pit[tj_nodes, HEIGHT], int_node_number)
int_node_pit[:, PINIT] = vinterp(junction_pit[fj_nodes, PINIT],
junction_pit[tj_nodes, PINIT], int_node_number)
int_node_pit[:, TINIT_NODE] = vinterp(junction_pit[fj_nodes, TINIT_NODE],
junction_pit[tj_nodes, TINIT_NODE],
int_node_number)
int_node_pit[:, PAMB] = p_correction_height_air(int_node_pit[:, HEIGHT])
int_node_pit[:, ACTIVE_ND] = \
np.repeat(net[cls.table_name()][cls.active_identifier()].values, int_node_number)

@classmethod
def create_pit_branch_entries(cls, net, branch_pit):
"""
Expand Down
2 changes: 1 addition & 1 deletion src/pandapipes/idx_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
LOSS_COEFFICIENT = 18
ALPHA = 19 # Slot for heat transfer coefficient
JAC_DERIV_DT = 20
JAC_DERIV_DT1 = 21
JAC_DERIV_DTOUT = 21
LOAD_VEC_BRANCHES_T = 22
TOUTINIT = 23 # Internal slot for outlet pipe temperature
JAC_DERIV_DT_NODE = 24 # Slot for the derivative fpr T for the nodes connected to branch
Expand Down
4 changes: 2 additions & 2 deletions src/pandapipes/pf/build_system_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import numpy as np
from pandapipes.idx_branch import FROM_NODE, TO_NODE, JAC_DERIV_DM, JAC_DERIV_DP, JAC_DERIV_DP1, \
JAC_DERIV_DM_NODE, LOAD_VEC_NODES, LOAD_VEC_BRANCHES, JAC_DERIV_DT, JAC_DERIV_DT1, \
JAC_DERIV_DM_NODE, LOAD_VEC_NODES, LOAD_VEC_BRANCHES, JAC_DERIV_DT, JAC_DERIV_DTOUT, \
JAC_DERIV_DT_NODE, LOAD_VEC_NODES_T, LOAD_VEC_BRANCHES_T, FROM_NODE_T, TO_NODE_T, BRANCH_TYPE
from pandapipes.idx_node import LOAD, TINIT
from pandapipes.idx_node import P, PC, NODE_TYPE, T, NODE_TYPE_T
Expand Down Expand Up @@ -81,7 +81,7 @@ def build_system_matrix(net, branch_pit, node_pit, heat_mode):
else:
system_data[:len_b] = branch_pit[:, JAC_DERIV_DT]
# pdF_dpi1
system_data[len_b:2 * len_b] = branch_pit[:, JAC_DERIV_DT1]
system_data[len_b:2 * len_b] = branch_pit[:, JAC_DERIV_DTOUT]
# jdF_dv_from_nodes
system_data[2 * len_b:len_fn1] = inc_flow_sum[tn_unique_der]
# jdF_dv_to_nodes
Expand Down
4 changes: 2 additions & 2 deletions src/pandapipes/pf/derivative_calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pandapipes.idx_branch import LENGTH, D, K, RE, LAMBDA, LOAD_VEC_BRANCHES, \
JAC_DERIV_DM, JAC_DERIV_DP, JAC_DERIV_DP1, LOAD_VEC_NODES, JAC_DERIV_DM_NODE, \
FROM_NODE, TO_NODE, FROM_NODE_T, TOUTINIT, TEXT, AREA, ALPHA, TL, QEXT, LOAD_VEC_NODES_T, \
LOAD_VEC_BRANCHES_T, JAC_DERIV_DT, JAC_DERIV_DT1, JAC_DERIV_DT_NODE, MDOTINIT, MDOTINIT_T
LOAD_VEC_BRANCHES_T, JAC_DERIV_DT, JAC_DERIV_DTOUT, JAC_DERIV_DT_NODE, MDOTINIT, MDOTINIT_T
from pandapipes.idx_node import TINIT as TINIT_NODE
from pandapipes.properties.fluids import get_fluid
from pandapipes.constants import NORMAL_TEMPERATURE
Expand Down Expand Up @@ -97,7 +97,7 @@ def calculate_derivatives_thermal(net, branch_pit, node_pit, options):
-(cp * m_init * (-t_init_i + t_init_i1 - tl) - alpha * (t_amb - t_m) * length + qext)

branch_pit[:, JAC_DERIV_DT] = - cp * m_init + alpha / 2 * length
branch_pit[:, JAC_DERIV_DT1] = cp * m_init + alpha / 2 * length
branch_pit[:, JAC_DERIV_DTOUT] = cp * m_init + alpha / 2 * length

branch_pit[:, JAC_DERIV_DT_NODE] = m_init
branch_pit[:, LOAD_VEC_NODES_T] = m_init * t_init_i1
Expand Down
78 changes: 78 additions & 0 deletions src/pandapipes/plotting/pipeflow_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import pandapipes.topology as top
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np


def pressure_profile_to_junction_geodata(net):
Expand Down Expand Up @@ -36,3 +38,79 @@ def pressure_profile_to_junction_geodata(net):
return pd.DataFrame({"x": dist.loc[junctions].values,
"y": net.res_junction.p_bar.loc[junctions].values},
index=junctions)


def plot_pressure_profile(net, ax=None, x0_junctions=None, plot_pressure_controller=True, xlabel="Distance from Slack in km",
ylabel="Pressure in bar", x0=0, pipe_color="tab:grey", pc_color="r",
junction_color="tab:blue", junction_size=3, pipes=None, **kwargs):
"""Plot the pressure profile depending on the distance from the x0_junction (slack).
Parameters
----------
net : pp.PandapowerNet
net including pipeflow results
ax : matplotlib.axes, optional
axis to plot to, by default None
x0_junctions : Any[list[int], pd.Index[int]], optional
list of junction indices which should be at position x0. If None, all in service slack junctions are considered,
by default None
plot_pressure_controller : bool, optional
Whether vertical lines should be plotted to display the pressure drop of the pressure controller,
by default True
xlabel : str, optional
xlable of the figure, by default "Distance from Slack in km"
ylabel : str, optional
ylable of the figure, by default "Pressure in bar"
x0 : int, optional
x0_junction position at the xaxis, by default 0
pipe_color : str, optional
color used to plot the pipes, by default "tab:grey"
pc_color : str, optional
color used to plot the pressure controller, by default "r"
junction_color : [str, dict[int, str]], optional
colors used to plot the junctions. Can be passed as string (to give all junctions the same color),
or as dict, by default "tab:blue"
junction_size : int, optional
size of junction representations, by default 3
pipes : Any[list[int], pd.Index[int]], optional
list of pipe indices which should be plottet. If None, all pipes are plotted, by default None
Returns
-------
matplotlib.axes
axis of the plot
"""
if ax is None:
plt.figure(facecolor="white", dpi=120)
ax = plt.gca()
if not net.converged:
raise ValueError("no results in this pandapipes network")
if pipes is None:
pipes = net.pipe.index
if x0_junctions is None:
x0_junctions = net.ext_grid[net.ext_grid.in_service].junction.values.tolist()

d = top.calc_distance_to_junctions(net, x0_junctions)
pipe_table = net.pipe[net.pipe.in_service & net.pipe.index.isin(pipes)]

x = np.array([d.loc[pipe_table.from_junction].values, d.loc[pipe_table.to_junction].values]) + x0
y = np.array([net.res_junction.p_bar.loc[pipe_table.from_junction].values, net.res_junction.p_bar.loc[pipe_table.to_junction].values])
linewidth = kwargs.get("linewidth", 1)
ax.plot(x, y, linewidth=linewidth, color=pipe_color, **kwargs)

x = d.values + x0
y = net.res_junction.p_bar.loc[d.index]
ax.plot(x, y, 'o', color=junction_color, ms=junction_size)

if plot_pressure_controller and ("press_control" in net.keys()):
pressure_controller = net.press_control.query('in_service')
x = np.array([d.loc[pressure_controller.from_junction].values, d.loc[pressure_controller.to_junction].values]) + x0
y = np.array([net.res_junction.p_bar.loc[pressure_controller.from_junction].values, net.res_junction.p_bar.loc[pressure_controller.to_junction].values])
ax.plot(x, y, color=pc_color, **{k: v for k, v in kwargs.items() if not k == "color"})

if xlabel is not None:
ax.set_xlabel(xlabel)
if ylabel is not None:
ax.set_ylabel(ylabel)
return ax
34 changes: 34 additions & 0 deletions src/pandapipes/test/topology/test_graph_searches.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import pandapipes.networks as nw
import pandapipes.topology as top
import networkx as nx
import pytest


def test_connected_components():
Expand All @@ -18,3 +20,35 @@ def test_connected_components():
assert len(list(top.connected_components(mg))) == 6
mg = top.create_nxgraph(net, include_pipes=False, include_valves=False)
assert len(list(top.connected_components(mg))) == 8


def test_calc_distance_to_junctions():
net = nw.gas_versatility()
egs = net.ext_grid.junction
assert top.calc_distance_to_junctions(net, egs, respect_status_valves=True).sum() == 30.93
assert top.calc_distance_to_junctions(net, egs, respect_status_valves=False).sum() == 26.96


def test_unsupplied_buses_with_in_service():
net = nw.gas_versatility()
assert top.unsupplied_junctions(net) == set()
net.pipe.loc[7, "in_service"] = False
assert top.unsupplied_junctions(net) == {8}


def test_elements_on_path():
net = nw.gas_versatility()
for multi in [True, False]:
mg = top.create_nxgraph(net, multi=multi)
path = nx.shortest_path(mg, 0, 6)
assert top.elements_on_path(mg, path, "pipe") == [0, 9]
assert top.elements_on_path(mg, path) == [0, 9]
assert top.elements_on_path(mg, path, "valve") == []
assert top.elements_on_path(mg, path, "pump") == [0]
with pytest.raises(ValueError) as exception_info:
top.elements_on_path(mg, path, element="source")
assert str(exception_info.value) == "Invalid element type source"


if __name__ == "__main__":
pytest.main([__file__])
1 change: 1 addition & 0 deletions src/pandapipes/topology/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

from pandapipes.topology.create_graph import *
from pandapipes.topology.topology_toolbox import *
from pandapipes.topology.graph_searches import *
from pandapower.topology.graph_searches import connected_component, connected_components
Loading

0 comments on commit 155e9fe

Please sign in to comment.