为了解决一些微分值问题,我编写了一个类和一个步进器来计算二阶Runge-Kutta方法。我的问题是,如果我从一个类中调用此函数,则会出现错误,如果我由另一个步进器调用,则不会!
我在这里报告了很短的代码: 这是类方法stepper:
@classmethod
def step(cls,func , t : np.float , u : np.float , dt ):
def f(ti,ui):
return np.array([function(ti,ui) for function in func])
k1 = f(t,u)
k2 = f(t+dt/2. , u + dt/2.*k1)
unext = u + dt*k2
return unext
如果我使用它来解决微分问题,它将起作用……就像这种情况:
with open('RungeKutta2_p1.dat', 'w') as f:
while True:
f.write('%10.4f %10.4f \n' %(t, u[0]) )
u = rungekutta.RK2.step(func0,t,u,dt)
t += dt
if t >= tf:
break
如果我从另一个步进器中调用它:
t,u = t0,u0
with open('AdamsBashforth2_p1.dat', 'w') as f:
while True:
f.write('%10.4f %10.4f \n' %(t, u[0]) )
u = adamsmethods.AdamsBashforth._2nd.step(func0,t,u,dt)
t += dt
if t >= tf:
break
其中定义了adamsmethods.AdamsBashforth._2nd.step
的地方:
@classmethod
def step(cls, func , t : np.float , u : np.float, dt ):
def f(ti,ui):
return np.array([function(ti,ui) for function in func])
if cls.startup == False:
#print ("AB start-up")
AdamsBashforth._2nd.u1 = rungekutta.RK2.step(func,t,u,dt)
AdamsBashforth._2nd.startup = True
unext = AdamsBashforth._2nd.u1 + dt/2.*3.*f(t+dt,AdamsBashforth._2nd.u1) - f(t,u))
AdamsBashforth._2nd.u1 = u
问题是当我尝试调用它以启动方法时: 考虑这个课程:
class AdamsBashforth(AdamsMethods):
def __init__(self, dydt, filename :str = None , save : bool = True):
self._2nd = AdamsBashforth._2nd(dydt,filename,save)
def solve(self):
pass
class _2nd(AdamsMethods):
startup = False
solved = False
def __init__(self, dydt : Rhs, filename :str = None , save : bool = True ):
self.file = filename
self.save = save
super().__init__(dydt)
def solve(self):
self.time , self.u = self.dydt.createArray()
self.u[1] = rungekutta.RK2.step(self.dydt.f, self.time[0], self.u[0], self.dt)
for i in range(1,len(self.time)-1):
self.u[i+1] = u[i] + self.dt/2*(3*self.dydt.f(self.time[i],self.u[i])-self.dydt.f(self.time[i-1],self.u[i-1]))
_2nd.solved = True
if self.file != None:
super().write2file()
if self.save:
return self.time,self.u
此处失败并显示此错误:
Traceback (most recent call last):
File "drive.py", line 287, in <module>
main()
File "drive.py", line 225, in main
ab2t,ab2u = ab2_p1._2nd.solve()
File "/home/marco/Programming/Python/Numeric/OdeSystem/adamsmethods.py", line 46, in solve
self.u[1] = rungekutta.RK2.step(self.dydt.f, self.time[0], self.u[0], self.dt)
File "/home/marco/Programming/Python/Numeric/OdeSystem/rungekutta.py", line 100, in step
k1 = f(t,u)
File "/home/marco/Programming/Python/Numeric/OdeSystem/rungekutta.py", line 98, in f
return np.array([function(ti,ui) for function in func])
TypeError: 'method' object is not iterable
------------------------------------------------------------
如果问题没有得到很好的解释,请告诉我,我给您所有进一步的信息!
编辑 你是对的 !!这是一个方法而不是np.array ...我该如何解决?我在通话之前尝试写这个:
def fn(ti,ui):
return np.array([function(ti,ui) for function in self.dydt.f])
但我收到此消息:
Traceback (most recent call last):
File "drive.py", line 287, in <module>
main()
File "drive.py", line 225, in main
ab2t,ab2u = ab2_p1._2nd.solve()
File "/home/marco/Programming/Python/Numeric/OdeSystem/adamsmethods.py", line 51, in solve
print(type(fn(self.time[0],self.u[0])))
File "/home/marco/Programming/Python/Numeric/OdeSystem/adamsmethods.py", line 50, in fn
return np.array([function(ti,ui) for function in self.dydt.f])
TypeError: 'method' object is not iterable
------------------------------------------------------------