The following is a brief introduction to Ansys Workbench and Finite Element Analysis (FEA). FEA can be a very powerful tool in analyzing heat transfer and momentum transfer problems. For fluids, another name for it is Computational Fluid Dynamics (CFD). The following is a brief introduction to Ansys Workbench and Finite Element Analysis (FEA).
#import needed packages
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from scipy.optimize import fsolve1D Heat Equation (Heat Conduction Down a Rod)¶


# consistent parameters
alpha = 0.01 # Thermal diffusivity, k/Cp/rhoSteady-state Scenario¶
For each of the finite elements:
Equations for each element can be set and then solved using Scipy’s fsolve.
totallength = 1
sections = np.arange(0,11,1)
dx = totallength/sections[-1]#initial temps/boundary conditions
Temps = np.ones(len(sections))*400
Temps[0] = 500; Temps[-1] = 300
Tinitial = Temps.copy()#equations to solve
def func(T):
dTdt = [T[0]-Temps[0]]
for each in range(1,len(sections)-1):
dTdt.append(alpha/dx**2*(T[each+1] - 2*T[each] + T[each-1]))
#dTdt.append(alpha/dx**2*(T[-2] - 2*T[-1] + T[-2])) #adiabatic end
dTdt.append(T[-1]-Temps[-1]) #constant temperature end
return dTdtsol = fsolve(func,Temps)xpos = np.arange(0,len(Temps)*dx,dx)
plt.plot(xpos,Tinitial,'k-+',alpha=0.5,label='initial condition')
plt.plot(xpos,sol,'b-+',alpha=0.5,label='steady state')
plt.xlabel('x position'); plt.ylabel('Temperature');plt.legend()
plt.show()
Transient Scenario¶
For each of the finite elements:
Equations for each element can be set and then solved using Euler’s method or with odeint. Both methods are shown below.
Eulers Method¶
# Parameters
L = 1.0 # Length of the rod
T = 0.5 # Total time
Nx = 100 # Number of spatial points
Nt = 100 # Number of time steps
dx = L / (Nx - 1) # Spatial step size
dt = T / Nt # Time step size
# Initial condition
def initial_condition(x):
return 400*np.ones(len(x)) #np.sin(np.pi * x)
# Set up the grid
x_values = np.linspace(0, L, Nx)
u = initial_condition(x_values) # Set initial temperature distribution
# Applying boundary conditions
u[0] = 500 # Set left end of the bar to 500°C
u[-1] = 300 # Set right end of the bar to 300°C
# Explicit finite difference method
for n in range(Nt):
u_new = u.copy()
for i in range(1, Nx - 1):
d2Tdx2 = 1 / (dx**2) * (u[i + 1] - 2 * u[i] + u[i - 1])
u_new[i] = u[i] + alpha * dt * d2Tdx2
u = u_new
# Plotting the results
plt.figure(figsize=(8, 6))
plt.plot(x_values, u, label='Temperature distribution')
plt.title('1D Heat Equation with Boundary Conditions')
plt.xlabel('Rod Length')
plt.ylabel('Temperature')
plt.legend()
plt.grid(True)
plt.show()

Odeint (More stable)¶
totallength = 1
sections = np.arange(0,100,1)
dx = totallength/sections[-1]#initial temps
Temps = np.ones(len(sections))*400
Temps[0] = 500; Temps[-1] = 300
#time array
times = np.linspace(0,5,100)def dTdt(T,time): #T is an array
dTdt = [0]
for each in range(1,len(sections)-1):
dTdt.append(alpha/dx**2*(T[each+1] - 2*T[each] + T[each-1]))
#dTdt.append(alpha/dx**2*(T[-2] - 2*T[-1] + T[-2])) #adiabatic end
dTdt.append(0) #constant temperature end
return dTdtsol = odeint(dTdt,Temps,times)xpos = np.arange(0,len(Temps)*dx,dx)for i in range(len(times)):
if i%10 == 0:
plt.plot(xpos,sol[i])
pass
plt.plot(xpos,sol[-1])
plt.plot(x_values,u,'k-.',alpha=0.5,label='Euler')
plt.ylim([295,505]);plt.legend()
plt.show()
Concentration in a Tube¶
What is the concentration as a function of time given a plug of gas is introduced into one end of a tube? Or in other words, if I inject methane into one end, how long do I have to wait before the entire tube has a concentration in the flammability limit? Given the ends are sealed.
totallength = 6/3.281 #6 feet to meters
from difflib import Differ
sections = np.arange(0,200,1)
dx = totallength/sections[-1]
DifAB = 2.2E-5*3600 #m2/hr diffusion coefficient of methane into air#initial temps
Conc = np.zeros(len(sections))
Conc[0:20] = 1
#time array
times = np.linspace(0,18,100) #hoursdef dCdt(C,time): #T is an array
dCdt = [DifAB / dx**2 * (2 * C[1] - 2 * C[0])] #left boundary
for each in range(1,len(sections)-1):
dCdt.append(DifAB/dx**2*(C[each+1] - 2*C[each] + C[each-1]))
dCdt.append(DifAB/dx**2*(2*C[-2] - 2*C[-1])) #right boundary
return dCdtsolC = odeint(dCdt,Conc,times)xpos = np.arange(0,len(Conc)*dx,dx)num_curves = 10 # Adjust this to control how many lines are plotted
log_indices = np.unique(
np.concatenate([[0], np.logspace(0, np.log10(len(times) - 1), num=num_curves).astype(int)])
)
for i in log_indices:
plt.plot(xpos, solC[i], label=f'time = {times[i]:.2f} hr')
plt.plot(xpos,solC[-1])
plt.grid(alpha=0.2); plt.ylabel('Fraction Gas'); plt.xlabel('Distance')
plt.legend()
plt.show()
times[33]np.float64(6.0)solC[33][0], solC[33][-1](np.float64(0.1461180240862919), np.float64(0.051208907684133914))About 6 hours until the conentration in the entire pipe is within the flammability zone. That’s a long time, way longer than expected especially considering the speed of individual gas molecules.
First term Fourier series (from the analytical solution of the heat equation) time constant as a check:
tauC = 2**2/(np.pi**2*2.2E-5*3600) #hrs
tauC5.117231497087767Twice the time constant gets you to almost the full mixing which is 10 hours. You can see that the time required for smaller changes in the overall concentration gets longer.
Ansys Workbench¶
The above programmatic solutions are great for simple problems, but for more complex problems, it is better to use a program like Ansys Workbench. Ansys Workbench is a program that allows you to build a model and then solve it using FEA. The following video is shows a brief introduction to Ansys Workbench.
