Your first simulation
This page walks through a complete simulation: steady-state heat conduction in a 2D fin, solved in the browser and plotted with Plotly. The same steps apply to every model.
The problem
A fin is heated at its bottom and right sides, cooled by convection at its top side, and symmetric about its left side. The temperature satisfies (heat conduction with and no heat source), with:
| Side | Boundary key | Condition |
|---|---|---|
| Bottom | "0" | Constant temperature, |
| Left | "1" | Symmetry (zero heat flux) |
| Top | "2" | Convection, , |
| Right | "3" | Constant temperature, |
The code
Save the following as an .html file and open it through a local web server (for example
npx serve in the same folder):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/11.12.0/math.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/plotly.js/2.35.3/plotly.min.js"></script>
</head>
<body>
<div id="resultsCanvas"></div>
<script type="module">
import {
FEAScriptModel,
plotSolution,
} from "https://cdn.jsdelivr.net/npm/feascript@0.2.0/dist/feascript.esm.js";
// 1. Create the model and select the physics
const model = new FEAScriptModel();
model.setModelConfig("heatConductionScript");
// 2. Define the mesh: 8 × 4 quadratic quadrilaterals on 0 ≤ x ≤ 4, 0 ≤ y ≤ 2
model.setMeshConfig({
meshDimension: "2D",
elementOrder: "quadratic",
numElementsX: 8,
numElementsY: 4,
maxX: 4,
maxY: 2,
});
// 3. Apply the boundary conditions
model.addBoundaryCondition("0", ["constantTemp", 200]); // Bottom
model.addBoundaryCondition("1", ["symmetry"]); // Left
model.addBoundaryCondition("2", ["convection", 1, 20]); // Top
model.addBoundaryCondition("3", ["constantTemp", 200]); // Right
// 4. Solve
model.setSolverMethod("lusolve");
const result = model.solve();
// 5. Plot the results
plotSolution(model, result, "contour", "resultsCanvas");
</script>
</body>
</html>
Step by step
- Create the model and select the physics.
FEAScriptModelholds the whole configuration of the simulation.setModelConfigtakes the name of the model, here heat conduction. - Define the mesh.
setMeshConfiggenerates a structured mesh from the size of the domain and the number of elements (see Mesh), or uses a mesh imported from Gmsh. - Apply the boundary conditions. The sides of a structured 2D mesh have the keys
"0"(bottom),"1"(left),"2"(top) and"3"(right). See Boundary conditions for the condition types of every model. - Solve.
setSolverMethodis optional (the default is"lusolve").solve()returns thesolutionVectorand thenodesCoordinatesof the mesh (see Solvers). - Plot the results.
plotSolutiondraws the result in theresultsCanvaselement.
The result
result.solutionVector holds the temperature at every node, and result.nodesCoordinates the coordinates of the
nodes, in the same order:
const { solutionVector, nodesCoordinates } = result;
const { nodesXCoordinates, nodesYCoordinates } = nodesCoordinates;
for (let i = 0; i < solutionVector.length; i++) {
console.log(nodesXCoordinates[i], nodesYCoordinates[i], solutionVector[i]);
}
The same simulation in Node.js
import * as math from "mathjs";
global.math = math;
import { FEAScriptModel } from "feascript";
// 1. Create the model and select the physics
const model = new FEAScriptModel();
model.setModelConfig("heatConductionScript");
// 2. Define the mesh
model.setMeshConfig({
meshDimension: "2D",
elementOrder: "quadratic",
numElementsX: 8,
numElementsY: 4,
maxX: 4,
maxY: 2,
});
// 3. Apply the boundary conditions
model.addBoundaryCondition("0", ["constantTemp", 200]); // Bottom
model.addBoundaryCondition("1", ["symmetry"]); // Left
model.addBoundaryCondition("2", ["convection", 1, 20]); // Top
model.addBoundaryCondition("3", ["constantTemp", 200]); // Right
// 4. Solve
const { solutionVector, nodesCoordinates } = model.solve();
// 5. Print the results
console.log(`Number of nodes: ${nodesCoordinates.nodesXCoordinates.length}`);
console.log("Temperature:", solutionVector);
Run it with node fin.js (with "type": "module" in your package.json, and feascript@0.2.0 and mathjs
installed, see Installation).
Next steps
- Read about the physical models and their equations.
- Import an unstructured mesh with
importGmshQuadTri. - Run the simulation in a web worker, or on the GPU with the
"jacobi-gpu"solver. - Explore the examples of FEAScript-core 0.2.0.