使用scipy.weave和GSL ODE

时间:2013-05-03 12:39:33

标签: python c scipy gsl

在之前的question之后我试图使用GSL解决一些ODE。复杂的因素是我希望它用python编写,所以我试图让scipy.weave来帮助。

使用标准GSL ODE example我有以下内容基于this示例

import scipy.weave as weave

time =0.0
y1 = [0.0,0.0]
# Define the function 

support_code=r'''

 int
 func (double t, const double y[], double f[],
       void *params)
 {
   double mu = *(double *)params;
   f[0] = y[1];
   f[1] = -y[0] - mu*y[1]*(y[0]*y[0] - 1);
   return GSL_SUCCESS;
 }

 int
 jac (double t, const double y[], double *dfdy, 
      double dfdt[], void *params)
 {
   double mu = *(double *)params;
   gsl_matrix_view dfdy_mat 
     = gsl_matrix_view_array (dfdy, 2, 2);
   gsl_matrix * m = &dfdy_mat.matrix; 
   gsl_matrix_set (m, 0, 0, 0.0);
   gsl_matrix_set (m, 0, 1, 1.0);
   gsl_matrix_set (m, 1, 0, -2.0*mu*y[0]*y[1] - 1.0);
   gsl_matrix_set (m, 1, 1, -mu*(y[0]*y[0] - 1.0));
   dfdt[0] = 0.0;
   dfdt[1] = 0.0;
   return GSL_SUCCESS;
 }
'''


# Define the main

c_main = r'''
 int
 main (void)
 {
   double mu = 10;
   gsl_odeiv2_system sys = {func, jac, 2, &mu};

   gsl_odeiv2_driver * d = 
     gsl_odeiv2_driver_alloc_y_new (&sys, gsl_odeiv2_step_rk8pd,
                  1e-6, 1e-6, 0.0);
   int i;
   double t1 = 100.0;


   for (i = 1; i <= 100; i++)
     {
       double ti = i * t1 / 100.0;
       int status = gsl_odeiv2_driver_apply (d, &t, ti, y);

       if (status != GSL_SUCCESS)
    {
      printf ("error, return value=%d\n", status);
      break;
    }

       time = t;
     }

   gsl_odeiv2_driver_free (d);
   return 0;
 }
'''


compiler = 'gcc'
vars = ['time','y']
libs = ['gsl','gslcblas','m']
headers = ['<stdio.h>','<gsl/gsl_errno.h>','<gsl/gsl_odeiv2.h>','<gsl/gsl_matrix.h>']


res = weave.inline(c_main, vars, compiler=compiler,
                   libraries=libs, headers=headers,
                   support_code = support_code)

print time

返回this horrible spew。我真的不知道我在做什么,但知道这应该相当容易!问题是网上编织的所有例子都是基于printf或短迭代。

0 个答案:

没有答案