Skip to content

Commit 0b49757

Browse files
committed
added symmetric matrix type for the equilibrium enforcement
1 parent 6538fb0 commit 0b49757

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/common/DirectSystemSolve.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "macros.hpp"
4+
#include "symmetricMatrix.hpp"
45
#include <cmath>
56

67
namespace hpcReact
@@ -75,6 +76,47 @@ void solveNxN_Cholesky(REAL_TYPE const (&A)[N][N], REAL_TYPE const (&b)[N], REAL
7576
}
7677

7778

79+
80+
template< typename REAL_TYPE, int N >
81+
void solveNxN_Cholesky( symmetricMatrix<REAL_TYPE,int,N> const & A,
82+
REAL_TYPE const (&b)[N],
83+
REAL_TYPE (&x)[N])
84+
{
85+
symmetricMatrix<REAL_TYPE,int,N> L = {0};
86+
87+
// **Cholesky Decomposition**
88+
for (int i = 0; i < N; i++) {
89+
for (int j = 0; j <= i; j++) {
90+
REAL_TYPE sum = 0;
91+
for (int k = 0; k < j; k++) {
92+
sum += L(i,k) * L(j,k);
93+
}
94+
if (i == j)
95+
L(i,j) = sqrt(A(i,i) - sum);
96+
else
97+
L(i,j) = (A(i,j) - sum) / L[j][j];
98+
}
99+
}
100+
101+
// **Forward Substitution: Solve L y = b**
102+
REAL_TYPE y[N];
103+
for (int i = 0; i < N; i++) {
104+
y[i] = b[i];
105+
for (int j = 0; j < i; j++)
106+
y[i] -= L(i,j) * y[j];
107+
y[i] /= L(i,i);
108+
}
109+
110+
// **Backward Substitution: Solve L^T x = y**
111+
for (int i = N - 1; i >= 0; i--) {
112+
x[i] = y[i];
113+
for (int j = i + 1; j < N; j++)
114+
x[i] -= L(j,i) * x[j];
115+
x[i] /= L(i,i);
116+
}
117+
}
118+
119+
78120
template< typename REAL_TYPE, int N >
79121
HPCREACT_HOST_DEVICE
80122
void solveNxN_pivoted( REAL_TYPE (&A)[N][N], REAL_TYPE (&b)[N], REAL_TYPE (&x)[N] )

0 commit comments

Comments
 (0)