Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
import plotly.graph_objects as go
|
| 5 |
from scipy.signal import square, sawtooth
|
| 6 |
|
| 7 |
-
#
|
| 8 |
DIODE_DROPS = {"Silicon": 0.7, "Germanium": 0.3, "Ideal": 0.0}
|
| 9 |
COMPONENTS = {
|
| 10 |
"AC Source": {"symbol": "~", "orientation": True},
|
|
@@ -13,7 +14,7 @@ COMPONENTS = {
|
|
| 13 |
"Diode": {"symbol": "→", "orientation": True}
|
| 14 |
}
|
| 15 |
|
| 16 |
-
# Initialize session state
|
| 17 |
if "circuit" not in st.session_state:
|
| 18 |
st.session_state.circuit = []
|
| 19 |
if "connections" not in st.session_state:
|
|
@@ -90,8 +91,7 @@ def analyze_circuit():
|
|
| 90 |
voltage = source["value"] - diode_drop
|
| 91 |
current = voltage / total_resistance if total_resistance > 0 else 0
|
| 92 |
else:
|
| 93 |
-
# AC analysis
|
| 94 |
-
pass
|
| 95 |
|
| 96 |
return pd.DataFrame({
|
| 97 |
"Total Voltage": [voltage],
|
|
@@ -100,82 +100,103 @@ def analyze_circuit():
|
|
| 100 |
"Diode Drop": [diode_drop]
|
| 101 |
})
|
| 102 |
|
| 103 |
-
# Main
|
| 104 |
-
st.title("
|
| 105 |
-
st.markdown("Build your circuit by placing components and connecting them")
|
| 106 |
|
| 107 |
-
#
|
| 108 |
-
|
| 109 |
-
col1, col2, col3 = st.columns(3)
|
| 110 |
-
with col1:
|
| 111 |
-
comp_type = st.selectbox("Component Type", list(COMPONENTS.keys()))
|
| 112 |
-
with col2:
|
| 113 |
-
if comp_type in ["Resistor"]:
|
| 114 |
-
value = st.number_input("Value (Ω)", 1, 1000, 1000)
|
| 115 |
-
elif comp_type in ["DC Source"]:
|
| 116 |
-
value = st.number_input("Voltage (V)", 1.0, 24.0, 5.0)
|
| 117 |
-
else:
|
| 118 |
-
value = None
|
| 119 |
-
with col3:
|
| 120 |
-
if comp_type == "Diode":
|
| 121 |
-
subtype = st.selectbox("Diode Type", list(DIODE_DROPS.keys()))
|
| 122 |
-
orientation = st.selectbox("Orientation", ["forward", "reverse"])
|
| 123 |
-
else:
|
| 124 |
-
subtype = None
|
| 125 |
-
orientation = "forward"
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
-
|
| 137 |
-
st.
|
| 138 |
-
|
| 139 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 140 |
|
| 141 |
-
#
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
waveform_type = st.selectbox("Select Waveform Type", ["Sine", "Square", "Triangular"])
|
| 152 |
-
t = np.linspace(0, 0.05, 1000)
|
| 153 |
-
if waveform_type == "Sine":
|
| 154 |
-
wave = np.sin(2 * np.pi * 50 * t)
|
| 155 |
-
elif waveform_type == "Square":
|
| 156 |
-
wave = square(2 * np.pi * 50 * t)
|
| 157 |
else:
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
""")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from streamlit.components.v1 import html
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
import plotly.graph_objects as go
|
| 6 |
from scipy.signal import square, sawtooth
|
| 7 |
|
| 8 |
+
# Configuration for visual simulator
|
| 9 |
DIODE_DROPS = {"Silicon": 0.7, "Germanium": 0.3, "Ideal": 0.0}
|
| 10 |
COMPONENTS = {
|
| 11 |
"AC Source": {"symbol": "~", "orientation": True},
|
|
|
|
| 14 |
"Diode": {"symbol": "→", "orientation": True}
|
| 15 |
}
|
| 16 |
|
| 17 |
+
# Initialize session state for visual simulator
|
| 18 |
if "circuit" not in st.session_state:
|
| 19 |
st.session_state.circuit = []
|
| 20 |
if "connections" not in st.session_state:
|
|
|
|
| 91 |
voltage = source["value"] - diode_drop
|
| 92 |
current = voltage / total_resistance if total_resistance > 0 else 0
|
| 93 |
else:
|
| 94 |
+
pass # AC analysis placeholder
|
|
|
|
| 95 |
|
| 96 |
return pd.DataFrame({
|
| 97 |
"Total Voltage": [voltage],
|
|
|
|
| 100 |
"Diode Drop": [diode_drop]
|
| 101 |
})
|
| 102 |
|
| 103 |
+
# Main app
|
| 104 |
+
st.title("Circuit Simulator Suite")
|
|
|
|
| 105 |
|
| 106 |
+
# Create tabs
|
| 107 |
+
tab1, tab2 = st.tabs(["CircuitJS Simulator", "Visual Circuit Simulator"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
with tab1:
|
| 110 |
+
st.header("Interactive Circuit Simulator (CircuitJS1)")
|
| 111 |
+
circuitjs_html = """
|
| 112 |
+
<div>
|
| 113 |
+
<iframe src="https://www.falstad.com/circuit/circuitjs.html"
|
| 114 |
+
width="100%"
|
| 115 |
+
height="600px"
|
| 116 |
+
style="border:none;">
|
| 117 |
+
</iframe>
|
| 118 |
+
</div>
|
| 119 |
+
"""
|
| 120 |
+
html(circuitjs_html, height=600)
|
| 121 |
+
st.markdown("""
|
| 122 |
+
### Instructions:
|
| 123 |
+
1. Use the CircuitJS1 simulator above to design and simulate electrical circuits.
|
| 124 |
+
2. You can add components, connect them, and simulate the circuit in real-time.
|
| 125 |
+
3. Explore the menu options in CircuitJS1 for advanced features.
|
| 126 |
+
""")
|
| 127 |
|
| 128 |
+
with tab2:
|
| 129 |
+
st.header("🔌 Visual Circuit Simulator")
|
| 130 |
+
st.markdown("Build your circuit by placing components and connecting them")
|
|
|
|
| 131 |
|
| 132 |
+
# Visual simulator components
|
| 133 |
+
with st.expander("🛠️ Component Toolbox"):
|
| 134 |
+
col1, col2, col3 = st.columns(3)
|
| 135 |
+
with col1:
|
| 136 |
+
comp_type = st.selectbox("Component Type", list(COMPONENTS.keys()))
|
| 137 |
+
with col2:
|
| 138 |
+
if comp_type in ["Resistor"]:
|
| 139 |
+
value = st.number_input("Value (Ω)", 1, 1000, 1000)
|
| 140 |
+
elif comp_type in ["DC Source"]:
|
| 141 |
+
value = st.number_input("Voltage (V)", 1.0, 24.0, 5.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
else:
|
| 143 |
+
value = None
|
| 144 |
+
with col3:
|
| 145 |
+
if comp_type == "Diode":
|
| 146 |
+
subtype = st.selectbox("Diode Type", list(DIODE_DROPS.keys()))
|
| 147 |
+
orientation = st.selectbox("Orientation", ["forward", "reverse"])
|
| 148 |
+
else:
|
| 149 |
+
subtype = None
|
| 150 |
+
orientation = "forward"
|
| 151 |
+
|
| 152 |
+
if st.button("Add to Board"):
|
| 153 |
+
st.session_state.circuit.append({
|
| 154 |
+
"type": comp_type,
|
| 155 |
+
"value": value,
|
| 156 |
+
"subtype": subtype,
|
| 157 |
+
"orientation": orientation,
|
| 158 |
+
"position": [5, 5]
|
| 159 |
+
})
|
| 160 |
+
|
| 161 |
+
st.subheader("Circuit Board")
|
| 162 |
+
fig = draw_schematic()
|
| 163 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 164 |
+
|
| 165 |
+
if st.button("▶️ Run Analysis"):
|
| 166 |
+
results = analyze_circuit()
|
| 167 |
+
if not results.empty:
|
| 168 |
+
st.subheader("🔍 Analysis Results")
|
| 169 |
+
st.dataframe(results)
|
| 170 |
|
| 171 |
+
if "AC Source" in [c["type"] for c in st.session_state.circuit]:
|
| 172 |
+
st.subheader("📈 Waveform Visualization")
|
| 173 |
+
waveform_type = st.selectbox("Select Waveform Type", ["Sine", "Square", "Triangular"])
|
| 174 |
+
t = np.linspace(0, 0.05, 1000)
|
| 175 |
+
if waveform_type == "Sine":
|
| 176 |
+
wave = np.sin(2 * np.pi * 50 * t)
|
| 177 |
+
elif waveform_type == "Square":
|
| 178 |
+
wave = square(2 * np.pi * 50 * t)
|
| 179 |
+
else:
|
| 180 |
+
wave = sawtooth(2 * np.pi * 50 * t)
|
| 181 |
+
|
| 182 |
+
fig_wave = go.Figure()
|
| 183 |
+
fig_wave.add_trace(go.Scatter(x=t, y=wave))
|
| 184 |
+
st.plotly_chart(fig_wave)
|
| 185 |
+
else:
|
| 186 |
+
st.error("Invalid circuit configuration")
|
| 187 |
|
| 188 |
+
with st.expander("❓ How to use"):
|
| 189 |
+
st.markdown("""
|
| 190 |
+
1. **Add Components**:
|
| 191 |
+
- Select component type and parameters
|
| 192 |
+
- Click 'Add to Board'
|
| 193 |
+
- Drag components on the canvas (positioning not fully implemented)
|
| 194 |
+
|
| 195 |
+
2. **Connect Components**:
|
| 196 |
+
- Click on component terminals to connect them
|
| 197 |
+
- Create complete circuits with power sources
|
| 198 |
+
|
| 199 |
+
3. **Run Analysis**:
|
| 200 |
+
- Get voltage, current, and resistance values
|
| 201 |
+
- View waveforms for AC circuits
|
| 202 |
+
""")
|
|
|