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
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
#[cfg(test)]
mod tests {
    use super::super::SimpleReactorBVP::NrbvpHandoffConfig;
    use super::super::SimpleReactorBVP::ReactorError;
    use super::super::reactor_bvp_test_support::{
        compact_hmx_reactor, compact_hmx_reactor_with_product_fraction,
    };
    use super::super::solver_backend::ReactorBvpSolverConfig;
    use crate::Kinetics::User_reactions::KinDataState;
    use crate::SimpleReactorsBVP::reactor_BVP_utils::{BoundsConfig, ToleranceConfig};
    use RustedSciThe::numerical::BVP_Damp::NR_Damp_solver_frozen::FrozenSolverOptions;
    use RustedSciThe::numerical::BVP_Damp::telemetry::BvpTelemetryMode;
    use RustedSciThe::symbolic::codegen::codegen_provider_api::MatrixBackend;
    use RustedSciThe::symbolic::symbolic_engine::Expr;
    use RustedSciThe::symbolic::symbolic_functions_BVP::BvpSymbolicAssemblyBackend;
    use nalgebra::DMatrix;
    use std::collections::HashMap;
    use std::error::Error;
    use std::path::PathBuf;

    #[test]
    fn checked_in_physical_bvp_documents_load_and_solve() {
        for file in ["hmx_compact.task", "hmx_compact_two_percent_product.task"] {
            let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
                .join("task_docs/reactors/bvp")
                .join(file);
            let mut reactor = super::super::SimpleReactorBVP::SimpleReactorTask::new();
            reactor
                .solve_from_file(path)
                .unwrap_or_else(|error| panic!("{file}: file-to-solution story failed: {error}"));

            let data = reactor
                .solution_render_data()
                .unwrap_or_else(|error| panic!("{file}: missing solution data: {error}"));
            assert!(data.x_mesh.iter().all(|value| value.is_finite()), "{file}");
            assert!(
                data.solution.iter().all(|value| value.is_finite()),
                "{file}"
            );
            let temperature_index = data
                .unknowns
                .iter()
                .position(|name| name == "Teta")
                .expect("BVP state should include temperature");
            let temperatures = data.solution.column(temperature_index);
            assert!(
                temperatures
                    .iter()
                    .all(|temperature| (0.0..=3000.0).contains(temperature)),
                "{file}: physical temperature outside [0, 3000] K: {temperatures:?}"
            );
            for (index, name) in data.unknowns.iter().enumerate() {
                if name.starts_with('C') {
                    assert!(
                        data.solution
                            .column(index)
                            .iter()
                            .all(|fraction| (0.0..=1.0).contains(fraction)),
                        "{file}: invalid mass fraction in {name}"
                    );
                }
            }
            println!(
                "BVP task-doc {file}: solved {} species / {} reactions, T={:.2}..{:.2} K, points={}",
                reactor.kindata.substances.len(),
                reactor.kindata.equations().len(),
                temperatures.iter().copied().fold(f64::INFINITY, f64::min),
                temperatures
                    .iter()
                    .copied()
                    .fold(f64::NEG_INFINITY, f64::max),
                data.x_mesh.len()
            );
        }
    }

