0

在上一个问题之后,我正在尝试使用 GSL 解决一些 ODE。复杂的因素是我希望它用 python 编写,所以我试图让scipy.weave提供帮助。

使用标准 GSL ODE示例,我有以下基于示例的

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

这会返回这个可怕的喷射。我真的不知道我在做什么,但知道这应该相当容易!问题是 web 上的所有 weave 示例都是基于 printf 或短迭代。

4

0 回答 0