1 '''Defines the BeamTree class for theia.'''
2
3
4
5
6
7
8
9
10
11
12
13
14 from ..helpers import settings
15 from ..helpers.units import mm, deg
16 from ..helpers.tools import formatter
17 from ..helpers.geometry import rectToSph
18
20 '''
21
22 BeamTree class.
23
24 A BeamTree is a binary tree which allows to keep track of the beams as they
25 are traced throughout the optical setup. The Root of the tree is a Gaussian
26 beam and the other attributes are the daughter trees and all the data of the
27 interaction producing these with the Root beam
28
29 *=== Attributes ===*
30 Root: beam of this node of the tree. [GaussianBeam]
31 T: beam resulting from the transmission of the Root beam. [BeamTree]
32 R: beam resulting from the reflection of the Root beam. [BeamTree]
33
34 '''
35
36 - def __init__(self, Root = None,
37 T = None, R = None):
38 '''BeamTree initializer.'''
39 self.Root = Root
40 self.T = T
41 self.R = R
42
44 '''String representation of a BeamTree, for print(tree).
45
46 '''
47 return formatter(self.lines())
48
50 '''Returns the list of lines necessary to print the object.
51
52 '''
53 ans = []
54 ans.append("Tree: {")
55 ans.append("Root beam: " + str(self.Root.Ref))
56 ans.append("Number of beams: " + str(self.numberOfBeams()))
57 ans.append("}")
58
59 return ans
60
62 '''Returns the string representation the tree of beams.
63
64 '''
65 before = ["Tree: Root beam = " + str(self.Root.Ref) + " {"]
66 after = ["}"]
67 return formatter(before + self.beamLines() + after)
68
70 '''Returns the list of lines necessary to print the list of beams.
71
72 '''
73 ans = []
74 if self.Root is not None:
75 ans = self.Root.lines()
76 if self.R is not None:
77 ans = ans + self.R.beamLines()
78 if self.T is not None:
79 ans = ans + self.T.beamLines()
80
81 return ans
82
84 '''Return the total number of beams.'''
85 if self.Root is None:
86 return 0
87
88 if self.T is None and self.R is None:
89 return 1
90 elif self.T is None and self.R is not None:
91 return 1 + self.R.numberOfBeams()
92 elif self.T is not None and self.R is None:
93 return 1 + self.T.numberOfBeams()
94 else:
95 return 1 + self.T.numberOfBeams() + self.R.numberOfBeams()
96
98 '''Return the list of lines necessary to write the output of simulation.
99
100 '''
101 sList = []
102 if self.Root is not None:
103 if self.Root.Optic is not None:
104 if self.Root.Length > 0.:
105 if self.R is not None and self.R.Root is not None:
106 sList = ['(' + self.Root.Optic + ', ' + self.Root.Face \
107 + ') ' + str(self.Root.Length)\
108 + 'm (' + self.R.Root.Optic + ', '\
109 + self.R.Root.Face + ') ' + self.Root.Ref + ' {']
110 elif self.T is not None and self.T.Root is not None:
111 sList = ['(' + self.Root.Optic + ', '+ self.Root.Face \
112 + ') ' + str(self.Root.Length) \
113 + 'm (' + self.T.Root.Optic + ', '+ self.T.Root.Face \
114 + ') ' + self.Root.Ref + ' {']
115 else:
116 sList = ['(' + self.Root.Optic \
117 + ', ' +self.Root.Face + ') ' + str(self.Root.Length) \
118 + 'm [End] ' + self.Root.Ref + ' {']
119 else:
120 sList = ['(' + self.Root.Optic + ', ' +self.Root.Face \
121 + ') [Open] ' + self.Root.Ref + ' {']
122 else:
123 if self.Root.Length > 0.:
124 if self.R is not None and self.R.Root is not None:
125 sList = ['[InBeam] ' + str(self.Root.Length) \
126 + 'm (' + self.R.Root.Optic + ', '+ self.R.Root.Face \
127 + ') ' + self.Root.Ref + ' {']
128 elif self.T is not None and self.T.Root is not None:
129 sList = ['[InBeam] ' + str(self.Root.Length) \
130 + 'm (' + self.T.Root.Optic + ', '+ self.T.Root.Face \
131 + ') ' + self.Root.Ref + ' {']
132 else:
133 sList = ['[InBeam] ' + str(self.Root.Length)\
134 + 'm [End] ' + self.Root.Ref + ' {']
135 else:
136 sList = ['[InBeam] [Open] ' + self.Root.Ref + ' {']
137 sList.append("Waist Pos: " + str(self.Root.waistPos()) + 'm')
138 sList.append("Waist Size: (" \
139 + str(self.Root.waistSize()[0]/mm) + ', ' \
140 + str(self.Root.waistSize()[1]/mm)\
141 + ')mm')
142 sph = rectToSph(self.Root.Dir)
143 sList.append("Direction: (" + str(sph[0]/deg) + ', ' \
144 + str(sph[1]/deg) + ')deg')
145 sList.append('}')
146 if self.R is not None:
147 sList = sList + self.R.outputLines()
148 if self.T is not None:
149 sList = sList + self.T.outputLines()
150
151 return sList
152
153
154 -def treeOfBeam(srcBeam, optList, order, threshold):
155 '''Function to calculate the tree of daughter beams of srcBeam.
156
157 srcBeam: Input beam. [GaussianBeam]
158 optList: List of optical components of the setup. [list of OpticalComponent]
159 order: order of simulation. [integer]
160 threshold: power threshold for daughter beams. [float]
161
162 Returns a BeamTree.
163
164 '''
165
166 if len(optList) == 0:
167
168 return BeamTree(Root = srcBeam)
169
170 if srcBeam is None:
171
172 return BeamTree()
173
174
175 dist = settings.inf
176 finalHit = {}
177 fianlisHit = {}
178 finalOpt = optList[0]
179 hitAtLeastOnce = False
180
181
182 for opt in optList:
183 dicoisHit = opt.isHit(srcBeam)
184 if dicoisHit['isHit'] and dicoisHit['distance'] < dist:
185 hitAtLeastOnce = True
186 dist = dicoisHit['distance']
187 finalOpt = opt
188
189 if not hitAtLeastOnce:
190
191 if settings.info:
192 print "theia: Info: Reached open beam "\
193 + srcBeam.Ref + "."
194 return BeamTree(Root = srcBeam)
195
196
197 finalisHit = finalOpt.isHit(srcBeam)
198 finalHit = finalOpt.hit(srcBeam, order = order, threshold = threshold)
199
200 return BeamTree(Root = srcBeam,
201 T = treeOfBeam(finalHit['t'], optList, order, threshold),
202 R = treeOfBeam(finalHit['r'], optList, order, threshold))
203