KiThe 0.3.5

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
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
#[cfg(test)]
mod tests {
    use super::super::combustion::{CombustionApp, ProblemsEnum};
    use crate::ReactorsBVP::task_parser_reactor_BVP::normalize_reactor_physics_task_map;
    use egui_kittest::kittest::Queryable;
    use egui_kittest::Harness;
    use RustedSciThe::command_interpreter::task_parser::{DocumentMap, DocumentParser, Value};
    use RustedSciThe::numerical::BVP_Damp::task_parser_damped;
    use std::cell::RefCell;
    use std::rc::Rc;
    use tempfile::tempdir;

    fn first_value<'a>(document: &'a DocumentMap, section: &str, field: &str) -> &'a Value {
        document
            .get(section)
            .and_then(|section_map| section_map.get(field))
            .and_then(|slot| slot.as_ref())
            .and_then(|values| values.first())
            .expect("expected BVP solver field to exist")
    }

    fn as_string(value: &Value) -> &str {
        match value {
            Value::String(value) => value,
            other => panic!("expected Value::String, got {other:?}"),
        }
    }

    fn as_bool(value: &Value) -> bool {
        match value {
            Value::Boolean(value) => *value,
            other => panic!("expected Value::Boolean, got {other:?}"),
        }
    }

    fn as_usize(value: &Value) -> usize {
        match value {
            Value::Usize(value) => *value,
            other => panic!("expected Value::Usize, got {other:?}"),
        }
    }

    fn as_vector(value: &Value) -> &[f64] {
        match value {
            Value::Vector(value) => value,
            other => panic!("expected Value::Vector, got {other:?}"),
        }
    }

    fn load_problem_test_document() -> DocumentMap {
        let content = std::fs::read_to_string("problem_test.txt")
            .expect("problem_test.txt must be readable from the crate root");
        let mut parser = DocumentParser::new(content);
        parser
            .parse_document()
            .expect("problem_test.txt must parse as a valid BVP document");
        parser
            .get_result()
            .cloned()
            .expect("parsed problem_test.txt must yield a document map")
    }

    #[test]
    fn bvp_solver_backend_defaults_are_seeded() {
        let app = CombustionApp::new_with_problem(ProblemsEnum::BVPSimple);
        let solver_settings = app
            .document
            .get("solver_settings")
            .expect("solver_settings section must be seeded for the BVP template");

        assert_eq!(as_string(first_value(&app.document, "solver_settings", "scheme")), "forward");
        assert_eq!(as_string(first_value(&app.document, "solver_settings", "method")), "Banded");
        assert_eq!(
            as_string(first_value(
                &app.document,
                "solver_settings",
                "generated_backend"
            )),
            "banded_lambdify"
        );
        assert_eq!(
            as_string(first_value(&app.document, "solver_settings", "matrix_backend")),
            "Banded"
        );
        assert_eq!(
            as_usize(first_value(
                &app.document,
                "solver_settings",
                "refinement_steps"
            )),
            5
        );
        assert_eq!(
            as_string(first_value(&app.document, "solver_settings", "symbolic_backend")),
            "AtomView"
        );
        assert!(matches!(
            first_value(&app.document, "solver_settings", "linear_sys_method"),
            Value::Optional(None)
        ));
        assert!(as_bool(first_value(
            &app.document,
            "solver_settings",
            "dont_save_log"
        )));
        assert_eq!(
            as_string(first_value(
                &app.document,
                "solver_settings",
                "backend_policy"
            )),
            "lambdify_only"
        );
        assert!(!solver_settings.contains_key("aot_c_compiler"));
        assert!(!solver_settings.contains_key("aot_build_policy"));
        assert!(!solver_settings.contains_key("aot_execution_policy"));
    }

    #[test]
    fn bvp_solver_backend_panel_renders_new_controls() {
        let mut app = CombustionApp::new_with_problem(ProblemsEnum::BVPSimple);
        let mut open = true;
        let mut harness = Harness::new_ui(move |ui| {
            app.show(ui.ctx(), &mut open);
        });

        harness.run();

        for label in [
            "Solver backend",
            "Core solve",
            "Execution and assembly",
            "Residuals and Jacobian Backend",
            "scheme",
            "Linear algebra backend",
            "Linear system method override",
            "Symbolic backend",
            "Banded linear solver",
            "refinement_steps",
        ] {
            harness.get_by_label(label);
        }

    }

    #[test]
    fn bvp_solver_backend_keeps_linear_system_override_typed() {
        let app = Rc::new(RefCell::new(CombustionApp::new_with_problem(
            ProblemsEnum::BVPSimple,
        )));
        {
            let mut app_ref = app.borrow_mut();
            let solver_settings = app_ref
                .document
                .get_mut("solver_settings")
                .expect("solver_settings section must exist in the BVP template");
            solver_settings.insert(
                "linear_sys_method".to_string(),
                Some(vec![Value::Optional(Some(Box::new(Value::Float(0.0))))]),
            );
        }

        let mut open = true;
        let app_for_ui = Rc::clone(&app);
        let mut harness = Harness::new_ui(move |ui| {
            app_for_ui.borrow_mut().show(ui.ctx(), &mut open);
        });

        harness.run();
        harness.get_by_label("Linear system method override");

        let app_ref = app.borrow();
        assert!(matches!(
            first_value(&app_ref.document, "solver_settings", "linear_sys_method"),
            Value::Optional(None)
        ));
    }

    #[test]
    fn bvp_screen_renders_structured_sections() {
        let mut app = CombustionApp::new_with_problem(ProblemsEnum::BVPSimple);
        let mut open = true;
        let mut harness = Harness::new_ui(move |ui| {
            app.show(ui.ctx(), &mut open);
        });

        harness.run();

        for label in [
            "Physics",
            "Initial guess",
            "Postprocessing",
            "Legacy raw document",
            "process_conditions",
            "boundary_condition",
            "initial_guess",
            "postprocessing",
        ] {
            harness.get_by_label(label);
        }
    }

    #[test]
    fn bvp_screen_renders_advanced_solver_sections_separately_from_physics() {
        let mut app = CombustionApp::new_with_problem(ProblemsEnum::BVPSimple);
        let mut open = true;
        let mut harness = Harness::new_ui(move |ui| {
            app.show(ui.ctx(), &mut open);
        });

        harness.run();

        for label in [
            "Advanced solver settings",
            "rel_tolerance",
            "strategy_params",
            "Adaptive grid refinement",
            "Adaptive strategy version: 1",
            "Grid refinement strategy",
        ] {
            harness.get_by_label(label);
        }
    }

    #[test]
    fn bvp_adaptive_checkbox_builds_complete_native_rst_contract() {
        let app = Rc::new(RefCell::new(CombustionApp::new_with_problem(
            ProblemsEnum::BVPSimple,
        )));
        app.borrow_mut().document = load_problem_test_document();

        let mut open = true;
        let app_for_ui = Rc::clone(&app);
        let mut harness = Harness::new_ui(move |ui| {
            app_for_ui.borrow_mut().show(ui.ctx(), &mut open);
        });

        harness.run();
        assert!(!app.borrow().document.contains_key("adaptive_strategy"));
        assert!(!app.borrow().document.contains_key("grid_refinement"));

        harness.get_by_label("Adaptive grid refinement").click();
        harness.run();

        let app_ref = app.borrow();
        assert_eq!(
            as_usize(first_value(
                &app_ref.document,
                "adaptive_strategy",
                "version"
            )),
            1
        );
        assert_eq!(
            as_usize(first_value(
                &app_ref.document,
                "adaptive_strategy",
                "max_refinements"
            )),
            3
        );
        assert!(app_ref
            .document
            .get("grid_refinement")
            .is_some_and(|section| section.len() == 1));
        assert!(!app_ref
            .document
            .get("strategy_params")
            .is_some_and(|section| section.contains_key("adaptive")));

        let solver_document = normalize_reactor_physics_task_map(&app_ref.document);
        task_parser_damped::parse_bvp_damped_solver_settings_from_document(&solver_document)
            .expect("the GUI must emit a complete adaptive-grid contract accepted by RST");
    }

    #[test]
    fn bvp_matrix_backend_aliases_collapse_to_one_user_choice() {
        let app = Rc::new(RefCell::new(CombustionApp::new_with_problem(
            ProblemsEnum::BVPSimple,
        )));
        {
            let mut app_ref = app.borrow_mut();
            let solver_settings = app_ref
                .document
                .get_mut("solver_settings")
                .expect("solver_settings section must exist");
            solver_settings.insert(
                "method".to_string(),
                Some(vec![Value::String("Sparse".to_string())]),
            );
            solver_settings.insert(
                "matrix_backend".to_string(),
                Some(vec![Value::String("Banded".to_string())]),
            );
        }

        let mut open = true;
        let app_for_ui = Rc::clone(&app);
        let mut harness = Harness::new_ui(move |ui| {
            app_for_ui.borrow_mut().show(ui.ctx(), &mut open);
        });
        harness.run();
        harness.get_by_label("Linear algebra backend");

        let app_ref = app.borrow();
        assert_eq!(
            as_string(first_value(&app_ref.document, "solver_settings", "method")),
            "Sparse"
        );
        assert_eq!(
            as_string(first_value(
                &app_ref.document,
                "solver_settings",
                "matrix_backend"
            )),
            "Sparse"
        );
        assert_eq!(
            as_string(first_value(
                &app_ref.document,
                "solver_settings",
                "generated_backend"
            )),
            "sparse_lambdify"
        );
    }

    #[test]
    fn bvp_screen_preserves_custom_backend_values_and_seeds_missing_defaults() {
        let app = Rc::new(RefCell::new(CombustionApp::new_with_problem(
            ProblemsEnum::BVPSimple,
        )));
        {
            let mut app_ref = app.borrow_mut();
            let solver_settings = app_ref
                .document
                .get_mut("solver_settings")
                .expect("solver_settings section must exist in the BVP template");

            solver_settings.insert(
                "generated_backend".to_string(),
                Some(vec![Value::String("custom_backend".to_string())]),
            );
            solver_settings.remove("matrix_backend");
            solver_settings.remove("aot_build_policy");
        }

        let mut open = true;
        let app_for_ui = Rc::clone(&app);
        let mut harness = Harness::new_ui(move |ui| {
            app_for_ui.borrow_mut().show(ui.ctx(), &mut open);
        });

        harness.run();
        harness.get_by_label("Solver backend");

        let app_ref = app.borrow();
        assert_eq!(
            as_string(first_value(
                &app_ref.document,
                "solver_settings",
                "generated_backend"
            )),
            "custom_backend"
        );
        assert_eq!(
            as_string(first_value(
                &app_ref.document,
                "solver_settings",
                "matrix_backend"
            )),
            "Banded"
        );
        assert!(!app_ref
            .document
            .get("solver_settings")
            .is_some_and(|section| section.contains_key("aot_build_policy")));
    }

    #[test]
    fn bvp_screen_recreates_missing_structured_sections() {
        let app = Rc::new(RefCell::new(CombustionApp::new_with_problem(
            ProblemsEnum::BVPSimple,
        )));
        {
            let mut app_ref = app.borrow_mut();
            app_ref.document.remove("initial_guess");
            app_ref.document.remove("postprocessing");
        }

        let mut open = true;
        let app_for_ui = Rc::clone(&app);
        let mut harness = Harness::new_ui(move |ui| {
            app_for_ui.borrow_mut().show(ui.ctx(), &mut open);
        });

        harness.run();
        harness.get_by_label("Initial guess");
        harness.get_by_label("Postprocessing");

        let app_ref = app.borrow();
        assert!(app_ref.document.contains_key("initial_guess"));
        assert!(app_ref.document.contains_key("postprocessing"));
    }

    #[test]
    fn bvp_screen_canonicalizes_legacy_grid_refinement_aliases() {
        let app = Rc::new(RefCell::new(CombustionApp::new_with_problem(
            ProblemsEnum::BVPSimple,
        )));
        {
            let mut app_ref = app.borrow_mut();
            let grid_refinement = app_ref
                .document
                .get_mut("grid_refinement")
                .expect("grid_refinement section must exist in the BVP template");
            grid_refinement.remove("grcar_smooke");
            grid_refinement.insert(
                "grcarsmooke".to_string(),
                Some(vec![Value::Float(0.05), Value::Float(0.05), Value::Float(1.25)]),
            );
            grid_refinement.insert(
                "double_points".to_string(),
                Some(vec![Value::Vector(vec![])]),
            );
        }

        let mut open = true;
        let app_for_ui = Rc::clone(&app);
        let mut harness = Harness::new_ui(move |ui| {
            app_for_ui.borrow_mut().show(ui.ctx(), &mut open);
        });

        harness.run();
        harness.get_by_label("Physics");
        harness.get_by_label("grid_refinement");
        harness.get_by_label("Grid refinement strategy");

        let app_ref = app.borrow();
        let grid_refinement = app_ref
            .document
            .get("grid_refinement")
            .expect("grid_refinement section must still be present");
        assert_eq!(
            grid_refinement.len(),
            1,
            "the structured editor should keep one active refinement strategy"
        );
        assert!(grid_refinement.contains_key("grcarsmooke"));
        assert!(!grid_refinement.contains_key("grcar_smooke"));
        assert!(!grid_refinement.contains_key("double_points"));
        assert_eq!(
            as_vector(first_value(&app_ref.document, "grid_refinement", "grcarsmooke")),
            &[0.05, 0.05, 1.25]
        );
    }

    #[test]
    fn bvp_grid_refinement_defaults_to_single_dropdown_strategy() {
        let app = Rc::new(RefCell::new(CombustionApp::new_with_problem(
            ProblemsEnum::BVPSimple,
        )));
        let mut open = true;
        let app_for_ui = Rc::clone(&app);
        let mut harness = Harness::new_ui(move |ui| {
            app_for_ui.borrow_mut().show(ui.ctx(), &mut open);
        });

        harness.run();
        harness.get_by_label("Grid refinement strategy");

        let app_ref = app.borrow();
        let grid_refinement = app_ref
            .document
            .get("grid_refinement")
            .expect("grid_refinement section should exist");
        assert_eq!(
            grid_refinement.len(),
            1,
            "RST accepts one active grid refinement method per task"
        );
    }

    #[test]
    fn bvp_screen_renders_atomic_composition_as_structured_sections() {
        let mut app = CombustionApp::new_with_problem(ProblemsEnum::BVPSimple);
        app.document = load_problem_test_document();

        let mut open = true;
        let mut harness = Harness::new_ui(move |ui| {
            app.show(ui.ctx(), &mut open);
        });

        harness.run();
        harness.get_by_label("Atomic composition");
        harness.get_by_label("HMX");
        harness.get_by_label("HMXprod");
    }

    #[test]
    fn run_calculation_reports_validation_failure_instead_of_silence() {
        let mut app = CombustionApp::new_with_problem(ProblemsEnum::BVPSimple);
        app.document.remove("process_conditions");

        app.run_calculation();

        assert!(app.last_run_is_error);
        let message = app
            .last_run_message
            .as_deref()
            .expect("run_calculation should surface an error message");
        assert!(message.contains("Calculation failed"));
    }

    #[test]
    fn bvp_solver_backend_normalizes_refinement_steps_to_usize() {
        let app = Rc::new(RefCell::new(CombustionApp::new_with_problem(
            ProblemsEnum::BVPSimple,
        )));
        {
            let mut app_ref = app.borrow_mut();
            let solver_settings = app_ref
                .document
                .get_mut("solver_settings")
                .expect("solver_settings section must exist in the BVP template");
            solver_settings.insert(
                "refinement_steps".to_string(),
                Some(vec![Value::Float(3.0)]),
            );
        }

        let mut open = true;
        let app_for_ui = Rc::clone(&app);
        let mut harness = Harness::new_ui(move |ui| {
            app_for_ui.borrow_mut().show(ui.ctx(), &mut open);
        });

        harness.run();

        let app_ref = app.borrow();
        let value = first_value(&app_ref.document, "solver_settings", "refinement_steps");
        match value {
            Value::Usize(step) => assert_eq!(*step, 3),
            other => panic!("refinement_steps should be normalized to Value::Usize, got {other:?}"),
        }
    }

    #[test]
    fn bvp_solver_backend_normalizes_max_iterations_to_usize() {
        let app = Rc::new(RefCell::new(CombustionApp::new_with_problem(
            ProblemsEnum::BVPSimple,
        )));
        {
            let mut app_ref = app.borrow_mut();
            let solver_settings = app_ref
                .document
                .get_mut("solver_settings")
                .expect("solver_settings section must exist in the BVP template");
            solver_settings.insert(
                "max_iterations".to_string(),
                Some(vec![Value::Integer(77)]),
            );
        }

        let mut open = true;
        let app_for_ui = Rc::clone(&app);
        let mut harness = Harness::new_ui(move |ui| {
            app_for_ui.borrow_mut().show(ui.ctx(), &mut open);
        });

        harness.run();

        let app_ref = app.borrow();
        let value = first_value(&app_ref.document, "solver_settings", "max_iterations");
        match value {
            Value::Usize(step) => assert_eq!(*step, 77),
            other => panic!("max_iterations should be normalized to Value::Usize, got {other:?}"),
        }
    }

    #[test]
    fn bvp_solver_backend_normalizes_string_encoded_max_iterations_to_usize() {
        let app = Rc::new(RefCell::new(CombustionApp::new_with_problem(
            ProblemsEnum::BVPSimple,
        )));
        {
            let mut app_ref = app.borrow_mut();
            let solver_settings = app_ref
                .document
                .get_mut("solver_settings")
                .expect("solver_settings section must exist in the BVP template");
            solver_settings.insert(
                "max_iterations".to_string(),
                Some(vec![Value::String("88".to_string())]),
            );
        }

        let mut open = true;
        let app_for_ui = Rc::clone(&app);
        let mut harness = Harness::new_ui(move |ui| {
            app_for_ui.borrow_mut().show(ui.ctx(), &mut open);
        });

        harness.run();

        let app_ref = app.borrow();
        let value = first_value(&app_ref.document, "solver_settings", "max_iterations");
        match value {
            Value::Usize(step) => assert_eq!(*step, 88),
            other => panic!("max_iterations should accept string input and normalize to Value::Usize, got {other:?}"),
        }
    }

    #[test]
    fn bvp_document_save_read_save_roundtrip_is_stable() {
        let app = Rc::new(RefCell::new(CombustionApp::new_with_problem(
            ProblemsEnum::BVPSimple,
        )));
        {
            let mut app_ref = app.borrow_mut();
            let grid_refinement = app_ref
                .document
                .get_mut("grid_refinement")
                .expect("grid_refinement section should exist");
            grid_refinement.clear();
            grid_refinement.insert(
                "twopnt".to_string(),
                Some(vec![Value::Vector(vec![0.02, 0.03, 1.4])]),
            );
        }

        let mut open = true;
        let app_for_ui = Rc::clone(&app);
        let mut harness = Harness::new_ui(move |ui| {
            app_for_ui.borrow_mut().show(ui.ctx(), &mut open);
        });
        harness.run();
        harness.get_by_label("Grid refinement strategy");

        let temp_dir = tempdir().expect("temp dir should be available");
        let path = temp_dir.path().join("bvp_roundtrip.txt");
        app.borrow().save_document(path.clone());

        let saved = std::fs::read_to_string(&path).expect("saved BVP document should be readable");
        let mut parser = DocumentParser::new(saved.clone());
        parser
            .parse_document()
            .expect("saved BVP document should parse again");

        let mut reloaded = CombustionApp::new_with_problem(ProblemsEnum::BVPSimple);
        reloaded.document = parser
            .get_result()
            .cloned()
            .expect("roundtrip parser should return a document");

        assert_eq!(
            saved,
            reloaded.document_to_string(),
            "save -> read -> save should be a stable reflection of the same BVP task"
        );
    }
}