Question: this python program give me three plots, can I get a modified version where it prints a table of the data points for the plots?

this python program give me three plots, can I get a modified version where it prints a table of the data points for the plots?
import numpy as np
import matplotlib.pyplot as plt
# Constants
gamma =1.4 # Specific heat ratio for air
CFL =0.5 # (Courant-Friedrichs-Lewy)
# Function to initialize grid and initial conditions
def initialize_grid(nx, xmax, rho_l, u_l, p_l, rho_r, u_r, p_r):
dx = xmax /(nx -1)
x = np.linspace(0, xmax, nx)
rho = np.zeros(nx)
u = np.zeros(nx)
p = np.zeros(nx)
# Initial conditions
rho[x xmax /2]= rho_l
rho[x >= xmax /2]= rho_r
u[x xmax /2]= u_l
u[x >= xmax /2]= u_r
p[x xmax /2]= p_l
p[x >= xmax /2]= p_r
return x, rho, u, p, dx
# Function to compute time step based on CFL condition
def compute_dt(dx, u):
dt = CFL * dx / np.max(np.abs(u)+ np.sqrt(gamma * p / rho))
return dt
# Function to update solution using Lax-Friedrichs scheme
def lax_friedrichs_step(rho, u, p, dt, dx):
rho_n = np.copy(rho)
u_n = np.copy(u)
p_n = np.copy(p)
# Fluxes
F_rho = rho * u
F_u = rho * u **2+ p
F_p =(gamma * p)/(gamma -1)* u +0.5* rho * u **3
# Update solution
rho_n[1:-1]=0.5*(rho[:-2]+ rho[2:])- dt /(2* dx)*(F_rho[2:]- F_rho[:-2])
u_n[1:-1]=0.5*(u[:-2]+ u[2:])- dt /(2* dx)*(F_u[2:]- F_u[:-2])
p_n[1:-1]=0.5*(p[:-2]+ p[2:])- dt /(2* dx)*(F_p[2:]- F_p[:-2])
return rho_n, u_n, p_n
# Function to plot results
def plot_results(x, rho, u, p, title):
plt.figure(figsize=(12,6))
plt.subplot(3,1,1)
plt.plot(x, rho, 'r-', label='Density Profile') #'r-'stand for the wave color red
plt.xlabel('Position')
plt.ylabel('Density')
plt.title(title)
plt.legend()
plt.subplot(3,1,2)
plt.plot(x, u,'b-', label='Velocity Profile') #'b-'stand for the wave color blue
plt.xlabel('Position')
plt.ylabel('Velocity')
plt.legend()
plt.subplot(3,1,3)
plt.plot(x, p,'g-', label='Pressure Profile') #'g-'stand for the wave color green
plt.xlabel('Position')
plt.ylabel('Pressure')
plt.legend()
plt.tight_layout()
plt.show()
# Parameters of the plots
nx =201 # Number of grid points
xmax =10.0 # Maximum x value
rho_l =1.0 # Left density
u_l =0.0 # Left velocity
p_l =1.0 # Left pressure
rho_r =0.125 # Right density
u_r =0.0 # Right velocity
p_r =0.1 # Right pressure
T =5.0 # Total simulation time
# Initialize grid and initial conditions
x, rho, u, p, dx = initialize_grid(nx, xmax, rho_l, u_l, p_l, rho_r, u_r, p_r)
# Main loop
t =0.0
while t T:
dt = compute_dt(dx, u)
rho, u, p = lax_friedrichs_step(rho, u, p, dt, dx)
t += dt
# Plot results
plot_results(x, rho, u, p, 'Shock Wave Simulation')
Output:
Shock Wave Simulation
this python program give me three plots, can I

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!