### Verifying Julia Installation with versioninfo() in REPL Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/installation.md This snippet demonstrates how to verify a successful Julia installation by running the `versioninfo()` command in the Julia REPL. It outputs detailed information about the Julia version, CPU, and operating system, confirming the environment setup. ```Julia versioninfo() ``` -------------------------------- ### Installing Required Julia Packages Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/fit_simulation.md This snippet demonstrates how to install the necessary Julia packages for this tutorial using the `Pkg.add` function within the Julia Pkg REPL. It includes packages like DifferentialEquations, Optimization, SciMLSensitivity, ForwardDiff, and Plots. ```Julia using Pkg Pkg.add([ "DifferentialEquations", "Optimization", "OptimizationPolyalgorithms", "SciMLSensitivity", "ForwardDiff", "Plots" ]) ``` -------------------------------- ### Installing DifferentialEquations SciML Package in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/installation.md This snippet illustrates how to install the `DifferentialEquations` package, a core component of the SciML ecosystem, using Julia's built-in package manager (`Pkg`). It shows two methods: direct execution in the REPL or using the `pkg>` environment for enhanced features like auto-completion. This command downloads and precompiles the package and its dependencies. ```Julia using Pkg; Pkg.add("DifferentialEquations"); ``` -------------------------------- ### Loading Julia Packages Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/fit_simulation.md This code loads the previously installed Julia packages into the current session using the `using` keyword. It makes functions and types from `DifferentialEquations`, `Optimization`, `OptimizationPolyalgorithms`, `SciMLSensitivity`, `ForwardDiff`, and `Plots` available for use. ```Julia using DifferentialEquations, Optimization, OptimizationPolyalgorithms, SciMLSensitivity using ForwardDiff, Plots ``` -------------------------------- ### Testing Julia VS Code Extension with Basic Arithmetic Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/installation.md This code snippet is used to test the functionality of the Julia VS Code extension. By typing `1+1` in a `.jl` file and executing it, users can confirm that the Julia REPL correctly launches within VS Code and processes basic arithmetic operations, displaying the result. ```Julia 1+1 ``` -------------------------------- ### Installing SciML and Plotting Packages in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/first_simulation.md This Julia snippet demonstrates how to add the necessary SciML ecosystem packages (ModelingToolkit, DifferentialEquations) and the Plots.jl visualization package to your Julia environment using the Pkg manager. This is a prerequisite step for running simulations and visualizations. ```Julia using Pkg Pkg.add(["ModelingToolkit", "DifferentialEquations", "Plots"]) ``` -------------------------------- ### Setting Up Relativistic Orbit Model Parameters in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/showcase/blackhole.md This example code block demonstrates how to define key parameters for a relativistic orbital model simulation. It includes setting the mass ratio (e.g., for a test particle), initial conditions, data size, time span for the gravitational wave waveform, and specific model parameters like 'p', 'M', and 'e'. ```text ``` -------------------------------- ### Formatting Julia Code with JuliaFormatter Source: https://github.com/sciml/scimldocs/blob/main/docs/src/highlevels/developer_documentation.md This snippet demonstrates how to use JuliaFormatter.jl to reformat Julia code within a package according to the SciML Style guide. It requires JuliaFormatter and the target package (e.g., DevedPackage) to be loaded. The pkgdir function is used to get the root directory of the package for formatting. ```Julia using JuliaFormatter, DevedPackage JuliaFormatter.format(pkgdir(DevedPackage)) ``` -------------------------------- ### Installing StructuralIdentifiability.jl in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/showcase/symbolic_analysis.md This snippet demonstrates how to add the `StructuralIdentifiability.jl` package to your Julia environment using the built-in package manager. It ensures the necessary library is available for assessing parameter identifiability in ODE models. ```Julia using Pkg Pkg.add("StructuralIdentifiability") ``` -------------------------------- ### Running Second-Stage Optimization with LBFGS for UDE in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/showcase/missing_physics.md This snippet continues the optimization process using the LBFGS algorithm, starting from the parameters found by the ADAM optimizer (`res1.u`). It creates a new `OptimizationProblem` and solves it with `LBFGS(linesearch = BackTracking())` for 4000 iterations, aiming to quickly hone in on a local minimum. The best candidate parameters are then renamed to `p_trained`. ```Julia optprob2 = Optimization.OptimizationProblem(optf, res1.u) res2 = Optimization.solve( optprob2, LBFGS(linesearch = BackTracking()), callback = callback, maxiters = 4000) println("Final training loss after $(length(losses)) iterations: $(losses[end])") # Rename the best candidate p_trained = res2.u ``` -------------------------------- ### Creating a NonlinearProblem with Overridden Values (Julia) Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/find_root.md This snippet demonstrates how to create a NonlinearProblem while overriding specific initial conditions or parameter values. Here, the initial condition for x is set to 2.0 and the parameter σ is set to 4.0, allowing for custom problem setups. ```Julia prob2 = NonlinearProblem(ns, [x => 2.0, σ => 4.0]) ``` -------------------------------- ### Full PDE Solver Example with DifferentialEquations.jl (Julia) Source: https://github.com/sciml/scimldocs/blob/main/docs/src/showcase/gpu_spde.md This comprehensive Julia snippet defines and solves a Partial Differential Equation (PDE) by discretizing it into an Ordinary Differential Equation (ODE) system. It includes constant definitions, initialization of spatial operators (Mx, My), the ODE function f that describes the PDE dynamics, and the solution using ODEProblem with ROCK2 solver. Finally, it visualizes the results using Plots.jl. ```julia using OrdinaryDiffEq, LinearAlgebra # Define the constants for the PDE const α₂ = 1.0 const α₃ = 1.0 const β₁ = 1.0 const β₂ = 1.0 const β₃ = 1.0 const r₁ = 1.0 const r₂ = 1.0 const D = 100.0 const γ₁ = 0.1 const γ₂ = 0.1 const γ₃ = 0.1 const N = 100 const X = reshape([i for i in 1:100 for j in 1:100], N, N) const Y = reshape([j for i in 1:100 for j in 1:100], N, N) const α₁ = 1.0 .* (X .>= 80) const Mx = Array(Tridiagonal([1.0 for i in 1:(N - 1)], [-2.0 for i in 1:N], [1.0 for i in 1:(N - 1)])) const My = copy(Mx) Mx[2, 1] = 2.0 Mx[end - 1, end] = 2.0 My[1, 2] = 2.0 My[end, end - 1] = 2.0 # Define the initial condition as normal arrays u0 = zeros(N, N, 3) const MyA = zeros(N, N); const AMx = zeros(N, N); const DA = zeros(N, N) # Define the discretized PDE as an ODE function function f(du, u, p, t) A = @view u[:, :, 1] B = @view u[:, :, 2] C = @view u[:, :, 3] dA = @view du[:, :, 1] dB = @view du[:, :, 2] dC = @view du[:, :, 3] mul!(MyA, My, A) mul!(AMx, A, Mx) @. DA = D * (MyA + AMx) @. dA = DA + α₁ - β₁ * A - r₁ * A * B + r₂ * C @. dB = α₂ - β₂ * B - r₁ * A * B + r₂ * C @. dC = α₃ - β₃ * C + r₁ * A * B - r₂ * C end # Solve the ODE prob = ODEProblem(f, u0, (0.0, 100.0)) sol = solve(prob, ROCK2(), progress = true, save_everystep = false, save_start = false) using Plots; gr(); p1 = surface(X, Y, sol[end][:, :, 1], title = "[A]") p2 = surface(X, Y, sol[end][:, :, 2], title = "[B]") p3 = surface(X, Y, sol[end][:, :, 3], title = "[C]") plot(p1, p2, p3, layout = grid(3, 1)) ``` -------------------------------- ### Solving Lorenz Equations with ComponentArrays and DifferentialEquations.jl in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/highlevels/array_libraries.md This comprehensive example demonstrates how to set up and define an `ODEProblem` for the Lorenz system using `ComponentArrays.jl` for named state and parameter access, integrated with `DifferentialEquations.jl`. It leverages `@unpack` from `Parameters.jl` for convenient variable extraction. The `ComponentArray` allows for contiguous memory layout while retaining named field access, making it efficient for numerical solvers. ```Julia using ComponentArrays using DifferentialEquations using Parameters: @unpack tspan = (0.0, 20.0) ## Lorenz system function lorenz!(D, u, p, t; f = 0.0) @unpack σ, ρ, β = p @unpack x, y, z = u D.x = σ * (y - x) D.y = x * (ρ - z) - y - f D.z = x * y - β * z return nothing end lorenz_p = (σ = 10.0, ρ = 28.0, β = 8 / 3) lorenz_ic = ComponentArray(x = 0.0, y = 0.0, z = 0.0) lorenz_prob = ODEProblem(lorenz!, lorenz_ic, tspan, lorenz_p) ``` -------------------------------- ### Performing GPU-Accelerated Linear Algebra in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/comparisons/python.md This snippet illustrates how to perform GPU-accelerated linear algebra operations in Julia using the CUDA.jl package. Multiplying "cu(A)" and "cu(B)" automatically leverages the GPU for large-scale computations, simplifying high-performance computing. This requires the CUDA.jl package to be installed. ```Julia cu(A)*cu(B) ``` -------------------------------- ### Unit Mismatch Error Example Source: https://github.com/sciml/scimldocs/blob/main/docs/src/showcase/ode_types.md This snippet is provided as an example of an incorrect operation that Unitful.jl will flag. Attempting to add t (seconds) and sqrt(t) (square root of seconds) results in a unit mismatch error, as these quantities have incompatible dimensions for addition. This highlights the unit-checking capability. ```Julia t + sqrt(t) ``` -------------------------------- ### Simulating Lotka-Volterra Dynamics with ModelingToolkit and DifferentialEquations in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/first_simulation.md This comprehensive Julia code snippet defines, solves, and plots the Lotka-Volterra predator-prey system. It utilizes ModelingToolkit for symbolic equation definition, DifferentialEquations for numerical integration, and Plots.jl for visualization. The snippet sets initial conditions, parameters, and algebraic equations, then solves the ODE over a specified time span and generates two plots: one for individual species populations and another for the total animal count. ```Julia using ModelingToolkit, DifferentialEquations, Plots using ModelingToolkit: t_nounits as t, D_nounits as D # Define our state variables: state(t) = initial condition @variables x(t)=1 y(t)=1 z(t) # Define our parameters @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 # Define the differential equations eqs = [D(x) ~ α * x - β * x * y D(y) ~ -γ * y + δ * x * y z ~ x + y] # Bring these pieces together into an ODESystem with independent variable t @mtkbuild sys = ODESystem(eqs, t) # Convert from a symbolic to a numerical problem to simulate tspan = (0.0, 10.0) prob = ODEProblem(sys, [], tspan) # Solve the ODE sol = solve(prob) # Plot the solution p1 = plot(sol, title = "Rabbits vs Wolves") p2 = plot(sol, idxs = z, title = "Total Animals") plot(p1, p2, layout = (2, 1)) ``` -------------------------------- ### Setting Up and Solving Initial ODE Problem - Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/fit_simulation.md This code combines the Lotka-Volterra ODE function, initial conditions, time span, and parameter guess to define an `ODEProblem` using `DifferentialEquations.jl`. It then solves this problem, saving the solution at every `Δt = 1` to match the experimental data, providing an `initial_sol` for comparison. ```Julia # Set up the ODE problem with our guessed parameter values prob = ODEProblem(lotka_volterra!, u0, tspan, pguess) # Solve the ODE problem with our guessed parameter values initial_sol = solve(prob, saveat = 1) ``` -------------------------------- ### Simulating Long-Term True Dynamics - Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/showcase/missing_physics.md This code defines an `ODEProblem` for the true Lotka-Volterra dynamics (`lotka!`) over the same long time horizon. It solves this problem to get `true_solution_long` and then overlays this true solution onto the existing plot from the estimated dynamics for comparison. ```Julia true_prob = ODEProblem(lotka!, u0, t_long, p_) true_solution_long = solve(true_prob, Tsit5(), saveat = estimate_long.t) plot!(true_solution_long) ``` -------------------------------- ### Importing SciML and Plotting Modules in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/first_simulation.md This Julia code imports the core modules required for the simulation: ModelingToolkit for symbolic modeling, DifferentialEquations for solving ODEs, and Plots for visualization. It also renames `t_nounits` and `D_nounits` from ModelingToolkit to `t` and `D` respectively for convenience in defining equations. ```Julia using ModelingToolkit, DifferentialEquations, Plots using ModelingToolkit: t_nounits as t, D_nounits as D ``` -------------------------------- ### Building an ODESystem from Equations in ModelingToolkit Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/first_simulation.md This code uses the `@mtkbuild` macro to construct an `ODESystem` object from the previously defined equations (`eqs`) and specifies `t` as the independent variable. This step integrates parameters, variables, and equations into a unified symbolic system, enabling further symbolic manipulation and numerical simulation. ```Julia @mtkbuild sys = ODESystem(eqs, t) ``` -------------------------------- ### Setting Initial Conditions and Parameters for ODE - Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/fit_simulation.md This snippet defines the initial conditions and parameters required for solving the Lotka-Volterra ODE. `u0` sets the initial populations for x and y, `tspan` defines the simulation time interval, and `pguess` provides an initial guess for the Lotka-Volterra model parameters (α, β, δ, γ). ```Julia # Initial condition u0 = [1.0, 1.0] # Simulation interval tspan = (0.0, 10.0) # LV equation parameter. p = [α, β, δ, γ] pguess = [1.0, 1.2, 2.5, 1.2] ``` -------------------------------- ### Defining the Loss Function for UDE Training in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/showcase/missing_physics.md This function calculates the L2 loss between the predicted UDE output and the actual dataset. It calls the `predict` function with the current parameters (`θ`) to get the model's output (`X̂`) and then computes the mean squared error against the true data `Xₙ`. ```Julia function loss(θ) X̂ = predict(θ) mean(abs2, Xₙ .- X̂) end ``` -------------------------------- ### Loading SciML Optimization Packages in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/first_optimization.md This snippet loads the previously added Julia packages: Optimization.jl, OptimizationNLopt.jl, and ForwardDiff.jl. These packages provide the core functionalities for defining optimization problems, accessing various optimizers, and performing automatic differentiation for gradient calculations, respectively, enabling the subsequent steps of the optimization tutorial. ```Julia using Optimization, OptimizationNLopt, ForwardDiff ``` -------------------------------- ### Adding Required SciML Optimization Packages in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/first_optimization.md This code snippet demonstrates how to add the necessary Julia packages for numerical optimization using the Pkg manager. It adds "Optimization", "OptimizationNLopt", and "ForwardDiff" to the current Julia environment, which are prerequisites for defining and solving optimization problems within the SciML ecosystem. ```Julia using Pkg Pkg.add(["Optimization", "OptimizationNLopt", "ForwardDiff"]) ``` -------------------------------- ### Creating an ODEProblem for Numerical Simulation in DifferentialEquations.jl Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/first_simulation.md This snippet converts the symbolic `ODESystem` (`sys`) into a numerical `ODEProblem` suitable for solving with `DifferentialEquations.jl`. It defines the time span for integration as `(0.0, 10.0)` and passes an empty array `[]` to use default initial conditions and parameter values. This prepares the system for numerical approximation. ```Julia tspan = (0.0, 10.0) prob = ODEProblem(sys, [], tspan) ``` -------------------------------- ### Calculating Expected Observable from Monte Carlo Ensemble in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/showcase/optimization_under_uncertainty.md This snippet demonstrates how to compute the expected value of the previously defined observable (obs) using the results from the Monte Carlo simulation. It iterates through each solution in the ensemblesol (the collection of 100 trajectories) and applies the obs function, then calculates the mean of these observed values to get the empirical expectation. ```Julia mean_ensemble = mean([obs(sol, p) for sol in ensemblesol]) ``` -------------------------------- ### Setting Up and Solving an ODE Parameter Optimization Problem in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/fit_simulation.md This snippet demonstrates how to set up and solve an `OptimizationProblem` for fitting ODE parameters. It defines an `OptimizationFunction` using `AutoForwardDiff` for automatic differentiation and initializes the `OptimizationProblem` with an initial parameter guess (`pguess`). The problem is then solved using the `PolyOpt()` solver, incorporating the previously defined `callback` function for monitoring and limiting iterations to `maxiters = 200`. Finally, the optimized parameters are extracted and rounded. ```Julia # Set up the optimization problem with our loss function and initial guess adtype = AutoForwardDiff() pguess = [1.0, 1.2, 2.5, 1.2] optf = OptimizationFunction((x, _) -> loss(x), adtype) optprob = OptimizationProblem(optf, pguess) # Optimize the ODE parameters for best fit to our data pfinal = solve(optprob, PolyOpt(), callback = callback, maxiters = 200) α, β, γ, δ = round.(pfinal, digits = 1) ``` -------------------------------- ### Assessing Global Identifiability with Inputs and Specific Parameters in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/showcase/symbolic_analysis.md This code snippet extends the previous example by introducing two input variables (`u1`, `u2`) into the ODE system. It then performs a global identifiability check specifically for parameters `b` and `c` with a reduced probability of correctness (`p=0.9`), demonstrating how to customize the assessment. ```Julia using StructuralIdentifiability, ModelingToolkit @parameters b c a beta g delta sigma @variables t x1(t) x2(t) x3(t) x4(t) y(t) u1(t) [input = true] u2(t) [input = true] D = Differential(t) eqs = [ D(x1) ~ -b * x1 + 1 / (c + x4), D(x2) ~ a * x1 - beta * x2 - u1, D(x3) ~ g * x2 - delta * x3 + u2, D(x4) ~ sigma * x4 * (g * x2 - delta * x3) / x3 ] measured_quantities = [y1 ~ x1 + x2, y2 ~ x2] # check only 2 parameters to_check = [b, c] ode = ODESystem(eqs, t, name = :GoodwinOsc) global_id = assess_identifiability(ode, measured_quantities = measured_quantities, funcs_to_check = to_check, p = 0.9) # Dict{Num, Symbol} with 2 entries: # b => :globally # c => :globally ``` -------------------------------- ### Defining Loss Function for Neural ODE - Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/showcase/bayesian_neural_ode.md This code defines the `predict_neuralode` function, which takes model parameters `p` and returns the predicted ODE solution. It also defines `loss_neuralode`, which calculates the mean squared error between the predicted solution and the `ode_data`. This loss function will be used to guide the optimization or Bayesian sampling process. ```Julia function predict_neuralode(p) p = p isa ComponentArray ? p : convert(typeof(_p), p) Array(prob_neuralode(u0, p)) end function loss_neuralode(p) pred = predict_neuralode(p) loss = sum(abs2, ode_data .- pred) return loss, pred end ``` -------------------------------- ### Initializing and Solving Lotka-Volterra ODE with Guessed Parameters - Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/fit_simulation.md This snippet sets up the initial conditions (`u0`), the time span (`tspan`), and an initial guess for the Lotka-Volterra parameters (`pguess`). It then creates an `ODEProblem` object using the defined `lotka_volterra!` function and solves it using `DifferentialEquations.jl`, saving the solution at discrete time points for comparison with experimental data. ```Julia u0 = [1.0, 1.0] tspan = (0.0, 10.0) pguess = [1.0, 1.2, 2.5, 1.2] prob = ODEProblem(lotka_volterra!, u0, tspan, pguess) initial_sol = solve(prob, saveat = 1) ``` -------------------------------- ### Setting Up Optimization Problem - Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/fit_simulation.md This snippet initializes the optimization problem by defining the automatic differentiation type using `AutoForwardDiff()`, setting an initial guess for the parameters, and creating an `OptimizationFunction` from the `loss` function. Finally, an `OptimizationProblem` is constructed with the function and initial guess. ```Julia adtype = AutoForwardDiff() pguess = [1.0, 1.2, 2.5, 1.2] optf = OptimizationFunction((x, _) -> loss(x), adtype) optprob = OptimizationProblem(optf, pguess) ``` -------------------------------- ### Defining Parameters for an ODE System in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/first_simulation.md This snippet defines symbolic parameters for a differential equation system using the `@parameters` macro. These parameters, `α`, `β`, `γ`, and `δ`, are initialized with specific numerical values, which can be overridden later. This is a foundational step for building symbolic models in ModelingToolkit. ```Julia @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 ``` -------------------------------- ### Defining a Linear ODE Problem with Float64 Source: https://github.com/sciml/scimldocs/blob/main/docs/src/showcase/ode_types.md Initializes a linear ordinary differential equation (ODE) problem using DifferentialEquations.jl. It defines the function f(u, p, t) = p * u, sets the initial condition u0 to 1/2 (which defaults to Float64), the time span tspan to (0.0, 1.0), and a parameter p to 1.01. This setup uses standard Float64 precision. ```Julia using DifferentialEquations f(u, p, t) = p * u prob_ode_linear = ODEProblem(f, 1 / 2, (0.0, 1.0), 1.01); ``` -------------------------------- ### Plotting All States of an ODE Solution in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/first_simulation.md This code generates a plot of all primary states (`x` and `y`) from the `sol` object, representing 'Rabbits vs Wolves'. The `plot` function automatically visualizes the time series data. The `title` argument adds a descriptive title to the plot. ```Julia p1 = plot(sol, title = "Rabbits vs Wolves") ``` -------------------------------- ### Generating LaTeX from Equations in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/first_simulation.md This snippet demonstrates how to convert the defined symbolic equations (`eqs`) into a LaTeX string using the `Latexify.jl` package. It requires `Latexify` to be added and imported first. This is useful for rendering mathematical expressions in a visually appealing format. ```Julia using Latexify # add the package first latexify(eqs) ``` -------------------------------- ### Defining Loss Function for Neural Differential Equation Training in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/showcase/blackhole.md This snippet defines the objective (loss) function used for training the neural differential equations. It calculates the difference between the predicted gravitational waveform (generated by the current neural network parameters) and the observed waveform, using the sum of squared absolute differences as the loss metric. This function guides the optimization process. ```Julia function loss(NN_params) first_obs_to_use_for_training = 1 last_obs_to_use_for_training = length(waveform) obs_to_use_for_training = first_obs_to_use_for_training:last_obs_to_use_for_training pred = Array(solve( prob_nn, RK4(), u0 = u0, p = NN_params, saveat = tsteps, dt = dt, adaptive = false)) pred_waveform = compute_waveform(dt_data, pred, mass_ratio, model_params)[1] loss = ( sum(abs2, view(waveform,obs_to_use_for_training) .- view(pred_waveform,obs_to_use_for_training) ) ) return loss end ``` -------------------------------- ### Defining Differential and Algebraic Equations in ModelingToolkit Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/first_simulation.md This code block defines the system of differential and algebraic equations (DAE) using ModelingToolkit's syntax. `D(x)` represents the time derivative of `x`, and `~` denotes equation equality. The system includes two differential equations for `x` and `y`, and one algebraic equation for `z`, which will be treated as an observable. ```Julia eqs = [D(x) ~ α * x - β * x * y D(y) ~ -γ * y + δ * x * y z ~ x + y] ``` -------------------------------- ### Defining ODE Problem with Unitful Quantities (Initial Attempt) Source: https://github.com/sciml/scimldocs/blob/main/docs/src/showcase/ode_types.md Attempts to define an ODE problem where the initial condition u0 is 1.5u"N" (Newtons) and the time span uses u"s" (seconds). The function f(u, p, t) = 0.5 * u is defined. This setup will lead to a unit mismatch error because f must represent a rate (change in u per unit time), and 0.5 * u does not have the correct units of N/s. ```Julia using DifferentialEquations f(u, p, t) = 0.5 * u u0 = 1.5u"N" prob = ODEProblem(f, u0, (0.0u"s", 1.0u"s")) #sol = solve(prob,Tsit5()) ``` -------------------------------- ### Plotting Initial Model Prediction Against Experimental Data - Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/fit_simulation.md This snippet visualizes the solution obtained from the Lotka-Volterra model using the initial guessed parameters. It plots the predicted rabbit and wolf populations over time and overlays the experimental data points for comparison, highlighting the discrepancy between the initial model and the observed data before optimization. ```Julia plt = plot(initial_sol, label = ["x Prediction" "y Prediction"]) scatter!(plt, t_data, xy_data', label = ["x Data" "y Data"]) ``` -------------------------------- ### Solving a Nonlinear System with ModelingToolkit and NonlinearSolve (Julia) Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/find_root.md This snippet demonstrates the complete workflow for solving a nonlinear system. It imports necessary packages, defines symbolic variables and parameters, sets up the system equations, builds a NonlinearSystem object, converts it into a NonlinearProblem, solves it using the NewtonRaphson algorithm, and displays the solution and residuals. ```Julia # Import the packages using ModelingToolkit, NonlinearSolve # Define the nonlinear system @variables x=1.0 y=0.0 z=0.0 @parameters σ=10.0 ρ=26.0 β=8 / 3 eqs = [0 ~ σ * (y - x), 0 ~ x * (ρ - z) - y, 0 ~ x * y - β * z] @mtkbuild ns = NonlinearSystem(eqs, [x, y, z], [σ, ρ, β]) # Convert the symbolic system into a numerical system prob = NonlinearProblem(ns, []) # Solve the numerical problem sol = solve(prob, NewtonRaphson()) # Analyze the solution @show sol[[x, y, z]], sol.resid ``` -------------------------------- ### Initializing Finite Difference Discretization with MethodOfLines.jl (Julia) Source: https://github.com/sciml/scimldocs/blob/main/docs/src/showcase/brusselator.md This snippet initializes the MOLFiniteDifference construct for spatial discretization. It defines the grid resolution (N), calculates step sizes (dx, dy) for x and y dimensions, and sets the approximation order. The discretization object is configured for center alignment. ```Julia N = 32 dx = (x_max - x_min) / N dy = (y_max - y_min) / N order = 2 discretization = MOLFiniteDifference([x => dx, y => dy], t, approx_order = order, grid_align = center_align) ``` -------------------------------- ### Loading Dependencies and Defining Experimental Data - Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/fit_simulation.md This snippet loads necessary SciML and general Julia packages for differential equations, optimization, sensitivity analysis, automatic differentiation, and plotting. It then defines the experimental time series data (`t_data`) and corresponding rabbit (`x_data`) and wolf (`y_data`) population data, combining them into `xy_data` for convenience. ```Julia using DifferentialEquations, Optimization, OptimizationPolyalgorithms, SciMLSensitivity using ForwardDiff, Plots # Define experimental data t_data = 0:10 x_data = [1.000 2.773 6.773 0.971 1.886 6.101 1.398 1.335 4.353 3.247 1.034] y_data = [1.000 0.259 2.015 1.908 0.323 0.629 3.458 0.508 0.314 4.547 0.906] xy_data = vcat(x_data, y_data) ``` -------------------------------- ### Running Initial Optimization with ADAM for UDE in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/showcase/missing_physics.md This code performs the first stage of optimization using the ADAM optimizer. It solves the `optprob` with `OptimizationOptimisers.Adam()`, applies the defined `callback` function, and sets a maximum of 5000 iterations. This step aims to find a good general area in the parameter space. ```Julia res1 = Optimization.solve( optprob, OptimizationOptimisers.Adam(), callback = callback, maxiters = 5000) println("Training loss after $(length(losses)) iterations: $(losses[end])") ``` -------------------------------- ### Importing Core Libraries for PINN PDE Solving in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/showcase/pinngpu.md This snippet imports essential Julia packages required for setting up and solving Physics-Informed Neural Networks (PINNs). It includes NeuralPDE for the high-level interface, ModelingToolkit for symbolic PDE definition, Optimization and OptimizationOptimisers for the optimization backend, Lux and LuxCUDA for neural network creation and GPU acceleration, and standard libraries for printing and random number generation. It also initializes a GPU device for subsequent operations. ```Julia # High Level Interface using NeuralPDE import ModelingToolkit: Interval # Optimization Libraries using Optimization, OptimizationOptimisers # Machine Learning Libraries and Helpers using Lux, LuxCUDA, ComponentArrays const gpud = gpu_device() # allocate a GPU device # Standard Libraries using Printf, Random # Plotting using Plots ``` -------------------------------- ### Initializing Julia Packages for Relativistic Black Hole Dynamics Source: https://github.com/sciml/scimldocs/blob/main/docs/src/showcase/blackhole.md This snippet initializes all required Julia packages for the project, including SciML tools like OrdinaryDiffEq and Optimization, standard libraries such as LinearAlgebra, and external libraries like Lux and Plots. It also sets the `gr()` backend for Plots and establishes a stable random number generator (`rng`) for reproducible results, crucial for machine learning and simulation tasks. ```Julia # SciML Tools using OrdinaryDiffEq, ModelingToolkit, DataDrivenDiffEq, SciMLSensitivity, DataDrivenSparse using Optimization, OptimizationOptimisers, OptimizationOptimJL # Standard Libraries using LinearAlgebra, Statistics # External Libraries using ComponentArrays, Lux, Zygote, Plots, StableRNGs, DataFrames, CSV, LineSearches gr() # Set a random seed for reproducible behaviour rng = StableRNG(1111) ``` -------------------------------- ### Defining Parameters for Lotka-Volterra Model in ModelingToolkit (Julia) Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/first_simulation.md This Julia snippet defines the parameters for the Lotka-Volterra predator-prey model using ModelingToolkit's `@parameters` macro. It assigns default numerical values to `α`, `β`, `γ`, and `δ`, which represent growth rates, predation rates, and conversion efficiencies within the system. ```Julia @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 ``` -------------------------------- ### Solving Rosenbrock Optimization Problem with SciML and NLopt in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/first_optimization.md This snippet demonstrates the complete workflow for solving the Rosenbrock optimization problem using Optimization.jl, OptimizationNLopt.jl, and ForwardDiff.jl. It defines the objective function L, initializes parameters and an initial guess u0, creates an OptimizationProblem with lower and upper bounds, and then solves it using the NLopt.LD_LBFGS algorithm. The solution sol.u and the minimized loss L(sol.u, p) are then displayed. ```Julia # Import the package using Optimization, OptimizationNLopt, ForwardDiff # Define the problem to optimize L(u, p) = (p[1] - u[1])^2 + p[2] * (u[2] - u[1]^2)^2 u0 = zeros(2) p = [1.0, 100.0] optfun = OptimizationFunction(L, Optimization.AutoForwardDiff()) prob = OptimizationProblem(optfun, u0, p, lb = [-1.0, -1.0], ub = [1.0, 1.0]) # Solve the optimization problem sol = solve(prob, NLopt.LD_LBFGS()) # Analyze the solution @show sol.u, L(sol.u, p) ``` -------------------------------- ### Solving an ODEProblem in DifferentialEquations.jl Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/first_simulation.md This code solves the previously defined `ODEProblem` (`prob`) using the `solve` function from `DifferentialEquations.jl`. The solver automatically selects an appropriate algorithm based on the problem type. The result, `sol`, contains the numerical solution, including time series data for all states. ```Julia sol = solve(prob) ``` -------------------------------- ### Visualizing Experimental Data - Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/fit_simulation.md This snippet uses the `Plots.jl` package to create a scatter plot of the experimental rabbit and wolf population data against time. It helps visualize the raw data that the simulation will be fitted to, providing an initial understanding of the dataset. ```Julia scatter(t_data, xy_data', label = ["x Data" "y Data"]) ``` -------------------------------- ### Defining Lotka-Volterra System with Emoji Variables in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/first_simulation.md This snippet demonstrates how to define a Lotka-Volterra predator-prey system using Julia's ModelingToolkit.jl and DifferentialEquations.jl. It highlights Julia's unique ability to use Unicode emojis (🐰 for rabbits, 🐺 for wolves) as variable names. The system's differential equations are set up, an ODESystem is built, and an ODEProblem is created for simulation. ```Julia using ModelingToolkit, DifferentialEquations @parameters α=1.5 β=1.0 γ=3.0 δ=1.0 @variables t 🐰(t)=1.0 🐺(t)=1.0 D = Differential(t) eqs = [D(🐰) ~ α * 🐰 - β * 🐰 * 🐺, D(🐺) ~ -γ * 🐺 + δ * 🐰 * 🐺] @mtkbuild sys = ODESystem(eqs, t) prob = ODEProblem(sys, [], (0.0, 10.0)) sol = solve(prob) ``` -------------------------------- ### Optimizing ODE Parameters - Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/fit_simulation.md This code executes the optimization process to find the best-fit ODE parameters. It uses the `solve` function from `Optimization.jl` with the `optprob`, specifying `PolyOpt()` as the optimization algorithm, including the `callback` function for progress monitoring, and setting a maximum of 200 iterations. The final parameters are then rounded. ```Julia pfinal = solve(optprob, PolyOpt(), callback = callback, maxiters = 200) α, β, γ, δ = round.(pfinal, digits = 1) ``` -------------------------------- ### Displaying Julia Version and System Information Source: https://github.com/sciml/scimldocs/blob/main/docs/src/index.md This snippet utilizes the InteractiveUtils module to print detailed information about the Julia version, operating system, and hardware. This is crucial for debugging and ensuring reproducibility across different environments. ```Julia using InteractiveUtils # hide versioninfo() # hide ``` -------------------------------- ### Adding SciML Packages (Julia) Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/find_root.md This code snippet shows how to add the required Julia packages, ModelingToolkit and NonlinearSolve, to the current project environment using Julia's built-in package manager, Pkg. This is a prerequisite for using the SciML ecosystem for nonlinear problem solving. ```Julia using Pkg Pkg.add(["ModelingToolkit", "NonlinearSolve"]) ``` -------------------------------- ### Plotting Initial ODE Solution and Data - Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/fit_simulation.md This snippet visualizes the initial guessed solution of an Ordinary Differential Equation (ODE) alongside the observed training data. It uses `plot` to display the predicted `x` and `y` values and `scatter!` to overlay the actual `x` and `y` data points, allowing for a visual comparison of the initial model fit. ```Julia plt = plot(initial_sol, label = ["x Prediction" "y Prediction"]) scatter!(plt, t_data, xy_data', label = ["x Data" "y Data"]) ``` -------------------------------- ### Creating a NonlinearProblem with Default Values (Julia) Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/find_root.md This code converts the symbolic NonlinearSystem (ns) into a numerical NonlinearProblem object. By passing an empty array [] as the second argument, it indicates that the default initial conditions and parameter values defined in the symbolic system should be used. ```Julia # Convert the symbolic system into a numerical system prob = NonlinearProblem(ns, []) ``` -------------------------------- ### Generating Dynamic Links to Project Configuration Files in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/index.md This Julia snippet dynamically constructs URLs for the Project.toml and Manifest.toml files hosted on GitHub. It reads the project's version and name from Project.toml to create accurate links, facilitating easy access to project configuration for reproducibility. ```Julia using TOML using Markdown version = TOML.parse(read("../../Project.toml", String))["version"] name = TOML.parse(read("../../Project.toml", String))["name"] link_manifest = "https://github.com/SciML/" * name * ".jl/tree/gh-pages/v" * version * "/assets/Manifest.toml" link_project = "https://github.com/SciML/" * name * ".jl/tree/gh-pages/v" * version * "/assets/Project.toml" Markdown.parse("""You can also download the\n[manifest]($link_manifest)\nfile and the\n[project]($link_project)\nfile.\n""") ``` -------------------------------- ### Importing ModelingToolkit and NonlinearSolve (Julia) Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/find_root.md This snippet imports the ModelingToolkit and NonlinearSolve packages into the current Julia session. These packages provide the necessary functionalities for symbolic modeling and numerical solving of nonlinear equations, respectively. ```Julia # Import the packages using ModelingToolkit, NonlinearSolve ``` -------------------------------- ### Analyzing Optimization Solution and Final Loss in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/first_optimization.md This snippet analyzes the obtained optimization solution. It accesses the optimal variable values via `sol.u` and then re-evaluates the original loss function `L` at these optimal values and the given parameters `p` to display the final minimized loss, providing the key results of the optimization. ```Julia # Analyze the solution @show sol.u, L(sol.u, p) ``` -------------------------------- ### Generating Lotka-Volterra Training Data in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/showcase/missing_physics.md This snippet defines the Lotka-Volterra equations and generates synthetic training data for model discovery. It sets up an ODEProblem with initial conditions and parameters, solves it using DifferentialEquations.jl, and then adds Gaussian noise to simulate real-world observations. The `tspan` defines the time range, `u0` the initial state, and `p_` the true parameters. ```Julia function lotka!(du, u, p, t) α, β, γ, δ = p du[1] = α * u[1] - β * u[2] * u[1] du[2] = γ * u[1] * u[2] - δ * u[2] end # Define the experimental parameter tspan = (0.0, 5.0) u0 = 5.0f0 * rand(rng, 2) p_ = [1.3, 0.9, 0.8, 1.8] prob = ODEProblem(lotka!, u0, tspan, p_) solution = solve(prob, Vern7(), abstol = 1e-12, reltol = 1e-12, saveat = 0.25) # Add noise in terms of the mean X = Array(solution) t = solution.t x̄ = mean(X, dims = 2) noise_magnitude = 5e-3 Xₙ = X .+ (noise_magnitude * x̄) .* randn(rng, eltype(X), size(X)) plot(solution, alpha = 0.75, color = :black, label = ["True Data" nothing]) scatter!(t, transpose(Xₙ), color = :red, label = ["Noisy Data" nothing]) ``` -------------------------------- ### Setting Up the Optimization Problem for UDE Training in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/showcase/missing_physics.md This snippet initializes the `OptimizationProblem` for training the UDE. It specifies `Optimization.AutoZygote()` as the automatic differentiation type and constructs an `OptimizationFunction` using the previously defined `loss` function. The problem is then created with the initial parameters `p` wrapped in a `ComponentVector`. ```Julia adtype = Optimization.AutoZygote() optf = Optimization.OptimizationFunction((x, p) -> loss(x), adtype) optprob = Optimization.OptimizationProblem(optf, ComponentVector{Float64}(p)) ``` -------------------------------- ### Combining Multiple Plots with a Custom Layout in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/first_simulation.md This code combines two previously generated plot objects, `p1` and `p2`, into a single figure. The `layout = (2, 1)` argument arranges the plots in two rows and one column, providing a consolidated view of different aspects of the simulation results. ```Julia plot(p1, p2, layout = (2, 1)) ``` -------------------------------- ### Defining OptimizationProblem with Initial Conditions and Bounds in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/first_optimization.md This snippet defines the `OptimizationProblem` by setting the initial guess `u0` to `[0.0, 0.0]` and the parameters `p` to `[1.0, 100.0]`. It also specifies explicit lower (`lb`) and upper (`ub`) bounds for the optimization variables, constraining them within `[-1.0, -1.0]` and `[1.0, 1.0]` respectively. ```Julia u0 = zeros(2) p = [1.0, 100.0] prob = OptimizationProblem(optfun, u0, p, lb = [-1.0, -1.0], ub = [1.0, 1.0]) ``` -------------------------------- ### Solving OptimizationProblem with NLopt.LD_LBFGS in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/first_optimization.md This snippet executes the optimization process by calling the `solve` function. It takes the previously defined `prob` (OptimizationProblem) and specifies `NLopt.LD_LBFGS()` as the chosen solver, which is a robust and performant algorithm from the NLopt library for unconstrained and bound-constrained optimization. ```Julia # Solve the optimization problem sol = solve(prob, NLopt.LD_LBFGS()) ``` -------------------------------- ### Plotting a Specific Observable from an ODE Solution in Julia Source: https://github.com/sciml/scimldocs/blob/main/docs/src/getting_started/first_simulation.md This snippet creates a plot specifically for the observable `z` (Total Animals) from the `sol` object. The `idxs` argument is used to specify which variable to plot, allowing visualization of reconstructed observables that were symbolically eliminated from the core system. ```Julia p2 = plot(sol, idxs = z, title = "Total Animals") ```