Spaces:
Build error
Build error
File size: 2,167 Bytes
9fdeb77 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | import streamlit as st
import numpy as np
import pandas as pd
class ProcessModel:
def __init__(self, Kp, tau, theta, dt):
self.Kp = Kp
self.tau = tau
self.theta = theta
self.pv = 0.0
self.buffer = [0.0] * (int(theta / dt) if dt > 0 else 0)
def update(self, cv, dt):
if self.buffer:
delayed_cv = self.buffer.pop(0)
self.buffer.append(cv)
else:
delayed_cv = cv
# Simple process simulation
d_pv = (self.Kp * delayed_cv - self.pv) / self.tau
self.pv += d_pv * dt
return self.pv
class PIDController:
def __init__(self, Kp, Ki, Kd):
self.Kp = Kp
self.Ki = Ki
self.Kd = Kd
self.integral = 0.0
self.previous_error = 0.0
def calculate(self, sp, pv, dt):
error = sp - pv
self.integral += error * dt
derivative = (error - self.previous_error) / dt
self.previous_error = error
return self.Kp * error + self.Ki * self.integral + self.Kd * derivative
def run_simulation(process, controller, setpoints, duration, dt):
time_points = np.arange(0, duration, dt)
pv_history, cv_history, sp_history = [], [], []
iae = coi = oscillation_index = 0.0
error_last = cv_last = 0.0
current_sp = setpoints[0][1]
for t in time_points:
for sp_time, sp_value in reversed(setpoints):
if t >= sp_time:
current_sp = sp_value
break
sp_history.append(current_sp)
cv = controller.calculate(current_sp, process.pv, dt)
pv = process.update(cv, dt)
pv_history.append(pv)
cv_history.append(cv)
error = current_sp - pv
iae += abs(error) * dt
if t > 0:
coi += abs(cv - cv_last) * dt
if error * error_last < 0:
oscillation_index += 1
cv_last = cv
error_last = error
metrics = {"IAE": iae, "COI": coi, "Oscillation Index": oscillation_index}
df = pd.DataFrame(
{"Time": time_points, "SP": sp_history, "PV": pv_history, "MV": cv_history}
)
return df, metrics
|