Skip to content

Commit 1fcc8a8

Browse files
committed
more testing of kinetic reactions. using ln(c) results in a very poorly conditioned system
1 parent bb61818 commit 1fcc8a8

File tree

3 files changed

+305
-302
lines changed

3 files changed

+305
-302
lines changed

src/common/DirectSystemSolve.hpp

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,59 @@ namespace hpcReact
77
template< typename REAL_TYPE, int N >
88
HPCREACT_HOST_DEVICE void solveNxN_pivoted(REAL_TYPE A[N][N], REAL_TYPE b[N], REAL_TYPE x[N])
99
{
10-
int pivot[N] = {0, 1, 2}; // Row index tracker
11-
for (int i = 0; i < N; i++)
10+
11+
12+
int pivot[N] = {0, 1, 2}; // Row index tracker
13+
for (int i = 0; i < N; i++)
14+
{
15+
pivot[i] = i;
16+
}
17+
18+
// **Step 1: Forward Elimination with Pivoting**
19+
for (int k = 0; k < N-1; k++)
20+
{
21+
// **Find Pivot Row**
22+
int max_row = k;
23+
REAL_TYPE max_val = abs(A[pivot[k]][k]);
24+
for (int i = k + 1; i < N; i++)
1225
{
13-
pivot[i] = i;
26+
if (fabs(A[pivot[i]][k]) > max_val)
27+
{
28+
max_val = fabs(A[pivot[i]][k]);
29+
max_row = i;
30+
}
1431
}
1532

16-
// **Step 1: Forward Elimination with Pivoting**
17-
for (int k = 0; k < N-1; k++)
33+
// **Swap Rows in Pivot Array**
34+
if (max_row != k)
1835
{
19-
// **Find Pivot Row**
20-
int max_row = k;
21-
REAL_TYPE max_val = abs(A[pivot[k]][k]);
22-
for (int i = k + 1; i < N; i++)
23-
{
24-
if (fabs(A[pivot[i]][k]) > max_val)
25-
{
26-
max_val = fabs(A[pivot[i]][k]);
27-
max_row = i;
28-
}
29-
}
30-
31-
// **Swap Rows in Pivot Array**
32-
if (max_row != k)
33-
{
34-
int temp = pivot[k];
35-
pivot[k] = pivot[max_row];
36-
pivot[max_row] = temp;
37-
}
38-
39-
// **Gaussian Elimination**
40-
for (int i = k + 1; i < N; i++)
41-
{
42-
REAL_TYPE factor = A[pivot[i]][k] / A[pivot[k]][k];
43-
for (int j = k; j < N; j++)
44-
{
45-
A[pivot[i]][j] -= factor * A[pivot[k]][j];
46-
}
47-
b[pivot[i]] -= factor * b[pivot[k]];
48-
}
36+
int temp = pivot[k];
37+
pivot[k] = pivot[max_row];
38+
pivot[max_row] = temp;
4939
}
5040

51-
// **Step 2: Back-Substitution**
52-
for (int i = N - 1; i >= 0; --i) {
53-
x[i] = b[pivot[i]];
54-
for (int j = i + 1; j < N; j++) {
55-
x[i] -= A[pivot[i]][j] * x[j];
56-
}
57-
x[i] /= A[pivot[i]][i]; // Normalize
58-
}
41+
// **Gaussian Elimination**
42+
for (int i = k + 1; i < N; i++)
43+
{
44+
REAL_TYPE factor = A[pivot[i]][k] / A[pivot[k]][k];
45+
for (int j = k; j < N; j++)
46+
{
47+
A[pivot[i]][j] -= factor * A[pivot[k]][j];
48+
}
49+
b[pivot[i]] -= factor * b[pivot[k]];
50+
}
51+
}
52+
53+
// **Step 2: Back-Substitution**
54+
for (int i = N - 1; i >= 0; --i)
55+
{
56+
x[i] = b[pivot[i]];
57+
for (int j = i + 1; j < N; j++)
58+
{
59+
x[i] -= A[pivot[i]][j] * x[j];
60+
}
61+
x[i] /= A[pivot[i]][i]; // Normalize
62+
}
5963

6064
}
6165

