Reaktoro  v2.11.0
A unified framework for modeling chemically reactive systems
MoleFractionUtils.hpp
1 // Reaktoro is a unified framework for modeling chemically reactive systems.
2 //
3 // Copyright © 2014-2024 Allan Leal
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library. If not, see <http://www.gnu.org/licenses/>.
17 
18 #pragma once
19 
20 // Reaktoro includes
21 #include <Reaktoro/Common/TraitsUtils.hpp>
22 #include <Reaktoro/Common/Matrix.hpp>
23 
24 namespace Reaktoro {
25 
29 template<typename ArrayConstRef, typename ArrayRef>
30 auto moleFractions(ArrayConstRef&& n, ArrayRef&& x)
31 {
32  const auto nspecies = n.size();
33  if(nspecies == 1) {
34  x.fill(1.0);
35  return;
36  }
37  const auto nsum = n.sum();
38  if(nsum != 0.0) x = n/nsum;
39  else x.fill(0.0);
40 }
41 
44 template<typename ArrayConstRef>
45 auto moleFractions(ArrayConstRef&& n)
46 {
47  using T = Decay<decltype(n[0])>;
48  const auto N = n.size();
49  ArrayX<T> x(N);
50  moleFractions(n, x);
51  return x;
52 }
53 
57 template<typename ArrayConstRef, typename MatrixRef>
58 auto moleFractionsJacobian(ArrayConstRef&& n, MatrixRef&& J)
59 {
60  //-----------------------------------------------------------------------------------------------------
61  // == LATEX ==
62  //-----------------------------------------------------------------------------------------------------
63  // \frac{\partial x_{i}}{\partial n_{j}}=\frac{x_{i}}{n_{i}}\left(\delta_{ij}-x_{i}\right)=\frac{1}{n_{\Sigma}}\left(\delta_{ij}-x_{i}\right)
64  //-----------------------------------------------------------------------------------------------------
65  const auto N = n.size();
66  assert(J.rows() == N);
67  assert(J.cols() == N);
68  const auto nsum = n.sum();
69  if(nsum == 0.0) J.fill(0.0);
70  else for(auto i = 0; i < N; ++i) {
71  const auto ni = n[i];
72  const auto xi = ni/nsum;
73  const auto aux = -xi/nsum;
74  for(auto j = 0; j < N; ++j)
75  J(i, j) = aux; // J(i, j) = -xi/nsum
76  J(i, i) = (1 - xi)/nsum; // J(i, i) = (1 - xi)/nsum
77  }
78 }
79 
82 template<typename ArrayConstRef>
83 auto moleFractionsJacobian(ArrayConstRef&& n)
84 {
85  using T = Decay<decltype(n[0])>;
86  const auto N = n.size();
87  MatrixX<T> J(N, N);
89  return J;
90 }
91 
95 template<typename ArrayConstRef, typename MatrixRef>
96 auto lnMoleFractionsJacobian(ArrayConstRef&& n, MatrixRef&& J) -> void
97 {
98  //-----------------------------------------------------------------------------------------------------
99  // == LATEX ==
100  //-----------------------------------------------------------------------------------------------------
101  // \frac{\partial\ln x_{i}}{\partial n_{j}}=\frac{\delta_{ij}}{n_{i}}-\frac{1}{n_{\Sigma}}=\frac{1}{n_{i}}\left(\delta_{ij}-x_{i}\right)
102  //-----------------------------------------------------------------------------------------------------
103  using T = Decay<decltype(J(0, 0))>;
104  const auto N = n.size();
105  assert(J.rows() == N);
106  assert(J.cols() == N);
107  const auto nsum = n.sum();
108  if(nsum == 0.0) J.fill(0.0);
109  else for(auto i = 0; i < N; ++i) {
110  const auto ni = n[i];
111  const auto xi = ni/nsum;
112  const auto aux = -xi/ni;
113  for(auto j = 0; j < N; ++j)
114  J(i, j) = static_cast<T>(aux); // J(i, j) = -xi/ni
115  J(i, i) = static_cast<T>((1 - xi)/ni); // J(i, i) = (1 - xi)/ni
116  }
117 }
118 
121 template<typename ArrayConstRef>
122 auto lnMoleFractionsJacobian(ArrayConstRef&& n)
123 {
124  using T = Decay<decltype(n[0])>;
125  const auto N = n.size();
126  MatrixX<T> J(N, N);
128  return J;
129 }
130 
134 template<typename ArrayConstRef, typename MatrixRef>
135 auto lnMoleFractionsJacobianDiagonal(ArrayConstRef&& n, MatrixRef&& D) -> void
136 {
137  //-----------------------------------------------------------------------------------------------------
138  // == LATEX ==
139  //-----------------------------------------------------------------------------------------------------
140  // \frac{\partial\ln x_{i}}{\partial n_{i}}=\frac{1}{n_{i}}-\frac{1}{n_{\Sigma}}
141  //-----------------------------------------------------------------------------------------------------
142  using T = Decay<decltype(D[0])>;
143  const auto N = n.size();
144  assert(D.rows() == N);
145  const auto nsum = n.sum();
146  if(nsum == 0.0) {
147  D.fill(0.0);
148  return;
149  }
150  const auto nsuminv = 1.0/nsum;
151  for(auto i = 0; i < N; ++i)
152  D[i] = static_cast<T>(1.0/n[i] - nsuminv);
153 }
154 
157 template<typename ArrayConstRef>
158 auto lnMoleFractionsJacobianDiagonal(ArrayConstRef&& n)
159 {
160  using T = Decay<decltype(n[0])>;
161  const auto N = n.size();
162  ArrayX<T> D(N);
164  return D;
165 }
166 
167 } // namespace Reaktoro
The namespace containing all components of the Reaktoro library.
Definition: Algorithms.hpp:29
auto moleFractions(ArrayConstRef &&n, ArrayRef &&x)
Compute the mole fractions of the species with given species amounts.
Definition: MoleFractionUtils.hpp:30
Eigen::Matrix< T, -1, -1, 0, -1, -1 > MatrixX
Convenient alias to Eigen type.
Definition: Matrix.hpp:42
auto lnMoleFractionsJacobianDiagonal(ArrayConstRef &&n, MatrixRef &&D) -> void
Compute the diagonal only of the Jacobian matrix of the species mole fractions in natural log ( ).
Definition: MoleFractionUtils.hpp:135
auto moleFractionsJacobian(ArrayConstRef &&n, MatrixRef &&J)
Compute the Jacobian matrix of the species mole fractions ( ).
Definition: MoleFractionUtils.hpp:58
Eigen::Array< T, -1, 1, 0, -1, 1 > ArrayX
Convenient alias to Eigen type.
Definition: Matrix.hpp:46
auto lnMoleFractionsJacobian(ArrayConstRef &&n, MatrixRef &&J) -> void
Compute the Jacobian matrix of the species mole fractions in natural log ( ).
Definition: MoleFractionUtils.hpp:96