Reaktoro 
A unified framework for modeling chemically reactive systems
GeneralMixture.hpp
1 // Reaktoro is a unified framework for modeling chemically reactive systems.
2 //
3 // Copyright (C) 2014-2015 Allan Leal
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 #pragma once
19 
20 // C++ includes
21 #include <string>
22 #include <vector>
23 
24 // Reaktoro includes
25 #include <Reaktoro/Common/Index.hpp>
26 #include <Reaktoro/Common/ChemicalScalar.hpp>
27 #include <Reaktoro/Common/ChemicalVector.hpp>
28 #include <Reaktoro/Common/ScalarTypes.hpp>
29 #include <Reaktoro/Common/SetUtils.hpp>
30 #include <Reaktoro/Common/ThermoScalar.hpp>
31 #include <Reaktoro/Core/Utils.hpp>
32 
33 namespace Reaktoro {
34 
37 {
40 
43 
46 };
47 
49 inline auto operator==(const MixtureState& l, const MixtureState& r) -> bool
50 {
51  return l.T == r.T && r.P == r.P && l.x == r.x;
52 }
53 
56 template<class SpeciesType>
58 {
59 public:
62 
65  explicit GeneralMixture(const std::vector<SpeciesType>& species);
66 
68  virtual ~GeneralMixture();
69 
71  auto setName(std::string name) -> void;
72 
74  auto numSpecies() const -> unsigned;
75 
77  auto name() const -> std::string;
78 
81  auto species() const -> const std::vector<SpeciesType>&;
82 
86  auto species(const Index& index) const -> const SpeciesType&;
87 
91  auto indexSpecies(const std::string& name) const -> Index;
92 
96  auto indexSpeciesAny(const std::vector<std::string>& names) const -> Index;
97 
99  auto namesSpecies() const -> std::vector<std::string>;
100 
102  auto chargesSpecies() const -> Vector;
103 
107  auto molarFractions(const Vector& n) const -> ChemicalVector;
108 
113  auto state(double T, double P, const Vector& n) const -> MixtureState;
114 
115 private:
117  std::string _name;
118 
120  std::vector<SpeciesType> _species;
121 };
122 
123 template<class SpeciesType>
125 {}
126 
127 template<class SpeciesType>
128 GeneralMixture<SpeciesType>::GeneralMixture(const std::vector<SpeciesType>& species)
129 : _species(species)
130 {}
131 
132 template<class SpeciesType>
134 {}
135 
136 template<class SpeciesType>
138 {
139  _name = name;
140 }
141 
142 template<class SpeciesType>
144 {
145  return _species.size();
146 }
147 
148 template<class SpeciesType>
149 auto GeneralMixture<SpeciesType>::name() const -> std::string
150 {
151  return _name;
152 }
153 
154 template<class SpeciesType>
155 auto GeneralMixture<SpeciesType>::species() const -> const std::vector<SpeciesType>&
156 {
157  return _species;
158 }
159 
160 template<class SpeciesType>
161 auto GeneralMixture<SpeciesType>::species(const Index& index) const -> const SpeciesType&
162 {
163  return _species[index];
164 }
165 
166 template<class SpeciesType>
167 auto GeneralMixture<SpeciesType>::indexSpecies(const std::string& name) const -> Index
168 {
169  return index(name, _species);
170 }
171 
172 template<class SpeciesType>
173 auto GeneralMixture<SpeciesType>::indexSpeciesAny(const std::vector<std::string>& names) const -> Index
174 {
175  return indexAny(names, _species);
176 }
177 
178 template<class SpeciesType>
179 auto GeneralMixture<SpeciesType>::namesSpecies() const -> std::vector<std::string>
180 {
181  std::vector<std::string> names(_species.size());
182  for(unsigned i = 0; i < names.size(); ++i)
183  names[i] = _species[i].name();
184  return names;
185 }
186 
187 template<class SpeciesType>
189 {
190  const unsigned nspecies = numSpecies();
191  Vector charges(nspecies);
192  for(unsigned i = 0; i < nspecies; ++i)
193  charges[i] = _species[i].charge();
194  return charges;
195 }
196 
197 template<class SpeciesType>
199 {
200  const unsigned nspecies = numSpecies();
201  if(nspecies == 1)
202  {
203  ChemicalVector x(1);
204  x.val[0] = 1.0;
205  return x;
206  }
207  ChemicalVector x(nspecies);
208  const double nt = n.sum();
209  if(nt == 0.0) return x;
210  x.val = n/nt;
211  for(unsigned i = 0; i < nspecies; ++i)
212  {
213  x.ddn.row(i).fill(-x.val[i]/nt);
214  x.ddn(i, i) += 1.0/nt;
215  }
216  return x;
217 }
218 
219 template<class SpeciesType>
220 auto GeneralMixture<SpeciesType>::state(double T, double P, const Vector& n) const -> MixtureState
221 {
222  MixtureState res;
223  res.T = T;
224  res.P = P;
225  res.x = molarFractions(n);
226  return res;
227 }
228 
229 } // namespace Reaktoro
Provide a base of implementation for the mixture classes.
Definition: GeneralMixture.hpp:57
auto name() const -> std::string
Return the name of the mixture.
Definition: GeneralMixture.hpp:149
auto indexAny(const Names &names, const NamedValues &values) -> Index
Return the index of the first entry in a container of named values with any of the given names...
Definition: SetUtils.hxx:47
auto state(double T, double P, const Vector &n) const -> MixtureState
Calculate the state of the mixture.
Definition: GeneralMixture.hpp:220
auto index(const T &value, const std::vector< T > &values) -> Index
Find the index of a value in a container of values.
Definition: SetUtils.hxx:21
auto names(const NamedValues &values) -> std::vector< std::string >
Return the names of the entries in a container.
Definition: Utils.hxx:22
auto setName(std::string name) -> void
Set the name of the mixture.
Definition: GeneralMixture.hpp:137
STL namespace.
A type used to describe the state of a mixture.
Definition: GeneralMixture.hpp:36
Temperature T
The temperature of the mixture (in units of K)
Definition: GeneralMixture.hpp:39
auto indexSpecies(const std::string &name) const -> Index
Return the index of a species in the mixture.
Definition: GeneralMixture.hpp:167
auto species() const -> const std::vector< SpeciesType > &
Return the species that compose the mixture.
Definition: GeneralMixture.hpp:155
Pressure P
The pressure of the mixture (in units of Pa)
Definition: GeneralMixture.hpp:42
GeneralMixture()
Construct a default GeneralMixture instance.
Definition: GeneralMixture.hpp:124
V val
The vector of chemical scalars.
Definition: ChemicalVector.hpp:48
virtual ~GeneralMixture()
Destroy the instance.
Definition: GeneralMixture.hpp:133
Eigen::VectorXd Vector
Define an alias to the vector type of the Eigen library.
Definition: Matrix.hpp:384
auto molarFractions(const Eigen::MatrixBase< Derived > &n) -> ChemicalVector
Return the molar fractions of the species.
Definition: Utils.hxx:50
ChemicalVector x
The molar fractions of the species in the mixture and their partial derivatives.
Definition: GeneralMixture.hpp:45
A type that describes temperature in units of K.
Definition: ThermoScalar.hpp:177
auto charge(const std::string &formula) -> double
Extract the electrical charge of a chemical formula.
Definition: ElementUtils.cpp:197
auto molarFractions(const Vector &n) const -> ChemicalVector
Calculates the molar fractions of the species and their partial derivatives.
Definition: GeneralMixture.hpp:198
ChemicalVectorBase< Vector, Vector, Vector, Matrix > ChemicalVector
A type that represents a vector of chemical scalars and their derivatives.
Definition: ChemicalVector.hpp:39
N ddn
The matrix of partial mole derivatives of the chemical scalars.
Definition: ChemicalVector.hpp:57
std::size_t Index
Define a type that represents an index.
Definition: Index.hpp:26
auto indexSpeciesAny(const std::vector< std::string > &names) const -> Index
Return the index of the first species in the mixture with any of the given names. ...
Definition: GeneralMixture.hpp:173
auto numSpecies() const -> unsigned
Return the number of species in the mixture.
Definition: GeneralMixture.hpp:143
auto chargesSpecies() const -> Vector
Return the charges of the species in the mixture.
Definition: GeneralMixture.hpp:188
auto charges(const ChargedValues &values) -> Vector
Return the electrical charges of all species in a list of species.
A type that describes pressure in units of Pa.
Definition: ThermoScalar.hpp:194
The namespace containing all components of the Reaktoro library.
Definition: ChemicalScalar.hpp:24
auto namesSpecies() const -> std::vector< std::string >
Return the names of the species in the mixture.
Definition: GeneralMixture.hpp:179