Reactors with walls and heat transfer#

Two reactors connected with a piston, with heat loss to the environment

This script simulates the following situation. A closed cylinder with volume 2 m^3 is divided into two equal parts by a massless piston that moves with speed proportional to the pressure difference between the two sides. It is initially held in place in the middle. One side is filled with 1000 K argon at 20 atm, and the other with a combustible 500 K methane/air mixture at 0.1 atm (\(\phi = 1.1\)). At \(t = 0\), the piston is released and begins to move due to the large pressure difference, compressing and heating the methane/air mixture, which eventually explodes. At the same time, the argon cools as it expands. The piston allows heat transfer between the reactors and some heat is lost through the outer cylinder walls to the environment.

Note that this simulation, being zero-dimensional, takes no account of shock wave propagation. It is somewhat artificial, but nevertheless instructive.

Requires: cantera >= 2.5.0, matplotlib >= 2.0

Tags: combustion reactor network plotting

reactor2
finished setup, begin solution...
0 0.0004 500.0
1 0.0008 506.904245195628
2 0.0012000000000000001 513.6996289344827
3 0.0016 520.4177565777705
4 0.002 527.0847257436436
5 0.0024000000000000002 533.7226597018098
6 0.0028000000000000004 540.3507678488164
7 0.0032000000000000006 546.9860943041527
8 0.0036000000000000008 553.6440683989028
9 0.004000000000000001 560.3389171973483
10 0.004400000000000001 567.0839831588197
11 0.004800000000000001 573.8919751737893
12 0.0052000000000000015 580.7751699610214
13 0.005600000000000002 587.7455793825312
14 0.006000000000000002 594.8150917356439
15 0.006400000000000002 601.9955937314699
16 0.006800000000000002 609.2990784860789
17 0.007200000000000002 616.7377428857249
18 0.007600000000000003 624.3240765943377
19 0.008000000000000002 632.070945073051
20 0.008400000000000001 639.9916683636555
21 0.0088 648.1000946332891
22 0.0092 656.4106698035927
23 0.0096 664.9385030637334
24 0.009999999999999998 673.6994252325231
25 0.010399999999999998 682.7100400756962
26 0.010799999999999997 691.9877648847621
27 0.011199999999999996 701.5508520754502
28 0.011599999999999996 711.4183914643152
29 0.011999999999999995 721.6102820703838
30 0.012399999999999994 732.1471586070222
31 0.012799999999999994 743.0502595214078
32 0.013199999999999993 754.3412143440128
33 0.013599999999999992 766.0417230111441
34 0.013999999999999992 778.1730874568589
35 0.014399999999999991 790.7555524709268
36 0.01479999999999999 803.8073945553786
37 0.01519999999999999 817.3436889867266
38 0.015599999999999989 831.3746627402775
39 0.01599999999999999 845.903553193068
40 0.01639999999999999 860.9238781032087
41 0.016799999999999992 876.4160426438538
42 0.017199999999999993 892.3432874850063
43 0.017599999999999994 908.6470771909617
44 0.017999999999999995 925.2422465088771
45 0.018399999999999996 942.0125186289355
46 0.018799999999999997 958.8073481228084
47 0.0192 975.4414592903506
48 0.0196 991.6986421733284
49 0.02 1007.3419348561143
50 0.0204 1022.1320622282919
51 0.020800000000000003 1035.838976863915
52 0.021200000000000004 1048.2685328842222
53 0.021600000000000005 1059.2834913008342
54 0.022000000000000006 1068.8161527362493
55 0.022400000000000007 1076.8705452263148
56 0.022800000000000008 1083.5141225406403
57 0.02320000000000001 1088.8622166084156
58 0.02360000000000001 1093.059920758587
59 0.02400000000000001 1096.2650724322662
60 0.024400000000000012 1098.6344608074348
61 0.024800000000000013 1100.3142080572777
62 0.025200000000000014 1101.4342462632358
63 0.025600000000000015 1102.1061040520228
64 0.026000000000000016 1102.422986325641
65 0.026400000000000017 1102.461198795273
66 0.026800000000000018 1102.2822413912982
67 0.02720000000000002 1101.9350822122574
68 0.02760000000000002 1101.4583564890129
69 0.02800000000000002 1100.8823244238783
70 0.028400000000000022 1100.230558853814
71 0.028800000000000023 1099.521346579542
72 0.029200000000000025 1098.768828978227
73 0.029600000000000026 1097.9839163231723
74 0.030000000000000027 1097.1750121673685
75 0.030400000000000028 1096.3485823900855
76 0.03080000000000003 1095.5095991543092
77 0.03120000000000003 1094.661887016425
78 0.03160000000000003 1093.8083887514608
79 0.03200000000000003 1092.9513715671033
80 0.032400000000000026 1092.0925863388172
81 0.032800000000000024 1091.2333891989165
82 0.03320000000000002 1090.3748354107302
83 0.03360000000000002 1089.517751151955
84 0.034000000000000016 1088.6627886408664
85 0.034400000000000014 1087.810468392459
86 0.03480000000000001 1086.9612115922237
87 0.03520000000000001 1086.115364952457
88 0.03560000000000001 1085.273219785858
89 0.036000000000000004 1084.4350266806093
90 0.0364 1083.6010068144697
91 0.0368 1082.7713606919283
92 0.0372 1081.9462749811964
93 0.037599999999999995 1081.125927842395
94 0.03799999999999999 1080.3104931553387
95 0.03839999999999999 1079.5001439114938
96 0.03879999999999999 1078.6950549946894
97 0.039199999999999985 1077.8954055019078
98 0.03959999999999998 1077.1013807390596
99 0.03999999999999998 1076.3131739923926
100 0.04039999999999998 1075.5309881550174
101 0.040799999999999975 1074.7550372724238
102 0.04119999999999997 1073.9855480534284
103 0.04159999999999997 1073.2227614030298
104 0.04199999999999997 1072.4669340055152
105 0.042399999999999965 1071.7183399950898
106 0.04279999999999996 1070.9772727513184
107 0.04319999999999996 1070.2440468442608
108 0.04359999999999996 1069.5190001735414
109 0.043999999999999956 1068.80249633535
110 0.04439999999999995 1068.0949272401235
111 0.04479999999999995 1067.3967160566228
112 0.04519999999999995 1066.70832050624
113 0.045599999999999946 1066.0302365740527
114 0.045999999999999944 1065.3630027183224
115 0.04639999999999994 1064.7072046367969
116 0.04679999999999994 1064.0634806986648
117 0.047199999999999936 1063.43252815375
118 0.047599999999999934 1062.815110255642
119 0.04799999999999993 1062.212064437853
120 0.04839999999999993 1061.6243117972829
121 0.04879999999999993 1061.0528680653326
122 0.049199999999999924 1060.4988563851427
123 0.04959999999999992 1059.9635222518991
124 0.04999999999999992 1059.4482510726145
125 0.05039999999999992 1058.9545889131714
126 0.050799999999999915 1058.4842670538128
127 0.05119999999999991 1058.0392313109562
128 0.05159999999999991 1057.621677222174
129 0.05199999999999991 1057.2340925006424
130 0.052399999999999905 1056.8793085734915
131 0.0527999999999999 1056.5605637223284
132 0.0531999999999999 1056.2815808405078
133 0.0535999999999999 1056.046664063737
134 0.053999999999999895 1055.86081992604
135 0.05439999999999989 1055.7299106870119
136 0.05479999999999989 1055.660850399959
137 0.05519999999999989 1055.6618584737641
138 0.055599999999999886 1055.7427917818427
139 0.05599999999999988 1055.915585441126
140 0.05639999999999988 1056.1948470185541
141 0.05679999999999988 1056.598671139773
142 0.057199999999999876 1057.1497776829904
143 0.057599999999999874 1057.8771370962504
144 0.05799999999999987 1058.8183488092832
145 0.05839999999999987 1060.023222486586
146 0.058799999999999866 1061.5593528992756
147 0.059199999999999864 1063.5211462686702
148 0.05959999999999986 1066.0451455722857
149 0.05999999999999986 1069.3376154537211
150 0.06039999999999986 1073.7279970523962
151 0.060799999999999854 1079.782966630689
152 0.06119999999999985 1088.5841349818281
153 0.06159999999999985 1102.5486399217843
154 0.06199999999999985 1128.7684927242767
155 0.062399999999999844 1206.0917168834128
156 0.06279999999999984 2864.3800905956996
157 0.06319999999999984 2781.347463653076
158 0.06359999999999984 2733.119603972094
159 0.06399999999999983 2699.4416738716936
160 0.06439999999999983 2674.0983606269724
161 0.06479999999999983 2654.151640766343
162 0.06519999999999983 2637.961380183327
163 0.06559999999999983 2624.516908854965
164 0.06599999999999982 2613.1515830759563
165 0.06639999999999982 2603.4030062850156
166 0.06679999999999982 2594.93777063046
167 0.06719999999999982 2587.5079720564936
168 0.06759999999999981 2580.9246408131935
169 0.06799999999999981 2575.0407380702354
170 0.06839999999999981 2569.739889104393
171 0.0687999999999998 2564.9286540096787
172 0.0691999999999998 2560.531089758902
173 0.0695999999999998 2556.4848304711154
174 0.0699999999999998 2552.7382004055194
175 0.0703999999999998 2549.248054179095
176 0.0707999999999998 2545.9781292370217
177 0.07119999999999979 2542.8977731156324
178 0.07159999999999979 2539.9809464096434
179 0.07199999999999979 2537.2054331731088
180 0.07239999999999978 2534.552208347025
181 0.07279999999999978 2532.004927020581
182 0.07319999999999978 2529.5495040770193
183 0.07359999999999978 2527.1737742478053
184 0.07399999999999977 2524.8672066389563
185 0.07439999999999977 2522.620668074696
186 0.07479999999999977 2520.4262245321956
187 0.07519999999999977 2518.2769738383035
188 0.07559999999999976 2516.1669039339063
189 0.07599999999999976 2514.0907723676855
190 0.07639999999999976 2512.044003635837
191 0.07679999999999976 2510.022600992847
192 0.07719999999999975 2508.0230707310916
193 0.07759999999999975 2506.042357704953
194 0.07799999999999975 2504.0777875039894
195 0.07839999999999975 2502.1270192284683
196 0.07879999999999975 2500.188002779143
197 0.07919999999999974 2498.258942212721
198 0.07959999999999974 2496.338263868387
199 0.07999999999999974 2494.4245888335863
200 0.08039999999999974 2492.5167081350905
201 0.08079999999999973 2490.613562679915
202 0.08119999999999973 2488.714223549451
203 0.08159999999999973 2486.817877262855
204 0.08199999999999973 2484.9238110485603
205 0.08239999999999972 2483.0313985947514
206 0.08279999999999972 2481.1400920027645
207 0.08319999999999972 2479.249412718113
208 0.08359999999999972 2477.3589408189105
209 0.08399999999999971 2475.4683088782404
210 0.08439999999999971 2473.577195567698
211 0.08479999999999971 2471.685320014234
212 0.0851999999999997 2469.7924370835863
213 0.0855999999999997 2467.8983328311483
214 0.0859999999999997 2466.0028206218844
215 0.0863999999999997 2464.1057381590163
216 0.0867999999999997 2462.2069444147915
217 0.0871999999999997 2460.306317118735
218 0.08759999999999969 2458.4037503880645
219 0.08799999999999969 2456.499152978912
220 0.08839999999999969 2454.592446393113
221 0.08879999999999968 2452.683563499317
222 0.08919999999999968 2450.7724469264035
223 0.08959999999999968 2448.859048232977
224 0.08999999999999968 2446.9433267243107
225 0.09039999999999968 2445.025248625072
226 0.09079999999999967 2443.1047861393085
227 0.09119999999999967 2441.1819168147445
228 0.09159999999999967 2439.256622940442
229 0.09199999999999967 2437.328891187721
230 0.09239999999999966 2435.398712071902
231 0.09279999999999966 2433.466079391319
232 0.09319999999999966 2431.530989907517
233 0.09359999999999966 2429.5934430879556
234 0.09399999999999965 2427.653440826959
235 0.09439999999999965 2425.71098725333
236 0.09479999999999965 2423.7660883978233
237 0.09519999999999965 2421.8187520629313
238 0.09559999999999964 2419.8689875916252
239 0.09599999999999964 2417.916805853448
240 0.09639999999999964 2415.962219051426
241 0.09679999999999964 2414.005240551137
242 0.09719999999999963 2412.0458848495914
243 0.09759999999999963 2410.0841675023335
244 0.09799999999999963 2408.120105025969
245 0.09839999999999963 2406.1537147916115
246 0.09879999999999962 2404.1850150146774
247 0.09919999999999962 2402.214024674715
248 0.09959999999999962 2400.240763398598
249 0.09999999999999962 2398.2652515067257
250 0.10039999999999961 2396.287510074357
251 0.10079999999999961 2394.3075608407394
252 0.10119999999999961 2392.3254258514035
253 0.10159999999999961 2390.3411279154157
254 0.1019999999999996 2388.354690219378
255 0.1023999999999996 2386.366136446561
256 0.1027999999999996 2384.375490957647
257 0.1031999999999996 2382.3827782675726
258 0.1035999999999996 2380.388023584314
259 0.10399999999999959 2378.3912523785953
260 0.10439999999999959 2376.392490594642
261 0.10479999999999959 2374.391764508049
262 0.10519999999999959 2372.389100808341
263 0.10559999999999958 2370.3845265104915
264 0.10599999999999958 2368.3780689621535
265 0.10639999999999958 2366.3697558088147
266 0.10679999999999958 2364.359615072393
267 0.10719999999999957 2362.3476751280964
268 0.10759999999999957 2360.333964520654
269 0.10799999999999957 2358.3185120584694
270 0.10839999999999957 2356.301346877193
271 0.10879999999999956 2354.2824984346007
272 0.10919999999999956 2352.261996324185
273 0.10959999999999956 2350.239870361657
274 0.10999999999999956 2348.216150709134
275 0.11039999999999955 2346.1908676150215
276 0.11079999999999955 2344.164051462529
277 0.11119999999999955 2342.135732943281
278 0.11159999999999955 2340.10594302676
279 0.11199999999999954 2338.0747126435044
280 0.11239999999999954 2336.042072676586
281 0.11279999999999954 2334.0080546543722
282 0.11319999999999954 2331.9726902447214
283 0.11359999999999953 2329.9360104126336
284 0.11399999999999953 2327.898047156544
285 0.11439999999999953 2325.858831988964
286 0.11479999999999953 2323.818396852288
287 0.11519999999999953 2321.7767736032606
288 0.11559999999999952 2319.7339941925425
289 0.11599999999999952 2317.6900906249903
290 0.11639999999999952 2315.6450949317905
291 0.11679999999999952 2313.599039200062
292 0.11719999999999951 2311.5519555535725
293 0.11759999999999951 2309.5038760619723
294 0.11799999999999951 2307.4548328298883
295 0.1183999999999995 2305.4048579911964
296 0.1187999999999995 2303.3539836282625
297 0.1191999999999995 2301.3022418060177
298 0.1195999999999995 2299.2496645620954
299 0.1199999999999995 2297.1962838606228
Output written to file piston.csv
Directory: /home/runner/work/cantera/cantera/build/doc/samples/python/reactors

