Reaktoro 
A unified framework for modeling chemically reactive systems
Utils.hxx
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 // C++ includes
19 namespace Reaktoro {
20 
21 template<typename NamedValues>
22 auto names(const NamedValues& values) -> std::vector<std::string>
23 {
24  std::vector<std::string> names;
25  names.reserve(values.size());
26  for(const auto& entry : values)
27  names.push_back(entry.name());
28  return names;
29 }
30 
31 template<typename SpeciesValues>
32 auto charges(const SpeciesValues& species) -> Vector
33 {
34  Vector charges(species.size());
35  for(unsigned i = 0; i < species.size(); ++i)
36  charges[i] = species[i].charge();
37  return charges;
38 }
39 
40 template<typename SpeciesValues>
41 auto molarMasses(const SpeciesValues& species) -> Vector
42 {
43  Vector molar_masses(species.size());
44  for(unsigned i = 0; i < species.size(); ++i)
45  molar_masses[i] = species[i].molarMass();
46  return molar_masses;
47 }
48 
49 template<typename Derived>
50 auto molarFractions(const Eigen::MatrixBase<Derived>& n) -> ChemicalVector
51 {
52  const auto nc = composition(n);
53  const unsigned nspecies = n.size();
54  if(nspecies == 1)
55  return ChemicalVector(nspecies, nspecies, 1.0);
56  const ChemicalScalar nt = sum(nc);
57  return (nt != 0.0) ? nc/nt : ChemicalVector(nspecies);
58 }
59 
60 } // namespace Reaktoro
auto names(const NamedValues &values) -> std::vector< std::string >
Return the names of the entries in a container.
Definition: Utils.hxx:22
auto molarMass(const std::map< std::string, double > &elements) -> double
Calculate the molar mass of a chemical compound (in units of kg/mol).
Definition: ElementUtils.cpp:184
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
auto charge(const std::string &formula) -> double
Extract the electrical charge of a chemical formula.
Definition: ElementUtils.cpp:197
auto molarMasses(const SpeciesValues &species) -> Vector
Return the molar masses of all species in a list of species (in units of kg/mol)
Definition: Utils.hxx:41
ChemicalVectorBase< Vector, Vector, Vector, Matrix > ChemicalVector
A type that represents a vector of chemical scalars and their derivatives.
Definition: ChemicalVector.hpp:39
auto charges(const ChargedValues &values) -> Vector
Return the electrical charges of all species in a list of species.
auto composition(const Eigen::MatrixBase< Derived > &n) -> Composition< Derived >
Return a Composition instance for a given MatrixBase instance.
Definition: ChemicalVector.hpp:326
The namespace containing all components of the Reaktoro library.
Definition: ChemicalScalar.hpp:24
A template base class to represent a chemical scalar and its partial derivatives. ...
Definition: ChemicalScalar.hpp:28