CSE 111: Programming with Functions

W03 Testing and Fixing Functions

Purpose

Prove that you can write a Python program and write and run test functions to help you find and fix mistakes in your program.

Assignment

Write the second half of the Python program to help an engineer design a water distribution system. Also, write more test functions that will automatically verify that your program functions work correctly.

Helpful Documentation

Help from a Tutor

As a BYU-Pathway student you can get help from a tutor to help you complete your CSE 111 assignments. Each tutor is a current student employee. Meeting with a tutor is free. It will not cost you any money to meet with a tutor. To get help from a tutor, you simply make an appointment and then meet with the tutor. Tutors typically use Zoom to meet with students. To make an appointment, use the tutoring link on the course home page.

Steps

Do the following:

  1. In your water_flow.py program, write a function named pressure_loss_from_fittings that calculates the water pressure lost because of fittings such as 45° and 90° bends that are in a pipeline. The function must have this header:
    def pressure_loss_from_fittings(
            fluid_velocity, quantity_fittings):

    In your function, use the following formula for calculating pressure loss from pipe fittings.

    P =
    −0.04 ρv2n
    2000 
    where
    • P is the lost pressure in kilopascals
    • ρ is the density of water (998.2 kilogram / meter3)
    • v is the velocity of the water flowing through the pipe in meters / second
    • n is the quantity of fittings
  2. In your test_water_flow.py file, write a test function named test_pressure_loss_from_fittings. This test function must call pressure_loss_from_fittings at least five times to verify that it is working correctly. Use the following numbers in your test function.
    Fluid
    Velocity
    Quantity
    of Fittings
    Expected
    Pressure
    Loss
    approx
    Absolute
    Tolerance
    0 3 0 0.001
    1.65 0 0 0.001
    1.65 2 -0.109 0.001
    1.75 2 -0.122 0.001
    1.75 5 -0.306 0.001
  3. In your water_flow.py program write a function named reynolds_number that calculates and returns the Reynolds number for a pipe with water flowing through it. The Reynolds number is a unitless ratio of the inertial and viscous forces in a fluid that is useful for predicting fluid flow in different situations. The function must have this header.
    def reynolds_number(hydraulic_diameter, fluid_velocity):

    In your function, use the following formula for calculating the Reynolds number.

    R =
    ρdv
    μ
    where
    • R is the Reynolds number
    • ρ is the density of water (998.2 kilogram / meter3)
    • d is the hydraulic diameter of a pipe in meters. For a round pipe, the hydraulic diameter is the same as the pipe’s inner diameter.
    • v is the velocity of the water flowing through the pipe in meters / second
    • μ is the dynamic viscosity of water (0.0010016 Pascal seconds)
  4. In your test_water_flow.py file, write a test function named test_reynolds_number. This test function must call reynolds_number at least five times to verify that it is working correctly. Use the following numbers in your test function.
    Hydraulic
    Diameter
    Fluid
    Velocity
    Expected
    Reynolds
    Number
    approx
    Absolute
    Tolerance
    0.048692 0 0 1
    0.048692 1.65 80069 1
    0.048692 1.75 84922 1
    0.28687 1.65 471729 1
    0.28687 1.75 500318 1
  5. In your water_flow.py program write a function named pressure_loss_from_pipe_reduction that calculates the water pressure lost because of water moving from a pipe with a large diameter into a pipe with a smaller diameter. The function must have this header.
    def pressure_loss_from_pipe_reduction(larger_diameter,
            fluid_velocity, reynolds_number, smaller_diameter):

    In your function, use the following two formulas for calculating pressure loss from a rounded reduction in a pipe’s diameter.

    k =
    0.1 +
    50
    R
    D
    d
    4 − 1
    P =
    − kρv2
    2000 
    where
    • k is a constant computed by the first formula and used in the second formula
    • R is the Reynolds number that corresponds to the pipe with the larger diameter
    • D is the diameter of the larger pipe in meters
    • d is the diameter of the smaller pipe in meters
    • P is the lost pressure kilopascals
    • ρ is the density of water (998.2 kilogram / meter3)
    • v is the velocity of the water flowing through the larger diameter pipe in meters / second
  6. In your test_water_flow.py file, write a test function named test_pressure_loss_from_pipe_reduction. This test function must call pressure_loss_from_pipe_reduction at least three times to verify that it is working correctly. Use the following numbers in your test function.
    Larger
    Diameter
    Fluid
    Velocity
    Reynolds
    Number
    Smaller
    Diameter
    Expected
    Pressure
    Loss
    approx
    Absolute
    Tolerance
    0.28687 0 1 0.048692 0 0.001
    0.28687 1.65 471729 0.048692 -163.744 0.001
    0.28687 1.75 500318 0.048692 -184.182 0.001
  7. Copy and paste the following code at the bottom of your water_flow.py program.
    PVC_SCHED80_INNER_DIAMETER = 0.28687 # (meters)  11.294 inches
    PVC_SCHED80_FRICTION_FACTOR = 0.013  # (unitless)
    SUPPLY_VELOCITY = 1.65               # (meters / second)
    HDPE_SDR11_INNER_DIAMETER = 0.048692 # (meters)  1.917 inches
    HDPE_SDR11_FRICTION_FACTOR = 0.018   # (unitless)
    HOUSEHOLD_VELOCITY = 1.75            # (meters / second)
    def main():
        tower_height = float(input("Height of water tower (meters): "))
        tank_height = float(input("Height of water tank walls (meters): "))
        length1 = float(input("Length of supply pipe from tank to lot (meters): "))
        quantity_angles = int(input("Number of 90° angles in supply pipe: "))
        length2 = float(input("Length of pipe from supply to house (meters): "))
        water_height = water_column_height(tower_height, tank_height)
        pressure = pressure_gain_from_water_height(water_height)
        diameter = PVC_SCHED80_INNER_DIAMETER
        friction = PVC_SCHED80_FRICTION_FACTOR
        velocity = SUPPLY_VELOCITY
        reynolds = reynolds_number(diameter, velocity)
        loss = pressure_loss_from_pipe(diameter, length1, friction, velocity)
        pressure += loss
        loss = pressure_loss_from_fittings(velocity, quantity_angles)
        pressure += loss
        loss = pressure_loss_from_pipe_reduction(diameter,
                velocity, reynolds, HDPE_SDR11_INNER_DIAMETER)
        pressure += loss
        diameter = HDPE_SDR11_INNER_DIAMETER
        friction = HDPE_SDR11_FRICTION_FACTOR
        velocity = HOUSEHOLD_VELOCITY
        loss = pressure_loss_from_pipe(diameter, length2, friction, velocity)
        pressure += loss
        print(f"Pressure at house: {pressure:.1f} kilopascals")
    if __name__ == "__main__":
        main()

