.. _Auto_Meshing_Internal_Flow:

.. currentmodule:: flow360

Automated Meshing for Internal Flow
====================================

This tutorial shows how to create a mesh for internal flow using the :ref:`automated meshing<automatedMeshing>` process.

Geometry
---------

As shown in :numref:`sphere_in_wind_tunnel_geometry`, the geometry is a wind tunnel enclosing a sphere that is supported by four cylindrical struts. The `corresponding CSM file <https://simcloud-public-1.s3.amazonaws.com/tutorials/auto_meshing_internal_flow/internalFlow.csm>`_ is provided, so the readers can easily reconstruct the geometry on their local machines.

Several details are worth emphasizing in the CSM file:

1. For internal flow, a single solid body representing the fluid must be provided to the automated meshing process. To do this, once the wind tunnel (box) and the sphere with struts are constructed, the next step is to :code:`subtract` the sphere and struts from the wind tunnel.
2. :numref:`sphere_in_wind_tunnel_geometry` shows the :code:`faceName` and :code:`groupName` attributes on the faces of the wind tunnel. In general faces with the same :code:`faceName` will be treated as the same surface patch for assigning surface (maxEdgeLength) and volume (firstLayerThickness) mesh attributes. Conversely, the user can set different surface and volume mesh attributes for faces with a different :code:`faceName`. Faces with the same :code:`groupName` will be considered as the same boundary when exporting the CGNS file.

.. _sphere_in_wind_tunnel_geometry:
.. figure:: Figures/geometry.svg
   :align: center

   Sphere in wind tunnel, geometry with face attributes. :code:`sideNegY` is hidden to show the interior of the wind tunnel.

Meshing
-------

The meshing parameters is set up as follows:

.. code-block:: python
   :linenos:

    geometry.group_faces_by_tag("faceName")
    farfield = fl.UserDefinedFarfield(name="farfield")
    params = fl.SimulationParams(
        meshing=fl.MeshingParams(
            defaults=fl.MeshingDefaults(
                surface_max_edge_length=1.2 * fl.u.m,
                curvature_resolution_angle=15 * fl.u.deg,
                surface_edge_growth_rate=1.2,
                boundary_layer_first_layer_thickness=1e-6,
                boundary_layer_growth_rate=1.2,
            ),
            refinement_factor=1.0,
            volume_zones=[farfield],
            refinements=[
                fl.SurfaceRefinement(
                    name="sphere", max_edge_length=0.1, faces=[geometry["sphere"]]
                ),
                fl.SurfaceRefinement(name="strut", max_edge_length=0.01, faces=[geometry["strut"]]),
                fl.BoundaryLayer(
                    name="floor", first_layer_thickness=1e-5, faces=[geometry["floor"]]
                ),
                fl.PassiveSpacing(
                    name="adjacent2floor", type="projected", faces=[geometry["adjacent2floor"]]
                ),
                fl.PassiveSpacing(name="ceiling", type="unchanged", faces=[geometry["ceiling"]]),
            ],
        ),
    )

1. :class:`UserDefinedFarfield` is used to generate the farfield volume zone based on the supplied geometry.
2. :class:`MeshingDefaults` defines the global settings for surface and volume meshing. For example, :py:attr:`~MeshingDefaults.surface_max_edge_length` governs the mesh size on all faces. 
3. Regarding the details :py:attr:`~MeshingParams.refinements` setting, please note that:

   - When setting up parameters for the specific faces, the :code:`faceName` tag defined in the CSM file can be used to select a group of faces:

     - The grouping tag **MUST** be chosen first (:code:`geometry.group_faces_by_tag("faceName")`) to allow the usage of group selection. 
     - All the keys of :code:`geometry` (e.g. :code:`"floor"` in :code:`geometry["floor"]`) need to match the resulting grouped face names under :code:`faceName` tag in the CSM file. 

   - To overwrite the global :py:attr:`~MeshingDefaults.surface_max_edge_length` setting, we define local :py:attr:`SurfaceRefinement.max_edge_length` for the sphere and strut faces.
   - For the :code:`floor` faces, we set :py:attr:`BoundaryLayer.first_layer_thickness` as 1e-5 to overwrite the global value of 1e-6. The first layer thicknesses provided here are just for illustration. The users need to adjust these values according to the target :math:`y^+` in their simulations.
   - For the :code:`ceiling` faces, :py:attr:`PassiveSpacing.type` is set as :code:`unchanged`, hence the anisotropic layers won't be grown from the ceiling. Meanwhile, the surface mesh on the ceiling remains unaltered when generating the volume mesh.
   - For the faces adjacent to the floor :code:`adjacent2floor`, :py:attr:`PassiveSpacing.type` is set as :code:`projected`. The surface mesh on those faces is updated when populating the volume mesh. As shown in :numref:`sphere_in_wind_tunnel_mesh`, the "spacing" of anisotropic layers grown from the floor are "projected" onto the faces adjacent to the floor.

.. _sphere_in_wind_tunnel_mesh:
.. figure:: Figures/mesh.png
   :width: 75%
   :align: center

   Sphere in wind tunnel, populated volume mesh.