### Setup Environment and Install Packages Source: https://docs.sciml.ai/Catalyst/stable/inverse_problems/examples/ode_fitting_oscillation Sets up a temporary Julia environment and installs necessary packages for the example. This environment is deleted when the Julia session ends. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("Catalyst") Pkg.add("OptimizationBase") Pkg.add("OptimizationOptimisers") Pkg.add("OrdinaryDiffEqRosenbrock") Pkg.add("Plots") Pkg.add("SciMLSensitivity") ``` -------------------------------- ### Setup Environment and Install Packages Source: https://docs.sciml.ai/Catalyst/stable/inverse_problems/turing_ode_param_fitting Installs necessary Julia packages for running the ODE parameter fitting example. Ensure you are in the correct project directory before running. ```julia using Pkg Pkg.activate(".") Pkg.add("Catalyst") Pkg.add("Distributions") Pkg.add("OrdinaryDiffEqDefault") Pkg.add("Plots") Pkg.add("StatsPlots") Pkg.add("SymbolicIndexingInterface") Pkg.add("Turing") ``` -------------------------------- ### Quick-Start Simulation Examples Source: https://docs.sciml.ai/Catalyst/stable/model_simulation/simulation_introduction Demonstrates how to set up and run ODE, SDE, and jump simulations using Catalyst. Requires prior environment setup and package installation. ```julia using Catalyst, JumpProcesses, OrdinaryDiffEqDefault, StochasticDiffEq # First we designate our model, initial condition, time span, and parameter values. rn = @reaction_network begin (p,d), 0 <--> X end u0 = [:X => 2] tspan = (0.0, 10.0) ps = [:p => 5.0, :d => 0.25] # Now, for each simulation type, we first bundle these into a `XProblem`. # This can be simulated using `solve` and the solution plotted using `plot`. # ODE simulation. oprob = ODEProblem(rn, u0, tspan, ps) osol = solve(oprob) plot(osol) # SDE simulation. sprob = SDEProblem(rn, u0, tspan, ps) ssol = solve(sprob) plot(ssol) # Jump simulation. jprob = JumpProblem(rn, u0, tspan, ps) jsol = solve(jprob) plot(jsol) ``` -------------------------------- ### Setup Environment and Install Packages Source: https://docs.sciml.ai/Catalyst/stable/model_creation/compositional_modeling Installs the necessary Catalyst, GraphRecipes, and Plots packages in a temporary environment. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("Catalyst") Pkg.add("GraphRecipes") Pkg.add("Plots") ``` -------------------------------- ### Setup Environment and Install Packages Source: https://docs.sciml.ai/Catalyst/stable/inverse_problems/optimization_ode_param_fitting Installs necessary packages for ODE parameter fitting in a temporary Julia environment. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("Catalyst") Pkg.add("OptimizationBase") Pkg.add("OptimizationNLopt") Pkg.add("OptimizationEvolutionary") Pkg.add("OptimizationOptimJL") Pkg.add("OrdinaryDiffEqDefault") Pkg.add("Plots") ``` -------------------------------- ### Environment Setup and Package Installation Source: https://docs.sciml.ai/Catalyst/stable/model_simulation/examples/periodic_events_simulation Sets up a temporary Julia environment and installs necessary packages: Catalyst, JumpProcesses, OrdinaryDiffEqDefault, and Plots. This is a prerequisite for running the examples in the tutorial. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("Catalyst") Pkg.add("JumpProcesses") Pkg.add("OrdinaryDiffEqDefault") Pkg.add("Plots") ``` -------------------------------- ### Environment Setup and Package Installation Source: https://docs.sciml.ai/Catalyst/stable/model_creation/events Installs Catalyst, OrdinaryDiffEqTsit5, and Plots in a temporary Julia environment. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("Catalyst") Pkg.add("OrdinaryDiffEqTsit5") Pkg.add("Plots") ``` -------------------------------- ### Setup Environment and Install Packages Source: https://docs.sciml.ai/Catalyst/stable/model_creation/examples/hodgkin_huxley_equation Installs necessary packages for running the Hodgkin-Huxley model examples. Creates a temporary environment that is deleted upon session end. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("Catalyst") Pkg.add("ModelingToolkitBase") Pkg.add("NonlinearSolveFirstOrder") Pkg.add("OrdinaryDiffEqRosenbrock") Pkg.add("Plots") ``` -------------------------------- ### Environment Setup and Package Installation Source: https://docs.sciml.ai/Catalyst/stable/model_simulation/ode_simulation_performance Sets up a temporary Julia environment and installs necessary packages for performing ODE simulations and benchmarking. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("BenchmarkTools") Pkg.add("Catalyst") Pkg.add("CUDA") Pkg.add("DiffEqGPU") Pkg.add("IncompleteLU") Pkg.add("OrdinaryDiffEqBDF") Pkg.add("OrdinaryDiffEqRosenbrock") Pkg.add("OrdinaryDiffEqTsit5") Pkg.add("OrdinaryDiffEqVerner") Pkg.add("Plots") Pkg.add("StaticArrays") ``` -------------------------------- ### Environment Setup and Package Installation Source: https://docs.sciml.ai/Catalyst/stable/steady_state_functionality/steady_state_stability_computation Sets up a temporary Julia environment and installs the necessary Catalyst and HomotopyContinuation packages. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("Catalyst") Pkg.add("HomotopyContinuation") ``` -------------------------------- ### Setup Catalyst Environment Source: https://docs.sciml.ai/Catalyst/stable/model_creation/programmatic_CRN_construction Installs the Catalyst package in a temporary environment for running the code examples. ```julia using Pkg Pkg.activate(; temp = true) Pkg.add("Catalyst") ``` -------------------------------- ### Environment Setup and Package Installation Source: https://docs.sciml.ai/Catalyst/stable/model_creation/parametric_stoichiometry Sets up a temporary Julia environment and installs necessary packages for running Catalyst examples. This includes Catalyst, Distributions, JumpProcesses, Latexify, ModelingToolkitBase, OrdinaryDiffEqTsit5, and Plots. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("Catalyst") Pkg.add("Distributions") Pkg.add("JumpProcesses") Pkg.add("Latexify") Pkg.add("ModelingToolkitBase") Pkg.add("OrdinaryDiffEqTsit5") Pkg.add("Plots") ``` -------------------------------- ### Environment Setup and Package Installation Source: https://docs.sciml.ai/Catalyst/stable/inverse_problems/global_sensitivity_analysis Sets up a temporary Julia environment and installs necessary packages for Global Sensitivity Analysis. ```julia using Pkg Pkg.activate(; temp = true) Mkdir.add("Catalyst") Mkdir.add("GlobalSensitivity") Mkdir.add("Plots") Mkdir.add("OrdinaryDiffEqDefault") ``` -------------------------------- ### Setup Environment and Install Packages Source: https://docs.sciml.ai/Catalyst/stable/model_creation/reactionsystem_content_accessing Installs necessary packages for running Catalyst code in a temporary environment. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("Catalyst") Pkg.add("OrdinaryDiffEqDefault") Pkg.add("Plots") Pkg.add("SymbolicIndexingInterface") ``` -------------------------------- ### Environment Setup and Package Installation Source: https://docs.sciml.ai/Catalyst/stable/model_simulation/simulation_structure_interfacing Sets up a temporary Julia environment and installs necessary packages: Catalyst, OrdinaryDiffEqDefault, and SymbolicIndexingInterface. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("Catalyst") Pkg.add("OrdinaryDiffEqDefault") Pkg.add("SymbolicIndexingInterface") ``` -------------------------------- ### Environment Setup and Package Installation Source: https://docs.sciml.ai/Catalyst/stable/steady_state_functionality/examples/nullcline_plotting Sets up a temporary Julia environment and installs necessary packages: Catalyst, HomotopyContinuation, and Plots. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("Catalyst") Pkg.add("HomotopyContinuation") Pkg.add("Plots") ``` -------------------------------- ### Setup Environment and Install Packages Source: https://docs.sciml.ai/Catalyst/stable/model_simulation/examples/interactive_brusselator_simulation Installs necessary packages for running the simulation and visualization. Creates a temporary environment that is deleted when the Julia session ends. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("CairoMakie") Pkg.add("Catalyst") Pkg.add("GLMakie") Pkg.add("OrdinaryDiffEqTsit5") ``` -------------------------------- ### Environment Setup and Package Installation Source: https://docs.sciml.ai/Catalyst/stable/steady_state_functionality/dynamical_systems This code block sets up a temporary Julia environment and installs necessary packages for running the examples on this page. The temporary environment is deleted upon session end. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("CairoMakie") Pkg.add("Catalyst") Pkg.add("DynamicalSystems") Pkg.add("OrdinaryDiffEqRosenbrock") Pkg.add("OrdinaryDiffEqTsit5") Pkg.add("Plots") ``` -------------------------------- ### Setup Environment and Install Catalyst Source: https://docs.sciml.ai/Catalyst/stable/model_creation/chemistry_related_functionality Sets up a temporary Julia environment and installs the Catalyst package. This is useful for ensuring a clean and isolated environment for running the code examples. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("Catalyst") ``` -------------------------------- ### Julia Environment Setup for SDE Simulations Source: https://docs.sciml.ai/Catalyst/stable/model_simulation/sde_simulation_performance Installs necessary packages for SDE simulations in a temporary Julia environment. This setup is recommended before running the provided code examples. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("Catalyst") Pkg.add("CUDA") Pkg.add("DiffEqGPU") Pkg.add("StaticArrays") Pkg.add("StochasticDiffEq") ``` -------------------------------- ### Environment Setup and Package Installation Source: https://docs.sciml.ai/Catalyst/stable/steady_state_functionality/examples/bifurcationkit_codim2 Sets up a temporary Julia environment and installs necessary packages: BifurcationKit, Catalyst, OrdinaryDiffEqDefault, and Plots. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("BifurcationKit") Pkg.add("Catalyst") Pkg.add("OrdinaryDiffEqDefault") Pkg.add("Plots") ``` -------------------------------- ### Setup Environment and Install Packages Source: https://docs.sciml.ai/Catalyst/stable/model_creation/conservation_laws Installs necessary packages for running Catalyst models, including Catalyst itself, Latexify for equation formatting, OrdinaryDiffEqDefault for ODE solving, and Plots for visualization. Creates a temporary environment. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("Catalyst") Pkg.add("Latexify") Pkg.add("OrdinaryDiffEqDefault") Pkg.add("Plots") ``` -------------------------------- ### Setup Julia Environment and Install Packages Source: https://docs.sciml.ai/Catalyst/stable/steady_state_functionality/examples/bifurcationkit_periodic_orbits Installs necessary packages for BifurcationKit.jl and Catalyst.jl in a temporary environment. ```julia using Pkg Pkg.activate(; temp = true) Pkg.add("BifurcationKit") Pkg.add("Catalyst") Pkg.add("OrdinaryDiffEqDefault") Pkg.add("Plots") ``` -------------------------------- ### Setup Environment and Install Packages Source: https://docs.sciml.ai/Catalyst/stable/model_creation/examples/basic_CRN_library Installs necessary packages for running CRN models and simulations. Ensure you are in the correct project directory. ```julia using Pkg Pkg.activate(".") Pkg.add("Catalyst") Pkg.add("OrdinaryDiffEqDefault") Pkg.add("StochasticDiffEq") Pkg.add("JumpProcesses") Pkg.add("Plots") ``` -------------------------------- ### Quick-start Deep Equilibrium Network Example Source: https://docs.sciml.ai/DeepEquilibriumNetworks/stable This example demonstrates setting up and running a Deep Equilibrium Network using Lux.jl and DifferentialEquations.jl. It includes model definition, GPU setup, forward pass, and gradient calculation. ```julia using DeepEquilibriumNetworks, Lux, Random, NonlinearSolve, Zygote, SciMLSensitivity using LuxCUDA # For NVIDIA GPU support seed = 0 rng = Random.default_rng() Random.seed!(rng, seed) model = Chain(Dense(2 => 2), DeepEquilibriumNetwork( Parallel(+, Dense(2 => 2; use_bias=false), Dense(2 => 2; use_bias=false)), NewtonRaphson())) gdev = gpu_device() cdev = cpu_device() ps, st = Lux.setup(rng, model) |> gdev x = rand(rng, Float32, 2, 3) |> gdev y = rand(rng, Float32, 2, 3) |> gdev res, st_ = model(x, ps, st) st_.layer_2.solution ``` ```text DeepEquilibriumSolution * Initial Guess: Float32[0.0 0.0 0.0; 0.0 0.0 0.0] * Steady State: Float32[-0.219407 0.637479 -0.744242; 1.50412 -0.962679 3.12426] * Residual: Float32[-4.47035f-8 -5.96046f-8 -5.96046f-8; -1.19209f-7 5.96046f-8 -2.38419f-7] * Jacobian Loss: 0.0 * NFE: 3 ``` ```julia gs = only(Zygote.gradient(p -> sum(abs2, first(model(x, p, st)) .- y), ps)) ``` ```text (layer_1 = (weight = Float32[-4.6991134 -8.227526; -13.163104 -20.53097], bias = Float32[-11.837078, -31.839073]), layer_2 = (model = (layer_1 = (weight = Float32[15.304397 -58.909546; -21.828897 81.71437],), layer_2 = (weight = Float32[17.71767 3.5711353; -23.398441 -5.474779],)), init = nothing)) ``` -------------------------------- ### Environment Setup and Package Installation Source: https://docs.sciml.ai/Catalyst/stable/model_creation/functional_parameters Sets up a temporary Julia environment and installs necessary packages: Catalyst, DataInterpolations, OrdinaryDiffEqDefault, and Plots. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("Catalyst") Pkg.add("DataInterpolations") Pkg.add("OrdinaryDiffEqDefault") Pkg.add("Plots") ``` -------------------------------- ### Environment Setup and Package Installation Source: https://docs.sciml.ai/Catalyst/stable/model_simulation/examples/activation_time_distribution_measurement Sets up a temporary Julia environment and installs necessary packages like Catalyst, Plots, and StochasticDiffEq. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("Catalyst") Pkg.add("Plots") Pkg.add("StochasticDiffEq") ``` -------------------------------- ### Setup for Demonstrating Extrapolation Methods Source: https://docs.sciml.ai/DataInterpolations/stable/extrapolation_methods Initializes data and an interpolation object for demonstrating extrapolation. This setup is used across multiple examples. ```julia using DataInterpolations, Plots u = [0.86, 0.65, 0.44, 0.76, 0.73] t = [0.0022, 0.68, 1.41, 2.22, 2.46] t_eval_left = range(-1, first(t), length = 25) t_eval_right = range(last(t), 3.5, length = 25) A = QuadraticSpline(u, t) plot(A) ``` -------------------------------- ### Environment Setup and Package Installation Source: https://docs.sciml.ai/Catalyst/stable/model_simulation/hybrid_simulations Sets up a temporary Julia environment and installs necessary packages for running hybrid simulations. This includes Catalyst, JumpProcesses, DiffEqNoiseProcess, OrdinaryDiffEqTsit5, Plots, and StochasticDiffEq. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("Catalyst") Pkg.add("JumpProcesses") Pkg.add("DiffEqNoiseProcess") Pkg.add("OrdinaryDiffEqTsit5") Pkg.add("Plots") Pkg.add("StochasticDiffEq") ``` -------------------------------- ### Setup Environment and Install Packages Source: https://docs.sciml.ai/Catalyst/stable/inverse_problems/structural_identifiability Sets up a temporary Julia environment and installs the necessary Catalyst and StructuralIdentifiability packages. This environment is deleted when the Julia session ends. ```julia using Pkg Pkg.activate(; temp = true) Pkg.add("Catalyst") Pkg.add("StructuralIdentifiability") ``` -------------------------------- ### Setup Environment and Install Packages Source: https://docs.sciml.ai/Catalyst/stable/model_creation/examples/programmatic_generative_linear_pathway Sets up a temporary Julia environment and installs necessary packages: Catalyst, OrdinaryDiffEqDefault, and Plots. This environment is deleted when the Julia session ends. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("Catalyst") Pkg.add("OrdinaryDiffEqDefault") Pkg.add("Plots") ``` -------------------------------- ### Setup Environment and Install Packages Source: https://docs.sciml.ai/Catalyst/stable/model_creation/dsl_basics Sets up a temporary Julia environment and installs the Catalyst and Latexify packages. This environment is deleted when the Julia session ends. ```julia using Pkg Pkg.activate(; temp = true) Pkg.add("Catalyst") Pkg.add("Latexify") ``` -------------------------------- ### Example: System Definition with Parameters Source: https://docs.sciml.ai/ModelingToolkit/stable/basics/FAQ Defines a system with scalar and array parameters, then completes the system. This setup is used to demonstrate parameter ordering. ```julia using ModelingToolkit @parameters p q[1:3] r[1:2, 1:2] @named sys = System(Equation[], ModelingToolkit.t_nounits, [], [p, q, r]) sys = complete(sys) ``` -------------------------------- ### Example: DeepSplitting Algorithm Setup Source: https://docs.sciml.ai/HighDimPDE/stable/DeepSplitting.html Illustrates how to set up the DeepSplitting algorithm by defining a neural network architecture and configuring the algorithm's parameters including Monte Carlo sampling. ```julia hls = d + 50 # hidden layer size d = 10 # size of the sample # Neural network used by the scheme nn = Flux.Chain(Dense(d, hls, tanh), Dense(hls,hls,tanh), Dense(hls, 1, x->x^2)) alg = DeepSplitting(nn, K=10, opt = ADAM(), λs = [5e-3,1e-3], mc_sample = UniformSampling(zeros(d), ones(d)) ) ``` -------------------------------- ### Environment Setup and Package Installation Source: https://docs.sciml.ai/Catalyst/stable/model_simulation/simulation_introduction Sets up a temporary Julia environment and adds necessary packages for Catalyst simulations. This environment is deleted when the Julia session ends. ```julia using Pkg Pkg.activate(; temp = true) # Creates a temporary environment, which is deleted when the Julia session ends. Pkg.add("Catalyst") Pkg.add("JumpProcesses") Pkg.add("OrdinaryDiffEqDefault") Pkg.add("OrdinaryDiffEqRosenbrock") Pkg.add("OrdinaryDiffEqTsit5") Pkg.add("Plots") Pkg.add("StochasticDiffEq") ``` -------------------------------- ### Setup Julia Environment for Simulations Source: https://docs.sciml.ai/Catalyst/stable/model_simulation/ensemble_simulations Installs necessary packages for running simulations. Creates a temporary environment that is deleted upon session end. ```julia using Pkg Pkg.activate(; temp = true) Pkg.add("Catalyst") Pkg.add("OrdinaryDiffEqTsit5") Pkg.add("Plots") Pkg.add("StochasticDiffEq") ``` -------------------------------- ### Basic Nonlinear Problem Setup and Solve Source: https://docs.sciml.ai/NonlinearSolve/stable/basics/diagnostics_api Sets up a simple nonlinear problem and solves it using NonlinearSolve.jl. This is a fundamental example to get started. ```julia import NonlinearSolve as NLS function nlfunc(resid, u0, p) resid[1] = u0[1] * u0[1] - p resid[2] = u0[2] * u0[2] - p resid[3] = u0[3] * u0[3] - p nothing end prob = NLS.NonlinearProblem(nlfunc, [1.0, 3.0, 5.0], 2.0) NLS.solve(prob) ``` -------------------------------- ### Setup and Function Definition Source: https://docs.sciml.ai/SciMLOperators/stable/tutorials/fftw Initializes packages, defines grid parameters, and creates a sample function `v` and its analytical derivative `w` for testing. ```julia using SciMLOperators using LinearAlgebra, FFTW L = 2π n = 256 dx = L / n x = range(start = -L / 2, stop = L / 2 - dx, length = n) |> Array v = @. sin(5x)cos(7x); w = @. 5cos(5x)cos(7x) - 7sin(5x)sin(7x); ``` -------------------------------- ### Configure Optimization Algorithm Setup Source: https://docs.sciml.ai/SciMLBenchmarksOutput/stable/GlobalOptimization/blackbox_global_optimizers Creates a dictionary `setup` to configure various global optimization algorithms for benchmarking. It includes algorithms from NLopt, OptimizationEvolutionary, Optim.jl, BBO, OptimizationMetaheuristics, Optimisers, and SciPy, specifying their parameters and whether they are boxed. ```julia setup = Dict( "NelderMead" => NelderMead(), #Optim.BFGS(), #"NLopt.GN_MLSL_LDS" => chain(NLopt.GN_MLSL_LDS(), isboxed=true), # gives me errors "NLopt.GN_CRS2_LM()" => chain(NLopt.GN_CRS2_LM(), isboxed = true), "NLopt.GN_DIRECT()" => chain(NLopt.GN_DIRECT(), isboxed = true), "NLopt.GN_ESCH()" => chain(NLopt.GN_ESCH(), isboxed = true), "OptimizationEvolutionary.GA()" => chain(OptimizationEvolutionary.GA(), isboxed = true), "OptimizationEvolutionary.DE()" => chain(OptimizationEvolutionary.DE(), isboxed = true), "OptimizationEvolutionary.ES()" => chain(OptimizationEvolutionary.ES(), isboxed = true), "Optim.SAMIN" => chain(SAMIN(verbosity = 0), isboxed = true), "BBO_adaptive_de_rand_1_bin" => chain(BBO_adaptive_de_rand_1_bin(), isboxed = true), "BBO_adaptive_de_rand_1_bin_radiuslimited" => chain( BBO_adaptive_de_rand_1_bin_radiuslimited(), isboxed = true), # same as BBO_adaptive_de_rand_1_bin "BBO_separable_nes" => chain(BBO_separable_nes(), isboxed = true), "BBO_de_rand_2_bin" => chain(BBO_de_rand_2_bin(), isboxed = true), #"BBO_xnes" => chain(BBO_xnes(), isboxed=true), # good but slow #"BBO_dxnes" => chain(BBO_dxnes(), isboxed=true), "OptimizationMetaheuristics.ECA" => chain(OptimizationMetaheuristics.ECA(), isboxed = true), #"OptimizationMetaheuristics.CGSA" => () -> chain(OptimizationMetaheuristics.CGSA(), isboxed=true), #give me strange results "OptimizationMetaheuristics.DE" => chain(OptimizationMetaheuristics.DE(), isboxed=true), "Optimisers.AdamW" => chain(Optimisers.AdamW(), isboxed=false), "Optimisers.RMSProp" => chain(Optimisers.RMSProp(), isboxed=false), # SciPy global optimizers "ScipyDifferentialEvolution" => chain(ScipyDifferentialEvolution(), isboxed=true), #"ScipyBasinhopping" => chain(ScipyBasinhopping(), isboxed=true), #"ScipyDualAnnealing" => chain(ScipyDualAnnealing(), isboxed=true), # taking long time "ScipyShgo" => chain(ScipyShgo(), isboxed=true), "ScipyDirect" => chain(ScipyDirect(), isboxed=true), "ScipyBrute" => chain(ScipyBrute(), isboxed=true), # "NOMADOpt" => chain(NOMADOpt()), too much printing # "OptimizationPRIMA.UOBYQA()" => chain(OptimizationPRIMA.UOBYQA()), :StackOverflowError? # "OptimizationPRIMA.NEWUOA()" => OptimizationPRIMA.UOBYQA(), # ) ``` -------------------------------- ### Install and Open SciML Tutorials Source: https://docs.sciml.ai/SciMLTutorialsOutput/stable Installs the SciMLTutorials package, activates its environment, and opens the interactive notebooks. Ensure you are in the correct directory when running. ```julia ] add SciMLTutorials#master ]activate SciMLTutorials ]instantiate using SciMLTutorials SciMLTutorials.open_notebooks() ``` -------------------------------- ### Setup: Fetch Required Packages Source: https://docs.sciml.ai/SciMLBenchmarksOutput/stable/NonlinearProblem/nonlinear_battery_problem Imports necessary packages for nonlinear solving, linear algebra, automatic differentiation, and benchmarking. ```julia using NonlinearSolve, LinearSolve, StaticArrays, Sundials, SpeedMapping, BenchmarkTools, LinearAlgebra, DiffEqDevTools, PolyesterForwardDiff, CairoMakie, RecursiveFactorization, Enzyme import MINPACK, NLsolve, PETSc import LineSearches const RUS = RadiusUpdateSchemes; ``` -------------------------------- ### Load and Solve a Linear ODE Problem Source: https://docs.sciml.ai/DiffEqDocs/latest/types/ode_types Demonstrates how to add the DiffEqProblemLibrary, import ODE problems, and solve a linear ODE using DifferentialEquations.jl. This is a basic example for getting started. ```julia #] add DiffEqProblemLibrary import DiffEqProblemLibrary.ODEProblemLibrary import DifferentialEquations as DE # load problems ODEProblemLibrary.importodeproblems() prob = ODEProblemLibrary.prob_ode_linear sol = DE.solve(prob) ``` -------------------------------- ### Setting up the computation device Source: https://docs.sciml.ai/NeuralOperators/stable/models/fno Initializes the computation device using Reactant.jl for potential hardware acceleration. ```julia xdev = reactant_device() ``` -------------------------------- ### Complete example for precompilation-friendly components Source: https://docs.sciml.ai/ModelingToolkit/stable/basics/PrecompileComponents This example demonstrates a complete structure for a precompilation-friendly component, including variables, parameters, initial conditions, equations, and subsystems, adhering to best practices for performance. ```julia using ModelingToolkit using PrecompileTools @precompile_all ModelingToolkit begin @parameters t k @variables x(t) vars = SymbolicT[] params = SymbolicT[] push!(vars, @variables x(t)) push!(params, @parameters k) initial_conditions = Dict{SymbolicT, SymbolicT}() guesses = Dict{SymbolicT, SymbolicT}() initial_conditions[x] = 0.0 guesses[x] = 0.0 eqs = Equation[] push!(eqs, D(x) ~ k*x) subsystems = Vector{System}() # Example of creating a simple system sys = @system begin t k x(t) D(x) ~ k*x end push!(subsystems, sys) # Example of creating a model from components # This part would typically involve composing systems # For demonstration, we'll just show the structure # mtkmodel = @mtkmodel begin ... end end ``` -------------------------------- ### Setup and PDE Definition Source: https://docs.sciml.ai/SciMLSensitivity/stable/examples/pde/pde_constrained Imports necessary libraries and defines the parameters, initial conditions, and the differential equations for the Heat Equation. Includes auxiliary functions for spatial derivatives. ```julia import SciMLSensitivity as SMS import DelimitedFiles, Plots import OrdinaryDiffEq as ODE, Optimization as OPT, OptimizationPolyalgorithms as OPA, Zygote # Problem setup parameters: Lx = 10.0 x = 0.0:0.01:Lx dx = x[2] - x[1] Nx = size(x) u0 = exp.(-(x .- 3.0) .^ 2) # I.C ## Problem Parameters p = [1.0, 1.0] # True solution parameters const xtrs = [dx, Nx] # Extra parameters dt = 0.40 * dx^2 # CFL condition t0, tMax = 0.0, 1000 * dt tspan = (t0, tMax) t = t0:dt:tMax; ## Definition of Auxiliary functions function ddx(u, dx) """ 2nd order Central difference for 1st degree derivative """ return [[zero(eltype(u))]; (u[3:end] - u[1:(end - 2)]) ./ (2.0 * dx); [zero(eltype(u))]] end function d2dx(u, dx) """ 2nd order Central difference for 2nd degree derivative """ return [zero(eltype(u)); (@view(u[3:end]) .- 2.0 .* @view(u[2:(end - 1)]) .+ @view(u[1:(end - 2)])) ./ (dx^2) zero(eltype(u))] end ## ODE description of the Physics: function heat(u, p, t, xtrs) # Model parameters a0, a1 = p dx, Nx = xtrs #[1.0,3.0,0.125,100] return 2.0 * a0 .* u + a1 .* d2dx(u, dx) end heat_closure(u, p, t) = heat(u, p, t, xtrs) # Testing Solver on linear PDE prob = ODE.ODEProblem(heat_closure, u0, tspan, p) sol = ODE.solve(prob, ODE.Tsit5(); dt, saveat = t); arr_sol = Array(sol) Plots.plot(x, sol.u[1], lw = 3, label = "t0", size = (800, 500)) Plots.plot!(x, sol.u[end], lw = 3, ls = :dash, label = "tMax") ``` -------------------------------- ### Install and Import IRKGaussLegendre Source: https://docs.sciml.ai/DiffEqDocs/latest/solvers/ode_solve To use the IRKGL16 solver, you need to install and import the IRKGaussLegendre.jl package. This setup is not automatically included with DifferentialEquations.jl. ```julia import Pkg Pkg.add("IRKGaussLegendre") import IRKGaussLegendre ``` -------------------------------- ### Install and Import LSODA Source: https://docs.sciml.ai/DiffEqDocs/latest/solvers/ode_solve To use the LSODA solver, you must install and import the LSODA.jl package. This setup is not automatically included with DifferentialEquations.jl. ```julia import Pkg Pkg.add("LSODA") import LSODA ``` -------------------------------- ### Install and Import ODEInterfaceDiffEq Source: https://docs.sciml.ai/DiffEqDocs/latest/solvers/ode_solve To use the ODEInterface algorithms, you need to install and import the ODEInterfaceDiffEq package. This setup is not included by default with DifferentialEquations.jl. ```julia import Pkg Pkg.add("ODEInterfaceDiffEq") import ODEInterfaceDiffEq ``` -------------------------------- ### Initial Solver Setup and First Simulation Source: https://docs.sciml.ai/SciMLBenchmarksOutput/stable/ComplicatedPDE/SpringBlockNonLinearResistance Configures the solver with KLU factorization and sets absolute and relative tolerances. It then defines the initial conditions and time span, creates an ODEProblem, and solves it using the specified solver and callbacks. The solution is then used to create a TestSolution object. ```julia solver = KenCarp47(linsolve=KLUFactorization()) abstol = 1e-12 reltol = 1e-8 u0 = get_IC() tspan = (0.0,1e9) prob1 = ODEProblem(f,u0,tspan,nothing) sys = modelingtoolkitize(prob1) sys_simplified = structural_simplify(sys) prob_mtk1 = ODEProblem(sys_simplified,[],tspan,jac=true,sparse=true) global sol,tcpu, bytes, gctime, memallocs = @timed solve(prob_mtk1,solver,reltol = reltol,abstol = abstol,maxiters = Int(1e12),save_everystep = false,dtmin = 1e-20,callback = cb1) u1 = sol.u[end] t1 = sol.t[end] test_sol1 = TestSolution(sol) ``` -------------------------------- ### Parameter Estimation Setup Source: https://docs.sciml.ai/ModelingToolkit/stable/tutorials/dynamic_optimization Initializes a dynamic optimization problem for parameter estimation with `tune_parameters=true`. Requires a defined system `sys_pe′`, initial conditions `u0map_pe`, and time span `tspan_pe`. ```julia iprob_pe = InfiniteOptDynamicOptProblem(sys_pe′, u0map_pe, tspan_pe; dt=1/50, tune_parameters=true) isol_pe = solve(iprob_pe, InfiniteOptCollocation(Ipopt.Optimizer, InfiniteOpt.OrthogonalCollocation(3))) ``` -------------------------------- ### Setup and Problem Definition for qmax Determination Source: https://docs.sciml.ai/SciMLBenchmarksOutput/stable/AdaptiveSDE/qmaxDetermination Initializes parameters, loads necessary packages, and defines the SDE problem for benchmarking. Includes a compilation run to ensure accurate timing. ```julia qs = 1.0 .+ 2.0 .^ (-5:2) times = Array{Float64}(undef, length(qs), 4) means = Array{Float64}(undef, length(qs), 4) using StochasticDiffEq, SDEProblemLibrary, Random, Plots, ParallelDataTransfer, DiffEqMonteCarlo, Distributed Random.seed!(99) full_prob = SDEProblemLibrary.oval2ModelExample(largeFluctuations = true, useBigs = false) import SDEProblemLibrary: prob_sde_additivesystem, prob_sde_additive, prob_sde_2Dlinear, prob_sde_linear, prob_sde_wave prob = remake(full_prob, tspan = (0.0, 1.0)) println("Solve once to compile.") sol = solve(prob, EM(), dt = 1/2^(18)) Int(sol.u[end][1]!=NaN) println("Compilation complete.") num_runs = 10000 probs = Vector{SDEProblem}(undef, 3) p1 = Vector{Any}(undef, 3) p2 = Vector{Any}(undef, 3) p3 = Vector{Any}(undef, 3) ## Problem 1 probs[1] = prob_sde_linear ## Problem 2 probs[2] = prob_sde_wave ## Problem 3 probs[3] = prob_sde_additive println("Setup Complete") ``` -------------------------------- ### Full Distributed Batching Example Source: https://docs.sciml.ai/SciMLSensitivity/stable/tutorials/data_parallel A complete example of distributed batching for an optimization problem. This setup requires `Distributed.addprocs` and defining necessary functions on all worker processes. ```julia import Distributed Distributed.addprocs(4) Distributed.@everywhere begin import OrdinaryDiffEq as ODE import Optimization as OPT import OptimizationOptimisers as OPO function f(u, p, t) 1.01u .* p end end pa = [1.0] u0 = [3.0] θ = [u0; pa] function model1(θ, ensemble) prob = ODE.ODEProblem(f, [θ[1]], (0.0, 1.0), [θ[2]]) function prob_func(prob, i, repeat) ODE.remake(prob, u0 = 0.5 .+ i / 100 .* prob.u0) end ensemble_prob = ODE.EnsembleProblem(prob; prob_func) sim = ODE.solve(ensemble_prob, ODE.Tsit5(), ensemble, saveat = 0.1, trajectories = 100) end callback = function (θ, l) # callback function to observe training @show l false end opt = OPO.Adam(0.1) loss_distributed(θ) = sum(abs2, 1.0 .- Array(model1(θ, ODE.EnsembleDistributed()))) l1 = loss_distributed(θ) adtype = OPT.AutoZygote() optf = OPT.OptimizationFunction((x, p) -> loss_distributed(x), adtype) optprob = OPT.OptimizationProblem(optf, θ) res_distributed = OPT.solve(optprob, opt; callback, maxiters = 100) ``` -------------------------------- ### Benchmark Setup and Execution Source: https://docs.sciml.ai/SciMLBenchmarksOutput/stable/OptimizationCUTEst/CUTEst_unconstrained This code sets up the necessary packages, defines optimizers, and implements functions to run benchmarks on CUTEst problems. It includes logic for handling large problems, timing solutions, and collecting results. ```julia using Optimization using OptimizationNLPModels using CUTEst using OptimizationOptimJL using OptimizationOptimisers using OptimizationOptimJL: LBFGS, ConjugateGradient, NelderMead, SimulatedAnnealing, ParticleSwarm using Ipopt using OptimizationMOI using OptimizationMOI: MOI as MOI using DataFrames using Plots using StatsPlots using StatsBase: countmap optimizers = [ ("LBFGS", LBFGS()), ("ConjugateGradient", ConjugateGradient()), ("NelderMead", NelderMead()), ("SimulatedAnnealing", SimulatedAnnealing()), ("ParticleSwarm", ParticleSwarm()), ] function get_stats(sol, optimizer_name) if hasfield(typeof(sol), :stats) && hasfield(typeof(sol.stats), :time) solve_time = sol.stats.time else solve_time = NaN end return (length(sol.u), solve_time, optimizer_name, Symbol(sol.retcode)) end function run_benchmarks(problems, optimizers; chunk_size=1) problem = String[] n_vars = Int64[] secs = Float64[] solver = String[] retcode = Symbol[] optz = length(optimizers) n = length(problems) @info "Processing $(n) problems with $(optz) optimizers in chunks of $(chunk_size)" broadcast(c -> sizehint!(c, optz * n), [problem, n_vars, secs, solver, retcode]) for chunk_start in 1:chunk_size:n chunk_end = min(chunk_start + chunk_size - 1, n) chunk_problems = problems[chunk_start:chunk_end] @info "Processing chunk $(div(chunk_start-1, chunk_size)+1)/$(div(n-1, chunk_size)+1): problems $(chunk_start)-$(chunk_end)" for (idx, prob_name) in enumerate(chunk_problems) current_problem = chunk_start + idx - 1 @info "Problem $(current_problem)/$(n): $(prob_name)" nlp_prob = nothing try nlp_prob = CUTEstModel(prob_name) if nlp_prob.meta.nvar > 10000 @info " Skipping $(prob_name) (too large: $(nlp_prob.meta.nvar) variables)" finalize(nlp_prob) continue end prob = OptimizationNLPModels.OptimizationProblem(nlp_prob, Optimization.AutoFiniteDiff()) for (optimizer_name, optimizer) in optimizers try sol = solve(prob, optimizer; maxiters = 1000, maxtime = 30.0) @info "✓ Solved $(prob_name) with $(optimizer_name) - Status: $(sol.retcode)" vars, time, alg, code = get_stats(sol, optimizer_name) push!(problem, prob_name) push!(n_vars, vars) push!(secs, time) push!(solver, alg) push!(retcode, code) catch e push!(problem, prob_name) push!(n_vars, nlp_prob !== nothing ? nlp_prob.meta.nvar : -1) push!(secs, NaN) push!(solver, optimizer_name) push!(retcode, :FAILED) end end catch e for (optimizer_name, optimizer) in optimizers push!(problem, prob_name) push!(n_vars, -1) push!(secs, NaN) push!(solver, optimizer_name) push!(retcode, :LOAD_FAILED) end finally if nlp_prob !== nothing try finalize(nlp_prob) catch e end end end end GC.gc() @info "Completed chunk, memory usage cleaned up" end return DataFrame(problem = problem, n_vars = n_vars, secs = secs, solver = solver, retcode = retcode) end unc_problems = collect(CUTEst.select_sif_problems(contype="unc")) println("Number of problems: ", length(unc_problems)) println("First 5 problems: ", unc_problems[1:min(5, end)]) unc_problems = unc_problems[1:min(50, length(unc_problems))] println("Limited to ", length(unc_problems), " problems for comprehensive testing") unc_results = run_benchmarks(unc_problems, optimizers) @show unc_results successful_codes = [:Success, :MaxIters, :MaxTime, :FirstOrderOptimal] successful_results = filter(row -> row.retcode in successful_codes, unc_results) total_attempts = nrow(unc_results) ``` -------------------------------- ### Get and Set Variable Metadata Source: https://docs.sciml.ai/ModelingToolkit/stable/basics/ContextualVariables Demonstrates how to get and set metadata for a variable using `hasmetadata`, `get_unit`, and `setmetadata`. This example shows setting and retrieving the unit for a variable. ```julia @variables x [unit = u"m^3/s"]; hasmetadata(x, VariableUnit) ModelingToolkit.get_unit(x) x = setmetadata(x, VariableUnit, u"m/s") ModelingToolkit.get_unit(x) ``` -------------------------------- ### Steady-State Initialization Example Source: https://docs.sciml.ai/ModelingToolkit/stable/tutorials/initialization This example demonstrates how to initialize an ODE system to start at a steady state by setting the derivatives to zero. It also shows how to use an observable's initial condition directly. ```APIDOC ## Steady-State and Observable Initialization Let's take a Lotka-Volterra system: ```julia @variables x(t) y(t) z(t) @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 eqs = [D(x) ~ α * x - β * x * y D(y) ~ -γ * y + δ * x * y z ~ x + y] @named sys = System(eqs, t) simpsys = mtkcompile(sys) tspan = (0.0, 10.0) ``` Using the derivative initializations, we can set the ODE to start at the steady state by initializing the derivatives to zero: ```julia prob = ODEProblem(simpsys, [D(x) => 0.0, D(y) => 0.0], tspan, guesses = [x => 1, y => 1]) sol = solve(prob, Tsit5(), abstol = 1e-16) ``` Notice that this is a "numerical zero", not an exact zero, and thus the solution will leave the steady state in this instance because it's an unstable steady state. Additionally, notice that in this setup we have an observable `z ~ x + y`. If we instead know the initial condition for the observable we can use that directly: ```julia prob = ODEProblem(simpsys, [D(x) => 0.0, z => 2.0], tspan, guesses = [x => 1, y => 1]) sol = solve(prob, Tsit5()) ``` We can check that indeed the solution does satisfy that D(x) = 0 at the start: ```julia sol[α * x - β * x * y] ``` ```julia plot(sol) ``` ``` -------------------------------- ### IDA Examples Source: https://docs.sciml.ai/DiffEqDocs/stable/api/sundials Demonstrates basic usage of the `IDA` constructor with different linear solver configurations. ```julia IDA() # Newton + Dense solver ``` ```julia IDA(linear_solver=:Band,jac_upper=3,jac_lower=3) # Banded solver with nonzero diagonals 3 up and 3 down ``` ```julia IDA(linear_solver=:BCG) # Biconjugate gradient method ``` -------------------------------- ### Copy-Pasteable Full Example Source: https://docs.sciml.ai/DataDrivenDiffEq/stable/libs/datadrivendmd/examples/example_04 A consolidated code block containing all necessary imports, function definitions, problem setup, and DMD solving steps for the nonlinear system example. This is useful for quick execution and testing. ```julia using DataDrivenDiffEq using LinearAlgebra using OrdinaryDiffEq using SciMLBase: NoSpecialize using DataDrivenDMD function slow_manifold(du, u, p, t) du[1] = p[1] * u[1] return du[2] = p[2] * (u[2] - u[1]^2) end u0 = [3.0; -2.0] tspan = (0.0, 5.0) p = [-0.8; -0.7] problem = ODEProblem{true, NoSpecialize}(slow_manifold, u0, tspan, p) solution = solve(problem, Tsit5(), saveat = 0.1); prob = DataDrivenProblem(solution) @parameters t @variables u(t)[1:2] Ψ = Basis([u; u[1]^2], u, independent_variable = t) res = solve(prob, Ψ, DMDPINV(), digits = 2) # This file was generated using Literate.jl, https://github.com/fredrikekre/Literate.jl ``` -------------------------------- ### PartitionedSolversAlgorithm Default Example Source: https://docs.sciml.ai/LinearSolve/stable/solvers/solvers A basic example demonstrating the use of PartitionedSolversAlgorithm with its default CG solver for distributed problems. ```julia using LinearSolve, PartitionedArrays, PartitionedSolvers alg = PartitionedSolversAlgorithm(PartitionedSolvers.cg) sol = solve(prob, alg) ```