RustedSciThe 0.4.19

Rust framework for symbolic and numerical computing:BVP ( Newton-Raphson frozen/damped/with collocations ), IVP( BDF, Radau, Backward Euler, LSODE, LSODA, RK45, DoPri), nonlinear equations ( Levenberg, Gavin) and more
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Residual and Jacobian callback contracts for the new Radau solver.
//!
//! The eventual implementation will keep symbolic preparation outside the
//! step loop and make callback failures part of the typed solver error path.

use super::error::{RadauConfigError, RadauError, RadauStage, RadauUnsupportedRoute};
use super::{
    aot::{AotPlan, AotWorkspace},
    atom_native::{AtomNativePlan, AtomNativeWorkspace},
    config::{RadauAssembly, RadauMatrixLayout},
    lambdify::{LambdifyPlan, LambdifyWorkspace},
    telemetry::{RadauFrontendStage, RadauTelemetry, RadauTelemetryMode},
};
use crate::symbolic::ivp_telemetry::IvpLambdifyExecutionPolicy;
use crate::symbolic::symbolic_engine::Expr;

/// The symbolic frontend is selected once during preparation.
///
/// The numerical core only sees this common callback boundary and never
/// branches on the symbolic representation inside a Newton step.  Keeping the
/// prepared plan separate from its mutable workspace is what makes parameter
/// rebind and continuation possible without repeating symbolic construction.
pub(crate) enum PreparedSymbolicCallbacks {
    ExprLegacy(LambdifyPlan),
    AtomViewNative(AtomNativePlan),
    Aot(AotPlan),
}

/// Scratch storage paired with the prepared symbolic frontend.
///
/// These buffers belong to the callback/session layer, not to the Radau
/// Newton workspace.  That ownership boundary lets telemetry attribute
/// symbolic preparation and callback binding separately from linear algebra.
pub(crate) enum SymbolicCallbackWorkspace {
    ExprLegacy(LambdifyWorkspace),
    AtomViewNative(AtomNativeWorkspace),
    Aot(AotWorkspace),
}

/// Mutable callback binding for one solve/continuation session.
///
/// The prepared frontend remains immutable and can be shared by multiple
/// sessions. Parameter values and evaluator scratch are isolated per session;
/// value-only rebinds therefore do not rebuild symbolic closures.
pub(crate) struct SymbolicCallbackSession<'a> {
    callbacks: &'a PreparedSymbolicCallbacks,
    workspace: SymbolicCallbackWorkspace,
    /// Cached once so Sparse/Banded preparation is not repeated per step.
    jacobian_pattern: Vec<(usize, usize)>,
    parameters: Vec<f64>,
    generation: u64,
    telemetry: RadauTelemetry,
    execution_policy: IvpLambdifyExecutionPolicy,
}

impl PreparedSymbolicCallbacks {
    /// Prepare one selected symbolic frontend and its immutable callback plan.
    pub(crate) fn prepare(
        assembly: RadauAssembly,
        residual: Vec<Expr>,
        jacobian: Option<Vec<Expr>>,
        independent_variable: &str,
        variables: &[&str],
        parameters: &[&str],
    ) -> Result<Self, RadauError> {
        let mut telemetry = RadauTelemetry::new(Default::default());
        Self::prepare_with_telemetry(
            assembly,
            residual,
            jacobian,
            independent_variable,
            variables,
            parameters,
            &mut telemetry,
        )
    }

    /// Prepare a frontend while collecting detailed cold-stage telemetry.
    pub(crate) fn prepare_with_telemetry(
        assembly: RadauAssembly,
        residual: Vec<Expr>,
        jacobian: Option<Vec<Expr>>,
        independent_variable: &str,
        variables: &[&str],
        parameters: &[&str],
        telemetry: &mut RadauTelemetry,
    ) -> Result<Self, RadauError> {
        Self::prepare_with_telemetry_and_policy(
            assembly,
            residual,
            jacobian,
            independent_variable,
            variables,
            parameters,
            telemetry,
            IvpLambdifyExecutionPolicy::Sequential,
        )
    }

    /// Prepare one frontend while retaining the selected callback scheduling policy.
    pub(crate) fn prepare_with_telemetry_and_policy(
        assembly: RadauAssembly,
        residual: Vec<Expr>,
        jacobian: Option<Vec<Expr>>,
        independent_variable: &str,
        variables: &[&str],
        parameters: &[&str],
        telemetry: &mut RadauTelemetry,
        execution_policy: IvpLambdifyExecutionPolicy,
    ) -> Result<Self, RadauError> {
        match assembly {
            RadauAssembly::ExprLegacy => {
                telemetry.measure_frontend(RadauFrontendStage::ExprLegacyPrepare, || {
                    LambdifyPlan::from_expressions_with_policy(
                        residual,
                        jacobian,
                        independent_variable,
                        variables,
                        parameters,
                        execution_policy,
                    )
                    .map(Self::ExprLegacy)
                })
            }
            RadauAssembly::AtomViewNative => {
                let telemetry_mode = telemetry.mode();
                let result = telemetry.measure_frontend(RadauFrontendStage::AtomConversion, || {
                    AtomNativePlan::from_expressions_with_telemetry_and_policy(
                        residual,
                        jacobian,
                        independent_variable,
                        variables,
                        parameters,
                        telemetry_mode,
                        execution_policy,
                    )
                });
                result.map(|plan| {
                    plan.absorb_preparation_telemetry(telemetry);
                    Self::AtomViewNative(plan)
                })
            }
        }
    }

    /// Allocate session-local callback buffers without rebuilding the plan.
    pub(crate) fn workspace(&self) -> SymbolicCallbackWorkspace {
        self.workspace_with_telemetry(RadauTelemetryMode::Off)
    }

    /// Allocate workspace and, for AOT, the shared runtime telemetry stream.
    pub(crate) fn workspace_with_telemetry(
        &self,
        mode: RadauTelemetryMode,
    ) -> SymbolicCallbackWorkspace {
        match self {
            Self::ExprLegacy(plan) => SymbolicCallbackWorkspace::ExprLegacy(plan.workspace()),
            Self::AtomViewNative(plan) => {
                SymbolicCallbackWorkspace::AtomViewNative(plan.workspace())
            }
            Self::Aot(plan) => SymbolicCallbackWorkspace::Aot(plan.workspace_with_telemetry(mode)),
        }
    }

    pub(crate) fn execution_policy(&self) -> IvpLambdifyExecutionPolicy {
        match self {
            Self::ExprLegacy(plan) => plan.execution_policy(),
            Self::AtomViewNative(plan) => plan.execution_policy(),
            Self::Aot(plan) => plan.execution_policy(),
        }
    }

    /// Create a callback session with telemetry disabled.
    pub(crate) fn session(&self) -> SymbolicCallbackSession<'_> {
        self.session_with_telemetry(RadauTelemetryMode::Off)
    }

    /// Create a session with caller-selected telemetry and isolated buffers.
    pub(crate) fn session_with_telemetry(
        &self,
        mode: RadauTelemetryMode,
    ) -> SymbolicCallbackSession<'_> {
        let parameter_count = match self {
            Self::ExprLegacy(plan) => plan.parameter_count(),
            Self::AtomViewNative(plan) => plan.parameter_count(),
            Self::Aot(plan) => plan.parameter_count(),
        };
        SymbolicCallbackSession {
            callbacks: self,
            workspace: self.workspace_with_telemetry(mode),
            jacobian_pattern: self.jacobian_pattern(),
            parameters: vec![0.0; parameter_count],
            generation: 1,
            telemetry: RadauTelemetry::new(mode),
            execution_policy: self.execution_policy(),
        }
    }

    pub(crate) fn evaluate_residual(
        &self,
        t: f64,
        state: &[f64],
        parameters: &[f64],
        output: &mut [f64],
        workspace: &mut SymbolicCallbackWorkspace,
    ) -> Result<(), RadauError> {
        let mut telemetry = RadauTelemetry::new(RadauTelemetryMode::Off);
        self.evaluate_residual_with_telemetry(
            t,
            state,
            parameters,
            output,
            workspace,
            &mut telemetry,
        )
    }

    pub(crate) fn evaluate_residual_with_telemetry(
        &self,
        t: f64,
        state: &[f64],
        parameters: &[f64],
        output: &mut [f64],
        workspace: &mut SymbolicCallbackWorkspace,
        telemetry: &mut RadauTelemetry,
    ) -> Result<(), RadauError> {
        match (self, workspace) {
            (Self::ExprLegacy(plan), SymbolicCallbackWorkspace::ExprLegacy(workspace)) => plan
                .evaluate_residual_with_telemetry(
                    t, state, parameters, output, workspace, telemetry,
                ),
            (Self::AtomViewNative(plan), SymbolicCallbackWorkspace::AtomViewNative(workspace)) => {
                plan.evaluate_residual_with_telemetry(
                    t, state, parameters, output, workspace, telemetry,
                )
            }
            (Self::Aot(plan), SymbolicCallbackWorkspace::Aot(workspace)) => {
                plan.evaluate_residual(t, state, parameters, output, workspace, telemetry)
            }
            _ => Err(RadauConfigError::CallbackFrontendMismatch.into()),
        }
    }

    pub(crate) fn evaluate_jacobian(
        &self,
        t: f64,
        state: &[f64],
        parameters: &[f64],
        output: &mut [f64],
        workspace: &mut SymbolicCallbackWorkspace,
    ) -> Result<(), RadauError> {
        let mut telemetry = RadauTelemetry::new(RadauTelemetryMode::Off);
        self.evaluate_jacobian_with_telemetry(
            t,
            state,
            parameters,
            output,
            workspace,
            &mut telemetry,
        )
    }

    pub(crate) fn evaluate_jacobian_with_telemetry(
        &self,
        t: f64,
        state: &[f64],
        parameters: &[f64],
        output: &mut [f64],
        workspace: &mut SymbolicCallbackWorkspace,
        telemetry: &mut RadauTelemetry,
    ) -> Result<(), RadauError> {
        match (self, workspace) {
            (Self::ExprLegacy(plan), SymbolicCallbackWorkspace::ExprLegacy(workspace)) => plan
                .evaluate_jacobian_with_telemetry(
                    t, state, parameters, output, workspace, telemetry,
                ),
            (Self::AtomViewNative(plan), SymbolicCallbackWorkspace::AtomViewNative(workspace)) => {
                plan.evaluate_jacobian_with_telemetry(
                    t, state, parameters, output, workspace, telemetry,
                )
            }
            (Self::Aot(plan), SymbolicCallbackWorkspace::Aot(workspace)) => plan.evaluate_jacobian(
                t,
                state,
                parameters,
                RadauMatrixLayout::Dense,
                output,
                workspace,
                telemetry,
            ),
            _ => Err(RadauConfigError::CallbackFrontendMismatch.into()),
        }
    }

    /// Return the structural pattern needed to construct a native backend.
    ///
    /// The returned vector is a preparation-time snapshot.  The numerical
    /// step later borrows the session's cached pattern and writes only values.
    pub(crate) fn jacobian_pattern(&self) -> Vec<(usize, usize)> {
        match self {
            Self::ExprLegacy(plan) => plan.jacobian_pattern().to_vec(),
            Self::AtomViewNative(plan) => plan.jacobian_pattern(),
            Self::Aot(plan) => plan.jacobian_pattern().to_vec(),
        }
    }

    /// Evaluate directly in the matrix layout requested by the future Radau
    /// linear backend. Dense uses row-major values; Sparse uses the prepared
    /// immutable entry order; Banded uses compact `(kl + ku + 1) x n` storage.
    pub(crate) fn evaluate_jacobian_layout(
        &self,
        t: f64,
        state: &[f64],
        parameters: &[f64],
        layout: RadauMatrixLayout,
        output: &mut [f64],
        workspace: &mut SymbolicCallbackWorkspace,
    ) -> Result<(), RadauError> {
        let mut telemetry = RadauTelemetry::new(RadauTelemetryMode::Off);
        self.evaluate_jacobian_layout_with_telemetry(
            t,
            state,
            parameters,
            layout,
            output,
            workspace,
            &mut telemetry,
        )
    }

    pub(crate) fn evaluate_jacobian_layout_with_telemetry(
        &self,
        t: f64,
        state: &[f64],
        parameters: &[f64],
        layout: RadauMatrixLayout,
        output: &mut [f64],
        workspace: &mut SymbolicCallbackWorkspace,
        telemetry: &mut RadauTelemetry,
    ) -> Result<(), RadauError> {
        match (self, workspace) {
            (Self::ExprLegacy(plan), SymbolicCallbackWorkspace::ExprLegacy(workspace)) => plan
                .evaluate_jacobian_layout_with_telemetry(
                    t, state, parameters, layout, output, workspace, telemetry,
                ),
            (Self::AtomViewNative(plan), SymbolicCallbackWorkspace::AtomViewNative(workspace)) => {
                plan.evaluate_jacobian_layout_with_telemetry(
                    t, state, parameters, layout, output, workspace, telemetry,
                )
            }
            (Self::Aot(plan), SymbolicCallbackWorkspace::Aot(workspace)) => {
                plan.evaluate_jacobian(t, state, parameters, layout, output, workspace, telemetry)
            }
            _ => Err(RadauConfigError::CallbackFrontendMismatch.into()),
        }
    }
}

impl<'a> SymbolicCallbackSession<'a> {
    /// Borrow the session's diagnostic counters and timings.
    pub(crate) fn telemetry(&self) -> &RadauTelemetry {
        &self.telemetry
    }

    /// Mutably borrow the session telemetry for integration with a solve.
    pub(crate) fn telemetry_mut(&mut self) -> &mut RadauTelemetry {
        &mut self.telemetry
    }

    /// Borrow the current parameter values.
    pub(crate) fn parameters(&self) -> &[f64] {
        &self.parameters
    }

    /// Return the monotonically increasing parameter generation.
    pub(crate) fn generation(&self) -> u64 {
        self.generation
    }

    /// Return the immutable Jacobian pattern prepared by the frontend.
    pub(crate) fn jacobian_pattern(&self) -> &[(usize, usize)] {
        &self.jacobian_pattern
    }

    /// Return the capacity of the frontend-specific callback workspace.
    pub(crate) fn workspace_capacity(&self) -> usize {
        match &self.workspace {
            SymbolicCallbackWorkspace::ExprLegacy(workspace) => workspace.argument_capacity(),
            SymbolicCallbackWorkspace::AtomViewNative(workspace) => workspace.value_capacity(),
            SymbolicCallbackWorkspace::Aot(workspace) => workspace.argument_capacity(),
        }
    }

    /// Rebind values without recompiling or replacing callback storage.
    pub(crate) fn rebind_parameters(&mut self, values: &[f64]) -> Result<(), RadauError> {
        if values.len() != self.parameters.len() {
            return Err(RadauError::ShapeMismatch {
                stage: RadauStage::Preparation,
                expected: self.parameters.len(),
                actual: values.len(),
            });
        }
        self.telemetry.measure_callback(
            super::telemetry::RadauCallbackStage::ParameterRebind,
            || self.parameters.copy_from_slice(values),
        );
        self.telemetry.count_copy();
        self.generation = self.generation.saturating_add(1);
        Ok(())
    }

    pub(crate) fn evaluate_residual(
        &mut self,
        t: f64,
        state: &[f64],
        output: &mut [f64],
    ) -> Result<(), RadauError> {
        // Keep the solver-level callback count distinct from the evaluator
        // count. The former counts logical residual requests; the latter
        // records frontend scalar evaluation work inside that request.
        self.telemetry.count_stage(RadauStage::Residual);
        self.telemetry
            .record_policy_dispatch(self.execution_policy, output.len(), output.len());
        self.callbacks.evaluate_residual_with_telemetry(
            t,
            state,
            &self.parameters,
            output,
            &mut self.workspace,
            &mut self.telemetry,
        )
    }

    pub(crate) fn evaluate_jacobian(
        &mut self,
        t: f64,
        state: &[f64],
        output: &mut [f64],
    ) -> Result<(), RadauError> {
        // Structured Jacobian callbacks are still one logical solver-level
        // Jacobian request even when their evaluator writes a projected
        // Sparse/Banded layout directly.
        self.telemetry.count_stage(RadauStage::Jacobian);
        self.telemetry
            .record_policy_dispatch(self.execution_policy, output.len(), output.len());
        self.callbacks.evaluate_jacobian_with_telemetry(
            t,
            state,
            &self.parameters,
            output,
            &mut self.workspace,
            &mut self.telemetry,
        )
    }

    pub(crate) fn evaluate_jacobian_layout(
        &mut self,
        t: f64,
        state: &[f64],
        layout: RadauMatrixLayout,
        output: &mut [f64],
    ) -> Result<(), RadauError> {
        self.telemetry.count_stage(RadauStage::Jacobian);
        self.telemetry
            .record_policy_dispatch(self.execution_policy, output.len(), output.len());
        self.callbacks.evaluate_jacobian_layout_with_telemetry(
            t,
            state,
            &self.parameters,
            layout,
            output,
            &mut self.workspace,
            &mut self.telemetry,
        )
    }

    /// Merge shared AOT worker/chunk telemetry after the session is no longer borrowed.
    pub(crate) fn absorb_runtime_telemetry(&mut self) {
        if let (PreparedSymbolicCallbacks::Aot(plan), SymbolicCallbackWorkspace::Aot(workspace)) =
            (self.callbacks, &self.workspace)
        {
            plan.absorb_runtime_telemetry(workspace, &mut self.telemetry);
        }
    }
}

/// Caller-owned output contract for the residual callback.
/// Minimal residual callback used by the numerical stepper.
pub(crate) trait ResidualCallback {
    fn eval(&mut self, t: f64, y: &[f64], out: &mut [f64]) -> Result<(), RadauError>;
}

/// Caller-owned output contract for a dense Jacobian callback.
/// Dense analytic Jacobian callback used by the numerical stepper.
pub(crate) trait DenseJacobianCallback {
    fn eval_into(&mut self, t: f64, y: &[f64], out: &mut [f64]) -> Result<(), RadauError>;
}

/// Combined callback boundary for a solver step using one mutable evaluator
/// session for both residual and Jacobian calls.
/// Unified callback adapter used by symbolic and direct numerical steps.
pub(crate) trait RadauStepCallbacks {
    /// Evaluate `f(t, y)` into caller-owned storage.
    fn eval_residual(&mut self, t: f64, y: &[f64], out: &mut [f64]) -> Result<(), RadauError>;
    /// Evaluate a Jacobian in the callback's default representation.
    fn eval_jacobian(&mut self, t: f64, y: &[f64], out: &mut [f64]) -> Result<(), RadauError>;
    /// Evaluate a Jacobian directly in the selected matrix layout.
    ///
    /// Symbolic callbacks override this for Sparse/Banded routes.  A generic
    /// closure has no safe way to promise a structural layout, so the default
    /// implementation deliberately returns a typed capability error instead
    /// of falling back to an implicit dense conversion.
    fn eval_jacobian_layout(
        &mut self,
        t: f64,
        y: &[f64],
        layout: RadauMatrixLayout,
        out: &mut [f64],
    ) -> Result<(), RadauError> {
        if layout == RadauMatrixLayout::Dense {
            self.eval_jacobian(t, y, out)
        } else {
            Err(RadauUnsupportedRoute::StructuredJacobianCallbackRequired.into())
        }
    }
}

impl<'a> RadauStepCallbacks for SymbolicCallbackSession<'a> {
    fn eval_residual(&mut self, t: f64, y: &[f64], out: &mut [f64]) -> Result<(), RadauError> {
        self.evaluate_residual(t, y, out)
    }

    fn eval_jacobian(&mut self, t: f64, y: &[f64], out: &mut [f64]) -> Result<(), RadauError> {
        self.evaluate_jacobian(t, y, out)
    }

    fn eval_jacobian_layout(
        &mut self,
        t: f64,
        y: &[f64],
        layout: RadauMatrixLayout,
        out: &mut [f64],
    ) -> Result<(), RadauError> {
        self.evaluate_jacobian_layout(t, y, layout, out)
    }
}

impl<F> ResidualCallback for F
where
    F: FnMut(f64, &[f64], &mut [f64]) -> Result<(), RadauError>,
{
    fn eval(&mut self, t: f64, y: &[f64], out: &mut [f64]) -> Result<(), RadauError> {
        self(t, y, out)
    }
}

impl<F> DenseJacobianCallback for F
where
    F: FnMut(f64, &[f64], &mut [f64]) -> Result<(), RadauError>,
{
    fn eval_into(&mut self, t: f64, y: &[f64], out: &mut [f64]) -> Result<(), RadauError> {
        self(t, y, out)
    }
}

/// Validate callback shape and reject non-finite values before linear algebra.
pub(crate) fn validate_callback_output(
    stage: RadauStage,
    expected: usize,
    values: &[f64],
) -> Result<(), RadauError> {
    if values.len() != expected {
        return Err(RadauError::ShapeMismatch {
            stage,
            expected,
            actual: values.len(),
        });
    }
    if values.iter().any(|value| !value.is_finite()) {
        return Err(RadauError::NonFiniteCallback { stage });
    }
    Ok(())
}