    #[test]
    fn correction_newton_physical_task_file_solves_and_reports_telemetry() {
        let source_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("task_docs/reactors/bvp/hmx_compact.task");
        let source = std::fs::read_to_string(&source_path).expect("read compact HMX task");
        let settings_start = source
            .find("solver_settings\n")
            .expect("checked-in task has solver settings");
        let suffix = &source[settings_start..];
        let bounds_start = suffix
            .find("\nbounds\n")
            .map(|relative| settings_start + relative + 1)
            .expect("checked-in task has grouped bounds");
        let correction_settings = "solver_settings\nsolver_family: correction_newton\nassembly: expr_legacy\nexecution: lambdify\nmatrix_layout: sparse\nabsolute_tolerance: 1e-7\ndefault_relative_tolerance: 1e-7\nmax_iterations: 100\nmax_jacobian_age: 5\nmax_damping_trials: 7\ntelemetry: detailed\nloglevel: off\n\n";
        let task_text = format!(
            "{}{}{}",
            &source[..settings_start],
            correction_settings,
            &source[bounds_start..]
        );
        let temp_dir = tempfile::tempdir().expect("temporary task directory");
        let task_path = temp_dir.path().join("hmx_correction_newton.task");
        std::fs::write(&task_path, task_text).expect("write CorrectionNewton task document");

        let mut reactor = super::super::SimpleReactorBVP::SimpleReactorTask::new();
        reactor
            .solve_from_file(task_path)
            .expect("CorrectionNewton should solve the physical HMX task file");

        let data = reactor
            .solution_render_data()
            .expect("CorrectionNewton should publish renderable task data");
        assert!(data.solution.iter().all(|value| value.is_finite()));
        assert!(data.x_mesh.len() >= 2);
        let teta = data
            .unknowns
            .iter()
            .position(|name| name == "Teta")
            .expect("temperature state");
        assert!(data.solution.column(teta).iter().all(|temperature| {
            temperature.is_finite() && *temperature > 0.0 && *temperature < 3000.0
        }));
        let Some(RustedSciThe::numerical::BVP_api::BvpSolverTelemetrySnapshot::CorrectionNewton(
            Some(telemetry),
        )) = reactor.solver.last_telemetry.as_ref()
        else {
            panic!("task-file CorrectionNewton solve should retain native telemetry");
        };
        assert!(telemetry.residual_evaluations > 0);
        assert!(telemetry.jacobian_evaluations > 0);
        assert!(telemetry.linear_solves > 0);
        let successful_counts = (
            telemetry.residual_evaluations,
            telemetry.jacobian_evaluations,
            telemetry.linear_solves,
        );
        let previous_solution = reactor.solver.solution.clone().expect("published profile");
        let previous_mesh = reactor.solver.x_mesh.clone().expect("published mesh");
        let mut failing_options =
            RustedSciThe::numerical::BVP_CorrectionNewton::CorrectionNewtonOptions {
                absolute_tolerance: 1e-16,
                default_relative_tolerance: 1e-16,
                max_iterations: 1,
                max_damping_trials: 1,
                telemetry_mode: RustedSciThe::numerical::BVP_CorrectionNewton::CorrectionNewtonTelemetryMode::Counters,
                ..Default::default()
            };
        failing_options.relative_tolerances = reactor
            .solver
            .unknowns
            .iter()
            .map(|state| (state.clone(), 1e-16))
            .collect();
        failing_options.bounds = reactor
            .solver
            .unknowns
            .iter()
            .map(|state| {
                let interval = if state == "Teta" {
                    (-100.0, 100.0)
                } else if state == "q" || state.starts_with('J') {
                    (-1e20, 1e20)
                } else {
                    (-0.1, 1.1)
                };
                (state.clone(), interval)
            })
            .collect();
        let state_count = reactor.solver.unknowns.len();
        let failed_retry = reactor.solver.solve_with_hybrid_settings(
            RustedSciThe::numerical::BVP_api::BvpSolverSettings::CorrectionNewton(failing_options),
            DMatrix::from_element(state_count, 32, 0.5),
            0.0,
            1.0,
            32,
        );
        assert!(matches!(failed_retry, Err(ReactorError::BvpApi(_))));
        assert_eq!(reactor.solver.solution.as_ref(), Some(&previous_solution));
        assert_eq!(reactor.solver.x_mesh.as_ref(), Some(&previous_mesh));
        let Some(RustedSciThe::numerical::BVP_api::BvpSolverTelemetrySnapshot::CorrectionNewton(
            Some(failed_telemetry),
        )) = reactor.solver.last_telemetry.as_ref()
        else {
            panic!("failed CorrectionNewton retry should retain partial telemetry");
        };
        assert!(failed_telemetry.residual_evaluations > 0);
        println!(
            "CorrectionNewton task-file: nodes={} T={:.3}..{:.3} K residuals={} jacobians={} solves={}; failed retry residuals={} retained prior result",
            data.x_mesh.len(),
            data.solution
                .column(teta)
                .iter()
                .copied()
                .fold(f64::INFINITY, f64::min),
            data.solution
                .column(teta)
                .iter()
                .copied()
                .fold(f64::NEG_INFINITY, f64::max),
            successful_counts.0,
            successful_counts.1,
            successful_counts.2,
            failed_telemetry.residual_evaluations,
        );
    }

