1
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
24
39
59
62 self.given_time = given_time
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
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
176
198
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
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