c++ - 为什么 GSL 在没有尺寸时会给出尺寸不匹配的错误
问题描述
我正在包装 GSL 的 ODE 函数是我正在定义的一个类。在不涉及类的情况下编写时,这些功能可以完美运行。但是,当使用该类时,我在第二个之后得到一个错误gsl_odeiv2_evolve_apply_fixed_step
,得到错误gsl: evolve.c:317: ERROR: step dimension must match evolution size
。我很困惑为什么第一步应该有效,而第二步不应该。在打印出step
和evolve
函数的维度时,我得到了同样的结果。这是我的课程以及我在main()
函数中调用的内容。
class DynSys {
public:
DynSys(const size_t size, double startTime, double endTime, double stepSize,
double* iState,
int (*func)(double, const double*, double*, void*),
int (*jac)(double, const double*, double*, double*, void*),
const gsl_odeiv2_step_type* T = gsl_odeiv2_step_rk8pd)
: T(T), size(size), t(startTime), t1(endTime), h(stepSize) {
y = new double[size];
y = iState;
yPrev = new double[size];
s = gsl_odeiv2_step_alloc(T, size);
c = gsl_odeiv2_control_y_new(1e-6, 0.0);
e = gsl_odeiv2_evolve_alloc(size);
sys = { func, jac, size, 0 };
}
~DynSys() {
delete [] y;
delete [] yPrev;
gsl_odeiv2_evolve_free(e);
gsl_odeiv2_control_free(c);
gsl_odeiv2_step_free(s);
}
void step() {
printf("e dim: %ld\n", e->dimension);
printf("s dim: %ld\n", s->dimension);
printf("y: %.5f %.5f %.5f %.5f %.5f %.5f\n", y[0], y[1],
y[2], y[3], y[4], y[5]);
tPrev = t;
yPrev = std::copy(y, y+size, yPrev);
int status = gsl_odeiv2_evolve_apply_fixed_step(e, c, s, &sys,
&t, h, y);
if (status != GSL_SUCCESS) {
printf("Error: %s\n", gsl_strerror(status));
throw std::logic_error(gsl_strerror(status));
}
}
double getT() {
return t;
}
void setY(double* y) {
y = y;
}
private:
const gsl_odeiv2_step_type* T;
gsl_odeiv2_step* s;
gsl_odeiv2_control* c;
gsl_odeiv2_evolve* e;
gsl_odeiv2_system sys;
const size_t size;
double t;
double t1;
double h;
double* y;
double tPrev;
double* yPrev;
};
接着
int main() {
double state[] = { 1.0, 0.0, 0.0, 0.796975, 0.11637, 0.0185312};
const size_t size = 6;
DynSys system(size, 0.0, 40.0, 1e-3, state, func, jac);
system.step();
printf("t: %.5f\n", system.getT());
system.step();
printf("t: %.5f\n", system.getT());
return 0;
}
编辑:这是 GSL 函数GSL ODE的链接
解决方案
推荐阅读
- java - 通过 API Gateway 运行 Java Lambda
- python - 网页抓取新闻文章
- pandas - Pandas,将 MultiIndex 数据框的某些列转换为行
- go - 如何在 delve mozilla rr(record-replay)重播会话期间显示十进制的浮点值。十进制
- laravel - 在一个月中按周分组数据并获取开始和结束日期
- python-3.x - 为最后一条记录打印不同的文本
- .net-core - Mock.Verify 无法识别对 IServiceProvider 提供的模拟服务的调用
- c - 如何正确对齐数字?
- c - 在 C 中的同一终端窗口上分别使用 2 个线程处理 I/O
- python - Matplotlib:imshow 图像的共享轴