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