|
| 1 | +/** |
| 2 | + * ════════════════════════════════════════════════════════════════ |
| 3 | + * FEAScript Core Library |
| 4 | + * Lightweight Finite Element Simulation in JavaScript |
| 5 | + * Version: 0.3.0 (RC) | https://feascript.com |
| 6 | + * MIT License © 2023–2026 FEAScript |
| 7 | + * ════════════════════════════════════════════════════════════════ |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * Unit tests for triangular element basis functions and quadrature |
| 12 | + * |
| 13 | + * Covers (linear and quadratic triangles): |
| 14 | + * - Partition of unity and zero-sum derivatives |
| 15 | + * - Kronecker delta property at the nodes |
| 16 | + * - Derivatives against central finite differences |
| 17 | + * - Quadrature weights sum and exact integration of monomials |
| 18 | + * |
| 19 | + * Run: node tests/unit/triangleElements.test.js (or npm test) |
| 20 | + */ |
| 21 | + |
| 22 | +import { BasisFunctions } from "../../src/mesh/basisFunctions.js"; |
| 23 | +import { NumericalIntegration } from "../../src/methods/numericalIntegration.js"; |
| 24 | +import { basicLog, errorLog } from "../../src/utilities/logging.js"; |
| 25 | + |
| 26 | +basicLog(""); |
| 27 | +basicLog("================================"); |
| 28 | +basicLog("Unit tests: triangular elements"); |
| 29 | + |
| 30 | +let passed = 0; |
| 31 | +let failed = 0; |
| 32 | + |
| 33 | +function assert(condition, message) { |
| 34 | + if (!condition) { |
| 35 | + errorLog(`FAIL: ${message}`); |
| 36 | + failed++; |
| 37 | + } else { |
| 38 | + basicLog(`PASS: ${message}`); |
| 39 | + passed++; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +const TOL = 1e-12; |
| 44 | +const close = (a, b, tol = TOL) => Math.abs(a - b) < tol; |
| 45 | + |
| 46 | +// Reference-triangle node coordinates (Gmsh ordering) |
| 47 | +const nodes = { |
| 48 | + linear: [ |
| 49 | + [0, 0], |
| 50 | + [1, 0], |
| 51 | + [0, 1], |
| 52 | + ], |
| 53 | + quadratic: [ |
| 54 | + [0, 0], |
| 55 | + [1, 0], |
| 56 | + [0, 1], |
| 57 | + [0.5, 0], |
| 58 | + [0.5, 0.5], |
| 59 | + [0, 0.5], |
| 60 | + ], |
| 61 | +}; |
| 62 | + |
| 63 | +// ∫∫ ksi^p eta^q over the reference triangle = p! q! / (p + q + 2)! |
| 64 | +const factorial = (n) => (n <= 1 ? 1 : n * factorial(n - 1)); |
| 65 | +const exactMonomialIntegral = (p, q) => (factorial(p) * factorial(q)) / factorial(p + q + 2); |
| 66 | + |
| 67 | +for (const order of ["linear", "quadratic"]) { |
| 68 | + const basis = new BasisFunctions({ meshDimension: "2D", elementOrder: order, elementShape: "triangle" }); |
| 69 | + const integration = new NumericalIntegration({ |
| 70 | + meshDimension: "2D", |
| 71 | + elementOrder: order, |
| 72 | + elementShape: "triangle", |
| 73 | + }); |
| 74 | + const numNodes = nodes[order].length; |
| 75 | + |
| 76 | + // Partition of unity and zero-sum derivatives at an interior point |
| 77 | + const interior = basis.getBasisFunctions(0.2, 0.3); |
| 78 | + const sum = (arr) => arr.reduce((s, v) => s + v, 0); |
| 79 | + assert(interior.basisFunction.length === numNodes, `${order}: returns ${numNodes} basis functions`); |
| 80 | + assert(close(sum(interior.basisFunction), 1), `${order}: basis functions sum to 1`); |
| 81 | + assert(close(sum(interior.basisFunctionDerivKsi), 0), `${order}: ksi derivatives sum to 0`); |
| 82 | + assert(close(sum(interior.basisFunctionDerivEta), 0), `${order}: eta derivatives sum to 0`); |
| 83 | + |
| 84 | + // Kronecker delta at the nodes |
| 85 | + let deltaOk = true; |
| 86 | + nodes[order].forEach(([ksi, eta], nodeIndex) => { |
| 87 | + const { basisFunction } = basis.getBasisFunctions(ksi, eta); |
| 88 | + basisFunction.forEach((value, i) => { |
| 89 | + if (!close(value, i === nodeIndex ? 1 : 0)) deltaOk = false; |
| 90 | + }); |
| 91 | + }); |
| 92 | + assert(deltaOk, `${order}: Kronecker delta property at the nodes`); |
| 93 | + |
| 94 | + // Derivatives against central finite differences |
| 95 | + const h = 1e-6; |
| 96 | + const ksi0 = 0.25; |
| 97 | + const eta0 = 0.35; |
| 98 | + const analytic = basis.getBasisFunctions(ksi0, eta0); |
| 99 | + const dKsiPlus = basis.getBasisFunctions(ksi0 + h, eta0).basisFunction; |
| 100 | + const dKsiMinus = basis.getBasisFunctions(ksi0 - h, eta0).basisFunction; |
| 101 | + const dEtaPlus = basis.getBasisFunctions(ksi0, eta0 + h).basisFunction; |
| 102 | + const dEtaMinus = basis.getBasisFunctions(ksi0, eta0 - h).basisFunction; |
| 103 | + let derivOk = true; |
| 104 | + for (let i = 0; i < numNodes; i++) { |
| 105 | + const fdKsi = (dKsiPlus[i] - dKsiMinus[i]) / (2 * h); |
| 106 | + const fdEta = (dEtaPlus[i] - dEtaMinus[i]) / (2 * h); |
| 107 | + if (!close(analytic.basisFunctionDerivKsi[i], fdKsi, 1e-6)) derivOk = false; |
| 108 | + if (!close(analytic.basisFunctionDerivEta[i], fdEta, 1e-6)) derivOk = false; |
| 109 | + } |
| 110 | + assert(derivOk, `${order}: derivatives match finite differences`); |
| 111 | + |
| 112 | + // Quadrature |
| 113 | + const { gaussPoints, gaussWeights } = integration.getGaussPointsAndWeights(); |
| 114 | + assert(gaussPoints.length === gaussWeights.length, `${order}: one weight per quadrature point`); |
| 115 | + assert(close(sum(gaussWeights), 0.5), `${order}: quadrature weights sum to 1/2`); |
| 116 | + |
| 117 | + const maxDegree = order === "linear" ? 2 : 4; |
| 118 | + let exactOk = true; |
| 119 | + for (let p = 0; p <= maxDegree; p++) { |
| 120 | + for (let q = 0; p + q <= maxDegree; q++) { |
| 121 | + const numeric = gaussPoints.reduce((s, [ksi, eta], i) => s + gaussWeights[i] * ksi ** p * eta ** q, 0); |
| 122 | + if (!close(numeric, exactMonomialIntegral(p, q))) exactOk = false; |
| 123 | + } |
| 124 | + } |
| 125 | + assert(exactOk, `${order}: quadrature exact for monomials up to degree ${maxDegree}`); |
| 126 | +} |
| 127 | + |
| 128 | +basicLog(`Triangular element tests: ${passed} passed, ${failed} failed`); |
| 129 | +if (failed > 0) process.exit(1); |
0 commit comments