    /// End-to-end story test for the modern default reactor BVP setup.
    #[test]
    fn story_compact_hmx_setup_uses_the_default_facade() {
        let mut reactor = compact_hmx_reactor();
        reactor
            .setup_bvp()
            .expect("compact HMX setup should succeed");
        let initial_guess = DMatrix::from_element(reactor.solver.unknowns.len(), 4, 0.5);
        let config = NrbvpHandoffConfig::new(
            initial_guess,
            0.0,
            1.0,
            4,
            "forward".to_string(),
            "Damped".to_string(),
            None,
            None,
            "Sparse".to_string(),
            1e-8,
            None,
            50,
            None,
            Some("info".to_string()),
        )
        .with_solver_backend_config(ReactorBvpSolverConfig::default_lambdify());
        let backend = reactor
            .solver
            .build_nrbvp_backend(config)
            .expect("backend snapshot should build");

        assert_eq!(
            backend.generated_backend_config().symbolic_assembly_backend,
            BvpSymbolicAssemblyBackend::AtomView
        );
        assert_eq!(
            backend.generated_backend_config().matrix_backend_override,
            Some(MatrixBackend::Banded)
        );
        assert_eq!(
            reactor.solver.unknowns,
            vec!["Teta", "q", "C0", "J0", "C1", "J1"]
        );
        assert_eq!(reactor.kindata.state, KinDataState::Analyzed);
    }

    /// Story-level handoff test that keeps the solver facade and the backend snapshot aligned.
    #[test]
    fn story_build_nrbvp_backend_uses_facade_generated_options() {
        let mut reactor = compact_hmx_reactor();
        reactor
            .setup_bvp()
            .expect("compact HMX setup should succeed");
        let initial_guess = DMatrix::from_element(reactor.solver.unknowns.len(), 4, 0.5);
        let config = NrbvpHandoffConfig::new(
            initial_guess,
            0.0,
            1.0,
            4,
            "forward".to_string(),
            "Damped".to_string(),
            None,
            None,
            "Sparse".to_string(),
            1e-8,
            None,
            50,
            None,
            Some("info".to_string()),
        )
        .with_solver_backend_config(ReactorBvpSolverConfig::default_lambdify());

        let backend = reactor
            .solver
            .build_nrbvp_backend(config)
            .expect("backend snapshot should build");

        assert_eq!(
            backend.generated_backend_config().symbolic_assembly_backend,
            BvpSymbolicAssemblyBackend::AtomView
        );
        assert_eq!(
            backend.generated_backend_config().matrix_backend_override,
            Some(MatrixBackend::Banded)
        );
        assert_eq!(reactor.kindata.state, KinDataState::Analyzed);
    }

    #[test]
    fn story_compact_reactor_solves_through_damped_bvp_api_with_boundary_conditions() {
        let mut reactor = compact_hmx_reactor();
        reactor
            .setup_bvp()
            .expect("compact reactor should prepare through the production setup");
        let state_count = reactor.solver.unknowns.len();
        let intervals = 32;
        let substances = ["HMX".to_string(), "DecompositionProduct".to_string()];
        let config = NrbvpHandoffConfig::new(
            DMatrix::from_element(state_count, intervals, 0.5),
            0.0,
            1.0,
            intervals,
            "forward".to_string(),
            "Damped".to_string(),
            None,
            None,
            "Banded".to_string(),
            1e-8,
            Some(ToleranceConfig::new(1e-8, 1e-8, 1e-8, 1e-8).to_full_tolerance_map(&substances)),
            80,
            Some(
                BoundsConfig::new((-0.1, 1.1), (-1e20, 1e20), (-100.0, 100.0), (-1e20, 1e20))
                    .to_full_bounds_map(&substances),
            ),
            Some("off".to_string()),
        );

        reactor
            .solver
            .solve_with_bvp_api(config)
            .expect("compact reactor should solve through BVP_api");

        let solution = reactor
            .solver
            .get_solution()
            .expect("successful facade solve should publish a solution");
        let mesh = reactor
            .solver
            .x_mesh
            .as_ref()
            .expect("successful facade solve should publish its mesh");
        assert_eq!(solution.shape(), (mesh.len(), state_count));
        assert_eq!(mesh.len(), intervals + 1);
        assert!(solution.iter().all(|value| value.is_finite()));
        for (state, name) in reactor.solver.unknowns.iter().enumerate() {
            let &(endpoint, expected) = reactor
                .solver
                .BorderConditions
                .get(name)
                .expect("each state should have one endpoint condition");
            let row = if endpoint == 0 { 0 } else { mesh.len() - 1 };
            assert!(
                (solution[(row, state)] - expected).abs() < 1e-5,
                "boundary residual for {name}: actual={}, expected={expected}",
                solution[(row, state)]
            );
        }
    }

