Skip to content

Commit

Permalink
Fix specificAmountOfParticles
Browse files Browse the repository at this point in the history
  • Loading branch information
MarekMatejak committed Dec 10, 2023
1 parent 7dfa6fd commit 859ac82
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions Chemical/Interfaces.mo
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,8 @@ package Interfaces "Chemical interfaces"
input Modelica.Units.SI.ElectricPotential v=0
"Electric potential of the substance";
input Modelica.Units.SI.MoleFraction I=0 "Ionic strengh (mole fraction based)";
input Modelica.Units.SI.Mass mass=1 "Mass of substance";
input Modelica.Units.SI.AmountOfSubstance nSolution=1 "Amount of substances in solution";
output Real specificAmountOfSubstance(unit="mol/kg")
"Amount of substance particles per its mass";
algorithm
Expand All @@ -684,7 +686,7 @@ package Interfaces "Chemical interfaces"
input Modelica.Units.SI.ElectricPotential v=0
"Electric potential of the substance";
input Modelica.Units.SI.MoleFraction I=0 "Ionic strengh (mole fraction based)";
input Modelica.Units.SI.Mass massH2O=1 "Mass of H2O";
input Modelica.Units.SI.Mass mass=1 "Mass of substance";
input Modelica.Units.SI.AmountOfSubstance nSolution=1 "Amount of substances in solution";
output Real specificAmountOfFreeBaseMolecule(unit="mol/kg")
"Amount of substance free base molecule per substance mass";
Expand Down Expand Up @@ -1015,10 +1017,14 @@ end solution_temperature_;
"Electric potential of the substance";
input Modelica.Units.SI.MoleFraction I=0
"Ionic strengh (mole fraction based)";
input Modelica.Units.SI.Mass mass=1 "Mass of substance";
input Modelica.Units.SI.AmountOfSubstance nSolution=1 "Amount of substances in solution";
output Real specificAmountOfSubstance(unit="mol/kg") "Amount of substance particles per its mass";
protected
Modelica.Units.SI.MolarEnergy SelfClustering_dG;
Real SelfClustering_K;
Real amountOfBaseMolecules,amountOfFreeMolecule,amountOfParticles;
Real x;
algorithm
if not selfClustering(substanceData) then
specificAmountOfSubstance := 1/substanceData.MolarWeight;
Expand All @@ -1028,7 +1034,18 @@ end solution_temperature_;

SelfClustering_K := exp(-SelfClustering_dG/(Modelica.Constants.R*T));

specificAmountOfSubstance := 1/((SelfClustering_K + 1)*substanceData.MolarWeight);
amountOfBaseMolecules:=mass/substanceData.MolarWeight;
x:=((2*SelfClustering_K+nSolution/amountOfBaseMolecules) -
sqrt((4*SelfClustering_K*nSolution/amountOfBaseMolecules)+
(nSolution/amountOfBaseMolecules)^2)) / (2*(SelfClustering_K^2));

amountOfFreeMolecule := x*nSolution;

amountOfParticles := amountOfFreeMolecule/(1 - SelfClustering_K*x);

specificAmountOfSubstance := amountOfParticles/mass;

//specificAmountOfSubstance := 1/((SelfClustering_K + 1)*substanceData.MolarWeight);
end if;
end specificAmountOfParticles;

Expand All @@ -1041,7 +1058,7 @@ end solution_temperature_;
input Modelica.Units.SI.ElectricPotential v=0
"Electric potential of the substance";
input Modelica.Units.SI.MoleFraction I=0 "Ionic strengh (mole fraction based)";
input Modelica.Units.SI.Mass massH2O=1 "Mass of H2O in solution";
input Modelica.Units.SI.Mass mass=1 "Mass of substance in solution";
input Modelica.Units.SI.AmountOfSubstance nSolution=1 "Amount of substances in solution";
output Real specificAmountOfFreeBaseMolecule(unit="mol/kg")
"Amount of substance free base molecule per substance mass";
Expand All @@ -1057,12 +1074,12 @@ end solution_temperature_;

SelfClustering_K := exp(-SelfClustering_dG/(Modelica.Constants.R*T));

amountOfBaseMolecules:=massH2O/substanceData.MolarWeight;
amountOfBaseMolecules:=mass/substanceData.MolarWeight;
x:=((2*SelfClustering_K+nSolution/amountOfBaseMolecules) -
sqrt((4*SelfClustering_K*nSolution/amountOfBaseMolecules)+
(nSolution/amountOfBaseMolecules)^2)) / (2*(SelfClustering_K^2));

specificAmountOfFreeBaseMolecule := (x*nSolution)/massH2O;
specificAmountOfFreeBaseMolecule := (x*nSolution)/mass;

end if;
annotation (Inline=true, smoothOrder=2);
Expand Down

0 comments on commit 859ac82

Please sign in to comment.