Added javascript formulas for color calculations. Added IBU total calculation. Better IBI calculation for the hop entries. Blocked selection of IBU calculation for Garetz, Mosher and Noonan. Better sg calculation in the recipe print.

Fri, 16 Nov 2018 16:20:17 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 16 Nov 2018 16:20:17 +0100
changeset 98
92d6bc8a4cdd
parent 97
22c277be7e7e
child 99
f433193f7bb6

Added javascript formulas for color calculations. Added IBU total calculation. Better IBI calculation for the hop entries. Blocked selection of IBU calculation for Garetz, Mosher and Noonan. Better sg calculation in the recipe print.

www/js/global.js file | annotate | diff | comparison | revisions
www/js/rec_edit.js file | annotate | diff | comparison | revisions
www/rec_print.php file | annotate | diff | comparison | revisions
--- a/www/js/global.js	Thu Nov 15 22:36:09 2018 +0100
+++ b/www/js/global.js	Fri Nov 16 16:20:17 2018 +0100
@@ -83,6 +83,41 @@
 
 
 
+function ebc_to_srm(ebc) {
+	return  -1.32303E-12 * Math.pow(ebc, 4) - 0.00000000291515 * Math.pow(ebc, 3) + 0.00000818515 * Math.pow(ebc, 2) + 0.372038 * ebc + 0.596351;
+}
+
+
+
+function srm_to_ebc(srm)
+{
+	var ebc = 0.000000000176506 * Math.pow(srm, 4) + 0.000000154529 * Math.pow(srm, 3) - 0.000159428 * Math.pow(srm, 2) + 2.68837 * srm - 1.6004;
+	return Math.round(ebc);
+}
+
+
+
+/*
+ * Kleurwerking naar SRM
+ */
+function kw_to_srm(colormethod, c) {
+
+	if (colormethod == "Morey")
+		return 1.4922 * Math.pow(c, 0.6859);
+	if (colormethod == "Mosher")
+		return 0.3 * c + 4.7;
+	if (colormethod == "Daniels")
+		return 0.2 * c + 8.4;
+}
+
+
+
+function kw_to_ebc(colormethod, c) {
+	return srm_to_ebc(kw_to_srm(colormethod, c));
+}
+
+
+
 /*
  * Berekeningen uit https://www.hobbybrouwen.nl/forum/index.php/topic,6079.msg69464.html#msg69464
  */
@@ -96,8 +131,6 @@
 	var pfactor = 1.0;
 	var ibu     = 0;
 
-	console.log("toIBU("+Use+"," + Form + "," + SG + "," + Volume + "," + Amount + "," + Boiltime + "," + Alpha + "," + Method + ")");
-
 	if ((Use == "Dry Hop") || (Use == "Dry hop") || (Use == "Whirlpool") || (Use == "Aroma")) {
 		fmoment = 0.0;
 	} else if (Use == "Mash") {
@@ -113,32 +146,12 @@
 		pfactor += /* Settings.PlugFactor.Value = 2% */ 2 / 100;
 	}
 
-	// TODO:  sg = (postBoilGravity - 1.0) * batchSize / boilSize;
-
 	if (Method == "Tinseth") {
-		/*
-		 * http://realbeer.com/hops/research.html
-		 *
-		 *                             decimal AA rating * grams hops * 1000
-		 * mg/l of added alpha acids = -------------------------------------
-		 *			         volume of finished beer in liters
-		 *
-		 * Bigness factor = 1.65 * 0.000125^(wort gravity - 1)
-		 *
-		 *                    1 - e^(-0.04 * time in mins)
-		 * Boil Time factor = ----------------------------
-		 *                               4.15
-		 *
-		 * decimal alpha acid utilization = Bigness factor * Boil Time factor
-		 *
-		 * IBUs = decimal alpha acid utilization * mg/l of added alpha acids
-		 */
+		/* http://realbeer.com/hops/research.html */
 		var AddedAlphaAcids = (alpha * mass * 1000) / liters;
 		var Bigness_factor = 1.65 * Math.pow( 0.000125, gravity - 1);
-		var BoilTime_factor = ((1 - Math.exp(-0.04 * time)) / 4.15);		// Glen Tinseth
+		var BoilTime_factor = ((1 - Math.exp(-0.04 * time)) / 4.15);
 		var utiisation = Bigness_factor * BoilTime_factor;
-
-		console.log(" AddedAlphaAcids:"+AddedAlphaAcids+"  Bigness_factor:"+Bigness_factor+"  BoilTime_factor:"+BoilTime_factor+"  utilisation:"+utiisation);
 		ibu = Math.round(utiisation * AddedAlphaAcids * fmoment * pfactor * 10) / 10.0;
 	}
 	if (Method == "Daniels") {
@@ -165,7 +178,7 @@
 		ibu = Math.round((mass * (alpha * 100) * boilfactor * 0.1) / (liters * (1 + sgfactor)) * 10) / 10;
 	}
 
