Reaktoro  v2.11.0
A unified framework for modeling chemically reactive systems
ChemicalState.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/Matrix.hpp>
22 #include <Reaktoro/Common/Types.hpp>
23 #include <Reaktoro/Core/ChemicalProps.hpp>
24 #include <Reaktoro/Core/ChemicalSystem.hpp>
25 
26 // Forward declarations (Optima)
27 namespace Optima { class State; }
28 
29 namespace Reaktoro {
30 
31 //=================================================================================================
32 //
33 // ChemicalState
34 //
35 //=================================================================================================
36 
41 {
42 public:
43  // Forward declarations
44  class Equilibrium;
45 
50 
53 
55  virtual ~ChemicalState();
56 
59 
60  // --------------------------------------------------------------------------------------------
61  // METHODS FOR SETTING/GETTING TEMPERATURE
62  // --------------------------------------------------------------------------------------------
63 
66  auto setTemperature(real const& value) -> void;
67 
71  auto setTemperature(real value, Chars unit) -> void;
72 
76  auto temperature(real const& value) -> void;
77 
82  auto temperature(real value, Chars unit) -> void;
83 
85  auto temperature() const -> real;
86 
87  // --------------------------------------------------------------------------------------------
88  // METHODS FOR SETTING/GETTING PRESSURE
89  // --------------------------------------------------------------------------------------------
90 
93  auto setPressure(real const& value) -> void;
94 
98  auto setPressure(real value, Chars unit) -> void;
99 
103  auto pressure(real const& value) -> void;
104 
109  auto pressure(real value, Chars unit) -> void;
110 
112  auto pressure() const -> real;
113 
114  // --------------------------------------------------------------------------------------------
115  // METHODS FOR SETTING THE AMOUNT OR MASS OF SPECIES
116  // --------------------------------------------------------------------------------------------
117 
119  auto setSpeciesAmounts(real const& value) -> void;
120 
122  auto setSpeciesAmounts(ArrayXrConstRef const& n) -> void;
123 
125  auto setSpeciesAmounts(ArrayXdConstRef const& n) -> void;
126 
130  auto setSpeciesAmount(Index ispecies, real const& amount) -> void;
131 
136  auto setSpeciesAmount(StringOrIndex const& species, real amount, Chars unit) -> void;
137 
142  auto setSpeciesMass(StringOrIndex const& species, real mass, Chars unit) -> void;
143 
149  auto set(StringOrIndex const& species, real value, Chars unit) -> void;
150 
156  auto add(StringOrIndex const& species, real value, Chars unit) -> void;
157 
158  // --------------------------------------------------------------------------------------------
159  // METHODS FOR GETTING THE AMOUNT OR MASS OF SPECIES, ELEMENTS, AND CHARGE
160  // --------------------------------------------------------------------------------------------
161 
164 
168 
171  auto speciesAmount(StringOrIndex const& species) const -> real;
172 
175  auto speciesMass(StringOrIndex const& species) const -> real;
176 
178  auto componentAmounts() const -> ArrayXr;
179 
181  auto elementAmounts() const -> ArrayXr;
182 
184  auto charge() const -> real;
185 
186  // --------------------------------------------------------------------------------------------
187  // METHODS TO SCALE THE AMOUNTS OF SPECIES IN THE SYSTEM OR PART OF IT
188  // --------------------------------------------------------------------------------------------
189 
192  auto scaleSpeciesAmounts(real const& scalar) -> void;
193 
197  auto scaleSpeciesAmounts(real const& scalar, Indices const& indices) -> void;
198 
202  auto scaleSpeciesAmountsInPhase(StringOrIndex const& phase, real const& scalar) -> void;
203 
204  // --------------------------------------------------------------------------------------------
205  // METHODS TO SCALE THE AMOUNT OF THE SYSTEM OR PART OF IT
206  // --------------------------------------------------------------------------------------------
207 
211  auto scaleAmount(real amount, Chars unit) -> void;
212 
217  auto scalePhaseAmount(StringOrIndex const& phase, real amount, Chars unit) -> void;
218 
222  auto scaleFluidAmount(real amount, Chars unit) -> void;
223 
227  auto scaleSolidAmount(real amount, Chars unit) -> void;
228 
229  // --------------------------------------------------------------------------------------------
230  // METHODS TO SCALE THE MASS OF THE SYSTEM OR PART OF IT
231  // --------------------------------------------------------------------------------------------
232 
236  auto scaleMass(real mass, Chars unit) -> void;
237 
242  auto scalePhaseMass(StringOrIndex const& phase, real mass, Chars unit) -> void;
243 
247  auto scaleFluidMass(real mass, Chars unit) -> void;
248 
252  auto scaleSolidMass(real mass, Chars unit) -> void;
253 
254  // --------------------------------------------------------------------------------------------
255  // METHODS TO SCALE THE VOLUME OF THE SYSTEM OR PART OF IT
256  // --------------------------------------------------------------------------------------------
257 
261  auto scaleVolume(real volume, Chars unit) -> void;
262 
267  auto scalePhaseVolume(StringOrIndex const& phase, real volume, Chars unit) -> void;
268 
272  auto scaleFluidVolume(real volume, Chars unit) -> void;
273 
277  auto scaleSolidVolume(real volume, Chars unit) -> void;
278 
279  // --------------------------------------------------------------------------------------------
280  // METHODS FOR UPDATING CHEMICAL STATE AND ITS PROPERTIES
281  // --------------------------------------------------------------------------------------------
282 
287  auto update(real const& T, real const& P, ArrayXrConstRef const& n) -> void;
288 
293  auto updateIdeal(real const& T, real const& P, ArrayXrConstRef const& n) -> void;
294 
295  // --------------------------------------------------------------------------------------------
296  // MISCELLANEOUS METHODS
297  // --------------------------------------------------------------------------------------------
298 
300  auto system() const -> ChemicalSystem const&;
301 
306  auto props() const -> ChemicalProps const&;
307 
312  auto props() -> ChemicalProps&;
313 
315  auto equilibrium() const -> Equilibrium const&;
316 
319 
321  auto output(std::ostream& out) const -> void;
322 
324  auto output(String const& filename) const -> void;
325 
326 private:
327  struct Impl;
328 
329  Ptr<Impl> pimpl;
330 };
331 
332 //=================================================================================================
333 //
334 // ChemicalState::Equilibrium
335 //
336 //=================================================================================================
337 
340 {
341 public:
344 
346  Equilibrium(Equilibrium const& other);
347 
349  virtual ~Equilibrium();
350 
353 
357  auto reset() -> void;
358 
360  auto setNamesInputVariables(Strings const& wnames) -> void;
361 
363  auto setNamesControlVariablesP(Strings const& pnames) -> void;
364 
366  auto setNamesControlVariablesQ(Strings const& qnames) -> void;
367 
369  auto setInputVariables(ArrayXdConstRef const& w) -> void;
370 
372  auto setControlVariablesP(ArrayXdConstRef const& p) -> void;
373 
375  auto setControlVariablesQ(ArrayXdConstRef const& q) -> void;
376 
379 
381  auto setOptimaState(Optima::State const& state) -> void;
382 
384  auto empty() const -> bool;
385 
387  auto numPrimarySpecies() const -> Index;
388 
390  auto numSecondarySpecies() const -> Index;
391 
393  auto indicesPrimarySpecies() const -> ArrayXlConstRef;
394 
396  auto indicesSecondarySpecies() const -> ArrayXlConstRef;
397 
399  auto elementChemicalPotentials() const -> ArrayXdConstRef;
400 
406  auto speciesStabilities() const -> ArrayXdConstRef;
407 
410  auto explicitTitrantAmount(String const& name) const -> real;
411 
414  auto implicitTitrantAmount(String const& name) const -> real;
415 
418  auto titrantAmount(String const& name) const -> real;
419 
421  auto namesInputVariables() const -> Strings const&;
422 
424  auto namesControlVariablesP() const -> Strings const&;
425 
427  auto namesControlVariablesQ() const -> Strings const&;
428 
430  auto inputVariables() const -> ArrayXdConstRef;
431 
433  auto controlVariablesP() const -> ArrayXdConstRef;
434 
436  auto controlVariablesQ() const -> ArrayXdConstRef;
437 
439  auto initialComponentAmounts() const -> ArrayXdConstRef;
440 
442  auto w() const -> ArrayXdConstRef;
443 
445  auto p() const -> ArrayXdConstRef;
446 
448  auto q() const -> ArrayXdConstRef;
449 
451  auto c() const -> ArrayXdConstRef;
452 
454  auto optimaState() const -> Optima::State const&;
455 
456 private:
457  struct Impl;
458 
459  Ptr<Impl> pimpl;
460 };
461 
463 auto operator<<(std::ostream& out, ChemicalState const& state) -> std::ostream&;
464 
465 } // namespace Reaktoro
466 
467 //=========================================================================
468 // CODE BELOW NEEDED FOR MEMOIZATION TECHNIQUE INVOLVING CHEMICALSTATE
469 //=========================================================================
470 
471 namespace Reaktoro {
472 
473 template<typename T>
474 struct MemoizationTraits;
475 
477 template<>
479 {
480  using Type = ChemicalState;
481 
484 
485  auto equal(const Tuple<real, real, ArrayXr>& a, ChemicalState const& b)
486  {
487  auto const& [T, P, n] = a;
488  return T == b.temperature() && P == b.pressure() && (n == b.speciesAmounts()).all();
489  }
490 
491  auto assign(Tuple<real, real, ArrayXr>& a, ChemicalState const& b)
492  {
493  auto& [T, P, n] = a;
494  T = b.temperature();
495  P = b.pressure();
496  n = b.speciesAmounts();
497  }
498 };
499 
500 } // namespace Reaktoro
The class that computes chemical properties of a chemical system.
Definition: ChemicalProps.hpp:34
The access to the properties related to an equilibrium state in a ChemicalState object.
Definition: ChemicalState.hpp:340
virtual ~Equilibrium()
Destroy this ChemicalState::Equilibrium instance.
auto setOptimaState(Optima::State const &state) -> void
Set the Optima::State object computed as part of the equilibrium calculation.
auto setInitialComponentAmounts(ArrayXdConstRef const &c0) -> void
Set initial component amounts used in the equilibrium calculation.
auto setNamesControlVariablesQ(Strings const &qnames) -> void
Set the names of the computed control variables q in the equilibrium calculation.
auto setControlVariablesP(ArrayXdConstRef const &p) -> void
Set the values of the computed control variables p in the equilibrium calculation.
auto empty() const -> bool
Return true if no equilibrium information available.
auto setControlVariablesQ(ArrayXdConstRef const &q) -> void
Set the values of the computed control variables q in the equilibrium calculation.
auto setNamesControlVariablesP(Strings const &pnames) -> void
Set the names of the computed control variables p in the equilibrium calculation.
Equilibrium(ChemicalSystem const &system)
Construct a ChemicalState::Equilibrium instance.
auto reset() -> void
Reset this object by clearing all information related to a previous computed equilibrium state.
auto operator=(Equilibrium other) -> Equilibrium &
Assign a ChemicalState::Equilibrium instance to this instance.
auto setInputVariables(ArrayXdConstRef const &w) -> void
Set the values of the input variables w used in the equilibrium calculation.
auto setNamesInputVariables(Strings const &wnames) -> void
Set the names of the input variables w used in the equilibrium calculation.
Equilibrium(Equilibrium const &other)
Construct a copy of a ChemicalState::Equilibrium instance.
The chemical state of a chemical system.
Definition: ChemicalState.hpp:41
ChemicalState(ChemicalState const &other)
Construct a copy of a ChemicalState instance.
auto temperature() const -> real
Return the temperature in the chemical state (in K).
auto scaleSpeciesAmountsInPhase(StringOrIndex const &phase, real const &scalar) -> void
Scale the amounts of the species in a phase by a given factor.
auto updateIdeal(real const &T, real const &P, ArrayXrConstRef const &n) -> void
Update the chemical state and properties of the system using ideal activity models.
auto pressure() const -> real
Return the pressure in the chemical state (in Pa).
auto pressure(real const &value) -> void
Set the pressure of the chemical state.
auto scalePhaseMass(StringOrIndex const &phase, real mass, Chars unit) -> void
Scale the mass of a phase to a new value.
auto props() const -> ChemicalProps const &
Return the chemical properties of the system.
auto charge() const -> real
Return the electric charge in the chemical state (in mol).
auto speciesAmountsInPhase(StringOrIndex const &phase) const -> ArrayXrConstRef
Return the amounts of the species from a phase in the chemical state (in mol).
auto scaleAmount(real amount, Chars unit) -> void
Scale the amount of the system to a new value.
auto set(StringOrIndex const &species, real value, Chars unit) -> void
Set the amount or mass of a chemical species in the chemical state.
auto elementAmounts() const -> ArrayXr
Return the amounts of the elements in the chemical state (in mol).
auto speciesAmount(StringOrIndex const &species) const -> real
Return the amount of a species in the chemical state (in mol).
auto output(std::ostream &out) const -> void
Output this ChemicalState instance to a stream.
auto scaleFluidVolume(real volume, Chars unit) -> void
Scale the total volume of fluids in the system to a new value.
auto speciesAmounts() const -> ArrayXrConstRef
Return the amounts of the species in the chemical state (in mol).
auto scaleSolidAmount(real amount, Chars unit) -> void
Scale the total amount of solids in the system to a new value.
auto setTemperature(real value, Chars unit) -> void
Set the temperature of the chemical state with given unit.
auto scaleSpeciesAmounts(real const &scalar) -> void
Scale the amounts of every species by a given factor.
auto add(StringOrIndex const &species, real value, Chars unit) -> void
Add a specified amount or mass of a chemical species in the chemical state.
auto temperature(real const &value) -> void
Set the temperature of the chemical state.
auto scalePhaseAmount(StringOrIndex const &phase, real amount, Chars unit) -> void
Scale the amount of a phase to a new value.
auto temperature(real value, Chars unit) -> void
Set the temperature of the chemical state with given unit.
auto setPressure(real const &value) -> void
Set the pressure of the chemical state.
auto operator=(ChemicalState other) -> ChemicalState &
Assign a ChemicalState instance to this instance.
auto componentAmounts() const -> ArrayXr
Return the amounts of the conservative components (elements and charge) in the chemical state (in mol...
auto scalePhaseVolume(StringOrIndex const &phase, real volume, Chars unit) -> void
Scale the volume of a phase to a new value.
virtual ~ChemicalState()
Destroy this ChemicalState instance.
auto setSpeciesAmounts(real const &value) -> void
Set the amounts of all species in the chemical state to a common value (in mol).
auto system() const -> ChemicalSystem const &
Return the underlying chemical system for this chemical state.
auto setTemperature(real const &value) -> void
Set the temperature of the chemical state.
auto scaleVolume(real volume, Chars unit) -> void
Scale the volume of the system to a new value.
auto scaleFluidAmount(real amount, Chars unit) -> void
Scale the total amount of fluids in the system to a new value.
auto update(real const &T, real const &P, ArrayXrConstRef const &n) -> void
Update the chemical state and properties of the system.
auto scaleFluidMass(real mass, Chars unit) -> void
Scale the total mass of fluids in the system to a new value.
ChemicalState(ChemicalSystem const &system)
Construct a ChemicalState instance with standard conditions.
auto scaleSolidMass(real mass, Chars unit) -> void
Scale the total mass of solids in the system to a new value.
auto setSpeciesAmount(Index ispecies, real const &amount) -> void
Set the amount of a specific species in the system (in mol).
auto scaleMass(real mass, Chars unit) -> void
Scale the mass of the system to a new value.
auto setSpeciesMass(StringOrIndex const &species, real mass, Chars unit) -> void
Set the mass of a specific species in the system.
auto scaleSolidVolume(real volume, Chars unit) -> void
Scale the total volume of solids in the system to a new value.
auto equilibrium() const -> Equilibrium const &
Return the equilibrium properties of a calculated chemical equilibrium state.
auto speciesMass(StringOrIndex const &species) const -> real
Return the mass of a species in the chemical state (in kg).
The class used to represent a chemical system and its attributes and properties.
Definition: ChemicalSystem.hpp:70
The namespace containing all components of the Reaktoro library.
Definition: Algorithms.hpp:29
auto unit(Index rows, Index i) -> decltype(VectorXd::Unit(rows, i))
Return an expression of a unit vector.
Definition: Matrix.hpp:546
std::string String
Convenient alias for std::string.
Definition: Types.hpp:52
std::size_t Index
Define a type that represents an index.
Definition: Index.hpp:26
autodiff::real real
The number type used throughout the library.
Definition: Real.hpp:26
std::vector< std::string > Strings
Convenient alias for std::vector<String>.
Definition: Types.hpp:55
std::unique_ptr< T > Ptr
Convenient alias for std::unique_ptr<T>.
Definition: Types.hpp:102
Eigen::Ref< const ArrayXl > ArrayXlConstRef
Convenient alias to Eigen type.
Definition: Matrix.hpp:97
std::variant< Index, int, std::string > StringOrIndex
The type used to accept either a name or an index.
Definition: Types.hpp:58
std::tuple< Args... > Tuple
Convenient alias for std::tuple<Args...>.
Definition: Types.hpp:94
const char * Chars
Convenient alias for const char*.
Definition: Types.hpp:49
Eigen::Ref< const ArrayXd > ArrayXdConstRef
Convenient alias to Eigen type.
Definition: Matrix.hpp:105
std::vector< Index > Indices
Define a type that represents a collection of indices.
Definition: Index.hpp:29
Eigen::Ref< const ArrayXr > ArrayXrConstRef
Convenient alias to Eigen type.
Definition: Matrix.hpp:89
autodiff::ArrayXreal ArrayXr
Convenient alias to Eigen type.
Definition: Matrix.hpp:87
Tuple< real, real, ArrayXr > CacheType
The type used instead to cache a ChemicalState object.
Definition: ChemicalState.hpp:483
Used to enable function arguments of a type T to be cached in a memoized version of the function.
Definition: Memoization.hpp:30