Optimal Salt Addition Calculator

pruby

New Member
Trial Member
Joined
Apr 13, 2016
Messages
8
Reaction score
0
Points
1
A useful feature would, rather than fiddling with the salts, be to have the optimum addition to match a profile pre-calculated. I've done some linear optimisation before, and this is a classic case.

UI: User checks boxes for which salts are available for use. Could either start with all checked, or look at the user's inventory to determine which salts are available by default. User hits an "Optimize" button to kick off the process.

Implementation: You'd be best off using a linear constraint solver library or a minimal implementation of the Simplex algorithm.

Problem format: We have a series of salts (those checked as available), and a series of ions, and construct a linear constraint system as:

  • Qx: quantity of salt x to add in units (>= 0)
  • Ey: excess of ion y (>= 0)
  • Uy: under-amount of ion y (>= 0)

And have the following other pre-calculated values (from water volume, etc):

  • Rx,y: rate at which adding one unit of salt x increases concentration of ion y
  • Iy: initial concentration of ion y
  • Ty: target concentration of ion y

Our constraints are that:

For each ion y:
Ey >= Sum(For all salts x -> Rx,y * Qy) + Iy - Ty
Uy >= Ty - Iy - Sum(For all salts x -> Rx,y * Qy)

This gives us variables in the problem indicating the excess and/or under amount of each ion. A simple goal to optimise would be to:

Minimise Sum(For all ions y -> Ey + Uy)

You could add default weights to those as how bad 1 unit over of any given ion is, vs. how bad it is under, vs. each of those for another ion. UI to configure those would be complicated, so probably best to just fix sensible values.

I would be happy to write the optimisation routine in some free time if you're interested. I gather you use PHP?

Cheers,

Tim
 
This would be a great feature! +1 for sure!
 
Right, I've implemented a first cut version of an optimisation routine in PHP. It handles optimising salt additions for the stable, linearly calculated ions (Ca, Cl, Mg, etc). There are a couple of limitations:

  • Doesn't handle alkalinity - this doesn't seem to be a linear approximation. I'd need to see the original water calculator code to incorporate this.
  • Default salt additions are all metric, someone with a sense for imperial units should add those.

The code is up at https://github.com/pruby/brew-salt-optimiser . The files are:

  • simplex_optimiser.php - a pretty bad implementation of two-phase Simplex in PHP. While a proper library would be better, this doesn't require installing anything. Works for this case, probably won't for bigger problems.
  • brew_salt_optimiser.php - given a source water profile and target water profile returns the salt concentrations to add (by default mg/L of each ion as inputs, g/L of each salt to add as outputs, could easily support imperial units by changing constants though).
  • test_brew_salt_optimiser.php - a test script converting my water supply to Balanced Profile I. Note that it can't hit the perfect balance - something has to be a bit off but it minimises the total error.

Cheers,

Tim
 
Update - now does bicarbonate alkalinity. Doesn't do slaked lime or lye yet, as these are based on precipitating other salts and interact with the other additions in non-linear ways.

UI concept image attached - would just be a checkbox for each salt, enable/disable the fields on click, and use those to determine which salts to let the optimiser use.

Plugging this in to my optimiser by hand, it gets very close to the target profile automatically. Worth testing for a range of sources and targets.
 

Attachments

  • Screen Shot 2016-04-17 at 2.48.54 pm.png
    Screen Shot 2016-04-17 at 2.48.54 pm.png
    258 KB · Views: 1,257
  • Screen Shot 2016-04-17 at 2.30.38 pm.png
    Screen Shot 2016-04-17 at 2.30.38 pm.png
    368.9 KB · Views: 1,258
So if I am reading this right? this program ,if integrated into BF would automatically calculate the salt addition's once you selected your target profile and your starting water profile? I would LOVE to see them do this. I want to use salt's to change build my water but there current calculator is beyond me. PLEASE people at Brewer's Friend make this happen.
 
will this optimal salt calculator take ph into consideration? it sounds just like the one on beer smith that calculates the salts to the required water profile and doesnt take ph into consideration, to be honest i like it how it is, besides people need to learn what salts do not just press a button, if its anything like the one on beer smith its as much as useless getting the salts for a chosen profile when your mash ph is ignored and totally wrong after the salts have been calculated
 
brewmer said:
will this optimal salt calculator take ph into consideration? it sounds just like the one on beer smith that calculates the salts to the required water profile and doesnt take ph into consideration, to be honest i like it how it is, besides people need to learn what salts do not just press a button, if its anything like the one on beer smith its as much as useless getting the salts for a chosen profile when your mash ph is ignored and totally wrong after the salts have been calculated

+1

Honestly, I used Beer Smith in conjunction with Bru'n Water for years. I came over to BF because I was really getting into building water more and more. I found myself needing something more in depth for water, plus the need for it to all be in one program. BF has been awesome for this, and was the sole reason it attracted me to it. I am all for simple but not too simple. I have learned so much about water, ions, and salt additions because of BF, not to mention PH and using lactic acid etc.
Matt...
 
pruby said:
UI concept image attached - would just be a checkbox for each salt, enable/disable the fields on click, and use those to determine which salts to let the optimiser use.

Wow!

First, I vote "premium for life" for building an awesome tool to help the community.

Second, this would be an AWESOME way to differentiate Brewer's Friend from the competition. I already think the web based interface is a huge advantage, and the range of recipe steps/instructions are significantly easier to use than the desktop competitors. Adding in "easy water calculations" would be awesome!
 
Water calculator is simple to use as is but to each their own I suppose.
 

Back
Top