<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=445056146107513&amp;ev=PageView&amp;noscript=1">

Simulation Nation

Python Workflow with Autodesk CFD

Posted by Tommy Mello on Jun 20, 2019 10:56:00 AM

Python is a versatile programing language that is used for many tasks.  A portable (pre-compiled) version of python is installed with Autodesk CFD; along with a documented application programming interface (API). Read a couple of our other Python related blog posts as a reference:

  1. Python in CFD
  2. Thermal summary utility

Most Python scripts for engineering tasks require the inclusion of modules such as NumPy, SciPy, matplotlib, etc.  Many modules are available for installation with Python, however, modifying the pre-compiled version included with Autodesk CFD can be cumbersome to manage.  It may also be time consuming to start an Autodesk CFD session for a simple change or manipulation of existing data.

To overcome the challenges associated with the pre-compiled version of Python included with Autodesk CFD, a workflow of abstracting CFD dependencies is used.  In other words, any code referencing CFD specific modules, is extracted as a separate script.  CFD specific scripts are executed within the Autodesk CFD environment, while all other tasks are done independent of Autodesk CFD.

Work Flow

 news-sim-2019-june-python-1

Scripting Structure

 news-sim-2019-june-python-2

 

By abstracting all Autodesk CFD dependencies into a separate script, the data (from the Autodesk CFD specific script) can be manipulated and analyzed in the future with no need to start an Autodesk CFD session; this is especially useful for large models or times when the machine’s resources are being utilized for something else.

For example, a script (without using this technique) to get the max temperature of critical faces may be the following:

from CFD import Setup
from CFD import Results
from CFD import DSE

# Get current scenario & activate results

study = Setup.DesignStudy.Create()
scenario = study.getActiveScenario()
curr_results = scenario.results()
curr_results.activate()

#list of critical ID's
critical_surfaces = [1,3,5,7,9]

# Create a WallResults object
wr = Results.WallResults(scenario)

#loop through surfaces
for surface in critical_surfaces:
    wr.select(surface)
wr.calculate()

#empty list for results
surface_results = []

#append ID, temp to results list
for surface in critical_surfaces:
    surface_results.append( wr.temperature(surface) )

#get max temperature
return max(surface_results)

This works fine, but what if we now wanted the average temperature?  We can add the appropriate math at the end of the file:

#get average temperature
return sum(surface_results)/ len(surface_results)

This would also require us to execute the script again, selecting each surface, compiling the same list of numbers in memory, and then performing the appropriate math.  As the math gets more complicated, the number of surfaces increases, or for convenience, we may need to import various python modules such as statistics, functools, Numpy, etc.  Some of these may not be readily available in the pre-compiled version of python included with Autodesk CFD.  It would also be more efficient to extract the data once and manipulate it, instead of constantly extracting the same results over and over.

To extend our post processing beyond the limitations of the pre-packaged Autodesk CFD python, we abstract the CFD only dependencies into a separate script (which requires an Autodesk CFD session) that outputs a csv file of the raw data.  Once the data is in a .csv file, we can read that in and manipulate it by any means (python, excel, on difference computers without Autodesk CFD).

from CFD import Setup
from CFD import Results
from CFD import DSE
import csv

# Get current scenario & activate results

study = Setup.DesignStudy.Create()
scenario = study.getActiveScenario()
curr_results = scenario.results()
curr_results.activate()

#list of critical ID's
critical_surfaces = [1,3,5,7,9]

# Create a WallResults object
wr = Results.WallResults(scenario)

#loop through surfaces
for surface in critical_surfaces:
    wr.select(surface)
wr.calculate()

#write ID, temp to results file
with open(‘results_output.csv’,’wb’) as csvfile:
writer = csv.writer(csvfile, quoting=csv.QUOTE_NONE)
for surface in critical_surfaces:
writer.writerow( [surface, wr.temperature(surface)] )

 Our final script would now read from the csv file and manipulate it as needed, without having to start an Autodesk CFD session or re-calculate the temperatures for each surface.

Enjoy!

Download CFD python script (txt)

Topics: CFD

Back to home