@@ -22,7 +22,7 @@ import { basicLog, debugLog, errorLog } from "./utilities/loggingScript.js";
22
22
* - converged: Boolean indicating whether the method converged
23
23
*/
24
24
25
- export function newtonRaphson ( computeSystem , context , maxIterations = 100 , tolerance = 1e-7 ) {
25
+ export function newtonRaphson ( assembleMat , context , maxIterations = 100 , tolerance = 1e-7 ) {
26
26
let errorNorm = 0 ;
27
27
let converged = false ;
28
28
let iterations = 0 ;
@@ -32,18 +32,71 @@ export function newtonRaphson(computeSystem, context, maxIterations = 100, toler
32
32
let residualVector = [ ] ;
33
33
let nodesCoordinates = { } ;
34
34
35
+ // Initialize solution and deltaX
36
+ for ( let i = 0 ; i < residualVector . length ; i ++ ) {
37
+ solution [ i ] = 0 ;
38
+ deltaX [ i ] = 0 ;
39
+ }
40
+
35
41
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 } ` ) ;
38
78
}
39
- // Compute Residuals and Jacobian. Then solve the linear system
79
+
80
+ // Check convergence
40
81
errorNorm = euclideanNorm ( deltaX ) ;
41
82
if ( errorNorm <= tolerance ) {
42
83
converged = true ;
43
84
} else if ( errorNorm > 1e5 ) {
44
85
errorLog ( `Solution not converged. Error norm: ${ errorNorm } ` ) ;
45
86
break ;
46
87
}
88
+
47
89
iterations ++ ;
48
90
}
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
+ } ;
49
102
}
0 commit comments