u = prob.addControllerInputs(1, [0.0], [1.0]) rhs = Expression(SX.zeros(3)) rhs[0] = x[0] - x[0] * x[1] + w[0] rhs[1] = -x[1] + x[0] * x[1] + u * x[2] * x[1] + w[1] rhs[2] = -x[2] + 0.50 + w[2] m = SX.zeros(2, 2) m[0, 0] = 2 + 1.414 m[1, 0] = 1 + 1.414 m[0, 1] = 1 + 1.414 m[1, 1] = 1 + 1.414 prob.addControllerODEs(x, rhs) f = sum((1 - x[0]('coll')) * (1 - x[0]('coll')) + (1 - x[1]('coll')) * (1 - x[1]('coll')) + u('coll') * u('coll')) + (mul((mul( (x[0:2](-1).T, m)), x[0:2](-1)))) prob.addControlObjective(f) #prob.addConstraints(x(0),[1,1,0.50]) x1 = prob.addPlantStates(3) u1 = prob.addPlantInputs(1) rhs1 = SX.zeros(3) rhs1[0] = x1[0] - x1[0] * x1[1] rhs1[1] = -x1[1] + x1[0] * x1[1] + u1 * x1[2] * x1[1] rhs1[2] = -x1[2] + 0.50 prob.addPlantODEs(x1, rhs1) x0 = NP.array([1, 1, 0.50]) prob.setInitCondition(x0) solver = MPCsolve(prob, printlevel=0) solver.solve() solver.plotStates() # solver.plotControls() #
#### ESTIMATOR sigma_y = NP.array([5,5]) V = (sigma_y**2) * NP.eye(2) sigma_x0 = 0.05 P00 = (sigma_x0**2) * NP.eye(4) est = Estimator('ukf',[2,3],V,P00) x2 = est.addStates(4,[0.0,0.0,50.0,50.0],[10.0,10.0,250.0,250.0],[2.14,1.09,114.2,112.9,0.0]) u2 = est.addControls(2,[3.0,-9000.0],[35.0,0.0]) cA = x2[0] cB = x2[1] theta = x2[2] thetaK = x2[3] k1 = k10*exp(E1/(273.15 +theta)) k2 = k20*exp(E2/(273.15 +theta)) k3 = k30*exp(E3/(273.15 +theta)) rhs2 = SX.zeros(4) rhs2[0] = (1/tph)*(u2[0]*(cA0-cA) - k1*cA - k3*cA*cA) rhs2[1] = (1/tph)* (- u2[0]*cB + k1*cA - k2*cB) rhs2[2] = (1/tph)*(u2[0]*(theta0-theta) - (1/(rho*Cp)) *(k1*cA*H1 + k2*cB*H2 + k3*cA*cA*H3)+(kw*AR/(rho*Cp*VR))*(thetaK -theta)) rhs2[3] = (1/tph)*((1/(mK*CPK))*(u2[1] + kw*AR*(theta-thetaK))) yE = SX.zeros(2) yE[0] = x2[2]; yE[1] = x2[3] est.addMeasurements(yE) est.addEstimatorODEs(x2,rhs2) solver = MPCsolve(MPC,Estimator = est) solver.solve() solver.plotStates()# solver.plotControls()#
rhs1[6] = stream_5_flow * stream_5_conc_G + stream_8_flow * stream_8_conc_G - stream_6_flow * (mixing[6]/mixing_zone_N) rhs1[7] = stream_5_flow * stream_5_conc_H + stream_8_flow * stream_8_conc_H - stream_6_flow * (mixing[7]/mixing_zone_N) rhs1[8] = (u1[3] * stream_1_cp * (stream_1_T - mixing[8]) + u1[1] * stream_2_cp * (stream_2_T - mixing[8]) + u1[2] * stream_3_cp * (stream_3_T - mixing[8]) + stream_5_flow * stream_5_cp * (Tstr - mixing[8]) + stream_8_flow * stream_8_cp * (stream_8_T - mixing[8]))/mixing_zone_Ncp; rhs1[9] = stream_6_flow * (stream_6_conc[0]) - stream_7_flow * (stream_7_conc[0]) + reactor_conv_rate[0]; rhs1[10] = stream_6_flow * (stream_6_conc[1]) - stream_7_flow * (stream_7_conc[1]) + reactor_conv_rate[1]; rhs1[11] = stream_6_flow * (stream_6_conc[2]) - stream_7_flow * (stream_7_conc[2]) + reactor_conv_rate[2]; rhs1[12] = stream_6_flow * (stream_6_conc[3]) - stream_7_flow * (stream_7_conc[3]) + reactor_conv_rate[3]; rhs1[13] = stream_6_flow * (stream_6_conc[4]) - stream_7_flow * (stream_7_conc[4]) + reactor_conv_rate[4]; rhs1[14] = stream_6_flow * (stream_6_conc[5]) - stream_7_flow * (stream_7_conc[5]) + reactor_conv_rate[5]; rhs1[15] = stream_6_flow * (stream_6_conc[6]) - stream_7_flow * (stream_7_conc[6]) + reactor_conv_rate[6]; rhs1[16] = stream_6_flow * (stream_6_conc[7]) - stream_7_flow * (stream_7_conc[7]) + reactor_conv_rate[7]; rhs1[17] = ((1/1000.0) * stream_6_flow * stream_6_cp * (mixing[8] - reactor[8]) - Qr - reactor_exoth_heat)/reactor_Ncp; MPC.addPlantODEs(x1,rhs1) x0 = NP.array([4.883796012e+01,1.3581698782601494e01,4.003019454e01,9.7240317507928289e00,2.7443144213409383e01,2.5287892794899194e00,5.4376801699256117e00,2.5340908634231365e00,3.626444078091077e02,5.031608878009897e00,2.1296567956657944e00,3.7868352943264569e00,1.5154871302534503e-01,9.6154000218439766e00,1.3071231152493332e00,6.328731837338335e01,6.9378697766081004e01,393.55]) MPC.setInitCondition(x0) solver = MPCsolve(MPC,printlevel=0) solver.solve() solver.plotStates()# solver.plotControls()# ### x = NP.loadtxt('xTE') mixing = x[0:9,:] #mixing(8) is temperature reactor = x[9:18,:] #reactor(8) is temperature mixing_zone_N = (mixing[0,:] + mixing[1,:] + mixing[2,:] + mixing[3,:] + mixing[4,:] + mixing[5,:] + mixing[6,:] + mixing[7,:]) pm_MPa = mixing_zone_N * (RkJ * mixing[8] / Vm) / 1000.0 t = NP.linspace(0,108000,301) plt.plot(t,pm_MPa) plt.show()
sigma_y = NP.array([2,2]) V = (sigma_y**2) * NP.eye(2) sigma_x0 = 0.00005 P00 = (1.0/sigma_x0) * NP.eye(4) est = Estimator('mhe',[0,3],V,P00) est.defineHorizon(20,10.) x2 = est.addStates(4,[0.0,0.0,50.0,50.0],[10.0,10.0,250.0,250.0],[2.14,1.09,114.2,112.9,0.0]) u2 = est.addControls(2,[3.0,-9000.0],[35.0,0.0]) cA = x2[0] cB = x2[1] theta = x2[2] thetaK = x2[3] k1 = k10*exp(E1/(273.15 +theta)) k2 = k20*exp(E2/(273.15 +theta)) k3 = k30*exp(E3/(273.15 +theta)) rhs2 = Expression(SX.zeros(4)) rhs2[0] = (1/tph)*(u2[0]*(cA0-cA) - k1*cA - k3*cA*cA) rhs2[1] = (1/tph)* (- u2[0]*cB + k1*cA - k2*cB) rhs2[2] = (1/tph)*(u2[0]*(theta0-theta) - (1/(rho*Cp)) *(k1*cA*H1 + k2*cB*H2 + k3*cA*cA*H3)+(kw*AR/(rho*Cp*VR))*(thetaK -theta)) rhs2[3] = (1/tph)*((1/(mK*CPK))*(u2[1] + kw*AR*(theta-thetaK))) yE = Expression(SX.zeros(2)) yE[0] = x2[2]; yE[1] = x2[3] est.addMeasurements(yE) est.addEstimatorODEs(x2,rhs2) solver = MPCsolve(MPC,Estimator = est) solver.solve() solver.plotStates()# solver.plotControls()# solver.plotMeasurements()