Skip to content

Commit 6538fb0

Browse files
committed
fixed equilibrium solution method to avoid negative concentrations
1 parent 4efb580 commit 6538fb0

File tree

7 files changed

+866
-306
lines changed

7 files changed

+866
-306
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ target_include_directories( hpcReact
3737
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/src>
3838
$<INSTALL_INTERFACE:include> )
3939

40+
blt_print_target_properties( TARGET hpcReact )
4041
# install( FILES ${hpcReact_headers}
4142
# DESTINATION include )
4243

src/common/DirectSystemSolve.hpp

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,79 @@
55

66
namespace hpcReact
77
{
8+
9+
template< typename REAL_TYPE, int N >
10+
bool isPositiveDefinite( REAL_TYPE const (&A)[N][N])
11+
{
12+
REAL_TYPE temp[N][N];
13+
14+
// Copy A into temp to avoid modifying original
15+
for (int i = 0; i < N; i++)
16+
{
17+
for (int j = 0; j < N; j++)
18+
{
19+
temp[i][j] = A[i][j];
20+
}
21+
}
22+
23+
for (int k = 0; k < N; k++)
24+
{
25+
// Compute determinant of k-th leading principal minor using Gaussian elimination
26+
if (temp[k][k] <= 0) return false; // Must be positive
27+
28+
for (int i = k + 1; i < N; i++)
29+
{
30+
REAL_TYPE factor = temp[i][k] / temp[k][k];
31+
for (int j = k; j < N; j++)
32+
{
33+
temp[i][j] -= factor * temp[k][j];
34+
}
35+
}
36+
}
37+
return true;
38+
}
39+
40+
41+
template< typename REAL_TYPE, int N >
42+
void solveNxN_Cholesky(REAL_TYPE const (&A)[N][N], REAL_TYPE const (&b)[N], REAL_TYPE (&x)[N]) {
43+
REAL_TYPE L[N][N] = {{0}};
44+
45+
// **Cholesky Decomposition**
46+
for (int i = 0; i < N; i++) {
47+
for (int j = 0; j <= i; j++) {
48+
REAL_TYPE sum = 0;
49+
for (int k = 0; k < j; k++) {
50+
sum += L[i][k] * L[j][k];
51+
}
52+
if (i == j)
53+
L[i][j] = sqrt(A[i][i] - sum);
54+
else
55+
L[i][j] = (A[i][j] - sum) / L[j][j];
56+
}
57+
}
58+
59+
// **Forward Substitution: Solve L y = b**
60+
REAL_TYPE y[N];
61+
for (int i = 0; i < N; i++) {
62+
y[i] = b[i];
63+
for (int j = 0; j < i; j++)
64+
y[i] -= L[i][j] * y[j];
65+
y[i] /= L[i][i];
66+
}
67+
68+
// **Backward Substitution: Solve L^T x = y**
69+
for (int i = N - 1; i >= 0; i--) {
70+
x[i] = y[i];
71+
for (int j = i + 1; j < N; j++)
72+
x[i] -= L[j][i] * x[j];
73+
x[i] /= L[i][i];
74+
}
75+
}
76+
77+
878
template< typename REAL_TYPE, int N >
979
HPCREACT_HOST_DEVICE
10-
void solveNxN_pivoted( REAL_TYPE A[N][N], REAL_TYPE b[N], REAL_TYPE x[N] )
80+
void solveNxN_pivoted( REAL_TYPE (&A)[N][N], REAL_TYPE (&b)[N], REAL_TYPE (&x)[N] )
1181
{
1282

1383

0 commit comments

Comments
 (0)