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
//! Legacy Lambdify backend for nonlinear systems.
//!
//! This module intentionally preserves the historical Jacobian construction
//! and compatibility callback semantics. The prepared symbolic orchestration
//! lives in the sibling symbolic module; this file owns only the legacy
//! Lambdify evaluator implementation and its optional execution policy.

use super::symbolic::{
    LambdifyExecutionPolicy, PreparationStage, PreparationTelemetryRecorder, SymbolicBackendKind,
    SymbolicEvaluationBackend,
};
use crate::global::THRESHOLD as SYMBOLIC_ZERO_THRESHOLD;
use crate::numerical::Nonlinear_systems::error::SolveError;
use crate::symbolic::symbolic_engine::Expr;
use crate::symbolic::symbolic_functions::Jacobian;
use nalgebra::{DMatrix, DVector};
use rayon::prelude::*;
use std::cell::RefCell;

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

thread_local! {
    /// Reused input workspace for parameterized Lambdify callbacks.
    ///
    /// The scalar expression ABI accepts one contiguous slice. Keeping this
    /// scratch per worker thread avoids allocating parameters + variables
    /// for every residual/Jacobian callback while retaining a Sync backend.
    static LAMBDIFY_INPUT_WORKSPACE: RefCell<Vec<f64>> = const { RefCell::new(Vec::new()) };
}

pub(crate) fn lambdify_input_workspace_capacity() -> usize {
    LAMBDIFY_INPUT_WORKSPACE.with(|workspace| workspace.borrow().capacity())
}

/// Concrete backend implementation that uses the existing `Jacobian` lambdify path.
pub(crate) struct LegacyLambdifySymbolicBackend {
    /// Symbolic Jacobian retained as preparation metadata and diagnostics.
    symbolic_jacobian: Vec<Vec<Expr>>,
    /// Scalar residual evaluators compiled once during preparation.
    residual_evaluators: Vec<ScalarEvaluator>,
    /// Non-zero symbolic Jacobian entries compiled once during preparation.
    jacobian_evaluators: Vec<Vec<Option<ScalarEvaluator>>>,
    /// Number of variables in the fixed evaluation schema.
    variable_count: usize,
    /// Number of parameters in the fixed evaluation schema.
    parameter_count: usize,
    /// Runtime policy used by residual/Jacobian callbacks.
    execution_policy: LambdifyExecutionPolicy,
    /// Number of non-zero Jacobian evaluators known during preparation.
    nonzero_jacobian_entries: usize,
    /// Non-zero Jacobian row positions grouped by column.
    ///
    /// `DMatrix` is column-major, so each group can be evaluated through a
    /// disjoint mutable column slice without a mutex or temporary matrix.
    jacobian_nonzero_rows_by_column: Vec<Vec<usize>>,
}

impl LegacyLambdifySymbolicBackend {
    /// Builds the lambdify backend from symbolic equations.
    ///
    /// This is the legacy-backed execution branch: symbolic equations are
    /// differentiated through [`Jacobian`] and then lambdified into callable
    /// dense residual/Jacobian closures.
    pub(crate) fn from_expressions(
        equations: &[Expr],
        variables: &[String],
        equation_parameters: Option<&[String]>,
        execution_policy: LambdifyExecutionPolicy,
        mut preparation_recorder: Option<&mut PreparationTelemetryRecorder>,
    ) -> Result<Self, SolveError> {
        let differentiation_started = std::time::Instant::now();
        let mut jacobian = Jacobian::new();
        let variable_refs = variables
            .iter()
            .map(|value| value.as_str())
            .collect::<Vec<_>>();
        jacobian.set_vector_of_functions(equations.to_vec());
        jacobian.set_variables(variable_refs);

        if let Some(parameters) = equation_parameters {
            jacobian.set_params(parameters.to_vec());
            jacobian.calc_jacobian();
        } else {
            jacobian.calc_jacobian();
        }
        if let Some(recorder) = preparation_recorder.as_mut() {
            recorder.record(
                PreparationStage::JacobianDifferentiation,
                differentiation_started,
                Some(1),
                Some(equations.len() * variables.len()),
            );
        }

        let mut input_names = equation_parameters
            .unwrap_or(&[])
            .iter()
            .map(String::as_str)
            .collect::<Vec<_>>();
        input_names.extend(variables.iter().map(String::as_str));

        // The generic symbolic_functions2 closures return freshly allocated
        // DVector/DMatrix values and use a mutex for every parallel write.
        // Nonlinear solves repeatedly evaluate one point, so keep the same
        // compiled scalar expressions but fill solver-owned output buffers
        // directly on this production Lambdify path.
        let residual_callback_started = std::time::Instant::now();
        let residual_evaluators = equations
            .iter()
            .map(|equation| Expr::lambdify_borrowed_thread_safe(equation, input_names.as_slice()))
            .collect();
        if let Some(recorder) = preparation_recorder.as_mut() {
            recorder.record(
                PreparationStage::ResidualCallbackPreparation,
                residual_callback_started,
                Some(equations.len() as u64),
                Some(equations.len()),
            );
        }
        let symbolic_jacobian = jacobian.symbolic_jacobian;
        let mut jacobian_nonzero_rows_by_column = vec![Vec::new(); variables.len()];
        for (row_index, row) in symbolic_jacobian.iter().enumerate() {
            for (column_index, entry) in row.iter().enumerate() {
                if !entry.is_zero() {
                    jacobian_nonzero_rows_by_column[column_index].push(row_index);
                }
            }
        }
        let nonzero_jacobian_entries = jacobian_nonzero_rows_by_column.iter().map(Vec::len).sum();
        let jacobian_callback_started = std::time::Instant::now();
        let jacobian_evaluators = symbolic_jacobian
            .iter()
            .map(|row| {
                row.iter()
                    .map(|entry| {
                        (!entry.is_zero()).then(|| {
                            Expr::lambdify_borrowed_thread_safe(entry, input_names.as_slice())
                        })
                    })
                    .collect()
            })
            .collect();
        if let Some(recorder) = preparation_recorder.as_mut() {
            recorder.record(
                PreparationStage::JacobianCallbackPreparation,
                jacobian_callback_started,
                Some(nonzero_jacobian_entries as u64),
                Some(nonzero_jacobian_entries),
            );
        }

        Ok(Self {
            symbolic_jacobian,
            residual_evaluators,
            jacobian_evaluators,
            variable_count: variables.len(),
            parameter_count: equation_parameters.map_or(0, <[String]>::len),
            execution_policy,
            nonzero_jacobian_entries,
            jacobian_nonzero_rows_by_column,
        })
    }
}

