Bug Fix
This commit is contained in:
@@ -351,14 +351,19 @@ public class Compound
|
|||||||
return Null;
|
return Null;
|
||||||
if ((!byRatio && amount.IsEqualApprox(Amount)) || (byRatio && amount.IsEqualApprox(1)))
|
if ((!byRatio && amount.IsEqualApprox(Amount)) || (byRatio && amount.IsEqualApprox(1)))
|
||||||
{
|
{
|
||||||
|
|
||||||
|
double deltaV = Volume;
|
||||||
HomogeneousMixture.Compounds.Remove(this);
|
HomogeneousMixture.Compounds.Remove(this);
|
||||||
HomogeneousMixture = HomogeneousMixture.Null;
|
HomogeneousMixture = HomogeneousMixture.Null;
|
||||||
|
HomogeneousMixture.Volume -= deltaV;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
double ebs = Energy;
|
double ebs = Energy;
|
||||||
Compound res = Copy();
|
Compound res = Copy();
|
||||||
if(!byRatio)
|
if(!byRatio)
|
||||||
{
|
{
|
||||||
|
if (amount > Amount)
|
||||||
|
throw new Exception("????");
|
||||||
double ratio = amount / Amount;
|
double ratio = amount / Amount;
|
||||||
Amount -= amount;
|
Amount -= amount;
|
||||||
res.Amount = amount;
|
res.Amount = amount;
|
||||||
@@ -373,6 +378,8 @@ public class Compound
|
|||||||
Energy = ebs * (1d - amount);
|
Energy = ebs * (1d - amount);
|
||||||
}
|
}
|
||||||
res.HomogeneousMixture = HomogeneousMixture.Null;
|
res.HomogeneousMixture = HomogeneousMixture.Null;
|
||||||
|
if (Amount < 0 || res.Amount < 0)
|
||||||
|
throw new Exception("?");
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -149,8 +149,6 @@ public class HomogeneousMixture
|
|||||||
{
|
{
|
||||||
if ((c.Amount * 0.125).IsEqualApprox(0) || c.Atoms.Count > 15)
|
if ((c.Amount * 0.125).IsEqualApprox(0) || c.Atoms.Count > 15)
|
||||||
continue;
|
continue;
|
||||||
if(c.Expression.Equals("P2So"))
|
|
||||||
Console.WriteLine("?");
|
|
||||||
foreach(BaseBond b in c.Bonds)
|
foreach(BaseBond b in c.Bonds)
|
||||||
{
|
{
|
||||||
if(b.Connected || !(b.Energy > b.BondingEnergy))
|
if(b.Connected || !(b.Energy > b.BondingEnergy))
|
||||||
@@ -322,6 +320,17 @@ public class HomogeneousMixture
|
|||||||
into.AddLayer(res);
|
into.AddLayer(res);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Remove a compound from the homogeneous mixture
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="compound"></param>
|
||||||
|
public void RemoveCompound(Compound compound)
|
||||||
|
{
|
||||||
|
double deltaV = compound.Volume;
|
||||||
|
Compounds.Remove(compound);
|
||||||
|
compound.HomogeneousMixture = Null;
|
||||||
|
Volume -= deltaV;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Add compound into the homogeneous mixture
|
/// Add compound into the homogeneous mixture
|
||||||
@@ -672,12 +681,17 @@ public class HomogeneousMixture
|
|||||||
.Rayleigh(Ita) * 255
|
.Rayleigh(Ita) * 255
|
||||||
);
|
);
|
||||||
|
|
||||||
|
public double OsmoticLevel => StateMatrix.Get
|
||||||
|
.ConjugateOn(new DiagR3(Phase, Math.PI, -Phase))
|
||||||
|
.Rayleigh(LogStateMatrix.Get.ColumnAverage);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Should not be used by any methods except ResolveNext and ResolvePrev
|
/// Should not be used by any methods except ResolveNext and ResolvePrev
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void PureResolve(HomogeneousMixture hOther)
|
private void PureResolve(HomogeneousMixture hOther)
|
||||||
{
|
{
|
||||||
HashSet<Compound> toAbsorb = new ();
|
HashSet< (Compound, double)> toAbsorb = new ();
|
||||||
|
if (hOther.OsmoticLevel > OsmoticLevel)
|
||||||
|
return;
|
||||||
foreach (Compound c in hOther.Compounds)
|
foreach (Compound c in hOther.Compounds)
|
||||||
{
|
{
|
||||||
double eigenRes = EigenResolvability(c);
|
double eigenRes = EigenResolvability(c);
|
||||||
@@ -687,7 +701,7 @@ public class HomogeneousMixture
|
|||||||
continue;
|
continue;
|
||||||
if (eigenRes.IsEqualApprox(0))
|
if (eigenRes.IsEqualApprox(0))
|
||||||
{
|
{
|
||||||
toAbsorb.Add(c);
|
toAbsorb.Add((c, c.Amount));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -701,17 +715,19 @@ public class HomogeneousMixture
|
|||||||
double splitAmount = Math.Min(maxResAmount, c.Amount);
|
double splitAmount = Math.Min(maxResAmount, c.Amount);
|
||||||
if(splitAmount.IsEqualApprox(0))
|
if(splitAmount.IsEqualApprox(0))
|
||||||
continue;
|
continue;
|
||||||
string log = $"{splitAmount} split from {c.Amount} * {c.Expression}";
|
toAbsorb.Add((c, splitAmount));
|
||||||
Compound cSplit = c.Split(splitAmount);
|
//Compound cSplit = c.Split(splitAmount);
|
||||||
cSplit.TraceLog.Add(log);
|
//toAbsorb.Add(cSplit);
|
||||||
toAbsorb.Add(cSplit);
|
|
||||||
}
|
}
|
||||||
foreach (Compound c in toAbsorb)
|
foreach ((Compound c, double a) in toAbsorb)
|
||||||
{
|
{
|
||||||
double v = c.Volume;
|
Compound sp = c.Split(a);
|
||||||
|
double v = sp.Volume;
|
||||||
hOther.Volume -= v;
|
hOther.Volume -= v;
|
||||||
Volume += v;
|
Volume += v;
|
||||||
AddCompound(c);
|
/*if (c.Amount.IsEqualApprox(0))
|
||||||
|
hOther.RemoveCompound(c);*/
|
||||||
|
AddCompound(sp);
|
||||||
}
|
}
|
||||||
if(hOther.Amount.IsEqualApprox(0))
|
if(hOther.Amount.IsEqualApprox(0))
|
||||||
HeterogeneousMixture.RemoveLayer(hOther);
|
HeterogeneousMixture.RemoveLayer(hOther);
|
||||||
@@ -738,43 +754,46 @@ public class HomogeneousMixture
|
|||||||
|
|
||||||
private bool Precipitate()
|
private bool Precipitate()
|
||||||
{
|
{
|
||||||
|
HashSet<(Compound, double)> toDeposit = new();
|
||||||
foreach (Compound c in Compounds)
|
foreach (Compound c in Compounds)
|
||||||
{
|
{
|
||||||
double maxRes = Resolvability(c);
|
double maxRes = Resolvability(c);
|
||||||
if (maxRes < 0)
|
if (maxRes < 0 || maxRes > c.Concentration)
|
||||||
continue;
|
continue;
|
||||||
double maxResAmount = maxRes * (Amount - c.Amount);
|
double maxResAmount = maxRes * (Amount - c.Amount);
|
||||||
if(maxResAmount.IsEqualApprox(0))
|
if(maxResAmount.IsEqualApprox(0))
|
||||||
continue;
|
continue;
|
||||||
|
double aDiff = c.Amount - maxResAmount;
|
||||||
double aDiff = c.Amount - maxResAmount;
|
|
||||||
if (aDiff.IsSignificantlyGreaterThen(0) )
|
if (aDiff.IsSignificantlyGreaterThen(0) )
|
||||||
|
toDeposit.Add((c, aDiff));
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ((Compound c, double a) in toDeposit)
|
||||||
|
{
|
||||||
|
Compound oth = c.Split(a);
|
||||||
|
if (oth.FreeDensity.Get > FreeDensity.Get)
|
||||||
{
|
{
|
||||||
Compound oth = c.Split(c.Amount - maxResAmount);
|
if (Layer.Next.IsEnding)
|
||||||
if (oth.FreeDensity.Get > FreeDensity.Get)
|
|
||||||
{
|
{
|
||||||
if (Layer.Next.IsEnding)
|
HomogeneousMixture m = HeterogeneousMixture.AddLayer();
|
||||||
{
|
m.Layer.MoveAfter(Layer);
|
||||||
HomogeneousMixture m = HeterogeneousMixture.AddLayer();
|
m.AddCompound(oth);
|
||||||
m.Layer.MoveAfter(Layer);
|
m.Volume = m.FreeVolume;
|
||||||
m.AddCompound(oth);
|
return true;
|
||||||
m.Volume = m.FreeVolume;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
Layer.Next.Value.AddCompound(oth);
|
|
||||||
}
|
}
|
||||||
else
|
Layer.Next.Value.AddCompound(oth);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (Layer.Previous.IsEnding)
|
||||||
{
|
{
|
||||||
if (Layer.Previous.IsEnding)
|
HomogeneousMixture m = HeterogeneousMixture.AddLayer();
|
||||||
{
|
m.Layer.MoveBefore(Layer);
|
||||||
HomogeneousMixture m = HeterogeneousMixture.AddLayer();
|
m.AddCompound(oth);
|
||||||
m.Layer.MoveBefore(Layer);
|
m.Volume = m.FreeVolume;
|
||||||
m.AddCompound(oth);
|
return true;
|
||||||
m.Volume = m.FreeVolume;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
Layer.Previous.Value.AddCompound(oth);
|
|
||||||
}
|
}
|
||||||
|
Layer.Previous.Value.AddCompound(oth);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -792,7 +811,7 @@ public class HomogeneousMixture
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// combine identical compounds in the miture
|
/// combine identical compounds in the mixture
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Degenerate()
|
public void Degenerate()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ public static class HomogeneousMixtureResolver
|
|||||||
private static readonly Regex Matcher = new Regex(MatchString);
|
private static readonly Regex Matcher = new Regex(MatchString);
|
||||||
|
|
||||||
private const string VolumeRegex = @"\?V\(([0-9,]+)\)";
|
private const string VolumeRegex = @"\?V\(([0-9,]+)\)";
|
||||||
private const string EnergyRegex = @"\?E\(([0-9,]+)\)";
|
private const string EnergyRegex = @"\?ENG\(([0-9,]+)\)";
|
||||||
private const string EnvMatchString = VolumeRegex + EnergyRegex;
|
private const string EnvMatchString = EnergyRegex + VolumeRegex;
|
||||||
private static readonly Regex EnvMatcher = new Regex(EnvMatchString);
|
private static readonly Regex EnvMatcher = new Regex(EnvMatchString);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// split different compounds
|
/// split different compounds
|
||||||
|
|||||||
Reference in New Issue
Block a user