Skip to content

Commit 510ae1a

Browse files
committed
parameters limits defined in cal_bernabeu.py
1 parent 5c0bbbe commit 510ae1a

File tree

4 files changed

+21054
-4781
lines changed

4 files changed

+21054
-4781
lines changed

src/IHSetBernabeu/bernabeu.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@ def Bernabeu(A, B, C, D, M, h, xo):
2828
# 4) Always finds the breakpoint by minimizing |x1–x2_interp|
2929
diff = np.abs(x1 - x2_interp)
3030
i_break = np.argmin(diff)
31-
31+
3232
#5) Assemble the complete profile where xi = hi
3333
x = np.empty_like(x1)
3434
x[:i_break] = x1[:i_break]
3535
x[i_break:] = x2_interp[i_break:]
3636

3737
return x, x1, x2, h2
38-
39-

src/IHSetBernabeu/cal_bernabeu.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,35 @@ class cal_Bernabeu(object):
1212
1313
This class reads input datasets, calculates its parameters.
1414
"""
15-
def __init__(self, CM, Hs50, D50, Tp50, doc, hr, HTL=0):
16-
self.CM = CM
15+
def __init__(self, HTL, Hs50, Tp50, D50, CM, hr, doc):
16+
17+
# --- Input data validation ---
18+
gamma_break = CM / Hs50
19+
if not (0.5 <= gamma_break <= 1.5):
20+
raise ValueError(
21+
f"CM should be between 0.7 and 1.5 times Hs50 "
22+
f"(CM/Hs50 given = {gamma_break:.2f})"
23+
)
24+
if not (-17.0 <= HTL <= 17.0):
25+
raise ValueError(f"HTL must be between -17 and 17 m (given {HTL})")
26+
if not (0.1 <= Hs50 <= 4):
27+
raise ValueError(f"Hs50 must be between 0.1 and and be 2x smaller than CM (given {Hs50})")
28+
if not (4.0 <= Tp50 <= 20.0):
29+
raise ValueError(f"Tp50 must be between 1 and 25 s (given {Tp50})")
30+
if not (0.06 <= D50 <= 4.0):
31+
raise ValueError(f"D50 must be between 0.06 and 4.0 mm (given {D50})")
32+
if not (HTL < doc <= HTL+20.0):
33+
raise ValueError(f"doc must be between HTL and CM/2 (given doc={doc}, HTL={HTL} and CM/2={CM/2})")
34+
if not (HTL < hr < CM/2):
35+
raise ValueError(f"hr must satisfy HTL-0.5 < hr < CM/2 (given hr={hr}, HTL={HTL}, CM/2={CM/2})")
36+
37+
self.HTL = HTL
1738
self.Hs50 = Hs50
18-
self.D50 = D50
1939
self.Tp50 = Tp50
20-
self.doc = doc
21-
self.HTL = HTL
40+
self.D50 = D50
41+
self.CM = CM
2242
self.hr = hr
43+
self.doc = doc
2344

2445
# observed data placeholders
2546
self.x_raw = None
@@ -45,7 +66,8 @@ def __init__(self, CM, Hs50, D50, Tp50, doc, hr, HTL=0):
4566
def params(self):
4667
ws = wMOORE(self.D50 / 1000)
4768
gamma = self.Hs50 / (ws * self.Tp50)
48-
self.Ar = 0.21 - 0.02 * gamma
69+
A_raw = 0.21 - 0.02 * gamma
70+
self.A = max(A_raw, 1e-3)
4971
self.B = 0.89 * np.exp(-1.24 * gamma)
5072
self.C = 0.06 + 0.04 * gamma
5173
self.D = 0.22 * np.exp(-0.83 * gamma)
@@ -55,9 +77,9 @@ def def_hvec(self):
5577

5678
def def_xo(self):
5779
self.xo = (
58-
(self.hr + self.CM) / self.Ar
80+
(self.hr + self.CM) / self.A
5981
)**1.5 - (self.hr / self.C)**1.5 + (
60-
self.B / (self.Ar**1.5)
82+
self.B / (self.A**1.5)
6183
) * (self.hr + self.CM)**3 - (
6284
self.D / (self.C**1.5)
6385
) * self.hr**3
@@ -71,10 +93,10 @@ def run(self):
7193
self.def_xo()
7294
# raw computation
7395
x_raw, x1_raw, x2_raw, h2 = Bernabeu(
74-
self.Ar, self.B, self.C, self.D,
96+
self.A, self.B, self.C, self.D,
7597
self.CM, self.h, self.xo
7698
)
77-
y2_raw = self.h + self.HTL
99+
#y2_raw = self.h + self.HTL
78100

79101
self.x_full = x_raw + self.x_drift
80102
self.y_full = self.h + self.HTL
@@ -191,20 +213,23 @@ def resid(log_params):
191213
return np.concatenate([res1, res2])
192214

193215
x0 = np.array([
194-
np.log(self.Ar), np.log(self.B),
216+
np.log(self.A), np.log(self.B),
195217
np.log(self.C), np.log(self.D),
196218
self.hr
197219
])
198-
lb = [np.log(1e-3)]*4 + [0.0]
199-
ub = [np.log(10.0)]*4 + [self.h.max()]
220+
lb = [np.log(1e-4)]*4 + [0.0]
221+
ub = [np.log(100.0)]*4 + [self.h.max()]
222+
223+
x0 = np.maximum(x0, lb)
224+
x0 = np.minimum(x0, ub)
200225

201226
res = least_squares(
202227
resid, x0, bounds=(lb, ub),
203228
loss='huber', f_scale=0.1,
204229
xtol=1e-8, ftol=1e-8, max_nfev=2000
205230
)
206231

207-
self.Ar, self.B, self.C, self.D = np.exp(res.x[:4])
232+
self.A, self.B, self.C, self.D = np.exp(res.x[:4])
208233
self.hr = res.x[4]
209234
self.def_hvec()
210235
return self.run()

0 commit comments

Comments
 (0)