Fixes and optimisations for water calculation.

Sun, 30 Dec 2018 22:01:27 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 30 Dec 2018 22:01:27 +0100
changeset 158
3b9e4dfb0476
parent 157
5b2f553175a6
child 159
6428dae0605d

Fixes and optimisations for water calculation.

www/js/rec_edit.js file | annotate | diff | comparison | revisions
--- a/www/js/rec_edit.js	Sun Dec 30 20:10:19 2018 +0100
+++ b/www/js/rec_edit.js	Sun Dec 30 22:01:27 2018 +0100
@@ -331,8 +331,7 @@
 	//Z alkalinity is the amount of acid (in mEq/l) needed to bring water to the target pH (Z pH)
 	function ZAlkalinity(pHZ) {
 		var C43 = Charge(4.3);
-		//var Cw = Charge(parseFloat($("#wg_ph").jqxNumberInput('decimal')));
-		var Cw = Charge(7.0);
+		var Cw = Charge(parseFloat($("#wg_ph").jqxNumberInput('decimal')));
 		var Cz = Charge(pHZ);
 		var DeltaCNaught = -C43+Cw;
 		var CT = parseFloat($("#wg_total_alkalinity").jqxNumberInput('decimal')) / 50 / DeltaCNaught;
@@ -555,7 +554,7 @@
 		MolWt = result.MolWt;
 		AcidSG = result.AcidSG;
 		AcidPrc = result.AcidPrc;
-		console.log(AT+" pK1: "+pK1+" pK2: "+pK2+" pK3: "+pK3+" MolWt: "+MolWt+" AcidSG: "+AcidSG+" AcidPrc: "+AcidPrc);
+	//	console.log(AT+" pK1: "+pK1+" pK2: "+pK2+" pK3: "+pK3+" MolWt: "+MolWt+" AcidSG: "+AcidSG+" AcidPrc: "+AcidPrc);
 
 		if (dataRecord.calc_acid) {
 			TpH = parseFloat(dataRecord.mash_ph);
@@ -712,8 +711,7 @@
 			}
 
 			TpH = parseFloat(dataRecord.mash_ph);
-		//	pHa = MashpH();
-			pHa = parseFloat($("#wb_ph").jqxNumberInput('decimal'));
+			pHa = MashpH();	// This one is in demi water, should be in adjusted water???
 			// Then calculate the new pH with added acids
 			if (parseFloat($("#wa_acid").jqxNumberInput('decimal')) > 0) {
 				console.log("TpH: "+TpH+" water: "+pHa);
@@ -721,8 +719,8 @@
 				if (parseFloat($("#wa_acid_perc").jqxNumberInput('decimal')) == 0)
 					$("#wa_acid_perc").val(AcidPrc);
 				Acid = Acid / AcidPrc * (parseFloat($("#wa_acid_perc").jqxNumberInput('decimal')) / 100); // ml
-				Acid = Acid * AcidSG; // ml
-				Acid = Acid / MolWt;  // mg
+				Acid *= AcidSG; // ml
+				Acid /= MolWt;  // mg
 				Acidmg = Acid;
 
 				//find the pH where the protondeficit = protondeficit by the acid
@@ -731,20 +729,19 @@
 
 				deltapH = 0.001;
 				deltapd = 0.1;
-				pd = ProtonDeficit(TpH);
+				pd = ProtonDeficit(pHa);
 				n = 0;
-				console.log("n: "+n+" pd: "+pd+" protonDeficit: "+protonDeficit+" frac: "+frac+" pHa: "+pHa);
+		//		console.log("n: "+n+" pd: "+pd+" protonDeficit: "+protonDeficit+" frac: "+frac+" pHa: "+pHa);
 
 				while (((pd < (protonDeficit - deltapd)) || (pd > (protonDeficit + deltapd))) && (n < 2000)) {
 					n++;
 					if (pd < (protonDeficit-deltapd))
-						pHa = pHa - deltapH;
+						pHa -= deltapH;
 					else if (pd > (protonDeficit+deltapd))
-						pHa = pHa + deltapH;
+						pHa += deltapH;
 					frac = CalcFrac(pHa, pK1, pK2, pK3);
 					protonDeficit = Acid * frac;
 					pd = ProtonDeficit(pHa);
-			//		console.log("n: "+n+" pd: "+pd+" protonDeficit: "+protonDeficit+" frac: "+frac+" pHa: "+pHa);
 				}
 				console.log("n: "+n+" pd: "+pd+" protonDeficit: "+protonDeficit+" frac: "+frac+" pHa: "+pHa);
 				RA = wg_bicarbonate - protonDeficit * frac / liters;
@@ -832,6 +829,7 @@
 		var TargetpH = dataRecord.sparge_ph;
 		var Source_pH = dataRecord.w1_ph;
 		var Source_alkalinity = dataRecord.w1_total_alkalinity;
+		// Select watersource or fallback to the first source.
 		if (dataRecord.sparge_source == 'Bron 2') {
 			if (dataRecord.w2_ph > 0.0) {
 				Source_pH = dataRecord.w2_ph;
@@ -914,7 +912,6 @@
 		Acid = Math.round(Acid * 100) / 100;
 		dataRecord.sparge_acid_amount = Acid / 1000;
 		$("#sparge_acid_amount").val(Acid);
-		console.log("acid needed: "+Acid);
 	}
 
 	function calcFermentablesFromOG(OG) {

mercurial