Debian Codegraph @

examples/99-advanced/antarctica.py

raw
1 """
2 .. _antarctica_example:
3
4 Compare Field Across Mesh Regions
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
7 Here is some velocity data from a glacier modelling simulation that is compared
8 across nodes in the simulation. We have simplified the mesh to have the
9 simulation node value already on the mesh.
10
11 This was originally posted to `pyvista/pyvista-support#83 <https://github.com/pyvista/pyvista-support/issues/83>`_.
12
13 The modeling results are courtesy of `Urruty Benoit <https://github.com/BenoitURRUTY>`_
14 and are from the `Elmer/Ice <http://elmerice.elmerfem.org>`_ simulation
15 software.
16
17 """
18
19 import numpy as np
20
21 # sphinx_gallery_thumbnail_number = 2
22
23 # sphinx_gallery_start_ignore
24 # labels are not supported in vtk-js
25 PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True
26 # sphinx_gallery_end_ignore
27
28 import pyvista as pv
29 from pyvista import examples
30
31 # Load the sample data :func:`~pyvista.examples.downloads.download_antarctica_velocity`
32 mesh = examples.download_antarctica_velocity()
33 mesh['magnitude'] = np.linalg.norm(mesh['ssavelocity'], axis=1)
34 mesh
35
36 # %%
37 # Here is a helper to extract regions of the mesh based on the simulation node.
38
39
40 def extract_node(node):
41 idx = mesh['node_value'] == node
42 return mesh.extract_points(idx)
43
44
45 # %%
46
47 pl = pv.Plotter()
48 pl.add_mesh(mesh, scalars='node_value')
49 for node in np.unique(mesh['node_value']):
50 loc = extract_node(node).center
51 pl.add_point_labels(loc, [f'Node {node}'])
52 pl.show(cpos='xy')
53
54
55 # %%
56
57 vel_dargs = dict(scalars='magnitude', clim=[1e-3, 1e4], cmap='Blues', log_scale=True)
58
59 mesh.plot(cpos='xy', **vel_dargs)
60
61 # %%
62
63 a = extract_node(12)
64 b = extract_node(20)
65
66 # %%
67
68 pl = pv.Plotter()
69 pl.add_mesh(a, **vel_dargs)
70 pl.add_mesh(b, **vel_dargs)
71 pl.show(cpos='xy')
72
73 # %%
74 # plot vectors without mesh
75
76 pl = pv.Plotter()
77 pl.add_mesh(a.glyph(orient='ssavelocity', factor=20), **vel_dargs)
78 pl.add_mesh(b.glyph(orient='ssavelocity', factor=20), **vel_dargs)
79 pl.camera_position = pv.CameraPosition(
80 position=(-1114684.6969340036, 293863.65389149904, 752186.603224546),
81 focal_point=(-1114684.6969340036, 293863.65389149904, 0.0),
82 viewup=(0.0, 1.0, 0.0),
83 )
84 pl.show()
85
86
87 # %%
88 # Compare directions. Normalize them so we can get a reasonable direction
89 # comparison.
90
91 flow_a = a.point_data['ssavelocity'].copy()
92 flow_a /= np.linalg.norm(flow_a, axis=1).reshape(-1, 1)
93 flow_b = b.point_data['ssavelocity'].copy()
94 flow_b /= np.linalg.norm(flow_b, axis=1).reshape(-1, 1)
95
96
97 # plot normalized vectors
98 pl = pv.Plotter()
99 pl.add_arrows(a.points, flow_a, mag=10000, color='b', label='flow_a')
100 pl.add_arrows(b.points, flow_b, mag=10000, color='r', label='flow_b')
101 pl.add_legend()
102 pl.camera_position = pv.CameraPosition(
103 position=(-1044239.3240694795, 354805.0268606294, 484178.24825854995),
104 focal_point=(-1044239.3240694795, 354805.0268606294, 0.0),
105 viewup=(0.0, 1.0, 0.0),
106 )
107 pl.show()
108
109
110 # %%
111 # flow_a that agrees with the mean flow path of flow_b
112 agree = flow_a.dot(flow_b.mean(0))
113
114 pl = pv.Plotter()
115 pl.add_mesh(
116 a, scalars=agree, cmap='bwr', scalar_bar_args={'title': 'Flow agreement with block b'}
117 )
118 pl.add_mesh(b, color='w')
119 pl.show(cpos='xy')
120
121 # %%
122 agree = flow_b.dot(flow_a.mean(0))
123
124 pl = pv.Plotter()
125 pl.add_mesh(a, color='w')
126 pl.add_mesh(
127 b, scalars=agree, cmap='bwr', scalar_bar_args={'title': 'Flow agreement with block a'}
128 )
129 pl.show(cpos='xy')