| Home | | Science & Math | | Storage Tank Calculations | | ![]() | ![]() | Submit |
(double-click any word to see its definition)
import java.io.*; public class TankVolume { static double horizontal_tank_volume(double L,double R,double r,double y) { double v = -1.0; if(y >= 0.0 && y <= 2.0*R) { double a = (Math.PI * r * (3.0 * R - y)*y*y)/(3.0 * R); double q = 1.0 / Math.sqrt((2.0 * R / y)-1.0); double b = 2.0 * Math.atan(q) * R * R; double c = Math.sqrt((2.0 * R-y) * y) * (y-R); v = a + L * (b + c); } return v; } static double vertical_tank_volume(double L,double R,double r,double y) { double v = -1.0; if(0.0 <= y && y < r) { v = (Math.PI*R*R*(3.0*r-y)*y*y)/(3.0*r*r); } else if(r <= y && y < r+L) { v = -(Math.PI*R*R/3.0)*(r-3.0*y); } else if (r+L <= y && y <= 2.0*r+L) { v = (Math.PI*R*R/3.0)*((((L+3.0*r-y)*(L-y)*(L-y))/(r*r))+3.0*L); } return v; } static void compute_volume(double L,double R,double r,boolean vert,double y) { double v; if(vert) { v = vertical_tank_volume(L,R,r,y); } else { v = horizontal_tank_volume(L,R,r,y); } System.out.println(y + "," + v); } static public void main(String[] args) throws Exception { if(args.length < 4 || (!args[0].equals("h") && !args[0].equals("v"))) { System.out.println("Usage: (h/v) (horizontal/vertical)"); System.out.println(" L (cylinder length)"); System.out.println(" R (cylinder radius/ellipse major radius)"); System.out.println(" r (ellipse minor radius)"); System.out.println(" [optional step size, default 1.0]"); System.out.println(" All consistent units."); System.out.println("Result: data table of tank content heights and volumes in units^3."); } else { boolean vert = (args[0].equals("v")); double L = Double.parseDouble(args[1]); double R = Double.parseDouble(args[2]); double r = Double.parseDouble(args[3]); double step = (args.length > 4)?Double.parseDouble(args[4]):1.0; double y; double top = (vert)?L+(2*r):2*R; int i = 1; System.out.println("Height,Volume"); while((y = i * step) < top) { compute_volume(L,R,r,vert,y); i++; } compute_volume(L,R,r,vert,top); } } };
To use this program:
This Java application produces a volume result expressed in the input units cubed (e.g. if the input is expressed in inches, the output is expressed in cubic inches). Since this article was originally written, the Java program has been amended to handle the vertical tank case described here.
This Java program is Copyright © 2008, P. Lutus and is released under the GPL. It should be easy to adapt to other languages and environnments.
Click here to download an example spreadsheet (runs with Excel but is compatible with the preferrred, free Open Office program suite) that computes tank volumes using the horizontal tank mathematical method described in this article.
The spreadsheet uses multiple copies of the cell content listed below, plus some variables defined in the spreadsheet, to create a table of volumes for corresponding Y values:
=F*((PI()*_r*(3*R-A12)*A12*A12)/(3*R)+L*(2*ATAN2(SQRT((2*R/A12)-1);1)*R*R+SQRT((2*R-A12)*A12)*(A12-R)))
Where:
| L,R,r | Variables described in this article and defined in the spreadsheet. |
| F | A volume conversion factor, also defined in the spreadsheet. A typical value is 1/231, to convert cubic inches to gallons. |
| A12 | An arbitrary cell address containing a value for Y. Change this address as required and note that it appears multiple times in the equation. |
| Home | | Science & Math | | Storage Tank Calculations | | ![]() | ![]() | Submit |