import sys
import os
import csv

import cantera as ct

# First create each gas needed, and a reactor or reservoir for each one.

# create an argon gas object and set its state
ar = ct.Solution('air.yaml')
ar.TPX = 1000.0, 20.0 * ct.one_atm, "AR:1"

# create a reactor to represent the side of the cylinder filled with argon
r1 = ct.IdealGasReactor(ar)

# create a reservoir for the environment, and fill it with air.
env = ct.Reservoir(ct.Solution('air.yaml'))

# use GRI-Mech 3.0 for the methane/air mixture, and set its initial state
gas = ct.Solution('gri30.yaml')
gas.TP = 500.0, 0.2 * ct.one_atm
gas.set_equivalence_ratio(1.1, 'CH4:1.0', 'O2:2, N2:7.52')

# create a reactor for the methane/air side
r2 = ct.IdealGasReactor(gas)

# Now couple the reactors by defining common walls that may move (a piston) or
# conduct heat

# add a flexible wall (a piston) between r2 and r1
w = ct.Wall(r2, r1, A=1.0, K=0.5e-4, U=100.0)

# heat loss to the environment. Heat loss always occur through walls, so we
# create a wall separating r2 from the environment, give it a non-zero area,
# and specify the overall heat transfer coefficient through the wall.
w2 = ct.Wall(r2, env, A=1.0, U=500.0)

