0% found this document useful (0 votes)
32 views3 pages

Solution of 2D Continuity Equation in Python

The document presents a solution to the 2D Continuity Equation using both analytical and numerical methods in Python. It includes code that utilizes Numpy and Matplotlib to simulate a Gaussian distribution for density, calculate velocity components, and generate visualizations of the velocity field, density contours, and density variations along specified axes. The assignment focuses on hypersonic aerothermodynamics and demonstrates the application of computational techniques in fluid dynamics.

Uploaded by

Code One
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views3 pages

Solution of 2D Continuity Equation in Python

The document presents a solution to the 2D Continuity Equation using both analytical and numerical methods in Python. It includes code that utilizes Numpy and Matplotlib to simulate a Gaussian distribution for density, calculate velocity components, and generate visualizations of the velocity field, density contours, and density variations along specified axes. The assignment focuses on hypersonic aerothermodynamics and demonstrates the application of computational techniques in fluid dynamics.

Uploaded by

Code One
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Pranav Balaji | RA2111019010020

Solution of 2D Continuity Equation in Python


Assignment – 2 | Hypersonic Aerothermodynamics

Statement
Solve analytically & numerically, the 2D Continuity Equation.

Analytical Solution-

Assignment 2 | 1/3
Pranav Balaji | RA2111019010020

Numerical Solution-
Language used- Python
Libraries used- Numpy, Matplotlib

Code-
import numpy as np
import matplotlib.pyplot as plt

# Grid params
nx = 50
ny = 50
Lx = 1.0
Ly = 1.0
dx = Lx / (nx - 1)
dy = Ly / (ny - 1)

# Gaussian distribution for rho


x = np.linspace(0, Lx, nx)
y = np.linspace(0, Ly, ny)
X, Y = np.meshgrid(x, y)
rho = np.exp(-((X - 0.5)**2 + (Y - 0.5)**2) / 0.1**2)

# Velocity components u, v
u = np.zeros((nx, ny)) # x-component of velocity
v = np.zeros((nx, ny)) # y-component of velocity

# Boundary conditions for u, v


u[:, 0], u[:, -1] = 0, 0
u[0, :], u[-1, :] = 0, 0

v[:, 0], v[:, -1] = 0, 0


v[0, :], v[-1, :] = 0, 0

# Iteration for discretized 2d continuity


for i in range(1, nx-1):
for j in range(1, ny-1):
u[i, j] = -(rho[i+1, j] - rho[i-1, j]) / (2 * dx)
v[i, j] = -(rho[i, j+1] - rho[i, j-1]) / (2 * dy)

fig, axs = plt.subplots(1, 3, figsize=(15, 5))

# Velocity field plot


mask = np.isnan(u) | np.isnan(v) | np.isinf(u) | np.isinf(v) # Create mask for invalid
values
u_masked = np.ma.masked_where(mask, u)
v_masked = np.ma.masked_where(mask, v)

Assignment 2 | 2/3
Pranav Balaji | RA2111019010020

non_zero_mask = (u_masked**2 + v_masked**2) > 0


if np.any(non_zero_mask):
axs[0].quiver(u_masked[non_zero_mask], v_masked[non_zero_mask])

axs[0].set_title('Velocity Field')
axs[0].set_xlabel('x')
axs[0].set_ylabel('y')
axs[0].grid(True)

# Density contour plot


axs[1].contourf(rho, cmap='viridis')
axs[1].set_title('Contour Plot of Density')
axs[1].set_xlabel('x')
axs[1].set_ylabel('y')
axs[1].grid(True)

# Rho vs length plot


x_line = np.linspace(0, Lx, nx)
y_line = np.linspace(0, Ly, ny)
x_index = nx // 2
y_index = ny // 2
axs[2].plot(x_line, rho[:, y_index], label='Density along y')
axs[2].plot(y_line, rho[x_index, :], label='Density along x')
axs[2].set_title('Density vs Length')
axs[2].set_xlabel('Length')
axs[2].set_ylabel('Density')
axs[2].legend()
axs[2].grid(True)

plt.tight_layout()
plt.show()

Output-

Assignment 2 | 3/3

You might also like