Skip to content

Commit

Permalink
- started cleaning up the import of low level functions
Browse files Browse the repository at this point in the history
- shifted low level functions from the component or pf files to the new folder "utils"
- shifted the file pf.result_extraction to utils
  • Loading branch information
Moritz Franz committed Nov 11, 2024
1 parent bc9eea5 commit decf5ed
Show file tree
Hide file tree
Showing 38 changed files with 692 additions and 685 deletions.
2 changes: 2 additions & 0 deletions src/pandapipes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
pd.options.mode.chained_assignment = None # default='warn'
pp_dir = os.path.dirname(os.path.realpath(__file__))


from pandapipes.utils.internals import *
from pandapipes.properties.fluids import *
from pandapipes.component_models.component_registry import *
from pandapipes.create import *
Expand Down
2 changes: 1 addition & 1 deletion src/pandapipes/component_models/_base_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

from abc import ABC, abstractmethod
from pandapipes.component_models.component_toolbox import init_results_element
from pandapipes.utils.internals import init_results_element


class Component(ABC):
Expand Down
4 changes: 1 addition & 3 deletions src/pandapipes/component_models/_branch_element_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
from numpy import pi

from pandapipes.component_models._branch_models import BranchComponent

from pandapipes.idx_branch import FROM_NODE, TO_NODE, TOUTINIT, ELEMENT_IDX, ACTIVE, LENGTH, K, TEXT, ALPHA, D, AREA
from pandapipes.idx_node import TINIT as TINIT_NODE

from pandapipes.pf.pipeflow_setup import add_table_lookup
from pandapipes.utils.internals import add_table_lookup


class BranchElementComponent(BranchComponent):
Expand Down
2 changes: 1 addition & 1 deletion src/pandapipes/component_models/_branch_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pandapipes.component_models._base_component import Component
from pandapipes.component_models.junction_component import Junction
from pandapipes.idx_branch import MDOTINIT, branch_cols, TEXT
from pandapipes.pf.pipeflow_setup import get_table_number, get_lookup, get_net_option
from pandapipes.utils.internals import get_net_option, get_table_number, get_lookup


class BranchComponent(Component):
Expand Down
4 changes: 2 additions & 2 deletions src/pandapipes/component_models/_circulation_pump.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from pandapipes.component_models.component_toolbox import set_fixed_node_entries, standard_branch_wo_internals_result_lookup
from pandapipes.idx_branch import D, AREA, BRANCH_TYPE, CIRC, LOAD_VEC_BRANCHES_T, TO_NODE
from pandapipes.idx_node import MDOTSLACKINIT, VAR_MASS_SLACK, JAC_DERIV_MSL
from pandapipes.pf.pipeflow_setup import get_fluid
from pandapipes.pf.result_extraction import extract_branch_results_without_internals
from pandapipes.properties.fluids import get_fluid
from pandapipes.utils.result_extraction import extract_branch_results_without_internals


class CirculationPump(BranchElementComponent):
Expand Down
11 changes: 5 additions & 6 deletions src/pandapipes/component_models/_const_flow_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
# 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 numpy as np
from numpy import dtype
from numpy import dtype, nan_to_num, isin

from pandapipes.component_models._node_element_models import NodeElementComponent
from pandapipes.idx_node import LOAD, ELEMENT_IDX
from pandapipes.pf.internals_toolbox import _sum_by_group
from pandapipes.pf.pipeflow_setup import get_lookup, get_net_option
from pandapipes.utils.internals import get_net_option, get_lookup, _sum_by_group


