1#ifndef CHEMMISOL_SYSTEM_H
2#define CHEMMISOL_SYSTEM_H
96 const std::string& name;
113 const std::string& name,
115 chemical_system(chemical_system),
116 name(name), phase(phase) {
125 return *chemical_system;
163 const std::string& name
170 const char*
what() const noexcept
override {
171 return message.c_str();
204 struct CompiledReaction {
207 std::vector<ComponentReagent> components;
209 CompiledReaction() =
default;
210 CompiledReaction(
const Reaction* reaction)
211 : reaction(reaction) {
215 std::size_t component_index = 0;
216 std::size_t species_index = 0;
217 std::size_t reaction_index = 0;
220 std::vector<std::unique_ptr<ChemicalSpecies>> species;
221 std::unordered_map<std::string, const ChemicalSpecies*> species_by_name;
224 std::vector<std::unique_ptr<ChemicalComponent>> components;
225 std::unordered_map<std::string, const ChemicalComponent*> components_by_name;
228 std::vector<std::unique_ptr<Reaction>> reactions;
229 std::unordered_map<std::string, const Reaction*> reactions_by_name;
232 std::vector<std::vector<double>> reaction_matrix;
233 std::vector<CompiledReaction> compiled_reactions;
239 std::size_t species_index,
240 std::size_t component_index);
244 std::size_t max_iteration = 200;
247 double solid_concentration = 0;
248 double specific_surface_area = 0;
249 double site_concentration = 0;
252 const std::string& name,
254 double concentration,
255 std::size_t species_index,
256 std::size_t component_index
259 const std::string& name,
261 double concentration,
262 std::size_t species_index,
263 std::size_t component_index
267 const std::string& name,
269 double concentration,
274 const std::string& name,
276 const std::vector<Reagent>& reagents,
279 void compile(
const Reaction* reaction);
288 void initReactionMatrix();
291 void _massConservationLaw(
292 const std::vector<T>& activities,
293 std::vector<T>& result)
const;
296 T _distanceToEquilibrium(
297 const std::vector<T>& activities,
348 double solid_concentration,
349 double specific_surface_area,
350 double site_concentration
388 const std::string& name,
390 const std::vector<Reagent>& reagents
404 const std::string& name,
405 double total_concentration
429 const std::string& name,
431 double total_concentration
442 const std::string& name,
443 double total_concentration
465 const std::string& name,
467 double total_concentration
489 void initPH(
double pH,
const std::string& h_component_name);
509 void fixPH(
double pH,
const std::string& h_component_name);
524 double getPH(
const std::string& h_component_name)
const;
542 solid_concentration, specific_surface_area, site_concentration
661 const std::vector<std::unique_ptr<ChemicalComponent>>&
getComponents()
const {
668 const std::vector<std::unique_ptr<ChemicalSpecies>>&
getSpecies()
const {
738 return reaction_matrix;
797 const std::vector<double>& activities,
801 const std::vector<std::complex<double>>& activities,
983 const std::vector<double>& activities,
984 std::vector<double>& result)
const;
987 const std::vector<std::complex<double>>& activities,
988 std::vector<std::complex<double>>& result)
const;
1010 return max_iteration;
1018 this->max_iteration = max_iteration;
1022 template<
typename T>
1023 void ChemicalSystem::_massConservationLaw(
1024 const std::vector<T>& activities,
1025 std::vector<T>& result)
const {
1027 if(component->isFixed()) {
1028 result[component->getIndex()] = 0.0;
1030 result[component->getIndex()]
1031 = component->getSpecies()->quantity(
1032 activities[component->getSpecies()->getIndex()]
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()]
1049 if(!component->isFixed()) {
1050 result[component->getIndex()]
1051 -= component->getTotalQuantity();
1056 template<
typename T>
1057 T ChemicalSystem::_distanceToEquilibrium(
1058 const std::vector<T>& activities,
1059 const Reaction& reaction)
const {
1060 T reactives = {reaction.getK()};
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);
1069 products *= std::pow(activity, -reagent.coefficient);
1072 if(reagent.coefficient > 0) {
1079 T activity = activities[compiled_reaction.produced_species.species->getIndex()];
1082 products *= std::pow(
1084 -compiled_reaction.produced_species.coefficient);
1087 return products - reactives;
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)
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 chemmisol.h:31
Phase
Definition species.h:18
double coefficient
Definition system.h:64
ChemicalSpeciesReagent(double coefficient, ChemicalSpecies *species)
Definition system.h:78
ChemicalSpecies * species
Definition system.h:68
ChemicalComponent * component
Definition system.h:38
ComponentReagent(double coefficient, ChemicalComponent *component)
Definition system.h:47
double coefficient
Definition system.h:34