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 Name: class attribute, name of object. [string]
31 Root: beam of this node of the tree. [GaussianBeam]
32 T: beam resulting from the transmission of the Root beam. [BeamTree]
33 R: beam resulting from the reflection of the Root beam. [BeamTree]
34
35 '''
36 Name = "BeamTree"
37
38 - def __init__(self, Root = None,
39 T = None, R = None):
40 '''BeamTree initializer.'''
41 self.Root = Root
42 self.T = T
43 self.R = R
44
46 '''String representation of a BeamTree, for print(tree).
47
48 '''
49 return formatter(self.lines())
50
52 '''Returns the list of lines necessary to print the object.
53
54 '''
55 ans = []
56 ans.append("Tree: {")
57 ans.append("Root beam: " + str(self.Root.Ref))
58 ans.append("Number of beams: " + str(self.numberOfBeams()))
59 ans.append("}")
60
61 return ans
62
64 '''Returns the string representation the tree of beams.
65
66 '''
67 before = ["Tree: Root beam = " + str(self.Root.Ref) + " {"]
68 after = ["}"]
69 return formatter(before + self.beamLines() + after)
70
72 '''Returns the list of lines necessary to print the list of beams.
73
74 '''
75 ans = []
76 if self.Root is not None:
77 ans = self.Root.lines()
78 if self.R is not None:
79 ans = ans + self.R.beamLines()
80 if self.T is not None:
81 ans = ans + self.T.beamLines()
82
83 return ans
84
86 '''Return the total number of beams.'''
87 if self.Root is None:
88 return 0
89
90 if self.T is None and self.R is None:
91 return 1
92 elif self.T is None and self.R is not None:
93 return 1 + self.R.numberOfBeams()
94 elif self.T is not None and self.R is None:
95 return 1 + self.T.numberOfBeams()
96 else:
97 return 1 + self.T.numberOfBeams() + self.R.numberOfBeams()
98
100 '''Return the list of lines necessary to write the output of simulation.
101
102 '''
103 sList = []
104 if self.Root is not None:
105 if self.Root.Optic is not None:
106 if self.Root.Length > 0.:
107 if self.R is not None and self.R.Root is not None:
108 sList = ['(' + self.Root.Optic + ', ' + self.Root.Face \
109 + ') ' + str(self.Root.Length)\
110 + 'm (' + self.R.Root.Optic + ', '\
111 + self.R.Root.Face + ') ' + self.Root.Ref + ' {']
112 elif self.T is not None and self.T.Root is not None:
113 sList = ['(' + self.Root.Optic + ', '+ self.Root.Face \
114 + ') ' + str(self.Root.Length) \
115 + 'm (' + self.T.Root.Optic + ', '+ self.T.Root.Face \
116 + ') ' + self.Root.Ref + ' {']
117 else:
118 sList = ['(' + self.Root.Optic \
119 + ', ' +self.Root.Face + ') ' + str(self.Root.Length) \
120 + 'm [End] ' + self.Root.Ref + ' {']
121 else:
122 sList = ['(' + self.Root.Optic + ', ' +self.Root.Face \
123 + ') [Open] ' + self.Root.Ref + ' {']
124 else:
125 if self.Root.Length > 0.:
126 if self.R is not None and self.R.Root is not None:
127 sList = ['[InBeam] ' + str(self.Root.Length) \
128 + 'm (' + self.R.Root.Optic + ', '+ self.R.Root.Face \
129 + ') ' + self.Root.Ref + ' {']
130 elif self.T is not None and self.T.Root is not None:
131 sList = ['[InBeam] ' + str(self.Root.Length) \
132 + 'm (' + self.T.Root.Optic + ', '+ self.T.Root.Face \
133 + ') ' + self.Root.Ref + ' {']
134 else:
135 sList = ['[InBeam] ' + str(self.Root.Length)\
136 + 'm [End] ' + self.Root.Ref + ' {']
137 else:
138 sList = ['[InBeam] [Open] ' + self.Root.Ref + ' {']
139 sList.append("Waist Pos: " + str(self.Root.waistPos()) + 'm')
140 sList.append("Waist Size: (" \
141 + str(self.Root.waistSize()[0]/mm) + ', ' \
142 + str(self.Root.waistSize()[1]/mm)\
143 + ')mm')
144 sph = rectToSph(self.Root.Dir)
145 sList.append("Direction: (" + str(sph[0]/deg) + ', ' \
146 + str(sph[1]/deg) + ')deg')
147 sList.append('}')
148 if self.R is not None:
149 sList = sList + self.R.outputLines()
150 if self.T is not None:
151 sList = sList + self.T.outputLines()
152
153 return sList
154
155
156 -def treeOfBeam(srcBeam, optList, order, threshold):
157 '''Function to calculate the tree of daughter beams of srcBeam.
158
159 srcBeam: Input beam. [GaussianBeam]
160 optList: List of optical components of the setup. [list of OpticalComponent]
161 order: order of simulation. [integer]
162 threshold: power threshold for daughter beams. [float]
163
164 Returns a BeamTree.
165
166 '''
167
168 if len(optList) == 0:
169
170 return BeamTree(Root = srcBeam)
171
172 if srcBeam is None:
173
174 return BeamTree()
175
176
177 dist = settings.inf
178 finalHit = {}
179 fianlisHit = {}
180 finalOpt = optList[0]
181 hitAtLeastOnce = False
182
183
184 for opt in optList:
185 dicoisHit = opt.isHit(srcBeam)
186 if dicoisHit['isHit'] and dicoisHit['distance'] < dist:
187 hitAtLeastOnce = True
188 dist = dicoisHit['distance']
189 finalOpt = opt
190
191 if not hitAtLeastOnce:
192
193 if settings.info:
194 print "theia: Info: Reached open beam "\
195 + srcBeam.Ref + "."
196 return BeamTree(Root = srcBeam)
197
198
199 finalisHit = finalOpt.isHit(srcBeam)
200 finalHit = finalOpt.hit(srcBeam, order = order, threshold = threshold)
201
202 return BeamTree(Root = srcBeam,
203 T = treeOfBeam(finalHit['t'], optList, order, threshold),
204 R = treeOfBeam(finalHit['r'], optList, order, threshold))
205