sim = ct.ReactorNet([r1, r2])

# Now the problem is set up, and we're ready to solve it.
print('finished setup, begin solution...')

time = 0.0
n_steps = 300
output_data = []
states1 = ct.SolutionArray(ar, extra=['t', 'V'])
states2 = ct.SolutionArray(gas, extra=['t', 'V'])

for n in range(n_steps):
    time += 4.e-4
    print(n, time, r2.T)
    sim.advance(time)
    states1.append(r1.thermo.state, t=time, V=r1.volume)
    states2.append(r2.thermo.state, t=time, V=r2.volume)
    output_data.append(
        [time, r1.thermo.T, r1.thermo.P, r1.volume, r2.thermo.T,
         r2.thermo.P, r2.volume]
    )

with open('piston.csv', 'w', newline="") as outfile:
    csvfile = csv.writer(outfile)
    csvfile.writerow(
        ['time (s)', 'T1 (K)', 'P1 (Bar)', 'V1 (m3)', 'T2 (K)',
         'P2 (Bar)', 'V2 (m3)']
    )
    csvfile.writerows(output_data)

print('Output written to file piston.csv')
print('Directory: '+os.getcwd())

if '--plot' in sys.argv:
    import matplotlib.pyplot as plt
    plt.clf()
    plt.subplot(2, 2, 1)
    h = plt.plot(states1.t, states1.T, 'g-', states2.t, states2.T, 'b-')
    # plt.legend(['Reactor 1','Reactor 2'], 2)
    plt.xlabel('Time (s)')
    plt.ylabel('Temperature (K)')

    plt.subplot(2, 2, 2)
    plt.plot(states1.t, states1.P / 1e5, 'g-', states2.t, states2.P / 1e5, 'b-')
    # plt.legend(['Reactor 1','Reactor 2'], 2)
    plt.xlabel('Time (s)')
    plt.ylabel('Pressure (Bar)')

    plt.subplot(2, 2, 3)
    plt.plot(states1.t, states1.V, 'g-', states2.t, states2.V, 'b-')
    # plt.legend(['Reactor 1','Reactor 2'], 2)
    plt.xlabel('Time (s)')
    plt.ylabel('Volume (m$^3$)')

    plt.figlegend(h, ['Reactor 1', 'Reactor 2'], loc='lower right')
    plt.tight_layout()
    plt.show()
else:
    print("To view a plot of these results, run this script with the option --plot")

Total running time of the script: (0 minutes 0.534 seconds)

Gallery generated by Sphinx-Gallery