Spyder/Jupyter内核崩溃重启、Atom代码无图像输出问题求助
Kernel Crash in Spyder/Jupyter & Missing Plot in Atom for Python Heat Flow Simulation
我原本运行正常的代码,再次运行时在Atom中代码执行完成但图像无法弹出。尝试在Spyder和Jupyter中运行时,两者均出现"kernel died, restarting"错误提示。我试过其他问题提到的解决方法但都无效,还更新了Anaconda Navigator。我电脑操作经验不多,不确定下一步该怎么做,当前使用Python 3.6。非常感谢您的帮助。
编辑:补充代码
编辑2:问题已解决,是我的代码存在错误(当然不是Python的问题 :D)
原代码
#CIE4365-16 #Assignment 1 - Heat flow import numpy as np import matplotlib.pyplot as plt import scipy.integrate as spin from collections import namedtuple #grid/discretization def topTbound(t, bPar): '''Temperature for top boundary, where t is time''' boundT = bPar.avgT - bPar.rangeT * np.cos(2 * np.pi * (t - bPar.tmin) / 365.25) return boundT def heatflux(t, T, sPar, mDim, bPar): boundT = topTbound(t, bPar) #temperature for top boundary funtion locT = T.copy() #copies T, doesn't change T in the original, can now change it here locT[mDim.nin-2, 0] = boundT #setting the boundary Temperature q = np.zeros((mDim.nin, 1)) # ,1 to make it a column for i in range(1, mDim.nin-1): q[i, 0] = - sPar.lambdaIN[i, 0] * (locT[i, 0] - locT[i - 1, 0]) / mDim.dzn[i - 1, 0] # Robin boundary at the top q[mDim.nin - 1, 0] = - bPar.kRobinTop * (boundT - T[mDim.nin - 2, 0]) #q[nin - 1, 0] = q[nin-2, 0] #setting the top q[0, 0] = 0 #to show that the bottom layer has no flow, so q must be zero return q #divergence of heatflux through nodes def divheatflux(t, T, sPar, mDim, bPar): locT = T.copy().reshape(nn, 1) qH = heatflux(locT, t, sPar, mDim, bPar) #heatflux equation defined earlier divqH = np.zeros((mDim.nn, 1)) for i in range(0, mDim.nn): divqH[i, 0] = -(qH[i + 1, 0] - qH[i, 0]) / (mDim.dzin[i, 0] * sPar.zetaBN[i, 0]) divqHRet = divqH.reshape(mDim.nn) return divqHRet #reshape to columns so it is a vertical profile depth = -15 dz = 0.1 #distance between internode layers nin = 151 #number of internodes nn = nin - 1 #number of nodes is always one less that number of internodes #zin is the internodes boundaries depth location zin = np.arange(depth, 0 + dz, dz).reshape(nin, 1) #nr, nc = np.shape(zin) #nr (number of rows) is the number of internodes #number of nodes, will be filled by the for loop zn = np.zeros([nin-1, 1]) for i in range(0, len(zn)): zn[i] = (zin[i, 0] + zin[i+1, 0]) / 2 zn[0, 0] = zin[0, 0] #this is to change the first value in the array from 0 to -10 zn[len(zn)-1] = zin[len(zin)-1] #this is to change the last value in the array from -0.25 to 0 #dzn will be the distance between the node dzn = np.zeros([len(zn)-1, 1]) for i in range(0, len(zn)-1): dzn[i, 0]=zn[i, 0]-zn[i+1, 0] #dzn = np.reshape(dzn, ((nin - 2), 1)) #dzin will be the distance between the internodes dzin = np.zeros(len(zin)-1) for i in range(0, len(dzin)): dzin[i]=zin[i]-zin[i+1] dzin = np.reshape(dzin, ((nin-1), 1)) #use namedtuple to put dimensions in same place modDim = namedtuple('ModDim',['zn', 'zin', 'dzn', 'dzin', 'nn' , 'nin']) mDim = modDim(zn=zn,zin=zin,dzn=dzn,dzin=dzin,nn=nn,nin=nin) #Properties of the soil zetasolid = 2.235e6 # [J m^-3 K^-1] volumetric heat capacity of solid zetawater = 4.154e6 # [J m^-3 K^-1] volumetric heat capacity of water qu = 0.75 # quartz content sw = 0 #sw is the water saturation rhowater = 1000 # [kg/m^3] density of water rhosolid = 2650 # [kg/m^3] density of solid rhobulk = 1700 # [kg/m^3] dry bulk density of soil p = (1 - rhobulk) / rhosolid # [-] porosity of soil = saturated water content. lambdaQuartz = 6 # [W/(mK)] thermal conductivity of quartz #combine soil parameters in one place soilPar = namedtuple('soilPar',['zetaBN','lambdaIN']) sPar = soilPar(zetaBN = np.ones(np.shape(zn)) * ((1 - p) * zetasolid + p * zetawater * sw), lambdaIN = np.ones(np.shape(zin)) * lambdaQuartz * (24 * 3600)) # boundary parameters boundPar = namedtuple('boundPar',['avgT', 'rangeT', 'tmin', 'kRobinTop', 'kRobinBot']) bPar = boundPar(avgT = 283, rangeT = 15, tmin = 46, kRobinTop = 1, kRobinBot = 0) # Initial Conditions Tini = np.ones(np.shape(zn)) * (283) # degrees K tOut = np.linspace(0, 365.25 * 10, 365) # time, *10 = 10 years, steps per day nOut = np.shape(tOut)[0] T0 = Tini.copy().squeeze() t_span = [tOut[0], tOut[-1]] #TODE = spin.solve_ivp(divheatflux, t_span, T0, sPar, mDim, bPar, # method='RK45', t_eval=tOut, vectorized=True, rtol=1e-5) TODE = spin.odeint(divheatflux, tOut, T0, args = (sPar,mDim,bPar)) for i in range(0, nOut-1): TODE[i, nn - 1] = topTbound(tOut[i], bPar) #plotting plt.close('all') #closing the previous plots fig1, ax1 = plt.subplots(figsize=(9,4)) ax1.plot(tOut,TODE[:, 0:nn-1:20], '.-') ax1.set_title('Temperature') fig2, ax2 = plt.subplots(figsize=(5,8)) for i in np.arange(0, nOut-1, 10): ax2.plot(TODE[i, :], zn.squeeze(),'-') ax2.set_title('Temperature vs. depth') plt.show()
问题根源说明
排查代码后发现,divheatflux函数调用heatflux时参数顺序搞反了:
# 错误调用:把温度数组locT放在了时间参数t的位置 qH = heatflux(locT, t, sPar, mDim, bPar)
而heatflux的定义明确要求第一个参数是时间t,第二个是温度数组T:
def heatflux(t, T, sPar, mDim, bPar):
这种参数不匹配会导致后续数值计算出现异常,直接引发Spyder/Jupyter内核崩溃,Atom中虽然代码执行完成,但后续绘图逻辑也受影响无法正常弹出图像。修正参数顺序后,代码就能正常运行并生成预期的温度曲线了。
内容的提问来源于stack exchange,提问作者J.S.
相关产品推荐
相关产品推荐