Testing Procedure

Verify that your test program works correctly by following each step in this procedure:

  1. Run your test_water_flow.py file and ensure that all six of the test functions pass. If any of the test functions don’t pass, there is a mistake in either the program function that you wrote or the test function that you wrote. Read the output from pytest, fix the mistake, and run the test_water_flow.py file again until all the test functions pass.
    > python test_water_flow.py
    ========================== test session starts ==========================
    platform win32 -- Python 3.11.1, pytest-7.2.1, pluggy-1.0.0 --
    rootdir: C:\Users\cse111\week03
    collected 3 items
    test_water_flow.py::test_water_column_height PASSED                [ 16%]
    test_water_flow.py::test_pressure_gain_from_water_height PASSED    [ 33%]
    test_water_flow.py::test_pressure_loss_from_pipe PASSED            [ 50%]
    test_water_flow.py::test_pressure_loss_from_fittings PASSED        [ 66%]
    test_water_flow.py::test_reynolds_number PASSED                    [ 83%]
    test_water_flow.py::test_pressure_loss_from_pipe_reduction PASSED  [100%]
    =========================== 3 passed in 0.08s ===========================
  2. Run your finished water_flow.py program. Enter the input shown below and ensure that your program prints the output shown below.
    Height of water tower (meters): 36.6
    Height of water tank walls (meters): 9.1
    Length of supply pipe from tank to lot (meters): 1524.0
    Number of 90° angles in supply pipe: 3
    Length of pipe from supply to house (meters): 15.2
    Pressure at house: 158.7 kilopascals
    

Exceeding the Requirements

If your program fulfills the requirements for this assignment as described in the previous prove milestone and the Assignment section above, your program will earn 93% of the possible points. In order to earn the remaining 7% of points, you will need to add one or more features to your program so that it exceeds the requirements. Here are a few suggestions for additional features that you could add to your program if you wish.

Ponder

To complete this prove assignment, you wrote at least six program functions that perform calculations. Some of the calculations are complex, especially the calculation for pressure loss from pipe reduction. You also wrote a test function for each program function except main. Did the test functions help you find mistakes in your program functions? Can you see why it is important to write and run test functions when you are developing a large program?

Submission

To submit your program, return to Canvas and do these two things:

  1. Upload your water_flow.py and test_water_flow.py files for feedback.
  2. Add a submission comment that specifies the grading category that best describes your program along with a one or two sentence justification for your choice. The grading criteria are:
    1. Some attempt made
    2. Developing but significantly deficient
    3. Slightly deficient
    4. Meets requirements
    5. Exceeds requirements
NOTE: If an error prevents your program from running to completion the grader will score your assignment with a 0 and request that you fix and resubmit your program. In other words, rather than submitting a program that doesn't work, it's best to ask for help to understand how to fix the problem before submitting your program.

Up Next

Other Links: