forked from t-sommer/fmi-cloud-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webapp_solution.py
115 lines (103 loc) · 4.31 KB
/
webapp_solution.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
import dash
import dash_html_components as html
import dash_core_components as dcc
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State
from fmpy import simulate_fmu, read_model_description
from fmpy.util import create_plotly_figure
import numpy as np
app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
model_description = read_model_description('Heater.fmu')
app.layout = dbc.Container([
dbc.Container(
[
html.Img(src=app.get_asset_url('Heater.png'), style={'max-width': '80%', 'display': 'block', 'margin-left': 'auto', 'margin-right': 'auto'})
],
className='my-5'
),
dbc.Container(
[
dbc.Alert(model_description.description, color='dark')
],
className='my-5'
),
dbc.Container(
[
dbc.Form(
[
dbc.Button('Simulate', id='simulate-button', color='primary', className='mr-4'),
dbc.InputGroup(
[
dbc.Input(id='stop-time', value='50', style={'text-align': 'right', 'width': '5rem'}),
dbc.InputGroupAddon('s', addon_type="append", style={'width': '2rem'})
], className='mr-4'
)
], inline=True
),
dbc.Row(
[
dbc.Col(
[
dbc.FormGroup(
[
dbc.Label('TAmb', html_for='TAmb', width=6),
dbc.Col(
dbc.InputGroup(
[
dbc.Input(id='TAmb', value='283.15', style={'text-align': 'right'}),
dbc.InputGroupAddon('K', addon_type='append'),
], size='sm'
),
width=6,
),
html.Small("Ambient temperature", className='form-text text-muted ml-3')
],
row=True, className='mb-2'
),
dbc.FormGroup(
[
dbc.Label('V', html_for='V', width=6),
dbc.Col(
dbc.InputGroup(
[
dbc.Input(id='V', value='10', style={'text-align': 'right'}),
dbc.InputGroupAddon('V', addon_type='append'),
], size='sm'
),
width=6,
),
html.Small("Voltage", className='form-text text-muted ml-3')
],
row=True, className='mb-2'
),
],
width=12, lg=4, style={'margin-top': '2rem'}
),
dbc.Col(id='result-col', width=12, lg=8),
], className='mt-4'
),
],
id='simulation-container'
),
])
@app.callback(
Output('result-col', 'children'),
[Input('simulate-button', 'n_clicks')],
[State('stop-time', 'value'), State('TAmb', 'value'), State('V', 'value')]
)
def update_output_div(n_clicks, stop_time, TAmb, V):
try:
input = np.array([(0, False), (10, True), (20, False)], dtype=[('time', np.float64), ('u', np.bool_)])
result = simulate_fmu(
filename='Heater.fmu',
stop_time=stop_time,
start_values={'TAmb': TAmb, 'constantVoltage.V': V},
output=['T', 'u'],
input=input
)
fig = create_plotly_figure(result=result)
return dcc.Graph(figure=fig)
except Exception as e:
return dbc.Alert("Simulation failed. %s" % e, color='danger'),
if __name__ == "__main__":
app.run_server()