繁体   English   中英

在 python 中求解一组耦合微分方程(使用稀疏数组)时获得的奇怪结果

[英]Weird results obtained while solving a set of coupled differential equations (using a sparse array) in python

在尝试求解耦合微分方程系统并重现附图中显示的结果时,我尝试了一周都无济于事。 我似乎也得到了奇怪的结果,如图所示。 我似乎不知道我可能做错了什么。耦合微分方程组是使用 Newman 的 BAND 求解的。 这是 python 实现的链接: python solution using BAND 如果附件不够清晰,还有另一个指向问题原始图像的链接:在这里你可以找到更清晰的问题图像 现在我要做的是通过使用 sympy 和 numpy 的组合直接从离散化方程创建一个稀疏数组来解决相同的问题,然后使用 scipy 的 spsolve 求解。 下面是我的代码。 我需要一些帮助来弄清楚我做错了什么。 问题的图像,正确的解决方案和我的解决方案在这里 我在我的代码中将变量表示为 c1 = cA、c2 = cB、c3 = cC、c4 = cD。 等式 2 已经线性化,phi10 和 phi20 是变量 cC 和 cD 的试验值。

# import modules
import numpy as np
import sympy
from sympy.core.function import _mexpand
import scipy as sp
import scipy.sparse as ss
import scipy.sparse.linalg as ssl
import matplotlib.pyplot as plt

# define functions
def flatten(t):
    """
    function to flatten lists
    """
    return [item for sublist in t for item in sublist]


def get_coeffs(coeff_dict, func_vars):
    """
    function to extract coefficients from variables
    and form the sparse symbolic array
    """
    c = coeff_dict
    for i in list(c.keys()):
        b, _ = i.as_base_exp()
        if b == i:
            continue
        if b in c:
            c[i] = 0
        if any(k.has(b) for k in c):
            c[i] = 0

    return [coeff_dict[val] for val in func_vars]  

# Constants for the problem
I = 0.1                 # A/cm2
L = 1.0                 # distance (x) in cm
m = 100                 # grid spacing
h = L / (m-1)
a = 23300               # 1/cm
io = 2e-7               # A/cm2
n = 1
F = 96500               # C/mol
R = 8.314               # J/mol-K
T = 298                 # K
sigma = 20              # S/cm
kappa = 0.06            # S/cm
alpha = 0.5
beta = -(1-alpha)*n*F/R/T
phi10 , phi20 = 5, 0.5 # these are just guesses
P = a*io*np.exp(beta*(phi10-phi20))

j = sympy.symbols('j',integer = True)
cA = sympy.IndexedBase('cA')
cB = sympy.IndexedBase('cB')
cC = sympy.IndexedBase('cC')
cD = sympy.IndexedBase('cD')

# write the boundary conditions at x = 0
bc=[cA[1], cB[1],
(4/3)  * cC[2] - (1/3)*cC[3], # use a three point approximation for cC_prime
cD[1]
]

# form a list of expressions from the boundary conditions and equations
expr=flatten([bc,flatten([[
-cA[j-1] - cB[j-1] + cA[j+1] + cB[j+1],
cB[j-1] - 2*h*P*beta*cC[j] + 2*h*P*beta*cD[j] - cB[j+1],
-sigma*cC[j-1] + 2*h*cA[j] + sigma * cC[j+1],
-kappa * cD[j-1] + 2*h * cB[j] + kappa * cD[j+1]] for j in range(2, m)])])

vars = [cA[j], cB[j], cC[j], cD[j]]


# flatten the list of variables
unknowns = flatten([[cA[j], cB[j], cC[j], cD[j]] for j in range(1,m)])
var_len = len(unknowns)

# # # substitute in the boundary conditions at x = L while getting the coefficients
A = sympy.SparseMatrix([get_coeffs(_mexpand(i.subs({cA[m]:I}))\
    .as_coefficients_dict(), unknowns) for i in expr])




# convert to a numpy array
mat_temp = np.array(A).astype(np.float64)

# you can view the sparse array with this
fig = plt.figure(figsize=(6,6))
ax = fig.add_axes([0,0, 1,1])
cmap = plt.cm.binary
plt.spy(mat_temp, cmap = cmap, alpha = 0.8)

def solve_sparse(b0, error):
    # create the b column vector
    b = np.copy(b0)
    b[0:4] = np.array([0.0, I, 0.0, 0.0])
    b[var_len-4] =   I
    b[var_len-3] =   0
    b[var_len-2] =   0
    b[var_len-1] =   0

    print(b.shape)
    
    old = np.copy(b0)

    mat = np.copy(mat_temp)
    b_2 = np.copy(b)
       
    resid = 10
    lss = 0
    while lss < 100:
        mat_2 = np.copy(mat)
  
        
        for j in range(3, var_len - 3, 4):

            # update the forcing term of equation 2
            b_2[j+2] = 2*h*(1-beta*old[j+3]+beta*old[j+4])*a*io*np.exp(beta*(old[j+3]-old[j+4]))
            
            # update the sparse array at every iteration for variables cC and cD in equation2
            mat_2[j+2, j+3] += 2*h*beta*a*io*np.exp(beta*(old[j+3]-old[j+4]))
            mat_2[j+2, j+4] += 2*h*beta*a*io*np.exp(beta*(old[j+3]-old[j+4]))
       
        # form the column sparse matrix 
        A_s = ss.csc_matrix(mat_2)
        new = ssl.spsolve(A_s, b_2).flatten()


        resid = np.sum((new - old)**2)/var_len
 
        lss += 1

        old = np.copy(new)
        
    return new


val0 = np.array([[0.0, 0.0, 0.0, 0.0] for _ in range(m-1)]).flatten() # form an array of initial values
error = 1e-7
## Run the code
conc = solve_sparse(val0, error).reshape(m-1, len(vars))


conc.shape  # gives (99, 4)
# Plot result for cA:
plt.plot(conc[:,0], marker = 'o', linestyle = '')

发生的事情似乎很清楚。 您正在使用中心欧拉方法作为离散化,对于u'=F(u)这读作

u[j+1]-u[j-1] = 2*h*F(u[j])

这种方法仅是弱稳定的,并且允许奇数和偶数索引的子序列相当独立地演化。 作为方程,这意味着该解可能近似系统ue'=F(uo), uo'=F(ue)具有独立函数ue, uo遵循偶数或奇数子序列的路径。

为了避免这种情况,需要非常小心地处理边界点的边界值和方程。


但是使用梯形方法可以避免所有这些不愉快

u[j+1]-u[j] = 0.5*h*(F(u[j+1])+F(u[j]))

这也减少了系统矩阵的带宽。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM