def __init__(self, nsim, delta=1, nx=3, nu=2, nt=10, f0=0.1, t0=350, c0=1, r=0.219, k0=7.2e10, er_ratio=8750, u=54.94, rho=1000, cp=0.239, dh=-5e4, xs=np.array([0.878, 324.5, 0.659]), us=np.array([300, 0.1]), x0=np.array([1, 310, 0.659]), control=False): self.Delta = delta self.Nsim = nsim self.Nx = nx self.Nu = nu self.Nt = nt self.F0 = f0 self.T0 = t0 self.c0 = c0 self.r = r self.k0 = k0 self.er_ratio = er_ratio self.U = u self.rho = rho self.Cp = cp self.dH = dh self.xs = xs self.us = us self.xsp = np.zeros([self.Nsim + 1, self.Nx]) self.xsp[0, :] = xs self.x0 = x0 self.x = np.zeros([self.Nsim + 1, self.Nx]) self.x[0, :] = self.x0 self.u = np.zeros([self.Nsim + 1, self.Nu]) self.u[0, :] = [300, 0.1] self.u[:, 1] = 0.1 self.control = control self.observation_space = np.zeros(nx) self.action_space = Box(low=-2.5, high=2.5, shape=(1, )) self.cstr_sim = mpc.DiscreteSimulator(self.ode, self.Delta, [self.Nx, self.Nu], ["x", "u"]) self.cstr_ode = mpc.getCasadiFunc(self.ode, [self.Nx, self.Nu], ["x", "u"], funcname="odef")
def disturbance(self): # 10% increase in flow rate self.F0 = self.F0 * 1.1 # Rebuild the simulator self.cstr_sim = mpc.DiscreteSimulator(self.ode, self.Delta, [self.Nx, self.Nu], ["x", "u"])
def __init__(self, nsim, x0=np.array([3]), u0=np.array([3, 6]), xs=np.array([4.5]), us=np.array([5.5, 8]), step_size=0.2, control=False, q_cost=1, r_cost=0.5, random_seed=1): self.Nsim = nsim self.x0 = x0 self.u0 = u0 self.xs = xs self.us = us self.step_size = step_size self.t = np.linspace(0, nsim * self.step_size, nsim + 1) self.control = control # Model Parameters if self.control: self.Nx = 2 # Because of offset free control else: self.Nx = 1 self.Nu = 2 self.action_space = Box(low=np.array([-12, -12]), high=np.array([12, 12])) self.Q = q_cost self.R = r_cost * np.eye(self.Nu) self.A = np.array([-3]) self.B = np.array([1, 1]) self.C = np.array([1]) self.D = 0 # State and Input Trajectories self.x = np.zeros([nsim + 1, self.Nx]) self.u = np.zeros([nsim + 1, self.Nu]) self.x[0, :] = x0 self.u[0, :] = u0 # Build the CasaDI functions self.system_sim = mpc.DiscreteSimulator(self.ode, self.step_size, [self.Nx, self.Nu], ["x", "u"]) self.system_ode = mpc.getCasadiFunc(self.ode, [self.Nx, self.Nu], ["x", "u"], funcname="odef") # Set-point trajectories self.xsp = np.zeros([self.Nsim + 1, self.Nx]) self.xsp[0, :] = self.xs self.usp = np.zeros([self.Nsim + 1, self.Nu]) self.usp[0, :] = self.us # Seed the system for reproducability random.seed(random_seed) np.random.seed(random_seed)
def _get_cstrs_rectified_xs(*, parameters): """ Get the steady state of the plant.""" # (xs, us, ps) xs = np.zeros((parameters['Nx'], 1)) us = np.zeros((parameters['Nu'], 1)) ps = np.zeros((parameters['Np'], 1)) cstrs_ode = lambda x, u, p: _cstrs_ode(x, u, p, parameters) # Construct the casadi class. model = mpc.DiscreteSimulator( cstrs_ode, parameters['sample_time'], [parameters['Nx'], parameters['Nu'], parameters['Np']], ["x", "u", "p"]) # steady state of the plant. for _ in range(7200): xs = model.sim(xs, us, ps) # Return the disturbances. return parameters['xs'] + xs
def __init__(self, *, fxup, hx, Rv, Nx, Nu, Np, Ny, sample_time, x0): # Set attributes. self.fxup = mpc.DiscreteSimulator(fxup, sample_time, [Nx, Nu, Np], ["x", "u", "p"]) self.hx = mpc.getCasadiFunc(hx, [Nx], ["x"], funcname="hx") (self.Nx, self.Nu, self.Ny, self.Np) = (Nx, Nu, Ny, Np) self.measurement_noise_std = np.sqrt(np.diag(Rv)[:, np.newaxis]) self.sample_time = sample_time # Create lists to save data. self.x = [x0] self.u = [] self.p = [] self.y = [ np.asarray(self.hx(x0)) + self.measurement_noise_std * np.random.randn(self.Ny, 1) ] self.t = [0.]
def __init__(self, nsim, delta=1, nx=3, nu=2, nt=10, f0=0.1, t0=350, c0=1, r=0.219, k0=7.2e10, er_ratio=8750, u=54.94, rho=1000, cp=0.239, dh=-5e4, xs=np.array([0.878, 324.5, 0.659]), us=np.array([300, 0.1]), x0=np.array([1, 310, 0.659]), u0=np.array([300, 10]), control=False, q_cost=1, r_cost=0.5): self.Delta = delta self.Nsim = nsim self.Nx = nx self.Nu = nu self.Nt = nt self.F0 = f0 self.T0 = t0 self.c0 = c0 self.r = r self.k0 = k0 self.er_ratio = er_ratio self.U = u self.rho = rho self.Cp = cp self.dH = dh self.xs = xs self.us = us self.xsp = np.zeros([self.Nsim + 1, self.Nx]) self.xsp[0, :] = xs self.x0 = x0 self.u0 = u0 self.x = np.zeros([self.Nsim + 1, self.Nx]) self.x[0, :] = self.x0 self.u = np.zeros([self.Nsim + 1, self.Nu]) self.u[0, :] = u0 self.u[:, 1] = 0.1 self.control = control self.cstr_sim = mpc.DiscreteSimulator(self.ode, self.Delta, [self.Nx, self.Nu], ["x", "u"]) self.cstr_ode = mpc.getCasadiFunc(self.ode, [self.Nx, self.Nu], ["x", "u"], funcname="odef") # Cost function Q and R tuning parameters self.q_cost = q_cost self.r_cost = r_cost
def __init__(self, nsim, x0=np.array([65.13, 42.55, 0.0, 0.0]), u0=np.array([3.9, 3.9, 0.0, 0.0]), xs=np.array([261.78, 171.18, 111.43, 76.62]), us=np.array([15.7, 15.7, 5.337, 5.337]), step_size=1, control=False, q_cost=1, r_cost=0.5, random_seed=1): # Initial conditions and other required parameters self.Nsim = nsim self.x0 = x0 self.u0 = u0 self.xs = xs self.us = us self.step_size = step_size self.t = np.linspace(0, nsim * self.step_size, nsim + 1) self.control = control # Model Parameters if self.control: self.Nx = 8 else: self.Nx = 4 # Double Nu to account for time delay self.Nu = int(self.u0.shape[0]) self.action_space = Box(low=np.array([-5]), high=np.array([5])) self.observation_space = np.zeros(self.Nx) self.Q = q_cost * np.eye(self.Nx) self.R = r_cost * np.eye(self.Nu) # State space model self.A = np.array([[-0.0629, 0, 0, 0], [0, -0.0963, 0, 0], [0, 0, -0.05, 0], [0, 0, 0, -0.0729]]) self.B = np.array([[1, 0], [1, 0], [0, 1], [0, 1]]) self.C = np.array([[0.7665, 0, -0.9, 0], [0, 0.6055, 0, -1.3472]]) self.D = 0 # State and input trajectories self.x = np.zeros([nsim + 1, self.Nx]) self.u = np.zeros([nsim + 1, int(self.Nu)]) self.x[0:self.Nx, :] = x0 self.u[0:self.Nx, :] = u0 # Set initial conditions for the first # Output trajectory self.y = np.zeros([nsim + 1, 2]) # Build the CasaDI functions self.system_sim = mpc.DiscreteSimulator(self.ode, self.step_size, [self.Nx, self.Nu], ["x", "u"]) self.system_ode = mpc.getCasadiFunc(self.ode, [self.Nx, self.Nu], ["x", "u"], funcname="odef") # Set-point trajectories self.xsp = np.zeros([self.Nsim + 1, self.Nx]) self.xsp[0, :] = self.xs self.usp = np.zeros([self.Nsim + 1, int(self.Nu)]) self.usp[0, :] = self.us # Seed the system for reproducability random.seed(random_seed) np.random.seed(random_seed)
# Time interval # KIni = hydraulic_conductivity(hIni) # CIni = capillary_capacity(hIni) # dt = 0.5*dz*dz/(KIni/CIni)*3 dt = 60 # second timeSpan = 1444 # min # 19 hour # timeSpan = 600 interval = int(timeSpan*60/dt) solList_theta = [] solList_h = [] solList_thetaAvg = [] model = mpc.DiscreteSimulator(ode, dt, [numberOfNodes, 1], ['x', 'u']) for t in range(0, interval): if t in range(0, 22): irr_amount = (0.001/(3.1415*0.22*0.22)/(22*60)) # irr_amount = 7.3999e-08 elif t in range(59, 87): irr_amount = (0.000645/(pi*0.22*0.22)/(27*60)) # irr_amount = 7.3999e-08 elif t in range(161, 189): irr_amount = (0.000645/(pi*0.22*0.22)/(27*60)) # irr_amount = 7.3999e-08 elif t in range(248, 276): irr_amount = (0.000645/(pi*0.22*0.22)/(27*60)) # irr_amount = 7.3999e-08 elif t in range(335, 361):
def __init__(self, nsim, model_type='SISO', x0=np.array([0.5]), u0=np.array([1]), xs=np.array([5]), us=np.array([10]), step_size=0.1, control=False, q_cost=1, r_cost=0.5, random_seed=1): self.Nsim = nsim self.x0 = x0 self.u0 = u0 self.xs = xs self.us = us self.model_type = model_type self.step_size = step_size self.t = np.linspace(0, nsim * self.step_size, nsim + 1) self.control = control # Initialization Characteristics if model_type == "SISO": if self.control is True: self.Nx = 2 else: self.Nx = 1 self.Nu = 1 self.action_space = Box(low=-5, high=5, shape=(1, )) self.Q = q_cost self.R = r_cost # Model parameters self.A = np.array([-4]) self.B = np.array([2]) self.C = np.array([1]) self.D = 0 elif model_type == "MIMO": if self.control is True: self.Nx = 4 else: self.Nx = 2 self.Nu = 2 self.action_space = Box(low=np.array([-5, -5]), high=np.array([5, 5])) self.Q = q_cost * np.eye(self.Nx) self.R = r_cost * np.eye(self.Nu) # Model parameters self.A = np.array([[-3, -2], [0, -3]]) self.B = np.array([[4, 0], [0, 2]]) self.C = np.array([1, 1]) self.D = 0 else: raise ValueError("Model type not specified properly") self.observation_space = np.zeros(self.Nx) # State and input trajectories self.x = np.zeros([nsim + 1, self.Nx]) self.u = np.zeros([nsim + 1, self.Nu]) self.x[0, :] = x0 self.u[0, :] = u0 self.xsp = np.zeros([self.Nsim + 1, self.Nx]) self.xsp[0, :] = self.xs self.usp = np.zeros([self.Nsim + 1, self.Nu]) self.usp[0, :] = self.us # Build the CasaDI functions self.system_sim = mpc.DiscreteSimulator(self.ode, self.step_size, [self.Nx, self.Nu], ["x", "u"]) self.system_ode = mpc.getCasadiFunc(self.ode, [self.Nx, self.Nu], ["x", "u"], funcname="odef") # Seed the system for reproducability random.seed(random_seed) np.random.seed(random_seed)
rate = k0*c*np.exp(-E/T) dxdt = np.array([ F0*(c0 - c)/(np.pi*r**2*h) - rate, F0*(T0 - T)/(np.pi*r**2*h) - dH/(rho*Cp)*rate + 2*U/(r*rho*Cp)*(Tc - T), (F0 - F)/(np.pi*r**2) ]) return dxdt # Turn into casadi function and simulator. ode_casadi = mpc.getCasadiFunc(ode,[Nx,Nu,Nd],["x","u","d"],funcname="ode") ode_rk4_casadi = mpc.getCasadiFunc(ode,[Nx,Nu,Nd],["x","u","d"], funcname="ode_rk4",rk4=False,Delta=Delta) cstr = mpc.DiscreteSimulator(ode, Delta, [Nx,Nu,Nd], ["x","u","d"]) # Steady-state values. cs = .878 Ts = 324.5 hs = .659 Fs = .1 Tcs = 300 F0s = .1 # Update the steady-state values a few times to make sure they don't move. for i in range(10): [cs,Ts,hs] = cstr.sim([cs,Ts,hs],[Tcs,Fs],[F0s]).tolist() xs = np.array([cs,Ts,hs]) us = np.array([Tcs,Fs]) ds = np.array([F0s])
sequence_length = 0 alarms_in_plant = [] masked_alarm_log = [] Alarms = np.zeros((1, Nsim + 1)) # Alarm Matrix alarm_pri_matrix = np.array([["Alarms"], ["Optimal RL Values"]]) value_sequence_dict = {} length_keymaker = 0 """ Simulation Characteristics """ x = np.zeros((Nx, Nsim + 1)) # States, Simulation Time u = np.zeros((Nu, Nsim)) # Inputs, Simulation Time wwtp_sim = mpc.DiscreteSimulator(Model.ode_bsm1model_constant_distur, Delta, [Nx, Nu], ["x", "u"]) # u[3:16, :] = Z0_14 # To test, Comment # u[2, :] = Q0_14 # To test, Comment x0 = open_ss_bsm1 # Load initial states """ Initiate parameters as zero """ Q1_14 = np.zeros(Nsim) Qe_14 = np.zeros(Nsim) Qf_14 = np.zeros(Nsim) Qw_14 = np.zeros(Nsim) Qr_14 = np.zeros(Nsim) Qa_14 = np.zeros(Nsim)
# u[kk,0] = data_input[kk,0] # u[kk,1] = data_input[kk,1] u[kk, 0] = 300.0 u[kk, 1] = 0.1 #=================================================================# # Simulate open-loop system #=================================================================# # test if the model works well #xs = np.array([0.878,324.5,0.659]) #us = np.array([300,0.1]) #derivative = cstr_xy_ode(xs,us) #measure = measurement_xy_model(xs) # Make a simulator. model_cstr_casadi = mpc.DiscreteSimulator(cstr_xy_ode_scale, Delta, [Nx, Nu, Nw], ["x", "u", "w"]) # Convert continuous-time f to explicit discrete-time F with RK4. F = mpc.getCasadiFunc(cstr_xy_ode_scale, [Nx, Nu, Nw], ["x", "u", "w"], "F", rk4=True, Delta=Delta, M=1) H = mpc.getCasadiFunc(measurement_xy_model, [Nx], ["x"], "H") # Define stage costs. def lfunc(w, v): return mpc.mtimes(w.T, linalg.inv(Q), w) + mpc.mtimes( v.T, linalg.inv(R), v)