Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add split_pipe function #521

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/toolbox.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ Topology Modification

.. autofunction:: pandapipes.drop_pipes

.. autofunction:: pandapipes.split_pipe

====================================
pandapower toolbox functions
====================================
Expand Down
10 changes: 10 additions & 0 deletions pandapipes/test/test_toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,16 @@ def test_pit_extraction():
))


def test_split_pipe():
net = nw.simple_gas_networks.gas_tcross1()
net_org = net.deepcopy()
split_id = 0
p = 0.3
new_pipe, new_junc = pandapipes.split_pipe(net, split_id, p)
assert net.pipe.at[new_pipe, "length_km"] == net_org.pipe.at[split_id, "length_km"] * (1 - p)
assert net.pipe.at[split_id, "length_km"] == net_org.pipe.at[split_id, "length_km"] * p


def runpp_with_mark(net, **kwargs):
pandapower.runpp(net, **kwargs)
net['mark'] = "runpp"
Expand Down
62 changes: 62 additions & 0 deletions pandapipes/toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,68 @@ def check_pressure_controllability(net, to_junction, controlled_junction):
# logger.info("dropped %d %s elements with %d switches" % (len(trafos), table, num_switches))


def split_pipe(net, pipe_idx, position_along_pipe=0.5, new_pipe_prefix="split"):
"""Splits a pipe into two pipes by inserting a new junction.

The new junction is located in a relative distance of `position_along_pipe` to the
from_junction. The length of the old (long) pipe is modified accordingly.
The new pipe segment is inserted between the newly created junction and the
to_junction. The initial pressure, height and coordinates of the new junction are interpolated.

:param net: pandapipes net that contains the pipe that will be split
:type net: pandapipesNet
:param pipe_idx: index of the pipe that will be split
:type pipe_idx: int
:param position_along_pipe: (>=0, <= 1), position where the pipe will be split, seen from
the from_junction (0 = at the from_junction, 1 = at the to_junction)
:type position_along_pipe: float
:param new_pipe_prefix: prefix for the new junction and new pipe names
:type new_pipe_prefix: str, default "split"
:return: id of newly created pipe and id of new junction
:rtype: int, int
"""
if new_pipe_prefix is not None and len(new_pipe_prefix):
new_pipe_prefix = new_pipe_prefix + "_"

# 1. insert new junction and interpolate values
old_fj = int(net.pipe.loc[pipe_idx, "from_junction"])
old_tj = int(net.pipe.loc[pipe_idx, "to_junction"])
p = position_along_pipe
assert (position_along_pipe >= 0) & (position_along_pipe <= 1), \
"Position along pipe is relative. It has to be >= 0 and <= 1."

param = ["pn_bar", "tfluid_k", "height_m"]
new_junction_parameters = (net.junction.loc[old_fj, param] * (1 - p)
+ net.junction.loc[old_tj, param] * p).to_dict()
new_junction_parameters["name"] = new_pipe_prefix + str(pipe_idx)

if not any(net.junction_geodata.loc[[old_fj, old_tj]].isna()): # if all coordinates exist
# interpolate coordinates
new_junction_parameters["geodata"] = ((net.junction_geodata.loc[old_fj, "x"] * (1-p) +
net.junction_geodata.loc[old_tj, "x"] * p),
(net.junction_geodata.loc[old_fj, "y"] * (1 - p) +
net.junction_geodata.loc[old_tj, "y"] * p))

nj = pandapipes.create_junction(net, **new_junction_parameters)

# 2. add new pipe between old junction and new junction
pipe_parameters = net.pipe.loc[pipe_idx].to_dict()
pipe_parameters = {k: pipe_parameters[k] for k in pipe_parameters.keys()
if k not in ["name", "from_junction", "to_junction", "std_type"]}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont quite get this line, but the rest looks good

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because we want to keep all attributes from the initial pipe (also individual attributes that the users added by kwargs) but not name, from_junction and to_junction because these values will be different for the new pipe. And we don't want std_type in the parameters because it raises a UserWarning when being passed to create_pipe_from_parameters

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it to a .pop formulation which is hopefully a bit clearer to understand

pipe_parameters["length_km"] *= (1 - p)

new_pipe_idx = pandapipes.create_pipe_from_parameters(net, from_junction=nj,
to_junction=old_tj,
name=new_pipe_prefix + str(pipe_idx),
**pipe_parameters)

# 3. reroute old pipe to new junction and adjust parameters
net.pipe.loc[pipe_idx, "to_junction"] = nj
net.pipe.loc[pipe_idx, "length_km"] *= p

return new_pipe_idx, nj


node_pit_indices = {
TABLE_IDX_NODE: "TABLE_IDX",
ELEMENT_IDX_NODE: "ELEMENT_IDX",
Expand Down