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

.. only:: html

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

        :ref:`Go to the end <sphx_glr_download_examples_mesh_statistics_report.py>`
        to download the full example code

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

.. _sphx_glr_examples_mesh_statistics_report.py:


.. _mesh_statistics_report:

Mesh Statistics Report
----------------------

This example demonstrates how to use the mesh_statistics module and CCL queries to generate a
simple report.

The jinja library is used to produce the report in HTML format, starting from the report
template file "report_template.html".

.. GENERATED FROM PYTHON SOURCE LINES 14-26

.. code-block:: default



    from collections import OrderedDict
    from datetime import date
    import os.path as ospath

    from ansys.turbogrid.api.cfx.ccl_object_db import CCLObjectDB
    from jinja2 import Environment, FileSystemLoader

    from ansys.turbogrid.core.launcher.launcher import get_turbogrid_exe_path, launch_turbogrid
    from ansys.turbogrid.core.mesh_statistics import mesh_statistics


.. GENERATED FROM PYTHON SOURCE LINES 28-30

Set up a TurboGrid session with a basic case and mesh, similar to the
:ref:`read_inf_rotor37` example.

.. GENERATED FROM PYTHON SOURCE LINES 30-41

.. code-block:: default

    turbogrid = launch_turbogrid()
    exec_path = get_turbogrid_exe_path()
    turbogrid_install_location = "/".join(exec_path.parts[:-2])
    turbogrid_install_location = turbogrid_install_location.replace("\\", "")
    examples_path_str = turbogrid_install_location + "/examples"
    if not ospath.isdir(examples_path_str):
        print("examples folder not found in the TurboGrid installation")
        exit()
    turbogrid.read_inf(examples_path_str + "/rotor37/BladeGen.inf")
    turbogrid.unsuspend(object="/TOPOLOGY SET")


.. GENERATED FROM PYTHON SOURCE LINES 42-44

Determine which domains are available by querying the CCL (TurboGrid command
language).

.. GENERATED FROM PYTHON SOURCE LINES 44-49

.. code-block:: default

    ALL_DOMAINS = "ALL"
    ccl_db = CCLObjectDB(turbogrid)
    domain_list = [obj.get_name() for obj in ccl_db.get_objects_by_type("DOMAIN")]
    domain_list.append(ALL_DOMAINS)


.. GENERATED FROM PYTHON SOURCE LINES 50-51

Set up the information to be shown under 'Case Details' in the report.

.. GENERATED FROM PYTHON SOURCE LINES 51-58

.. code-block:: default

    case_info = OrderedDict()
    case_info["Case Name"] = "rotor37"
    case_info["Number of Bladesets"] = ccl_db.get_object_by_path("/GEOMETRY/MACHINE DATA").get_value(
        "Bladeset Count"
    )
    case_info["Report Date"] = date.today()


.. GENERATED FROM PYTHON SOURCE LINES 59-60

Create the MeshStatistics object for obtaining the mesh statistics.

.. GENERATED FROM PYTHON SOURCE LINES 60-62

.. code-block:: default

    ms = mesh_statistics.MeshStatistics(turbogrid)


.. GENERATED FROM PYTHON SOURCE LINES 63-65

Calculate and store the basic mesh statistics for each domain separately and
for 'All Domains'.

.. GENERATED FROM PYTHON SOURCE LINES 65-70

.. code-block:: default

    domain_count = dict()
    for domain in domain_list:
        ms.update_mesh_statistics(domain)
        domain_count[ms.get_domain_label(domain)] = ms.get_mesh_statistics().copy()


.. GENERATED FROM PYTHON SOURCE LINES 71-72

Ensure that the currently-loaded mesh statistics are for all domains.

.. GENERATED FROM PYTHON SOURCE LINES 72-75

.. code-block:: default

    ms.update_mesh_statistics(ALL_DOMAINS)
    all_dom_stats = ms.get_mesh_statistics()


.. GENERATED FROM PYTHON SOURCE LINES 76-78

Get the mesh statistics table information in a form that can easily be used to
generate the table in the report.

.. GENERATED FROM PYTHON SOURCE LINES 78-80

.. code-block:: default

    stat_table_rows = ms.get_table_rows()


.. GENERATED FROM PYTHON SOURCE LINES 81-82

Generate the histogram figures for all required mesh quality measures.

.. GENERATED FROM PYTHON SOURCE LINES 82-94

.. code-block:: default

    hist_var_list = ["Minimum Face Angle", "Skewness"]
    hist_dict = dict()
    for var in hist_var_list:
        file_name = "tg_hist_" + var + ".png"
        var_units = all_dom_stats[var]["Units"]
        if var_units == "rad":
            var_units = "deg"
        ms.create_histogram(
            variable=var, use_percentages=True, bin_units=var_units, image_file=file_name, show=False
        )
        hist_dict[var] = file_name


.. GENERATED FROM PYTHON SOURCE LINES 95-97

Quit the TurboGrid session as all of the relevant information has now been
assembled.

.. GENERATED FROM PYTHON SOURCE LINES 97-99

.. code-block:: default

    turbogrid.quit()


.. GENERATED FROM PYTHON SOURCE LINES 100-101

Set up the jinja library with the relevant template and data.

.. GENERATED FROM PYTHON SOURCE LINES 101-110

.. code-block:: default

    environment = Environment(loader=FileSystemLoader(ospath.dirname(__file__)))
    html_template = environment.get_template("report_template.html")
    html_context = {
        "case_info": case_info,
        "domain_count": domain_count,
        "stat_table_rows": stat_table_rows,
        "hist_dict": hist_dict,
    }


.. GENERATED FROM PYTHON SOURCE LINES 111-112

Generate the html report.

.. GENERATED FROM PYTHON SOURCE LINES 112-117

.. code-block:: default

    filename = f"tg_report.html"
    content = html_template.render(html_context)
    with open(filename, mode="w", encoding="utf-8") as message:
        message.write(content)
        print(f"... wrote {filename}")


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  0.000 seconds)


.. _sphx_glr_download_examples_mesh_statistics_report.py:

.. only:: html

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




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

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

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

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


.. only:: html

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

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