chemmisol 0.1
Loading...
Searching...
No Matches
system.h
Go to the documentation of this file.
1#ifndef CHEMMISOL_SYSTEM_H
2#define CHEMMISOL_SYSTEM_H
3
5#include "reaction.h"
6
19namespace chemmisol {
20 namespace solver {
21 class Solver;
22 }
23
52
83
87 class InvalidSpecies : public std::exception {
88 private:
92 const ChemicalSystem* chemical_system;
96 const std::string& name;
100 Phase phase;
101
102 protected:
112 const ChemicalSystem* chemical_system,
113 const std::string& name,
114 Phase phase) :
115 chemical_system(chemical_system),
116 name(name), phase(phase) {
117 }
118
119 public:
125 return *chemical_system;
126 }
127
131 const std::string& getName() const {
132 return name;
133 }
134
138 Phase getPhase() const {
139 return phase;
140 }
141 };
142
148 private:
149 std::string message;
150
151 public:
162 const ChemicalSystem* chemical_system,
163 const std::string& name
164 );
165
170 const char* what() const noexcept override {
171 return message.c_str();
172 }
173 };
174
197 private:
204 struct CompiledReaction {
205 const Reaction* reaction;
206 ChemicalSpeciesReagent produced_species;
207 std::vector<ComponentReagent> components;
208
209 CompiledReaction() = default;
210 CompiledReaction(const Reaction* reaction)
211 : reaction(reaction) {
212 }
213 };
214
215 std::size_t component_index = 0;
216 std::size_t species_index = 0;
217 std::size_t reaction_index = 0;
218
219 /* species indexes */
220 std::vector<std::unique_ptr<ChemicalSpecies>> species;
221 std::unordered_map<std::string, const ChemicalSpecies*> species_by_name;
222
223 /* components indexes */
224 std::vector<std::unique_ptr<ChemicalComponent>> components;
225 std::unordered_map<std::string, const ChemicalComponent*> components_by_name;
226
227 /* reaction indexes */
228 std::vector<std::unique_ptr<Reaction>> reactions;
229 std::unordered_map<std::string, const Reaction*> reactions_by_name;
230
231 /* Internal structures initialized by the setUp() method. */
232 std::vector<std::vector<double>> reaction_matrix;
233 std::vector<CompiledReaction> compiled_reactions;
234
235 void addSpecies(ChemicalSpecies* component, std::size_t index);
236
237 void addComponent(
238 ChemicalComponent* component,
239 std::size_t species_index,
240 std::size_t component_index);
241
242 const Reaction& addReaction(Reaction* reaction, std::size_t index);
243
244 std::size_t max_iteration = 200;
245
246 // Adsorption model parameters
247 double solid_concentration = 0;
248 double specific_surface_area = 0;
249 double site_concentration = 0;
250
251 void addComponent(
252 const std::string& name,
253 Phase phase,
254 double concentration,
255 std::size_t species_index,
256 std::size_t component_index
257 );
258 void fixComponent(
259 const std::string& name,
260 Phase phase,
261 double concentration,
262 std::size_t species_index,
263 std::size_t component_index
264 );
265
266 void addSpecies(
267 const std::string& name,
268 Phase phase,
269 double concentration,
270 std::size_t index
271 );
272
273 const Reaction& addReaction(
274 const std::string& name,
275 double K,
276 const std::vector<Reagent>& reagents,
277 std::size_t index);
278
279 void compile(const Reaction* reaction);
280
288 void initReactionMatrix();
289
290 template<typename T>
291 void _massConservationLaw(
292 const std::vector<T>& activities,
293 std::vector<T>& result) const;
294
295 template<typename T>
296 T _distanceToEquilibrium(
297 const std::vector<T>& activities,
298 const Reaction& reaction) const;
299
300 public:
305
306 /*
307 * TODO: ChemicalSystem copy assignment operator.
308 */
309 const ChemicalSystem& operator=(const ChemicalSystem& other) = delete;
310
311 /*
312 * TODO: ChemicalSystem move constructor.
313 */
314 ChemicalSystem(ChemicalSystem&& other) = delete;
315
316 /*
317 * TODO: ChemicalSystem move assignment operator.
318 */
319 const ChemicalSystem& operator=(ChemicalSystem&& other) = delete;
320
327 ChemicalSystem() = default;
328
345 // TODO: possibility to initialize surface components with a non
346 // null initial molar fraction.
348 double solid_concentration,
349 double specific_surface_area,
350 double site_concentration
351 );
352
372 void setUp();
373
388 const std::string& name,
389 double log_K,
390 const std::vector<Reagent>& reagents
391 );
392
404 const std::string& name,
405 double total_concentration
406 );
407
429 const std::string& name,
430 Phase phase,
431 double total_concentration
432 );
442 const std::string& name,
443 double total_concentration
444 );
445
465 const std::string& name,
466 Phase phase,
467 double total_concentration
468 );
469
475 void addSolvent(const std::string& name);
476
489 void initPH(double pH, const std::string& h_component_name);
490
496 void initPH(double pH) {
497 initPH(pH, "H+");
498 }
499
509 void fixPH(double pH, const std::string& h_component_name);
510
516 void fixPH(double pH) {
517 fixPH(pH, "H+");
518 };
519
524 double getPH(const std::string& h_component_name) const;
525
530 double getPH() const {
531 return getPH("H+");
532 }
533
540 double sitesQuantity() const {
542 solid_concentration, specific_surface_area, site_concentration
543 );
544 }
545
556 void setTotalQuantity(const ChemicalComponent& component, double quantity);
557
570 void setTotalConcentration(const ChemicalComponent& component, double concentration);
571
580 const Reaction& getReaction(const std::string& name) const;
581
593 const ChemicalSpecies& getSpecies(const std::string& name) const;
594
607 const ChemicalSpecies& getSpecies(const std::size_t& index) const;
608
619 const ChemicalComponent& getComponent(const std::string& name) const;
620
632 const ChemicalComponent& getComponent(const std::size_t& index) const;
633
647 bool isComponent(const std::string& species_name) const;
648
656 bool isComponent(const ChemicalSpecies& species) const;
657
661 const std::vector<std::unique_ptr<ChemicalComponent>>& getComponents() const {
662 return components;
663 }
664
668 const std::vector<std::unique_ptr<ChemicalSpecies>>& getSpecies() const {
669 return species;
670 }
671
675 const std::vector<std::unique_ptr<Reaction>>& getReactions() const {
676 return reactions;
677 }
678
690 const std::vector<ComponentReagent>& getComponentReagents(
691 const Reaction& reaction) const;
692
704 const Reaction& reaction) const;
705
737 const std::vector<std::vector<double>>& getReactionMatrix() const {
738 return reaction_matrix;
739 }
740
759 void proceed(const Reaction& reaction, double extent);
760
797 const std::vector<double>& activities,
798 const Reaction& reaction) const;
799
800 std::complex<double> distanceToEquilibrium(
801 const std::vector<std::complex<double>>& activities,
802 const Reaction& reaction) const;
803
811 double degree(const Reaction& reaction) const;
812
822 double distanceToEquilibrium(const Reaction& reaction) const;
823
840
855 void solveEquilibrium(const solver::Solver& solver);
856
869 double reactionQuotient(const Reaction& reaction) const;
870
881 double reactionQuotient(const std::string& name) const;
882
983 const std::vector<double>& activities,
984 std::vector<double>& result) const;
985
987 const std::vector<std::complex<double>>& activities,
988 std::vector<std::complex<double>>& result) const;
989
1003 void massConservationLaw(std::vector<double>& result) const;
1004
1009 std::size_t getMaxIteration() const {
1010 return max_iteration;
1011 }
1012
1017 void setMaxIteration(const std::size_t& max_iteration) {
1018 this->max_iteration = max_iteration;
1019 }
1020 };
1021
1022 template<typename T>
1023 void ChemicalSystem::_massConservationLaw(
1024 const std::vector<T>& activities,
1025 std::vector<T>& result) const {
1026 for(auto& component : getComponents()) {
1027 if(component->isFixed()) {
1028 result[component->getIndex()] = 0.0;
1029 } else {
1030 result[component->getIndex()]
1031 = component->getSpecies()->quantity(
1032 activities[component->getSpecies()->getIndex()]
1033 );
1034 }
1035 }
1036 for(auto& reaction : compiled_reactions) {
1037 for(const ComponentReagent& reagent
1038 : reaction.components) {
1039 if(!reagent.component->isFixed()) {
1040 result[reagent.component->getIndex()] +=
1041 reagent.coefficient / (-reaction.produced_species.coefficient)
1042 * reaction.produced_species.species->quantity(
1043 activities[reaction.produced_species.species->getIndex()]
1044 );
1045 }
1046 }
1047 }
1048 for(auto& component : getComponents()) {
1049 if(!component->isFixed()) {
1050 result[component->getIndex()]
1051 -= component->getTotalQuantity();
1052 }
1053 }
1054 }
1055
1056 template<typename T>
1057 T ChemicalSystem::_distanceToEquilibrium(
1058 const std::vector<T>& activities,
1059 const Reaction& reaction) const {
1060 T reactives = {reaction.getK()};
1061 T products {1.0};
1062 auto& compiled_reaction = compiled_reactions[reaction.getIndex()];
1063 for(const auto& reagent : compiled_reaction.components) {
1064 T activity = activities[reagent.component->getSpecies()->getIndex()];
1065 if(activity != 0.0) {
1066 if(reagent.coefficient > 0) {
1067 reactives *= std::pow(activity, reagent.coefficient);
1068 } else {
1069 products *= std::pow(activity, -reagent.coefficient);
1070 }
1071 } else {
1072 if(reagent.coefficient > 0) {
1073 reactives = 0.0;
1074 } else {
1075 products = 0.0;
1076 }
1077 }
1078 }
1079 T activity = activities[compiled_reaction.produced_species.species->getIndex()];
1080 if(activity != 0.0)
1081 // This coefficient is necessarily negative
1082 products *= std::pow(
1083 activity,
1084 -compiled_reaction.produced_species.coefficient);
1085 else
1086 products = 0.0;
1087 return products - reactives;
1088 }
1089}
1090#endif /*CHEMMISOL_SYSTEM_H*/
Definition species.h:682
Definition species.h:39
Definition system.h:196
double getPH() const
Definition system.h:530
const ChemicalSpeciesReagent & getSpeciesReagent(const Reaction &reaction) const
void addSolvent(const std::string &name)
const ChemicalSpecies & getSpecies(const std::string &name) const
double sitesQuantity() const
Definition system.h:540
const ChemicalComponent & getComponent(const std::string &name) const
void setMaxIteration(const std::size_t &max_iteration)
Definition system.h:1017
const std::vector< ComponentReagent > & getComponentReagents(const Reaction &reaction) const
ChemicalSystem(double solid_concentration, double specific_surface_area, double site_concentration)
void proceed(const Reaction &reaction, double extent)
const std::vector< std::unique_ptr< ChemicalComponent > > & getComponents() const
Definition system.h:661
const ChemicalSpecies & getSpecies(const std::size_t &index) const
void initPH(double pH)
Definition system.h:496
const std::vector< std::vector< double > > & getReactionMatrix() const
Definition system.h:737
double distanceToEquilibrium(const Reaction &reaction) const
bool isComponent(const ChemicalSpecies &species) const
void massConservationLaw(const std::vector< double > &activities, std::vector< double > &result) const
const std::vector< std::unique_ptr< ChemicalSpecies > > & getSpecies() const
Definition system.h:668
double degree(const Reaction &reaction) const
std::size_t getMaxIteration() const
Definition system.h:1009
void addComponent(const std::string &name, Phase phase, double total_concentration)
void solveEquilibrium(const solver::Solver &solver)
void initPH(double pH, const std::string &h_component_name)
const Reaction & getReaction(const std::string &name) const
void setTotalQuantity(const ChemicalComponent &component, double quantity)
const std::vector< std::unique_ptr< Reaction > > & getReactions() const
Definition system.h:675
double getPH(const std::string &h_component_name) const
double distanceToEquilibrium(const std::vector< double > &activities, const Reaction &reaction) const
void addComponent(const std::string &name, double total_concentration)
bool isComponent(const std::string &species_name) const
double reactionQuotient(const Reaction &reaction) const
void massConservationLaw(std::vector< double > &result) const
void fixComponent(const std::string &name, double total_concentration)
const ChemicalComponent & getComponent(const std::size_t &index) const
void fixPH(double pH, const std::string &h_component_name)
void setTotalConcentration(const ChemicalComponent &component, double concentration)
const Reaction & addReaction(const std::string &name, double log_K, const std::vector< Reagent > &reagents)
void fixPH(double pH)
Definition system.h:516
void fixComponent(const std::string &name, Phase phase, double total_concentration)
double reactionQuotient(const std::string &name) const
ChemicalSystem(const ChemicalSystem &other)
const char * what() const noexcept override
Definition system.h:170
InvalidMineralSpeciesWithUndefinedSitesCount(const ChemicalSystem *chemical_system, const std::string &name)
Definition system.h:87
const ChemicalSystem & getChemicalSystem() const
Definition system.h:124
const std::string & getName() const
Definition system.h:131
InvalidSpecies(const ChemicalSystem *chemical_system, const std::string &name, Phase phase)
Definition system.h:111
Phase getPhase() const
Definition system.h:138
static double sites_quantity(double solid_concentration, double specific_surface_area, double site_concentration)
Definition reaction.h:289
Definition solver.h:457
Definition chemmisol.h:31
Phase
Definition species.h:18
Definition system.h:60
double coefficient
Definition system.h:64
ChemicalSpeciesReagent(double coefficient, ChemicalSpecies *species)
Definition system.h:78
ChemicalSpecies * species
Definition system.h:68
Definition system.h:30
ChemicalComponent * component
Definition system.h:38
ComponentReagent(double coefficient, ChemicalComponent *component)
Definition system.h:47
double coefficient
Definition system.h:34