我在使用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.
我尝试加入
ax = Axes3D(fig,auto_add_to_figure=False)
fig.add_axes(ax)
代码也无济于事
一是我想要解决这个报错。
二是我想咨询下我该如何进行并行化计算
1.你的报错实际上不影响运行
2.可以使用threading线程加速
有GPU环境的情况下,建议使用并行计算