首页 文章

Scipy:腌制颂歌实例

提问于
浏览
1

我正在进行长时间的模拟,其中涉及一个颂歌的整合 . 由于运行时间长,我决定生成回退,允许从检查点继续进行模拟 . 对于ode集成,我决定挑选scipy ode solver实例 . 此方法适用于可重入求解器'dopri5'和'dop853',但'vode'和'lsoda'失败 . 对于我的特殊问题,'vode'算法似乎是最快的,所以我感兴趣,是否可以绕过这个问题 . 我希望这是可能的,因为在重启时也只需要一个求解器实例 .

运行以下两个代码片段演示了我的问题,请查看此gist

initialize.py

开始集成并通过酸洗(使用莳萝,也可以腌制lambda)来持久化ode求解器实例 .

import scipy.integrate
import dill

# Solve a simple ode up to some time t
rhs= lambda t, y: -y
ode_solver = scipy.integrate.ode(rhs).set_integrator('vode')
ode_solver.set_initial_value(1)
ode_solver.integrate(1)
print ode_solver.t, ode_solver.y

## Stop solving and persist ode solver to a file
file_name='ode_instance.pkl'
with open(file_name, "w") as ode_file:
    dill.dump(ode_solver, ode_file)

continue.py

尝试通过加载ode解算器继续上述集成 .

import scipy.integrate
import dill

## Restart the ode solver
file_name='ode_instance.pkl'
with open(file_name, "r") as ode_file:
    ode_solver = dill.load(ode_file)

ode_solver.integrate(2)
print ode_solver.t, ode_solver.y

抛出

Traceback (most recent call last):
  File "continue.py", line 9, in <module>
    ode_solver.integrate(2)
  File "~/Applications/miniconda2/envs/cooperativity/lib/python2.7/site-packages/scipy/integrate/_ode.py", line 408, in integrate
    self.f_params, self.jac_params)
  File "~/Applications/miniconda2/envs/cooperativity/lib/python2.7/site-packages/scipy/integrate/_ode.py", line 852, in run
    self.check_handle()
  File "~/Applications/miniconda2/envs/cooperativity/lib/python2.7/site-packages/scipy/integrate/_ode.py", line 651, in check_handle
    raise IntegratorConcurrencyError(self.__class__.__name__)
scipy.integrate._ode.IntegratorConcurrencyError: Integrator `vode` can be used to solve only a single problem at a time. If you want to integrate multiple problems, consider using a different integrator (see `ode.set_integrator`)

1 回答

  • 0

    SciPy的 ode 使用需要在Python“外部”初始化的编译集成器,其状态(在集成步骤之外)不会完全反映在Python端存储的内容 . 因此,当您对求解器实例进行pickle和unpickle时,编译的求解器实例的初始化和内部状态将丢失 . 在 dopri5dop853 的情况下,似乎没有任何东西要存储(这是有道理的,因为这些本质上是简单的Runge-Kutta求解器),但这不适用于 vodelsoda . 因此你的问题 .

    可能对您有用的是存储解析器的内部状态,只要Python可以访问(特别是步长) .

相关问题