-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGas_model.pyw
718 lines (515 loc) · 19.1 KB
/
Gas_model.pyw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
import sys, re
import PyQt5
from PyQt5.QtWidgets import *
from PyQt5 import uic
from PyQt5.QtCore import pyqtSlot, QDate, Qt
from PyQt5.QtGui import QIcon, QPixmap, QFont, QImage
import numpy as np
import matplotlib.pyplot as plt
import random
from matplotlib.animation import FuncAnimation
import matplotlib.colors
import matplotlib.image as mpimg
from mpl_toolkits.axes_grid1 import AxesGrid
import matplotlib.patches as mpatches
from scipy import stats
import time
import os
from shutil import copyfile
from Gas_classes import *
dir_principal = os.getcwd()
carpeta_data = dir_principal + '\Data'
if not os.path.exists(carpeta_data): os.mkdir(carpeta_data)
os.chdir(carpeta_data)
create_database()
class Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
os.chdir(carpeta_data)
uic.loadUi('main.ui', self)
os.chdir(dir_principal)
self.showMaximized()
#Animation and simulations
self.simulation.clicked.connect(self.fer_simu)
self.animation.clicked.connect(self.fer_anim)
#Save files
self.save_files = Save_files()
self.save_pressure.clicked.connect(self.obrir_save_files_pressure)
self.save_cm.clicked.connect(self.obrir_save_files_cm)
self.save_velocity_plot.clicked.connect(self.obrir_save_files_velocity_plot)
self.save_velocity_hist.clicked.connect(self.obrir_save_files_velocity_hist)
self.save_pv_plot.clicked.connect(self.obrir_save_files_pv_plot)
self.save_expansion_V.clicked.connect(self.obrir_save_files_expansion_V)
self.save_expansion_t.clicked.connect(self.obrir_save_files_expansion_time)
self.save_pt_plot.clicked.connect(self.obrir_save_files_pt_plot)
self.save_heating_T.clicked.connect(self.obrir_save_files_heating_T)
self.save_heating_t.clicked.connect(self.obrir_save_files_heating_t)
self.save_kt.clicked.connect(self.obrir_save_files_kt)
#Plots
self.pressure.clicked.connect(self.veure_grafic_pressure)
self.cm.clicked.connect(self.veure_grafic_CM)
self.velocity_plot.clicked.connect(self.veure_grafic_velocity)
self.velocity_hist.clicked.connect(self.veure_grafic_velocity_hist)
self.pv_plot.clicked.connect(self.veure_grafic_pv_diagram)
self.expansion_V.clicked.connect(self.veure_grafic_expansion_volume)
self.expansion_t.clicked.connect(self.veure_grafic_expansion_time)
self.pt_plot.clicked.connect(self.veure_grafic_pt_diagram)
self.heating_T.clicked.connect(self.veure_grafic_heating_temperature)
self.heating_t.clicked.connect(self.veure_grafic_heating_time)
self.k_T_plot.clicked.connect(self.veure_grafic_kT)
#L evolution
self.pv_diagram.clicked.connect(self.P_V_diagram)
self.expansion.clicked.connect(self.isothermal_expansion)
#T evolution
self.pt_diagram.clicked.connect(self.P_T_diagram)
self.heating.clicked.connect(self.isochoric_heating)
#kT(T)
self.calculate_kt.clicked.connect(self.kT_T)
def obrir_save_files_pressure(self):
self.save_files.saveFileDialog('Pressure')
def obrir_save_files_cm(self):
self.save_files.saveFileDialog('CM_plots')
def obrir_save_files_velocity_plot(self):
self.save_files.saveFileDialog('Velocity')
def obrir_save_files_velocity_hist(self):
self.save_files.saveFileDialog('Velocity_hist')
def obrir_save_files_pv_plot(self):
self.save_files.saveFileDialog('PV_diagram')
def obrir_save_files_expansion_V(self):
self.save_files.saveFileDialog('Expansion_volume')
def obrir_save_files_expansion_time(self):
self.save_files.saveFileDialog('Expansion_time')
def obrir_save_files_pt_plot(self):
self.save_files.saveFileDialog('PT_diagram')
def obrir_save_files_heating_T(self):
self.save_files.saveFileDialog('Heating_temperature')
def obrir_save_files_heating_t(self):
self.save_files.saveFileDialog('Heating_time')
def obrir_save_files_kt(self):
self.save_files.saveFileDialog('k_T(T)')
def veure_grafic_pressure(self):
self.imageLabel.clear()
filename = 'Pressure.png'
os.chdir(carpeta_data)
if os.path.exists(filename):
image = QImage(filename)
self.imageLabel.setPixmap(QPixmap.fromImage(image))
else:
QMessageBox.warning(self, 'Warning!', 'You must first simulate the system!')
def veure_grafic_CM(self):
self.imageLabel.clear()
filename = 'CM_plots.png'
os.chdir(carpeta_data)
if os.path.exists(filename):
image = QImage(filename)
self.imageLabel.setPixmap(QPixmap.fromImage(image))
else:
QMessageBox.warning(self, 'Warning!', 'You must first simulate the system!')
def veure_grafic_velocity(self):
self.imageLabel.clear()
filename = 'Velocity.png'
os.chdir(carpeta_data)
if os.path.exists(filename):
image = QImage(filename)
self.imageLabel.setPixmap(QPixmap.fromImage(image))
else:
QMessageBox.warning(self, 'Warning!', 'You must first simulate the real gas system!')
def veure_grafic_velocity_hist(self):
self.imageLabel.clear()
filename = 'Velocity_hist.png'
os.chdir(carpeta_data)
if os.path.exists(filename):
image = QImage(filename)
self.imageLabel.setPixmap(QPixmap.fromImage(image))
else:
QMessageBox.warning(self, 'Warning!', 'You must first simulate the real gas system!')
def veure_grafic_pv_diagram(self):
self.imageLabel.clear()
filename = 'PV_diagram.png'
os.chdir(carpeta_data)
if os.path.exists(filename):
image = QImage(filename)
self.imageLabel.setPixmap(QPixmap.fromImage(image))
else:
QMessageBox.warning(self, 'Warning!', 'You must first simulate the system!')
def veure_grafic_expansion_volume(self):
self.imageLabel.clear()
filename = 'Expansion_volume.png'
os.chdir(carpeta_data)
if os.path.exists(filename):
image = QImage(filename)
self.imageLabel.setPixmap(QPixmap.fromImage(image))
else:
QMessageBox.warning(self, 'Warning!', 'You must first simulate the system!')
def veure_grafic_expansion_time(self):
self.imageLabel.clear()
filename = 'Expansion_time.png'
os.chdir(carpeta_data)
if os.path.exists(filename):
image = QImage(filename)
self.imageLabel.setPixmap(QPixmap.fromImage(image))
else:
QMessageBox.warning(self, 'Warning!', 'You must first simulate the system!')
def veure_grafic_pt_diagram(self):
self.imageLabel.clear()
filename = 'PT_diagram.png'
os.chdir(carpeta_data)
if os.path.exists(filename):
image = QImage(filename)
self.imageLabel.setPixmap(QPixmap.fromImage(image))
else:
QMessageBox.warning(self, 'Warning!', 'You must first simulate the system!')
def veure_grafic_heating_temperature(self):
self.imageLabel.clear()
filename = 'Heating_temperature.png'
os.chdir(carpeta_data)
if os.path.exists(filename):
image = QImage(filename)
self.imageLabel.setPixmap(QPixmap.fromImage(image))
else:
QMessageBox.warning(self, 'Warning!', 'You must first simulate the system!')
def veure_grafic_heating_time(self):
self.imageLabel.clear()
filename = 'Heating_time.png'
os.chdir(carpeta_data)
if os.path.exists(filename):
image = QImage(filename)
self.imageLabel.setPixmap(QPixmap.fromImage(image))
else:
QMessageBox.warning(self, 'Warning!', 'You must first simulate the system!')
def veure_grafic_kT(self):
self.imageLabel.clear()
filename = 'K_T(T).png'
os.chdir(carpeta_data)
if os.path.exists(filename):
image = QImage(filename)
self.imageLabel.setPixmap(QPixmap.fromImage(image))
else:
QMessageBox.warning(self, 'Warning!', 'You must first simulate the system!')
def fer_simu(self):
t_0 = current_milli_time()
N = self.N.value()
L = self.L.value()
T = self.T.value()
m = float(self.m.text())
R = float(self.R.text())
dt = float(self.dt.text())
T_MAX = self.t_max.value()
if self.gas_type_sim.currentText() == "Ideal gas":
gas = IdealGas(N, L, T, m, R, dt, T_MAX)
gas.simulation()
gas.experiments()
else:
gas = RealGas(N, L, T, m, R, dt, T_MAX)
gas.simulation()
gas.experiments()
time_used = (current_milli_time() - t_0) / 1000
self.execution_time.setText(str(time_used) + ' s')
QMessageBox.information(self, 'Information', 'Simulation finished!')
def fer_anim(self):
t_0 = current_milli_time()
N = self.N.value()
L = self.L.value()
T = self.T.value()
m = float(self.m.text())
R = float(self.R.text())
dt = float(self.dt.text())
T_MAX = self.t_max.value()
if self.gas_type_sim.currentText() == "Ideal gas":
gas = IdealGas(N, L, T, m, R, dt, T_MAX)
gas.animation()
gas.experiments()
else:
gas = RealGas(N, L, T, m, R, dt, T_MAX)
gas.animation()
gas.experiments()
time_used = (current_milli_time() - t_0) / 1000
self.execution_time.setText(str(time_used) + ' s')
def P_V_diagram(self):
t_0 = current_milli_time()
N = self.N.value()
L = self.L.value()
T = self.T.value()
m = float(self.m.text())
R = float(self.R.text())
dt = float(self.dt.text())
T_MAX = self.t_max.value()
initial_L = self.initial_L.value()
final_L = self.final_L.value()
n = self.points_L.value()
pull_period = self.pull_period.value()
Ls = np.linspace(initial_L, final_L, n)
exp_pressures = []
theo_pressures = []
if self.isothermal_process.currentText() == "Ideal gas":
i = 0
total = len(Ls)
for L in Ls:
i += 1
gas = IdealGas(N, L, T, m, R, dt, T_MAX)
p = gas.simulation()
(exp, theo) = calculate_P(N, L, T, dt, p)
exp_pressures.append(exp)
theo_pressures.append(theo)
self.pv_bar.setValue(int(i / total * 100))
Vs = []
for i in range(len(Ls)):
Vs.append(Ls[i]**3)
plt.plot(Vs, theo_pressures, '-o', label = 'Ideal gas pressure')
plt.plot(Vs, exp_pressures, '-o', label = 'Experimental pressure')
plt.title('Isothermal compression (T=%.2f K)'%T)
plt.xlabel('Volume ($m^3$)')
plt.ylabel('Pressure (Pa)')
plt.legend()
os.chdir(carpeta_data)
plt.savefig('PV_diagram.png')
plt.gcf().clear()
time_used = (current_milli_time() - t_0) / 1000
self.execution_time.setText(str(time_used) + ' s')
QMessageBox.information(self, 'Information', 'Simulation finished!')
self.pv_bar.setValue(0)
else:
i = 0
total = len(Ls)
for L in Ls:
i += 1
gas = RealGas(N, L, T, m, R, dt, T_MAX)
p = gas.simulation()
(exp, theo) = calculate_P(N, L, T, dt, p)
exp_pressures.append(exp)
theo_pressures.append(theo)
self.pv_bar.setValue(int(i / total * 100))
Vs = []
for i in range(len(Ls)):
Vs.append(Ls[i]**3)
plt.plot(Vs, theo_pressures, '-o', label = 'Ideal gas pressure')
plt.plot(Vs, exp_pressures, '-o', label = 'Experimental pressure')
plt.title('Isothermal compression (T=%.2f K)'%T)
plt.xlabel('Volume ($m^3$)')
plt.ylabel('Pressure (Pa)')
plt.legend()
os.chdir(carpeta_data)
plt.savefig('PV_diagram.png')
plt.gcf().clear()
time_used = (current_milli_time() - t_0) / 1000
self.execution_time.setText(str(time_used) + ' s')
QMessageBox.information(self, 'Information', 'Simulation finished!')
self.pv_bar.setValue(0)
def isothermal_expansion(self):
t_0 = current_milli_time()
N = self.N.value()
L = self.L.value()
T = self.T.value()
m = float(self.m.text())
R = float(self.R.text())
dt = float(self.dt.text())
T_MAX = self.t_max.value()
Lf = self.final_L.value()
L0 = self.initial_L.value()
pull_period = self.pull_period.value()
dL = pull_period * (Lf-L0)/T_MAX
if pull_period > T_MAX :
QMessageBox.warning(self, 'Warning', 'Pull period cant\'t be greater than simulation time!')
else:
if self.isothermal_process.currentText() == "Ideal gas":
gas = IdealGas(N, L, T, m, R, dt, T_MAX)
gas.isothermal_expansion(pull_period, L0, dL)
else:
gas = RealGas(N, L, T, m, R, dt, T_MAX)
gas.isothermal_expansion(pull_period, L0, dL)
time_used = (current_milli_time() - t_0) / 1000
self.execution_time.setText(str(time_used) + ' s')
QMessageBox.information(self, 'Information', 'Simulation finished!')
def P_T_diagram(self):
t_0 = current_milli_time()
N = self.N.value()
L = self.L.value()
T = self.T.value()
m = float(self.m.text())
R = float(self.R.text())
dt = float(self.dt.text())
T_MAX = self.t_max.value()
initial_T = self.initial_T.value()
final_T = self.final_T.value()
n = self.points_T.value()
heat_period = self.heat_period.value()
Ts = np.linspace(initial_T, final_T, n)
exp_pressures = []
theo_pressures = []
if self.isochoric_process.currentText() == "Ideal gas":
i = 0
total = len(Ts)
for T in Ts:
i += 1
gas = IdealGas(N, L, T, m, R, dt, T_MAX)
p = gas.simulation()
(exp, theo) = calculate_P(N, L, T, dt, p)
exp_pressures.append(exp)
theo_pressures.append(theo)
self.pt_bar.setValue(int(i / total * 100))
plt.plot(Ts, theo_pressures, '-o', label = 'Ideal gas pressure')
plt.plot(Ts, exp_pressures, '-o', label = 'Experimental pressure')
plt.title(r'Isochoric heating (V=%.2f $m^3$)'% L**3)
plt.xlabel(r'Volume ($m^3$)')
plt.ylabel('Pressure (Pa)')
plt.legend()
os.chdir(carpeta_data)
plt.savefig('PT_diagram.png')
plt.gcf().clear()
time_used = (current_milli_time() - t_0) / 1000
self.execution_time.setText(str(time_used) + ' s')
QMessageBox.information(self, 'Information', 'Simulation finished!')
self.pt_bar.setValue(0)
else:
i = 0
total = len(Ts)
for T in Ts:
i += 1
gas = RealGas(N, L, T, m, R, dt, T_MAX)
p = gas.simulation()
(exp, theo) = calculate_P(N, L, T, dt, p)
exp_pressures.append(exp)
theo_pressures.append(theo)
self.pt_bar.setValue(int(i / total * 100))
plt.plot(Ts, theo_pressures, '-o', label = 'Ideal gas pressure')
plt.plot(Ts, exp_pressures, '-o', label = 'Experimental pressure')
plt.title(r'Isothermal compression (V=%.2f $m^3$)' % L**3)
plt.xlabel(r'Volume ($m^3$)')
plt.ylabel('Pressure (Pa)')
plt.legend()
os.chdir(carpeta_data)
plt.savefig('PT_diagram.png')
plt.gcf().clear()
time_used = (current_milli_time() - t_0) / 1000
self.execution_time.setText(str(time_used) + ' s')
QMessageBox.information(self, 'Information', 'Simulation finished!')
self.pt_bar.setValue(0)
def isochoric_heating(self):
t_0 = current_milli_time()
N = self.N.value()
L = self.L.value()
T = self.T.value()
m = float(self.m.text())
R = float(self.R.text())
dt = float(self.dt.text())
T_MAX = self.t_max.value()
Tf = self.final_T.value()
T0 = self.initial_T.value()
heat_period = self.heat_period.value()
dT = heat_period * (Tf-T0)/T_MAX
if heat_period > T_MAX :
QMessageBox.warning(self, 'Warning', 'Pull period cant\'t be greater than simulation time!')
else:
if self.isochoric_process.currentText() == "Ideal gas":
gas = IdealGas(N, L, T, m, R, dt, T_MAX)
gas.isochoric_heating(heat_period, T0, dT)
else:
gas = RealGas(N, L, T, m, R, dt, T_MAX)
gas.isochoric_heating(heat_period, T0, dT)
time_used = (current_milli_time() - t_0) / 1000
self.execution_time.setText(str(time_used) + ' s')
QMessageBox.information(self, 'Information', 'Simulation finished!')
def kT_T(self):
t_0 = current_milli_time()
N = self.N.value()
L = self.L.value()
T = self.T.value()
m = float(self.m.text())
R = float(self.R.text())
dt = float(self.dt.text())
T_MAX = self.t_max.value()
initial_L = self.initial_L.value()
final_L = self.final_L.value()
n_L = self.points_L.value()
initial_T = self.initial_T.value()
final_T = self.final_T.value()
n_T = self.points_T.value()
Ts = np.linspace(initial_T, final_T, n_T)
Ls = np.linspace(initial_L, final_L, n_L)
COLORS = ['r', 'b', 'g', 'y', 'm']
k_Ts = []
pressures = []
total = len(Ts) * len(Ls)
counter = 0
for T in Ts:
Exp_pr = []
Theo_pr = []
for L in Ls:
gas = IdealGas(N, L, T, m, R, dt, T_MAX)
gas.init_particles()
p = gas.simulation()
(exp, theo) = calculate_P(N, L, T, dt, p)
Exp_pr.append(exp)
Theo_pr.append(theo)
counter += 1
self.kt_bar.setValue(int(counter / total * 100))
Vs = []
for i in range(len(Ls)):
Vs.append(Ls[i]**3)
#Calculate k_T
k_T = []
k_T_theo = []
real_pr = []
for i in range(1, len(Ls)):
k_T.append((-1/Vs[i])*((Vs[i]-Vs[i-1])/(Exp_pr[i]-Exp_pr[i-1])))
real_pr.append(Exp_pr[i-1])
k_T_theo.append(1/Theo_pr[i-1])
#print('k_T experimental at T=%i:' % T, k_T)
#print('k_T teòrica:', k_T_theo)
k_Ts.append(k_T)
pressures.append(real_pr)
for i in range(len(Ts)):
plt.plot(k_Ts[i], pressures[i], color = COLORS[i % len(COLORS)], marker = 's', label = 'T=%i' % Ts[i])
plt.title(r'$k_T(T)$')
plt.xlabel(r'P ($Pa$)')
plt.ylabel(r'$k_T$ ($Pa^{-1}$)')
plt.legend()
os.chdir(carpeta_data)
plt.savefig('K_T(T).png')
time_used = (current_milli_time() - t_0) / 1000
self.execution_time.setText(str(time_used) + ' s')
QMessageBox.information(self, 'Information', 'Simulation finished!')
self.kt_bar.setValue(0)
def results(self):
pass
def closeEvent(self, event):
os.chdir(carpeta_data)
result = QMessageBox.question(self, 'Leaving...','Do you want to exit?', QMessageBox.Yes | QMessageBox.No)
if result == QMessageBox.Yes:
event.accept()
if os.path.exists('Pressure.png'): os.remove('Pressure.png')
if os.path.exists('CM_plots.png'): os.remove('CM_plots.png')
if os.path.exists('Velocity.png'): os.remove('Velocity.png')
if os.path.exists('Velocity_hist.png'): os.remove('Velocity_hist.png')
if os.path.exists('PT_diagram.png'): os.remove('PT_diagram.png')
if os.path.exists('PV_diagram.png'): os.remove('PV_diagram.png')
if os.path.exists('Expansion_time.png'): os.remove('Expansion_time.png')
if os.path.exists('Expansion_volume.png'): os.remove('Expansion_volume.png')
if os.path.exists('Heating_temperature.png'): os.remove('Heating_temperature.png')
if os.path.exists('Heating_time.png'): os.remove('Heating_time.png')
if os.path.exists('k_T(T).png'): os.remove('k_T(T).png')
else:event.ignore()
class Save_files(QFileDialog):
def __init__(self):
QFileDialog.__init__(self)
self.title = 'Save files'
self.left = 10
self.top = 10
self.width = 640
self.height = 400
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
def saveFileDialog(self, name):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getSaveFileName(self, 'Save files')
if fileName:
os.chdir(carpeta_data)
if os.path.exists('%s.png' % name): copyfile('%s.png' % name, fileName + '.png')
else: QMessageBox.warning(self, 'Warning!', 'The plot doesn\'t exist!')
app = QApplication(sys.argv)
_window=Window()
_window.show()
app.exec_()