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-2018 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 // 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 
108 
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>
137 auto GeneralMixture<SpeciesType>::setName(std::string name) -> void
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>
221 {
222  MixtureState res;
223  res.T = T;
224  res.P = P;
225  res.x = moleFractions(n);
226  return res;
227 }
228 
229 } // namespace Reaktoro
Provide a base of implementation for the mixture classes.
Definition: GeneralMixture.hpp:58
auto state(Temperature T, Pressure P, VectorConstRef 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 name() const -> std::string
Return the name of the mixture.
Definition: GeneralMixture.hpp:149
ChemicalVector x
The mole fractions of the species in the mixture and their partial derivatives.
Definition: GeneralMixture.hpp:45
A type that describes pressure in units of Pa.
Definition: ThermoScalar.hpp:194
Temperature T
The temperature of the mixture (in units of K)
Definition: GeneralMixture.hpp:39
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 species() const -> const std::vector< SpeciesType > &
Return the species that compose the mixture.
Definition: GeneralMixture.hpp:155
GeneralMixture(const std::vector< SpeciesType > &species)
Construct a GeneralMixture instance with given species.
Definition: GeneralMixture.hpp:128
auto namesSpecies() const -> std::vector< std::string >
Return the names of the species in the mixture.
Definition: GeneralMixture.hpp:179
auto charge(const std::string &formula) -> double
Extract the electrical charge of a chemical formula.
Definition: ElementUtils.cpp:197
The namespace containing all components of the Reaktoro library.
Definition: ChemicalScalar.hpp:24
auto numSpecies() const -> unsigned
Return the number of species in the mixture.
Definition: GeneralMixture.hpp:143
A type that describes temperature in units of K.
Definition: ThermoScalar.hpp:177
N ddn
The matrix of partial mole derivatives of the chemical scalars.
Definition: ChemicalVector.hpp:63
auto moleFractions(Composition n) -> ChemicalVector
Return the mole fractions of the species.
Definition: Utils.hpp:45
A type used to describe the state of a mixture.
Definition: GeneralMixture.hpp:37
auto setName(std::string name) -> void
Set the name of the mixture.
Definition: GeneralMixture.hpp:137
auto moleFractions(VectorConstRef n) const -> ChemicalVector
Calculates the mole fractions of the species and their partial derivatives.
Definition: GeneralMixture.hpp:198
std::size_t Index
Define a type that represents an index.
Definition: Index.hpp:26
V val
The vector of chemical scalars.
Definition: ChemicalVector.hpp:54
Pressure P
The pressure of the mixture (in units of Pa)
Definition: GeneralMixture.hpp:42
auto charges(const ChargedValues &values) -> Vector
Return the electrical charges of all species in a list of species.
auto chargesSpecies() const -> Vector
Return the charges of the species in the mixture.
Definition: GeneralMixture.hpp:188
GeneralMixture()
Construct a default GeneralMixture instance.
Definition: GeneralMixture.hpp:124
Eigen::Ref< const Eigen::VectorXd > VectorConstRef
< Alias to Eigen type Ref<VectorXd>.
Definition: Matrix.hpp:31
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 indexSpecies(const std::string &name) const -> Index
Return the index of a species in the mixture.
Definition: GeneralMixture.hpp:167
virtual ~GeneralMixture()
Destroy the instance.
Definition: GeneralMixture.hpp:133