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
//! Prepared Lambdify callbacks for the new Radau route.
//!
//! Expressions are compiled once. Evaluation reuses a session-owned argument
//! buffer and writes directly into caller-owned residual/Jacobian storage.
//! The flattened ABI follows LSODE2: `[t, parameters..., state...]`.

use crate::symbolic::ivp_telemetry::IvpLambdifyExecutionPolicy;
use crate::symbolic::symbolic_engine::Expr;

use super::{
    callbacks::validate_callback_output,
    config::RadauMatrixLayout,
    error::{RadauConfigError, RadauError, RadauStage, RadauUnsupportedRoute},
    jacobian::RadauJacobianPattern,
    telemetry::{RadauCallbackStage, RadauTelemetry},
};

type ScalarLambdified = Box<dyn Fn(&[f64]) -> f64 + Send + Sync>;

/// Prepared ExprLegacy residual/Jacobian closures and their structural pattern.
///
/// The closures are immutable after preparation.  Runtime calls only bind
/// values into `LambdifyWorkspace::args` and write evaluator results into
/// caller-owned buffers, which keeps parameter continuation out of the
/// symbolic-construction path.
pub(crate) struct LambdifyPlan {
    residual: Vec<ScalarLambdified>,
    jacobian: Option<Vec<ScalarLambdified>>,
    jacobian_pattern: Vec<(usize, usize)>,
    dimension: usize,
    parameter_count: usize,
    execution_policy: IvpLambdifyExecutionPolicy,
}

/// Caller-owned argument buffer reused by every Lambdify callback invocation.
/// Its ABI is `[time, parameters..., state...]`; changing that order would
/// silently evaluate valid expressions with the wrong values.
pub(crate) struct LambdifyWorkspace {
    args: Vec<f64>,
}

impl LambdifyWorkspace {
    /// Allocate an argument buffer of the exact callback schema size.
    pub(crate) fn new(argument_count: usize) -> Self {
        Self {
            args: vec![0.0; argument_count],
        }
    }

    /// Report capacity for continuation allocation diagnostics.
    pub(crate) fn argument_capacity(&self) -> usize {
        self.args.capacity()
    }
}

impl LambdifyPlan {
    /// Compile ExprLegacy residual/Jacobian closures without evaluating them.
    pub(crate) fn from_expressions(
        residual: Vec<Expr>,
        jacobian: Option<Vec<Expr>>,
        independent_variable: &str,
        variables: &[&str],
        parameters: &[&str],
    ) -> Result<Self, RadauError> {
        Self::from_expressions_with_policy(
            residual,
            jacobian,
            independent_variable,
            variables,
            parameters,
            IvpLambdifyExecutionPolicy::Sequential,
        )
    }

    /// Compile ExprLegacy callbacks and retain the selected warm execution policy.
    pub(crate) fn from_expressions_with_policy(
        residual: Vec<Expr>,
        jacobian: Option<Vec<Expr>>,
        independent_variable: &str,
        variables: &[&str],
        parameters: &[&str],
        execution_policy: IvpLambdifyExecutionPolicy,
    ) -> Result<Self, RadauError> {
        let dimension = residual.len();
        if dimension == 0 {
            return Err(RadauConfigError::ZeroDimension.into());
        }
        if let Some(ref jacobian) = jacobian {
            let expected = dimension
                .checked_mul(dimension)
                .ok_or(RadauError::WorkspaceSizeOverflow { dimension })?;
            if jacobian.len() != expected {
                return Err(RadauError::ShapeMismatch {
                    stage: RadauStage::Preparation,
                    expected,
                    actual: jacobian.len(),
                });
            }
        }

        // Build the callback schema once.  Every compiled closure borrows this
        // stable name order, while later calls copy only numeric values.
        let mut names = Vec::with_capacity(1 + variables.len() + parameters.len());
        names.push(independent_variable.to_owned());
        names.extend(parameters.iter().map(|name| (*name).to_owned()));
        names.extend(variables.iter().map(|name| (*name).to_owned()));
        let name_refs: Vec<&str> = names.iter().map(String::as_str).collect();

        for expression in residual.iter().chain(jacobian.iter().flatten()) {
            validate_expression_variables(expression, &name_refs)?;
        }

        let residual = residual
            .iter()
            .map(|expression| Expr::lambdify_borrowed_thread_safe(expression, &name_refs))
            .collect();
        let jacobian_pattern = jacobian
            .as_ref()
            .map(|expressions| {
                expressions
                    .iter()
                    .enumerate()
                    .filter_map(|(index, expression)| {
                        (!expression.is_zero()).then_some((index / dimension, index % dimension))
                    })
                    .collect::<Vec<_>>()
            })
            .unwrap_or_default();
        let jacobian = jacobian.map(|expressions| {
            expressions
                .iter()
                .map(|expression| Expr::lambdify_borrowed_thread_safe(expression, &name_refs))
                .collect()
        });

        Ok(Self {
            residual,
            jacobian,
            jacobian_pattern,
            dimension,
            parameter_count: parameters.len(),
            execution_policy,
        })
    }

