MATH 676 – Finite element methods in scientific computing Wolfgang Bangerth, Texas A&M University http://www.dealii.org/ Wolfgang Bangerth Lecture 21.6: Boundary conditions Part 3a: Homogenous Dirichlet boundary conditions http://www.dealii.org/ Wolfgang Bangerth Zero Dirichlet conditions Consider this simple example: ● Solve the Laplace equation with zero boundary values: −Δ u = f u = 0 ● in Ω on Γ=∂ Ω Assume we use the following mesh: 3 2 1 0 9 4 5 6 7 http://www.dealii.org/ {1,5,6}: “real” degrees of freedom {0,2…4,7…9}: Constrained to zero 8 Wolfgang Bangerth Zero Dirichlet conditions There are three approaches: ● ● ● Consider only “free” degrees of freedom Consider all degrees of freedom – do local assembly without boundary conditions – copy to global system – eliminate boundary DoFs Consider all degrees of freedom – do local assembly without boundary conditions – eliminate boundary DoFs – copy to global system Criteria: All approaches are correct; which one we like best is determined by considerations of algorithm design! http://www.dealii.org/ Wolfgang Bangerth Zero Dirichlet conditions Approach 1: ● ● Don't enumerate constrained degrees of freedom Only enumerate the 3 unconstrained ones: 1→0, 5→1, 6→2 3 2 1 0 9 4 5 6 7 http://www.dealii.org/ 0 X X 8 X X X 1 2 X X Wolfgang Bangerth Zero Dirichlet conditions Approach 1: ● Then assemble a 3x3 linear system as usual: (φi , f ) K ∑ j=0 [⏟ ∑K =0 (∇ φi , ∇ φ j)K ] U j=∑ K =0 ⏟ 2 11 11 A ij 3 2 1 0 9 6 7 http://www.dealii.org/ Fi 0 X X 8 X X 4 5 ∀ i=0 ...2 X 1 2 X X Wolfgang Bangerth Zero Dirichlet conditions Advantages of approach 1: ● ● All degrees of freedom are really “free” (i.e., determined by a linear system) Linear system is much smaller (3x3 vs 10x10) 3 2 1 0 9 5 6 7 http://www.dealii.org/ 0 X X 8 X X 4 X 1 2 X X Wolfgang Bangerth Zero Dirichlet conditions Advantages of approach 1: ● ● All degrees of freedom are really “free” (i.e., determined by a linear system) Linear system is much smaller (3x3 vs 10x10) But: This size reduction doesn't matter on finer meshes! 1 vs 9 DoFs http://www.dealii.org/ 9 vs 25 DoFs 49 vs 81 DoFs Wolfgang Bangerth Zero Dirichlet conditions Disdvantages of approach 1: ● The FEM is at its best if you can do the same on every cell: – when enumerating degrees of freedom – when assembling – when evaluating the solution at individual points –… X X 0 X X 1 2 X http://www.dealii.org/ X X Wolfgang Bangerth Zero Dirichlet conditions Disdvantages of approach 1: ● If Dirichlet conditions only on parts of the boundary: Some boundary nodes would have to be treated differently than others. 6 4 0 X X 1 2 X http://www.dealii.org/ 5 X Wolfgang Bangerth Zero Dirichlet conditions Disdvantages of approach 1: ● What to do in cases of vector-valued problems and conditions of the sort u.n=0? ● Example: Fluid flow with velocity components (ux,uy) ● Boundary condition here is nxux=-nyuy 2 3 0 1 8 9 10 11 12 13 14 15 18 19 http://www.dealii.org/ 2 X 6 7 4 5 X 1 8 9 10 11 X 13 16 17 X 7 4 X 14 15 18 X 16 X Wolfgang Bangerth Zero Dirichlet conditions Approach 2: ● Enumerate all degrees of freedom regardless of boundary conditions Variation 2a: ● ● ● ● Compute local contributions regardless of boundary DoFs Assemble into one big linear system Zero out rows and columns for boundary DoFs {0,2,3,4,7,8,9} Fix up zero rows 3 2 1 0 9 5 6 7 http://www.dealii.org/ 4 8 Wolfgang Bangerth Zero Dirichlet conditions Example for approach 2a: ● ● Compute local contributions regardless of boundary DoFs Assemble into one big linear system (forget about sparsity for a moment) ( a00 a10 a20 a30 a40 a50 a60 a70 a80 a90 a01 a11 a21 a31 a41 a51 a61 a71 a81 a91 a02 a12 a22 a32 a42 a52 a62 a72 a82 a92 a03 a13 a23 a33 a43 a53 a63 a73 a83 a93 a04 a14 a24 a34 a44 a54 a64 a74 a84 a94 http://www.dealii.org/ a05 a15 a25 a35 a 45 a55 a65 a75 a85 a 95 a 06 a16 a 26 a 36 a 46 a56 a 66 a76 a 86 a 96 a07 a17 a27 a37 a 47 a57 a67 a77 a87 a97 a08 a18 a28 a38 a48 a58 a68 a78 a88 a98 a09 a19 a29 a39 a49 a59 a69 a79 a89 a99 u0 f0 u1 f1 u2 f2 u3 f3 u4 f = 4 u5 f5 u6 f6 u7 f7 u8 f8 u9 f9 )( ) ( ) 3 2 1 0 9 4 5 6 7 8 Wolfgang Bangerth Zero Dirichlet conditions Example for approach 2a: ● Compute local contributions regardless of boundary DoFs ● Assemble into one big linear system ● Zero out rows and columns for DoFs {0,2,3,4,7,8,9} ( a00 a10 a20 a30 a40 a50 a60 a70 a80 a90 a01 a11 a21 a31 a41 a51 a61 a71 a81 a91 a02 a12 a22 a32 a42 a52 a62 a72 a82 a92 a03 a13 a23 a33 a43 a53 a63 a73 a83 a93 a04 a14 a24 a34 a44 a54 a64 a74 a84 a94 http://www.dealii.org/ a05 a15 a25 a35 a 45 a55 a65 a75 a85 a 95 a 06 a16 a 26 a 36 a 46 a56 a 66 a76 a 86 a 96 a07 a17 a27 a37 a 47 a57 a67 a77 a87 a97 a08 a18 a28 a38 a48 a58 a68 a78 a88 a98 a09 a19 a29 a39 a49 a59 a69 a79 a89 a99 u0 f0 u1 f1 u2 f2 u3 f3 u4 f = 4 u5 f5 u6 f6 u7 f7 u8 f8 u9 f9 )( ) ( ) 3 2 1 0 9 4 5 6 7 8 Wolfgang Bangerth Zero Dirichlet conditions Example for approach 2a: ● Compute local contributions regardless of boundary DoFs ● Assemble into one big linear system ● Zero out rows and columns for DoFs {0,2,3,4,7,8,9} The resulting system has a 3x3 sub-block for “free” DoFs ( 0 0 0 a11 0 0 0 0 0 0 0 a51 0 a61 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 a15 a16 0 0 0 0 0 0 0 0 0 0 a55 a56 0 a65 a66 0 0 0 0 0 0 0 0 0 http://www.dealii.org/ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 u0 0 u1 f1 u2 0 u3 0 u4 0 = f5 u5 f6 u6 0 u7 0 u8 0 u9 )( ) ( ) 3 2 1 0 9 4 5 6 7 8 Wolfgang Bangerth Zero Dirichlet conditions Example for approach 2a: ● Compute local contributions regardless of boundary DoFs ● Assemble into one big linear system ● Zero out rows and columns for DoFs {0,2,3,4,7,8,9} ● Fix under-determination: ( 1 0 0 a11 0 0 0 0 0 0 0 a51 0 a61 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 a15 a16 0 0 0 0 0 0 1 0 0 0 a55 a56 0 a65 a66 0 0 0 0 0 0 0 0 0 http://www.dealii.org/ 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 u0 0 u1 f1 u2 0 u3 0 u4 0 = f5 u5 f6 u6 0 u7 0 u8 0 u9 )( ) ( ) 3 2 1 0 9 4 5 6 7 8 Wolfgang Bangerth Zero Dirichlet conditions Result for approach 2a: We get a linear system of the form ( 1 a11 a15 a16 1 1 1 a51 a61 a55 a56 a65 a66 u0 0 u1 f1 u2 0 u3 0 u4 0 = f5 u5 f6 u6 1 0 u7 1 0 u8 1 u 0 9 )( ) ( ) The solution satisfies U 0 =U 2=U 3 =U 4 =U 7 =U 8 =U 9=0 3 2 1 0 9 5 6 7 http://www.dealii.org/ 4 8 Wolfgang Bangerth Zero Dirichlet conditions Result for approach 2a: We get a linear system of the form ( 1 a11 a15 a 16 1 1 1 a 51 a 61 a 55 a 56 a 65 a 66 u0 0 u1 f1 u2 0 u3 0 u4 0 = f5 u5 f6 u6 1 0 u7 1 0 u8 1 u 0 9 The solution satisfies ( )( ) ( ) a 11 a15 a16 u1 f1 a51 a55 a56 u 5 = f 5 a61 a65 a66 u 6 f6 http://www.dealii.org/ )( ) ( ) 3 2 1 0 9 4 5 6 7 8 Wolfgang Bangerth Zero Dirichlet conditions Summary approach 2a: ● Compute local contributions regardless of boundary DoFs ● Assemble into one big linear system ● Zero out rows and columns for boundary DoFs ● Fix up zero rows 2 1 This results in a linear system that: ● Has the desired solution 9 6 ● Is invertible 7 ● Has a few more zeros in it ● Is symmetric if the original matrix was symmetric ● Is SPD if the original matrix was SPD 0 http://www.dealii.org/ 3 4 5 8 Wolfgang Bangerth Zero Dirichlet conditions Implementation approach 2a (step-4): void Step4::assemble_system { …; for (cell=...) { ...assemble cell_matrix, cell_rhs...; ...add cell_matrix, cell_rhs to system_matrix, system_rhs...; } std::map<types::global_dof_index,double> boundary_values; VectorTools::interpolate_boundary_values (dof_handler, 0, ZeroFunction<dim>(), boundary_values); MatrixTools::apply_boundary_values (boundary_values, system_matrix, solution, system_rhs); } http://www.dealii.org/ Wolfgang Bangerth Zero Dirichlet conditions Summary approach 2a: ● Compute local contributions regardless of boundary DoFs ● Assemble into one big linear system ● Zero out rows and columns for boundary DoFs ● Fix up zero rows 2 3 4 1 5 Advantages: ● Can do the exact same thing on 9 6 every cell during assembly/postprocessing 7 ● Relegate handling b.c. to a separate 8 part of the code, independent of assembly ● Can use same solver regardless of boundary conditions 0 http://www.dealii.org/ Wolfgang Bangerth Zero Dirichlet conditions Summary approach 2a: ● Compute local contributions regardless of boundary DoFs ● Assemble into one big linear system ● Zero out rows and columns for boundary DoFs ● Fix up zero rows Disadvantages: ● We have a few more rows and columns ● We have to modify the linear system after global assembly http://www.dealii.org/ 3 2 1 0 9 4 5 6 7 8 Wolfgang Bangerth Zero Dirichlet conditions Approach 2: ● Enumerate all degrees of freedom regardless of boundary conditions Variation 2b: ● ● ● ● Compute local contributions regardless of boundary DoFs Zero out rows and columns for boundary DoFs {0,2,3,4,7,8,9} Fix up zero rows Assemble into one big linear system 3 2 1 0 9 5 6 7 http://www.dealii.org/ 4 8 Wolfgang Bangerth Zero Dirichlet conditions Example for approach 2b: Recall that the linear system AU = F is computed from contributions of all cells: A = F = 10 ∑K =0 10 AK ∑K =0 F 3 2 K 0 0 7 8 1 4 10 5 1 6 2 9 9 6 5 3 7 http://www.dealii.org/ 4 8 Wolfgang Bangerth Zero Dirichlet conditions Example for approach 2b: Recall that the linear system AU = F is computed from contributions of all cells. For example: ( 1 1 1 1 1 a00 a 01 0 0 0 0 a06 0 0 0 a10 a11 0 0 0 0 1 0 A= 0 0 0 1 1 a60 a 61 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 a16 0 0 0 0 0 0 0 0 1 0 a66 0 0 0 0 0 0 http://www.dealii.org/ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 ) () 0 0 0 0 , 0 0 0 0 0 f0 1 f1 0 0 1 F = 0 0 1 f6 0 0 0 3 2 0 0 7 8 1 4 10 5 1 6 2 9 9 6 5 3 7 4 8 Wolfgang Bangerth Zero Dirichlet conditions Example for approach 2b: Recall that the linear system AU = F is computed from contributions of all cells. In reality, of course, we only store the dense 3x3 sub-block: ( 1 1 1 0 1 ) () a00 a01 a06 1, loc A = a110 a111 a116 , 1 1 1 a60 a61 a66 f0 1, loc F = f 11 1 f6 3 2 0 7 8 1 4 10 5 1 6 2 9 9 6 5 3 7 http://www.dealii.org/ 4 8 Wolfgang Bangerth Zero Dirichlet conditions Example for approach 2b: Recall that the linear system AU = F is computed from contributions of all cells. Zero out rows and columns DoFs {0,2,3,4,7,8,9}: 3 2 ( 1 1 1 0 1 ) () a00 a01 a06 1, loc A = a110 a111 a116 , 1 1 1 a60 a61 a66 f0 1, loc F = f 11 1 f6 0 7 8 1 4 10 5 1 6 2 9 9 6 5 3 7 http://www.dealii.org/ 4 8 Wolfgang Bangerth Zero Dirichlet conditions Example for approach 2b: Recall that the linear system AU = F is computed from contributions of all cells. Zero out rows and columns DoFs {0,2,3,4,7,8,9}: 3 2 0 0 0 1, loc 1 A = 0 a11 a116 , 1 1 0 a61 a66 ( ) 0 1, loc F = f 11 1 f6 () 0 0 7 8 1 4 10 5 1 6 2 9 9 6 5 3 7 http://www.dealii.org/ 4 8 Wolfgang Bangerth Zero Dirichlet conditions Example for approach 2b: Recall that the linear system AU = F is computed from contributions of all cells. Fix up under-determinedness: 3 2 1 0 0 1, loc 1 A = 0 a11 a116 , 1 1 0 a61 a66 ( ) 0 1, loc F = f 11 1 f6 () 0 0 7 8 1 4 10 5 1 6 2 9 9 6 5 3 7 http://www.dealii.org/ 4 8 Wolfgang Bangerth Zero Dirichlet conditions Example for approach 2b: Recall that the linear system AU = F is computed from contributions of all cells. Then add to global linear system: A := A +expand ( A 1, loc 0 F := F +expand ( F 1, loc ) 3 2 ) 0 7 8 1 4 10 5 1 6 2 9 9 6 5 3 7 http://www.dealii.org/ 4 8 Wolfgang Bangerth Zero Dirichlet conditions Summary approach 2b: ● Compute local contributions regardless of boundary DoFs ● Zero out rows and columns for boundary DoFs ● Fix up zero rows ● Assemble into one big linear system Actual implementation: ● The last three operations are all done at the same time ● The local contributions are not modified http://www.dealii.org/ 3 2 1 0 9 4 5 6 7 8 Wolfgang Bangerth Zero Dirichlet conditions Implementation approach 2b (step-6): void Step6::assemble_system { ConstraintMatrix constraints; VectorTools::interpolate_boundary_values (dof_handler, 0, ZeroFunction<dim>(), constraints); …; for (cell=...) { ...assemble cell_matrix, cell_rhs...; cell­>get_dof_indices (local_dof_indices); constraints.distribute_local_to_global (cell_matrix, cell_rhs, local_dof_indices, system_matrix, system_rhs); } } http://www.dealii.org/ Wolfgang Bangerth Zero Dirichlet conditions Summary approach 2b: ● Compute local contributions regardless of boundary DoFs ● Zero out rows and columns for boundary DoFs ● Fix up zero rows ● Assemble into one big linear system 2 3 4 1 5 Advantages: ● Can do the exact same thing on 9 6 every cell during assembly/postprocessing 7 ● Relegate handling b.c. to a separate 8 part of the code, independent of assembly ● Can use same solver regardless of boundary conditions 0 http://www.dealii.org/ Wolfgang Bangerth Zero Dirichlet conditions Summary approach 2b: ● Compute local contributions regardless of boundary DoFs ● Zero out rows and columns for boundary DoFs ● Fix up zero rows ● Assemble into one big linear system Disdvantages: ● We have a few more rows and columns ● We have to modify the linear system after local integration or during copy-local-to-global (both options are easy and cheap) http://www.dealii.org/ 3 2 1 0 9 4 5 6 7 8 Wolfgang Bangerth Summary We are driven by algorithm design considerations: ● We want to do the exact same thing on every cell ● We want to isolate handling of boundary values to as few code pieces as possible ● Dealing with small, dense, local contributions is simpler than with global linear systems 3 2 1 0 9 5 6 Consequently: 7 ● Enumerate all degrees of freedom ● Integrate locally regardless of boundary conditions ● Deal with boundary values during assembly or after http://www.dealii.org/ 4 8 Wolfgang Bangerth A final implementation detail As a postscript: Consider a linear system of the form ( 1 a11 a15 a 16 1 1 1 a 51 a 61 a 55 a 56 a 65 a 66 This matrix is ill-conditioned if u0 0 u1 f1 u2 0 u3 0 u4 0 = f5 u5 f6 u6 1 0 u7 1 0 u8 1 u 0 9 )( ) ( ) |aij|≫1 or |aij|≪1 Consequence: ● We should not place 1.0 on the diagonal ● Algorithm instead use values “similar to other entries” http://www.dealii.org/ Wolfgang Bangerth MATH 676 – Finite element methods in scientific computing Wolfgang Bangerth, Texas A&M University http://www.dealii.org/ Wolfgang Bangerth