impl SymbolicEvaluationBackend for LegacyLambdifySymbolicBackend {
    fn kind(&self) -> SymbolicBackendKind {
        SymbolicBackendKind::Lambdify
    }

    fn symbolic_jacobian(&self) -> Option<&[Vec<Expr>]> {
        Some(&self.symbolic_jacobian)
    }

    fn lambdify_execution_policy(&self) -> Option<LambdifyExecutionPolicy> {
        Some(self.execution_policy)
    }

    fn supports_residual_into(&self) -> bool {
        true
    }

    fn supports_jacobian_into(&self) -> bool {
        true
    }

    fn residual_into(
        &self,
        x: &DVector<f64>,
        equation_parameters: Option<&[String]>,
        equation_parameter_values: Option<&DVector<f64>>,
        variables: &[String],
        out: &mut DVector<f64>,
    ) -> Result<(), SolveError> {
        if out.len() != self.residual_evaluators.len() {
            return Err(SolveError::DimensionMismatch {
                expected: self.residual_evaluators.len(),
                actual: out.len(),
                context: "symbolic residual output",
            });
        }
        self.with_evaluation_input(
            x,
            equation_parameters,
            equation_parameter_values,
            variables,
            "symbolic residual input",
            |input| {
                if self.should_parallelize(self.residual_evaluators.len()) {
                    out.as_mut_slice()
                        .par_iter_mut()
                        .zip(self.residual_evaluators.par_iter())
                        .for_each(|(slot, evaluator)| *slot = evaluator(input));
                } else {
                    for (slot, evaluator) in out.iter_mut().zip(&self.residual_evaluators) {
                        *slot = evaluator(input);
                    }
                }
                if out.iter().any(|value| !value.is_finite()) {
                    return Err(SolveError::ResidualEvaluation(
                        "symbolic residual returned NaN or Inf".to_string(),
                    ));
                }
                Ok(())
            },
        )
    }

