/[ascend]/trunk/models/johnpye/fprops/cycle_plot.py
ViewVC logotype

Contents of /trunk/models/johnpye/fprops/cycle_plot.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3158 - (show annotations) (download) (as text)
Mon Jul 25 02:38:14 2016 UTC (5 years, 11 months ago) by jpye
File MIME type: text/x-python
File size: 15089 byte(s)
a better way to detect the python path for FPROPS.

1 # -*- coding: utf8 -*-
2 import extpy, sys
3 from solverreporter import *
4 from __builtin__ import *
5
6 import sys, os, os.path
7 if 'ASCENDLIBRARY' in os.environ:
8 p1 = os.path.join(os.environ['ASCENDLIBRARY'],'johnpye/fprops/python')
9 print "ADDING PATH",p1
10 sys.path.append(p1)
11 import fprops
12
13 try:
14 from pylab import *
15 except:
16 pass
17
18 #--- for (T,s) plots ---
19
20 def sat_curve(D):
21 Tt = D.T_t
22 Tc = D.T_c
23 TT = []
24 pp = []
25 ssf = []
26 ssg = []
27 for T in linspace(Tt,Tc,100):
28 # TODO this is inefficient because of repeated saturation solutions.
29 SF = D.set_Tx(T,0)
30 SG = D.set_Tx(T,1)
31 TT.append(SF.T - 273.15)
32 pp.append(SF.p)
33 ssf.append(SF.s/1.e3)
34 ssg.append(SG.s/1.e3)
35 plot(ssf,TT,"b--")
36 plot(ssg,TT,"r--")
37
38 class TSPoint:
39 def __init__(self,T,s):
40 self.T = T
41 self.s = s
42
43 def write(msg):
44 extpy.getbrowser().reporter.reportNote(msg)
45
46 def pconst(S1,S2,n):
47 """Return a set of (T,s) points between two states, with pressure held constant."""
48 D = fprops.fluid(str(S1.cd.component.getSymbolValue()))
49 out = []
50 hh = linspace(float(S1.h), float(S2.h), n)
51 for h in hh:
52 S = D.set_ph(float(S1.p), h)
53 out += [TSPoint(S.T,S.s)]
54 return out
55
56
57 def plot_Ts(SS,style='b-'):
58 xx = []
59 yy = []
60 for S in SS:
61 yy.append(float(S.T) - 273.15)
62 xx.append(float(S.s)/1.e3)
63 plot(xx,yy,style)
64
65 #--- for (T,H) plots ---
66
67 class THPoint:
68 def __init__(self,T,h,mdot = 1.):
69 self.T = T
70 self.h = h
71 self.mdot = mdot
72
73 def pconsth(S1,S2,n):
74 """Return a set of (T,H) points between two states, with pressure constant"""
75 D = fprops.fluid(str(S1.cd.component.getSymbolValue()))
76 out = []
77 hh = linspace(float(S1.h), float(S2.h), n)
78 mdot = float(S1.mdot)
79 for h in hh:
80 # TODO add try/except
81 S = D.set_ph(float(S1.p),h)
82 out += [THPoint(S.T,h * mdot)]
83 return out
84
85 def plot_TH(SS,style='b-',Href = 0):
86 xx = []
87 yy = []
88 for S in SS:
89 yy.append(float(S.T) - 273.15)
90 xx.append(((float(S.h)*float(S.mdot)) - Href)/1.e6)
91 plot(xx,yy,style)
92
93 #--- various Rankine cycle configurations ---
94
95 def cycle_plot_rankine(self):
96 """Plot T-s diagram for a simple Rankine cycle"""
97 import loading
98 loading.load_matplotlib(throw=True)
99 ioff()
100 figure()
101 hold(1)
102 D = fprops.fluid(str(self.cd.component.getSymbolValue()))
103 sat_curve(D)
104
105 boiler_curve = pconst(self.BO.inlet, self.BO.outlet,100)
106 condenser_curve = pconst(self.CO.inlet,self.CO.outlet,100)
107 SS = [self.PU.outlet, self.BO.inlet] + boiler_curve + [self.TU.inlet, self.TU.outlet] + condenser_curve + [self.CO.outlet, self.PU.outlet]
108 plot_Ts(SS)
109
110 title(unicode(r"Rankine cycle with %s" % D.name))
111 ylabel(unicode(r"T / [��C]"))
112 aa = axis(); axis([aa[0],aa[1],-100,600])
113 xlabel("s / [kJ/kg/K]")
114
115 extpy.getbrowser().reporter.reportNote("Plotting completed")
116 ion()
117 show()
118 #savefig(os.path.expanduser("~/Desktop/rankine.eps"))
119
120 def cycle_plot_rankine_reheat(self):
121 """Plot T-s diagram for a reheat Rankine cycle"""
122 import loading
123 loading.load_matplotlib(throw=True)
124 ioff()
125 figure()
126 hold(1)
127 D = fprops.fluid(str(self.cd.component.getSymbolValue()))
128 sat_curve(D)
129
130 boiler1_curve = pconst(self.BO1.inlet, self.BO1.outlet,100)
131 boiler2_curve = pconst(self.BO2.inlet, self.BO2.outlet,50)
132 condenser_curve = pconst(self.CO.inlet,self.CO.outlet,100)
133 SS = [self.PU.outlet, self.BO1.inlet] + \
134 boiler1_curve + [self.TU1.inlet, self.TU1.outlet] + \
135 boiler2_curve + [self.TU2.inlet, self.TU2.outlet] + \
136 condenser_curve + [self.CO.outlet, self.PU.outlet]
137 plot_Ts(SS)
138
139 plot_Ts(
140 [self.PU.inlet, self.BO1.inlet, self.TU1.inlet, self.BO2.inlet
141 ,self.TU2.inlet, self.CO.inlet]
142 ,'bo'
143 )
144
145 title(unicode(r"Reheat Rankine cycle with %s" % D.name))
146 ylabel(unicode(r"T / [��C]"))
147 aa = axis(); axis([aa[0],aa[1],-100,600])
148 xlabel("s / [kJ/kg/K]")
149
150 extpy.getbrowser().reporter.reportNote("Plotting completed")
151 ion()
152 show()
153 #savefig(os.path.expanduser("~/Desktop/rankine-reheat.eps"))
154
155 def cycle_plot_rankine_regen2(self):
156 """Plot T-s diagram for a regenerative Rankine cycle (bleed steam regen)"""
157 import loading
158 loading.load_matplotlib(throw=True)
159 ioff()
160 figure()
161 hold(1)
162 D = fprops.fluid(str(self.cd.component.getSymbolValue()))
163 sat_curve(D)
164
165 boiler_curve = pconst(self.BO.inlet, self.BO.outlet,100)
166 condenser_curve = pconst(self.CO.inlet,self.CO.outlet,100)
167
168 SS = [self.PU1.inlet, self.PU1.outlet] + \
169 pconst(self.HE.inlet, self.HE.outlet, 100) + \
170 [self.PU2.inlet, self.PU2.outlet] + \
171 boiler_curve + \
172 [self.TU1.inlet, self.TU1.outlet, self.TU2.outlet] + \
173 condenser_curve + [self.PU1.inlet]
174
175 plot_Ts(SS)
176 plot_Ts(
177 [self.PU1.inlet, self.PU1.outlet, self.HE.inlet, self.HE.outlet,
178 self.PU2.inlet, self.PU2.outlet, self.TU1.inlet, self.TU1.outlet,
179 self.TU2.outlet, self.PU1.inlet]
180 ,'bo'
181 )
182
183 # line for the heat exchanger
184 plot_Ts(pconst(self.HE.inlet_heat, self.HE.outlet,100),'b:')
185
186 title(unicode(r"Regenerative Rankine cycle with %s" % D.name))
187 ylabel(unicode(r"T / [��C]"))
188 aa = axis(); axis([aa[0],aa[1],-100,600])
189 xlabel("s / [kJ/kg/K]")
190
191 extpy.getbrowser().reporter.reportNote("Plotting completed")
192 ion()
193 show()
194 #savefig(os.path.expanduser("~/Desktop/regen2.eps"))
195
196
197
198 def cycle_plot_rankine_regen1(self):
199 """Plot T-s diagram for a regenerative Rankine cycle"""
200 import loading
201 loading.load_matplotlib(throw=True)
202 ioff()
203 figure()
204 hold(1)
205 D = fprops.fluid(str(self.cd.component.getSymbolValue()))
206 sat_curve(D)
207
208 boiler_curve = pconst(self.BO.inlet, self.BO.outlet,100)
209 condenser_curve = pconst(self.CO.inlet,self.CO.outlet,100)
210 he_hot = pconst(self.HE.inlet_heat, self.HE.outlet_heat,100)
211 he_cold = pconst(self.HE.inlet, self.HE.outlet,100)
212
213 SS = [self.PU.outlet] + he_cold + [self.BO.inlet] + boiler_curve + [self.TU.inlet, self.TU.outlet] + he_hot + condenser_curve + [self.PU.inlet, self.PU.outlet]
214
215 plot_Ts(SS)
216 plot_Ts(
217 [self.PU.outlet,self.BO.inlet,self.TU.inlet, self.TU.outlet
218 ,self.HE.outlet_heat, self.PU.inlet, self.PU.outlet]
219 ,'bo'
220 )
221
222 # dotted lines for the heat exchanger
223 plot_Ts([self.HE.inlet_heat, self.HE.outlet],'b:')
224 plot_Ts([self.HE.outlet_heat, self.HE.inlet],'b:')
225
226 title(unicode(r"Regenerative Rankine cycle with %s" % D.name))
227 ylabel(unicode(r"T / [��C]"))
228 aa = axis(); axis([aa[0],aa[1],-100,600])
229 xlabel("s / [kJ/kg/K]")
230
231 extpy.getbrowser().reporter.reportNote("Plotting completed")
232 ion()
233 show()
234 #savefig(os.path.expanduser("~/Desktop/regen1.eps"))
235
236
237 #--- heat exchange (T,H) plot ---
238
239 def heater_closed_plot(self):
240 """Plot T-H diagram of heat transfer in a heater_closed model"""
241 import loading
242 loading.load_matplotlib(throw=True)
243 ioff()
244 figure()
245 hold(1)
246 D = fprops.fluid(str(self.cd.component.getSymbolValue()))
247 HE = self.HE
248
249 extpy.getbrowser().reporter.reportNote("Fluid is %s" % D.name)
250
251 plot_TH(pconsth(HE.inlet_heat, HE.outlet_heat, 50),'r-',
252 Href = (float(HE.outlet_heat.h)*float(HE.outlet_heat.mdot))\
253 )
254
255 plot_TH(pconsth(HE.inlet, HE.outlet, 50),'b-',
256 Href = (float(HE.inlet.h)*float(HE.inlet.mdot))\
257 )
258
259 title(unicode(r"Closed feedwater heater with %s" % D.name))
260 ylabel(unicode(r"T / [��C]"))
261 xlabel("H / [MW]")
262
263 extpy.getbrowser().reporter.reportNote("Plotting completed")
264 ion()
265 show()
266 #savefig(os.path.expanduser("~/Desktop/heater_closed.eps"))
267
268 #--- the big one: a combined-cycle GT ---
269
270 def cycle_plot_ccgt(self):
271 """Plot T-s diagram for combined-cycle gas turbine"""
272 import loading
273 loading.load_matplotlib(throw=True)
274 ioff()
275 figure()
276
277 D = fprops.fluid(str(self.cd_rankine.component.getSymbolValue()))
278
279 # plot gas turbine cycle
280 SS = [self.GC.inlet, self.GC.outlet, self.GT.inlet, self.GT.outlet, self.HE.inlet, self.HE.outlet, self.GC.inlet]
281 plot_Ts(SS,'g-')
282 plot_Ts(SS,'go')
283 hold(1)
284
285 sat_curve(D)
286
287 boiler_curve = pconst(self.HE.inlet_cold,self.HE.outlet_cold,100)
288 condenser_curve = pconst(self.CO.inlet,self.CO.outlet,100)
289 SS2 = [self.PU.outlet, self.HE.inlet_cold] + boiler_curve + [self.HE.outlet_cold, self.TU.inlet, self.TU.outlet, self.CO.inlet] + condenser_curve + [self.CO.outlet, self.PU.inlet, self.PU.outlet]
290 plot_Ts(SS2)
291 plot_Ts([self.PU.outlet, self.HE.inlet_cold,self.HE.outlet_cold, self.TU.inlet, self.TU.outlet, self.CO.inlet,self.CO.outlet, self.PU.inlet, self.PU.outlet],'bo')
292
293 title(unicode(r"Combined cycle with air and %s" % D.name))
294 ylabel(unicode(r"T / [��C]"))
295 xlabel("s / [kJ/kg/K]")
296
297 extpy.getbrowser().reporter.reportNote("Plotting completed")
298 ion()
299 show()
300 #savefig(os.path.expanduser("~/Desktop/ccgt.eps"))
301 #savefig(os.path.expanduser("~/Desktop/ccgt.png"))
302
303
304 #--- simple gas turbine models ---
305
306
307 def cycle_plot_brayton_regen(self):
308 """Plot T-s diagran for regenerative gas turbine"""
309 import loading
310 loading.load_matplotlib(throw=True)
311 ioff()
312 figure()
313
314 D = fprops.fluid(str(self.cd.component.getSymbolValue()))
315 sat_curve(D)
316
317 # plot gas turbine cycle
318 regen_cold_curve = pconst(self.RE.inlet, self.RE.outlet, 50)
319 burner_curve = pconst(self.BO.inlet, self.BO.outlet,50)
320 regen_hot_curve = pconst(self.RE.inlet_hot, self.RE.outlet_hot, 50)
321 diss_curve = pconst(self.CO.inlet, self.CO.outlet,50)
322
323 SS = [self.PU.inlet, self.PU.outlet, self.RE.inlet] + regen_cold_curve + [self.RE.outlet, self.BO.inlet] + burner_curve + [self.BO.outlet, self.TU.inlet, self.TU.outlet,self.RE.inlet_hot] + regen_hot_curve + [self.RE.outlet_hot, self.CO.inlet] + diss_curve + [self.CO.outlet,self.PU.inlet]
324
325 plot_Ts(SS,'g-')
326
327 SS2 = [self.PU.inlet, self.PU.outlet, self.RE.inlet, self.RE.outlet, self.BO.inlet, self.BO.outlet, self.TU.inlet, self.TU.outlet,self.RE.inlet_hot, self.RE.outlet_hot, self.CO.inlet, self.CO.outlet,self.PU.inlet]
328 plot_Ts(SS2,'go')
329
330 SS3 = [self.RE.inlet, self.RE.outlet_hot]
331 plot_Ts(SS3,'g--')
332
333 SS4 = [self.RE.outlet, self.RE.inlet_hot]
334 plot_Ts(SS4,'g--')
335
336 hold(1)
337
338 title(unicode(r"Regenerative Brayton cycle"))
339 ylabel(unicode(r"T / [��C]"))
340 xlabel("s / [kJ/kg/K]")
341
342 extpy.getbrowser().reporter.reportNote("Plotting completed")
343 ion()
344 show()
345 #savefig(os.path.expanduser("~/Desktop/brayton_regen.eps"))
346
347
348 def cycle_plot_brayton_reheat_regen_intercool(self):
349 """Plot T-s diagram for reheat-regenerative gas turbine"""
350 import loading
351 loading.load_matplotlib(throw=True)
352 ioff()
353 figure()
354 hold(1)
355 D = fprops.fluid(str(self.cd.component.getSymbolValue()),str(self.cd.type.getSymbolValue()))
356 sat_curve(D)
357
358 # add some dots for the points in the cycle
359 seq = "CO1.inlet DI2.inlet CO2.inlet RE.inlet BU1.inlet BU2.inlet TU1.inlet BU2.inlet TU2.inlet RE.inlet_hot DI1.inlet".split(" ")
360 lalign = "TU2.inlet RE.inlet_hot DI1.inlet DI2.inlet CO1.inlet".split(" ")
361 SS1 = []; SS1a = []
362 for s in seq:
363 print "looking at '%s'"%s
364 p = reduce(getattr,s.split("."),self)
365 SS1.append(p)
366 SS1a.append((p,s))
367 plot_Ts(SS1,'go')
368
369 print "ANNOTATIONS"
370 for s in SS1a:
371 align = "right"
372 if s[1] in lalign:
373 align = "left"
374 annotate(s[1]+" ", xy =(float(s[0].s)/1.e3,float(s[0].T) - 273.15)
375 ,horizontalalignment=align
376 )
377
378 # plot the cycle with smooth curves
379 BU1_curve = pconst(self.BU1.inlet, self.BU1.outlet,30)
380 BU2_curve = pconst(self.BU2.inlet, self.BU2.outlet,20)
381 DI1_curve = pconst(self.DI1.inlet,self.DI1.outlet,20)
382 DI2_curve = pconst(self.DI2.inlet,self.DI2.outlet,20)
383 REH_curve = pconst(self.RE.inlet_hot,self.RE.outlet_hot,50)
384 REL_curve = pconst(self.RE.inlet,self.RE.outlet,50)
385
386 SS2 = [self.CO1.inlet, self.CO1.outlet] + DI2_curve + [self.CO2.inlet, self.CO2.outlet] + REL_curve + BU1_curve + [self.TU1.inlet, self.TU1.outlet] + BU2_curve + [self.TU2.inlet, self.TU2.outlet] + REH_curve + DI1_curve + [self.CO1.inlet]
387 plot_Ts(SS2,'g-')
388
389 SS3 = [self.RE.inlet, self.RE.outlet_hot]
390 plot_Ts(SS3,'g--')
391
392 SS4 = [self.RE.outlet, self.RE.inlet_hot]
393 plot_Ts(SS4,'g--')
394
395 title(unicode(r"Reheat Regenerative Brayton cycle with Intercooling"))
396 ylabel(unicode(r"T / [��C]"))
397 xlabel("s / [kJ/kg/K]")
398
399 extpy.getbrowser().reporter.reportNote("Plotting completed")
400 ion()
401 show()
402
403
404 def cycle_plot_brayton_split(self):
405 """Plot T-s diagran for split-regeneration gas turbine"""
406 import loading
407 loading.load_matplotlib(throw=True)
408 ioff()
409 figure()
410 hold(1)
411 D = fprops.fluid(str(self.cd.component.getSymbolValue()),str(self.cd.type.getSymbolValue()))
412 sat_curve(D)
413
414 # add some dots for the points in the cycle
415 # seq = "CO2.inlet HEL.inlet HEL.outlet HEH.inlet BO.inlet TU.inlet HEH.inlet_hot HEL.inlet_hot CO1.inlet CO1.outlet".split(" ")
416 seq = "CO2.inlet HEL.inlet HEH.inlet BO.inlet TU.inlet HEH.inlet_hot HEH.outlet_hot CO1.inlet".split(" ")
417 lalign = "CO1.inlet HEH.outlet_hot ".split(" ")
418 SS1 = []; SS1a = []
419 for s in seq:
420 print "looking at '%s'"%s
421 p = reduce(getattr,s.split("."),self)
422 SS1.append(p)
423 SS1a.append((p,s))
424 plot_Ts(SS1,'go')
425
426 print "ANNOTATIONS"
427 for s in SS1a:
428 align = "right"
429 if s[1] in lalign:
430 align = "left"
431 annotate(s[1]+" ", xy =(float(s[0].s)/1.e3,float(s[0].T) - 273.15)
432 ,horizontalalignment=align
433 )
434
435 SS2 = pconst(self.DI.inlet, self.DI.outlet, 50) + [self.CO2.inlet,self.CO2.outlet] + pconst(self.HEL.inlet,self.HEH.outlet,50) + pconst(self.BO.inlet,self.BO.outlet,50) + [self.TU.inlet, self.TU.outlet] + pconst(self.HEH.inlet_hot,self.HEL.outlet_hot,50) + [self.CO1.inlet,self.CO1.outlet]
436 plot_Ts(SS2,'g-')
437
438 SS3 = [self.HEL.inlet, self.HEL.outlet_hot]
439 plot_Ts(SS3,'g--')
440 SS4 = [self.HEL.outlet, self.HEL.inlet_hot]
441 plot_Ts(SS4,'g--')
442
443 SS5 = [self.HEH.inlet, self.HEH.outlet_hot]
444 plot_Ts(SS5,'g--')
445 SS6 = [self.HEH.outlet, self.HEH.inlet_hot]
446 plot_Ts(SS6,'g--')
447
448
449 title(unicode(r"Split Regenerative Brayton cycle"))
450 ylabel(unicode(r"T / [��C]"))
451 xlabel("s / [kJ/kg/K]")
452
453 extpy.getbrowser().reporter.reportNote("Plotting completed")
454 ion()
455 show()
456 savefig(os.path.expanduser("~/Desktop/brayton__split_regen.eps"))
457
458
459
460
461 #--- air-to-stream heat exchanger plot ---
462
463 def air_stream_heat_exchanger_plot(self):
464 """Plot T-H diagram of heat transfer in a heater_closed model"""
465 import loading
466 loading.load_matplotlib(throw=True)
467 ioff()
468 figure()
469 hold(1)
470 D = fprops.fluid(str(self.cd_cold.component.getSymbolValue()))
471
472 n = self.n.getIntValue()
473 extpy.getbrowser().reporter.reportNote("Fluid is %s" % D.name)
474
475 # hot side is the air, calculated in the model
476 plot_TH( [self.H[i] for i in range(1+int(n))],'r-',\
477 Href = (float(self.outlet.h)*float(self.outlet.mdot))\
478 )
479
480 plot_TH(pconsth(self.inlet_cold, self.outlet_cold, 50),'b-',
481 Href = (float(self.inlet_cold.h)*float(self.inlet_cold.mdot))\
482 )
483
484 title(unicode(r"Combined-cycle air-%s heat exchanger" % D.name))
485 ylabel(unicode(r"T / [��C]"))
486 xlabel("H / [MW]")
487
488 extpy.getbrowser().reporter.reportNote("Plotting completed")
489 ion()
490 show()
491 #savefig(os.path.expanduser("~/Desktop/air_stream_heatex.eps"))
492
493
494 extpy.registermethod(cycle_plot_rankine)
495 extpy.registermethod(cycle_plot_rankine_reheat)
496 extpy.registermethod(cycle_plot_rankine_regen1)
497 extpy.registermethod(cycle_plot_rankine_regen2)
498 extpy.registermethod(cycle_plot_brayton_regen)
499 extpy.registermethod(cycle_plot_brayton_reheat_regen_intercool)
500 extpy.registermethod(cycle_plot_brayton_split)
501 extpy.registermethod(cycle_plot_ccgt)
502
503 extpy.registermethod(heater_closed_plot)
504 extpy.registermethod(air_stream_heat_exchanger_plot)
505
506 #the above method can be called using "EXTERNAL fourbarplot(SELF)" in ASCEND.

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22