Debian Codegraph @

examples/99-advanced/openfoam_cooling.py

raw
1 """
2 .. _openfoam_cooling_example:
3
4 Electronics Cooling CFD
5 -----------------------
6 Plot an electronics cooling CFD example from OpenFoam hosted on the public
7 SimScale examples at `SimScale Project Library
8 <https://www.simscale.com/projects/>`_ and generated from the `Thermal
9 Management Tutorial: CHT Analysis of an Electronics Box
10 <https://www.simscale.com/docs/tutorials/thermal-management-cht-analysis-electronics-box/>`_.
11
12 This example dataset was read using the :class:`pyvista.POpenFOAMReader` and
13 post processed according to this `README.md
14 <https://github.com/pyvista/data/blob/master/Data/fvm/cooling_electronics/README.md>`_.
15
16 """
17
18 import numpy as np
19 import pyvista as pv
20 from pyvista import examples
21
22 # %%
23 # Load the Datasets
24 # ~~~~~~~~~~~~~~~~~
25 # Download and load the datasets.
26 #
27 # The ``structure`` dataset consists of a box with several components, being
28 # cooled down by a fan, while the ``air`` dataset is the air, containing
29 # several scalar arrays including the velocity and temperature of the air.
30
31 structure, air = examples.download_electronics_cooling()
32 structure, air
33
34
35 # %%
36 # Plot the Electronics
37 # ~~~~~~~~~~~~~~~~~~~~
38 # Here we plot the temperature of the electronics using the ``"reds"`` colormap
39 # and improve the look of the plot using surface space ambient occlusion with
40 # :func:`enable_ssao() <pyvista.Plotter.enable_ssao>`.
41
42 pl = pv.Plotter()
43 pl.enable_ssao(radius=0.01)
44 pl.add_mesh(
45 structure,
46 scalars='T',
47 smooth_shading=True,
48 split_sharp_edges=True,
49 cmap='reds',
50 ambient=0.2,
51 )
52 pl.enable_anti_aliasing('fxaa') # also try 'ssaa'
53 pl.show()
54
55
56 # %%
57 # Plot Air Velocity
58 # ~~~~~~~~~~~~~~~~~
59 # Let's plot the velocity of the air.
60 #
61 # Start by clipping the air dataset with :func:`clip()
62 # <pyvista.DataObjectFilters.clip>` and plotting it alongside the electronics.
63 #
64 # As you can see, the air enters from the front of the case (left) and is being
65 # pushed out of the "back" of the case via a fan.
66
67 # Clip the air in the XY plane
68 z_slice = air.clip('z', value=-0.005)
69
70 # Plot it
71 pl = pv.Plotter()
72 pl.enable_ssao(radius=0.01)
73 pl.add_mesh(z_slice, scalars='U', lighting=False, scalar_bar_args={'title': 'Velocity'})
74 pl.add_mesh(structure, color='w', smooth_shading=True, split_sharp_edges=True)
75 pl.camera_position = 'xy'
76 pl.camera.roll = 90
77 pl.enable_anti_aliasing('fxaa')
78 pl.show()
79
80
81 # %%
82 # Plot Air Temperature
83 # ~~~~~~~~~~~~~~~~~~~~
84 # Let's also plot the temperature of the air. This time, let's also plot the
85 # temperature of the components.
86
87 pl = pv.Plotter()
88 pl.enable_ssao(radius=0.01)
89 pl.add_mesh(
90 z_slice,
91 scalars='T',
92 lighting=False,
93 scalar_bar_args={'title': 'Temperature'},
94 cmap='reds',
95 )
96 pl.add_mesh(
97 structure,
98 scalars='T',
99 smooth_shading=True,
100 split_sharp_edges=True,
101 cmap='reds',
102 show_scalar_bar=False,
103 )
104 pl.camera_position = 'xy'
105 pl.camera.roll = 90
106 pl.enable_anti_aliasing('fxaa')
107 pl.show()
108
109
110 # %%
111 # Plot Streamlines - Flow Velocity
112 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
113 # Now, let's plot the streamlines of this dataset so we can see how the air is
114 # flowing through the case.
115 #
116 # Generate streamlines using :func:`streamlines_from_source()
117 # <pyvista.DataSetFilters.streamlines_from_source>`.
118
119 # Have our streamlines start from the regular openings of the case.
120 points = []
121 for x in np.linspace(0.045, 0.105, 7, endpoint=True):
122 points.extend([x, 0.2, z] for z in np.linspace(0, 0.03, 5))
123 points = pv.PointSet(points)
124 lines = air.streamlines_from_source(points, max_length=2.0)
125
126 # Plot
127 pl = pv.Plotter()
128 pl.enable_ssao(radius=0.01)
129 pl.add_mesh(
130 lines,
131 line_width=2,
132 scalars='T',
133 cmap='reds',
134 scalar_bar_args={'title': 'Temperature'},
135 )
136 pl.add_mesh(
137 structure,
138 scalars='T',
139 smooth_shading=True,
140 split_sharp_edges=True,
141 cmap='reds',
142 show_scalar_bar=False,
143 )
144 pl.camera_position = 'xy'
145 pl.camera.roll = 90
146 pl.enable_anti_aliasing('fxaa') # also try 'ssaa'
147 pl.show()
148
149
150 # %%
151 # Volumetric Plot - Visualize High Temperatures
152 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
153 # Show a 3D plot of areas of temperature.
154 #
155 # For this example, we will first sample the results from the
156 # :class:`pyvista.UnstructuredGrid` onto a :class:`pyvista.ImageData` using
157 # :func:`sample() <pyvista.DataObjectFilters.sample>`. This is so we can visualize
158 # it using :func:`add_volume() <pyvista.Plotter.add_volume>`
159
160
161 # sphinx_gallery_start_ignore
162 # volume rendering does not work in interactive plots currently
163 PYVISTA_GALLERY_FORCE_STATIC = True
164 # sphinx_gallery_end_ignore
165
166 bounds = np.array(air.bounds) * 1.2
167 origin = (bounds[0], bounds[2], bounds[4])
168 spacing = (0.002, 0.002, 0.002)
169 dimensions = (
170 int((bounds[1] - bounds[0]) // spacing[0] + 2),
171 int((bounds[3] - bounds[2]) // spacing[1] + 2),
172 int((bounds[5] - bounds[4]) // spacing[2] + 2),
173 )
174 grid = pv.ImageData(dimensions=dimensions, spacing=spacing, origin=origin)
175 grid = grid.sample(air)
176
177 opac = np.zeros(20)
178 opac[1:] = np.geomspace(1e-7, 0.1, 19)
179 opac[-5:] = [0.05, 0.1, 0.5, 0.5, 0.5]
180
181 pl = pv.Plotter()
182 pl.add_mesh(structure, color='w', smooth_shading=True, split_sharp_edges=True)
183 vol = pl.add_volume(
184 grid,
185 scalars='T',
186 opacity=opac,
187 cmap='autumn_r',
188 show_scalar_bar=True,
189 scalar_bar_args={'title': 'Temperature'},
190 )
191 vol.prop.interpolation_type = 'linear'
192 pl.camera.zoom(2)
193 pl.show()