FEM foundation A: DofKind/DofLayout, shape-function library with Hessians, solvers on DofLayout (#35)

* fem: DofKind/DofLayout and the shape-function library with second derivatives

DofKind (Ux..Rz, T, P), DofSet, ElementDofs (per-node kinds and condensed
internal unknowns) and DofLayout: mesh nodes by label, kinds in order, then
reference points. basis::Basis covers line2/3, tri3/6, quad4/8/9, tet4/10,
wedge6/15, pyramid5, hex8/20/27 with values, first and second natural
derivatives (second-order forward AD, fem::jet) and the physical gradients,
Hessians and Laplacians with the map's second-derivative term. Tested for
Kronecker delta, partition of unity, polynomial completeness, finite
differences, affine and curved maps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NmAttHhUZTokgPeEJia8sm

* solver, explicit: number degrees of freedom with DofLayout

The implicit mesh numbers its unknowns with a DofLayout (mesh nodes by label
with the kinds their elements declare, then six per coupled reference point);
assembly adds element blocks by (node, DofKind), so a node may carry any mix
of kinds; couplings, boundary conditions, loads and results address
(node, kind). The explicit system keeps node-major translations, now checked
against its layout (NODE_DOFS). Solid formulations declare their ElementDofs.

Every static, frequency and explicit result of the solver, explicit, engine
and io test suites hashes bit-identically before and after (957 results).
docs/fem.md describes the DOF model and the shape-function conventions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NmAttHhUZTokgPeEJia8sm

* fem, solver: simplify after review

Block offsets resolved once per node pair; add_scalar/add_diagonal take
the kinds like add_block; one checked Jacobian inverse (dense::positive_inverse)
for the solid kernels and the basis library; static natural-node tables; the
Hessian path inverts the Jacobian once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NmAttHhUZTokgPeEJia8sm

---------

Co-authored-by: jyviko <2501648+jyviko@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-10-13 19:54:32 -04:00
1 parent c686cbd531
commit bd55abea7f
16 files changed
+1805 -192

No files matched your search

+5 -26
View File
@@ -21,8 +21,8 @@
use modelcraft_model::ElementKind;
/// Most nodes an element in this crate has (hex20).
pub const MAX_NODES: usize = 20;
/// Most nodes an element family has (hex27, [`crate::basis::Basis::Hex27`]).
pub const MAX_NODES: usize = 27;
/// Shape function values and natural derivatives at one point.
#[derive(Debug, Clone, Copy)]
@@ -157,33 +157,12 @@ pub fn hex20(xi: [f64; 3]) -> Shape {
mod tests {
use super::*;
fn nodes_of(kind: &str) -> Vec<[f64; 3]> {
match kind {
"tet4" => TET_CORNERS.to_vec(),
"tet10" => {
let mut v = TET_CORNERS.to_vec();
for e in ElementKind::Tet10.edges() {
v.push([0, 1, 2].map(|k| (TET_CORNERS[e[0]][k] + TET_CORNERS[e[1]][k]) / 2.0));
}
v
}
"hex8" => HEX_CORNERS.to_vec(),
"wedge6" => WEDGE_CORNERS.to_vec(),
_ => {
let mut v = HEX_CORNERS.to_vec();
for e in ElementKind::Hex20.edges() {
v.push([0, 1, 2].map(|k| (HEX_CORNERS[e[0]][k] + HEX_CORNERS[e[1]][k]) / 2.0));
}
v
}
}
}
#[test]
fn kronecker_delta_partition_of_unity_and_derivatives() {
let kinds: [(&str, fn([f64; 3]) -> Shape); 5] = [("tet4", tet4), ("tet10", tet10), ("wedge6", wedge6), ("hex8", hex8), ("hex20", hex20)];
for (name, f) in kinds {
let nodes = nodes_of(name);
let nodes = crate::Basis::ALL.iter().find(|b| b.name() == name).map(|b| b.nodes()).unwrap_or_default();
assert!(!nodes.is_empty());
for (i, p) in nodes.iter().enumerate() {
let s = f(*p);
for (j, v) in s.values().iter().enumerate() {
@@ -197,7 +176,7 @@ mod tests {
assert!((s.values().iter().sum::<f64>() - 1.0).abs() < 1e-14);
for k in 0..3 {
assert!(s.derivatives().iter().map(|d| d[k]).sum::<f64>().abs() < 1e-13);
let x: f64 = s.values().iter().zip(&nodes).map(|(n, x)| n * x[k]).sum();
let x: f64 = s.values().iter().zip(nodes).map(|(n, x)| n * x[k]).sum();
assert!((x - p[k]).abs() < 1e-14, "{name}");
let h = 1e-6;
let (mut a, mut b) = (p, p);