### Setup Python Environment and Install Dependencies Source: https://github.com/martinjrobins/diffsol/blob/main/examples/neural-ode-weather-prediction/README.md Create a Python virtual environment and install necessary packages using pip. Ensure you activate the environment before installing. ```bash python -m venv venv source venv/bin/activate pip install -r requirements.txt ``` -------------------------------- ### Install Trunk Build Tool Source: https://github.com/martinjrobins/diffsol/blob/main/book/src/use/webassembly.md Install Trunk, a build tool for single-page Rust applications, if you haven't already. ```bash cargo install trunk ``` -------------------------------- ### Serve mdBook Documentation Source: https://github.com/martinjrobins/diffsol/blob/main/CONTRIBUTING.md Build and serve the mdBook documentation located in the 'book/' directory. This requires installing 'mdbook' first. ```bash # Install mdbook if you haven't already cargo install mdbook # Build and serve the book cd book mdbook serve --open ``` -------------------------------- ### Build and Run the Neural ODE Weather Prediction Example Source: https://github.com/martinjrobins/diffsol/blob/main/examples/neural-ode-weather-prediction/README.md Build and execute the main weather prediction example using Cargo. Ensure the 'onnx' feature is enabled for ONNX model support. Run in release mode for performance. ```bash cargo run -p neural-ode-weather-prediction --features onnx --release ``` -------------------------------- ### Documentation Test Example Source: https://github.com/martinjrobins/diffsol/blob/main/CONTRIBUTING.md Include runnable examples within doc comments using triple backticks. These examples are automatically tested when 'cargo test' is run. ```rust /// Adds two numbers together. /// /// # Examples /// /// ``` /// use diffsol::add; /// assert_eq!(add(2, 2), 4); /// ``` pub fn add(a: i32, b: i32) -> i32 { a + b } ``` -------------------------------- ### Install Rust with rustup Source: https://github.com/martinjrobins/diffsol/blob/main/CONTRIBUTING.md Installs Rust using rustup. Ensure you have the latest stable version after installation. ```bash curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh ``` ```bash rustup update stable ``` -------------------------------- ### Environment Setup and Execution Commands Source: https://github.com/martinjrobins/diffsol/blob/main/examples/lid-driven-cavity-stokes/README.md These bash commands set up the necessary environment variables for LLVM and then execute the simulation pipeline, including exporting the DiffSL tensors, running the main simulation, and plotting the results. ```bash export LLVM_DIR=/usr/lib/llvm-21 export LLVM_SYS_211_PREFIX=/usr/lib/llvm-21 python3 python/export_diffsl_from_firedrake.py cargo run --release --features diffsol-llvm21 python3 python/plot_diffsol_solution.py ``` -------------------------------- ### Build and Serve Yew App Source: https://github.com/martinjrobins/diffsol/blob/main/book/src/use/webassembly.md Use Trunk to build and serve your Yew WebAssembly application. This command compiles the Rust code to Wasm and starts a local development server. ```bash trunk serve ``` -------------------------------- ### Run Benchmarks with Sundials Backend Source: https://github.com/martinjrobins/diffsol/blob/main/diffsol/benches/README.md Execute benchmarks with the sundials backend. This requires sundials to be installed and the 'sundials' feature to be enabled. You may need to specify the library and include directories. ```bash SUNDIALS_LIBRARY_DIR=~/.local/lib SUNDIALS_INCLUDE_DIR=~/.local/include cargo bench --features sundials ``` -------------------------------- ### Create ODE Solvers using OdeSolverProblem Source: https://github.com/martinjrobins/diffsol/blob/main/book/src/solver/creating_a_solver.md This example demonstrates how to create different ODE solvers (Bdf, Sdirk, ExplicitRk) by utilizing the `OdeSolverProblem` struct. It shows how to specify the linear solver to be used with each ODE solver. ```rust use diffsol::{ ode_solver::problem::OdeSolverProblem, linear_solver::nalgebra_lu::NalgebraLU, ode_solver::bdf::Bdf, ode_solver::sdirk::Sdirk, ode_solver::explicit_rk::ExplicitRk, }; fn main() { // Create a BDF solver with NalgebraLU linear solver let bdf_solver = OdeSolverProblem::<_, Bdf>::new(); // Create a SDIRK solver with NalgebraLU linear solver let sdirk_solver = OdeSolverProblem::<_, Sdirk>::new(); // Create an ExplicitRk solver with NalgebraLU linear solver let explicit_rk_solver = OdeSolverProblem::<_, ExplicitRk>::new(); println!("Solvers created successfully!"); } ``` -------------------------------- ### Integration Test Example Source: https://github.com/martinjrobins/diffsol/blob/main/CONTRIBUTING.md Place integration tests in the 'tests/' directory at the root of the crate. These tests can access the crate's public API. ```rust // tests/my_integration_test.rs use diffsol::*; #[test] fn test_integration() { // Your integration test here } ``` -------------------------------- ### Solve Lorenz System with BDF and DiffSL Source: https://github.com/martinjrobins/diffsol/blob/main/README.md Example demonstrating how to solve the Lorenz system of ODEs using the BDF solver and the DiffSL DSL with the LLVM JIT backend. This snippet requires the diffsol crate and its associated features. ```rust use diffsol::{LlvmModule, NalgebraLU, NalgebraMat, OdeBuilder, OdeSolverMethod}; pub fn lorenz() -> Result<(), Box> { let problem = OdeBuilder::>::new().build_from_diffsl::( "\n a { 14.0 } b { 10.0 } c { 8.0 / 3.0 }\n u_i {\n x = 1.0,\n y = 0.0,\n z = 0.0,\n }\n F_i {\n b * (y - x);\n x * (a - z) - y;\n x * y - c * z;\n }\n ", )?; let mut solver = problem.bdf::>()?; let (_ys, _ts, _stop_reason) = solver.solve(0.0)?; Ok(()) } ``` -------------------------------- ### Unit Test Example Source: https://github.com/martinjrobins/diffsol/blob/main/CONTRIBUTING.md Add unit tests within the same file as the code, typically in a nested 'tests' module. Use '#[cfg(test)]' to ensure they are only compiled during testing. ```rust #[cfg(test)] mod tests { use super::*; #[test] fn test_my_function() { assert_eq!(my_function(2), 4); } } ``` -------------------------------- ### Phase Plane Plot for Varying Initial Conditions Source: https://github.com/martinjrobins/diffsol/blob/main/book/src/primer/population_dynamics.md Generates phase plane plots for the Lotka-Volterra model with varying initial predator and prey populations. This helps visualize how different starting points affect system dynamics. Uses Diffsol and DiffSL. ```rust let mut problem = Problem::new(); problem.add_ode( "dx/dt", "a * x - b * x * y", &["a", "b", "x", "y"], ); problem.add_ode( "dy/dt", "c * x * y - d * y", &["c", "d", "x", "y"], ); let mut params = HashMap::new(); params.insert("a", 1.5); params.insert("b", 1.0); params.insert("c", 3.0); params.insert("d", 1.0); let mut t_eval = vec![0.0]; let mut current_t = 0.0; while current_t < 10.0 { current_t += 0.1; t_eval.push(current_t); } for y0 in [0.5, 1.0, 1.5, 2.0] { let mut initial_conditions = HashMap::new(); initial_conditions.insert("x", y0); initial_conditions.insert("y", y0); let solution = problem.solve(¶ms, &initial_conditions, &t_eval); solution.plot(&format!("prey-predator-{}.png", y0)); } ``` -------------------------------- ### Build Documentation for All Features Source: https://github.com/martinjrobins/diffsol/blob/main/CONTRIBUTING.md Generate API documentation that includes all features of the project and open it in your browser by using the '--all-features' flag with 'cargo doc'. ```bash cargo doc --all-features --open ``` -------------------------------- ### Install pydiffsol Package Source: https://github.com/martinjrobins/diffsol/blob/main/book/src/use/python.md Install the pydiffsol Python package using pip. This command is used to add the library to your Python environment. ```bash pip install pydiffsol ``` -------------------------------- ### Run Code Coverage Locally Source: https://github.com/martinjrobins/diffsol/blob/main/CONTRIBUTING.md To run code coverage locally with tarpaulin, first install it using 'cargo install cargo-tarpaulin', then execute the command with any necessary features. ```bash cargo install cargo-tarpaulin ``` ```bash cargo tarpaulin --features diffsl-cranelift ``` -------------------------------- ### Run All Benchmarks Source: https://github.com/martinjrobins/diffsol/blob/main/diffsol/benches/README.md Execute all benchmarks using the default configuration. This is the standard command for running benchmarks. ```bash cargo bench ``` -------------------------------- ### Build and Open API Documentation Source: https://github.com/martinjrobins/diffsol/blob/main/CONTRIBUTING.md Build the API documentation for the project locally and automatically open it in your web browser using 'cargo doc --open'. ```bash cargo doc --open ``` -------------------------------- ### Initialize Electrical Circuit Plot Source: https://github.com/martinjrobins/diffsol/blob/main/book/src/primer/images/electrical-circuit.html Initializes a new Plotly plot for an electrical circuit. This is the starting point for rendering circuit diagrams. ```javascript Plotly.newPlot("electrical-circuit", ``` -------------------------------- ### Set LLVM Environment Variables Source: https://github.com/martinjrobins/diffsol/blob/main/CONTRIBUTING.md Sets LLVM environment variables for specific diffsl-llvm features. Adjust paths based on your LLVM installation. ```bash export LLVM_SYS_170_PREFIX=/usr/lib/llvm-17 export LLVM_DIR=/usr/lib/llvm-17 ``` -------------------------------- ### Run Tests in Main Crate Only Source: https://github.com/martinjrobins/diffsol/blob/main/CONTRIBUTING.md To run tests only in the main crate and exclude examples, use the '-p diffsol' flag with 'cargo test'. ```bash cargo test -p diffsol ``` -------------------------------- ### Interpolate Solution at Specific Time Source: https://github.com/martinjrobins/diffsol/blob/main/book/src/solve/interpolation.md Use `step` to advance the solver past the target time, then `interpolate` to get the solution at that exact time. ```rust use diffsol::prelude::*; fn main() { let mut solver = Logistic::new(); let t0 = 0.0; let tf = 10.0; let dt = 0.1; let mut ts = vec![]; let mut ys = vec![]; // Step until just beyond t_interpolate let t_interpolate = 5.3; solver.step(t0, tf, dt, |t, y| { if t >= t_interpolate { return false; } // Stop stepping just after t_interpolate ts.push(t); ys.push(y); true }); // Interpolate at t_interpolate let y_interpolate = solver.interpolate(t_interpolate); // Continue stepping to the end if needed solver.step(t_interpolate, tf, dt, |t, y| { ts.push(t); ys.push(y); true }); // You can now use y_interpolate and the rest of the solution println!("Interpolated value at t={}: {:?}", t_interpolate, y_interpolate); } ``` -------------------------------- ### Rust: DiffSL code for Logistic Equation Source: https://github.com/martinjrobins/diffsol/blob/main/book/src/use/c.md This snippet defines the DiffSL code for the logistic ODE. It is used within the Rust dynamic dispatch API example. ```rust fn logistic_code() -> String { "dy/dt = r * y * (1 - y)".to_string() } ``` -------------------------------- ### Rust: Create OdeWrapper with dynamic dispatch Source: https://github.com/martinjrobins/diffsol/blob/main/book/src/use/c.md Demonstrates creating an OdeWrapper instance using dynamic dispatch. It configures the JIT backend, scalar type, and ODE solver method at runtime. ```rust let ode_wrapper = OdeWrapper::new( JitBackendType::Cranelift, ScalarType::F64, OdeSolverType::Bdf, LinearSolverType::Dense, logistic_code(), ); ``` -------------------------------- ### Clone and Set Up diffsol Repository Source: https://github.com/martinjrobins/diffsol/blob/main/CONTRIBUTING.md Clones your forked diffsol repository and adds the upstream remote for synchronization. ```bash git clone https://github.com/YOUR-USERNAME/diffsol.git cd diffsol ``` ```bash git remote add upstream https://github.com/martinjrobins/diffsol.git ``` -------------------------------- ### Rust: Configure solver tolerances Source: https://github.com/martinjrobins/diffsol/blob/main/book/src/use/c.md Shows how to set the absolute and relative tolerances for the ODE solver using the OdeWrapper. This is crucial for controlling the accuracy of the solution. ```rust ode_wrapper.set_tolerances(1e-6, 1e-8); ``` -------------------------------- ### Solve Lotka-Volterra ODEs with Diffsol Source: https://github.com/martinjrobins/diffsol/blob/main/book/src/primer/population_dynamics.md Solves the Lotka-Volterra predator-prey model using the Diffsol crate. Requires DiffSL for problem specification. Ensure correct setup of parameters and initial conditions. ```rust let mut problem = Problem::new(); problem.add_ode( "dx/dt", "a * x - b * x * y", &["a", "b", "x", "y"], ); problem.add_ode( "dy/dt", "c * x * y - d * y", &["c", "d", "x", "y"], ); let mut params = HashMap::new(); params.insert("a", 1.5); params.insert("b", 1.0); params.insert("c", 3.0); params.insert("d", 1.0); let mut initial_conditions = HashMap::new(); initial_conditions.insert("x", 1.0); initial_conditions.insert("y", 1.0); let mut t_eval = vec![0.0]; let mut current_t = 0.0; while current_t < 10.0 { current_t += 0.1; t_eval.push(current_t); } let solution = problem.solve(¶ms, &initial_conditions, &t_eval); solution.plot("prey-predator.png"); ``` -------------------------------- ### Run Benchmarks with Diffsl-LLVM Backend Source: https://github.com/martinjrobins/diffsol/blob/main/diffsol/benches/README.md Run benchmarks specifically using the diffsl-llvm backend. Requires setting LLVM directory environment variables and enabling the corresponding feature flag. ```bash LLVM_DIR=/usr/lib/llvm-21 LLVM_SYS_211_PREFIX=/usr/lib/llvm-21 cargo bench --features diffsl-llvm21 ``` -------------------------------- ### Create Interactive Diffsol Solver Source: https://github.com/martinjrobins/diffsol/blob/main/book/src/use/images/webassembly-jit.html Initializes an interactive differential equation solver using WebAssembly. Configure differential equations, sliders for parameters, and output variables. Requires a div element with the ID 'solver' for rendering. ```javascript const { createInteractiveSolver, MatrixType, LinearSolverType, OdeSolverType } = window.diffsol; createInteractiveSolver({ divId: 'solver', diffslCode: `in_i { y0 = 1.0 } a { 2.0/3.0 } b { 4.0/3.0 } c { 1.0 } d { 1.0 } u_i { y1 = y0, y2 = y0, } F_i { a * y1 - b * y1 * y2, c * y1 * y2 - d * y2, }`, sliders: { y0: { label: 'Initial Value (y0)', min: 0.1, max: 5, initial: 1, }, }, outputs: { y1: { label: 'prey (y1)', }, y2: { label: 'predator (y2)', }, }, moduleConfig: { backendUrl: 'https://diffsol-js.fly.dev', }, finalTime: 20.0, matrixType: MatrixType.FaerDense, linearSolverType: LinearSolverType.Lu, showCodeEditor: true, }); ```