.. _ex_foam:

====
Foam
====

In this example, a foam microstructure is generated by first tesselating the
voids, then adding foam material to the edges between the voids.

Python Script
=============

The basename for this file is ``foam.py``.
The file can be run using this command::

    microstructpy --demo=foam.py

The full text of the script is:

.. literalinclude:: ../../../../src/microstructpy/examples/foam.py
    :language: python

Domain
======

The domain of the microstructure is a :class:`.Square`.
Without arguments, the square's center is (0, 0) and side length is 15.

Seeds
=====

Initially, the seed list is entirely voids following a lognormal size
distribution.
These are then tessellated to determine the boundaries between the voids.
These voids are generated to fill 70% of the domain and are positioned with
a custom ``rtol`` value of 3%.
This ensures that most of the voids do not connect with each other and that
the foam seeds positioned along the edges do not become consumed by the void
cells.

Next, foam seeds are added to the edges between voids.
These seeds are given a size distribution, however there are no foam grains
since the material is amorphous.
Once the foam seeds are positioned in the domain, the lists of void and foam
seeds are combined into a single seed list.

Polygon Mesh
============

A polygon mesh is created from the list of seed points using the
:func:`~microstructpy.meshing.PolyMesh.from_seeds` class method.

Triangular Mesh
===============

A triangular mesh is created from the polygonal mesh using the
:func:`~microstructpy.meshing.TriMesh.from_polymesh` class method.
The optional ``phases`` parameter is used in this case since the mesh contains
non-crystalline materials.
Additionally, the minimum interior angle of the mesh elements is set to 20 to
ensure good mesh quality and the maximum edge length is set to increase mesh
resolution near the voids.

Plotting
========

The triangular mesh in this example is plotted in aquamarine, one of
several named colors in matplotlib.
Next, the axes are turned off and the limits are set to equal the bounds of
the domain.
Finally, the triangular mesh is saved as a PNG and as a PDF, with the
resulting plot shown in :numref:`f_ex_foam_tri`.

.. _f_ex_foam_tri:
.. figure:: ../../../../src/microstructpy/examples/foam/trimesh.png

  Foam example - triangular mesh.