-	console.log(" fmoment:"+fmoment+"  pfactor:"+pfactor+"  IBU:"+ibu);
+	console.log("toIBU("+Use+"," + Form + "," + SG + "," + Volume + "," + Amount + "," + Boiltime + "," + Alpha + "," + Method + ") :" + ibu);
 	return ibu;
 }
 
--- a/www/js/rec_edit.js	Thu Nov 15 22:36:09 2018 +0100
+++ b/www/js/rec_edit.js	Fri Nov 16 16:20:17 2018 +0100
@@ -52,17 +52,13 @@
 
 	console.log("record:" + my_record + "  return:" + my_return + "  theme:" + theme);
 
-//	$("#jqxNotification").jqxNotification({ width: "auto", position: "top-right", opacity: 0.9,
-//		autoOpen: false, closeOnClick: true, autoClose: true, template: "info", blink: false,
-//		icon: { width: 25, height: 25, url: '../../images/smiley.png', padding: 5 }
-//	});
-
 	function calcFermentables() {
 		console.log("calcFermentables()");
-		sugarsf = 0;	// Sugar weight
+		sugarsf = 0;
 		sugarsm = 0;
-		psugar = 0;	// Percentage real sugars
-		pcara = 0;	// Percentage cara/crystal malts
+		psugar = 0;
+		pcara = 0;
+		var colorw = 0;	// Colors working
 
 		var rows = $('#fermentableGrid').jqxGrid('getboundrows');
 		for (var i = 0; i < rows.length; i++) {
@@ -77,19 +73,48 @@
 				sugarsm += d;
 			}
 			sugarsf += d;
-			// colorw here too
+			colorw += row.f_amount * ebc_to_srm(row.f_color) / parseFloat($("#batch_size").jqxNumberInput('decimal')) * 8.34436;
 		}
 		$('#est_og').val(estimate_sg(sugarsf, parseFloat($("#batch_size").jqxNumberInput('decimal'))));
 		preboil_sg = estimate_sg(sugarsm, parseFloat($("#boil_size").jqxNumberInput('decimal')));
+		$('#est_color').val(kw_to_ebc($("#color_method").val(), colorw));
+	};
+
+	function calcIBUs() {
+		console.log("calcIBUs()");
+
+		var total_ibus = 0;
+		var rows = $('#hopGrid').jqxGrid('getboundrows');
+		for (var i = 0; i < rows.length; i++) {
+			var row = rows[i];
+
+			total_ibus += toIBU(row.h_useat,
+					row.h_form,
+					preboil_sg,
+					parseFloat($("#batch_size").jqxNumberInput('decimal')),
+					parseFloat(row.h_amount),
+					parseFloat(row.h_time),
+					parseFloat(row.h_alpha),
+					$("#ibu_method").val()
+					);
+		}
+		console.log("IBUs: " + total_ibus);
+		$('#est_ibu').val(total_ibus);
+		$('#hopGrid').jqxGrid('render');
 	};
 
 	function calcInit () {
 		console.log("calc.init()");
 //		$('#est_og').on('change', function (event) { calcFermentables(); });
-		$('#efficiency').on('change', function (event) { calcFermentables(); });
-		$('#batch_size').on('change', function (event) { calcFermentables(); });
-		$('#boil_time').on('change', function (event) { calcFermentables(); });
-		$('#fermentableGrid').on('change', function (event) { calcFermentables(); });
+		$('#color_method').on('change', function (event) { calcFermentables(); });
+		$('#ibu_method').on('change', function (event) {
+			calcFermentables();
+			calcIBUs(); 
+		});
+//		$('#efficiency').on('change', function (event) { calcFermentables(); });
+//		$('#batch_size').on('change', function (event) { calcFermentables(); });
+//		$('#boil_time').on('change', function (event) { calcFermentables(); });
+//		$('#fermentableGrid').on('change', function (event) { calcFermentables(); });
 	};
 
 	// Styles dropdown list
@@ -642,6 +667,7 @@
                                 });
                         },
 			ready: function() {
+				calcIBUs();
 				$('#jqxTabs').jqxTabs('next');
 			},
                         columns: [
@@ -672,7 +698,8 @@
 				  cellsrenderer: function (index, datafield, value, defaultvalue, column, rowdata) {
 					  var ibu = toIBU(rowdata.h_useat,
 							  rowdata.h_form,
-							  parseFloat($("#est_og").jqxNumberInput('decimal')),
+							  preboil_sg,
+							  /*parseFloat($("#est_og").jqxNumberInput('decimal')),*/
 							  parseFloat($("#batch_size").jqxNumberInput('decimal')),
 							  parseFloat(rowdata.h_amount),
 							  parseFloat(rowdata.h_time),
@@ -683,7 +710,11 @@
 				  }
 				}
                         ]
