-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdash_app.py
187 lines (165 loc) · 5.91 KB
/
dash_app.py
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
import time
import os
import dash
from dash import Dash, dcc, html, Input, Output
import plotly.graph_objects as go
import math
app = dash.Dash(requests_pathname_prefix="/dashboard/")
# Initialize variables
ratio, length, point0_x, point0_y, current_x, current_y = 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
origin_x, origin_y, height, velocity = 0.0, 0.0, 0.0, 0.0
gravitational, distance, totaltime = [0.0], [0.0], [0.0]
gravitational0, kinetic = 0.0, 0.0
periodtime = 0.0
mass = 109 / 100
onetime, flag = True, 0
starttime, periodstarttime = time.time(), time.time()
file_path = "pendulum_values.txt"
app.layout = html.Div(
html.Div(
[
html.Div(id="live-update-text"),
dcc.Graph(id="graph"),
dcc.Graph(id="gravitationalgraph"),
dcc.Interval(
id="interval-component",
interval=1 * 100, # in milliseconds
n_intervals=1,
),
]
)
)
def euclidean(p1, p2):
"""Calculate Euclidean distance between two points."""
return math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)
@app.callback(
Output("live-update-text", "children"),
Input("interval-component", "n_intervals"),
)
def update_metrics(n):
global ratio, length, point0_x, point0_y, current_x, current_y, distance, starttime, periodstarttime, totaltime, origin_x, origin_y, gravitational0, kinetic, onetime, flag, periodtime
if os.path.exists(file_path):
with open(file_path, "r") as file:
values = {
line.split(":")[0].strip(): float(line.split(":")[1]) for line in file
}
ratio, length, point0_x, point0_y, current_x, current_y = (
values.get("Ratio", 0.0),
values.get("Length", 0.0),
values.get("Point 0 X", 0.0),
values.get("Point 0 Y", 0.0),
values.get("Current Point X", 0.0),
values.get("Current Point Y", 0.0),
)
flag, origin_x, origin_y = (
values.get("Flag", 0),
point0_x,
point0_y - (length / ratio) if ratio > 0 else 0,
)
length = length / 100
if flag == 0:
gravitational0, starttime, periodstarttime = 0.0, time.time(), time.time()
if current_x != point0_x:
totaltime.append(round(time.time() - starttime, 2))
periodtime = round(time.time() - periodstarttime, 2)
if current_x > point0_x:
distance.append(
euclidean((current_x, current_y), (point0_x, point0_y)) * ratio
)
elif current_x < point0_x:
distance.append(
euclidean((current_x, current_y), (point0_x, point0_y)) * -ratio
)
T = 2 * math.pi * (math.sqrt(length / 9.83))
angle = math.degrees(math.atan2(current_x - origin_x, current_y - origin_y))
angleradians = math.radians(angle)
height = length - (length * math.cos(angleradians))
gravitational.append(mass * 9.83 * height)
if (flag == 1 and onetime) or all(
gravitational[i] > gravitational[-1] for i in range(-2, -6, -1)
):
if onetime:
gravitational0, onetime, periodtime, periodstarttime = (
mass * 9.83 * height,
False,
0.0,
time.time(),
)
else:
now = (
gravitational[-2]
+ gravitational[-3]
+ gravitational[-4]
+ gravitational[-5]
) / 4
if now > gravitational[-1]:
gravitational0 = now
periodtime = 0.0
style = {"padding": "5px", "fontSize": "16px"}
return [
html.Span(f"{key}: {value:.2f}", style=style)
for key, value in {
"Ratio": ratio,
"Length": length,
"Point 0 X": point0_x,
"Point 0 Y": point0_y,
"Current X": current_x,
"Current Y": current_y,
"Origin X": origin_x,
"Origin Y": origin_y,
"Angle": angle,
"ENERGIATOTAL": gravitational0,
"FLAG": flag,
"Distance from Center": distance[-1],
"Total Time": totaltime[-1],
"Total Period Time": periodtime,
}.items()
]
else:
return "File not found"
@app.callback(Output("graph", "figure"), Input("interval-component", "n_intervals"))
def display_graph(n):
global totaltime, distance
fig = go.Figure(
go.Scatter(
x=totaltime,
y=distance,
mode="lines+markers",
name="Pendulum",
marker=dict(color="rgb(105, 219, 124)"),
)
)
fig.update_layout(
title="Pendulum Motion",
xaxis_title="Total Time (s)",
yaxis_title="Distance from Center (cm)",
paper_bgcolor="rgb(13, 17, 23)",
plot_bgcolor="rgb(22, 27, 34)",
font=dict(color="rgb(230, 237, 243)"),
)
return fig
@app.callback(
Output("gravitationalgraph", "figure"), Input("interval-component", "n_intervals")
)
def display_gravitationalgraph(n):
global gravitational, gravitational0
kinetic = max(0, gravitational0 - gravitational[-1])
colors = ["rgb(255, 169, 77)", "rgb(121, 192, 255)", "rgb(174, 62, 201)"]
fig = go.Figure(
go.Bar(
x=["Gravitational Energy", "Kinetic Energy", "Total Energy"],
y=[gravitational[-1], kinetic, gravitational0],
name="Energys",
marker_color=colors,
)
)
fig.update_layout(
title="Energys",
yaxis_title="Joules (J)",
paper_bgcolor="rgb(13, 17, 23)",
plot_bgcolor="rgb(22, 27, 34)",
font=dict(color="rgb(230, 237, 243)"),
)
return fig
if __name__ == "__main__":
app.run_server(debug=True)