-
Notifications
You must be signed in to change notification settings - Fork 0
/
functionaries.py
368 lines (344 loc) · 15.1 KB
/
functionaries.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
# -*- coding: utf-8 -*-
__author__ = 'GRIDDIC'
import os
import numpy as np
import obr
from os.path import join as pjoin
from matplotlib import pyplot as plt
from matplotlib.mlab import griddata
from scipy.interpolate import griddata as griddata_sc
import itertools
FIGURES_SIZE = (6,4)
LEGENDS_SIZE = 12
def __parse_name(file_name):
ans = {}
name = os.path.split(file_name)[1]
ans['mode'] = name[:2]
ans['is_carbon'] = name.find('C') != -1
if ans['is_carbon']:
ans['length'] = int(name[3:-1])
else:
ans['length'] = int(name[2:-1])
return ans
def parse_file_name(file_name, what_to_return):
ans = []
p = __parse_name(file_name)
for ret in what_to_return:
ans.append(p[ret])
return ans
def construct_initial_spectrum_tally(file_, x, y, z):
tallies = obr.construct_tallies(file_)
INITIAL_SPECTRUM_TALLY = None
for tally in tallies:
if tally.x == x and tally.y == y and tally.z == z:
INITIAL_SPECTRUM_TALLY = tally
break
assert INITIAL_SPECTRUM_TALLY is not None, "No tally with such coordinates."
return INITIAL_SPECTRUM_TALLY
def sample_length_and_mode_by_file_name(file_name):
name = os.path.split(file_name)[1]
if len(name) == 4:
length = int(name[2:3])
else:
length = int(name[2:4])
return length, name[:2]
def norm(arr):
arr = np.array(arr).astype(float)
return arr/np.sum(arr)
def extend_x_y(x,y):
if x == 0 and y == 0:
yield x, y
return
if x == 0:
yield x, y
yield x, -y
yield y, x
yield -y, x
return
if y == 0:
yield y, x
yield y, -x
yield x, y
yield -x, y
return
if y == x:
yield x, y
yield x, -y
yield -x, y
yield -x, -y
return
yield x, y
yield x, -y
yield -x, y
yield -x, -y
yield y, x
yield y, -x
yield -y, x
yield -y, -x
return
def plot_en_spectrums_in_back_going_flow(file_name, tally_distances, colors, file_without_sample, folder_to_place_images):
tallies = obr.construct_tallies(file_name)
sample_length, mode = sample_length_and_mode_by_file_name(file_name)
title = 'back flow.spectrum.sample len = ' + str(sample_length) + 'cm. mode = ' + mode
outt = open(pjoin(folder_to_place_images,title + ".csv"), 'w')
outt.write("distance_to_the_tally,E_min,E_max,value,dispersion\n")
#ax = plt.figure()
for ind in range(len(tally_distances)):
tally_distance = tally_distances[ind]
col = colors[ind]
import matplotlib.pyplot as plt
left = []
width = []
values = []
dy = []
mid = []
x = 0
y = 0
z = 100 - tally_distance - 1.5
etalon = construct_initial_spectrum_tally(file_without_sample, x, y, z)
count_tallies = 0
for tally in tallies:
if tally.x == x and tally.y == y and tally.z == z:
count_tallies += 1
kk = tally.values.keys()
kk.sort()
kk = sorted(kk)
#print [x[0] for x in kk]
for en_diap in kk:
outt.write(str(tally_distance) + ',' + str(en_diap[0]) + ',' + str(en_diap[1]) + ',' + str(tally.values[en_diap]) + ',' + str(tally.dispersion[en_diap]) + '\n')
if en_diap[0] > 7:
continue
if mode == 'PP':
if en_diap[0] > 2:
continue
left.append(en_diap[0])
width.append(en_diap[1] - en_diap[0])
values.append((tally.values[en_diap] - etalon.values[en_diap]) * (en_diap[1] - en_diap[0]) / 4)
dy.append(tally.dispersion[en_diap]*values[-1] * (en_diap[1] - en_diap[0]))
mid.append((en_diap[1] + en_diap[0])/2)
if count_tallies == 0:
print "Warning! No tallies for distance {len} found".format(len=tally_distance)
plt.bar(left, values,width, color=col, yerr=dy, log=True, bottom=0.000000001, label=('distance to detector = ' + str(tally_distance) + 'cm.'))
#plt.errorbar(left, values, color=col, yerr=dy, label=('distance to detector = ' + str(tally_distance) + 'cm.'))
#plt.yscale('log')
#plt.ylim([10**(-11),10**(-6)])
plt.title(r'back flow.spectrum.\\sample len = ' + str(sample_length) + r'cm. mode = ' + mode)
plt.xlabel(r'Energy, MEV')
plt.ylabel(r'particles, $\bf \frac{F}{cm^2 sec MEV}$')
outt.close()
plt.legend()
plt.grid()
#plt.xscale('log')
plt.savefig(pjoin(folder_to_place_images,title + ".png"), dpi = 300)
print pjoin(folder_to_place_images,title + ".png")
plt.show()
plt.close()
return left, values, width
def plot_sum_en_spectrum_in_back_going_flow(file_names, tally_distances, colors, files_without_sample, folder_to_place_images):
neutron_file, photon_file = file_names
etalon_n_file, etalon_p_file = files_without_sample
if os.path.split(neutron_file)[1][1:] != os.path.split(photon_file)[1][1:]:
print "files_not_correspond_to_each_other"
return
tallies_n, tallies_p = obr.construct_tallies(neutron_file), obr.construct_tallies(photon_file)
sample_length, garbage = sample_length_and_mode_by_file_name(file_names[0])
title = 'back flow.spectrum.sample len = ' + str(sample_length) + 'cm. mode = full'
outt = open(pjoin(folder_to_place_images, title + ".csv"), 'w')
outt.write("distance_to_the_tally,E_min,E_max,value,dispersion\n")
for ind in range(len(tally_distances)):
tally_distance = tally_distances[ind]
col = colors[ind]
import matplotlib.pyplot as plt
left = []
width = []
values = []
dy = []
mid = []
x = 0
y = 0
z = 100 - tally_distance - 1.5
etalon_n = construct_initial_spectrum_tally(etalon_n_file,x,y,z)
etalon_p = construct_initial_spectrum_tally(etalon_p_file,x,y,z)
for tally_ind, tally in enumerate(tallies_n):
if tallies_n[tally_ind].x == x and tallies_n[tally_ind].y == y and tallies_n[tally_ind].z == z:
if tallies_p[tally_ind].x != x or tallies_p[tally_ind].y != y or tallies_p[tally_ind].z != z:
print "wrong tallies order"
return
kk = tallies_n[tally_ind].values.keys()
kk.sort()
for en_diap in kk:
outt.write(str(tally_distance) + ',' + str(en_diap[0]) + ',' + str(en_diap[1]) + ',' + str(tally.values[en_diap]) + ',' + str(tally.dispersion[en_diap]) + '\n')
if en_diap[0] > 7:
continue
left.append(en_diap[0])
width.append(en_diap[1] - en_diap[0])
values.append((tallies_n[tally_ind].values[en_diap] - etalon_n.values[en_diap] +
(tallies_p[tally_ind].values[en_diap] - etalon_p.values[en_diap])/4)
* (en_diap[1] - en_diap[0]))
temp = (tallies_n[tally_ind].dispersion[en_diap]*(tallies_n[tally_ind].values[en_diap] - etalon_n.values[en_diap]) + tallies_p[tally_ind].dispersion[en_diap]*(tallies_p[tally_ind].values[en_diap] - etalon_p.values[en_diap])/4 ) * (en_diap[1] - en_diap[0])
dy.append(min(temp, values[-1]))
mid.append((en_diap[1] + en_diap[0])/2)
plt.bar(left, values,width, color=col, yerr=dy, log=True, bottom=0.000000001, label=('distance to detector = ' + str(tally_distance) + 'cm.'))
#plt.errorbar(left, values, color=col, yerr=dy, label=('distance to detector = ' + str(tally_distance) + 'cm.'))
#plt.yscale('log')
#plt.ylim([10**(-11),10**(-6)])
plt.title(r'back flow.spectrum.\\sample len = ' + str(sample_length) + r'cm. mode = full')
plt.xlabel(r'Energy, MEV')
plt.ylabel(r'particles, $\bf \frac{F}{cm^2 sec MEV}$')
#plt.plot(mid, y, 'r--')
#plt.savefig("images\\" + title + ".png", dpi = 300)
#plt.show()
#plt.close()
outt.close()
plt.legend()
plt.grid()
plt.savefig(pjoin(folder_to_place_images, title + ".png"), dpi = 300)
print pjoin(folder_to_place_images, title + ".png")
plt.show()
plt.close()
return left, values, width
def plot_dose_in_back_going_flow_for_each_file(names, tally_distanses, file_without_sample, folder_to_place_images):
plt.figure(figsize=FIGURES_SIZE)
legend = []
for file_name in names:
sample_length, mode = sample_length_and_mode_by_file_name(file_name)
tallies = obr.construct_tallies(file_name)
dosa_at_length = {}
for tally_ind, tally in enumerate(tallies):
if tally.x == 0 and tally.y == 0 and (100 - tally.z - 1.5 in tally_distanses):
etalon = construct_initial_spectrum_tally(file_without_sample,
tally.x,
tally.y,
tally.z)
dosa, dispersion = (tally.get_dose() - etalon.get_dose())
if mode == 'PP':
dosa = dosa/4.
dosa_at_length[100 - tally.z - 1.5] = (dosa, dispersion)
x = sorted(dosa_at_length.keys())
y = [dosa_at_length[k][0] for k in x]
dy = [dosa_at_length[k][0]*dosa_at_length[k][1] for k in x]
plt.yscale('log')
plt.errorbar(x,y,yerr=dy)
plt.xlabel('Distance to detector, sm.')
plt.ylabel(r'Dose $\bf \frac{mkr}{sec}$')
plt.grid('on')
legend.append("Sample length = " + str(sample_length))
title = "Dose in back going flow mode = " + mode
plt.title(title)
plt.legend(legend, loc='best', prop={'size':LEGENDS_SIZE})
plt.gcf().subplots_adjust(bottom=0.2, top=0.8, left=0.2)
plt.savefig(pjoin(folder_to_place_images,title.replace(' ','_') + ".png"), dpi = 300)
plt.show()
def plot_full_dose_in_back_going_flow_for_each_file(names, tally_distanses, files_without_sample, folder_to_place_images):
plt.figure(figsize=FIGURES_SIZE)
legend = []
etalon_n_file, etalon_p_file = files_without_sample
for neutron_file, photon_file in names:
sample_length, garbage = sample_length_and_mode_by_file_name(neutron_file)
tallies_n, tallies_p = obr.construct_tallies(neutron_file), obr.construct_tallies(photon_file)
dosa_at_length = {}
for tally_ind, (tally_n, tally_p) in enumerate(zip(tallies_n, tallies_p)):
if tally_n.x == 0 and tally_n.y == 0 and (100 - tally_n.z - 1.5 in tally_distanses):
etalon_n = construct_initial_spectrum_tally(etalon_n_file,
tally_p.x,
tally_p.y,
tally_p.z)
etalon_p = construct_initial_spectrum_tally(etalon_p_file,
tally_p.x,
tally_p.y,
tally_p.z)
dosa, dispersion = tally_n.get_dose() - etalon_n.get_dose() + (tally_p.get_dose() - etalon_p.get_dose())/4.
dosa_at_length[100 - tally_n.z - 1.5] = (dosa, dispersion)
x = sorted(dosa_at_length.keys())
y = [dosa_at_length[k][0] for k in x]
dy = [dosa_at_length[k][0]*dosa_at_length[k][1] for k in x]
plt.yscale('log')
plt.errorbar(x,y,yerr=dy)
plt.xlabel('Distance to detector, sm.')
plt.ylabel(r'Dose $\bf \frac{mkr}{sec}$')
plt.grid('on')
legend.append("Sample length = " + str(sample_length))
title = "Dose in back going flow mode = full"
plt.title(title)
plt.legend(legend, loc='best', prop={'size':LEGENDS_SIZE})
plt.gcf().subplots_adjust(bottom=0.2, top=0.8, left=0.2)
plt.savefig(pjoin(folder_to_place_images,title.replace(' ','_') + ".png"), dpi = 300)
plt.show()
def extend_x_y(x,y):
if x == 0 and y == 0:
yield x, y
return
if x == 0:
yield x, y
yield x, -y
yield y, x
yield -y, x
return
if y == 0:
yield y, x
yield y, -x
yield x, y
yield -x, y
return
if y == x:
yield x, y
yield x, -y
yield -x, y
yield -x, -y
return
yield x, y
yield x, -y
yield -x, y
yield -x, -y
yield y, x
yield y, -x
yield -y, x
yield -y, -x
return
def plot_dose_after_the_sample(names, FOLDER_TO_SAVE_IMAGES, subpl=None, interp_method='linear'):
counter = 1
if subpl is not None:
plt.figure(figsize=(FIGURES_SIZE[0]*subpl[1], FIGURES_SIZE[0]*subpl[0]))
else:
plt.figure(figsize=FIGURES_SIZE)
for neutron_file, photon_file in names:
if subpl is not None:
plt.subplot(subpl[0], subpl[1], counter)
counter += 1
sample_length, garbage = sample_length_and_mode_by_file_name(neutron_file)
tallies_n, tallies_p = obr.construct_tallies(neutron_file), obr.construct_tallies(photon_file)
xs = []
ys = []
doses = []
for tally_n, tally_p in zip(tallies_n, tallies_p):
assert tally_n.is_the_same(tally_p), "Tallies are not correspond to each other."
if abs(tally_n.x) > 20 or abs(tally_n.y) > 20 or tally_n.z < 100:
continue
dose = tally_n.get_dose()[0] + tally_p.get_dose()[0]/4.
for x,y in extend_x_y(tally_n.x,tally_n.y):
xs.append(x)
ys.append(y)
doses.append(dose)
xis = np.linspace(-20,20,100)
yis = np.linspace(-20,20,100)
#zis = griddata(xs,ys,doses,xis,yis,interp='linear')
#zis = griddata(xs,ys,doses,xis,yis,interp='nn')
zis = griddata_sc(np.array(list(zip(xs, ys))), np.array(doses), np.array(list(itertools.product(xis, yis))), method=interp_method, fill_value=0).reshape((100,100))
CS = plt.contour(xis, yis, zis, 5, linewidths=0.5, colors='b')
CS = plt.contourf(xis, yis, zis, 5, cmap=plt.cm.rainbow,
vmax=abs(zis).max(), vmin=-abs(zis).max())
cbar = plt.colorbar()
plt.scatter(xs,ys,c=doses, cmap=plt.cm.rainbow, vmax=abs(zis).max(), vmin=-abs(zis).max())#, s=norm(doses)*30000)
plt.xlim(-21,21)
plt.ylim(-21,21)
cbar.set_label(r'$\frac{mkr}{sec}$')
title = "Dose after the sample \n sample length = " + str(sample_length) + ' mode = full'
plt.title(title)
plt.xlabel("'x' dimension, sm")
plt.ylabel("'y' dimension, sm")
if subpl is None:
plt.gcf().subplots_adjust(bottom=0.2, top=0.8, left=0.2)
plt.savefig(pjoin(FOLDER_TO_SAVE_IMAGES, title.replace('\n','_').replace(' ','_') + '.png'), dpi=300)
plt.show()
if subpl is not None:
plt.savefig(pjoin(FOLDER_TO_SAVE_IMAGES, 'Doses_after_the_sample.' + '.png'), dpi=300)