From e6900ed9464979bb42dabbe3d4b686cffca23936 Mon Sep 17 00:00:00 2001 From: sujal-tipnis <97630067+sujal-tipnis@users.noreply.github.com> Date: Wed, 6 Apr 2022 09:39:46 -0400 Subject: [PATCH] Add 3 examples for Parametric Workflows (#250) * Add 3 examples for Parametric Workflows * Add 3 examples for Parametric Workflows indentation change * remove pandas dataframe display * Disable archive operation as it is failing in server * Update examples/01-parametric/parametric_static_mixer_1.py Co-authored-by: Kathy Pippert <84872299+PipKat@users.noreply.github.com> * Update examples/01-parametric/parametric_static_mixer_1.py Co-authored-by: Kathy Pippert <84872299+PipKat@users.noreply.github.com> * Update examples/01-parametric/parametric_static_mixer_1.py Co-authored-by: Kathy Pippert <84872299+PipKat@users.noreply.github.com> * Update examples/01-parametric/parametric_static_mixer_1.py Co-authored-by: Kathy Pippert <84872299+PipKat@users.noreply.github.com> * Update examples/01-parametric/parametric_static_mixer_1.py Co-authored-by: Kathy Pippert <84872299+PipKat@users.noreply.github.com> * Update examples/01-parametric/parametric_static_mixer_2.py Co-authored-by: Kathy Pippert <84872299+PipKat@users.noreply.github.com> * Update examples/01-parametric/parametric_static_mixer_2.py Co-authored-by: Kathy Pippert <84872299+PipKat@users.noreply.github.com> * Update examples/01-parametric/parametric_static_mixer_3.py Co-authored-by: Kathy Pippert <84872299+PipKat@users.noreply.github.com> * Update examples/01-parametric/parametric_static_mixer_2.py Co-authored-by: Kathy Pippert <84872299+PipKat@users.noreply.github.com> * Update examples/01-parametric/parametric_static_mixer_1.py Co-authored-by: Dan Williams * made formatting changes to the script * fixed style * minor variable name changes * minor style change * Re-enable project archive (#259) * make variable name changes * make variable name changes(2) Co-authored-by: U-ANSYS\stipnis Co-authored-by: Mainak Kundu Co-authored-by: Kathy Pippert <84872299+PipKat@users.noreply.github.com> Co-authored-by: Dan Williams Co-authored-by: Mainak Kundu <94432368+mkundu1@users.noreply.github.com> --- doc/source/conf.py | 4 + .../parametric_static_mixer_1.py | 220 ++++++++++++++++++ .../parametric_static_mixer_2.py | 77 ++++++ .../parametric_static_mixer_3.py | 94 ++++++++ requirements_docs.txt | 1 + 5 files changed, 396 insertions(+) create mode 100755 examples/01-parametric/parametric_static_mixer_1.py create mode 100755 examples/01-parametric/parametric_static_mixer_2.py create mode 100755 examples/01-parametric/parametric_static_mixer_3.py diff --git a/doc/source/conf.py b/doc/source/conf.py index 577fa34ba7b..21b07d10f6d 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -112,6 +112,10 @@ def _start_or_stop_fluent_container(gallery_conf, fname, when): if fname in ["mixing_elbow_settings_api.py", "mixing_elbow_tui_api.py"]: args = ["3ddp", "-t4", "-meshing"] + elif fname in ["parametric_static_mixer_1.py", + "parametric_static_mixer_2.py", + "parametric_static_mixer_3.py"]: + args = ["3ddp", "-t4"] subprocess.run([sys.executable, _START_FLUENT_FILE] + args) elif when == "after": subprocess.run([sys.executable, _STOP_FLUENT_FILE]) diff --git a/examples/01-parametric/parametric_static_mixer_1.py b/examples/01-parametric/parametric_static_mixer_1.py new file mode 100755 index 00000000000..af7c9e5fd06 --- /dev/null +++ b/examples/01-parametric/parametric_static_mixer_1.py @@ -0,0 +1,220 @@ +""" +.. _ref_parametric_static_mixer_1: + +Parametric Study Workflow +------------------------------ +This example for executing a parametric study workflow +performs these steps: + +- Reads a case file and data file +- Creates input and output parameters +- Instantiates a design point study +- Accesses and modifies the input parameters of + the base design point (DP) +- Updates the current DP +- Accesses output parameters of the base DP +- Creates, updates, and deletes more DPs +- Creates, renames, and deletes parametric studies + +""" + +############################################################################ +# Import the pyfluent module +import ansys.fluent.core as pyfluent + +############################################################################ +# Import the path module +from pathlib import Path + +############################################################################ +# Launch Fluent in 3D and double precision + +session = pyfluent.launch_fluent(precision="double", processor_count=4) + +############################################################################ +# Enable the settings API (Beta) + +root = session.get_settings_root() + +############################################################################ +# Read the hopper/mixer case + +from ansys.fluent.core import examples + +import_filename = examples.download_file( + "Static_Mixer_main.cas.h5", "pyfluent/static_mixer" +) + +session.tui.solver.file.read_case(case_file_name=import_filename) + +############################################################################ +# Set number of iterations to 1000 to ensure convergence + +session.tui.solver.solve.set.number_of_iterations("1000") + +############################################################################ +# Create input parameters after enabling parameter creation in the TUI: +# Parameter values: +# Inlet1: velocity (inlet1_vel) 5 m/s and temperature (inlet1_temp) at 300 K +# Inlet2: velocity (inlet2_vel) 10 m/s and temperature (inlet2_temp) at 350 K + +session.tui.solver.define.parameters.enable_in_TUI("yes") + +session.tui.solver.define.boundary_conditions.set.velocity_inlet( + "inlet1", (), "vmag", "yes", "inlet1_vel", 5, "quit" +) +session.tui.solver.define.boundary_conditions.set.velocity_inlet( + "inlet1", (), "temperature", "yes", "inlet1_temp", 300, "quit" +) + +session.tui.solver.define.boundary_conditions.set.velocity_inlet( + "inlet2", (), "vmag", "yes", "no", "inlet2_vel", 10, "quit" +) +session.tui.solver.define.boundary_conditions.set.velocity_inlet( + "inlet2", (), "temperature", "yes", "no", "inlet2_temp", 350, "quit" +) + +########################################################################### +# Create output parameters using report definitions + +root.solution.report_definitions.surface["outlet-temp-avg"] = {} +root.solution.report_definitions.surface[ + "outlet-temp-avg" +].report_type = "surface-areaavg" +root.solution.report_definitions.surface[ + "outlet-temp-avg" +].field = "temperature" +root.solution.report_definitions.surface["outlet-temp-avg"].surface_names = [ + "outlet" +] + +root.solution.report_definitions.surface["outlet-vel-avg"] = {} +root.solution.report_definitions.surface[ + "outlet-vel-avg" +].report_type = "surface-areaavg" +root.solution.report_definitions.surface[ + "outlet-vel-avg" +].field = "velocity-magnitude" +root.solution.report_definitions.surface["outlet-vel-avg"].surface_names = [ + "outlet" +] + +session.tui.solver.define.parameters.enable_in_TUI("yes") +session.tui.solver.define.parameters.output_parameters.create( + "report-definition", "outlet-temp-avg" +) +session.tui.solver.define.parameters.output_parameters.create( + "report-definition", "outlet-vel-avg" +) + +########################################################################### +# Enable convergence condition check + +session.tui.solver.solve.monitors.residual.criterion_type("0") + +########################################################################### +# Write case with all the settings in place +case_path = str( + Path(pyfluent.EXAMPLES_PATH) / "Static_Mixer_Parameters.cas.h5" +) +session.tui.solver.file.write_case(case_path) + +########################################################################### +# Parametric study workflow +# Import the parametric study module + +from ansys.fluent.parametric import ParametricStudy + +########################################################################### +# Instantiate a parametric study from a Fluent session + +study_1 = ParametricStudy(root.parametric_studies).initialize() + +########################################################################### +# Access and modify input parameters of base DP + +input_parameters_update = study_1.design_points["Base DP"].input_parameters +input_parameters_update["inlet1_vel"] = 15 +study_1.design_points["Base DP"].input_parameters = input_parameters_update + +########################################################################### +# Update current design point + +study_1.update_current_design_point() + +########################################################################### +# Change value of specific design points + +design_point_1 = study_1.add_design_point() +design_point_1_input_parameters = study_1.design_points["DP1"].input_parameters +design_point_1_input_parameters["inlet1_temp"] = 450 +design_point_1_input_parameters["inlet1_vel"] = 30 +design_point_1_input_parameters["inlet2_vel"] = 20 +study_1.design_points["DP1"].input_parameters = design_point_1_input_parameters + +########################################################################### +# Add another design point with different values of the input parameters + +design_point_2 = study_1.add_design_point() +design_point_2_input_parameters = study_1.design_points["DP2"].input_parameters +design_point_2_input_parameters["inlet1_temp"] = 500 +design_point_2_input_parameters["inlet1_vel"] = 45 +design_point_2_input_parameters["inlet2_vel"] = 30 +study_1.design_points["DP2"].input_parameters = design_point_2_input_parameters + +########################################################################## +# Duplicate design points + +design_point_3 = study_1.duplicate_design_point(design_point_2) + +######################################################################### +# Update all design points for study 1 + +study_1.update_all_design_points() + +######################################################################### +# Export design point table as a CSV table + +design_point_table = str( + Path(pyfluent.EXAMPLES_PATH) / "design_point_table_study_1.csv" +) +study_1.export_design_table(design_point_table) + +######################################################################### +# Display CSV table as a pandas dataframe + +import pandas as pd + +data_frame = pd.read_csv(design_point_table) +print(data_frame) + +########################################################################## +# Delete design points + +study_1.delete_design_points([design_point_1, design_point_2]) + +########################################################################## +# Create a new parametric study by duplicating the current one + +study_2 = study_1.duplicate() + +######################################################################### +# Rename the newly created parametric study +# Currently affected by issue # 249, hence commented out + +# study_2.rename("New Study") + +######################################################################### +# Delete the old parametric study +# Currently affected by issue #249, hence commented out + +# study_1.delete() + +######################################################################### +# Save parametric project + +project_filepath = str( + Path(pyfluent.EXAMPLES_PATH) / "static_mixer_study.flprj" +) + +session.tui.solver.file.parametric_project.save_as(project_filepath) diff --git a/examples/01-parametric/parametric_static_mixer_2.py b/examples/01-parametric/parametric_static_mixer_2.py new file mode 100755 index 00000000000..9a42c4e60c8 --- /dev/null +++ b/examples/01-parametric/parametric_static_mixer_2.py @@ -0,0 +1,77 @@ +""" +.. _ref_parametric_static_mixer_2: + +Parametric Project-Based Workflow +---------------------------------------------------- +This example for executing a parametric project-based workflow +performs these steps: + +- Instantiates a parametric study from a Fluent session +- Reads the previously saved project ``- static_mixer_study.flprj`` +- Saves the current project +- Saves the current project to a different file name +- Exports the current project +- Archives the current project +- Exits the parametric project-based workflow + + +""" + +######################################################################### +# Parametric project-based workflow + +######################################################################### +# Import the parametric project module and the parametric study module + +from ansys.fluent.parametric import ParametricProject + +############################################################################ +# Import the pyfluent module and path + +import ansys.fluent.core as pyfluent +from pathlib import Path + +######################################################################### +# Launch Fluent and enable the settings API (Beta) + +session = pyfluent.launch_fluent(precision="double", processor_count=4) +root = session.get_settings_root() + +######################################################################### +# Read the previously saved project - static_mixer_study.flprj + +project_filepath_read = str( + Path(pyfluent.EXAMPLES_PATH) / "static_mixer_study.flprj" +) + +proj = ParametricProject( + root.file.parametric_project, + root.parametric_studies, + project_filepath_read, +) + +######################################################################### +# Save the current project + +proj.save() + +######################################################################### +# Save the current project to a different file name + +project_filepath_save_as = str( + Path(pyfluent.EXAMPLES_PATH) / "static_mixer_study_save_as.flprj" +) +proj.save_as(project_filepath=project_filepath_save_as) + +######################################################################### +# Export the current project + +project_filepath_export = str( + Path(pyfluent.EXAMPLES_PATH) / "static_mixer_study_export.flprj" +) +proj.export(project_filepath=project_filepath_export) + +######################################################################### +# Archive the current project + +proj.archive() diff --git a/examples/01-parametric/parametric_static_mixer_3.py b/examples/01-parametric/parametric_static_mixer_3.py new file mode 100755 index 00000000000..7bc3f9cce5c --- /dev/null +++ b/examples/01-parametric/parametric_static_mixer_3.py @@ -0,0 +1,94 @@ +""" +.. _ref_parametric_static_mixer_3: + +Parametric Session Workflow +---------------------------------------------- +This example for executing a parametric session workflow performs these steps: + +- Launches a parametric session using the hopper/mixer case file +- Prints the input parameters of the current parametric session +- Accesses the current study of the current parametric session +- Creates a new study in a parametric session +- Renames this newly created study +- Creates a new parametric session using the flprj saved earlier + + +""" + +######################################################################### +# Parametric session workflow +# Import the parametric session workflow + +from ansys.fluent.parametric import ParametricSession + +############################################################################ +# Import the pyfluent module and path + +import ansys.fluent.core as pyfluent +from pathlib import Path + +######################################################################### +# Launch parametric session using the hopper/mixer case File +# This case file contains pre-created input and output parameters + +case_path = str( + Path(pyfluent.EXAMPLES_PATH) / "Static_Mixer_Parameters.cas.h5" +) + +session = ParametricSession(case_filepath=case_path) + +######################################################################### +# Print the input parameters of the current parametric session. + +session.studies["Static_Mixer_Parameters-Solve"].design_points[ + "Base DP" +].input_parameters + +######################################################################### +# Access the current study of the current parametric session + +study_1 = session.studies["Static_Mixer_Parameters-Solve"] + +input_parameters_update = study_1.design_points["Base DP"].input_parameters +input_parameters_update["inlet1_vel"] = 15 +study_1.design_points["Base DP"].input_parameters = input_parameters_update + +design_point_1 = study_1.add_design_point() +design_point_1_input_parameters = study_1.design_points["DP1"].input_parameters +design_point_1_input_parameters["inlet1_temp"] = 323 +design_point_1_input_parameters["inlet1_vel"] = 33 +design_point_1_input_parameters["inlet2_vel"] = 25 +study_1.design_points["DP1"].input_parameters = design_point_1_input_parameters + +######################################################################### +# In this parametric project create a new study + +study_2 = session.new_study() + +######################################################################### +# Update all design points +study_2.update_all_design_points() + +######################################################################### +# Export design point table as a CSV table + +design_point_table_study_2 = str( + Path(pyfluent.EXAMPLES_PATH) / "design_point_table_study_2.csv" +) +study_2.export_design_table(design_point_table_study_2) + +######################################################################### +# Display CSV table as a pandas dataframe + +import pandas as pd + +data_frame = pd.read_csv(design_point_table_study_2) +print(data_frame) + +######################################################################### +# Access a new parametric session using the flprj saved earlier + +project_session_filepath = str( + Path(pyfluent.EXAMPLES_PATH) / "static_mixer_study_save_as.flprj" +) +new_session = ParametricSession(project_filepath=project_session_filepath) diff --git a/requirements_docs.txt b/requirements_docs.txt index 21c8ab13648..c8d091e20b2 100644 --- a/requirements_docs.txt +++ b/requirements_docs.txt @@ -11,3 +11,4 @@ sphinx-gallery==0.10.1 sphinx-notfound-page==0.8 sphinxcontrib-websupport==1.2.4 sphinxemoji==0.2.0 +pandas==1.4.1 \ No newline at end of file