.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/gallery_examples/gallery/06_blade_morph.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_examples_gallery_examples_gallery_06_blade_morph.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_gallery_examples_gallery_06_blade_morph.py:


.. _ref_turbine_blade:

=========================================================
Morph a hexahedral mesh of a turbine blade to a new shape
=========================================================

**Summary**: This example demonstrates how to morph a structural
hexahedral mesh of a turbine blade to a new deformed shape
defined by a target geometry file.

Objective
~~~~~~~~~~

This example appends a CDB mesh with a CAD geometry
and match morphs the mesh to the geometry.

.. image:: ../../../images/turbine_blade.png
   :align: center
   :width: 800
   :alt: Turbine blade hexahedral mesh.

Procedure
~~~~~~~~~~
#. Launch an Ansys Prime Server instance and connect the PyPrimeMesh client.
#. Read the mesh and append the new CAD geometry shape.
#. Define the mesh source faces and the target geometry faces to match morph.
#. Match morph the turbine blade mesh to the new CAD geometry shape.
#. Write the mesh for structural analysis.

.. GENERATED FROM PYTHON SOURCE LINES 56-61

Launch Ansys Prime Server
~~~~~~~~~~~~~~~~~~~~~~~~~
Import all necessary modules.
Launch the Ansys Prime Server instance and connect the client.
Get the client model and instantiate meshing utilities from the ``lucid`` class.

.. GENERATED FROM PYTHON SOURCE LINES 61-72

.. code-block:: Python


    import os
    import tempfile

    import ansys.meshing.prime as prime
    from ansys.meshing.prime.graphics import PrimePlotter

    prime_client = prime.launch_prime()
    model = prime_client.model
    mesh_util = prime.lucid.Mesh(model=model)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Using Ansys Prime Server from container ansys-prime-server-7




.. GENERATED FROM PYTHON SOURCE LINES 73-78

Read files
~~~~~~~~~~
Download the turbine blade mesh file and CAD geometry.
Read the mesh and append the geometry.
Display the source and the target.

.. GENERATED FROM PYTHON SOURCE LINES 78-94

.. code-block:: Python


    # For Windows OS users, scdoc is also available for geometry:
    # target_geometry = prime.examples.download_turbine_blade_target_scdoc()

    source_mesh = prime.examples.download_turbine_blade_cdb()
    target_geometry = prime.examples.download_deformed_blade_fmd()
    mesh_util.read(file_name=source_mesh)
    mesh_util.read(file_name=target_geometry, append=True)


    display = PrimePlotter()
    display.plot(model)
    display.show()

    print(model)








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/gallery_examples/gallery/images/sphx_glr_06_blade_morph_001.png
        :alt: 06 blade morph
        :srcset: /examples/gallery_examples/gallery/images/sphx_glr_06_blade_morph_001.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyprimemesh/pyprimemesh/doc/source/examples/gallery_examples/gallery/images/sphx_glr_06_blade_morph_001.vtksz



.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Part Summary:

    Part Name: blade
    Part ID: 2
        0 Edge Zonelets
        6 Face Zonelets
        1 Cell Zonelets

        0 Edge Zones
            Edge Zone Name(s) : []
        0 Face Zones
            Face Zone Name(s) : []
        1 Volume Zones
            Volume Zone Name(s) : [volume]

        5 Label(s)
            Names: [FIRTREE1, FIRTREE2, FIRTREE3, FIRTREE4, PRESSURE_SURF]

        Bounding box (-0.781465 4.39775 -0.7)
                     (0.79353 6.8 0.7)

    Part Name: blade_deformed
    Part ID: 3
        202 Topo Edges
        80 Topo Faces
        1 Topo Volumes

        0 Edge Zones
            Edge Zone Name(s) : []
        0 Face Zones
            Face Zone Name(s) : []
        1 Volume Zones
            Volume Zone Name(s) : [solid]

        0 Label(s)
            Names: []

        Bounding box (-0.781465 4.39775 -0.7)
                     (0.79353 6.8 0.7)





.. GENERATED FROM PYTHON SOURCE LINES 95-97

Define source and target faces
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. GENERATED FROM PYTHON SOURCE LINES 97-103

.. code-block:: Python


    source_part = model.get_part(2)
    target_part = model.get_part(3)
    source = source_part.get_face_zonelets()
    target = target_part.get_topo_faces()








.. GENERATED FROM PYTHON SOURCE LINES 104-109

Match morph mesh
~~~~~~~~~~~~~~~~
Set the target type to be for topoface because the target is geometry.
Morph the source face zonelets of ``source_part`` to the
target topofaces of the geometry.

.. GENERATED FROM PYTHON SOURCE LINES 109-135

.. code-block:: Python


    morpher = prime.Morpher(model)
    match_pair = prime.MatchPair(
        model=model,
        source_surfaces=source,
        target_surfaces=target,
        target_type=prime.MatchPairTargetType.TOPOFACE,
    )

    params = prime.MatchMorphParams(model)
    bc_params = prime.MorphBCParams(model)
    solver_params = prime.MorphSolveParams(model)

    morpher.match_morph(
        part_id=source_part.id,
        match_pairs=[match_pair],
        match_morph_params=params,
        bc_params=bc_params,
        solve_params=solver_params,
    )

    # Display the morphed mesh

    display = PrimePlotter()
    display.plot(model, update=True)
    display.show()







.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/gallery_examples/gallery/images/sphx_glr_06_blade_morph_002.png
        :alt: 06 blade morph
        :srcset: /examples/gallery_examples/gallery/images/sphx_glr_06_blade_morph_002.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyprimemesh/pyprimemesh/doc/source/examples/gallery_examples/gallery/images/sphx_glr_06_blade_morph_002.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 136-140

Write mesh
~~~~~~~~~~
Write the morphed CDB file. The geometry is ignored when exporting to a CDB
file.

.. GENERATED FROM PYTHON SOURCE LINES 140-147

.. code-block:: Python


    with tempfile.TemporaryDirectory() as temp_folder:
        mesh_file = os.path.join(temp_folder, "morphed_turbine_blade.cdb")
        mesh_util.write(mesh_file)
        assert os.path.exists(mesh_file)
        print("\nExported file:\n", mesh_file)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    This get_abaqus_simulation_data is a beta API. The behavior and implementation may change in future.

    Exported file:
     /tmp/tmptojxvniw/morphed_turbine_blade.cdb




.. GENERATED FROM PYTHON SOURCE LINES 148-150

Exit PyPrimeMesh
~~~~~~~~~~~~~~~~

.. GENERATED FROM PYTHON SOURCE LINES 150-152

.. code-block:: Python


    prime_client.exit()








.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (1 minutes 25.277 seconds)


.. _sphx_glr_download_examples_gallery_examples_gallery_06_blade_morph.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: 06_blade_morph.ipynb <06_blade_morph.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: 06_blade_morph.py <06_blade_morph.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: 06_blade_morph.zip <06_blade_morph.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_