KiThe 0.3.6

A numerical suite for chemical kinetics and thermodynamics, combustion, heat and mass transfer,chemical engeneering. Work in progress. Advices and contributions will be appreciated
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
//! Solver-backend facade for condensed-phase IVP tasks.
//!
//! The IVP reactor workflow starts from symbolic equations and typed initial
//! conditions, so the user-facing facade mirrors only the meaningful LSODE2
//! routes: method family, symbolic execution backend, and matrix structure.

use RustedSciThe::numerical::LSODE2::{
    Lsode2AotProfile, Lsode2AotToolchain, Lsode2ControllerConfig, Lsode2LinearSolverPolicy,
    Lsode2LinearSystemStructure, Lsode2Method, Lsode2NativeExecutionConfig, Lsode2ProblemConfig,
    Lsode2ResidualJacobianSource, Lsode2SymbolicAssemblyBackend, Lsode2SymbolicExecutionMode,
};
use RustedSciThe::symbolic::symbolic_engine::Expr;
use nalgebra::DVector;
use std::fmt;

const REACTOR_IVP_SPECIES_STATE_OFFSET: usize = 2;

/// Typed solver-backend error used while building the LSODE2 handoff.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ReactorIvpBackendError {
    InvalidConfiguration(String),
}

impl fmt::Display for ReactorIvpBackendError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidConfiguration(msg) => write!(f, "Invalid configuration: {msg}"),
        }
    }
}

impl std::error::Error for ReactorIvpBackendError {}

/// Time-integration family exposed by the condensed-phase IVP facade.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ReactorIvpMethod {
    Auto,
    Bdf,
    Adams,
}

impl Default for ReactorIvpMethod {
    fn default() -> Self {
        Self::Auto
    }
}

impl ReactorIvpMethod {
    pub fn to_controller(self) -> Lsode2ControllerConfig {
        match self {
            Self::Auto => Lsode2ControllerConfig::automatic_adams_bdf(),
            Self::Bdf => Lsode2ControllerConfig::bdf_only(),
            Self::Adams => Lsode2ControllerConfig::adams_only(),
        }
    }

    /// LSODE2 currently exposes the fixed BDF method at the problem layer.
    ///
    /// The Auto/BDF/Adams choice is carried by the controller instead of by a
    /// separate lower-level method enum.
    pub fn to_lsode2_method(self) -> Lsode2Method {
        Lsode2Method::Bdf
    }
}

/// Symbolic assembly route used before Lambdify/AOT lowering.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ReactorIvpSymbolicBackend {
    AtomView,
    ExprLegacy,
}

impl Default for ReactorIvpSymbolicBackend {
    fn default() -> Self {
        Self::AtomView
    }
}

impl ReactorIvpSymbolicBackend {
    pub fn to_rusted(self) -> Lsode2SymbolicAssemblyBackend {
        match self {
            Self::AtomView => Lsode2SymbolicAssemblyBackend::AtomView,
            Self::ExprLegacy => Lsode2SymbolicAssemblyBackend::ExprLegacy,
        }
    }
}

/// Matrix representation used by generated IVP callbacks.
///
/// The public facade intentionally keeps `Banded` parameter-free. LSODE2 can
/// infer the effective band width from the symbolic Jacobian route, so the
/// user-facing contract should not ask for `kl`/`ku` manually.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ReactorIvpMatrixBackend {
    Sparse,
    Banded,
}

impl Default for ReactorIvpMatrixBackend {
    fn default() -> Self {
        Self::Sparse
    }
}

impl ReactorIvpMatrixBackend {
    /// Validate the matrix route for a problem with `variable_count` states.
    pub fn validate_for_dimension(
        self,
        variable_count: usize,
    ) -> Result<(), ReactorIvpBackendError> {
        match self {
            Self::Sparse => Ok(()),
            Self::Banded => {
                if variable_count == 0 {
                    return Err(ReactorIvpBackendError::InvalidConfiguration(
                        "banded matrix backend requires at least one variable".to_string(),
                    ));
                }
                Ok(())
            }
        }
    }

    pub fn to_rusted(self) -> Lsode2LinearSystemStructure {
        match self {
            Self::Sparse => Lsode2LinearSystemStructure::Sparse,
            Self::Banded => Lsode2LinearSystemStructure::Banded { kl: 0, ku: 0 },
        }
    }

    pub fn is_banded(self) -> bool {
        matches!(self, Self::Banded)
    }
}

/// Execution route for symbolic residual/Jacobian lowering.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReactorIvpExecutionBackend {
    Lambdify,
    Aot {
        toolchain: Lsode2AotToolchain,
        profile: Lsode2AotProfile,
    },
}

impl Default for ReactorIvpExecutionBackend {
    fn default() -> Self {
        Self::Lambdify
    }
}

/// Early-termination rule for the condensed IVP solver.
///
/// The reactor UI exposes this as a selected species concentration plus a
/// lower-or-equal threshold. We keep the backend representation index-based so
/// the typed config stays `Copy` and can be cheaply moved through the task.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ReactorIvpStopCondition {
    pub species_index: usize,
    pub threshold: f64,
}

impl ReactorIvpStopCondition {
    pub fn new(species_index: usize, threshold: f64) -> Self {
        Self {
            species_index,
            threshold,
        }
    }

    /// Validate the stop rule against the current variable list.
    pub fn validate(self, species_count: usize) -> Result<(), ReactorIvpBackendError> {
        if self.species_index >= species_count {
            return Err(ReactorIvpBackendError::InvalidConfiguration(format!(
                "stop condition species index {} is out of range for {} species",
                self.species_index, species_count
            )));
        }
        if !self.threshold.is_finite() {
            return Err(ReactorIvpBackendError::InvalidConfiguration(
                "stop condition threshold must be finite".to_string(),
            ));
        }
        if self.threshold < 0.0 {
            return Err(ReactorIvpBackendError::InvalidConfiguration(
                "stop condition threshold must not be negative".to_string(),
            ));
        }
        Ok(())
    }
}

/// Typed condensed-phase IVP solver configuration.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ReactorIvpSolverConfig {
    pub method: ReactorIvpMethod,
    pub symbolic_backend: ReactorIvpSymbolicBackend,
    pub matrix_backend: ReactorIvpMatrixBackend,
    pub execution_backend: ReactorIvpExecutionBackend,
    pub stop_condition: Option<ReactorIvpStopCondition>,
    pub x0: f64,
    pub x_bound: f64,
    pub first_step: Option<f64>,
    pub max_step: f64,
    pub rtol: f64,
    pub atol: f64,
    pub native_execution: Lsode2NativeExecutionConfig,
}

impl Default for ReactorIvpSolverConfig {
    fn default() -> Self {
        Self {
            method: ReactorIvpMethod::default(),
            symbolic_backend: ReactorIvpSymbolicBackend::default(),
            matrix_backend: ReactorIvpMatrixBackend::default(),
            execution_backend: ReactorIvpExecutionBackend::default(),
            stop_condition: None,
            x0: 0.0,
            x_bound: 1.0,
            first_step: None,
            max_step: 1.0,
            rtol: 1e-6,
            atol: 1e-8,
            native_execution: Lsode2NativeExecutionConfig::faithful_bdf_solve(200_000, 200_000),
        }
    }
}

impl ReactorIvpSolverConfig {
    pub fn default_lambdify() -> Self {
        Self::default()
    }

    pub fn with_method(mut self, method: ReactorIvpMethod) -> Self {
        self.method = method;
        self
    }

    pub fn with_symbolic_backend(mut self, backend: ReactorIvpSymbolicBackend) -> Self {
        self.symbolic_backend = backend;
        self
    }

    pub fn with_matrix_backend(mut self, backend: ReactorIvpMatrixBackend) -> Self {
        self.matrix_backend = backend;
        self
    }

    pub fn with_execution_backend(mut self, backend: ReactorIvpExecutionBackend) -> Self {
        self.execution_backend = backend;
        self
    }

    /// Attach a stop condition that ends the solve when the selected species
    /// concentration reaches the supplied lower bound.
    pub fn with_stop_condition_le(mut self, species_index: usize, threshold: f64) -> Self {
        self.stop_condition = Some(ReactorIvpStopCondition::new(species_index, threshold));
        self
    }

    /// Clear any configured stop condition.
    pub fn without_stop_condition(mut self) -> Self {
        self.stop_condition = None;
        self
    }

    /// Convenience constructor for the explicit AOT route.
    pub fn with_aot_backend(
        mut self,
        toolchain: Lsode2AotToolchain,
        profile: Lsode2AotProfile,
    ) -> Self {
        self.execution_backend = ReactorIvpExecutionBackend::Aot { toolchain, profile };
        self
    }

    pub fn with_integration_domain(mut self, x0: f64, x_bound: f64) -> Self {
        self.x0 = x0;
        self.x_bound = x_bound;
        self
    }

    pub fn with_first_step(mut self, first_step: Option<f64>) -> Self {
        self.first_step = first_step;
        self
    }

    pub fn with_max_step(mut self, max_step: f64) -> Self {
        self.max_step = max_step;
        self
    }

    pub fn with_rtol(mut self, rtol: f64) -> Self {
        self.rtol = rtol;
        self
    }

    pub fn with_atol(mut self, atol: f64) -> Self {
        self.atol = atol;
        self
    }

    pub fn with_native_execution(mut self, native_execution: Lsode2NativeExecutionConfig) -> Self {
        self.native_execution = native_execution;
        self
    }

