Plot a scalar finite element function using warp by scalar.
()
| 52 | |
| 53 | |
| 54 | def plot_scalar(): |
| 55 | """Plot a scalar finite element function using warp by scalar.""" |
| 56 | # We start by creating a unit square mesh and interpolating a |
| 57 | # function into a degree 1 Lagrange space |
| 58 | msh = create_unit_square( |
| 59 | MPI.COMM_WORLD, 12, 12, cell_type=CellType.quadrilateral, dtype=np.float64 |
| 60 | ) |
| 61 | V = functionspace(msh, ("Lagrange", 1)) |
| 62 | u = Function(V, dtype=np.float64) |
| 63 | u.interpolate(lambda x: np.sin(np.pi * x[0]) * np.sin(2 * x[1] * np.pi)) |
| 64 | |
| 65 | # To visualize the function u, we create a VTK-compatible grid to |
| 66 | # values of u to |
| 67 | cells, types, x = plot.vtk_mesh(V) |
| 68 | grid = pyvista.UnstructuredGrid(cells, types, x) |
| 69 | grid.point_data["u"] = u.x.array |
| 70 | |
| 71 | # The function "u" is set as the active scalar for the mesh, and |
| 72 | # warp in z-direction is set |
| 73 | grid.set_active_scalars("u") |
| 74 | warped = grid.warp_by_scalar() |
| 75 | |
| 76 | # A plotting window is created with two sub-plots, one of the scalar |
| 77 | # values and the other of the mesh is warped by the scalar values in |
| 78 | # z-direction |
| 79 | subplotter = pyvista.Plotter(shape=(1, 2)) |
| 80 | subplotter.subplot(0, 0) |
| 81 | subplotter.add_text("Scalar contour field", font_size=14, color="black", position="upper_edge") |
| 82 | subplotter.add_mesh(grid, show_edges=True, show_scalar_bar=True) |
| 83 | subplotter.view_xy() |
| 84 | |
| 85 | subplotter.subplot(0, 1) |
| 86 | subplotter.add_text("Warped function", position="upper_edge", font_size=14, color="black") |
| 87 | sargs = dict( |
| 88 | height=0.8, |
| 89 | width=0.1, |
| 90 | vertical=True, |
| 91 | position_x=0.05, |
| 92 | position_y=0.05, |
| 93 | fmt="%1.2e", |
| 94 | title_font_size=40, |
| 95 | color="black", |
| 96 | label_font_size=25, |
| 97 | ) |
| 98 | subplotter.set_position([-3, 2.6, 0.3]) |
| 99 | subplotter.set_focus([3, -1, -0.15]) |
| 100 | subplotter.set_viewup([0, 0, 1]) |
| 101 | subplotter.add_mesh(warped, show_edges=True, scalar_bar_args=sargs) |
| 102 | if pyvista.OFF_SCREEN: |
| 103 | subplotter.screenshot( |
| 104 | out_folder / "2D_function_warp.png", |
| 105 | transparent_background=transparent, |
| 106 | window_size=[figsize, figsize], |
| 107 | ) |
| 108 | else: |
| 109 | subplotter.show() |
| 110 | |
| 111 |
no test coverage detected