As a fish and shrimp keeper on different tanks with different creatures requiring different water parameters and conditions, it does me good to keep track of all those especially when replacing or changing water, or adjusting parameters for optimal suitability of the creatures.

For example, for the shrimp tank, keeping track of KH, GH are paramount in addition to the usual pH, Nitrite, Nitrate, and of course, Ammonia, Chlorine, Chloramite, temperate values. To that end, not being a chemist, I had to search for reliable math to calculate what happens if I add this much water to top-off or replace this much water…when the new water has these parameters (pH, GH, KH) and the existing/old water has these parameters…what will be the final parameters in the tank after they’re mixed?

These are critical questions and getting the answer, even estimate, saves me time and money (from doing chemical testing along the way and adjusting/readjusting). Because if I can calculate beforehand, I can foresee if I need to adjust at all and if so, by how much. So of course, I wrote a couple of scripts in Python to accomplish just that. They are embedded below. You can run them right on this page (nothing to download…these are simple apps).

The first one estimates the pH of the final water mixture, and the second one estimates the KH and GH of the final water mixture.

When you run the apps, it’ll ask you a few questions about the water you’re adding, and about the water you’re adding ** to**. All inputs about water volume must be in gallons (not liters because the app actually converts the inputs to liters to do internal calculations). And inputs about KH, GH values must be in ppm (not degrees, the app will show the final result in both degrees and ppm units).

You may be wondering, can’t I just take the average of the new water parameters and old water parameters and guess the final mixture values? No! These are not linear. pH is logarithmic; additionally these require calculating of ions concentrations, molarity of hydrogen atoms, Ca2+, Mg2, CO3, HCO3 etc. I explain the math behind how it’s calculated near the end of this post. The math are based on curated and proven formula used in the industry.

**Example I/O session (for pH estimator):**

`How many gallons of water do you want to add? 1`

What is the pH of the water you want to add? 6.5

How many gallons of water are you adding to? 25

What is the pH of the water you are adding to? 7

`The estimated final pH of the water mixture is 6.97`

Now go ahead and try it yourself below. Click in the widget below to run the program and enter your own values.

Similarly, I also want to know the final GH and KH values of a mixture considering their values of the water I am adding, how many gallons I’m adding, and to what amount of water I’m adding, and the old water’s GH, KH parameters.

**Example I/O session (for KH, GH estimator):**

`How many gallons of new water do you want to add? 3`

What is the KH of the new water you want to add? 0

What is the GH of the new water you want to add? 0

How many gallons of old water are you adding to? 25

What is the KH of the old water you are adding to? 70

What is the GH of the old water you are adding to? 180

`The estimated final KH of the water mixture is 62.50 ppm. dKH: 3.5`

The estimated final GH of the water mixture is 160.71 ppm. dGH: 9.0

Now go ahead and try it yourself below. Click in the widget below to run the program and enter your own values.

## The Math Behind the Estimators

**For the pH estimator**: first we need to calculate the concentration of hydrogen ions ([H+]) in the mixed solution, and then use the formula: pH= −log_{10}[H+]

Next, we need to find the molarity of [H+] in each solution, using the formula: [H+]=10^{−pH}

Then, we need to find the total number of moles of [H+] in each solution, by multiplying the molarity by the volume. Next, we need to find the total moles of [H+] in the mixed solution, by adding the moles from each solution.

Finally, we need to find the pH of the mixed solution, by taking the negative logarithm of the molarity of [H+].

**For the KH, GH estimator**: We need to use these foundational formulas: and

- We also need to convert the volumes from gallons to liters, using the conversion factor 1 gallon = 3.78541 liters.
- Next, we need to find the total ppm of Ca2+ and Mg2+}in the tank water, by multiplying the GH by the volume.
- Then, we need to find the total ppm of CO2- and HCO3- in the tank water, by multiplying the KH by the volume.
- Next, we need to find the total volume of the mixed solution, by adding the volumes from each solution.
- Then, we need to find the GH of the mixed solution, by dividing the total ppm of Ca2+ and Mg2+ by the total volume.
- Finally, we need to find the KH of the mixed solution, by dividing the total ppm of CO2- and HCO3- by the total volume.

**That’s a lot of math, but totally worth it!** No more guessing, testing with kits multiple times, waiting in-between. The answers save me time, money, and offers me sense of relief of knowing what I am doing 🙂

Hope you found this helpful. Come back here and run the apps as you need.

For additional posts about Shrimp/fish keeping, be sure to **go to this link** and choose the post that you want to read/use for reference.