首页 > 解决方案 > 为什么 GSL 在没有尺寸时会给出尺寸不匹配的错误

问题描述

我正在包装 GSL 的 ODE 函数是我正在定义的一个类。在不涉及类的情况下编写时,这些功能可以完美运行。但是,当使用该类时,我在第二个之后得到一个错误gsl_odeiv2_evolve_apply_fixed_step,得到错误gsl: evolve.c:317: ERROR: step dimension must match evolution size。我很困惑为什么第一步应该有效,而第二步不应该。在打印出stepevolve函数的维度时,我得到了同样的结果。这是我的课程以及我在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的链接

标签: c++gsl

解决方案


推荐阅读