1 '''Defines the Simulation class for theia.'''
2
3
4
5
6
7
8
9
10
11
12
13 import numpy as np
14 from time import strftime
15 from ..helpers import settings
16 from ..helpers.units import mW
17 from ..helpers.tools import formatter
18 from ..optics.optic import Optic
19 from ..optics.beam import userGaussianBeam
20 from ..optics.beamdump import BeamDump
21 from ..optics.thinlens import ThinLens
22 from ..optics.thicklens import ThickLens
23 from ..optics.mirror import Mirror
24 from ..tree import beamtree
25 from . import parser
26
27
29 '''
30
31 Simulation class.
32
33 This class is a wrapper for all the metadata (names of setup and of files,
34 etc.) as well as for the high level functions of a simulation.
35
36 *=== Attributes ===*
37 LName: name of the simulation [string]
38 FName: name of the file for outputs (without extension) [string]
39 OptList: list of optical components of the setup [list of optics]
40 InBeams: list of input beams [list of beams]
41 BeamTreeList: list of binary trees of beams [list of BeamTree]
42 Order: order of the simulation, beams transmitted by HRs or reflected by ARs
43 have their orders augmented by 1, and simulation calculates only until
44 this Order attribute. [int]
45 Threshold: Power under which beams are no longer traced. [float]
46 Date: string of the date-time when the simulation was created (not run).
47 [string]
48
49 '''
50
51 - def __init__(self, FName = 'simulationinput'):
52 '''Simulation initializer.
53
54 FName: output files name without extension. [string]
55
56 '''
57
58 self.LName = 'Simulation'
59 self.FName = FName
60 self.OptList = []
61 self.InBeams = []
62 self.BeamTreeList = []
63 self.Order = np.inf
64 self.Threshold = -1.*mW
65 self.Date = strftime("%c")
66
67
69 '''String representation of the simulation, for print(simulation).
70
71 '''
72 sList = ["Simulation: " + str(self.LName) + " (" + str(self.FName)\
73 + ".*) {"]
74 sList.append("OptList: {")
75 for opt in self.OptList:
76 sList = sList + opt.lines()
77 sList.append("}")
78 sList.append("InBeams: {")
79 for beam in self.InBeams:
80 sList = sList + beam.lines()
81 sList.append("}")
82 sList.append("BeamTrees: {")
83 for tree in self.BeamTreeList:
84 sList = sList + tree.lines()
85 sList.append("}")
86 sList.append("}")
87
88 return formatter(sList)
89
91 '''Calculate the number of optics of OptList.
92
93 Returns the number of optics (not components, optics).
94
95 '''
96 n = 0
97 for obj in self.OptList:
98 if isinstance(obj, Optic):
99 n = obj.OptCount
100 break
101 return n
102
104 '''Initialize simulation attributes by input from .tia file.
105
106 See documantation for the format of the input file.
107
108 No return value.
109
110 '''
111 finalList = parser.readIn(self.FName + '.tia')
112
113 for uple in finalList:
114 if uple[0] == 'LName':
115 self.LName = uple[1]
116 elif uple[0] == 'order':
117 self.Order = uple[1]
118 elif uple[0] == 'threshold':
119 self.Threshold = uple[1]
120 elif uple[0] == 'bm':
121 self.InBeams.append(userGaussianBeam(**uple[1]))
122 elif uple[0] == 'mr':
123 self.OptList.append(Mirror(**uple[1]))
124 elif uple[0] == 'th':
125 self.OptList.append(ThinLens(**uple[1]))
126 elif uple[0] == 'tk':
127 self.OptList.append(ThickLens(**uple[1]))
128 elif uple[0] == 'bd':
129 self.OptList.append(BeamDump(**uple[1]))
130
132 '''Run simulation with input as read by load.
133
134 threshold: power of beam below which the simulation stops tracing child
135 beams. [float]
136 order: maximum order to keep daughter beams. [integer]
137
138 No return value.
139 '''
140
141 if settings.warning and self.Threshold < 0.:
142 print "theia: Warning: Running simulation with negative threshold,"\
143 + " termination not guaranteed."
144
145 if settings.warning and self.Order is np.inf:
146 print "theia: Warning: Running simulation with infinite order,"\
147 + " termination not guaranteed."
148
149
150 self.BeamTreeList = []
151
152 for k in range(len(self.InBeams)):
153 self.BeamTreeList.append(beamtree.treeOfBeam(self.InBeams[k],
154 self.OptList, self.Order, self.Threshold ))
155
157 '''Write the results from the simulation in the .out file.
158 '''
159 outList = []
160 outList.append("########theia output file for simulation:########")
161 outList.append("\t\t\t" + self.LName + "\n")
162 outList.append('#'*10 + "META DATA" + '#'*10)
163
164 outList.append("Generated at: " + strftime("%c"))
165 outList.append("Input file: "+ self.FName + ".tia")
166 outList.append("Simulation Order: " + str(self.Order) )
167 outList.append("Simulation Threshold: "+ str(self.Threshold/mW) +'mW')
168 outList.append("Number of Components: " + str(len(self.OptList)))
169 outList.append("Number of Optics: " + str(self.numberOfOptics()) + '\n')
170 outList.append('#' *10 + 'SIMULATION DATA' + '#' * 10)
171 outList.append("Simulation: " + self.LName + " (" + self.FName\
172 + ".*) {")
173 outList.append("Components: {")
174
175 for opt in self.OptList:
176 outList.append(opt.Name + ' (' + opt.Ref + ') ' + str(opt.HRCenter))
177 outList.append("}")
178 outList.append("BeamTrees: {")
179
180 for tree in self.BeamTreeList:
181 outList = outList + tree.lines()
182
183 outList.append("}")
184 outList.append("}\n")
185 outList.append('#' * 10 + "BEAM LISTING" + '#' * 10)
186
187 for tree in self.BeamTreeList:
188 outList.append("Tree: Root beam = " + str(tree.Root.Ref) + " {")
189 outList = outList + tree.outputLines()
190 outList.append("}")
191
192 with open(self.FName + '.out', 'w') as outF:
193 outF.write(formatter(outList))
194
196 print "theia: Warning: CAD file writing not implemented yet. Passing."
197