CSE 111: Programming with Functions

W03 Project: Water Pressure

Purpose

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

Project

Your town needs a volunteer to finish writing and testing a computer program that will be used to help design a new drinking water system.

Background

The small town you live in has received a grant to build and install a freshwater supply system for your community. This will bring great benefit to your community, providing clean water for all. The grant will cover the cost of materials but not the labor to do the job. The city has assembled volunteers to help design and install this new water system.

The team has decided to build an elevated water tank and install a pump that pushes water up to the tank where the water is stored. When a person opens a water faucet, water runs from the tank through pipes to the faucet. Earth’s gravity pulling on the water in the elevated tank pressurizes the water and causes it to flow from the faucet.

A diagram of a city water distribution system that stores
        water in an elevated tank
A diagram of a city water distribution system that stores water in an elevated tank

The team must design the system and ensure water will flow to all buildings. To accomplish this the designer must choose the tower height, pipe type, pipe diameter, and pipe path.

A computer program was written by a volunteer to help with the design, but when the team started to use the program, the numbers didn't seem to be correct. The original programmer is not available to fix the program so you have been asked to help out. Your task is to find out why the program is not working correctly, fix it, and prove that you fixed it. To do this you will need to write unit tests to prove the functions operate according to specifications.

Requirements

The following functions need to be fixed and validated.

  1. Water Column Height

    def water_column_height(tower_height, tank_height):

    h = t +
    3w
    4
    Formula details:

    Use the following formula details to calculate the function's return value.

    • h is height of the water column
    • t is the height of the tower (tower_height)
    • w is the height of the walls of the tank that is on top of the tower (tank_height)
  2. Pressure gain from height

    def pressure_gain_from_water_height(height):

    P =
    ρgh
    1000
    Formula details:

    Use the following formula details to calculate the function's return value.

    • P is the pressure in kilopascals
    • ρ is the density of water 998.2 (kilogram / meter3)
    • g is the acceleration from Earths gravity 9.80665 (meter / second2)
    • h is the height of the water column inmeters (height)
  3. def pressure_loss_from_pipe(pipe_diameter, pipe_length, friction_factor, fluid_velocity):

    P =
    − fLρv2
    2000 d
    Formula details:

    Use the following formula details to calculate the function's return value.

    • P is the lost pressure in kilopascals
    • f is the pipe’s friction factor (friction_factor)
    • L is the length of the pipe in meters (pipe_length)
    • ρ is the density of water 998.2 (kilogram / meter3)
    • v is the velocity of the water flowing through the pipe in meters / second (fluid_velocity)
    • d is the diameter of the pipe in meters (pipe_diameter)
  4. Pressure loss from fittings

    def pressure_loss_from_fittings(fluid_velocity, quantity_fittings):

    P =
    −0.04 ρv2n
    2000 
    Formula details:

    Use the following formula details to calculate the function's return value.

    • 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 (fluid_velocity)
    • n is the quantity of fittings (quantity_fittings)
  5. Reynolds Number

    def reynolds_number(hydraulic_diameter, fluid_velocity):

    R =
    ρdv
    μ
    Formula details:

    Use the following formula details to calculate the function's return value.

    • 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. (hydraulic_diameter)
    • v is the velocity of the water flowing through the pipe in meters / second (fluid_velocity)
    • μ is the dynamic viscosity of water (0.0010016 Pascal seconds)
  6. Pressure loss from pipe reduction

    def pressure_loss_from_pipe_reduction(larger_diameter, fluid_velocity, reynolds_number, smaller_diameter):

    k =
    0.1 +
    50
    R
    D
    d
    4 − 1
    P =
    − kρv2
    2000 
    Formula details:

    Use the following formula details to calculate the function's return value.

    • 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 (reynolds_number)
    • D is the diameter of the larger pipe in meters (larger_diameter)
    • d is the diameter of the smaller pipe in meters (smaller_diameter)
    • 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 (fluid_velocity)
Helpful information.

Milestone

Begin fixing and testing the water_flow.py program which was created by another person. For this milestone you will write test functions to test three of the functions in water_flow.py. You will use the results of the tests to identify problems that you will fix.

Errors:

You will find errors in the provided code. Some functions work, some have errors, and some are not yet implemented. If a test fails it could be one of two reasons, either the function is not implemented correctly, or the test is not implemented correctly. You will have to figure out which one and fix where needed.

  1. Create a folder for this week's project, name it whatever you want.
  2. Download the waterflow.py program and save it in the folder you created.
  3. Open the folder you just created in VSCode.
  4. Create a file named test_water_flow.py in your project directory.
  5. Add the following import statements at the top of your test_water_flow.py file.
    from pytest import approx
    import pytest
  6. Copy and paste the following code at the bottom of your test_water_flow.py file.
    # Call the main function that is part of pytest so that the
    # computer will execute the test functions in this file.
    pytest.main(["-v", "--tb=line", "-rN", __file__])
  7. In your test_water_flow.py file, write a test function named test_water_column_height. This test function must call water_column_height at least four times to verify that it is working correctly. Use the following numbers in your test function.
    Tower
    Height
    Tank Wall
    Height
    Expected Water
    Column Height
    0 0 0
    0 10 7.5
    25 0 25
    48.3 12.8 57.9
    Tips:
    • Don't forget to import the function from water_flow.py
    • Write one test at a time, test it then move on.
  8. In your test_water_flow.py file, write a test function named test_pressure_gain_from_water_height. This test function must call pressure_gain_from_water_height at least three times to verify that it is working correctly. Use the following numbers in your test function.
    Height Expected
    Pressure
    approx
    Absolute
    Tolerance
    0 0 0.001
    30.2 295.628 0.001
    50 489.450 0.001
  9. In your test_water_flow.py file, write a test function named test_pressure_loss_from_pipe. This test function must call pressure_loss_from_pipe at least seven times to verify that it is working correctly. Use the following numbers in your test function.
    Pipe
    Diameter
    Pipe
    Length
    Friction
    Factor
    Fluid
    Velocity
    Expected
    Pressure
    Loss
    approx
    Absolute
    Tolerance
    0.048692 0 0.018 1.75 0 0.001
    0.048692 200 0 1.75 0 0.001
    0.048692 200 0.018 0 0 0.001
    0.048692 200 0.018 1.75 -113.008 0.001
    0.048692 200 0.018 1.65 -100.462 0.001
    0.28687 1000 0.013 1.65 -61.576 0.001
    0.28687 1800.75 0.013 1.65 -110.884 0.001
  10. Run your test_water_flow.py file and ensure that all three of the test functions pass.

Milestone Submission

On or before the due date, return to Canvas and report your progress on this milestone.

Project Completion

Finish your project by completing the remaining test functions and correcting any errors discovered.

  1. 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
  2. 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
  3. 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

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 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. Use your creativity to add features. Add a comment to the top of your code that explains your enhancement(s). Here are a few suggestions for additional features.

Document your enhancements:

If you choose to "exceed requirements" place a comment at the top of your code file that describes what you did to enhance your program.

Project Submission

Return to Canvas and upload your water_flow.py and test_water_flow.py files for feedback.

Don't get a zero!

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.

Useful Links:

  1. Return to: Week Overview | Course Home