Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/19/18 16:05:20 (6 years ago)
Author:
gkronber
Message:

#2925: working example with forward sensitivity analysis

File:
1 edited

Legend:

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

    r16245 r16246  
    223223    public const int CV_REIFWD_FAIL = -105;
    224224    public const int CV_FWD_FAIL = -106;
     225
     226
    225227    public const int CV_GETY_BADT = -107;
    226228
     
    284286      );
    285287
     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
    286304    [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeCreate", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    287305    // returns a void* to the cvodes memory block if successful otherwise NULL
     
    289307
    290308    [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeInit", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    291 
    292309    public static extern int CVodeInit(
    293310      IntPtr cvode_mem, // pointer returned by CVodeCreate
     
    316333      IntPtr cvode_mem,
    317334      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
    318366      );
    319367
     
    452500    [DllImport("sundials_cvodes.dll", EntryPoint = "SUNMatDestroy", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    453501    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
    454525    #endregion
    455526
     
    515586
    516587    [DllImport("sundials_cvodes.dll", EntryPoint = "N_VCloneVectorArray_Serial", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    517     public static extern IntPtr N_VCloneVectorArray_Serial(int count, IntPtr vec);
     588    public static extern IntPtr N_VCloneVectorArray_Serial(int count, IntPtr vec); // returns N_Vector* !
     589
    518590
    519591    /*
Note: See TracChangeset for help on using the changeset viewer.