-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcpgui_all.py
432 lines (410 loc) · 13.7 KB
/
cpgui_all.py
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
# -*- coding: utf-8 -*-
#
import sys
import os
import pickle
import json
import gettext
#
from tkinter import *
from tkinter import ttk
from tkinter.simpledialog import Dialog
from tkinter import messagebox
#
from platform import system
from os import walk,path
from CoolProp.CoolProp import PropsSI
def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
else:
# The application is not frozen
# Change this bit to match where you store your data files:
datadir = os.path.dirname(__file__)
return os.path.join(datadir, filename)
gui_unit={}
cpgui_config={}
#
myfile=find_data_file('cpgui.dat')
try:
settingsfile = open(myfile)
except FileNotFoundError:
print('cpgui.dat not found! ... creating file')
cpgui_config['cpgui']={}
cpgui_config['cpgui']['language'] = 'en'
cpgui_config['units']={}
cpgui_config['units']['T'] ='°C'
cpgui_config['units']['dT'] ='K'
cpgui_config['units']['p'] ='bar'
cpgui_config['units']['D'] ='kg/m³'
cpgui_config['units']['v'] ='m³/kg'
cpgui_config['units']['H'] ='kJ/kg'
cpgui_config['units']['S'] ='kJ/kg/K'
cpgui_config['units']['Q'] ='kg/kg'
cpgui_config['units']['P'] ='kW'
cpgui_config['units']['U'] ='kJ'
cpgui_config['units']['V'] ='m/s'
cpgui_config['units']['dv'] ='µPa s'
cpgui_config['units']['kv'] ='m²/s'
cpgui_config['units']['Cp'] ='kJ/kg/K'
cpgui_config['units']['Cv'] ='kJ/kg/K'
cpgui_config['units']['M'] ='kg/kmol'
cpgui_config['units']['c'] ='m/s'
cpgui_config['units']['kxa']='W/K'
cpgui_config['units']['eta']='1'
cpgui_config['debug']={}
cpgui_config['debug']['ShowPanel'] = True
#
with open(myfile, 'wb') as datafile:
pickle.dump(cpgui_config,datafile)
# We must have a configfile here!
with open(myfile, 'rb') as configfile:
cpgui_config = pickle.load(configfile)
# Now set language
cpgui_language=cpgui_config['cpgui']['language']
localedir=find_data_file('locale')
cpgui_lang = gettext.translation('cpgui_all', localedir=localedir, languages=[cpgui_language],fallback=False)
cpgui_lang.install()
# Now read units into gui_unit
for quant in cpgui_config['units'].keys():
gui_unit[quant]=cpgui_config['units'][quant]
#print('Gui Unit for {} set to {} .'.format(quant,gui_unit[quant]))
class myDialog(Dialog):
# use dialogOptions dictionary to set any values in the dialog
def __init__(self, parent, title = None,dialogOptions = None):
self.initComplete = 0
self.dialogOptions = dialogOptions
Dialog.__init__(self, parent, 'Coolprop GUI')
self.master = parent
self.x, self.y, self.w, self.h = -1,-1,-1,-1
#self.master.bind('<Enter>', self.bindConfigure)
def refprop_mixname(reflist,masslist):
'''
Creates a refprop mixture string from names and mass fractions
e.G.
reflist = list of refrigerants in a mixture
masslist = massfraction in kg/kg
'''
# Calculate total
molelist=[]
molefrac=[]
namelist=[]
denominator=0
# Get moles and calculate denominator
for index in range(len(reflist)):
m=PropsSI("M",reflist[index])
molelist.append(m)
namelist.append(reflist[index].split(':')[-1])
denominator+=(masslist[index]/molelist[index])
# Denominator is calculated, now molefractions
for index in range(len(reflist)):
#
mol=(masslist[index]/molelist[index])/denominator
molefrac.append(mol)
# now create string from data
refpropstring='REFPROP::'
add_ampersand=False
#
for index in range(len(namelist)):
if add_ampersand :
refpropstring+='&'+namelist[index]+'[%0.16f]'%molefrac[index]
else :
refpropstring+=namelist[index]+'[%0.16f]'%molefrac[index]
add_ampersand=True
#
return refpropstring,molefrac
def get_refprop_fluids():
#
mysys=system()
if mysys=='Linux' or mysys=='Darwin':
if path.isdir('/opt/refprop') :
rpfluids1 = next(walk('/opt/refprop/fluids'))[2]
rpfluids1.sort()
rpfluids=[]
for fluid in rpfluids1 :
rpfluids.append((fluid.split('.')[0]))
rpmixtures1 = next(walk('/opt/refprop/mixtures'))[2]
rpmixtures1.sort()
rpmixtures=[]
for mixture in rpmixtures1 :
rpfluids.append((mixture))
else :
rpfluids=['Refprop not found']
return rpfluids
#
elif mysys=='Windows':
if path.isdir('C:\\Program Files (x86)\\Refprop') :
rpfluids1 = next(walk('C:\\Program Files (x86)\\Refprop\\fluids'))[2]
rpfluids1.sort()
rpfluids=[]
for fluid in rpfluids1 :
rpfluids.append((fluid.split('.')[0]))
rpmixtures1 = next(walk('C:\\Program Files (x86)\\Refprop\\mixtures'))[2]
rpmixtures1.sort()
rpmixtures=[]
for mixture in rpmixtures1 :
rpfluids.append((mixture))
return rpfluids
else :
rpfluids=['No Refprop']
return rpfluids
#return ['R134a']
# Units, SI unit must be first in list
units={}
units['T'] =['K','°C']
units['dT'] =['K']
units['p'] =['Pa','kPa','bar']
units['D'] =['kg/m³','kg/dm³']
units['v'] =['m³/kg']
units['H'] =['J/kg','kJ/kg']
units['S'] =['J/kg/K','kJ/kg/K']
units['Q'] =['kg/kg']
units['P'] =['kW','W']
units['U'] =['J','kJ']
units['V'] =['m/s']
units['dv'] =['Pa s','mPa s','µPa s']
units['kv'] =['m²/s']
units['Cp'] =['J/kg/K','kJ/kg/K']
units['Cv'] =['J/kg/K','kJ/kg/K']
units['M'] =['kg/mol','kg/kmol']
units['c'] =['m/s']
# now the wired stuff
units['kxa'] =['W/K']
units['eta'] =['1']
# we have to convert all those units to SI
tosi = {
'K' : lambda x: x,
'°C' : lambda x: x +273.15,
'bar' : lambda x: x * 100000.0,
'Pa' : lambda x: x,
'kPa' : lambda x: x*1000.0,
'kg/m³' : lambda x: x,
'kg/dm³' : lambda x: x*1000.0,
'm³/kg' : lambda x: x,
'J/kg' : lambda x: x,
'kJ/kg' : lambda x: x/1000.0,
'J/kg/K' : lambda x: x,
'kJ/kg/K' : lambda x: x*1000.0,
'kg/kg' : lambda x: x,
'kW' : lambda x: x*1000.0,
'W' : lambda x: x,
'J' : lambda x: x,
'kJ' : lambda x: x*1000.0,
'm/s' : lambda x: x,
'Pa s' : lambda x: x,
'mPa s' : lambda x: x/1000.0,
'µPa s' : lambda x: x/1000000.0,
'm²/s' : lambda x: x,
'kg/mol' : lambda x: x,
'kg/kmol' : lambda x: x/1000,
'W/K' : lambda x: x,
'1' : lambda x: x,
}
# and from SI
sito = {
'K' : lambda x: x,
'°C' : lambda x: x -273.15,
'bar' : lambda x: x / 100000.0,
'Pa' : lambda x: x,
'kPa' : lambda x: x/1000.0,
'kg/m³' : lambda x: x,
'kg/dm³' : lambda x: x/1000.0,
'm³/kg' : lambda x: x,
'J/kg' : lambda x: x,
'kJ/kg' : lambda x: x/1000.0,
'J/kg/K' : lambda x: x,
'kJ/kg/K' : lambda x: x/1000.0,
'kg/kg' : lambda x: x,
'kW' : lambda x: x/1000.0,
'W' : lambda x: x,
'J' : lambda x: x,
'kJ' : lambda x: x/1000.0,
'm/s' : lambda x: x,
'Pa s' : lambda x: x,
'mPa s' : lambda x: x*1000.0,
'µPa s' : lambda x: x*1000000.0,
'm²/s' : lambda x: x,
'kg/mol' : lambda x: x,
'kg/kmol' : lambda x: x*1000,
'W/K' : lambda x: x,
'1' : lambda x: x,
}
# default Values for forms as string
default = {
'K' : '273.15',
'°C' : '0',
'bar' : '1.01325',
'Pa' : '101325',
'kPa' : '101.325',
'kg/m³' : '1000.0',
'kg/dm³' : '1.0',
'm³/kg' : '0.001',
'J/kg' : '200000.0',
'kJ/kg' : '200.0',
'J/kg/K' : '1000.0',
'kJ/kg/K' : '1.0',
'kg/kg' : '1.0',
'kW' : '1.0',
'W' : '1000.0',
'J' : '1000.0',
'kJ' : '1.0',
'm/s' : '1.0',
'Pa s' : '1000000.0',
'mPa s' : '1000.0',
'µPa s' : '1.0',
'm²/s' : '1.0',
'kg/mol' : '0.1',
'kg/kmol' : '100.0',
'1' : '0.6',
'W/K' : '23',
}
label={}
label['T'] = _('Temperature')
label['dT'] = _('Temperature difference')
label['p'] = _('Pressure')
label['D'] = _('Density')
label['v'] = _('Specific Volume')
label['H'] = _('Specific Enthalpy')
label['S'] = _('Specific Entropy')
label['Q'] = _('Quality')
label['P'] = _('Power')
label['U'] = _('Internal Energy')
label['V'] = _('Speed')
label['dv'] = _('Dynamic Viscosity')
label['kv'] = _('Kinematic Viscosity')
label['Cp'] = _('Isobaric heat Capacity')
label['Cv'] = _('Iscochoric heat Capacity')
label['M'] = _('Molar mass')
label['c'] = _('Speed of sound')
label['kxa'] = _('Heat transfer k x A')
label['eta'] = _('Efficiency')
#
cpcode={}
cpcode['T'] = 'T'
cpcode['p'] = 'P'
cpcode['D'] = 'D'
cpcode['H'] = 'H'
cpcode['S'] = 'S'
cpcode['Q'] = 'Q'
cpcode['U'] = 'U'
cpcode['dv'] = 'V'
cpcode['Cp'] = 'C'
cpcode['Cv'] = 'CVMASS'
cpcode['M'] = 'M'
cpcode['c'] = 'A'
#
def german_units():
if 'units' in cpgui_config.sections() :
pass
else :
cpgui_config.add_section('units')
#
cpgui_config['cpgui']={}
cpgui_config['cpgui']['language'] = 'de'
cpgui_config['units']={}
cpgui_config['units']['T'] ='°C'
cpgui_config['units']['dT'] ='K'
cpgui_config['units']['p'] ='bar'
cpgui_config['units']['D'] ='kg/m³'
cpgui_config['units']['v'] ='m³/kg'
cpgui_config['units']['H'] ='kJ/kg'
cpgui_config['units']['S'] ='kJ/kg/K'
cpgui_config['units']['Q'] ='kg/kg'
cpgui_config['units']['P'] ='kW'
cpgui_config['units']['U'] ='kJ'
cpgui_config['units']['V'] ='m/s'
cpgui_config['units']['dv'] ='µPa s'
cpgui_config['units']['kv'] ='m²/s'
cpgui_config['units']['Cp'] ='kJ/kg/K'
cpgui_config['units']['Cv'] ='kJ/kg/K'
cpgui_config['units']['M'] ='kg/kmol'
cpgui_config['units']['c'] ='m/s'
cpgui_config['units']['kxa']='W/K'
cpgui_config['units']['eta']='1'
cpgui_config['debug']={}
cpgui_config['debug']['ShowPanel'] = True
with open(myfile, 'wb') as datafile:
pickle.dump(cpgui_config,datafile)
messagebox.showinfo( _('Units changed'), _('Units changed ! \nRestart GUI for Unit change to take effect!'))
def si_units():
if 'units' in cpgui_config.sections() :
pass
else :
cpgui_config.add_section('units')
#
cpgui_config['cpgui']={}
cpgui_config['cpgui']['language'] = 'en'
cpgui_config['units']={}
cpgui_config['units']['T'] ='K'
cpgui_config['units']['dT'] ='K'
cpgui_config['units']['p'] ='Pa'
cpgui_config['units']['D'] ='kg/m³'
cpgui_config['units']['v'] ='m³/kg'
cpgui_config['units']['H'] ='J/kg'
cpgui_config['units']['S'] ='J/kg/K'
cpgui_config['units']['Q'] ='kg/kg'
cpgui_config['units']['P'] ='kW'
cpgui_config['units']['U'] ='J'
cpgui_config['units']['V'] ='m/s'
cpgui_config['units']['dv'] ='Pa s'
cpgui_config['units']['kv'] ='m²/s'
cpgui_config['units']['Cp'] ='J/kg/K'
cpgui_config['units']['Cv'] ='J/kg/K'
cpgui_config['units']['M'] ='kg/kmol'
cpgui_config['units']['c'] ='m/s'
cpgui_config['units']['kxa']='W/K'
cpgui_config['units']['eta']='1'
cpgui_config['debug']={}
cpgui_config['debug']['ShowPanel'] = True
with open(myfile, 'wb') as datafile:
pickle.dump(cpgui_config,datafile)
messagebox.showinfo( _('Units changed'), _('Units changed ! \nRestart GUI for Unit change to take effect!'))
def save_settings():
with open(myfile, 'wb') as datafile:
pickle.dump(cpgui_config,datafile)
messagebox.showinfo( _('Major settings changed'), _('Settings changed ! \nRestart GUI for change to take effect!'))
def SI_UNIT(Quantity):
return units[Quantity][0]
def GUI_UNIT(Quantity):
try :
return gui_unit[Quantity]
except KeyError :
return ' '
def TO_SI(Quantity,Value):
if Quantity in cpgui_config['units'] :
localunit=gui_unit[Quantity]
if localunit in tosi.keys():
#print('converting ',Value,GUI_UNIT(Quantity),' to ', tosi[localunit](Value) ,SI_UNIT(Quantity))
return tosi[localunit](Value)
else:
print('Error in tosi : No conversion for unit ',localunit,' defined')
else :
print('Error in get_SI : No unit for Quantity',Quantity,' defined')
def SI_TO(Quantity,Value):
if Quantity in cpgui_config['units'] :
localunit=gui_unit[Quantity]
if localunit in sito.keys():
#print('converting ',Value,SI_UNIT(Quantity),' to ', sito[localunit](Value) ,localunit)
return sito[localunit](Value)
else:
print('Error in sito : No conversion for unit ',localunit,' defined')
else :
print('Error in get_SI : No unit for Quantity',Quantity,' defined')
class RedirectText():
def __init__(self, text_ctrl):
self.output = text_ctrl
def write(self, string):
self.output.insert(END, string)
if __name__=='__main__':
#
print(refprop_mixname(['REFPROP::R134a','REFPROP::R1234ze'],[0.42,0.58]))
print(refprop_mixname(['R125','R143a','R134a'],[0.44,0.52,0.04]))
#
#german_units()
#
print(type(tosi))
print(tosi['°C'](0))
print(sito['°C'](293.15))
print(TO_SI('T',0))
#print(SI_TO('H', 300000))