使用scipy.weave和GSL ODE

2024-10-03 17:19:05 发布

您现在位置:Python中文网/ 问答频道 /正文

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

使用标准gslodeexample我有以下基于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或短迭代。在


Tags: returntimemaindriverparamsmatrixintdouble