|
| 1 | +# Simpler gist here: https://gist.github.com/xhlulu/773fe238773ea69c8bc2b26560ab67d7 |
| 2 | +import random |
| 3 | + |
| 4 | +import dash |
| 5 | +import dash_bootstrap_components as dbc |
| 6 | +import dash_vtk |
| 7 | +import dash_core_components as dcc |
| 8 | +import dash_html_components as html |
| 9 | +from dash.dependencies import Input, Output, State |
| 10 | +import plotly.express as px |
| 11 | +import floris.tools as wfct |
| 12 | +import numpy as np |
| 13 | + |
| 14 | + |
| 15 | +def Header(name, app): |
| 16 | + title = html.H1(name) |
| 17 | + logo = html.Img( |
| 18 | + src=app.get_asset_url("dash-logo.png"), style={"float": "right", "height": 60} |
| 19 | + ) |
| 20 | + link = html.A(logo, href="https://plotly.com/dash/") |
| 21 | + |
| 22 | + return dbc.Row([dbc.Col(title, md=8), dbc.Col(link, md=4)], align="center") |
| 23 | + |
| 24 | + |
| 25 | +def CustomSlider(data: np.array, label: str, id: str): |
| 26 | + n_unique = np.unique(data).shape[0] |
| 27 | + |
| 28 | + return dbc.FormGroup( |
| 29 | + [ |
| 30 | + dbc.Label(label), |
| 31 | + dcc.Slider(min=0, max=n_unique, value=n_unique / 2, step=1, id=id,), |
| 32 | + ] |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def build_view_child( |
| 37 | + dimensions, spacing, origin, field, enabled, i, j, k, window, level |
| 38 | +): |
| 39 | + slice_prop = {"colorWindow": window, "colorLevel": level} |
| 40 | + child = [ |
| 41 | + dash_vtk.ShareDataSet( |
| 42 | + dash_vtk.ImageData( |
| 43 | + dimensions=dimensions, |
| 44 | + spacing=spacing, |
| 45 | + origin=origin, |
| 46 | + children=dash_vtk.PointData( |
| 47 | + dash_vtk.DataArray(registration="setScalars", values=field) |
| 48 | + ), |
| 49 | + ), |
| 50 | + ), |
| 51 | + ] |
| 52 | + |
| 53 | + if "Volume" in enabled: |
| 54 | + child.append( |
| 55 | + dash_vtk.VolumeRepresentation( |
| 56 | + [dash_vtk.VolumeController(), dash_vtk.ShareDataSet()], |
| 57 | + ) |
| 58 | + ) |
| 59 | + if "i" in enabled: |
| 60 | + child.append( |
| 61 | + dash_vtk.SliceRepresentation( |
| 62 | + iSlice=int(round(i)), |
| 63 | + property=slice_prop, |
| 64 | + children=dash_vtk.ShareDataSet(), |
| 65 | + ) |
| 66 | + ) |
| 67 | + |
| 68 | + if "j" in enabled: |
| 69 | + child.append( |
| 70 | + dash_vtk.SliceRepresentation( |
| 71 | + jSlice=int(round(j)), |
| 72 | + property=slice_prop, |
| 73 | + children=dash_vtk.ShareDataSet(), |
| 74 | + ) |
| 75 | + ) |
| 76 | + |
| 77 | + if "k" in enabled: |
| 78 | + child.append( |
| 79 | + dash_vtk.SliceRepresentation( |
| 80 | + kSlice=int(round(k)), |
| 81 | + property=slice_prop, |
| 82 | + children=dash_vtk.ShareDataSet(), |
| 83 | + ) |
| 84 | + ) |
| 85 | + |
| 86 | + return child |
| 87 | + |
| 88 | + |
| 89 | +# Initialize the FLORIS interface fi |
| 90 | +fi = wfct.floris_interface.FlorisInterface("./data/example_input.json") |
| 91 | +fd = fi.get_flow_data() |
| 92 | + |
| 93 | +# compute dimensions, origins and spacing based on flow data |
| 94 | +origin = [axis.mean().round().astype(int) for axis in [fd.x, fd.y, fd.z]] |
| 95 | +ranges = np.array([axis.ptp().round().astype(int) for axis in [fd.x, fd.y, fd.z]]) |
| 96 | +dimensions = np.array([np.unique(axis).shape[0] for axis in [fd.x, fd.y, fd.z]]) |
| 97 | +x, y, z = dimensions |
| 98 | +spacing = np.round(ranges / dimensions).astype(int) |
| 99 | + |
| 100 | +# Create the volumes |
| 101 | +views = dict(u=fd.u, v=fd.v, w=fd.w) |
| 102 | + |
| 103 | + |
| 104 | +controls = [ |
| 105 | + dbc.FormGroup( |
| 106 | + [ |
| 107 | + dbc.Label("Dimension"), |
| 108 | + dbc.RadioItems( |
| 109 | + options=[{"label": x, "value": x} for x in ["u", "v", "w"]], |
| 110 | + value="u", |
| 111 | + id="radio-dimension", |
| 112 | + inline=True, |
| 113 | + ), |
| 114 | + ] |
| 115 | + ), |
| 116 | + dbc.FormGroup( |
| 117 | + [ |
| 118 | + dbc.Label("Enabled"), |
| 119 | + dbc.Checklist( |
| 120 | + options=[ |
| 121 | + {"label": x, "value": x.replace("Slice ", "")} |
| 122 | + for x in ["Volume", "Slice i", "Slice j", "Slice k"] |
| 123 | + ], |
| 124 | + value=["Volume", "i"], |
| 125 | + id="child-enabled", |
| 126 | + inline=True, |
| 127 | + ), |
| 128 | + ] |
| 129 | + ), |
| 130 | + dbc.FormGroup( |
| 131 | + [ |
| 132 | + dbc.Label("Color Window"), |
| 133 | + dcc.Slider( |
| 134 | + min=0.01, max=1, value=0.5, step=0.01, id="color-window", tooltip={}, |
| 135 | + ), |
| 136 | + ] |
| 137 | + ), |
| 138 | + dbc.FormGroup( |
| 139 | + [ |
| 140 | + dbc.Label("Color Level"), |
| 141 | + dcc.Slider( |
| 142 | + min=0.01, max=1, value=0.5, step=0.01, id="color-level", tooltip={}, |
| 143 | + ), |
| 144 | + ] |
| 145 | + ), |
| 146 | + CustomSlider(data=fd.x, id="slider-slice-i", label="Slice i"), |
| 147 | + CustomSlider(data=fd.y, id="slider-slice-j", label="Slice j"), |
| 148 | + CustomSlider(data=fd.z, id="slider-slice-k", label="Slice k"), |
| 149 | +] |
| 150 | + |
| 151 | +vtk_view = dash_vtk.View(id="vtk-view") |
| 152 | + |
| 153 | + |
| 154 | +app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP]) |
| 155 | +server = app.server # gunicorn needs this for deployment |
| 156 | + |
| 157 | +app.layout = dbc.Container( |
| 158 | + fluid=True, |
| 159 | + style={"height": "100vh"}, |
| 160 | + children=[ |
| 161 | + Header("Flow Visualization with FLORIS and VTK", app), |
| 162 | + html.Hr(), |
| 163 | + dbc.Row( |
| 164 | + [ |
| 165 | + dbc.Col( |
| 166 | + width=4, |
| 167 | + children=dbc.Card( |
| 168 | + [dbc.CardHeader("Controls"), dbc.CardBody(controls),] |
| 169 | + ), |
| 170 | + ), |
| 171 | + dbc.Col( |
| 172 | + width=8, |
| 173 | + children=dbc.Card( |
| 174 | + [ |
| 175 | + dbc.CardHeader("Flow Visualization"), |
| 176 | + dbc.CardBody(vtk_view, style={"height": "100%"}), |
| 177 | + ], |
| 178 | + style={"height": "80vh"}, |
| 179 | + ), |
| 180 | + ), |
| 181 | + ], |
| 182 | + ), |
| 183 | + ], |
| 184 | +) |
| 185 | + |
| 186 | + |
| 187 | +@app.callback( |
| 188 | + Output("vtk-view", "children"), |
| 189 | + Output("vtk-view", "triggerRender"), |
| 190 | + Input("radio-dimension", "value"), |
| 191 | + Input("child-enabled", "value"), |
| 192 | + Input("color-window", "value"), |
| 193 | + Input("color-level", "value"), |
| 194 | + Input("slider-slice-i", "value"), |
| 195 | + Input("slider-slice-j", "value"), |
| 196 | + Input("slider-slice-k", "value"), |
| 197 | +) |
| 198 | +def update_flow_viz( |
| 199 | + selected_dim, enabled, window_coef, level_coef, slice_i, slice_j, slice_k |
| 200 | +): |
| 201 | + field = views[selected_dim] |
| 202 | + window = (field.max() - field.min()) * window_coef |
| 203 | + level = (field.max() + field.min()) * level_coef |
| 204 | + |
| 205 | + new_view_child = build_view_child( |
| 206 | + dimensions, |
| 207 | + spacing, |
| 208 | + origin, |
| 209 | + window=window, |
| 210 | + level=level, |
| 211 | + field=field, |
| 212 | + enabled=enabled, |
| 213 | + i=slice_i, |
| 214 | + j=slice_j, |
| 215 | + k=slice_k, |
| 216 | + ) |
| 217 | + return new_view_child, random.random() |
| 218 | + |
| 219 | + |
| 220 | +if __name__ == "__main__": |
| 221 | + app.run_server(debug=True) |
0 commit comments