def energy_conservation_tester(): w0 = numpy.pi*2 sho_funcs = [ (lambda config: 1), (lambda config: config[2]), (lambda config: -config[1]*(w0)**2) ] x0 = 1.0 tfin = 50.0 start = time.time() path = rk_comp.rk45(sho_funcs, [0, float(x0), 0], tfin, precision=10**-10) end = time.time() print(len(path), tfin/len(path)) print('took %.3f seconds' % (end-start)) times = map(lambda point: point[0], path) potential = map(lambda point: (w0**2/2.)*point[1]**2, path) kinetic = map(lambda point: point[2]**2/2, path) total = numpy.array(potential)+numpy.array(kinetic) # pyplot.plot(times, potential) # pyplot.plot(times, kinetic) # pyplot.plot(times, total) error = abs(total-total[0])/total[0] pyplot.plot(times, numpy.log10(error)) #-5 got about 4.8 digits, -6 got 5.9, -7 got 6.5, -8 got 7.7 #-9 got 8.3 digits -10 get 9.6 digit after 6 minutes pyplot.show() return
def pather_drawing(): target = [(lambda state: 1), (lambda state: state[3]), (lambda state: state[4]), (lambda state: -2 * state[1] * state[2]**2 * (1 - state[1]**2) * numpy.exp(-(state[1]**2 + state[2]**2))), (lambda state: -2 * state[1]**2 * state[2] * (1 - state[2]**2) * numpy.exp(-(state[1]**2 + state[2]**2)))] runs = 4 # b_vals = numpy.linspace(.9, .25, runs) b_vals = numpy.linspace(.9, .25, runs - 1) b_vals = numpy.append(b_vals, 2.3) fig, axes = pyplot.subplots(runs, 3) axes[0][0].set_xlabel('x') axes[0][0].set_ylabel('y') axes[0][1].set_xlabel('x') axes[0][1].set_ylabel('vx') axes[0][2].set_xlabel('y') axes[0][2].set_ylabel('vy') for b_index in range(len(b_vals)): b = b_vals[b_index] path = rk_comp.rk45(target, [0, -5.5, b, .5, 0], 25.0) x_vals = column(path, 1) y_vals = column(path, 2) vx_vals = column(path, 3) vy_vals = column(path, 4) axes[b_index][0].scatter([1, 1, -1, -1], [1, -1, 1, -1], facecolor='r') axes[b_index][0].scatter(x_vals, y_vals) axes[b_index][1].scatter(x_vals, vx_vals) axes[b_index][2].scatter(y_vals, vy_vals) # axes.set_ylim([-2.5, 2.5]) # axes.set_xlim([-2.5, 2.5]) fig.tight_layout() pyplot.savefig("%d.png" % int(time.time()))
def backwards_test(): w0 = numpy.pi * 2 forward_funcs = [(lambda config: 1), (lambda config: config[2]), (lambda config: -config[1] * (w0)**2)] backward_funcs = [(lambda config: +1), (lambda config: -config[2]), (lambda config: +config[1] * (w0)**2)] path1 = rk_comp.rk45(forward_funcs, [0, 1., 0], 5.0) path2 = rk_comp.rk45(backward_funcs, [0, path1[-1][1], path1[-1][2]], path1[-1][0]) flipped_path2 = time_reverser(path1[-1][0], path2) ordered_path2 = sorted(flipped_path2, key=(lambda point: point[0])) print(len(path1), len(ordered_path2)) print(path1[0], ordered_path2[0:3]) #results: reversed path has 4 times as many points in it?! by nature of rk45, path2 does not actually include t=0, does get within 10**-13, though #decision: rk45 as implemented is messy for this purpose, numerov should be implemented # rk_comp.plot_paths([path1, flipped_path2], ['forward', 'back']) return
def beating(): w0 = numpy.pi*2 f0 = w0**2 wd = numpy.pi*2.5 sho_funcs = [ (lambda config: 1), (lambda config: config[2]), (lambda config: -config[1]*(w0)**2) ] drive_funcs = [ (lambda config: 1), (lambda config: config[2]), (lambda config: -config[1]*(w0)**2+f0*numpy.sin(wd*config[0])) ] x0 = 1.0 tfin = 50.0 start = time.time() path0 = rk_comp.rk45(sho_funcs, [0, float(x0), 0], tfin, precision=10**-6) path1 = rk_comp.rk45(drive_funcs, [0, float(x0), 0], tfin, precision=10**-6) end = time.time() print(end-start) rk_comp.plot_paths([path0, path1])
def sho_tester(): w0 = numpy.pi*2 sho_funcs = [ (lambda config: 1), (lambda config: config[2]), (lambda config: -config[1]*(w0)**2) ] calculated_periods = [] for x0 in range(1,3): path = rk_comp.rk45(sho_funcs, [0, float(x0), 0], 20.0, precision=10**-5) print(len(path)) calculated_periods.append(period_finder(path)) compare_extremas(max_displace(path, count=3), min_veloc(path, count=3)) print(calculated_periods) return
def non_harmonic_beating(): w0 = numpy.pi*2 f0 = w0**2 wd = numpy.pi*1.2 drive_funcs = [ (lambda config: 1), (lambda config: config[2]), (lambda config: -config[1]**5+3*numpy.sin(wd*config[0])-0.1*config[2]) ] x0 = 1.5 tfin = 150.0 start = time.time() path1 = rk_comp.rk45(drive_funcs, [0, float(x0), 0], tfin, precision=10**-6) end = time.time() print(end-start) rk_comp.plot_paths([ path1])
def metastable_seperatrix(): a, b = 4.0, 3.0 well_func = [ (lambda config: 1), (lambda config: config[2]), (lambda config: -a*config[1]-b*config[1]**2.0) ] print(-a/b) seperatrix = -a/b # seperatrix = -a/(2.*b) starts = numpy.linspace(seperatrix*.01, seperatrix, 20)-seperatrix*.000001 periods = [] for x0 in starts: print(x0) path = rk_comp.rk45(well_func, [0,x0, 0], 20.0, precision=10**-5) periods.append(period_finder(path)) pyplot.plot(starts, periods) pyplot.show() return
def period_vs_totalE(): ''' amplitudes selected to compare total energy to total energy ''' energies = 1.25**numpy.arange(-5, 7) sets_of_periods = [] for power in range(5): p = 2*power + 1 periods = [] well_funcs = [ (lambda config: 1), (lambda config: config[2]), (lambda config: -p*config[1]**p) ] for energy in energies: x0 = energy**(1.0/p) path = rk_comp.rk45(well_funcs, [0, x0, 0], 20.0, precision=10**-5) periods.append(period_finder(path)) pyplot.plot(energies, periods) pyplot.show() return
def deflection(scattering_func, y0=.5, v0=.5): path = rk_comp.rk45(scattering_func, [0, -5.5, y0, v0, 0], 15.0) theta = numpy.arctan2(path[-1][4], path[-1][3]) return theta