0% found this document useful (0 votes)
375 views

VBA Code For Colebrook Equation

This document contains code for several functions: - The coeff function calculates a coefficient using linear interpolation based on input values and a 2D range. - The interp function performs linear interpolation between two x-y points. - The colebrook and colebrook_circ functions calculate friction factors and pressure losses in pipes using the Colebrook equation, iterating until convergence is reached.

Uploaded by

aaditya3015
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
375 views

VBA Code For Colebrook Equation

This document contains code for several functions: - The coeff function calculates a coefficient using linear interpolation based on input values and a 2D range. - The interp function performs linear interpolation between two x-y points. - The colebrook and colebrook_circ functions calculate friction factors and pressure losses in pipes using the Colebrook equation, iterating until convergence is reached.

Uploaded by

aaditya3015
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Attribute VB_Name = "Module1"

Function coeff(tbl As Range, x As Double, y As Double) As Variant


Dim a, B As Integer
Dim var1, var2 As Range
Dim p As Double
Dim q As Double
Dim R As Double
Dim s As Double
Dim x_1 As Double
Dim x_2 As Double
Dim y_1 As Double
Dim y_2 As Double
Dim y1_int As Double
Dim y2_int As Double
Set var1 = WorksheetFunction.Index(tbl, 1, 0)
Set var2 = WorksheetFunction.Index(tbl, 0, 1)
'Set var2 = tb1.Columns(1)
'var2 = "E15: E18"
B = WorksheetFunction.Match(x, var1, 1)
a = WorksheetFunction.Match(y, var2, 1)
'a = 3
'b = 2
p = WorksheetFunction.Index(tbl, a, B)
q = WorksheetFunction.Index(tbl, a, B + 1)
R = WorksheetFunction.Index(tbl, a + 1, B)
s = WorksheetFunction.Index(tbl, a + 1, B + 1)
x_1 = WorksheetFunction.Index(tbl, 1, B)
x_2 = WorksheetFunction.Index(tbl, 1, B + 1)
y_1 = WorksheetFunction.Index(tbl, a, 1)
y_2 = WorksheetFunction.Index(tbl, a + 1, 1)
y1_int = interp(x_1, p, x_2, q, x)
y2_int = interp(x_1, R, x_2, s, x)
coeff = Round(interp(y_1, y1_int, y_2, y2_int, y), 2)
'coeff = b
End Function
Function interp(a1 As Double, b1 As Double, a2 As Double, b2 As Double, a As Dou
ble) As Variant
interp = ((a - a1) / (a2 - a1)) * (b2 - b1) + b1
End Function
Function colebrook(L As Double, B As Double, V As Double, var As Integer) As Var
iant
'this function calculates the friction loss using darcy equation and colebrook e
quation for friction factor
Dim D As Double
Dim R As Double
Dim f(1 To 11) As Variant
Dim fr0(1 To 11) As Variant
Dim f_0 As Double
Dim f_1 As Double
Dim fr1 As Double
Dim frc As Double
frc = 0.09 'Roughness factor of the duct
D = 2 * L * B / (L + B) 'Hydraulic diameter of the duct
R = 66.4 * D * V 'Reynolds number of the flow
fr0(1) = 0.01 'Initial guess value of the friction factor
For i = 1 To 10
f(i) = fr0(i)
f_0 = 1 / Math.Sqr(f(i)) + 2 * WorksheetFunction.Log10(frc / 3.7 / D + 2.51 / R

/ Math.Sqr(f(i)))
f_1 = -(3.4165 * D * Math.Sqr(f(i))) / (3.4165 * D * f(i) ^ (3 / 2) + f(i) ^ 2 *
R) - (1.70825 * D) / (3.4165 * D * f(i) ^ (3 / 2) + f(i) ^ 2 * R) - (0.5 * Math
.Sqr(f(i)) * R) / (3.4165 * D * f(i) ^ (3 / 2) + f(i) ^ 2 * R)
fr0(i + 1) = fr0(i) - f_0 / f_1
Next i
If var = 1 Then
colebrook = f(10) * 1000 * 1.204 * V ^ 2 / (2 * D)
ElseIf var = 2 Then
colebrook = f(10) - f(9)
ElseIf var = 3 Then
colebrook = f(10)
Else
colebrook = MsgBox("Invalid value for var, Enter 1 for Pressure loss, Enter 2 fo
r error term, Enter 3 for friction factor")
End If
End Function
Function colebrook_circ(Dia As Double, V As Double, var As Integer) As Variant
Dim D As Double
Dim R As Double
Dim f(1 To 11) As Variant
Dim fr0(1 To 11) As Variant
Dim f_0 As Double
Dim f_1 As Double
Dim fr1 As Double
Dim frc As Double
frc = 0.09 'Roughness factor of the duct
D = Dia 'Hydraulic diameter of the duct
R = 66.4 * D * V 'Reynolds number of the flow
fr0(1) = 0.01 'Initial guess value of the friction factor
For i = 1 To 10
f(i) = fr0(i)
f_0 = 1 / Math.Sqr(f(i)) + 2 * WorksheetFunction.Log10(frc / 3.7 / D + 2.51 / R
/ Math.Sqr(f(i)))
f_1 = -(3.4165 * D * Math.Sqr(f(i))) / (3.4165 * D * f(i) ^ (3 / 2) + f(i) ^ 2 *
R) - (1.70825 * D) / (3.4165 * D * f(i) ^ (3 / 2) + f(i) ^ 2 * R) - (0.5 * Math
.Sqr(f(i)) * R) / (3.4165 * D * f(i) ^ (3 / 2) + f(i) ^ 2 * R)
fr0(i + 1) = fr0(i) - f_0 / f_1
Next i
If var = 1 Then
colebrook_circ = f(10) * 1000 * 1.204 * V ^ 2 / (2 * D)
ElseIf var = 2 Then
colebrook_circ = f(10) - f(9)
ElseIf var = 3 Then
colebrook_circ = f(10)
Else
colebrook_circ = MsgBox("Invalid value for var, Enter 1 for Pressure loss, Enter
2 for error term, Enter 3 for friction factor")
End If
End Function

You might also like