Using GNU Scientific Library with root

Hi,

I’m trying to use the GSL inside a root code (specifically to use the ODE solvers), but I’m running into trouble. I took one of the examples from the GSL documentation.

[code] #include <stdio.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_odeiv2.h>

 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;
 }

 int
 gsl_ode_eg1 (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 t = 0.0, t1 = 100.0;
   double y[2] = { 1.0, 0.0 };

   TGraph* g0 = new TGraph(100);
   TGraph* g1 = new TGraph(100);

   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;
    }

       printf ("%.5e %.5e %.5e\n", t, y[0], y[1]);
       g0->SetPoint(i-1, t, y[0]);
       g1->SetPoint(i-1, t, y[1]);
     }

     TCanavas* c = new TCanvas("y", "y");
     g0->Draw("*");
     g1->Draw("*SAME");

   gsl_odeiv2_driver_free (d);
   return 0;
 }

[/code]

I need to load the libraries, so I made a rootlogon.C

void rootlogon(void) { printf("loading GSL libraries...\n"); gSystem->Load("/usr/lib/libgsl.so"); gSystem->Load("/usr/lib/libgslcblas.so"); gSystem->SetIncludePath("-I/usr/include"); gROOT->ProcessLine(".L /usr/include/gsl/gsl_math.h+"); gROOT->ProcessLine(".L /usr/include/gsl/gsl_errno.h+"); gROOT->ProcessLine(".L /usr/include/gsl/gsl_matrix.h+"); gROOT->ProcessLine(".L /usr/include/gsl/gsl_odeiv2.h+"); }

However, I get the following when I try and run it


ebutler@alphaeoinxps:~/ccm/2012_12/gsl_ode$ root .x gsl_ode_eg1.cpp 
Warning in <TApplication::GetOptions>: macro .x not found
  *******************************************
  *                                         *
  *        W E L C O M E  to  R O O T       *
  *                                         *
  *   Version   5.28/00  14 December 2010   *
  *                                         *
  *  You are welcome to visit our Web site  *
  *          http://root.cern.ch            *
  *                                         *
  *******************************************

ROOT 5.28/00 (trunk@37585, Dec 14 2010, 15:20:27 on linux)

CINT/ROOT C/C++ Interpreter version 5.18.00, July 2, 2010
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
loading GSL libraries...
Warning in <ACLiC>: /usr/include/gsl is not writeable!
Warning in <ACLiC>: Output will be written to /tmp/ebutler
Warning in <ACLiC>: /usr/include/gsl is not writeable!
Warning in <ACLiC>: Output will be written to /tmp/ebutler
Warning in <ACLiC>: /usr/include/gsl is not writeable!
Warning in <ACLiC>: Output will be written to /tmp/ebutler
Warning in <ACLiC>: /usr/include/gsl is not writeable!
Warning in <ACLiC>: Output will be written to /tmp/ebutler
root [0] 
Processing gsl_ode_eg1.cpp...
Error: Missing one of '
\/' expected at or after line 30.
Error: Unexpected end of file (G__fgetstream():2) /usr/include/gsl/gsl_errno.h:155:
Error: extern"C"{__BEGIN_DECLSenum{GSL_SUCCESS=0,GSL_FAILURE=-1,GSL_CONTINUE=-2,GSL_EDOM=1,GSL_ERANGE=2,GSL_EFAULT=3,GSL_EINVAL=4,GSL_EFAILED=5,GSL_EFACTOR=6,GSL_ESANITY=7,GSL_ENOMEM=8,GSL_EBADFUNC=9,GSL_ERUNAWAY=10,GSL_EMAXITER=11,GSL_EZERODIV=12,GSL_EBADTOL=13,GSL_ETOL=14,GSL_EUNDRFLW=15,GSL_EOVRFLW=16,GSL_ELOSS=17,GSL_EROUND=18,GSL_EBADLEN=19,GSL_ENOTSQR=20,GSL_ESING=21,GSL_EDIVERGE=22,GSL_EUNSUP=23,GSL_EUNIMPL=24,GSL_ECACHE=25,GSL_ETABLE=26,GSL_ENOPROG=27,GSL_ENOPROGJ=28,GSL_ETOLF=29,GSL_ETOLX=30,GSL_ETOLG=31,GSL_EOF=32}voidgsl_error(constchar*reason,constchar*file,intline,intgsl_errno)voidgsl_stream_printf(constchar*label,constchar*file,intline,constchar*reason)constchar*gsl_strerror(constintgsl_errno)typedefvoidgsl_error_handler_t(constchar*reason,constchar*file,intline,intgsl_errno)typedefvoidgsl_stream_handler_t(constchar*label,constchar*file,intline,constchar*reason)gsl_error_handler_t*gsl_set_error_handler(gsl_error_handler_t*new_handler)gsl_error_handler_t*gsl_set_error_handler_off(void)gsl_stream_handler_t*gsl_set_stream_handler(gsl_stream_handler_t*new_handler)FILE*gsl_set_stream(FILE*new_stream)__END_DECLS  Syntax error? /usr/include/gsl/gsl_errno.h:155:
Error: voidgsl_stream_printf(constchar*label,constchar*file,intline,constchar*reason)constchar  Syntax error? /usr/include/gsl/gsl_errno.h:155:
Error: gsl_strerror(constintgsl_errno)typedefvoidgsl_error_handler_t(constchar*reason,constchar*file,intline,intgsl_errno)typedefvoidgsl_stream_handler_t(constchar*label,constchar*file,intline,constchar*reason)gsl_error_handler_t  Syntax error? /usr/include/gsl/gsl_errno.h:155:
Error: typedefvoidgsl_error_handler_t(constchar*reason,constchar*file,intline,intgsl_errno)typedefvoidgsl_stream_handler_t(constchar*label,constchar*file,intline,constchar*reason)gsl_error_handler_t  Syntax error? /usr/include/gsl/gsl_errno.h:155:
Error: typedefvoidgsl_stream_handler_t(constchar*label,constchar*file,intline,constchar*reason)gsl_error_handler_t  Syntax error? /usr/include/gsl/gsl_errno.h:155:
Error: gsl_set_error_handler(gsl_error_handler_t*new_handler)gsl_error_handler_t  Syntax error? /usr/include/gsl/gsl_errno.h:155:
Error: gsl_set_error_handler_off(void)gsl_stream_handler_t  Syntax error? /usr/include/gsl/gsl_errno.h:155:
Error: gsl_set_stream_handler(gsl_stream_handler_t*new_handler)FILE  Syntax error? /usr/include/gsl/gsl_errno.h:155:
*** Interpreter error recovered ***

Anyone ever done something like this before and can help?

Playing around, if I comment out the #include <gsl/…> directives at the start of the .cpp file, I get the errors

Error: Illegal pointer operation (letvalue) gsl_ode_eg1.cpp:36:
Error: Illegal pointer operation (letvalue) gsl_ode_eg1.cpp:36:

Hi,

I think you don’t need to add these in the root_logon

    gROOT->ProcessLine(".L /usr/include/gsl/gsl_math.h+");
    gROOT->ProcessLine(".L /usr/include/gsl/gsl_errno.h+");
    gROOT->ProcessLine(".L /usr/include/gsl/gsl_matrix.h+");
    gROOT->ProcessLine(".L /usr/include/gsl/gsl_odeiv2.h+");

But you need to compile with ACLIC the example

Lorenzo

Yes, it works! Thanks!