    fn jacobian_into(
        &self,
        x: &DVector<f64>,
        equation_parameters: Option<&[String]>,
        equation_parameter_values: Option<&DVector<f64>>,
        variables: &[String],
        out: &mut DMatrix<f64>,
    ) -> Result<(), SolveError> {
        if out.nrows() != self.jacobian_evaluators.len() || out.ncols() != self.variable_count {
            return Err(SolveError::InvalidConfig(format!(
                "symbolic Jacobian output is {}x{}, expected {}x{}",
                out.nrows(),
                out.ncols(),
                self.jacobian_evaluators.len(),
                self.variable_count
            )));
        }
        out.fill(0.0);
        self.with_evaluation_input(
            x,
            equation_parameters,
            equation_parameter_values,
            variables,
            "symbolic jacobian input",
            |input| {
                if self.should_parallelize(self.nonzero_jacobian_entries) {
                    let row_count = self.jacobian_evaluators.len();
                    out.as_mut_slice()
                        .par_chunks_mut(row_count)
                        .enumerate()
                        .for_each(|(column_index, column)| {
                            for &row_index in &self.jacobian_nonzero_rows_by_column[column_index] {
                                if let Some(evaluator) =
                                    &self.jacobian_evaluators[row_index][column_index]
                                {
                                    let value = evaluator(input);
                                    if !value.is_finite() || value.abs() > SYMBOLIC_ZERO_THRESHOLD {
                                        column[row_index] = value;
                                    }
                                }
                            }
                        });
                } else {
                    for (row_index, row) in self.jacobian_evaluators.iter().enumerate() {
                        for (column_index, evaluator) in row.iter().enumerate() {
                            if let Some(evaluator) = evaluator {
                                let value = evaluator(input);
                                if !value.is_finite() {
                                    return Err(SolveError::JacobianEvaluation(
                                        "symbolic jacobian returned NaN or Inf".to_string(),
                                    ));
                                }
                                if value.abs() > SYMBOLIC_ZERO_THRESHOLD {
                                    out[(row_index, column_index)] = value;
                                }
                            }
                        }
                    }
                }
                if out.iter().any(|value| !value.is_finite()) {
                    return Err(SolveError::JacobianEvaluation(
                        "symbolic jacobian returned NaN or Inf".to_string(),
                    ));
                }
                Ok(())
            },
        )
    }

    fn residual(
        &self,
        x: &DVector<f64>,
        equation_parameters: Option<&[String]>,
        equation_parameter_values: Option<&DVector<f64>>,
        variables: &[String],
    ) -> Result<DVector<f64>, SolveError> {
        let mut result = DVector::zeros(self.residual_evaluators.len());
        self.residual_into(
            x,
            equation_parameters,
            equation_parameter_values,
            variables,
            &mut result,
        )?;
        Ok(result)
    }

    fn jacobian(
        &self,
        x: &DVector<f64>,
        equation_parameters: Option<&[String]>,
        equation_parameter_values: Option<&DVector<f64>>,
        variables: &[String],
    ) -> Result<DMatrix<f64>, SolveError> {
        let mut result = DMatrix::zeros(self.jacobian_evaluators.len(), self.variable_count);
        self.jacobian_into(
            x,
            equation_parameters,
            equation_parameter_values,
            variables,
            &mut result,
        )?;
        Ok(result)
    }
}

impl LegacyLambdifySymbolicBackend {
    fn should_parallelize(&self, work: usize) -> bool {
        match self.execution_policy {
            LambdifyExecutionPolicy::Sequential => false,
            LambdifyExecutionPolicy::Parallel { min_work } => work >= min_work.max(1),
        }
    }

    /// Validates the fixed symbolic schema and runs one evaluator call.
    fn with_evaluation_input(
        &self,
        x: &DVector<f64>,
        equation_parameters: Option<&[String]>,
        equation_parameter_values: Option<&DVector<f64>>,
        variables: &[String],
        context: &'static str,
        evaluate: impl FnOnce(&[f64]) -> Result<(), SolveError>,
    ) -> Result<(), SolveError> {
        if x.len() != self.variable_count || variables.len() != self.variable_count {
            return Err(SolveError::DimensionMismatch {
                expected: self.variable_count,
                actual: x.len(),
                context,
            });
        }
        let supplied_parameter_count = equation_parameters.map_or(0, <[String]>::len);
        if supplied_parameter_count != self.parameter_count {
            return Err(SolveError::DimensionMismatch {
                expected: self.parameter_count,
                actual: supplied_parameter_count,
                context: "symbolic parameter schema",
            });
        }
        if self.parameter_count == 0 {
            if equation_parameter_values.is_some() {
                return Err(SolveError::InvalidParameterSchema(
                    "symbolic evaluator received parameter values without a parameter schema"
                        .to_string(),
                ));
            }
            return evaluate(x.as_slice());
        }
        let values = equation_parameter_values.ok_or_else(|| {
            SolveError::InvalidParameterSchema(
                "symbolic evaluator requires values for the declared parameter schema".to_string(),
            )
        })?;
        if values.len() != self.parameter_count {
            return Err(SolveError::DimensionMismatch {
                expected: self.parameter_count,
                actual: values.len(),
                context: "symbolic parameter values",
            });
        }
        if let Some((index, value)) = values
            .iter()
            .copied()
            .enumerate()
            .find(|(_, value)| !value.is_finite())
        {
            return Err(SolveError::NonFiniteParameterValue { index, value });
        }
        LAMBDIFY_INPUT_WORKSPACE.with(|workspace| {
            let mut input = workspace.borrow_mut();
            input.clear();
            input.extend(values.iter().copied());
            input.extend(x.iter().copied());
            evaluate(input.as_slice())
        })
    }
}