我想在以下平流方程问题中实现迎风法:
为了
有风系数,,导致库朗数
但这似乎不对。我做错了什么?我将不胜感激任何帮助。
import numpy as np
import matplotlib.pyplot as plt
class UpwindMethod:
def __init__(self, Nx,Nt, tmax):
self.Nx = Nx # number of space nodes
self.Nt = Nt # number of time nodes
self.tmax = tmax
self.xmin = 0
self.xmax = 1
self.dt = tmax/self.Nt # timestep
self.a = 2 # velocity
self.v = (self.a * self.dt ) / ((self.xmax - self.xmin)/self.Nx)
self.initializeDomain()
self.initializeU()
self.initializeParams()
def initializeDomain(self):
self.dx = (self.xmax - self.xmin)/self.Nx
self.x = np.arange(self.xmin-self.dx, self.xmax+(2*self.dx), self.dx)
def initializeU(self):
u0 = np.where( (0.1<self.x) & (self.x<0.2) , ((10**4)*((0.1-self.x))**2 *((0.2-self.x)**2)) ,0)
self.u = u0.copy()
self.unp1 = u0.copy()
def initializeParams(self):
self.nsteps = round(self.tmax/self.dt)
def solve_and_plot(self):
tc = 0.
for n in range(self.nsteps):
plt.clf()
for i in range(self.Nx+2):
self.unp1[i] = self.u[i] - ((self.a*self.dt)/(self.dx))*(self.u[i]-self.u[i-1])
self.u = self.unp1.copy()
# Periodic boundary conditions
self.u[0] = self.u[self.Nx+1]
self.u[self.Nx+2] = self.u[1]
uexact = np.where( (0.1<self.x) & (self.x<0.2) , ((10**4)*((0.1-self.x-self.a* tc)**2) *((0.2-self.x-self.a* tc)**2)) ,0)
plt.plot(self.x, uexact, label=" Exact solution ")
plt.plot(self.x, self.u, label=" Upwind Approximation ")
plt.xlabel("x")
plt.ylabel("u")
plt.legend(loc=1, fontsize=13)
tc += self.dt
def main():
sim = UpwindMethod(100,250, 0.02)
sim.solve_and_plot()
plt.show()
if __name__ == "__main__":
main()
```

