/************************************************************************** * Copyright (C) 2008, Paul Lutus * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ 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); } } };