EGS Brachy
An egs++ user code for rapid brachytherapy calculations
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
iaea.py
Go to the documentation of this file.
1import ctypes
2import glob
3import os
4import sys
5
6HEN_HOUSE = os.getenv("HEN_HOUSE")
7try:
8 if os.name == "nt":
9 IAEA_DLL = glob.glob(os.path.join(HEN_HOUSE,"egs++/dso/*/", "iaea_phsp.dll"))[0]
10 else:
11 IAEA_DLL = glob.glob(os.path.join(HEN_HOUSE,"egs++/dso/*/", "libiaea_phsp.so"))[0]
12except IndexError:
13 raise ValueError("iaea_phsp shared library not found")
14from ctypes import byref
15
16import os
17
18from . import iaea_errors
19from . import iaea_types
20
21
22iaeadll = ctypes.CDLL(IAEA_DLL)
23
24
25class IAEAPhaseSpace(object):
26
27 header_ext = '.IAEAheader'
28 phsp_ext = '.IAEAphsp'
29
30 #--------------------------------------------------------------------------
31 def __init__(self,path,mode='r'):
32 """Set up access to an IAEA phase space file
33
34 Arguments:
35 path -- The path to the iaea phase space file
36
37 Keyword arguments:
38 mode -- 'r' for read, 'w' for read/write or 'a' for 'append' (default 'r')
39
40 """
41 self._set_path(path)
42 self._source_id = iaea_types.IAEA_I32(-1)
43
44 try:
45 self.access = iaea_types.iaea_file_modes[mode]
46 except:
47 err_msg = 'Invalid file mode specified: %s' % mode
49
50 self._create_source()
51 #--------------------------------------------------------------------------
52 def _create_source(self):
53 result = iaea_types.IAEA_I32(0)
54 iaeadll.iaea_new_source(byref(self._source_id), self.path.encode(), byref(self.access),
55 byref(result), ctypes.c_int(len(self.path.encode())))
56
57 print( result)
58 if 0 > result.value > iaea_types.max_sources:
59 raise iaea_errors.IAEAPhaseSpaceError(result.value)
60 #--------------------------------------------------------------------------
61 def num_particles(self,particle_type = 'all'):
62 """Return max number of particles of type particle_type
63
64 Keyword arguments:
65 particle_type -- type or category of particle to check (default 'all')
66
67 """
68
69 num_particles = 0
70 np = iaea_types.IAEA_I64(0)
71
72 try:
73 ptypes = tuple(iaea_types.particle_types[particle_type])
74 except TypeError:
75 ptypes = (iaea_types.particle_types[particle_type],)
76
77 for ptype in ptypes:
78 ptype = iaea_types.IAEA_I32(ptype)
79 iaeadll.iaea_get_max_particles(byref(self._source_id),byref(ptype),byref(np))
80 num_particles += np.value
81
82 if num_particles < 0:
83 message = "Unable to read number of particles of type: %s" % particle_type
84 raise iaea_errors.IAEAPhaseSpaceError(message=message)
85
86 return num_particles
87 #--------------------------------------------------------------------------
89 """Return max number of particles of type particle_type
90
91 Keyword arguments:
92 particle_type -- type or category of particle to check (default 'all')
93
94 """
95
96 num_particles = 0
97 np = iaea_types.IAEA_I64(0)
98
99 iaeadll.iaea_get_total_original_particles(byref(self._source_id), byref(np))
100 num_particles = np.value
101
102 if num_particles < 0:
103 message = "Unable to read number of particles of type: %s" % particle_type
104 raise iaea_errors.IAEAPhaseSpaceError(message=message)
105
106 return num_particles
107 #----------------------------------------------------------------------
108 def maximum_energy(self):
109 """Return maximum energy in this source in (MeV)"""
110
111 emax = iaea_types.IAEA_Float()
112 iaeadll.iaea_get_maximum_energy(byref(self._source_id),byref(emax))
113 if emax.value <0.:
114 raise iaea_errors.IAEAPhaseSpaceError(message="Source not initialized")
115
116 return emax.value
117 #--------------------------------------------------------------------------
118 @property
119 def source_id(self):
120 return self._source_id.value
121 #--------------------------------------------------------------------------
122 def _set_path(self,path):
123 self.path = os.path.realpath(path)
124 self.path = self.path.replace(self.header_ext,"").replace(self.phsp_ext, "")
num_particles(self, particle_type='all')
Definition iaea.py:61
__init__(self, path, mode='r')
Definition iaea.py:31