-
Notifications
You must be signed in to change notification settings - Fork 0
/
NIST_Conversion.py
466 lines (356 loc) · 19.1 KB
/
NIST_Conversion.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
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
# This is a script designed to convert thermodynamic data of elements taken from nist.gov into different units.
import time
import scipy.constants as spc
''' Conversion equations:
Each conversion has its inverse equation supported.
Supported conversions for entropy: (Entropy conversions start with "S")
J/mol/k <-> kB/atom
erg/g/K (cgs) <-> kB/atom
J/g/K (SI units) <-> kB/atom
Mbar-cc/g/K (bdivK) <-> kB/atom
J/mol/K <-> J/g/K
J/g/K <-> erg/g/K (cgs)
Mbar-cc/g/K (bdivK) <-> J/g/K
Supported conversions for energy: (Energy conversions start with "E")
kJ/mol <-> meV/atom
eV/atom <-> er/g
J/g <-> eV/atom
Ry/atom <-> eV/atom
Ry/atom <-> erg/g
'''
# Entropy conversions:
def Sconvert_JmolK_to_kBatom(entropy):
return entropy / (spc.k * spc.N_A)
def Sconvert_kBatom_to_JmolK(entropy):
return entropy * (spc.k * spc.N_A)
def Sconvert_erggK_to_kBatom(entropy, atomic_mass):
return entropy * atomic_mass / (spc.k / spc.erg * spc.N_A)
def Sconvert_kBatom_to_erggK(entropy, atomic_mass):
return entropy * spc.k / spc.erg * spc.N_A / atomic_mass
def Sconvert_JgK_to_kBatom(entropy, atomic_mass):
return entropy * atomic_mass / (spc.k * spc.N_A)
def Sconvert_kBatom_to_JgK(entropy, atomic_mass):
return entropy * spc.k * spc.N_A / atomic_mass
def Sconvert_MbarccgK_to_kBatom(entropy, atomic_mass):
return entropy * atomic_mass * pow(10, 5) / (spc.k * spc.N_A)
def Sconvert_kBatom_to_MbarccgK(entropy, atomic_mass):
return entropy * spc.k * spc.N_A / (atomic_mass * pow(10, 5))
def Sconvert_JmolK_to_JgK(entropy, atomic_mass):
return entropy / atomic_mass
def Sconvert_JgK_to_JmolK(entropy, atomic_mass):
return entropy * atomic_mass
def Sconvert_JgK_to_erggK(entropy):
return entropy / spc.erg
def Sconvert_erggK_to_JgK(entropy):
return entropy * spc.erg
def Sconvert_MbarccgK_to_JgK(entropy):
return entropy * pow(10, 5)
def Sconvert_JgK_to_MbarccgK(entropy):
return entropy / pow(10, 5)
# Energy conversions:
def Econvert_kJmol_to_meVatom(energy):
return 1000 * energy / (spc.e * spc.N_A / 1000)
def Econvert_meVatom_to_kJmol(energy):
return energy * (spc.e * spc.N_A / 1000) / 1000
def Econvert_eVatom_to_ergg(energy, atomic_mass):
return energy * (spc.e * spc.N_A / spc.erg) / atomic_mass
def Econvert_ergg_to_eVatom(energy, atomic_mass):
return energy * atomic_mass / (spc.e * spc.N_A / spc.erg)
def Econvert_Jg_to_eVatom(energy, atomic_mass):
return energy * atomic_mass / (spc.e * spc.N_A)
def Econvert_eVatom_to_Jg(energy, atomic_mass):
return energy * spc.e * spc.N_A / atomic_mass
def Econvert_Ryatom_to_ergg(energy, atomic_mass):
return energy * (2.17987 * pow(10, -11)) * spc.N_A / atomic_mass
def Econvert_ergg_to_Ryatom(energy, atomic_mass):
return energy * atomic_mass / ((2.17987 * pow(10, -11)) * spc.N_A)
def Econvert_Ryatom_to_eVatom(energy):
return energy * 13.6056
def Econvert_eVatom_to_Ryatom(energy):
return energy / 13.6056
''' Updates and creates a file called "atomic_masses_list.txt" to use for creating the conversion tables for
entropy/energy. This draws values from a .txt file called "howerton_atomic_masses.txt" which is extracted from
a .pdf file containing the tested atomic mass values from a paper by "Robert J. Howerton" on July 16, 1985.
'''
def update_atomic_mass():
import mendeleev as md
atomic_mass_list = []
for i in range(118):
element = md.element(i + 1)
atomic_mass_list.append(element.atomic_weight)
try:
with open("howerton_atomic_masses.txt") as in_file:
for line in in_file:
atomic_contents = line.split(" ")
atomic_number = int(atomic_contents[0])
atomic_mass = float(atomic_contents[2])
atomic_mass_list[atomic_number - 1] = atomic_mass
except FileNotFoundError:
print("\nThere is no such file called howerton_atomic_masses!")
exit(-1)
try:
with open("atomic_masses_list.txt", "w") as out_file:
for i in range(118):
element = md.element(i + 1)
out_file.write("{} %s %s : {}\n".format((i + 1), atomic_mass_list[i]) % (element.symbol, element.name))
except FileNotFoundError:
print("\nThere is no such file called atomic_masses_list!")
exit(-1)
''' Returns an array of all the atomic mass values extracted from the .txt file, "atomic_masses_list.txt"
The index of an atomic mass refers to their atomic number - 1; ex. atomic_masses[0] = Hydrogen's AM = 1.00798.
'''
def create_atomic_mass_list():
atomic_masses = []
try:
with open("atomic_masses_list.txt") as in_file:
for line in in_file:
line_elements = line.split(" ")
atomic_masses.append(float(line_elements[4]))
except FileNotFoundError:
print("\nThere is no such file called atomic_masses_list")
exit(-1)
return atomic_masses
# Creation of conversion tables:
''' Each conversion table goes through this process:
Extracts the values from "atomic_masses_list.txt" into an array using create_atomic_mass_list().
Creates a new file corresponding to the type of table and unit conversion direction.
Writes a table with all of the conversion factors corresponding to their unit and element.
'''
def create_conversion_table_entropy():
atomic_masses = create_atomic_mass_list()
out_file_name = "entropy_conversion_table.txt"
try:
with open(out_file_name, "w") as out_file:
out_file.write("Starting Unit\t\t\terg/g/K\t\tMbar-cc/g/K\tJ/g/K\t\tJ/mol/K\n")
out_file.write("Ending Unit\t\t\tkB/atom\t\tkB/atom\t\tkB/atom\t\tJ/g/K\n")
out_file.write("Element\t\tAMass\n")
for i in range(118):
# Obtain all of the correct conversion factors for each unit conversion
conversion_1 = Sconvert_erggK_to_kBatom(1, atomic_masses[i])
conversion_2 = Sconvert_JgK_to_kBatom(1, atomic_masses[i])
conversion_3 = Sconvert_MbarccgK_to_kBatom(1, atomic_masses[i])
conversion_4 = Sconvert_JmolK_to_JgK(1, atomic_masses[i])
out_file.write("{}\t\t{:.6f}\t{:.6E}\t{:.6f}\t{:.6E}\t{:.6f}\n".format(i + 1, atomic_masses[i],
conversion_1, conversion_2,
conversion_3, conversion_4))
except FileNotFoundError:
print("\nThere is no such file called %s!" % out_file_name)
exit(-1)
def create_conversion_table_entropy_reverse():
atomic_masses = create_atomic_mass_list()
out_file_name = "entropy_conversion_table_reverse.txt"
try:
with open(out_file_name, "w") as out_file:
out_file.write("Starting Unit\t\t\tkB/atom\t\tkB/atom\t\tkB/atom\t\tJ/g/K\n")
out_file.write("Ending Unit\t\t\terg/g/K\t\tMbar-cc/g/K\tJ/g/K\t\tJ/mol/K\n")
out_file.write("Element\t\tAMass\n")
for i in range(118):
# Obtain all of the correct conversion factors for each unit conversion
conversion_1 = Sconvert_kBatom_to_erggK(1, atomic_masses[i])
conversion_2 = Sconvert_kBatom_to_JgK(1, atomic_masses[i])
conversion_3 = Sconvert_kBatom_to_MbarccgK(1, atomic_masses[i])
conversion_4 = Sconvert_JgK_to_JmolK(1, atomic_masses[i])
out_file.write("{}\t\t{:.6f}\t{:.6E}\t{:.6f}\t{:.6E}\t{:.6f}\n".format(i + 1, atomic_masses[i],
conversion_1, conversion_2,
conversion_3, conversion_4))
except FileNotFoundError:
print("\nThere is no such file called %s!" % out_file_name)
exit(-1)
def create_conversion_table_energy():
atomic_masses = create_atomic_mass_list()
out_file_name = "energy_conversion_table.txt"
try:
with open(out_file_name, "w") as out_file:
out_file.write("Starting Unit\t\t\terg/g\t\tJ/g\t\tRy/atom\t\tRy/atom\n")
out_file.write("Ending Unit\t\t\teV/atom\t\teV/atom\t\teV/atom\t\terg/g\n")
out_file.write("Element\t\tAMass\n")
for i in range(118):
# Obtain all of the correct conversion factors for each unit conversion
conversion_1 = Econvert_ergg_to_eVatom(1, atomic_masses[i])
conversion_2 = Econvert_Jg_to_eVatom(1, atomic_masses[i])
conversion_3 = Econvert_Ryatom_to_eVatom(1)
conversion_4 = Econvert_Ryatom_to_ergg(1, atomic_masses[i])
out_file.write("{}\t\t{:.6f}\t{:.6E}\t{:.6E}\t{:.6f}\t{:.6E}\n".format(i + 1, atomic_masses[i],
conversion_1, conversion_2,
conversion_3, conversion_4))
except FileNotFoundError:
print("\nThere is no such file called %s!" % out_file_name)
exit(-1)
def create_conversion_table_energy_reverse():
atomic_masses = create_atomic_mass_list()
out_file_name = "energy_conversion_table_reverse.txt"
try:
with open(out_file_name, "w") as out_file:
out_file.write("Starting Unit\t\t\teV/atom\t\teV/atom\t\teV/atom\t\terg/g\n")
out_file.write("Ending Unit\t\t\terg/g\t\tJ/g\t\tRy/atom\t\tRy/atom\n")
out_file.write("Element\t\tAMass\n")
for i in range(118):
# Obtain all of the correct conversion factors for each unit conversion
conversion_1 = Econvert_eVatom_to_ergg(1, atomic_masses[i])
conversion_2 = Econvert_eVatom_to_Jg(1, atomic_masses[i])
conversion_3 = Econvert_eVatom_to_Ryatom(1)
conversion_4 = Econvert_ergg_to_Ryatom(1, atomic_masses[i])
out_file.write("{}\t\t{:.6f}\t{:.6E}\t{:.6f}\t{:.6f}\t{:.6E}\n".format(i + 1, atomic_masses[i],
conversion_1, conversion_2,
conversion_3, conversion_4))
except FileNotFoundError:
print("\nThere is no such file called %s!" % out_file_name)
exit(-1)
''' This method searches a specific table in order to find the right factor asked by the user.
The table used for the parameter is specified when using the method "get_factor_from_table()"
'''
def access_factor_from_table(table, element, starting_unit, ending_unit):
in_file_name = table + ".txt"
starting_index = []
ending_index = []
try:
with open(in_file_name) as in_file:
for line in in_file:
line_elements = line.split("\t")
line_data = list(filter(None, line_elements))
line_data[len(line_data) - 1] = line_data[len(line_data) - 1].rstrip("\n")
''' Find the correct index for both the starting unit and ending unit, as some tables have the same unit
listed for a starting unit or ending unit.
'''
if line_data[0] == "Starting Unit":
for count, unit in enumerate(line_data):
if unit == starting_unit:
starting_index.append(count)
if line_data[0] == "Ending Unit":
for count, unit in enumerate(line_data):
if unit == ending_unit:
ending_index.append(count)
# If a common index exists when finding the indices for the starting unit and ending unit:
unit_index = common_member(starting_index, ending_index)
for i in unit_index:
index = i
''' Index will always be defined before this call, as the program searches for the common index before
trying to find the element to match the user inputted.
'''
if line_data[0] == str(element):
with open("atomic_masses_list.txt") as f:
for _line in f:
element_data = _line.split(" ")
if str(element) == element_data[0]:
element_name = element_data[2]
print("%s, %s -> %s: %s " % (element_name, starting_unit, ending_unit, line_data[index + 1]))
choice = input("\nWould you like to get another conversion factor? (Y/N) ")
if choice.upper() == "Y":
get_factor_from_table()
else:
print("\nExiting...")
time.sleep(.5)
exit(0)
except FileNotFoundError:
print("\nThere is no such file called %s!" % in_file_name)
exit(-1)
''' Helper method for access_factor_from_table()
Finds a common member between two Sets.
'''
def common_member(a, b):
a_set = set(a)
b_set = set(b)
return a_set & b_set
''' This is the main method for getting the user input and outputting the correct conversion factor.
This method first gets the user's input of parameters consisting of the element, starting unit, and ending unit.
The input is protected regardless of whether the wrong number of elements
'''
def get_factor_from_table():
# Manual list of units; to be updated when adding more units for functionality updates.
units = ["erg/g/K", "Mbar-cc/g/K", "J/g/K", "J/mol/K", "kB/atom", "erg/g", "J/g", "Ry/atom", "eV/atom"]
while True:
try:
choice = input("Please state the element, starting unit, and ending unit in this order:\n")
choice_parameters = choice.split(" ")
# Check if it is possible to fetch the element, and grab the element data from the mendeleev database.
try:
element = int(choice_parameters[0])
except ValueError:
with open("atomic_masses_list.txt") as in_file:
for line in in_file:
line_elements = line.split(" ")
if line_elements[1] == choice_parameters[0] or line_elements[2] == choice_parameters[0]:
element = int(line_elements[0])
break
# Make sure there are exactly three elements in the parameters
if len(choice_parameters) != 3:
continue
# Check if the units are valid and supported
if choice_parameters[1] not in units:
print("%s is not a supported unit / Please check your spelling.\n" % choice_parameters[1])
continue
if choice_parameters[2] not in units:
print("%s is not a supported unit / Please check your spelling.\n" % choice_parameters[2])
continue
except (ValueError, IndexError):
continue
else:
break
# Entropy conditions:
if choice_parameters[1] == "erg/g/K" or choice_parameters[1] == "Mbar-cc/g/K" or \
choice_parameters[1] == "J/mol/K":
access_factor_from_table("entropy_conversion_table", element, choice_parameters[1], choice_parameters[2])
elif choice_parameters[1] == "J/g/K":
if choice_parameters[2] == "kB/atom":
access_factor_from_table("entropy_conversion_table", element, choice_parameters[1], choice_parameters[2])
else:
access_factor_from_table("entropy_conversion_table_reverse", element, choice_parameters[1],
choice_parameters[2])
elif choice_parameters[1] == "kB/atom":
access_factor_from_table("entropy_conversion_table_reverse", element, choice_parameters[1],
choice_parameters[2])
# Energy conditions:
elif choice_parameters[1] == "J/g" or choice_parameters == "Ry/atom":
access_factor_from_table("energy_conversion_table", element, choice_parameters[1], choice_parameters[2])
elif choice_parameters[1] == "erg/g":
if choice_parameters[2] == "eV/atom":
access_factor_from_table("energy_conversion_table", element, choice_parameters[1], choice_parameters[2])
else:
access_factor_from_table("energy_conversion_table_reverse", element, choice_parameters[1],
choice_parameters[2])
elif choice_parameters[1] == "eV/atom":
access_factor_from_table("energy_conversion_table_reverse", element, choice_parameters[1], choice_parameters[2])
# If it does match the correct conditions for conversion, restart the method and try again.
else:
print("\nInvalid input for element, starting unit, or ending unit!\n")
get_factor_from_table()
# ==================================================================================================================== #
def main():
""" This print statement prints out information about the parameters and conversions supported at the beginning of
the program. It looks like this:
------------------------------
Parameter Format:
"Name/Atomic Number/Symbol" "Starting Unit" "Ending Unit"
------------------------------
Current Conversions Supported:
erg/g/K (cgs) <-> kB/atom
J/g/K (SI units) <-> kB/atom
Mbar-cc/g/K (bdivK) <-> kB/atom
J/mol/K <-> J/g/K
eV/atom <-> erg/g
J/g <-> eV/atom
Ry/atom <-> eV/atom
Ry/atom <-> erg/g
------------------------------
Examples:
4 J/g/K kB/atom
Carbon erg/g eV/atom
Zr kB/atom Mbar-cc/g/K%s"
------------------------------
"""
line_break = "------------------------------\n"
print("%sParameter Format:\n\"Name/Atomic Number/Symbol\" \"Starting Unit\" \"Ending Unit\"\n%sCurrent Conversions "
"Supported:\n\n\terg/g/K (cgs) <-> kB/atom\n\tJ/g/K (SI units) <-> kB/atom\n\tMbar-cc/g/K (bdivK) <-> "
"kB/atom\n\tJ/mol/K <-> J/g/K\n\n\teV/atom <-> erg/g\n\tJ/g <-> eV/atom\n\tRy/atom <-> eV/atom\n\tRy/atom "
"<-> erg/g\n%sExamples:\n4 J/g/K kB/atom\nCarbon erg/g eV/atom\nZr kB/atom Mbar-cc/g/K\n%s" % (line_break,
line_break,
line_break,
line_break))
# update_atomic_mass() # Only use this line to update the atomic mass (AM) list if there are changes to AM values
create_conversion_table_entropy()
create_conversion_table_entropy_reverse()
create_conversion_table_energy()
create_conversion_table_energy_reverse()
get_factor_from_table()
if __name__ == "__main__":
main()