gpt4 book ai didi

c++ - std::bad_alloc 和 GSL ODE 求解器

转载 作者:行者123 更新时间:2023-11-28 07:43:23 27 4
gpt4 key购买 nike

我正在尝试使用 GSL 求解器求解一个(大型)ODE 系统。当我使用驱动程序方法时,我收到错误消息无法为 gsl_interp_accel 分配空间,当我手动定义控制、错误和步进器时,我收到 bad_alloc 异常,这是由,据我所知,在另一种情况下,同样的原因导致 无法为 gsl_interp_accel 分配空间 - 内存不足。

我咨询过其他bad_alloc 查询,例如this one ,但我还没有发现任何对我的特殊情况有用的东西。此外,我尝试过其他 ODE 求解器,但它们最终也会出现内存错误。我还使用 valgrind 检查了我的程序,以确保除了求解器之外的任何其他地方都没有内存错误/泄漏。

任何求解器都有“积分限制”,在我的例子中,程序在上限的大约 10% 处工作正常(与下限相比很大 - 我很确定这是我得到的错误来源 - 但是我确实需要在这些特定限制之间进行整合),然后以我在上面引用的异常之一终止。我尝试了各种(固定/自适应)步长,但从未达到我想要的 10% 以上。

出现异常的那段代码是:

gsl_ode_struct inputstruct;  // Struct that contains parameters for ODEs 
gsl_odeiv2_system sys = {func, NULL, 2*G.getsize(), &inputstruct};
const gsl_odeiv2_step_type * T = gsl_odeiv2_step_rk8pd;
gsl_odeiv2_step * stepper = gsl_odeiv2_step_alloc (T, size_of_the_system);
gsl_odeiv2_control * control = gsl_odeiv2_control_standard_new (1.e-6, 0.0, 1., 0.);
gsl_odeiv2_evolve * error = gsl_odeiv2_evolve_alloc (size_of_the_system);
double hparam = 1.e-6; // Initial step size
double t = xspan[0]; // Initial time
while(t < final_time){
// Here the excpection comes
int status = gsl_odeiv2_evolve_apply (error, control, stepper, &sys, &t, final_time, &hparam, array);
if(status != GSL_SUCCESS)
break;
// Do some stuff that includes reading all the intermediate results to a container as I need them later.
}
gsl_odeiv2_evolve_free (error);
gsl_odeiv2_control_free (control);
gsl_odeiv2_step_free (stepper);

因此,如果我将 final_time 更改为 final_time/10 代码会执行,但结果没有任何意义。即使在求解器之后什么也没做,仍然会抛出异常,不过,无法为 gsl_interp_accel 分配空间

我曾尝试将循环拆分为多个(许多)循环,并在其间删除内存,但这并没有太大帮助。

如果这很重要,我使用 Ubuntu 12.10,使用 GNU 编译器和 Intel C++ Composer 编译。还在 Mac 上测试(不知道是哪个版本的操作系统),结果相同。

问题是:有没有办法“欺骗”求解器,使程序正常运行?

P.S.:ODEint 求解器以更智能的方式获取中间结果,但也会抛出异常。

最佳答案

我遇到过类似的问题。程序在特定的 final_time 以 bad_alloc 错误终止。如果我缩短积分时间,程序就会正常终止,但这不是我想要的。然后我将 epsabs 从 1e-9 减少到 1e-6,程序可以正常运行直到我需要的 final_time。

这不是解决方案,而是妥协。

关于c++ - std::bad_alloc 和 GSL ODE 求解器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15388403/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com