src/reactions/bulkGeneric/KineticReactions_impl.hpp

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,13 @@ HPCREACT_HOST_DEVICE inline void
256256
KineticReactions< REAL_TYPE,
257257
INT_TYPE,
258258
INDEX_TYPE,
259-
LOGE_CONCENTRATION
260-
>::timeStep(
261-
RealType const dt,
262-
RealType const & temperature,
263-
PARAMS_DATA const & params,
264-
ARRAY_1D_TO_CONST const & speciesConcentration_n,
265-
ARRAY_1D & speciesConcentration,
266-
ARRAY_1D & speciesRates,
267-
ARRAY_2D & speciesRatesDerivatives )
259+
LOGE_CONCENTRATION >::timeStep( RealType const dt,
260+
RealType const & temperature,
261+
PARAMS_DATA const & params,
262+
ARRAY_1D_TO_CONST const & speciesConcentration_n,
263+
ARRAY_1D & speciesConcentration,
264+
ARRAY_1D & speciesRates,
265+
ARRAY_2D & speciesRatesDerivatives )
268266
{
269267
// constexpr int numReactions = PARAMS_DATA::numReactions;
270268
constexpr int numSpecies = PARAMS_DATA::numSpecies;
@@ -285,32 +283,82 @@ KineticReactions< REAL_TYPE,
285283

286284
double residual[numSpecies] = { 0.0 };
287285
double deltaPrimarySpeciesConcentration[numSpecies] = { 0.0 };
286+
287+
// form residual and Jacobian
288288
for( int i = 0; i < numSpecies; ++i )
289289
{
290-
residual[i] = -(speciesConcentration[i] - speciesConcentration_n[i] - dt * speciesRates[i]);
290+
291+
292+
RealType nonLogC;
293+
RealType nonLogC_n;
294+
if constexpr( LOGE_CONCENTRATION )
295+
{
296+
nonLogC = exp( speciesConcentration[i] );
297+
nonLogC_n = exp( speciesConcentration_n[i] );
298+
}
299+
else
300+
{
301+
nonLogC = speciesConcentration[i];
302+
nonLogC_n = speciesConcentration_n[i];
303+
}
304+
residual[i] = -(nonLogC - nonLogC_n - dt * speciesRates[i]);
305+
306+
291307
for( int j = 0; j < numSpecies; ++j )
292308
{
293309
speciesRatesDerivatives( i, j ) = - dt * speciesRatesDerivatives( i, j );
294310
}
295-
speciesRatesDerivatives( i, i ) += 1.0;
311+
if constexpr( LOGE_CONCENTRATION )
312+
{
313+
speciesRatesDerivatives( i, i ) += nonLogC;
314+
}
315+
else
316+
{
317+
speciesRatesDerivatives( i, i ) += 1.0;
318+
}
296319
}
297320

321+
322+
298323
residualNorm = 0.0;
299324
for( int j = 0; j < numSpecies; ++j )
300325
{
301326
residualNorm += residual[j] * residual[j];
302327
}
303328
residualNorm = sqrt( residualNorm );
304-
if( residualNorm < 1.0e-8 )
329+
if( residualNorm < 1.0e-14 )
305330
{
306331
break;
307332
}
308333

334+
335+
// printf( "residual = { ");
336+
// for( int i = 0; i < numSpecies; ++i )
337+
// {
338+
// printf( " %g, ", residual[i] );
339+
// }
340+
// printf( "}\n" );
341+
342+
// printf( "Jacobian = { \n" );
343+
// for( int i = 0; i < numSpecies; ++i )
344+
// {
345+
// printf( " { ");
346+
// for( int j = 0; j < numSpecies; ++j )
347+
// {
348+
// printf( " %g ", speciesRatesDerivatives( i, j ) );
349+
// // printf( " %g ", speciesRatesDerivatives( i, j ) / exp(speciesConcentration[j]) );
350+
// if( j < numSpecies-1 ) { printf( ", " ); }
351+
// }
352+
// printf( "}, \n" );
353+
// }
354+
// printf( "}\n" );
355+
309356
solveNxN_pivoted<double,numSpecies>( speciesRatesDerivatives.data, residual, deltaPrimarySpeciesConcentration );
310357

311358
for( int i = 0; i < numSpecies; ++i )
312359
{
313-
speciesConcentration[i] += deltaPrimarySpeciesConcentration[i];
360+
// printf( "species %2d: concentration = %e, residual = %e, delta = %e \n", i, speciesConcentration[i], residual[i], deltaPrimarySpeciesConcentration[i] );
361+
speciesConcentration[i] = speciesConcentration[i] + deltaPrimarySpeciesConcentration[i];
314362
}
315363

316364
}

0 commit comments

Comments
 (0)