Alternative Boundary Condition Implementations for Crank Nicolson Solution to the Heat Equation ME 448/548 Notes Gerald Recktenwald Portland State University Department of Mechanical Engineering gerry@pdx.edu ME 448/548: Alternative BC Implementation for the Heat Equation
Overview. Goal is to allow Dirichlet, Neumann and mixed boundary conditions 2. Use ghost node formulation Preserve spatial accuracy of O( x 2 ) Preserve tridiagonal structure to the coefficient matrix 3. Implement in a code that uses the Crank-Nicolson scheme. 4. Demonstrate the technique on sample problems ME 448/548: Alternative BC Implementation for the Heat Equation page
Heat Transfer Boundary Conditions T w i = x = 2 3 Constant wall temperature, T w, is prescribed. The solution gives the internal temperatures and the wall heat flux, q w. q w i = x = 2 3 Constant wall heat flux, q w, is prescribed. The solution gives the internal temperatures temperatures and wall temperature, T w h, T i = x = 2 3 Convective boundary condition where h and T are prescribed. The solution gives the internal temperatures, the wall temperature, T w, and the wall heat flux, q w. ME 448/548: Alternative BC Implementation for the Heat Equation page 2
Convective Boundary Condition The general form of a convective boundary condition is u x = g + h u () x= This is also known as a Robin boundary condition or a boundary condition of the third kind. The simplistic implementation is to replace the derivative in Equation () with a one-sided difference u k+ 2 u k+ = g + h u k+ (2) x
Introduce a Ghost Node Imagine that there is a node û that is outside of the domain u u u 2 u 3... x x x x x 2 this node is used to enforce the boundary condition from Equation (). The value û does not explicitly appear in the numerical scheme. ME 448/548: Alternative BC Implementation for the Heat Equation page 4
Use the BC to compute û by extrapolation Use a central difference approximation at x = (x = x ) to impose the boundary condition. The value of û condition is u 2 û 2 x = g + h u. (3) consistent with the boundary Edge of physical domain u u 2... û = u 2 2 x(g + h u ). (4) x x x x x 2 ME 448/548: Alternative BC Implementation for the Heat Equation page 5
Equation for u Evaluate the finite difference form of the heat equation at x = x. u k+ u k t = θα 2u k+ + u k+ ] [ûk+ x 2 2 [ûk + ( θ)α 2uk + uk 2 x 2 ] Choose θ = /2 and use the formulas for û at time step k and time step k + u k+ u k t = θα x 2 [ u k+ 2 2 x(g k+ + h k+ u k+ ) 2u k+ + u k+ 2 ( θ)α + x 2 The terms in boxes are from the boundary condition [ u k 2 2 x(gk + hk uk ) 2uk + uk 2 ] ] ME 448/548: Alternative BC Implementation for the Heat Equation page 6
Rearrange the Equation for u Algebraically rearranging the preceding equation gives where a u k+ + b u k+ 2 = d (5) a = t + 2θα x2( + xhk+ ) (6) b = 2θα (7) x [ 2 ] 2( θ)α d = ( + xh k t x 2 ) u k (8) 2( θ)α + u k x 2 2 2α [ ] θg k+ + ( θ)g k x These equations define the terms for the first row in the system of equations ME 448/548: Alternative BC Implementation for the Heat Equation page 7
Data structure for implementing alternative BC in the Matlab code Store the boundary condition specification in a 2 3 matrix. The first row has data for x = and the second row has data for x = L. The first column is a flag with the boundary condition type. ubc = Type Value Value 2 Type Value Value 2 b = at the x = boundary and b = 2 at the x = L boundary ubc(b, ) = : u(x b ) = value ubc(b, 2) = value of u at boundary ubc(b, 3) = not used ubc(b, ) = 2: u/ x xb = g + hu(x b ) ubc(b, 2) = g ubc(b, 3) = h ME 448/548: Alternative BC Implementation for the Heat Equation page 8
Verification: Solve the toy problem on half of the domain The toy problem used to test the codes u t = α 2 u x 2 t >, x L u(, t) = u(l, t) = ; u(x, ) = sin(πx/l) only needs to be solved on one half of the domain u t = u α 2 t >, x L/2 x 2 u u(, t) = ; x = L/2 u(x, ) = sin(πx/l) u t = u α 2 t >, L/2 x L x 2 u x = u(l, t) = ; L/2 u(x, ) = sin(πx/l) ME 448/548: Alternative BC Implementation for the Heat Equation page 9
Verification: Solve the toy problem on half of the domain Output of democnbc.5 IC CN Exact.5 IC CN Exact u.5.5..2.3.4 x.5.6.7.8.9 x ME 448/548: Alternative BC Implementation for the Heat Equation page