tespy 0.3.0__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,536 @@
1
+ # -*- coding: utf-8
2
+
3
+ """Module for piping components.
4
+
5
+ Components in this module:
6
+
7
+ - :func:`tespy.components.piping.pipe`
8
+ - :func:`tespy.components.piping.valve`
9
+
10
+
11
+ This file is part of project TESPy (github.com/oemof/tespy). It's copyrighted
12
+ by the contributors recorded in the version control history of the file,
13
+ available from its original location tespy/components/piping.py
14
+
15
+ SPDX-License-Identifier: MIT
16
+ """
17
+
18
+ import numpy as np
19
+
20
+ from tespy.components.components import component
21
+ from tespy.components.heat_exchangers import heat_exchanger_simple
22
+ from tespy.tools.data_containers import dc_cc
23
+ from tespy.tools.data_containers import dc_cp
24
+ from tespy.tools.data_containers import dc_simple
25
+ from tespy.tools.fluid_properties import s_mix_ph
26
+ from tespy.tools.fluid_properties import v_mix_ph
27
+ from tespy.tools.global_vars import err
28
+
29
+ # %%
30
+
31
+
32
+ class pipe(heat_exchanger_simple):
33
+ r"""
34
+ The pipe is a subclass of a simple heat exchanger.
35
+
36
+ Equations
37
+
38
+ **mandatory equations**
39
+
40
+ - :func:`tespy.components.components.component.fluid_func`
41
+ - :func:`tespy.components.components.component.mass_flow_func`
42
+
43
+ **optional equations**
44
+
45
+ .. math::
46
+
47
+ 0 = \dot{m}_{in} \cdot \left(h_{out} - h_{in} \right) -
48
+ \dot{Q}
49
+
50
+ 0 = p_{in} \cdot pr - p_{out}
51
+
52
+ - :func:`tespy.components.components.component.zeta_func`
53
+
54
+ - :func:`tespy.components.heat_exchangers.heat_exchanger_simple.darcy_func`
55
+ or :func:`tespy.components.heat_exchangers.heat_exchanger_simple.hw_func`
56
+
57
+ **additional equations**
58
+
59
+ - :func:`tespy.components.heat_exchangers.heat_exchanger_simple.additional_equations`
60
+
61
+ Inlets/Outlets
62
+
63
+ - in1
64
+ - out1
65
+
66
+ Image
67
+
68
+ .. image:: _images/pipe.svg
69
+ :scale: 100 %
70
+ :alt: alternative text
71
+ :align: center
72
+
73
+ Parameters
74
+ ----------
75
+ label : str
76
+ The label of the component.
77
+
78
+ design : list
79
+ List containing design parameters (stated as String).
80
+
81
+ offdesign : list
82
+ List containing offdesign parameters (stated as String).
83
+
84
+ design_path: str
85
+ Path to the components design case.
86
+
87
+ local_offdesign : boolean
88
+ Treat this component in offdesign mode in a design calculation.
89
+
90
+ local_design : boolean
91
+ Treat this component in design mode in an offdesign calculation.
92
+
93
+ char_warnings: boolean
94
+ Ignore warnings on default characteristics usage for this component.
95
+
96
+ printout: boolean
97
+ Include this component in the network's results printout.
98
+
99
+ Q : str/float/tespy.tools.data_containers.dc_cp
100
+ Heat transfer, :math:`Q/\text{W}`.
101
+
102
+ pr : str/float/tespy.tools.data_containers.dc_cp
103
+ Outlet to inlet pressure ratio, :math:`pr/1`.
104
+
105
+ zeta : str/float/tespy.tools.data_containers.dc_cp
106
+ Geometry independent friction coefficient,
107
+ :math:`\frac{\zeta}{D^4}/\frac{1}{\text{m}^4}`.
108
+
109
+ D : str/float/tespy.tools.data_containers.dc_cp
110
+ Diameter of the pipes, :math:`D/\text{m}`.
111
+
112
+ L : str/float/tespy.tools.data_containers.dc_cp
113
+ Length of the pipes, :math:`L/\text{m}`.
114
+
115
+ ks : str/float/tespy.tools.data_containers.dc_cp
116
+ Pipe's roughness, :math:`ks/\text{m}` for darcy friction,
117
+ :math:`ks/\text{1}` for hazen-williams equation.
118
+
119
+ hydro_group : String/tespy.helpers.dc_gcp
120
+ Parametergroup for pressure drop calculation based on pipes dimensions.
121
+ Choose 'HW' for hazen-williams equation, else darcy friction factor is
122
+ used.
123
+
124
+ kA : str/float/tespy.tools.data_containers.dc_cp
125
+ Area independent heat transition coefficient,
126
+ :math:`kA/\frac{\text{W}}{\text{K}}`.
127
+
128
+ kA_char : tespy.tools.characteristics.char_line/tespy.tools.data_containers.dc_cc
129
+ Characteristic line for heat transfer coefficient.
130
+
131
+ Tamb : float/tespy.tools.data_containers.dc_cp
132
+ Ambient temperature, provide parameter in network's temperature
133
+ unit.
134
+
135
+ kA_group : tespy.helpers.dc_gcp
136
+ Parametergroup for heat transfer calculation from ambient temperature
137
+ and area independent heat transfer coefficient kA.
138
+
139
+ Example
140
+ -------
141
+ A mass flow of 10 kg/s ethanol is transported in a pipeline. The pipe is
142
+ considered adiabatic and has a length of 500 meters. We can calculate the
143
+ diameter required at a given pressure loss of 2.5 %. After we determined
144
+ the required diameter, we can predict pressure loss at a different mass
145
+ flow through the pipeline.
146
+
147
+ >>> from tespy.components import sink, source, pipe
148
+ >>> from tespy.connections import connection
149
+ >>> from tespy.networks import network
150
+ >>> import shutil
151
+ >>> fluid_list = ['ethanol']
152
+ >>> nw = network(fluids=fluid_list)
153
+ >>> nw.set_attr(p_unit='bar', T_unit='C', h_unit='kJ / kg', iterinfo=False)
154
+ >>> so = source('source 1')
155
+ >>> si = sink('sink 1')
156
+ >>> pi = pipe('pipeline')
157
+ >>> pi.component()
158
+ 'pipe'
159
+ >>> pi.set_attr(pr=0.975, Q=0, design=['pr'], L=100, D='var', ks=5e-5)
160
+ >>> inc = connection(so, 'out1', pi, 'in1')
161
+ >>> outg = connection(pi, 'out1', si, 'in1')
162
+ >>> nw.add_conns(inc, outg)
163
+ >>> inc.set_attr(fluid={'ethanol': 1}, m=10, T=30, p=3)
164
+ >>> nw.solve('design')
165
+ >>> nw.save('tmp')
166
+ >>> round(pi.D.val, 3)
167
+ 0.119
168
+ >>> outg.p.val / inc.p.val == pi.pr.val
169
+ True
170
+ >>> inc.set_attr(m=15)
171
+ >>> pi.set_attr(D=pi.D.val)
172
+ >>> nw.solve('offdesign', design_path='tmp')
173
+ >>> round(pi.pr.val, 2)
174
+ 0.94
175
+ >>> shutil.rmtree('./tmp', ignore_errors=True)
176
+ """
177
+
178
+ @staticmethod
179
+ def component():
180
+ return 'pipe'
181
+
182
+ # %%
183
+
184
+
185
+ class valve(component):
186
+ r"""
187
+ The valve throttles a fluid without changing enthalpy.
188
+
189
+ Equations
190
+
191
+ **mandatory equations**
192
+
193
+ - :func:`tespy.components.components.component.fluid_func`
194
+ - :func:`tespy.components.components.component.mass_flow_func`
195
+
196
+ .. math::
197
+
198
+ 0 = h_{in} - h_{out}
199
+
200
+ **optional equations**
201
+
202
+ .. math::
203
+
204
+ 0 = p_{in} \cdot pr - p_{out}
205
+
206
+ - :func:`tespy.components.components.component.zeta_func`
207
+
208
+ - :func:`tespy.components.piping.valve.dp_char_func`
209
+
210
+
211
+ Inlets/Outlets
212
+
213
+ - in1
214
+ - out1
215
+
216
+ Image
217
+
218
+ .. image:: _images/valve.svg
219
+ :scale: 100 %
220
+ :alt: alternative text
221
+ :align: center
222
+
223
+ Parameters
224
+ ----------
225
+ label : str
226
+ The label of the component.
227
+
228
+ design : list
229
+ List containing design parameters (stated as String).
230
+
231
+ offdesign : list
232
+ List containing offdesign parameters (stated as String).
233
+
234
+ design_path: str
235
+ Path to the components design case.
236
+
237
+ local_offdesign : boolean
238
+ Treat this component in offdesign mode in a design calculation.
239
+
240
+ local_design : boolean
241
+ Treat this component in design mode in an offdesign calculation.
242
+
243
+ char_warnings: boolean
244
+ Ignore warnings on default characteristics usage for this component.
245
+
246
+ printout: boolean
247
+ Include this component in the network's results printout.
248
+
249
+ pr : str/float/tespy.tools.data_containers.dc_cp
250
+ Outlet to inlet pressure ratio, :math:`pr/1`
251
+
252
+ zeta : str/float/tespy.tools.data_containers.dc_cp
253
+ Geometry independent friction coefficient,
254
+ :math:`\frac{\zeta}{D^4}/\frac{1}{\text{m}^4}`.
255
+
256
+ dp_char : tespy.tools.characteristics.char_line/tespy.tools.data_containers.dc_cc
257
+ Characteristic line for difference pressure to mass flow.
258
+
259
+ Example
260
+ -------
261
+ A mass flow of 1 kg/s methane is throttled from 80 bar to 15 bar in a
262
+ valve. The inlet temperature is at 50 °C. It is possible to determine the
263
+ outlet temperature as the throttling does not change enthalpy.
264
+
265
+ >>> from tespy.components import sink, source, valve
266
+ >>> from tespy.connections import connection
267
+ >>> from tespy.networks import network
268
+ >>> import shutil
269
+ >>> fluid_list = ['CH4']
270
+ >>> nw = network(fluids=fluid_list, p_unit='bar', T_unit='C',
271
+ ... iterinfo=False)
272
+ >>> so = source('source')
273
+ >>> si = sink('sink')
274
+ >>> v = valve('valve')
275
+ >>> v.component()
276
+ 'valve'
277
+ >>> so_v = connection(so, 'out1', v, 'in1')
278
+ >>> v_si = connection(v, 'out1', si, 'in1')
279
+ >>> nw.add_conns(so_v, v_si)
280
+ >>> v.set_attr(offdesign=['zeta'])
281
+ >>> so_v.set_attr(fluid={'CH4': 1}, m=1, T=50, p=80, design=['m'])
282
+ >>> v_si.set_attr(p=15)
283
+ >>> nw.solve('design')
284
+ >>> nw.save('tmp')
285
+ >>> round(v_si.T.val, 1)
286
+ 26.3
287
+ >>> round(v.pr.val, 3)
288
+ 0.188
289
+
290
+ The simulation determined the area independant zeta value
291
+ :math:`\frac{\zeta}{D^4}`. This zeta remains constant if the cross
292
+ sectional area of the valve opening does not change. Using the zeta value
293
+ we can determine the pressure ratio at a different feed pressure.
294
+
295
+ >>> so_v.set_attr(p=70)
296
+ >>> nw.solve('offdesign', design_path='tmp')
297
+ >>> round(so_v.m.val, 1)
298
+ 0.9
299
+ >>> round(v_si.T.val, 1)
300
+ 30.0
301
+ >>> shutil.rmtree('./tmp', ignore_errors=True)
302
+ """
303
+
304
+ @staticmethod
305
+ def component():
306
+ return 'valve'
307
+
308
+ @staticmethod
309
+ def attr():
310
+ return {
311
+ 'pr': dc_cp(min_val=1e-4, max_val=1), 'zeta': dc_cp(min_val=0),
312
+ 'dp_char': dc_cc(param='m'),
313
+ 'Sirr': dc_simple()
314
+ }
315
+
316
+ @staticmethod
317
+ def inlets():
318
+ return ['in1']
319
+
320
+ @staticmethod
321
+ def outlets():
322
+ return ['out1']
323
+
324
+ def comp_init(self, nw):
325
+
326
+ component.comp_init(self, nw)
327
+
328
+ # number of mandatroy equations for
329
+ # fluid balance: num_fl
330
+ # mass flow: 1
331
+ # enthalpy: 1
332
+ self.num_eq = self.num_nw_fluids + 2
333
+ for var in [self.pr, self.zeta, self.dp_char]:
334
+ if var.is_set is True:
335
+ self.num_eq += 1
336
+
337
+ self.jacobian = np.zeros((
338
+ self.num_eq,
339
+ self.num_i + self.num_o + self.num_vars,
340
+ self.num_nw_vars))
341
+
342
+ self.residual = np.zeros(self.num_eq)
343
+ pos = self.num_nw_fluids
344
+ self.jacobian[0:pos] = self.fluid_deriv()
345
+ self.jacobian[pos:pos + 1] = self.mass_flow_deriv()
346
+ self.jacobian[pos + 1:pos + 2] = self.enthalpy_deriv()
347
+
348
+ def equations(self):
349
+ r"""Calculate residual vector with results of equations."""
350
+ k = 0
351
+ ######################################################################
352
+ # eqations for fluids
353
+ self.residual[k:k + self.num_nw_fluids] = self.fluid_func()
354
+ k += self.num_nw_fluids
355
+
356
+ ######################################################################
357
+ # eqation for mass flow
358
+ self.residual[k] = self.mass_flow_func()
359
+ k += 1
360
+
361
+ ######################################################################
362
+ # eqation for enthalpy
363
+ self.residual[k] = self.inl[0].h.val_SI - self.outl[0].h.val_SI
364
+ k += 1
365
+
366
+ ######################################################################
367
+ # eqation for specified pressure ratio
368
+ if self.pr.is_set:
369
+ self.residual[k] = (
370
+ self.inl[0].p.val_SI * self.pr.val - self.outl[0].p.val_SI)
371
+ k += 1
372
+
373
+ ######################################################################
374
+ # eqation specified zeta
375
+ if self.zeta.is_set:
376
+ if np.absolute(self.residual[k]) > err ** 2 or self.it % 4 == 0:
377
+ self.residual[k] = self.zeta_func(zeta='zeta')
378
+ k += 1
379
+
380
+ ######################################################################
381
+ # equation for specified difference pressure char
382
+ if self.dp_char.is_set:
383
+ if np.absolute(self.residual[k]) > err ** 2 or self.it % 4 == 0:
384
+ self.residual[k] = self.dp_char_func()
385
+ k += 1
386
+
387
+ def derivatives(self, increment_filter):
388
+ r"""Calculate partial derivatives for given equations."""
389
+ ######################################################################
390
+ # derivatives fluid, mass flow and enthalpy balance are static
391
+ k = self.num_nw_fluids + 2
392
+
393
+ ######################################################################
394
+ # derivatives for specified pressure ratio
395
+ if self.pr.is_set:
396
+ self.jacobian[k, 0, 1] = self.pr.val
397
+ self.jacobian[k, 1, 1] = -1
398
+ if self.pr.is_var:
399
+ self.jacobian[k, 2 + self.pr.var_pos, 0] = (
400
+ self.inl[0].p.val_SI)
401
+ k += 1
402
+
403
+ ######################################################################
404
+ # derivatives for specified zeta
405
+ if self.zeta.is_set:
406
+ f = self.zeta_func
407
+ if not increment_filter[0, 0]:
408
+ self.jacobian[k, 0, 0] = self.numeric_deriv(
409
+ f, 'm', 0, zeta='zeta')
410
+ if not increment_filter[0, 1]:
411
+ self.jacobian[k, 0, 1] = self.numeric_deriv(
412
+ f, 'p', 0, zeta='zeta')
413
+ if not increment_filter[0, 2]:
414
+ self.jacobian[k, 0, 2] = self.numeric_deriv(
415
+ f, 'h', 0, zeta='zeta')
416
+ if not increment_filter[1, 1]:
417
+ self.jacobian[k, 1, 1] = self.numeric_deriv(
418
+ f, 'p', 1, zeta='zeta')
419
+ if not increment_filter[1, 2]:
420
+ self.jacobian[k, 1, 2] = self.numeric_deriv(
421
+ f, 'h', 1, zeta='zeta')
422
+ if self.zeta.is_var:
423
+ self.jacobian[k, 2 + self.zeta.var_pos, 0] = (
424
+ self.numeric_deriv(f, 'zeta', 2, zeta='zeta'))
425
+ k += 1
426
+
427
+ ######################################################################
428
+ # derivatives for specified difference pressure
429
+ if self.dp_char.is_set:
430
+ if not increment_filter[0, 0]:
431
+ self.jacobian[k, 0, 0] = self.numeric_deriv(
432
+ self.dp_char_func, 'm', 0)
433
+ self.jacobian[k, 0, 1] = 1
434
+ self.jacobian[k, 1, 1] = -1
435
+ k += 1
436
+
437
+ def enthalpy_deriv(self):
438
+ r"""
439
+ Calculate matrix of partial derivatives for enthalpy balance equation.
440
+
441
+ Returns
442
+ -------
443
+ deriv : list
444
+ Matrix of partial derivatives.
445
+ """
446
+ deriv = np.zeros((1, 2 + self.num_vars, self.num_nw_vars))
447
+ deriv[0, 0, 2] = 1
448
+ deriv[0, 1, 2] = -1
449
+ return deriv.tolist()
450
+
451
+ def dp_char_func(self):
452
+ r"""
453
+ Equation for characteristic line of difference pressure to mass flow.
454
+
455
+ Returns
456
+ -------
457
+ res : ndarray
458
+ Residual value of equation.
459
+
460
+ .. math::
461
+
462
+ res = p_1 - p_2 - f \left( \dot{m} \right)
463
+ """
464
+ i = self.inl[0].to_flow()
465
+ o = self.outl[0].to_flow()
466
+
467
+ return i[1] - o[1] - self.dp_char.func.evaluate(i[0])
468
+
469
+ def initialise_source(self, c, key):
470
+ r"""
471
+ Return a starting value for pressure and enthalpy at outlet.
472
+
473
+ Parameters
474
+ ----------
475
+ c : tespy.connections.connection
476
+ Connection to perform initialisation on.
477
+
478
+ key : str
479
+ Fluid property to retrieve.
480
+
481
+ Returns
482
+ -------
483
+ val : float
484
+ Starting value for pressure/enthalpy in SI units.
485
+
486
+ .. math::
487
+
488
+ val = \begin{cases}
489
+ 4 \cdot 10^5 & \text{key = 'p'}\\
490
+ 5 \cdot 10^5 & \text{key = 'h'}
491
+ \end{cases}
492
+ """
493
+ if key == 'p':
494
+ return 4e5
495
+ elif key == 'h':
496
+ return 5e5
497
+
498
+ def initialise_target(self, c, key):
499
+ r"""
500
+ Return a starting value for pressure and enthalpy at inlet.
501
+
502
+ Parameters
503
+ ----------
504
+ c : tespy.connections.connection
505
+ Connection to perform initialisation on.
506
+
507
+ key : str
508
+ Fluid property to retrieve.
509
+
510
+ Returns
511
+ -------
512
+ val : float
513
+ Starting value for pressure/enthalpy in SI units.
514
+
515
+ .. math::
516
+
517
+ val = \begin{cases}
518
+ 5 \cdot 10^5 & \text{key = 'p'}\\
519
+ 5 \cdot 10^5 & \text{key = 'h'}
520
+ \end{cases}
521
+ """
522
+ if key == 'p':
523
+ return 5e5
524
+ elif key == 'h':
525
+ return 5e5
526
+
527
+ def calc_parameters(self):
528
+ r"""Postprocessing parameter calculation."""
529
+ i = self.inl[0].to_flow()
530
+ o = self.outl[0].to_flow()
531
+ self.pr.val = o[1] / i[1]
532
+ self.zeta.val = ((i[1] - o[1]) * np.pi ** 2 /
533
+ (8 * i[0] ** 2 * (v_mix_ph(i) + v_mix_ph(o)) / 2))
534
+ self.Sirr.val = i[0] * (s_mix_ph(o) - s_mix_ph(i))
535
+
536
+ self.check_parameter_bounds()