forked from samuelshaner/casmo_reader
/
casmo_reader.py
555 lines (425 loc) · 21.3 KB
/
casmo_reader.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
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
import Image
import ImageDraw
import ImageFont
import paramiko
import os
import sys
import getopt
import numpy
import math
class Bundle(object):
def __init__(self, pass_word, home_dir, input_file):
self.pass_word = pass_word
self.home_dir = home_dir
self.input_file = input_file
self.font = ImageFont.truetype(os.getcwd() + '/Helvetica.ttf', 20)
print 'removing old files...'
# if old files exist, remove them
if (os.path.exists('bwr.out')):
os.system('rm bwr.out')
if (os.path.exists('pin_powers00.000.png')):
os.system('rm *.png')
def makeGeometry(self):
# parse input file and plot enrichments and gad percents
print 'parsing casmo input...'
logfile = open(self.input_file, 'r').readlines()
self.pin_type = numpy.zeros(shape=(10,10))
self.pin_num = numpy.zeros(shape=(10,10))
counter = 0
for line in logfile:
if 'LFU' in line:
line_num = 0
for i in range(counter+1,counter+11):
char_num = 0
for j in range(0,line_num+1):
if logfile[i][char_num+1] == ' ':
self.pin_type[line_num,j] = float(logfile[i][char_num])
char_num += 2
else:
self.pin_type[line_num,j] = float(logfile[i][char_num] + logfile[i][char_num+1])
char_num += 3
line_num += 1
if 'LPI' in line:
line_num = 0
for i in range(counter+1,counter+11):
char_num = 0
for j in range(0,line_num+1):
self.pin_num[line_num,j] = float(logfile[i][char_num])
char_num += 2
line_num += 1
counter += 1
# fill in empty pin_types nad pin_nums
for row in range(0,10):
for col in range(row,10):
self.pin_type[row,col] = self.pin_type[col,row]
self.pin_num[row,col] = self.pin_num[col,row]
'''
This portion of the code parses the 'bwr.inp' input file for casmo to find the number of each pin type,
the enrichment for each pin type. It uses this information to compute the total cost for the BWR fuel
bundle represented by the casmo input file using current fuel costs from the UxC website.
'''
# parse bwr.inp and find the ids Gd and non-Gd pins
logfile = open(self.input_file, "r").readlines()
start_pins = 'FUE'
end_pins = 'LFU'
Gd_pin = '64016='
# Dictionaries of pin IDs (keys) to uranium enrichments (values)
self.non_Gd_pin_IDs_to_enr = {}
self.Gd_pin_IDs_to_enr = {}
# Dictionaries of pin IDs (keys) to pin quantities (values)
self.non_Gd_pin_IDs_to_qty = {}
self.Gd_pin_IDs_to_qty = {}
self.pin_IDs_to_gad = {}
line_counter = 0
for line in logfile:
if start_pins in line:
while start_pins in line:
if Gd_pin in line:
# First set the number of this given pin to zero - count pins on next loop in script
self.Gd_pin_IDs_to_qty[(int(logfile[line_counter].split()[1]))] = 0
# Next set the enrichment for this pin type
self.Gd_pin_IDs_to_enr[(int(logfile[line_counter].split()[1]))] = float(logfile[line_counter].split()[2][5:len(logfile[line_counter].split()[2])])
# Next set the gad concentration for this pin type
self.pin_IDs_to_gad[(int(logfile[line_counter].split()[1]))] = float(logfile[line_counter].split()[3][6:8])
else:
# First set number of this given pin to zero - count pins on next loop in script
self.non_Gd_pin_IDs_to_qty[(int(logfile[line_counter].split()[1]))] = 0
# Next set the enrichment for this pin type
self.non_Gd_pin_IDs_to_enr[(int(logfile[line_counter].split()[1]))] = float(logfile[line_counter].split()[2][5:len(logfile[line_counter].split()[2])])
# Next set the gad concentration for this pin type
self.pin_IDs_to_gad[(int(logfile[line_counter].split()[1]))] = 0.0
line_counter += 1
line = logfile[line_counter]
break
line_counter += 1
# parse bwr.inp and find the quantity of each pin type in the geometry
logfile = open(self.input_file, "r").readlines()
start_geometry = 'LFU'
end_geometry = 'DEP'
num_non_Gd_pins = 0
num_Gd_pins = 0
line_counter = 0
for line in logfile:
if start_geometry in line:
line_counter += 1
line = logfile[line_counter]
while end_geometry not in line:
pin_IDs = logfile[line_counter].split()
for id in pin_IDs:
if int(id) in self.Gd_pin_IDs_to_qty.iterkeys():
self.Gd_pin_IDs_to_qty[int(id)] += 1
num_Gd_pins += 1
else:
self.non_Gd_pin_IDs_to_qty[int(id)] += 1
num_non_Gd_pins += 1
line_counter += 1
line = logfile[line_counter]
break
line_counter += 1
# plot enrichments and gad conc.
self.pin_enr = numpy.zeros(shape=(10,10))
self.pin_gad = numpy.zeros(shape=(10,10))
for row in range(0,10):
for col in range(0,10):
self.pin_gad [row,col] = self.pin_IDs_to_gad[int(self.pin_type[row,col])]
if self.pin_gad[row,col] == 0:
self.pin_enr[row,col] = self.non_Gd_pin_IDs_to_enr[int(self.pin_type[row,col])]
else:
self.pin_enr[row,col] = self.Gd_pin_IDs_to_enr[int(self.pin_type[row,col])]
# create array of normalized pin powers to plot
gad_max = 10
enr_max = 4.9
self.pin_enr[3,5] = 0.0
self.pin_enr[4,5] = 0.0
self.pin_enr[3,6] = 0.0
self.pin_enr[4,6] = 0.0
self.pin_enr[5,3] = 0.0
self.pin_enr[5,4] = 0.0
self.pin_enr[6,3] = 0.0
self.pin_enr[6,4] = 0.0
pin_enr_draw = self.pin_enr/enr_max
pin_gad_draw = self.pin_gad/gad_max
# create image
img_enr = Image.new('RGB', (1000,1000), 'white')
img_gad = Image.new('RGB', (1000,1000), 'white')
draw_enr = ImageDraw.Draw(img_enr)
draw_gad = ImageDraw.Draw(img_gad)
for i in range(0,10):
for j in range(0,10):
# get color for enr pins
if (pin_enr_draw[i,j] <= 1.0/3.0):
red_enr = 0.0
green_enr = 3.0 * pin_gad_draw[i,j]
blue_enr = 1.0
elif (pin_enr_draw[i,j] <= 2.0/3.0):
red_enr = 3.0 * pin_enr_draw[i,j] - 1.0
green_enr = 1.0
blue_enr = -3.0 * pin_enr_draw[i,j] + 2.0
else:
red_enr = 1.0
green_enr = -3.0 * pin_enr_draw[i,j] + 3.0
blue_enr = 0.0
# get color for gad pins
if (pin_gad_draw[i,j] <= 1.0/3.0):
red_gad = 0.0
green_gad = 3.0 * pin_gad_draw[i,j]
blue_gad = 1.0
elif (pin_gad_draw[i,j] <= 2.0/3.0):
red_gad = 3.0 * pin_gad_draw[i,j] - 1.0
green_gad = 1.0
blue_gad = -3.0 * pin_gad_draw[i,j] + 2.0
else:
red_gad = 1.0
green_gad = -3.0 * pin_gad_draw[i,j] + 3.0
blue_gad = 0.0
# convert color to RGB triplet
red_enr = int(255*red_enr)
green_enr = int(255*green_enr)
blue_enr = int(255*blue_enr)
# convert color to RGB triplet
red_gad = int(255*red_gad)
green_gad = int(255*green_gad)
blue_gad = int(255*blue_gad)
# draw pin and pin power
draw_enr.rectangle([i*100, j*100, (i+1)*100, (j+1)*100], (red_enr,green_enr,blue_enr))
draw_gad.rectangle([i*100, j*100, (i+1)*100, (j+1)*100], (red_gad,green_gad,blue_gad))
draw_enr.text([i*100+35,j*100+40], str(self.pin_enr[i,j]), (0,0,0), font=self.font)
draw_gad.text([i*100+35,j*100+40], str(self.pin_gad[i,j]), (0,0,0), font=self.font)
# save image
img_enr.save('enr.png')
img_gad.save('gad.png')
def runCasmo(self):
# open transport link to cheezit.mit.edu
port = 22
local_path = os.getcwd() + '/bwr.out'
host = 'cheezit.mit.edu'
transport = paramiko.Transport((host,port))
user_name = '22.39'
# copy the input file to cheezit.mit.edu
print 'transferring ' + self.input_file + ' to cheezit...'
transport.connect(username=user_name, password = self.pass_word)
sftp = paramiko.SFTPClient.from_transport(transport)
remote_path = '/home/22.39/' + self.home_dir + '/bwr.inp'
sftp.put(self.input_file, remote_path)
# ssh onto cheezit.mit.edu
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=user_name, password = self.pass_word)
# run casmo on cheezit
cmd_str = 'cd /home/22.39/' + self.home_dir
stdin, stdout, stderr = ssh.exec_command(cmd_str + '; qsub casmo.qsub')
# Get the first 3 characters of the name of the job - this is the job id
job_name = stdout.readlines()[0]
if job_name[3] == '.':
job_id = job_name[0:3]
else:
job_id = job_name[0:4]
# Pause the
print 'waiting for cheezit to run casmo....'
cmd_str = 'qstat | grep ' + str(job_id)
is_file_running = 'initially'
while (is_file_running is not ''):
stdin, stdout, stderr = ssh.exec_command(cmd_str)
try:
is_file_running = stdout.readlines()[0]
except:
break
print 'casmo run complete!'
# copy bwr.out to local directory
print 'getting casmo output from cheezit...'
file_path = '/home/22.39/' + self.home_dir + '/bwr.out'
sftp.get(file_path, local_path)
# get name the last .o file generated
cmd_str = 'ls ' + self.home_dir + " -t | grep 'casmo.qsub.o*' | head -n1"
stdin, stdout, stderr = ssh.exec_command(cmd_str)
self.o_file = stdout.readlines()[0]
self.o_file = self.o_file[:-1]
# copy last .o file to local directory
file_path = '/home/22.39/' + self.home_dir + '/' + self.o_file
local_path = os.getcwd() + '/' + self.o_file
sftp.get(file_path, local_path)
# close ssh, sftp, and transport
ssh.close()
sftp.close()
transport.close()
def parseOutput(self):
print 'parsing casmo output...'
# parse output file and make array of pin powers
logfile = open("bwr.out", "r").readlines()
summary = 'C A S M O - 4 S U M M A R Y'
self.powers = numpy.zeros(shape=(10,10))
counter = 0
for line in logfile:
if summary in line:
burnup_str = 'burnup = ' + logfile[counter+1].split()[2] + ' MWD / kg'
keff_str = 'keff = ' + logfile[counter+1].split()[6]
peak_power_str = 'peak power = ' + logfile[counter+4].split()[6]
line_num = 0
for i in range(counter+5,counter+15):
char_start = 2
for j in range(0,line_num+1):
self.powers[line_num,j] = float(logfile[i][char_start]+logfile[i][char_start+1]+logfile[i][char_start+2]+logfile[i][char_start+3]+logfile[i][char_start+4])
char_start += 7
line_num += 1
# fill in empty pin powers
for row in range(0,10):
for col in range(row,10):
self.powers[row,col] = self.powers[col,row]
# create array of normalized pin powers to plot
pmax = numpy.max(self.powers)
powers_draw = self.powers/pmax
# create image
img = Image.new('RGB', (1000,1000), 'white')
draw = ImageDraw.Draw(img)
for i in range(0,10):
for j in range(0,10):
# get color
if (powers_draw[i,j] <= 1.0/3.0):
red = 0.0
green = 3.0 * powers_draw[i,j]
blue = 1.0
elif (powers_draw[i,j] <= 2.0/3.0):
red = 3.0 * powers_draw[i,j] - 1.0
green = 1.0
blue = -3.0 * powers_draw[i,j] + 2.0
else:
red = 1.0
green = -3.0 * powers_draw[i,j] + 3.0
blue = 0.0
# convert color to RGB triplet
red = int(255*red)
green = int(255*green)
blue = int(255*blue)
# draw pin and pin power
draw.rectangle([i*100, j*100, (i+1)*100, (j+1)*100], (red,green,blue))
draw.text([i*100+25,j*100+40], str(self.powers[i,j]), (0,0,0), font=self.font)
# save image
sum_str = burnup_str + ' ' + keff_str + ' ' + peak_power_str
draw.text([250,5], sum_str, font=self.font)
if float(logfile[counter+1].split()[2]) / 10 < 1.0:
img_str = 'pin_powers0' + logfile[counter+1].split()[2] + '.png'
else:
img_str = 'pin_powers' + logfile[counter+1].split()[2] + '.png'
img.save(img_str)
counter += 1
def computeGrade(self):
'''
This portion of the code parses the 'casmo.qsub.o*' condensed output file to find
the maximum pin power peaking factors, k_inf, and the burnup for each depletion cycle.'
It finds the maximum power peaking factor for all cycles, the initial k_inf, and the
maximum burnup (where k_inf < 0.95 indicates EOL).
'''
logfile = open(self.o_file, "r").readlines()
start_table = 'TWO-GROUP'
end_table = 'RUN TERMINATED'
self.peak_pin_powers = []
self.k_inf = []
self.burnup = []
line_counter = 0
data_counter = 0
# parse casmo.qsub.o954 and find the
for line in logfile:
if start_table in line:
line_counter += 1
# pull the initial k_inf value in the table
self.peak_pin_powers.append(float(logfile[line_counter].split()[10]))
self.k_inf.append(float(logfile[line_counter].split()[8]))
self.burnup.append(float(logfile[line_counter].split()[5]))
line_counter += 1
# loop over the rest of the table
while end_table not in line:
if len(line.split()) > 1:
self.peak_pin_powers.append(float(logfile[line_counter].split()[5]))
self.k_inf.append(float(logfile[line_counter].split()[2]))
self.burnup.append(float(logfile[line_counter].split()[1]))
# update the counters for the logfile and arrays of data
data_counter += 1
line_counter += 1
# fetch the next line in the logfile
line = logfile[line_counter]
line_counter += 1
# readjust to only include data for k_inf > 0.95 (EOL)
if len(self.peak_pin_powers) > 1:
self.peak_pin_powers = self.peak_pin_powers[:-1]
self.k_inf = self.k_inf[0:len(self.k_inf)-1]
self.burnup = self.burnup[0:len(self.burnup)-1]
# compute max pin power and max k_inf
max_pin_power = max(self.peak_pin_powers)
initial_k_inf = self.k_inf[0]
max_k_inf = max(self.k_inf)
eol_burnup = 0.0
for i in range(len(self.k_inf)):
if self.k_inf[i] < .95:
eol_burnup = self.burnup[i-1] + (self.burnup[i] - self.burnup[i-1]) * \
(self.k_inf[i-1] - .95) / (self.k_inf[i-1] - self.k_inf[i])
break
print '\tEOL Burnup = \t\t\t' + str(eol_burnup) + ' [MWD/kg]'
print '\tMax Pin Power Peaking Factor = \t' + str(max_pin_power)
print '\tInitial k_inf = \t\t' + str(initial_k_inf)
print '\tMax k_inf = \t\t\t' + str(max_k_inf)
# Hack to account for the water pins
self.non_Gd_pin_IDs_to_qty[2] -= 4
# Double the quantities of pins to account for a full bundle
for id in self.Gd_pin_IDs_to_qty.iterkeys():
self.Gd_pin_IDs_to_qty[id] *= 2
for id in self.non_Gd_pin_IDs_to_qty.iterkeys():
self.non_Gd_pin_IDs_to_qty[id] *= 2
# Create a dictionary with key-value pairs of enrichment (w/o) and cost ($/kgU) - 10/7/2012
U_cost = {2.0 : 727.36, 2.1 : 777.87, 2.2 : 828.67, 2.3 : 879.74, 2.4 : 931.06,
2.5 : 982.60, 2.6 : 1034.36, 2.7 : 1086.31, 2.8 : 1138.44, 2.9 : 1190.74,
3.0 : 1243.20, 3.1 : 1295.81, 3.2 : 1384.55, 3.3 : 1401.43, 3.4 : 1454.43,
3.5 : 1507.54, 3.6 : 1560.77, 3.7 : 1614.09, 3.8 : 1667.52, 3.9 : 1721.04,
4.0 : 1774.65, 4.1 : 1828.34, 4.2 : 1882.12, 4.3 : 1935.97, 4.4 : 1989.89,
4.5 : 2042.89, 4.6 : 2097.96, 4.7 : 2152.08, 4.8 : 2206.28, 4.9 : 2260.53}
pin_radius = 0.44 # cm
pin_length = 409 # cm
pin_area = math.pi * pin_radius**2 # cm^2
pin_volume = pin_area * pin_length # cm^3
rho_non_Gd_pins = 10.5 # g/cm^3
rho_Gd_pins = 10.2 # g/cm^3
tot_cost = 0.0
burnup_cost = 0.0
non_Gd_pin_mass = pin_volume * rho_non_Gd_pins * 0.001 # kg
Gd_pin_mass = pin_volume * rho_Gd_pins * 0.001 # kg
tot_fuel_mass = 0
# loop over the non-Gd pins and add up the cost
for id in self.non_Gd_pin_IDs_to_qty.iterkeys():
tot_cost += self.non_Gd_pin_IDs_to_qty[id] * non_Gd_pin_mass * U_cost[self.non_Gd_pin_IDs_to_enr[id]]
tot_fuel_mass += non_Gd_pin_mass
for id in self.Gd_pin_IDs_to_qty.iterkeys():
tot_cost += self.Gd_pin_IDs_to_qty[id] * Gd_pin_mass * U_cost[self.Gd_pin_IDs_to_enr[id]]
tot_fuel_mass += Gd_pin_mass
# convert cost to cents / kW-hr
burnup_cost = (100*tot_cost) / (eol_burnup*tot_fuel_mass*24*1000)
print '\tTot. Fuel Cost = $' + str(int(tot_cost)) + ' = ' + str(burnup_cost)[0:5] + ' [cents / kWhr]'
# compute the final grade!
grade = 8*(eol_burnup - 46.5) + 4*(1.30 - max_pin_power) + 2*(1.11 - max_k_inf) - 25*burnup_cost
print '\tYour final grade is: \t\t' + str(int(grade))
def main():
print 'parsing command line input...'
# parse commandl line options
try:
opts, args = getopt.getopt(sys.argv[1:], "p:d:i:", ["home_dir", "password", "inputfile"])
except getopt.GetoptError, err:
print str(err)
usage()
sys.exit(2)
pass_word = ''
home_dir = ''
input_file = ''
for o, a in opts:
if o in ("-p", "--password"):
pass_word = a
elif o in ("-d", "--home_dir"):
home_dir = a
elif o in ("-i", "--input_file"):
input_file = a
else:
assert False, "unhandled option"
bundle = Bundle(pass_word, home_dir, input_file)
bundle.makeGeometry()
bundle.runCasmo()
bundle.parseOutput()
bundle.computeGrade()
if __name__ == '__main__':
main()