-
Notifications
You must be signed in to change notification settings - Fork 3
/
MCNP5_script.m
548 lines (453 loc) · 18.5 KB
/
MCNP5_script.m
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
function MCNP5_script(s, type)
% Created by Patrick Snouffer Spring 2008
% Uses the classes CoordinateSystems, XYZCoorSys, CylCoorSys, and SphCoorSys
% It also uses the function read_tallies. This script reads in MCNP5 mesh
% files and then allows the user to add, average, and/or write out the
% final tallies in the same text format as the original files. As the
% program reads in, adds, or averages the tallies, it saves each of the
% tallies as a .mat file so the program does not over load.
% A few things to note:
% ~ all user entered files should be without the extension
% ~ there is no check to make sure that the tallies being added or
% averaged are of the same size, coordinate system, or tally type
% ~ when reading in files their names must be indexed without an
% extension
% ~ other functions can only be done to files that have been read in
disp(nargin);
if nargin == 1
usage();
elseif strcmpi(type,'file')==1
fid = fopen(s);
while(~feof(fid))
line = fgetl(fid);
[word, last] = strtok(line);
sc(word, last);
end
fclose(fid);
elseif strcmp(type,'command')
[word, last] = strtok(s);
sc(word, last);
else
usage();
end
end
function usage()
disp('ERROR: bad function call');
disp('Usage: MCNP5_script(''input_script_name'',''file'') or');
disp(' MCNP5_script(''single_command'',''command'')');
end
function sc(first, last)
switch lower(first)
case {'#',''}
case 'read'
disp('Reading meshtal files');
sd = textscan(last, '%s %f %f %f');
rootFile = char(sd{1});
startF = sd{2};
nFiles = sd{3};
saveByFile = sd{4};
if numel(saveByFile) == 0
saveByFile = 1;
end
read_tallies(rootFile, startF, nFiles, saveByFile );
clear sd rootFile startF nFiles saveByFile
case 'add'
disp('Adding tallies');
[sd, last] =strtok(last);
numFiles = str2double(sd);
[sd, pos] = textscan(last, '%s', numFiles);
fileNames = sd{1};
sd = textscan(last(pos+1:end), '%f', 1);
numTallies = sd{1,1};
[sd, pos2] = textscan(last(pos+3:end), '%f', numTallies);
tallyNums = sd{1};
sd = textscan(last(pos+3+pos2:end), '%s %f');
outFile = sd{1};
style = sd{2};
total = CoordinateSystems();
% if files saved by index set up names
if (style ~= 1)
for i = 1 : numFiles
for j = 1 : numTallies;
fName = strcat(fileNames{i}, 'tally', num2str(tallyNums{j}), '.mat');
load(fName);
total = total.AddResults(tally);
clear tally
end
end
else
for i = 1 : numFiles;
fName = strcat(fileNames{i}, 'tally.mat');
load(fName);
for j = 1 : numTallies;
talNum = tallyNums(j);
mesh = tally(talNum);
total = total.AddResults(mesh);
clear mesh
end
clear tally
end
end
if exist(strcat(outFile{1},'tally.mat'), 'file')
load(strcat(outFile{1},'tally.mat'));
tally(length(tally)+1) = total;
save(strcat(outFile{1}, 'tally.mat'), 'tally');
disp(['Fianl tally saved to file ', outFile{1},'tally.mat',...
' in position ', num2str(length(tally))]);
else
tally = total;
save(strcat(outFile{1}, 'tally.mat'), 'tally');
disp(['Final tally saved as ', outFile{1}, 'tally.mat']);
end
disp(' ');
clear tally total fName talNum style outFile numTallies fileNames numFiles sd
% use modify this code if want to be able to add different tally
% positions from different files
% [sd, pos] = textscan(last, '%f %f %f');
% nF = sd{1};
% style = sd{3};
% formStr = '%s';
% for i=1: sd{2};
% formStr = [formStr, ' %f'];
% end
% disp(formStr);
% [sd, pos2] = textscan(last(pos+1:end), formStr, nF, 'CollectOutput', 1);
% sd1 = textscan(last(pos+pos2+1:end), '%s');
% files = sd{1};
% talNums = sd{2};
case 'average'
disp('Averaging tallies');
sd = textscan(last, '%s %f %f %s %f');
rootF = sd{1};
startF = sd{2};
numF = sd{3};
outF = sd{4};
style = sd{5};
if style ~=1
h = 1;
ex = exist([rootF{1},num2str(startF),'tally1.mat'],'file');
while(ex == 1)
average = CoordinateSystems();
for g = startF : (numF + startF - 1)
load([rootF{1},num2str(g),'tally',num2str(h),'.mat']);
average = average.AverageResults(tally);
clear tally;
end
h = h + 1;
tally = average;
ex = exist([rootF{1},num2str(startF),'tally',num2str(h), ...
'.mat'],'file');
save([outF{1}, 'tally', num2str(h),'.mat'],'tally');
clear tally average;
end
else
load([rootF{1},num2str(startF),'tally.mat']);
for h = 1 : length(tally)
average = CoordinateSystems();
for j = startF : (numF + startF-1)
load([rootF{1},num2str(j),'tally.mat']);
average = average.AverageResults(tally(h));
clear tally;
end
ave(h) = average;
clear tally average;
end
tally = ave;
save([outF{1},'tally.mat'],'tally');
clear tally ave;
end
disp(['Tallies ',num2str(1),' through ',num2str(h),...
' were averaged over ']);
disp([num2str(numF),' files and saved with root file name ',outF{1}]);
disp(' ');
clear numF rootF ex h outF style startF sd
%end of averaging case
case 'trim'
disp('Trimming tallies');
sd = textscan(last, '%f %f %f %f %f %f %s %f %f %f %f');
dA = [sd{1} sd{2}];
dB = [sd{3} sd{4}];
dC = [sd{5} sd{6}];
rootF = sd{7};
startF = sd{8};
numF = sd{9};
talPos = sd{10};
style = sd{11};
if style ~=1
disp(' NOT SUPPORTED FOR STYLE ~= 1');
else
for j = startF : (numF + startF-1)
load([rootF{1},num2str(j),'tally.mat']);
tally(talPos) = tally(talPos).TrimMesh(dA,dB,dC);
save(['trim',rootF{1},num2str(j),'tally.mat'], 'tally');
clear tally;
end
end
clear numF rootF talPos style startF sd dA dB dC
%end of trimming case
case 'mult'
disp('Multipling tallies by constant');
[sd, pos] = textscan(last, '%f %f');
mult = sd{1};
numFiles = sd{2};
[sd, pos2] = textscan(last(pos+1:end), '%s', numFiles);
fileNames = sd{1};
sd = textscan(last(pos+pos2:end), '%f', 1);
numTallies = sd{1};
[sd, pos3] = textscan(last(pos+pos2+2:end), '%f', numTallies);
tallyNums = sd{1};
sd = textscan(last(pos+pos2+pos3+2:end), '%f');
style = sd{1};
if style ~=1
for j = 0 : numFiles - 1
for i = 1 : numTallies
load([fileNames{j+1},'tally', num2str(tallyNums(i)), '.mat']);
tally = tally.Multiplier(mult);
end
k = strfind(fileNames{j+1},'mult');
if(isempty(k) || k(1) ~= 1)
save(['mult',fileNames{j+1},'tally', num2str(tallyNums(i)), '.mat'], 'tally');
disp(['Multiplied tally', fileNames{j+1},'tally', num2str(tallyNums(i)), '.mat'...
'by ', num2str(mult), ' and saved as mult',fileNames{j+1}, 'tally',...
num2str(tallyNums(i)), '.mat']);
else
save([fileNames{j+1},'tally.mat'], 'tally');
disp(['Multiplied tallies', str, 'of ', fileNames{j+1}, ...
'tally.mat saved as ',fileNames{j+1}, 'tally.mat']);
end
clear tally;
end
else
for j = 0 : numFiles - 1
load([fileNames{j+1},'tally.mat']);
str = ' ';
for i = 1 : numTallies
tally(tallyNums(i)) = tally(tallyNums(i)).Multiplier(mult);
str = [str, num2str(tallyNums(i)), ' '];
end
k = strfind(fileNames{j+1},'mult');
if(isempty(k) || k(1) ~= 1)
save(['mult',fileNames{j+1},'tally.mat'], 'tally');
disp(['Multiplied tallies', str, 'of ', fileNames{j+1}, ...
'tally.mat saved as mult',fileNames{j+1}, 'tally.mat']);
else
save([fileNames{j+1},'tally.mat'], 'tally');
disp(['Multiplied tallies', str, 'of ', fileNames{j+1}, ...
'tally.mat saved as ',fileNames{j+1}, 'tally.mat']);
end
clear tally;
end
end
disp(' ');
clear fileNames tallyNums mult str k style sd numTallies numFiles pos pos2 pos3
case 'percent'
disp('Finding percent differences of tallies');
[sd, pos] = textscan(last, '%s %f', 1);
bName = sd{1};
numFiles = sd{2};
[sd, pos2] = textscan(last(pos+1:end), '%s', numFiles);
fileNames = sd{1};
sd = textscan(last(pos+pos2:end), '%f', 1);
numTallies = sd{1};
[sd, pos3] = textscan(last(pos+pos2+2:end), '%f', numTallies);
tallyNums = sd{1};
sd = textscan(last(pos+pos2+pos3+2:end), '%f');
style = sd{1};
str1 = '';
str2 = '';
if style ~= 1
for i = 1 : numTallies
load([bName{1}, 'tally',num2str(tallyNums(i)),'.mat']);
bench = tally;
for j = 1 : numFiles
load([fileNames{j},'tally',num2str(tallyNums(i)),'.mat']);
tally = tally.PercentDiff(bench);
save(['percent',fileNames{j},'tally',num2str(tallyNums(i)),'mat']);
if i == 1
str2 = [str2, ' ',fileNames{j}];
end
clear tally
end
clear bench
str1 = [str1, ' ', num2str(tallyNums(i))];
end
disp(['% difference taken for tallies ',str1, ' from files ', ...
str2]);
else
load([bName{1},'tally.mat']);
bench = tally;
for j = 1 : numFiles
load([fileNames{j},'tally.mat']);
for i = 1 : numTallies
tally(i)=tally(i).PercentDiff(bench(i));
if j == 1
str1 = [str1, ' ', num2str(tallyNums(i))];
end
end
str2 = [str2, ' ',fileNames{j}];
save(['percent',fileNames{j},'tally.mat']);
clear tally
end
disp(['% difference taken for tallies ',str1, ' from files ', ...
str2]);
clear bench
end
disp(' ');
clear str1 str2 numTallies numFiles bName fileNames tallyNums sd pos pos2 pos3
case 'write'
disp('Writing tally');
sd = textscan(last, '%s %s %f %f %f %f');
outF = sd{1};
fName = sd{2};
start = sd{3};
numTal = sd{4};
writeOp = sd{5};
style = sd{6};
if exist(outF{1}, 'file') ~= 0
if writeOp == 2
delete(outF{1})
disp(['File ', outF{1} ' will be overwritten']);
elseif writeOp == 1
disp(['Tallies will be added to the end of file ', outF{1}]);
else
disp(['***** ',outF{1},' exists and no overwrite or apend option was given', ...
' so no tallies were written']);
end
end
if style ~= 1
for i = start : (start + numTal - 1)
load([fName{1},'tally',num2str(i),'.mat']);
write2file(tally, outF{1});
end
else
load([fName{1},'tally.mat']);
for i = start : (start + numTal - 1)
write2file(tally(i), outF{1});
end
end
fin = start+numTal-1;
disp(['Tallies ',num2str(start),' to ', num2str(fin), ...
' from ', char(fName) ,'tally.mat were written to ', outF{1}]);
disp(' ');
clear fName fin start outF numTal writeOp style sd
case 'thzslice'
sd = textscan(last, '%s %f%f %f %f %f');
fName = sd{1};
talNum = sd{2};
th = sd{3};
z = sd{4};
energy = sd{5};
style = sd{6};
if (th < 0 || th > 1)
disp('***** Theta is not between 0 and 1. ');
res = 0;
elseif style ~=1
load([fName{1},'tally',num2str(talNum),'.mat']);
res = tally.plotThZSlice(th,z,energy);
else
load([fName{1},'tally.mat']);
res = tally(talNum).plotThZSlice(th,z,energy);
end
if res == 0
disp(['Tally ', num2str(talNum), ' from file ', fName{1}, ' was not plotted']);
disp(' ');
end
clear sd fName talNum th z energy style res
case 'thslice'
sd = textscan(last, '%s %f %f %f %f');
fName = sd{1};
talNum = sd{2};
th = sd{3};
energy = sd{4};
style = sd{5};
if (th < 0 || th > 1)
disp('Theta is not between 0 and 1. ');
res = 0;
elseif style ~=1
load([fName{1},'tally',num2str(talNum),'.mat']);
res = tally.plotThSlice(th,energy);
else
load([fName{1},'tally.mat']);
res = tally(talNum).plotThSlice(th,energy);
end
if res == 0
disp(['Tally ', num2str(talNum), ' from file ', fName{1}, ' was not plotted']);
disp(' ');
end
clear sd fName talNum th energy style res
case 'zslice'
sd = textscan(last, '%s %f %f %f %f %s');
fName = sd{1};
talNum = sd{2};
z = sd{3};
energy = sd{4};
style = sd{5};
title = sd{6};
if (isempty(title))
title = ['Radial Flux Profile, Z=',num2str(z),' cm (n/cm^2*s)'];
end
if style ~=1
load([fName{1},'tally',num2str(talNum),'.mat']);
res = tally.plotZSlice(z,energy, title);
else
load([fName{1},'tally.mat']);
res = tally(talNum).plotZSlice(z,energy, title);
end
if res == 0
disp(['Tally ', num2str(talNum), ' from file ', fName{1}, ' was not plotted']);
disp(' ');
end
clear sd fName talNum z energy style res
case 'xslice'
sd = textscan(last, '%s %f %f %f %f');
fName = sd{1};
talNum = sd{2};
x = sd{3};
energy = sd{4};
style = sd{5};
if style ~=1
load([fName{1},'tally',num2str(talNum),'.mat']);
res = tally.plotXSlice(x,energy);
else
load([fName{1},'tally.mat']);
res = tally(talNum).plotXSlice(x,energy);
end
if res == 0
disp(['Tally ', num2str(talNum), ' from file ', fName{1}, ' was not plotted']);
disp(' ');
end
clear sd fName talNum x energy style res
case 'yslice'
sd = textscan(last, '%s %f %f %f %f');
fName = sd{1};
talNum = sd{2};
y = sd{3};
energy = sd{4};
style = sd{5};
if style ~=1
load([fName{1},'tally',num2str(talNum),'.mat']);
res = tally.plotYSlice(y,energy);
else
load([fName{1},'tally.mat']);
res = tally(talNum).plotYSlice(y,energy);
end
if res == 0
disp(['Tally ', num2str(talNum), ' from file ', fName{1}, ' was not plotted']);
disp(' ');
end
clear sd fName talNum y energy style res
otherwise
disp('Bad keyword! Acceptible keywords are:');
disp(' read');
disp(' add');
disp(' average');
disp(' mult');
disp(' percent');
disp(' write');
disp(' thslice');
disp(' zslice');
disp(' thzslice');
disp(' xslice');
disp(' yslice');
end
end