MATH 212 Advanced Calculus 2 for Electrical Engineering Advanced Calculus 2 for Nanotechnology Engineering NE 217 Finite-Element Methods in Two Dimensions Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada Copyright © 2011 by Douglas Wilhelm Harder. All rights reserved. Finite-element Methods in Two Dimensions Outline This topic discusses an introduction to finite-element methods in two dimensions – – – – – – What we did in one dimension Dividing up a 2-D region Defining planar test functions on those regions Converting the test functions to systems of linear equations Solving the system Examples 2 Finite-element Methods in Two Dimensions Outcomes Based Learning Objectives By the end of this laboratory, you will: – You will understand dividing regions into triangular sub-regions (tessellations) – Understand the mathematical theory for performing finite-element methods in two dimensions 3 Finite-element Methods in Two Dimensions Integration-by-Parts in Higher Dimensions Before we start, we need a higher-dimensional version of integration by parts: – Using the properties of the gradient: u v uv v u u v dxdy uv dxdy v u dxdy uv u v v u R R R – Now use a corollary of Stokes’ theorem: u v dxdy uvds v u dxdy R R R Ref: http://en.wikipedia.org/wiki/Vector_calculus_identities 4 Finite-element Methods in Two Dimensions Integration-by-Parts in Higher Dimensions This variation has the product of two scalar-valued functions replaced with a product of scalar- and vector-valued functions – Using the properties of the gradient: u v uv u v u v dxdy uv dxdy u vdxdy uv u v v u R R R – Now use a corollary of Stokes’ theorem: u v dxdy uv ds v u dxdy R R R 5 Finite-element Methods in Two Dimensions Poisson’s Equation in Two Dimensions At this point, we have generalized the finite-difference method to allow arbitrarily spaced points in one dimension to solve systems such as Poisson’s equation: x, y 2 u x, y 0 The ultimate goal is to do this in 2 and 3 dimensions – In order to achieve this, we need one further idea 6 Finite-element Methods in Two Dimensions Extending Test Functions In the previous – Use triangles to divide up the region instead of intervals • Tessellations – Approximate the solution using interpolating polynomials Intervals Tessellation 7 Finite-element Methods in Two Dimensions Partitioning a Region Given a region R, a partial differential equation defines conditions on the boundary ∂R and we attempt to find an approximation of the solution in the region 8 Finite-element Methods in Two Dimensions Partitioning a Region We have seen how using finite differences, we: – Create a uniform grid – Assign points to either the boundary or the interior of the region 9 Finite-element Methods in Two Dimensions Partitioning a Region Unfortunately, uniform grids make a poor approximation of most real-world situations 10 Finite-element Methods in Two Dimensions Partitioning a Region It would be much more desirable to allow the user to select the placement of points where appropriate: – Non-linear boundaries can be fitted – Points can be concentrated at regions of interest Let N be the number interior points – in this case, 22 11 Finite-element Methods in Two Dimensions Partitioning a Region In one dimension, the regions between points were intervals – Similarly, we must divide the region into a number of sub-regions 12 Finite-element Methods in Two Dimensions Partitioning a Region Our goal will be to find approximations of the solution on the interior of the region R 13 Finite-element Methods in Two Dimensions Partitioning a Region We will divide the region into triangular elements: – This forms a connected graph – The collection of all triangles is a tessellation – Tessella is the Latin word for a small piece of a mosaic 14 Finite-element Methods in Two Dimensions Partitioning a Region From wikipedia: a tessellation of a 2D magnetostatic configuration User: Zureks 15 Finite-element Methods in Two Dimensions Defining Test Functions In one dimension, for each interior point, xk, we used the two adjacent intervals upon which we defined the test functions f6(x) – The support of the test function f6(x) is the interval [x5, x7] f7(x) 16 Finite-element Methods in Two Dimensions Defining Test Functions In two dimensions, given the point xk, we will define a test function on the region comprised of triangles adjacent to xk – Call that region Rk 17 Finite-element Methods in Two Dimensions Defining Test Functions For ease of reference, given a point xk, we will number the adjacent points, in this case: xk,1 xk,2 xk,3 xk,4 xk,5 xk,6 18 Finite-element Methods in Two Dimensions Defining Test Functions Similarly, we will label each triangles with Rk, in this case: Rk,1 Rk,2 Rk,3 Rk,4 Rk,5 Rk,6 19 Finite-element Methods in Two Dimensions Defining Test Functions On each region Rk, we will define a piecewise planar test function – The test function is fk(x, y) – It is defined to be zero outside Rk – It is also useful to have it zero on the boundary ∂Rk 20 Finite-element Methods in Two Dimensions Defining Test Functions The formula for each plane fk, ℓ(x, y) defined on Rk, ℓ is a plane of the form ax + by + g – To find it, we use linear algebra 21 Finite-element Methods in Two Dimensions Finding Interpolating Planes Given three points in the plane (x1, y1), (x2, y2), (x3, y3) if we want to find the interpolating plane ax + by + g that passes through these points z1, z2, and z3, respectively, we must solve the system of linear equations a x1 b y1 g z1 a x2 b y2 g z2 a x3 b y3 g z3 22 Finite-element Methods in Two Dimensions Finding Interpolating Planes Rewritten using matrices and vectors, we must solve: x1 x2 x 3 y1 1 a z1 y2 1 b z 2 y3 1 g z3 23 Finite-element Methods in Two Dimensions Finding Interpolating Planes Example, given the points in the plane (1.3, 5.4), (2.9, 7.0) and (6.5, 4.9) 24 Finite-element Methods in Two Dimensions Finding Interpolating Planes Example, given the points in the plane (1.3, 5.4), (2.9, 7.0) and (6.5, 4.9) we want to find the polynomial ax + by + g that passes through the points 2.8, 1.6 and 3.9, respectively 25 Finite-element Methods in Two Dimensions Finding Interpolating Planes Example, given the points in the plane (1.3, 5.4), (2.9, 7.0) and (6.5, 4.9) we want to find the polynomial ax + by + g that passes through the points 2.8, 1.6 and 3.9, respectively We must therefore solve 1.3 5.4 1 a 2.8 2.9 7.0 1 b 1.6 6.5 4.9 1 g 3.9 >> [1.3 5.4 1; 2.9 7.0 1; 6.5 4.9 1] \ [2.8 1.6 3.9]' ans = 0.1272 -0.8772 7.3715 26 Finite-element Methods in Two Dimensions Finding Interpolating Planes From >> [1.3 5.4 1; 2.9 7.0 1; 6.5 4.9 1] \ [2.8 1.6 3.9]' ans = 0.1272 -0.8772 7.3715 we have that the interpolating plane is 0.1272x – 0.8772y + 7.3715 27 Finite-element Methods in Two Dimensions Using the Test Function Now, going back to the problem: – We have the test function fk(x, y) and we have a function V(x, y) that is known to satisfy the formula V(x, y) = 0 As we did in one dimension, it must therefore be true that f x, y V x, y dxdy 0 k Rk 28 Finite-element Methods in Two Dimensions Using the Test Function Again, let’s consider Poisson’s equation: x, y u x, y 0 2 We defined def V x, y 2u x, y and thus our integral is x, y 0 x, y 2 R fk x, y u x, y 0 dxdy 0 k 29 Finite-element Methods in Two Dimensions Using the Test Function Expanding the integral, we get x, y R fk x, y u x, y dxdy R fk x, y 0 dxdy 2 k k where the right-hand side is reasonably easy to calculate The left-hand side, however, requires further calculus 30 Finite-element Methods in Two Dimensions Using the Test Function Using the 2-dimensional equivalent of integration-by-parts, we get: 2 f x , y u x, y dxdy k Rk f x, y u x, y ds f x, y u x, y dxdy k Rk k Rk 31 Finite-element Methods in Two Dimensions Using the Test Function Using the 2-dimensional equivalent of the fundamental theorem of calculus: 0 f x, y u x, y dxdy f x, y u x, y ds f x, y u x, y dxdy 2 k Rk k Rk k Rk However, recall that we specifically chose a test function that is zero on the boundary ∂Rk – Therefore, the first term disappears! 32 Finite-element Methods in Two Dimensions Using the Test Function Therefore, we have the easier integral: 2 f x , y u x, y dxdy fk x, y u x, y dxdy k Rk Rk and therefore we have x, y fk x, y u x, y dxdy fk x, y dxdy 0 R R k k 33 Finite-element Methods in Two Dimensions Using the Test Function We can calculate the right-hand side: both are known x, y fk x, y u x, y dxdy fk x, y dxdy 0 R R k k The left-hand side has the unknown function u(x, y)… 34 Finite-element Methods in Two Dimensions Using the Test Function We can calculate the right-hand side: both are known x, y fk x, y u x, y dxdy fk x, y dxdy 0 R R k k First, rewrite the left-hand side as integrals over each sub-region nk fk , x, y u x, y dxdy 1 Rk , x, y R fk x, y 0 dxdy k Let nk be the number of triangles touching xk – in this case, 6 35 Finite-element Methods in Two Dimensions Finding the Test Function Gradient The test function defined on each sub-region is planar, that is fk , x, y a x b y g thus we can easily calculate: f x , y x k , a fk , x, y f x, y b y k , nk fk , x, y u x, y dxdy 1 Rk , x, y R fk x, y 0 dxdy k 36 Finite-element Methods in Two Dimensions Finding the Test Function Gradient a To find the gradient of this plane, we must find the vector b x k : x1 y1 1 a 1 x k ,1 : x2 y2 1 b 0 x k ,2 : x3 y3 1 g 0 nk fk , x, y u x, y dxdy 1 Rk , x, y R fk x, y 0 dxdy k 37 Finite-element Methods in Two Dimensions Finding the Test Function Gradient Solving for a is as follows: 1 y1 1 det 0 y2 1 0 y 1 y2 y3 3 a x1 y1 1 x1 y1 1 det x2 y2 1 det x2 y2 1 x y 1 x y 1 3 3 3 3 6 fk , x, y u x, y dxdy 1 Rk , x, y R fk x, y 0 dxdy k 38 Finite-element Methods in Two Dimensions Finding the Test Function Gradient Solving for b is as follows: x1 1 1 det x2 0 1 x 0 1 x3 x2 3 b x1 y1 1 x1 y1 1 det x2 y2 1 det x2 y2 1 x y 1 x y 1 3 3 3 3 6 fk , x, y u x, y dxdy 1 Rk , x, y R fk x, y 0 dxdy k 39 Finite-element Methods in Two Dimensions Approximating the Solution Gradient The next step is to approximate the unknown u x, y 6 fk , x, y u x, y dxdy 1 Rk , x, y R fk x, y 0 dxdy k 40 Finite-element Methods in Two Dimensions Approximating the Solution Gradient The actual unknown function u(x, y) an unknown and likely nonlinear function – You don’t get paid $100 000/a to find a linear function… 41 Finite-element Methods in Two Dimensions Approximating the Solution Gradient Recall from 1-D finite element methods: – We approximated the solution with an unknown piecewise linear function 42 Finite-element Methods in Two Dimensions Approximating the Solution Gradient We are attempting to approximate u(x, y) at a number of interior points where the solution is defined on the border 43 Finite-element Methods in Two Dimensions Approximating the Solution Gradient We can do the same as we did in one dimension: – Approximate the solution by a piecewise planer approximation 44 Finite-element Methods in Two Dimensions Approximating the Solution Gradient As before, we can find the interpolating polynomial: x k : x1 x k ,1 : x2 x k ,2 : x3 y1 1 a uk y2 1 b uk ,1 y3 1 c uk ,2 6 fk , x, y u x, y dxdy 1 Rk , x, y R fk x, y 0 dxdy k 45 Finite-element Methods in Two Dimensions Approximating the Solution Gradient a To find the gradient of this triangle, we must find the vector b x k : x1 y1 1 a uk x k ,1 : x2 y2 1 b uk ,1 x k ,2 : x3 y3 1 c uk ,2 6 fk , x, y u x, y dxdy 1 Rk , x, y R fk x, y 0 dxdy k 46 Finite-element Methods in Two Dimensions Approximating the Solution Gradient Solving for a is as follows: uk y1 1 det uk ,1 y2 1 u y3 1 uk y2 y1uk ,2 uk ,1 y3 uk y3 y1uk ,1 y2uk ,2 k ,2 a x1 y1 1 x1 y1 1 det x2 y2 1 det x2 y2 1 x y 1 x y 1 3 3 3 3 6 fk , x, y u x, y dxdy 1 Rk , x, y R fk x, y 0 dxdy k 47 Finite-element Methods in Two Dimensions Approximating the Solution Gradient Solving for b is as follows: x1 uk 1 det x2 uk ,1 1 x u 1 x1uk ,1 x3uk x2uk ,2 x1uk ,2 uk x2 uk ,1 x3 3 k ,2 b x1 y1 1 x1 y1 1 det x2 y2 1 det x2 y2 1 x y 1 x y 1 3 3 3 3 6 fk , x, y u x, y dxdy 1 Rk , x, y R fk x, y 0 dxdy k 48 Finite-element Methods in Two Dimensions Approximating the Double Integral Thus, we can find both gradients: fk , x, y u x, y dxdy Rk ,1 a a , b b dxdy Rk ,1 If you look back, all values are either known – The positions of the points xk, xk,1 and xk,2: x1, x2, x3, y1, y2 , y3 or unknown constants uk, uk,1 and uk,2 : – Take them out of the integral a a b b 1dxdy Rk ,1 49 Finite-element Methods in Two Dimensions Approximating the Double Integral Finally, we note that the right-hand integral f x, y u x, y dxdy a a b b 1dxdy k, Rk ,1 Rk ,1 is just the area of the region Rk,1, call it Ak,1: x1 1 Ak ,1 det x2 2 x 3 y1 1 y2 1 y3 1 50 Finite-element Methods in Two Dimensions Approximating the Double Integral Thus, for each interior point xk, we must calculate: nk nk 1 Rk , 1 Rk , fk , x, y u x, y dxdy fk , x, y x, y dxdy 0 For each xk, there are nk adjacent triangles Rk,ℓ, and each of these is associated given three points, call them x1 = (x1, y1), x2 = (x2, y2), x3 = (x3, y3) and the unknowns associated with, call them u1, u2 and u3: – Each interior point ends up with nk linear equations that are summed together forming a single linear equation 51 Finite-element Methods in Two Dimensions Approximating the Double Integral x, y is a constant, the right-hand integral can be If the ratio 0 simplified further: x, y 1 f x , y dxdy Ak 1 k , 3 0 R nk k, where Ak is the area of the region Rk 52 Finite-element Methods in Two Dimensions Finding the points This will produce N linear equations and unknowns – We can solve this system of linear equations for the unknown values u1, u2, u3, …, uN 53 Finite-element Methods in Two Dimensions Code For 2-D Finite Elements A set of functions are available on the web site that set a finite element system and solve for the interior values – It assumes the right-hand side of Poisson’s equation is constant 2u x, y fe_system( rho, pts ) Create a data structure for the above equation by passing in a set of points (interior and boundary) with values for the boundary points and -Inf for the interior points fe_update( obj, pt, alist ) Indicate the tessellation around an interior point by passing a list of neighbouring points fe_solve( obj ) Having called fe_update for each interior point, now solve the system of equations returning the initial argument pts with all values -Inf replaced with values fe_coeffs( x1, x2, x3 ) A helper function 54 Finite-element Methods in Two Dimensions Example 1 Consider the following region and we are trying to approximate Laplace’s equation ( = 0) with the following region and boundary values: 55 Finite-element Methods in Two Dimensions Example 1 We can define the system as follows: pts1 = [ 0 -1 -1 -1 0 1 0 0 -Inf 1 0 1 -1 1 0 0 1 -Inf 0 2 -4]'; obj1 = fe_system( 0, % 1 % 2 % 3 % 4 % 5 % 6 % 7 pts1 ); 56 Finite-element Methods in Two Dimensions Example 1 We define the tessellation around the point 3 by listing the adjacent points in order (counter-clockwise): obj1 = fe_update( obj1, 3, [4 6 2 1] ); Note, Matlab does not have proper objects, thus we simulate object-oriented programming by replacing obj1.fe_update( 3, [4, 6, 2, 1] ) with obj1 = fe_update( obj1, 3, [4, 6, 2, 1] ) 57 Finite-element Methods in Two Dimensions Example 1 Similarly, we define the tessellation around the point 6 by listing the adjacent points in order (counter-clockwise): obj1 = fe_update( obj1, 6, [4 7 5 2 3] ); 58 Finite-element Methods in Two Dimensions Example 1 Having defined the system, we solve the system and plot the result: u1 = fe_solve( obj1 ) u1 = 0 -1.0000 0 -1.0000 0 0 -1.0000 1.0000 -0.1053 1.0000 -1.0000 0 0 0 1.0000 1.0000 2.0000 1.0000 0 -1.4211 -4.0000 plot3( u1(1,:), u1(2,:), u1(3,:), 'ko' ) The plot was augmented in CorelDRAW! 59 Finite-element Methods in Two Dimensions Example 2 Consider the following region and we are trying to approximate Poisson’s equation with = 4, the following region and boundary values: 60 Finite-element Methods in Two Dimensions Example 2 We can define the system as follows: pts2 = [-2 -2 8 % 1 1 -2 5 % 2 -3 -1 10 % 3 0 -1 -Inf % 4 3 -1 10 % 5 -1 0 -Inf % 6 2 0 -Inf % 7 -3 1 10 % 8 1 1 –Inf % 9 3 1 10 % 10 -2 2 8 % 11 0 2 -Inf % 12 1 3 10]'; % 13 obj2 = fe_system( 4, pts2 ); 61 Finite-element Methods in Two Dimensions Example 2 We then define the various tessellations: obj2 obj2 obj2 obj2 obj2 = = = = = fe_update( fe_update( fe_update( fe_update( fe_update( obj2, 4, obj2, 6, obj2, 7, obj2, 9, obj2, 12, [1 [1 [2 [4 [6 2 7 9 6] ); 4 9 12 11 8 3] ); 5 10 9 4] ); 7 10 13 12 6] ); 9 13 11] ); 62 Finite-element Methods in Two Dimensions Example 2 Finally, we solve the system: u2 = fe_solve( obj2 ) u = Columns 1 through 8 -2.0000 1.0000 -3.0000 0 3.0000 -1.0000 2.0000 -3.0000 -2.0000 -2.0000 -1.0000 -1.0000 -1.0000 0 0 1.0000 8.0000 5.0000 10.0000 1.8256 10.0000 1.2883 4.6679 10.0000 Columns 9 through 13 1.0000 3.0000 -2.0000 1.0000 1.0000 2.0000 2.9111 10.0000 8.0000 0 1.0000 2.0000 3.0000 5.5663 10.0000 plot3( u2(1,:), u2(2,:), u2(3,:), 'o' ) 63 Finite-element Methods in Two Dimensions Example 2 Here we see the boundary points and the actual solution (light blue) and the approximations (in red) 64 Finite-element Methods in Two Dimensions Summary This topic discusses an introduction to finite-element methods in two dimensions – We generalized the test functions in one dimension – Created a tessellation of the region R – Defined planar test functions • Found the gradient of the planes making the test functions – Approximated the solution with piecewise planar sections – Created a linear equation • This is done for each interior point – The resulting system of linear equations may be solved – Two examples… 65 Finite-element Methods in Two Dimensions What’s Next? The next step is to go to three dimensions – Divide the region into tetrahedral regions – 3-dimensional tessellations Robert Webb's Great Stella software http://www.software3d.com/Stella.html Tessellation User: Tom Ruen 66 Finite-element Methods in Two Dimensions References [1] Glyn James, Advanced Modern Engineering Mathematics, 4th Ed., Prentice Hall, 2011, §§9.2-3. 67 Finite-element Methods in Two Dimensions Usage Notes • • These slides are made publicly available on the web for anyone to use If you choose to use them, or a part thereof, for a course at another institution, I ask only three things: – that you inform me that you are using the slides, – that you acknowledge my work, and – that you alert me of any mistakes which I made or changes which you make, and allow me the option of incorporating such changes (with an acknowledgment) in my set of slides Sincerely, Douglas Wilhelm Harder, MMath dwharder@alumni.uwaterloo.ca 68