Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16248 for branches


Ignore:
Timestamp:
10/22/18 10:02:42 (6 years ago)
Author:
gkronber
Message:

#2925 extracted CVODES external methods into a separate class

Location:
branches/2925_AutoDiffForDynamicalModels
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2925_AutoDiffForDynamicalModels/AutoDiffForDynamicalModelsTest/TestCvodes.cs

    r16246 r16248  
    1010      // test vectors
    1111      var arr = new double[] { 3.14, 2.71 };
    12       var vec = Problem.N_VMake_Serial(2, arr);
    13       Problem.N_VDestroy_Serial(vec);
    14 
    15       vec = Problem.N_VNew_Serial(10);
     12      var vec = CVODES.N_VMake_Serial(2, arr);
     13      CVODES.N_VDestroy_Serial(vec);
     14
     15      vec = CVODES.N_VNew_Serial(10);
    1616
    1717      unsafe {
     
    2424        double data0 = *data;
    2525      }
    26       Problem.N_VConst_Serial(2.0, vec);
    27       Problem.N_VPrint_Serial(vec);
    28       Assert.AreEqual(20, Problem.N_VL1Norm_Serial(vec));
    29       Problem.N_VDestroy_Serial(vec);
     26      CVODES.N_VConst_Serial(2.0, vec);
     27      CVODES.N_VPrint_Serial(vec);
     28      Assert.AreEqual(20, CVODES.N_VL1Norm_Serial(vec));
     29      CVODES.N_VDestroy_Serial(vec);
    3030
    3131
    3232      // linear oscillator
    3333      int numberOfEquations = 2;
    34       var y = Problem.N_VNew_Serial(numberOfEquations);
     34      var y = CVODES.N_VNew_Serial(numberOfEquations);
    3535      // y must be initialized before calling CVodeInit
    36       // Problem.N_VConst_Serial(100.0, y);
    37       Problem.NV_Set_Ith_S(y, 0, 0.5); // x
    38       Problem.NV_Set_Ith_S(y, 1, 1);  // v
    39 
    40       var cvode_mem = Problem.CVodeCreate(Problem.MultistepMethod.CV_ADAMS, Problem.NonlinearSolverIteration.CV_FUNCTIONAL);
    41 
    42       var flag = Problem.CVodeInit(cvode_mem, F, 0.0, y);
    43       Assert.AreEqual(Problem.CV_SUCCESS, flag);
    44 
    45       flag = Problem.CVodeSStolerances(cvode_mem, 1E-4, 1.0);
    46       Assert.AreEqual(Problem.CV_SUCCESS, flag);
    47 
    48       var A = Problem.SUNDenseMatrix(numberOfEquations, numberOfEquations);
     36      // CVODES.N_VConst_Serial(100.0, y);
     37      CVODES.NV_Set_Ith_S(y, 0, 0.5); // x
     38      CVODES.NV_Set_Ith_S(y, 1, 1);  // v
     39
     40      var cvode_mem = CVODES.CVodeCreate(CVODES.MultistepMethod.CV_ADAMS, CVODES.NonlinearSolverIteration.CV_FUNCTIONAL);
     41
     42      var flag = CVODES.CVodeInit(cvode_mem, F, 0.0, y);
     43      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
     44
     45      flag = CVODES.CVodeSStolerances(cvode_mem, 1E-4, 1.0);
     46      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
     47
     48      var A = CVODES.SUNDenseMatrix(numberOfEquations, numberOfEquations);
    4949      Assert.AreNotSame(A, IntPtr.Zero);
    5050
    5151
    52       var linearSolver = Problem.SUNDenseLinearSolver(y, A);
     52      var linearSolver = CVODES.SUNDenseLinearSolver(y, A);
    5353      Assert.AreNotSame(linearSolver, IntPtr.Zero);
    5454
    55       flag = Problem.CVDlsSetLinearSolver(cvode_mem, linearSolver, A);
    56       Assert.AreEqual(Problem.CV_SUCCESS, flag);
    57 
    58       // flag = Problem.CVDlsSetJacFn(cvode_mem, JacF);
    59       // Assert.AreEqual(Problem.CV_SUCCESS, flag);
     55      flag = CVODES.CVDlsSetLinearSolver(cvode_mem, linearSolver, A);
     56      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
     57
     58      // flag = CVODES.CVDlsSetJacFn(cvode_mem, JacF);
     59      // Assert.AreEqual(CVODES.CV_SUCCESS, flag);
    6060
    6161      // var ns = 1; // number of parameters
    6262      // var p = new double[1]; // set as user-data
    63       // var yS0 = Problem.N_VCloneVectorArray_Serial(ns, y); // clone the output vector for each parameter, TODO: free
     63      // var yS0 = CVODES.N_VCloneVectorArray_Serial(ns, y); // clone the output vector for each parameter, TODO: free
    6464      // for(int i=0;i<ns;i++) {
    6565      //
     
    6767
    6868
    69       var q = Problem.N_VNew_Serial(1);
    70       Problem.NV_Set_Ith_S(q, 0, 0.0);
    71       flag = Problem.CVodeQuadInit(cvode_mem, FQ, q);
    72       Assert.AreEqual(Problem.CV_SUCCESS, flag);
     69      var q = CVODES.N_VNew_Serial(1);
     70      CVODES.NV_Set_Ith_S(q, 0, 0.0);
     71      flag = CVODES.CVodeQuadInit(cvode_mem, FQ, q);
     72      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
    7373
    7474
    7575      int steps = 10;
    76       flag = Problem.CVodeAdjInit(cvode_mem, steps, Problem.CV_HERMITE);
    77       Assert.AreEqual(Problem.CV_SUCCESS, flag);
     76      flag = CVODES.CVodeAdjInit(cvode_mem, steps, CVODES.CV_HERMITE);
     77      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
    7878
    7979      /* step by step forward integration
     
    8282      int nout = 100; // number of output times
    8383      for (int iout = 0; iout < nout; iout++) {
    84         flag = Problem.CVode(cvode_mem, tout, y, ref t, Problem.CV_NORMAL);
    85         Assert.AreEqual(Problem.CV_SUCCESS, flag);
    86         Console.WriteLine("{0} {1} {2}", t, Problem.NV_Get_Ith_S(y, 0), Problem.NV_Get_Ith_S(y, 1));
    87         // Problem.N_VPrint_Serial(y);
     84        flag = CVODES.CVode(cvode_mem, tout, y, ref t, CVODES.CV_NORMAL);
     85        Assert.AreEqual(CVODES.CV_SUCCESS, flag);
     86        Console.WriteLine("{0} {1} {2}", t, CVODES.NV_Get_Ith_S(y, 0), CVODES.NV_Get_Ith_S(y, 1));
     87        // CVODES.N_VPrint_Serial(y);
    8888        tout += 0.1;
    8989      }
     
    9494      double time = 0.0;
    9595      int ncheck = 0; // number of checkpoints
    96       flag = Problem.CVodeF(cvode_mem, tout, y, ref time, Problem.CV_NORMAL, ref ncheck);
    97       Assert.AreEqual(Problem.CV_SUCCESS, flag);
     96      flag = CVODES.CVodeF(cvode_mem, tout, y, ref time, CVODES.CV_NORMAL, ref ncheck);
     97      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
    9898
    9999      long numSteps = 0;
    100       flag = Problem.CVodeGetNumSteps(cvode_mem, ref numSteps);
    101       Assert.AreEqual(Problem.CV_SUCCESS, flag);
    102 
    103       var yB = Problem.N_VNew_Serial(numberOfEquations);
    104       Problem.N_VConst_Serial(0.0, yB);
     100      flag = CVODES.CVodeGetNumSteps(cvode_mem, ref numSteps);
     101      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
     102
     103      var yB = CVODES.N_VNew_Serial(numberOfEquations);
     104      CVODES.N_VConst_Serial(0.0, yB);
    105105
    106106      int numberOfParameters = 2;
    107       var qB = Problem.N_VNew_Serial(numberOfParameters);
    108       Problem.N_VConst_Serial(0.0, qB);
     107      var qB = CVODES.N_VNew_Serial(numberOfParameters);
     108      CVODES.N_VConst_Serial(0.0, qB);
    109109
    110110      int indexB = 0;
    111       flag = Problem.CVodeCreateB(cvode_mem, Problem.MultistepMethod.CV_BDF, Problem.NonlinearSolverIteration.CV_NEWTON, ref indexB);
    112       Assert.AreEqual(Problem.CV_SUCCESS, flag);
     111      flag = CVODES.CVodeCreateB(cvode_mem, CVODES.MultistepMethod.CV_BDF, CVODES.NonlinearSolverIteration.CV_NEWTON, ref indexB);
     112      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
    113113
    114114      var TB1 = tout;
    115       flag = Problem.CVodeInitB(cvode_mem, indexB, FB, TB1, yB);
    116       Assert.AreEqual(Problem.CV_SUCCESS, flag);
     115      flag = CVODES.CVodeInitB(cvode_mem, indexB, FB, TB1, yB);
     116      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
    117117
    118118      var relTolB = 1E-6;
    119119      var absTolB = 1E-8;
    120       flag = Problem.CVodeSStolerancesB(cvode_mem, indexB, relTolB, absTolB);
    121       Assert.AreEqual(Problem.CV_SUCCESS, flag);
    122 
    123       var AB = Problem.SUNDenseMatrix(numberOfEquations, numberOfEquations);
     120      flag = CVODES.CVodeSStolerancesB(cvode_mem, indexB, relTolB, absTolB);
     121      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
     122
     123      var AB = CVODES.SUNDenseMatrix(numberOfEquations, numberOfEquations);
    124124      Assert.AreNotSame(linearSolver, IntPtr.Zero);
    125125
    126       var lsB = Problem.SUNDenseLinearSolver(yB, AB);
     126      var lsB = CVODES.SUNDenseLinearSolver(yB, AB);
    127127      Assert.AreNotSame(linearSolver, IntPtr.Zero);
    128128
    129       flag = Problem.CVDlsSetLinearSolverB(cvode_mem, indexB, lsB, AB);
    130       Assert.AreEqual(Problem.CV_SUCCESS, flag);
    131 
    132       flag = Problem.CVDlsSetJacFnB(cvode_mem, indexB, JacFB);
    133       Assert.AreEqual(Problem.CV_SUCCESS, flag);
    134 
    135       flag = Problem.CVodeQuadInitB(cvode_mem, indexB, FQB, qB);
    136       Assert.AreEqual(Problem.CV_SUCCESS, flag);
     129      flag = CVODES.CVDlsSetLinearSolverB(cvode_mem, indexB, lsB, AB);
     130      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
     131
     132      flag = CVODES.CVDlsSetJacFnB(cvode_mem, indexB, JacFB);
     133      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
     134
     135      flag = CVODES.CVodeQuadInitB(cvode_mem, indexB, FQB, qB);
     136      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
    137137
    138138      /* First get results at t = TBout1 */
    139139
    140       /* Call CVodeB to integrate the backward ODE problem. */
     140      /* Call CVodeB to integrate the backward ODE CVODES. */
    141141      var tBackOut = 50.0;
    142       flag = Problem.CVodeB(cvode_mem, tBackOut, Problem.CV_NORMAL);
    143       Assert.AreEqual(Problem.CV_SUCCESS, flag);
    144 
    145       /* Call CVodeGetB to get yB of the backward ODE problem. */
    146       flag = Problem.CVodeGetB(cvode_mem, indexB, ref time, yB);
    147       Assert.AreEqual(Problem.CV_SUCCESS, flag);
     142      flag = CVODES.CVodeB(cvode_mem, tBackOut, CVODES.CV_NORMAL);
     143      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
     144
     145      /* Call CVodeGetB to get yB of the backward ODE CVODES. */
     146      flag = CVODES.CVodeGetB(cvode_mem, indexB, ref time, yB);
     147      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
    148148
    149149      /* Call CVodeGetAdjY to get the interpolated value of the forward solution
    150150         y during a backward integration. */
    151       flag = Problem.CVodeGetAdjY(cvode_mem, tBackOut, y);
    152       Assert.AreEqual(Problem.CV_SUCCESS, flag);
     151      flag = CVODES.CVodeGetAdjY(cvode_mem, tBackOut, y);
     152      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
    153153
    154154      /* Then at t = T0 */
    155155
    156156      double t0 = 0.0;
    157       flag = Problem.CVodeB(cvode_mem, t0, Problem.CV_NORMAL);
    158       Assert.AreEqual(Problem.CV_SUCCESS, flag);
     157      flag = CVODES.CVodeB(cvode_mem, t0, CVODES.CV_NORMAL);
     158      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
    159159
    160160      long nstB = 0;
    161       Problem.CVodeGetNumSteps(Problem.CVodeGetAdjCVodeBmem(cvode_mem, indexB), ref nstB);
    162 
    163       flag = Problem.CVodeGetB(cvode_mem, indexB, ref time, yB);
    164       Assert.AreEqual(Problem.CV_SUCCESS, flag);
    165 
    166 
    167       flag = Problem.CVodeGetQuadB(cvode_mem, indexB, ref time, qB);
    168       Assert.AreEqual(Problem.CV_SUCCESS, flag);
    169 
    170       flag = Problem.CVodeGetAdjY(cvode_mem, t0, y);
    171       Assert.AreEqual(Problem.CV_SUCCESS, flag);
    172 
    173       Problem.N_VDestroy_Serial(y);
    174       Problem.CVodeFree(cvode_mem);
    175       Problem.SUNLinSolFree(linearSolver);
    176       Problem.SUNMatDestroy(A);
     161      CVODES.CVodeGetNumSteps(CVODES.CVodeGetAdjCVodeBmem(cvode_mem, indexB), ref nstB);
     162
     163      flag = CVODES.CVodeGetB(cvode_mem, indexB, ref time, yB);
     164      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
     165
     166
     167      flag = CVODES.CVodeGetQuadB(cvode_mem, indexB, ref time, qB);
     168      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
     169
     170      flag = CVODES.CVodeGetAdjY(cvode_mem, t0, y);
     171      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
     172
     173      CVODES.N_VDestroy_Serial(y);
     174      CVODES.CVodeFree(cvode_mem);
     175      CVODES.SUNLinSolFree(linearSolver);
     176      CVODES.SUNMatDestroy(A);
    177177    }
    178178
     
    182182      // linear oscillator
    183183      int numberOfEquations = 2;
    184       var y = Problem.N_VNew_Serial(numberOfEquations);
     184      var y = CVODES.N_VNew_Serial(numberOfEquations);
    185185      // y must be initialized before calling CVodeInit
    186       // Problem.N_VConst_Serial(100.0, y);
    187       Problem.NV_Set_Ith_S(y, 0, 0.5); // x
    188       Problem.NV_Set_Ith_S(y, 1, 1);  // v
    189 
    190       var cvode_mem = Problem.CVodeCreate(Problem.MultistepMethod.CV_ADAMS, Problem.NonlinearSolverIteration.CV_FUNCTIONAL);
    191 
    192       var flag = Problem.CVodeInit(cvode_mem, F, 0.0, y);
    193       Assert.AreEqual(Problem.CV_SUCCESS, flag);
    194 
    195       flag = Problem.CVodeSStolerances(cvode_mem, 1E-4, 1.0);
    196       Assert.AreEqual(Problem.CV_SUCCESS, flag);
    197 
    198       var A = Problem.SUNDenseMatrix(numberOfEquations, numberOfEquations);
     186      // CVODES.N_VConst_Serial(100.0, y);
     187      CVODES.NV_Set_Ith_S(y, 0, 0.5); // x
     188      CVODES.NV_Set_Ith_S(y, 1, 1);  // v
     189
     190      var cvode_mem = CVODES.CVodeCreate(CVODES.MultistepMethod.CV_ADAMS, CVODES.NonlinearSolverIteration.CV_FUNCTIONAL);
     191
     192      var flag = CVODES.CVodeInit(cvode_mem, F, 0.0, y);
     193      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
     194
     195      flag = CVODES.CVodeSStolerances(cvode_mem, 1E-4, 1.0);
     196      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
     197
     198      var A = CVODES.SUNDenseMatrix(numberOfEquations, numberOfEquations);
    199199      Assert.AreNotSame(A, IntPtr.Zero);
    200       Console.WriteLine(Problem.SUNDenseMatrix_Get(A, 0, 0));
    201 
    202 
    203       var linearSolver = Problem.SUNDenseLinearSolver(y, A);
     200      Console.WriteLine(CVODES.SUNDenseMatrix_Get(A, 0, 0));
     201
     202
     203      var linearSolver = CVODES.SUNDenseLinearSolver(y, A);
    204204      Assert.AreNotSame(linearSolver, IntPtr.Zero);
    205205
    206       flag = Problem.CVDlsSetLinearSolver(cvode_mem, linearSolver, A);
    207       Assert.AreEqual(Problem.CV_SUCCESS, flag);
    208 
    209       flag = Problem.CVDlsSetJacFn(cvode_mem, JacF);
    210       Assert.AreEqual(Problem.CV_SUCCESS, flag);
     206      flag = CVODES.CVDlsSetLinearSolver(cvode_mem, linearSolver, A);
     207      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
     208
     209      flag = CVODES.CVDlsSetJacFn(cvode_mem, JacF);
     210      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
    211211
    212212      var ns = 1; // number of parameters
    213213      var p = new double[1]; // set as user-data
    214       var yS0 = Problem.N_VCloneVectorArray_Serial(ns, y); // clone the output vector for each parameter, TODO: free
     214      var yS0 = CVODES.N_VCloneVectorArray_Serial(ns, y); // clone the output vector for each parameter, TODO: free
    215215      unsafe {
    216216        for (int i = 0; i < ns; i++) {
    217217          var yS0_i = *((IntPtr*)yS0.ToPointer() + i);
    218           Problem.N_VConst_Serial(0.0, yS0_i);
     218          CVODES.N_VConst_Serial(0.0, yS0_i);
    219219        }
    220220      }
    221221
    222222
    223       flag = Problem.CVodeSensInit(cvode_mem, ns, Problem.CV_SIMULTANEOUS, FS, yS0);
    224       Assert.AreEqual(Problem.CV_SUCCESS, flag);
     223      flag = CVODES.CVodeSensInit(cvode_mem, ns, CVODES.CV_SIMULTANEOUS, FS, yS0);
     224      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
    225225
    226226      double reltolS = 1E-5;
    227227      var abstolS = new double[] { 1E-6 };
    228       // flag = Problem.CVodeSensSStolerances(cvode_mem, reltolS, abstolS);
    229       // Assert.AreEqual(Problem.CV_SUCCESS, flag);
    230       flag = Problem.CVodeSensEEtolerances(cvode_mem);
    231       Assert.AreEqual(Problem.CV_SUCCESS, flag);
    232 
    233 
    234       // var q = Problem.N_VNew_Serial(1);
    235       // Problem.NV_Set_Ith_S(q, 0, 0.0);
    236       // flag = Problem.CVodeQuadInit(cvode_mem, FQ, q);
    237       // Assert.AreEqual(Problem.CV_SUCCESS, flag);
     228      // flag = CVODES.CVodeSensSStolerances(cvode_mem, reltolS, abstolS);
     229      // Assert.AreEqual(CVODES.CV_SUCCESS, flag);
     230      flag = CVODES.CVodeSensEEtolerances(cvode_mem);
     231      Assert.AreEqual(CVODES.CV_SUCCESS, flag);
     232
     233
     234      // var q = CVODES.N_VNew_Serial(1);
     235      // CVODES.NV_Set_Ith_S(q, 0, 0.0);
     236      // flag = CVODES.CVodeQuadInit(cvode_mem, FQ, q);
     237      // Assert.AreEqual(CVODES.CV_SUCCESS, flag);
    238238
    239239
    240240      // int steps = 10;
    241       // flag = Problem.CVodeAdjInit(cvode_mem, steps, Problem.CV_HERMITE);
    242       // Assert.AreEqual(Problem.CV_SUCCESS, flag);
     241      // flag = CVODES.CVodeAdjInit(cvode_mem, steps, CVODES.CV_HERMITE);
     242      // Assert.AreEqual(CVODES.CV_SUCCESS, flag);
    243243
    244244      // step by step forward integration
     
    247247      int nout = 1000; // number of output times
    248248      for (int iout = 0; iout < nout; iout++) {
    249         flag = Problem.CVode(cvode_mem, tout, y, ref t, Problem.CV_NORMAL);
    250         Assert.AreEqual(Problem.CV_SUCCESS, flag);
     249        flag = CVODES.CVode(cvode_mem, tout, y, ref t, CVODES.CV_NORMAL);
     250        Assert.AreEqual(CVODES.CV_SUCCESS, flag);
    251251
    252252        // get sensitivities
    253         flag = Problem.CVodeGetSens(cvode_mem, ref t, yS0);
    254         Assert.AreEqual(Problem.CV_SUCCESS, flag);
     253        flag = CVODES.CVodeGetSens(cvode_mem, ref t, yS0);
     254        Assert.AreEqual(CVODES.CV_SUCCESS, flag);
    255255        unsafe {
    256256          var ySP0 = *(IntPtr*)yS0.ToPointer(); // parameter of sensitivities for first component
    257           Console.WriteLine("{0} {1} {2} {3} {4}", t, Problem.NV_Get_Ith_S(y, 0), Problem.NV_Get_Ith_S(y, 1), Problem.NV_Get_Ith_S(ySP0, 0), Problem.NV_Get_Ith_S(ySP0, 1));
     257          Console.WriteLine("{0} {1} {2} {3} {4}", t, CVODES.NV_Get_Ith_S(y, 0), CVODES.NV_Get_Ith_S(y, 1), CVODES.NV_Get_Ith_S(ySP0, 0), CVODES.NV_Get_Ith_S(ySP0, 1));
    258258        }
    259259
     
    263263
    264264
    265       Problem.N_VDestroy_Serial(y);
    266       Problem.CVodeFree(cvode_mem);
    267       Problem.SUNLinSolFree(linearSolver);
    268       Problem.SUNMatDestroy(A);
     265      CVODES.N_VDestroy_Serial(y);
     266      CVODES.CVodeFree(cvode_mem);
     267      CVODES.SUNLinSolFree(linearSolver);
     268      CVODES.SUNMatDestroy(A);
    269269    }
    270270
     
    280280      // ∂y1/∂t = y2
    281281      // ∂y2/∂t = -0.3 y1
    282       Problem.NV_Set_Ith_S(ydot, 0, Problem.NV_Get_Ith_S(y, 1));
    283       Problem.NV_Set_Ith_S(ydot, 1, -0.3 * Problem.NV_Get_Ith_S(y, 0));
     282      CVODES.NV_Set_Ith_S(ydot, 0, CVODES.NV_Get_Ith_S(y, 1));
     283      CVODES.NV_Set_Ith_S(ydot, 1, -0.3 * CVODES.NV_Get_Ith_S(y, 0));
    284284      return 0;
    285285      ;
     
    297297      //  ∂f2/∂y1  ∂f2/∂y2
    298298
    299       Problem.SUNDenseMatrix_Set(Jac, 0, 0, 0.0);
    300       Problem.SUNDenseMatrix_Set(Jac, 0, 1, 1.0);
    301       Problem.SUNDenseMatrix_Set(Jac, 1, 0, -0.3);
    302       Problem.SUNDenseMatrix_Set(Jac, 1, 1, 0.0);
     299      CVODES.SUNDenseMatrix_Set(Jac, 0, 0, 0.0);
     300      CVODES.SUNDenseMatrix_Set(Jac, 0, 1, 1.0);
     301      CVODES.SUNDenseMatrix_Set(Jac, 1, 0, -0.3);
     302      CVODES.SUNDenseMatrix_Set(Jac, 1, 1, 0.0);
    303303      return 0;
    304304    }
     
    323323
    324324      // (∂f /∂y)s_i(t) + ∂f /∂p_i
    325       var y1 = Problem.NV_Get_Ith_S(y, 0); var y2 = Problem.NV_Get_Ith_S(y, 1);
     325      var y1 = CVODES.NV_Get_Ith_S(y, 0); var y2 = CVODES.NV_Get_Ith_S(y, 1);
    326326
    327327      unsafe {
    328328        var yS_p0 = *(IntPtr*)yS.ToPointer();
    329         var s1 = Problem.NV_Get_Ith_S(yS_p0, 0); var s2 = Problem.NV_Get_Ith_S(yS_p0, 1);
    330 
    331 
    332         // (∂f /∂y)s_i(t)  = Jac s_i(t)
     329        var s1 = CVODES.NV_Get_Ith_S(yS_p0, 0); var s2 = CVODES.NV_Get_Ith_S(yS_p0, 1);
     330
     331
     332        // (∂f /∂y)s_i(t)  == Jac s_i(t)
    333333        var sd1 = s2;
    334334        var sd2 = -0.3 * s1;
    335335
    336336        var s_p0 = *(IntPtr*)ySdot.ToPointer();
    337         Problem.NV_Set_Ith_S(s_p0, 0, sd1 + 0.0);
    338         Problem.NV_Set_Ith_S(s_p0, 1, sd2 - 0.3);
     337        CVODES.NV_Set_Ith_S(s_p0, 0, sd1 + 0.0);
     338        CVODES.NV_Set_Ith_S(s_p0, 1, sd2 - 0.3);
    339339      }
    340340
     
    365365      // λ2' = 1.0 λ1 - 0.0
    366366
    367       Problem.NV_Set_Ith_S(yBdot, 0, 0.3 * Problem.NV_Get_Ith_S(yB, 1));
    368       Problem.NV_Set_Ith_S(yBdot, 1, Problem.NV_Get_Ith_S(yB, 0));
     367      CVODES.NV_Set_Ith_S(yBdot, 0, 0.3 * CVODES.NV_Get_Ith_S(yB, 1));
     368      CVODES.NV_Set_Ith_S(yBdot, 1, CVODES.NV_Get_Ith_S(yB, 0));
    369369
    370370      return 0;
     
    374374    private int FQ(double t, IntPtr y, IntPtr yQdot, IntPtr user_data) {
    375375      // TODO: squared error
    376       Problem.NV_Set_Ith_S(yQdot, 0, Problem.NV_Get_Ith_S(y, 2));
     376      CVODES.NV_Set_Ith_S(yQdot, 0, CVODES.NV_Get_Ith_S(y, 2));
    377377      return 0;
    378378    }
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/HeuristicLab.Problems.DynamicalSystemsModelling-3.3.csproj

    r16225 r16248  
    111111  </ItemGroup>
    112112  <ItemGroup>
     113    <Compile Include="CVODES.cs" />
    113114    <Compile Include="Plugin.cs" />
    114115    <Compile Include="Problem.cs" />
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Problem.cs

    r16246 r16248  
    2424using System.Diagnostics;
    2525using System.Linq;
    26 using System.Runtime.InteropServices;
    2726using HeuristicLab.Analysis;
    2827using HeuristicLab.Collections;
     
    132131  [StorableClass]
    133132  public sealed class Problem : SingleObjectiveBasicProblem<MultiEncoding>, IRegressionProblem, IProblemInstanceConsumer<IRegressionProblemData>, IProblemInstanceExporter<IRegressionProblemData> {
    134 
    135     // CVODES types
    136     public enum MultistepMethod : int { CV_ADAMS = 1, CV_BDF = 2 };
    137     public enum NonlinearSolverIteration : int { CV_NEWTON = 1, CV_FUNCTIONAL = 2 };
    138 
    139 
    140     /* itask */
    141     public const int CV_NORMAL = 1;
    142     public const int CV_ONE_STEP = 2;
    143 
    144     /* ism */
    145     public const int CV_SIMULTANEOUS = 1;
    146     public const int CV_STAGGERED = 2;
    147     public const int CV_STAGGERED1 = 3;
    148 
    149     /* DQtype */
    150     public const int CV_CENTERED = 1;
    151     public const int CV_FORWARD = 2;
    152 
    153     /* interp */
    154     public const int CV_HERMITE = 1;
    155     public const int CV_POLYNOMIAL = 2;
    156 
    157     /*
    158      * ----------------------------------------
    159      * CVODES return flags
    160      * ----------------------------------------
    161      */
    162 
    163     public const int CV_SUCCESS = 0;
    164     public const int CV_TSTOP_RETURN = 1;
    165     public const int CV_ROOT_RETURN = 2;
    166 
    167     public const int CV_WARNING = 99;
    168 
    169     public const int CV_TOO_MUCH_WORK = -1;
    170     public const int CV_TOO_MUCH_ACC = -2;
    171     public const int CV_ERR_FAILURE = -3;
    172     public const int CV_CONV_FAILURE = -4;
    173 
    174     public const int CV_LINIT_FAIL = -5;
    175     public const int CV_LSETUP_FAIL = -6;
    176     public const int CV_LSOLVE_FAIL = -7;
    177     public const int CV_RHSFUNC_FAIL = -8;
    178     public const int CV_FIRST_RHSFUNC_ERR = -9;
    179     public const int CV_REPTD_RHSFUNC_ERR = -10;
    180     public const int CV_UNREC_RHSFUNC_ERR = -11;
    181     public const int CV_RTFUNC_FAIL = -12;
    182     public const int CV_CONSTR_FAIL = -13;
    183 
    184     public const int CV_MEM_FAIL = -20;
    185     public const int CV_MEM_NULL = -21;
    186     public const int CV_ILL_INPUT = -22;
    187     public const int CV_NO_MALLOC = -23;
    188     public const int CV_BAD_K = -24;
    189     public const int CV_BAD_T = -25;
    190     public const int CV_BAD_DKY = -26;
    191     public const int CV_TOO_CLOSE = -27;
    192 
    193     public const int CV_NO_QUAD = -30;
    194     public const int CV_QRHSFUNC_FAIL = -31;
    195     public const int CV_FIRST_QRHSFUNC_ERR = -32;
    196     public const int CV_REPTD_QRHSFUNC_ERR = -33;
    197     public const int CV_UNREC_QRHSFUNC_ERR = -34;
    198 
    199     public const int CV_NO_SENS = -40;
    200     public const int CV_SRHSFUNC_FAIL = -41;
    201     public const int CV_FIRST_SRHSFUNC_ERR = -42;
    202     public const int CV_REPTD_SRHSFUNC_ERR = -43;
    203     public const int CV_UNREC_SRHSFUNC_ERR = -44;
    204 
    205     public const int CV_BAD_IS = -45;
    206 
    207     public const int CV_NO_QUADSENS = -50;
    208     public const int CV_QSRHSFUNC_FAIL = -51;
    209     public const int CV_FIRST_QSRHSFUNC_ERR = -52;
    210     public const int CV_REPTD_QSRHSFUNC_ERR = -53;
    211     public const int CV_UNREC_QSRHSFUNC_ERR = -54;
    212 
    213     /*
    214      * ----------------------------------------
    215      * CVODEA return flags
    216      * ----------------------------------------
    217      */
    218 
    219     public const int CV_NO_ADJ = -101;
    220     public const int CV_NO_FWD = -102;
    221     public const int CV_NO_BCK = -103;
    222     public const int CV_BAD_TB0 = -104;
    223     public const int CV_REIFWD_FAIL = -105;
    224     public const int CV_FWD_FAIL = -106;
    225 
    226 
    227     public const int CV_GETY_BADT = -107;
    228 
    229     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    230     public delegate int CVRhsFunc(
    231         double t, // realtype
    232         IntPtr y, // N_Vector
    233         IntPtr ydot, // N_Vector
    234         IntPtr user_data
    235       );
    236 
    237     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    238     public delegate int CVRhsFuncB(
    239         double t, // realtype
    240         IntPtr y, // N_Vector
    241         IntPtr yB, // N_Vector
    242         IntPtr yBdot, // N_Vector
    243         IntPtr user_data
    244       );
    245 
    246     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    247     public delegate int CVDlsJacFunc(
    248       double t,
    249       IntPtr y, // N_Vector
    250       IntPtr fy, // N_Vector
    251       IntPtr Jac, // SUNMatrix
    252       IntPtr user_data,
    253       IntPtr tmp1, // N_Vector
    254       IntPtr tmp2, // N_Vector
    255       IntPtr tmp3 // N_Vector
    256       );
    257 
    258     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    259     public delegate int CVDlsJacFuncB(
    260       double t,
    261       IntPtr y, // N_Vector
    262       IntPtr yB, // N_Vector
    263       IntPtr fyB, // N_Vector
    264       IntPtr Jac, // SUNMatrix
    265       IntPtr user_data,
    266       IntPtr tmp1, // N_Vector
    267       IntPtr tmp2, // N_Vector
    268       IntPtr tmp3 // N_Vector
    269     );
    270 
    271     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    272     public delegate int CVQuadRhsFn(
    273       double t,
    274       IntPtr y, // N_Vector
    275       IntPtr yQdot, // N_Vector
    276       IntPtr user_data
    277       );
    278 
    279     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    280     public delegate int CVQuadRhsFnB(
    281       double t,
    282       IntPtr y, // N_Vector
    283       IntPtr yB, // N_Vector
    284       IntPtr qBdot, // N_Vector
    285       IntPtr user_data
    286       );
    287 
    288 
    289     // to calculate sensitivities RHS for all equations at once
    290     // must compute (∂f/∂y)s_i(t) + ∂f/∂p_i
    291     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    292     public delegate int CVSensRhsFn(
    293       int Ns,
    294       double t,
    295       IntPtr y, // N_Vector
    296       IntPtr ydot, // N_Vector
    297       IntPtr yS, // N_Vector* one vector for each parameter
    298       IntPtr ySdot, // N_Vector* one vector for each parameter
    299       IntPtr user_data,
    300       IntPtr tmp1, // N_Vector
    301       IntPtr tmp2 // N_Vector
    302     );
    303 
    304     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeCreate", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    305     // returns a void* to the cvodes memory block if successful otherwise NULL
    306     public static extern IntPtr CVodeCreate(MultistepMethod lmm, NonlinearSolverIteration iter);
    307 
    308     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeInit", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    309     public static extern int CVodeInit(
    310       IntPtr cvode_mem, // pointer returned by CVodeCreate
    311       CVRhsFunc f,
    312       double t0, // realtype, the inital value of t
    313       IntPtr y0 // N_Vector the initial value of y
    314     );
    315 
    316 
    317     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeSStolerances", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    318     public static extern int CVodeSStolerances(
    319       IntPtr cvode_mem,
    320       double reltol,
    321       double abstol
    322       );
    323 
    324     [DllImport("sundials_cvodes.dll", EntryPoint = "CVDlsSetLinearSolver", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    325     public static extern int CVDlsSetLinearSolver(
    326       IntPtr cvode_mem,
    327       IntPtr linearSolver, // SUNLinearSolver
    328       IntPtr j // SUNMatrix
    329       );
    330 
    331     [DllImport("sundials_cvodes.dll", EntryPoint = "CVDlsSetJacFn", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    332     public static extern int CVDlsSetJacFn(
    333       IntPtr cvode_mem,
    334       CVDlsJacFunc jacFunc
    335       );
    336 
    337     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeSensInit", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    338     public static extern int CVodeSensInit(
    339       IntPtr cvode_mem,
    340       int Ns, // number of parameters
    341       int ism, // sensitivity solution method, CV_SIMULTANEOUS or CV_STAGGERED
    342       CVSensRhsFn fS, // right hand side function which computes all sensitivity RHS at the same time
    343       IntPtr yS0 // N_Vector
    344       );
    345 
    346     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeSensSStolerances", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    347     public static extern int CVodeSensSStolerances(
    348       IntPtr cvode_mem,
    349       double reltol,
    350       double[] abstol
    351     );
    352 
    353     /* Call CVodeSensEEtolerances to estimate tolerances for sensitivity
    354    variables based on the rolerances supplied for states variables and
    355    the scaling factor pbar */
    356     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeSensEEtolerances", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    357     public static extern int CVodeSensEEtolerances(
    358       IntPtr cvode_mem
    359       );
    360 
    361     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeGetSens", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    362     public static extern int CVodeGetSens(
    363       IntPtr cvode_mem,
    364       ref double tret,
    365       IntPtr yS //N_Vector*, one vector for each parameter
    366       );
    367 
    368     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeQuadInit", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    369     public static extern int CVodeQuadInit(
    370       IntPtr cvode_mem,
    371       CVQuadRhsFn qF,
    372       IntPtr yQ0 // N_Vector, initial value of yQ
    373       );
    374 
    375     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeQuadInitB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    376     public static extern int CVodeQuadInitB(
    377       IntPtr cvode_mem,
    378       int indexB,
    379       CVQuadRhsFnB rhsQB,
    380       IntPtr yQB0 // N_Vector, initial value of yQB
    381     );
    382 
    383 
    384     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeAdjInit", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    385     public static extern int CVodeAdjInit(
    386       IntPtr cvode_mem,
    387       int nd,  // integration steps between checkpoints
    388       int interpType // either CV_POLYNOMIAL or CV_HERMITE
    389       );
    390 
    391     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeF", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    392     public static extern int CVodeF(
    393         IntPtr cvode_mem,
    394         double t_out, // the next time at which a computed solution is desired
    395         IntPtr y, // N_Vector, the solution vector y
    396         ref double t_ret, // the time reached by the solver (output)
    397         int itask, // CV_NORMAL or CV_ONE_STEP
    398         ref int ncheck  // the number of internal checkpoints stored so far.
    399       );
    400 
    401     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeGetNumSteps", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    402     public static extern int CVodeGetNumSteps(
    403       IntPtr cvode_mem,
    404       ref long ncheck  // the number of steps taken
    405     );
    406 
    407     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeCreateB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    408     public static extern int CVodeCreateB(
    409         IntPtr cvode_mem,
    410         MultistepMethod lmmB,
    411         NonlinearSolverIteration iterB,
    412         ref int which
    413       );
    414 
    415     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeInitB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    416     public static extern int CVodeInitB(
    417         IntPtr cvode_mem,
    418         int which,
    419         CVRhsFuncB rightHandSide,
    420         double tB0, // endpoint T where final conditions are provided (equal to endpoint of forward integration)
    421         IntPtr yB0 // N_Vector inital value at t=tb0 of backward solution
    422       );
    423 
    424     [DllImport("sundials_cvodes.dll", EntryPoint = "CVode", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    425     public static extern int CVode(
    426       IntPtr cvode_mem,
    427       double tout, // next time at which a solution is desired
    428       IntPtr yout, // N_Vector
    429       ref double tret, // the time reached by the solver (output)
    430       int itask // flag indicating the job of the solver for the next step.
    431       );
    432 
    433 
    434     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeSStolerancesB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    435     public static extern int CVodeSStolerancesB(
    436       IntPtr cvode_mem,
    437       int which,
    438       double reltol,
    439       double abstol
    440     );
    441 
    442     [DllImport("sundials_cvodes.dll", EntryPoint = "CVDlsSetLinearSolverB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    443     public static extern int CVDlsSetLinearSolverB(
    444       IntPtr cvode_mem,
    445       int which,
    446       IntPtr linearSolver, // SUNLinearSolver
    447       IntPtr j // SUNMatrix
    448     );
    449 
    450     [DllImport("sundials_cvodes.dll", EntryPoint = "CVDlsSetJacFnB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    451     public static extern int CVDlsSetJacFnB(
    452       IntPtr cvode_mem,
    453       int which,
    454       CVDlsJacFuncB jacFunc
    455     );
    456 
    457     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    458     public static extern int CVodeB(
    459       IntPtr cvode_mem,
    460       double tout, // next time at which a solution is desired
    461       int itask // flag indicating the job of the solver for the next step.
    462     );
    463 
    464     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeGetB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    465     public static extern int CVodeGetB(
    466           IntPtr cvode_mem,
    467           int which,
    468           ref double tret,
    469           IntPtr yB // the backward solution at time tret
    470         );
    471     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeGetAdjY", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    472     public static extern int CVodeGetAdjY(
    473           IntPtr cvode_mem,
    474           double t,
    475           IntPtr y // the forward solution y(t)
    476         );
    477 
    478     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeGetAdjCVodeBmem", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    479     public static extern IntPtr CVodeGetAdjCVodeBmem(
    480               IntPtr cvode_mem,
    481               int which
    482             );
    483 
    484     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeGetQuadB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    485     public static extern int CVodeGetQuadB(
    486         IntPtr cvode_mem,
    487         int which,
    488         ref double tret,
    489         IntPtr yQB // N_Vector
    490       );
    491 
    492     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeFree", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    493 
    494     public static extern int CVodeFree(IntPtr cvode_mem);
    495 
    496     #region matrix
    497     [DllImport("sundials_cvodes.dll", EntryPoint = "SUNDenseMatrix", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    498     public static extern IntPtr SUNDenseMatrix(long m, long n);
    499 
    500     [DllImport("sundials_cvodes.dll", EntryPoint = "SUNMatDestroy", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    501     public static extern void SUNMatDestroy(IntPtr A);
    502 
    503     [DllImport("sundials_cvodes.dll", EntryPoint = "SUNDenseMatrix_Data", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    504     public unsafe static extern double* SUNDenseMatrix_Data(IntPtr matrix);
    505 
    506     [DllImport("sundials_cvodes.dll", EntryPoint = "SUNDenseMatrix_Cols", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    507     public static extern long SUNDenseMatrix_Cols(IntPtr matrix);
    508 
    509     [DllImport("sundials_cvodes.dll", EntryPoint = "SUNDenseMatrix_Rows", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    510     public static extern long SUNDenseMatrix_Rows(IntPtr matrix);
    511 
    512 
    513     public unsafe static double SUNDenseMatrix_Get(IntPtr mat, long i, long j) {
    514       long M = SUNDenseMatrix_Rows(mat);
    515       // the(i, j)th element of A(with 0 <= i<M and 0 <= j<N) is given by(A->data)[j*M+i]
    516       return SUNDenseMatrix_Data(mat)[j*M+i];
    517     }
    518 
    519     public unsafe static void SUNDenseMatrix_Set(IntPtr mat, long i, long j, double val) {
    520       long M = SUNDenseMatrix_Rows(mat);
    521       // the(i, j)th element of A(with 0 <= i<M and 0 <= j<N) is given by(A->data)[j*M+i]
    522       SUNDenseMatrix_Data(mat)[j * M + i] = val;
    523     }
    524 
    525     #endregion
    526 
    527     #region linear solver
    528     [DllImport("sundials_cvodes.dll", EntryPoint = "SUNDenseLinearSolver", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    529     public static extern IntPtr SUNDenseLinearSolver(
    530       IntPtr y, // N_Vector
    531       IntPtr A // SUNMatrix
    532       );
    533 
    534     [DllImport("sundials_cvodes.dll", EntryPoint = "SUNLinSolInitialize", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    535     public static extern int SUNLinSolInitialize(IntPtr linearSolver);
    536 
    537     [DllImport("sundials_cvodes.dll", EntryPoint = "SUNLinSolSetup", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    538     public static extern int SUNLinSolSetup(
    539       IntPtr linearSolver,
    540       IntPtr A // SUNMatrix
    541       );
    542 
    543     [DllImport("sundials_cvodes.dll", EntryPoint = "SUNLinSolSolve", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    544     public static extern int SUNLinSolSolve(
    545       IntPtr linearSolver,
    546       IntPtr A, // SUNMatrix
    547       IntPtr x, // N_Vector
    548       IntPtr b, // N_Vector
    549       double tol
    550       );
    551 
    552     [DllImport("sundials_cvodes.dll", EntryPoint = "SUNLinSolFree", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    553     public static extern int SUNLinSolFree(IntPtr linearSolver);
    554 
    555     #endregion
    556 
    557     #region N_Vector
    558     [DllImport("sundials_cvodes.dll", EntryPoint = "N_VNew_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    559     public static extern IntPtr N_VNew_Serial(long vec_length);
    560 
    561     [DllImport("sundials_cvodes.dll", EntryPoint = "N_VDestroy_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    562     public static extern void N_VDestroy_Serial(IntPtr vec);
    563 
    564     [DllImport("sundials_cvodes.dll", EntryPoint = "N_VPrint_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    565     public static extern void N_VPrint_Serial(IntPtr vec);
    566 
    567     [DllImport("sundials_cvodes.dll", EntryPoint = "N_VConst_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    568     public static extern void N_VConst_Serial(double c, IntPtr vec);
    569 
    570 
    571     [DllImport("sundials_cvodes.dll", EntryPoint = "N_VL1Norm_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    572     public static extern double N_VL1Norm_Serial(IntPtr vec);
    573 
    574     [DllImport("sundials_cvodes.dll", EntryPoint = "N_VMake_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    575     public static extern IntPtr N_VMake_Serial(long vec_length, double[] v_data);
    576 
    577     [DllImport("sundials_cvodes.dll", EntryPoint = "N_VScale", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    578     ///  Performs the operation z = c*x
    579     public static extern void N_VScale(double s,
    580       IntPtr x, // N_Vector
    581       IntPtr z // N_Vector
    582       );
    583 
    584     [DllImport("sundials_cvodes.dll", EntryPoint = "N_VMake_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    585     public unsafe static extern double* N_VGetArrayPointer_Serial(IntPtr vec);
    586 
    587     [DllImport("sundials_cvodes.dll", EntryPoint = "N_VCloneVectorArray_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    588     public static extern IntPtr N_VCloneVectorArray_Serial(int count, IntPtr vec); // returns N_Vector* !
    589 
    590 
    591     /*
    592 #define NV_CONTENT_S(v)  ( (N_VectorContent_Serial)(v->content) )
    593 
    594 #define NV_LENGTH_S(v)   ( NV_CONTENT_S(v)->length )
    595 
    596 #define NV_OWN_DATA_S(v) ( NV_CONTENT_S(v)->own_data )
    597 
    598 #define NV_DATA_S(v)     ( NV_CONTENT_S(v)->data )
    599 
    600 #define NV_Ith_S(v,i)    ( NV_DATA_S(v)[i] )
    601 */
    602     // methods for macros
    603     public unsafe static int* NV_CONTENT_S(IntPtr v) {
    604       int* content = (int*)*(int*)v.ToPointer();
    605       return content;
    606     }
    607 
    608     public unsafe static long NV_LENGTH_S(IntPtr v) {
    609       long length = *NV_CONTENT_S(v);
    610       return length;
    611     }
    612     public unsafe static bool NV_OWN_DATA_S(IntPtr v) {
    613       var content = NV_CONTENT_S(v);
    614       int own_data = *(content + 2);
    615       return own_data > 0;
    616     }
    617     public unsafe static double* NV_DATA_S(IntPtr v) {
    618       var content = NV_CONTENT_S(v);
    619       double* data = (double*)*(content + 3);
    620       return data;
    621     }
    622     public unsafe static double NV_Get_Ith_S(IntPtr v, long i) {
    623       return NV_DATA_S(v)[i];
    624     }
    625     public unsafe static void NV_Set_Ith_S(IntPtr v, long i, double val) {
    626       NV_DATA_S(v)[i] = val;
    627     }
    628     #endregion
    629 
    630133
    631134
Note: See TracChangeset for help on using the changeset viewer.