Skip to content

Commit e5cbfcd

Browse files
committed
Implement brentq for Reynolds and allow Newton to break after first residual calculation if value is 0
1 parent 9cfc87d commit e5cbfcd

3 files changed

Lines changed: 24 additions & 39 deletions

File tree

src/tespy/components/piping/pipe.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ class Pipe(SimpleHeatExchanger):
294294
... )
295295
>>> nw.solve("design")
296296
>>> round(pi.Q.val, 2)
297-
-2434.12
297+
-2434.13
298298
"""
299299

300300
def _preprocess(self, row_idx):

src/tespy/tools/fluid_properties/helpers.py

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import CoolProp.CoolProp as CP
1717
import numpy as np
18+
from scipy.optimize import brentq
1819

1920
from tespy.tools.global_vars import ERR
2021
from tespy.tools.helpers import central_difference
@@ -219,40 +220,18 @@ def darcy_friction_factor(re, ks, d):
219220
elif re < 1e6:
220221
return hanakov(re)
221222
else:
222-
l0 = 0.02
223-
function_kwargs = {
224-
"function": prandtl_karman,
225-
"parameter": "darcy_friction_factor",
226-
"reynolds": re
227-
228-
}
229-
return newton_with_kwargs(
230-
prandtl_karman_derivative,
231-
0,
232-
val0=l0,
233-
valmin=0.00001,
234-
valmax=0.2,
235-
**function_kwargs
236-
)
223+
try:
224+
return brentq(lambda lam: prandtl_karman(re, lam), 0.00001, 0.2)
225+
except ValueError:
226+
a, b = 0.00001, 0.2
227+
return a if abs(prandtl_karman(re, a)) <= abs(prandtl_karman(re, b)) else b
237228

238229
else:
239-
l0 = 0.002
240-
function_kwargs = {
241-
"function": colebrook,
242-
"parameter": "darcy_friction_factor",
243-
"reynolds": re,
244-
"ks": ks,
245-
"diameter": d,
246-
"delta": 0.001
247-
}
248-
return newton_with_kwargs(
249-
central_difference,
250-
0,
251-
val0=l0,
252-
valmin=0.0001,
253-
valmax=0.2,
254-
**function_kwargs
255-
)
230+
try:
231+
return brentq(lambda lam: colebrook(re, ks, d, lam), 0.0001, 0.2)
232+
except ValueError:
233+
a, b = 0.0001, 0.2
234+
return a if abs(colebrook(re, ks, d, a)) <= abs(colebrook(re, ks, d, b)) else b
256235

257236

258237
def blasius(re):

src/tespy/tools/helpers.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -573,11 +573,22 @@ def newton_with_kwargs(
573573
tol_mode = "abs"
574574

575575
while expr:
576-
# calculate function residual and new value
577576
function_kwargs[parameter] = x
578577
residual = target_value - function(**function_kwargs)
578+
579+
if residual == 0:
580+
break
581+
582+
if tol_mode == 'abs':
583+
expr = abs(residual) >= tol_abs
584+
elif tol_mode == 'rel':
585+
expr = abs(residual / target_value) >= tol_rel
586+
579587
x += residual / derivative(**function_kwargs) * relax
580588

589+
if not expr:
590+
break
591+
581592
# check for value ranges
582593
if x < valmin:
583594
x = valmin
@@ -599,12 +610,7 @@ def newton_with_kwargs(
599610
'iterations.'
600611
)
601612
logger.debug(msg)
602-
603613
break
604-
if tol_mode == 'abs':
605-
expr = abs(residual) >= tol_abs
606-
elif tol_mode == 'rel':
607-
expr = abs(residual / target_value) >= tol_rel
608614

609615
return x
610616

0 commit comments

Comments
 (0)