我是这个社区的新手,也是科学编程的新手。我为 1-D Cahn-Hilliard 方程编写了一个简单的 4 阶 Runge-Kutta,用于对图案形成系统进行一些初步的简单计算。事实证明,它对空间网格大小的变化极为敏感。对于 100 个网格元素,时间步长需要小 100 倍才能收敛……这是我使用的代码:
import numpy as np
import matplotlib.pyplot as plt
# Parameters
k_c = 1.
eps = 0.5
L=12. # Domainsize
size = 50 # Gridsize
dx = L/size
T = 100 # total time
dt = .001 # time step
n = int(T/dt)
# Random Initial Conditions
U = np.random.normal(0,0.01,size) # Normalverteilte Zufallswerte um 0
# Laplace-Operator
def laplacian(Z):
Zleft = np.roll(Z,1)
Zright = np.roll(Z,-1)
Zcenter = Z
return (Zright + Zleft -2*Zcenter)/dx**2
def DoubleLaplacian(Z):
Z2left = np.roll(Z,2)
Z2right = np.roll(Z,-2)
Zleft = np.roll(Z,1)
Zright = np.roll(Z,-1)
Zcenter = Z
return (Z2left - 4* Zleft + 6*Zcenter - 4*Zright + Z2right)/dx**4
# Equation Body
def equation(U):
return laplacian(U**3)-2*eps/(k_c**2)*laplacian(U)-eps*(k_c**4)*DoubleLaplacian(U)
# Runge-Kutta-Step
def RK4Step(U, dt, equation):
k1 = dt*equation(U)
U_temp = U + k1/2; k2 = dt*equation(U_temp);
U_temp = U + k2/2; k3 = dt*equation(U_temp);
U_temp = U + k3; k4 = dt*equation(U_temp);
U += (k1 + 2*k2 + 2*k3 + k4)/6
# Integration
for i in range(n):
RK4Step(U, dt, equation)
由于我对这类东西不熟悉,所以我想问一下我的思维或编码是否存在概念错误,或者仅仅是由于非线性。我也很乐意得到一些关于如何提高代码性能的提示。