KiThe 0.3.22

A numerical suite for chemical kinetics and thermodynamics, combustion, heat and mass transfer,chemical equilibrium, chemical engeneering
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
//! Non-transactional diagnostics for reaction-library maintenance.
//!
//! Production APIs remain fail-fast and transactional. These audit functions
//! instead retain one outcome per requested reaction so a damaged catalog can
//! be inspected without losing later valid records.
//!
//! A clean report confirms that selected records exist, satisfy the current
//! payload schema, and have parseable equations. It does not replace runtime
//! model checks performed by the production calculation workflow.
//!
//! A typical caller audits first, decides whether the requested set is clean,
//! and then independently uses the ordinary production facade:
//!
//! ```no_run
//! use KiThe::Kinetics::library_audit::{
//!     audit_library_payload_range, audit_parsed_equations,
//! };
//! use KiThe::Kinetics::User_reactions::KinData;
//! use KiThe::Kinetics::error::{KineticsError, KineticsResult};
//!
//! fn prepare_mechanism() -> KineticsResult<KinData> {
//!     let payloads = audit_library_payload_range("Cantera", 1..=3)?;
//!     let equations = audit_parsed_equations(&payloads);
//!     if !payloads.is_clean() || !equations.is_clean() {
//!         return Err(KineticsError::InvalidReactionData(
//!             "selected Cantera reactions failed library audit".into(),
//!         ));
//!     }
//!
//!     let mut kinetics = KinData::new();
//!     kinetics.set_reactions_from_shortcut_range("C1..3".into())?;
//!     kinetics.get_reactions_from_shortcuts()?;
//!     kinetics.reactdata_parsing()?;
//!     kinetics.analyze_reactions()?;
//!     Ok(kinetics)
//! }
//! ```

