Skip to content

Commit 9c090d9

Browse files
committed
Refactor Newton-Raphson method to accept matrix assembly function and context, enhancing front propagation solver with eikonal viscous term parameterization
1 parent 7eecf29 commit 9c090d9

File tree

3 files changed

+87
-15
lines changed

3 files changed

+87
-15
lines changed

src/FEAScript.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,51 @@ export class FEAScriptModel {
7373
this.boundaryConditions
7474
));
7575
} else if (this.solverConfig === "frontPropagationScript") {
76+
basicLog(`Using solver: ${this.solverConfig}`);
7677
let eikonalActivationFlag = 0; // Parameterization flag (from 0 to 1)
7778
const initialEikonalViscousTerm = 0.1; // Initial viscous term for the front propagation (eikonal) equation
7879
let eikonalViscousTerm = 1 - eikonalActivationFlag + initialEikonalViscousTerm; // Viscous term for the front propagation (eikonal) equation
79-
basicLog(`Using solver: ${this.solverConfig}`);
8080

81-
// Iterations for fully activating Eikonal equation
81+
// Create context object with all necessary properties
82+
const context = {
83+
meshConfig: this.meshConfig,
84+
boundaryConditions: this.boundaryConditions,
85+
eikonalActivationFlag,
86+
eikonalViscousTerm,
87+
solverMethod: this.solverMethod
88+
};
89+
8290
while (eikonalActivationFlag <= 1) {
83-
// Newton-Raphson iterations
84-
({ jacobianMatrix, residualVector, nodesCoordinates } = assembleFrontPropagationMat(
85-
this.meshConfig,
86-
this.boundaryConditions
87-
));
91+
// Use Newton-Raphson to iterate
92+
const newtonRaphsonResult = newtonRaphson(
93+
assembleFrontPropagationMat, // Pass the function reference
94+
context,
95+
100, // maxIterations
96+
1e-7 // tolerance
97+
);
98+
99+
// Extract results
100+
jacobianMatrix = newtonRaphsonResult.jacobianMatrix;
101+
residualVector = newtonRaphsonResult.residualVector;
102+
nodesCoordinates = newtonRaphsonResult.nodesCoordinates;
103+
solutionVector = newtonRaphsonResult.solution;
104+
105+
// Increment eikonalActivationFlag for next step if needed
106+
eikonalActivationFlag += 0.1;
88107
}
89108
}
90109
console.timeEnd("assemblyMatrices");
91110
basicLog("Matrix assembly completed");
92111

93-
// System solving
112+
// Solve the linear system based on the specified solver method
94113
basicLog(`Solving system using ${this.solverMethod}...`);
95114
console.time("systemSolving");
96115
if (this.solverMethod === "lusolve") {
116+
// Use LU decomposition method
97117
solutionVector = math.lusolve(jacobianMatrix, residualVector);
98118
} else if (this.solverMethod === "jacobi") {
99-
// Create initial guess of zeros
119+
// Use Jacobi method
100120
const initialGuess = new Array(residualVector.length).fill(0);
101-
// Call Jacobi method with desired max iterations and tolerance
102121
const jacobiResult = jacobiMethod(jacobianMatrix, residualVector, initialGuess, 1000, 1e-6);
103122

104123
// Log convergence information

src/methods/newtonRaphsonScript.js

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { basicLog, debugLog, errorLog } from "./utilities/loggingScript.js";
2222
* - converged: Boolean indicating whether the method converged
2323
*/
2424

25-
export function newtonRaphson(computeSystem, context, maxIterations = 100, tolerance = 1e-7) {
25+
export function newtonRaphson(assembleMat, context, maxIterations = 100, tolerance = 1e-7) {
2626
let errorNorm = 0;
2727
let converged = false;
2828
let iterations = 0;
@@ -32,18 +32,71 @@ export function newtonRaphson(computeSystem, context, maxIterations = 100, toler
3232
let residualVector = [];
3333
let nodesCoordinates = {};
3434

35+
// Initialize solution and deltaX
36+
for (let i = 0; i < residualVector.length; i++) {
37+
solution[i] = 0;
38+
deltaX[i] = 0;
39+
}
40+
3541
while (iterations <= maxIterations && !converged) {
36-
for (i = 0; i < solution.length; i++) {
37-
solution[i] = solution[i] + deltaX[i]; // solution[i] or solution[i-1]? - Have to check
42+
// Update solution
43+
for (let i = 0; i < solution.length; i++) {
44+
solution[i] = solution[i] + deltaX[i];
45+
}
46+
47+
// Compute Jacobian and Residual matrices
48+
if (assembleMat === "assembleFrontPropagationMat") {
49+
// Pass an additional artificial visous parameter for front propagation
50+
({ jacobianMatrix, residualVector, nodesCoordinates } = assembleMat(
51+
context.meshConfig,
52+
context.boundaryConditions,
53+
context.eikonalViscousTerm
54+
));
55+
} else {
56+
// Standard call for other assembly functions
57+
({ jacobianMatrix, residualVector, nodesCoordinates } = assembleMat(
58+
context.meshConfig,
59+
context.boundaryConditions
60+
));
61+
}
62+
63+
// Solve the linear system based on the specified solver method
64+
basicLog(`Solving system using ${context.solverMethod}...`);
65+
if (context.solverMethod === "jacobi") {
66+
// Use Jacobi method
67+
const initialGuess = new Array(residualVector.length).fill(0);
68+
const jacobiResult = jacobiMethod(jacobianMatrix, residualVector, initialGuess, 1000, 1e-6);
69+
debugLog(
70+
`Used Jacobi solver in Newton-Raphson iteration ${iterations + 1}, converged: ${
71+
jacobiResult.converged
72+
}`
73+
);
74+
} else if (context.solverMethod === "lusolve") {
75+
// Use LU decomposition method
76+
deltaX = math.lusolve(jacobianMatrix, residualVector);
77+
debugLog(`Used LU decomposition solver in Newton-Raphson iteration ${iterations + 1}`);
3878
}
39-
// Compute Residuals and Jacobian. Then solve the linear system
79+
80+
// Check convergence
4081
errorNorm = euclideanNorm(deltaX);
4182
if (errorNorm <= tolerance) {
4283
converged = true;
4384
} else if (errorNorm > 1e5) {
4485
errorLog(`Solution not converged. Error norm: ${errorNorm}`);
4586
break;
4687
}
88+
4789
iterations++;
4890
}
91+
92+
debugLog(`Newton-Raphson ${converged ? "converged" : "did not converge"} in ${iterations} iterations`);
93+
94+
return {
95+
solution,
96+
converged,
97+
iterations,
98+
jacobianMatrix,
99+
residualVector,
100+
nodesCoordinates,
101+
};
49102
}

src/solvers/frontPropagationScript.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { basicLog, debugLog, errorLog } from "../utilities/loggingScript.js";
2323
* - residualVector: The assembled residual vector
2424
* - nodesCoordinates: Object containing x and y coordinates of nodes
2525
*/
26-
export function assembleFrontPropagationMat(meshConfig, boundaryConditions) {
26+
export function assembleFrontPropagationMat(meshConfig, boundaryConditions, eikonalViscousTerm) {
2727
basicLog("Starting front propagation matrix assembly...");
2828

2929
// Extract mesh details from the configuration object

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy