Skip to content

Commit

Permalink
add print_pf_summary; add first draft for diagnostic function
Browse files Browse the repository at this point in the history
  • Loading branch information
jkisse committed May 22, 2023
1 parent cf96a09 commit 69fcb7f
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 0 deletions.
145 changes: 145 additions & 0 deletions pandapipes/diagnostic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Copyright (c) 2020-2023 by Fraunhofer Institute for Energy Economics
# and Energy System Technology (IEE), Kassel, and University of Kassel. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

import pandapipes as pp
import numpy as np

Check warning on line 6 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L5-L6

Added lines #L5 - L6 were not covered by tests

from pandapipes import PipeflowNotConverged

Check warning on line 8 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L8

Added line #L8 was not covered by tests

try:
import pandaplan.core.pplog as logging
except ImportError:
import logging

Check warning on line 13 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L10-L13

Added lines #L10 - L13 were not covered by tests

logger = logging.getLogger(__name__)

Check warning on line 15 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L15

Added line #L15 was not covered by tests


def check_net(net, low_length_limit_km=0.01, check_scaling_factor=1e-5):

Check warning on line 18 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L18

Added line #L18 was not covered by tests
"""
Run some diagnostic checks on the net to identify potential flaws.
"""
net = net.deepcopy() # do not modify the direct input
try:
pp.pipeflow(net)
if net.converged:
logger.info("The initial, unmodified pipeflow converges.")

Check warning on line 26 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L22-L26

Added lines #L22 - L26 were not covered by tests
else:
logger.warning("The initial, unmodified pipeflow does NOT converge.")
except Exception as e:
logger.info(f"The initial, unmodified pipeflow does NOT converge.\n"

Check warning on line 30 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L28-L30

Added lines #L28 - L30 were not covered by tests
f"\t\tThis exception is raised:\n\t\t{e}")

# check ext_grid
if net.fluid.is_gas & (not hasattr(net, "ext_grid") | net.ext_grid.empty):
logger.warning("The net does not have an external grid! "

Check warning on line 35 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L34-L35

Added lines #L34 - L35 were not covered by tests
"An external grid is required for gas networks.")

# check zero / low length
zl = net.pipe.loc[net.pipe.length_km == 0]
ll = net.pipe.loc[net.pipe.length_km <= low_length_limit_km]
if not zl.empty:
logger.warning(f"{len(zl.index)} pipes have a length of 0.0 km. (IDs: {zl.index})")
if not ll.empty:
logger.warning(f"{len(ll.index)} pipes have a length below"

Check warning on line 44 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L39-L44

Added lines #L39 - L44 were not covered by tests
f" {low_length_limit_km} km. "
f"This could lead to convergence issues. The lowest length in the net is "
f"{ll.length_km.min()} km.\n"
f"(IDs of pipelines with low length: {ll.index})")

net2 = net.deepcopy()
net2.pipe.loc[net2.pipe.length_km < low_length_limit_km].length_km = low_length_limit_km
try:
pp.pipeflow(net2)
if net2.converged:
logger.info(f"If all short pipelines (< {low_length_limit_km} km) were set to "

Check warning on line 55 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L50-L55

Added lines #L50 - L55 were not covered by tests
f"{low_length_limit_km} km, the pipeflow would converge.")
else:
logger.warning(f"If all short pipelines (< {low_length_limit_km} km) were set to "

Check warning on line 58 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L58

Added line #L58 was not covered by tests
f"{low_length_limit_km} km, the pipeflow would still NOT converge.")
except Exception as e:
logger.info(f"Pipeflow does not converge, even if all short pipelines (< 10 m) were set "

Check warning on line 61 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L60-L61

Added lines #L60 - L61 were not covered by tests
f"to {low_length_limit_km} km. \n"
f"\t\tThe error message is: {e}")

# check iterations
iter = 200
try:
pp.pipeflow(net, iter=iter)
logger.info(f"The pipeflow converges after {net._internal_results['iterations']:d} "

Check warning on line 69 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L66-L69

Added lines #L66 - L69 were not covered by tests
f"iterations.")
except PipeflowNotConverged:
logger.info(f"After {iter:d} iterations the pipeflow did NOT converge.")

Check warning on line 72 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L71-L72

Added lines #L71 - L72 were not covered by tests

# check with little sink and source scaling
logger.info("Testing with scaled-down sinks and sources.")
net3 = net.deepcopy()
if hasattr(net, "sink"):
net3.sink.scaling *= check_scaling_factor
if hasattr(net, "source"):
net3.source.scaling *= check_scaling_factor
try:
pp.pipeflow(net3)
if net3.converged:
logger.info(f"If sinks and sources were scaled with a factor of to "

Check warning on line 84 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L75-L84

Added lines #L75 - L84 were not covered by tests
f"{check_scaling_factor}, the pipeflow would converge.")
else:
logger.warning(f"If sinks and sources were scaled with a factor of to "

Check warning on line 87 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L87

Added line #L87 was not covered by tests
f"{check_scaling_factor}, the pipeflow would still NOT converge.")
except Exception as e:
logger.info(f"Pipeflow does not converge with sinks/sources scaled by"

Check warning on line 90 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L89-L90

Added lines #L89 - L90 were not covered by tests
f" {check_scaling_factor}.\n"
f"\t\tThe error message is: {e}")

# check k
if any(net.pipe.k_mm > 0.5):
logger.warning(f"Some pipes have a friction factor k_mm > 0.5 (extremely rough). The "

Check warning on line 96 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L95-L96

Added lines #L95 - L96 were not covered by tests
f"highest value in the net is {net.pipe.k_mm.max()}. Up to "
f"0.2 mm is a common value for old steel pipes."
f"\nRough pipes: {net.pipe.loc[net.pipe.k_mm > 0.5]}.")
net4 = net.deepcopy()
net4.pipe.k_mm = 1e-5
try:
pp.pipeflow(net4)
if net4.converged:
logger.info(f"If the friction factor would be reduced to 1e-5 for all pipes, "

Check warning on line 105 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L100-L105

Added lines #L100 - L105 were not covered by tests
f"the pipeflow would converge.")
else:
logger.warning(f"If the friction factor would be reduced to 1e-5 for all pipes, "

Check warning on line 108 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L108

Added line #L108 was not covered by tests
f"the pipeflow would still NOT converge.")
except Exception as e:
logger.info(f"Pipeflow does not converge with k_mm = 1-e5 for all pipes.\n"

Check warning on line 111 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L110-L111

Added lines #L110 - L111 were not covered by tests
f"\t\tThe error message is: {e}")

# check sink and source junctions:
node_component = ["sink", "source", "ext_grid"]
for nc in node_component:
if hasattr(net, nc):
missing = np.setdiff1d(net[nc].junction, net.junction.index)
if len(missing):
logger.warning(f"Some {nc}s are connected to non-existing junctions!"

Check warning on line 120 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L115-L120

Added lines #L115 - L120 were not covered by tests
f"\n{nc}s:{net[nc].loc[net[nc].junction.isin(missing)]}"
f"\nmissing junctions:{missing}")

# check from and to junctions
branch_component = ["pipe", "valve", "compressor", "pump", "heat_exchanger", "circulation_pump"]
for bc in branch_component:
if hasattr(net, bc):
missing_f = np.setdiff1d(net[bc].from_junction, net.junction.index)
missing_t = np.setdiff1d(net[bc].to_junction, net.junction.index)
if len(missing_t) | len(missing_t):
logger.warning(f"Some {bc}s are connected to non-existing junctions!")
logger.warning(f"missing 'from' junctions:{missing_f}")
logger.warning(f"missing 'to' junctions:{missing_t}")

Check warning on line 133 in pandapipes/diagnostic.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/diagnostic.py#L125-L133

Added lines #L125 - L133 were not covered by tests


if __name__ == '__main__':
import pandapipes.networks
net = pandapipes.networks.schutterwald()
net.ext_grid.p_bar = 0.08
net.sink.loc[1505, "mdot_kg_per_s"] = 1000
try:
pandapipes.pipeflow(net)
except Exception as e:
print(f"pipeflow raised: \n {e}")
check_net(net)
37 changes: 37 additions & 0 deletions pandapipes/toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,3 +646,40 @@ def get_internal_tables_pandas(net):
branch_table["TABLE_IDX"].replace(branch_table_lookup["n2t"], inplace=True)

return node_table, branch_table


def print_pf_summary(net):
"""Print some basic results of the pipeflow.
Min./max. pressure, junctions with NaN results, max. v, sum of sinks / sources.
"""
if not net.converged:
try:
pandapipes.pipeflow(net)
except Exception as e:
return logger.Error(f"Could not print pipeflow summary because the pipeflow "

Check warning on line 660 in pandapipes/toolbox.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/toolbox.py#L656-L660

Added lines #L656 - L660 were not covered by tests
f"calculation was not successful (Exception: {e})")
if not net.converged:
return logger.Error(f"Could not print pipeflow summary because the pipeflow "

Check warning on line 663 in pandapipes/toolbox.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/toolbox.py#L662-L663

Added lines #L662 - L663 were not covered by tests
f"calculation did not converge.")
else:
if any(net.res_junction.loc[net.junction.in_service].p_bar.isna()):
print(f"For {sum(net.res_junction.loc[net.junction.in_service].p_bar.isna())} "

Check warning on line 667 in pandapipes/toolbox.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/toolbox.py#L666-L667

Added lines #L666 - L667 were not covered by tests
f"junctions, no pressure could be calculated (NaN)!")
print(f"The minimum pressure is {net.res_junction.p_bar.min():.2f} bar.")
print(f"The maximum pressure is {net.res_junction.p_bar.max():.2f} bar.")
print(f"The highest velocity is {net.res_pipe.v_mean_m_per_s.abs().max():.2f} m/s.")
if hasattr(net, "source") & (~net.source.empty):
total_source_mdot = net.res_source.mdot_kg_per_s.sum()
print(f"The total gas infeed from sources is {total_source_mdot:.2f} kg/s "

Check warning on line 674 in pandapipes/toolbox.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/toolbox.py#L669-L674

Added lines #L669 - L674 were not covered by tests
f"(i.e. {total_source_mdot * float(net.fluid.get_property('hhv'))*3.6:.2f} "
f"MW_th (HHV).)")
else:
print("There are no sources connected to the net.")
if hasattr(net, "sink") & (~net.sink.empty):
total_sink_mdot = net.res_sink.mdot_kg_per_s.sum()
print(f"The total gas demand from sinks is {total_sink_mdot:.2f} kg/s "

Check warning on line 681 in pandapipes/toolbox.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/toolbox.py#L678-L681

Added lines #L678 - L681 were not covered by tests
f"(i.e. {total_sink_mdot * float(net.fluid.get_property('hhv'))*3.6:.2f} MW_th "
f"(HHV).)")
else:
print("There are no sinks connected to the net.")

Check warning on line 685 in pandapipes/toolbox.py

View check run for this annotation

Codecov / codecov/patch

pandapipes/toolbox.py#L685

Added line #L685 was not covered by tests

0 comments on commit 69fcb7f

Please sign in to comment.