Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/CVODES.cs @ 16663

Last change on this file since 16663 was 16256, checked in by gkronber, 5 years ago

#2925: added x64 version of CVODES as well as dispatching code for x86 and x64.

File size: 48.1 KB
Line 
1using System;
2using System.Runtime.InteropServices;
3
4namespace HeuristicLab.Problems.DynamicalSystemsModelling {
5  public static class CVODES {
6
7    // CVODES types
8    public enum MultistepMethod : int { CV_ADAMS = 1, CV_BDF = 2 };
9    public enum NonlinearSolverIteration : int { CV_NEWTON = 1, CV_FUNCTIONAL = 2 };
10
11
12    /* itask */
13    public const int CV_NORMAL = 1;
14    public const int CV_ONE_STEP = 2;
15
16    /* ism */
17    public const int CV_SIMULTANEOUS = 1;
18    public const int CV_STAGGERED = 2;
19    public const int CV_STAGGERED1 = 3;
20
21    /* DQtype */
22    public const int CV_CENTERED = 1;
23    public const int CV_FORWARD = 2;
24
25    /* interp */
26    public const int CV_HERMITE = 1;
27    public const int CV_POLYNOMIAL = 2;
28
29    /*
30     * ----------------------------------------
31     * CVODES return flags
32     * ----------------------------------------
33     */
34
35    public const int CV_SUCCESS = 0;
36    public const int CV_TSTOP_RETURN = 1;
37    public const int CV_ROOT_RETURN = 2;
38
39    public const int CV_WARNING = 99;
40
41    public const int CV_TOO_MUCH_WORK = -1;
42    public const int CV_TOO_MUCH_ACC = -2;
43    public const int CV_ERR_FAILURE = -3;
44    public const int CV_CONV_FAILURE = -4;
45
46    public const int CV_LINIT_FAIL = -5;
47    public const int CV_LSETUP_FAIL = -6;
48    public const int CV_LSOLVE_FAIL = -7;
49    public const int CV_RHSFUNC_FAIL = -8;
50    public const int CV_FIRST_RHSFUNC_ERR = -9;
51    public const int CV_REPTD_RHSFUNC_ERR = -10;
52    public const int CV_UNREC_RHSFUNC_ERR = -11;
53    public const int CV_RTFUNC_FAIL = -12;
54    public const int CV_CONSTR_FAIL = -13;
55
56    public const int CV_MEM_FAIL = -20;
57    public const int CV_MEM_NULL = -21;
58    public const int CV_ILL_INPUT = -22;
59    public const int CV_NO_MALLOC = -23;
60    public const int CV_BAD_K = -24;
61    public const int CV_BAD_T = -25;
62    public const int CV_BAD_DKY = -26;
63    public const int CV_TOO_CLOSE = -27;
64
65    public const int CV_NO_QUAD = -30;
66    public const int CV_QRHSFUNC_FAIL = -31;
67    public const int CV_FIRST_QRHSFUNC_ERR = -32;
68    public const int CV_REPTD_QRHSFUNC_ERR = -33;
69    public const int CV_UNREC_QRHSFUNC_ERR = -34;
70
71    public const int CV_NO_SENS = -40;
72    public const int CV_SRHSFUNC_FAIL = -41;
73    public const int CV_FIRST_SRHSFUNC_ERR = -42;
74    public const int CV_REPTD_SRHSFUNC_ERR = -43;
75    public const int CV_UNREC_SRHSFUNC_ERR = -44;
76
77    public const int CV_BAD_IS = -45;
78
79    public const int CV_NO_QUADSENS = -50;
80    public const int CV_QSRHSFUNC_FAIL = -51;
81    public const int CV_FIRST_QSRHSFUNC_ERR = -52;
82    public const int CV_REPTD_QSRHSFUNC_ERR = -53;
83    public const int CV_UNREC_QSRHSFUNC_ERR = -54;
84
85    /*
86     * ----------------------------------------
87     * CVODEA return flags
88     * ----------------------------------------
89     */
90
91    public const int CV_NO_ADJ = -101;
92    public const int CV_NO_FWD = -102;
93    public const int CV_NO_BCK = -103;
94    public const int CV_BAD_TB0 = -104;
95    public const int CV_REIFWD_FAIL = -105;
96    public const int CV_FWD_FAIL = -106;
97
98
99    public const int CV_GETY_BADT = -107;
100
101    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
102    public delegate int CVRhsFunc(
103        double t, // realtype
104        IntPtr y, // N_Vector
105        IntPtr ydot, // N_Vector
106        IntPtr user_data
107      );
108
109    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
110    public delegate int CVRhsFuncB(
111        double t, // realtype
112        IntPtr y, // N_Vector
113        IntPtr yB, // N_Vector
114        IntPtr yBdot, // N_Vector
115        IntPtr user_data
116      );
117
118    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
119    public delegate int CVDlsJacFunc(
120      double t,
121      IntPtr y, // N_Vector
122      IntPtr fy, // N_Vector
123      IntPtr Jac, // SUNMatrix
124      IntPtr user_data,
125      IntPtr tmp1, // N_Vector
126      IntPtr tmp2, // N_Vector
127      IntPtr tmp3 // N_Vector
128      );
129
130    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
131    public delegate int CVDlsJacFuncB(
132      double t,
133      IntPtr y, // N_Vector
134      IntPtr yB, // N_Vector
135      IntPtr fyB, // N_Vector
136      IntPtr Jac, // SUNMatrix
137      IntPtr user_data,
138      IntPtr tmp1, // N_Vector
139      IntPtr tmp2, // N_Vector
140      IntPtr tmp3 // N_Vector
141    );
142
143    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
144    public delegate int CVQuadRhsFn(
145      double t,
146      IntPtr y, // N_Vector
147      IntPtr yQdot, // N_Vector
148      IntPtr user_data
149      );
150
151    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
152    public delegate int CVQuadRhsFnB(
153      double t,
154      IntPtr y, // N_Vector
155      IntPtr yB, // N_Vector
156      IntPtr qBdot, // N_Vector
157      IntPtr user_data
158      );
159
160
161    // to calculate sensitivities RHS for all equations at once
162    // must compute (∂f/∂y)s_i(t) + ∂f/∂p_i
163    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
164    public delegate int CVSensRhsFn(
165      int Ns,
166      double t,
167      IntPtr y, // N_Vector
168      IntPtr ydot, // N_Vector
169      IntPtr yS, // N_Vector* one vector for each parameter
170      IntPtr ySdot, // N_Vector* one vector for each parameter
171      IntPtr user_data,
172      IntPtr tmp1, // N_Vector
173      IntPtr tmp2 // N_Vector
174    );
175
176    // returns a void* to the cvodes memory block if successful otherwise NULL
177    public static IntPtr CVodeCreate(MultistepMethod lmm, NonlinearSolverIteration iter) {
178      if (Environment.Is64BitProcess) return CVodeCreate_x64(lmm, iter);
179      else return CVodeCreate_x86(lmm, iter);
180    }
181
182    public static int CVodeInit(
183      IntPtr cvode_mem, // pointer returned by CVodeCreate
184      CVRhsFunc f,
185      double t0, // realtype, the inital value of t
186      IntPtr y0 // N_Vector the initial value of y
187    ) {
188      if (Environment.Is64BitProcess) return CVodeInit_x64(cvode_mem, f, t0, y0);
189      else return CVodeInit_x86(cvode_mem, f, t0, y0);
190    }
191
192
193    public static int CVodeSStolerances(
194      IntPtr cvode_mem,
195      double reltol,
196      double abstol
197      ) {
198      if (Environment.Is64BitProcess) return CVodeSStolerances_x64(cvode_mem, reltol, abstol);
199      else return CVodeSStolerances_x86(cvode_mem, reltol, abstol);
200    }
201
202    public static int CVDlsSetLinearSolver(
203      IntPtr cvode_mem,
204      IntPtr linearSolver, // SUNLinearSolver
205      IntPtr j // SUNMatrix
206      ) {
207      if (Environment.Is64BitProcess) return CVDlsSetLinearSolver_x64(cvode_mem, linearSolver, j);
208      else return CVDlsSetLinearSolver_x86(cvode_mem, linearSolver, j);
209    }
210
211    public static int CVDlsSetJacFn(
212      IntPtr cvode_mem,
213      CVDlsJacFunc jacFunc
214      ) {
215      if (Environment.Is64BitProcess) return CVDlsSetJacFn_x64(cvode_mem, jacFunc);
216      else return CVDlsSetJacFn_x86(cvode_mem, jacFunc);
217    }
218
219    public static int CVodeSensInit(
220      IntPtr cvode_mem,
221      int Ns, // number of parameters
222      int ism, // sensitivity solution method, CV_SIMULTANEOUS or CV_STAGGERED
223      CVSensRhsFn fS, // right hand side function which computes all sensitivity RHS at the same time
224      IntPtr yS0 // N_Vector
225      ) {
226      if (Environment.Is64BitProcess) return CVodeSensInit_x64(cvode_mem, Ns, ism, fS, yS0);
227      else return CVodeSensInit_x86(cvode_mem, Ns, ism, fS, yS0);
228    }
229
230    public static int CVodeSensSStolerances(
231      IntPtr cvode_mem,
232      double reltol,
233      double[] abstol
234    ) {
235      if (Environment.Is64BitProcess) return CVodeSensSStolerances_x64(cvode_mem, reltol, abstol);
236      else return CVodeSensSStolerances_x86(cvode_mem, reltol, abstol);
237    }
238
239    /* Call CVodeSensEEtolerances to estimate tolerances for sensitivity
240   variables based on the rolerances supplied for states variables and
241   the scaling factor pbar */
242    public static int CVodeSensEEtolerances(
243      IntPtr cvode_mem
244      ) {
245      if (Environment.Is64BitProcess) return CVodeSensEEtolerances_x64(cvode_mem);
246      else return CVodeSensEEtolerances_x86(cvode_mem);
247    }
248
249    public static int CVodeGetSens(
250      IntPtr cvode_mem,
251      ref double tret,
252      IntPtr yS //N_Vector*, one vector for each parameter
253      ) {
254      if (Environment.Is64BitProcess) return CVodeGetSens_x64(cvode_mem, ref tret, yS);
255      else return CVodeGetSens_x86(cvode_mem, ref tret, yS);
256    }
257
258    public static int CVodeQuadInit(
259      IntPtr cvode_mem,
260      CVQuadRhsFn qF,
261      IntPtr yQ0 // N_Vector, initial value of yQ
262      ) {
263      if (Environment.Is64BitProcess) return CVodeQuadInit_x64(cvode_mem, qF, yQ0);
264      else return CVodeQuadInit_x86(cvode_mem, qF, yQ0);
265    }
266
267    public static int CVodeQuadInitB(
268      IntPtr cvode_mem,
269      int indexB,
270      CVQuadRhsFnB rhsQB,
271      IntPtr yQB0 // N_Vector, initial value of yQB
272    ) {
273      if (Environment.Is64BitProcess) return CVodeQuadInitB_x64(cvode_mem, indexB, rhsQB, yQB0);
274      else return CVodeQuadInitB_x86(cvode_mem, indexB, rhsQB, yQB0);
275    }
276
277
278    public static int CVodeAdjInit(
279      IntPtr cvode_mem,
280      int nd,  // integration steps between checkpoints
281      int interpType // either CV_POLYNOMIAL or CV_HERMITE
282      ) {
283      if (Environment.Is64BitProcess) return CVodeAdjInit_x64(cvode_mem, nd, interpType);
284      else return CVodeAdjInit_x86(cvode_mem, nd, interpType);
285    }
286
287    public static int CVodeF(
288        IntPtr cvode_mem,
289        double t_out, // the next time at which a computed solution is desired
290        IntPtr y, // N_Vector, the solution vector y
291        ref double t_ret, // the time reached by the solver (output)
292        int itask, // CV_NORMAL or CV_ONE_STEP
293        ref int ncheck  // the number of internal checkpoints stored so far.
294      ) {
295      if (Environment.Is64BitProcess) return CVodeF_x64(cvode_mem, t_out, y, ref t_ret, itask, ref ncheck);
296      else return CVodeF_x86(cvode_mem, t_out, y, ref t_ret, itask, ref ncheck);
297    }
298
299    public static int CVodeGetNumSteps(
300      IntPtr cvode_mem,
301      ref long ncheck  // the number of steps taken
302    ) {
303      if (Environment.Is64BitProcess) return CVodeGetNumSteps_x64(cvode_mem, ref ncheck);
304      else return CVodeGetNumSteps_x86(cvode_mem, ref ncheck);
305    }
306
307    public static int CVodeCreateB(
308        IntPtr cvode_mem,
309        MultistepMethod lmmB,
310        NonlinearSolverIteration iterB,
311        ref int which
312      ) {
313      if (Environment.Is64BitProcess) return CVodeCreateB_x64(cvode_mem, lmmB, iterB, ref which);
314      else return CVodeCreateB_x86(cvode_mem, lmmB, iterB, ref which);
315    }
316
317    public static int CVodeInitB(
318        IntPtr cvode_mem,
319        int which,
320        CVRhsFuncB rightHandSide,
321        double tB0, // endpoint T where final conditions are provided (equal to endpoint of forward integration)
322        IntPtr yB0 // N_Vector inital value at t=tb0 of backward solution
323      ) {
324      if (Environment.Is64BitProcess) return CVodeInitB_x64(cvode_mem, which, rightHandSide, tB0, yB0);
325      else return CVodeInitB_x86(cvode_mem, which, rightHandSide, tB0, yB0);
326    }
327
328    public static int CVode(
329      IntPtr cvode_mem,
330      double tout, // next time at which a solution is desired
331      IntPtr yout, // N_Vector
332      ref double tret, // the time reached by the solver (output)
333      int itask // flag indicating the job of the solver for the next step.
334      ) {
335      if (Environment.Is64BitProcess) return CVode_x64(cvode_mem, tout, yout, ref tret, itask);
336      else return CVode_x86(cvode_mem, tout, yout, ref tret, itask);
337    }
338
339
340    public static int CVodeSStolerancesB(
341      IntPtr cvode_mem,
342      int which,
343      double reltol,
344      double abstol
345    ) {
346      if (Environment.Is64BitProcess) return CVodeSStolerancesB_x64(cvode_mem, which, reltol, abstol);
347      else return CVodeSStolerancesB_x86(cvode_mem, which, reltol, abstol);
348    }
349
350    public static int CVDlsSetLinearSolverB(
351      IntPtr cvode_mem,
352      int which,
353      IntPtr linearSolver, // SUNLinearSolver
354      IntPtr j // SUNMatrix
355    ) {
356      if (Environment.Is64BitProcess) return CVDlsSetLinearSolverB_x64(cvode_mem, which, linearSolver, j);
357      else return CVDlsSetLinearSolverB_x86(cvode_mem, which, linearSolver, j);
358    }
359
360    public static int CVDlsSetJacFnB(
361      IntPtr cvode_mem,
362      int which,
363      CVDlsJacFuncB jacFunc
364    ) {
365      if (Environment.Is64BitProcess) return CVDlsSetJacFnB_x64(cvode_mem, which, jacFunc);
366      else return CVDlsSetJacFnB_x86(cvode_mem, which, jacFunc);
367    }
368
369    public static int CVodeB(
370      IntPtr cvode_mem,
371      double tout, // next time at which a solution is desired
372      int itask // flag indicating the job of the solver for the next step.
373    ) {
374      if (Environment.Is64BitProcess) return CVodeB_x64(cvode_mem, tout, itask);
375      else return CVodeB_x86(cvode_mem, tout, itask);
376    }
377
378    public static int CVodeGetB(
379          IntPtr cvode_mem,
380          int which,
381          ref double tret,
382          IntPtr yB // the backward solution at time tret
383        ) {
384      if (Environment.Is64BitProcess) return CVodeGetB_x64(cvode_mem, which, ref tret, yB);
385      else return CVodeGetB_x86(cvode_mem, which, ref tret, yB);
386    }
387    public static int CVodeGetAdjY(
388          IntPtr cvode_mem,
389          double t,
390          IntPtr y // the forward solution y(t)
391        ) {
392      if (Environment.Is64BitProcess) return CVodeGetAdjY_x64(cvode_mem, t, y);
393      else return CVodeGetAdjY_x86(cvode_mem, t, y);
394    }
395
396    public static IntPtr CVodeGetAdjCVodeBmem(
397              IntPtr cvode_mem,
398              int which
399            ) {
400      if (Environment.Is64BitProcess) return CVodeGetAdjCVodeBmem_x64(cvode_mem, which);
401      else return CVodeGetAdjCVodeBmem_x86(cvode_mem, which);
402    }
403
404    public static int CVodeGetQuadB(
405        IntPtr cvode_mem,
406        int which,
407        ref double tret,
408        IntPtr yQB // N_Vector
409      ) {
410      if (Environment.Is64BitProcess) return CVodeGetQuadB_x64(cvode_mem, which, ref tret, yQB);
411      else return CVodeGetQuadB_x86(cvode_mem, which, ref tret, yQB);
412    }
413
414    public static int CVodeFree(ref IntPtr cvode_mem) {
415      if (Environment.Is64BitProcess) return CVodeFree_x64(ref cvode_mem);
416      else return CVodeFree_x86(ref cvode_mem);
417    }
418
419    #region matrix
420    public static IntPtr SUNDenseMatrix(long m, long n) {
421      if (Environment.Is64BitProcess) return SUNDenseMatrix_x64(m, n);
422      else return SUNDenseMatrix_x86(m, n);
423    }
424
425    public static void SUNMatDestroy(IntPtr A) {
426      if (Environment.Is64BitProcess) SUNMatDestroy_x64(A);
427      else SUNMatDestroy_x86(A);
428    }
429
430    public unsafe static double* SUNDenseMatrix_Data(IntPtr matrix) {
431      if (Environment.Is64BitProcess) return SUNDenseMatrix_Data_x64(matrix);
432      else return SUNDenseMatrix_Data_x86(matrix);
433    }
434
435    public static long SUNDenseMatrix_Cols(IntPtr matrix) {
436      if (Environment.Is64BitProcess) return SUNDenseMatrix_Cols_x64(matrix);
437      else return SUNDenseMatrix_Cols_x86(matrix);
438    }
439
440    public static long SUNDenseMatrix_Rows(IntPtr matrix) {
441      if (Environment.Is64BitProcess) return SUNDenseMatrix_Rows_x64(matrix);
442      else return SUNDenseMatrix_Rows_x86(matrix);
443    }
444
445    public unsafe static double SUNDenseMatrix_Get(IntPtr mat, long i, long j) {
446      if (Environment.Is64BitProcess) {
447        long M = SUNDenseMatrix_Rows_x64(mat);
448        // the(i, j)th element of A(with 0 <= i<M and 0 <= j<N) is given by(A->data)[j*M+i]
449        return SUNDenseMatrix_Data_x64(mat)[j * M + i];
450      } else {
451        long M = SUNDenseMatrix_Rows_x86(mat);
452        return SUNDenseMatrix_Data_x86(mat)[j * M + i];
453      }
454    }
455
456    public unsafe static void SUNDenseMatrix_Set(IntPtr mat, long i, long j, double val) {
457      if (Environment.Is64BitProcess) {
458        long M = SUNDenseMatrix_Rows_x64(mat);
459        // the(i, j)th element of A(with 0 <= i<M and 0 <= j<N) is given by(A->data)[j*M+i]
460        SUNDenseMatrix_Data_x64(mat)[j * M + i] = val;
461      } else {
462        long M = SUNDenseMatrix_Rows_x86(mat);
463        // the(i, j)th element of A(with 0 <= i<M and 0 <= j<N) is given by(A->data)[j*M+i]
464        SUNDenseMatrix_Data_x86(mat)[j * M + i] = val;
465      }
466    }
467
468    #endregion
469
470    #region linear solver
471    public static IntPtr SUNDenseLinearSolver(
472      IntPtr y, // N_Vector
473      IntPtr A // SUNMatrix
474      ) {
475      if (Environment.Is64BitProcess) return SUNDenseLinearSolver_x64(y, A);
476      else return SUNDenseLinearSolver_x86(y, A);
477    }
478
479    public static int SUNLinSolInitialize(IntPtr linearSolver) {
480      if (Environment.Is64BitProcess) return SUNLinSolInitialize_x64(linearSolver);
481      else return SUNLinSolInitialize_x86(linearSolver);
482    }
483
484    public static int SUNLinSolSetup(
485      IntPtr linearSolver,
486      IntPtr A // SUNMatrix
487      ) {
488      if (Environment.Is64BitProcess) return SUNLinSolSetup_x64(linearSolver, A);
489      else return SUNLinSolSetup_x86(linearSolver, A);
490    }
491
492    public static int SUNLinSolSolve(
493      IntPtr linearSolver,
494      IntPtr A, // SUNMatrix
495      IntPtr x, // N_Vector
496      IntPtr b, // N_Vector
497      double tol
498      ) {
499      if (Environment.Is64BitProcess) return SUNLinSolSolve_x64(linearSolver, A, x, b, tol);
500      else return SUNLinSolSolve_x86(linearSolver, A, x, b, tol);
501    }
502
503    public static int SUNLinSolFree(IntPtr linearSolver) {
504      if (Environment.Is64BitProcess) return SUNLinSolFree_x64(linearSolver);
505      else return SUNLinSolFree_x86(linearSolver);
506    }
507
508    #endregion
509
510    #region N_Vector
511    public static IntPtr N_VNew_Serial(long vec_length) {
512      if (Environment.Is64BitProcess) return N_VNew_Serial_x64(vec_length);
513      else return N_VNew_Serial_x86(vec_length);
514    }
515
516    public static void N_VDestroy_Serial(IntPtr vec) {
517      if (Environment.Is64BitProcess) N_VDestroy_Serial_x64(vec);
518      else N_VDestroy_Serial_x86(vec);
519    }
520
521    public static void N_VDestroyVectorArray_Serial(IntPtr vecArr, int count) {
522      if (Environment.Is64BitProcess) N_VDestroyVectorArray_Serial_x64(vecArr, count);
523      else N_VDestroyVectorArray_Serial_x86(vecArr, count);
524    }
525
526    public static void N_VPrint_Serial(IntPtr vec) {
527      if (Environment.Is64BitProcess) N_VPrint_Serial_x64(vec);
528      else N_VPrint_Serial_x86(vec);
529    }
530
531    public static void N_VConst_Serial(double c, IntPtr vec) {
532      if (Environment.Is64BitProcess) N_VConst_Serial_x64(c, vec);
533      else N_VConst_Serial_x86(c, vec);
534    }
535
536
537    public static double N_VL1Norm_Serial(IntPtr vec) {
538      if (Environment.Is64BitProcess) return N_VL1Norm_Serial_x64(vec);
539      else return N_VL1Norm_Serial_x86(vec);
540    }
541
542    public static IntPtr N_VMake_Serial(long vec_length, double[] v_data) {
543      if (Environment.Is64BitProcess) return N_VMake_Serial_x64(vec_length, v_data);
544      else return N_VMake_Serial_x86(vec_length, v_data);
545    }
546
547    ///  Performs the operation z = c*x
548    public static void N_VScale(double s,
549      IntPtr x, // N_Vector
550      IntPtr z // N_Vector
551      ) {
552      if (Environment.Is64BitProcess) N_VScale_x64(s, x, z);
553      else N_VScale_x86(s, x, z);
554    }
555
556    public unsafe static double* N_VGetArrayPointer_Serial(IntPtr vec) {
557      if (Environment.Is64BitProcess) return N_VGetArrayPointer_Serial_x64(vec);
558      else return N_VGetArrayPointer_Serial_x86(vec);
559    }
560
561    public static IntPtr N_VCloneVectorArray_Serial(int count, IntPtr vec) {
562      if (Environment.Is64BitProcess) return N_VCloneVectorArray_Serial_x64(count, vec);
563      else return N_VCloneVectorArray_Serial_x86(count, vec);
564    }
565
566
567    /*
568#define NV_CONTENT_S(v)  ( (N_VectorContent_Serial)(v->content) )
569
570#define NV_LENGTH_S(v)   ( NV_CONTENT_S(v)->length )
571
572#define NV_OWN_DATA_S(v) ( NV_CONTENT_S(v)->own_data )
573
574#define NV_DATA_S(v)     ( NV_CONTENT_S(v)->data )
575
576#define NV_Ith_S(v,i)    ( NV_DATA_S(v)[i] )
577*/
578    // methods for macros
579    // the vector is a pointer to a struct
580    // the struct has following layout
581    // {
582    //    long length,
583    //    int  own_data,   // flag to store whether the memory for the vector is managed internally
584    //    double* data,    // pointer to the actual elements
585    // }
586
587    private unsafe static IntPtr NV_CONTENT_S(IntPtr v) {
588      if (Environment.Is64BitProcess) {
589        return new IntPtr(*(Int64*)v.ToPointer());
590      } else {
591        return new IntPtr(*(Int32*)v.ToPointer());
592      }
593    }
594
595    public unsafe static long NV_LENGTH_S(IntPtr v) {
596      long length = *(long*)NV_CONTENT_S(v).ToPointer();
597      return length;
598    }
599    public unsafe static bool NV_OWN_DATA_S(IntPtr v) {
600      var content = (int*)NV_CONTENT_S(v).ToPointer();
601      int own_data = *(content + 2);
602      return own_data > 0;
603    }
604    public unsafe static double* NV_DATA_S(IntPtr v) {
605      if (Environment.Is64BitProcess) {
606        var content = (long*)NV_CONTENT_S(v).ToPointer();
607        double* data = (double*)*(content + 2); // different alignment or sizeof(int)?
608        return data;
609      } else {
610        var content = (int*)NV_CONTENT_S(v).ToPointer();
611        double* data = (double*)*(content + 3);
612        return data;
613      }
614    }
615    public unsafe static double NV_Get_Ith_S(IntPtr v, long i) {
616      return NV_DATA_S(v)[i];
617    }
618    public unsafe static void NV_Set_Ith_S(IntPtr v, long i, double val) {
619      NV_DATA_S(v)[i] = val;
620    }
621    #endregion
622
623
624    #region x86
625
626    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeCreate", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
627    // returns a void* to the cvodes memory block if successful otherwise NULL
628    private static extern IntPtr CVodeCreate_x86(MultistepMethod lmm, NonlinearSolverIteration iter);
629
630    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeInit", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
631    private static extern int CVodeInit_x86(
632      IntPtr cvode_mem, // pointer returned by CVodeCreate
633      CVRhsFunc f,
634      double t0, // realtype, the inital value of t
635      IntPtr y0 // N_Vector the initial value of y
636    );
637
638
639    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeSStolerances", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
640    private static extern int CVodeSStolerances_x86(
641      IntPtr cvode_mem,
642      double reltol,
643      double abstol
644      );
645
646    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVDlsSetLinearSolver", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
647    private static extern int CVDlsSetLinearSolver_x86(
648      IntPtr cvode_mem,
649      IntPtr linearSolver, // SUNLinearSolver
650      IntPtr j // SUNMatrix
651      );
652
653    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVDlsSetJacFn", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
654    private static extern int CVDlsSetJacFn_x86(
655      IntPtr cvode_mem,
656      CVDlsJacFunc jacFunc
657      );
658
659    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeSensInit", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
660    private static extern int CVodeSensInit_x86(
661      IntPtr cvode_mem,
662      int Ns, // number of parameters
663      int ism, // sensitivity solution method, CV_SIMULTANEOUS or CV_STAGGERED
664      CVSensRhsFn fS, // right hand side function which computes all sensitivity RHS at the same time
665      IntPtr yS0 // N_Vector
666      );
667
668    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeSensSStolerances", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
669    private static extern int CVodeSensSStolerances_x86(
670      IntPtr cvode_mem,
671      double reltol,
672      double[] abstol
673    );
674
675    /* Call CVodeSensEEtolerances to estimate tolerances for sensitivity
676   variables based on the rolerances supplied for states variables and
677   the scaling factor pbar */
678    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeSensEEtolerances", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
679    private static extern int CVodeSensEEtolerances_x86(
680      IntPtr cvode_mem
681      );
682
683    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeGetSens", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
684    private static extern int CVodeGetSens_x86(
685      IntPtr cvode_mem,
686      ref double tret,
687      IntPtr yS //N_Vector*, one vector for each parameter
688      );
689
690    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeQuadInit", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
691    private static extern int CVodeQuadInit_x86(
692      IntPtr cvode_mem,
693      CVQuadRhsFn qF,
694      IntPtr yQ0 // N_Vector, initial value of yQ
695      );
696
697    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeQuadInitB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
698    private static extern int CVodeQuadInitB_x86(
699      IntPtr cvode_mem,
700      int indexB,
701      CVQuadRhsFnB rhsQB,
702      IntPtr yQB0 // N_Vector, initial value of yQB
703    );
704
705
706    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeAdjInit", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
707    private static extern int CVodeAdjInit_x86(
708      IntPtr cvode_mem,
709      int nd,  // integration steps between checkpoints
710      int interpType // either CV_POLYNOMIAL or CV_HERMITE
711      );
712
713    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeF", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
714    private static extern int CVodeF_x86(
715        IntPtr cvode_mem,
716        double t_out, // the next time at which a computed solution is desired
717        IntPtr y, // N_Vector, the solution vector y
718        ref double t_ret, // the time reached by the solver (output)
719        int itask, // CV_NORMAL or CV_ONE_STEP
720        ref int ncheck  // the number of internal checkpoints stored so far.
721      );
722
723    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeGetNumSteps", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
724    private static extern int CVodeGetNumSteps_x86(
725      IntPtr cvode_mem,
726      ref long ncheck  // the number of steps taken
727    );
728
729    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeCreateB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
730    private static extern int CVodeCreateB_x86(
731        IntPtr cvode_mem,
732        MultistepMethod lmmB,
733        NonlinearSolverIteration iterB,
734        ref int which
735      );
736
737    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeInitB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
738    private static extern int CVodeInitB_x86(
739        IntPtr cvode_mem,
740        int which,
741        CVRhsFuncB rightHandSide,
742        double tB0, // endpoint T where final conditions are provided (equal to endpoint of forward integration)
743        IntPtr yB0 // N_Vector inital value at t=tb0 of backward solution
744      );
745
746    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVode", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
747    private static extern int CVode_x86(
748      IntPtr cvode_mem,
749      double tout, // next time at which a solution is desired
750      IntPtr yout, // N_Vector
751      ref double tret, // the time reached by the solver (output)
752      int itask // flag indicating the job of the solver for the next step.
753      );
754
755
756    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeSStolerancesB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
757    private static extern int CVodeSStolerancesB_x86(
758      IntPtr cvode_mem,
759      int which,
760      double reltol,
761      double abstol
762    );
763
764    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVDlsSetLinearSolverB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
765    private static extern int CVDlsSetLinearSolverB_x86(
766      IntPtr cvode_mem,
767      int which,
768      IntPtr linearSolver, // SUNLinearSolver
769      IntPtr j // SUNMatrix
770    );
771
772    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVDlsSetJacFnB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
773    private static extern int CVDlsSetJacFnB_x86(
774      IntPtr cvode_mem,
775      int which,
776      CVDlsJacFuncB jacFunc
777    );
778
779    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
780    private static extern int CVodeB_x86(
781      IntPtr cvode_mem,
782      double tout, // next time at which a solution is desired
783      int itask // flag indicating the job of the solver for the next step.
784    );
785
786    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeGetB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
787    private static extern int CVodeGetB_x86(
788          IntPtr cvode_mem,
789          int which,
790          ref double tret,
791          IntPtr yB // the backward solution at time tret
792        );
793    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeGetAdjY", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
794    private static extern int CVodeGetAdjY_x86(
795          IntPtr cvode_mem,
796          double t,
797          IntPtr y // the forward solution y(t)
798        );
799
800    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeGetAdjCVodeBmem", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
801    private static extern IntPtr CVodeGetAdjCVodeBmem_x86(
802              IntPtr cvode_mem,
803              int which
804            );
805
806    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeGetQuadB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
807    private static extern int CVodeGetQuadB_x86(
808        IntPtr cvode_mem,
809        int which,
810        ref double tret,
811        IntPtr yQB // N_Vector
812      );
813
814    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "CVodeFree", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
815
816    private static extern int CVodeFree_x86(ref IntPtr cvode_mem);
817
818    #region matrix
819    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "SUNDenseMatrix", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
820    private static extern IntPtr SUNDenseMatrix_x86(long m, long n);
821
822    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "SUNMatDestroy", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
823    private static extern void SUNMatDestroy_x86(IntPtr A);
824
825    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "SUNDenseMatrix_Data", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
826    private unsafe static extern double* SUNDenseMatrix_Data_x86(IntPtr matrix);
827
828    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "SUNDenseMatrix_Cols", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
829    private static extern long SUNDenseMatrix_Cols_x86(IntPtr matrix);
830
831    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "SUNDenseMatrix_Rows", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
832    private static extern long SUNDenseMatrix_Rows_x86(IntPtr matrix);
833
834
835    #endregion
836
837    #region linear solver
838    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "SUNDenseLinearSolver", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
839    private static extern IntPtr SUNDenseLinearSolver_x86(
840      IntPtr y, // N_Vector
841      IntPtr A // SUNMatrix
842      );
843
844    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "SUNLinSolInitialize", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
845    private static extern int SUNLinSolInitialize_x86(IntPtr linearSolver);
846
847    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "SUNLinSolSetup", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
848    private static extern int SUNLinSolSetup_x86(
849      IntPtr linearSolver,
850      IntPtr A // SUNMatrix
851      );
852
853    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "SUNLinSolSolve", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
854    private static extern int SUNLinSolSolve_x86(
855      IntPtr linearSolver,
856      IntPtr A, // SUNMatrix
857      IntPtr x, // N_Vector
858      IntPtr b, // N_Vector
859      double tol
860      );
861
862    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "SUNLinSolFree", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
863    private static extern int SUNLinSolFree_x86(IntPtr linearSolver);
864
865    #endregion
866
867    #region N_Vector
868    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "N_VNew_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
869    private static extern IntPtr N_VNew_Serial_x86(long vec_length);
870
871    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "N_VDestroy_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
872    private static extern void N_VDestroy_Serial_x86(IntPtr vec);
873
874    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "N_VDestroyVectorArray_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
875    private static extern void N_VDestroyVectorArray_Serial_x86(IntPtr vecArr, int count);
876
877    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "N_VPrint_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
878    private static extern void N_VPrint_Serial_x86(IntPtr vec);
879
880    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "N_VConst_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
881    private static extern void N_VConst_Serial_x86(double c, IntPtr vec);
882
883
884    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "N_VL1Norm_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
885    private static extern double N_VL1Norm_Serial_x86(IntPtr vec);
886
887    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "N_VMake_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
888    private static extern IntPtr N_VMake_Serial_x86(long vec_length, double[] v_data);
889
890    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "N_VScale", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
891    ///  Performs the operation z = c*x
892    private static extern void N_VScale_x86(double s,
893      IntPtr x, // N_Vector
894      IntPtr z // N_Vector
895      );
896
897    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "N_VMake_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
898    private unsafe static extern double* N_VGetArrayPointer_Serial_x86(IntPtr vec);
899
900    [DllImport("sundials_cvodes-x86.dll", EntryPoint = "N_VCloneVectorArray_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
901    private static extern IntPtr N_VCloneVectorArray_Serial_x86(int count, IntPtr vec); // returns N_Vector* !
902
903    #endregion
904
905    #endregion
906
907    #region x64
908
909    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeCreate", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
910    // returns a void* to the cvodes memory block if successful otherwise NULL
911    private static extern IntPtr CVodeCreate_x64(MultistepMethod lmm, NonlinearSolverIteration iter);
912
913    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeInit", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
914    private static extern int CVodeInit_x64(
915      IntPtr cvode_mem, // pointer returned by CVodeCreate
916      CVRhsFunc f,
917      double t0, // realtype, the inital value of t
918      IntPtr y0 // N_Vector the initial value of y
919    );
920
921
922    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeSStolerances", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
923    private static extern int CVodeSStolerances_x64(
924      IntPtr cvode_mem,
925      double reltol,
926      double abstol
927      );
928
929    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVDlsSetLinearSolver", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
930    private static extern int CVDlsSetLinearSolver_x64(
931      IntPtr cvode_mem,
932      IntPtr linearSolver, // SUNLinearSolver
933      IntPtr j // SUNMatrix
934      );
935
936    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVDlsSetJacFn", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
937    private static extern int CVDlsSetJacFn_x64(
938      IntPtr cvode_mem,
939      CVDlsJacFunc jacFunc
940      );
941
942    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeSensInit", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
943    private static extern int CVodeSensInit_x64(
944      IntPtr cvode_mem,
945      int Ns, // number of parameters
946      int ism, // sensitivity solution method, CV_SIMULTANEOUS or CV_STAGGERED
947      CVSensRhsFn fS, // right hand side function which computes all sensitivity RHS at the same time
948      IntPtr yS0 // N_Vector
949      );
950
951    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeSensSStolerances", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
952    private static extern int CVodeSensSStolerances_x64(
953      IntPtr cvode_mem,
954      double reltol,
955      double[] abstol
956    );
957
958    /* Call CVodeSensEEtolerances to estimate tolerances for sensitivity
959   variables based on the rolerances supplied for states variables and
960   the scaling factor pbar */
961    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeSensEEtolerances", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
962    private static extern int CVodeSensEEtolerances_x64(
963      IntPtr cvode_mem
964      );
965
966    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeGetSens", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
967    private static extern int CVodeGetSens_x64(
968      IntPtr cvode_mem,
969      ref double tret,
970      IntPtr yS //N_Vector*, one vector for each parameter
971      );
972
973    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeQuadInit", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
974    private static extern int CVodeQuadInit_x64(
975      IntPtr cvode_mem,
976      CVQuadRhsFn qF,
977      IntPtr yQ0 // N_Vector, initial value of yQ
978      );
979
980    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeQuadInitB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
981    private static extern int CVodeQuadInitB_x64(
982      IntPtr cvode_mem,
983      int indexB,
984      CVQuadRhsFnB rhsQB,
985      IntPtr yQB0 // N_Vector, initial value of yQB
986    );
987
988
989    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeAdjInit", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
990    private static extern int CVodeAdjInit_x64(
991      IntPtr cvode_mem,
992      int nd,  // integration steps between checkpoints
993      int interpType // either CV_POLYNOMIAL or CV_HERMITE
994      );
995
996    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeF", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
997    private static extern int CVodeF_x64(
998        IntPtr cvode_mem,
999        double t_out, // the next time at which a computed solution is desired
1000        IntPtr y, // N_Vector, the solution vector y
1001        ref double t_ret, // the time reached by the solver (output)
1002        int itask, // CV_NORMAL or CV_ONE_STEP
1003        ref int ncheck  // the number of internal checkpoints stored so far.
1004      );
1005
1006    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeGetNumSteps", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1007    private static extern int CVodeGetNumSteps_x64(
1008      IntPtr cvode_mem,
1009      ref long ncheck  // the number of steps taken
1010    );
1011
1012    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeCreateB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1013    private static extern int CVodeCreateB_x64(
1014        IntPtr cvode_mem,
1015        MultistepMethod lmmB,
1016        NonlinearSolverIteration iterB,
1017        ref int which
1018      );
1019
1020    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeInitB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1021    private static extern int CVodeInitB_x64(
1022        IntPtr cvode_mem,
1023        int which,
1024        CVRhsFuncB rightHandSide,
1025        double tB0, // endpoint T where final conditions are provided (equal to endpoint of forward integration)
1026        IntPtr yB0 // N_Vector inital value at t=tb0 of backward solution
1027      );
1028
1029    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVode", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1030    private static extern int CVode_x64(
1031      IntPtr cvode_mem,
1032      double tout, // next time at which a solution is desired
1033      IntPtr yout, // N_Vector
1034      ref double tret, // the time reached by the solver (output)
1035      int itask // flag indicating the job of the solver for the next step.
1036      );
1037
1038
1039    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeSStolerancesB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1040    private static extern int CVodeSStolerancesB_x64(
1041      IntPtr cvode_mem,
1042      int which,
1043      double reltol,
1044      double abstol
1045    );
1046
1047    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVDlsSetLinearSolverB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1048    private static extern int CVDlsSetLinearSolverB_x64(
1049      IntPtr cvode_mem,
1050      int which,
1051      IntPtr linearSolver, // SUNLinearSolver
1052      IntPtr j // SUNMatrix
1053    );
1054
1055    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVDlsSetJacFnB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1056    private static extern int CVDlsSetJacFnB_x64(
1057      IntPtr cvode_mem,
1058      int which,
1059      CVDlsJacFuncB jacFunc
1060    );
1061
1062    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1063    private static extern int CVodeB_x64(
1064      IntPtr cvode_mem,
1065      double tout, // next time at which a solution is desired
1066      int itask // flag indicating the job of the solver for the next step.
1067    );
1068
1069    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeGetB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1070    private static extern int CVodeGetB_x64(
1071          IntPtr cvode_mem,
1072          int which,
1073          ref double tret,
1074          IntPtr yB // the backward solution at time tret
1075        );
1076    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeGetAdjY", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1077    private static extern int CVodeGetAdjY_x64(
1078          IntPtr cvode_mem,
1079          double t,
1080          IntPtr y // the forward solution y(t)
1081        );
1082
1083    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeGetAdjCVodeBmem", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1084    private static extern IntPtr CVodeGetAdjCVodeBmem_x64(
1085              IntPtr cvode_mem,
1086              int which
1087            );
1088
1089    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeGetQuadB", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1090    private static extern int CVodeGetQuadB_x64(
1091        IntPtr cvode_mem,
1092        int which,
1093        ref double tret,
1094        IntPtr yQB // N_Vector
1095      );
1096
1097    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "CVodeFree", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1098
1099    private static extern int CVodeFree_x64(ref IntPtr cvode_mem);
1100
1101    #region matrix
1102    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "SUNDenseMatrix", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1103    private static extern IntPtr SUNDenseMatrix_x64(long m, long n);
1104
1105    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "SUNMatDestroy", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1106    private static extern void SUNMatDestroy_x64(IntPtr A);
1107
1108    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "SUNDenseMatrix_Data", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1109    private unsafe static extern double* SUNDenseMatrix_Data_x64(IntPtr matrix);
1110
1111    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "SUNDenseMatrix_Cols", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1112    private static extern long SUNDenseMatrix_Cols_x64(IntPtr matrix);
1113
1114    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "SUNDenseMatrix_Rows", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1115    private static extern long SUNDenseMatrix_Rows_x64(IntPtr matrix);
1116
1117    #endregion
1118
1119    #region linear solver
1120    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "SUNDenseLinearSolver", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1121    private static extern IntPtr SUNDenseLinearSolver_x64(
1122      IntPtr y, // N_Vector
1123      IntPtr A // SUNMatrix
1124      );
1125
1126    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "SUNLinSolInitialize", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1127    private static extern int SUNLinSolInitialize_x64(IntPtr linearSolver);
1128
1129    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "SUNLinSolSetup", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1130    private static extern int SUNLinSolSetup_x64(
1131      IntPtr linearSolver,
1132      IntPtr A // SUNMatrix
1133      );
1134
1135    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "SUNLinSolSolve", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1136    private static extern int SUNLinSolSolve_x64(
1137      IntPtr linearSolver,
1138      IntPtr A, // SUNMatrix
1139      IntPtr x, // N_Vector
1140      IntPtr b, // N_Vector
1141      double tol
1142      );
1143
1144    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "SUNLinSolFree", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1145    private static extern int SUNLinSolFree_x64(IntPtr linearSolver);
1146
1147    #endregion
1148
1149    #region N_Vector
1150    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "N_VNew_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1151    private static extern IntPtr N_VNew_Serial_x64(long vec_length);
1152
1153    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "N_VDestroy_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1154    private static extern void N_VDestroy_Serial_x64(IntPtr vec);
1155
1156    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "N_VDestroyVectorArray_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1157    private static extern void N_VDestroyVectorArray_Serial_x64(IntPtr vecArr, int count);
1158
1159    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "N_VPrint_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1160    private static extern void N_VPrint_Serial_x64(IntPtr vec);
1161
1162    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "N_VConst_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1163    private static extern void N_VConst_Serial_x64(double c, IntPtr vec);
1164
1165
1166    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "N_VL1Norm_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1167    private static extern double N_VL1Norm_Serial_x64(IntPtr vec);
1168
1169    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "N_VMake_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1170    private static extern IntPtr N_VMake_Serial_x64(long vec_length, double[] v_data);
1171
1172    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "N_VScale", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1173    ///  Performs the operation z = c*x
1174    private static extern void N_VScale_x64(double s,
1175      IntPtr x, // N_Vector
1176      IntPtr z // N_Vector
1177      );
1178
1179    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "N_VMake_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1180    private unsafe static extern double* N_VGetArrayPointer_Serial_x64(IntPtr vec);
1181
1182    [DllImport("sundials_cvodes-x64.dll", EntryPoint = "N_VCloneVectorArray_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
1183    private static extern IntPtr N_VCloneVectorArray_Serial_x64(int count, IntPtr vec); // returns N_Vector* !
1184    #endregion
1185
1186    #endregion
1187  }
1188}
Note: See TracBrowser for help on using the repository browser.