import math

def sound_pressure_level_to_pa(dB):
    """Convert sound pressure level in dB to Pascals."""
    return 20e-6 * 10**(dB / 20)

def deflection_of_wall(pressure, wall_thickness, young_modulus, poisson_ratio, length):
    """Calculate deflection of a wall under pressure load using Young's Modulus and Poisson's Ratio."""
    # Simplified formula for deflection due to pressure load on a wall
    deflection = (pressure * length**4) / (young_modulus * wall_thickness**3 * (1 - poisson_ratio**2))
    return deflection

def calculate_cube_box_displacement(internal_dimension_cm, wall_thickness_cm, bracing_per_wall):
    print(f"\nCube of {internal_dimension_cm} cm with {wall_thickness_cm} cm plywood and {bracing_per_wall} braces per wall.\n")
  
    # Given data
    power = 1000  # watts
    efficiency = 90  # dB/W
    subwoofer_spl = 120  # dB
    subwoofer_displacement = 2.5  # liters

    # Constants
    young_modulus = 10e9  # Pa (10 GPa for plywood)
    poisson_ratio = 0.3
    length = internal_dimension_cm / 100  # internal wall length in meters
    wall_thickness_meters = wall_thickness_cm / 100  # Convert plywood thickness to meters

    # Print the cube volume
    print(f"Cube volume: {(internal_dimension_cm/10) ** 3:.2f} liters")

    # Calculate sound pressure in Pascals (Pa)
    sound_pressure = sound_pressure_level_to_pa(subwoofer_spl)

    # Calculate the length of each single flat wall square (unbraced)
    flat_length = length / (bracing_per_wall + 1)
    print(f"Size of each unbraced squared piece of wall: {flat_length * 100:.1f} centimeters")
    
    # Calculate the deflection of a single wall (simplified model)
    deflection = deflection_of_wall(sound_pressure, wall_thickness_meters, young_modulus, poisson_ratio, flat_length)

    # Print the deflection for a single flat wall
    print(f"Deflection of a single flat wall: {deflection * 1000:.6f} millimeters")

    # Calculate the displacement volume for a single wall (as volume change due to deflection)
    wall_area = flat_length ** 2  # area of one wall
    displacement_single_wall = wall_area * deflection / (math.pi/4)  # cubic meters of material displaced by deflection
    displacement_single_wall_liters = displacement_single_wall * 1000  # Convert to liters

    # Total displacement for all 5 walls (since one face has the subwoofer)
    total_displacement = 5 * displacement_single_wall_liters * (bracing_per_wall + 1)**2  # in liters
    print(f"Total leakage due to deflection of the cube walls: {total_displacement:.6f} liters")
    
    return total_displacement

def calculate_cube_box_displacements():
    # Given data
    subwoofer_displacement = 2.5  # liters

    total_displacement = \
      calculate_cube_box_displacement(37, 1.5, 1) +\
      calculate_cube_box_displacement(58, 1.5, 3)

    # Compare with subwoofer displacement
    displacement_percentage = (total_displacement / subwoofer_displacement) * 100
    print(f"\nPercentage of subwoofer displacement leakage: {displacement_percentage:.2f}%")

# Run the calculation
calculate_cube_box_displacements()
