Integrating PyFluent with PyWorkbench:#

This example showcases how to use PyFluent workflow together with PyWorkbench - (Python client scripting for Ansys Workbench).

This example sets up and solves a three-dimensional turbulent fluid flow and heat transfer problem in a mixing elbow, which is common in piping systems in power plants and process industries. Predicting the flow field and temperature field in the area of the mixing region is important to designing the junction properly.

This example uses Pyfluent settings objects API’s.

Problem description#

A cold fluid at 20 deg C flows into the pipe through a large inlet. It then mixes with a warmer fluid at 40 deg C that enters through a smaller inlet located at the elbow. The pipe dimensions are in inches, and the fluid properties and boundary conditions are given in SI units. Because the Reynolds number for the flow at the larger inlet is 50,800, a turbulent flow model is required.

Performed required imports#

Performing essential imports for Ansys Workbench, Fluent Pythonic Interface and for downloading examples data.

[1]:
import os
import pathlib
from ansys.workbench.core import launch_workbench
import ansys.fluent.core as pyfluent
from ansys.fluent.core import examples
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 3
      1 import os
      2 import pathlib
----> 3 from ansys.workbench.core import launch_workbench
      4 import ansys.fluent.core as pyfluent
      5 from ansys.fluent.core import examples

ModuleNotFoundError: No module named 'ansys'

Specify client and server directories with launch WB service.#

[2]:
workdir = pathlib.Path("__file__").parent
[3]:
wb = launch_workbench(client_workdir=str(workdir.absolute()))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[3], line 1
----> 1 wb = launch_workbench(client_workdir=str(workdir.absolute()))

NameError: name 'launch_workbench' is not defined

Download the input file from example data and upload to server directory.#

[4]:
import_filename = examples.download_file("mixing_elbow.msh.h5", "pyfluent/mixing_elbow")
wb.upload_file(import_filename)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[4], line 1
----> 1 import_filename = examples.download_file("mixing_elbow.msh.h5", "pyfluent/mixing_elbow")
      2 wb.upload_file(import_filename)

NameError: name 'examples' is not defined

Generate a “FLUENT” System using Ansys Workbench Scripting API (used for Journaling) and parse it to the PyWorkbench API.#

[5]:
export_path = "wb_log_file.log"
wb.set_log_file(export_path)
wb.run_script_string('template1 = GetTemplate(TemplateName="FLUENT")', log_level="info")
wb.run_script_string("system1 = template1.CreateSystem()")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[5], line 2
      1 export_path = "wb_log_file.log"
----> 2 wb.set_log_file(export_path)
      3 wb.run_script_string('template1 = GetTemplate(TemplateName="FLUENT")', log_level="info")
      4 wb.run_script_string("system1 = template1.CreateSystem()")

NameError: name 'wb' is not defined

Launch Fluent & Connect to Fluent#

Launch Fluent as server with PyWorkbench API and and connect to Pyfluent session

[6]:
server_info_file = wb.start_fluent_server(system_name="FLU")
fluent_session = pyfluent.connect_to_fluent(server_info_file_name=server_info_file)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[6], line 1
----> 1 server_info_file = wb.start_fluent_server(system_name="FLU")
      2 fluent_session = pyfluent.connect_to_fluent(server_info_file_name=server_info_file)

NameError: name 'wb' is not defined

Import mesh and perform mesh check#

[7]:
# Import the mesh and perform a mesh check, which lists the minimum and maximum
# x, y, and z values from the mesh in the default SI units of meters. The mesh
# check also reports a number of other mesh features that are checked. Any errors
# in the mesh are reported. Ensure that the minimum volume is not negative because
# Fluent cannot begin a calculation when this is the case.

fluent_session.file.read_mesh(file_name=import_filename)
fluent_session.mesh.check()

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[7], line 7
      1 # Import the mesh and perform a mesh check, which lists the minimum and maximum
      2 # x, y, and z values from the mesh in the default SI units of meters. The mesh
      3 # check also reports a number of other mesh features that are checked. Any errors
      4 # in the mesh are reported. Ensure that the minimum volume is not negative because
      5 # Fluent cannot begin a calculation when this is the case.
----> 7 fluent_session.file.read_mesh(file_name=import_filename)
      8 fluent_session.mesh.check()

NameError: name 'fluent_session' is not defined

Set working units for mesh#

Set the working units for the mesh to inches. Because the default SI units are used for everything except length, you do not have to change any other units in this example. If you want working units for length to be other than inches (for example, millimeters), make the appropriate change.

[8]:
fluent_session.tui.define.units("length", "in")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[8], line 1
----> 1 fluent_session.tui.define.units("length", "in")

NameError: name 'fluent_session' is not defined

Enable heat transfer#

Enable heat transfer by activating the energy equation.

[9]:
fluent_session.setup.models.energy.enabled = True
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[9], line 1
----> 1 fluent_session.setup.models.energy.enabled = True

NameError: name 'fluent_session' is not defined

Create material#

Create a material named "water-liquid".

[10]:
fluent_session.setup.materials.database.copy_by_name(type="fluid", name="water-liquid")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[10], line 1
----> 1 fluent_session.setup.materials.database.copy_by_name(type="fluid", name="water-liquid")

NameError: name 'fluent_session' is not defined

Set up cell zone conditions#

Set up the cell zone conditions for the fluid zone (elbow-fluid). Set material to "water-liquid".

[11]:
fluent_session.setup.cell_zone_conditions.fluid['elbow-fluid'].general.material = "water-liquid"
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[11], line 1
----> 1 fluent_session.setup.cell_zone_conditions.fluid['elbow-fluid'].general.material = "water-liquid"

NameError: name 'fluent_session' is not defined

Set up boundary conditions for CFD analysis#

Set up the boundary conditions for the inlets, outlet, and walls for CFD analysis. - cold inlet (cold-inlet), Setting: Value: - Velocity Specification Method: Magnitude, Normal to Boundary - Velocity Magnitude: 0.4 [m/s] - Specification Method: Intensity and Hydraulic Diameter - Turbulent Intensity: 5 [%] - Hydraulic Diameter: 4 [inch] - Temperature: 293.15 [K]

[12]:
cold_inlet = fluent_session.setup.boundary_conditions.velocity_inlet["cold-inlet"]
cold_inlet.get_state()
cold_inlet.momentum.velocity.value = 0.4
cold_inlet.turbulence.turbulent_specification = "Intensity and Hydraulic Diameter"
cold_inlet.turbulence.turbulent_intensity = 0.05
cold_inlet.turbulence.hydraulic_diameter = "4 [in]"
cold_inlet.thermal.t.value = 293.15
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[12], line 1
----> 1 cold_inlet = fluent_session.setup.boundary_conditions.velocity_inlet["cold-inlet"]
      2 cold_inlet.get_state()
      3 cold_inlet.momentum.velocity.value = 0.4

NameError: name 'fluent_session' is not defined
  • hot inlet (hot-inlet), Setting: Value:

  • Velocity Specification Method: Magnitude, Normal to Boundary

  • Velocity Magnitude: 1.2 [m/s]

  • Specification Method: Intensity and Hydraulic Diameter

  • Turbulent Intensity: 5 [%]

  • Hydraulic Diameter: 1 [inch]

  • Temperature: 313.15 [K]

[13]:
hot_inlet = fluent_session.setup.boundary_conditions.velocity_inlet["hot-inlet"]
hot_inlet.momentum.velocity.value = 1.2
hot_inlet.turbulence.turbulent_specification = "Intensity and Hydraulic Diameter"
hot_inlet.turbulence.turbulent_intensity = 0.05
hot_inlet.turbulence.hydraulic_diameter = "1 [in]"
hot_inlet.thermal.t.value = 313.15
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[13], line 1
----> 1 hot_inlet = fluent_session.setup.boundary_conditions.velocity_inlet["hot-inlet"]
      2 hot_inlet.momentum.velocity.value = 1.2
      3 hot_inlet.turbulence.turbulent_specification = "Intensity and Hydraulic Diameter"

NameError: name 'fluent_session' is not defined
  • pressure outlet (outlet), Setting: Value:

  • Backflow Turbulent Intensity: 5 [%]

  • Backflow Turbulent Viscosity Ratio: 4

[14]:
fluent_session.setup.boundary_conditions.pressure_outlet[
    "outlet"
].turbulence.backflow_turbulent_viscosity_ratio = 4
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[14], line 1
----> 1 fluent_session.setup.boundary_conditions.pressure_outlet[
      2     "outlet"
      3 ].turbulence.backflow_turbulent_viscosity_ratio = 4

NameError: name 'fluent_session' is not defined

Initialize flow field#

[15]:
fluent_session.solution.initialization.hybrid_initialize()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[15], line 1
----> 1 fluent_session.solution.initialization.hybrid_initialize()

NameError: name 'fluent_session' is not defined

Solve for 150 iterations#

Setting iteration count to 150 to solve the model.

[16]:
fluent_session.solution.run_calculation.iter_count = 100
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[16], line 1
----> 1 fluent_session.solution.run_calculation.iter_count = 100

NameError: name 'fluent_session' is not defined

Update Solution using Workbench Journal Commands#

[17]:
script_string = """
solutionComponent1 = system1.GetComponent(Name="Solution")
system1 = GetSystem(Name="FLU")
solutionComponent1 = system1.GetComponent(Name="Solution")
solutionComponent1.Update(AllDependencies=True)
"""
[18]:
wb.run_script_string(script_string)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[18], line 1
----> 1 wb.run_script_string(script_string)

NameError: name 'wb' is not defined

Postprocessing#

Create and display velocity vectors on the symmetry-xyplane plane.

Configure graphics picture export#

Since Fluent is being run without the GUI, you must to export plots as picture files. Edit the picture settings to use a custom resolution so that the images are large enough.

[19]:
graphics = fluent_session.results.graphics
if graphics.picture.use_window_resolution.is_active():
    graphics.picture.use_window_resolution = False
graphics.picture.x_resolution = 1920
graphics.picture.y_resolution = 1440
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[19], line 1
----> 1 graphics = fluent_session.results.graphics
      2 if graphics.picture.use_window_resolution.is_active():
      3     graphics.picture.use_window_resolution = False

NameError: name 'fluent_session' is not defined

Create velocity vectors#

Create and display velocity vectors on the symmetry-xyplane plane. Then, export the image for inspection.

[20]:
graphics = fluent_session.results.graphics
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[20], line 1
----> 1 graphics = fluent_session.results.graphics

NameError: name 'fluent_session' is not defined
[21]:
graphics.vector["velocity_vector_symmetry"] = {}
velocity_symmetry = fluent_session.results.graphics.vector["velocity_vector_symmetry"]
velocity_symmetry.print_state()
velocity_symmetry.field = "velocity-magnitude"
velocity_symmetry.surfaces_list = [
    "symmetry-xyplane",
]
velocity_symmetry.scale.scale_f = 4
velocity_symmetry.style = "arrow"
velocity_symmetry.display()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[21], line 1
----> 1 graphics.vector["velocity_vector_symmetry"] = {}
      2 velocity_symmetry = fluent_session.results.graphics.vector["velocity_vector_symmetry"]
      3 velocity_symmetry.print_state()

NameError: name 'graphics' is not defined
[22]:
graphics.views.restore_view(view_name="front")
graphics.views.auto_scale()
graphics.picture.save_picture(file_name="velocity_vector_symmetry.png")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[22], line 1
----> 1 graphics.views.restore_view(view_name="front")
      2 graphics.views.auto_scale()
      3 graphics.picture.save_picture(file_name="velocity_vector_symmetry.png")

NameError: name 'graphics' is not defined

Compute mass flow rate#

Compute the mass flow rate.

[23]:
fluent_session.solution.report_definitions.flux["mass_flow_rate"] = {}

mass_flow_rate = fluent_session.solution.report_definitions.flux["mass_flow_rate"]
mass_flow_rate.boundaries = [
    "cold-inlet",
    "hot-inlet",
    "outlet",
]
mass_flow_rate.print_state()
fluent_session.solution.report_definitions.compute(report_defs=["mass_flow_rate"])
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[23], line 1
----> 1 fluent_session.solution.report_definitions.flux["mass_flow_rate"] = {}
      3 mass_flow_rate = fluent_session.solution.report_definitions.flux["mass_flow_rate"]
      4 mass_flow_rate.boundaries = [
      5     "cold-inlet",
      6     "hot-inlet",
      7     "outlet",
      8 ]

NameError: name 'fluent_session' is not defined

Exit Fluent Session#

[24]:
fluent_session.exit()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[24], line 1
----> 1 fluent_session.exit()

NameError: name 'fluent_session' is not defined

Save project#

[25]:
save_string = """import os
workdir = GetServerWorkingDirectory()
path = os.path.join(workdir, "mixing_elbow.wbpj")
Save(FilePath=path , Overwrite=True)"""
wb.run_script_string(save_string)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[25], line 5
      1 save_string = """import os
      2 workdir = GetServerWorkingDirectory()
      3 path = os.path.join(workdir, "mixing_elbow.wbpj")
      4 Save(FilePath=path , Overwrite=True)"""
----> 5 wb.run_script_string(save_string)

NameError: name 'wb' is not defined

Archive Project#

[26]:
archive_string ="""import os
workdir = GetServerWorkingDirectory()
path = os.path.join(workdir, "mixing_elbow.wbpz")
Archive(FilePath=path , IncludeExternalImportedFiles=True)"""
wb.run_script_string(archive_string)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[26], line 5
      1 archive_string ="""import os
      2 workdir = GetServerWorkingDirectory()
      3 path = os.path.join(workdir, "mixing_elbow.wbpz")
      4 Archive(FilePath=path , IncludeExternalImportedFiles=True)"""
----> 5 wb.run_script_string(archive_string)

NameError: name 'wb' is not defined

Download the archived project which has all simulation data and results.#

[27]:
wb.download_file("mixing_elbow.wbpz")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[27], line 1
----> 1 wb.download_file("mixing_elbow.wbpz")

NameError: name 'wb' is not defined

Exit Workbench Session.#

[28]:
wb.exit()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[28], line 1
----> 1 wb.exit()

NameError: name 'wb' is not defined