KiThe 0.3.9

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
use crate::gui::all_libs_gui;
use crate::gui::combustion;

use crate::gui::equilibrium_gui;
use crate::gui::experimental_kinetics_gui::experimental_kinetics_gui_main;
use crate::gui::gui_main::egui::IconData;
use crate::gui::gui_solid_ivp;
use crate::gui::kinetics_gui;
use crate::gui::reactor_ivp_gui;
use crate::gui::settings_gui;
use crate::gui::thermochemistry_gui;
use crate::gui::transport_gui;
use eframe::CreationContext;
use eframe::egui;
use egui::ColorImage;
use egui::TextureHandle;
pub fn gui_main() -> Result<(), eframe::Error> {
    // Try to load icon from assets, fallback to programmatic icon
    let icon = match std::fs::read("src/assets/icon.png") {
        Ok(icon_bytes) => match eframe::icon_data::from_png_bytes(&icon_bytes) {
            Ok(icon_data) => icon_data,
            Err(_) => create_programmatic_icon(),
        },
        Err(_) => create_programmatic_icon(),
    };

    let options = eframe::NativeOptions {
        viewport: egui::ViewportBuilder::default()
            .with_inner_size([800.0, 600.0])
            .with_title("Main menu ")
            .with_icon(icon),
        ..Default::default()
    };
    /*
    eframe::run_native(
        "Chemical Analysis Suite",
        options,
        Box::new(|cc| Ok(Box::new(MainApp::new(cc)))),
    )
    */
    eframe::run_native(
        "Chemical Analysis Suite",
        options,
        Box::new(|cc: &eframe::CreationContext<'_>| Ok(Box::new(MainApp::new(cc)))),
    )
}

fn create_programmatic_icon() -> IconData {
    let size = 32;
    let mut rgba = vec![0u8; size * size * 4];
    for y in 0..size {
        for x in 0..size {
            let idx = (y * size + x) * 4;
            let center_x = size as f32 / 2.0;
            let center_y = size as f32 / 2.0;
            let dist = ((x as f32 - center_x).powi(2) + (y as f32 - center_y).powi(2)).sqrt();
            if dist < 12.0 {
                rgba[idx] = 255;
                rgba[idx + 1] = (200.0 * (1.0 - dist / 12.0)) as u8;
                rgba[idx + 2] = 0;
                rgba[idx + 3] = 255;
            }
        }
    }
    IconData {
        rgba,
        width: 32,
        height: 32,
    }
}

#[derive(Default)]
struct MainApp {
    kinetics_open: bool,
    kinetics_app: Option<kinetics_gui::KineticsApp>,
    combustion_open: bool,
    combustion_app: Option<combustion::CombustionApp>,
    thermochemistry_open: bool,
    thermochemistry_app: Option<thermochemistry_gui::ThermochemistryApp>,
    transport_open: bool,
    transport_app: Option<transport_gui::TransportApp>,
    settings_open: bool,
    settings_app: Option<settings_gui::SettingsGui>,
    all_libs_open: bool,
    all_libs_app: Option<all_libs_gui::AllLibsGui>,
    reactor_ivp_open: bool,
    reactor_ivp_app: Option<reactor_ivp_gui::ReactorIvpApp>,
    solid_ivp_open: bool,
    solid_ivp_app: Option<gui_solid_ivp::SolidIVPApp>,
    experimental_kinetics_open: bool,
    experimental_kinetics_app: Option<experimental_kinetics_gui_main::PlotApp>,
    equilibrium_open: bool,
    equilibrium_app: Option<equilibrium_gui::EquilibriumApp>,
    logo_texture: Option<egui::TextureHandle>,
}
impl MainApp {
    pub fn new(cc: &CreationContext<'_>) -> Self {
        let ctx = &cc.egui_ctx;

        Self::setup_visuals(ctx);
        Self::setup_fonts(ctx);
        let logo_texture = Self::load_logo_texture(ctx);

        Self {
            kinetics_open: false,
            kinetics_app: None,
            combustion_open: false,
            combustion_app: None,
            thermochemistry_open: false,
            thermochemistry_app: None,
            transport_open: false,
            transport_app: None,
            settings_open: false,
            settings_app: None,
            all_libs_open: false,
            all_libs_app: None,
            reactor_ivp_open: false,
            reactor_ivp_app: None,
            solid_ivp_open: false,
            solid_ivp_app: None,
            experimental_kinetics_open: false,
            experimental_kinetics_app: None,
            equilibrium_open: false,
            equilibrium_app: None,
            logo_texture,
        }
    }

    /// Opens the equilibrium window without sharing its document or runtime
    /// state with any other application owned by the main menu.
    fn open_equilibrium(&mut self) {
        self.equilibrium_open = true;
        if self.equilibrium_app.is_none() {
            self.equilibrium_app = Some(equilibrium_gui::EquilibriumApp::new());
        }
    }

    fn setup_visuals(ctx: &egui::Context) {
        use egui::Visuals;
        ctx.set_visuals(Visuals::dark()); // or .light()
    }

    fn setup_fonts(ctx: &egui::Context) {
        use egui::FontDefinitions;
        let fonts = FontDefinitions::default();
        // You can load custom fonts here if desired.
        ctx.set_fonts(fonts);
    }

    fn load_logo_texture(ctx: &egui::Context) -> Option<TextureHandle> {
        if let Ok(logo_bytes) = std::fs::read("src/assets/logo.png") {
            if let Ok(color_image) = eframe::icon_data::from_png_bytes(&logo_bytes) {
                let egui_image = ColorImage::from_rgba_unmultiplied(
                    [color_image.width as usize, color_image.height as usize],
                    &color_image.rgba,
                );
                return Some(ctx.load_texture(
                    "kithe_logo",
                    egui_image,
                    egui::TextureOptions::default(),
                ));
            }
        }
        None
    }
}

#[cfg(test)]
mod tests {
    use super::MainApp;
    use crate::gui::equilibrium_gui_model::{EquilibriumProblemDraft, TemperatureDraft};
    use egui::accesskit::Role;
    use egui_kittest::Harness;
    use egui_kittest::kittest::Queryable;
    use std::cell::RefCell;
    use std::rc::Rc;

    #[test]
    fn equilibrium_menu_owns_one_persistent_document() {
        let mut main = MainApp::default();
        main.open_equilibrium();
        assert!(main.equilibrium_open);
        let equilibrium = main
            .equilibrium_app
            .as_mut()
            .expect("opening the menu creates the equilibrium app");
        equilibrium.document.config.problem = EquilibriumProblemDraft::FixedPt {
            pressure_pa: "90000".into(),
            reference_pressure_pa: "101325".into(),
            temperature: TemperatureDraft::Point {
                temperature_k: "777".into(),
            },
        };
        let expected = equilibrium.document.clone();

        main.equilibrium_open = false;
        main.open_equilibrium();
        assert_eq!(
            main.equilibrium_app
                .as_ref()
                .expect("reopen keeps the app instance")
                .document,
            expected
        );
        assert!(main.kinetics_app.is_none());
        assert!(main.thermochemistry_app.is_none());
        assert!(main.transport_app.is_none());
    }

    #[test]
    fn equilibrium_menu_opens_a_real_child_window_through_egui() {
        let main = Rc::new(RefCell::new(MainApp::default()));
        let main_for_ui = Rc::clone(&main);
        let mut harness = Harness::new_ui(move |ui| {
            main_for_ui.borrow_mut().render(ui);
        });

        harness.run();
        harness
            .get_by_role_and_label(Role::Button, "Chemical equilibrium")
            .click_accesskit();
        harness.run();

        harness.get_by_role_and_label(Role::Window, "Chemical equilibrium");
        assert!(main.borrow().equilibrium_open);
        assert!(main.borrow().equilibrium_app.is_some());
    }
}

impl MainApp {
    /// Renders the main menu and owned child windows without requiring an
    /// `eframe::Frame`. Keeping this boundary separate makes native menu
    /// ownership testable with the same egui harness used by child GUIs.
    fn render(&mut self, ui: &mut egui::Ui) {
        // Set button text color and size to be more visible.
        {
            let style = ui.style_mut();
            style.visuals.widgets.inactive.fg_stroke.color = egui::Color32::BLACK;
            style.visuals.widgets.hovered.fg_stroke.color = egui::Color32::BLACK;
            style.visuals.widgets.active.fg_stroke.color = egui::Color32::WHITE;
            style.text_styles.insert(
                egui::TextStyle::Button,
                egui::FontId::new(16.0, egui::FontFamily::Proportional),
            );
        }

        let ctx = ui.ctx().clone();
        egui::CentralPanel::default().show(ui, |ui| {
            ui.vertical_centered(|ui| {
                ui.add_space(50.0);
                // Main title
                ui.heading("Heat and mass transfer, chemical reactors \ncombustion and macrokinetics suite");
                ui.add_space(30.0);
                // Menu buttons in a grid layout
                ui.horizontal(|ui| {
                    ui.add_space(50.0);
                    ui.vertical(|ui| {
                        // First row of buttons
                        ui.horizontal(|ui| {
                            if ui.add_sized([200.0, 80.0], egui::Button::new("🧪 Kinetics!")).clicked() {
                                self.kinetics_open = true;
                                if self.kinetics_app.is_none() {
                                    self.kinetics_app = Some(kinetics_gui::KineticsApp::new());
                                }
                            }
                            ui.add_space(20.0);
                            if ui.add_sized([200.0, 80.0], egui::Button::new("🌡️ Thermodynamical properties!")).clicked() {
                                self.thermochemistry_open = true;
                                if self.thermochemistry_app.is_none() {
                                    self.thermochemistry_app = Some(thermochemistry_gui::ThermochemistryApp::new());
                                }
                            }
                        });

                        ui.add_space(20.0);
                        ui.horizontal(|ui| {
                            if ui.add_sized([200.0, 60.0], egui::Button::new("Chemical equilibrium")).clicked() {
                                self.open_equilibrium();
                            }
                        });
                        ui.add_space(20.0);
                        // Second row of buttons
                        ui.horizontal(|ui| {
                            if ui.add_sized([200.0, 80.0], egui::Button::new("🚚 Transport properties!")).clicked() {
                                self.transport_open = true;
                                if self.transport_app.is_none() {
                                    self.transport_app = Some(transport_gui::TransportApp::new());
                                }
                            }
                            ui.add_space(20.0);
                            if ui.add_sized([200.0, 80.0], egui::Button::new("🔥 Gas-phase combustuion/steady state plug flow")).clicked() {
                                self.combustion_open = true;
                                if self.combustion_app.is_none() {
                                    self.combustion_app = Some(combustion::CombustionApp::new());
                                }
                            }
                        });
                        ui.add_space(20.0);
                        // Third row of buttons
                        ui.horizontal(|ui| {
                            if ui.add_sized([200.0, 80.0], egui::Button::new("📚 Library Review")).clicked() {
                                self.all_libs_open = true;
                                if self.all_libs_app.is_none() {
                                    self.all_libs_app = Some(all_libs_gui::AllLibsGui::new());
                                }
                            }
                            ui.add_space(20.0);
                            if ui.add_sized([200.0, 80.0], egui::Button::new("⚙️ Settings")).clicked() {
                                self.settings_open = true;
                                if self.settings_app.is_none() {
                                    self.settings_app = Some(settings_gui::SettingsGui::new());
                                }
                            }
                        });

                        // Fourth row of buttons
                        ui.add_space(20.0);
                        ui.horizontal(|ui| {
                            if ui.add_sized([200.0, 80.0], egui::Button::new("Condensed reactor IVP")).clicked() {
                                self.reactor_ivp_open = true;
                                if self.reactor_ivp_app.is_none() {
                                    self.reactor_ivp_app = Some(reactor_ivp_gui::ReactorIvpApp::new());
                                }
                            }
                            ui.add_space(20.0);
                            if ui.add_sized([200.0, 80.0], egui::Button::new("Solid-state kinetics")).clicked() {
                                self.solid_ivp_open = true;
                                if self.solid_ivp_app.is_none() {
                                    self.solid_ivp_app = Some(gui_solid_ivp::SolidIVPApp::new());
                                }
                            }
                            ui.add_space(20.0);
                            if ui.add_sized([200.0, 80.0], egui::Button::new("📊 Experimental Kinetics")).clicked() {
                                self.experimental_kinetics_open = true;
                                if self.experimental_kinetics_app.is_none() {
                                    self.experimental_kinetics_app = Some(experimental_kinetics_gui_main::PlotApp::new());
                                }
                            }
                        });
                    });
                });
                ui.add_space(50.0);
                // Footer information
                ui.separator();
                ui.add_space(20.0);
                // Load and display logo (cached)
                if self.logo_texture.is_none() {
                    if let Ok(logo_bytes) = std::fs::read("src/assets/logo.png") {
                        if let Ok(color_image) = eframe::icon_data::from_png_bytes(&logo_bytes) {
                            let egui_image = egui::ColorImage::from_rgba_unmultiplied(
                                [color_image.width as usize, color_image.height as usize],
                                &color_image.rgba
                            );
                            self.logo_texture = Some(ctx.load_texture("logo", egui_image, egui::TextureOptions::default()));
                        }
                    }
                }
                if let Some(texture) = &self.logo_texture {
                    ui.add(egui::Image::new(texture).max_width(200.0));
                    ui.add_space(10.0);
                } else {
                    ui.label("Logo not found");
                }
                ui.label("developed by Gleb E. Zaslavsky 2024-2026 (c)");
            }) ;
        });

        // Show kinetics window if opened
        if self.kinetics_open {
            if let Some(kinetics_app) = &mut self.kinetics_app {
                kinetics_app.show(&ctx, &mut self.kinetics_open);
            }
        }

        // Show combustion window if opened
        if self.combustion_open {
            if let Some(combustion_app) = &mut self.combustion_app {
                combustion_app.show(&ctx, &mut self.combustion_open);
            }
        }

        // Show thermochemistry window if opened
        if self.thermochemistry_open {
            if let Some(thermochemistry_app) = &mut self.thermochemistry_app {
                thermochemistry_app.show(&ctx, &mut self.thermochemistry_open);
            }
        }

        // Show transport window if opened
        if self.transport_open {
            if let Some(transport_app) = &mut self.transport_app {
                transport_app.show(&ctx, &mut self.transport_open);
            }
        }

        // Show settings window if opened
        if self.settings_open {
            if let Some(settings_app) = &mut self.settings_app {
                settings_app.show(&ctx, &mut self.settings_open);
            }
        }

        // Show all libs window if opened
        if self.all_libs_open {
            if let Some(all_libs_app) = &mut self.all_libs_app {
                all_libs_app.show(&ctx, &mut self.all_libs_open);
            }
        }

        if self.solid_ivp_open {
            if let Some(solid_ivp_app) = &mut self.solid_ivp_app {
                solid_ivp_app.show(&ctx, &mut self.solid_ivp_open);
            }
        }

        if self.reactor_ivp_open {
            if let Some(reactor_ivp_app) = &mut self.reactor_ivp_app {
                reactor_ivp_app.show(&ctx, &mut self.reactor_ivp_open);
            }
        }

        if self.experimental_kinetics_open {
            if let Some(experimental_kinetics_app) = &mut self.experimental_kinetics_app {
                experimental_kinetics_app.show(&ctx, &mut self.experimental_kinetics_open);
            }
        }

        if self.equilibrium_open {
            if let Some(equilibrium_app) = &mut self.equilibrium_app {
                equilibrium_app.show(&ctx, &mut self.equilibrium_open);
            }
        }
    }
}

impl eframe::App for MainApp {
    fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
        self.render(ui);
    }
}