06 Prove Assignment: Troubleshooting 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 that you began in the previous lesson’s prove milestone. Also, write more test functions that will automatically verify that your program functions work correctly.
Helpful Documentation
- The preparation content for the previous lesson explains how to use
pytest
,assert
, andapprox
to automatically verify that functions are correct. It also contains an example test function and links to additional documentation aboutpytest
. - The preparation content for this lesson explains how to use the Python debugger to watch the computer execute a program and to find mistakes in the program.
Help from a Tutor
As a BYU-Idaho campus or online student you can get help from a tutor to complete your CSE 111 assignments. Each tutor is a current BYU-Idaho student employed by BYU-Idaho. 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. Campus students meet with tutors in the tutoring center. Online students meet with tutors in Zoom. To make an appointment, follow the instructions in the course tutoring guide.
Steps
Do the following:
- In your
water_flow.py
program, write a function namedpressure_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 =where−0.04 ρ v2 n2000- 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
- In your
test_water_flow.py
file, write a test function namedtest_pressure_loss_from_fittings
. This test function must callpressure_loss_from_fittings
at least five times to verify that it is working correctly. Use the following numbers in your test function.Fluid
VelocityQuantity
of FittingsExpected
Pressure
Lossapprox
Absolute
Tolerance0.00 3 0.000 0.001 1.65 0 0.000 0.001 1.65 2 -0.109 0.001 1.75 2 -0.122 0.001 1.75 5 -0.306 0.001 - In your
water_flow.py
program write a function namedreynolds_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 =whereρdvμ- 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)
- In your
test_water_flow.py
file, write a test function namedtest_reynolds_number
. This test function must callreynolds_number
at least five times to verify that it is working correctly. Use the following numbers in your test function.Hydraulic
DiameterFluid
VelocityExpected
Reynolds
Numberapprox
Absolute
Tolerance0.048692 0.00 0 1 0.048692 1.65 80069 1 0.048692 1.75 84922 1 0.286870 1.65 471729 1 0.286870 1.75 500318 1 - In your
water_flow.py
program write a function namedpressure_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 +50RDdP =where− k ρ v22000- 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
- In your
test_water_flow.py
file, write a test function namedtest_pressure_loss_from_pipe_reduction
. This test function must callpressure_loss_from_pipe_reduction
at least three times to verify that it is working correctly. Use the following numbers in your test function.Larger
DiameterFluid
VelocityReynolds
NumberSmaller
DiameterExpected
Pressure
Lossapprox
Absolute
Tolerance0.28687 0.00 1 0.048692 0.000 0.001 0.28687 1.65 471729 0.048692 -163.744 0.001 0.28687 1.75 500318 0.048692 -184.182 0.001 - 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:
- 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 frompytest
, fix the mistake, and run thetest_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\lesson05 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 ===========================
- 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.
- Inside the functions of your
water_flow.py
program, you may have typed numbers for Earth’s acceleration of gravity, the density of water, and the dynamic viscosity of water. Instead of using the numbers inside your functions, define the following constants outside your functions. Then use the constant names in place of the numbers inside your functions.Name Value EARTH_ACCELERATION_OF_GRAVITY 9.8066500 WATER_DENSITY 998.2000000 WATER_DYNAMIC_VISCOSITY 0.0010016 - The functions that you wrote for this assignment, calculate water pressure in kilopascals (kPa). In the United States, water pressure is usually expressed in pounds per square inch (psi). Write a function in your
water_flow.py
program that converts kPa to psi. Then at the bottom of yourmain
function, add code that calls your conversion function and prints the final pressure value in both kPa and psi. - Add a test function to your
test_water_flow.py
file that verifies that your psi-from-kPa conversion function works correctly. In your test function, call your conversion function multiple times so that your test function is a rigorous test.
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 I‑Learn and do these two things:
- Upload your
water_flow.py
andtest_water_flow.py
files for feedback. - 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:
- Some attempt made
- Developing but significantly deficient
- Slightly deficient
- Meets requirements
- Exceeds requirements