Running XFoil from Python

In one of my YouTube videos, I show how to run XFoil from a MATLAB script. For those of you that don’t have MATLAB or prefer Python, here is the same script, but now in Python. The functionality should be the exact same. I have uploaded a .txt file with the Python code because I am not able to upload Python files to my website at the moment. Just save it as a .py file when you download it (it will open in a new window).

Python_XFoil.py
Python_XFoil.py

The code can also be seen below.

import os
import numpy as np
import matplotlib.pyplot as plt

# %% CREATE LOADING FILE

# Knowns
NACA       = '0012'
AoA        = '0'
numNodes   = '170'
saveFlnmAF = 'Save_Airfoil.txt'
saveFlnmCp = 'Save_Cp.txt'
xfoilFlnm  = 'xfoil_input.txt'

# Delete files if they exist
if os.path.exists(saveFlnmAF):
    os.remove(saveFlnmAF)

if os.path.exists(saveFlnmCp):
    os.remove(saveFlnmCp)
    
# Create the airfoil
fid = open(xfoilFlnm,"w")
fid.write("NACA " + NACA + "\n")
fid.write("PPAR\n")
fid.write("N " + numNodes + "\n")
fid.write("\n\n")
fid.write("PSAV " + saveFlnmAF + "\n")
fid.write("OPER\n")
fid.write("ALFA " + AoA + "\n")
fid.write("CPWR " + saveFlnmCp + "\n")
fid.close()

# Run the XFoil calling command
os.system("xfoil.exe < xfoil_input.txt")

# Delete file after running
if os.path.exists(xfoilFlnm):
    os.remove(xfoilFlnm)

# %% READ DATA FILE: AIRFOIL

flpth = "C:/Users/Josh/Documents/Python/Panel_Methods/"
flnm  = flpth + saveFlnmAF
    
# Load the data from the text file
dataBuffer = np.loadtxt(flnm, skiprows=0)

# Extract data from the loaded dataBuffer array
XB = dataBuffer[:,0]
YB = dataBuffer[:,1]

Health treatments that are used to cope with cruel ailments are presented there to cope with Low Sexual Drive Or Libido? * Take much touted libido boosting foods (known as aphrodisiacs) * Indulge in chocolate for some aphrodisiac and mood-lifting effects * Take daily herbs to stimulate blood flow * Yohimbine to enhance the quality of an erection * Boosting self confidence attributes to increase the. http://www.icks.org/data/ijks/1483321954_add_file_1.pdf ordine cialis on line Without gallbladder acidic, corroded liver bile irritates bile ducts, sphincter of Oddi, duodenum; the first part of the answer comes from  vardenafil cost the nature of the US retail selling operation. Second, the spikes and falls in lowest prices viagra  blood sugar levels can leave men feeling sluggish, irritable, and not at all in the mood for sex. Lots of young adults are there who are suffering from this problem and they want to collect online medicine instead of branded medicines. viagra without side effects # Delete file after loading
if os.path.exists(saveFlnmAF):
    os.remove(saveFlnmAF)

# %% READ DATA FILE: PRESSURE COEFFICIENT

# Load the data from the text file
dataBuffer = np.loadtxt(saveFlnmCp, skiprows=3)

# Extract data from the loaded dataBuffer array
X_0  = dataBuffer[:,0]
Y_0  = dataBuffer[:,1]
Cp_0 = dataBuffer[:,2]

# Delete file after loading
if os.path.exists(saveFlnmCp):
    os.remove(saveFlnmCp)

# %% EXTRACT UPPER AND LOWER AIRFOIL DATA

# Split airfoil into (U)pper and (L)ower
XB_U = XB[YB >= 0]
XB_L = XB[YB < 0]
YB_U = YB[YB >= 0]
YB_L = YB[YB < 0]

# Split XFoil results into (U)pper and (L)ower
Cp_U = Cp_0[YB >= 0]
Cp_L = Cp_0[YB < 0]
X_U  = X_0[YB >= 0]
X_L  = X_0[YB < 0]

# %% PLOT DATA

# Plot airfoil
fig = plt.figure(1)
plt.cla()
plt.plot(XB_U,YB_U,'b.-',label='Upper')
plt.plot(XB_L,YB_L,'r.-',label='Lower')
plt.xlabel('X-Coordinate')
plt.ylabel('Y-Coordinate')
plt.title('Airfoil')
plt.axis('equal')
plt.legend()
plt.show()

# Plot pressure coefficient
fig = plt.figure(2)
plt.cla()
plt.plot(X_U,Cp_U,'b.-',label='Upper')
plt.plot(X_L,Cp_L,'r.-',label='Lower')
plt.xlim(0,1)
plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')
plt.title('Pressure Coefficient')
plt.show()
plt.legend()
plt.gca().invert_yaxis()