Skip to content

Commit 96d667d

Browse files
committed
Modify FEAScript to use class-based structure and remove JSON configuration files. Update README for clarity on usage and configuration.
1 parent a372446 commit 96d667d

File tree

9 files changed

+124
-142
lines changed

9 files changed

+124
-142
lines changed

CONTRIBUTING.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
## Contributing to FEAScript
22

3-
Thank you for your interest in contributing! FEAScript is in early development with continuous addition of new features and improvements. To ensure a smooth and collaborative development process, please review and follow the guidelines.
3+
Thank you for your interest in contributing! FEAScript is in early development, with continuous additions of new features and improvements. To ensure a smooth and collaborative development process, please review and follow the guidelines below.
44

55
## Contribution Guidelines
66

7-
<!--1. Search the <a href="https://github.com/FEAScript/FEAScript/wiki/Roadmap" target="_blank">project roadmap</a> to see areas where help is needed.-->
7+
1. **Respect the existing coding style:** Observe the code near your intended changes and aim to preserve that style in your modifications.
88

9-
1. Respect the existing FEAScript coding style. Observe the code near your intended change and attempt to preserve that style with your modifications.
9+
2. **Recommended tools:**
10+
We recommend using [Visual Studio Code](https://code.visualstudio.com/) with the [Prettier plugin](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode) for automatic code formatting. Additionally, use a **110-character line width** to maintain consistent formatting.
1011

11-
2. We recommend using <a href="https://code.visualstudio.com/" target="_blank">Visual Studio Code</a> with the <a href="https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode" target="_blank">Prettier</a> plugin for automatic code formatting. Additionally, we use a 110-character line width to maintain consistent formatting.
12+
3. **Naming conventions:**
13+
Use [camelCase](https://en.wikipedia.org/wiki/Camel_case) formatting for variable names throughout the code.
1214

13-
3. Ensure that you use <a href="https://en.wikipedia.org/wiki/Camel_case" target="_blank">camelCase</a> formatting for variable names throughout the code.
15+
4. **Testing changes locally:**
16+
Before submitting a pull request, test your modifications by running the FEAScript library from a local directory. For example, you can load the library in your HTML file as follows:
1417

15-
4. Before applying for a pull request, you can test your modifications by running the FEAScript library from a local server. For security reasons, it is recommended to use a <a href="https://docs.python.org/3/library/http.server.html" target="_blank">Python HTTP Server</a> by executing the following <a href="https://github.com/FEAScript/FEAScript-website/blob/main/corsHttpServer.py" target="_blank">script</a>. Start the Python server by running `python3 corsHttpServer.py` in your local repository folder. The server address will be http://127.0.0.1:8000/.
18+
```javascript
19+
import { FEAScriptModel, plotSolution, printVersion } from "../../FEAScript/src/index.js";
20+
```
1621

17-
Alternatively, you can disable CORS in your browser for local testing. To disable CORS in Firefox:
22+
For security reasons, it is still recommended to use a local server to handle CORS policies correctly. You can use a <a href="https://docs.python.org/3/library/http.server.html" target="_blank">Python HTTP Server</a> by executing the following <a href="https://github.com/FEAScript/FEAScript-website/blob/main/corsHttpServer.py" target="_blank">script</a> to start a local server:
1823

19-
1. Open Firefox and type `about:config` in the address bar, then press Enter.
20-
2. Accept the risk and continue to the advanced settings.
21-
3. In the search bar, type `security.fileuri.strict_origin_policy`.
22-
4. Double-click on the `security.fileuri.strict_origin_policy` preference to set it to `false`.
24+
```bash
25+
python3 corsHttpServer.py
26+
```
27+
28+
The server will be available at:
29+
`http://127.0.0.1:8000/`

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
## How to Use
1111

12-
FEAScript is built entirely in pure JavaScript, requiring only an HTML page to run. Simulation inputs are provided as <a href="https://www.w3schools.com/js/js_json_intro.asp" target="_blank">JSON</a> files, which can be stored locally or hosted online using platforms like <a href="https://pages.github.com/" target="_blank">GitHub Pages</a> or <a href="https://www.jsonkeeper.com/" target="_blank">JSON Keeper</a>. FEAScript operates entirely within your browser, with no reliance on cloud services. You can explore various examples demonstrating how to use FEAScript to solve a range of problems <a href="https://github.com/FEAScript/FEAScript/tree/main/examples" target="_blank">here</a>.
12+
FEAScript is built entirely in pure JavaScript, requiring only an HTML page to run. Simulation inputs are provided directly through JavaScript objects. FEAScript operates entirely within your browser, with no reliance on cloud services. You can explore various examples demonstrating how to use FEAScript to solve a range of problems <a href="https://github.com/FEAScript/FEAScript/tree/main/examples" target="_blank">here</a>.
1313

1414
## Contribute
1515

examples/solidHeatTransferScript/exampleSolidHeatTransfer01/FEAScriptExampleSolidHeatTransfer01.html

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
<!DOCTYPE html>
2+
3+
<!-- ______ ______ _____ _ _ -->
4+
<!-- | ____| ____| /\ / ____| (_) | | -->
5+
<!-- | |__ | |__ / \ | (___ ___ ____ _ ____ | |_ -->
6+
<!-- | __| | __| / /\ \ \___ \ / __| __| | _ \| __| -->
7+
<!-- | | | |____ / ____ \ ____) | (__| | | | |_) | | -->
8+
<!-- |_| |______/_/ \_\_____/ \___|_| |_| __/| | -->
9+
<!-- | | | | -->
10+
<!-- |_| | |_ -->
11+
<!-- Website: https://feascript.com/ \__| -->
12+
213
<html lang="en">
314
<head>
415
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
@@ -19,10 +30,9 @@ <h1>Heat Conduction in a Two-Dimensional Fin Example</h1>
1930
<div id="solutionPlot"></div>
2031

2132
<p>
22-
The mesh configuration and boundary conditions can be adjusted by editing the respective JSON files
23-
(`meshConfig.json` and `boundaryConditionsConfig.json`). The solver configuration is set explicitly to
24-
`solidHeatTransferScript` in this example. Please refresh the page to update the results. Detailed
25-
instructions for this example can be found in the corresponding
33+
The mesh configuration and boundary conditions are defined directly within the JavaScript code in this
34+
example. Please refresh the page to update the results. Detailed instructions for this example can be
35+
found in the corresponding
2636
<a
2737
href="https://github.com/FEAScript/FEAScript-website/blob/main/tutorialSolidHeatTransfer01/tutorialSolidHeatTransfer01.html"
2838
target="_blank"
@@ -31,70 +41,54 @@ <h1>Heat Conduction in a Two-Dimensional Fin Example</h1>
3141
<a href="https://feascript.com/" target="_blank">FEAScript website</a>.
3242
</p>
3343

34-
<p>
35-
This example requires Cross-Origin Resource Sharing (CORS) to run if the JSON files are stored locally.
36-
To enable CORS on Firefox, you can follow these steps:
37-
</p>
38-
<ol>
39-
<li>Open Firefox and type <code>about:config</code> in the address bar, then press Enter.</li>
40-
<li>Accept the risk and continue to the advanced settings.</li>
41-
<li>
42-
In the search bar, type
43-
<code>security.fileuri.strict_origin_policy</code>.
44-
</li>
45-
<li>
46-
Double-click on the
47-
<code>security.fileuri.strict_origin_policy</code> preference to set it to <code>false</code>.
48-
</li>
49-
</ol>
50-
5144
<p>
5245
FEAScript is distributed under the terms of the
5346
<a href="https://opensource.org/license/mit" target="_blank">MIT license</a>. © 2024 FEAScript.
5447
</p>
5548

56-
<!-- JSON fetching -->
57-
<script>
58-
let meshConfig, boundaryConditionsConfig;
59-
const solverConfig = "solidHeatTransferScript"; // Set solverConfig explicitly
60-
61-
// Function to fetch and parse JSON files
62-
function fetchJSON(url) {
63-
return fetch(url).then((response) => response.json());
64-
}
65-
66-
// Fetch JSON files asynchronously from the provided URLs
67-
Promise.all([fetchJSON("meshConfig.json"), fetchJSON("boundaryConditionsConfig.json")]).then(
68-
([meshConfigData, boundaryConditionsConfigData]) => {
69-
meshConfig = meshConfigData;
70-
boundaryConditionsConfig = boundaryConditionsConfigData;
71-
}
72-
);
73-
</script>
74-
7549
<!-- FEAScript module and visualization -->
7650
<script type="module">
7751
import {
78-
FEAScript,
52+
FEAScriptModel,
7953
plotSolution,
8054
printVersion,
8155
} from "https://feascript.github.io/FEAScript/src/index.js";
8256

8357
window.addEventListener("DOMContentLoaded", () => {
8458
// Print FEAScript version in the console
8559
printVersion();
86-
// Calculate solution and render plot once JSON data is loaded
87-
let { solutionVector, nodesCoordinates } = FEAScript(
88-
solverConfig,
89-
meshConfig,
90-
boundaryConditionsConfig
91-
);
60+
61+
// Create a new FEAScript model
62+
const model = new FEAScriptModel();
63+
64+
// Set solver configuration
65+
model.setSolverConfig("solidHeatTransferScript");
66+
67+
// Define mesh configuration
68+
model.setMeshConfig({
69+
meshDimension: "2D",
70+
elementOrder: "quadratic",
71+
numElementsX: 8,
72+
numElementsY: 4,
73+
maxX: 4,
74+
maxY: 2,
75+
});
76+
77+
// Define boundary conditions
78+
model.addBoundaryCondition("0", ["constantTemp", 200]);
79+
model.addBoundaryCondition("1", ["symmetry"]);
80+
model.addBoundaryCondition("2", ["convection", 1, 20]);
81+
model.addBoundaryCondition("3", ["constantTemp", 200]);
82+
83+
// Solve the problem and get the solution
84+
const { solutionVector, nodesCoordinates } = model.solve();
85+
9286
// Plot the solution as a 2D contour plot
9387
plotSolution(
9488
solutionVector,
9589
nodesCoordinates,
96-
solverConfig,
97-
meshConfig.meshDimension,
90+
model.solverConfig,
91+
model.meshConfig.meshDimension,
9892
"contour",
9993
"solutionPlot"
10094
);
Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,9 @@
1-
<img src="https://feascript.github.io/FEAScript-website/images/FEAScriptLogo.png" width="80">
1+
<img src="https://feascript.github.io/FEAScript-website/images/FEAScriptLogo.png" width="80" alt="FEAScript Logo">
22

33
## Heat Conduction in a Two-Dimensional Fin
44

5-
This example demonstrates how to solve a stationary heat transfer problem within a 2D rectangular domain using the FEAScript library. The problem is a typical cooling fin scenario.
5+
This example demonstrates solving a steady-state heat transfer problem in a 2D rectangular domain using the **FEAScript** library. The problem represents a typical cooling fin scenario, where the objective is to model heat conduction and understand temperature distribution under specific boundary conditions.
66

7-
### Files
7+
### Instructions
88

9-
- `meshConfig.json`: Defines the computational mesh parameters.
10-
- `boundaryConditionsConfig.json`: Specifies the boundary conditions for the problem.
11-
- `FEAScriptExampleSolidHeatTransfer01.html`: The main HTML file that sets up and runs the example.
12-
13-
### CORS Configuration
14-
15-
<p>
16-
This example requires Cross-Origin Resource Sharing (CORS) to run if the JSON files are stored locally. To
17-
enable CORS on Firefox, you can follow these steps:
18-
</p>
19-
<ol>
20-
<li>
21-
Open Firefox and type <code>about:config</code> in the address bar, then
22-
press Enter.
23-
</li>
24-
<li>Accept the risk and continue to the advanced settings.</li>
25-
<li>
26-
In the search bar, type
27-
<code>security.fileuri.strict_origin_policy</code>.
28-
</li>
29-
<li>
30-
Double-click on the
31-
<code>security.fileuri.strict_origin_policy</code> preference to set it
32-
to <code>false</code>.
33-
</li>
34-
</ol>
9+
The **mesh configuration** and **boundary conditions** are defined directly in the JavaScript section of the HTML file. For a step-by-step guide and additional details, refer to the corresponding [tutorial](https://github.com/FEAScript/FEAScript-website/blob/main/tutorialSolidHeatTransfer01/tutorialSolidHeatTransfer01.html).

examples/solidHeatTransferScript/exampleSolidHeatTransfer01/boundaryConditionsConfig.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

examples/solidHeatTransferScript/exampleSolidHeatTransfer01/meshConfig.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/FEAScript.js

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,55 @@ import { assembleSolidHeatTransferMat } from "./solvers/solidHeatTransferScript.
1717
* @param {object} boundaryConditions - Object containing boundary conditions for the finite element analysis
1818
* @returns {object} An object containing the solution vector and additional mesh information
1919
*/
20-
export function FEAScript(solverConfig, meshConfig, boundaryConditions) {
21-
let jacobianMatrix = []; // Jacobian matrix
22-
let residualVector = []; // Galerkin residuals
23-
let solutionVector = []; // Solution vector
24-
let nodesCoordinates = {}; // Object to store x and y coordinates of nodes
25-
26-
// Assembly matrices
27-
console.time("assemblyMatrices");
28-
if (solverConfig === "solidHeatTransferScript") {
29-
console.log("FEAScript solver:", solverConfig);
30-
({ jacobianMatrix, residualVector, nodesCoordinates } = assembleSolidHeatTransferMat(
31-
meshConfig,
32-
boundaryConditions
33-
));
20+
export class FEAScriptModel {
21+
constructor() {
22+
this.solverConfig = null;
23+
this.meshConfig = {};
24+
this.boundaryConditions = {};
25+
}
26+
27+
setSolverConfig(solverConfig) {
28+
this.solverConfig = solverConfig;
29+
}
30+
31+
setMeshConfig(meshConfig) {
32+
this.meshConfig = meshConfig;
33+
}
34+
35+
addBoundaryCondition(boundaryKey, condition) {
36+
this.boundaryConditions[boundaryKey] = condition;
37+
}
38+
39+
solve() {
40+
if (!this.solverConfig || !this.meshConfig || !this.boundaryConditions) {
41+
throw new Error("Solver config, mesh config, and boundary conditions must be set before solving.");
42+
}
43+
44+
let jacobianMatrix = []; // Jacobian matrix
45+
let residualVector = []; // Galerkin residuals
46+
let solutionVector = []; // Solution vector
47+
let nodesCoordinates = {}; // Object to store x and y coordinates of nodes
48+
49+
// Assembly matrices
50+
console.time("assemblyMatrices");
51+
if (this.solverConfig === "solidHeatTransferScript") {
52+
console.log("FEAScript solver:", this.solverConfig);
53+
({ jacobianMatrix, residualVector, nodesCoordinates } = assembleSolidHeatTransferMat(
54+
this.meshConfig,
55+
this.boundaryConditions
56+
));
57+
}
58+
console.timeEnd("assemblyMatrices");
59+
60+
// System solving
61+
console.time("systemSolving");
62+
solutionVector = math.lusolve(jacobianMatrix, residualVector); // Solve the system of linear equations using LU decomposition
63+
console.timeEnd("systemSolving");
64+
65+
// Return the solution matrix and nodes coordinates
66+
return {
67+
solutionVector,
68+
nodesCoordinates,
69+
};
3470
}
35-
console.timeEnd("assemblyMatrices");
36-
37-
// System solving
38-
console.time("systemSolving");
39-
solutionVector = math.lusolve(jacobianMatrix, residualVector); // Solve the system of linear equations using LU decomposition
40-
console.timeEnd("systemSolving");
41-
42-
// Debugging statements
43-
//console.log(x);
44-
//console.log("nodesCoordinates:", nodesCoordinates);
45-
46-
// Return the solution matrix and nodes coordinates
47-
return {
48-
solutionVector,
49-
nodesCoordinates,
50-
};
5171
}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
// |_| | |_ //
99
// Website: https://feascript.com/ \__| //
1010

11-
export { FEAScript } from "./FEAScript.js";
11+
export { FEAScriptModel } from "./FEAScript.js";
1212
export { plotSolution } from "./visualization/plotSolutionScript.js";
1313
export { printVersion } from "./utilities/helperFunctionsScript.js";

src/utilities/helperFunctionsScript.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
*/
1414
export async function printVersion() {
1515
// Fetch the latest release information
16-
const releaseResponse = await fetch("https://api.github.com/repos/FEAScript/FEAScript/releases/latest");
17-
const releaseData = await releaseResponse.json();
18-
console.log(`FEAScript version: ${releaseData.tag_name} - ${releaseData.name}`);
16+
//const releaseResponse = await fetch("https://api.github.com/repos/FEAScript/FEAScript/releases/latest");
17+
//const releaseData = await releaseResponse.json();
18+
//console.log(`FEAScript version: ${releaseData.tag_name} - ${releaseData.name}`);
1919

2020
// Fetch the latest commit date
21-
//const commitResponse = await fetch('https://api.github.com/repos/FEAScript/FEAScript/commits/main');
22-
//const commitData = await commitResponse.json();
23-
//const latestCommitDate = new Date(commitData.commit.committer.date).toLocaleString();
24-
//console.log(`Latest FEAScript update: ${latestCommitDate}`);
21+
const commitResponse = await fetch("https://api.github.com/repos/FEAScript/FEAScript/commits/main");
22+
const commitData = await commitResponse.json();
23+
const latestCommitDate = new Date(commitData.commit.committer.date).toLocaleString();
24+
console.log(`Latest FEAScript update: ${latestCommitDate}`);
2525
}

0 commit comments

Comments
 (0)