-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim_controller.py
More file actions
264 lines (224 loc) · 11.2 KB
/
sim_controller.py
File metadata and controls
264 lines (224 loc) · 11.2 KB
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
import math
import random
from textual.widgets import Input, Select, Static
from textual_plotext import PlotextPlot
import simulators
import statistics_engine
#tau = R * C
#0.693 = time to half voltage / (r times c)
class SimulationController: # handles logic
@staticmethod
def _get_graph_theme(app_theme_name: str) -> str:
if app_theme_name == "matrix":
return "matrix"
return "pro"
@staticmethod
def _prepare_plot(app, title: str):
widget = app.query_one("#main_plot", PlotextPlot)
plt = widget.plt
current_app_theme = getattr(app, "theme", "textual-dark")
graph_theme = SimulationController._get_graph_theme(current_app_theme)
prefs = getattr(app, "app_prefs", {})
line_col = prefs.get("graph_line_color", "yellow")
plt.clear_figure()
plt.theme(graph_theme)
plt.title(title)
return plt, line_col
@staticmethod
def _update_elec_stats(app, data, mode):
text = statistics_engine.StatsEngine.analyze_simulation(data, mode)
try:
app.query_one("#stats_display_elec", Static).update(text)
except (AttributeError, NameError):
pass
@staticmethod
def run_rc_filter(app):
try:
v_in = float(app.query_one("#rc_voltage", Input).value)
c_uf = float(app.query_one("#rc_cap", Input).value)
mode = app.query_one("#rc_mode", Select).value
try:
dur = float(app.query_one("#sim_duration", Input).value)
except (ValueError, AttributeError):
dur = 0.0
if mode == "step":
raw = app.query_one("#rc_res", Input).value
res = [float(x) for x in raw.split(',') if x.strip()]
if not res:
return "No resistors." # early exit
data = simulators.calculate_rc_transient(v_in, c_uf, res, dur)
plt, _ = SimulationController._prepare_plot(app, "RC Step Response")
for c in data["curves"]:
plt.plot(data["time"], c["voltage"], label=f"R={c['R']}Ω")
app.last_data = data
app.last_mode = "rc_step"
# Stats
SimulationController._update_elec_stats(app, data, "rc_step")
return f"RC Step: {len(res)} curves."
elif mode == "square":
freq = float(app.query_one("#rc_freq", Input).value)
res = float(app.query_one("#rc_res", Input).value.split(',')[0])
data = simulators.calculate_square_wave_response(v_in, c_uf, res, freq)
data['freq'] = freq
plt, col = SimulationController._prepare_plot(app, f"RC Filter ({freq}Hz)")
plt.plot(data["time"], data["input_wave"], label="In", color="green")
plt.plot(data["time"], data["output_wave"], label="Out", color=col)
app.last_data = data
app.last_mode = "rc_square"
# Stats
SimulationController._update_elec_stats(app, data, "rc_square")
return f"RC Square: Tau={data['tau']:.4f}s"
except Exception as e: return f"Error: {e}"
@staticmethod
def run_555_astable(app):
try:
r1 = float(app.query_one("#timer_r1", Input).value)
r2 = float(app.query_one("#timer_r2", Input).value)
c = float(app.query_one("#timer_c", Input).value)
try:
dur = float(app.query_one("#sim_duration", Input).value)
except (ValueError, AttributeError):
dur = 0.0
data = simulators.calculate_555_astable(r1, r2, c, dur)
plt, col = SimulationController._prepare_plot(
app, f"555 Astable (Freq={data['freq']:.1f}Hz)")
plt.plot(data["time"], data["voltage"], label="Output", color=col)
app.last_data = data
app.last_mode = "555_astable"
SimulationController._update_elec_stats(app, data, "555_astable")
return f"Freq={data['freq']:.2f}Hz"
except (ValueError, KeyError, AttributeError) as e:
return f"Error: {e}"
@staticmethod
def run_555_monostable(app):
try:
r = float(app.query_one("#mono_r", Input).value)
c = float(app.query_one("#mono_c", Input).value)
try:
dur = float(app.query_one("#sim_duration", Input).value)
except (ValueError, AttributeError):
dur = 0.0
data = simulators.calculate_555_monostable(r, c, dur)
plt, col = SimulationController._prepare_plot(
app, f"555 Monostable (R={r}Ω, C={c}µF)")
plt.plot(data["time"], data["trigger"], label="Trigger", color="green")
plt.plot(data["time"], data["output"], label="Output", color=col)
plt.plot(data["time"], data["cap_voltage"], label="Capacitor",
color="blue")
app.last_data = data
app.last_mode = "555_mono"
SimulationController._update_elec_stats(app, data, "555_mono")
return f"Pulse: {data['pulse_width']:.4f}s"
except (ValueError, KeyError, AttributeError) as e:
return f"Error: {e}"
@staticmethod
def run_general_plot(app):
try:
data_state = getattr(app, "gen_data_state", {})
cols = data_state.get("columns", [])
rows = data_state.get("rows", [])
if len(cols) < 2 or not rows: return "No data."
labels = [str(r[0]) for r in rows]
series_names = cols[1:]
series_data = []
for col_idx in range(1, len(cols)):
col_vals = []
for r in rows:
try:
val = float(r[col_idx])
except (ValueError, IndexError, TypeError):
val = 0.0
col_vals.append(val)
series_data.append(col_vals)
app.last_gen_data = {"labels": labels, "series": series_data, "names": series_names}
app.last_gen_mode = app.query_one("#gen_type", Select).value
mode = app.last_gen_mode
# stats update
try:
active_idx = getattr(app, "active_series_index", 0)
if active_idx >= len(series_data): active_idx = 0
if series_data:
current_vals = series_data[active_idx]
s_name = series_names[active_idx]
# Use modular engine
stats_text = statistics_engine.StatsEngine.calculate_generic(current_vals, s_name)
else:
stats_text = "No data"
app.query_one("#stats_display", Static).update(stats_text)
except (ValueError, KeyError, AttributeError):
app.query_one("#stats_display", Static).update("stats error")
# -------------------------
widget = app.query_one("#gen_plot", PlotextPlot)
plt = widget.plt
current_app_theme = getattr(app, "theme", "textual-dark")
graph_theme = SimulationController._get_graph_theme(current_app_theme)
plt.clear_figure()
plt.theme(graph_theme)
plt.title("Data view")
active_idx = getattr(app, "active_series_index", 0)
if active_idx >= len(series_data): active_idx = 0
colors = ["red", "green", "yellow", "blue", "magenta", "cyan", "white"]
if mode == "pie": # plotting with dots. normal pie is hard-impossible in plotext
if series_data:
vals = [abs(x) for x in series_data[active_idx]]
name = series_names[active_idx]
total = sum(vals)
if total > 0:
angles = []
current = 0
for v in vals:
share = (v / total) * 360
angles.append((current, current + share))
current += share
points_x = [[] for _ in vals]
points_y = [[] for _ in vals]
for _ in range(2000):
r = math.sqrt(random.random()) # EVERY PREVIEW will be different, due to added noise.
theta = random.random() * 360
slice_idx = 0
for i, (start, end) in enumerate(angles):
if start <= theta < end:
slice_idx = i
break
x = r * math.cos(math.radians(theta)) * 1.8
y = r * math.sin(math.radians(theta))
points_x[slice_idx].append(x)
points_y[slice_idx].append(y)
for i, _ in enumerate(vals): # finish up
label_txt = f"{labels[i]}" if i < len(labels) else ""
col = colors[i % len(colors)]
if points_x[i]:
plt.scatter(points_x[i], points_y[i], label=label_txt, color=col)
plt.title(f"{name} ({active_idx + 1}/{len(series_data)})")
plt.plotsize(None, None)
plt.xaxes(False, False)
plt.yaxes(False, False)
plt.frame(False)
else: # other plot types (they take up less lines than the whole pie function)
if mode == "bar":
try:
plt.multiple_bar(labels, series_data)
except (ValueError, AttributeError):
plt.multiple_bar(labels, series_data)
elif mode == "area":
for i, s in enumerate(series_data):
col = colors[i % len(colors)]
try:
plt.plot(s, label=series_names[i], fillx=True, color=col)
except (ValueError, AttributeError):
plt.plot(s, label=series_names[i], color=col)
plt.xticks(range(len(labels)), labels)
elif mode == "scatter":
for i, s in enumerate(series_data):
col = colors[i % len(colors)]
plt.scatter(s, label=series_names[i], color=col)
plt.xticks(range(len(labels)), labels)
else:
for i, s in enumerate(series_data):
col = colors[i % len(colors)]
plt.plot(s, label=series_names[i], color=col)
plt.xticks(range(len(labels)), labels)
widget.refresh()
return f"Rendered {mode}."
except (ValueError, KeyError, AttributeError, IndexError) as e:
return f"Render error: {e}"