    #[test]
    fn story_compact_reactor_frozen_and_naive_publish_full_nodal_profiles() {
        let mut reactor = compact_hmx_reactor();
        reactor
            .setup_bvp()
            .expect("compact reactor should prepare through the production setup");
        let state_count = reactor.solver.unknowns.len();
        let intervals = 32;
        let nodes = intervals + 1;
        let substances = ["HMX".to_string(), "DecompositionProduct".to_string()];
        let damped_config = NrbvpHandoffConfig::new(
            DMatrix::from_element(state_count, intervals, 0.5),
            0.0,
            1.0,
            intervals,
            "forward".to_string(),
            "Damped".to_string(),
            None,
            None,
            "Banded".to_string(),
            1e-8,
            Some(ToleranceConfig::new(1e-8, 1e-8, 1e-8, 1e-8).to_full_tolerance_map(&substances)),
            80,
            Some(
                BoundsConfig::new((-0.1, 1.1), (-1e20, 1e20), (-100.0, 100.0), (-1e20, 1e20))
                    .to_full_bounds_map(&substances),
            ),
            Some("off".to_string()),
        );
        reactor
            .solver
            .solve_with_bvp_api(damped_config)
            .expect("Damped should provide a physical profile to seed Frozen");
        let damped_profile = reactor
            .solver
            .get_solution()
            .expect("Damped solve should publish its profile")
            .clone();
        let initial_guess = DMatrix::from_fn(state_count, nodes, |state, node| {
            damped_profile[(node, state)]
        });
        reactor.solver = reactor.solver.with_bvp_telemetry_mode(
            RustedSciThe::numerical::BVP_api::BvpApiTelemetryMode::Counters,
        );

        reactor
            .solver
            .solve_BVPfrozen(
                initial_guess.clone(),
                FrozenSolverOptions::default()
                    .with_tolerance(1e-6)
                    .with_max_iterations(500),
            )
            .expect("Frozen should publish its converged full nodal profile");

        let frozen_profile = reactor
            .solver
            .get_solution()
            .expect("accepted Frozen profile should be available");
        let frozen_mesh = reactor
            .solver
            .x_mesh
            .as_ref()
            .expect("accepted Frozen mesh should be available");
        assert_eq!(frozen_profile.shape(), (nodes, state_count));
        assert_eq!(frozen_mesh.len(), nodes);
        assert!(frozen_profile.iter().all(|value| value.is_finite()));
        for (state, name) in reactor.solver.unknowns.iter().enumerate() {
            let &(endpoint, expected) = reactor
                .solver
                .BorderConditions
                .get(name)
                .expect("each state should have one endpoint condition");
            let row = if endpoint == 0 {
                0
            } else {
                frozen_mesh.len() - 1
            };
            assert!(
                (frozen_profile[(row, state)] - expected).abs() < 1e-5,
                "Frozen boundary residual for {name}: actual={}, expected={expected}",
                frozen_profile[(row, state)]
            );
        }
        let Some(RustedSciThe::numerical::BVP_api::BvpSolverTelemetrySnapshot::Frozen(snapshot)) =
            reactor.solver.last_telemetry.as_ref()
        else {
            panic!("accepted Frozen attempt should retain its route-tagged telemetry");
        };
        assert_eq!(
            snapshot.telemetry_mode,
            RustedSciThe::numerical::BVP_Damp::telemetry::BvpTelemetryMode::Counters
        );
        assert!(snapshot.counters.iterations > 0);
        assert_eq!(
            snapshot.last_solve_status,
            RustedSciThe::numerical::BVP_Damp::telemetry::BvpSolveStatus::Converged
        );

        reactor
            .solver
            .solve_BVPfrozen(
                initial_guess,
                FrozenSolverOptions::dense_naive()
                    .with_tolerance(1e-6)
                    .with_max_iterations(500),
            )
            .expect("Naive should publish the reconstructed full nodal profile");
        let naive_profile = reactor
            .solver
            .get_solution()
            .expect("accepted Naive profile should be available");
        let naive_mesh = reactor
            .solver
            .x_mesh
            .as_ref()
            .expect("accepted Naive mesh should be available");
        assert_eq!(naive_profile.shape(), (nodes, state_count));
        assert_eq!(naive_mesh.len(), nodes);
        assert!(naive_profile.iter().all(|value| value.is_finite()));
        for (state, name) in reactor.solver.unknowns.iter().enumerate() {
            let &(endpoint, expected) = reactor
                .solver
                .BorderConditions
                .get(name)
                .expect("each state should have one endpoint condition");
            let row = if endpoint == 0 {
                0
            } else {
                naive_mesh.len() - 1
            };
            assert_eq!(naive_profile[(row, state)], expected);
        }
        let Some(RustedSciThe::numerical::BVP_api::BvpSolverTelemetrySnapshot::Frozen(snapshot)) =
            reactor.solver.last_telemetry.as_ref()
        else {
            panic!("accepted Naive attempt should retain its route-tagged telemetry");
        };
        assert_eq!(
            snapshot.telemetry_mode,
            RustedSciThe::numerical::BVP_Damp::telemetry::BvpTelemetryMode::Counters
        );
        assert!(snapshot.counters.iterations > 0);
        assert_eq!(
            snapshot.last_solve_status,
            RustedSciThe::numerical::BVP_Damp::telemetry::BvpSolveStatus::Converged
        );
    }

