.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/00-fluent/ahmed_body_workflow.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_examples_00-fluent_ahmed_body_workflow.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_00-fluent_ahmed_body_workflow.py:

.. _ahmed_body_simulation:

Ahmed Body External Aerodynamics Simulation
-------------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 8-29

Objective
=====================================================================================

Ahmed body is a simplified car model used for studying the flow around it and to
predict the drag and lift forces. The model consists of a slanted back and a blunt
front.

In this example, PyFluent API is used to perform Ahmed Body external aerodynamics
simulation. which includes typical workflow of CFD Simulation as follows:

* Importing the geometry/CAD model.
* Meshing the geometry.
* Setting up the solver.
* Running the solver.
* Post-processing the results.



.. image:: ../../_static/ahmed_body_model.png
   :align: center
   :alt: Ahmed Body Model

.. GENERATED FROM PYTHON SOURCE LINES 32-34

Import required libraries/modules
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 34-42

.. code-block:: Python


    from pathlib import Path

    import ansys.fluent.core as pyfluent
    from ansys.fluent.core import examples
    from ansys.fluent.visualization import set_config
    import ansys.fluent.visualization.pyvista as pv


.. GENERATED FROM PYTHON SOURCE LINES 43-48

Specifying save path
=====================================================================================
save_path can be specified as Path("E:/", "pyfluent-examples-tests") or
Path("E:/pyfluent-examples-tests") in a Windows machine for example,  or
Path("~/pyfluent-examples-tests") in Linux.

.. GENERATED FROM PYTHON SOURCE LINES 48-50

.. code-block:: Python

    save_path = Path(pyfluent.EXAMPLES_PATH)


.. GENERATED FROM PYTHON SOURCE LINES 51-53

Configure specific settings for this example
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 53-55

.. code-block:: Python

    set_config(blocking=True, set_view_on_display="isometric")


.. GENERATED FROM PYTHON SOURCE LINES 56-58

Launch Fluent session with meshing mode
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 58-61

.. code-block:: Python

    session = pyfluent.launch_fluent(mode="meshing", cleanup_on_exit=True)
    session.health_check.status()


.. GENERATED FROM PYTHON SOURCE LINES 62-64

Meshing Workflow
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 66-68

Initialize the Meshing Workflow
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 68-80

.. code-block:: Python


    workflow = session.workflow
    geometry_filename = examples.download_file(
        "ahmed_body_20_0degree_boi_half.scdoc",
        "pyfluent/examples/Ahmed-Body-Simulation",
        save_path=save_path,
    )
    workflow.InitializeWorkflow(WorkflowType="Watertight Geometry")
    workflow.TaskObject["Import Geometry"].Arguments = dict(FileName=geometry_filename)
    workflow.TaskObject["Import Geometry"].Execute()



.. GENERATED FROM PYTHON SOURCE LINES 81-83

Add Local Face Sizing
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 83-123

.. code-block:: Python

    add_local_sizing = workflow.TaskObject["Add Local Sizing"]
    add_local_sizing.Arguments = dict(
        {
            "AddChild": "yes",
            "BOIControlName": "facesize_front",
            "BOIFaceLabelList": ["wall_ahmed_body_front"],
            "BOIGrowthRate": 1.15,
            "BOISize": 8,
        }
    )
    add_local_sizing.Execute()

    add_local_sizing.InsertCompoundChildTask()
    workflow.TaskObject["Add Local Sizing"].Execute()
    add_local_sizing = workflow.TaskObject["Add Local Sizing"]
    add_local_sizing.Arguments = dict(
        {
            "AddChild": "yes",
            "BOIControlName": "facesize_rear",
            "BOIFaceLabelList": ["wall_ahmed_body_rear"],
            "BOIGrowthRate": 1.15,
            "BOISize": 5,
        }
    )
    add_local_sizing.Execute()

    add_local_sizing.InsertCompoundChildTask()
    workflow.TaskObject["Add Local Sizing"].Execute()
    add_local_sizing = workflow.TaskObject["Add Local Sizing"]
    add_local_sizing.Arguments = dict(
        {
            "AddChild": "yes",
            "BOIControlName": "facesize_main",
            "BOIFaceLabelList": ["wall_ahmed_body_main"],
            "BOIGrowthRate": 1.15,
            "BOISize": 12,
        }
    )
    add_local_sizing.Execute()


.. GENERATED FROM PYTHON SOURCE LINES 124-126

Add BOI (Body of Influence) Sizing
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 126-141

.. code-block:: Python

    add_boi_sizing = workflow.TaskObject["Add Local Sizing"]
    add_boi_sizing.InsertCompoundChildTask()
    add_boi_sizing.Arguments = dict(
        {
            "AddChild": "yes",
            "BOIControlName": "boi_1",
            "BOIExecution": "Body Of Influence",
            "BOIFaceLabelList": ["ahmed_body_20_0degree_boi_half-boi"],
            "BOISize": 20,
        }
    )
    add_boi_sizing.Execute()
    add_boi_sizing.InsertCompoundChildTask()



.. GENERATED FROM PYTHON SOURCE LINES 142-144

Add Surface Mesh Sizing
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 144-163

.. code-block:: Python

    generate_surface_mesh = workflow.TaskObject["Generate the Surface Mesh"]
    generate_surface_mesh.Arguments = dict(
        {
            "CFDSurfaceMeshControls": {
                "CurvatureNormalAngle": 12,
                "GrowthRate": 1.15,
                "MaxSize": 50,
                "MinSize": 1,
                "SizeFunctions": "Curvature",
            }
        }
    )

    generate_surface_mesh.Execute()
    generate_surface_mesh.InsertNextTask(CommandName="ImproveSurfaceMesh")
    improve_surface_mesh = workflow.TaskObject["Improve Surface Mesh"]
    improve_surface_mesh.Arguments.update_dict({"FaceQualityLimit": 0.4})
    improve_surface_mesh.Execute()


.. GENERATED FROM PYTHON SOURCE LINES 164-166

Describe Geometry, Update Boundaries, Update Regions
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 166-174

.. code-block:: Python

    workflow.TaskObject["Describe Geometry"].Arguments = dict(
        CappingRequired="Yes",
        SetupType="The geometry consists of only fluid regions with no voids",
    )
    workflow.TaskObject["Describe Geometry"].Execute()
    workflow.TaskObject["Update Boundaries"].Execute()
    workflow.TaskObject["Update Regions"].Execute()


.. GENERATED FROM PYTHON SOURCE LINES 175-177

Add Boundary Layers
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 177-191

.. code-block:: Python

    add_boundary_layers = workflow.TaskObject["Add Boundary Layers"]
    add_boundary_layers.AddChildToTask()
    add_boundary_layers.InsertCompoundChildTask()
    workflow.TaskObject["smooth-transition_1"].Arguments.update_dict(
        {
            "BLControlName": "smooth-transition_1",
            "NumberOfLayers": 14,
            "Rate": 1.15,
            "TransitionRatio": 0.5,
        }
    )
    add_boundary_layers.Execute()



.. GENERATED FROM PYTHON SOURCE LINES 192-194

Generate the Volume Mesh
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 194-198

.. code-block:: Python

    generate_volume_mesh = workflow.TaskObject["Generate the Volume Mesh"]
    generate_volume_mesh.Arguments.update_dict({"VolumeFill": "poly-hexcore"})
    generate_volume_mesh.Execute()


.. GENERATED FROM PYTHON SOURCE LINES 199-201

Switch to the Solver Mode
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 201-203

.. code-block:: Python

    session = session.switch_to_solver()


.. GENERATED FROM PYTHON SOURCE LINES 204-206

Mesh Visualization
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 208-211

.. image:: ../../_static/ahmed_body_mesh_1.png
   :align: center
   :alt: Ahmed Body Mesh

.. GENERATED FROM PYTHON SOURCE LINES 213-216

.. image:: ../../_static/ahmed_body_mesh_2.png
   :align: center
   :alt: Ahmed Body Mesh

.. GENERATED FROM PYTHON SOURCE LINES 218-220

Solver Setup and Solve Workflow
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 222-224

Define Constants
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 224-228

.. code-block:: Python

    density = 1.225
    inlet_velocity = 30
    inlet_area = 0.11203202


.. GENERATED FROM PYTHON SOURCE LINES 229-231

Define Materials
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 231-236

.. code-block:: Python

    session.tui.define.materials.change_create("air", "air", "yes", "constant", density)
    session.settings.setup.models.viscous.model = "k-epsilon"
    session.settings.setup.models.viscous.k_epsilon_model = "realizable"
    session.settings.setup.models.viscous.options.curvature_correction = True


.. GENERATED FROM PYTHON SOURCE LINES 237-239

Define Boundary Conditions
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 239-247

.. code-block:: Python

    inlet = session.settings.setup.boundary_conditions.velocity_inlet["inlet"]
    inlet.turbulence.turb_intensity = 0.05
    inlet.momentum.velocity.value = inlet_velocity
    inlet.turbulence.turb_viscosity_ratio = 5

    outlet = session.settings.setup.boundary_conditions.pressure_outlet["outlet"]
    outlet.turbulence.turb_intensity = 0.05


.. GENERATED FROM PYTHON SOURCE LINES 248-250

Define Reference Values
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 250-254

.. code-block:: Python

    session.settings.setup.reference_values.area = inlet_area
    session.settings.setup.reference_values.density = density
    session.settings.setup.reference_values.velocity = inlet_velocity


.. GENERATED FROM PYTHON SOURCE LINES 255-257

Define Solver Settings
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 257-281

.. code-block:: Python

    session.tui.solve.set.p_v_coupling(24)

    session.tui.solve.set.discretization_scheme("pressure", 12)
    session.tui.solve.set.discretization_scheme("k", 1)
    session.tui.solve.set.discretization_scheme("epsilon", 1)
    session.tui.solve.initialize.set_defaults("k", 0.000001)

    session.settings.solution.monitor.residual.equations["continuity"].absolute_criteria = (
        0.0001
    )
    session.settings.solution.monitor.residual.equations["x-velocity"].absolute_criteria = (
        0.0001
    )
    session.settings.solution.monitor.residual.equations["y-velocity"].absolute_criteria = (
        0.0001
    )
    session.settings.solution.monitor.residual.equations["z-velocity"].absolute_criteria = (
        0.0001
    )
    session.settings.solution.monitor.residual.equations["k"].absolute_criteria = 0.0001
    session.settings.solution.monitor.residual.equations["epsilon"].absolute_criteria = (
        0.0001
    )


.. GENERATED FROM PYTHON SOURCE LINES 282-284

Define Report Definitions
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 284-298

.. code-block:: Python


    session.settings.solution.report_definitions.drag["cd-mon1"] = {}
    session.settings.solution.report_definitions.drag["cd-mon1"] = {
        "zones": ["wall_ahmed_body_main", "wall_ahmed_body_front", "wall_ahmed_body_rear"],
        "force_vector": [0, 0, 1],
    }
    session.parameters.output_parameters.report_definitions.create(name="parameter-1")
    session.parameters.output_parameters.report_definitions["parameter-1"] = {
        "report_definition": "cd-mon1"
    }

    session.settings.solution.monitor.report_plots.create(name="cd-mon1")
    session.settings.solution.monitor.report_plots["cd-mon1"] = {"report_defs": ["cd-mon1"]}


.. GENERATED FROM PYTHON SOURCE LINES 299-301

Initialize and Run Solver
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 301-307

.. code-block:: Python


    session.settings.solution.run_calculation.iter_count = 5
    session.settings.solution.initialization.initialization_type = "standard"
    session.settings.solution.initialization.standard_initialize()
    session.settings.solution.run_calculation.iterate(iter_count=5)


.. GENERATED FROM PYTHON SOURCE LINES 308-310

Post-Processing Workflow
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 310-326

.. code-block:: Python

    session.results.surfaces.iso_surface.create(name="xmid")
    session.results.surfaces.iso_surface["xmid"].field = "x-coordinate"
    session.results.surfaces.iso_surface["xmid"] = {"iso_values": [0]}

    graphics_session1 = pv.Graphics(session)
    contour1 = graphics_session1.Contours["contour-1"]
    contour1.field = "velocity-magnitude"
    contour1.surfaces_list = ["xmid"]
    contour1.display("window-1")

    contour2 = graphics_session1.Contours["contour-2"]
    contour2.field.allowed_values
    contour2.field = "pressure-coefficient"
    contour2.surfaces_list = ["xmid"]
    contour2.display("window-2")


.. GENERATED FROM PYTHON SOURCE LINES 327-329

Simulation Results Visualization
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 331-334

.. image:: ../../_static/ahmed_body_model_velocity_mag.png
   :align: center
   :alt: Velocity Magnitude

.. GENERATED FROM PYTHON SOURCE LINES 336-337

Velocity Magnitude Contour

.. GENERATED FROM PYTHON SOURCE LINES 339-342

.. image:: ../../_static/ahmed_body_model_pressure_coeff.png
   :align: center
   :alt: Peressure Coefficient

.. GENERATED FROM PYTHON SOURCE LINES 344-345

Pressure Coefficient Contour

.. GENERATED FROM PYTHON SOURCE LINES 347-349

Save the case file
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 349-352

.. code-block:: Python


    save_case_data_as = Path(save_path) / "ahmed_body_final.cas.h5"
    session.settings.file.write(file_type="case-data", file_name=str(save_case_data_as))

.. GENERATED FROM PYTHON SOURCE LINES 353-355

Close the session
=====================================================================================

.. GENERATED FROM PYTHON SOURCE LINES 355-358

.. code-block:: Python

    session.exit()



.. GENERATED FROM PYTHON SOURCE LINES 359-364

References
=====================================================================================

[1] S.R. Ahmed, G. Ramm, Some Salient Features of the Time-Averaged Ground Vehicle
Wake,SAE-Paper 840300,1984


.. _sphx_glr_download_examples_00-fluent_ahmed_body_workflow.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: ahmed_body_workflow.ipynb <ahmed_body_workflow.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: ahmed_body_workflow.py <ahmed_body_workflow.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: ahmed_body_workflow.zip <ahmed_body_workflow.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_