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,965 @@
1
+ # -*- coding: utf-8
2
+
3
+ """Module for components of type reactor.
4
+
5
+ Components in this module:
6
+
7
+ - :func:`tespy.components.reactors.water_electrolyzer`
8
+
9
+
10
+ This file is part of project TESPy (github.com/oemof/tespy). It's copyrighted
11
+ by the contributors recorded in the version control history of the file,
12
+ available from its original location tespy/components/reactors.py
13
+
14
+ SPDX-License-Identifier: MIT
15
+ """
16
+
17
+ import logging
18
+
19
+ import CoolProp.CoolProp as CP
20
+ import numpy as np
21
+
22
+ from tespy.components.components import component
23
+ from tespy.tools.data_containers import dc_cc
24
+ from tespy.tools.data_containers import dc_cp
25
+ from tespy.tools.data_containers import dc_simple
26
+ from tespy.tools.fluid_properties import T_mix_ph
27
+ from tespy.tools.fluid_properties import dT_mix_dph
28
+ from tespy.tools.fluid_properties import dT_mix_pdh
29
+ from tespy.tools.fluid_properties import h_mix_pT
30
+ from tespy.tools.fluid_properties import v_mix_ph
31
+ from tespy.tools.global_vars import err
32
+ from tespy.tools.global_vars import molar_masses
33
+ from tespy.tools.helpers import TESPyComponentError
34
+
35
+ # %%
36
+
37
+
38
+ class water_electrolyzer(component):
39
+ r"""
40
+ The water electrolyzer produces hydrogen and oxygen from water and power.
41
+
42
+ Equations
43
+
44
+ **mandatory equations**
45
+
46
+ .. math::
47
+
48
+ 0 = x_{i,in1} - x_{i,out1} \forall i \in \text{fluids}
49
+
50
+ \forall i \in \text{network fluids}:
51
+
52
+ 0 = \begin{cases}
53
+ 1 - x_{i,in2} & \text{i=}H_{2}O\\
54
+ x_{i,in2} & \text{else}
55
+ \end{cases}\\
56
+
57
+ 0 = \begin{cases}
58
+ 1 - x_{i,out2} & \text{i=}O_{2}\\
59
+ x_{i,out2} & \text{else}
60
+ \end{cases}\\
61
+
62
+ 0 = \begin{cases}
63
+ 1 - x_{i,out3} & \text{i=}H_{2}\\
64
+ x_{i,out3} & \text{else}
65
+ \end{cases}\\
66
+
67
+ O_2 = \frac{M_{O_2}}{M_{O_2} + 2 \cdot M_{H_2}}\\
68
+
69
+ 0 = \dot{m}_{H_{2}O,in1} - \dot{m}_{H_{2}O,out1}\\
70
+ 0 = O_2 \cdot \dot{m}_{H_{2}O,in2} - \dot{m}_{O_2,out2}\\
71
+ 0 = \left(1 - O_2\right) \cdot \dot{m}_{H_{2}O,in2} -
72
+ \dot{m}_{H_2,out3}\\
73
+
74
+ 0 = p_{H_{2}O,in2} - p_{O_2,out2}\\
75
+ 0 = p_{H_{2}O,in2} - p_{H_2,out3}
76
+
77
+ Energy balance equation
78
+
79
+ :func:`tespy.components.reactors.water_electrolyzer.energy_balance`.
80
+
81
+ .. math::
82
+
83
+ 0 = T_{O_2,out2} - T_{H_2,out3}
84
+
85
+ **optional equations**
86
+
87
+ .. math::
88
+
89
+ 0 = P - \dot{m}_{H_2,out3} \cdot e\\
90
+
91
+ 0 = p_{H_{2}O,in1} \cdot pr - p_{H_{2}O,out1}
92
+
93
+ - :func:`tespy.components.components.component.zeta_func`
94
+
95
+ .. math::
96
+
97
+ 0 = \dot{Q} - \dot{m}_{in1} \cdot \left(h_{in1} -
98
+ h_{out1}\right) \\
99
+
100
+ 0 = P - \dot{m}_{H_2,out3} \cdot \frac{e_0}{\eta}
101
+
102
+ - :func:`tespy.components.reactors.water_electrolyzer.eta_char_func`
103
+
104
+ Inlets/Outlets
105
+
106
+ - in1 (cooling inlet), in2 (feed water inlet)
107
+ - out1 (cooling outlet), out2 (hydrogen outlet), out3 (oxigen outlet)
108
+
109
+ Image
110
+
111
+ .. image:: _images/water_electrolyzer.svg
112
+ :scale: 100 %
113
+ :alt: alternative text
114
+ :align: center
115
+
116
+ Parameters
117
+ ----------
118
+ label : str
119
+ The label of the component.
120
+
121
+ design : list
122
+ List containing design parameters (stated as String).
123
+
124
+ offdesign : list
125
+ List containing offdesign parameters (stated as String).
126
+
127
+ design_path: str
128
+ Path to the components design case.
129
+
130
+ local_offdesign : boolean
131
+ Treat this component in offdesign mode in a design calculation.
132
+
133
+ local_design : boolean
134
+ Treat this component in design mode in an offdesign calculation.
135
+
136
+ char_warnings: boolean
137
+ Ignore warnings on default characteristics usage for this component.
138
+
139
+ printout: boolean
140
+ Include this component in the network's results printout.
141
+
142
+ P : float/tespy.helpers.dc_cp
143
+ Power input, :math:`P/\text{W}`.
144
+
145
+ Q : float/tespy.tools.data_containers.dc_cp
146
+ Heat output of cooling, :math:`Q/\text{W}`
147
+
148
+ e : str/float/tespy.tools.data_containers.dc_cp
149
+ Electrolysis specific energy consumption,
150
+ :math:`e/(\text{J}/\text{m}^3)`.
151
+
152
+ eta : str/float/tespy.tools.data_containers.dc_cp
153
+ Electrolysis efficiency, :math:`\eta/1`.
154
+
155
+ eta_char : tespy.tools.characteristics.char_line/tespy.tools.data_containers.dc_cc
156
+ Electrolysis efficiency characteristic line.
157
+
158
+ pr : float/tespy.tools.data_containers.dc_cp
159
+ Cooling loop pressure ratio, :math:`pr/1`.
160
+
161
+ zeta : str/float/tespy.tools.data_containers.dc_cp
162
+ Geometry independent friction coefficient for cooling loop pressure
163
+ drop, :math:`\frac{\zeta}{D^4}/\frac{1}{\text{m}^4}`.
164
+
165
+ Note
166
+ ----
167
+ Other than usual components, the water electrolyzer has the fluid
168
+ composition built into its equations for the feed water inlet and the
169
+ hydrogen and oxygen outlet. Thus, the user must not specify the fluid
170
+ composition at these connections!
171
+
172
+ Example
173
+ -------
174
+ Create a water electrolyzer and compress the hydrogen, e.g. for a hydrogen
175
+ storage.
176
+
177
+ >>> from tespy.components import (sink, source, compressor,
178
+ ... water_electrolyzer)
179
+ >>> from tespy.connections import connection
180
+ >>> from tespy.networks import network
181
+ >>> from tespy.tools.data_containers import dc_cc
182
+ >>> import shutil
183
+ >>> fluid_list = ['O2', 'H2O', 'H2']
184
+ >>> nw = network(fluids=fluid_list, T_unit='C', p_unit='bar',
185
+ ... v_unit='l / s', iterinfo=False)
186
+ >>> fw = source('feed water')
187
+ >>> oxy = sink('oxygen sink')
188
+ >>> hydro = sink('hydrogen sink')
189
+ >>> cw_cold = source('cooling water source')
190
+ >>> cw_hot = sink('cooling water sink')
191
+ >>> comp = compressor('compressor', eta_s=0.9)
192
+ >>> el = water_electrolyzer('electrolyzer')
193
+ >>> el.component()
194
+ 'water electrolyzer'
195
+
196
+ The electrolyzer should produce 100 l/s of hydrogen at an operating
197
+ pressure of 10 bars and an outlet temperature of 50 °C. The fluid
198
+ composition needs to be specified for the cooling liquid only. The storage
199
+ pressure is 25 bars. The electrolysis efficiency is at 80 % and the
200
+ compressor isentropic efficiency at 85 %. After designing the plant the
201
+ offdesign electrolysis efficiency is predicted by the characteristic line.
202
+ The default characteristic line can be found here: :py:mod:`tespy.data`.
203
+
204
+ >>> fw_el = connection(fw, 'out1', el, 'in2')
205
+ >>> el_o = connection(el, 'out2', oxy, 'in1')
206
+ >>> el_cmp = connection(el, 'out3', comp, 'in1')
207
+ >>> cmp_h = connection(comp, 'out1', hydro, 'in1')
208
+ >>> cw_el = connection(cw_cold, 'out1', el, 'in1')
209
+ >>> el_cw = connection(el, 'out1', cw_hot, 'in1')
210
+ >>> nw.add_conns(fw_el, el_o, el_cmp, cmp_h, cw_el, el_cw)
211
+ >>> fw_el.set_attr(p=10, T=15)
212
+ >>> cw_el.set_attr(p=5, T=15, fluid={'H2O': 1, 'H2': 0, 'O2': 0})
213
+ >>> el_cw.set_attr(T=45)
214
+ >>> cmp_h.set_attr(p=25)
215
+ >>> el_cmp.set_attr(v=100, T=50)
216
+ >>> el.set_attr(eta=0.8, pr_c=0.99, design=['eta', 'pr_c'],
217
+ ... offdesign=['eta_char', 'zeta'])
218
+ >>> comp.set_attr(eta_s=0.85)
219
+ >>> nw.solve('design')
220
+ >>> nw.save('tmp')
221
+ >>> round(el.e0 / el.P.val * el_cmp.m.val_SI, 1)
222
+ 0.8
223
+ >>> P_design = el.P.val / 1e6
224
+ >>> round(P_design, 1)
225
+ 13.2
226
+ >>> nw.solve('offdesign', design_path='tmp')
227
+ >>> round(el.eta.val, 1)
228
+ 0.8
229
+ >>> el_cmp.set_attr(v=np.nan)
230
+ >>> el.set_attr(P=P_design * 0.66)
231
+ >>> nw.solve('offdesign', design_path='tmp')
232
+ >>> round(el.eta.val, 2)
233
+ 0.88
234
+ >>> shutil.rmtree('./tmp', ignore_errors=True)
235
+ """
236
+
237
+ @staticmethod
238
+ def component():
239
+ return 'water electrolyzer'
240
+
241
+ @staticmethod
242
+ def attr():
243
+ return {
244
+ 'P': dc_cp(min_val=0), 'Q': dc_cp(max_val=0),
245
+ 'pr_c': dc_cp(max_val=1), 'zeta': dc_cp(min_val=0),
246
+ 'eta': dc_cp(min_val=0, max_val=1), 'eta_char': dc_cc(),
247
+ 'e': dc_cp(),
248
+ 'S': dc_simple()
249
+ }
250
+
251
+ @staticmethod
252
+ def inlets():
253
+ return ['in1', 'in2']
254
+
255
+ @staticmethod
256
+ def outlets():
257
+ return ['out1', 'out2', 'out3']
258
+
259
+ def comp_init(self, nw):
260
+
261
+ if not self.P.is_set:
262
+ self.set_attr(P='var')
263
+ msg = ('The power output of cogeneration units must be set! '
264
+ 'We are adding the power output of component ' +
265
+ self.label + ' as custom variable of the system.')
266
+ logging.info(msg)
267
+
268
+ component.comp_init(self, nw)
269
+
270
+ o2 = [x for x in nw.fluids if x in [
271
+ a.replace(' ', '') for a in CP.get_aliases('O2')]]
272
+ if len(o2) == 0:
273
+ msg = ('Missing oxygen in network fluids, component ' +
274
+ self.label + ' of type ' + self.component() +
275
+ ' requires oxygen in network fluids.')
276
+ logging.error(msg)
277
+ raise TESPyComponentError(msg)
278
+ else:
279
+ self.o2 = o2[0]
280
+
281
+ h2o = [x for x in nw.fluids if x in [
282
+ a.replace(' ', '') for a in CP.get_aliases('H2O')]]
283
+ if len(h2o) == 0:
284
+ msg = ('Missing water in network fluids, component ' +
285
+ self.label + ' of type ' + self.component() +
286
+ ' requires water in network fluids.')
287
+ logging.error(msg)
288
+ raise TESPyComponentError(msg)
289
+ else:
290
+ self.h2o = h2o[0]
291
+
292
+ h2 = [x for x in nw.fluids if x in [
293
+ a.replace(' ', '') for a in CP.get_aliases('H2')]]
294
+ if len(h2) == 0:
295
+ msg = ('Missing hydrogen in network fluids, component ' +
296
+ self.label + ' of type ' + self.component() +
297
+ ' requires hydrogen in network fluids.')
298
+ logging.error(msg)
299
+ raise TESPyComponentError(msg)
300
+ else:
301
+ self.h2 = h2[0]
302
+
303
+ self.e0 = self.calc_e0()
304
+
305
+ # number of mandatroy equations for
306
+ # cooling loop fluids: num_fl
307
+ # fluid composition at reactor inlets/outlets: 3 * num_fl
308
+ # mass flow balances: 3
309
+ # pressure: 2
310
+ # energy balance: 1
311
+ # reactor outlet temperatures: 1
312
+ self.num_eq = self.num_nw_fluids * 4 + 7
313
+ for var in [self.e, self.eta, self.eta_char, self.Q, self.pr_c,
314
+ self.zeta]:
315
+ if var.is_set is True:
316
+ self.num_eq += 1
317
+
318
+ self.jacobian = np.zeros((
319
+ self.num_eq,
320
+ self.num_i + self.num_o + self.num_vars,
321
+ self.num_nw_vars))
322
+
323
+ self.residual = np.zeros(self.num_eq)
324
+ pos = self.num_nw_fluids * 4
325
+ self.jacobian[0:pos] = self.fluid_deriv()
326
+ self.jacobian[pos:pos + 3] = self.mass_flow_deriv()
327
+ self.jacobian[pos + 3:pos + 5] = self.pressure_deriv()
328
+
329
+ def calc_e0(self):
330
+ r"""
331
+ Calculate the minimum specific energy required for electrolysis.
332
+
333
+ Returns
334
+ -------
335
+ val : float
336
+ Minimum specific energy.
337
+
338
+ .. math::
339
+ LHV = -\frac{\sum_i {\Delta H_f^0}_i -
340
+ \sum_j {\Delta H_f^0}_j }
341
+ {M_{fuel}}\\
342
+ \forall i \in \text{reation products},\\
343
+ \forall j \in \text{reation educts},\\
344
+ \Delta H_f^0: \text{molar formation enthalpy}
345
+ """
346
+ hf = {}
347
+ hf['H2O'] = -286
348
+ hf['H2'] = 0
349
+ hf['O2'] = 0
350
+ M = molar_masses['H2']
351
+ e0 = -(2 * hf['H2O'] - 2 * hf['H2'] + hf['O2']) / (2 * M)
352
+
353
+ return e0 * 1000
354
+
355
+ def equations(self):
356
+ r"""Calculate residual vector with results of equations."""
357
+ k = 0
358
+ ######################################################################
359
+ # equations for fluids
360
+
361
+ # equations for fluid composition in cooling water
362
+ for fluid, x in self.inl[0].fluid.val.items():
363
+ self.residual[k] = x - self.outl[0].fluid.val[fluid]
364
+ k += 1
365
+
366
+ # equations to constrain fluids to inlets/outlets
367
+ self.residual[k] = 1 - self.inl[1].fluid.val[self.h2o]
368
+ k += 1
369
+ self.residual[k] = 1 - self.outl[1].fluid.val[self.o2]
370
+ k += 1
371
+ self.residual[k] = 1 - self.outl[2].fluid.val[self.h2]
372
+ k += 1
373
+
374
+ # equations to ban fluids off inlets/outlets
375
+ for fluid in self.inl[1].fluid.val.keys():
376
+ if fluid != self.h2o:
377
+ self.residual[k] = 0 - self.inl[1].fluid.val[fluid]
378
+ k += 1
379
+ if fluid != self.o2:
380
+ self.residual[k] = 0 - self.outl[1].fluid.val[fluid]
381
+ k += 1
382
+ if fluid != self.h2:
383
+ self.residual[k] = 0 - self.outl[2].fluid.val[fluid]
384
+ k += 1
385
+
386
+ ######################################################################
387
+ # eqations for mass flow balance
388
+ # equation to calculate the ratio of o2 in water
389
+ o2 = molar_masses[self.o2] / (
390
+ molar_masses[self.o2] + 2 * molar_masses[self.h2])
391
+
392
+ # equation for mass flow balance cooling water
393
+ self.residual[k] = self.inl[0].m.val_SI - self.outl[0].m.val_SI
394
+ k += 1
395
+
396
+ # equations for mass flow balance electrolyzer
397
+ self.residual[k] = o2 * self.inl[1].m.val_SI - self.outl[1].m.val_SI
398
+ k += 1
399
+ self.residual[k] = (
400
+ (1 - o2) * self.inl[1].m.val_SI - self.outl[2].m.val_SI)
401
+ k += 1
402
+
403
+ ######################################################################
404
+ # equations for pressure to set o2 and h2 output equal
405
+ self.residual[k] = self.inl[1].p.val_SI - self.outl[1].p.val_SI
406
+ k += 1
407
+ self.residual[k] = self.inl[1].p.val_SI - self.outl[2].p.val_SI
408
+ k += 1
409
+
410
+ ######################################################################
411
+ # equation for energy balance
412
+ if np.absolute(self.residual[k]) > err ** 2 or self.it % 4 == 0:
413
+ self.residual[k] = self.P.val + self.energy_balance()
414
+ k += 1
415
+
416
+ ######################################################################
417
+ # temperature electrolyzer outlet
418
+ if np.absolute(self.residual[k]) > err ** 2 or self.it % 4 == 0:
419
+ self.residual[k] = (
420
+ T_mix_ph(self.outl[1].to_flow()) -
421
+ T_mix_ph(self.outl[2].to_flow()))
422
+ k += 1
423
+
424
+ ######################################################################
425
+ # power vs hydrogen production
426
+ if self.e.is_set:
427
+ self.residual[k] = self.P.val - self.outl[2].m.val_SI * self.e.val
428
+ k += 1
429
+
430
+ ######################################################################
431
+ # specified pressure ratio
432
+ if self.pr_c.is_set:
433
+ self.residual[k] = (
434
+ self.inl[0].p.val_SI * self.pr_c.val - self.outl[0].p.val_SI)
435
+ k += 1
436
+
437
+ ######################################################################
438
+ # specified zeta value
439
+ if self.zeta.is_set:
440
+ if np.absolute(self.residual[k]) > err ** 2 or self.it % 4 == 0:
441
+ self.residual[k] = self.zeta_func(zeta='zeta')
442
+ k += 1
443
+
444
+ # equation for heat transfer
445
+
446
+ if self.Q.is_set:
447
+ self.residual[k] = (
448
+ self.Q.val - self.inl[0].m.val_SI *
449
+ (self.inl[0].h.val_SI - self.outl[0].h.val_SI))
450
+ k += 1
451
+
452
+ ######################################################################
453
+ # specified efficiency (efficiency definition: e0 / e)
454
+ if self.eta.is_set:
455
+ self.residual[k] = (
456
+ self.P.val - self.outl[2].m.val_SI *
457
+ self.e0 / self.eta.val)
458
+ k += 1
459
+
460
+ ######################################################################
461
+ # specified characteristic line for efficiency
462
+ if self.eta_char.is_set:
463
+ self.residual[k] = self.eta_char_func()
464
+ k += 1
465
+
466
+ def derivatives(self, increment_filter):
467
+ r"""Calculate partial derivatives for given equations."""
468
+ ######################################################################
469
+ # derivatives fluid, mass flow and reactor pressure are static
470
+ k = self.num_nw_fluids * 4 + 5
471
+ ######################################################################
472
+ # derivatives for energy balance equations
473
+ T_ref = 293.15
474
+ p_ref = 1e5
475
+ h_refh2o = h_mix_pT([1, p_ref, 0, self.inl[1].fluid.val], T_ref)
476
+ h_refh2 = h_mix_pT([1, p_ref, 0, self.outl[2].fluid.val], T_ref)
477
+ h_refo2 = h_mix_pT([1, p_ref, 0, self.outl[1].fluid.val], T_ref)
478
+
479
+ # derivatives cooling water inlet
480
+ self.jacobian[k, 0, 0] = -(
481
+ self.outl[0].h.val_SI - self.inl[0].h.val_SI)
482
+ self.jacobian[k, 0, 2] = self.inl[0].m.val_SI
483
+
484
+ # derivatives feed water inlet
485
+ self.jacobian[k, 1, 0] = (self.inl[1].h.val_SI - h_refh2o)
486
+ self.jacobian[k, 1, 2] = self.inl[1].m.val_SI
487
+
488
+ # derivative cooling water outlet
489
+ self.jacobian[k, 2, 2] = - self.inl[0].m.val_SI
490
+
491
+ # derivatives oxygen outlet
492
+ self.jacobian[k, 3, 0] = - (self.outl[1].h.val_SI - h_refo2)
493
+ self.jacobian[k, 3, 2] = - self.outl[1].m.val_SI
494
+
495
+ # derivatives hydrogen outlet
496
+ self.jacobian[k, 4, 0] = - self.e0 - (self.outl[2].h.val_SI - h_refh2)
497
+ self.jacobian[k, 4, 2] = - self.outl[2].m.val_SI
498
+
499
+ # derivatives for variable P
500
+ if self.P.is_var:
501
+ self.jacobian[k, 5 + self.P.var_pos, 0] = 1
502
+
503
+ k += 1
504
+
505
+ ######################################################################
506
+ # derivatives for temperature at gas outlets
507
+
508
+ # derivatives for outlet 1
509
+ if not increment_filter[3, 1]:
510
+ self.jacobian[k, 3, 1] = dT_mix_dph(self.outl[1].to_flow())
511
+ if not increment_filter[3, 2]:
512
+ self.jacobian[k, 3, 2] = dT_mix_pdh(self.outl[1].to_flow())
513
+
514
+ # derivatives for outlet 2
515
+ if not increment_filter[4, 1]:
516
+ self.jacobian[k, 4, 1] = - dT_mix_dph(self.outl[2].to_flow())
517
+ if not increment_filter[4, 2]:
518
+ self.jacobian[k, 4, 2] = - dT_mix_pdh(self.outl[2].to_flow())
519
+
520
+ k += 1
521
+
522
+ ######################################################################
523
+ # derivatives for power vs. hydrogen production
524
+ if self.e.is_set:
525
+ self.jacobian[k, 4, 0] = - self.e.val
526
+ # derivatives for variable P
527
+ if self.P.is_var:
528
+ self.jacobian[k, 5 + self.P.var_pos, 0] = 1
529
+ # derivatives for variable e
530
+ if self.e.is_var:
531
+ self.jacobian[k, 5 + self.e.var_pos, 0] = -(
532
+ self.outl[2].m.val_SI)
533
+ k += 1
534
+
535
+ ######################################################################
536
+ # derivatives for pressure ratio
537
+ if self.pr_c.is_set:
538
+ self.jacobian[k, 0, 1] = self.pr_c.val
539
+ self.jacobian[k, 2, 1] = - 1
540
+ k += 1
541
+
542
+ ######################################################################
543
+ # derivatives for zeta value
544
+ if self.zeta.is_set:
545
+ f = self.zeta_func
546
+ if not increment_filter[0, 0]:
547
+ self.jacobian[k, 0, 0] = self.numeric_deriv(
548
+ f, 'm', 0, zeta='zeta')
549
+ if not increment_filter[0, 1]:
550
+ self.jacobian[k, 0, 1] = self.numeric_deriv(
551
+ f, 'p', 0, zeta='zeta')
552
+ if not increment_filter[0, 2]:
553
+ self.jacobian[k, 0, 2] = self.numeric_deriv(
554
+ f, 'h', 0, zeta='zeta')
555
+ if not increment_filter[2, 1]:
556
+ self.jacobian[k, 2, 1] = self.numeric_deriv(
557
+ f, 'p', 2, zeta='zeta')
558
+ if not increment_filter[2, 2]:
559
+ self.jacobian[k, 2, 2] = self.numeric_deriv(
560
+ f, 'h', 2, zeta='zeta')
561
+
562
+ # derivatives for variable zeta
563
+ if self.zeta.is_var:
564
+ self.jacobian[k, 5 + self.zeta.var_pos, 0] = (
565
+ self.numeric_deriv(
566
+ f, 'zeta', 5, zeta='zeta'))
567
+ k += 1
568
+
569
+ ######################################################################
570
+ # derivatives for heat flow
571
+ if self.Q.is_set:
572
+ self.jacobian[k, 0, 0] = -(
573
+ self.inl[0].h.val_SI - self.outl[0].h.val_SI)
574
+ self.jacobian[k, 0, 2] = - self.inl[0].m.val_SI
575
+ self.jacobian[k, 2, 2] = self.inl[0].m.val_SI
576
+ k += 1
577
+
578
+ ######################################################################
579
+ # specified efficiency (efficiency definition: e0 / e)
580
+ if self.eta.is_set:
581
+ self.jacobian[k, 4, 0] = - self.e0 / self.eta.val
582
+ # derivatives for variable P
583
+ if self.P.is_var:
584
+ self.jacobian[k, 5 + self.P.var_pos, 0] = 1
585
+ k += 1
586
+
587
+ ######################################################################
588
+ # specified characteristic line for efficiency
589
+ if self.eta_char.is_set:
590
+ self.jacobian[k, 4, 0] = self.numeric_deriv(
591
+ self.eta_char_func, 'm', 4)
592
+ # derivatives for variable P
593
+ if self.P.is_var:
594
+ self.jacobian[k, 5 + self.P.var_pos, 0] = 1
595
+ k += 1
596
+
597
+ def eta_char_func(self):
598
+ r"""
599
+ Equation for given efficiency characteristic of a water electrolyzer.
600
+
601
+ Efficiency is linked to hydrogen production.
602
+
603
+ Returns
604
+ -------
605
+ res : ndarray
606
+ Residual value of equation.
607
+
608
+ .. math::
609
+
610
+ 0 = P - \dot{m}_{H_2,out3} \cdot \frac{e_0}{\eta_0 \cdot
611
+ f\left(\frac{\dot{m}_{H_2,out3}}{\dot{m}_{H_2,out3,0}} \right)}
612
+ """
613
+ expr = self.outl[2].m.val_SI / self.outl[2].m.design
614
+
615
+ return (self.P.val - self.outl[2].m.val_SI * self.e0 / (
616
+ self.eta.design * self.eta_char.func.evaluate(expr)))
617
+
618
+ def fluid_deriv(self):
619
+ r"""
620
+ Calculate the partial derivatives for cooling loop fluid balance.
621
+
622
+ Returns
623
+ -------
624
+ deriv : ndarray
625
+ Matrix with partial derivatives for the fluid equations.
626
+ """
627
+ # derivatives for cooling liquid composition
628
+ deriv = np.zeros((
629
+ self.num_nw_fluids * 4,
630
+ 5 + self.num_vars,
631
+ self.num_nw_vars))
632
+
633
+ k = 0
634
+ for fluid, x in self.inl[0].fluid.val.items():
635
+ deriv[k, 0, 3 + k] = 1
636
+ deriv[k, 2, 3 + k] = -1
637
+ k += 1
638
+
639
+ # derivatives to constrain fluids to inlets/outlets
640
+ i = 0
641
+ for fluid in self.nw_fluids:
642
+ if fluid == self.h2o:
643
+ deriv[k, 1, 3 + i] = -1
644
+ elif fluid == self.o2:
645
+ deriv[k + 1, 3, 3 + i] = -1
646
+ elif fluid == self.h2:
647
+ deriv[k + 2, 4, 3 + i] = -1
648
+ i += 1
649
+ k += 3
650
+
651
+ # derivatives to ban fluids off inlets/outlets
652
+ i = 0
653
+ for fluid in self.nw_fluids:
654
+ if fluid != self.h2o:
655
+ deriv[k, 1, 3 + i] = -1
656
+ k += 1
657
+ if fluid != self.o2:
658
+ deriv[k, 3, 3 + i] = -1
659
+ k += 1
660
+ if fluid != self.h2:
661
+ deriv[k, 4, 3 + i] = -1
662
+ k += 1
663
+ i += 1
664
+
665
+ return deriv
666
+
667
+ def mass_flow_deriv(self):
668
+ r"""
669
+ Calculate the partial derivatives for all mass flow balance equations.
670
+
671
+ Returns
672
+ -------
673
+ deriv : ndarray
674
+ Matrix with partial derivatives for the mass flow equations.
675
+ """
676
+ # deritatives for mass flow balance in the heat exchanger
677
+ deriv = np.zeros((3, 5 + self.num_vars, self.num_nw_vars))
678
+ deriv[0, 0, 0] = 1
679
+ deriv[0, 2, 0] = -1
680
+ # derivatives for mass flow balance for oxygen output
681
+ o2 = molar_masses[self.o2] / (molar_masses[self.o2] +
682
+ 2 * molar_masses[self.h2])
683
+ deriv[1, 1, 0] = o2
684
+ deriv[1, 3, 0] = -1
685
+ # derivatives for mass flow balance for hydrogen output
686
+ deriv[2, 1, 0] = (1 - o2)
687
+ deriv[2, 4, 0] = -1
688
+
689
+ return deriv
690
+
691
+ def pressure_deriv(self):
692
+ r"""
693
+ Calculate the partial derivatives for combustion pressure equations.
694
+
695
+ Returns
696
+ -------
697
+ deriv : ndarray
698
+ Matrix with partial derivatives for the pressure equations.
699
+ """
700
+ deriv = np.zeros((2, 5 + self.num_vars, self.num_nw_vars))
701
+ # derivatives for pressure oxygen outlet
702
+ deriv[0, 1, 1] = 1
703
+ deriv[0, 3, 1] = -1
704
+ # derivatives for pressure hydrogen outlet
705
+ deriv[1, 1, 1] = 1
706
+ deriv[1, 4, 1] = -1
707
+
708
+ return deriv
709
+
710
+ def bus_func(self, bus):
711
+ r"""
712
+ Calculate the value of the bus function.
713
+
714
+ Parameters
715
+ ----------
716
+ bus : tespy.connections.bus
717
+ TESPy bus object.
718
+
719
+ Returns
720
+ -------
721
+ val : float
722
+ Value of energy transfer :math:`\dot{E}`. This value is passed to
723
+ :py:meth:`tespy.components.components.component.calc_bus_value`
724
+ for value manipulation according to the specified characteristic
725
+ line of the bus.
726
+
727
+ .. math::
728
+
729
+ \dot{E} = \begin{cases}
730
+ P & \text{key = 'P'}\\
731
+ - \dot{m}_{1,in} \cdot \left(h_{out,1} - h_{in,1} \right) &
732
+ \text{key = 'Q'}\\
733
+ \end{cases}
734
+ """
735
+ ######################################################################
736
+ # equations for power on bus
737
+ if bus['param'] == 'P':
738
+ val = - self.energy_balance()
739
+
740
+ ######################################################################
741
+ # equations for heat on bus
742
+
743
+ elif bus['param'] == 'Q':
744
+ val = - self.inl[0].m.val_SI * (
745
+ self.outl[0].h.val_SI - self.inl[0].h.val_SI)
746
+
747
+ ######################################################################
748
+ # missing/invalid bus parameter
749
+
750
+ else:
751
+ msg = ('The parameter ' + str(bus['param']) + ' is not a valid '
752
+ 'parameter for a component of type ' + self.component() +
753
+ '. Please specify a bus parameter (P/Q) for component ' +
754
+ self.label + '.')
755
+ logging.error(msg)
756
+ raise ValueError(msg)
757
+
758
+ return val
759
+
760
+ def bus_deriv(self, bus):
761
+ r"""
762
+ Calculate partial derivatives of the bus function.
763
+
764
+ Parameters
765
+ ----------
766
+ bus : tespy.connections.bus
767
+ TESPy bus object.
768
+
769
+ Returns
770
+ -------
771
+ deriv : ndarray
772
+ Matrix of partial derivatives.
773
+ """
774
+ deriv = np.zeros((1, 5 + self.num_vars, self.num_nw_vars))
775
+ f = self.calc_bus_value
776
+ b = bus.comps.loc[self]
777
+
778
+ ######################################################################
779
+ # derivatives for power on bus
780
+ if b['param'] == 'P':
781
+ deriv[0, 0, 0] = self.numeric_deriv(f, 'm', 0, bus=bus)
782
+ deriv[0, 0, 2] = self.numeric_deriv(f, 'h', 0, bus=bus)
783
+
784
+ deriv[0, 1, 0] = self.numeric_deriv(f, 'm', 1, bus=bus)
785
+ deriv[0, 1, 2] = self.numeric_deriv(f, 'h', 1, bus=bus)
786
+
787
+ deriv[0, 2, 2] = self.numeric_deriv(f, 'h', 2, bus=bus)
788
+
789
+ deriv[0, 3, 0] = self.numeric_deriv(f, 'm', 3, bus=bus)
790
+ deriv[0, 3, 2] = self.numeric_deriv(f, 'h', 3, bus=bus)
791
+
792
+ deriv[0, 4, 0] = self.numeric_deriv(f, 'm', 4, bus=bus)
793
+ deriv[0, 4, 2] = self.numeric_deriv(f, 'h', 4, bus=bus)
794
+ # variable power
795
+ if self.P.is_var:
796
+ deriv[0, 5 + self.P.var_pos, 0] = (
797
+ self.numeric_deriv(f, 'P', 5, bus=bus))
798
+
799
+ ######################################################################
800
+ # derivatives for heat on bus
801
+ elif b['param'] == 'Q':
802
+
803
+ deriv[0, 0, 0] = self.numeric_deriv(f, 'm', 0, bus=bus)
804
+ deriv[0, 0, 2] = self.numeric_deriv(f, 'h', 0, bus=bus)
805
+ deriv[0, 2, 2] = self.numeric_deriv(f, 'h', 2, bus=bus)
806
+
807
+ ######################################################################
808
+ # missing/invalid bus parameter
809
+
810
+ else:
811
+ msg = ('The parameter ' + str(b['param']) + ' is not a valid '
812
+ 'parameter for a component of type ' + self.component() +
813
+ '. Please specify a bus parameter (P/Q) for component ' +
814
+ self.label + '.')
815
+ logging.error(msg)
816
+ raise ValueError(msg)
817
+
818
+ return deriv
819
+
820
+ def energy_balance(self):
821
+ r"""
822
+ Calculate the residual in energy balance of the water electrolyzer.
823
+
824
+ The residual is the negative to the necessary power input.
825
+
826
+ Returns
827
+ -------
828
+ res : float
829
+ Residual value.
830
+
831
+ .. math::
832
+
833
+ \begin{split}
834
+ res = & \dot{m}_{in,2} \cdot \left( h_{in,2} - h_{in,2,ref}
835
+ \right)\\ & - \dot{m}_{out,3} \cdot e_0\\
836
+ & -\dot{m}_{in,1} \cdot \left( h_{out,1} - h_{in,1} \right)\\
837
+ & - \dot{m}_{out,2} \cdot \left( h_{out,2} - h_{out,2,ref}
838
+ \right)\\
839
+ & - \dot{m}_{out,3} \cdot \left( h_{out,3} - h_{out,3,ref}
840
+ \right)\\
841
+ \end{split}
842
+
843
+ Note
844
+ ----
845
+ The temperature for the reference state is set to 20 °C, thus
846
+ the feed water must be liquid as proposed in the calculation of
847
+ the minimum specific energy consumption for electrolysis:
848
+ :func:`tespy.components.reactors.water_electrolyzer.calc_e0`.
849
+ The part of the equation regarding the cooling water is implemented
850
+ with negative sign as the energy for cooling is extracted from the
851
+ reactor.
852
+
853
+ - Reference temperature: 293.15 K.
854
+ - Reference pressure: 1 bar.
855
+ """
856
+ T_ref = 293.15
857
+ p_ref = 1e5
858
+
859
+ # equations to set a reference point for each h2o, h2 and o2
860
+ h_refh2o = h_mix_pT([1, p_ref, 0, self.inl[1].fluid.val], T_ref)
861
+ h_refh2 = h_mix_pT([1, p_ref, 0, self.outl[2].fluid.val], T_ref)
862
+ h_refo2 = h_mix_pT([1, p_ref, 0, self.outl[1].fluid.val], T_ref)
863
+
864
+ val = (self.inl[1].m.val_SI * (self.inl[1].h.val_SI - h_refh2o) -
865
+ self.outl[2].m.val_SI * self.e0 -
866
+ self.inl[0].m.val_SI * (self.outl[0].h.val_SI -
867
+ self.inl[0].h.val_SI) -
868
+ self.outl[1].m.val_SI * (self.outl[1].h.val_SI - h_refo2) -
869
+ self.outl[2].m.val_SI * (self.outl[2].h.val_SI - h_refh2))
870
+ return val
871
+
872
+ def initialise_fluids(self, nw):
873
+ r"""
874
+ Set values to pure fluid on water inlet and gas outlets.
875
+
876
+ Parameters
877
+ ----------
878
+ nw : tespy.networks.network
879
+ Network using this component object.
880
+ """
881
+ self.outl[1].fluid.val[self.o2] = 1
882
+ self.outl[2].fluid.val[self.h2] = 1
883
+ self.inl[1].fluid.val[self.h2o] = 1
884
+
885
+ def initialise_source(self, c, key):
886
+ r"""
887
+ Return a starting value for pressure and enthalpy at outlet.
888
+
889
+ Parameters
890
+ ----------
891
+ c : tespy.connections.connection
892
+ Connection to perform initialisation on.
893
+
894
+ key : str
895
+ Fluid property to retrieve.
896
+
897
+ Returns
898
+ -------
899
+ val : float
900
+ Starting value for pressure/enthalpy in SI units.
901
+
902
+ .. math::
903
+
904
+ val = \begin{cases}
905
+ 5 \cdot 10^5 & \text{key = 'p'}\\
906
+ h\left(T=323.15, p=5 \cdot 10^5\right) & \text{key = 'h'}
907
+ \end{cases}
908
+ """
909
+ if key == 'p':
910
+ return 5e5
911
+ elif key == 'h':
912
+ flow = c.to_flow()
913
+ T = 50 + 273.15
914
+ return h_mix_pT(flow, T)
915
+
916
+ def initialise_target(self, c, key):
917
+ r"""
918
+ Return a starting value for pressure and enthalpy at inlet.
919
+
920
+ Parameters
921
+ ----------
922
+ c : tespy.connections.connection
923
+ Connection to perform initialisation on.
924
+
925
+ key : str
926
+ Fluid property to retrieve.
927
+
928
+ Returns
929
+ -------
930
+ val : float
931
+ Starting value for pressure/enthalpy in SI units.
932
+
933
+ .. math::
934
+
935
+ val = \begin{cases}
936
+ 5 \cdot 10^5 & \text{key = 'p'}\\
937
+ h\left(T=293.15, p=5 \cdot 10^5\right) & \text{key = 'h'}
938
+ \end{cases}
939
+ """
940
+ if key == 'p':
941
+ return 5e5
942
+ elif key == 'h':
943
+ flow = c.to_flow()
944
+ T = 20 + 273.15
945
+ return h_mix_pT(flow, T)
946
+
947
+ def calc_parameters(self):
948
+ r"""Postprocessing parameter calculation."""
949
+ self.Q.val = - self.inl[0].m.val_SI * (self.outl[0].h.val_SI -
950
+ self.inl[0].h.val_SI)
951
+ self.pr_c.val = self.outl[0].p.val_SI / self.inl[0].p.val_SI
952
+ self.e.val = self.P.val / self.outl[2].m.val_SI
953
+ self.eta.val = self.e0 / self.e.val
954
+
955
+ i = self.inl[0].to_flow()
956
+ o = self.outl[0].to_flow()
957
+ self.zeta.val = ((i[1] - o[1]) * np.pi ** 2 /
958
+ (8 * i[0] ** 2 * (v_mix_ph(i) + v_mix_ph(o)) / 2))
959
+
960
+ if self.eta_char.is_set:
961
+ # get bound errors for efficiency characteristics
962
+ expr = self.outl[2].m.val_SI / self.outl[2].m.design
963
+ self.eta_char.func.get_bound_errors(expr, self.label)
964
+
965
+ self.check_parameter_bounds()