    #[test]
    fn story_compact_reactor_solves_through_sci_bvp_api_with_boundary_conditions() {
        let mut reactor = compact_hmx_reactor();
        reactor
            .setup_bvp()
            .expect("compact reactor should prepare through the production setup");
        let state_count = reactor.solver.unknowns.len();
        let nodes = 32;
        reactor.solver = reactor.solver.with_bvp_telemetry_mode(
            RustedSciThe::numerical::BVP_api::BvpApiTelemetryMode::Counters,
        );

        reactor
            .solver
            .solve_BVPsci(
                DMatrix::from_element(state_count, nodes, 0.5),
                nodes,
                128,
                1e-5,
            )
            .expect("compact reactor should solve through the BVP_sci route");

        let solution = reactor
            .solver
            .get_solution()
            .expect("successful Sci solve should publish a solution");
        let mesh = reactor
            .solver
            .x_mesh
            .as_ref()
            .expect("successful Sci solve should publish its mesh");
        assert_eq!(solution.shape(), (mesh.len(), state_count));
        assert!(mesh.len() >= nodes);
        assert!(solution.iter().all(|value| value.is_finite()));
        for (state, name) in reactor.solver.unknowns.iter().enumerate() {
            let &(endpoint, expected) = reactor
                .solver
                .BorderConditions
                .get(name)
                .expect("each state should have one endpoint condition");
            let row = if endpoint == 0 { 0 } else { mesh.len() - 1 };
            assert!(
                (solution[(row, state)] - expected).abs() < 1e-5,
                "boundary residual for {name}: actual={}, expected={expected}",
                solution[(row, state)]
            );
        }
    }

    #[test]
    fn story_compact_reactor_inlet_composition_is_accepted_by_damped_and_sci() {
        let mut reactor = compact_hmx_reactor_with_product_fraction(0.02);
        reactor
            .setup_bvp()
            .expect("alternate inlet composition should prepare");
        let state_count = reactor.solver.unknowns.len();
        let intervals = 32;
        let substances = ["HMX".to_string(), "DecompositionProduct".to_string()];
        let damped_config = NrbvpHandoffConfig::new(
            DMatrix::from_element(state_count, intervals, 0.5),
            0.0,
            1.0,
            intervals,
            "forward".to_string(),
            "Damped".to_string(),
            None,
            None,
            "Banded".to_string(),
            1e-8,
            Some(ToleranceConfig::new(1e-8, 1e-8, 1e-8, 1e-8).to_full_tolerance_map(&substances)),
            80,
            Some(
                BoundsConfig::new((-0.1, 1.1), (-1e20, 1e20), (-100.0, 100.0), (-1e20, 1e20))
                    .to_full_bounds_map(&substances),
            ),
            Some("off".to_string()),
        );

        reactor
            .solver
            .solve_with_bvp_api(damped_config)
            .expect("Damped should solve the alternate inlet composition");
        let damped = reactor
            .solver
            .get_solution()
            .expect("Damped should publish its profile")
            .clone();
        let damped_mesh = reactor
            .solver
            .x_mesh
            .as_ref()
            .expect("Damped should publish its mesh")
            .clone();
        assert_eq!(damped.shape(), (damped_mesh.len(), state_count));
        assert!(damped.iter().all(|value| value.is_finite()));
        for (state, name) in reactor.solver.unknowns.iter().enumerate() {
            let &(endpoint, expected) = reactor
                .solver
                .BorderConditions
                .get(name)
                .expect("each state should have an endpoint condition");
            let row = if endpoint == 0 {
                0
            } else {
                damped_mesh.len() - 1
            };
            assert!(
                (damped[(row, state)] - expected).abs() < 1e-5,
                "Damped boundary residual for {name}: actual={}, expected={expected}",
                damped[(row, state)]
            );
        }

        reactor
            .solver
            .solve_BVPsci(
                DMatrix::from_element(state_count, intervals, 0.5),
                intervals,
                128,
                1e-5,
            )
            .expect("BVP_sci should solve the alternate inlet composition");
        let sci = reactor
            .solver
            .get_solution()
            .expect("BVP_sci should publish its profile");
        let sci_mesh = reactor
            .solver
            .x_mesh
            .as_ref()
            .expect("BVP_sci should publish its mesh");
        assert_eq!(sci.ncols(), state_count);
        assert!(sci.nrows() >= intervals);
        assert!(sci.iter().all(|value| value.is_finite()));
        for (state, name) in reactor.solver.unknowns.iter().enumerate() {
            let &(endpoint, expected) = reactor
                .solver
                .BorderConditions
                .get(name)
                .expect("each state should have an endpoint condition");
            let row = if endpoint == 0 { 0 } else { sci_mesh.len() - 1 };
            assert!(
                (sci[(row, state)] - expected).abs() < 1e-5,
                "BVP_sci boundary residual for {name}: actual={}, expected={expected}",
                sci[(row, state)]
            );
        }
    }

    /// The facade publishes a shared API result and retains route-native diagnostics.
    #[test]
    fn story_scalar_bvp_solves_through_bvp_api_and_retains_native_telemetry() {
        let mut solver = super::super::SimpleReactorBVP::BVPSolver::default()
            .with_bvp_telemetry_mode(
                RustedSciThe::numerical::BVP_api::BvpApiTelemetryMode::Counters,
            );
        solver.arg_name = "x".to_string();
        solver.x_range = (0.0, 1.0);
        solver.unknowns = vec!["y".to_string()];
        solver.eq_system = vec![Expr::parse_expression("-y")];
        solver.BorderConditions.insert("y".to_string(), (0, 1.0));
        let state_count = solver.unknowns.len();
        let intervals = 32;
        let config = NrbvpHandoffConfig::new(
            DMatrix::from_element(state_count, intervals, 0.5),
            0.0,
            1.0,
            intervals,
            "forward".to_string(),
            "Damped".to_string(),
            None,
            None,
            "Banded".to_string(),
            1e-8,
            Some(HashMap::from([("y".to_string(), 1e-8)])),
            80,
            Some(HashMap::from([("y".to_string(), (-10.0, 10.0))])),
            Some("off".to_string()),
        );

        solver
            .solve_with_bvp_api(config)
            .expect("scalar BVP should solve through BVP_api");

        let solution = solver
            .get_solution()
            .expect("successful BVP_api solve should publish a solution");
        assert_eq!(solution.nrows(), intervals + 1);
        assert_eq!(solution.ncols(), state_count);
        assert!(solution.iter().all(|value| value.is_finite()));
        let mesh = solver
            .x_mesh
            .as_ref()
            .expect("successful BVP_api solve should publish its mesh");
        assert_eq!(mesh.len(), intervals + 1);
        assert_eq!(mesh[0], 0.0);
        assert_eq!(mesh[intervals], 1.0);
        let Some(RustedSciThe::numerical::BVP_api::BvpSolverTelemetrySnapshot::Damped(telemetry)) =
            solver.last_telemetry.as_ref()
        else {
            panic!("expected the route-tagged Damped telemetry snapshot");
        };
        assert_eq!(telemetry.telemetry_mode, BvpTelemetryMode::Counters);
        assert!(telemetry.counters.residual_calls > 0);

        let previous_solution = solution.clone();
        let previous_mesh = mesh.clone();
        let invalid_retry = NrbvpHandoffConfig::new(
            DMatrix::from_element(state_count, 1, 0.5),
            0.0,
            1.0,
            1,
            "forward".to_string(),
            "Damped".to_string(),
            None,
            None,
            "Banded".to_string(),
            1e-8,
            None,
            20,
            None,
            None,
        );
        assert!(solver.solve_with_bvp_api(invalid_retry).is_err());
        assert_eq!(solver.get_solution(), Some(&previous_solution));
        assert_eq!(solver.x_mesh.as_ref(), Some(&previous_mesh));
        assert!(solver.last_telemetry.is_none());
    }

