-
Notifications
You must be signed in to change notification settings - Fork 7
/
source.py
260 lines (252 loc) · 12 KB
/
source.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
from colour import Color
from pyb import pyb
import numpy as np
class source():
""" ``source`` is an object that defines an MCNP source. Currently there are
only a few implemented type of sources:
- point source: use ``pos`` or ``x``, ``y``, and ``z``
- disk source: use ``pos``, ``shape = 'disk'``, and ``direction``
- cell source: use ``cell`` and an enclosing disk
- energy spectrum: use ``spectrum`` and a ``dist`` object
- intensity distribution:
:param str particle: one of 'n', 'p', 'd', 't', 's', 'a', or 'fission'
:param tup pos: position of the source
:param float x, y, z: position of the source - alternate method
:param spectrum: either a distribution or a two-d list to create an
energy distribution
:param str shape: right now, can only be ``'disk'``, more coming.
:param str direction: one of '+x' or '-x', '+y' or '-y', or '+z' or '-z'
:param str id: an identifying string
:param float radius: the radius of a disk source
:param wig.cell cell: the cell for a volumetric source
:param bool show: whether the source should be rendered or not
.. todo:: implement the whole MCNP Primer of sources
"""
def __init__(self, particle='n', pos=None, x=None, y=None, z=None,
spectrum=None, shape=None, direction=None, id=None,
radius=None, cell=None, show=True, dist_type='C',
axis=None, lx=None, ly=None, lz=None, anisotropic=False,
half_angle=None):
self.show = show
self.blender_cmd = None
self.blender_cmd_args = {}
self.coned = False
if cell is not None:
positioned = False
else:
positioned = True
if pos is None:
self.x = x
self.y = y
self.z = z
else:
self.x = pos[0]
self.y = pos[1]
self.z = pos[2]
self.dist_num = 1
self.dists = []
self.string = ""
self.comment = "c --- %s" % id
if lx is None:
lx = 1.0
if ly is None:
ly = 1.0
if lz is None:
lz = 1.0
types = {"n": 'n', "p": 'p', "e": 3, "fission": 1, "d": 31, "t": 32, "he3": 33, "he4": 34}
colors = {"n": '#7299C6', "p": "#E3AE24", "fission": '#B95915', 'd': "#5C6F7B", 't': "#F8981D"}
self.string += "par=%s " % (types[particle])
color = colors[particle]
if direction is None:
direction = 'isotropic'
if 'cone' in direction:
# extract cone from the direction
self.coned = True
anisotropic = True
direction = direction.replace('cone:', '').replace('cone', '')
# extract angle spread from the direction keyword
half_angle = float(direction.split(':')[1])
direction = direction.split(':')[0]
if direction == '-z' or direction == 'z-':
if anisotropic:
self.string += "vec=0 0 -1 "
if not self.coned:
self.string += "dir=1 "
else:
self.string += "dir=d%d " % self.dist_num
self.axis = (0, 0, lz)
elif direction == '+z' or direction == 'z+':
if anisotropic:
self.string += "vec=0 0 1 "
if not self.coned:
self.string += "dir=1 "
else:
self.string += "dir=d%d " % self.dist_num
self.axis = (0, 0, lz)
elif direction == '+x' or direction == 'x+':
self.string += "vec=1 0 0 dir=1 "
self.axis = (1, 0, lx)
elif direction == '+y' or direction == 'y+':
self.string += "vec=0 1 0 dir=1 "
self.axis = (0, 1, ly)
elif direction == '-y' or direction == 'y-':
self.string += "vec=0 -1 0 dir=1 "
self.axis = (0, 1, ly)
if self.coned:
self.string += "wgt=%e " % (1.0 / (2.0 * np.pi * (1.0 - np.cos(np.radians(half_angle)))/(4.0 * np.pi)))
self.dists.extend([dist([half_angle], [], self.dist_num, dist_type='ipb')])
self.dist_num += 1
if shape == 'disk' and radius is not None:
self.string += "pos=%6.4f %6.4f %6.4f " % (self.x, self.y, self.z)
self.dists.extend([dist([0, radius], [-21, 1], self.dist_num,
format='d')])
self.string += "axs=%d %d %d rad=d%d " % (self.axis[0],
self.axis[1],
self.axis[2],
self.dist_num)
self.dist_num += 1
self.blender_cmd = [pyb.pyb.rcc]
self.blender_cmd_args = [{"c": (self.x, self.y, self.z),
"r": radius, "h": 0.1, "name": id,
"color": color, "direction": direction.replace('-', '').replace('+', ''),
"alpha": 1.0, "emis": True}]
if shape == 'sphere' and radius is not None:
self.string += "pos=%6.4f %6.4f %6.4f " % (self.x, self.y, self.z)
self.dists.extend([dist([0, radius], [-21, 2], self.dist_num,
format='d')])
self.string += "rad=d%d " % (self.dist_num)
self.dist_num += 1
self.blender_cmd = [pyb.pyb.sph]
self.blender_cmd_args = [{"c": (self.x, self.y, self.z),
"r": radius, "name": id,
"color": color, "alpha": 0.1,
"emis": True}]
if shape == 'rect':
self.dists.extend([dist([lx[0], lx[1]], [0, 1], self.dist_num,
format='d')])
self.string += "X=d%d " % self.dist_num
self.dist_num += 1
self.dists.extend([dist([ly[0], ly[1]], [0, 1], self.dist_num,
format='d')])
self.string += "Y=d%d " % self.dist_num
self.dist_num += 1
self.dists.extend([dist([lz[0], lz[1]], [0, 1], self.dist_num,
format='d')])
self.string += "Z=d%d " % self.dist_num
self.dist_num += 1
self.blender_cmd = []
self.blender_cmd_args = [{}]
if shape == 'plane':
if len(lx) > 1:
self.dists.extend([dist([lx[0], lx[1]], [0, 1], self.dist_num,
format='d')])
self.string += "X=d%d " % self.dist_num
self.dist_num += 1
else:
self.string += "X=%f " % lx[0]
lx = [lx[0] - 0.01, lx[0] + 0.01]
if len(ly) > 1:
self.dists.extend([dist([ly[0], ly[1]], [0, 1], self.dist_num,
format='d')])
self.string += "Y=d%d " % self.dist_num
self.dist_num += 1
else:
self.string += "Y=%f " % ly[0]
ly = [ly[0] - 0.01, ly[0] + 0.01]
if len(lz) > 1:
self.dists.extend([dist([lz[0], lz[1]], [0, 1], self.dist_num,
format='d')])
self.string += "Z=d%d " % self.dist_num
self.dist_num += 1
else:
self.string += "Z=%f " % lz[0]
lz = [lz[0] - 0.01, lz[0] + 0.01]
c = [0., 0., 0.]
l = [0., 0., 0.]
c[0] = (lx[1] - lx[0]) / 2.0 + lx[0]
l[0] = lx[1] - lx[0]
c[1] = (ly[1] - ly[0]) / 2.0 + ly[0]
l[1] = ly[1] - ly[0]
c[2] = (lz[1] - lz[0]) / 2.0 + lz[0]
l[2] = lz[1] - lz[0]
self.blender_cmd = [pyb.pyb.rpp]
self.blender_cmd_args = [{"c": c, "l": l, "name": id, "color": color,
"alpha": 0.1, "emis": True}]
color = '#2EAFA4'
if positioned and shape != 'disk' and shape != 'plane' and shape != 'rect' and shape != 'sphere':
self.string += "pos=%6.4f %6.4f %6.4f " % (self.x, self.y, self.z)
self.blender_cmd = [pyb.pyb.sph]
self.blender_cmd_args = [{"c": (self.x, self.y, self.z), "r": 1.0,
"name": id, "color": color,
"alpha": 1.0, "emis": True}]
elif cell is not None:
self.string += "cel=%d " % (cell.cell_num)
if self.x is not None:
self.string += "pos=%6.4f %6.4f %6.4f " % (self.x, self.y, self.z)
self.dists.extend([dist([0, radius], [-21, 1], self.dist_num, format='d')])
self.string += 'rad=d%d ' % (self.dist_num)
self.dist_num += 1
if self.show:
self.blender_cmd = cell.b_cmds
self.blender_cmd_args = cell.b_kwargs
if type(spectrum) is type([]):
self.dists.extend([dist(spectrum[0], spectrum[1], self.dist_num,
dist_type=dist_type)])
self.string += 'erg=d%d ' % self.dist_num
self.dist_num += 1
elif particle == "fission":
self.dists.extend([dist(dist_type='Watt', dist_num=self.dist_num)])
self.string += 'erg=d%d ' % self.dist_num
self.dist_num += 1
elif isinstance(spectrum, float):
self.string += 'erg=%15.10e ' % spectrum
self.string = self.string[:-1]
self.string += '\n'
for _dist in self.dists:
self.string += _dist.dist_string
self.string = self.string[:-1]
class dist():
""" ``dist`` creates a distribution that MCNP uses for its source energy,
or position, or intensity
:param list x: the independent variable values of the distribution
:param list y: the dependent variable values of the distribution
:param int dist_num: the identifying number, usually assigned
automatically by ``wig.source``
:param str dist_type: can be ``'C'``, ``'Maxwellian'``, ``'Watt'``
:param str format: currently can be ``'d'`` to use only integers in the
distribution
.. todo:: implement more distribution types
.. todo:: make semantic names for the distribution types
"""
def __init__(self, x=None, y=None, dist_num=None, dist_type=None,
format=None):
self.dist_num = dist_num
self.dist_string = ''
if dist_type is None or len(dist_type) == 1:
if dist_type is None:
self.dist_string += 'si%d ' % (self.dist_num)
else:
self.dist_string += 'si%d ' % (self.dist_num)
for _x in x:
self.dist_string += "%15.10e " % (_x)
self.dist_string = self.dist_string[:-1]
self.dist_string += '\n'
self.dist_string += 'sp%d ' % (self.dist_num)
for _y in y:
if format is None:
self.dist_string += '%15.10e ' % (_y)
elif format == 'd':
self.dist_string += '%d ' % (_y)
self.dist_string = self.dist_string[:-1]
elif dist_type is "Maxwellian":
self.dist_string += 'sp%d -2 %f' % (self.dist_num, a)
elif dist_type is "Watt":
# A and B for U-235 induced fission
a = 0.988
b = 2.249
self.dist_string += 'sp%d -3 %e %e' % (self.dist_num, a, b)
elif dist_type is "ipb":
self.dist_string += 'si%d -1.0 %e 1.0\n' % (self.dist_num, np.cos(np.radians(x[0])))
self.dist_string += 'sp%d 0.0 %e %e\n' % (self.dist_num, 2.0 * np.pi * (1.0 - np.cos(np.pi - np.radians(x[0])))/(4.0 * np.pi), 2.0 * np.pi * (1.0 - np.cos(np.radians(x[0])))/(4.0 * np.pi))
self.dist_string += 'sb%d 0.0 0.0 1.0' % (self.dist_num)
self.dist_string += '\n'