Package theia :: Package running :: Module simulation
[hide private]
[frames] | no frames]

Source Code for Module theia.running.simulation

  1  '''Defines the Simulation class for theia.''' 
  2   
  3  # Provides: 
  4  #   class Simulation 
  5  #       __init__ 
  6  #       __str__ 
  7  #       numberOfOptics 
  8  #       load 
  9  #       run 
 10  #       writeOut 
 11  #       writeCAD 
 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   
29 -class Simulation(object):
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
69 - def __str__(self):
70 '''String representation of the simulation, for print(simulation).''' 71 sList = ["Simulation: %s (%s.*) {" %(str(self.LName), str(self.FName))] 72 sList.append("OptList: {") 73 for opt in self.OptList: 74 sList = sList + opt.lines() 75 sList.append("}") 76 sList.append("InBeams: {") 77 for beam in self.InBeams: 78 sList = sList + beam.lines() 79 sList.append("}") 80 sList.append("BeamTrees: {") 81 for tree in self.BeamTreeList: 82 sList = sList + tree.lines() 83 sList.append("}") 84 sList.append("}") 85 86 return formatter(sList)
87
88 - def numberOfOptics(self):
89 '''Calculate the number of optics of OptList. 90 91 Returns the number of optics (not components, optics). 92 93 ''' 94 n = 0 95 for obj in self.OptList: 96 if isinstance(obj, Optic): 97 n = obj.OptCount 98 break 99 return n
100
101 - def load(self):
102 '''Initialize simulation attributes by input from .tia file. 103 104 See documantation for the format of the input file. 105 106 No return value. 107 108 ''' 109 finalList = parser.readIn(settings.fname + '.tia') 110 111 # default dictionary for translation 112 translateDic = {'X': 0., 'Y': 0., 'Z': 0.} 113 114 # populate simulation attributes with objects from input 115 for uple in finalList: 116 if uple[0] == 'bo': 117 #update translation dic 118 translateDic = uple[1] 119 elif uple[0] == 'LName': 120 self.LName = uple[1] 121 elif uple[0] == 'order': 122 self.Order = uple[1] 123 elif uple[0] == 'threshold': 124 self.Threshold = uple[1] 125 elif uple[0] == 'bm': 126 self.InBeams.append(userGaussianBeam(**uple[1])) 127 #translate last read object 128 self.InBeams[len(self.InBeams)-1].translate(**translateDic) 129 elif uple[0] == 'mr': 130 self.OptList.append(Mirror(**uple[1])) 131 self.OptList[len(self.OptList)-1].translate(**translateDic) 132 elif uple[0] == 'th': 133 self.OptList.append(ThinLens(**uple[1])) 134 self.OptList[len(self.OptList)-1].translate(**translateDic) 135 elif uple[0] == 'tk': 136 self.OptList.append(ThickLens(**uple[1])) 137 self.OptList[len(self.OptList)-1].translate(**translateDic) 138 elif uple[0] == 'bd': 139 self.OptList.append(BeamDump(**uple[1])) 140 self.OptList[len(self.OptList)-1].translate(**translateDic) 141 elif uple[0] == 'gh': 142 self.OptList.append(Ghost(**uple[1])) 143 self.OptList[len(self.OptList)-1].translate(**translateDic)
144
145 - def run(self):
146 '''Run simulation with input as read by load. 147 148 threshold: power of beam below which the simulation stops tracing child 149 beams. [float] 150 order: maximum order to keep daughter beams. [integer] 151 152 No return value. 153 ''' 154 #warn if threshold is negative or order is inf 155 if settings.warning and self.Threshold < 0.: 156 print "theia: Warning: Running simulation with negative threshold,"\ 157 + " termination not guaranteed." 158 159 if settings.warning and self.Order is np.inf: 160 print "theia: Warning: Running simulation with infinite order,"\ 161 + " termination not guaranteed." 162 163 # reinitialize treeList 164 self.BeamTreeList = [] 165 166 for k in range(len(self.InBeams)): 167 self.BeamTreeList.append(beamtree.treeOfBeam(self.InBeams[k], 168 self.OptList, self.Order, self.Threshold ))
169
170 - def writeOut(self):
171 '''Write the results from the simulation in the .out file.''' 172 outList = [] 173 outList.append("########theia output file for simulation:########") 174 outList.append("\t\t\t %s\n" %self.LName) 175 outList.append('#'*10 + "META DATA" + '#'*10) 176 177 outList.append("Generated at: %s" %strftime("%c")) 178 outList.append("Input file: %s.tia" %self.FName) 179 outList.append("Simulation Order: %s" %str(self.Order) ) 180 outList.append("Simulation Threshold: %smW" %str(self.Threshold/mW)) 181 outList.append("Number of Components: %s" %str(len(self.OptList))) 182 outList.append("Number of Optics: %s\n" %str(self.numberOfOptics())) 183 outList.append('#' *10 + 'SIMULATION DATA' + '#' * 10) 184 outList.append("Simulation: %s (%s.*) {" %(self.LName, self.FName)) 185 outList.append("Components: {") 186 187 for opt in self.OptList: 188 outList.append("%s (%s) %s" %(opt.Name, opt.Ref, str(opt.HRCenter))) 189 outList.append("}") 190 outList.append("BeamTrees: {") 191 192 for tree in self.BeamTreeList: 193 outList = outList + tree.lines() 194 195 outList.append("}") 196 outList.append("}\n") 197 outList.append('#' * 10 + "BEAM LISTING" + '#' * 10) 198 199 for tree in self.BeamTreeList: 200 outList.append("Tree: Root beam = %s {" %str(tree.Root.Ref)) 201 outList = outList + tree.outputLines() 202 outList.append("}") 203 204 with open(settings.fname + '.out', 'w') as outF: 205 outF.write(formatter(outList))
206
207 - def writeCAD(self):
208 '''Write the CAD .fcstd file by calling rendering functions.''' 209 210 # these two following import statements are here because they require 211 # that the PYTHONPATH be updated, and thus they are not 212 # at the beginning of the module, which is read at the very beginning 213 # of main. 214 import FreeCAD as App 215 from ..rendering.writer import writeToCAD 216 217 #New fcstd document 218 App.newDocument(self.FName) 219 App.setActiveDocument(self.FName) 220 doc = App.ActiveDocument 221 222 #write all optics 223 for opt in self.OptList: 224 writeToCAD(opt, doc) 225 226 # write beams recursively 227 for tree in self.BeamTreeList: 228 writeToCAD(tree, doc) 229 230 #Wrap up 231 doc.recompute() 232 doc.saveAs(settings.fname + '.fcstd')
233