Fixed Point Solver
import numpy as np
def fp(g, x, tol=1E-5, maxit=10000):
for k in range(1, maxit+1):
xnew = g(x)
if np.linalg.norm(xnew-x)/np.linalg.norm(x) <= tol:
return xnew, k
x = xnew
print(f"No convergence it {maxit} iterations")
return x, k
Define the fluids functions
def gfluids(vF):
v = vF[0]
F = vF[1]
rho = 1000. # kg/m3
mu = 1E-3 # Pa*s = kg/m*s
D = 0.1 # m
L = 100. # m
eps = D/100. # m
dP = 101325. # Pa
Re = rho*D*v/mu
F = -np.log10(eps/D/3.7 + 2.51*F/Re)
v = np.sqrt(2*D*dP/rho/L)*F
return np.array([v, F])
Solve and output the result
vFguess = np.array([1.0, 1.0])
vF, nit = fp(gfluids, vFguess)
print(f"v = {vF[0]:.4f} (m/s)")
print(f"f = {1.0/vF[1]**2:.4f}")
print(f"iterations = {nit}")
v = 1.1521 (m/s)
f = 0.1527
iterations = 3