@@ -36,7 +36,75 @@ class EquilibriumReactions
3636 ARRAY_1D_TO_CONST const & speciesConcentration0,
3737 ARRAY_1D_TO_CONST2 const & xi,
3838 ARRAY_1D & residual,
39- ARRAY_2D & jacobian );
39+ ARRAY_2D & jacobian )
40+ {
41+ HPCREACT_UNUSED_VAR ( temperature );
42+ constexpr int numSpecies = PARAMS_DATA::numSpecies;
43+ constexpr int numReactions = PARAMS_DATA::numReactions;
44+
45+ // initialize the species concentration
46+ RealType speciesConcentration[numSpecies];
47+ for ( IndexType i=0 ; i<numSpecies; ++i )
48+ {
49+ speciesConcentration[i] = speciesConcentration0[i];
50+ for ( IndexType r=0 ; r<numReactions; ++r )
51+ {
52+ speciesConcentration[i] += params.stoichiometricMatrix ( r, i ) * xi[r];
53+ }
54+ }
55+
56+ // loop over reactions
57+ for ( IndexType a=0 ; a<numReactions; ++a )
58+ {
59+ // get the equilibrium constant for this reaction
60+ RealType const Keq = params.equilibriumConstant ( a );
61+
62+ RealType forwardProduct = 1.0 ;
63+ RealType reverseProduct = 1.0 ;
64+ RealType dForwardProduct_dxi[numReactions] = {0.0 };
65+ RealType dReverseProduct_dxi[numReactions] = {0.0 };
66+ // loop over species
67+ for ( IndexType i=0 ; i<numSpecies; ++i )
68+ {
69+ RealType const s_ai = params.stoichiometricMatrix ( a, i );
70+
71+ if ( s_ai < 0.0 )
72+ {
73+ // forward reaction
74+ forwardProduct *= pow ( speciesConcentration[i], -s_ai );
75+ // derivative of forward product with respect to xi
76+ for ( IndexType b=0 ; b<numReactions; ++b )
77+ {
78+ dForwardProduct_dxi[b] += -s_ai / speciesConcentration[i] * params.stoichiometricMatrix ( b, i );
79+ }
80+ }
81+ else if ( s_ai > 0.0 )
82+ {
83+ // reverse reaction
84+ reverseProduct *= pow ( speciesConcentration[i], s_ai );
85+ // derivative of reverse product with respect to xi
86+ for ( IndexType b=0 ; b<numReactions; ++b )
87+ {
88+ dReverseProduct_dxi[b] += s_ai / speciesConcentration[i] * params.stoichiometricMatrix ( b, i );
89+ }
90+ }
91+ }
92+ // compute the residual for this reaction
93+ residual[a] = forwardProduct - Keq * reverseProduct;
94+ // printf( "residual[%d] = %g - %g * %g = %g\n", a, forwardProduct, Keq, reverseProduct, residual[a] );
95+
96+ // Finish the derivatives of the product terms with respect to xi
97+ for ( IndexType b=0 ; b<numReactions; ++b )
98+ {
99+ dForwardProduct_dxi[b] *= forwardProduct;
100+ dReverseProduct_dxi[b] *= reverseProduct;
101+ // printf( "(%d) dForwardProduct_dxi[%d] = %f\n", a, b, dForwardProduct_dxi[b] );
102+ // printf( "(%d) dReverseProduct_dxi[%d] = %f\n", a, b, dReverseProduct_dxi[b] );
103+ // printf( "Keq = %f\n", Keq );
104+ jacobian ( a, b ) = dForwardProduct_dxi[b] - Keq * dReverseProduct_dxi[b];
105+ }
106+ }
107+ }
40108
41109
42110 template < typename PARAMS_DATA,
@@ -47,13 +115,58 @@ class EquilibriumReactions
47115 enforceEquilibrium ( RealType const & temperature,
48116 PARAMS_DATA const & params,
49117 ARRAY_1D_TO_CONST const & speciesConcentration0,
50- ARRAY_1D & speciesConcentration );
118+ ARRAY_1D & speciesConcentration )
119+ {
120+ HPCREACT_UNUSED_VAR ( temperature );
121+ constexpr int numSpecies = PARAMS_DATA::numSpecies;
122+ constexpr int numReactions = PARAMS_DATA::numReactions;
123+ double residual[numReactions] = { 0.0 };
124+ double xi[numReactions] = { 0.0 };
125+ double dxi[numReactions] = { 0.0 };
126+ CArrayWrapper< double , numReactions, numReactions > jacobian;
127+
128+ REAL_TYPE residualNorm = 0.0 ;
129+ for ( int k=0 ; k<10 ; ++k )
130+ {
131+ computeResidualAndJacobian ( temperature,
132+ params,
133+ speciesConcentration0,
134+ xi,
135+ residual,
136+ jacobian );
137+
138+ residualNorm = 0.0 ;
139+ for ( int j = 0 ; j < numReactions; ++j )
140+ {
141+ residualNorm += residual[j] * residual[j];
142+ }
143+ residualNorm = sqrt ( residualNorm );
144+ if ( residualNorm < 1.0e-14 )
145+ {
146+ break ;
147+ }
148+
149+ solveNxN_pivoted< double , numReactions >( jacobian.data , residual, dxi );
150+
151+ // solve for the change in xi
152+ for ( IndexType r=0 ; r<numReactions; ++r )
153+ {
154+ xi[r] -= dxi[r];
155+ }
156+ }
157+
158+ for ( IndexType i=0 ; i<numSpecies; ++i )
159+ {
160+ speciesConcentration[i] = speciesConcentration0[i];
161+ for ( IndexType r=0 ; r<numReactions; ++r )
162+ {
163+ speciesConcentration[i] += params.stoichiometricMatrix ( r, i ) * xi[r];
164+ }
165+ }
166+ }
51167
52168};
53169
54170
55171} // namespace bulkGeneric
56172} // namespace hpcReact
57-
58- #include " EquilibriumReactions_impl.hpp"
59- #include " common/macrosCleanup.hpp"
0 commit comments