|
| 1 | +<!DOCTYPE html> |
| 2 | + |
| 3 | +<!-- |
| 4 | + ════════════════════════════════════════════════════════════════ |
| 5 | + FEAScript Core Library |
| 6 | + Lightweight Finite Element Simulation in JavaScript |
| 7 | + Version: 0.2.0 | https://feascript.com |
| 8 | + MIT License © 2023–2026 FEAScript |
| 9 | + ════════════════════════════════════════════════════════════════ |
| 10 | +--> |
| 11 | + |
| 12 | +<html> |
| 13 | + <head> |
| 14 | + <title>FEAScript Example: Lid-Driven Cavity 2D Creeping Flow</title> |
| 15 | + <meta charset="UTF-8" /> |
| 16 | + <!-- Link to the CSS files --> |
| 17 | + <link href="https://feascript.com/feascript-website.css" rel="stylesheet" type="text/css" /> |
| 18 | + <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet" /> |
| 19 | + <!-- Import the Math.js library for mathematical operations --> |
| 20 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/11.12.0/math.min.js"></script> |
| 21 | + <!-- Import the Plotly.js library for plotting --> |
| 22 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/plotly.js/2.35.3/plotly.min.js"></script> |
| 23 | + </head> |
| 24 | + |
| 25 | + <body> |
| 26 | + <h2>Lid-Driven Cavity 2D Creeping Flow</h2> |
| 27 | + |
| 28 | + <h3>Velocity Magnitude</h3> |
| 29 | + <div id="velocityMagnitudeCanvas"></div> |
| 30 | + |
| 31 | + <h3>u-Velocity Component</h3> |
| 32 | + <div id="uVelocityCanvas"></div> |
| 33 | + |
| 34 | + <h3>v-Velocity Component</h3> |
| 35 | + <div id="vVelocityCanvas"></div> |
| 36 | + |
| 37 | + <script type="module"> |
| 38 | + // Import FEAScript library |
| 39 | + import { FEAScriptModel, plotSolution, printVersion } from "https://core.feascript.com/dist/feascript.esm.js"; |
| 40 | + // import { FEAScriptModel, plotSolution, printVersion } from "../../../src/index.js"; |
| 41 | + |
| 42 | + window.addEventListener("DOMContentLoaded", () => { |
| 43 | + console.log("FEAScript Version:", printVersion); |
| 44 | + |
| 45 | + // Create a new FEAScript model |
| 46 | + const model = new FEAScriptModel(); |
| 47 | + |
| 48 | + // Select physics/PDE |
| 49 | + model.setModelConfig("creepingFlowScript"); |
| 50 | + |
| 51 | + // Define mesh configuration |
| 52 | + model.setMeshConfig({ |
| 53 | + meshDimension: "2D", |
| 54 | + elementOrder: "quadratic", |
| 55 | + numElementsX: 12, |
| 56 | + numElementsY: 6, |
| 57 | + maxX: 4, |
| 58 | + maxY: 2, |
| 59 | + }); |
| 60 | + |
| 61 | + // Define boundary conditions |
| 62 | + model.addBoundaryCondition("0", ["constantVelocity", 0, 0]); // Bottom boundary |
| 63 | + model.addBoundaryCondition("1", ["constantVelocity", 0, 0]); // Left boundary |
| 64 | + model.addBoundaryCondition("2", ["constantVelocity", 1, 0]); // Top boundary |
| 65 | + model.addBoundaryCondition("3", ["constantVelocity", 0, 0]); // Right boundary |
| 66 | + |
| 67 | + // Set solver method |
| 68 | + model.setSolverMethod("lusolve"); |
| 69 | + |
| 70 | + // Solve the problem |
| 71 | + const result = model.solve(); |
| 72 | + |
| 73 | + // Print results to console |
| 74 | + console.log(`Number of nodes in mesh: ${result.nodesCoordinates.nodesXCoordinates.length}`); |
| 75 | + console.log("Solution vector:", result.solutionVector); |
| 76 | + |
| 77 | + // Extract velocity and pressure fields from the solution vector |
| 78 | + // DOF layout: [u₀…u_{N₂−1}, v₀…v_{N₂−1}, p₀…p_{N₁−1}] |
| 79 | + const totalNodesVelocity = model._creepingFlowMetadata.totalNodesVelocity; |
| 80 | + const solutionVector = result.solutionVector; |
| 81 | + const uVelocity = solutionVector.slice(0, totalNodesVelocity); |
| 82 | + const vVelocity = solutionVector.slice(totalNodesVelocity, 2 * totalNodesVelocity); |
| 83 | + const velocityMagnitude = uVelocity.map((u, i) => Math.sqrt(u * u + vVelocity[i] * vVelocity[i])); |
| 84 | + |
| 85 | + // Plot velocity magnitude as a 2D contour plot |
| 86 | + plotSolution( |
| 87 | + model, |
| 88 | + { solutionVector: velocityMagnitude, nodesCoordinates: result.nodesCoordinates }, |
| 89 | + "contour", |
| 90 | + "velocityMagnitudeCanvas" |
| 91 | + ); |
| 92 | + |
| 93 | + // Plot u-velocity component as a 2D contour plot |
| 94 | + plotSolution( |
| 95 | + model, |
| 96 | + { solutionVector: uVelocity, nodesCoordinates: result.nodesCoordinates }, |
| 97 | + "contour", |
| 98 | + "uVelocityCanvas" |
| 99 | + ); |
| 100 | + |
| 101 | + // Plot v-velocity component as a 2D contour plot |
| 102 | + plotSolution( |
| 103 | + model, |
| 104 | + { solutionVector: vVelocity, nodesCoordinates: result.nodesCoordinates }, |
| 105 | + "contour", |
| 106 | + "vVelocityCanvas" |
| 107 | + ); |
| 108 | + }); |
| 109 | + </script> |
| 110 | + </body> |
| 111 | +</html> |
0 commit comments