use std::ops::RangeInclusive;
use std::path::Path;

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::Kinetics::error::{KineticsError, KineticsResult};
use crate::Kinetics::kinetics_lib_api::KineticData;
use crate::Kinetics::mechfinder_api::ReactionData;
use crate::Kinetics::stoichiometry_analyzer::StoichAnalyzer;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AuditStage {
    CatalogLookup,
    PayloadDeserialize,
    PayloadValidation,
    EquationAnalysis,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AuditErrorKind {
    MissingReaction,
    Json,
    InvalidPayload,
    ReactionSyntax,
    UnknownSpecies,
    Other,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuditFailure {
    pub stage: AuditStage,
    pub kind: AuditErrorKind,
    pub message: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PayloadAuditRecord {
    pub reaction_id: String,
    pub range_offset: u64,
    pub raw_payload: Option<Value>,
    pub result: Result<ReactionData, AuditFailure>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PayloadAuditReport {
    pub library: String,
    pub requested_reaction_ids: Vec<String>,
    pub requested_range: Option<RangeInclusive<u64>>,
    pub records: Vec<PayloadAuditRecord>,
}

impl PayloadAuditReport {
    pub fn successful_count(&self) -> usize {
        self.records
            .iter()
            .filter(|record| record.result.is_ok())
            .count()
    }

    pub fn failed_count(&self) -> usize {
        self.records.len() - self.successful_count()
    }

    pub fn is_clean(&self) -> bool {
        self.failed_count() == 0
    }

    pub fn write_json(&self, path: impl AsRef<Path>) -> KineticsResult<()> {
        write_json(path, self)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EquationParseSummary {
    pub equation: String,
    pub species: Vec<String>,
    pub stoichiometric_coefficients: Vec<f64>,
    pub reactant_coefficients: Vec<f64>,
    pub product_coefficients: Vec<f64>,
    pub reactant_orders: Vec<f64>,
    pub product_orders: Vec<f64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EquationAuditRecord {
    pub reaction_id: String,
    pub equation: String,
    pub result: Result<EquationParseSummary, AuditFailure>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EquationAuditReport {
    pub library: String,
    pub requested_reaction_ids: Vec<String>,
    pub requested_range: Option<RangeInclusive<u64>>,
    pub records: Vec<EquationAuditRecord>,
}

impl EquationAuditReport {
    pub fn successful_count(&self) -> usize {
        self.records
            .iter()
            .filter(|record| record.result.is_ok())
            .count()
    }

    pub fn failed_count(&self) -> usize {
        self.records.len() - self.successful_count()
    }

    pub fn is_clean(&self) -> bool {
        self.failed_count() == 0
    }

    pub fn write_json(&self, path: impl AsRef<Path>) -> KineticsResult<()> {
        write_json(path, self)
    }
}

/// Audit every numeric reaction ID in an inclusive range, continuing after
/// missing IDs, JSON-shape errors, and domain validation failures.
pub fn audit_library_payload_range(
    library: &str,
    range: RangeInclusive<u64>,
) -> KineticsResult<PayloadAuditReport> {
    if range.is_empty() {
        return Err(KineticsError::InvalidShortcutRange(format!(
            "{}..={}",
            range.start(),
            range.end()
        )));
    }

    let reaction_ids: Vec<_> = range.clone().map(|id| id.to_string()).collect();
    audit_library_reactions_inner(library, &reaction_ids, Some(range))
}

/// Audit an explicit list of canonical reaction IDs from one library.
///
/// Each requested ID receives an outcome, including IDs absent from the
/// library. This returns diagnostics only and never mutates production state.
pub fn audit_library_reactions(
    library: &str,
    reaction_ids: &[String],
) -> KineticsResult<PayloadAuditReport> {
    if reaction_ids.is_empty() {
        return Err(KineticsError::InvalidReactionData(
            "library audit requires at least one reaction ID".to_string(),
        ));
    }
    audit_library_reactions_inner(library, reaction_ids, None)
}

fn audit_library_reactions_inner(
    library: &str,
    reaction_ids: &[String],
    requested_range: Option<RangeInclusive<u64>>,
) -> KineticsResult<PayloadAuditReport> {
    let mut catalog = KineticData::new();
    catalog.open_json_files(library)?;
    let records = reaction_ids
        .iter()
        .enumerate()
        .map(|(offset, reaction_id)| {
            audit_catalog_entry(
                library,
                reaction_id.clone(),
                offset as u64,
                catalog.LibKineticData.get(reaction_id).cloned(),
            )
        })
        .collect();

    Ok(PayloadAuditReport {
        library: library.to_string(),
        requested_reaction_ids: reaction_ids.to_vec(),
        requested_range,
        records,
    })
}

fn audit_catalog_entry(
    library: &str,
    reaction_id: String,
    range_offset: u64,
    raw_payload: Option<Value>,
) -> PayloadAuditRecord {
    let result = match raw_payload.as_ref() {
        None => Err(AuditFailure {
            stage: AuditStage::CatalogLookup,
            kind: AuditErrorKind::MissingReaction,
            message: format!("reaction `{reaction_id}` is absent from library `{library}`"),
        }),
        Some(payload) => audit_payload(payload.clone(), &reaction_id),
    };
    PayloadAuditRecord {
        reaction_id,
        range_offset,
        raw_payload,
        result,
    }
}

/// Parse equations from successful payload records independently. Failed
/// payload records are intentionally excluded; each equation failure is kept
/// beside its source reaction ID while later equations continue to be checked.
pub fn audit_parsed_equations(payloads: &PayloadAuditReport) -> EquationAuditReport {
    let records = payloads
        .records
        .iter()
        .filter_map(|record| {
            let reaction = record.result.as_ref().ok()?;
            Some(EquationAuditRecord {
                reaction_id: record.reaction_id.clone(),
                equation: reaction.eq.clone(),
                result: audit_equation(reaction.eq.clone()),
            })
        })
        .collect();

    EquationAuditReport {
        library: payloads.library.clone(),
        requested_reaction_ids: payloads.requested_reaction_ids.clone(),
        requested_range: payloads.requested_range.clone(),
        records,
    }
}

fn audit_payload(payload: Value, reaction_id: &str) -> Result<ReactionData, AuditFailure> {
    let reaction =
        serde_json::from_value::<ReactionData>(payload).map_err(|error| AuditFailure {
            stage: AuditStage::PayloadDeserialize,
            kind: AuditErrorKind::Json,
            message: error.to_string(),
        })?;
    reaction.validate().map_err(|error| AuditFailure {
        stage: AuditStage::PayloadValidation,
        kind: classify_error(&error),
        message: format!("reaction `{reaction_id}`: {error}"),
    })?;
    Ok(reaction)
}

fn audit_equation(equation: String) -> Result<EquationParseSummary, AuditFailure> {
    let mut analyzer = StoichAnalyzer::new();
    analyzer.reactions = vec![equation.clone()];
    analyzer
        .search_substances()
        .and_then(|()| analyzer.analyse_reactions())
        .map_err(|error| AuditFailure {
            stage: AuditStage::EquationAnalysis,
            kind: classify_error(&error),
            message: error.to_string(),
        })?;

    Ok(EquationParseSummary {
        equation,
        species: analyzer.substances,
        stoichiometric_coefficients: analyzer.stecheo_matrx.remove(0),
        reactant_coefficients: analyzer.stecheo_reags.remove(0),
        product_coefficients: analyzer.stecheo_prods.remove(0),
        reactant_orders: analyzer.G_matrix_reag.remove(0),
        product_orders: analyzer.G_matrix_prod.remove(0),
    })
}

fn classify_error(error: &KineticsError) -> AuditErrorKind {
    match error {
        KineticsError::Context { source, .. } => classify_error(source),
        KineticsError::Json(_) => AuditErrorKind::Json,
        KineticsError::InvalidReactionPayload { .. } | KineticsError::InvalidReactionData(_) => {
            AuditErrorKind::InvalidPayload
        }
        KineticsError::ReactionSyntax { .. } | KineticsError::InvalidShortcutRange(_) => {
            AuditErrorKind::ReactionSyntax
        }
        KineticsError::UnknownReactionSpecies { .. } => AuditErrorKind::UnknownSpecies,
        _ => AuditErrorKind::Other,
    }
}

fn write_json(path: impl AsRef<Path>, report: &impl Serialize) -> KineticsResult<()> {
    let path = path.as_ref();
    let contents = serde_json::to_vec_pretty(report).map_err(KineticsError::from)?;
    std::fs::write(path, contents).map_err(|error| {
        KineticsError::Io(error).with_context(format!(
            "writing library audit report to `{}`",
            path.display()
        ))
    })
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;

    fn payload(eq: &str, arrhenius: Value) -> Value {
        json!({ "type": "elem", "eq": eq, "Arrenius": arrhenius })
    }

    #[test]
    fn non_transactional_audits_keep_good_records_around_middle_failures() {
        let values = [
            payload("A=>B", json!([1.0, 0.0, 0.0])),
            payload("malformed equation", json!([1.0, 0.0, 0.0])),
            payload("C=>D", json!([1.0, 0.0])),
            payload("E=>F", json!([1.0, 0.0, 0.0])),
        ];

        let records = values
            .iter()
            .enumerate()
            .map(|(index, value)| {
                let id = (index + 10).to_string();
                let result = audit_payload(value.clone(), &id);
                PayloadAuditRecord {
                    reaction_id: id,
                    range_offset: index as u64,
                    raw_payload: Some(value.clone()),
                    result,
                }
            })
            .collect();
        let payload_report = PayloadAuditReport {
            library: "fixture".into(),
            requested_reaction_ids: vec!["10".into(), "11".into(), "12".into(), "13".into()],
            requested_range: Some(10..=13),
            records,
        };

        assert_eq!(payload_report.successful_count(), 3);
        assert_eq!(payload_report.failed_count(), 1);
        assert!(!payload_report.is_clean());
        assert_eq!(payload_report.records[2].reaction_id, "12");
        assert_eq!(
            payload_report.records[2].result.as_ref().unwrap_err().stage,
            AuditStage::PayloadValidation
        );

        let equation_report = audit_parsed_equations(&payload_report);
        assert_eq!(equation_report.records.len(), 3);
        assert_eq!(equation_report.successful_count(), 2);
        assert_eq!(equation_report.failed_count(), 1);
        assert_eq!(equation_report.records[0].reaction_id, "10");
        assert_eq!(equation_report.records[1].reaction_id, "11");
        assert_eq!(
            equation_report.records[1]
                .result
                .as_ref()
                .unwrap_err()
                .stage,
            AuditStage::EquationAnalysis
        );
        assert_eq!(equation_report.records[2].reaction_id, "13");
    }

    #[test]
    fn audit_reports_serialize_as_json() {
        let record = PayloadAuditRecord {
            reaction_id: "1".into(),
            range_offset: 0,
            raw_payload: Some(payload("A=>B", json!([1.0, 0.0, 0.0]))),
            result: audit_payload(payload("A=>B", json!([1.0, 0.0, 0.0])), "1"),
        };
        let report = PayloadAuditReport {
            library: "fixture".into(),
            requested_reaction_ids: vec!["1".into()],
            requested_range: Some(1..=1),
            records: vec![record],
        };
        let json = serde_json::to_value(report).expect("audit report should serialize");
        assert_eq!(json["records"][0]["reaction_id"], "1");
        assert!(json["records"][0]["result"]["Ok"].is_object());
    }

    #[test]
    fn payload_audit_records_missing_ids_instead_of_skipping_them() {
        let record = audit_catalog_entry("fixture", "12".into(), 2, None);

        assert!(record.raw_payload.is_none());
        assert!(matches!(
            record.result,
            Err(AuditFailure {
                stage: AuditStage::CatalogLookup,
                kind: AuditErrorKind::MissingReaction,
                ..
            })
        ));
    }

    #[test]
    #[ignore = "bundled catalog audit story; included in the Kinetics campaign"]
    fn story_cantera_range_audit_writes_payload_and_equation_reports() {
        let payload_report = audit_library_payload_range("Cantera", 0..=100)
            .expect("Cantera catalog should open for a bounded audit");
        assert_eq!(payload_report.records.len(), 101);
        assert!(payload_report.is_clean());

        let equation_report = audit_parsed_equations(&payload_report);
        assert!(equation_report.is_clean());
        assert_eq!(
            equation_report.records.len(),
            payload_report.successful_count()
        );

        let output = tempfile::tempdir().expect("temporary audit report directory");
        let payload_path = output.path().join("payload-audit.json");
        let equation_path = output.path().join("equation-audit.json");
        payload_report
            .write_json(&payload_path)
            .expect("payload audit report should be persisted");
        equation_report
            .write_json(&equation_path)
            .expect("equation audit report should be persisted");

        let payload_json: Value = serde_json::from_slice(
            &std::fs::read(&payload_path).expect("payload report file should exist"),
        )
        .expect("payload report should be valid JSON");
        let equation_json: Value = serde_json::from_slice(
            &std::fs::read(&equation_path).expect("equation report file should exist"),
        )
        .expect("equation report should be valid JSON");
        assert_eq!(payload_json["records"].as_array().unwrap().len(), 101);
        assert_eq!(
            equation_json["records"].as_array().unwrap().len(),
            payload_report.successful_count()
        );

        let explicit_ids = vec!["1".to_string(), "__missing_audit_fixture_id__".to_string()];
        let explicit_report = audit_library_reactions("Cantera", &explicit_ids)
            .expect("explicit reaction IDs should be auditable");
        assert_eq!(explicit_report.records.len(), 2);
        assert_eq!(explicit_report.successful_count(), 1);
        assert_eq!(explicit_report.failed_count(), 1);
        assert!(!explicit_report.is_clean());
        assert_eq!(
            explicit_report.records[1].result.as_ref().unwrap_err().kind,
            AuditErrorKind::MissingReaction
        );

        println!(
            "Cantera audit 0..=100: payloads_ok={}, payloads_failed={}, equations_ok={}, equations_failed={}",
            payload_report.successful_count(),
            payload_report.failed_count(),
            equation_report.successful_count(),
            equation_report.failed_count()
        );
        for record in payload_report
            .records
            .iter()
            .filter_map(|record| record.result.as_ref().err().map(|error| (record, error)))
        {
            println!(
                "payload issue id={} stage={:?} kind={:?}: {}",
                record.0.reaction_id, record.1.stage, record.1.kind, record.1.message
            );
        }
        for record in equation_report
            .records
            .iter()
            .filter_map(|record| record.result.as_ref().err().map(|error| (record, error)))
        {
            println!(
                "equation issue id={} equation={:?} kind={:?}: {}",
                record.0.reaction_id, record.0.equation, record.1.kind, record.1.message
            );
        }
    }
}