Skip to content

Instantly share code, notes, and snippets.

@serac
Created October 23, 2012 01:19
Show Gist options
  • Save serac/3936097 to your computer and use it in GitHub Desktop.
Save serac/3936097 to your computer and use it in GitHub Desktop.
Fuel Tank Measuring Stick Calibration
#!/usr/bin/env python
# Computes the volume of a fuel tank as a function of height from the bottom.
# The computation is intended to support creating a dipstick to measure
# fuel remaining as a function of fuel depth obtained from the dipstick.
# Tank shape is assumed to be the common one where the cross section is
# composed of a semicircle on the top and bottom separated by a rectangular
# region of arbitrary size.
# The input to the script are tank measurements length, width, and height
# in inches.
from math import acos, pi, sqrt
from os.path import basename
import sys
"""Gallons per cubic inch"""
GAL_PER_CI = 0.004329
"""Liters per cubic inch"""
L_PER_CI = 0.0163871
def a_chord(R, r):
"""Computes the area of a chord as a function of radius, R, and apothem, r."""
return R**2 * acos(r/R) - r * sqrt(R**2 - r**2)
def v_lo(W, L, z):
"""Computes volume for bottom semicircular region."""
R = W/2
return L * a_chord(R, R - z)
def v_mid(W, L, z):
"""Computes volume for region up to bottom of top semicircular region."""
R = W/2
return v_lo(W, L, R) + L * W * (z - R)
def v_hi(W, L, z):
"""Computes volume for region up to top of tank."""
R = W/2
h2 = H - R
top = (pi * R**2)/2 - a_chord(R, z - h2)
return v_mid(W, L, h2) + L * top
if len(sys.argv) < 4:
print """USAGE: %s length width height
where units are in inches""" % basename(sys.argv[0])
sys.exit(0)
try:
L = float(sys.argv[1])
W = float(sys.argv[2])
H = float(sys.argv[3])
except:
print 'Invalid tank dimensions. Integer or decimal input required.'
sys.exit(0)
for z in range(1, int(H)+1, 1):
if z < W/2:
V = v_lo(W, L, z)
elif z <= H - W/2:
V = v_mid(W, L, z)
else:
V = v_hi(W, L, z)
print '%3d in %7.0f gal %8.0f L' % (z, V * GAL_PER_CI, V * L_PER_CI)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment