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