Package empro :: Package toolkit :: Module simulation
[frames] | no frames]

Source Code for Module empro.toolkit.simulation

  1  # Copyright 1983-2019 Keysight Technologies, Inc , Keysight Confidential 
  2   
  3  from empro import _deprecation 
  4   
  5  ENGINES = FDTD, FEM = 'FdtdEngine', 'FemEngine' 
  6   
  7  _DEFAULT_CONVERGENCE = -30 
  8  _DEFAULT_MAX_TIME = "20000 * timestep" 
  9  _DEFAULT_ENGINE = FDTD 
10 11 -def setupTerminationCriteria(convergence=_DEFAULT_CONVERGENCE, maxTime=_DEFAULT_MAX_TIME):
12 ''' 13 setupTerminationCriteria([convergence [, maxTime [, engine]]]) 14 sets termination criterira on simulation on active project 15 @rtype: L{NewSimulationData} 16 @return: returns the simulation setup of the active project 17 ''' 18 import empro 19 sim = empro.activeProject.createSimulationData() 20 sim.terminationCriteria.enableConvergence = True 21 sim.terminationCriteria.convergenceThreshold = convergence 22 sim.terminationCriteria.maximumSimulationTime = empro.core.Expression(maxTime) 23 return sim
24
25 -def setupSParameterSimulation(convergence=_DEFAULT_CONVERGENCE, maxTime=_DEFAULT_MAX_TIME, engine=_DEFAULT_ENGINE, name=None):
26 ''' 27 setupSParameterSimulation([convergence [, maxTime [, engine]]]) 28 Setups simulation on active project for S parameter simulation 29 @rtype: L{NewSimulationData} 30 @return: returns the simulation setup of the active project 31 ''' 32 sim = setupTerminationCriteria(convergence, maxTime) 33 sim.engine = engine 34 sim.name = name or 'S-parameter simulation' 35 sim.parameterSweepEnabled = False 36 sim.sParametersEnabled = True 37 sim.foiParameters.collectSteadyStateData = False 38 return sim
39
40 -def setupSteadyStateSimulation(frequencies, convergence=_DEFAULT_CONVERGENCE, maxTime=_DEFAULT_MAX_TIME, engine=_DEFAULT_ENGINE, name=None):
41 ''' 42 setupSteadyStateSimulation(frequencies [, convergence [, maxTime [, engine]]]) 43 Setups simulation on active project for S parameter simulation 44 @type frequencies: list<float> 45 @param frequencies: list of frequencies of interest 46 @rtype: L{NewSimulationData} 47 @return: returns the simulation setup of the active project 48 ''' 49 sim = setupTerminationCriteria(convergence, maxTime) 50 sim.engine = engine 51 sim.name = name or 'Steady State simulation' 52 sim.parameterSweepEnabled = False 53 sim.sParametersEnabled = False 54 sim.foiParameters.collectSteadyStateData = True 55 sim.foiParameters.foiSource = "Specified" 56 for f in frequencies: 57 sim.foiParameters.addSpecifiedFrequency(f) 58 return sim
59
60 -class TimeOutError(RuntimeError):
61 - def __init__(self, given_time):
62 self.given_time = given_time
63 - def __str__(self):
64 return "Executing simulation(s) failed to complete within specified time '%s s'." % (self.given_time)
65
66 -def wait(simulations=None, completionTimeSeconds=0):
67 ''' 68 wait() -> None 69 wait(L{Simulation}) -> None 70 wait([L{Simulation}, ...]) -> None 71 72 This function blocks execution as long as there are simulations that are 73 Running or Queued. It will not wait for simulations in any other state like 74 Created, Ready, Completed, killed or Error. When called without arguments, 75 it will check all simulations in the active project. When called with a 76 single simulation or a list, it will only wait for those specific 77 simulations. 78 ''' 79 import empro 80 import time 81 startTime = time.time() 82 _enforceBaseApp() 83 if simulations is None: 84 simulations = empro.activeProject.simulations() 85 if isinstance(simulations, empro.simulation.Simulation): 86 simulations = [simulations] 87 while any(sim.status in ('Queued', 'Running', 'PostProcessing', 'Interrupting', 'Killing') for sim in simulations): 88 if completionTimeSeconds and time.time()-startTime>completionTimeSeconds: 89 raise TimeOutError(completionTimeSeconds) 90 time.sleep(.1) 91 empro.gui.processEvents()
92
93 94 @_deprecation.deprecated(390, "Use wait(simulations, completionTimeSeconds) instead.") 95 -def timedWait(simulations=None, completionTimeSeconds=0):
96 ''' 97 wait() -> None 98 wait(L{Simulation}) -> None 99 wait([L{Simulation}, ...]) -> None 100 101 This function blocks execution as long as there are simulations that are 102 Running or Queued. It will not wait for simulations in any other state like 103 Created, Ready, Completed, killed or Error. When called without arguments, 104 it will check all simulations in the active project. When called with a 105 single simulation or a list, it will only wait for those specific 106 simulations. 107 108 When the simulation time exceeds the completion time then an error is raised. 109 ''' 110 if completionTimeSeconds==0: 111 empro.toolkit.simulation.wait(simulations) 112 else: 113 import empro 114 import time 115 startTime = time.time() 116 _enforceBaseApp() 117 if simulations is None: 118 simulations = empro.activeProject.simulations() 119 if isinstance(simulations, empro.simulation.Simulation): 120 simulations = [simulations] 121 while any(sim.status in ('Queued', 'Running', 'PostProcessing', 'Interrupting', 'Killing') for sim in simulations): 122 if time.time()-startTime>completionTimeSeconds: 123 raise TimeOutError, (completionTimeSeconds) 124 else: 125 time.sleep(.1) 126 empro.gui.processEvents()
127
128 -def simulateParameterSweep(addToQueue=True, simName=None, simEngine=None, **paramSweeps):
129 ''' 130 create multiple simulations to perform a parameter sweep, even in FEM mode. 131 paramSweeps is a dictionary containing the parameters to be swept 132 The keys are the parameter sweeps, the values are either single values (numbers 133 or expressions), or a sequence of values. In the latter case, one simulation 134 per item in the sequence will be made. If multiple parameters must be swept, 135 the product space will be used. For example: 136 137 simulateParameterSweep(A=[1, 2], B=3, C=["4 mm", "5 mm"]) 138 139 The following simulations will be created: 140 A = 1, B = 3, C = "4 mm" 141 A = 1, B = 3, C = "5 mm" 142 A = 2, B = 3, C = "4 mm" 143 A = 2, B = 3, C = "5 mm" 144 145 Optional arguments: 146 - addToQueue: if True, the created simulations are added to the run queue 147 - simName: the simulation name to be used. 148 ''' 149 import empro 150 oldSimName = empro.activeProject.createSimulationData().name 151 try: 152 simName = simName or oldSimName or "parameter sweep" 153 if not paramSweeps: 154 return [_createSimulation(addToQueue, simName, simEngine)] 155 keys = sorted(paramSweeps) 156 key = keys[0] 157 values = _asSequence(paramSweeps[key]) 158 others = dict((k, paramSweeps[k]) for k in keys[1:]) 159 sims = [] 160 for x in values: 161 _setParameter(key, x) 162 newSimName = "%(simName)s, %(key)s=%(x)s" % vars() 163 sims += simulateParameterSweep(addToQueue, newSimName, simEngine, **others) 164 return sims 165 finally: 166 empro.activeProject.createSimulationData().name = oldSimName
167
168 169 # --- implementation details --- 170 171 -def _setParameter(name, formula):
172 import empro 173 params = empro.activeProject.parameters() 174 assert params.contains(name), "Project does not define parameter %r" % name 175 params.setFormula(name, formula)
176
177 178 -def _createSimulation(addToQueue=True, simName=None, simEngine=None):
179 ''' 180 create a simulation, and runs it if addToQueue is True 181 ''' 182 import empro 183 sim = empro.activeProject.createSimulationData() 184 oldSweep = sim.parameterSweepEnabled 185 oldName = sim.name 186 oldEngine = sim.engine 187 try: 188 sim.parameterSweepEnabled = False 189 if simName: 190 sim.name = simName 191 if simEngine: 192 sim.engine = simEngine 193 return empro.activeProject.createSimulation(addToQueue) 194 finally: 195 sim.parameterSweepEnabled = oldSweep 196 sim.name = oldName 197 sim.engine = oldEngine
198
199 200 -def _asSequence(x):
201 ''' 202 If x is not a sequence (excluding strings), 203 encapsulate it in a list so we can iterate over it 204 ''' 205 import operator 206 if not operator.isSequenceType(x): 207 return [x] 208 if isinstance(x, basestring): 209 return [x] 210 return x
211
212 213 -def _enforceBaseApp():
214 ''' 215 make sure there's a base app when running from commandline. 216 has no effect within GUI. 217 ''' 218 import sys 219 import empro 220 try: 221 app = empro.gui.BaseApp() 222 except: 223 return 224 app.init(sys.argv)
225