import streamlit as st import pandas as pd import os import sys from models.rlc import create_model, run_simulation, generate_graph_image # Set page layout to wide st.set_page_config(layout="wide") def main(): st.title("🔋 Switched RLC Circuit Simulator") with open("./markdowns/rlc.md") as file: introduction = file.read() st.markdown(introduction) if "mod_graph" not in st.session_state: parameters = { "R": 100, # Resistance in Ohms "L": 1, # Inductance in Henrys "C": 1e-3, # Capacitance in Farads "Vb": 0.1 # Battery Voltage in Volts } mod, mod_graph = create_model(parameters) st.session_state.mod_graph = mod_graph st.session_state.mod = mod # Input parameters col1, col2, col3 = st.columns(3) with col1: R = st.number_input("Resistance (Ohms)", value=200) L = st.number_input("Inductance (Henrys)", value=1) with col2: C = st.number_input("Capacitance (Farads)", value=0.001, format="%.5f") Vb = st.number_input("Battery Voltage (Volts)", value=0.1) with col3: duration = st.number_input("Simulation Duration (s)", value=2.0) dt = st.number_input("Time Step (s)", value=0.001) # Update model parameters mod_graph = st.session_state.mod_graph node = mod_graph.nodes[0] node.parameters[0].conditions[1].value = Vb node.parameters[1].value = R node.parameters[2].value = L node.parameters[3].value = C # Run simulation times, i_L_values, i_R_values, i_C_values, V_values = run_simulation(mod_graph, duration, dt) chart_data = pd.DataFrame({ "Inductor Current (i_L)": i_L_values, "Resistor Current (i_R)": i_R_values, "Capacitor Current (i_C)": i_C_values, "Response Voltage (V)": V_values }) # Layout for charts and image col1, col2 = st.columns(2) with col1: st.subheader("Currents in RLC Circuit") st.line_chart(chart_data[["Inductor Current (i_L)", "Resistor Current (i_R)", "Capacitor Current (i_C)"]], use_container_width=True, height=400) with col2: st.subheader("Voltage in RLC Circuit") st.line_chart(chart_data[["Response Voltage (V)"]], use_container_width=True, height=400) st.subheader("Model Graph Visualization") image_path = generate_graph_image(st.session_state.mod) st.image(image_path, caption="Model Graph Visualization") st.success('Graph generated successfully.') if __name__ == "__main__": main()