def plot_single_modes(): home1 = home + 'single_modes/' directory_checker(home1) if os.path.isfile(home + 'Z_ntxy.npy') == False: mk_array_single() Z_ntxy = np.load(home + 'Z_ntxy.npy') def plot_single(n): Z0n = Z_ntxy[n, 0, :, :] Zmax = np.max(Z0n) w = omega[0, n] fig = plt.figure() ax = plt.gca(projection='3d') ax.set_title('$\omega_{0,%s} = %.2f$' % (n, w)) ax.set_aspect(1) ax.set_xticks([]) ax.set_yticks([]) ax.set_zticks([0]) surf = ax.plot_surface(X, Y, Z0n, cmap=color, vmin=-Zmax, vmax=Zmax) fig.colorbar(surf, cmap=color, shrink=.8) fig.savefig(home1 + 'm_0_n_%s.png' % (n)) plt.close() for n in range(modes): plot_single(n)
def exercise_52(color=cm.seismic): print("\nExercise 52") home = parent + 'exercise_52/' directory_checker(parent) directory_checker(home) set_mpl_params() def plot_mn(i): m = M[i] n = N[i] w = Omega[i] Z = Z_t(1, 0, i, 0) fig = plt.figure() ax = plt.gca(projection='3d') ax.set_title('$\omega_{%s%s} = %.2f$' % (m, n, w)) ax.set_aspect(1) ax.set_xticks([]) ax.set_yticks([]) ax.set_zticks([0]) ax.set_zlim([-1, 1]) surf = ax.plot_surface(X, Y, Z, cmap=color) # fig.colorbar(surf, cmap=color, shrink=.8) name = home + 'm_%s_n_%s.png' % (m, n) fig.savefig(name) plt.close() print("saved figure to %s" % name) for i in range(6): plot_mn(i)
def mk_movie(): directory_checker('square_drum/') FFMpegWriter = manimation.writers['ffmpeg'] metadata = dict(title='Square Drum', artist='Matplotlib') writer = FFMpegWriter(fps=10, metadata=metadata) fig = plt.figure() ax = fig.gca(projection='3d') with writer.saving(fig, "square_drum/Square_drum_animation.mp4", 100): for i in range(N_lowest): fig.clear() ax = fig.gca(projection='3d') period = 2 * pi / Omega[i] T = np.linspace(0, period, 10) Z0 = Z_tmn(0, M[i], N[i], Omega[i]) Zmax = np.max(Z0) surf = ax.plot_surface(X, Y, Z0, cmap=cm.seismic) fig.colorbar(surf, shrink=0.5, aspect=5) for t in T: Z = Z_tmn(t, M[i], N[i], Omega[i]) ax.clear() ax.set_title( 't = %.2f, $\omega$ = %.2f, m = %s, n = %s, a = %s, b = %s, v = %s, $\epsilon$ = %s' % (t, Omega[i], M[i], N[i], a, b, v, ep)) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlim(-1.1 * Zmax, 1.1 * Zmax) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) ax.plot_surface(X, Y, Z, cmap=cm.seismic, vmin=-Zmax, vmax=Zmax) writer.grab_frame()
def problem_2(modes=5, fpm=50, color=cm.seismic): """ args ---- modes: The number of modes that will be in the movies fpm: frames per mode color: the color scheme for the images and movies """ home = parent + 'Problem2/' directory_checker(home) def solve_boundary(): """ set up expression """ # indecies m, n = sy.symbols('m n', integer=True) # constants phim, a, d, v = sy.symbols('phi_m a d v', real=True) # variables rho, phi, t, omega, x = sy.symbols('rho phi t omega x', real=True, positive=True) # Functions # A = sy.Function('A')(m,n) A = sy.symbols('A', real=True) J = besselj(m, omega * rho / v) Z = A * J * sy.cos(m * phi) * sy.cos(omega * t + phim) """ solve first initial condition Zdot(rho,phi,t=0) = 0 """ # find the left hand side - time derivative of Z lhs = sy.diff(Z, t) # set t = 0 lhs = lhs.subs(t, 0) # set Z1 = 0 eq1 = sy.Eq(lhs, 0) # all factors are save except for phi_m; solve for phi_m phim1 = m * sy.pi # sympy.solve can be used, but it returns the trival case # substitute new value of phim into original Z Z = Z.subs(phim, phim1) """ solve the implied condition that d/drho Z(rho=a,phi,t) = 0 """ # set left hand side to d/drho Z lhs = sy.diff(Z, rho) # set rho = a lhs = lhs.subs(rho, a) """ J' has to vanish, which means that the two terms shown in the output must equal 0 which implies m = 0 """ # set m = 0 Z = Z.subs(m, 0) """ plug in condition Z(rho,phi,t=0) = d(1-rho/a) """ lhs = Z.subs(t, 0) rhs = d * (1 - rho / a) """ we need to get rid of rho dependance, so exploit orthoganality of Jm(x) http://www.math.usm.edu/lambers/mat415/lecture15.pdf """ omega1 = sy.symbols('omega_1', positive=True, real=True) lhs *= rho * besselj(0, omega1 * rho / v) rhs *= rho * besselj(0, omega1 * rho / v) # integrate both sides WRT rho lhs = sy.integrate(lhs, (rho, 0, a)) rhs = sy.integrate(rhs, (rho, 0, a)) """ looking at the output, we know that omega = omega1 and that the first term will vanish when the root is plugged in.""" eq = sy.Eq(lhs, rhs) eq = eq.subs(omega1, omega) # replace a omega / v with zeta_0 (the nth root of J0) zeta0 = sy.symbols('zeta_0', positive=True) eq = eq.subs(a * omega / v, zeta0) A1 = solve(eq, A)[0] An = sy.lambdify((a, d, v, omega, zeta0), A1, 'sympy') # pdb.set_trace() return An def mk_array_single(): print("\nstarting Z_ntxy creation...") # calculate coefficient function An = solve_boundary() def A_n(n): return An(a, d, v, omega[0, n], zeta[0, n]).evalf() # make empty drum head height array w/ indecies (mode, time, x, y) Z_ntxy = np.zeros((modes, fpm, 100, 100)) T = np.zeros((modes, fpm)) for n in range(modes): print("\nmode: %s/%s..." % (n + 1, modes)) omega_n = omega[0, n] T_n = np.linspace(0, 2 * np.pi / omega_n, fpm) T[n, :] = T_n A = A_n(n) for ti, time in enumerate(T_n): print("time index: %s/%s... " % (ti + 1, fpm)) Z_ntxy[n, ti, :, :] = Z_t(A, 0, n, time) print("\nsaving arrays to %s" % home) np.save(home + 'Z_ntxy.npy', Z_ntxy) np.save(home + 'T_ntxy.npy', T) def mk_array_multiple(): print("\nstarting Z_txy creation...") # calculate coefficient function An = solve_boundary() def A_n(n): return An(a, d, v, omega[0, n], zeta[0, n]).evalf() omega0 = omega[0, :modes] T0 = 2 * np.pi / omega0 Tmin = np.min(T0) Tmax = np.max(T0) factor = math.ceil(Tmax / Tmin) print("factor: %s" % factor) # pdb.set_trace() T = np.linspace(0, factor * Tmin, factor * fpm) Z_txy = np.zeros((factor * fpm, 100, 100)) for it, time in enumerate(T): print("\ntime index: %s/%s..." % (it + 1, factor * fpm)) for n in range(modes): print("mode: %s/%s..." % (n + 1, modes)) A = A_n(n) Z_txy[it, :, :] += Z_t(A, 0, n, time) print("\nsaving arrays to %s" % home) np.save(home + "Z_txy.npy", Z_txy) np.save(home + "T_txy.npy", T) def plot_single_modes(): home1 = home + 'single_modes/' directory_checker(home1) if os.path.isfile(home + 'Z_ntxy.npy') == False: mk_array_single() Z_ntxy = np.load(home + 'Z_ntxy.npy') def plot_single(n): Z0n = Z_ntxy[n, 0, :, :] Zmax = np.max(Z0n) w = omega[0, n] fig = plt.figure() ax = plt.gca(projection='3d') ax.set_title('$\omega_{0,%s} = %.2f$' % (n, w)) ax.set_aspect(1) ax.set_xticks([]) ax.set_yticks([]) ax.set_zticks([0]) surf = ax.plot_surface(X, Y, Z0n, cmap=color, vmin=-Zmax, vmax=Zmax) fig.colorbar(surf, cmap=color, shrink=.8) fig.savefig(home1 + 'm_0_n_%s.png' % (n)) plt.close() for n in range(modes): plot_single(n) def mk_movie_single_modes(): print("\nstarting single modes...") if os.path.isfile(home + 'Z_ntxy.npy') == False: mk_array_single() Z_ntxy = np.load(home + 'Z_ntxy.npy') # [n,t,x,y] T = np.load(home + 'T_ntxy.npy') # [n,t] print("loaded arrays") plt.close('all') FFMpegWriter = manimation.writers['ffmpeg'] metadata = dict(title='Problem 2: Circular Drum', artist='Matplotlib') writer = FFMpegWriter(fps=40, metadata=metadata) fig = plt.figure() ax = fig.gca(projection='3d') with writer.saving(fig, home + "single_modes_animation.mp4", 100): for n in range(modes): print("\nmode: %s/%s..." % (n + 1, modes)) fig.clear() ax = fig.gca(projection='3d') Zn0 = Z_ntxy[n, 0, :, :] Zmax = np.max(Zn0) surf = ax.plot_surface(X, Y, Zn0, cmap=color, vmin=-Zmax, vmax=Zmax) fig.colorbar(surf, shrink=0.5) for it, time in enumerate(T[n, :]): print("time index: %s/%s..." % (it + 1, fpm)) ax.clear() ax.set_title('n:%s , t:%.2f' % (n, time)) ax.set_xticks([]) ax.set_yticks([]) ax.set_zticks([0]) ax.set_zlim(-Zmax, Zmax) ax.plot_surface(X, Y, Z_ntxy[n, it, :, :], cmap=color, vmin=-Zmax, vmax=Zmax) writer.grab_frame() plt.close() print("finished") def mk_movie_multiple_modes(): print("\nstarted multiple modes...") if os.path.isfile(home + 'Z_txy.npy') == False: mk_array_multiple() Z_txy = np.load(home + 'Z_txy.npy') T = np.load(home + 'T_txy.npy') print("loaded arrays") plt.close('all') FFMpegWriter = manimation.writers['ffmpeg'] metadata = dict(title='Problem 2: Circular Drum', artist='Matplotlib') writer = FFMpegWriter(fps=40, metadata=metadata) Z0 = Z_txy[0, :, :] Zmax = np.max(Z0) fig = plt.figure() ax = fig.gca(projection='3d') surf = ax.plot_surface(X, Y, Z0, cmap=color, vmin=-Zmax, vmax=Zmax) fig.colorbar(surf, shrink=.8) with writer.saving(fig, home + "multiple_modes_animation.mp4", 100): for it, time in enumerate(T): print("time index: %s/%s" % (it + 1, len(T))) ax.clear() ax.set_title('t:%.2f' % time) ax.set_xticks([]) ax.set_yticks([]) ax.set_zticks([0]) ax.set_zlim(-Zmax, Zmax) ax.plot_surface(X, Y, Z_txy[it, :, :], cmap=color, vmin=-Zmax, vmax=Zmax) writer.grab_frame() plt.close() print("finished...") # plot_single_modes() # mk_movie_single_modes() mk_movie_multiple_modes()
import matplotlib.pyplot as plt from library.misc import directory_checker, find_lowest from library.plotting import set_mpl_params from mpl_toolkits.mplot3d import Axes3D import matplotlib.animation as ani import matplotlib.animation as manimation from matplotlib import cm from sympy.solvers import solve from sympy import init_printing import os import math init_printing() set_mpl_params() parent = "T13_BesselFunctions/" directory_checker(parent) pr = sy.pprint #=============================================================================== """ Bessel Series Representation """ #=============================================================================== def Jm_pos(m, x, eps=1e-15): """ BF: Eq 29 """ def J_n(n): return (-1)**n * (x / 2)**(2 * n + m) / (sy.factorial(n) * sy.gamma(n + m + 1)) n = 1 Ji = J_n(0)
""" general parameters """ #=============================================================================== a, b = 1, 1 X = np.linspace(0, a, 100) Y = np.linspace(0, b, 100) X, Y = np.meshgrid(X, Y) Nmax = 100 N = np.arange(1, Nmax + 1) N = N * 2 - 1 #=============================================================================== """ square drum """ directory_checker('square_drum/') dirname = 'square_drum/single_frequency/' directory_checker(dirname) #=============================================================================== def Z_tmn(t, m, n, omega): """ square drum surface values at time t """ # amplitude factor outside of sum A = ep * (2 / pi)**6 # wavenumbers and frequency km = m * pi / a kn = n * pi / b