Skip to content

Commit

Permalink
[WIP] Normalize flow_rate profiles before fitting
Browse files Browse the repository at this point in the history
  • Loading branch information
schmoelder committed Dec 19, 2024
1 parent 1cf3a54 commit 39d50fd
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions CADETProcess/processModel/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 39d50fd

Please sign in to comment.