#!/usr/bin/env python3 # -*- coding: utf-8 -*- # *************************************************************************** # * Copyright (C) 2011, 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 sys from math import * # see http://arachnoid.com/TankCalc/volumes_in_depth.html # the functions vfy() and yfv() take these arguments: # r_a: one radius of a conical frustum # r_b: the other radius of a conical frustum # h: the overall height of the conical frustum # y (vfy): the height for which a partial volume is required, 0 <= y <= h # v (yfv): the partial volume for which a height is required # conical frustum volume for height def vfy(r_a,r_b,h,y): return 1.0/3.0*(3*h**2*r_a**2*y + (r_a**2 - 2*r_a*r_b + r_b**2)*y**3 - 3*(h*r_a**2 - h*r_a*r_b)*y**2)*pi/h**2 # conical frustum height for volume def yfv(r_a,r_b,h,v): return h*r_a/(r_a - r_b) - (pi*h*r_a**3 - 3*(r_a - r_b)*v)**(1.0/3.0)*(h**2)**(1.0/3.0)/((r_a - r_b)*pi**(1.0/3.0)) def process(r_a,r_b,h,x,inverse): if(inverse): r = yfv(r_a,r_b,h,x) else: r = vfy(r_a,r_b,h,x) print('%16.4f = %16.4f' % (x,r)) if(len(sys.argv) < 5): sys.stderr.write('usage: [-i inverse] r_a r_b h [y or start end step]\n') sys.exit(0) inverse = False coms = sys.argv[1:] if(coms[0] == '-i'): inverse = True coms.pop(0) step = y = None # must have four arguments as a minimum try: r_a,r_b,h = [float(x) for x in coms[:3]] coms = coms[3:] try: ys,ye,step = [float(x) for x in coms] except: y = float(coms[0]) except: None if(y != None): process(r_a,r_b,h,y,inverse) elif(step > 0 and ye > ys): print('%16s %16s' % ('Argument','Result')) print('-' * 40) y = ys while(y <= ye): process(r_a,r_b,h,y,inverse) y += step else: sys.stderr.write('Entry error -- try again.\n')