.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/010-create-plots-on-named-selections.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_010-create-plots-on-named-selections.py: .. _create_plots_example: Create plots scoped to named selections ============================================= This example demonstrates how to create named selections and use them to create plots in Result Explorer: - **Named selections** based on element IDs to filter and organize data. - **Plot definitions** for displacement and velocity using named selections. - **Multiple result sets** handling across different timesteps. - **Viewport assignment** to display plots side-by-side for comparison. This example uses a transient structural analysis result with multiple timesteps for visualization. .. GENERATED FROM PYTHON SOURCE LINES 36-37 Import the Result Explorer dependencies. .. GENERATED FROM PYTHON SOURCE LINES 37-53 .. code-block:: Python from ansys.result_explorer.core import ( Component, Field, Location, PlotDefinition, ResultFieldName, ResultType, launch_result_explorer, models, ) from ansys.result_explorer.core.examples import ( ExampleKeys, get_example_file, get_example_snapshot_settings, ) .. GENERATED FROM PYTHON SOURCE LINES 54-57 Launch Result Explorer ---------------------- Start a Result Explorer instance for this example. .. GENERATED FROM PYTHON SOURCE LINES 57-59 .. code-block:: Python rx = launch_result_explorer() .. GENERATED FROM PYTHON SOURCE LINES 60-63 Locate example data -------------------- Get the path to the example result file. .. GENERATED FROM PYTHON SOURCE LINES 63-65 .. code-block:: Python rst_path = get_example_file(ExampleKeys.RST_CP_TRANSIENT) .. GENERATED FROM PYTHON SOURCE LINES 66-69 Create a workspace and solution -------------------------------- Create a workspace and load a solution from the result file. .. GENERATED FROM PYTHON SOURCE LINES 69-78 .. code-block:: Python workspace = rx.create_workspace(name="PyRX NS Plot Workspace") sol_name = "Coupled Field Transient Analysis" sol = rx.create_solution( name=sol_name, file_path=rst_path, ) print(f"Created solution: {sol.name}") .. rst-class:: sphx-glr-script-out .. code-block:: none Created solution: Coupled Field Transient Analysis .. GENERATED FROM PYTHON SOURCE LINES 79-82 Identify available result sets -------------------------------- Verify the solution has multiple timesteps and extract the set IDs. .. GENERATED FROM PYTHON SOURCE LINES 82-92 .. code-block:: Python if sol.n_sets < 2: raise RuntimeError("This example requires at least two result sets/timesteps.") set_ids = sorted({tf.set_id for tf in sol.time_frequencies}) if len(set_ids) < 2: raise RuntimeError("Could not find two distinct set IDs in time frequencies.") set_id_1, set_id_2 = set_ids[0], set_ids[1] print(f"Using timesteps (set IDs): {set_id_1}, {set_id_2}") .. rst-class:: sphx-glr-script-out .. code-block:: none Using timesteps (set IDs): 1, 2 .. GENERATED FROM PYTHON SOURCE LINES 93-96 Create named selections ------------------------ Create named selections based on element IDs for filtering results. .. GENERATED FROM PYTHON SOURCE LINES 96-114 .. code-block:: Python ns_1 = sol.create_named_selection( models.NamedSelectionCreate( name="Elements NS 1", type=models.NamedSelectionType.NAMED_SELECTION_TYPE_ELEMENT, element_ids=[models.IdsScoping(range=models.Range(min=23, max=28))], ) ) ns_2 = sol.create_named_selection( models.NamedSelectionCreate( name="Elements NS 2", type=models.NamedSelectionType.NAMED_SELECTION_TYPE_ELEMENT, element_ids=[models.IdsScoping(values=[3, 4, 7, 8, 9, 11, 13, 15, 17, 19])], ) ) print(f"Created named selections: {ns_1.name}, {ns_2.name}") .. rst-class:: sphx-glr-script-out .. code-block:: none Created named selections: Elements NS 1, Elements NS 2 .. GENERATED FROM PYTHON SOURCE LINES 115-118 Create plot definitions ------------------------- Create displacement and velocity plots using the named selections. .. GENERATED FROM PYTHON SOURCE LINES 118-153 .. code-block:: Python existing_view_ids = {v.id for v in sol.views} plot_1 = sol.create_plot( PlotDefinition( name=f"Displacement - {ns_1.name} - set {set_id_1}", result_type=ResultType.displacement, location=Location.nodal, fields=[ Field( name=ResultFieldName.displacement, components=[Component.X, Component.Y, Component.Z], ) ], named_selection_id=ns_1.id, set_ids=[set_id_1], all_sets=False, last_set=False, ) ) existing_view_ids = {v.id for v in sol.views} plot_2 = sol.create_plot( PlotDefinition( name=f"Velocity - {ns_2.name} - set {set_id_2}", result_type=ResultType.velocity, location=Location.nodal, fields=[ Field(name=ResultFieldName.velocity, components=[Component.X, Component.Y, Component.Z]) ], named_selection_id=ns_2.id, set_ids=[set_id_2], all_sets=False, last_set=False, ) ) .. GENERATED FROM PYTHON SOURCE LINES 154-157 Assign plots to viewports --------------------------- Display the plots in side-by-side viewports for comparison. .. GENERATED FROM PYTHON SOURCE LINES 157-183 .. code-block:: Python left_viewport = workspace.assign_view(view=plot_1, wait=True) right_viewport = workspace.create_viewport( viewport=left_viewport, direction=models.ViewportDirection.VIEWPORT_DIRECTION_RIGHT, ) right_viewport.set_view(plot_2, wait=True) print("Opened plots in two side-by-side viewports.") print(f" - Left viewport: {plot_1.name}") print(f" - Right viewport: {plot_2.name}") # take screenshot of the two viewports left_viewport.display_options.show_mesh_edges = True left_viewport.save_snapshot( "left_viewport.png", settings=get_example_snapshot_settings(), ) right_viewport.display_options.show_mesh_edges = True right_viewport.save_snapshot( "right_viewport.png", settings=get_example_snapshot_settings(), ) rx.stop() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/images/sphx_glr_010-create-plots-on-named-selections_001.png :alt: 010 create plots on named selections :srcset: /examples/images/sphx_glr_010-create-plots-on-named-selections_001.png :class: sphx-glr-multi-img * .. image-sg:: /examples/images/sphx_glr_010-create-plots-on-named-selections_002.png :alt: 010 create plots on named selections :srcset: /examples/images/sphx_glr_010-create-plots-on-named-selections_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none Opened plots in two side-by-side viewports. - Left viewport: Displacement - Elements NS 1 - set 1 - Right viewport: Velocity - Elements NS 2 - set 2 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 22.379 seconds) .. _sphx_glr_download_examples_010-create-plots-on-named-selections.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 010-create-plots-on-named-selections.ipynb <010-create-plots-on-named-selections.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 010-create-plots-on-named-selections.py <010-create-plots-on-named-selections.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 010-create-plots-on-named-selections.zip <010-create-plots-on-named-selections.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_