-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.jl
98 lines (92 loc) · 3.57 KB
/
app.jl
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
using GenieFramework, PlotlyBase, DataFrames, JLD2, BSON
using StippleLatex, StippleDownloads
@genietools
# enable TailwindCSS
Stipple.Layout.add_script("https://cdn.tailwindcss.com")
# load some data to display on the plots on startup
@load "S.jld2" sol_stored
@app begin
# Reactive variables to hold the component states
@out states = unknowns_list
@in selected_states = ["Battery₊i(t)", "Battery₊v(t)", "MPPT₊in₊i(t)"]
@in T = 1.5
@in simulate = false
@private S = sol_stored
# Plots defined using PlotlyJS objects
# For plots made with Genie Builder, check out the #webinar branch
@out trace=[scatter()]
@out layout = PlotlyBase.Layout(
Dict{Symbol,Any}(:paper_bgcolor => "rgb(226, 232, 240)",
:plot_bgcolor => "rgb(226, 232, 240)");
xaxis_title="t (s)",
legend=attr( x=1, y=1.02, yanchor="bottom", xanchor="right", orientation="h",),
xaxis=attr(gridcolor="red", gridwidth=4),
yaxis=attr(gridcolor="red"),
margin=Dict(:l => 10, :r => 10, :b => 10, :t => 10),
)
@in components = component_config
# Reactive handler to run the simulation
@onbutton simulate begin
@info "Running simulation..."
prob = ODEProblem(sys, state_values, (0.0,T*1000), param_values)
sol = solve(prob,Rosenbrock23());
step = diff(sol.t)
S = DataFrame(hcat(sol.u...)', states)
insertcols!(S, 1, :t => sol.t)
xrange = [0.0, sol.t[end]]
selected_states = selected_states
@info "Simulation completed"
end
# Reactive handler to update the plot data
@onchange selected_states begin
plot_data = S[:, Symbol.(vcat(selected_states, "t"))]
end
# Handler to push new traces to the plot. Run it when the page is done loading
# in order to show some data on the plot
@onchange isready,selected_states begin
trace[!] = []
for u in selected_states
push!(trace,scatter(x=S[!,:t]/1000, y=S[!,u],mode="lines", name=u))
end
trace = copy(trace)
end
@event uploaded begin
if ENV["GENIE_ENV"] == "prod"
@info "Attempted upload in prod"
else
notify(__model__, "Upload finished")
notify(selected_states)
@info "uploaded"
end
end
# TODO: this only prevents file processing, prevent file uploading in prod
@onchange fileuploads begin
if ENV["GENIE_ENV"] == "prod"
notify(__model__, "Run locally to enable file uploads", :warning)
rm(fileuploads["path"])
else
if ! isempty(fileuploads)
@info "File was uploaded: " fileuploads
BSON.@load fileuploads["path"] solution
S = solution
rm(fileuploads["path"])
fileuploads = Dict{AbstractString,AbstractString}()
end
end
end
@event download begin
try
solution = S
io = IOBuffer()
BSON.@save io solution
seekstart(io)
download_binary(__model__, take!(io), "simulation.bson")
catch ex
println("Error during download: ", ex)
end
end
end
@page("/", "app.jl.html")
load_component("dynamic-plotly")
load_component("component-props")
Stipple.Layout.add_script("components/dynamic-plotly/index.js")