echo "html>";
$bb="
";
//$x = 0;
//eval('$name = $file'.$x.'_name;');
//This will result in the same code as:
//$name = $file321_name;
//echo $name;
$TartaricPercent = 50;
$MalicPercent = 30;
$CitricPercent = 4;
$SuccinicPercent = 5;
$LacticPercent = 10;
$AceticPercent = 1;
$TA=6.5;
$pHi=3.5;
$pHf=3.3;
$ln10 = log(10);
$MolWtKHCO3 = 100.12 ;
$MolWtNaOH = 40.00;
$Kw25 = 0.00000000000001; // Dissociation constant for water at 25C.
$DATAPOINTS = 1000;
// symbolic constants for the various acids:
$FIRSTACID = 0;
$TAR = 0;
$MAL = 1;
$CIT = 2;
$SUC = 3;
$LAC = 4;
$ACE = 5;
$TOPACID = $ACE+1;
// symbolic constants for K1..n for the various acids (see usage)!
$K1 = 0;
$K2 = 1;
$K3 = 2;
$STUB = 3; // needed for checks below!
$ARBITRARY = 1; // must be nonzero
// and for the various acid species!!
//$A0 = 0;
//$A1 = 1;
//$A2 = 2;
//$A3 = 3;
//$Atot = 4;
//$TACID = 10;
//$pHINIT = 11;
//$pHFIN = 12;
//$SG = 13;
$pHINIT1 = $pHi;
$pHFIN1 = $pHf;
$SG1 = 1; // ???????????????????????????????????????????????????????????!!!!!!!!!!!!!!!!!!!!!!!!!!!
$TAR = $TartaricPercent;
$MAL = $MalicPercent;
$CIT = $CitricPercent;
$SUC = $SuccinicPercent;
$LAC = $LacticPercent;
$ACE = $AceticPercent;
$pHindicator = 8.3;
$WaterDensity25 = .997044;
// Float array of concentrations of various acid species!
$A0 = 0;
$A1 = 1;
$A2 = 2;
$A3 = 3;
$Atot = 4;
$TACID = 10;
$pHINIT = 11;
$pHFIN = 12;
$SG = 13;
$Acid = array
(
"i"=>array(),
"x"=>array()
);
//echo "Letter ". $alpha['ABC'][$pos] ." is in position ". $alpha['123'][$pos];
//Acid [] [];
// First param is `ionisation state' eg A0, A1, .. Atot (5, redundant)
// second param is actual acid eg. TAR.
// echo $WaterDensity25;
$molalTA=0;
$currentSID=0;
$THEMODE = "ADDACID"; //arbitrary default
$MINMODE = -1;
$ADDACID = 0;
$MLFBASE = 1;
$TITRATE = 2;
$MAXMODE = 3;
function calcKd($n)
{
$val= exp(log(10) * (-$n));
return $val;
}
$kd_K1_TAR = calcKd(2.98); // ????????????????? NOOOOO. MUST BE 10**(-n) ???????
$kd_K2_TAR = calcKd(4.54);
$kd_K3_TAR = 0;
$iMax0 = 2;
//
$kd_K1_MAL = calcKd(3.40);
$kd_K2_MAL = calcKd(5.05);
$kd_K3_MAL = 0;
$iMax1 = 2;
//
$kd_K1_CIT = calcKd(3.13);
$kd_K2_CIT = calcKd(4.76);
$kd_K3_CIT = calcKd(5.40);
$iMax2 = 3;
$kd_K1_SUC = calcKd(4.16);
$kd_K2_SUC = calcKd(5.57);
$kd_K3_SUC = 0;
$iMax3 = 2;
//
$kd_K1_LAC = calcKd(3.86);
$kd_K2_LAC = 0;
$kd_K3_LAC = 0;
$iMax4 = 1;
//
$kd_K1_ACE = calcKd(4.75);
$kd_K2_ACE = 0;
$kd_K3_ACE = 0;
$iMax_AE = 1;
$iMax5 = 1;
//
$kd_STUB_TAR = ARBITRARY;
$kd_STUB_MAL = ARBITRARY;
$kd_STUB_CIT = ARBITRARY;
$kd_STUB_SUC = ARBITRARY;
$kd_STUB_LAC = ARBITRARY;
$kd_STUB_ACE = ARBITRARY; // only really needed for CIT, but be obsessive! A hack.
//Molecular weights:
$molWt_TAR = 150.09;
$molWt_MAL = 134.09;
$molWt_SUC = 118.09;
$molWt_LAC = 90.08;
$molWt_CIT = 192.12;
$molWt_ACE = 60.06;
$plotDATA_0 = -1; //prevent drawing at start!
// 1a2. Molar2Molal
// Convert amount per litre to amount per kg. As specific gravity is kg/l,
// the identity is:
// amt per kg = amt per litre / SG.
function Molar2Molal($molar,$SG1,$WaterDensity25)
{
//echo $molar;
//echo $SG1;
//echo "
";
//echo $WaterDensity25;
$val= ($molar / ($SG1 * $WaterDensity25)); // See appendix B.;
return $val;
}
function FromMolal($molal)
{
$val= ($molal * $SG1 * $WaterDensity25);
return $val;
};
// function 2b: We know titratable acidity and pH,
// but need to calculate [Atot(tar)] so that we
// can calculate SID, and all other species.
// public double calcAcids ()
// { //1. get TA, find its molar value, get [H+]:
// double TA;
// double H;
// double pH;
// TA = nrthcntrols.getDoubleVal(TACID);
$TA = Molar2Molal($TA,$SG1,$WaterDensity25); // v 0.91 entry is now per litre, so convert to per kg!
//echo $TA;
$molalTA = (2 * $TA / $molWt_TAR);
//echo $molalTA;
$pH = $pHINIT;
//echo $pH;
$H = exp(log(10) * (-$pH));
//echo $bb;
//echo $H;
// molalTA = 2*TA / molWt[TAR];
// pH = nrthcntrols.getDoubleVal(pHINIT); //clumsy.
// H = (java.lang.Math.exp(ln10 * (-pH)));
// pH is the log of the reciprocal of the hydrogen ion *activity*. Assuming (eugh) an
// activity coefficient of 1, pH = log(1/[H+]), thus -pH = log([H+]), or
// [H+] = 10**(-pH). Now the exp function in Java is e**n, NOT 10**n.
// Thus exp(n) in Java is e**n, and 10**k = (e**(ln(10)))**k = e**(k*ln(10))
// Note that e**(ln(10)) = 10.
//2. Populate Cratio array:
//2a. read in percentages as floats:
// double pTar, pMal, pCit, pSuc, pLac, pAce;
$pTar = $TAR/$molWt_TAR;
$pMal = $MAL/$molWt_MAL;
$pCit = $CIT/$molWt_CIT;
$pSuc = $SUC/$molWt_SUC;
$pLac = $LAC/$molWt_LAC;
$pAce = $ACE/$molWt_ACE; // convert g/kg to mol/kg!
$Cratio_TAR = 1; // tar:tar = 1
$Cratio_MAL = $pMal/$pTar;
$Cratio_CIT = $pCit/$pTar;
$Cratio_SUC = $pSuc/$pTar;
$Cratio_LAC = $pLac/$pTar;
$Cratio_ACE = $pAce/$pTar;
$myTOT = 0;
$inner = 0;
$i = 0;
$x = 0;
for ($x=$FIRSTACID;$x<$TOPACID;$x++)
{ //echo "x "; echo $x; echo $bb;
eval('$iMaxX = $iMax'.$x.';');
//echo "iMaxX = iMax"; echo $x; echo "= "; echo $iMaxX; echo $bb;
while ($i < $iMaxX)
{ $inner += ($iMaxX-$i)/calcTHETA($i,$x,$H);
$i ++;
};
if ($x == 0)
{
$CratioX = $Cratio_TAR;
};
if ($x == 1)
{
$CratioX = $Cratio_MAL;
};
if ($x == 2)
{
$CratioX = $Cratio_CIT;
};
if ($x == 3)
{
$CratioX = $Cratio_SUC;
};
if ($x == 4)
{
$CratioX = $Cratio_LAC;
};
if ($x == 5)
{
$CratioX = $Cratio_ACE;
};
if ($x > 5)
{
$CratioX = 0;
};
$myTOT += $CratioX*$inner;
};
$Acid_Atot_TAR = ($molalTA - $H)/$myTOT;
$RealityCheck = $Acid_Atot_TAR;
// 4.0 (v0.91) First populate for pH of pHindicator, and thus work out the tiny amount
// of EQUIVALENT undissociated acid at that pH, necessary for an accurate pH titration curve!!
$H83 = exp(log(10) * (-$pHindicator)); // get [H] at pH of 8.3
$OH83 = $Kw25 / $H83;
PopulateAcids($H83); // $H83 = [H] at pH of 8.3
$Aunion83 = 0;
for ($x=$FIRSTACID;$x<$TOPACID;$x++)
{
eval('$iMaxX = $iMax'.$x.';');
for ($i=0;$i 5)
{
$CratioX = 0;
};
$Acid['i']['x'] = ($CratioX*$Acid_Atot_TAR)/calcTHETA($i,$x,$H);
$myAtot += $Acid['i']['x']; // assume efficient compiler.
echo "I.... $i";
echo $bb;
echo "X.... $x";
echo $bb;
echo $Acid['i']['x'];
echo $bb;
$i ++;
};
while ($i < 4)
{ $Acid['i']['x'] = 0; // simple precaution. B+B. Not useful.
$i++;
}
$Acid[$Atot][$x] = $myAtot;
};
// note the calcTHETA in the above has previously been performed for most
// (inefficient).
}
// here we have the subsidiary function that calculates THETA values:
//public double calcTHETA (int ion, int acid, double H)
//{ // as per documentation, let's not fiddle around!
// double finalval;
// first calculate all K(i,acid)/[H] values:
// double Ka0H;
// double Ka1H;
// double Ka2H;
function calcTHETA($ion,$acid,$H)
{
extract($GLOBALS);
$bb="
";
// put an if here to get kd_k1 of acide matching iMaxX
if ($acid == 0)
{
$acd="TAR";
$Kd_K1_acid = $kd_K1_TAR;
$Kd_K2_acid = $kd_K2_TAR;
$Kd_K3_acid = $kd_K3_TAR;
//echo $Kd_K1_acid;
}
if ($acid == 1)
{
$acd="MAL";
//echo $acd;
$Kd_K1_acid = $kd_K1_MAL;
$Kd_K2_acid = $kd_K2_MAL;
$Kd_K3_acid = $kd_K3_MAL;
}
if ($acid == 2)
{
$acd="CIT";
//echo $acd;
$Kd_K1_acid = $kd_K1_CIT;
$Kd_K2_acid = $kd_K2_CIT;
$Kd_K3_acid = $kd_K3_CIT;
}
if ($acid == 3)
{
$acd="SUC";
//echo $acd;
$Kd_K1_acid = $kd_K1_SUC;
$Kd_K2_acid = $kd_K2_SUC;
$Kd_K3_acid = $kd_K3_SUC;
}
if ($acid == 4)
{
$acd="LAC";
//echo $acd;
$Kd_K1_acid = $kd_K1_LAC;
$Kd_K2_acid = $kd_K2_LAC;
$Kd_K3_acid = $kd_K3_LAC;
}
if ($acid == 5)
{
$acd="ACE";
//echo $acd;
$Kd_K1_acid = $kd_K1_ACE;
$Kd_K2_acid = $kd_K2_ACE;
$Kd_K3_acid = $kd_K2_ACE;
}
$acid="[$acd]";
//echo $acid;
//echo $bb;
$Ka0H = ($Kd_K1_acid)/$H; // will never be zero
$Ka1H = ($Kd_K2_acid)/$H; // these may be zero (signals NO K1, K2)
$Ka2H = ($Kd_K3_acid)/$H; //
//echo "ACIDS:$bb KDK1 $Kd_K1_acid $bb KDK2 $Kd_K2_acid $bb KDK3 $Kd_K3_acid $bb END ACIDS $bb $bb";
//echo "KA0H: $Ka0H $bb Ka1H: $Ka1H $bb Ka2H: $Ka2H $bb $bb";
// rather than getting tricksy, simply switch:
switch ($ion)
{ case 0:
$finalval = 1 + $Ka0H * ( 1 + $Ka1H * ( 1 + $Ka2H) );
//echo "THIS IS FINAL VAL 0 $bb";
break;
//
case 1:
$finalval = 1 + 1/$Ka0H + $Ka1H * (1 + $Ka2H);
//echo "THIS IS FINAL VAL 1 $bb";
break;
//
case 2:
if ($Ka1H == 0)
{ $finalval = 100000000000000000000; //arb large value safeguard
} else
{ $finalval = 1 + $Ka2H + (1 + (1/$Ka0H))/$Ka1H;
};
//echo "THIS IS FINAL VAL 2 $bb";
break;
//
case 3:
if (($Ka1H == 0) || ($Ka2H == 0))
{ $finalval = 100000000000000000000; //arb large value safeguard
} else
{ $finalval = 1 + (1 + (1 + 1/$Ka0H)/$Ka1H)/$Ka2H;
};
//echo "THIS IS FINAL VAL 3 $bb";
break;
//
default: // ideally should have error message!!
return 0; // will force overflow and terminate ???
};
//echo "FINAL VAL:";
//echo $finalval;
//echo $bb;
//echo $bb;
return $finalval;
}
?>