.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/gallery_examples/03_parametric_study.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_gallery_examples_03_parametric_study.py: Parametric study ================ This example shows how to use PyAdditive to perform a parametric study. You perform a parametric study if you want to optimize additive machine parameters to achieve a specific result. This examples first uses the :class:`ParametricStudy` class to perform the parametric study. It then uses the :obj:`display ` package in PyAdditive-Widgets to visualize the results of the study. Units are SI (m, kg, s, K) unless otherwise noted. .. GENERATED FROM PYTHON SOURCE LINES 36-39 Perform required imports and create study ----------------------------------------- Perform the required imports and create a :class:`ParametricStudy` instance. .. GENERATED FROM PYTHON SOURCE LINES 39-46 .. code-block:: Python from ansys.additive.core import Additive, SimulationStatus, SimulationType from ansys.additive.core.parametric_study import ColumnNames, ParametricStudy import ansys.additive.core.parametric_study.display as display study = ParametricStudy("demo-study") .. rst-class:: sphx-glr-script-out .. code-block:: none Saving parametric study to /home/runner/work/pyadditive-widgets/pyadditive-widgets/examples/demo-study.ps .. GENERATED FROM PYTHON SOURCE LINES 47-52 Get name of study file ---------------------- The current state of the parametric study is saved to a file upon each update. This code retrieves the name of the file. This file uses a binary format and is not human readable. .. GENERATED FROM PYTHON SOURCE LINES 52-55 .. code-block:: Python print(study.file_name) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/pyadditive-widgets/pyadditive-widgets/examples/demo-study.ps .. GENERATED FROM PYTHON SOURCE LINES 56-61 Select material for study ------------------------- Select a material to use in the study. The material name must be known by the Additive service. You can connect to the Additive service and print a list of available materials prior to selecting one. .. GENERATED FROM PYTHON SOURCE LINES 61-66 .. code-block:: Python additive = Additive() print("Available material names: {}".format(additive.materials_list())) material = "IN718" .. rst-class:: sphx-glr-script-out .. code-block:: none user data path: /home/runner/.local/share/pyadditive Available material names: ['Ti64', 'IN718', 'AlSi10Mg', 'Al357', '17-4PH', 'CoCr', 'IN625', '316L'] .. GENERATED FROM PYTHON SOURCE LINES 67-75 Create single bead evaluation ----------------------------- Parametric studies often start with single bead simulations to determine melt pool statistics. The following code uses the :meth:`~ParametricStudy.generate_single_bead_permutations` method to generate single bead simulation permutations. This method's parameters allow you to specify a range of machine parameters and filter them by energy density. Not all of the parameters listed are required. Optional parameters that are not specified use the default values defined in the :class:`MachineConstants` class. .. GENERATED FROM PYTHON SOURCE LINES 75-107 .. code-block:: Python import numpy as np # Specify a range of laser powers. Valid values are 50 to 700 W. initial_powers = np.linspace(50, 700, 7) # Specify a range of laser scan speeds. Valid values are 0.35 to 2.5 m/s. initial_scan_speeds = np.linspace(0.35, 2.5, 5) # Specify powder layer thicknesses. Valid values are 10e-6 to 100e-6 m. initial_layer_thicknesses = [40e-6, 50e-6] # Specify laser beam diameters. Valid values are 20e-6 to 140e-6 m. initial_beam_diameters = [80e-6] # Specify heater temperatures. Valid values are 20 - 500 C. initial_heater_temps = [80] # Restrict the permutations within a range of energy densities # For single bead, the energy density is laser power / (laser scan speed * layer thickness). min_energy_density = 2e6 max_energy_density = 8e6 # Specify a bead length in meters. bead_length = 0.001 study.generate_single_bead_permutations( material_name=material, bead_length=bead_length, laser_powers=initial_powers, scan_speeds=initial_scan_speeds, layer_thicknesses=initial_layer_thicknesses, beam_diameters=initial_beam_diameters, heater_temperatures=initial_heater_temps, min_area_energy_density=min_energy_density, max_area_energy_density=max_energy_density, ) .. GENERATED FROM PYTHON SOURCE LINES 108-112 Show simulations as a table --------------------------- You can use the :obj:`display ` package to list the simulations as a table. .. GENERATED FROM PYTHON SOURCE LINES 112-115 .. code-block:: Python display.show_table(study) .. image-sg:: /examples/gallery_examples/images/sphx_glr_03_parametric_study_001.png :alt: 03 parametric study :srcset: /examples/gallery_examples/images/sphx_glr_03_parametric_study_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none show_table .. GENERATED FROM PYTHON SOURCE LINES 116-124 Skip some simulations --------------------- If you are working with a large parametric study, you may want to skip some simulations to reduce processing time. To do so, set the simulation status, which is defined in the :class:`SimulationStatus` class, to :obj:`SimulationStatus.SKIP`. The following code obtains a :class:`~pandas.DataFrame` object, applies a filter to get a list of simulation IDs, and then updates the status on the simulations with those IDs. .. GENERATED FROM PYTHON SOURCE LINES 124-134 .. code-block:: Python df = study.data_frame() # Get IDs for single bead simulations with laser power below 75 W. ids = df.loc[ (df[ColumnNames.LASER_POWER] < 75) & (df[ColumnNames.TYPE] == SimulationType.SINGLE_BEAD), ColumnNames.ID, ].tolist() study.set_status(ids, SimulationStatus.SKIP) display.show_table(study) .. image-sg:: /examples/gallery_examples/images/sphx_glr_03_parametric_study_002.png :alt: 03 parametric study :srcset: /examples/gallery_examples/images/sphx_glr_03_parametric_study_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none show_table .. GENERATED FROM PYTHON SOURCE LINES 135-139 Run single bead simulations --------------------------- Run the simulations using the :meth:`~ParametricStudy.run_simulations` method. All simulations with a :obj:`SimulationStatus.PENDING` status are executed. .. GENERATED FROM PYTHON SOURCE LINES 139-142 .. code-block:: Python study.run_simulations(additive) .. rst-class:: sphx-glr-script-out .. code-block:: none 2024-06-14 20:10:26 Completed 0 of 31 simulations 2024-06-14 20:10:48 Completed 1 of 31 simulations 2024-06-14 20:11:03 Completed 2 of 31 simulations 2024-06-14 20:11:17 Completed 3 of 31 simulations 2024-06-14 20:11:29 Completed 4 of 31 simulations 2024-06-14 20:11:44 Completed 5 of 31 simulations 2024-06-14 20:11:58 Completed 6 of 31 simulations 2024-06-14 20:12:11 Completed 7 of 31 simulations 2024-06-14 20:12:23 Completed 8 of 31 simulations 2024-06-14 20:12:38 Completed 9 of 31 simulations 2024-06-14 20:12:54 Completed 10 of 31 simulations 2024-06-14 20:13:11 Completed 11 of 31 simulations 2024-06-14 20:13:24 Completed 12 of 31 simulations 2024-06-14 20:13:36 Completed 13 of 31 simulations 2024-06-14 20:13:50 Completed 14 of 31 simulations 2024-06-14 20:14:03 Completed 15 of 31 simulations 2024-06-14 20:14:18 Completed 16 of 31 simulations 2024-06-14 20:14:34 Completed 17 of 31 simulations 2024-06-14 20:14:47 Completed 18 of 31 simulations 2024-06-14 20:14:59 Completed 19 of 31 simulations 2024-06-14 20:15:13 Completed 20 of 31 simulations 2024-06-14 20:15:28 Completed 21 of 31 simulations 2024-06-14 20:15:45 Completed 22 of 31 simulations 2024-06-14 20:16:01 Completed 23 of 31 simulations 2024-06-14 20:16:25 Completed 24 of 31 simulations 2024-06-14 20:16:48 Completed 25 of 31 simulations 2024-06-14 20:17:02 Completed 26 of 31 simulations 2024-06-14 20:17:19 Completed 27 of 31 simulations 2024-06-14 20:17:35 Completed 28 of 31 simulations 2024-06-14 20:17:59 Completed 29 of 31 simulations 2024-06-14 20:18:16 Completed 30 of 31 simulations 2024-06-14 20:18:29 Completed 31 of 31 simulations .. GENERATED FROM PYTHON SOURCE LINES 143-147 Save study to CSV file ---------------------- The parametric study is saved with each update in a binary format. For other formats, use the ``to_*`` methods provided by the :class:`~pandas.DataFrame` class. .. GENERATED FROM PYTHON SOURCE LINES 147-150 .. code-block:: Python study.data_frame().to_csv("demo-study.csv") .. GENERATED FROM PYTHON SOURCE LINES 151-155 Plot single bead results ------------------------ Plot the single bead results using the :func:`~ansys.additive.widgets.display.single_bead_eval_plot` function. .. GENERATED FROM PYTHON SOURCE LINES 155-158 .. code-block:: Python display.single_bead_eval_plot(study) .. image-sg:: /examples/gallery_examples/images/sphx_glr_03_parametric_study_003.png :alt: 03 parametric study :srcset: /examples/gallery_examples/images/sphx_glr_03_parametric_study_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none single_bead_eval_plot .. GENERATED FROM PYTHON SOURCE LINES 159-171 Create porosity evaluation -------------------------- You can use the insights gained from the single bead evaluation to generate parameters for a porosity evaluation. Alternatively, you can perform a porosity evaluation without a previous single bead evaluation. The following code determines the laser power and scan speeds by filtering the single bead results, where the ratio of the melt pool reference depth to reference width is within a specified range. Additionally, it restricts the simulations to a minimum build rate, which is calculated as scan speed * layer thickness * hatch spacing. Finally, it uses the :meth:`~ParametricStudy.generate_porosity_permutations` method to add porosity simulations to the study. .. GENERATED FROM PYTHON SOURCE LINES 171-195 .. code-block:: Python df = study.data_frame() df = df[ (df[ColumnNames.MELT_POOL_REFERENCE_DEPTH_OVER_WIDTH] >= 0.3) & (df[ColumnNames.MELT_POOL_REFERENCE_DEPTH_OVER_WIDTH] <= 0.65) ] study.generate_porosity_permutations( material_name=material, laser_powers=df[ColumnNames.LASER_POWER].unique(), scan_speeds=df[ColumnNames.SCAN_SPEED].unique(), size_x=1e-3, size_y=1e-3, size_z=1e-3, layer_thicknesses=[40e-6], heater_temperatures=[80], beam_diameters=[80e-6], start_angles=[45], rotation_angles=[67.5], hatch_spacings=[100e-6], min_build_rate=5e-9, iteration=1, ) .. GENERATED FROM PYTHON SOURCE LINES 196-199 Run porosity simulations ------------------------ Run the simulations using the :meth:`~ParametricStudy.run_simulations` method. .. GENERATED FROM PYTHON SOURCE LINES 199-202 .. code-block:: Python study.run_simulations(additive) .. rst-class:: sphx-glr-script-out .. code-block:: none 2024-06-14 20:18:32 Completed 0 of 15 simulations 2024-06-14 20:19:23 Completed 1 of 15 simulations 2024-06-14 20:20:02 Completed 2 of 15 simulations 2024-06-14 20:20:34 Completed 3 of 15 simulations 2024-06-14 20:21:31 Completed 4 of 15 simulations 2024-06-14 20:22:12 Completed 5 of 15 simulations 2024-06-14 20:22:47 Completed 6 of 15 simulations 2024-06-14 20:23:56 Completed 7 of 15 simulations 2024-06-14 20:24:41 Completed 8 of 15 simulations 2024-06-14 20:25:15 Completed 9 of 15 simulations 2024-06-14 20:27:20 Completed 10 of 15 simulations 2024-06-14 20:28:11 Completed 11 of 15 simulations 2024-06-14 20:28:48 Completed 12 of 15 simulations 2024-06-14 20:31:48 Completed 13 of 15 simulations 2024-06-14 20:33:01 Completed 14 of 15 simulations 2024-06-14 20:33:42 Completed 15 of 15 simulations .. GENERATED FROM PYTHON SOURCE LINES 203-207 Plot porosity results --------------------- Plot the porosity simulation results using the :func:`~ansys.additive.widgets.display.porosity_contour_plot` method. .. GENERATED FROM PYTHON SOURCE LINES 207-210 .. code-block:: Python display.porosity_contour_plot(study) .. image-sg:: /examples/gallery_examples/images/sphx_glr_03_parametric_study_004.png :alt: 03 parametric study :srcset: /examples/gallery_examples/images/sphx_glr_03_parametric_study_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none porosity_contour_plot .. GENERATED FROM PYTHON SOURCE LINES 211-219 Create microstructure evaluation -------------------------------- The following code generates a set of microstructure simulations using many of the same parameters used for the porosity simulations. Because the ``cooling_rate``, ``thermal_gradient``, ``melt_pool_width``, and ``melt_pool_depth`` parameters are not specified, they are calculated. The code then uses the :meth:`~ParametricStudy.generate_microstructure_permutations` method to add microstructure simulations to the study. .. GENERATED FROM PYTHON SOURCE LINES 219-240 .. code-block:: Python df = study.data_frame() df = df[(df[ColumnNames.TYPE] == SimulationType.POROSITY)] study.generate_microstructure_permutations( material_name=material, laser_powers=df[ColumnNames.LASER_POWER].unique(), scan_speeds=df[ColumnNames.SCAN_SPEED].unique(), size_x=1e-3, size_y=1e-3, size_z=1.1e-3, sensor_dimension=1e-4, layer_thicknesses=df[ColumnNames.LAYER_THICKNESS].unique(), heater_temperatures=df[ColumnNames.HEATER_TEMPERATURE].unique(), beam_diameters=df[ColumnNames.BEAM_DIAMETER].unique(), start_angles=df[ColumnNames.START_ANGLE].unique(), rotation_angles=df[ColumnNames.ROTATION_ANGLE].unique(), hatch_spacings=df[ColumnNames.HATCH_SPACING].unique(), iteration=2, ) .. GENERATED FROM PYTHON SOURCE LINES 241-244 Run microstructure simulations ------------------------------ Run the simulations using the :meth:`~ParametricStudy.run_simulations` method. .. GENERATED FROM PYTHON SOURCE LINES 244-247 .. code-block:: Python study.run_simulations(additive) .. rst-class:: sphx-glr-script-out .. code-block:: none 2024-06-14 20:33:44 Completed 0 of 15 simulations 2024-06-14 20:34:35 Completed 1 of 15 simulations 2024-06-14 20:35:15 Completed 2 of 15 simulations 2024-06-14 20:35:52 Completed 3 of 15 simulations 2024-06-14 20:36:43 Completed 4 of 15 simulations 2024-06-14 20:37:24 Completed 5 of 15 simulations 2024-06-14 20:38:00 Completed 6 of 15 simulations 2024-06-14 20:38:57 Completed 7 of 15 simulations 2024-06-14 20:39:39 Completed 8 of 15 simulations 2024-06-14 20:40:15 Completed 9 of 15 simulations 2024-06-14 20:41:37 Completed 10 of 15 simulations 2024-06-14 20:42:24 Completed 11 of 15 simulations 2024-06-14 20:43:02 Completed 12 of 15 simulations 2024-06-14 20:45:19 Completed 13 of 15 simulations 2024-06-14 20:46:18 Completed 14 of 15 simulations 2024-06-14 20:46:57 Completed 15 of 15 simulations .. GENERATED FROM PYTHON SOURCE LINES 248-253 Plot microstructure results --------------------------- Plot and compare the average grain sizes from the microstructure simulations using the :func:`~ansys.additive.widgets.display.ave_grain_size_plot` function. .. GENERATED FROM PYTHON SOURCE LINES 253-255 .. code-block:: Python display.ave_grain_size_plot(study) .. image-sg:: /examples/gallery_examples/images/sphx_glr_03_parametric_study_005.png :alt: 03 parametric study :srcset: /examples/gallery_examples/images/sphx_glr_03_parametric_study_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ave_grain_size_plot .. rst-class:: sphx-glr-timing **Total running time of the script:** (36 minutes 38.885 seconds) .. _sphx_glr_download_examples_gallery_examples_03_parametric_study.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 03_parametric_study.ipynb <03_parametric_study.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 03_parametric_study.py <03_parametric_study.py>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_