這是爲我正在處理的項目。我正在試圖在地殼內的一些主體或鄉村岩石中模擬岩漿冷卻的侵入堤防。我對編碼相當陌生。我盡我所能將這種代碼格式從另一種編碼語言轉換爲python。我對發生了什麼有一個基本的想法。我知道我正在嘗試索引超出範圍的內容,但我不確定在哪裏以及如何解決它。任何幫助,我可以得到將不勝感激!提前致謝。不知道爲什麼我得到這個錯誤,我有一個想法,但不知道如何解決它
import numpy as np
import matplotlib.pyplot as plt
#Physical parameters
L = 100 #Length of modeled domain [m]
Tmagma = 1200 #Temp. magma [°C]
Trock = 300 #Temp. of country rock [°C]
kappa = 1e-6 #Thermal diffusivity of rock [m^2/s]
W = 5 #Width of dike [m]
day = 3600*24 #seconds per day
dt = 1*day #Timestep
print(kappa)
print(day)
print(dt)
#Numerical parameters
nx = 201 #Number of gridpoints in x-direction
nt = 500 #Number of timesteps to compute
dx = L/(nx-1) #Spacing of grid
x = np.linspace(-50,50,100) #Grid
print(dx)
print(x)
#Setup initial temp. profile
T = np.ones(np.shape(x))*Trock
T[x>=-W/2] = 1200
T[x>=W/2] = 300
print(T)
time = 0
for n in range(0,nt): #Timestep loop
#compute new temp.
Tnew = np.zeros((1,nx))
print(Tnew)
for i in range(2,nx-1):
Tnew[i] = T[i]+kappa*dt*((T[i+1]-2*T[i]+T[i-1])/(dt**2)) # Set BC's
Tnew[1] = T[1]
Tnew[nx] = T[nx]
#update temp. and time
T = Tnew
time = time+dt
#Plot solution
plt.figure()
plt.plot(x,Tnew)
plt.xlabel('x [m]')
plt.ylabel('Temerpature [°C]')
plt.legend()
surf = ax.plot_surface(X, Y, cmap=cm.coolwarm, linewidth=0, antialiased=False)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
IndexError Traceback (most recent call last)
<ipython-input-51-e80d6234a5b4> in <module>()
37 print(Tnew)
38 for i in range(2,nx-1):
---> 39 Tnew[i] = T[i]+kappa*dt*((T[i+1]-2*T[i]+T[i-1])/(dt**2))
40
41 # Set BC's
IndexError: index 2 is out of bounds for axis 0 with size