class ConstFlow(NodeElementComponent):
Expand All @@ -23,7 +22,7 @@ def create_pit_node_entries(self, net, node_pit):
"""
loads = net[self.table_name]
helper = loads.in_service.values * loads.scaling.values * self.sign
mf = np.nan_to_num(loads.mdot_kg_per_s.values)
mf = nan_to_num(loads.mdot_kg_per_s.values)
mass_flow_loads = mf * helper
juncts, loads_sum = _sum_by_group(get_net_option(net, "use_numba"), loads.junction.values,
mass_flow_loads)
Expand Down Expand Up @@ -54,7 +53,7 @@ def extract_results(self, net, options, branch_results, mode):
fj, tj = get_lookup(net, "node", "from_to")[self.connected_node_type.table_name]
junct_pit = net["_pit"]["node"][fj:tj, :]
nodes_connected_hyd = get_lookup(net, "node", "active_hydraulics")[fj:tj]
is_juncts = np.isin(loads.junction.values, junct_pit[nodes_connected_hyd, ELEMENT_IDX])
is_juncts = isin(loads.junction.values, junct_pit[nodes_connected_hyd, ELEMENT_IDX])

is_calc = is_loads & is_juncts
res_table["mdot_kg_per_s"].values[is_calc] = loads.mdot_kg_per_s.values[is_calc] \
Expand Down
92 changes: 2 additions & 90 deletions src/pandapipes/component_models/component_toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,100 +3,12 @@
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

import numpy as np
import pandas as pd

from pandapipes import get_fluid
from pandapipes.constants import NORMAL_PRESSURE, TEMP_GRADIENT_KPM, AVG_TEMPERATURE_K, \
HEIGHT_EXPONENT
from pandapipes.properties.fluids import get_fluid
from pandapipes.idx_branch import LOAD_VEC_NODES_FROM, LOAD_VEC_NODES_TO, FROM_NODE, TO_NODE
from pandapipes.idx_node import (EXT_GRID_OCCURENCE, EXT_GRID_OCCURENCE_T,
PINIT, NODE_TYPE, P, TINIT, NODE_TYPE_T, T, LOAD)
from pandapipes.pf.pipeflow_setup import get_net_option, get_lookup
from pandapipes.pf.internals_toolbox import _sum_by_group


def p_correction_height_air(height):
"""
:param height:
:type height:
:return:
:rtype:
"""
return NORMAL_PRESSURE * np.power(1 - height * TEMP_GRADIENT_KPM / AVG_TEMPERATURE_K,
HEIGHT_EXPONENT)


def vinterp(min_vals, max_vals, lengths):
"""
:param min_vals:
:type min_vals:
:param max_vals:
:type max_vals:
:param lengths: lengths for each range (same length as starts)
:type lengths: numpy.array
:return:
:rtype:
"""
intervals = (max_vals - min_vals) / (lengths + 1)
steps = np.repeat(intervals, lengths)
counter = np.arange(lengths.sum()) - np.repeat(lengths.cumsum() - lengths, lengths) + 1
return np.repeat(min_vals, lengths) + steps * counter


def vrange(starts, lengths):
"""
Create concatenated ranges of integers for multiple start/length
:param starts: starts for each range
:type starts: numpy.array
:param lengths: lengths for each range (same length as starts)
:type lengths: numpy.array
:return: cat_range - concatenated ranges
:rtype: numpy.array
:Example:
>>> starts = np.array([1, 3, 4, 6])
>>> lengths = np.array([0, 2, 3, 0])
>>> print vrange(starts, lengths)
"""
# Repeat start position index length times and concatenate
starting_array = np.repeat(starts, lengths)
# Create group counter that resets for each start/length
length_ranges = np.arange(lengths.sum()) - np.repeat(lengths.cumsum() - lengths, lengths)
# Add group counter to group specific starts
return starting_array + length_ranges


def init_results_element(net, element, output, all_float):
"""
:param net: The pandapipes network
:type net: pandapipesNet
:param element:
:type element:
:param output:
:type output:
:param all_float:
:type all_float:
:return: No Output.
"""
res_element = "res_" + element
if all_float:
net[res_element] = pd.DataFrame(np.nan, columns=output, index=net[element].index,
dtype=np.float64)
else:
net[res_element] = pd.DataFrame(np.zeros(0, dtype=output), index=[])
net[res_element] = pd.DataFrame(np.nan, index=net[element].index,
columns=net[res_element].columns)


def set_entry_check_repeat(pit, column, entry, repeat_number, repeated=True):
if repeated:
pit[:, column] = np.repeat(entry, repeat_number)
else:
pit[:, column] = entry
from pandapipes.utils.internals import get_net_option, get_lookup, _sum_by_group


def set_fixed_node_entries(net, node_pit, junctions, types, values, node_comp, mode):
Expand Down
2 changes: 1 addition & 1 deletion src/pandapipes/component_models/ext_grid_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

from pandapipes.component_models._node_element_models import NodeElementComponent
from pandapipes.component_models.component_toolbox import set_fixed_node_entries
from pandapipes.pf.pipeflow_setup import get_lookup
from pandapipes.idx_node import MDOTSLACKINIT, VAR_MASS_SLACK, JAC_DERIV_MSL
from pandapipes.utils.internals import get_lookup


class ExtGrid(NodeElementComponent):
Expand Down
10 changes: 5 additions & 5 deletions src/pandapipes/component_models/flow_control_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

from numpy import dtype, bool_, float64, zeros

from pandapipes.component_models import BranchElementComponent
from pandapipes.properties import get_fluid
from pandapipes.component_models.component_toolbox import \
standard_branch_wo_internals_result_lookup, get_component_array
from pandapipes.component_models._branch_element_models import BranchElementComponent
from pandapipes.component_models.component_toolbox import standard_branch_wo_internals_result_lookup, \
get_component_array
from pandapipes.idx_branch import JAC_DERIV_DP, JAC_DERIV_DP1, JAC_DERIV_DM, MDOTINIT, LOAD_VEC_BRANCHES
from pandapipes.pf.result_extraction import extract_branch_results_without_internals
from pandapipes.properties.fluids import get_fluid
from pandapipes.utils.result_extraction import extract_branch_results_without_internals


class FlowControlComponent(BranchElementComponent):
Expand Down
12 changes: 7 additions & 5 deletions src/pandapipes/component_models/heat_consumer_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@

from numpy import dtype, isnan, zeros, float64, any as any_

from pandapipes.component_models import (get_fluid, BranchElementComponent, get_component_array,
standard_branch_wo_internals_result_lookup)
from pandapipes.component_models._branch_element_models import BranchElementComponent
from pandapipes.component_models.component_toolbox import get_component_array, \
standard_branch_wo_internals_result_lookup
from pandapipes.idx_branch import (MDOTINIT, QEXT, JAC_DERIV_DP1, JAC_DERIV_DM,
JAC_DERIV_DP, LOAD_VEC_BRANCHES, TOUTINIT, JAC_DERIV_DT,
JAC_DERIV_DTOUT, LOAD_VEC_BRANCHES_T, ACTIVE)
from pandapipes.idx_node import TINIT
from pandapipes.pf.internals_toolbox import get_from_nodes_corrected
from pandapipes.pf.pipeflow_setup import get_lookup
from pandapipes.pf.result_extraction import extract_branch_results_without_internals
from pandapipes.properties.fluids import get_fluid
from pandapipes.properties.properties_toolbox import get_branch_cp
from pandapipes.utils.internals import get_lookup, get_from_nodes_corrected
from pandapipes.utils.result_extraction import extract_branch_results_without_internals


try:
import pandaplan.core.pplog as logging
Expand Down
6 changes: 3 additions & 3 deletions src/pandapipes/component_models/heat_exchanger_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

from numpy import dtype

from pandapipes.component_models import standard_branch_wo_internals_result_lookup
from pandapipes.component_models._branch_element_models import BranchElementComponent
from pandapipes.component_models.component_toolbox import standard_branch_wo_internals_result_lookup
from pandapipes.idx_branch import QEXT, LOSS_COEFFICIENT as LC
from pandapipes.pf.pipeflow_setup import get_fluid
from pandapipes.pf.result_extraction import extract_branch_results_without_internals
from pandapipes.properties.fluids import get_fluid
from pandapipes.utils.result_extraction import extract_branch_results_without_internals


class HeatExchanger(BranchElementComponent):
Expand Down
11 changes: 4 additions & 7 deletions src/pandapipes/component_models/junction_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

from warnings import warn

import numpy as np
from numpy import dtype
from numpy import dtype, array, int32, any as any_

from pandapipes.component_models._node_models import NodeComponent
from pandapipes.component_models.component_toolbox import p_correction_height_air
from pandapipes.idx_node import L, ELEMENT_IDX, PINIT, node_cols, HEIGHT, TINIT, PAMB, \
ACTIVE as ACTIVE_ND
from pandapipes.pf.pipeflow_setup import add_table_lookup, get_table_number, \
get_lookup
from pandapipes.idx_node import L, ELEMENT_IDX, PINIT, node_cols, HEIGHT, TINIT, PAMB, ACTIVE as ACTIVE_ND
from pandapipes.utils.internals import add_table_lookup, get_table_number, get_lookup, p_correction_height_air


class Junction(NodeComponent):
Expand Down
2 changes: 1 addition & 1 deletion src/pandapipes/component_models/mass_storage_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# 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.

from pandapipes.component_models import ConstFlow
from numpy import dtype

from pandapipes.component_models._const_flow_models import ConstFlow

class MassStorage(ConstFlow):
"""
Expand Down
Loading

0 comments on commit decf5ed

Please sign in to comment.