Home | Science & Math | Storage Tank Calculations |     Share This Page
Storage Tank Volume Calculations: Programs
P. LutusMessage Page

Copyright © 2008, P. Lutus

Java Program | Spreadsheet

(double-click any word to see its definition)

Java Program (for both vertical and horizontal tanks)
Here is a sample Java application listing that computes vertical and horizontal tank volumes using the methods outlined in this article:

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:

  • Download the Java source file by clicking here.
  • Install Java if you haven't already.
  • Open a command window (Linux: shell session).
  • Move to the directory where you have downloaded the Java source file.
  • Compile the Java program like this:
    • javac TankVolume.java
  • Alternative: download this precompiled class file and put it in your working directory.
  • Run the program this way:
    • java TankVolume
  • The program will list its required inputs.
  • To create a table of values for a horizontal tank:
    • java TankVolume h (L value) (R value) (r value) (optional step size)
  • The above entry will produce a comma-separated value table suitable for pasting into a spreadsheet program. To save the program's result directly into a file, do this:
    • java TankVolume h (L value) (R value) (r value) (optional step size) > filename.csv
  • The file "filename.csv" can then be opened using a database, spreadsheet or other program, many of which will know how to interpret the file as saved.

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.

Spreadsheet (horizontal tank only)

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.

To use the spreadsheet, create a column of Y values that meets your range requirements and place a copy of the above cell contents adjacent to each Y value. Change the relative address "A12" to the actual address required. The sample spreadsheet should serve as an example of how this is done.

 

Home | Science & Math | Storage Tank Calculations |     Share This Page