-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpyAnsys_DPF_dash.py
336 lines (299 loc) · 11.5 KB
/
pyAnsys_DPF_dash.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import numpy as np
import plotly.graph_objects as go
import dash
from dash.exceptions import PreventUpdate
from dash import dcc, Output, Input, State, html, dash_table
from dash.dash_table.Format import Format, Scheme
import dash_bootstrap_components as dbc
import dash_vtk
from dash_vtk.utils import to_mesh_state
from ansys.dpf import core as dpf
from ansys.dpf.core import examples
APP_ID = 'pyAnsys'
EXAMPLE_MAP = {
'simple_bar': examples.simple_bar,
'msup_transient': examples.msup_transient,
'static': examples.static_rst
}
def get_grid_with_field(meshed_region, field, grid=None):
name = '_'.join(field.name.split("_")[:-1])
location = field.location
if location == dpf.locations.nodal:
mesh_location = meshed_region.nodes
elif location == dpf.locations.elemental:
mesh_location = meshed_region.elements
elif location == dpf.locations.elemental_nodal:
location = dpf.locations.elemental
mesh_location = meshed_region.elements
# convert elemental_nodal to elemental
en_e_op = dpf.operators.averaging.elemental_mean()
en_e_op.inputs.field.connect(field)
field = en_e_op.outputs.field()
else:
raise ValueError(
"Only elemental or nodal location are supported for plotting."
)
component_count = field.component_count
if component_count > 1:
overall_data = np.full((len(mesh_location), component_count), np.nan)
else:
overall_data = np.full(len(mesh_location), np.nan)
ind, mask = mesh_location.map_scoping(field.scoping)
overall_data[ind] = field.data[mask]
if grid is None:
grid = meshed_region.grid
if location == dpf.locations.nodal:
grid.cell_data['dummy'] = np.zeros(grid.n_cells)
grid.point_data[name] = overall_data
elif location == dpf.locations.elemental:
grid.point_data['dummy'] = np.zeros(grid.n_points)
grid.cell_data[name] = overall_data
return grid
def make_colorbar(title, rng, bgnd='rgb(51, 76, 102)'):
fig = go.Figure()
fig.add_trace(
go.Scatter(x=[None],
y=[None],
mode='markers',
marker=dict(
colorscale='plasma',
showscale=True,
cmin=rng[0],
cmax=rng[1],
colorbar=dict(
title_text=title,
title_font_color='white',
title_side='top',
thicknessmode="pixels", thickness=50,
# lenmode="pixels", len=200,
yanchor="middle", y=0.5, ypad=10,
xanchor="left", x=0., xpad=10,
ticks="outside",
tickcolor='white',
tickfont={'color':'white'}
# dtick=5
)
),
hoverinfo='none'
)
)
fig.update_layout(width=150, margin={'b': 0, 'l': 0, 'r': 0, 't': 0}, autosize=False, plot_bgcolor=bgnd)
fig.update_xaxes(visible=False)
fig.update_yaxes(visible=False)
return fig
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = dbc.Container([
html.H1('PyAnsys DPF in a Dash App', className="mt-3"),
html.P('This app allows plotting results from built-in examples'),
dbc.Row([
dbc.Col([
dbc.Label('Select Example', html_for="dropdown"),
dcc.Dropdown(
id=f'{APP_ID}_example_dropdown',
options=[
{'label': 'simple_bar', 'value': 'simple_bar'},
{'label': 'msup_transient', 'value': 'msup_transient'},
{'label': 'static', 'value': 'static'},
],
clearable=False,
),
dbc.Label('Select Time / Frequency', html_for="dropdown"),
dcc.Dropdown(
id=f'{APP_ID}_example_tf_dropdown',
options=[],
value=None,
clearable=False
)
]),
dbc.Col([
dbc.Label('Select Result', html_for="dropdown"),
dcc.Dropdown(
id=f'{APP_ID}_example_result_dropdown',
options=[],
value=None,
clearable=False
),
dbc.Label('Select Component', html_for="dropdown"),
dcc.Dropdown(
id=f'{APP_ID}_example_comp_dropdown',
options=[{'label': 0, 'value': 0}],
value=0,
clearable=False,
disabled=True
)
]),
dbc.Col(
dbc.Button(
'Plot Results',
id=f'{APP_ID}_plot_button',
style={'height':'80%', 'width':'80%'},
class_name='h-50'
),
align="center"
)
]),
dbc.Row([
dbc.Col([
html.Div(
style={"width": "100%", "height": "60vh"},
children=[
dash_vtk.View(
id=f'{APP_ID}_vtk_view',
children=dash_vtk.GeometryRepresentation(
id=f'{APP_ID}_geom_rep_mesh',
children=[],
property={"edgeVisibility": True, "opacity": 1, "pointSize": 20, "lineWidth": 2},
colorMapPreset="Plasma (matplotlib)",
),
),
]
)
],
width=10
),
dbc.Col([
dcc.Graph(
id=f'{APP_ID}_results_colorbar_graph',
style={"width": "100%", "height": "60vh"},
),
],
width=2
)
],
className="g-0"
),
dbc.Card(
dbc.CardBody(
dash_table.DataTable(
id=f'{APP_ID}_results_min_max_dt',
columns=[
{'name': '', 'id': 'index'},
{'name': 'Model', 'id': 'model', 'type':'numeric', 'format': Format(precision=2, scheme=Scheme.decimal_or_exponent)},
],
data=[
{'index': 'Max', 'model': None},
{'index': 'Min', 'model': None}
],
style_data_conditional=[
{
'if': {
'column_id': 'index',
},
'fontWeight': 'bold'
},
],
style_header={
'fontWeight': 'bold'
},
)
),
style={"width": "18rem"},
)
])
@app.callback(
Output(f'{APP_ID}_example_tf_dropdown', 'options'),
Output(f'{APP_ID}_example_tf_dropdown', 'value'),
Output(f'{APP_ID}_example_result_dropdown','options'),
Output(f'{APP_ID}_example_result_dropdown','value'),
Input(f'{APP_ID}_example_dropdown', 'value')
)
def dash_vtk_update_result_options(example_name):
ctx = dash.callback_context
if not ctx.triggered:
raise PreventUpdate
model = dpf.Model(EXAMPLE_MAP[example_name])
result_info = model.metadata.result_info
res_options = [{'label': res.name, 'value': res.name} for res in result_info.available_results]
tf = model.metadata.time_freq_support.time_frequencies.data
tf_options = [{'label': tfi, 'value': i} for i, tfi in enumerate(tf)]
return tf_options, len(tf)-1, res_options, result_info.available_results[0].name
@app.callback(
Output(f'{APP_ID}_example_comp_dropdown','options'),
Output(f'{APP_ID}_example_comp_dropdown','value'),
Output(f'{APP_ID}_example_comp_dropdown','disabled'),
Input(f'{APP_ID}_example_result_dropdown','value'),
State(f'{APP_ID}_example_dropdown', 'value')
)
def dash_vtk_update_comp_options(res_name, example_name):
ctx = dash.callback_context
if not ctx.triggered:
raise PreventUpdate
model = dpf.Model(EXAMPLE_MAP[example_name])
result_info = model.metadata.result_info
res = next((r for r in result_info.available_results if r.name == res_name), None)
if res is not None:
if res.n_components == 1:
comp_options = [{'label': 0, 'value': 0}]
comp_value = 0
comp_disabled = True
else:
comp_options = [{'label': i, 'value': i} for i in range(res.n_components)]
comp_value = 0
comp_disabled = False
return comp_options, comp_value, comp_disabled
else:
raise PreventUpdate
@app.callback(
Output(f'{APP_ID}_geom_rep_mesh', 'children'),
Output(f'{APP_ID}_geom_rep_mesh', 'colorDataRange'),
Output(f'{APP_ID}_results_colorbar_graph', 'figure'),
Output(f'{APP_ID}_results_min_max_dt', 'data'),
Input(f'{APP_ID}_plot_button', 'n_clicks'),
State(f'{APP_ID}_example_dropdown', 'value'),
State(f'{APP_ID}_example_result_dropdown','value'),
State(f'{APP_ID}_example_comp_dropdown', 'value'),
State(f'{APP_ID}_example_tf_dropdown', 'value')
)
def dash_vtk_update_grid(n_clicks, example_name, result_name, comp_idx, tf_idx):
if any([v is None for v in [example_name, result_name, tf_idx]]):
raise PreventUpdate
ctx = dash.callback_context
if not ctx.triggered:
raise PreventUpdate
trig_id = ctx.triggered[0]['prop_id'].split('.')[0]
model = dpf.Model(EXAMPLE_MAP[example_name])
result_info = model.metadata.result_info
res = next((r for r in result_info.available_results if r.name == result_name), None)
mesh = model.metadata.meshed_region
ugrid = mesh.grid
if res.n_components == 1:
res_op = dpf.Operator(res.operator_name)
res_op.inputs.data_sources.connect(model.metadata.data_sources)
res_op.inputs.time_scoping([tf_idx+1])
fields = res_op.outputs.fields_container()
f0 = fields[0]
name = '_'.join(f0.name.split("_")[:-1])
ugrid = get_grid_with_field(mesh, f0)
mesh_state = to_mesh_state(ugrid.copy(), field_to_keep=name)
elif res.n_components > 1:
res_op = dpf.Operator(res.operator_name)
res_op.inputs.data_sources.connect(model.metadata.data_sources)
res_op.inputs.time_scoping([tf_idx+1])
comp_sel = dpf.operators.logic.component_selector_fc()
comp_sel.inputs.connect(res_op.outputs)
comp_sel.inputs.component_number.connect(comp_idx)
fields = comp_sel.outputs.fields_container()
f0 = fields[0]
name = '_'.join(f0.name.split("_")[:-1])
ugrid = get_grid_with_field(mesh, f0)
mesh_state = to_mesh_state(ugrid.copy(), field_to_keep=name)
else:
raise PreventUpdate
view_max = ugrid[name].max()
view_min = ugrid[name].min()
rng = [view_min, view_max]
name = '_'.join(f0.name.split("_")[:-1])
fig = make_colorbar(name, rng)
dt = [{'index':'Max', 'model':view_max}, {'index':'Min', 'model':view_min}]
return [dash_vtk.Mesh(state=mesh_state), rng, fig, dt]
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Launch PyDPF sample dash server')
parser.add_argument('--ip', type=str, metavar='',
required=False,
help='Set the IP address of the server')
args = parser.parse_args()
if args.ip:
app.run_server(debug=True, host=args.ip)
else:
app.run_server(debug=True)