.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/modeling_features/050-composite_cae_h5.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_050-composite_cae_h5.py>`
        to download the full example code.

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

.. _sphx_glr_examples_modeling_features_050-composite_cae_h5.py:


.. _composite_cae_h5_example:

HDF5 Composite CAE
==================

The HDF5 Composite CAE interface of PyACP is demonstrated in
this example. It shows how to write (export) and read (import)
layup data to and from a HDF5 Composite CAE file, respectively.
The HDF5 Composite CAE format is a vendor independent format
to exchange composite layup information between CAE tools.

This examples demonstrates how to:
- Load and manipulate a model
- Export data to a HDF5 Composite CAE file
- Import and map layup from HDF5 Composite CAE onto a different model (mesh)
- Export data with ply offsets (3D plies)
- Import a layup as :class:`.ImportedModelingPly`
- Import HDF5 Composite CAE with 3D plies and map the layup onto an :class:`.ImportedSolidModel`

.. GENERATED FROM PYTHON SOURCE LINES 46-47

Import the standard library and third-party dependencies.

.. GENERATED FROM PYTHON SOURCE LINES 47-52

.. code-block:: Python

    import pathlib
    import tempfile

    import pyvista








.. GENERATED FROM PYTHON SOURCE LINES 53-54

Import the PyACP dependencies.

.. GENERATED FROM PYTHON SOURCE LINES 54-69

.. code-block:: Python

    from ansys.acp.core import (
        HDF5CompositeCAEProjectionMode,
        LinkedSelectionRule,
        OffsetType,
        launch_acp,
    )
    from ansys.acp.core.extras import (
        FLAT_PLATE_SHELL_CAMERA,
        FLAT_PLATE_SOLID_CAMERA,
        ExampleKeys,
        get_example_file,
    )










.. GENERATED FROM PYTHON SOURCE LINES 71-75

Start ACP and load the model
----------------------------
%%
Get the example file from the server.

.. GENERATED FROM PYTHON SOURCE LINES 75-79

.. code-block:: Python

    tempdir = tempfile.TemporaryDirectory()
    WORKING_DIR = pathlib.Path(tempdir.name)
    acph5_input_file = get_example_file(ExampleKeys.BASIC_FLAT_PLATE_ACPH5, WORKING_DIR)








.. GENERATED FROM PYTHON SOURCE LINES 80-81

Launch the PyACP server and connect to it.

.. GENERATED FROM PYTHON SOURCE LINES 81-83

.. code-block:: Python

    acp = launch_acp()








.. GENERATED FROM PYTHON SOURCE LINES 84-85

Load the model from an acph5 file

.. GENERATED FROM PYTHON SOURCE LINES 85-87

.. code-block:: Python

    model = acp.import_model(acph5_input_file)








.. GENERATED FROM PYTHON SOURCE LINES 88-89

Crop some plies in order to generate a variable laminate

.. GENERATED FROM PYTHON SOURCE LINES 89-112

.. code-block:: Python

    pr_x = model.create_parallel_selection_rule(
        name="x axis",
        direction=(1, 0, 0),
        lower_limit=0.0025,
        upper_limit=0.0075,
    )
    pr_z = model.create_parallel_selection_rule(
        name="z axis",
        direction=(0, 0, 1),
        lower_limit=0.0015,
        upper_limit=0.0085,
    )
    boolean_rule = model.create_boolean_selection_rule(
        name="boolean rule",
        selection_rules=[LinkedSelectionRule(pr_x), LinkedSelectionRule(pr_z)],
    )

    for ply_name in ["ply_1_45_UD", "ply_2_-45_UD", "ply_3_45_UD", "ply_4_-45_UD"]:
        ply = model.modeling_groups["modeling_group"].modeling_plies[ply_name]
        ply.selection_rules = [LinkedSelectionRule(boolean_rule)]

    model.update()








.. GENERATED FROM PYTHON SOURCE LINES 113-114

Plot the thickness distribution

.. GENERATED FROM PYTHON SOURCE LINES 114-118

.. code-block:: Python

    thickness = model.elemental_data.thickness
    assert thickness is not None
    thickness.get_pyvista_mesh(mesh=model.mesh).plot(show_edges=True)








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_050-composite_cae_h5_001.png
        :alt: 050 composite cae h5
        :srcset: /examples/modeling_features/images/sphx_glr_050-composite_cae_h5_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_050-composite_cae_h5_001.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 119-123

Write HDF5 Composite CAE file
-----------------------------

Export the entire layup to a HDF5 Composite CAE file.

.. GENERATED FROM PYTHON SOURCE LINES 123-128

.. code-block:: Python

    h5_output_file = WORKING_DIR / "hdf5_composite_cae.h5"
    model.export_hdf5_composite_cae(
        path=h5_output_file,
    )








.. GENERATED FROM PYTHON SOURCE LINES 129-134

Load HDF5 Composite CAE file into a different model
---------------------------------------------------

A new acp model is created by importing a refined mesh of the same geometry.
Both meshes (initial mesh in blue, refined one in red) are shown below.

.. GENERATED FROM PYTHON SOURCE LINES 134-157

.. code-block:: Python

    dat_input_file_refined = get_example_file(ExampleKeys.BASIC_FLAT_PLATE_REFINED_DAT, WORKING_DIR)
    refined_model = acp.import_model(path=dat_input_file_refined, format="ansys:dat")

    plotter = pyvista.Plotter()
    plotter.add_mesh(
        model.shell_mesh.to_pyvista(),
        color="blue",
        edge_color="blue",
        show_edges=True,
        style="wireframe",
        line_width=4,
    )
    plotter.add_mesh(
        refined_model.shell_mesh.to_pyvista(),
        color="red",
        edge_color="red",
        show_edges=True,
        style="wireframe",
        line_width=2,
    )
    plotter.camera_position = FLAT_PLATE_SHELL_CAMERA
    plotter.show()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_050-composite_cae_h5_002.png
        :alt: 050 composite cae h5
        :srcset: /examples/modeling_features/images/sphx_glr_050-composite_cae_h5_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_050-composite_cae_h5_002.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 158-161

Import the HDF5 Composite CAE file which is then automatically mapped
onto the refined mesh. In this example, the default settings
(tolerances, etc.) are used.

.. GENERATED FROM PYTHON SOURCE LINES 161-166

.. code-block:: Python

    refined_model.import_hdf5_composite_cae(
        path=h5_output_file,
    )
    refined_model.update()








.. GENERATED FROM PYTHON SOURCE LINES 167-168

Plot the thickness distribution on the refined model

.. GENERATED FROM PYTHON SOURCE LINES 168-172

.. code-block:: Python

    thickness = refined_model.elemental_data.thickness
    assert thickness is not None
    thickness.get_pyvista_mesh(mesh=refined_model.mesh).plot(show_edges=True)








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_050-composite_cae_h5_003.png
        :alt: 050 composite cae h5
        :srcset: /examples/modeling_features/images/sphx_glr_050-composite_cae_h5_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_050-composite_cae_h5_003.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 173-180

3D plies with ply-offsets
-------------------------

The HDF5 Composite CAE interface also allows to export the 3D plies
(plies with offsets) which can then be used to create
imported modeling plies. The initial model is used to
write a new HDF5 with ``layup_representation_3d`` enabled.

.. GENERATED FROM PYTHON SOURCE LINES 180-187

.. code-block:: Python

    h5_output_file_3D = WORKING_DIR / "hdf5_composite_cae_3D.h5"
    model.export_hdf5_composite_cae(
        path=h5_output_file_3D,
        layup_representation_3d=True,
        offset_type=OffsetType.BOTTOM_OFFSET,
    )








.. GENERATED FROM PYTHON SOURCE LINES 188-189

A new acp model is created to properly separate the different workflows.

.. GENERATED FROM PYTHON SOURCE LINES 189-194

.. code-block:: Python

    refined_model_3D = acp.import_model(path=dat_input_file_refined, format="ansys:dat")
    refined_model_3D.import_hdf5_composite_cae(
        path=h5_output_file_3D, projection_mode=HDF5CompositeCAEProjectionMode.SOLID
    )








.. GENERATED FROM PYTHON SOURCE LINES 195-198

An imported solid model is required for the 3D workflow (with imported modeling plies).
Details about :class:`.ImportedSolidModel` and :class:`.ImportedModelingPly` can be found
in the examples :ref:`imported_solid_model_example` and :ref:`imported_plies_example`.

.. GENERATED FROM PYTHON SOURCE LINES 198-206

.. code-block:: Python

    local_solid_mesh_file = get_example_file(ExampleKeys.BASIC_FLAT_PLATE_SOLID_MESH_CDB, WORKING_DIR)
    remote_solid_mesh_file = acp.upload_file(local_solid_mesh_file)
    imported_solid_model = refined_model_3D.create_imported_solid_model(
        name="Imported Solid Model",
        external_path=remote_solid_mesh_file,
        format="ansys:cdb",
    )








.. GENERATED FROM PYTHON SOURCE LINES 207-209

The :class:`.LayupMappingObject` is used to configure the mapping of the imported plies
onto the imported solid model.

.. GENERATED FROM PYTHON SOURCE LINES 209-219

.. code-block:: Python

    imported_solid_model.create_layup_mapping_object(
        name="Map imported plies",
        use_imported_plies=True,  # enable imported plies
        select_all_plies=True,  # select all plies
        scale_ply_thicknesses=True,
        entire_solid_mesh=True,
        delete_lost_elements=True,  # elements without plies are deleted
    )
    refined_model_3D.update()








.. GENERATED FROM PYTHON SOURCE LINES 220-223

The mapped top layer of the imported laminate is shown below.
Note that the solid elements which do not intersect with the
layup are deleted in this example.

.. GENERATED FROM PYTHON SOURCE LINES 223-235

.. code-block:: Python

    imported_analysis_ply = (
        refined_model_3D.imported_modeling_groups["modeling_group"]
        .imported_modeling_plies["ply_5_0_UD"]
        .imported_production_plies["ImportedProductionPly.6"]
        .imported_analysis_plies["P1L1__ply_5_0_UD"]
    )
    plotter = pyvista.Plotter()
    plotter.add_mesh(imported_analysis_ply.solid_mesh.to_pyvista(), show_edges=True)
    plotter.add_mesh(refined_model_3D.solid_mesh.to_pyvista(), opacity=0.2, show_edges=False)
    plotter.camera_position = FLAT_PLATE_SOLID_CAMERA
    plotter.show()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_050-composite_cae_h5_004.png
        :alt: 050 composite cae h5
        :srcset: /examples/modeling_features/images/sphx_glr_050-composite_cae_h5_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_050-composite_cae_h5_004.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 236-239

Note that the visualization of imported plies and imported solid model
is limited. As an alternative, you can save the model and review it
in ACP standalone.


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

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


.. _sphx_glr_download_examples_modeling_features_050-composite_cae_h5.py:

.. only:: html

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

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

      :download:`Download Jupyter notebook: 050-composite_cae_h5.ipynb <050-composite_cae_h5.ipynb>`

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

      :download:`Download Python source code: 050-composite_cae_h5.py <050-composite_cae_h5.py>`

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

      :download:`Download zipped: 050-composite_cae_h5.zip <050-composite_cae_h5.zip>`


.. only:: html

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

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