Debian Codegraph @

examples/99-advanced/magnetic_fields.py

raw
1 """
2 .. _magnetic_fields_example:
3
4 Plot a Magnetic Field
5 ---------------------
6
7 The following example demonstrates how PyVista can be used to plot a magnetic
8 field.
9
10 This example relies on :func:`~pyvista.DataSetFilters.streamlines_from_source` to
11 generate streamlines and :func:`add_volume() <pyvista.Plotter.add_volume>` to plot
12 the strength of the magnetic field.
13
14 This dataset was created from the `Coil Field Lines
15 <https://magpylib.readthedocs.io/en/stable/_pages/user_guide/examples/examples_app_coils.html>`_
16 example from the awesome `magpylib <https://github.com/magpylib/magpylib>`_
17 library.
18
19 """
20
21 # sphinx_gallery_thumbnail_number = 3
22 import numpy as np
23 import pyvista as pv
24 from pyvista import examples
25
26 # %%
27 # Download the DataSet
28 # ~~~~~~~~~~~~~~~~~~~~
29 # Let's first download the example dataset and show that it's a
30 # :class:`pyvista.ImageData` with the magnetic field stored as the ``'B'``
31 # array in ``point_data``.
32
33 grid = examples.download_coil_magnetic_field()
34 grid.point_data
35
36
37 # %%
38 # Create Coils
39 # ~~~~~~~~~~~~
40 # Create several hoops to represent the coil. This matches the geometry in the
41 # original example.
42
43 coils = [
44 pv.Polygon(center=(0, 0, z), radius=5, n_sides=100, fill=False)
45 for z in np.linspace(-8, 8, 16)
46 ]
47 coil_block = pv.MultiBlock(coils)
48 coil_block.plot(render_lines_as_tubes=True, line_width=10)
49
50
51 # %%
52 # Compute and Plot Field Lines
53 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
54 # Next, let's compute streamlines from the center of the coil to represent the
55 # direction of the magnetic force. For this, we can create a simple
56 # :func:`pyvista.Disc` and use that as the source of the streamlines.
57
58 seed = pv.Disc(inner=1, outer=5.4, r_res=2, c_res=12)
59 strl = grid.streamlines_from_source(
60 seed,
61 vectors='B',
62 max_length=180,
63 initial_step_length=0.1,
64 integration_direction='both',
65 )
66
67 pl = pv.Plotter()
68 pl.add_mesh(
69 strl.tube(radius=0.1),
70 cmap='bwr',
71 ambient=0.2,
72 )
73 pl.add_mesh(coil_block, render_lines_as_tubes=True, line_width=5, color='w')
74 pl.camera.zoom(3)
75 pl.show()
76
77
78 # %%
79 # Plot the Magnet Field Strength
80 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
81 # Finally, let's bring this all together by plotting the magnetic field
82 # strength while also plotting the streamlines and the coil.
83
84 # Take the norm of the magnetic field
85 scalars = np.linalg.norm(grid['B'], axis=1)
86
87 # Customize the opacity to make it easier to visualize the strength of the
88 # field nearby the coil
89 opacity = [
90 0.00,
91 0.05,
92 0.1,
93 0.3,
94 0.3,
95 0.5,
96 0.95,
97 0.95,
98 1.0,
99 1.0,
100 ]
101
102 # sphinx_gallery_start_ignore
103 # volume rendering does not work in interactive plots currently
104 PYVISTA_GALLERY_FORCE_STATIC = True
105 # sphinx_gallery_end_ignore
106
107 # Add this all to the plotter
108 pl = pv.Plotter()
109 pl.add_mesh(
110 strl.tube(radius=0.1),
111 color='black',
112 )
113 pl.add_mesh(coil_block, render_lines_as_tubes=True, line_width=5, color='w')
114 vol = pl.add_volume(
115 grid,
116 scalars=scalars,
117 opacity=opacity,
118 cmap='hot',
119 show_scalar_bar=False,
120 )
121 vol.prop.interpolation_type = 'linear'
122 pl.camera.zoom(5)
123 pl.show()