time_needed = iters_needed*args.timestep print "\nTime needed to reach equilibrium: {} seconds.".format(time_needed) if not args.equilibrium: solution = dfe.evolve(rod, xnum, pref_inv, num_iters, cold=args.cold) if args.output: print "Printing temperature solution to file "\ "{}.dat".format(args.output), mio.print_matrix_to_file(solution, args.output+".dat") print "...done" if args.printout: print "\nPrinting temperature solution to stdout..." mio.print_matrix(solution) print "...done" if args.plot: for i in range(len(args.plot)): if args.plot[i] == "normal": plotting.plot_normal(xnum, solution, filename="rod_normal") if args.plot[i] == "contour": plotting.plot_contour(xnum, args.width, spacing, solution.transpose(), title="phi", filename="rod_contour") if args.plot[i] == "surface": plotting.plot_surface(xnum, args.width, spacing, solution.transpose(), title="phi", filename="rod_surface") # The title is just a way of making subtly different plots for the Laplace # and heat diffusion sections with the same reusable plotting code.
# Apply a zero-dirichlet condition b_dofs = V.on_boundary() for i in b_dofs: A[i, :] = 0 A[i, i] = 1 b[i] = 0 return A bc_apply(A, L) from numpy.linalg import solve u_h = solve(A, L) return u_h, V if __name__ == "__main__": f = "4*(-y**2+y)*sin(pi*x)" # f = "8*pi**2*sin(2*pi*x)*sin(2*pi*y)" # f = "x**2+cos(2*pi*y)" def f_internal(x, y): from sympy import pi, sin, cos return eval(f) u_h, V = main(25, 10, f_internal, quad_degree=2) from plotting import plot_at_vertex, plot_custom, plot_contour plot_at_vertex(u_h, V) plot_custom(u_h, V, 40) plot_contour(u_h, V)
xx, yy = mesh_wedge(domain) # determine cell metrics for grid from calc_cell_metrics import cellmetrics mesh = cellmetrics(xx, yy, domain) # initialize state vector, simulation parameters and fluid properties class parameters: M_in = 1.8 p_in = 101325 T_in = 300 iterations = 5000 tolerance = 1e-6 CFL = 0.4 class gas: gamma = 1.4 Cp = 1006 R = 287 from initialize import init_state state = init_state(domain, mesh, parameters, gas) # run AUSM scheme from schemes import AUSM, AUSMplusup, AUSMDV state = AUSMDV( domain, mesh, parameters, state, gas ) # call plotting functions from plotting import plot_mesh, plot_contour #plot_mesh(mesh) plot_contour(domain, mesh, state)
solution = lps.solve_laplace(args.xnum, args.ynum, args.method, err_tol=args.error, input_matrix=matrix, boundary_cond=args.boundary) end = time.clock() print "Time taken to converge: ", end - start, " processor seconds." print "\n... done" if args.plot: for i in range(len(args.plot)): if args.plot[i] == "surface": plotting.plot_surface(args.xnum, args.ynum, spacing, solution) if args.plot[i] == "contour": plotting.plot_contour(args.xnum, args.ynum, spacing, solution) if args.plot[i] == "vector": plotting.plot_vector(args.xnum, args.ynum, spacing, solution) if args.plot[i] == "magnitude": plotting.plot_magnitude(args.xnum, args.ynum, spacing, solution) if args.output: print "\nPrinting potential field solution to file "\ "'{}_potential.dat'... ".format(args.output), mio.print_matrix_to_file(solution, args.output + "_potential.dat") print "done" print "Printing magnitude of electric field solution to file "\ "'{}_field_strength.dat'...".format(args.output), grad = np.gradient(solution) mag = np.sqrt(grad[0] * grad[0] + grad[1] * grad[1])
from plotting import plot_contour import matplotlib.pyplot as plt import numpy as np format = 'png' P = np.array([[4, 1], [1, 2]]) x = np.array([[1], [1]]) f = lambda x: np.sum(np.array(x) @ P @ np.array(x)) dfx = 0.5 * P @ x # make contours fig, ax = plt.subplots() plot_contour(f, [0, 2], [0, 2], ax, False) ax.arrow(x[0, 0], x[1, 0], dfx[0, 0] * 0.2, dfx[1, 0] * 0.2, color=red) ax.text(x[0, 0] + dfx[0, 0] * 0.2 + 0.05, x[1, 0] + dfx[1, 0] * 0.2 + 0.05, r'$\nabla f(\mathbf{x}^{(k)})$', color=red) ax.text(x[0, 0] + 0.1, x[1, 0] + 0.1, r'$\mathbf{x}^{(k)}$', color=black) def compute_x2_line(x1): # function to compute the line in x, orth to dfx intercept = np.sum(dfx * x) return (intercept - x1 * dfx[0, 0]) / dfx[1, 0]