    /// Create fresh reusable callback storage for this prepared plan.
    pub(crate) fn workspace(&self) -> LambdifyWorkspace {
        LambdifyWorkspace::new(1 + self.parameter_count + self.dimension)
    }

    /// Return the number of parameter values expected by callbacks.
    pub(crate) fn parameter_count(&self) -> usize {
        self.parameter_count
    }

    pub(crate) fn execution_policy(&self) -> IvpLambdifyExecutionPolicy {
        self.execution_policy
    }

    /// Evaluate the residual using the default disabled telemetry mode.
    pub(crate) fn evaluate_residual(
        &self,
        t: f64,
        state: &[f64],
        parameters: &[f64],
        output: &mut [f64],
        workspace: &mut LambdifyWorkspace,
    ) -> Result<(), RadauError> {
        let mut telemetry = RadauTelemetry::new(Default::default());
        self.evaluate_residual_with_telemetry(
            t,
            state,
            parameters,
            output,
            workspace,
            &mut telemetry,
        )
    }

    /// Evaluate the residual while reporting binding and evaluation stages.
    pub(crate) fn evaluate_residual_with_telemetry(
        &self,
        t: f64,
        state: &[f64],
        parameters: &[f64],
        output: &mut [f64],
        workspace: &mut LambdifyWorkspace,
        telemetry: &mut RadauTelemetry,
    ) -> Result<(), RadauError> {
        telemetry.measure_callback(RadauCallbackStage::ArgumentBinding, || {
            self.fill_args(t, state, parameters, workspace, RadauStage::Residual)
        })?;
        if output.len() != self.dimension {
            return Err(RadauError::ShapeMismatch {
                stage: RadauStage::Residual,
                expected: self.dimension,
                actual: output.len(),
            });
        }
        let result = telemetry.measure_callback(RadauCallbackStage::ResidualEvaluation, || {
            evaluate_scalar_entries(
                &self.residual,
                output,
                &workspace.args,
                self.execution_policy,
            );
            validate_callback_output(RadauStage::Residual, self.dimension, output)
        });
        if result.is_ok() {
            telemetry.count_output_writes(output.len());
        }
        result
    }

    /// Evaluate the dense Jacobian using disabled telemetry.
    pub(crate) fn evaluate_jacobian(
        &self,
        t: f64,
        state: &[f64],
        parameters: &[f64],
        output: &mut [f64],
        workspace: &mut LambdifyWorkspace,
    ) -> Result<(), RadauError> {
        let mut telemetry = RadauTelemetry::new(Default::default());
        self.evaluate_jacobian_with_telemetry(
            t,
            state,
            parameters,
            output,
            workspace,
            &mut telemetry,
        )
    }

    /// Evaluate the dense Jacobian into caller-owned storage with telemetry.
    pub(crate) fn evaluate_jacobian_with_telemetry(
        &self,
        t: f64,
        state: &[f64],
        parameters: &[f64],
        output: &mut [f64],
        workspace: &mut LambdifyWorkspace,
        telemetry: &mut RadauTelemetry,
    ) -> Result<(), RadauError> {
        telemetry.measure_callback(RadauCallbackStage::ArgumentBinding, || {
            self.fill_args(t, state, parameters, workspace, RadauStage::Jacobian)
        })?;
        self.evaluate_jacobian_with_args(output, workspace, telemetry)
    }

    fn evaluate_jacobian_with_args(
        &self,
        output: &mut [f64],
        workspace: &LambdifyWorkspace,
        telemetry: &mut RadauTelemetry,
    ) -> Result<(), RadauError> {
        let Some(jacobian) = self.jacobian.as_ref() else {
            return Err(RadauUnsupportedRoute::LambdifyJacobianNotPrepared.into());
        };
        let expected = self.dimension * self.dimension;
        if output.len() != expected {
            return Err(RadauError::ShapeMismatch {
                stage: RadauStage::Jacobian,
                expected,
                actual: output.len(),
            });
        }
        let result = telemetry.measure_callback(RadauCallbackStage::JacobianEvaluation, || {
            evaluate_scalar_entries(jacobian, output, &workspace.args, self.execution_policy);
            validate_callback_output(RadauStage::Jacobian, expected, output)
        });
        if result.is_ok() {
            telemetry.count_output_writes(output.len());
        }
        result
    }

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

    /// Evaluate the Jacobian directly in Dense, Sparse, or compact Banded layout.
    pub(crate) fn evaluate_jacobian_layout(
        &self,
        t: f64,
        state: &[f64],
        parameters: &[f64],
        layout: RadauMatrixLayout,
        output: &mut [f64],
        workspace: &mut LambdifyWorkspace,
    ) -> Result<(), RadauError> {
        let mut telemetry = RadauTelemetry::new(Default::default());
        self.evaluate_jacobian_layout_with_telemetry(
            t,
            state,
            parameters,
            layout,
            output,
            workspace,
            &mut telemetry,
        )
    }

    /// Evaluate and project the Jacobian while measuring output assembly.
    pub(crate) fn evaluate_jacobian_layout_with_telemetry(
        &self,
        t: f64,
        state: &[f64],
        parameters: &[f64],
        layout: RadauMatrixLayout,
        output: &mut [f64],
        workspace: &mut LambdifyWorkspace,
        telemetry: &mut RadauTelemetry,
    ) -> Result<(), RadauError> {
        telemetry.measure_callback(RadauCallbackStage::ArgumentBinding, || {
            self.fill_args(t, state, parameters, workspace, RadauStage::Jacobian)
        })?;
        let Some(jacobian) = self.jacobian.as_ref() else {
            return Err(RadauUnsupportedRoute::LambdifyJacobianNotPrepared.into());
        };
        match layout {
            RadauMatrixLayout::Dense => {
                self.evaluate_jacobian_with_args(output, workspace, telemetry)
            }
            RadauMatrixLayout::Sparse => {
                if output.len() != self.jacobian_pattern.len() {
                    return Err(RadauError::ShapeMismatch {
                        stage: RadauStage::Jacobian,
                        expected: self.jacobian_pattern.len(),
                        actual: output.len(),
                    });
                }
                // Sparse values are selected from the prepared nonzero
                // pattern, so zero entries never incur evaluator calls.
                telemetry.measure_callback(RadauCallbackStage::JacobianEvaluation, || {
                    evaluate_sparse_entries(
                        jacobian,
                        &self.jacobian_pattern,
                        self.dimension,
                        output,
                        &workspace.args,
                        self.execution_policy,
                    );
                });
                let result = telemetry
                    .measure_callback(RadauCallbackStage::JacobianOutputAssembly, || {
                        validate_callback_output(RadauStage::Jacobian, output.len(), output)
                    });
                if result.is_ok() {
                    telemetry.count_output_writes(output.len());
                }
                result
            }
            RadauMatrixLayout::Banded { lower, upper } => {
                let pattern = RadauJacobianPattern::banded(self.dimension, lower, upper)?;
                let expected = pattern.values_len(layout);
                if output.len() != expected {
                    return Err(RadauError::ShapeMismatch {
                        stage: RadauStage::Jacobian,
                        expected,
                        actual: output.len(),
                    });
                }
                if self
                    .jacobian_pattern
                    .iter()
                    .any(|&(row, col)| !pattern.is_in_layout(layout, row, col))
                {
                    return Err(RadauUnsupportedRoute::JacobianOutsideBandedLayout.into());
                }
                // Banded output is compact storage.  We evaluate only entries
                // in the requested band and leave structural zeros explicit;
                // no dense-to-banded conversion is performed afterward.
                output.fill(0.0);
                let entries = pattern.entries().to_vec();
                telemetry.measure_callback(RadauCallbackStage::JacobianEvaluation, || {
                    evaluate_banded_entries(
                        jacobian,
                        &entries,
                        self.dimension,
                        layout,
                        output,
                        &workspace.args,
                        self.execution_policy,
                    )
                })?;
                let result = telemetry
                    .measure_callback(RadauCallbackStage::JacobianOutputAssembly, || {
                        validate_callback_output(RadauStage::Jacobian, output.len(), output)
                    });
                if result.is_ok() {
                    telemetry.count_output_writes(output.len());
                }
                result
            }
        }
    }

