.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/modeling_features/03-advanced-selection-rules.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_modeling_features_03-advanced-selection-rules.py: .. _advanced_selection_rules_example: Advanced selection rules example ================================ This example shows how to use advanced rules, including the geometrical, cut-off, and variable offset rules. It also demonstrates how rules can be templated and reused with different parameters. For more basic rules, see :ref:`basic_selection_rules_example`. This example only shows the PyACP part of the setup. For a complete composite analysis, see :ref:`pymapdl_workflow_example`. .. GENERATED FROM PYTHON SOURCE LINES 40-44 Import modules -------------- Import the standard library and third-party dependencies. .. GENERATED FROM PYTHON SOURCE LINES 44-50 .. code-block:: Python import pathlib import tempfile import numpy as np import pyvista .. GENERATED FROM PYTHON SOURCE LINES 51-52 Import the PyACP dependencies. .. GENERATED FROM PYTHON SOURCE LINES 52-64 .. code-block:: Python from ansys.acp.core import ( ACPWorkflow, BooleanOperationType, DimensionType, EdgeSetType, LinkedSelectionRule, launch_acp, ) from ansys.acp.core.extras import ExampleKeys, get_example_file .. GENERATED FROM PYTHON SOURCE LINES 66-68 Start ACP and load the model ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 70-71 Get the example file from the server. .. GENERATED FROM PYTHON SOURCE LINES 71-75 .. code-block:: Python tempdir = tempfile.TemporaryDirectory() WORKING_DIR = pathlib.Path(tempdir.name) input_file = get_example_file(ExampleKeys.MINIMAL_FLAT_PLATE, WORKING_DIR) .. GENERATED FROM PYTHON SOURCE LINES 76-77 Launch the PyACP server and connect to it. .. GENERATED FROM PYTHON SOURCE LINES 77-79 .. code-block:: Python acp = launch_acp() .. GENERATED FROM PYTHON SOURCE LINES 80-84 Define the input file and instantiate an ``ACPWorkflow`` instance. The ``ACPWorkflow`` class provides convenience methods that simplify file handling. It automatically creates a model based on the input file. This example's input file contains a flat plate with a single ply. .. GENERATED FROM PYTHON SOURCE LINES 84-95 .. code-block:: Python workflow = ACPWorkflow.from_acph5_file( acp=acp, acph5_file_path=input_file, local_working_directory=WORKING_DIR, ) model = workflow.model print(workflow.working_directory.path) print(model.unit_system) .. rst-class:: sphx-glr-script-out .. code-block:: none /tmp/tmpce23zj53 mks .. GENERATED FROM PYTHON SOURCE LINES 96-98 Add more layers to the modeling ply so that it is easier to see the effects of the selection rules. Plot the thickness of all the plies without any rules. .. GENERATED FROM PYTHON SOURCE LINES 98-106 .. code-block:: Python modeling_ply = model.modeling_groups["modeling_group"].modeling_plies["ply"] modeling_ply.number_of_layers = 10 model.update() assert model.elemental_data.thickness is not None model.elemental_data.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_03-advanced-selection-rules_001.png :alt: 03 advanced selection rules :srcset: /examples/modeling_features/images/sphx_glr_03-advanced-selection-rules_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_03-advanced-selection-rules_001.vtksz .. GENERATED FROM PYTHON SOURCE LINES 107-109 Parametrized Parallel Rule -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 111-114 Rules can be parametrized. This is useful when a rule is used multiple times but with different parameters. The :class:`.LinkedSelectionRule` class shows what parameters are available for each rule. This code modifies the extent of the parallel rule. .. GENERATED FROM PYTHON SOURCE LINES 116-117 Create a parallel rule. .. GENERATED FROM PYTHON SOURCE LINES 117-125 .. code-block:: Python parallel_rule = model.create_parallel_selection_rule( name="parallel_rule", origin=(0, 0, 0), direction=(1, 0, 0), lower_limit=0.005, upper_limit=1, ) .. GENERATED FROM PYTHON SOURCE LINES 126-127 Assign it the modeling ply. .. GENERATED FROM PYTHON SOURCE LINES 127-131 .. code-block:: Python linked_parallel_rule = LinkedSelectionRule(parallel_rule) modeling_ply.selection_rules = [linked_parallel_rule] .. GENERATED FROM PYTHON SOURCE LINES 132-133 Plot the thickness of the ply before the parametrization. .. GENERATED FROM PYTHON SOURCE LINES 133-137 .. code-block:: Python model.update() assert model.elemental_data.thickness is not None model.elemental_data.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_03-advanced-selection-rules_002.png :alt: 03 advanced selection rules :srcset: /examples/modeling_features/images/sphx_glr_03-advanced-selection-rules_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_03-advanced-selection-rules_002.vtksz .. GENERATED FROM PYTHON SOURCE LINES 138-140 Modify the parallel rule by changing the parameters of the linked rule. Parameters defined on the linked rule override the parameters of the original rule. .. GENERATED FROM PYTHON SOURCE LINES 140-144 .. code-block:: Python linked_parallel_rule.template_rule = True linked_parallel_rule.parameter_1 = 0.002 linked_parallel_rule.parameter_2 = 0.1 .. GENERATED FROM PYTHON SOURCE LINES 145-146 Plot the thickness of the ply with the modified rule. .. GENERATED FROM PYTHON SOURCE LINES 146-151 .. code-block:: Python model.update() assert model.elemental_data.thickness is not None model.elemental_data.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_03-advanced-selection-rules_003.png :alt: 03 advanced selection rules :srcset: /examples/modeling_features/images/sphx_glr_03-advanced-selection-rules_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_03-advanced-selection-rules_003.vtksz .. GENERATED FROM PYTHON SOURCE LINES 152-154 Create a geometrical selection rule ----------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 156-157 Add a CAD geometry to the model. .. GENERATED FROM PYTHON SOURCE LINES 157-165 .. code-block:: Python triangle_path = get_example_file(ExampleKeys.RULE_GEOMETRY_TRIANGLE, WORKING_DIR) triangle = workflow.add_cad_geometry_from_local_file(triangle_path) # Note: It is important to update the model here, because the root_shapes of the # cad_geometry are not available until the model is updated. model.update() .. GENERATED FROM PYTHON SOURCE LINES 166-167 Create a virtual geometry from the CAD geometry. .. GENERATED FROM PYTHON SOURCE LINES 167-171 .. code-block:: Python triangle_virtual_geometry = model.create_virtual_geometry( name="triangle_virtual_geometry", cad_components=triangle.root_shapes.values() ) .. GENERATED FROM PYTHON SOURCE LINES 172-173 Create the geometrical selection rule. .. GENERATED FROM PYTHON SOURCE LINES 173-178 .. code-block:: Python geometrical_selection_rule = model.create_geometrical_selection_rule( name="geometrical_rule", geometry=triangle_virtual_geometry, ) .. GENERATED FROM PYTHON SOURCE LINES 179-181 Assign the geometrical selection rule to the ply. Plot the ply extent with the outline of the geometry. .. GENERATED FROM PYTHON SOURCE LINES 181-192 .. code-block:: Python modeling_ply.selection_rules = [LinkedSelectionRule(geometrical_selection_rule)] model.update() assert model.elemental_data.thickness is not None plotter = pyvista.Plotter() plotter.add_mesh( triangle.visualization_mesh.to_pyvista(), style="wireframe", line_width=4, color="white" ) plotter.add_mesh(model.elemental_data.thickness.get_pyvista_mesh(mesh=model.mesh), show_edges=True) plotter.show() .. tab-set:: .. tab-item:: Static Scene .. image-sg:: /examples/modeling_features/images/sphx_glr_03-advanced-selection-rules_004.png :alt: 03 advanced selection rules :srcset: /examples/modeling_features/images/sphx_glr_03-advanced-selection-rules_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_03-advanced-selection-rules_004.vtksz .. GENERATED FROM PYTHON SOURCE LINES 193-195 Create a cutoff selection rule ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 197-198 Add the cutoff CAD geometry to the model. .. GENERATED FROM PYTHON SOURCE LINES 198-205 .. code-block:: Python cutoff_plane_path = get_example_file(ExampleKeys.CUT_OFF_GEOMETRY, WORKING_DIR) cut_off_plane = workflow.add_cad_geometry_from_local_file(cutoff_plane_path) # Note: It is important to update the model here, because the root_shapes of the # cad_geometry are not available until the model is updated. model.update() .. GENERATED FROM PYTHON SOURCE LINES 206-207 Create a virtual geometry from the CAD geometry. .. GENERATED FROM PYTHON SOURCE LINES 207-211 .. code-block:: Python cutoff_virtual_geometry = model.create_virtual_geometry( name="cutoff_virtual_geometry", cad_components=cut_off_plane.root_shapes.values() ) .. GENERATED FROM PYTHON SOURCE LINES 212-213 Create the cutoff selection rule. .. GENERATED FROM PYTHON SOURCE LINES 213-219 .. code-block:: Python cutoff_selection_rule = model.create_cutoff_selection_rule( name="cutoff_rule", cutoff_geometry=cutoff_virtual_geometry, ) .. GENERATED FROM PYTHON SOURCE LINES 220-222 Assign the cutoff selection rule to the ply. Plot the ply extent with the outline of the geometry. .. GENERATED FROM PYTHON SOURCE LINES 222-233 .. code-block:: Python modeling_ply.selection_rules = [LinkedSelectionRule(cutoff_selection_rule)] model.update() assert model.elemental_data.thickness is not None plotter = pyvista.Plotter() plotter.add_mesh(cut_off_plane.visualization_mesh.to_pyvista(), color="white") plotter.add_mesh(model.elemental_data.thickness.get_pyvista_mesh(mesh=model.mesh)) plotter.camera_position = [(-0.05, 0.01, 0), (0.005, 0.005, 0.005), (0, 1, 0)] plotter.show() .. tab-set:: .. tab-item:: Static Scene .. image-sg:: /examples/modeling_features/images/sphx_glr_03-advanced-selection-rules_005.png :alt: 03 advanced selection rules :srcset: /examples/modeling_features/images/sphx_glr_03-advanced-selection-rules_005.png :class: sphx-glr-single-img .. tab-item:: Interactive Scene .. offlineviewer:: /home/runner/work/pyacp/pyacp/doc/source/examples/modeling_features/images/sphx_glr_03-advanced-selection-rules_005.vtksz .. GENERATED FROM PYTHON SOURCE LINES 234-236 Create a variable offset selection rule --------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 238-239 Create the lookup table. .. GENERATED FROM PYTHON SOURCE LINES 239-245 .. code-block:: Python lookup_table = model.create_lookup_table_1d( name="lookup_table", origin=(0, 0, 0), direction=(0, 0, 1), ) .. GENERATED FROM PYTHON SOURCE LINES 246-248 Add the location data. The "Location" column of the lookup table is always created by default. .. GENERATED FROM PYTHON SOURCE LINES 248-250 .. code-block:: Python lookup_table.columns["Location"].data = np.array([0, 0.005, 0.01]) .. GENERATED FROM PYTHON SOURCE LINES 251-252 Create the offset column that defines the offsets from the edge. .. GENERATED FROM PYTHON SOURCE LINES 252-258 .. code-block:: Python offsets_column = lookup_table.create_column( name="offset", dimension_type=DimensionType.LENGTH, data=np.array([0.00, 0.004, 0]), ) .. GENERATED FROM PYTHON SOURCE LINES 259-261 Create the edge set from the "All_Elements" element set. Because you assigned 30 degrees to the limit angle, only one edge at x=0 is selected. .. GENERATED FROM PYTHON SOURCE LINES 261-269 .. code-block:: Python edge_set = model.create_edge_set( name="edge_set", edge_set_type=EdgeSetType.BY_REFERENCE, limit_angle=30, element_set=model.element_sets["All_Elements"], origin=(0, 0, 0), ) .. GENERATED FROM PYTHON SOURCE LINES 270-271 Create the variable offset rule and assign it to the ply. .. GENERATED FROM PYTHON SOURCE LINES 271-277 .. code-block:: Python variable_offset_rule = model.create_variable_offset_selection_rule( name="variable_offset_rule", edge_set=edge_set, offsets=offsets_column, distance_along_edge=True ) modeling_ply.selection_rules = [LinkedSelectionRule(variable_offset_rule)] .. GENERATED FROM PYTHON SOURCE LINES 278-279 Plot the ply extent. .. GENERATED FROM PYTHON SOURCE LINES 279-283 .. code-block:: Python model.update() assert model.elemental_data.thickness is not None model.elemental_data.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_03-advanced-selection-rules_006.png :alt: 03 advanced selection rules :srcset: /examples/modeling_features/images/sphx_glr_03-advanced-selection-rules_006.png :class: sphx-glr-single-img .. tab-item:: Interactive Scene .. offlineviewer:: /home/runner/work/pyacp/pyacp/doc/source/examples/modeling_features/images/sphx_glr_03-advanced-selection-rules_006.vtksz .. GENERATED FROM PYTHON SOURCE LINES 284-286 Create a boolean selection rule ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 288-293 Creating a Boolean selection rule and assigning it to a ply has the same effect as linking the individual rules directly to the ply. Boolean rules are still useful because they can help organize rules and make more complex ones. Create a cylindrical selection rule to combine with the parallel rule. .. GENERATED FROM PYTHON SOURCE LINES 293-318 .. code-block:: Python cylindrical_rule_boolean = model.create_cylindrical_selection_rule( name="cylindrical_rule", origin=(0.005, 0, 0.005), direction=(0, 1, 0), radius=0.002, ) parallel_rule_boolean = model.create_parallel_selection_rule( name="parallel_rule", origin=(0, 0, 0), direction=(1, 0, 0), lower_limit=0.005, upper_limit=1, ) linked_cylindrical_rule_boolean = LinkedSelectionRule(cylindrical_rule_boolean) linked_parallel_rule_boolean = LinkedSelectionRule(parallel_rule_boolean) boolean_selection_rule = model.create_boolean_selection_rule( name="boolean_rule", selection_rules=[linked_parallel_rule_boolean, linked_cylindrical_rule_boolean], ) modeling_ply.selection_rules = [LinkedSelectionRule(boolean_selection_rule)] .. GENERATED FROM PYTHON SOURCE LINES 319-320 Plot the ply extent. .. GENERATED FROM PYTHON SOURCE LINES 320-324 .. code-block:: Python model.update() assert model.elemental_data.thickness is not None model.elemental_data.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_03-advanced-selection-rules_007.png :alt: 03 advanced selection rules :srcset: /examples/modeling_features/images/sphx_glr_03-advanced-selection-rules_007.png :class: sphx-glr-single-img .. tab-item:: Interactive Scene .. offlineviewer:: /home/runner/work/pyacp/pyacp/doc/source/examples/modeling_features/images/sphx_glr_03-advanced-selection-rules_007.vtksz .. GENERATED FROM PYTHON SOURCE LINES 325-326 Modify the operation type of the Boolean selection rule so that the two rules are added. .. GENERATED FROM PYTHON SOURCE LINES 326-329 .. code-block:: Python linked_parallel_rule_boolean.operation_type = BooleanOperationType.INTERSECT linked_cylindrical_rule_boolean.operation_type = BooleanOperationType.ADD .. GENERATED FROM PYTHON SOURCE LINES 330-331 Plot the ply extent. .. GENERATED FROM PYTHON SOURCE LINES 331-334 .. code-block:: Python model.update() assert model.elemental_data.thickness is not None model.elemental_data.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_03-advanced-selection-rules_008.png :alt: 03 advanced selection rules :srcset: /examples/modeling_features/images/sphx_glr_03-advanced-selection-rules_008.png :class: sphx-glr-single-img .. tab-item:: Interactive Scene .. offlineviewer:: /home/runner/work/pyacp/pyacp/doc/source/examples/modeling_features/images/sphx_glr_03-advanced-selection-rules_008.vtksz .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.353 seconds) .. _sphx_glr_download_examples_modeling_features_03-advanced-selection-rules.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 03-advanced-selection-rules.ipynb <03-advanced-selection-rules.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 03-advanced-selection-rules.py <03-advanced-selection-rules.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 03-advanced-selection-rules.zip <03-advanced-selection-rules.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_