Skip to content

Commit

Permalink
Add progress bar for notebooks (NanoComp#1078)
Browse files Browse the repository at this point in the history
* Add progress bar for notebooks

* Update progress bar in quiet mode
  • Loading branch information
ChristopherHogan authored and stevengj committed Dec 20, 2019
1 parent 4ad47c3 commit 1dd7e61
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions python/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
except NameError:
basestring = str

try:
from ipywidgets import FloatProgress
from IPython.display import display
do_progress = True
except ImportError:
do_progress = False


# Send output from Meep, ctlgeom, and MPB to Python's stdout
mp.cvar.master_printf_callback = mp.py_master_printf_wrap
mp.set_ctl_printf_callback(mp.py_master_printf_wrap)
Expand Down Expand Up @@ -1371,6 +1379,10 @@ def stop_cond(sim):

step_funcs = list(step_funcs)
step_funcs.append(display_progress(t0, t0 + stop_time, self.progress_interval))

if do_progress:
self.progress = FloatProgress(value=t0, min=t0, max=t0 + stop_time, description="0% done ")
display(self.progress)
else:
assert callable(cond[i]), "Stopping condition {} is not an integer or a function".format(cond[i])

Expand All @@ -1388,6 +1400,10 @@ def stop_cond(sim):
for func in step_funcs:
_eval_step_func(self, func, 'finish')

if do_progress:
self.progress.value = t0 + stop_time
self.progress.description = "100% done "

if mp.cvar.verbosity > 0:
print("run {} finished at t = {} ({} timesteps)".format(self.run_index, self.meep_time(), self.fields.t))
self.run_index += 1
Expand Down Expand Up @@ -2591,14 +2607,21 @@ def display_progress(t0, t, dt):

def _disp(sim):
t1 = mp.wall_time()
if t1 - closure['tlast'] >= dt and mp.cvar.verbosity > 0:
if t1 - closure['tlast'] >= dt:
msg_fmt = "Meep progress: {}/{} = {:.1f}% done in {:.1f}s, {:.1f}s to go"
val1 = sim.meep_time() - t0
val2 = val1 / (0.01 * t)
val3 = t1 - t_0
val4 = (val3 * (t / val1) - val3) if val1 != 0 else 0
print(msg_fmt.format(val1, t, val2, val3, val4))

if do_progress:
sim.progress.value = val1
sim.progress.description = "{}% done ".format(int(val2))

if mp.cvar.verbosity > 0:
print(msg_fmt.format(val1, t, val2, val3, val4))
closure['tlast'] = t1

return _disp


Expand Down

0 comments on commit 1dd7e61

Please sign in to comment.