如何在python中模拟耦合的PDE

时间:2017-05-02 16:11:29

标签: python numpy pde

我试图在时间和空间上模拟以下偏微分方程系统。我正在使用python 3。

Here is a link to the set of equations with their boundary conditions

我的想法是将所有方程转换为离散形式(前向欧拉作为最简单的起点),然后运行代码。 转发欧拉意味着: Here lin to image i = 0,...,Nx - mesh for n = 0,1,...,Nt 这就是我所拥有的(通过numpy)

import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
#Define exponents for PDE
m = 0
n = 2
#Define constants for PDE
a = 0.2
b= -0.4
av = 5.0
c = 0.6
d = -0.8
Du = 1
Dv = 20
Dz = 1000
u0 = 0.5
v0 = 0.5
kz = 0.001

L = 10
Nx = 100
T = 5
Nt = 100
x = np.linspace(0, L, Nx+1)
dx = x[1] - x[0]
#print(dx)
#print(dt)
t = np.linspace(0, T, Nt+1)
dt = t[1] - t[0]
if dt<=0.5*dx**2:
    print("Ok!")
else:
    print("Alert! dt is not smaller than dx^2/2")
u = np.zeros(Nx+1)
v = np.zeros(Nx+1)
z = np.zeros(Nx+1)
u_1 = np.zeros(Nx+1)
v_1 = np.zeros(Nx+1)
z_1 = np.zeros(Nx+1)
# mesh points in space
# mesh points in time
# Set initial condition u(x,0) = I(x)
for i in range(0, Nx+1):
    u_1[i] = np.random.random_sample()
    v_1[i] = np.random.random_sample()
    z_1[i] = np.random.random_sample()
for n in range(0, Nt):
    # Compute u at inner mesh points
    for i in range(1, Nx):
        u[i] = u_1[i] + dt*(a*(u_1[i]-u0) + 
b*(v_1[i]-v0)+av*(u_1[i]-u0)**3+(Du/dx**2)*(u_1[i-1] - 
2*u_1[i] + u_1[i+1]))*z_1[i]**n
    v[i] = v_1[i] + dt*(c*(u_1[i]-u0)+d*(v_1[i]-v0)+(Dv/dx**2)*(v_1[i-1] - 2*v_1[i] + v_1[i+1]))*z_1[i]**n
    z[i] = (Dz/dx**2)*((z_1[i-1] - 2*z_1[i] + z_1[i+1]) - kz * z[i])
    # Insert boundary conditions u[0]=0; u[Nx]=0
    u[0]=0; u[Nx]=1/Dz
    v[0]=0; v[Nx]=1
    z[0]=0; z[Nx]=1
    # Update u_1 before next step
    u_1[:]= u
    v_1[:]= v
    z_1[:]= z

我遇到的第一个问题是不同的警告:

/miniconda3/lib/python3.6/site-packages/ipykernel_launcher.py:31: RuntimeWarning: overflow encountered in double_scalars
   /miniconda3/lib/python3.6/site-packages/ipykernel_launcher.py:31: RuntimeWarning: invalid value encountered in double_scalars
    /miniconda3/lib/python3.6/site-packages/ipykernel_launcher.py:32: RuntimeWarning: invalid value encountered in double_scalars
    /miniconda3/lib/python3.6/site-packages/ipykernel_launcher.py:32: RuntimeWarning: overflow encountered in double_scalars
    /miniconda3/lib/python3.6/site-packages/ipykernel_launcher.py:33: RuntimeWarning: overflow encountered in double_scalars
    /miniconda3/lib/python3.6/site-packages/ipykernel_launcher.py:33: RuntimeWarning: invalid value encountered in double_scalars

我的主要问题是:有可能用正在进行的前向欧拉方法尝试解决这个问题吗? 提前谢谢大家!

1 个答案:

答案 0 :(得分:0)

答案是&#34;是&#34;,但您的代码需要更多工作。例如,您需要处理算法的稳定性(以避免它爆炸)。 BC也似乎没有反映你的系统,我认为你正在寻找零通量条件,如果是这样,你没有正确编码。最后,您还可以考虑使用Fipy,它可以让您的生活更轻松。看看https://www.ctcms.nist.gov/fipy/,我在这里写一个基本的例子http://biological-complexity.blogspot.pe/