def calc_solution(courant, dx, axes, plot_initial=False): xmax = 4.0 tmax = 1.0 speed = 2.0 dt = dx * courant / speed nx = int(xmax / dx) nt = int(tmax / dt) x = np.linspace(0.0, xmax, nx) u = np.ones(nx) u[int(0.5 / dx):int(1.0 / dx + 1)] = speed strings = [] strings.append(f"dx = {dx:>5.2e}") strings.append(f"dt = {dt:>5.2e}") strings.append(f"CFL = {speed * dt / dx:>5.2e}") print(" | ".join(strings)) if plot_initial: axes.plot(x, u, "r-", label="Initial Solution") for _ in range(nt): solver(dx, dt, u) axes.title.set_text(f"Time = {dt*nt:12.5f}") axes.plot(x, u, "-", label=f"Courant = {courant:.3f}")
def calc_solution(diffusion, nu, dx, axes, plot_initial=False): xmax = 4.0 tmax = 1.0 speed = 2.0 dt = diffusion * dx * dx / nu nx = int(xmax / dx) nt = int(tmax / dt) + 1 x = np.linspace(0.0, xmax, nx) u = np.ones(nx) u[int(0.5 / dx):int(1.0 / dx + 1)] = speed strings = [] strings.append(f"nu = {nu:>5.2e}") strings.append(f"dx = {dx:>5.2e}") strings.append(f"dt = {dt:>5.2e}") strings.append(f"CFL = {speed * dt / dx:>5.2e}") strings.append(f"VIS = {dt * nu / dx / dx:>5.2e}") print(" | ".join(strings)) if plot_initial: axes.plot(x, u, "r-", label="Initial Solution") for _ in range(nt): solver(nu, dx, dt, u) axes.title.set_text(f"Time = {dt*nt:12.5f}") axes.plot(x, u, "-", label=f"Diffusion = {diffusion:.3f} (nu = {nu:.2e})")
def calc_solution(diffusion, nu, dx, axes, plot_initial=False): xmax = 2.0 * np.pi tmax = 0.1 dt = diffusion * dx * dx / nu nx = int(xmax / dx) nt = int(tmax / dt) + 1 func = get_function() x = np.linspace(0.0, xmax, nx) u = np.asarray([func(0.0, x0, nu) for x0 in x]) strings = [] strings.append(f"nu = {nu:>5.2e}") strings.append(f"dx = {dx:>5.2e}") strings.append(f"dt = {dt:>5.2e}") strings.append(f"CFL = {max(u) * dt / dx:>5.2e}") strings.append(f"VIS = {dt * nu / dx / dx:>5.2e}") print(" | ".join(strings)) if plot_initial: axes.plot(x, u, "r-", label="Initial Solution") for _ in range(nt): solver(nu, dx, dt, u) axes.title.set_text(f"Time = {dt*nt:12.5f}") axes.plot(x, u, "-", label=f"Diffusion = {diffusion:.3f} (nu = {nu:.2e})")
def calc_solution(inner, diffusion, nu, dx, dy): xmax = 5.0 ymax = 2.0 tmax = 5.0 rho = 1.0 dt = diffusion * min(dx, dy)**2 / nu nx = int(xmax / dx) ny = int(ymax / dy) nt = int(tmax / dt) + 1 x = np.linspace(0.0, xmax, nx) y = np.linspace(0.0, ymax, ny) X, Y = np.meshgrid(x, y, indexing="ij") u = np.zeros((nx, ny), order="F") v = np.zeros((nx, ny), order="F") p = np.ones((nx, ny), order="F") f = np.ones((nx, ny), order="F") * 5.0 title = "Navier-Stokes Channel Flow (2D)" print("-".center(80, "-")) print(title.center(80)) print("-".center(80, "-")) strings = [] strings.append(f"dx = {dx:>5.2e}") strings.append(f"dy = {dy:>5.2e}") strings.append(f"dt = {dt:>5.2e}") strings.append(f"CFL = {1.0 * dt / min(dx, dy):>5.2e}") strings.append(f"VIS = {dt * nu / min(dx, dy)**2:>5.2e}") print(" | ".join(strings)) for _ in range(nt): solver(inner, dx, dy, dt, rho, nu, f, p, u, v) figure = plt.figure(figsize=(15, 8), dpi=100) figure.suptitle(title) colormap = cm.viridis # @UndefinedVariable axes = figure.add_subplot(111) axes.title.set_text("Velocity U") k = 2 contours = axes.contourf(X, Y, u, 10, alpha=0.5) axes.contour(X, Y, u, cmap=colormap) axes.quiver(X[::k, ::k], Y[::k, ::k], u[::k, ::k], v[::k, ::k]) divider = make_axes_locatable(axes) cax = divider.append_axes("right", size="5%", pad=0.05) figure.colorbar(contours, cax=cax, orientation="vertical") axes.set_xlabel("x") axes.set_ylabel("y") axes.set_aspect("equal") plt.tight_layout()
def calc_solution(iterations, dx, dy, plot_3d): xmax = 2.0 ymax = 1.0 nx = int(xmax / dx) ny = int(ymax / dy) x = np.linspace(0.0, xmax, nx) y = np.linspace(0.0, ymax, ny) X, Y = np.meshgrid(x, y, indexing="ij") p = np.zeros((nx, ny), order="F") p[-1, :] = y title = "Laplace Equation (2D)" figure = plt.figure(figsize=(11, 7), dpi=100) figure.suptitle(title) colormap = cm.viridis # @UndefinedVariable axes = {} if plot_3d: axes[0] = figure.add_subplot(121, projection="3d") axes[1] = figure.add_subplot(122, projection="3d") for i in [0, 1]: axes[i].view_init(30, 225) axes[i].set_xlabel("x") axes[i].set_ylabel("y") axes[i].set_zlabel("u") axes[0].plot_surface(X, Y, p, cmap=colormap, rstride=1, cstride=1) else: axes[0] = figure.add_subplot(121) axes[1] = figure.add_subplot(122) for i in [0, 1]: axes[i].set_xlabel("x") axes[i].set_ylabel("y") contours = axes[0].contourf(X, Y, p) axes[0].set_aspect("equal") divider = make_axes_locatable(axes[0]) cax = divider.append_axes("right", size="5%", pad=0.05) figure.colorbar(contours, cax=cax, orientation="vertical") axes[0].title.set_text("Initial Solution") axes[1].title.set_text("Final Solution") for _ in range(iterations): residual = solver(dx, dy, p) print("-".center(80, "-")) print(title.center(80)) print("-".center(80, "-")) strings = [] strings.append(f"dx = {dx:>5.2e}") strings.append(f"dy = {dy:>5.2e}") strings.append(f"iterations = {iterations:>5d}") strings.append(f"residual = {residual:>5.2e}") print(" | ".join(strings)) if plot_3d: axes[1].plot_surface(X, Y, p, cmap=colormap, rstride=1, cstride=1) else: contours = axes[1].contourf(X, Y, p) axes[1].set_aspect("equal") divider = make_axes_locatable(axes[1]) cax = divider.append_axes("right", size="5%", pad=0.05) figure.colorbar(contours, cax=cax, orientation="vertical") plt.tight_layout()
def calc_solution(courant, dx, dy, plot_3d=False): xmax = 2.0 ymax = 2.0 tmax = 0.5 speed = 2.0 dt = min(dx, dy) * courant / speed nx = int(xmax / dx) ny = int(ymax / dy) nt = int(tmax / dt) x = np.linspace(0.0, xmax, nx) y = np.linspace(0.0, ymax, ny) X, Y = np.meshgrid(x, y, indexing="ij") u = np.ones((nx, ny), order="F") for j in range(ny): for i in range(nx): if (0.5 <= X[i, j] <= 1.0) and (0.5 <= Y[i, j] <= 1.0): u[i, j] = speed title = "Linear Convection (2D)" print("-".center(80, "-")) print(title.center(80)) print("-".center(80, "-")) strings = [] strings.append(f"dx = {dx:>5.2e}") strings.append(f"dy = {dy:>5.2e}") strings.append(f"dt = {dt:>5.2e}") strings.append(f"CFL = {speed * dt / min(dx, dy):>5.2e}") print(" | ".join(strings)) figure = plt.figure(figsize=(11, 7), dpi=100) figure.suptitle(title) colormap = cm.viridis # @UndefinedVariable axes = {} if plot_3d: axes[0] = figure.add_subplot(121, projection="3d") axes[1] = figure.add_subplot(122, projection="3d") for i in [0, 1]: axes[i].view_init(30, 225) axes[i].set_xlabel("x") axes[i].set_ylabel("y") axes[i].set_zlabel("u") axes[0].plot_surface(X, Y, u, cmap=colormap, rstride=1, cstride=1) else: axes[0] = figure.add_subplot(121) axes[1] = figure.add_subplot(122) for i in [0, 1]: axes[i].set_xlabel("x") axes[i].set_ylabel("y") contours = axes[0].contourf(X, Y, u) axes[0].set_aspect("equal") divider = make_axes_locatable(axes[0]) cax = divider.append_axes("right", size="5%", pad=0.05) figure.colorbar(contours, cax=cax, orientation="vertical") axes[0].title.set_text("Initial Solution") axes[1].title.set_text("Final Solution") for _ in range(nt): solver(dx, dy, dt, u) if plot_3d: axes[1].plot_surface(X, Y, u, cmap=colormap, rstride=1, cstride=1) else: contours = axes[1].contourf(X, Y, u) axes[1].set_aspect("equal") divider = make_axes_locatable(axes[1]) cax = divider.append_axes("right", size="5%", pad=0.05) figure.colorbar(contours, cax=cax, orientation="vertical") plt.tight_layout()