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 ..optics.ghost import Ghost
25 from ..tree import beamtree
26 from . import parser
27
28
30 '''
31
32 Simulation class.
33
34 This class is a wrapper for all the metadata (names of setup and of files,
35 etc.) as well as for the high level functions of a simulation.
36
37 *=== Attributes ===*
38 LName: name of the simulation [string]
39 FName: name of the file for outputs (without extension) [string]
40 OptList: list of optical components of the setup [list of optics]
41 InBeams: list of input beams [list of beams]
42 BeamTreeList: list of binary trees of beams [list of BeamTree]
43 Order: order of the simulation, beams transmitted by HRs or reflected by ARs
44 have their orders augmented by 1, and simulation calculates only until
45 this Order attribute. [int]
46 Threshold: Power under which beams are no longer traced. [float]
47 Date: string of the date-time when the simulation was created (not run).
48 [string]
49
50 '''
51
52 - def __init__(self, FName = 'simulationinput'):
53 '''Simulation initializer.
54
55 FName: output files name without extension. [string]
56
57 '''
58
59 self.LName = 'Simulation'
60 self.FName = FName
61 self.OptList = []
62 self.InBeams = []
63 self.BeamTreeList = []
64 self.Order = np.inf
65 self.Threshold = -1.*mW
66 self.Date = strftime("%c")
67
68
70 '''String representation of the simulation, for print(simulation).
71
72 '''
73 sList = ["Simulation: " + str(self.LName) + " (" + str(self.FName)\
74 + ".*) {"]
75 sList.append("OptList: {")
76 for opt in self.OptList:
77 sList = sList + opt.lines()
78 sList.append("}")
79 sList.append("InBeams: {")
80 for beam in self.InBeams:
81 sList = sList + beam.lines()
82 sList.append("}")
83 sList.append("BeamTrees: {")
84 for tree in self.BeamTreeList:
85 sList = sList + tree.lines()
86 sList.append("}")
87 sList.append("}")
88
89 return formatter(sList)
90
92 '''Calculate the number of optics of OptList.
93
94 Returns the number of optics (not components, optics).
95
96 '''
97 n = 0
98 for obj in self.OptList:
99 if isinstance(obj, Optic):
100 n = obj.OptCount
101 break
102 return n
103
105 '''Initialize simulation attributes by input from .tia file.
106
107 See documantation for the format of the input file.
108
109 No return value.
110
111 '''
112 finalList = parser.readIn(settings.fname + '.tia')
113
114
115 translateDic = {'X': 0., 'Y': 0., 'Z': 0.}
116
117
118 for uple in finalList:
119 if uple[0] == 'bo':
120
121 translateDic = uple[1]
122 elif uple[0] == 'LName':
123 self.LName = uple[1]
124 elif uple[0] == 'order':
125 self.Order = uple[1]
126 elif uple[0] == 'threshold':
127 self.Threshold = uple[1]
128 elif uple[0] == 'bm':
129 self.InBeams.append(userGaussianBeam(**uple[1]))
130
131 self.InBeams[len(self.InBeams)-1].translate(**translateDic)
132 elif uple[0] == 'mr':
133 self.OptList.append(Mirror(**uple[1]))
134 self.OptList[len(self.OptList)-1].translate(**translateDic)
135 elif uple[0] == 'th':
136 self.OptList.append(ThinLens(**uple[1]))
137 self.OptList[len(self.OptList)-1].translate(**translateDic)
138 elif uple[0] == 'tk':
139 self.OptList.append(ThickLens(**uple[1]))
140 self.OptList[len(self.OptList)-1].translate(**translateDic)
141 elif uple[0] == 'bd':
142 self.OptList.append(BeamDump(**uple[1]))
143 self.OptList[len(self.OptList)-1].translate(**translateDic)
144 elif uple[0] == 'gh':
145 self.OptList.append(Ghost(**uple[1]))
146 self.OptList[len(self.OptList)-1].translate(**translateDic)
147
149 '''Run simulation with input as read by load.
150
151 threshold: power of beam below which the simulation stops tracing child
152 beams. [float]
153 order: maximum order to keep daughter beams. [integer]
154
155 No return value.
156 '''
157
158 if settings.warning and self.Threshold < 0.:
159 print "theia: Warning: Running simulation with negative threshold,"\
160 + " termination not guaranteed."
161
162 if settings.warning and self.Order is np.inf:
163 print "theia: Warning: Running simulation with infinite order,"\
164 + " termination not guaranteed."
165
166
167 self.BeamTreeList = []
168
169 for k in range(len(self.InBeams)):
170 self.BeamTreeList.append(beamtree.treeOfBeam(self.InBeams[k],
171 self.OptList, self.Order, self.Threshold ))
172
174 '''Write the results from the simulation in the .out file.
175 '''
176 outList = []
177 outList.append("########theia output file for simulation:########")
178 outList.append("\t\t\t" + self.LName + "\n")
179 outList.append('#'*10 + "META DATA" + '#'*10)
180
181 outList.append("Generated at: " + strftime("%c"))
182 outList.append("Input file: "+ self.FName + ".tia")
183 outList.append("Simulation Order: " + str(self.Order) )
184 outList.append("Simulation Threshold: "+ str(self.Threshold/mW) +'mW')
185 outList.append("Number of Components: " + str(len(self.OptList)))
186 outList.append("Number of Optics: " + str(self.numberOfOptics()) + '\n')
187 outList.append('#' *10 + 'SIMULATION DATA' + '#' * 10)
188 outList.append("Simulation: " + self.LName + " (" + self.FName\
189 + ".*) {")
190 outList.append("Components: {")
191
192 for opt in self.OptList:
193 outList.append(opt.Name + ' (' + opt.Ref + ') ' + str(opt.HRCenter))
194 outList.append("}")
195 outList.append("BeamTrees: {")
196
197 for tree in self.BeamTreeList:
198 outList = outList + tree.lines()
199
200 outList.append("}")
201 outList.append("}\n")
202 outList.append('#' * 10 + "BEAM LISTING" + '#' * 10)
203
204 for tree in self.BeamTreeList:
205 outList.append("Tree: Root beam = " + str(tree.Root.Ref) + " {")
206 outList = outList + tree.outputLines()
207 outList.append("}")
208
209 with open(settings.fname + '.out', 'w') as outF:
210 outF.write(formatter(outList))
211
213 '''Write the CAD .fcstd file by calling rendering functions.
214
215 '''
216
217
218
219
220
221 import FreeCAD as App
222 from ..rendering.writer import writeToCAD
223
224
225 App.newDocument(self.FName)
226 App.setActiveDocument(self.FName)
227 doc = App.ActiveDocument
228
229
230 for opt in self.OptList:
231 writeToCAD(opt, doc)
232
233
234 for tree in self.BeamTreeList:
235 writeToCAD(tree, doc)
236
237
238 doc.recompute()
239 doc.saveAs(settings.fname + '.fcstd')
240