Skip to content

Commit 9cdbfbf

Browse files
committed
Add triangular element basis functions and triangle quadrature rules. Add unit tests
1 parent 63b60bf commit 9cdbfbf

3 files changed

Lines changed: 205 additions & 6 deletions

File tree

‎src/mesh/basisFunctions.js‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ export class BasisFunctions {
1818
* Constructor to initialize the BasisFunctions class
1919
* @param {string} meshDimension - The dimension of the mesh
2020
* @param {string} elementOrder - The order of elements
21+
* @param {string} [elementShape='quad'] - The 2D element shape, either 'quad' or 'triangle'
2122
*/
22-
constructor({ meshDimension, elementOrder }) {
23+
constructor({ meshDimension, elementOrder, elementShape = "quad" }) {
2324
this.meshDimension = meshDimension;
2425
this.elementOrder = elementOrder;
26+
this.elementShape = elementShape;
2527
}
2628

2729
/**
@@ -101,7 +103,47 @@ export class BasisFunctions {
101103
return;
102104
}
103105

104-
if (this.elementOrder === "linear") {
106+
if (this.elementShape === "triangle") {
107+
// Reference triangle with vertices (0,0), (1,0), (0,1); L1 = 1 - ksi - eta, L2 = ksi, L3 = eta
108+
const l1 = 1 - ksi - eta;
109+
110+
if (this.elementOrder === "linear") {
111+
// Node order: vertices 0, 1, 2 (counterclockwise)
112+
basisFunction[0] = l1;
113+
basisFunction[1] = ksi;
114+
basisFunction[2] = eta;
115+
116+
basisFunctionDerivKsi[0] = -1;
117+
basisFunctionDerivKsi[1] = 1;
118+
basisFunctionDerivKsi[2] = 0;
119+
120+
basisFunctionDerivEta[0] = -1;
121+
basisFunctionDerivEta[1] = 0;
122+
basisFunctionDerivEta[2] = 1;
123+
} else if (this.elementOrder === "quadratic") {
124+
// Node order (same as Gmsh): vertices 0, 1, 2, then midside nodes 3 (0-1), 4 (1-2), 5 (2-0)
125+
basisFunction[0] = l1 * (2 * l1 - 1);
126+
basisFunction[1] = ksi * (2 * ksi - 1);
127+
basisFunction[2] = eta * (2 * eta - 1);
128+
basisFunction[3] = 4 * l1 * ksi;
129+
basisFunction[4] = 4 * ksi * eta;
130+
basisFunction[5] = 4 * eta * l1;
131+
132+
basisFunctionDerivKsi[0] = 1 - 4 * l1;
133+
basisFunctionDerivKsi[1] = 4 * ksi - 1;
134+
basisFunctionDerivKsi[2] = 0;
135+
basisFunctionDerivKsi[3] = 4 * (l1 - ksi);
136+
basisFunctionDerivKsi[4] = 4 * eta;
137+
basisFunctionDerivKsi[5] = -4 * eta;
138+
139+
basisFunctionDerivEta[0] = 1 - 4 * l1;
140+
basisFunctionDerivEta[1] = 0;
141+
basisFunctionDerivEta[2] = 4 * eta - 1;
142+
basisFunctionDerivEta[3] = -4 * ksi;
143+
basisFunctionDerivEta[4] = 4 * ksi;
144+
basisFunctionDerivEta[5] = 4 * (l1 - eta);
145+
}
146+
} else if (this.elementOrder === "linear") {
105147
// Linear basis functions for 2D elements
106148
function l1(c) {
107149
return 1 - c;

‎src/methods/numericalIntegration.js‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,51 @@ export class NumericalIntegration {
1515
* Constructor to initialize the NumericalIntegration class
1616
* @param {string} meshDimension - The dimension of the mesh
1717
* @param {string} elementOrder - The order of elements
18+
* @param {string} [elementShape='quad'] - The 2D element shape, either 'quad' or 'triangle'
1819
*/
19-
constructor({ meshDimension, elementOrder }) {
20+
constructor({ meshDimension, elementOrder, elementShape = "quad" }) {
2021
this.meshDimension = meshDimension;
2122
this.elementOrder = elementOrder;
23+
this.elementShape = elementShape;
2224
}
2325

2426
/**
2527
* Function to return Gauss points and weights based on element configuration
2628
* @returns {object} An object containing:
27-
* - gaussPoints: Array of Gauss points
28-
* - gaussWeights: Array of Gauss weights
29+
* - gaussPoints: Array of Gauss points (for triangles: array of [ksi, eta] pairs)
30+
* - gaussWeights: Array of Gauss weights (for triangles: sum to 1/2, the reference triangle area)
2931
*/
3032
getGaussPointsAndWeights() {
3133
let gaussPoints = []; // Gauss points
3234
let gaussWeights = []; // Gauss weights
3335

34-
if (this.elementOrder === "linear") {
36+
if (this.elementShape === "triangle") {
37+
// Collapsed (non-tensor-product) rules on the reference triangle (0,0), (1,0), (0,1)
38+
if (this.elementOrder === "linear") {
39+
// 3-point rule, exact up to degree 2
40+
gaussPoints = [
41+
[1 / 6, 1 / 6],
42+
[2 / 3, 1 / 6],
43+
[1 / 6, 2 / 3],
44+
];
45+
gaussWeights = [1 / 6, 1 / 6, 1 / 6];
46+
} else if (this.elementOrder === "quadratic") {
47+
// 6-point rule, exact up to degree 4
48+
const a = 0.4459484909159649;
49+
const b = 0.0915762135097707;
50+
const wa = 0.2233815896780115 / 2;
51+
const wb = 0.1099517436553219 / 2;
52+
gaussPoints = [
53+
[a, a],
54+
[1 - 2 * a, a],
55+
[a, 1 - 2 * a],
56+
[b, b],
57+
[1 - 2 * b, b],
58+
[b, 1 - 2 * b],
59+
];
60+
gaussWeights = [wa, wa, wa, wb, wb, wb];
61+
}
62+
} else if (this.elementOrder === "linear") {
3563
// For linear elements, use 1-point Gauss quadrature
3664
gaussPoints[0] = 0.5;
3765
gaussWeights[0] = 1;
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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

Comments
 (0)