Adding Ansys Dynamic Reporting to Existing EnSight Scripts#

Modify your scripts to push the image files onto Ansys Dynamic Reporting#

EnSight with python scripting is a powerful tool that allows users to get their analysis performed in a reliable, repeatable, and fast fashion across a series of simulation results. One of the typical outcomes of these scripts is a set of images to be visually reviewed at a later time by the engineer to determine which models are the most promising. Due to the nature of the analysis and to the large number of simulation results that are available to the engineer today, the number of images to visually review can easily be in the hundreds, when not in the thousands. This poses some challenges in terms of data organization: organizing the files in such a way that they can easily be found, retrieved and categorized, is not a trivial task. This is one of the many reasons why engineers are switching to using structured databases for their data storage and organization. As a user approaching Ansys Dynamic Reporting, you probably already have your python scripts for EnSight, and do not want to re-create them from scratch. The good news is that you do not have to! With the addition of a handful of lines in your script, you can store and keep organized your results in Ansys Dynamic Reporting instead than in image files, scattered around your hard drive.

So, how to change your script to push the data into Ansys Dynamic Reporting, instead of (or in addition to) creating images? How about a worked example...

A Clip Image Example#

Let's say you have a script that sweeps a section cut of the model and saves an image for each position of the section cut. The script might be structured similar to:

for i in range(-20, 31, 1):
  if i == -20:

In this example, the section cut is swept between the position X = -2.0 and X = 30, taking a slice at each 0.1 delta.

Ansys Dynamic Reporting Script Modifications#

How to modify the script so that it sends the image to Ansys Dynamic Reporting instead than saving it to disk? Let's start by loading the modules you will need. At the top of the script, add:

from ansys.dynamicreporting.core.utils import report_remote_server, report_objects

Connecting to the Ansys Dynamic Reporting Server#

The first step will be to connect to the ADR Nexus server. In this example, we will suppose that there already is an existing ADR Nexus server that is up and running. We will use the http://localhost:8000 server.

serverobj = report_remote_server.Server("http://localhost:8000", "nexus", "cei")
  print("Can't connect to the server")

The first line will connect to the ADR Nexus server (username = nexus, password = cei). The try/except verifies that the connection was successful.

The Ansys Dynamic Reporting Session#

The next step is to get a Ansys Dynamic Reporting session. If you want to create a new session with every run of your script, you just need to change any attributes you want on the default session. For example, to create a new session with every run with the session name 'Cube Analysis', the code would be:

session = serverobj.get_default_session()
session.application = 'Cube Analysis'

If, on the other hand, you want to add the information to an existing Ansys Dynamic Reporting session, then you will need to get the session guid (identification string), and assign it to the session you are working on in this script. The following code will query the database, looking for an existing session with the name 'Cube Analysis'. If it finds one, it will set the guid and application name of the default session to the existing session and all data pushed to that session will be assigned to the existing session. Otherwise, the code will create a new session with the desired application name. The corresponding lines are:

session = serverobj.get_default_session()
items = serverobj.get_objects(objtype=report_objects.SessionREST, query='A|s_app|eq|Cube Analysis')
if len(items) > 0:
  session.guid = items[0].guid
  session.application = items[0].application
session.application = 'Cube Analysis'

Adding an Image#

Finally, you get to the section where you push the image data into Ansys Dynamic Reporting. You will first need to create an image item that corresponds to the current EnSight viewport, and then push it to Ansys Dynamic Reporting. You can add some tags that will help you store some extra information for this image item, which you can later use to retrieve the image from the Ansys Dynamic Reporting database. So in the original script, replace (or add) the following lines to the save_image(i+20) function:

img = ensight.render()
item = serverobj.create_item(name = "Clip\_" + str(i+20).zfill(2))
item.set_tags(str("X=" + str(i*0.1) + " var=temperature")) # Creates two tags, X and var
error = serverobj.put_objects([item])

The first line creates img, an object that corresponds to the current visualization in EnSight. The second and third lines create a Ansys Dynamic Reporting image item that corresponds to img. The fourth line adds some tags to the item, and finally the last line pushes it into the Ansys Dynamic Reporting server.

More Complete Example#

After running this new script, your Ansys Dynamic Reporting database will contain one image item for each section cut that you have created in the script, with corresponding tags that identify the X position of the cut and the variable the section cut is colored by (in this example, temperature). You can find the scripts (the original and the modified described above here. The scripts use the example dataset contained in the standard installation of EnSight. If you want to run directly these scripts, please consider changing the and the ensight.file.image_file() lines to reflect the paths on your machine.