Skip to main content

A Guide to Streamlining CFD Simulations and Reporting

| 11.28.2023

... With Ansys Fluent, Dynamic Reporting, and PyAnsys.

In the world of engineering, simulation plays a vital role in designing and optimizing complex systems. Whether you're working on aerodynamics, heat transfer, or fluid dynamics, the ability to simulate and analyse your designs is crucial. However, once you've run these simulations and processed the results, what's next? How do you turn the raw data into a presentable format for obtaining valuable insights? This is where tools like Ansys Dynamic Reporting come into play.

For example, consider ansys-fluent-core (PyFluent Documentation) provides Pythonic access to Ansys Fluent. Its features enable the seamless use of Fluent within the Python ecosystem and broad access to native Fluent features. It also provides a Python interface to generate simulation reports and export them in HTML, PDF, and PPTX formats, utilizing Ansys Dynamic Reporting behind the scenes.

This article will explore how to utilize the PyAnsys ecosystem to streamline CFD simulations and generate comprehensive reports using PyFluent and PyDynamicReporting. Additionally, it will demonstrate how to access and integrate the generated reports with modern tech stacks for use within an end-to-end workflow as a web application.

What is Ansys Dynamic Reporting?

Ansys Dynamic Reporting, documented as Nexus, is a report generator utilized in various Ansys products. It enables you to gather data from different sources and in multiple formats, which can then be consolidated, analysed, and presented in highly interactive reports.

Key features of Ansys Dynamic Reporting include:

  • Native support for multiple data formats.
  • Ability to store data from various sources, such as CAD packages, simulation software, and postprocessors.
  • Tools for data aggregation, filtering, and processing within the database.
  • A web-based interface for seamless and intuitive interaction with database items.
  • A template editor to create report templates.
  • Live reports with automatic updates when new data becomes available.
  • High interactivity in generated reports.

What is PyDynamicReporting?

PyDynamicReporting is a Python client library designed for Ansys Dynamic Reporting, formerly known as Nexus. Ansys Dynamic Reporting is a system that allows you to store various types of items, such as images, text, 3D scenes, and tables, in a database for organization and dynamic report generation. By using PyDynamicReporting, you can access all of Ansys Dynamic Reporting's features in a more Python-friendly manner when connected to an instance of the service.

Please refer detailed documentation and installation guide: Ansys PyDynamicReporting.

Getting Started with PyFluent & PyDynamicReporting

This article guides you through setting up the environment and crafting a simulation using PyFluent for an example scenario. Following that, it will demonstrate the use of PyFluent to create and export a report that takes advantage of Ansys Dynamic Reporting.

Upon finishing the PyFluent process, the article will re-establish a connection with the Fluent Report Server to retrieve the data and export the report's URL. This can be conveniently integrated into other processes, such as web applications.

Setting Up Your Environment

Before you dive into using these tools, you'll need to set up your environment. Ensure you have licensed copies of the relevant Ansys products.

The following products are required for a smooth setup process:

  1. Install Python (if not already installed):

    Ensure you have Python installed on your system. You can download Python from the official website. The recommended version for the setup is Python 3.10

  2. Create a virtual environment (if not already installed):

       python -m venv .venv      
    
  3. Activate the virtual environment:

  • For Windows CMD:

    
      .venv\Scripts\activate.bat
    
  • For Windows Powershell:

    
      .venv\Scripts\Activate.ps1
    

    From now on, all the commands must be executed within the virtual environment.

  1. Install the Libraries:

    Now that your virtual environment is active, you can install the required libraries using pip. Run the following commands to install ansys-fluent-core and ansys-dynamicreporting-core:

    python -m pip install ansys-fluent-core
    python -m pip install ansys-dynamicreporting-core
    

Note: For beginners who may find the above steps challenging, consider using the Ansys Python Manager to install Python and PyAnsys Metapackages. This tool also simplifies the process of creating and managing virtual environments.

Fluent Simulation Workflow for Example Case

Reading Ansys Fluent Simulation data using PyFluent.

Import required libraries/modules

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

Downloading cas/dat file

save_path = Path(pyfluent.EXAMPLES_PATH)

import_filename = examples.download_file(
    "mixing_elbow.cas.h5",
    "pyfluent/mixing_elbow",
)  

examples.download_file(
    "mixing_elbow.dat.h5",
    "pyfluent/mixing_elbow",
    save_path=save_path,
)

Launch Fluent session

session = pyfluent.launch_fluent(
    show_gui=True, processor_count=4, product_version="23.1.0"
)

Reading case and data file

session.file.read_case_data(file_type='case-data', file_name=import_filename)

Create a velocity vectors

session.results.graphics.vector["velocity_vector_symmetry"] = {}
session.results.graphics.vector["velocity_vector_symmetry"].print_state()
session.results.graphics.vector["velocity_vector_symmetry"].field = "temperature"
session.results.graphics.vector["velocity_vector_symmetry"].surfaces_list = [
    "symmetry-xyplane",
]
session.results.graphics.vector["velocity_vector_symmetry"].scale.scale_f = 4
session.results.graphics.vector["velocity_vector_symmetry"].style = "arrow"

Generate the Report using PyFluent

session.report.simulation_reports.generate_simulation_report(
    report_name="Mixing elbow simulation"
)
session.report.simulation_reports.export_simulation_report_as_pdf(
    report_name="Mixing elbow simulation", file_name_path=save_path
)

Close the Ansys Fluent Session

session.exit()

With the above steps, the process of accessing simulation data and initiating an Ansys Fluent session using PyFluent is discussed. This includes creating a velocity contour at the symmetry plane and generating a comprehensive simulation report using Ansys Dynamic Reporting. Furthermore, exporting this report as a PDF is demonstrated. The subsequent section guides users through utilizing PyDynamic Reporting to connect with FluentReportServer and obtain the report URL, enabling its integration with any framework using an iframe source.

