Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Builder

Each vector, matrix or operation in diffsol has its own context, which is used to configure the number of batches (as well as other backend-specific information). You can create a new context with a set number of batches using the Context::with_batch method.

    let context = NalgebraContext::with_nbatch(NBATCH);

This context can be passed to the OdeBuilder in order to create a new OdeSolverProblem with the specified number of batches. When setting the parameters, you can pass in a vector of values of length nstates * nbatch, which will be split into nbatch separate state vectors of length nstates.

Note that you can ignore batching when specifying your rhs, init and other equation closures. Each closure will be called once for each batch lane and will handle the specific broadcasting rules used by diffsol. The input arguments for each closure are simply T slices that correspond to a single batch lane, and the return values are mutable T slices of the same length.

    let problem = OdeBuilder::<M>::new()
        .context(context)
        // Two parameters per lane. Parameter blocks are stored end to end:
        // [lane 0 p0, lane 0 p1, lane 1 p0, lane 1 p1, ...].
        .p([1.0, 10.0, 2.0, 20.0, 3.0, 30.0])
        .rhs(|y: &[f64], p: &[f64], _t: f64, dy: &mut [f64]| {
            // y, p, and dy are slices for one batch lane.
            dy[0] = p[0] * y[0] + p[1];
        })
        .init(|p: &[f64], _t: f64, y: &mut [f64]| y[0] = p[1], 1)
        .build()
        .unwrap();

    assert_eq!(problem.context().nbatch(), NBATCH);
    let mut solver = problem.tsit45().unwrap();
    solver.step().unwrap();