.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/plot_03_volume_sampling.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_plot_03_volume_sampling.py: .. _random_volume_sampling_example: Random Sampling in a Volume ~~~~~~~~~~~~~~~~~~~~~~~~~~~ This example inspired by the discussion in `this PyVista discussion `_. Using ``pyransame`` makes the solution easier and handles more complex scenarios. This example is related to post-processing CFD data with a Eulerian description of particle density. .. GENERATED FROM PYTHON SOURCE LINES 13-19 .. code-block:: Python import numpy as np import pyvista as pv import pyransame .. GENERATED FROM PYTHON SOURCE LINES 20-26 This example shows how ``pyransame`` can be used to sampled from datasets that have volume. There is a cube of dimension 10 m x 10 m x 10 m with particles described by a volume fraction in each cell. In this synthetic dataset, the particles tend to float and are located near the top (z-position). ``cell_centers`` is used as cell_data is required for weighted sampling. .. GENERATED FROM PYTHON SOURCE LINES 26-31 .. code-block:: Python mesh = pv.ImageData(dimensions=(10, 10, 10)) mesh.cell_data["volume_frac"] = np.exp(mesh.cell_centers().points[:, 2]) mesh.cell_data["volume_frac"] /= np.sum(mesh["volume_frac"]) .. GENERATED FROM PYTHON SOURCE LINES 32-35 The particles have an age related to some history of the particle motion, in this case related to the y-position. Cell data is not required here as the point data can be interpolated onto the sampled points. .. GENERATED FROM PYTHON SOURCE LINES 35-38 .. code-block:: Python mesh["age"] = mesh.points[:, 1] .. GENERATED FROM PYTHON SOURCE LINES 39-41 Sample the points with respect to the volume fraction to obtain a realistic particle distribution in the domain. .. GENERATED FROM PYTHON SOURCE LINES 41-44 .. code-block:: Python points = pyransame.random_volume_dataset(mesh, 1000, weights="volume_frac") .. GENERATED FROM PYTHON SOURCE LINES 45-47 In this example, we also know the particles diameters come from a normal distribution that does not depend on the other particle attributes. .. GENERATED FROM PYTHON SOURCE LINES 47-51 .. code-block:: Python diameters = 0.03 * np.random.randn(1000) + 0.4 points["diameter"] = diameters .. GENERATED FROM PYTHON SOURCE LINES 52-53 Plot. .. GENERATED FROM PYTHON SOURCE LINES 53-59 .. code-block:: Python cpos = [[32.0, 16.0, 10.0], [5.0, 3.9, 3.6], [-0.21, -0.076, 0.97]] pl = pv.Plotter() pl.add_mesh(mesh, style="wireframe") spheres = points.glyph(geom=pv.Sphere(), scale="diameter", orient=False) pl.add_mesh(spheres, scalars="age") pl.show() .. tab-set:: .. tab-item:: Static Scene .. image-sg:: /examples/images/sphx_glr_plot_03_volume_sampling_001.png :alt: plot 03 volume sampling :srcset: /examples/images/sphx_glr_plot_03_volume_sampling_001.png :class: sphx-glr-single-img .. tab-item:: Interactive Scene .. offlineviewer:: /home/runner/work/pyransame/pyransame/doc/examples/images/sphx_glr_plot_03_volume_sampling_001.vtksz .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 7.930 seconds) .. _sphx_glr_download_examples_plot_03_volume_sampling.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_volume_sampling.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_volume_sampling.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_