.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/modeling_features/005-ply-direction-lookup-table.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

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

        :ref:`Go to the end <sphx_glr_download_examples_modeling_features_005-ply-direction-lookup-table.py>`
        to download the full example code.

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

.. _sphx_glr_examples_modeling_features_005-ply-direction-lookup-table.py:


.. _direction_definition_example:

Direction definition
====================

This example shows how to define directions from lookup tables. They can be either
reference directions for oriented selection sets or draping angles for modeling plies.
The example only shows the PyACP part of the setup. For a complete composite analysis,
see :ref:`pymapdl_workflow_example`.

.. GENERATED FROM PYTHON SOURCE LINES 36-40

Import modules
--------------

Import the standard library and third-party dependencies.

.. GENERATED FROM PYTHON SOURCE LINES 40-45

.. code-block:: Python

    import pathlib
    import tempfile

    import numpy as np








.. GENERATED FROM PYTHON SOURCE LINES 46-47

Import the PyACP dependencies.

.. GENERATED FROM PYTHON SOURCE LINES 47-58

.. code-block:: Python

    from ansys.acp.core import (
        DrapingType,
        LookUpTableColumnValueType,
        PhysicalDimension,
        PlyType,
        RosetteSelectionMethod,
        get_directions_plotter,
        launch_acp,
    )
    from ansys.acp.core.extras import ExampleKeys, get_example_file








.. GENERATED FROM PYTHON SOURCE LINES 59-61

Start ACP and load the model
----------------------------

.. GENERATED FROM PYTHON SOURCE LINES 63-64

Get the example file from the server.

.. GENERATED FROM PYTHON SOURCE LINES 64-68

.. code-block:: Python

    tempdir = tempfile.TemporaryDirectory()
    WORKING_DIR = pathlib.Path(tempdir.name)
    input_file = get_example_file(ExampleKeys.BASIC_FLAT_PLATE_DAT, WORKING_DIR)








.. GENERATED FROM PYTHON SOURCE LINES 69-70

Launch the PyACP server and connect to it.

.. GENERATED FROM PYTHON SOURCE LINES 70-72

.. code-block:: Python

    acp = launch_acp()








.. GENERATED FROM PYTHON SOURCE LINES 73-75

Import the model from the input file.
This example's input file contains a flat plate.

.. GENERATED FROM PYTHON SOURCE LINES 75-78

.. code-block:: Python

    model = acp.import_model(input_file, format="ansys:dat")
    print(model.unit_system)





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

 .. code-block:: none

    mks




.. GENERATED FROM PYTHON SOURCE LINES 79-81

Setup materials and oriented selection set
------------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 81-83

.. code-block:: Python


    #







.. GENERATED FROM PYTHON SOURCE LINES 84-85

Create a material and fabric.

.. GENERATED FROM PYTHON SOURCE LINES 85-92

.. code-block:: Python

    ud_material = model.create_material(
        name="UD",
        ply_type=PlyType.REGULAR,
    )

    fabric = model.create_fabric(name="UD", material=ud_material, thickness=0.1)








.. GENERATED FROM PYTHON SOURCE LINES 93-94

Create a parallel rosette.

.. GENERATED FROM PYTHON SOURCE LINES 94-96

.. code-block:: Python

    rosette = model.create_rosette()








.. GENERATED FROM PYTHON SOURCE LINES 97-98

Create an oriented selection set (OSS) and assign the rosette.

.. GENERATED FROM PYTHON SOURCE LINES 98-106

.. code-block:: Python

    oss = model.create_oriented_selection_set(
        name="oss",
        orientation_direction=(0.0, 1.0, 0),
        element_sets=[model.element_sets["All_Elements"]],
        rosettes=[rosette],
    )









.. GENERATED FROM PYTHON SOURCE LINES 107-109

Plot the orientation and reference direction of the OSS.
The reference direction is defined by the rosette.

.. GENERATED FROM PYTHON SOURCE LINES 109-115

.. code-block:: Python

    model.update()
    plotter = get_directions_plotter(
        model=model, components=[oss.elemental_data.orientation, oss.elemental_data.reference_direction]
    )
    plotter.show()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_005-ply-direction-lookup-table_001.png
        :alt: 005 ply direction lookup table
        :srcset: /examples/modeling_features/images/sphx_glr_005-ply-direction-lookup-table_001.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyacp/pyacp/doc/source/examples/modeling_features/images/sphx_glr_005-ply-direction-lookup-table_001.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 116-118

Define reference direction from lookup table
--------------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 120-121

Create a 3D lookup table to store the direction and angle corrections.

.. GENERATED FROM PYTHON SOURCE LINES 121-123

.. code-block:: Python

    lookup_table = model.create_lookup_table_3d()








.. GENERATED FROM PYTHON SOURCE LINES 124-125

Create a grid of points on the plate where the lookup table values are stored.

.. GENERATED FROM PYTHON SOURCE LINES 125-140

.. code-block:: Python

    plate_side_length = 0.01
    num_points = 10
    x_coordinates = np.linspace(0, plate_side_length, num_points)
    z_coordinates = np.linspace(0, plate_side_length, num_points)
    xx, zz = np.meshgrid(x_coordinates, z_coordinates)

    points = np.stack(
        [
            xx.ravel(),
            np.zeros(xx.ravel().shape),
            zz.ravel(),
        ],
        axis=1,
    )








.. GENERATED FROM PYTHON SOURCE LINES 141-142

Compute the directions tangential to circles around the point (0,0,0).

.. GENERATED FROM PYTHON SOURCE LINES 142-145

.. code-block:: Python

    normal = np.array([0, 1, 0])
    directions = np.cross(points, normal)








