import math

from .. import exceptions, models

TOTAL_HEIGHT = 300


def distance_to_cylindric_volume(distance, radius, height):
    total_vol = math.pi * radius**2 * height
    empty_vol = math.pi * radius**2 * distance
    return total_vol - empty_vol


def distance_to_cuboid_volume(distance, width, length, height):
    total_vol = width * length * height
    empty_vol = width * length * distance
    return total_vol - empty_vol


def distance_to_volume(distance):
    tank = models.config.tank()

    if tank.is_cylindric:
        return distance_to_cylindric_volume(distance, tank.radius, tank.height)

    if tank.is_cuboid:
        return distance_to_cuboid_volume(distance, tank.width, tank.length, tank.height)

@@ -122,30 +122,24 @@
 eight):%0A    
-total_
 vol = math.p
@@ -158,52 +158,17 @@
 2 * 
+(
 height
-%0A    empty_vol = math.pi * radius**2 *
+ -
  dis
@@ -164,32 +164,33 @@
 eight - distance
+)
 %0A    return tota
@@ -185,37 +185,27 @@
  return 
-total_vol - empty_
+max(0, 
 vol
+)
 %0A%0A%0Adef d
@@ -267,22 +267,16 @@
 t):%0A    
-total_
 vol = wi
@@ -294,47 +294,17 @@
 h * 
+(
 height
-%0A    empty_vol = width * length *
+ -
  dis
@@ -308,16 +308,17 @@
 distance
+)
 %0A    ret
@@ -325,29 +325,19 @@
 urn 
-total_vol - empty_
+max(0, 
 vol
+)
 %0A%0A%0Ad

Avoid negative volumes