|
| 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 | + * Benchmark test for 2D steady-state heat conduction with convection (NAFEMS T4) |
| 12 | + * |
| 13 | + * Replicates the NAFEMS T4 benchmark, also documented as a DIANA FEA tutorial |
| 14 | + * (https://tutorials.dianafea.com/2DHeatTransferConvection.pdf) and by Altair |
| 15 | + * (https://help.altair.com/hwsolvers/os/topics/solvers/os/nafems_test_problem_t4_r.htm): |
| 16 | + * a 0.6 m x 1.0 m rectangular plate ABCD with a fixed 100 degC temperature on the bottom edge |
| 17 | + * AB, an insulated left edge AD, and convection (h = 750 W/(m2.K), ambient = 0 degC) on the top |
| 18 | + * edge CD and right edge BC. The published target temperature at point E (x = 0.6 m, y = 0.2 m, |
| 19 | + * on the convective right edge) is 18.3 degC. |
| 20 | + * |
| 21 | + * This test is an independent reproduction of the NAFEMS T4 problem and is not sponsored, |
| 22 | + * endorsed, or affiliated with NAFEMS. |
| 23 | + * |
| 24 | + * Run: node tests/verification/benchmark/heatConduction2DNafemsT4/benchmark.test.js (or npm test) |
| 25 | + */ |
| 26 | + |
| 27 | +import * as mathjs from "mathjs"; |
| 28 | +import { FEAScriptModel } from "../../../../src/FEAScript.js"; |
| 29 | +import { basicLog, errorLog } from "../../../../src/utilities/logging.js"; |
| 30 | + |
| 31 | +// FEAScript.js references `math` as a global (loaded via CDN in browser). |
| 32 | +// Set it here before any solve() call. |
| 33 | +globalThis.math = mathjs; |
| 34 | + |
| 35 | +const POINT_E = { x: 0.6, y: 0.2 }; |
| 36 | +const NAFEMS_TARGET_T = 18.3; |
| 37 | + |
| 38 | +function runSimulation(numElementsX, numElementsY) { |
| 39 | + const model = new FEAScriptModel(); |
| 40 | + |
| 41 | + model.setModelConfig("heatConductionScript", { coefficientFunctions: { thermalConductivity: 52 } }); |
| 42 | + model.setMeshConfig({ |
| 43 | + meshDimension: "2D", |
| 44 | + elementOrder: "quadratic", |
| 45 | + numElementsX, |
| 46 | + numElementsY, |
| 47 | + maxX: 0.6, |
| 48 | + maxY: 1.0, |
| 49 | + }); |
| 50 | + |
| 51 | + model.addBoundaryCondition("0", ["constantTemperature", 100]); // Bottom edge (AB), fixed 100 degC |
| 52 | + // Left edge (AD) is insulated and left unspecified (natural, zero-flux boundary) |
| 53 | + model.addBoundaryCondition("2", ["convection", 750, 0]); // Top edge (CD), convection to 0 degC |
| 54 | + model.addBoundaryCondition("3", ["convection", 750, 0]); // Right edge (BC), convection to 0 degC |
| 55 | + model.setSolverMethod("lusolve"); |
| 56 | + |
| 57 | + const { solutionVector, nodesCoordinates } = model.solve(); |
| 58 | + const { nodesXCoordinates, nodesYCoordinates } = nodesCoordinates; |
| 59 | + |
| 60 | + const nodeIndex = nodesXCoordinates.findIndex( |
| 61 | + (x, i) => Math.abs(x - POINT_E.x) < 1e-10 && Math.abs(nodesYCoordinates[i] - POINT_E.y) < 1e-10, |
| 62 | + ); |
| 63 | + |
| 64 | + // solutionVector from math.lusolve is a nested array: [[T0], [T1], ...] |
| 65 | + const temperatureAtE = Array.isArray(solutionVector[nodeIndex]) |
| 66 | + ? solutionVector[nodeIndex][0] |
| 67 | + : solutionVector[nodeIndex]; |
| 68 | + |
| 69 | + return { nodeIndex, temperatureAtE }; |
| 70 | +} |
| 71 | + |
| 72 | +let passed = 0; |
| 73 | +let failed = 0; |
| 74 | + |
| 75 | +function assert(condition, message) { |
| 76 | + if (!condition) { |
| 77 | + errorLog(`FAIL: ${message}`); |
| 78 | + failed++; |
| 79 | + } else { |
| 80 | + basicLog(`PASS: ${message}`); |
| 81 | + passed++; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +basicLog(""); |
| 86 | +basicLog("================================"); |
| 87 | +basicLog("Starting benchmark test for NAFEMS T4 (2D heat conduction with convection)..."); |
| 88 | + |
| 89 | +const CONVERGENCE_TOLERANCE = 0.1; // degC |
| 90 | +const { nodeIndex, temperatureAtE } = runSimulation(12, 20); |
| 91 | + |
| 92 | +assert(nodeIndex !== -1, `Found node E at (x=${POINT_E.x}, y=${POINT_E.y})`); |
| 93 | +assert( |
| 94 | + Math.abs(temperatureAtE - NAFEMS_TARGET_T) < CONVERGENCE_TOLERANCE, |
| 95 | + `Mesh (12x20): temperature at E expected close to NAFEMS target ${NAFEMS_TARGET_T}, got ${temperatureAtE} (tolerance ${CONVERGENCE_TOLERANCE})`, |
| 96 | +); |
| 97 | + |
| 98 | +basicLog(""); |
| 99 | +if (failed > 0) { |
| 100 | + errorLog(`${passed} passed, ${failed} failed.`); |
| 101 | +} else { |
| 102 | + basicLog(`${passed} passed, ${failed} failed.`); |
| 103 | +} |
| 104 | +basicLog("================================"); |
| 105 | +if (failed > 0) process.exit(1); |
0 commit comments