Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/10/18 11:22:43 (6 years ago)
Author:
gkronber
Message:

#2925: first working version of CVODES integration

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Problem.cs

    r16222 r16225  
    2424using System.Diagnostics;
    2525using System.Linq;
     26using System.Runtime.InteropServices;
    2627using HeuristicLab.Analysis;
    2728using HeuristicLab.Collections;
     
    3940
    4041namespace HeuristicLab.Problems.DynamicalSystemsModelling {
     42
    4143  public class Vector {
    4244    public readonly static Vector Zero = new Vector(new double[0]);
     
    126128  [StorableClass]
    127129  public sealed class Problem : SingleObjectiveBasicProblem<MultiEncoding>, IRegressionProblem, IProblemInstanceConsumer<IRegressionProblemData>, IProblemInstanceExporter<IRegressionProblemData> {
     130
     131    // CVODES types
     132    public enum MultistepMethod : int { CV_ADAMS = 1, CV_BDF = 2 };
     133    public enum NonlinearSolverIteration : int { CV_NEWTON = 1, CV_FUNCTIONAL = 2 };
     134
     135
     136    /* itask */
     137    public const int CV_NORMAL = 1;
     138    public const int CV_ONE_STEP = 2;
     139
     140    /* ism */
     141    public const int CV_SIMULTANEOUS = 1;
     142    public const int CV_STAGGERED = 2;
     143    public const int CV_STAGGERED1 = 3;
     144
     145    /* DQtype */
     146    public const int CV_CENTERED = 1;
     147    public const int CV_FORWARD = 2;
     148
     149    /* interp */
     150    public const int CV_HERMITE = 1;
     151    public const int CV_POLYNOMIAL = 2;
     152
     153    /*
     154     * ----------------------------------------
     155     * CVODES return flags
     156     * ----------------------------------------
     157     */
     158
     159    public const int CV_SUCCESS = 0;
     160    public const int CV_TSTOP_RETURN = 1;
     161    public const int CV_ROOT_RETURN = 2;
     162
     163    public const int CV_WARNING = 99;
     164
     165    public const int CV_TOO_MUCH_WORK = -1;
     166    public const int CV_TOO_MUCH_ACC = -2;
     167    public const int CV_ERR_FAILURE = -3;
     168    public const int CV_CONV_FAILURE = -4;
     169
     170    public const int CV_LINIT_FAIL = -5;
     171    public const int CV_LSETUP_FAIL = -6;
     172    public const int CV_LSOLVE_FAIL = -7;
     173    public const int CV_RHSFUNC_FAIL = -8;
     174    public const int CV_FIRST_RHSFUNC_ERR = -9;
     175    public const int CV_REPTD_RHSFUNC_ERR = -10;
     176    public const int CV_UNREC_RHSFUNC_ERR = -11;
     177    public const int CV_RTFUNC_FAIL = -12;
     178    public const int CV_CONSTR_FAIL = -13;
     179
     180    public const int CV_MEM_FAIL = -20;
     181    public const int CV_MEM_NULL = -21;
     182    public const int CV_ILL_INPUT = -22;
     183    public const int CV_NO_MALLOC = -23;
     184    public const int CV_BAD_K = -24;
     185    public const int CV_BAD_T = -25;
     186    public const int CV_BAD_DKY = -26;
     187    public const int CV_TOO_CLOSE = -27;
     188
     189    public const int CV_NO_QUAD = -30;
     190    public const int CV_QRHSFUNC_FAIL = -31;
     191    public const int CV_FIRST_QRHSFUNC_ERR = -32;
     192    public const int CV_REPTD_QRHSFUNC_ERR = -33;
     193    public const int CV_UNREC_QRHSFUNC_ERR = -34;
     194
     195    public const int CV_NO_SENS = -40;
     196    public const int CV_SRHSFUNC_FAIL = -41;
     197    public const int CV_FIRST_SRHSFUNC_ERR = -42;
     198    public const int CV_REPTD_SRHSFUNC_ERR = -43;
     199    public const int CV_UNREC_SRHSFUNC_ERR = -44;
     200
     201    public const int CV_BAD_IS = -45;
     202
     203    public const int CV_NO_QUADSENS = -50;
     204    public const int CV_QSRHSFUNC_FAIL = -51;
     205    public const int CV_FIRST_QSRHSFUNC_ERR = -52;
     206    public const int CV_REPTD_QSRHSFUNC_ERR = -53;
     207    public const int CV_UNREC_QSRHSFUNC_ERR = -54;
     208
     209    /*
     210     * ----------------------------------------
     211     * CVODEA return flags
     212     * ----------------------------------------
     213     */
     214
     215    public const int CV_NO_ADJ = -101;
     216    public const int CV_NO_FWD = -102;
     217    public const int CV_NO_BCK = -103;
     218    public const int CV_BAD_TB0 = -104;
     219    public const int CV_REIFWD_FAIL = -105;
     220    public const int CV_FWD_FAIL = -106;
     221    public const int CV_GETY_BADT = -107;
     222
     223    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
     224    public delegate int CVRhsFunc(
     225        double t, // realtype
     226        IntPtr y, // N_Vector
     227        IntPtr ydot, // N_Vector
     228        IntPtr user_data
     229      );
     230
     231    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
     232    public delegate int CVDlsJacFunc(
     233      double t,
     234      IntPtr y, // N_Vector
     235      IntPtr fy, // N_Vector
     236      IntPtr Jac, // SUNMatrix
     237      IntPtr user_data,
     238      IntPtr tmp1, // N_Vector
     239      IntPtr tmp2, // N_Vector
     240      IntPtr tmp3 // N_Vector
     241      );
     242
     243
     244    [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeCreate", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     245    // returns a void* to the cvodes memory block if successful otherwise NULL
     246    public static extern IntPtr CVodeCreate(MultistepMethod lmm, NonlinearSolverIteration iter);
     247
     248    [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeInit", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     249
     250    public static extern int CVodeInit(
     251      IntPtr cvode_mem, // pointer returned by CVodeCreate
     252      CVRhsFunc f,
     253      double t0, // realtype, the inital value of t
     254      IntPtr y0 // N_Vector the initial value of y
     255    );
     256
     257
     258    [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeSStolerances", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     259    public static extern int CVodeSStolerances(
     260      IntPtr cvode_mem,
     261      double reltol,
     262      double abstol
     263      );
     264
     265    [DllImport("sundials_cvodes.dll", EntryPoint = "CVDlsSetLinearSolver", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     266    public static extern int CVDlsSetLinearSolver(
     267      IntPtr cvode_mem,
     268      IntPtr linearSolver, // SUNLinearSolver
     269      IntPtr j // SUNMatrix
     270      );
     271
     272    [DllImport("sundials_cvodes.dll", EntryPoint = "CVDlsSetJacFn", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     273    public static extern int CVDlsSetJacFn(
     274      IntPtr cvode_mem,
     275      CVDlsJacFunc jacFunc
     276      );
     277
     278    [DllImport("sundials_cvodes.dll", EntryPoint = "CVode", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     279    public static extern int CVode(
     280      IntPtr cvode_mem,
     281      double tout, // next time at which a solution is desired
     282      IntPtr yout, // N_Vector
     283      ref double tret, // the time reached by the solver (output)
     284      int itask // flag indicating the job of the solver for the next step.
     285      );
     286
     287    [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeFree", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     288
     289    public static extern int CVodeFree(IntPtr cvode_mem);
     290
     291    #region matrix
     292    [DllImport("sundials_cvodes.dll", EntryPoint = "SUNDenseMatrix", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     293    public static extern IntPtr SUNDenseMatrix(long m, long n);
     294
     295    [DllImport("sundials_cvodes.dll", EntryPoint = "SUNMatDestroy", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     296    public static extern void SUNMatDestroy(IntPtr A);
     297    #endregion
     298
     299    #region linear solver
     300    [DllImport("sundials_cvodes.dll", EntryPoint = "SUNDenseLinearSolver", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     301    public static extern IntPtr SUNDenseLinearSolver(
     302      IntPtr y, // N_Vector
     303      IntPtr A // SUNMatrix
     304      );
     305
     306    [DllImport("sundials_cvodes.dll", EntryPoint = "SUNLinSolInitialize", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     307    public static extern int SUNLinSolInitialize(IntPtr linearSolver);
     308
     309    [DllImport("sundials_cvodes.dll", EntryPoint = "SUNLinSolSetup", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     310    public static extern int SUNLinSolSetup(
     311      IntPtr linearSolver,
     312      IntPtr A // SUNMatrix
     313      );
     314
     315    [DllImport("sundials_cvodes.dll", EntryPoint = "SUNLinSolSolve", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     316    public static extern int SUNLinSolSolve(
     317      IntPtr linearSolver,
     318      IntPtr A, // SUNMatrix
     319      IntPtr x, // N_Vector
     320      IntPtr b, // N_Vector
     321      double tol
     322      );
     323
     324    [DllImport("sundials_cvodes.dll", EntryPoint = "SUNLinSolFree", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     325    public static extern int SUNLinSolFree(IntPtr linearSolver);
     326
     327    #endregion
     328
     329    #region N_Vector
     330    [DllImport("sundials_cvodes.dll", EntryPoint = "N_VNew_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     331    public static extern IntPtr N_VNew_Serial(long vec_length);
     332
     333    [DllImport("sundials_cvodes.dll", EntryPoint = "N_VDestroy_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     334    public static extern void N_VDestroy_Serial(IntPtr vec);
     335
     336    [DllImport("sundials_cvodes.dll", EntryPoint = "N_VPrint_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     337    public static extern void N_VPrint_Serial(IntPtr vec);
     338
     339    [DllImport("sundials_cvodes.dll", EntryPoint = "N_VConst_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     340    public static extern void N_VConst_Serial(double c, IntPtr vec);
     341
     342   
     343    [DllImport("sundials_cvodes.dll", EntryPoint = "N_VL1Norm_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     344    public static extern double N_VL1Norm_Serial(IntPtr vec);
     345
     346    [DllImport("sundials_cvodes.dll", EntryPoint = "N_VMake_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     347    public static extern IntPtr N_VMake_Serial(long vec_length, double[] v_data);
     348
     349    [DllImport("sundials_cvodes.dll", EntryPoint = "N_VScale", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     350    ///  Performs the operation z = c*x
     351    public static extern void N_VScale(double s,
     352      IntPtr x, // N_Vector
     353      IntPtr z // N_Vector
     354      );
     355
     356    [DllImport("sundials_cvodes.dll", EntryPoint = "N_VMake_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
     357    public unsafe static extern double* N_VGetArrayPointer_Serial(IntPtr vec);
     358    #endregion
     359
     360
     361
     362
    128363
    129364    #region parameter names
Note: See TracChangeset for help on using the changeset viewer.