1
# Copyright (c) 2020 by Fraunhofer Institute for Energy Economics
2
# and Energy System Technology (IEE), Kassel. All rights reserved.
3
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4

5 1
import numpy as np
6 1
from numpy import dtype
7 1
from pandapipes.component_models.abstract_models import BranchWZeroLengthComponent
8 1
from pandapipes.constants import NORMAL_TEMPERATURE, NORMAL_PRESSURE
9 1
from pandapipes.idx_branch import FROM_NODE, TO_NODE, D, TINIT, AREA, VINIT, \
10
    LOAD_VEC_NODES, LOSS_COEFFICIENT as LC, PL, TL, RE, LAMBDA
11 1
from pandapipes.idx_node import ELEMENT_IDX, PINIT, TINIT as TINIT_NODE, PAMB
12 1
from pandapipes.internals_toolbox import _sum_by_group
13 1
from pandapipes.pipeflow_setup import get_fluid, get_lookup, get_net_option
14

15

16 1
class Valve(BranchWZeroLengthComponent):
17
    """
18

19
    """
20

21 1
    @classmethod
22
    def table_name(cls):
23 1
        return "valve"
24

25 1
    @classmethod
26
    def active_identifier(cls):
27 1
        return "opened"
28

29 1
    @classmethod
30
    def create_pit_branch_entries(cls, net, valve_pit, node_name):
31
        """
32
        Function which creates pit branch entries with a specific table.
33

34
        :param net: The pandapipes network
35
        :type net: pandapipesNet
36
        :param valve_pit:
37
        :type valve_pit:
38
        :param node_name:
39
        :type node_name:
40
        :return: No Output.
41
        """
42 1
        valve_pit = super().create_pit_branch_entries(net, valve_pit, node_name)
43 1
        valve_pit[:, D] = net[cls.table_name()].diameter_m.values
44 1
        valve_pit[:, AREA] = valve_pit[:, D] ** 2 * np.pi / 4
45 1
        valve_pit[:, LC] = net[cls.table_name()].loss_coefficient.values
46

47 1
    @classmethod
48
    def calculate_pressure_lift(cls, net, valve_pit, node_pit):
49
        """
50

51
        :param net:
52
        :type net:
53
        :param valve_pit:
54
        :type valve_pit:
55
        :param node_pit:
56
        :type node_pit:
57
        :return:
58
        :rtype:
59
        """
60 1
        valve_pit[:, PL] = 0
61

62 1
    @classmethod
63
    def calculate_temperature_lift(cls, net, valve_pit, node_pit):
64
        """
65

66
        :param net:
67
        :type net:
68
        :param valve_pit:
69
        :type valve_pit:
70
        :param node_pit:
71
        :type node_pit:
72
        :return:
73
        :rtype:
74
        """
75 1
        valve_pit[:, TL] = 0
76

77 1
    @classmethod
78
    def extract_results(cls, net, options, node_name):
79
        """
80
        Function that extracts certain results.
81

82
        :param net: The pandapipes network
83
        :type net: pandapipesNet
84
        :param options:
85
        :type options:
86
        :param node_name:
87
        :type node_name:
88
        :return: No Output.
89
        """
90 1
        placement_table, valve_pit, res_table = super().extract_results(net, options, node_name)
91

92 1
        node_pit = net["_active_pit"]["node"]
93 1
        node_active_idx_lookup = get_lookup(net, "node", "index_active")[node_name]
94 1
        junction_idx_lookup = get_lookup(net, "node", "index")[node_name]
95 1
        from_junction_nodes = node_active_idx_lookup[junction_idx_lookup[
96
            net[cls.table_name()]["from_junction"].values[placement_table]]]
97 1
        to_junction_nodes = node_active_idx_lookup[junction_idx_lookup[
98
            net[cls.table_name()]["to_junction"].values[placement_table]]]
99

100 1
        from_nodes = valve_pit[:, FROM_NODE].astype(np.int32)
101 1
        to_nodes = valve_pit[:, TO_NODE].astype(np.int32)
102 1
        p_scale = get_net_option(net, "p_scale")
103 1
        fluid = get_fluid(net)
104

105 1
        v_mps = valve_pit[:, VINIT]
106

107 1
        t0 = node_pit[from_nodes, TINIT_NODE]
108 1
        t1 = node_pit[to_nodes, TINIT_NODE]
109 1
        mf = valve_pit[:, LOAD_VEC_NODES]
110 1
        vf = valve_pit[:, LOAD_VEC_NODES] / get_fluid(net).get_density((t0 + t1) / 2)
111

112 1
        idx_active = valve_pit[:, ELEMENT_IDX]
113 1
        idx_sort, v_sum, mf_sum, vf_sum = \
114
            _sum_by_group(idx_active, v_mps, mf, vf)
115

116 1
        if fluid.is_gas:
117
            # derived from the ideal gas law