    #[test]
    fn story_scalar_sci_bvp_uses_shared_api_and_publishes_adaptive_mesh() {
        let mut solver = super::super::SimpleReactorBVP::BVPSolver::default()
            .with_bvp_telemetry_mode(
                RustedSciThe::numerical::BVP_api::BvpApiTelemetryMode::Counters,
            );
        solver.arg_name = "x".to_string();
        solver.x_range = (0.0, 1.0);
        solver.unknowns = vec!["y".to_string()];
        solver.eq_system = vec![Expr::parse_expression("-y")];
        solver.BorderConditions.insert("y".to_string(), (0, 1.0));
        solver
            .solve_BVPsci(DMatrix::from_element(1, 16, 0.8), 16, 128, 1e-5)
            .expect("scalar collocation BVP should solve through BVP_api");

        let solution = solver
            .get_solution()
            .expect("successful BVP_sci route should publish a solution");
        let mesh = solver
            .x_mesh
            .as_ref()
            .expect("successful BVP_sci route should publish its adaptive mesh");
        assert_eq!(solution.nrows(), mesh.len());
        assert_eq!(solution.ncols(), 1);
        assert!(mesh.len() >= 16);
        assert!((mesh[0] - 0.0).abs() < 1e-14);
        assert!((mesh[mesh.len() - 1] - 1.0).abs() < 1e-14);
        assert!(solution.iter().all(|value| value.is_finite()));
        assert!((solution[(solution.nrows() - 1, 0)] - (-1.0f64).exp()).abs() < 2e-3);
        let Some(RustedSciThe::numerical::BVP_api::BvpSolverTelemetrySnapshot::Sci(snapshot)) =
            solver.last_telemetry.as_ref()
        else {
            panic!("expected the route-tagged Sci telemetry snapshot");
        };
        assert_eq!(
            snapshot.mode,
            RustedSciThe::numerical::BVP_sci::new::BvpSciTelemetryMode::Counters
        );
        assert!(snapshot.rhs_calls > 0);

        solver = solver
            .with_bvp_telemetry_mode(RustedSciThe::numerical::BVP_api::BvpApiTelemetryMode::Off);
        solver
            .solve_BVPsci(DMatrix::from_element(1, 16, 0.8), 16, 128, 1e-5)
            .expect("explicit Off mode should preserve the Sci solve");
        let Some(RustedSciThe::numerical::BVP_api::BvpSolverTelemetrySnapshot::Sci(snapshot)) =
            solver.last_telemetry.as_ref()
        else {
            panic!("expected the route-tagged Sci telemetry snapshot");
        };
        assert_eq!(
            snapshot.mode,
            RustedSciThe::numerical::BVP_sci::new::BvpSciTelemetryMode::Off
        );
        assert_eq!(snapshot.rhs_calls, 0);
        assert_eq!(snapshot.jacobian_calls, 0);
    }

    #[test]
    fn story_scalar_frozen_bvp_publishes_boundary_aligned_profile() {
        let mut solver = super::super::SimpleReactorBVP::BVPSolver::default()
            .with_bvp_telemetry_mode(
                RustedSciThe::numerical::BVP_api::BvpApiTelemetryMode::Counters,
            );
        solver.arg_name = "x".to_string();
        solver.x_range = (0.0, 1.0);
        solver.unknowns = vec!["y".to_string()];
        solver.eq_system = vec![Expr::parse_expression("-y")];
        solver.BorderConditions.insert("y".to_string(), (0, 1.0));
        let nodes = 257;
        let options = FrozenSolverOptions::default().with_tolerance(1e-6);

        solver
            .solve_BVPfrozen(DMatrix::from_element(1, nodes, 1.0), options)
            .expect("Frozen should publish the reconstructed full nodal profile");
        let profile = solver
            .get_solution()
            .expect("accepted Frozen profile should be available");
        let mesh = solver
            .x_mesh
            .as_ref()
            .expect("accepted Frozen mesh should be available");
        assert_eq!(profile.shape(), (nodes, 1));
        assert_eq!(mesh.len(), nodes);
        assert_eq!(mesh[0], 0.0);
        assert_eq!(mesh[nodes - 1], 1.0);
        assert_eq!(profile[(0, 0)], 1.0);
        assert!(profile.iter().all(|value| value.is_finite()));
        let Some(RustedSciThe::numerical::BVP_api::BvpSolverTelemetrySnapshot::Frozen(snapshot)) =
            solver.last_telemetry.as_ref()
        else {
            panic!("accepted Frozen attempt should retain route telemetry");
        };
        assert_eq!(
            snapshot.telemetry_mode,
            RustedSciThe::numerical::BVP_Damp::telemetry::BvpTelemetryMode::Counters
        );
        assert!(snapshot.counters.iterations > 0);
        assert_eq!(
            snapshot.last_solve_status,
            RustedSciThe::numerical::BVP_Damp::telemetry::BvpSolveStatus::Converged
        );
    }

