Hi, I am trying to make an iterative physics macro, but I have problems figuring out whats wrong. I have had success with a similar macro on another iterative macro earlier.
When I try to debug and run cursor to "P1" line, the formula goes blank and the debugger just stops...
Macro:
Function Navier_Stokes_compressibleflow_pressuredrop(SGavg, Dvisc, ID, T1, P2, T2, Lenght, PipeRoughness, scf_m, Zavg)
'March 2018 , function to calculate pressure drop for a compressible flow system with pure N2 gas
'INPUT:
'ID in mm
'T1 and T2 in celsius
'P2 in Bar
'Lenght in m
'Elevation in m
'Piperoughness in mm
'scf_m in standard cubic feet per minute
'Dvisc in Pa s
m = (scf_m / 35.3146667214886) / 60 ' m3/s
IDM = ID / 1000 ' ID in mm converted to m
T1K = T1 + 273.15 ' T1 in Celius converted to Kelvin
P2p = P2 * 10 ^ 5 'P2 in Bar converted to Pa
T2K = T2 + 273.15 ' T2 in Celius converted to Kelvin
R = 8.3145 'J/mol*K
Mw = 28.01348 'kg/kmol
Pi = 3.14159265358979
A = (IDM ^ 2 * Pi) / 4 'Area m2
Tavg = (T1K + T2K) * 0.5 ' average temperature Kelvin
Rho = SGavg * 1000
Relativeroughness = PipeRoughness / ID
P1new = 50 * 10 ^ 5 'initial guess
For i = 1 To 100
P1 = P1new
Pavg = (P1new + P2p) * 0.5
velocity = (m / A) / ((Pavg / (10 ^ 5)) / Zavg)
Nre = (Rho * velocity * IDM) / Dvisc ' Reynolds nr
Fd = 1.325 / (Log((Relativeroughness / 3.7) + (5.74 / (Nre ^ 0.9)))) ^ 2 'Darcy friction factor by Swamee Jain equation
P1 = (2 * ((R * Tavg) / Mw) * ((m / A) ^ 2) * ((Log(P1new / P2p)) * ((Fd * Lenght) / (2 * IDM))) - P2p ^ 2) ^ 0.5
Navier_Stokes_compressibleflow_pressuredrop = ((2 * ((R * Tavg) / Mw) * ((m / A) ^ 2) * ((Log(P1 / P2p)) * ((Fd * Lenght) / (2 * IDM))) - P2p ^ 2) ^ 0.5) / 10 ^ 5 'output in Bar
If Abs(P1new - P1) < 100 Then Exit Function
Next i
End
End
End Function
When I try to debug and run cursor to "P1" line, the formula goes blank and the debugger just stops...
Macro:
Function Navier_Stokes_compressibleflow_pressuredrop(SGavg, Dvisc, ID, T1, P2, T2, Lenght, PipeRoughness, scf_m, Zavg)
'March 2018 , function to calculate pressure drop for a compressible flow system with pure N2 gas
'INPUT:
'ID in mm
'T1 and T2 in celsius
'P2 in Bar
'Lenght in m
'Elevation in m
'Piperoughness in mm
'scf_m in standard cubic feet per minute
'Dvisc in Pa s
m = (scf_m / 35.3146667214886) / 60 ' m3/s
IDM = ID / 1000 ' ID in mm converted to m
T1K = T1 + 273.15 ' T1 in Celius converted to Kelvin
P2p = P2 * 10 ^ 5 'P2 in Bar converted to Pa
T2K = T2 + 273.15 ' T2 in Celius converted to Kelvin
R = 8.3145 'J/mol*K
Mw = 28.01348 'kg/kmol
Pi = 3.14159265358979
A = (IDM ^ 2 * Pi) / 4 'Area m2
Tavg = (T1K + T2K) * 0.5 ' average temperature Kelvin
Rho = SGavg * 1000
Relativeroughness = PipeRoughness / ID
P1new = 50 * 10 ^ 5 'initial guess
For i = 1 To 100
P1 = P1new
Pavg = (P1new + P2p) * 0.5
velocity = (m / A) / ((Pavg / (10 ^ 5)) / Zavg)
Nre = (Rho * velocity * IDM) / Dvisc ' Reynolds nr
Fd = 1.325 / (Log((Relativeroughness / 3.7) + (5.74 / (Nre ^ 0.9)))) ^ 2 'Darcy friction factor by Swamee Jain equation
P1 = (2 * ((R * Tavg) / Mw) * ((m / A) ^ 2) * ((Log(P1new / P2p)) * ((Fd * Lenght) / (2 * IDM))) - P2p ^ 2) ^ 0.5
Navier_Stokes_compressibleflow_pressuredrop = ((2 * ((R * Tavg) / Mw) * ((m / A) ^ 2) * ((Log(P1 / P2p)) * ((Fd * Lenght) / (2 * IDM))) - P2p ^ 2) ^ 0.5) / 10 ^ 5 'output in Bar
If Abs(P1new - P1) < 100 Then Exit Function
Next i
End
End
End Function