118 1
            p_from = node_pit[from_nodes, PAMB] + node_pit[from_nodes, PINIT] * p_scale
119 1
            p_to = node_pit[to_nodes, PAMB] + node_pit[to_nodes, PINIT] * p_scale
120 1
            numerator = NORMAL_PRESSURE * valve_pit[:, TINIT]
121 1
            normfactor_from = numerator * fluid.get_property("compressibility", p_from) \
122
                / (p_from * NORMAL_TEMPERATURE)
123 1
            normfactor_to = numerator * fluid.get_property("compressibility", p_to) \
124
                / (p_to * NORMAL_TEMPERATURE)
125 1
            v_gas_from = v_mps * normfactor_from
126 1
            v_gas_to = v_mps * normfactor_to
127 1
            mask = p_from != p_to
128 1
            p_mean = np.empty_like(p_to)
129 1
            p_mean[~mask] = p_from[~mask]
130 1
            p_mean[mask] = 2 / 3 * (p_from[mask] ** 3 - p_to[mask] ** 3) \
131
                / (p_from[mask] ** 2 - p_to[mask] ** 2)
132 1
            normfactor_mean = numerator * fluid.get_property("compressibility", p_mean) \
133
                / (p_mean * NORMAL_TEMPERATURE)
134 1
            v_gas_mean = v_mps * normfactor_mean
135

136 1
            idx_sort, v_gas_from_sum, v_gas_to_sum, v_gas_mean_sum, nf_from_sum, nf_to_sum = \
137
                _sum_by_group(idx_active, v_gas_from, v_gas_to, v_gas_mean, normfactor_from,
138
                              normfactor_to)
139

140 1
            res_table["v_from_m_per_s"].values[placement_table] = v_gas_from_sum
141 1
            res_table["v_to_m_per_s"].values[placement_table] = v_gas_to_sum
142 1
            res_table["v_mean_m_per_s"].values[placement_table] = v_gas_mean_sum
143 1
            res_table["normfactor_from"].values[placement_table] = nf_from_sum
144 1
            res_table["normfactor_to"].values[placement_table] = nf_to_sum
145
        else:
146 1
            res_table["v_mean_m_per_s"].values[placement_table] = v_sum
147

148 1
        res_table["p_from_bar"].values[placement_table] = node_pit[from_junction_nodes, PINIT]
149 1
        res_table["p_to_bar"].values[placement_table] = node_pit[to_junction_nodes, PINIT]
150 1
        res_table["t_from_k"].values[placement_table] = node_pit[from_junction_nodes, TINIT_NODE]
151 1
        res_table["t_to_k"].values[placement_table] = node_pit[to_junction_nodes, TINIT_NODE]
152 1
        res_table["mdot_to_kg_per_s"].values[placement_table] = -mf_sum
153 1
        res_table["mdot_from_kg_per_s"].values[placement_table] = mf_sum
154 1
        res_table["vdot_norm_m3_per_s"].values[placement_table] = vf_sum
155 1
        idx_pit = valve_pit[:, ELEMENT_IDX]
156 1
        idx_sort, lambda_sum, reynolds_sum, = \
157
            _sum_by_group(idx_pit, valve_pit[:, LAMBDA], valve_pit[:, RE])
158 1
        res_table["lambda"].values[placement_table] = lambda_sum
159 1
        res_table["reynolds"].values[placement_table] = reynolds_sum
160

161 1
    @classmethod
162
    def get_component_input(cls):
163
        """
164

165
        :return:
166
        :rtype:
167
        """
168 1
        return [("name", dtype(object)),
169
                ("from_junction", "i8"),
170
                ("to_junction", "i8"),
171
                ("diameter_m", "f8"),
172
                ("opened", "bool"),
173
                ("loss_coefficient", "f8"),
174
                ("type", dtype(object))]
175

176 1
    @classmethod
177
    def get_result_table(cls, net):
178
        """
179

180
        :param net: The pandapipes network
181
        :type net: pandapipesNet
182
        :return: (columns, all_float) - the column names and whether they are all float type. Only
183
                if False, returns columns as tuples also specifying the dtypes
184
        :rtype: (list, bool)
185
        """
186 1
        if get_fluid(net).is_gas:
187 1
            output = ["v_from_m_per_s", "v_to_m_per_s", "v_mean_m_per_s", "p_from_bar", "p_to_bar",
188
                      "t_from_k", "t_to_k", "mdot_from_kg_per_s", "mdot_to_kg_per_s",
189
                      "vdot_norm_m3_per_s", "reynolds", "lambda", "normfactor_from",
190
                      "normfactor_to"]
191
        else:
192 1
            output = ["v_mean_m_per_s", "p_from_bar", "p_to_bar", "t_from_k", "t_to_k",
193
                      "mdot_from_kg_per_s", "mdot_to_kg_per_s", "vdot_norm_m3_per_s", "reynolds",
194
                      "lambda"]
195 1
        return output, True

Read our documentation on viewing source code .

Loading