Finite Element & Finite Volume Methods

matrix assembly

Building a stone wall, you don't carve one monolithic slab — you shape many small bricks and lay them so they overlap and lock at the seams. The finite element method builds its giant global matrix the same way: it never computes the whole stiffness matrix in one go. Instead it computes a tiny matrix for each element separately, then ADDS each element's contributions into the right places of the big matrix. That accumulation is assembly.

Here is the loop in plain steps. For each element you compute a small element matrix — for a triangle with three nodes, a 3-by-3 stiffness matrix — by integrating the shape functions over just that one element (usually with numerical quadrature). Each of the element's local node numbers maps to a global node number; this 'connectivity' tells you where each of the 9 entries belongs in the global matrix. You then ADD (not overwrite) the local entries into those global positions: K_global[I][J] += k_element[i][j]. Because a node shared by several elements receives a contribution from each, the additions correctly accumulate the full integral, piece by piece. The load vector F is assembled the same way.

Assembly is why finite elements scale and why they are so flexible. It is purely local — every element is processed independently, knowing nothing of the others except where its nodes sit globally — which makes assembly naturally parallel and the resulting matrix automatically sparse (only co-element nodes ever interact). The honest subtlety is bookkeeping: the connectivity table, the local-to-global node map, and consistent orientation must be exactly right, because a single mis-mapped index silently corrupts the system. In practice one stores the sparse matrix in a compressed format and uses a scatter-add into preallocated structure to keep assembly fast.

A 1D bar of 2 elements has nodes 1-2-3. Element A (nodes 1,2) contributes its 2x2 matrix into rows/cols {1,2}; element B (nodes 2,3) into {2,3}. Node 2 is shared, so K[2][2] receives a contribution from BOTH elements, summing to the central '2' in (1/h) tridiag(-1,2,-1). That overlap at shared nodes is the heart of assembly.

Shared nodes accumulate contributions from every element that touches them.

Assembly ADDS into shared-node entries; it never overwrites. A wrong local-to-global index map produces a plausible-looking but silently incorrect system — connectivity bookkeeping is the most common source of FEM bugs.

Also called
element assemblyglobal assemblyscatter-add組合組裝