From 39d50fdba1dbb131b46c17810bc3fed3b74d573e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Schm=C3=B6lder?= Date: Thu, 19 Dec 2024 09:16:43 +0100 Subject: [PATCH] [WIP] Normalize flow_rate profiles before fitting --- CADETProcess/processModel/process.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/CADETProcess/processModel/process.py b/CADETProcess/processModel/process.py index 04ddbf91..febd956d 100644 --- a/CADETProcess/processModel/process.py +++ b/CADETProcess/processModel/process.py @@ -652,17 +652,35 @@ def add_flow_rate_profile(self, unit, time, flow_rate, s=1e-6): if max(time) > self.cycle_time: raise ValueError('Inlet profile exceeds cycle time') - tck = interpolate.splrep(time, flow_rate, s=s) + # Compute min and max for scaling + min_val = np.min(flow_rate) + max_val = np.max(flow_rate) + range_val = max_val - min_val + + if range_val == 0: + raise ValueError('Flow rate has no variation; cannot scale.') + + # Normalize flow_rate to [0, 1] + normalized_flow_rate = (flow_rate - min_val) / range_val + + # Fit the spline with normalized flow_rate + tck = interpolate.splrep(time, normalized_flow_rate, s=s) ppoly = interpolate.PPoly.from_spline(tck) + # Add events with unscaled coefficients for i, (t, sec) in enumerate(zip(ppoly.x, ppoly.c.T)): if i < 3: continue elif i > len(ppoly.x) - 5: continue - evt = self.add_event( + + # Unscale all coefficients + unscaled_sec = sec * range_val + # Adjust the constant term (last coefficient in the array) for shifting + unscaled_sec[-1] += min_val + self.add_event( f'{unit}_flow_rate_{i-3}', f'flow_sheet.{unit}.flow_rate', - np.flip(sec), t + np.flip(unscaled_sec), t ) def check_config(self):