    #[test]
    fn frozen_and_naive_routes_match_across_frontends_and_matrix_layouts() {
        let nodes = 33;
        let mut reference: Option<Vec<f64>> = None;

        for (strategy, naive) in [("Frozen", false), ("Naive", true)] {
            for frontend in [
                BvpSymbolicAssemblyBackend::ExprLegacy,
                BvpSymbolicAssemblyBackend::AtomView,
            ] {
                for (layout, options) in [
                    ("Dense", FrozenSolverOptions::dense_frozen()),
                    ("Sparse", FrozenSolverOptions::sparse_frozen()),
                    (
                        "Banded",
                        FrozenSolverOptions::banded_frozen().with_banded_lambdify(),
                    ),
                ] {
                    let mut options = options
                        .with_tolerance(1e-8)
                        .with_max_iterations(500)
                        .with_bvp_telemetry_mode(BvpTelemetryMode::Counters)
                        .with_symbolic_assembly_backend(frontend);
                    if naive {
                        options.strategy = "Naive".to_string();
                        options.strategy_params = None;
                    }
                    options = options.with_matrix_backend(match layout {
                        "Dense" => MatrixBackend::Dense,
                        "Sparse" => MatrixBackend::SparseCol,
                        "Banded" => MatrixBackend::Banded,
                        _ => unreachable!(),
                    });
                    let mut solver = super::super::SimpleReactorBVP::BVPSolver::default();
                    solver.arg_name = "x".to_string();
                    solver.x_range = (0.0, 1.0);
                    solver.unknowns = vec!["y".to_string()];
                    solver.eq_system = vec![Expr::parse_expression("-0.7 * y")];
                    solver.BorderConditions.insert("y".to_string(), (0, 1.0));
                    solver = solver.with_bvp_telemetry_mode(
                        RustedSciThe::numerical::BVP_api::BvpApiTelemetryMode::Counters,
                    );
                    solver
                        .solve_BVPfrozen(DMatrix::from_element(1, nodes, 0.8), options)
                        .unwrap_or_else(|error| {
                            panic!("{strategy}/{frontend:?}/{layout} solve: {error}")
                        });
                    let published = solver
                        .get_solution()
                        .expect("accepted facade route should publish a profile");
                    let published_mesh = solver
                        .x_mesh
                        .as_ref()
                        .expect("accepted facade route should publish its mesh");
                    assert_eq!(published.shape(), (nodes, 1));
                    assert_eq!(published_mesh.len(), nodes);
                    let profile = published_mesh
                        .iter()
                        .enumerate()
                        .map(|(node, x)| {
                            let actual = published[(node, 0)];
                            assert!(actual.is_finite() && (0.0..=1.0).contains(&actual));
                            let error = (actual - (-0.7 * x).exp()).abs();
                            assert!(
                                error <= 4e-2,
                                "{strategy}/{frontend:?}/{layout} at x={x}: error={error:e}"
                            );
                            actual
                        })
                        .collect::<Vec<_>>();
                    if let Some(expected) = &reference {
                        let max_delta = profile
                            .iter()
                            .zip(expected)
                            .map(|(actual, expected)| (actual - expected).abs())
                            .fold(0.0_f64, f64::max);
                        println!(
                            "BVP {strategy}/{frontend:?}/{layout} max_abs_profile_delta={max_delta:.6e}"
                        );
                        assert!(max_delta <= 4e-2, "cross-route profile drift {max_delta:e}");
                    } else {
                        reference = Some(profile);
                    }
                    assert!(matches!(
                        solver.last_telemetry,
                        Some(
                            RustedSciThe::numerical::BVP_api::BvpSolverTelemetrySnapshot::Frozen(_)
                        )
                    ));
                }
            }
        }
    }

    #[test]
    fn story_damped_api_preserves_rst_configuration_error_source() {
        let mut solver = super::super::SimpleReactorBVP::BVPSolver::default();
        solver.unknowns = vec!["y".to_string()];
        solver.eq_system = vec![Expr::parse_expression("-y")];
        solver.BorderConditions.insert("y".to_string(), (0, 1.0));
        let config = NrbvpHandoffConfig::new(
            DMatrix::from_element(1, 2, 0.5),
            0.0,
            1.0,
            2,
            "forward".to_string(),
            "Damped".to_string(),
            None,
            None,
            "Banded".to_string(),
            1e-8,
            None,
            20,
            None,
            None,
        );

        let error = solver
            .solve_with_bvp_api(config)
            .expect_err("RST should reject missing Damped bounds and tolerances");
        assert!(matches!(error, ReactorError::BvpApi(_)));
        let source =
            Error::source(&error).expect("native BVP error should remain in the source chain");
        assert!(source.to_string().contains("Damped"));
        assert!(solver.get_solution().is_none());
    }
}