assembly
Global assembly helpers
Minimal manual-assembly helpers for teaching examples.
These helpers are intentionally small and explicit. They support classroom examples without trying to hide topology or boundary-condition reasoning.
assemble_dense_matrix(global_matrix, local_matrix, connectivity)
Add a local element matrix into a dense global matrix in place.
assemble_dense_vector(global_vector, local_vector, connectivity)
Add a local element vector into a dense global vector in place.
apply_dirichlet_by_reduction(global_matrix, global_vector, constrained_dofs, prescribed_values)
Return the reduced linear system after enforcing Dirichlet values. Parameters ———- global_matrix, global_vector Full linear system K u = F. constrained_dofs Global dof indices where the solution is prescribed. prescribed_values Values at the constrained dofs. Defaults to homogeneous conditions.
expand_reduced_solution(free_solution, ndofs, free_dofs, constrained_dofs, prescribed_values)
Expand a reduced solution back into the full nodal vector.
apply_dirichlet_by_row_substitution(global_matrix, global_vector, constrained_dofs, prescribed_values)
Enforce Dirichlet BCs by modifying the global system in-place (row substitution). This is the textbook approach that most students learn first: 1. For each constrained DOF i with prescribed value g_i: - Zero out row i of K - Set K[i, i] = 1 - Subtract K[:, i] * g_i from the RHS for all other rows - Zero out column i of K - Set F[i] = g_i After modification, the system K u = F can be solved directly and u[i] = g_i for every constrained DOF. Parameters ———- global_matrix, global_vector The assembled system K u = F. Modified in-place and also returned. constrained_dofs Indices of DOFs with prescribed Dirichlet values. prescribed_values The prescribed values. Defaults to zeros (homogeneous). Returns ——- K_mod, F_mod : np.ndarray The modified system (same arrays, returned for convenience).
apply_dirichlet_by_lifting(global_matrix, global_vector, constrained_dofs, prescribed_values)
Enforce Dirichlet BCs using the lifting approach. The lifting approach decomposes the solution as u = u_0 + u_D, where: - u_D is a “lift” vector that satisfies the Dirichlet conditions (u_D[i] = g_i for constrained DOFs, zero elsewhere) - u_0 ∈ V_0 is the homogeneous part (u_0[i] = 0 for constrained DOFs) Substituting into a(u, v) = F(v): a(u_0 + u_D, v) = F(v) a(u_0, v) = F(v) - a(u_D, v) In matrix form this becomes: K_ff u_0f = F_f - K_fc g_c This is mathematically equivalent to the reduction approach but shows more clearly how the Dirichlet data enters as a correction to the RHS. The approach follows the treatment in Dokken’s FEniCS tutorial: the lift u_D lives in the full space, the modified RHS is F - K u_D, and the constrained rows/columns are then eliminated. Parameters ———- global_matrix, global_vector Assembled system K u = F. Not modified. constrained_dofs Indices of Dirichlet DOFs. prescribed_values Prescribed values at the Dirichlet DOFs. Defaults to zeros. Returns ——- K_ff : np.ndarray Reduced stiffness (free-free block). F_modified : np.ndarray Modified load vector = F_f - K_fc g_c. free_dofs : list[int] u_lift : np.ndarray The full lift vector (zero except at constrained DOFs).