scientific_comp_projects/CODE/CFD/[barbara]12_steps_navierstokes/1D_diffusion.py

52 lines
1.5 KiB
Python

import numpy as np
import matplotlib.pyplot as plt
import time, sys
"""
Simulation comments :
Since we doing diffusion, which is a isotropic phenomena, we add the central differences scheme to have a isotropic scheme.
"""
# Physical parameters.
vis = 0.3
# Spatial parameters.
nx = 41 # Number of grid points.
dx = 2 / (nx-1) # Distance between any pair of adjacent grid points.
#Time parameters.
nt = 40 # Number of timesteps we want to calculate.
sigma = 0.3 # Sigma is a parameter defined later, but the resolution uses it.
#dt = 0.01 # Amount of time each timesteps covers (delta t)
dt = sigma * dx**2 / vis # dt is defined using sigma, see later lessons for the definition.
tT = dt*nt
print('Total time : ' + str(tT))
print(dx)
# Boundary conditions.
# Starting all values as 1 m/s.
u = np.ones(nx)
# Setting u = 2 between 0.5 and 1 as per out initial conditions.
u[int(0.5 / dx):int(1 / dx + 1)] = 2
# PLot initial conditions.
fig, ax = plt.subplots(nrows=1, ncols =2, sharey=True)
ax[0].plot(np.linspace(0,2,nx),u)
# Temporary array for the solutions.
un = np.ones(nx)
for n in range(nt): # Loop for values of n from 0 to nt.
un = u.copy() # Copy the existing values of u into un.
print('This is time step : ' + str(n))
print(un)
for i in range(1, nx-1): # Loop for values in u from 1 to nx -1
u[i] = un[i] + vis * dt / (dx)**2 * (un[i+1]-2*un[i] + un[i-1])
# PLot the solution at t = t_final.
ax[1].plot(np.linspace(0,2,nx), u)
plt.show()