.. GENERATED FROM PYTHON SOURCE LINES 146-147

Create the lookup table and add the direction data.

.. GENERATED FROM PYTHON SOURCE LINES 147-154

.. code-block:: Python

    lookup_table.columns["Location"].data = points
    direction_column = lookup_table.create_column(
        data=directions,
        physical_dimension=PhysicalDimension.DIMENSIONLESS,
        value_type=LookUpTableColumnValueType.DIRECTION,
    )








.. GENERATED FROM PYTHON SOURCE LINES 155-156

Assign the lookup table to the OSS.

.. GENERATED FROM PYTHON SOURCE LINES 156-159

.. code-block:: Python

    oss.rosette_selection_method = RosetteSelectionMethod.DIRECTIONS_FROM_TABULAR_VALUES
    oss.reference_direction_field = direction_column








.. GENERATED FROM PYTHON SOURCE LINES 160-161

Plot the orientation and the reference direction of the OSS.

.. GENERATED FROM PYTHON SOURCE LINES 161-167

.. code-block:: Python

    model.update()
    plotter = get_directions_plotter(
        model=model, components=[oss.elemental_data.orientation, oss.elemental_data.reference_direction]
    )
    plotter.show()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_005-ply-direction-lookup-table_002.png
        :alt: 005 ply direction lookup table
        :srcset: /examples/modeling_features/images/sphx_glr_005-ply-direction-lookup-table_002.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyacp/pyacp/doc/source/examples/modeling_features/images/sphx_glr_005-ply-direction-lookup-table_002.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 168-169

Reset the OSS so that it may use the rosette again for the reference direction.

.. GENERATED FROM PYTHON SOURCE LINES 169-171

.. code-block:: Python

    oss.rosette_selection_method = RosetteSelectionMethod.MINIMUM_ANGLE








.. GENERATED FROM PYTHON SOURCE LINES 172-177

Define draping angles from lookup table
---------------------------------------

%%
Compute a correction angle to define circular fiber paths.

.. GENERATED FROM PYTHON SOURCE LINES 177-184

.. code-block:: Python

    correction_angle = np.arctan2(xx.ravel(), zz.ravel()) * 180 / np.pi
    angle_column_1 = lookup_table.create_column(
        data=correction_angle,  # type: ignore
        physical_dimension=PhysicalDimension.DIMENSIONLESS,
        value_type=LookUpTableColumnValueType.SCALAR,
    )








.. GENERATED FROM PYTHON SOURCE LINES 185-186

Compute the transverse correction angle, assuming a constant shear angle of -30°.

.. GENERATED FROM PYTHON SOURCE LINES 186-194

.. code-block:: Python

    shear_angle = -30
    transverse_correction_angle = correction_angle + shear_angle
    angle_column_2 = lookup_table.create_column(
        data=transverse_correction_angle,  # type: ignore
        physical_dimension=PhysicalDimension.DIMENSIONLESS,
        value_type=LookUpTableColumnValueType.SCALAR,
    )








.. GENERATED FROM PYTHON SOURCE LINES 195-196

Create a modeling ply with the angle corrections.

.. GENERATED FROM PYTHON SOURCE LINES 196-207

.. code-block:: Python

    modeling_group = model.create_modeling_group(name="modeling_group")
    modeling_ply = modeling_group.create_modeling_ply(
        name="ply",
        ply_angle=0,
        ply_material=fabric,
        oriented_selection_sets=[oss],
        draping_type=DrapingType.TABULAR_VALUES,
        draping_angle_1_field=angle_column_1,
        draping_angle_2_field=angle_column_2,
    )








.. GENERATED FROM PYTHON SOURCE LINES 208-209

Plot the directions of the modeling ply. First, plot the directions without correction angles.

.. GENERATED FROM PYTHON SOURCE LINES 209-219

.. code-block:: Python

    model.update()
    plotter = get_directions_plotter(
        model=model,
        components=[
            modeling_ply.elemental_data.fiber_direction,
            modeling_ply.elemental_data.transverse_direction,
        ],
    )
    plotter.show()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_005-ply-direction-lookup-table_003.png
        :alt: 005 ply direction lookup table
        :srcset: /examples/modeling_features/images/sphx_glr_005-ply-direction-lookup-table_003.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyacp/pyacp/doc/source/examples/modeling_features/images/sphx_glr_005-ply-direction-lookup-table_003.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 220-221

Next, plot the draped directions, including the correction angles, from the lookup table.

.. GENERATED FROM PYTHON SOURCE LINES 221-229

.. code-block:: Python

    plotter = get_directions_plotter(
        model=model,
        components=[
            modeling_ply.elemental_data.draped_fiber_direction,
            modeling_ply.elemental_data.draped_transverse_direction,
        ],
    )
    plotter.show()







.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_005-ply-direction-lookup-table_004.png
        :alt: 005 ply direction lookup table
        :srcset: /examples/modeling_features/images/sphx_glr_005-ply-direction-lookup-table_004.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyacp/pyacp/doc/source/examples/modeling_features/images/sphx_glr_005-ply-direction-lookup-table_004.vtksz







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

   **Total running time of the script:** (0 minutes 5.258 seconds)


.. _sphx_glr_download_examples_modeling_features_005-ply-direction-lookup-table.py:

.. only:: html

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

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

      :download:`Download Jupyter notebook: 005-ply-direction-lookup-table.ipynb <005-ply-direction-lookup-table.ipynb>`

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

      :download:`Download Python source code: 005-ply-direction-lookup-table.py <005-ply-direction-lookup-table.py>`

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

      :download:`Download zipped: 005-ply-direction-lookup-table.zip <005-ply-direction-lookup-table.zip>`


.. only:: html

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

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