Skip to content

Commit d70d28c

Browse files
committed
Refactor solidHeatTransferScript.js: replace individual boundary condition flags with ThermalBoundaryConditions class, simplifying convection and constant temperature boundary condition handling.
1 parent a910d97 commit d70d28c

File tree

2 files changed

+130
-196
lines changed

2 files changed

+130
-196
lines changed

src/methods/thermalBoundaryConditionsScript.js

Lines changed: 120 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -9,160 +9,137 @@
99
// Website: https://feascript.com/ \__| //
1010

1111
/**
12-
* Impose ConstantTemp boundary conditions
13-
* @param {object} boundaryConditions - Object containing boundary conditions for the finite element analysis
14-
* @param {array} boundaryElements - Array containing elements and their adjacent boundary side for each boundary
15-
* @param {array} nop - Nodal numbering array
16-
* @param {array} residualVector - Residual vector to be modified
17-
* @param {array} jacobianMatrix - Jacobian matrix to be modified
18-
* @param {string} dimension - The dimension of the mesh ('1D' or '2D')
12+
* Class to handle thermal boundary conditions application
1913
*/
20-
export function imposeConstantTempBoundaryConditions(
21-
boundaryConditions,
22-
boundaryElements,
23-
nop,
24-
residualVector,
25-
jacobianMatrix,
26-
dimension
27-
) {
28-
if (dimension === "2D") {
29-
const boundarySides = {
30-
0: [0, 3, 6], // Nodes at the bottom side of the reference element
31-
1: [0, 1, 2], // Nodes at the left side of the reference element
32-
2: [2, 5, 8], // Nodes at the top side of the reference element
33-
3: [6, 7, 8], // Nodes at the right side of the reference element
34-
};
14+
export class ThermalBoundaryConditions {
15+
constructor(boundaryConditions, boundaryElements, nop, dimension) {
16+
this.boundaryConditions = boundaryConditions;
17+
this.boundaryElements = boundaryElements;
18+
this.nop = nop;
19+
this.dimension = dimension;
20+
}
3521

36-
Object.keys(boundaryConditions).forEach((key) => {
37-
if (boundaryConditions[key][0] === "constantTemp") {
38-
const tempValue = boundaryConditions[key][1];
39-
boundaryElements[key].forEach(([elementIndex, side]) => {
40-
boundarySides[key].forEach((nodeIndex) => {
41-
const globalNodeIndex = nop[elementIndex][nodeIndex] - 1;
42-
// Set the residual vector to the ConstantTemp value
43-
residualVector[globalNodeIndex] = tempValue;
44-
// Set the Jacobian matrix row to zero
45-
for (let j = 0; j < residualVector.length; j++) {
46-
jacobianMatrix[globalNodeIndex][j] = 0;
47-
}
48-
// Set the diagonal entry of the Jacobian matrix to one
49-
jacobianMatrix[globalNodeIndex][globalNodeIndex] = 1;
22+
imposeConstantTempBoundaryConditions(residualVector, jacobianMatrix) {
23+
if (this.dimension === "2D") {
24+
const boundarySides = {
25+
0: [0, 3, 6], // Nodes at the bottom side of the reference element
26+
1: [0, 1, 2], // Nodes at the left side of the reference element
27+
2: [2, 5, 8], // Nodes at the top side of the reference element
28+
3: [6, 7, 8], // Nodes at the right side of the reference element
29+
};
30+
31+
Object.keys(this.boundaryConditions).forEach((key) => {
32+
if (this.boundaryConditions[key][0] === "constantTemp") {
33+
const tempValue = this.boundaryConditions[key][1];
34+
this.boundaryElements[key].forEach(([elementIndex, side]) => {
35+
boundarySides[key].forEach((nodeIndex) => {
36+
const globalNodeIndex = this.nop[elementIndex][nodeIndex] - 1;
37+
// Set the residual vector to the ConstantTemp value
38+
residualVector[globalNodeIndex] = tempValue;
39+
// Set the Jacobian matrix row to zero
40+
for (let j = 0; j < residualVector.length; j++) {
41+
jacobianMatrix[globalNodeIndex][j] = 0;
42+
}
43+
// Set the diagonal entry of the Jacobian matrix to one
44+
jacobianMatrix[globalNodeIndex][globalNodeIndex] = 1;
45+
});
5046
});
51-
});
52-
}
53-
});
54-
} else if (dimension === "1D") {
55-
// . . . implementation for 1D . . .
47+
}
48+
});
49+
} else if (this.dimension === "1D") {
50+
// . . . implementation for 1D . . .
51+
}
5652
}
57-
}
5853

59-
/**
60-
* Impose Convection boundary conditions
61-
* @param {object} boundaryConditions - Object containing boundary conditions for the finite element analysis
62-
* @param {array} boundaryElements - Array containing elements and their adjacent boundary side for each boundary
63-
* @param {array} nop - Nodal numbering array
64-
* @param {array} residualVector - Residual vector to be modified
65-
* @param {array} jacobianMatrix - Jacobian matrix to be modified
66-
* @param {string} dimension - The dimension of the mesh ('1D' or '2D')
67-
* @param {array} gaussPoints - Gauss points for numerical integration
68-
* @param {array} gaussWeights - Gauss weights for numerical integration
69-
* @param {array} nodesXCoordinates - X coordinates of the nodes
70-
* @param {array} nodesYCoordinates - Y coordinates of the nodes
71-
* @param {object} basisFunctionsData - Basis functions data
72-
* @param {array} convectionHeatTranfCoeff - Convection heat transfer coefficients
73-
* @param {array} convectionExtTemp - External temperatures for convection
74-
*/
75-
export function imposeConvectionBoundaryConditions(
76-
boundaryConditions,
77-
boundaryElements,
78-
nop,
79-
residualVector,
80-
jacobianMatrix,
81-
dimension,
82-
gaussPoints,
83-
gaussWeights,
84-
nodesXCoordinates,
85-
nodesYCoordinates,
86-
basisFunctionsData,
87-
convectionHeatTranfCoeff,
88-
convectionExtTemp
89-
) {
90-
if (dimension === "2D") {
91-
Object.keys(boundaryConditions).forEach((key) => {
92-
if (boundaryConditions[key][0] === "convection") {
93-
const convectionCoeff = convectionHeatTranfCoeff[key];
94-
const extTemp = convectionExtTemp[key];
95-
boundaryElements[key].forEach(([elementIndex, side]) => {
96-
for (let l = 0; l < 3; l++) {
97-
let gp1, gp2, firstNode, finalNode, nodeIncr;
98-
if (side === 0) {
99-
// Nodes at the bottom side of the reference element
100-
gp1 = gaussPoints[l];
101-
gp2 = 0;
102-
firstNode = 0;
103-
finalNode = 7;
104-
nodeIncr = 3;
105-
} else if (side === 1) {
106-
// Nodes at the left side of the reference element
107-
gp1 = 0;
108-
gp2 = gaussPoints[l];
109-
firstNode = 0;
110-
finalNode = 3;
111-
nodeIncr = 1;
112-
} else if (side === 2) {
113-
// Nodes at the top side of the reference element
114-
gp1 = gaussPoints[l];
115-
gp2 = 1;
116-
firstNode = 2;
117-
finalNode = 9;
118-
nodeIncr = 3;
119-
} else if (side === 3) {
120-
// Nodes at the right side of the reference element
121-
gp1 = 1;
122-
gp2 = gaussPoints[l];
123-
firstNode = 6;
124-
finalNode = 9;
125-
nodeIncr = 1;
126-
}
54+
imposeConvectionBoundaryConditions(
55+
residualVector,
56+
jacobianMatrix,
57+
gaussPoints,
58+
gaussWeights,
59+
nodesXCoordinates,
60+
nodesYCoordinates,
61+
basisFunctionsData,
62+
convectionHeatTranfCoeff,
63+
convectionExtTemp
64+
) {
65+
if (this.dimension === "2D") {
66+
Object.keys(this.boundaryConditions).forEach((key) => {
67+
if (this.boundaryConditions[key][0] === "convection") {
68+
const convectionCoeff = convectionHeatTranfCoeff[key];
69+
const extTemp = convectionExtTemp[key];
70+
this.boundaryElements[key].forEach(([elementIndex, side]) => {
71+
for (let l = 0; l < 3; l++) {
72+
let gp1, gp2, firstNode, finalNode, nodeIncr;
73+
if (side === 0) {
74+
// Nodes at the bottom side of the reference element
75+
gp1 = gaussPoints[l];
76+
gp2 = 0;
77+
firstNode = 0;
78+
finalNode = 7;
79+
nodeIncr = 3;
80+
} else if (side === 1) {
81+
// Nodes at the left side of the reference element
82+
gp1 = 0;
83+
gp2 = gaussPoints[l];
84+
firstNode = 0;
85+
finalNode = 3;
86+
nodeIncr = 1;
87+
} else if (side === 2) {
88+
// Nodes at the top side of the reference element
89+
gp1 = gaussPoints[l];
90+
gp2 = 1;
91+
firstNode = 2;
92+
finalNode = 9;
93+
nodeIncr = 3;
94+
} else if (side === 3) {
95+
// Nodes at the right side of the reference element
96+
gp1 = 1;
97+
gp2 = gaussPoints[l];
98+
firstNode = 6;
99+
finalNode = 9;
100+
nodeIncr = 1;
101+
}
127102

128-
let basisFunctionsAndDerivatives =
129-
basisFunctionsData.getBasisFunctions(gp1, gp2);
130-
let basisFunction = basisFunctionsAndDerivatives.basisFunction;
131-
let basisFunctionDerivKsi =
132-
basisFunctionsAndDerivatives.basisFunctionDerivKsi;
133-
let xCoordinates = 0;
134-
let ksiDerivX = 0;
135-
for (let k = 0; k < 9; k++) {
136-
xCoordinates +=
137-
nodesXCoordinates[nop[elementIndex][k] - 1] * basisFunction[k];
138-
ksiDerivX +=
139-
nodesXCoordinates[nop[elementIndex][k] - 1] *
140-
basisFunctionDerivKsi[k];
141-
}
103+
let basisFunctionsAndDerivatives =
104+
basisFunctionsData.getBasisFunctions(gp1, gp2);
105+
let basisFunction = basisFunctionsAndDerivatives.basisFunction;
106+
let basisFunctionDerivKsi =
107+
basisFunctionsAndDerivatives.basisFunctionDerivKsi;
108+
let xCoordinates = 0;
109+
let ksiDerivX = 0;
110+
for (let k = 0; k < 9; k++) {
111+
xCoordinates +=
112+
nodesXCoordinates[this.nop[elementIndex][k] - 1] *
113+
basisFunction[k];
114+
ksiDerivX +=
115+
nodesXCoordinates[this.nop[elementIndex][k] - 1] *
116+
basisFunctionDerivKsi[k];
117+
}
142118

143-
for (let m = firstNode; m < finalNode; m += nodeIncr) {
144-
let m1 = nop[elementIndex][m] - 1;
145-
residualVector[m1] +=
146-
-gaussWeights[l] *
147-
ksiDerivX *
148-
basisFunction[m] *
149-
convectionCoeff *
150-
extTemp;
151-
for (let n = firstNode; n < finalNode; n += nodeIncr) {
152-
let n1 = nop[elementIndex][n] - 1;
153-
jacobianMatrix[m1][n1] +=
119+
for (let m = firstNode; m < finalNode; m += nodeIncr) {
120+
let m1 = this.nop[elementIndex][m] - 1;
121+
residualVector[m1] +=
154122
-gaussWeights[l] *
155123
ksiDerivX *
156124
basisFunction[m] *
157-
basisFunction[n] *
158-
convectionCoeff;
125+
convectionCoeff *
126+
extTemp;
127+
for (let n = firstNode; n < finalNode; n += nodeIncr) {
128+
let n1 = this.nop[elementIndex][n] - 1;
129+
jacobianMatrix[m1][n1] +=
130+
-gaussWeights[l] *
131+
ksiDerivX *
132+
basisFunction[m] *
133+
basisFunction[n] *
134+
convectionCoeff;
135+
}
159136
}
160137
}
161-
}
162-
});
163-
}
164-
});
165-
} else if (dimension === "1D") {
166-
// . . . implementation for 1D . . .
138+
});
139+
}
140+
});
141+
} else if (this.dimension === "1D") {
142+
// . . . implementation for 1D . . .
143+
}
167144
}
168145
}

src/solvers/solidHeatTransferScript.js

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111
import { basisFunctions } from "../mesh/basisFunctionsScript.js";
1212
import { numericalIntegration } from "../methods/numericalIntegrationScript.js";
1313
import { meshGeneration } from "../mesh/meshGenerationScript.js";
14-
import {
15-
imposeConstantTempBoundaryConditions,
16-
imposeConvectionBoundaryConditions,
17-
} from "../methods/thermalBoundaryConditionsScript.js";
14+
import { ThermalBoundaryConditions } from "../methods/thermalBoundaryConditionsScript.js";
1815

1916
/**
2017
* Assemble the solid heat transfer matrix
@@ -69,12 +66,6 @@ export function assembleSolidHeatTransferMat(meshConfig, boundaryConditions) {
6966
// Initialize variables for matrix assembly
7067
const totalElements = numElementsX * numElementsY; // Total number of elements
7168
const totalNodes = totalNodesX * totalNodesY; // Total number of nodes
72-
let convectionBoundaryFlagTop = new Array(totalElements).fill(0); // Convection boundary condition flag (elements at the top side of the domain)
73-
let convectionBoundaryFlagBottom = new Array(totalElements).fill(0); // Convection boundary condition flag (elements at the bottom side of the domain)
74-
let convectionBoundaryFlagLeft = new Array(totalElements).fill(0); // Convection boundary condition flag (elements at the left side of the domain)
75-
let convectionBoundaryFlagRight = new Array(totalElements).fill(0); // Convection boundary condition flag (elements at the right side of the domain)
76-
let constantTempBoundaryFlag = new Array(totalNodes).fill(0); // ConstantTemp boundary condition flag
77-
let constantTempBoundaryValue = new Array(totalNodes).fill(0); // ConstantTemp boundary condition value
7869
let localNodalNumbers = []; // Local nodal numbering
7970
let gaussPoints = []; // Gauss points
8071
let gaussWeights = []; // Gauss weights
@@ -119,40 +110,6 @@ export function assembleSolidHeatTransferMat(meshConfig, boundaryConditions) {
119110
gaussPoints = gaussPointsAndWeights.gaussPoints;
120111
gaussWeights = gaussPointsAndWeights.gaussWeights;
121112

122-
// Check for elements to impose Convection boundary conditions
123-
for (
124-
let elementIndex = 0;
125-
elementIndex < totalElements - numElementsY;
126-
elementIndex += numElementsY
127-
) {
128-
if (boundaryConditions["0"][0] == "convection") {
129-
convectionBoundaryFlagBottom[elementIndex] = 1;
130-
}
131-
}
132-
for (let elementIndex = 0; elementIndex < numElementsY; elementIndex++) {
133-
if (boundaryConditions["1"][0] == "convection") {
134-
convectionBoundaryFlagLeft[elementIndex] = 1;
135-
}
136-
}
137-
for (
138-
let elementIndex = numElementsY - 1;
139-
elementIndex < totalElements;
140-
elementIndex += numElementsY
141-
) {
142-
if (boundaryConditions["2"][0] == "convection") {
143-
convectionBoundaryFlagTop[elementIndex] = 1;
144-
}
145-
}
146-
for (
147-
let elementIndex = totalElements - numElementsY;
148-
elementIndex < totalElements;
149-
elementIndex++
150-
) {
151-
if (boundaryConditions["3"][0] == "convection") {
152-
convectionBoundaryFlagRight[elementIndex] = 1;
153-
}
154-
}
155-
156113
// Matrix assembly
157114
for (let elementIndex = 0; elementIndex < totalElements; elementIndex++) {
158115
for (let localNodeIndex = 0; localNodeIndex < 9; localNodeIndex++) {
@@ -244,14 +201,18 @@ export function assembleSolidHeatTransferMat(meshConfig, boundaryConditions) {
244201
}
245202
}
246203

247-
// Impose Convection boundary conditions
248-
imposeConvectionBoundaryConditions(
204+
// Create an instance of ThermalBoundaryConditions
205+
const thermalBoundaryConditions = new ThermalBoundaryConditions(
249206
boundaryConditions,
250207
boundaryElements,
251208
nop,
209+
dimension
210+
);
211+
212+
// Impose Convection boundary conditions
213+
thermalBoundaryConditions.imposeConvectionBoundaryConditions(
252214
residualVector,
253215
jacobianMatrix,
254-
dimension,
255216
gaussPoints,
256217
gaussWeights,
257218
nodesXCoordinates,
@@ -262,13 +223,9 @@ export function assembleSolidHeatTransferMat(meshConfig, boundaryConditions) {
262223
);
263224

264225
// Impose ConstantTemp boundary conditions
265-
imposeConstantTempBoundaryConditions(
266-
boundaryConditions,
267-
boundaryElements,
268-
nop,
226+
thermalBoundaryConditions.imposeConstantTempBoundaryConditions(
269227
residualVector,
270-
jacobianMatrix,
271-
dimension
228+
jacobianMatrix
272229
);
273230

274231
return {

0 commit comments

Comments
 (0)