.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/Spinodoids/spinodoid.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_Spinodoids_spinodoid.py: Spinodoid ========= Spinodoids mimic the bicontinuous microstructures of spinodal decomposition. A :class:`microgen.Spinodoid` is a Gaussian random field: an FFT of white noise filtered by a Gaussian shell of radius ``k0`` (the dominant wave number) and width ``bandwidth``. The solid is the part where the field is above a threshold; giving ``density`` makes microgen find the threshold that matches this solid volume fraction. Each kept Fourier mode lives on the reciprocal lattice of the cell, so the field is exactly periodic: the spinodoid is a ready-made periodic RVE. .. GENERATED FROM PYTHON SOURCE LINES 15-21 .. code-block:: Python import numpy as np import pyvista as pv from microgen import Phase, Spinodoid .. GENERATED FROM PYTHON SOURCE LINES 23-25 An isotropic spinodoid with 50 % of solid. ``seed`` makes the random field reproducible. .. GENERATED FROM PYTHON SOURCE LINES 25-29 .. code-block:: Python spinodoid = Spinodoid(k0=30.0, bandwidth=4.0, density=0.5, resolution=64, seed=1) volume_fraction = spinodoid.grid_solid.volume / np.prod(spinodoid.cell_size) print(f"Solid volume fraction: {volume_fraction:.3f}") .. rst-class:: sphx-glr-script-out .. code-block:: none Solid volume fraction: 0.511 .. GENERATED FROM PYTHON SOURCE LINES 30-31 The field is periodic: its values on opposite faces of the cell match. .. GENERATED FROM PYTHON SOURCE LINES 31-36 .. code-block:: Python y, z = np.meshgrid(np.linspace(0, 1, 20), np.linspace(0, 1, 20)) left = spinodoid.evaluate(np.zeros(y.size), y.ravel(), z.ravel()) right = spinodoid.evaluate(np.ones(y.size), y.ravel(), z.ravel()) print(f"Max gap between the x = 0 and x = 1 faces: {np.abs(left - right).max():.2e}") .. rst-class:: sphx-glr-script-out .. code-block:: none Max gap between the x = 0 and x = 1 faces: 2.39e-16 .. GENERATED FROM PYTHON SOURCE LINES 37-40 A field-backed :class:`microgen.Phase` gives the connected components and the moments without CAD. At 50 % both phases percolate: nearly all the solid is one connected piece. .. GENERATED FROM PYTHON SOURCE LINES 40-49 .. code-block:: Python phase = Phase.from_shape(spinodoid, resolution=64) volumes = sorted((piece.volume for piece in phase.pieces), reverse=True) print(f"Pieces: {len(volumes)}, largest holds {volumes[0] / sum(volumes):.1%}") print(f"Centre of mass: {phase.center_of_mass}") surface = spinodoid.generate_surface_mesh() surface.save("spinodoid.stl") surface.plot(color="#ef7d2d", smooth_shading=True) .. tab-set:: .. tab-item:: Static Scene .. image-sg:: /examples/Spinodoids/images/sphx_glr_spinodoid_001.png :alt: spinodoid :srcset: /examples/Spinodoids/images/sphx_glr_spinodoid_001.png :class: sphx-glr-single-img .. tab-item:: Interactive Scene .. offlineviewer:: /home/runner/work/microgen/microgen/docs/examples/Spinodoids/images/sphx_glr_spinodoid_001.vtksz .. rst-class:: sphx-glr-script-out .. code-block:: none Pieces: 2, largest holds 100.0% Centre of mass: [0.50079827 0.50099002 0.50163997] /home/runner/work/microgen/microgen/microgen/shape/spinodoid.py:216: PyVistaFutureWarning: The default value of `algorithm` for the filter `UnstructuredGrid.extract_surface` will change in the future. It currently defaults to `'dataset_surface'`, but will change to `None`. Explicitly set the `algorithm` keyword to silence this warning. return self.grid_solid.extract_surface().clean().triangulate() .. GENERATED FROM PYTHON SOURCE LINES 50-54 ``anisotropy`` stretches the wave vectors along each axis before filtering. A large factor along an axis removes the modes with a component along it: ``(1, 1, 3)`` gives columns along z, ``(3, 3, 1)`` gives lamellae normal to z. .. GENERATED FROM PYTHON SOURCE LINES 54-71 .. code-block:: Python variants = { "isotropic": spinodoid, "columnar (1, 1, 3)": Spinodoid( k0=30.0, bandwidth=4.0, density=0.5, anisotropy=(1, 1, 3), resolution=64, seed=1 ), "lamellar (3, 3, 1)": Spinodoid( k0=30.0, bandwidth=4.0, density=0.5, anisotropy=(3, 3, 1), resolution=64, seed=1 ), } pl = pv.Plotter(shape=(1, 3), window_size=(1500, 500)) for i, (name, shape) in enumerate(variants.items()): pl.subplot(0, i) pl.add_text(name, font_size=12) pl.add_mesh(shape.generate_surface_mesh(), color="#ef7d2d", smooth_shading=True) pl.link_views() pl.show() .. image-sg:: /examples/Spinodoids/images/sphx_glr_spinodoid_002.png :alt: spinodoid :srcset: /examples/Spinodoids/images/sphx_glr_spinodoid_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 13.410 seconds) .. _sphx_glr_download_examples_Spinodoids_spinodoid.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: spinodoid.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: spinodoid.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: spinodoid.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_