-                })
+                });
+		$("#hopGrid").on('cellendedit', function (event) {
+			//calcIBUs();
+			//$('#hopGrid').jqxGrid('sortby', 'f_amount', 'desc');
+		});
         };
 
 	// Inline miscs editor
@@ -1159,6 +1190,7 @@
                                 });
                         },
 			ready: function() {
+				calcInit();
 				$('#jqxTabs').jqxTabs('first');
 			},
                         columns: [
@@ -1219,7 +1251,8 @@
 	// initialize the input fields.
 	var srcType = [ "All Grain", "Partial Mash", "Extract" ];
 	var srcColor = [ "Morey", "Mosher", "Daniels" ];
-	var srcIBU = [ "Tinseth", "Rager", "Garetz", "Daniels", "Mosher", "Noonan" ];
+	//var srcIBU = [ "Tinseth", "Rager", "Garetz", "Daniels", "Mosher", "Noonan" ];
+	var srcIBU = [ "Tinseth", "Rager", "Daniels" ];	// Only these are supported at this time.
 	$("#name").jqxInput({ theme: theme, width: 640, height: 23 });
 	$("#notes").jqxInput({ theme: theme, width: 960, height: 200 });
 	$("#st_name").jqxInput({ theme: theme, width: 250, height: 23 });
@@ -1254,7 +1287,7 @@
 	$("#est_ibu").jqxNumberInput({ inputMode: 'simple', theme: theme, width: 50, height: 23, decimalDigits: 0, readOnly: true });
 	$("#st_ibu_min").jqxNumberInput({ inputMode: 'simple', theme: theme, width: 50, height: 23, decimalDigits: 0, readOnly: true });
 	$("#st_ibu_max").jqxNumberInput({ inputMode: 'simple', theme: theme, width: 50, height: 23, decimalDigits: 0, readOnly: true });
-	$("#ibu_method").jqxDropDownList({ theme: theme, source: srcIBU, width: 125, height: 23, dropDownHeight: 180 });
+	$("#ibu_method").jqxDropDownList({ theme: theme, source: srcIBU, width: 125, height: 23, dropDownHeight: 95, dropDownVerticalAlignment: 'top' });
 
 	$("#est_carb").jqxNumberInput({ inputMode: 'simple', theme: theme, width: 50, height: 23, decimalDigits: 1, readOnly: true });
 	$("#st_carb_min").jqxNumberInput({ inputMode: 'simple', theme: theme, width: 50, height: 23, decimalDigits: 1, readOnly: true });
@@ -1273,8 +1306,6 @@
 		position: 'top'
 	});
 
-	calcInit();
-
 	$("#Print").jqxButton({ template: "info", width: '80px', theme: theme });
 	$("#Print").click(function () {
 		// Open print in a new tab.
--- a/www/rec_print.php	Thu Nov 15 22:36:09 2018 +0100
+++ b/www/rec_print.php	Fri Nov 16 16:20:17 2018 +0100
@@ -113,7 +113,8 @@
 		$this->TableHeader();
 		$this->ProcessingTable=true;
 
-		$sugf = 0;
+		$sugarsf = 0;
+		$sugarsm = 0;
 		$this->SetFont('Helvetica','',9);
 		$this->SetFillColor(250, 195, 65);
 		$arr = json_decode($row['json_fermentables'], true);
@@ -140,9 +141,11 @@
 			$total_fermentables += $amount;
 			/* Calculate the amount of sugars */
 			$d = $amount * ($yield / 100) * (1 - $moisture / 100);
-			if ($added == "Mash")
+			if ($added == "Mash") {
 				$d = floatval($row['efficiency']) / 100 * $d;
-			$sugf += $d;
+				$sugarsm += $d;
+			}
+			$sugarsf += $d;
 			$colorw += ($amount * ebc_to_srm($color) / $row['batch_size']) * 8.34436;
 
 			$this->Cell($vul,5,$name,0,0,'L',true);
@@ -156,8 +159,8 @@
 			$this->Ln();
 		}
 
-		$row['est_og'] = estimate_sg($sugf, floatval($row['batch_size']));
-		$preboil_sg = estimate_sg($sugf, floatval($row['boil_size']));
+		$row['est_og'] = estimate_sg($sugarsf, floatval($row['batch_size']));
+		$preboil_sg = estimate_sg($sugarsm, floatval($row['boil_size']));
 		$this->SetFillColor(210,245,255);
 		$this->Cell($vul+62,5,'',0,0,'L',false);
 		$this->Cell(20,5,sprintf("%8.3f",$total_fermentables),0,0,'R',true);

mercurial