Note: The FluentReportServer directory is created by Ansys Fluent at current working directory. It contains the Ansys Dynamic Reporting database and relevant database files.

Ansys PyDynamicReporting Workflow

Connecting with FluentReportServer (ADR Service) using PyDynamicReporting library

Import required libraries/modules

from pathlib import Path
import ansys.dynamicreporting.core as adr

Start the Report Server (ADR Service) on top of the Fluent database

data_base_ref = r'FluentReportServer'
ansys_installation = r"C:\Program Files\ANSYS Inc\v231"

adr_service = adr.Service(
    ansys_installation=ansys_installation,
    db_directory=data_base_ref,
)
session_guid = adr_service.start(create_db=False)

Get the port of ADR service and server object for Report Updates

adr_service._port
server = adr_service.serverobj

Updating the existing report template

This next section updates the Organization's branding logo and creates data items for it, adding a title, and Table of Contents.

## Place here the name of the top-level report
myr = adr_service.get_report("Mixing elbow simulation")
root_template = myr.report
report_tags = root_template.get_tags()

template_0=server.create_template(name="Top", parent=root_template, report_type="Layout:panel")
template_0.params='{"properties": {"TOCItem": "0"}}'
template_0.set_tags(report_tags)
server.put_objects(template_0)
server.put_objects(root_template)

template_1=server.create_template(name="Logo", parent=template_0, report_type="Layout:basic")
template_1.params='{"properties": {"TOCItem": "0"}}'
template_1.set_filter("A|i_type|eq|image;A|i_name|eq|UserLogo;")
template_1.set_tags(report_tags)
server.put_objects(template_1)
server.put_objects(template_0)
server.put_objects(root_template)

template_2=server.create_template(name="Title", parent=template_0, report_type="Layout:basic")
template_2.params='{"properties": {"TOCItem": "0"}}'
template_2.set_filter("A|i_type|eq|html;A|i_name|eq|ReportTitle;")
template_2.set_tags(report_tags)
server.put_objects(template_2)
server.put_objects(template_0)
server.put_objects(root_template)

template_3=server.create_template(name="Header", parent=template_0, report_type="Layout:basic")
template_3.params='{"properties": {"TOCItem": "0"}, "HTML": "", "skip_empty": 1}'
template_3.set_filter("A|i_type|eq|table;A|i_name|eq|HeaderTableItem;")
template_3.set_tags(report_tags)
server.put_objects(template_3)
server.put_objects(template_0)
server.put_objects(root_template)

template_4=server.create_template(name="Table of Contents", parent=template_0, report_type="Layout:toc")
template_4.params='{"HTML": "<h3>Table of Contents</h3>", "TOCitems": 1, "TOCtables": 0, "TOCfigures": 0}'
template_4.set_filter("A|i_name|cont|_nothing_selected_;")
template_4.set_tags(report_tags)
server.put_objects(template_4)
server.put_objects(template_0)
server.put_objects(root_template)

# Re-order the template children so the new one goes on top
root_template.children_order = template_0.guid+',' + root_template.children_order.replace(template_0.guid+',', '')
root_template.reorder_children()
server.put_objects(root_template)


## Now add the Logo the customer wants to use
logo_img = adr_service.create_item(obj_name='UserLogo')
logo_img.item.dataset = report_tags.split('=')[-1]
logo_img.item_image = r'logo.png'

Note: You can use any image file to create item_image, but it is recommended that you use PNG, or JPEG format.

Get the URL of the report

report_url = adr_service.get_report("Mixing elbow simulation")
report_url.get_url()

The URL returned by the ADR service can easily be embedded with any frontend technology.

Note: Once the ADR service has stopped, the url will not return a report any more. Make sure to call the stop method at the right time.

e.g., with Plotly Dash

To create a simple Dash web application that renders a URL with an iframe using Dash Bootstrap Components (dbc), you'll need to have Dash and dbc installed. You can install them using pip if you haven't already:

python -m pip install dash 
python -m pip install dash-bootstrap-components

Here's a simple example:

import dash
import dash_bootstrap_components as dbc
from dash import html, dcc

app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])

app.layout = html.Div([
    dbc.Card(
        [
            html.H1("Report Viewer", className="display-3"),
            html.P(
                "Enter a URL to render it in an iframe below:",
                className="lead",
            ),
            dcc.Input(id="url-input", type="text", placeholder="Enter URL"),
            html.Div(id="iframe-container"),
        ],
    )
])

@app.callback(
    dash.dependencies.Output("iframe-container", "children"),
    [dash.dependencies.Input("url-input", "value")]
)
def update_iframe(url):
    if url:
        return html.Iframe(src=url, width="100%", height="500")
    return html.Div()

if __name__ == '__main__':
    app.run_server(debug=True)

Save this code in a Python file (e.g., app.py) and run it. You can access the web application in your browser at http://localhost:8050, and it will allow you to enter a URL to render in an iframe.

Stop the ADR service

adr_service.stop()

Conclusion

The integration of Ansys Fluent, Ansys Dynamic Reporting, and their respective client libraries, PyFluent and PyDynamicReporting, not only simplifies simulation and reporting processes but also offers numerous application and integration opportunities. By exporting reports in different formats, users can tailor the generated insights to suit their particular requirements, such as interactive web applications, widespread insight sharing, SaaS applications, or sophisticated data analytics. These tools enable engineers and researchers to fully utilize their simulation data in today's technology-driven world.

Additional Resources

Ansys Fluent

Ansys PyFluent Documentation

Ansys PyDynamicReporting Documentation

Plotly Dash

Companion Jupyter Notebook for Experimentation