关于Laplace方程python并行化运算的问题

问题遇到的现象和发生背景

我在使用python进行laplace方程运算的练习,但是计算时间很长,虽然能出结果但是会报错。
同时我想要缩短计算时间,感觉并行化计算可以达成,但是查阅资料之后也毫无头绪。

问题相关代码,请勿粘贴截图
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import math

LIMIT = 1000 
N = 101 
M = 101 


def iteration(u):
    u_next = [[0 for i in range(N)] for j in range(M)]
    for i in range(1, N - 1):
        for j in range(1, M - 1):
              u_next[i][j] = (u[i][j - 1] + u[i -1][j] + u[i + 1][j]+ u[i][j + 1]) / 4

    
    for i in range(1, N - 1):
          for j in range(1, M - 1):
                u[i][j] = u_next[i][j]


u = [[0 for i in range(N)] for j in range(M)] 
for i in range(M):
    u[0][i] = math.sin(2 * math.pi * i / (M - 1))
#    u[M-1][i] = math.cos(2 * math.pi * i / (M - 1))

for i in range(LIMIT):
    iteration(u)
    
print(u)


x = np.arange(0, N)
y = np.arange(0, M)
X, Y = np.meshgrid(x, y)
fig = plt.figure()
ax = Axes3D(fig)
U = np.array(u)
ax.plot_surface(X, Y, U, cmap = cm.coolwarm)
plt.show() 


运行结果及报错内容
ax = Axes3D(fig)
MatplotlibDeprecationWarning: Axes3D(fig) adding itself to the figure is deprecated since 3.4. Pass the keyword argument auto_add_to_figure=False and use fig.add_axes(ax) to suppress this warning. The default value of auto_add_to_figure will change to False in mpl3.5 and True values will no longer work in 3.6.  This is consistent with other Axes classes.

img

我的解答思路和尝试过的方法

我尝试加入
ax = Axes3D(fig,auto_add_to_figure=False)
fig.add_axes(ax)
代码也无济于事

我想要达到的结果

一是我想要解决这个报错。
二是我想咨询下我该如何进行并行化计算

  • 你发的报错其实是警告,说Axes3D这个函数会弃用,但是不会影响画图结果
  • 你这个是多重for循环,可以用python的numba库进行即时编译加速

1.你的报错实际上不影响运行
2.可以使用threading线程加速

有GPU环境的情况下,建议使用并行计算