Exemple #1
0
for jac_grid in jac_grids:
    jac_grid.solve_jacobi( epsilon, N_iter_max )
#    print 'hi'
    jac_N_iters_list.append( jac_grid.N_iter )

    
for sor_grid in sor_grids:
    sor_grid.solve_SOR( epsilon, N_iter_max, accuracy )
#    print 'hey'
    sor_N_iters_list.append( sor_grid.N_iter )

# make plot figure of results
title = 'N iterations vs. n | SOR, Jacobi compared.'
subtitle = ''
x_label = 'n = sites on grid'
y_label = 'N = number of iterations'
legend_loc = 'upper left'
graph = Grapher( title, subtitle, x_label, y_label, legend_loc )
graph.add_data( n_list, jac_N_iters_list, 'Jacobi' )
graph.add_data( n_list, sor_N_iters_list, 'SOR' )
graph.save_to_file( '/home/res/Documents/duke/2012S/PHY260/midterm/jac_sor_' + time_string() + '.png' )

# save the data to be plotted
np.save( '/home/res/Documents/duke/2012S/PHY260/midterm/jac_N_iter_vs_n_' + time_string(), np.asarray( zip( n_list, jac_N_iters_list ) ) )
np.save( '/home/res/Documents/duke/2012S/PHY260/midterm/sor_N_iter_vs_n_' + time_string(), np.asarray( zip( n_list, sor_N_iters_list ) ) )

# show the plot
graph.show_figure()

print "Done with midterm_Dipole.py!"
Exemple #2
0
for method_class in ODE_Solver_v3.EulerCromer, ODE_Solver_v3.RungeKutta2:
    npoints_per_period = 500
    n = npoints_per_period * nperiods
    t_points = linspace( 0, T, n + 1 )      # should I make this a numpy array?
    u0_graph = Grapher( 'Theta(t)', '', 't (seconds)', 'theta (radians)', 'bottom right' )
    #title, subtitle, x_label, y_label, legend_loc 
    u1_graph = Grapher( 'Omega(t)', '', 't (seconds)', 'omega (radians per second)', 'bottom right' )
    for f in pendula:
        method = method_class( f )
        method.set_initial_condition( U0 )
        #TODO: how do I want to store results for comparing results by omega_D?
        u, t = method.solve( t_points )
        # u(t) is a 2 x n array with [u0,u1] for all t's
        u0_values = u[:, 0]  # get the u0 values from u for plotting
        u0_graph.add_data( t, u0_values, str( f.omega_D ) )
        u1_values = u[:, 1]
        u0_graph.add_data( t, u1_values, str( f.omega_D ) )
#    u0_graph.show_figure()
    u1_graph.show_figure()

for method_class, color in [( ODE_Solver_v3.RungeKutta2, 'r-' ), ( ODE_Solver_v3.EulerCromer, 'b-' )]:
    npoints_per_period = 500
    n = npoints_per_period * nperiods
    t_points = linspace( 0, T, n + 1 )      # should I make this a numpy array?
#    figure( 'theta' )       # make figure for theta(t)
    fig_u0 = figure( 1 )       # make figure for theta(t)
#    figure( 'omega' )       # and for omega(t)
    fig_u1 = figure( 2 )       # make figure for theta(t)
    #plot models for each driving frequency
    for f in pendula:
# Execute parts of the question
########

# some helper stuff
R = side_length * 0.5 # radius described as half the langth of one side of the current loop.
b_z_circular = lambda z: ( mu0_I * R ** 2 / ( 2 * ( z ** 2 + R ** 2 ) ** ( 1.5 ) ) )

# Part A:
y1_vectors = np.asarray( map( b_field, part_A_points ) )
y2 = map( b_z_circular, axis_points )
for ( label, i ) in zip( ( 'x', 'y', 'z' ), range( 3 ) ):
    title = 'Part A: B(' + label + ') for x = y = 0 (Numerical and Analytical) '
    y1 = np.hsplit( y1_vectors, 3 )[i]   # get z-axis points, in third column
    x_label = label
    graph = Grapher( title, subtitle, x_label, y_label, legend_loc )
    graph.add_data( axis_points, y1, 'Numerical: B for x = y = 0' )
    graph.add_data( axis_points, y2, 'Analytical: B for x = y = 0' )
    graph.save_to_file( '/tmp/' + 'A' + label + '.png' )

# Part B:
y1_vectors = np.asarray( map( b_field, part_B_points ) )
for ( label, i ) in zip( ( 'x', 'y', 'z' ), range( 3 ) ):
    title = 'Part B: B(' + label + ') for y=0, z=1'     
    y1 = np.hsplit( y1_vectors, 3 )[i]   # get x-axis points
    x_label = label
    graph = Grapher( title, subtitle, x_label, y_label, legend_loc )
    graph.add_data( axis_points, y1, 'Approximation: B for y=0, z=1' )
    #graph.show_figure()
    graph.save_to_file( '/tmp/' + 'B' + label + '.png' )

# Part C: