.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/00-fluent/radiation_headlamp.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_radiation_headlamp.py>`
        to download the full example code.

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

.. _sphx_glr_examples_00-fluent_radiation_headlamp.py:

.. _ref_radiation_headlamp:

Modeling Radiation in a Headlamp Using the Monte Carlo Method
-------------------------------------------------------------
This example solves for the radiative and conductive heat transfer within a car
headlamp exposed to the sun's rays to determine the severity of any hotspots
that form. It uses a Monte Carlo radiation model and the  pressure-based
solver. This is based on the Fluent tutorial titled "Using the Monte Carlo
Radiation Model".

**Workflow tasks**

The Modeling Radiation Using the Monte Carlo Method example guides you through
these tasks:

- Creation of a mesh using the Watertight Geometry workflow.
- Setting up a Monte Carlo radiation model.
- Creation of materials with thermal and radiation properties.
- Setting boundary conditions for heat transfer and radiation calculations.
- Calculating a solution using the pressure-based solver.

**Problem description**

The problem considers the headlamp of a parked car exposed to sunlight. The
lens focuses incoming radiation onto the internal components of the headlamp,
producing thermal hotspots that could cause damage due to thermal stresses or
material degradation.

.. GENERATED FROM PYTHON SOURCE LINES 53-56

.. image:: ../../_static/radiation_headlamp_geom.png
  :width: 500pt
  :align: center

.. GENERATED FROM PYTHON SOURCE LINES 58-67

Example Setup
-------------
Before you can use the watertight geometry meshing workflow, you must set up
the example and initialize this workflow.

Perform required imports
~~~~~~~~~~~~~~~~~~~~~~~~
Perform required imports, which includes downloading and importing the
geometry files.

.. GENERATED FROM PYTHON SOURCE LINES 67-79

.. code-block:: Python


    import ansys.fluent.core as pyfluent
    from ansys.fluent.core import examples

    headlamp_spaceclaim_file, headlamp_pmdb_file = [
        examples.download_file(
            f,
            "pyfluent/radiation_headlamp",
        )
        for f in ["headlamp.scdoc", "headlamp.pmdb"]
    ]


.. GENERATED FROM PYTHON SOURCE LINES 80-84

Launch Fluent
~~~~~~~~~~~~~
Launch Fluent as a service in meshing mode with single precision running on
four processors and print Fluent version.

.. GENERATED FROM PYTHON SOURCE LINES 84-92

.. code-block:: Python


    meshing = pyfluent.launch_fluent(
        precision="single",
        processor_count=4,
        mode="meshing",
    )
    print(meshing.get_fluent_version())


.. GENERATED FROM PYTHON SOURCE LINES 93-96

Initialize workflow
~~~~~~~~~~~~~~~~~~~
Initialize the watertight geometry meshing workflow.

.. GENERATED FROM PYTHON SOURCE LINES 96-99

.. code-block:: Python


    meshing.workflow.InitializeWorkflow(WorkflowType="Watertight Geometry")


.. GENERATED FROM PYTHON SOURCE LINES 100-108

Watertight geometry meshing workflow
------------------------------------
The fault-tolerant meshing workflow guides you through the several tasks that
follow.

Import CAD and set length units
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Import the CAD geometry and set the length units to millimeters.

.. GENERATED FROM PYTHON SOURCE LINES 108-119

.. code-block:: Python


    geo_import = meshing.workflow.TaskObject["Import Geometry"]
    geo_import.Arguments.set_state(
        {
            "FileName": headlamp_pmdb_file,
            "LengthUnit": "mm",
        }
    )

    geo_import.Execute()


.. GENERATED FROM PYTHON SOURCE LINES 120-123

Add local sizing
~~~~~~~~~~~~~~~~
Add local sizing controls to the geometry.

.. GENERATED FROM PYTHON SOURCE LINES 123-149

.. code-block:: Python


    local_sizing = meshing.workflow.TaskObject["Add Local Sizing"]
    local_sizing.Arguments.set_state(
        {
            "AddChild": "yes",
            "BOIControlName": "boi_lens",
            "BOIExecution": "Body Of Influence",
            "BOIFaceLabelList": ["boi"],
            "BOISize": 2,
        }
    )

    local_sizing.AddChildAndUpdate()

    local_sizing.Arguments.set_state(
        {
            "AddChild": "yes",
            "BOIControlName": "bodysize_lens",
            "BOIExecution": "Body Size",
            "BOIFaceLabelList": ["lens"],
            "BOISize": 2,
        }
    )

    local_sizing.AddChildAndUpdate()


.. GENERATED FROM PYTHON SOURCE LINES 150-153

Generate surface mesh
~~~~~~~~~~~~~~~~~~~~~
Generate the surface mesh.

.. GENERATED FROM PYTHON SOURCE LINES 153-166

.. code-block:: Python


    surface_mesh_gen = meshing.workflow.TaskObject["Generate the Surface Mesh"]
    surface_mesh_gen.Arguments.set_state(
        {
            "CFDSurfaceMeshControls": {
                "MinSize": 1,
                "MaxSize": 40,
            }
        }
    )

    surface_mesh_gen.Execute()


.. GENERATED FROM PYTHON SOURCE LINES 167-170

Improve surface mesh
~~~~~~~~~~~~~~~~~~~~
Improve the surface mesh.

.. GENERATED FROM PYTHON SOURCE LINES 170-175

.. code-block:: Python


    surface_mesh_gen.InsertNextTask(CommandName="ImproveSurfaceMesh")

    meshing.workflow.TaskObject["Improve Surface Mesh"].Execute()


.. GENERATED FROM PYTHON SOURCE LINES 176-179

Describe geometry
~~~~~~~~~~~~~~~~~
Describe geometry and define the fluid region.

.. GENERATED FROM PYTHON SOURCE LINES 179-193

.. code-block:: Python


    describe_geo = meshing.workflow.TaskObject["Describe Geometry"]
    describe_geo.Arguments.set_state(
        {
            "SetupType": "The geometry consists of both fluid and solid regions and/or voids",
            "CappingRequired": "No",
            "WallToInternal": "No",
            "InvokeShareTopology": "No",
            "Multizone": "No",
        }
    )

    describe_geo.Execute()


.. GENERATED FROM PYTHON SOURCE LINES 194-197

Update boundaries
~~~~~~~~~~~~~~~~~
Update the boundaries.

.. GENERATED FROM PYTHON SOURCE LINES 197-208

.. code-block:: Python


    update_bc = meshing.workflow.TaskObject["Update Boundaries"]
    update_bc.Arguments.set_state(
        {
            "BoundaryLabelList": ["rad-input"],
            "BoundaryLabelTypeList": ["wall"],
        }
    )

    update_bc.Execute()


.. GENERATED FROM PYTHON SOURCE LINES 209-212

Create fluid region
~~~~~~~~~~~~~~~~~~~
Create the fluid region.

.. GENERATED FROM PYTHON SOURCE LINES 212-218

.. code-block:: Python


    create_regions = meshing.workflow.TaskObject["Create Regions"]
    create_regions.Arguments.set_state({"NumberOfFlowVolumes": 1})

    create_regions.Execute()


.. GENERATED FROM PYTHON SOURCE LINES 219-222

Update regions
~~~~~~~~~~~~~~
Update the regions.

.. GENERATED FROM PYTHON SOURCE LINES 222-225

.. code-block:: Python


    meshing.workflow.TaskObject["Update Regions"].Execute()


.. GENERATED FROM PYTHON SOURCE LINES 226-229

Boundary layers
~~~~~~~~~~~~~~~~~~~
Do not add boundary layers and proceed to the next task.

.. GENERATED FROM PYTHON SOURCE LINES 229-235

.. code-block:: Python


    add_boundary_layers = meshing.workflow.TaskObject["Add Boundary Layers"]
    add_boundary_layers.Arguments.set_state({"AddChild": "no"})

    add_boundary_layers.Execute()


.. GENERATED FROM PYTHON SOURCE LINES 236-240

Generate volume mesh
~~~~~~~~~~~~~~~~~~~~
Generate the volume mesh, which consists of setting properties for the
volume mesh.

.. GENERATED FROM PYTHON SOURCE LINES 240-251

.. code-block:: Python


    volume_mesh_gen = meshing.workflow.TaskObject["Generate the Volume Mesh"]
    volume_mesh_gen.Arguments.set_state(
        {
            "VolumeMeshPreferences": {
                "PolyFeatureAngle": 40,
            },
        },
    )
    volume_mesh_gen.Execute()


.. GENERATED FROM PYTHON SOURCE LINES 252-255

Check mesh in meshing mode
~~~~~~~~~~~~~~~~~~~~~~~~~~
Check the mesh in meshing mode.

.. GENERATED FROM PYTHON SOURCE LINES 255-258

.. code-block:: Python


    meshing.tui.mesh.check_mesh()


.. GENERATED FROM PYTHON SOURCE LINES 259-262

Save mesh file
~~~~~~~~~~~~~~
Save the mesh file (``headlamp.msh.h5``).

.. GENERATED FROM PYTHON SOURCE LINES 262-265

.. code-block:: Python


    meshing.meshing.File.WriteMesh(FileName="headlamp.msh.h5")


.. GENERATED FROM PYTHON SOURCE LINES 266-276

Solve and postprocess
---------------------
Once you have completed the watertight geometry meshing workflow, you can
solve and postprocess the results.

Switch to solution mode
~~~~~~~~~~~~~~~~~~~~~~~
Switch to solution mode. Now that a high-quality mesh has been generated
using Fluent in meshing mode, you can switch to solver mode to complete the
setup of the simulation.

.. GENERATED FROM PYTHON SOURCE LINES 276-279

.. code-block:: Python


    solver = meshing.switch_to_solver()


.. GENERATED FROM PYTHON SOURCE LINES 280-283

Enable energy and viscosity models
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Set up the energy and viscosity models.

.. GENERATED FROM PYTHON SOURCE LINES 283-288

.. code-block:: Python


    models = solver.settings.setup.models
    models.energy.enabled = True
    models.viscous.model = "laminar"


.. GENERATED FROM PYTHON SOURCE LINES 289-294

Set up radiation model
~~~~~~~~~~~~~~~~~~~~~~
Set up the Monte Carlo radiation model. The number of histories is set to 10
million in order to reduce computation time, but this may need to be
increased to obtain accurate results.

.. GENERATED FROM PYTHON SOURCE LINES 294-300

.. code-block:: Python


    radiation = models.radiation
    radiation.model = "monte-carlo"
    radiation.monte_carlo.number_of_histories = 1e7
    radiation.solve_frequency.iteration_interval = 20


.. GENERATED FROM PYTHON SOURCE LINES 301-307

Define materials
~~~~~~~~~~~~~~~~
Create materials to represent the glass and plastic parts of the headlamp.
To demonstrate two different methods of creating materials through the
settings API, we will create glass using a dictionary and plastic using dot
syntax.

.. GENERATED FROM PYTHON SOURCE LINES 307-355

.. code-block:: Python


    # --- Properties of glass ---
    # Density: 2650 [kg/m^3]
    # Specific heat capacity: 1887 [J/(kg K)]
    # Thermal conductivity: 7.6 [W/(m K)]
    # Absorption coefficient: 5.302
    # Refractive index: 1.4714

    glass = solver.settings.setup.materials.solid.create("glass")
    glass.set_state(
        {
            "chemical_formula": "",
            "density": {
                "option": "constant",
                "value": 2650,
            },
            "specific_heat": {
                "option": "constant",
                "value": 1887,
            },
            "thermal_conductivity": {
                "option": "constant",
                "value": 7.6,
            },
            "absorption_coefficient": {
                "option": "constant",
                "value": 5.302,
            },
            "refractive_index": {
                "option": "constant",
                "value": 1.4714,
            },
        }
    )

    # --- Properties of plastic ---
    # Density: 1545.3 [kg/m^3]
    # Specific heat capacity: 2302 [J/(kg K)]
    # Thermal conductivity: 0.316 [W/(m K)]

    plastic = solver.settings.setup.materials.solid.create("plastic")
    plastic.chemical_formula = ""
    plastic.density.value = 1545.3
    plastic.specific_heat.value = 2302
    plastic.thermal_conductivity.value = 0.316
    plastic.absorption_coefficient.value = 0
    plastic.refractive_index.value = 1


.. GENERATED FROM PYTHON SOURCE LINES 356-359

Cell Zone Conditions
~~~~~~~~~~~~~~~~~~~~
Set the cell zone conditions for the bezel and the lens.

.. GENERATED FROM PYTHON SOURCE LINES 359-377

.. code-block:: Python


    solver.settings.setup.cell_zone_conditions.solid["bezel"].material = "plastic"
    solver.settings.setup.cell_zone_conditions.copy(
        from_="bezel",
        to=[
            "holder",
            "housing",
            "inner-bezel",
            "reflector",
            "rim-bezel",
            "seating-steel-rim",
        ],
    )

    lens_cellzone_conds = solver.settings.setup.cell_zone_conditions.solid["lens"]
    lens_cellzone_conds.material = "glass"
    lens_cellzone_conds.general.participates_in_radiation = True


.. GENERATED FROM PYTHON SOURCE LINES 378-381

Boundary Conditions
~~~~~~~~~~~~~~~~~~~
Set the boundary conditions.

.. GENERATED FROM PYTHON SOURCE LINES 381-475

.. code-block:: Python


    # --- Set up bezel-enclosure BC ---
    # Material: plastic
    # BC type: opaque
    # Internal emissivity: 1
    # Diffuse fraction: 1

    bezel_enc_bc = solver.settings.setup.boundary_conditions.wall["bezel-enclosure"]
    bezel_enc_bc.thermal.material = "plastic"
    bezel_enc_bc.radiation.radiation_bc = "Opaque"
    bezel_enc_bc.radiation.internal_emissivity = 1
    bezel_enc_bc.radiation.diffuse_irradiation_settings.diffuse_fraction_band = {"s-": 1}

    # Get list of wall zones
    bc_state = solver.settings.setup.boundary_conditions.get_state()
    zones_to_copy = list(bc_state["wall"].keys())
    incompatible_zones = ["bezel-enclosure", "enclosure:1", "rad-input"]
    for incompatible_zone in incompatible_zones:
        zones_to_copy.remove(incompatible_zone)

    # Copy bezel-enclosure BC to all other BCs
    solver.settings.setup.boundary_conditions.copy(
        from_="bezel-enclosure",
        to=zones_to_copy,
    )

    # --- Set up enclosure-lens BC ---
    # Material: glass
    # BC type: semi-transparent
    # Diffuse fraction: 0

    enc_lens_bc = solver.settings.setup.boundary_conditions.wall["enclosure-lens"]
    enc_lens_bc.thermal.material = "glass"
    enc_lens_bc.radiation.radiation_bc = "Semi Transparent"
    enc_lens_bc.radiation.diffuse_irradiation_settings.diffuse_fraction_band = {"s-": 0}

    # Copy enclosure-lens BC to other lens boundary
    solver.settings.setup.boundary_conditions.copy(
        from_="enclosure-lens",
        to=["enclosure-lens-shadow"],
    )

    # --- Set up enclosure-rim-bezel BC ---
    # Material: plastic
    # BC type: opaque
    # Internal emissivity: 0.16
    # Diffuse fraction: 0.1

    enc_rim_bezel_bc = solver.settings.setup.boundary_conditions.wall["enclosure-rim-bezel"]
    enc_rim_bezel_bc.thermal.material = "plastic"
    enc_rim_bezel_bc.radiation.radiation_bc = "Opaque"
    enc_rim_bezel_bc.radiation.internal_emissivity = 0.16
    enc_rim_bezel_bc.radiation.diffuse_irradiation_settings.diffuse_fraction_band = {
        "s-": 0.1
    }

    # Copy enclosure-rim-bezel BC to other rim bezel boundaries
    solver.settings.setup.boundary_conditions.copy(
        from_="enclosure-rim-bezel",
        to=[
            "enclosure-rim-bezel-shadow",
            "holder-rim-bezel",
            "holder-rim-bezel-shadow",
            "housing-rim-bezel",
            "housing-rim-bezel-shadow",
        ],
    )

    # --- Set up enclosure:1 (domain boundaries) BC ---
    # BC type: temperature
    # Temperature: 298.15 [K]

    enc1_bc = solver.settings.setup.boundary_conditions.wall["enclosure:1"]
    enc1_bc.thermal.thermal_condition = "Temperature"
    enc1_bc.thermal.temperature.value = 298.15

    # --- Set up radiation input BC ---
    # BC type: temperature
    # Temperature: 298.15 [K]
    # Boundary source: yes
    # Direct irradiation: 1200 [W/m^2]
    # Radiation direction: (-0.848, 0, -0.53)

    rad_input_bc = solver.settings.setup.boundary_conditions.wall["rad-input"]
    rad_input_bc.thermal.thermal_condition = "Temperature"
    rad_input_bc.thermal.temperature.value = 298.15
    rad_input_bc.radiation.boundary_source = True
    rad_input_bc.radiation.direct_irradiation_settings.direct_irradiation = {"s-": 1200}
    rad_input_bc.radiation.direct_irradiation_settings.reference_direction = [
        -0.848,
        0,
        -0.53,
    ]


.. GENERATED FROM PYTHON SOURCE LINES 476-479

Set convergence criteria
~~~~~~~~~~~~~~~~~~~~~~~~
Enable residual plots and set the convergence criteria to 'none'.

.. GENERATED FROM PYTHON SOURCE LINES 479-482

.. code-block:: Python


    solver.solution.monitor.residual.options.criterion_type = "none"


.. GENERATED FROM PYTHON SOURCE LINES 483-487

Define surface reports
~~~~~~~~~~~~~~~~~~~~~~
Define a surface report to find the maximum temperature of the inner bezel,
then print the state of the report object.

.. GENERATED FROM PYTHON SOURCE LINES 487-501

.. code-block:: Python


    solver.settings.solution.report_definitions.surface["max-temp"] = {}
    max_temp_surf_report = solver.settings.solution.report_definitions.surface["max-temp"]
    max_temp_surf_report.surface_names = ["enclosure-inner-bezel"]
    max_temp_surf_report.report_type = "surface-facetmax"
    max_temp_surf_report.field = "temperature"

    solver.settings.solution.monitor.report_plots["max-temp-rplot"] = {}
    max_temp_surf_report_plot = solver.settings.solution.monitor.report_plots[
        "max-temp-rplot"
    ]
    max_temp_surf_report_plot.report_defs = "max-temp"
    max_temp_surf_report_plot.print = True


.. GENERATED FROM PYTHON SOURCE LINES 502-505

Define report plots
~~~~~~~~~~~~~~~~~~~
Define a plot of the maximum temperature.

.. GENERATED FROM PYTHON SOURCE LINES 505-511

.. code-block:: Python


    solver.settings.solution.monitor.report_plots["max-temp-rplot"] = {}
    max_temp_rplot = solver.settings.solution.monitor.report_plots["max-temp-rplot"]
    max_temp_rplot.report_defs = "max-temp"
    max_temp_rplot.print = True


.. GENERATED FROM PYTHON SOURCE LINES 512-515

Save case file
~~~~~~~~~~~~~~
Save the case file (``headlamp.cas.h5``).

.. GENERATED FROM PYTHON SOURCE LINES 515-518

.. code-block:: Python


    solver.settings.file.write(file_name="headlamp.cas.h5", file_type="case")


.. GENERATED FROM PYTHON SOURCE LINES 519-522

Initialize flow field
~~~~~~~~~~~~~~~~~~~~~
Initialize the solution.

.. GENERATED FROM PYTHON SOURCE LINES 522-525

.. code-block:: Python


    solver.settings.solution.initialization.initialize()


.. GENERATED FROM PYTHON SOURCE LINES 526-530

Solve for 19 iterations
~~~~~~~~~~~~~~~~~~~~~~~
Solve for 19 iterations. 99 iterations is recommended by the tutorial, but is
reduced to 19 for this example for demonstration purposes.

.. GENERATED FROM PYTHON SOURCE LINES 530-533

.. code-block:: Python


    solver.settings.solution.run_calculation.iterate(iter_count=19)


.. GENERATED FROM PYTHON SOURCE LINES 534-538

Write final case file and data
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enable overwrite so that the original case file will be overwritten. Write
the final case file and the data.

.. GENERATED FROM PYTHON SOURCE LINES 538-542

.. code-block:: Python


    solver.settings.file.batch_options.confirm_overwrite = True
    solver.settings.file.write(file_name="headlamp.cas.h5", file_type="case-data")


.. GENERATED FROM PYTHON SOURCE LINES 543-546

Close Fluent
~~~~~~~~~~~~
Close Fluent.

.. GENERATED FROM PYTHON SOURCE LINES 546-549

.. code-block:: Python


    solver.exit()



.. _sphx_glr_download_examples_00-fluent_radiation_headlamp.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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