### Install and Import ODEInterfaceDiffEq Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/ode_solve.md Install the ODEInterfaceDiffEq package and import it to use its solvers. ```julia import Pkg Pkg.add("ODEInterfaceDiffEq") import ODEInterfaceDiffEq ``` -------------------------------- ### Install and Import LSODA Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/ode_solve.md Install the LSODA package and import it to use the LSODA wrapper algorithm. ```julia import Pkg Pkg.add("LSODA") import LSODA ``` -------------------------------- ### Install ODEInterfaceDiffEq.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/api/odeinterfacediffeq.md Install and import the ODEInterfaceDiffEq.jl package to use its solvers. ```julia using Pkg Pkg.add("ODEInterfaceDiffEq") import ODEInterfaceDiffEq ``` -------------------------------- ### Install and Import IRKGaussLegendre Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/ode_solve.md Install the IRKGaussLegendre package and import it to use the IRKGL16 solver. ```julia import Pkg Pkg.add("IRKGaussLegendre") import IRKGaussLegendre ``` -------------------------------- ### Install SteadyStateDiffEq.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/api/steadystatediffeq.md Install the SteadyStateDiffEq.jl package using the Julia package manager. This is necessary for standalone usage. ```julia using Pkg Pkg.add("SteadyStateDiffEq") import SteadyStateDiffEq ``` -------------------------------- ### Install and Import ProbNumDiffEq.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/ode_solve.md Install the ProbNumDiffEq.jl package for probabilistic numerical solvers for ODEs and import it. ```julia import Pkg Pkg.add("ProbNumDiffEq") import ProbNumDiffEq ``` -------------------------------- ### Install BoundaryValueDiffEq.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/api/boundaryvaluediffeq.md Install the BoundaryValueDiffEq.jl package using the Julia package manager. This is necessary for standalone usage. ```julia using Pkg Pkg.add("BoundaryValueDiffEq") import BoundaryValueDiffEq ``` -------------------------------- ### Install and Import DelayDiffEq.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/api/delaydiffeq.md Install the DelayDiffEq.jl package using the Julia package manager and import it for use. This is typically done once. ```julia using Pkg Pkg.add("DelayDiffEq") import DelayDiffEq ``` -------------------------------- ### Install GeometricIntegratorsDiffEq.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/dynamical_solve.md To use the algorithms from GeometricIntegrators.jl, you need to install and import the GeometricIntegratorsDiffEq package. This is a separate installation from DifferentialEquations.jl. ```julia Pkg.clone("https://github.com/SciML/GeometricIntegratorsDiffEq.jl") import GeometricIntegratorsDiffEq ``` -------------------------------- ### Install and Import ODE.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/ode_solve.md Installs and imports the ODE.jl package, offering a collection of classical ODE solvers. ```julia import Pkg Pkg.add("ODE") import ODE ``` -------------------------------- ### ModelingToolkit DAE System Setup Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/features/dae_initialization.md Illustrates setting up a DAE system using ModelingToolkit, where initialization information is often handled automatically. Requires `ModelingToolkit`, `OrdinaryDiffEqBDF`, and `SciMLBase`. ```julia using ModelingToolkit using OrdinaryDiffEqBDF # DFBDF using SciMLBase # DAEProblem, solve @variables t x(t) y(t) T(t) @parameters g L D = Differential(t) eqs = [ D(x) ~ -T * x/L, D(y) ~ -T * y/L - g, x^2 + y^2 ~ L^2 ] @named pendulum = ODESystem(eqs, t, [x, y, T], [g, L]) sys = structural_simplify(pendulum) ``` -------------------------------- ### Example: Dynamics Equations with ArrayPartition Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/features/diffeq_arrays.md Demonstrates using ArrayPartition for initial conditions in dynamics equations, allowing for heterogeneous units and efficient type-stable updates. ```julia import Unitful, RecursiveArrayTools, DifferentialEquations as DE import LinearAlgebra r0 = [1131.340, -2282.343, 6672.423]Unitful.u"km" v0 = [-5.64305, 4.30333, 2.42879]Unitful.u"km/s" Δt = 86400.0 * 365Unitful.u"s" μ = 398600.4418Unitful.u"km^3/s^2" rv0 = RecursiveArrayTools.ArrayPartition(r0, v0) ``` ```julia function f(dy, y, μ, t) r = LinearAlgebra.norm(y.x[1]) dy.x[1] .= y.x[2] dy.x[2] .= -μ .* y.x[1] / r^3 end ``` ```julia prob = DE.ODEProblem(f, rv0, (0.0Unitful.u"s", Δt), μ) sol = DE.solve(prob, DE.Vern8()) ``` -------------------------------- ### Install DiffEqCallbacks.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/api/diffeqcallbacks.md Install the DiffEqCallbacks.jl package using the Julia package manager. This is required for standalone usage. ```julia using Pkg Pkg.add("DiffEqCallbacks") import DiffEqCallbacks ``` -------------------------------- ### AutoTsit5 with Rodas5 Switching Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/ode_solve.md Example of using AutoTsit5 with Rodas5 as the stiff solver, demonstrating default and custom non-stiff tolerance settings. ```julia tsidas_alg = AutoTsit5(Rodas5()) sol = solve(prob, tsidas_alg) tsidas_alg = AutoTsit5(Rodas5(), nonstifftol = 11 / 10) ``` -------------------------------- ### Example: Controlling Integration with `advance_to_tstop` Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/basics/integrator.md Demonstrates how to use the `advance_to_tstop` option to make the integrator step directly to the next specified time stop. This is useful for targeted simulation segments. ```julia integrator = DE.init(prob, DE.Tsit5(); dt = 1 // 2^(4), tstops = [0.5], advance_to_tstop = true) for i in integrator @test integrator.t ∈ [0.5, 1.0] end ``` -------------------------------- ### Install and Import NeuralPDE.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/ode_solve.md Install the NeuralPDE.jl package for using neural networks to approximate ODE solutions and import it. ```julia import Pkg Pkg.add("NeuralPDE") import NeuralPDE ``` -------------------------------- ### Install and Import Sundials.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/api/sundials.md Installs the Sundials package and imports it for use. This is a prerequisite for utilizing Sundials solvers. ```julia using Pkg Pkg.add("Sundials") import Sundials ``` -------------------------------- ### Install DifferentialEquations.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/index.md Use this command to install the DifferentialEquations package within the Julia REPL. ```julia import Pkg Pkg.add("DifferentialEquations") ``` -------------------------------- ### Install and Import QuDiffEq.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/ode_solve.md Install the QuDiffEq.jl package for solving differential equations using quantum algorithms and import it into your Julia session. ```julia import Pkg Pkg.add(url = "https://github.com/QuantumBFS/QuDiffEq.jl") import QuDiffEq ``` -------------------------------- ### Install and Use SimpleDiffEq.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/api/simplediffeq.md Install the SimpleDiffEq.jl package and import it for use in your Julia project. This is a prerequisite for accessing its simplified ODE and SDE solvers. ```julia using Pkg Pkg.add("SimpleDiffEq") import SimpleDiffEq ``` -------------------------------- ### Install and Import SciPyDiffEq.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/ode_solve.md Installs and imports the SciPyDiffEq.jl package, a wrapper for SciPy ODE solvers. Useful for transitioning users and benchmarking, though significantly slower than pure-Julia methods. ```julia import Pkg Pkg.add(url = "https://github.com/SciML/SciPyDiffEq.jl") import SciPyDiffEq ``` -------------------------------- ### Example Usage of ImplicitDeuflhardExtrapolation Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/ode_solve.md Demonstrates how to instantiate and use the ImplicitDeuflhardExtrapolation solver with custom arguments for maximum order, minimum order, initial order, and the step-number sequence. ```julia alg = ImplicitDeuflhardExtrapolation(max_order = 7, min_order = 4, init_order = 4, sequence = :bulirsch) solve(prob, alg) ``` -------------------------------- ### Install and Import Plots.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/basics/plot.md Before plotting, you need to install and import the Plots.jl package. This snippet shows the necessary commands. ```julia #]add Plots # You need to install Plots.jl before your first time using it! import Plots Plots.plot(sol) # Plots the solution ``` -------------------------------- ### Install and Import LSODA.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/api/lsoda.md To use the LSODA algorithm, you must first install and import the LSODA.jl package. This is done using the Julia package manager. ```julia using Pkg Pkg.add("LSODA") import LSODA ``` -------------------------------- ### Install Julia packages for diffeqpy Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/index.md Run this command within a Python interpreter to install the necessary Julia packages for diffeqpy. ```python >>> import diffeqpy >>> diffeqpy.install() ``` -------------------------------- ### Double Pendulum Simulation Setup Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/examples/classical_physics.md Defines the differential equations for a double pendulum system and sets up the ODEProblem. Requires OrdinaryDiffEq and Plots packages. ```julia #Double Pendulum Problem import OrdinaryDiffEq as ODE, Plots #Constants and setup const m₁, m₂, L₁, L₂ = 1, 2, 1, 2 initial = [0, π / 3, 0, 3pi / 5] tspan = (0.0, 50.0) #Convenience function for transforming from polar to Cartesian coordinates function polar2cart(sol; dt = 0.02, l1 = L₁, l2 = L₂, vars = (2, 4)) u = sol.t[1]:dt:sol.t[end] p1 = l1 * map(x -> x[vars[1]], sol.(u)) p2 = l2 * map(y -> y[vars[2]], sol.(u)) x1 = l1 * sin.(p1) y1 = l1 * -cos.(p1) (u, (x1 + l2 * sin.(p2), y1 - l2 * cos.(p2))) end #Define the Problem function double_pendulum(xdot, x, p, t) xdot[1] = x[2] xdot[2] = -((g * (2 * m₁ + m₂) * sin(x[1]) + m₂ * (g * sin(x[1] - 2 * x[3]) + 2 * (L₂ * x[4]^2 + L₁ * x[2]^2 * cos(x[1] - x[3])) * sin(x[1] - x[3]))) / (2 * L₁ * (m₁ + m₂ - m₂ * cos(x[1] - x[3])^2))) xdot[3] = x[4] xdot[4] = (((m₁ + m₂) * (L₁ * x[2]^2 + g * cos(x[1])) + L₂ * m₂ * x[4]^2 * cos(x[1] - x[3])) * sin(x[1] - x[3])) / (L₂ * (m₁ + m₂ - m₂ * cos(x[1] - x[3])^2)) end #Pass to Solvers double_pendulum_problem = ODE.ODEProblem(double_pendulum, initial, tspan) sol = ODE.solve(double_pendulum_problem, ODE.Vern7(), abstol = 1e-10, dt = 0.05); ``` -------------------------------- ### Install and Import DASSL.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/api/dassl.md To use DASSL.jl algorithms, you must first install and import the package. This is typically done using the Julia package manager. ```julia using Pkg Pkg.add("DASSL") import DASSL ``` -------------------------------- ### Install and Import ODE Tableaus Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/ode_solve.md Install packages for explicit and implicit ODE tableaus and import them for use in custom solver configurations. ```julia import Pkg Pkg.add(["OrdinaryDiffEqExplicitTableaus", "OrdinaryDiffEqImplicitTableaus"]) using OrdinaryDiffEqExplicitTableaus, OrdinaryDiffEqImplicitTableaus ``` -------------------------------- ### Configure ExtrapolationMidpointDeuflhard Solver Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/ode_solve.md Example of configuring the ExtrapolationMidpointDeuflhard solver with custom parameters for order, sequence, and threading. This method is suitable for non-stiff ODEs. ```julia alg = ExtrapolationMidpointDeuflhard(max_order = 7, min_order = 4, init_order = 4, sequence = :bulirsch, threading = false) solve(prob, alg) ``` -------------------------------- ### Shooting Method with ODE Solver Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/bvp_solve.md Example of configuring a Shooting method for BVPs, requiring an inner ODE solver like Tsit5. ```julia Shooting(Tsit5()) ``` -------------------------------- ### Hénon-Heiles System Setup Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/examples/classical_physics.md Sets up and solves the Hénon-Heiles system using a standard ODE solver. Defines the potential, total energy, and the system's differential equations. ```julia import OrdinaryDiffEq as ODE, Plots #Setup initial = [0.0, 0.1, 0.5, 0] tspan = (0, 100.0) #Remember, V is the potential of the system and T is the Total Kinetic Energy, thus E will #the total energy of the system. V(x, y) = 1 // 2 * (x^2 + y^2 + 2x^2 * y - 2 // 3 * y^3) E(x, y, dx, dy) = V(x, y) + 1 // 2 * (dx^2 + dy^2); #Define the function function Hénon_Heiles(du, u, p, t) x, y, dx, dy = u du[1] = dx du[2] = dy du[3] = -x - 2x * y du[4] = y^2 - y - x^2 end #Pass to solvers prob = ODE.ODEProblem(Hénon_Heiles, initial, tspan) sol = ODE.solve(prob, ODE.Vern9(), abstol = 1e-16, reltol = 1e-16); ``` -------------------------------- ### Install and Import BridgeDiffEq.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/sde_solve.md Clone the BridgeDiffEq.jl repository and import it to use its fixed-timestep SDE algorithms. This package is not included by default with DifferentialEquations.jl. ```julia Pkg.clone("https://github.com/SciML/BridgeDiffEq.jl") import BridgeDiffEq ``` -------------------------------- ### RODE Solver Usage with RandomEM Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/rode_solve.md Example of how to use the RandomEM solver for RODE problems. Ensure StochasticDiffEq is loaded directly for v8 and later. ```julia using StochasticDiffEq # RODEProblem, RandomEM sol = solve(prob, RandomEM(), dt = 1 / 100) ``` -------------------------------- ### Multiple Shooting Method with ODE Solver Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/bvp_solve.md Example of configuring a Multiple Shooting method for BVPs, specifying the number of intervals and an inner ODE solver. ```julia MultipleShooting(5, FBDF()) ``` -------------------------------- ### Specify KrylovJL_GMRES with Preconditioner Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/features/linear_nonlinear.md This example demonstrates how to set up the TRBDF2 solver with KrylovJL_GMRES as the linear solver, and a custom preconditioner applied via the `precs` argument. ```julia using LinearSolve # KrylovJL_GMRES using OrdinaryDiffEqSDIRK: TRBDF2 alg = TRBDF2(linsolve = KrylovJL_GMRES(precs = mypreconditioner)) ``` -------------------------------- ### Solve ODE with Tsit5 Algorithm Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/ode_solve.md Example of solving an ODE problem using the Tsit5 explicit Runge-Kutta method. This is a common starting point for many ODE problems. ```julia alg = Tsit5() solve(prob, alg) ``` -------------------------------- ### DAE Initialization with CheckInit Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/features/dae_initialization.md Recommends using CheckInit() to verify initial conditions are consistent for DAEs with DFBDF (OrdinaryDiffEq) or IDA (Sundials) solvers. ```julia using OrdinaryDiffEqBDF # DFBDF # or using Sundials # IDA # RECOMMENDED: Verify conditions are consistent sol = solve(prob, DFBDF(), initializealg = CheckInit()) # OrdinaryDiffEq sol = solve(prob, IDA(), initializealg = CheckInit()) # Sundials ``` -------------------------------- ### Define Heston SDE Problem with Correlated Noise Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/tutorials/sde_example.md This example constructs the SDE problem for the Heston model using the previously defined drift and diffusion functions, initial conditions, time span, and the `CorrelatedWienerProcess`. This setup is essential for simulating correlated stochastic processes. ```julia SDE.SDEProblem(f!, g!, ones(2), tspan, noise = heston_noise) ``` -------------------------------- ### Using `init` with Verbosity Settings Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/features/verbosity.md Demonstrates how to apply verbosity settings, including algorithm switching and linear solver details, when initializing an integrator for manual stepping. ```julia verbose = DEVerbosity( alg_switch = SciMLLogging.InfoLevel(), linear_verbosity = SciMLLogging.Detailed() ) integrator = init(prob, Rosenbrock23(), verbose = verbose) # Step through the solution step!(integrator) ``` -------------------------------- ### Install diffeqr from R Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/index.md Install the diffeqr package from CRAN to use DifferentialEquations.jl from R. A working Julia installation is required. ```r install.packages("diffeqr") ``` -------------------------------- ### Install diffeqpy from Python Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/index.md Install the diffeqpy module using pip to use DifferentialEquations.jl from Python. Ensure Julia is installed and in your PATH. ```python pip install diffeqpy ``` -------------------------------- ### Install and Import MATLABDiffEq.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/ode_solve.md Installs and imports the MATLABDiffEq.jl package, which wraps MATLAB's ODE solvers. Requires a licensed MATLAB installation. ```julia import Pkg Pkg.add(url = "https://github.com/SciML/MATLABDiffEq.jl") import MATLABDiffEq ``` -------------------------------- ### Importing and Solving a Sample DDE Problem Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/types/dde_types.md Demonstrates how to add the DiffEqProblemLibrary, import necessary modules, load a sample DDE problem, and solve it using a specific DDE solver. This is useful for testing and understanding DDE solver capabilities. ```julia #] add DiffEqProblemLibrary import DiffEqProblemLibrary.DDEProblemLibrary import DelayDiffEq as DDE, DifferentialEquations as DE # load problems prob = DDEProblemLibrary.prob_dde_constant_1delay_ip sol = solve(prob, DDE.MethodOfSteps(DE.Tsit5())) ``` -------------------------------- ### DAE Consistency Check with CheckInit Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/features/dae_initialization.md Shows how to verify user-computed consistent initial conditions for a DAE problem using `CheckInit`. This is the recommended alternative to `NoInit` for safety. Requires `OrdinaryDiffEqBDF` and `SciMLBase`. ```julia # If you've computed consistent conditions yourself u0_consistent = [1.0, 0.0, 0.0] du0_consistent = [0.0, -1.0, compute_tension(u0_consistent, p)] prob2 = DAEProblem(pendulum!, du0_consistent, u0_consistent, tspan, p, differential_vars = [true, true, false]) # RECOMMENDED: Verify they're consistent with CheckInit sol = solve(prob2, DFBDF(), initializealg = CheckInit()) # NOT RECOMMENDED: NoInit skips all checks - use at your own risk! # sol = solve(prob2, DFBDF(), initializealg = NoInit()) # ⚠️ DANGEROUS ``` -------------------------------- ### Verify Initial Conditions with CheckInit Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/tutorials/dae_example.md Use `CheckInit` to verify if the initial conditions for a DAE problem are consistent. This will error if inconsistencies are found. ```julia # This will verify initial conditions and error if they're inconsistent sol_check = DE.solve(prob, Sundials.IDA(), initializealg = DiffEqBase.CheckInit()) ``` -------------------------------- ### DAE Initialization with NoInit (Not Recommended) Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/features/dae_initialization.md Demonstrates skipping all initialization checks with NoInit() for DFBDF (OrdinaryDiffEq) or IDA (Sundials) solvers. Use with extreme caution. ```julia # NOT RECOMMENDED: Skip all initialization checks (dangerous!) # sol = solve(prob, DFBDF(), initializealg = NoInit()) # ⚠️ USE AT YOUR OWN RISK # sol = solve(prob, IDA(), initializealg = NoInit()) # ⚠️ USE AT YOUR OWN RISK ``` -------------------------------- ### Define and Solve a Sample SDE Problem Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/types/sde_types.md Demonstrates how to add `DiffEqProblemLibrary`, import it, load a sample SDE problem, and solve it using `StochasticDiffEq.jl`. ```julia #] add DiffEqProblemLibrary import DiffEqProblemLibrary.SDEProblemLibrary import StochasticDiffEq as SDE # load problems SDEProblemLibrary.importsdeproblems() prob = SDEProblemLibrary.prob_sde_linear sol = SDE.solve(prob) ``` -------------------------------- ### Setup ILU Preconditioner for Sundials Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/tutorials/advanced_ode_example.md Defines the `psetup` function for the Incomplete LU (ILU) preconditioner. This function computes the `W = I - gamma*J` matrix and builds the ILU preconditioner on it. It's used when `jok` is true to indicate Jacobian reuse. ```julia import LinearAlgebra u0 = prob_ode_brusselator_2d_mtk.u0 p = prob_ode_brusselator_2d_mtk.p const jaccache = prob_ode_brusselator_2d_mtk.f.jac(u0, p, 0.0) const W = LinearAlgebra.I - 1.0 * jaccache prectmp = IncompleteLU.ilu(W, τ = 50.0) const preccache = Ref(prectmp) function psetupilu(p, t, u, du, jok, jcurPtr, gamma) if jok prob_ode_brusselator_2d_mtk.f.jac(jaccache, u, p, t) jcurPtr[] = true # W = I - gamma*J @. W = -gamma * jaccache idxs = LinearAlgebra.diagind(W) @. @view(W[idxs]) = @view(W[idxs]) + 1 # Build preconditioner on W preccache[] = IncompleteLU.ilu(W, τ = 5.0) end end ``` -------------------------------- ### StochasticCompositeAlgorithm Example Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/sde_solve.md Demonstrates how to create a composite algorithm that switches between different SDE solvers based on a custom choice function. The choice function can utilize integrator properties like time step size. ```julia choice_function(integrator) = (Int(integrator.dt < 0.001) + 1) alg_switch = StochasticCompositeAlgorithm((EM(), RKMil()), choice_function) ``` -------------------------------- ### Explicitly Use Brown's Initialization Algorithm Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/tutorials/dae_example.md Use `BrownFullBasicInit` to explicitly set Brown's initialization algorithm for solving DAE problems. ```julia sol = DE.solve(prob, Sundials.IDA(), initializealg = DiffEqBase.BrownFullBasicInit()) ``` -------------------------------- ### Set Up DDE Problem Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/tutorials/dde_example.md Initializes a `DDEProblem` object with the defined model function, initial conditions, history function, time span, parameters, and constant lags. This prepares the problem for solving. ```julia p0 = 0.2; q0 = 0.3; v0 = 1; d0 = 5; p1 = 0.2; q1 = 0.3; v1 = 1; d1 = 1; d2 = 1; beta0 = 1; beta1 = 1; p = (p0, q0, v0, d0, p1, q1, v1, d1, d2, beta0, beta1, tau) tspan = (0.0, 10.0) u0 = [1.0, 1.0, 1.0] prob = DDE.DDEProblem(bc_model, u0, h, tspan, p; constant_lags = lags) ``` -------------------------------- ### Install diffeqr master branch from R Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/index.md Install the master branch of the diffeqr package from GitHub for development purposes. Requires devtools. ```r devtools::install_github('SciML/diffeqr', build_vignettes=T) ``` -------------------------------- ### Install Numba for performance Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/index.md Install Numba using pip to improve the performance of your Python code by JIT compiling derivative functions. ```python pip install numba ``` -------------------------------- ### Setup Algebraic Multigrid Preconditioner for Sundials Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/tutorials/advanced_ode_example.md Defines the `psetup` function for the Algebraic Multigrid (AMG) preconditioner. This function computes `W = I - gamma*J` and then builds the AMG preconditioner on `W` using Ruge-Stuben algorithm with Jacobi smoothers. ```julia prectmp2 = AlgebraicMultigrid.aspreconditioner(AlgebraicMultigrid.ruge_stuben(W, presmoother = AlgebraicMultigrid.Jacobi(rand(size(W, 1))), postsmoother = AlgebraicMultigrid.Jacobi(rand(size(W, 1))))) const preccache2 = Ref(prectmp2) function psetupamg(p, t, u, du, jok, jcurPtr, gamma) if jok prob_ode_brusselator_2d_mtk.f.jac(jaccache, u, p, t) jcurPtr[] = true # W = I - gamma*J @. W = -gamma * jaccache idxs = LinearAlgebra.diagind(W) @. @view(W[idxs]) = @view(W[idxs]) + 1 # Build preconditioner on W preccache2[] = AlgebraicMultigrid.aspreconditioner(AlgebraicMultigrid.ruge_stuben( W, presmoother = AlgebraicMultigrid.Jacobi(rand(size(W, 1))), postsmoother = AlgebraicMultigrid.Jacobi(rand(size(W, 1))))) end end ``` -------------------------------- ### Show Complete Dependency Overview Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/index.md Provides a comprehensive list of all dependencies and their versions, including transitive dependencies, by using the manifest mode. This is crucial for full reproducibility. ```julia import Pkg # hide Pkg.status(; mode = Pkg.PKGMODE_MANIFEST) # hide ``` -------------------------------- ### Create SDE Problem Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/features/ensemble.md Initializes an SDEProblem with drift, noise functions, initial conditions, time span, and parameters. ```julia import DifferentialEquations as DE import StochasticDiffEq as SDE # SDEProblem, SRIW1 p = [1.5, 1.0, 0.1, 0.1] prob = SDE.SDEProblem(f, g, [1.0, 1.0], (0.0, 10.0), p) ``` -------------------------------- ### Instantiate and Use AutoAbstol DiscreteCallback Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/features/callback_functions.md Demonstrates how to instantiate the custom `AutoAbstol` callback and use it with an ODEProblem. It shows how to step through the integration and observe the change in absolute tolerance. ```julia import DifferentialEquations as DE import OrdinaryDiffEqLowOrderRK as ODELow # BS3 cb = AutoAbstol(true; init_curmax = 1e-6) ``` ```julia function g(u, p, t) -u[1] end u0 = 10.0 const V = 1 prob = DE.ODEProblem(g, u0, (0.0, 10.0)) integrator = DE.init(prob, ODELow.BS3(), callback = cb) at1 = integrator.opts.abstol DE.step!(integrator) at2 = integrator.opts.abstol at1 <= at2 ``` ```julia DE.step!(integrator) at3 = integrator.opts.abstol at2 <= at3 ``` -------------------------------- ### Install TaylorIntegration.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/ode_solve.md Install the TaylorIntegration.jl package for adaptive order Taylor series methods. This package is not included by default with DifferentialEquations.jl and is optimized for very low absolute tolerances. ```julia import Pkg Pkg.add("TaylorIntegration") import TaylorIntegration ``` -------------------------------- ### Install BridgeDiffEq.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/ode_solve.md Install the BridgeDiffEq.jl package to utilize fixed timestep algorithms optimized for out-of-place functions on immutable types. These methods are not automatically included with DifferentialEquations.jl. ```julia import Pkg Pkg.add(url = "https://github.com/SciML/BridgeDiffEq.jl") import BridgeDiffEq ``` -------------------------------- ### Install deSolveDiffEq.jl Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/ode_solve.md Install the deSolveDiffEq.jl package to use R's deSolve methods within Julia. This wrapper provides compatibility for users transitioning from R. ```julia import Pkg Pkg.add(url = "https://github.com/SciML/deSolveDiffEq.jl") import deSolveDiffEq ``` -------------------------------- ### DAE Initialization with ShampineCollocationInit Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/features/dae_initialization.md Applies Shampine's collocation method for general DAE initialization with DFBDF (OrdinaryDiffEq) or IDA (Sundials) solvers. ```julia using OrdinaryDiffEqBDF # DFBDF # or using Sundials # IDA # Use Shampine's collocation method for general DAEs sol = solve(prob, DFBDF(), initializealg = ShampineCollocationInit()) # OrdinaryDiffEq sol = solve(prob, IDA(), initializealg = ShampineCollocationInit()) # Sundials ``` -------------------------------- ### Example: Stopping Integration at the Next `tstop` Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/basics/integrator.md Illustrates how to use `stop_at_next_tstop` to halt the integration precisely at the next time stop. This is useful for simulations that need to pause at specific critical points. ```julia integrator = DE.init(prob, DE.Tsit5(); dt = 1 // 2^(4), tstops = [0.5]) integrator.opts.stop_at_next_tstop = true solve!(integrator) ``` -------------------------------- ### Adding AlgebraicMultigrid with Jacobi Smoother Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/tutorials/advanced_ode_example.md This example demonstrates using AlgebraicMultigrid with a Jacobi smoother for both pre- and post-smoothing. It's a variation of the previous AlgebraicMultigrid example, offering a different smoothing strategy for the preconditioner. ```julia function algebraicmultigrid2(W, p) A = convert(AbstractMatrix, W) Pl = AlgebraicMultigrid.aspreconditioner(AlgebraicMultigrid.ruge_stuben(A, presmoother = AlgebraicMultigrid.Jacobi(rand(size(A, 1))), postsmoother = AlgebraicMultigrid.Jacobi(rand(size(A, 1))))) Pl, nothing end BT.@btime DE.solve(prob_ode_brusselator_2d_sparse, ODESDIRK.KenCarp47(; linsolve = LS.KrylovJL_GMRES(precs = algebraicmultigrid2), concrete_jac = true); save_everystep = false); nothing # hide ``` -------------------------------- ### Define ODEProblem Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/features/ensemble.md Defines a basic ODE problem for linear growth starting at 0.5. ```julia prob = DE.ODEProblem((u, p, t) -> 1.01u, 0.5, (0.0, 1.0)) ``` -------------------------------- ### v6 VectorContinuousCallback affect! implementation Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/migration/ordinarydiffeq_v7.md Example of a v6 affect! implementation branching on the event index. ```julia # v6: branch on the event index, optionally a separate affect_neg! function affect!(integrator, event_index) if event_index == 1 # ball 1 hit the ground elseif event_index == 2 # ball 2 hit the ground end end cb = VectorContinuousCallback(condition, affect!, affect_neg!, 2) ``` -------------------------------- ### Custom Output Function Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/features/ensemble.md Example of an output_func that extracts only the end value of the 2nd dependent variable from each solution. ```julia output_func(sol, ctx) = (sol[end, 2], false) ``` -------------------------------- ### AutoSwitch Algorithm Parameters Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/ode_solve.md Illustrates the parameters for the AutoSwitch algorithm, used for automatic stiffness detection and switching between non-stiff and stiff ODE solvers. ```julia AutoSwitch(nonstiffalg::nAlg, stiffalg::sAlg; maxstiffstep = 10, maxnonstiffstep = 3, nonstifftol::T = 9 // 10, stifftol::T = 9 // 10, dtfac = 2.0, stiffalgfirst = false) ``` -------------------------------- ### Get Timeseries of Mean and Variance (Timesteps) Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/features/ensemble.md Generates series for the mean and variance at each time step of the simulation. ```julia m_series, v_series = DE.EnsembleAnalysis.timeseries_steps_meanvar(sim) ``` -------------------------------- ### General Runge-Kutta Solver with Tableau Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/solvers/ode_solve.md Example of using the general ExplicitRK solver with a specified Runge-Kutta tableau, such as Dormand-Prince. ```julia using OrdinaryDiffEqExplicitTableaus alg = ExplicitRK(tableau = OrdinaryDiffEqExplicitTableaus.DormandPrince()) solve(prob, alg) ``` -------------------------------- ### Get Timeseries of Mean (Timepoints) Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/features/ensemble.md Generates a series for the mean at specified time points, using interpolation if necessary. ```julia ts = 0:0.1:1 m_series = DE.EnsembleAnalysis.timeseries_point_mean(sim, ts) ``` -------------------------------- ### Solving with a Callback Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/features/callback_functions.md Demonstrates how to pass a callback function to the solver using the 'callback' keyword argument. ```julia sol = solve(prob, alg, callback = cb) ``` -------------------------------- ### Basic Solution Plotting Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/getting_started.md Plot the solution object directly using the Plots.jl integration. Ensure Plots.jl is installed and imported. ```julia #]add Plots # You need to install Plots.jl before your first time using it! import Plots #plotly() # You can optionally choose a plotting backend Plots.plot(sol) ``` -------------------------------- ### Modify Initial Condition with Array Values Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/features/ensemble.md Example of a prob_func that sets the initial condition based on the simulation ID from an array. ```julia function prob_func(prob, ctx) @. prob.u0 = u0_arr[ctx.sim_id] prob end ``` -------------------------------- ### Handle Inconsistent Initial Conditions with BrownFullBasicInit Source: https://github.com/sciml/diffeqdocs.jl/blob/master/docs/src/tutorials/dae_example.md Demonstrates how `BrownFullBasicInit` automatically corrects inconsistent initial conditions for DAEs by adjusting algebraic variables to satisfy constraint equations. ```julia # Inconsistent initial conditions - y₃ should be 0 to satisfy y₁ + y₂ + y₃ = 1 u₀_inconsistent = [1.0, 0.0, 0.5] # Sum is 1.5, not 1! du₀_inconsistent = [-0.04, 0.04, 0.0] prob_inconsistent = DE.DAEProblem(f2, du₀_inconsistent, u₀_inconsistent, tspan, differential_vars = differential_vars) # This would error with CheckInit() because conditions are inconsistent: # sol_error = DE.solve(prob_inconsistent, Sundials.IDA(), # initializealg = DiffEqBase.CheckInit()) # But BrownFullBasicInit() will fix the inconsistency automatically: sol_fixed = DE.solve(prob_inconsistent, Sundials.IDA(), initializealg = DiffEqBase.BrownFullBasicInit()) println("Original (inconsistent) y₃ = ", u₀_inconsistent[3]) println("Corrected y₃ after initialization = ", sol_fixed.u[1][3]) println("Sum after correction = ", sum(sol_fixed.u[1])) ```