    fn fill_args(
        &self,
        t: f64,
        state: &[f64],
        parameters: &[f64],
        workspace: &mut LambdifyWorkspace,
        stage: RadauStage,
    ) -> Result<(), RadauError> {
        if state.len() != self.dimension {
            return Err(RadauError::ShapeMismatch {
                stage,
                expected: self.dimension,
                actual: state.len(),
            });
        }
        if parameters.len() != self.parameter_count {
            return Err(RadauError::ShapeMismatch {
                stage,
                expected: self.parameter_count,
                actual: parameters.len(),
            });
        }
        // Keep argument binding centralized.  It is intentionally measured as
        // its own callback stage because continuation performance depends on
        // this copy cost independently of evaluator cost.
        workspace.args[0] = t;
        workspace.args[1..1 + self.parameter_count].copy_from_slice(parameters);
        workspace.args[1 + self.parameter_count..].copy_from_slice(state);
        Ok(())
    }
}

fn evaluate_scalar_entries(
    evaluators: &[ScalarLambdified],
    output: &mut [f64],
    args: &[f64],
    policy: IvpLambdifyExecutionPolicy,
) {
    if policy.should_parallel_with_tasks(evaluators.len(), evaluators.len()) {
        use rayon::prelude::*;
        evaluators
            .par_iter()
            .zip(output.par_iter_mut())
            .for_each(|(evaluator, slot)| *slot = evaluator(args));
    } else {
        for (slot, evaluator) in output.iter_mut().zip(evaluators) {
            *slot = evaluator(args);
        }
    }
}

fn evaluate_sparse_entries(
    jacobian: &[ScalarLambdified],
    pattern: &[(usize, usize)],
    dimension: usize,
    output: &mut [f64],
    args: &[f64],
    policy: IvpLambdifyExecutionPolicy,
) {
    if policy.should_parallel_with_tasks(pattern.len(), pattern.len()) {
        use rayon::prelude::*;
        pattern
            .par_iter()
            .zip(output.par_iter_mut())
            .for_each(|(&(row, col), slot)| *slot = jacobian[row * dimension + col](args));
    } else {
        for (slot, &(row, col)) in output.iter_mut().zip(pattern) {
            *slot = jacobian[row * dimension + col](args);
        }
    }
}

fn evaluate_banded_entries(
    jacobian: &[ScalarLambdified],
    entries: &[(usize, usize)],
    dimension: usize,
    layout: RadauMatrixLayout,
    output: &mut [f64],
    args: &[f64],
    policy: IvpLambdifyExecutionPolicy,
) -> Result<(), RadauError> {
    let pattern = RadauJacobianPattern::banded(
        dimension,
        match layout {
            RadauMatrixLayout::Banded { lower, .. } => lower,
            _ => 0,
        },
        match layout {
            RadauMatrixLayout::Banded { upper, .. } => upper,
            _ => 0,
        },
    )?;
    if policy.should_parallel_with_tasks(entries.len(), entries.len()) {
        // Evaluate into disjoint temporary slots, then place them in compact
        // storage. The copy is explicit so parallel workers never alias output.
        use rayon::prelude::*;
        let values = entries
            .par_iter()
            .map(|&(row, col)| jacobian[row * dimension + col](args))
            .collect::<Vec<_>>();
        for (entry, value) in entries.iter().zip(values) {
            let compact = pattern
                .compact_slot(layout, entry.0, entry.1)
                .ok_or(RadauConfigError::InvalidBandedSlot)?;
            output[compact] = value;
        }
    } else {
        for entry in entries {
            let compact = pattern
                .compact_slot(layout, entry.0, entry.1)
                .ok_or(RadauConfigError::InvalidBandedSlot)?;
            let value = jacobian[entry.0 * dimension + entry.1](args);
            output[compact] = value;
        }
    }
    Ok(())
}

fn validate_expression_variables(expression: &Expr, names: &[&str]) -> Result<(), RadauError> {
    if expression
        .all_arguments_are_variables()
        .iter()
        .all(|variable| names.iter().any(|name| *name == variable))
    {
        Ok(())
    } else {
        Err(RadauConfigError::ExpressionVariableOutsideSchema.into())
    }
}