    pub fn is_aot(self) -> bool {
        matches!(
            self.execution_backend,
            ReactorIvpExecutionBackend::Aot { .. }
        )
    }

    /// Validate the route-level backend contract before building LSODE2 config.
    pub fn validate_backend_contract(
        self,
        variable_count: usize,
    ) -> Result<(), ReactorIvpBackendError> {
        self.matrix_backend.validate_for_dimension(variable_count)?;
        Ok(())
    }

    pub fn to_rusted_linear_policy(self) -> Lsode2LinearSolverPolicy {
        Lsode2LinearSolverPolicy::Auto
    }

    pub fn to_rusted_residual_source(self) -> Lsode2ResidualJacobianSource {
        let execution = match self.execution_backend {
            ReactorIvpExecutionBackend::Lambdify => Lsode2SymbolicExecutionMode::LambdifyExpr,
            ReactorIvpExecutionBackend::Aot { toolchain, profile } => {
                Lsode2SymbolicExecutionMode::Aot { toolchain, profile }
            }
        };
        Lsode2ResidualJacobianSource::Symbolic {
            assembly: self.symbolic_backend.to_rusted(),
            execution,
        }
    }

    pub fn to_rusted_problem_config(
        self,
        eq_system: Vec<Expr>,
        values: Vec<String>,
        arg: String,
        y0: DVector<f64>,
    ) -> Result<Lsode2ProblemConfig, ReactorIvpBackendError> {
        if eq_system.len() != values.len() {
            return Err(ReactorIvpBackendError::InvalidConfiguration(format!(
                "equation count {} must match variable count {}",
                eq_system.len(),
                values.len()
            )));
        }
        if y0.len() != values.len() {
            return Err(ReactorIvpBackendError::InvalidConfiguration(format!(
                "initial-state length {} must match variable count {}",
                y0.len(),
                values.len()
            )));
        }
        self.validate_backend_contract(values.len())?;
        if !self.x0.is_finite() || !self.x_bound.is_finite() {
            return Err(ReactorIvpBackendError::InvalidConfiguration(
                "integration bounds must be finite".to_string(),
            ));
        }
        if self.x_bound <= self.x0 {
            return Err(ReactorIvpBackendError::InvalidConfiguration(format!(
                "integration bound must be larger than x0 (x0={}, x_bound={})",
                self.x0, self.x_bound
            )));
        }
        if !self.max_step.is_finite() || self.max_step <= 0.0 {
            return Err(ReactorIvpBackendError::InvalidConfiguration(format!(
                "max_step must be finite and positive, got {}",
                self.max_step
            )));
        }
        if !self.rtol.is_finite() || self.rtol <= 0.0 {
            return Err(ReactorIvpBackendError::InvalidConfiguration(format!(
                "rtol must be finite and positive, got {}",
                self.rtol
            )));
        }
        if !self.atol.is_finite() || self.atol <= 0.0 {
            return Err(ReactorIvpBackendError::InvalidConfiguration(format!(
                "atol must be finite and positive, got {}",
                self.atol
            )));
        }
        if let Some(first_step) = self.first_step {
            if !first_step.is_finite() || first_step <= 0.0 {
                return Err(ReactorIvpBackendError::InvalidConfiguration(format!(
                    "first_step must be finite and positive when set, got {}",
                    first_step
                )));
            }
        }
        let species_count = values
            .len()
            .checked_sub(REACTOR_IVP_SPECIES_STATE_OFFSET)
            .ok_or_else(|| {
                ReactorIvpBackendError::InvalidConfiguration(
                    "condensed IVP stop conditions require at least two thermal states".to_string(),
                )
            })?;
        if let Some(stop_condition) = self.stop_condition {
            stop_condition.validate(species_count)?;
        }

        let mut config = Lsode2ProblemConfig::new(
            eq_system,
            values,
            arg,
            self.x0,
            y0,
            self.x_bound,
            self.max_step,
            self.rtol,
            self.atol,
        );
        config.method = self.method.to_lsode2_method();
        config.first_step = self.first_step;
        config.controller = self.method.to_controller();
        config.residual_jacobian_source = self.to_rusted_residual_source();
        config.linear_system_structure = self.matrix_backend.to_rusted();
        config.linear_solver_policy = self.to_rusted_linear_policy();
        config.native_execution = self.native_execution;
        if let Some(stop_condition) = self.stop_condition {
            let species_name = config
                .values
                .get(REACTOR_IVP_SPECIES_STATE_OFFSET + stop_condition.species_index)
                .cloned()
                .ok_or_else(|| {
                    ReactorIvpBackendError::InvalidConfiguration(format!(
                        "stop condition species index {} is out of range",
                        stop_condition.species_index
                    ))
                })?;
            config = config.with_stop_condition_le(species_name, stop_condition.threshold);
        }
        Ok(config)
    }
}