|
64 | 64 | },
|
65 | 65 | {
|
66 | 66 | "cell_type": "code",
|
67 |
| - "execution_count": 2, |
| 67 | + "execution_count": 1, |
68 | 68 | "metadata": {},
|
69 | 69 | "outputs": [],
|
70 | 70 | "source": [
|
|
95 | 95 | },
|
96 | 96 | {
|
97 | 97 | "cell_type": "code",
|
98 |
| - "execution_count": 3, |
| 98 | + "execution_count": 2, |
99 | 99 | "metadata": {},
|
100 | 100 | "outputs": [],
|
101 | 101 | "source": [
|
|
126 | 126 | },
|
127 | 127 | {
|
128 | 128 | "cell_type": "code",
|
129 |
| - "execution_count": 4, |
| 129 | + "execution_count": 3, |
130 | 130 | "metadata": {},
|
131 | 131 | "outputs": [],
|
132 | 132 | "source": [
|
|
147 | 147 | },
|
148 | 148 | {
|
149 | 149 | "cell_type": "code",
|
150 |
| - "execution_count": 5, |
| 150 | + "execution_count": 4, |
151 | 151 | "metadata": {},
|
152 | 152 | "outputs": [],
|
153 | 153 | "source": [
|
|
176 | 176 | },
|
177 | 177 | {
|
178 | 178 | "cell_type": "code",
|
179 |
| - "execution_count": 6, |
| 179 | + "execution_count": 5, |
180 | 180 | "metadata": {},
|
181 | 181 | "outputs": [],
|
182 | 182 | "source": [
|
183 |
| - "from dolfinx.fem import locate_dofs_topological, DirichletBC\n", |
| 183 | + "from dolfinx.fem import locate_dofs_topological, dirichletbc\n", |
184 | 184 | "boundary_dofs = locate_dofs_topological(V, fdim, boundary_facets)\n",
|
185 |
| - "bc = DirichletBC(uD, boundary_dofs)" |
| 185 | + "bc = dirichletbc(uD, boundary_dofs)" |
186 | 186 | ]
|
187 | 187 | },
|
188 | 188 | {
|
|
199 | 199 | },
|
200 | 200 | {
|
201 | 201 | "cell_type": "code",
|
202 |
| - "execution_count": 7, |
| 202 | + "execution_count": 6, |
203 | 203 | "metadata": {},
|
204 | 204 | "outputs": [],
|
205 | 205 | "source": [
|
|
218 | 218 | },
|
219 | 219 | {
|
220 | 220 | "cell_type": "code",
|
221 |
| - "execution_count": 8, |
| 221 | + "execution_count": 7, |
222 | 222 | "metadata": {},
|
223 | 223 | "outputs": [],
|
224 | 224 | "source": [
|
|
241 | 241 | },
|
242 | 242 | {
|
243 | 243 | "cell_type": "code",
|
244 |
| - "execution_count": 9, |
| 244 | + "execution_count": 8, |
245 | 245 | "metadata": {},
|
246 | 246 | "outputs": [],
|
247 | 247 | "source": [
|
|
277 | 277 | },
|
278 | 278 | {
|
279 | 279 | "cell_type": "code",
|
280 |
| - "execution_count": 10, |
| 280 | + "execution_count": 9, |
281 | 281 | "metadata": {},
|
282 | 282 | "outputs": [],
|
283 | 283 | "source": [
|
|
297 | 297 | },
|
298 | 298 | {
|
299 | 299 | "cell_type": "code",
|
300 |
| - "execution_count": 11, |
| 300 | + "execution_count": 10, |
301 | 301 | "metadata": {},
|
302 | 302 | "outputs": [],
|
303 | 303 | "source": [
|
|
316 | 316 | },
|
317 | 317 | {
|
318 | 318 | "cell_type": "code",
|
319 |
| - "execution_count": 12, |
| 319 | + "execution_count": 11, |
320 | 320 | "metadata": {},
|
321 | 321 | "outputs": [],
|
322 | 322 | "source": [
|
323 |
| - "from dolfinx.fem import assemble_scalar\n", |
324 |
| - "L2_error = ufl.inner(uh - uex, uh - uex) * ufl.dx\n", |
| 323 | + "from dolfinx.fem import assemble_scalar, form\n", |
| 324 | + "L2_error = form(ufl.inner(uh - uex, uh - uex) * ufl.dx)\n", |
325 | 325 | "error_local = assemble_scalar(L2_error)\n",
|
326 | 326 | "error_L2 = numpy.sqrt(mesh.comm.allreduce(error_local, op=MPI.SUM))"
|
327 | 327 | ]
|
|
340 | 340 | },
|
341 | 341 | {
|
342 | 342 | "cell_type": "code",
|
343 |
| - "execution_count": 13, |
| 343 | + "execution_count": 12, |
344 | 344 | "metadata": {},
|
345 | 345 | "outputs": [
|
346 | 346 | {
|
|
374 | 374 | },
|
375 | 375 | {
|
376 | 376 | "cell_type": "code",
|
377 |
| - "execution_count": 14, |
| 377 | + "execution_count": 13, |
378 | 378 | "metadata": {},
|
379 | 379 | "outputs": [],
|
380 | 380 | "source": [
|
381 | 381 | "from dolfinx.plot import create_vtk_topology\n",
|
382 | 382 | "topology, cell_types = create_vtk_topology(mesh, mesh.topology.dim)"
|
383 | 383 | ]
|
384 | 384 | },
|
| 385 | + { |
| 386 | + "cell_type": "markdown", |
| 387 | + "metadata": {}, |
| 388 | + "source": [ |
| 389 | + "There are several backends that can be used with pyvista, and they have different benefits and drawbacks. See the [pyvista documentation](https://docs.pyvista.org/user-guide/jupyter/index.html) for more information and installation details. In this example and the rest of the tutorial we will use [ipygany](https://github.com/QuantStack/ipygany) and [pythreejs](https://github.com/jupyter-widgets/pythreejs)." |
| 390 | + ] |
| 391 | + }, |
385 | 392 | {
|
386 | 393 | "cell_type": "code",
|
387 |
| - "execution_count": 15, |
| 394 | + "execution_count": 14, |
388 | 395 | "metadata": {},
|
389 | 396 | "outputs": [],
|
390 | 397 | "source": [
|
391 | 398 | "import pyvista\n",
|
392 |
| - "grid = pyvista.UnstructuredGrid(topology, cell_types, mesh.geometry.x)" |
| 399 | + "pyvista.set_jupyter_backend(\"pythreejs\")" |
393 | 400 | ]
|
394 | 401 | },
|
395 | 402 | {
|
396 | 403 | "cell_type": "markdown",
|
397 | 404 | "metadata": {},
|
398 | 405 | "source": [
|
| 406 | + "We start by creating a pyvista grid the `vtk_topology` and the `mesh.geometry`.\n", |
399 | 407 | "Next, we attach data from our solution `uh` by computing the values of the function at each vertex."
|
400 | 408 | ]
|
401 | 409 | },
|
402 | 410 | {
|
403 | 411 | "cell_type": "code",
|
404 |
| - "execution_count": 16, |
| 412 | + "execution_count": 15, |
405 | 413 | "metadata": {},
|
406 | 414 | "outputs": [],
|
407 | 415 | "source": [
|
| 416 | + "grid = pyvista.UnstructuredGrid(topology, cell_types, mesh.geometry.x)\n", |
408 | 417 | "grid.point_data[\"u\"] = uh.compute_point_values().real\n",
|
409 | 418 | "grid.set_active_scalars(\"u\")"
|
410 | 419 | ]
|
|
414 | 423 | "metadata": {},
|
415 | 424 | "source": [
|
416 | 425 | "We can now use the `pyvista.Plotter` to visualize the solution. We visualize it by showing it in 2D and warped in 3D.\n",
|
417 |
| - "In the jupyter notebook environment, we use the default setting of `pyvista.OFF_SCREEN=False`, which will render plots directly in the notebook. . PYVITo render in the online notebooks or in docker containers without X-forwarding, we need to start a virtual framebuffer.\n", |
418 |
| - "```{admonition} Rendering with pyvista using Docker-containers\n", |
419 |
| - "To make the plots render, we have to call `pyvista.start_xvfb` to start a virtual framebuffer if using docker-containers or jupyter notebooks on Linux.\n", |
420 |
| - "```" |
| 426 | + "In the jupyter notebook environment, we use the default setting of `pyvista.OFF_SCREEN=False`, which will render plots directly in the notebook." |
421 | 427 | ]
|
422 | 428 | },
|
423 | 429 | {
|
424 | 430 | "cell_type": "code",
|
425 |
| - "execution_count": 17, |
| 431 | + "execution_count": 16, |
426 | 432 | "metadata": {},
|
427 | 433 | "outputs": [
|
428 | 434 | {
|
429 | 435 | "data": {
|
430 | 436 | "application/vnd.jupyter.widget-view+json": {
|
431 |
| - "model_id": "52f926f800b84003bd2eda5e14ba1871", |
| 437 | + "model_id": "026dd4bdc8354af88ecd23be302ee96c", |
432 | 438 | "version_major": 2,
|
433 | 439 | "version_minor": 0
|
434 | 440 | },
|
435 | 441 | "text/plain": [
|
436 |
| - "ViewInteractiveWidget(height=768, layout=Layout(height='auto', width='100%'), width=1024)" |
| 442 | + "Renderer(camera=PerspectiveCamera(aspect=1.3333333333333333, children=(DirectionalLight(color='#fefefe', inten…" |
437 | 443 | ]
|
438 | 444 | },
|
439 | 445 | "metadata": {},
|
440 | 446 | "output_type": "display_data"
|
441 | 447 | }
|
442 | 448 | ],
|
443 | 449 | "source": [
|
444 |
| - "pyvista.start_xvfb(wait=0.05)\n", |
445 | 450 | "plotter = pyvista.Plotter()\n",
|
446 |
| - "plotter.add_mesh(grid, show_edges=True, show_scalar_bar=True)\n", |
| 451 | + "plotter.add_mesh(grid, show_edges=True)\n", |
447 | 452 | "plotter.view_xy()\n",
|
448 | 453 | "if not pyvista.OFF_SCREEN:\n",
|
449 | 454 | " plotter.show()\n",
|
450 | 455 | "else:\n",
|
| 456 | + " pyvista.start_xvfb()\n", |
451 | 457 | " figure = plotter.screenshot(\"fundamentals.png\")"
|
452 | 458 | ]
|
453 | 459 | },
|
454 | 460 | {
|
455 | 461 | "cell_type": "markdown",
|
456 | 462 | "metadata": {},
|
457 | 463 | "source": [
|
458 |
| - "## Alternative plotting\n", |
459 |
| - "To create a more interactive plot using pyvista in a Jupyter notebook we us `pyvista.ITKPlotter` which uses `itkwidgets`. To modify the visualization, click on the three bars in the top left corner." |
| 464 | + "## ipygany\n", |
| 465 | + "We change plotting from `pythreejs` to `ipygany` by initializing another `Plotter` with `jupyter_backend=\"ipygany\"`. We also warp the mesh by scalar to make use of the 3D plotting." |
460 | 466 | ]
|
461 | 467 | },
|
462 | 468 | {
|
463 | 469 | "cell_type": "code",
|
464 |
| - "execution_count": 19, |
| 470 | + "execution_count": 17, |
465 | 471 | "metadata": {},
|
466 | 472 | "outputs": [
|
467 | 473 | {
|
468 | 474 | "data": {
|
469 | 475 | "application/vnd.jupyter.widget-view+json": {
|
470 |
| - "model_id": "6b86e726693d42f3b675cea1633f9136", |
| 476 | + "model_id": "2b14ac59f2cf4d129b2843ce038c4b90", |
471 | 477 | "version_major": 2,
|
472 | 478 | "version_minor": 0
|
473 | 479 | },
|
474 | 480 | "text/plain": [
|
475 |
| - "Viewer(geometries=[{'vtkClass': 'vtkPolyData', 'points': {'vtkClass': 'vtkPoints', 'name': '_points', 'numberO…" |
| 481 | + "AppLayout(children=(VBox(children=(HTML(value='<h3>u</h3>'), Dropdown(description='Colormap:', options={'BrBG'…" |
476 | 482 | ]
|
477 | 483 | },
|
478 | 484 | "metadata": {},
|
|
481 | 487 | ],
|
482 | 488 | "source": [
|
483 | 489 | "if not pyvista.OFF_SCREEN:\n",
|
484 |
| - " plotter_itk = pyvista.PlotterITK()\n", |
485 |
| - " warped = grid.warp_by_scalar(\"u\", factor=0.5)\n", |
486 |
| - " plotter_itk.add_mesh(warped)\n", |
487 |
| - " plotter_itk.show(True)" |
| 490 | + " warped = grid.warp_by_scalar()\n", |
| 491 | + " plotter2 = pyvista.Plotter()\n", |
| 492 | + " plotter2.add_mesh(warped, show_edges=True, show_scalar_bar=True)\n", |
| 493 | + " plotter2.show(jupyter_backend=\"ipygany\")" |
488 | 494 | ]
|
489 | 495 | },
|
490 | 496 | {
|
|
497 | 503 | },
|
498 | 504 | {
|
499 | 505 | "cell_type": "code",
|
500 |
| - "execution_count": 20, |
| 506 | + "execution_count": 18, |
501 | 507 | "metadata": {},
|
502 | 508 | "outputs": [],
|
503 | 509 | "source": [
|
|
0 commit comments