//Python Scripts

Python Scripts

Fluent in batch mode

os.system(r'"C:\Program Files\ANSYS Inc\v195\fluent\ntbin\win64/fluent.exe" 2d -g -t2 -i case4.jou' )

In these examples,

  • fluent is the command you type to execute ANSYS FLUENT interactively.
  • -g indicates that the program is to be run minimized in the taskbar.
  • -i journal reads the specified journal file.
  • -wait is the command you type in a DOS batch file or some other script in a situation where the script needs to wait until ANSYS FLUENT has completed its run.
  • -hidden is similar to the -wait command, but also executes ANSYS FLUENT completely hidden and noninteractively.

To get an output (or transcript) file while running ANSYS FLUENT in the background on a Windows system, the journal file must contain the following command to write a transcript file:

; start transcript file
/file/start-transcript outputfile.trn

where the outputfile is a file that the background job will create and which will contain the output that ANSYS FLUENT would normally print to the screen (e.g., the menu prompts and residual reports).

Remove duplicate zones in Tecplot

#flist = flist[0:5] # just for testing on 5 files and not all of them!
import tecplot as tp
from tecplot.exception import *
from tecplot.constant import *
# Uncomment the following line to connect to a running instance of Tecplot 360:

for fname in flist:
dataset = tp.data.load_tecplot(fname, read_data_option=2) #option 2 is replace, #option 1 is default which is append
zList = list(dataset.zone_names)
# Find duplicate zones and set to remove
dups = [i for i, x in enumerate(zList) if zList.count(x) > 1]
remDups = dups[0::2]
# Remove duplicate zones
# Save data
tp.data.save_tecplot_plt(f'clean_{fname[4:]}', dataset=dataset, variables=None, zones=None)

DPM to Tec

fnameClean = 'cleanTemp.dat'
def cleanFile(filename):
fnameClean = 'cleanTemp.dat'
with open(filename, 'r') as infile, open(fnameClean, 'w') as outfile:
data = infile.read()
data = data.replace("(", "")
data = data.replace(")", "")

def writeTec(pdData, suffx):
ffname = fname[:-4] + '_'+ suffx +'mic.dat'
ftec = open(ffname, 'w')
ftec.write(f'TITLE = "{fname[:-4]}_{suffx}mic"\n')
ftec.write('VARIABLES = "X", "Y", "Z", "u","v","w","diam" \n')
ftec.write(f'ZONE T="{fname[:-4]}_{suffx}mic" \n')
ftec.write(pdData.to_string(header=False, index=False))

Generate points in Fluent for monitoring

fname = 'points-for-monitoring.txt'
df = pd.read_csv(fname, skiprows=0, delim_whitespace=True, names=['x','y','z'])
fid = open('createPoints.jou','w')
count = 1
for index, row in df.iterrows():
fid.write(f'/surface/point-surf p{count:02} {row["x"]} {row["y"]} {row["z"]}\n')
count = count+1

Transient Flow Profile

import numpy as np
from matplotlib import pyplot as plt
tStart = (cycle-1)*4+1.65
timestep =0.0001 # timestep size
time = np.arange(timestep,2.35+timestep,timestep)
a1 =      0.3674
b1 =       1.429
a2 =      0.0351
b2 =       6.223
a3 =     0.08071
b3 =       3.572
# ===== THIS PRODUCES L/S convert kg/s by /1000*density     
mfr =  (a1*np.sin(b1*time) + a2*np.sin(b2*time) + a3*np.sin(b3*time))/1000*density
# ===== THIS PRODUCES L/S convert kg/s by /1000*density
printTime = tStart + time
plt.plot(printTime, mfr)
nTerms = len(time)
# check the volume
np.trapz(mfr, x=time)
#print adjusted time
# write transient profile -mass FR
fileName = "exhale-mfr.prof"
nTerms = len(printTime)
fid = open(fileName,"w")      # Open the file for writing
fid.write(f"( (exhale_cycle1 transient {nTerms} 1)\n") #1 =periodic 0=not periodic
fid.write("(time ")
fid.write(" ".join(str(format(i, ".7f")) for i in printTime))
fid.write("(mass-flow ")
fid.write(" ".join(str(format(i, ".12f")) for i in mfr))