Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16253


Ignore:
Timestamp:
10/24/18 18:16:43 (5 years ago)
Author:
gkronber
Message:

#2925: fixed memory leak and fixed bug in determination of integration time span

Location:
branches/2925_AutoDiffForDynamicalModels
Files:
3 edited

Legend:

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

    r16248 r16253  
    172172
    173173      CVODES.N_VDestroy_Serial(y);
    174       CVODES.CVodeFree(cvode_mem);
     174      CVODES.CVodeFree(ref cvode_mem);
    175175      CVODES.SUNLinSolFree(linearSolver);
    176176      CVODES.SUNMatDestroy(A);
     
    188188      CVODES.NV_Set_Ith_S(y, 1, 1);  // v
    189189
    190       var cvode_mem = CVODES.CVodeCreate(CVODES.MultistepMethod.CV_ADAMS, CVODES.NonlinearSolverIteration.CV_FUNCTIONAL);
     190      var cvode_mem = CVODES.CVodeCreate(CVODES.MultistepMethod.CV_BDF, CVODES.NonlinearSolverIteration.CV_NEWTON);
    191191
    192192      var flag = CVODES.CVodeInit(cvode_mem, F, 0.0, y);
     
    264264
    265265      CVODES.N_VDestroy_Serial(y);
    266       CVODES.CVodeFree(cvode_mem);
     266      CVODES.CVodeFree(ref cvode_mem);
    267267      CVODES.SUNLinSolFree(linearSolver);
    268268      CVODES.SUNMatDestroy(A);
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/CVODES.cs

    r16251 r16253  
    364364    [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeFree", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    365365
    366     public static extern int CVodeFree(IntPtr cvode_mem);
     366    public static extern int CVodeFree(ref IntPtr cvode_mem);
    367367
    368368    #region matrix
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Problem.cs

    r16251 r16253  
    537537
    538538    // for a solver with the necessary features see: https://computation.llnl.gov/projects/sundials/cvodes
    539     /*
    540      * SUNDIALS: SUite of Nonlinear and DIfferential/ALgebraic Equation Solvers
    541      * CVODES
    542      * CVODES is a solver for stiff and nonstiff ODE systems (initial value problem) given in explicit
    543      * form y’ = f(t,y,p) with sensitivity analysis capabilities (both forward and adjoint modes). CVODES
    544      * is a superset of CVODE and hence all options available to CVODE (with the exception of the FCVODE
    545      * interface module) are also available for CVODES. Both integration methods (Adams-Moulton and BDF)
    546      * and the corresponding nonlinear iteration methods, as well as all linear solver and preconditioner
    547      * modules, are available for the integration of the original ODEs, the sensitivity systems, or the
    548      * adjoint system. Depending on the number of model parameters and the number of functional outputs,
    549      * one of two sensitivity methods is more appropriate. The forward sensitivity analysis (FSA) method
    550      * is mostly suitable when the gradients of many outputs (for example the entire solution vector) with
    551      * respect to relatively few parameters are needed. In this approach, the model is differentiated with
    552      * respect to each parameter in turn to yield an additional system of the same size as the original
    553      * one, the result of which is the solution sensitivity. The gradient of any output function depending
    554      * on the solution can then be directly obtained from these sensitivities by applying the chain rule
    555      * of differentiation. The adjoint sensitivity analysis (ASA) method is more practical than the
    556      * forward approach when the number of parameters is large and the gradients of only few output
    557      * functionals are needed. In this approach, the solution sensitivities need not be computed
    558      * explicitly. Instead, for each output functional of interest, an additional system, adjoint to the
    559      * original one, is formed and solved. The solution of the adjoint system can then be used to evaluate
    560      * the gradient of the output functional with respect to any set of model parameters. The FSA module
    561      * in CVODES implements a simultaneous corrector method as well as two flavors of staggered corrector
    562      * methods–for the case when sensitivity right hand sides are generated all at once or separated for
    563      * each model parameter. The ASA module provides the infrastructure required for the backward
    564      * integration in time of systems of differential equations dependent on the solution of the original
    565      * ODEs. It employs a checkpointing scheme for efficient interpolation of forward solutions during the
    566      * backward integration.
    567      */
     539
    568540    private static IEnumerable<Tuple<double, Vector>[]> Integrate(
    569541      ISymbolicExpressionTree[] trees, IDataset dataset, string[] inputVariables, string[] targetVariables, string[] latentVariables, IEnumerable<IntRange> episodes,
     
    596568        var calculatedVariables = targetVariables.Concat(latentVariables).ToArray(); // TODO: must conincide with the order of trees in the encoding
    597569
     570        var prevT = rows.First(); // TODO: here we should use a variable for t if it is available. Right now we assume equidistant measurements.
    598571        foreach (var t in rows.Skip(1)) {
    599572          if (odeSolver == "HeuristicLab")
    600573            IntegrateHL(trees, calculatedVariables, variableValues, parameterValues, numericIntegrationSteps);
    601574          else if (odeSolver == "CVODES")
    602             IntegrateCVODES(trees, calculatedVariables, variableValues, parameterValues, t);
     575            IntegrateCVODES(trees, calculatedVariables, variableValues, parameterValues, t - prevT);
    603576          else throw new InvalidOperationException("Unknown ODE solver " + odeSolver);
     577          prevT = t;
    604578
    605579          if (variableValues.Count == targetVariables.Length) {
     
    623597    }
    624598
     599
     600    /// <summary>
     601    ///  Here we use CVODES to solve the ODE. Forward sensitivities are used to calculate the gradient for parameter optimization
     602    /// </summary>
     603    /// <param name="trees">Each equation in the ODE represented as a tree</param>
     604    /// <param name="calculatedVariables">The names of the calculated variables</param>
     605    /// <param name="variableValues">The start values of the calculated variables as well as their sensitivites over parameters</param>
     606    /// <param name="parameterValues">The current parameter values</param>
     607    /// <param name="t">The time t up to which we need to integrate.</param>
    625608    private static void IntegrateCVODES(
    626609      ISymbolicExpressionTree[] trees, // f(y,p) in tree representation
     
    725708      } finally {
    726709        if (y != IntPtr.Zero) CVODES.N_VDestroy_Serial(y);
    727         if (cvode_mem != IntPtr.Zero) CVODES.CVodeFree(cvode_mem);
     710        if (cvode_mem != IntPtr.Zero) CVODES.CVodeFree(ref cvode_mem);
    728711        if (linearSolver != IntPtr.Zero) CVODES.SUNLinSolFree(linearSolver);
    729712        if (A != IntPtr.Zero) CVODES.SUNMatDestroy(A);
Note: See TracChangeset for help on using the changeset viewer.