这部分对求解器进行了修改。
step
方法上 def step(self):
# this method represents the whole process of a major step
# it returns a generator, which yields at each minor step
t = self.t
h = self.step_size
y_init = self.state_array
K = np.empty((self.stage_num + 1, self.state_num), dtype='float')
K[0] = self.deriv_fun(t, y_init, 0)
yield
for s, (a, c) in enumerate(zip(self.A[1:], self.C[1:]), start=1):
# x = c
dy = np.dot(K[:s].T, a[:s]) * h
self.output_fun(t + c * h, y_init + dy, c * h)
yield
K[s] = self.deriv_fun(t + c * h, y_init + dy, c * h)
yield
y_new = y_init + h * np.dot(K[:-1].T, self.B)
# TODO: calculate f_new for error check
self.state_array = y_new
self.t_prev = t
self.t = t + h
step()
方法。所以step函数的时间开销比较大。_dummy_generator
方法。 def _dummy_generator(self):
# for systems with no need to solve
# make sure the generator works well
# TODO: get stage num from engine
for i in range(2 * self.solver.stage_num - 1):
yield
yield的次数保证和连续模块同步,但是在rzip模型的测试中结果偏差较大,也就是小周期不更新带来的偏差。step
,离散模块调用dummy_step
来替代。 def dummy_step(self):
# this method will be called by systems without continuous state variables
t = self.t
h = self.step_size
yield
for c in self.C[1:]:
self.output_fun(t + c * h, self.state_array, c * h)
yield
yield
self.t = t + h
本文章使用limfx的vscode插件快速发布