Sundials.jl

repository·master·Indexed 42 days ago

https://github.com/sciml/sundials.jl

A Julia interface to the SUNDIALS suite of nonlinear and differential/algebraic equation solvers. It provides high-performance tools for solving ODEs, DAEs, and nonlinear algebraic systems via a high-level common interface for the JuliaDiffEq ecosystem and a direct C-style API. Supported solvers include CVODE (CVODE_Adams, CVODE_BDF), ARKODE, IDA, and KINSOL.

Tokens
2.3K
Snippets
6
Records
10
Agent score
53%

What's inside Sundials.jl

  1. Overview of Sundials.jl algorithms

    master

    Sundials.jl provides SciML common-interface wrappers for the SUNDIALS C library. It includes support for the following algorithms:

    • CVODE: For solving initial value problems (IVPs) for ordinary differential equations.
    • ARKODE: For solving additive Runge-Kutta methods.
    • IDA: For solving initial value problems for differential-algebraic equations (DAEs).
    • KINSOL: For solving nonlinear systems of equations.
  2. Overview of Sundials components

    master

    Sundials.jl provides interfaces to several specialized solvers:

    • CVODES: Integration and sensitivity analysis of ODEs (y' = f(t,y,p)).
    • ARKStep: Integration of non-stiff, stiff, and mixed-mode ODEs via Runge-Kutta methods (My' = f_E(t,y,p) + f_i(t,y,p)).
    • ERKStep: Integration of non-stiff, stiff, and mixed-mode ODEs via Runge-Kutta methods (y' = f(t,y,p)).
    • IDAS: Integration and sensitivity analysis of DAEs (F(t,y,y',p) = 0).
    • KINSOL: Solution of nonlinear algebraic systems (F(u) = 0).

    Note: CVODES and IDAS include the functionality of CVODE and IDA (integration without sensitivity analysis).

  3. Use Sundials.jl with the Common Interface API

    master

    Sundials.jl implements the JuliaDiffEq common interface, making it compatible with DifferentialEquations.jl. You can use Sundials solvers like CVODE_Adams, CVODE_BDF, and ARKODE to solve ODEProblems. For DAEs, use the IDA solver. ARKODE can also be used with SplitODEProblems for IMEX (Implicit-Explicit) forms.

    Available ODE methods include:

    • CVODE_Adams
    • CVODE_BDF
    • ARKODE

    Available DAE methods include:

    • IDA
    using Sundials
    
    function lorenz(du,u,p,t)
     du[1] = 10.0(u[2]-u[1])
     du[2] = u[1]*(28.0-u[3]) - u[2]
     du[3] = u[1]*u[2] - (8/3)*u[3]
    end
    
    u0 = [1.0;0.0;0.0]
    tspan = (0.0,100.0)
    prob = ODEProblem(lorenz,u0,tspan)
    sol = solve(prob,CVODE_Adams())
    
    using Plots; plot(sol,vars=(1,2,3))
  4. Use reexported SciML common interface with Sundials

    master

    When you run using Sundials, the package automatically imports the necessary components of the SciML common interface. You do not need to import SciMLBase or DiffEqBase separately to use the following core types and functions:

    Problems and Functions

    • Problems: ODEProblem, SplitODEProblem, DAEProblem, SteadyStateProblem, NonlinearProblem, EnsembleProblem.
    • Functions: ODEFunction, SplitFunction, DAEFunction, NonlinearFunction.

    Solutions and Solving

    • Solutions: ODESolution, DAESolution, NonlinearSolution, SteadyStateSolution, EnsembleSolution, EnsembleSummary.
    • Solving Functions: solve, solve!, init, step!, remake.

    Integrator Interface

    Methods for interacting with an active integrator:

    • reinit!, u_modified!, add_tstop!, add_saveat!, set_proposed_dt!, get_du, get_du!, get_tmp_cache, savevalues!, change_t_via_interpolation!, reeval_internals_due_to_modification!, check_error, check_error!, terminate!.

    Callbacks and Initialization

    • Callbacks: ContinuousCallback, DiscreteCallback, VectorContinuousCallback, CallbackSet.
    • Initialization Algorithms: DefaultInit, BrownFullBasicInit, ShampineCollocationInit, NoInit, CheckInit, OverrideInit.

    Ensembles and Status

    • Ensemble Algorithms: EnsembleSerial, EnsembleThreads, EnsembleDistributed, EnsembleSplitThreads, and the EnsembleAnalysis module.
    • Return Status: ReturnCode, successful_retretcode.
    • Other: NullParameters.

    Note: If you require features from SciMLBase that are not listed here, you must import SciMLBase directly.

    using Sundials
    
    # You can immediately use ODEProblem and solve without extra imports
    prob = ODEProblem(f, u0, tspan)
    sol = solve(prob, CVODE_BDF())
  5. Install Sundials.jl

    master

    Install Sundials.jl using the Julia package manager. After installation, you can verify the installation by running the package tests.

    using Pkg
    Pkg.add("Sundials")
    
    # To test the installation
    Pkg.test("Sundials")
  6. Solve ODE problems using CVODE

    master

    Sundials.jl reexports the SciML common interface (ODEProblem, solve). You can solve an ordinary differential equation (ODE) by defining a function for the derivatives, creating an ODEProblem, and passing a Sundials solver like CVODE_Adams() to the solve function.

    using Sundials
    
    function lorenz!(du, u, p, t)
        du[1] = 10.0 * (u[2] - u[1])
        du[2] = u[1] * (28.0 - u[3]) - u[2]
        du[3] = u[1] * u[2] - (8 / 3) * u[3]
        return nothing
    end
    
    prob = ODEProblem(lorenz!, [1.0, 0.0, 0.0], (0.0, 100.0))
    sol = solve(prob, CVODE_Adams())
    sol.retcode
  7. Solve nonlinear algebraic systems with KINSOL High Level API

    master

    The kinsol function provides a high-level interface to the KINSOL nonlinear solver to find roots for systems of the form F(u) = 0.

    ```julia
    kinsol(f, y0::Vector{Float64};
                    userdata::Any = nothing,
                    linear_solver=:Dense, 
                    jac_upper=0, 
                    jac_lower=0)

    Parameters:

    • f(res, y): An in-place function that computes the residual. It should satisfy f(y) - res = 0.
    • y0: Initial guess as a Vector{Float64}.
    • userdata: Optional user data passed to the function.
    • linear_solver: The linear solver to use. :Dense is default. Use :Band for problems with a banded Jacobian (requires specifying jac_upper and jac_lower).
    • jac_upper: Number of upper bands for the Jacobian.
    • jac_lower: Number of lower bands for the Jacobian.

    Note: For general nonlinear solving, NLsolve.jl is recommended as it is more robust than KINSOL's techniques.

  8. Use the Sundials Direct API

    master

    Sundials.jl provides a direct wrapper around the Sundials C API using Clang.jl. This provides almost full coverage of the serial Sundials library.

    Important usage notes for the Direct API:

    • No Exports: Nothing is exported from the Sundials module. You must prefix all calls with Sundials. (e.g., Sundials.some_function()).
    • C-style Documentation: Since this is a direct wrap, refer to the official Sundials C documentation for function signatures and behavior.
    • Limitations:
      • Macros like DENSE_ELEM are not available.
      • Parallel versions of Sundials requiring different N_Vector types are not wrapped.
    • Data Types: Many functions support passing Julia Arrays instead of explicit Sundials N_Vector objects.
  9. Access reexported SciML interface types and helpers

    master

    Sundials reexports several specific SciML interface types and helper functions to ensure compatibility with the broader SciML ecosystem. These are primarily used for building custom integrators or inspecting solver state:

    • DECallback: The common callback interface used by SciML integrators.
    • DEIntegrator: The common differential-equation integrator interface.
    • DEStats: The standard container for differential-equation solver statistics.
    • addsteps!: Used to add accepted steps to an integrator's solution history.
    • last_step_failed: A query to determine if the most recent integrator step failed.
  10. Available Sundials algorithms

    master

    Sundials.jl provides access to several core algorithms for solving differential equations and nonlinear systems. These include:

    • ODE Algorithms: CVODE_BDF (Backward Differentiation Formula), CVODE_Adams (Adams-Moulton methods), and ARKODE (Additive Runge-Kutta).
    • DAE Algorithms: IDA (for Differential-Algebraic Equations).
    • Nonlinear/Steady-State Algorithms: KINSOL.
    • Generic Types: SundialsODEAlgorithm and SundialsDAEAlgorithm serve as base types for these algorithms.
    # Example algorithm identifiers
    CVODE_BDF
    CVODE_Adams
    ARKODE
    IDA
    KINSOL