def time_run_to_stop(n): au = atomic_units() trun = [] # n = 10 W0s = (np.random.random(n) * 3 - 2) * 150 * au['GHz'] # -100 -> 50 GHz # Eps = np.random.random(n)*300*au['mVcm'] Eps = np.array([100 * au['mVcm']] * n) Emw = 4 * 1000 * au['mVcm'] fmw = 2 * np.pi * 15.932 / au['ns'] t0 = 0 * au['ns'] toff = 20 * au['ns'] tring = 10 * au['ns'] tstop = toff + 5 * tring Wlim = -600 * au['GHz'] global lut_up_f global lut_down_f lut_up_f, lut_down_f = tab.import_lookup_table_f(Eps[0]) for i in range(n): progress("time_run_to_stop()", i, n) clock_start = time.clock() run_to_stop(W0s[i], Eps[i], Emw, fmw, t0, toff, tstop, Wlim, tring) clock_end = time.clock() trun = trun + [clock_end - clock_start] bins = np.linspace(0, np.mean(trun) * 2, float(n) / 10) print('mean = {} s'.format(np.mean(trun))) print('median = {} s'.format(np.median(trun))) fig, ax = plt.subplots() ax.hist(trun, bins) ax.set_title("{0} runs of 'run_to_stop()'".format(n)) plt.show() return trun
def time_run_to_stop(n): """n executions run_to_stop() with random W0, Ep. Plot histogram of times. Build n-length random arrays of W0 from -100 to 50 GHz and Ep from 0 to 300 mV/cm. Build an array of run times for run_to_20ns(), and then plot a histogram of the time distributions. Return the array of times. returns trun -- list: run times for run_to_stop() """ au = atomic_units() trun = [] W0s = (np.random.random(n) - 2/3)*150*au['GHz'] # -100 -> 50 GHz # Eps = np.random.random(n)*300*au['mVcm'] # Eps = np.array([100*au['mVcm']]*n) Eps = np.random.choice(range(0, 301, 1), size=n)*1.94469e-13 Emw = 4*1000*au['mVcm'] fmw = 2*math.pi*15.932/au['ns'] t0 = 0*au['ns'] toffs = (20 + np.random.random(n)*20)*au['ns'] # 20 to 40 ns tring = 180*au['ns'] tstops = toffs + 5*tring Wlim = -600*au['GHz'] dups = np.random.choice([True, False], size=n) # np.bool_ !!!!! # field lookup tables lut_up, lut_down = tab.import_lookup_table() lut_up_f, lut_down_f = tab.import_lookup_table_f(Eps[0]) # execute for i in range(n): progress("time_run_to_stop()", i, n) clock_start = time.clock() run_to_stop( W0s[i], Eps[i], Emw, fmw, t0, toffs[i], tstops[i], Wlim, tring, bool(dups[i]), lut_up, lut_down, lut_up_f, lut_down_f, tr=False) # np.bool_ !!!!! clock_end = time.clock() trun = trun + [clock_end - clock_start] # plot bins = np.linspace(0, np.mean(trun)*2, int(n/10)) print('mean = {} s'.format(np.mean(trun))) print('median = {} s'.format(np.median(trun))) fig, ax = plt.subplots() ax.hist(trun, bins) ax.set(title="{0} runs of 'run_to_stop()'".format(n), xlabel="time (s)") plt.show() return trun
def timeit_run_to_20ns(): """timeit callable for run_to_20ns()""" W0 = (random.random()*3 - 2)*150*1.51983e-7 Ep = random.choice(range(0, 301, 1))*1.94469e-13 Emw = 4*1000*1.94469e-13 fmw = 2*math.pi*15.932/41341400.0 t0 = 0*41341400.0 toff = 20*41341400.0 # tring = 10*41341400.0 # tstop = toff + 5*tring Wlim = -600*1.51983e-7 dup = random.choice([True, False]) # field lookup tables global lut_up_f global lut_down_f lut_up_f, lut_down_f = tab.import_lookup_table_f(Ep) # execute run_to_20ns(W0, Ep, Emw, fmw, t0, toff, Wlim, dup) return
def bulk_f(W0s, Ep, Emw, w_mw, t0, toff, tring, tstop, Wlim, dup): au = tab.atomic_units() # lookup tables lut_up, lut_down = tab.import_lookup_table() lut_up_f, lut_down_f = tab.import_lookup_table_f(Ep) # final energies DataFrame n = 2000 # uncertainty of ~ 1% m = n * len(W0s) Wfs = np.ones(m) * np.NaN df = pd.DataFrame() df['Wf'] = Wfs df['W0'] = np.repeat(W0s, n) df['Ep'] = Ep df['Emw'] = Emw df['w_mw'] = w_mw df['t0'] = t0 df['toff'] = toff df['tring'] = tring df['tstop'] = tstop df['Wlim'] = Wlim df['dup'] = dup # build results for i in df.index: tab.progress("bulk_f(): ", i, m) W0 = df.loc[i, 'W0'] if np.isnan(df.loc[i, 'toff']): toff = (20 + 20 * random.random()) * au['ns'] df.loc[i, 'toff'] = toff args = (W0, Ep, Emw, w_mw, t0, toff, tstop, Wlim, tring, dup, lut_up, lut_down, lut_up_f, lut_down_f) df.loc[i, 'Wf'] = s1d.run_to_stop(*args, tr=False) Epstring = str(int(round(Ep / au['mVcm'], 1) * 10)) Epstring = Epstring.zfill(4) fname = "wfinals" + "_" + Epstring + "_" if dup is True: fname = fname + "u" else: fname = fname + "d" fname = fname + ".h5" fname = os.path.join("wfinals", fname) print(fname) df.to_hdf(fname, 'df') return df
def time_run_to_20ns(n): """n executions run_to_20ns with random W0, Ep. Plot histogram of times. Build n-length random arrays of W0 from -100 to 50 GHz and Ep from 0 to 300 mV/cm. Build an array of run times for run_to_20ns(), and then plot a histogram of the time distributions. Return the array of times. returns trun -- list: run times for run_to_20ns() """ au = atomic_units() trun = [] W0s = (np.random.random(n) - 2/3)*150*au['GHz'] # Eps = np.random.random(n)*300*au['mVcm'] Eps = np.random.choice(range(0, 300+1, 1), size=n)*1.94469e-13 Emw = 4*1000*au['mVcm'] fmw = 2*math.pi*15.932/au['ns'] t0 = 0*au['ns'] tstop = 20*au['ns'] Wlim = -600*au['GHz'] dups = np.random.choice([True, False], size=n) # np.bool_ !!!!! # field lookup tables global lut_up_f global lut_down_f lut_up_f, lut_down_f = tab.import_lookup_table_f(Eps[0]) for i in range(n): progress("time_run_to_20ns()", i, n) clock_start = time.clock() run_to_20ns(W0s[i], Eps[i], Emw, fmw, t0, tstop, Wlim, bool(dups[i]), tr=False) # np.bool_ !!!!! clock_end = time.clock() trun = trun + [clock_end - clock_start] bins = np.linspace(0, max(trun)*1.2, 1001) print('mean = {} s'.format(np.mean(trun))) print('median = {} s'.format(np.median(trun))) fig, ax = plt.subplots() ax.hist(trun, bins) ax.set_title("{0} runs of 'run_to_20ns()'".format(n)) plt.show() return trun
def plot_run_to_20ns_track(): """Plot the energy vs. time from each step in the model.""" au = atomic_units() # ========== # Model # ========== # run_to_20ns_track() arguments W0 = (np.random.random() * 3 - 2) * 150 * au['GHz'] Ep = 100 * au['mVcm'] Emw = 4 * 1000 * au['mVcm'] fmw = 2 * np.pi * 15.932 / au['ns'] t0 = 0 * au['ns'] tstop = 20 * au['ns'] Wlim = -600 * au['GHz'] # lut global lut_up_f global lut_down_f lut_up_f, lut_down_f = tab.import_lookup_table_f(Ep) # run to 20ns Wfinal, y, t, track = run_to_20ns_track(W0, Ep, Emw, fmw, t0, tstop, Wlim) # convert to plottable format # track -- Group 0 df = track[['ti', 'Wi']].copy() df.rename(index=int, columns={'ti': 't', 'Wi': 'W'}, inplace=True) shift = track[['ti', 'Wf']].copy() shift.rename(index=int, columns={'ti': 't', 'Wf': 'W'}, inplace=True) shift['t'] = shift['t'] + 1 # 10 as shfit df = df.append(shift, ignore_index=True) df['group'] = 0 df.sort_values(by='t', inplace=True) # integrated path -- Group 1 df = df.append(pd.DataFrame({ 't': t, 'W': Wfinal, 'group': 1 }), ignore_index=True) # catch -- Group 2 # tf = catch_at_20ns(Wfinal[-1], y[-1], t[-1]) # df = df.append(pd.DataFrame({'t': np.array([t[-1], tf]), 'W': Wfinal[-1], # 'group': 2}), ignore_index=True) # orbits after Ep turns off -- Group 3 W0 = Wfinal[-1] y0 = y[-1] t0 = t[-1] tring = 10 * au['ns'] # guess at ringdown tstop = t0 + 5 * tring # very little MW left Wfinal, tc, track = run_after_20ns_track(W0, y0, Emw, fmw, t0, tstop, Wlim, tring) df = df.append(pd.DataFrame({ 't': np.array([t0, tc]), 'W': W0, 'group': 2 }), ignore_index=True) tdf = track[['ti', 'Wi']].copy() tdf.rename(index=int, columns={'ti': 't', 'Wi': 'W'}, inplace=True) shift = track[['ti', 'Wf']].copy() shift.rename(index=int, columns={'ti': 't', 'Wf': 'W'}, inplace=True) shift['t'] = shift['t'] + 1 # 10 as shift tdf = tdf.append(shift, ignore_index=True) tdf['group'] = 3 tdf.sort_values(by='t', inplace=True) df = df.append(tdf, ignore_index=True) # lab units df['t'] = df['t'] / au['ns'] df['W'] = df['W'] / au['GHz'] # ========== # Plot # ========== fig, ax = plt.subplots() ax.axvline(0, c='k') ax.axhline(0, c='k') df.where(df['group'] == 0).plot(x='t', y='W', label='dW', ax=ax, c='C0', marker='o') df.where(df['group'] == 3).plot(x='t', y='W', label='dWo', ax=ax, c='C3', marker='o') df.where(df['group'] == 2).plot(x='t', y='W', label='catch', ax=ax, c='C2', marker='o') df.where(df['group'] == 1).plot(x='t', y='W', label='intg', ax=ax, c='C1', marker='.') return df
def plot_run_to_stop(): """Plot the energy vs. time from each step in the model.""" au = atomic_units() # ========== # Model # ========== # run_to_20ns_track() arguments W0 = (random.random() - 2/3)*150*au['GHz'] Ep = random.choice(range(0, 301, 1))*au['mVcm'] Emw = 4*1000*au['mVcm'] fmw = 2*math.pi*15.932/au['ns'] t0 = 0*au['ns'] toff = 20*au['ns'] Wlim = -600*au['GHz'] tring = 10*au['ns'] # guess at ringdown tstop = toff + 5*tring # very little MW left dup = random.choice([True, False]) # otherwise, numpy.bool_ # dup = True # field lookup tables global lut_up_f global lut_down_f lut_up_f, lut_down_f = tab.import_lookup_table_f(Ep) # run to stop Wfinal, t, y, track1, tc, track2 = run_to_stop( W0, Ep, Emw, fmw, t0, toff, tstop, Wlim, tring, dup, tr=True) intg_w = y_to_w_vec(np.array(y)) # track1 -- Group 0 df = track1[['ti', 'Wi']].copy() df.rename(index=int, columns={'ti': 't', 'Wi': 'W'}, inplace=True) shift = track1[['ti', 'Wf']].copy() shift.rename(index=int, columns={'ti': 't', 'Wf': 'W'}, inplace=True) shift['t'] = shift['t'] + 1 # 10 as shfit df = df.append(shift, ignore_index=True) df['group'] = 0 df.sort_values(by='t', inplace=True) # integrated path -- Group 1 df = df.append(pd.DataFrame({'t': t, 'W': intg_w, 'group': 1}), ignore_index=True) # catch -- Group 2 df = df.append(pd.DataFrame( {'t': np.array([t[-1], tc]), 'W': intg_w[-1], 'group': 2}), ignore_index=True) # track2 -- Group 3 tdf = track2[['ti', 'Wi']].copy() tdf.rename(index=int, columns={'ti': 't', 'Wi': 'W'}, inplace=True) shift = track2[['ti', 'Wf']].copy() shift.rename(index=int, columns={'ti': 't', 'Wf': 'W'}, inplace=True) shift['t'] = shift['t'] + 1 # 10 as shift # stopping point ostop = track2.iloc[-1][['tf', 'Wf']] ostop.rename({'tf': 't', 'Wf': 'W'}, inplace=True) shift = shift.append(ostop) tdf = tdf.append(shift, ignore_index=True) tdf['group'] = 3 tdf.sort_values(by='t', inplace=True) df = df.append(tdf, ignore_index=True) # lab units df['t'] = df['t']/au['ns'] df['W'] = df['W']/au['GHz'] # ========== # Plot # ========== fig, ax = plt.subplots() ax.axvline(0, c='k') ax.axhline(0, c='k') df.where(df['group'] == 0).plot( x='t', y='W', label='dW', ax=ax, c='C0', marker='o') df.where(df['group'] == 3).plot( x='t', y='W', label='dWo', ax=ax, c='C3', marker='o') df.where(df['group'] == 2).plot( x='t', y='W', label='catch', ax=ax, c='C2', marker='o') df.where(df['group'] == 1).plot( x='t', y='W', label='intg', ax=ax, c='C1', marker='.') ax.set(title="dup = {}".format(dup)) return df