Reputation: 139
I wrote a simple Python code to solve a certain Hydraulic formula (The Manning's equation):
import math
def mannings(units,A,P,S,n):
if units=='SI':
k=1.0
elif units=='US':
k=1.49
R=A/P
V=(k/n)*(math.pow(R,(2/3)))*(math.sqrt(S))
Q=A*V
return R,V,Q
In the code above, the velocity V
is calculated from the k
, n
, R
and S
. The velocity is then used to calculate the discharge Q
by multiplying with Area A
. The user inputs the unit convention, the A
, P
, S
and n
. k
is decided on the basis of unit convention.
When I run the function using mannings('US',1.0618,2.7916,0.02,0.015)
, I get (0.38035535176959456, 14.047854719572745, 14.916012141242343)
. The R
value matches the R
calculated in a spreadsheet, but the V
and Q
are way off. The actual V
should be 7.374638178
and the Q
should be 7.830634155
.
It'd be great if someone can tell me what's going wrong here. This is a pretty straightforward formula and I was guessing it should work easily.
Upvotes: 3
Views: 859
Reputation: 184071
Your problem is that 2/3
is an integer division and therefore evaluates to 0
. You want 2.0/3
to force a floating-point division. Or else include from __future__ import division
at the top of your file to use the Python 3-style division in Python 2.x.
Assuming you don't use the __future__
solution, you will also want to write your R = A / P
as e.g. R = float(A) / P
because otherwise, if A
and P
are both integers, R
will also be an integer.
Upvotes: 14