CFDScript 

A JavaScript library for solving Partial Differential Equations (PDE) using the Finite Element Method (FEM)

CFDScript is a JavaScript library that is designed to solve Partial Differential Equations (PDE) using the Finite Element Method (FEM). It enables the creation and running of browser-based simulations for physics and engineering problems. Visualization tools are also provided by CFDScript to display the results of simulations.

Download

CFDScript is currently under development with continuous addition of new features and improvements. The latest version of CFDScript can be downloaded from the Git repository: https://github.com/CFDScript/CFDScript.

Documentation

Usage example (Heat transfer on a 2D solid)

A demonstration is provided below on how to utilize the CFDScript library for addressing a stationary heat transfer problem on a 2D rectangular domain. The following scripts should be included in the HTML page. The initial step involves the importation of the Math.js and Plotly.js libraries, which are essential for solving the system of equations and visualizing the solution.

<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/5.0.0/math.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/plotly.js/2.27.0/plotly.min.js"></script>

Following this, various problem parameters, such as the number of elements, domain boundaries, and boundary conditions, are defined using external JSON files. The first JSON file is the computational mesh configuration file (compuMesh.json):

{
  "nex": 8,
  "ney": 4,
  "xlast": 4,
  "ylast": 2
}

In this example, the parameters nex and ney represent the number of elements along the x-axis and y-axis, respectively. Additionally, xlast and ylast denote the final x-coordinate and y-coordinate of the mesh, respectively. The second essential JSON file is the boundary conditions configuration file (boundCond.json). Boundary conditions can be specified as Dirichlet, Robin, or Neumann types. The Dirichlet condition sets a constant temperature value, while the Robin condition describes a convective heat transfer scenario, allowing users to define a custom heat transfer coefficient and an external temperature. The Neumann boundary condition represents a zero-flux type. The boundary conditions configuration file is structured as follows:

{
  "topBound": ["robin", "placeholder"],
  "bottomBound": ["dirichlet", 200.0],
  "leftBound": ["neumann", "placeholder"],
  "rightBound": ["dirichlet", 200.0],
  "robinHeatTranfCoeff": 1,
  "robinExtTemp": 20
}

In the provided example, the second argument for a Dirichlet boundary condition corresponds to the constant temperature value. For a Robin boundary condition, the parameter robinHeatTranfCoeff represents the heat transfer coefficient, and robinExtTemp indicates the external temperature. These JSON files are fetched using the following script:

<script>
    let compuMesh, boundCond;
    // Function to fetch and parse JSON files
    function fetchJSON(url) {
      return fetch(url)
        .then(response => response.json());
    }
    // Fetch all JSON files asynchronously
    Promise.all([
      fetchJSON('./compuMesh.json'), // Computational mesh configuration file
      fetchJSON('./boundCond.json') // Boundary conditions configuration file
    ]).then(([compuMeshData, boundCondData]) => {
      compuMesh = compuMeshData;
      boundCond = boundCondData;
    });
</script>

Subsequently, the solidHeatScript solver, a module of the CFDScript library, is utilized. This solver implements the Finite Element Method for heat conduction problems. The solver accepts the number of elements, domain dimensions, and boundary conditions as inputs and returns the grid points (axpt, aypt) and the solution vector (u) as outputs. Finally, the solution is visualized in the form of a contour plot. The following script demonstrates these steps:

<script type="module">
// Import the CFDscript library
import { CFDScript } from 'https://cfdscript.github.io/CFDScript/src/CFDScript.js';
import { plotSol2D } from 'https://cfdscript.github.io/CFDScript/src/plotSolScript.js';
import { chkSolidHeatBoundCond, CFDScriptVersion } from 'https://cfdscript.github.io/CFDScript/src/auxFunScript.js';

window.addEventListener('DOMContentLoaded', (event) => {
  CFDScriptVersion() // Print CFDScript version
  chkSolidHeatBoundCond(boundCond); // Check boundary conditions
  let { u, nx, ny, axpt, aypt } = CFDScript('solidHeatScript', compuMesh, boundCond) // Assembly matrices and solve the system of equations
  plotSol2D(u, nx, ny, axpt, aypt); // Visualise the solution
});
</script>

To display the plotted results, an HTML container element is also required on the page.

<div id="plot"></div>

For a comprehensive understanding of the heat transfer problem and a demonstration of the provided code, please visit the example page. All issues and feedback should be directed to the following GitHub repository: https://github.com/CFDScript/CFDScript/issues.

Licensing

CFDScript is distributed under the terms of the GNU General Public License version 3. Copyright CFDScript.