def solve(var): # initialize model reaction_model = GEKKO() # set model time as time gathered data reaction_model.time = time # Constants R = reaction_model.Const(8.3145) # Gas constant J / mol K # Parameters T = reaction_model.Param(temperature) # Fixed Variables to change # bounds E_a_1 = reaction_model.FV(E_a_1_initial_guess) E_a_2 = reaction_model.FV(E_a_2_initial_guess) A_1 = reaction_model.FV(A_1_initial_guess) A_2 = reaction_model.FV(A_2_initial_guess) alpha = reaction_model.FV(alpha_initial_guess) beta = reaction_model.FV(beta_initial_guess) # NO bounds # state Variables Ph_3_minus = reaction_model.SV(Ph_3_minus_initial) # variable we will use to regress other Parameters Ph_2_minus = reaction_model.SV(Ph_2_minus_initial) # intermediates k1 = reaction_model.Intermediate(A_1 * reaction_model.exp(-E_a_1 / (R * T))) k2 = reaction_model.Intermediate(A_2 * reaction_model.exp(-E_a_2 / (R * T))) r1 = reaction_model.Intermediate(k1 * Ph_2_minus**alpha) r2 = reaction_model.Intermediate(k2 * Ph_3_minus**beta) # equations reaction_model.Equations( [Ph_2_minus.dt() == r2 - r1, Ph_3_minus.dt() == r1 - r2]) # parameter options # controlled variable options # model options and other to solve reaction_model.options.IMODE = 4 # set up dynamic simulation reaction_model.options.NODES = 2 # number of nodes for collocation equations reaction_model.options.SOLVER = 1 # use APOPT active set non-linear solverreaction_model.options.EV_TYPE = 2 # use l-1 norm rather than 2 norm reaction_model.solve(disp=True) return Ph_2_minus.value
from gekko import GEKKO import numpy as np import matplotlib.pyplot as plt # Initialize Model m = GEKKO() # Define constants #Reflux Ratio rr=m.Param(value=0.7) # Feed flowrate (mol/min) Feed=m.Const(value=2) # Mole fraction of feed x_Feed=m.Const(value=.5) #Relative volatility = (yA/xA)/(yB/xB) = KA/KB = alpha(A,B) vol=m.Const(value=1.6) # Total molar holdup on each tray atray=m.Const(value=.25) # Total molar holdup in condenser acond=m.Const(value=.5) # Total molar holdup in reboiler areb=m.Const(value=.1) # mole fraction of component A
class CGE(): ''' Computable general equilibrium (CGE) models are a class of economic models that use actual economic data to estimate how an economy might react to changes in policy, technology or other external factors. CGE models are also referred to as AGE (applied general equilibrium) models. In this model assumed two factors influancing economy: Capital (K) and Labour (L). Model has been written completly in Python. ''' def __init__(self, capital, labour): self.Shock = {'K': capital, 'L': labour} self.use = pd.read_csv('Data\\production_structure.csv', index_col=0) self.xdem = pd.read_csv('Data\\consumption_structure.csv', index_col=0) self.enfac = pd.read_csv('Data\\endowment_of_the_household.csv', index_col=0) self.taxrev = pd.read_csv('Data\\tax_revenue.csv', index_col=0) self.trans = pd.read_csv('Data\\transfers.csv', index_col=0) self.factors = self.use.index self.sectors = self.use.columns self.households = self.xdem.loc[:, self.xdem.columns != 'GOVR'].columns self.beta = pd.DataFrame(index=self.factors, columns=self.sectors) self.alpha = pd.DataFrame(index=self.xdem.index, columns=self.xdem.columns) self.omega = pd.DataFrame(index=self.enfac.index, columns=self.enfac.columns) self.B = {} self.A = {} self.itax = {} self.tr_in = {} self.tr_out = {} self.p = {} self.S = {} self.taxK = {} self.taxL = {} self.PW = {} self.W = {} self.INC = {} self.m = GEKKO(remote=False) def parameters(self): for sec in self.sectors: # Parameters of the Cobb Douglas production function for fac in self.factors: self.beta[sec][fac] = self.m.Const(self.use[sec][fac] / sum(self.use[sec])) # For Production self.B[sec] = self.m.Const( sum(self.use[sec]) / np.prod([ self.use[sec][fac] **(self.use[sec][fac] / sum(self.use[sec])) for fac in self.factors ])) self.p[sec] = self.m.Var(lb=0, value=1) self.S[sec] = self.m.Var(value=sum(self.use[sec])) self.taxK[sec] = self.m.Var(lb=0, value=0) self.taxL[sec] = self.m.Var(lb=0, value=0) for hou in self.xdem.columns: # Parameters of the Cobb Dougas utility function for sec in self.sectors: self.alpha[hou][sec] = self.m.Const(self.xdem[hou][sec] / sum(self.xdem[hou])) self.A[hou] = self.m.Const( sum(self.xdem[hou]) / np.prod([ self.xdem[hou][sec] **(self.xdem[hou][sec] / sum(self.xdem[hou])) for sec in self.sectors ])) for hou in self.enfac.index: for fac in self.enfac.columns: # Endowments of factors self.omega[fac][hou] = self.m.Const(self.enfac[fac][hou] * (1 - self.Shock[fac])) # Income tax rate (Tax revenues / Household gross income) self.itax[hou] = self.m.Const( self.taxrev[hou]['GOVR'] / np.sum([self.omega[fac][hou] for fac in self.enfac.columns])) # Transfers self.tr_in[hou] = self.m.Const(self.trans[hou].sum()) self.tr_out[hou] = self.m.Const(self.trans.loc[hou].sum()) for hou in self.xdem.columns: self.PW[hou] = self.m.Var(lb=0, value=1) self.W[hou] = self.m.Var(value=sum(self.xdem[hou])) self.INC[hou] = self.m.Var(lb=0, value=sum(self.xdem[hou])) self.pK = self.m.Var(lb=0, value=1) self.pL = self.m.Var(lb=0, value=1) def equilibrium(self): self.parameters() self.equations = { 'PRF_WG': (np.prod([(self.p[sec] / self.alpha['GOVR'][sec]) **(self.alpha['GOVR'][sec]) for sec in self.sectors]) / self.A['GOVR'] == self.PW['GOVR']), 'MKT_WG': (self.W['GOVR'] * self.PW['GOVR'] == self.INC['GOVR']), 'I_INCG': (np.sum([(self.pK * self.omega['K'][hou] + self.pL * self.omega['L'][hou] + self.tr_in[hou] - self.tr_out[hou]) * self.itax[hou] for hou in self.households]) + np.sum([(self.taxK[sec] * self.pK * self.beta[sec]['K'] * self.p[sec] * self.S[sec]) / (self.pK * (1 + self.taxK[sec])) for sec in self.sectors]) + np.sum([(self.taxL[sec] * self.pL * self.beta[sec]['L'] * self.p[sec] * self.S[sec]) / (self.pL * (1 + self.taxL[sec])) for sec in self.sectors]) == self.INC['GOVR']) } for sec in self.sectors: # Zero profit conditions (P = MC) self.equations[f'PRF_{sec}S'] = ( ((self.pK * (1 + self.taxK[sec]) / self.beta[sec]['K'])** (self.beta[sec]['K']) * (self.pL * (1 + self.taxL[sec]) / self.beta[sec]['L'])** (self.beta[sec]['L'])) / self.B[sec] == self.p[sec]) # Goods markets clearing self.equations[f'MKT_{sec}D'] = (np.sum([ self.alpha[hou][sec] * self.PW[hou] * self.W[hou] / self.p[sec] for hou in self.households ]) + self.alpha['GOVR'][sec] * self.W['GOVR'] * self.PW['GOVR'] / self.p[sec] == self.S[sec]) for hou in self.households: # Definition of the consumer price index self.equations['CPI_' + hou] = (np.prod( [(self.p[sec] / self.alpha[hou][sec])**(self.alpha[hou][sec]) for sec in self.sectors]) / self.A[hou] == self.PW[hou]) # Consumer spends everything on consumption self.equations['OUTLAY_' + hou] = self.PW[hou] * self.W[hou] == self.INC[hou] # Income determination self.equations['INC_' + hou] = ((self.pK * self.omega['K'][hou] + self.pL * self.omega['L'][hou] + self.tr_in[hou] - self.tr_out[hou]) * (1 - self.itax[hou]) == self.INC[hou]) # Factor market clearing for K and L self.equations['MKT_K'] = (np.sum([ self.beta[sec]['K'] * self.p[sec] * self.S[sec] / (self.pK * (1 + self.taxK[sec])) for sec in self.sectors ]) == np.sum([self.omega['K'][hou] for hou in self.households])) self.equations['MKT_L'] = (np.sum([ self.beta[sec]['L'] * self.p[sec] * self.S[sec] / (self.pL * (1 + self.taxL[sec])) for sec in self.sectors ]) == np.sum([self.omega['L'][hou] for hou in self.households])) self.m.Equations(list(self.equations.values())) self.m.options.SOLVER = 1 self.m.solve() def results(self): self.equilibrium() solution = ('Solutions:\n' + '------------------------------\n' + '%-12s%6s%12.3f\n' % ('Gov wealth', '|', self.W['GOVR'].value[0]) + '%-12s%6s%12.3f\n' % ('Gov PI', '|', self.PW['GOVR'].value[0]) + '%-12s%6s%12.3f\n' % ('Gov income', '|', self.INC['GOVR'].value[0]) + '------------------------------\n' + '%-12s%6s%12.3f\n' % ('K price', '|', self.pK.value[0]) + '%-12s%6s%12.3f\n' % ('L price', '|', self.pL.value[0]) + '------------------------------\n') for sec in self.sectors: solution += '%-12s%6s%12.3f\n' % (f'{sec} supply', '|', self.S[sec].value[0]) solution += '%-12s%6s%12.3f\n' % (f'{sec} price', '|', self.p[sec].value[0]) solution += '%-12s%6s%12.3f\n' % (f'K {sec} tax', '|', self.taxK[sec].value[0]) solution += '%-12s%6s%12.3f\n' % (f'L {sec} tax', '|', self.taxL[sec].value[0]) solution += '------------------------------\n' for hou in self.households: solution += '%-12s%6s%12.3f\n' % (hou + ' Wealth', '|', self.W[hou].value[0]) solution += '%-12s%6s%12.3f\n' % (hou + ' CPI', '|', self.PW[hou].value[0]) solution += '%-12s%6s%12.3f\n' % (hou + ' Income', '|', self.INC[hou].value[0]) return print(solution)
""" Partial differential equation example using GEKKO """ # Initialize model m = GEKKO() # Discretizations ( time and space ) m.time = np.linspace(0,1,100) npx = 100 xpos = np.linspace(0,2*np.pi,npx) dx = xpos[1]−xpos[0] # Define Variables c = m.Const(value = 10) u = [m.Var(value = np.cos(xpos[i])) for i in range(npx)] v = [m.Var(value = np.sin(2*xpos[i])) for i in range(npx)] m.Equations([u[i].dt()==v[i] for i in range(npx) ]) # Manual discretization in space ( central difference ) m.Equation(v[0].dt()==c**2 * (u[1] − 2.0*u[0] + u[npx−1])/dx**2 ) m.Equations([v[i+1].dt()== c**2*(u[i+2]−2.0*u[i+1]+u[i])/dx**2 for i in range(npx−2)]) m.Equation(v[npx−1].dt()== c**2 * (u[npx−2] − 2.0*u[npx−1] + u[0])/dx**2 ) # set options m.options.imode = 4 m.options.solver = 1 m.options.nodes = 3
from gekko import GEKKO import numpy as np import matplotlib.pyplot as plt m = GEKKO() m.time = np.linspace(0, 1200, 1201) Q = m.Param(value=100) #% heater T0 = m.Param(value=23 + 273.15) #K U = m.Param(value=10) #W/m2K mass = m.Param(value=4 / 1000) #kg cp = m.Param(value=0.5 * 1000) #J/kgK A = m.Param(value=12 / 100**2) #m2 alpha = m.Param(value=0.01) #W/% heater eps = m.Param(value=0.9) #emissivity sig = m.Const(5.67e-8) #stef-boltz const T = m.Var(value=T0) m.Equation(mass * cp * T.dt() == U * A * (T0 - T) + eps * sig * A * (T0**4 - T**4) + alpha * Q) m.options.IMODE = 4 m.solve() plt.plot(m.time, T.value) plt.savefig("simulation1.png") print(T.value[-1]) print((T.value[-1] - 273.15) * 9 / 5 + 32) plt.show()
def free_flight(vA, mission, rocket, stage, trajectory, general, optimization, Data): aux_vA = vA g0 = 9.810665 Re = 6371000 tb = trajectory.coast_time for i in range(0, rocket.num_stages): T = stage[i].thrust ISP = stage[i].Isp tb = tb + (stage[i].propellant_mass) / (T) * g0 * ISP tb = Data[0][-1] + (stage[-1].propellant_mass) / ( stage[-1].thrust) * g0 * stage[-1].Isp ##############Ccntrol law for no coast ARC######################## #### Only for last stage Obj = 1000 #Boundary Defined, for better convergence in Free flight t_lb = -(tb - Data[0][-1]) * 0.5 t_ub = 0 aux3 = 0 m = GEKKO(remote=False) m.time = np.linspace(0, 1, 100) final = np.zeros(len(m.time)) final[-1] = 1 final = m.Param(value=final) #Lagrange multipliers with l1=0 l2 = m.FV(-0.01, lb=trajectory.l2_lb, ub=trajectory.l2_ub) l2.STATUS = 1 l3 = m.FV(-1, lb=trajectory.l3_lb, ub=trajectory.l3_ub) l3.Status = 1 l4 = m.Var(value=0) rate = m.Const(value=T / ISP / g0) #value=(tb-Data[0][-1]+(t_ub+t_lb)/2) tf = m.FV(value=(tb - Data[0][-1] + t_lb), ub=tb - Data[0][-1] + t_ub, lb=tb - Data[0][-1] + t_lb) tf.STATUS = 1 aux = m.FV(0, lb=-5, ub=5) aux.STATUS = 1 vD = m.FV(value=Data[8][-1]) #Initial Conditions x = m.Var(value=Data[1][-1]) y = m.Var(value=Data[2][-1]) vx = m.Var(value=Data[3][-1] * cos(Data[4][-1])) vy = m.Var(value=Data[3][-1] * sin(Data[4][-1]), lb=0) vG = m.Var(value=Data[7][-1]) vA = m.Var(value=0) gamma = m.Var() alpha = m.Var() t = m.Var(value=0) m.Equations([l4.dt() / tf == -l2]) m.Equation(t.dt() / tf == 1) mrt = m.Intermediate(Data[5][-1] - rate * t) srt = m.Intermediate(m.sqrt(l3**2 + (l4 - aux)**2)) m.Equations([ x.dt() / tf == vx, y.dt() / tf == vy, vx.dt() / tf == (T / mrt * (-l3) / srt - (vx**2 + vy**2) / (Re + y) * m.sin(gamma)), vy.dt() / tf == (T / mrt * (-(l4 - aux) / srt) + (vx**2 + vy**2) / (Re + y) * m.cos(gamma) - g0 * (Re / (Re + y))**2), vG.dt() / tf == g0 * (Re / (Re + y))**2 * m.sin(m.atan(vy / vx)) ]) m.Equation(gamma == m.atan(vy / vx)) m.Equation(alpha == m.atan((l4 - aux) / l3)) m.Equation(vA.dt() / tf == T / mrt - T / mrt * m.cos((alpha - gamma))) #m.Obj(final*vA) # Soft constraints m.Obj(final * (y - mission.final_altitude)**2) m.Obj(final * 100 * (vx - mission.final_velocity * cos(mission.final_flight_angle))**2) m.Obj(final * 100 * (vy - mission.final_velocity * sin(mission.final_flight_angle))**2) #m.Obj(100*tf) m.Obj(final * (l2 * vy + l3 * (T / (Data[5][-1] - rate * t) * (-l3 / (m.sqrt(l3**2 + (l4 - aux)**2))) - (vx**2 + vy**2) / (Re + y) * m.sin(gamma)) + (l4 - aux) * (T / (Data[5][-1] - rate * t) * (-(l4 - aux) / (m.sqrt(l3**2 + (l4 - aux)**2))) + (vx**2 + vy**2) / (Re + y) * m.cos(gamma) - g0 * (Re / (Re + y))**2) + 1)**2) #Options m.options.IMODE = 6 m.options.SOLVER = 1 m.options.NODES = 3 m.options.MAX_MEMORY = 10 m.options.MAX_ITER = 500 m.options.COLDSTART = 0 m.options.REDUCE = 0 m.solve(disp=False) print("vA", vA.value[-1] + aux_vA) print() """ #print("Obj= ",Obj) print("altitude: ", y.value[-1], vx.value[-1], vy.value[-1]) print("Final mass=",Data[5][-1]-rate.value*t.value[-1]) print("Gravity= ",vG.value[-1], " Drag= ",vD.value[-1], " alpha= ", vA.value[-1]," Velocity= ", sqrt(vx.value[-1]**2+vy.value[-1]**2) ) print("Flight angle=",gamma.value[-1]) print("") print("l2:",l2.value[-1],"l3:", l3.value[-1], "aux:", aux.value[-1]) print("tf:", tf.value[-1], "tb:",tb-Data[0][-1]) print("Obj:", Obj) # if t_lb==-10.0 or t_ub==10.0: # break """ tm = np.linspace(Data[0][-1], tf.value[-1] + Data[0][-1], 100) mass = Data[5][-1] Data[0].extend(tm[1:]) Data[1].extend(x.value[1:]) Data[2].extend(y.value[1:]) for i in range(1, 100): Data[3].extend([sqrt(vx.value[i]**2 + vy.value[i]**2)]) Data[4].extend([atan(vy.value[i] / vx.value[i])]) Data[5].extend([mass - rate.value * t.value[i]]) Data[6].extend([atan((l4.value[i] - aux.value[i]) / l3.value[i])]) Data[7].extend(vG.value[1:]) Data[8].extend(vD.value[1:]) DV_required = mission.final_velocity + Data[7][-1] + Data[8][ -1] + aux_vA + vA.value[-1] Obj = m.options.objfcnval + DV_required return Data, DV_required, Obj
import numpy as np from gekko import GEKKO import matplotlib.pyplot as plt # Dynamic hydraulic model for pressure predictions with MPD m = GEKKO() # Constants p_0 = m.Const(1) # pressure downstream of the choke valve [bar] g = m.Const(9.81) # gravitational constant M_d = m.Const( 2500) # Lumped Density per length of Mud in Drill String [kg/m^4 * 1e5] M_a = m.Const( 800) # Lumped Density per length of Mud in Annulus [kg/m^4 * 1e5] Ad = m.Const(0.01025562) # Cross-sectional Area of Drill String [m^2] Aa = m.Const(0.1) # Cross-sectional Area of Annulus [m^2] # Parameters Kc = m.Param(0.3639) # Valve Coefficient Beta_d = m.Param(90000) # Bulk Modulus of Mud in Drill String [bar] Beta_a = m.Param(50000) f_d = m.Param(80) # Friction Factor in the drill string [bar*s^2/m^6] f_a = m.Param(330) # Friction Factor in the Annulus [bar*s^2/m^6] Ro_d = m.Param(1240) # Mud Density in the Drill String [kg/m^3] Ro_a = m.FV(1290, lb=Ro_d) # Mud Density in the Drill String Annulus [kg/m^3] q_p = m.Param(2.0) # Flow Rate through Pump [m^3/min] z_choke = m.Param(30) # Choke Valve Opening from 0-100 [%] q_res = m.Param(0) # reservoir gas influx flow rate [m^3/min] q_back = m.Param(0) # back pressure pump flow rate [m^3/min] ROP = m.Param(0) # rate of penetration (m/min)
time_data[0 + shift]) #makes the first set of data start at t=0 motor[d] = float(motor_data[d * space + shift]) mu[d] = float(mu_data[d * space + shift]) motor_efficiency[d] = float(motor_efficiency_data[d * space + shift]) motor_voltage[d] = float(motor_voltage_data[d * space + shift]) motor_current[d] = float(motor_current_data[d * space + shift]) #%% Gekko model m = GEKKO(server='http://byu.apmonitor.com') m.time = time #%% Constants, Variables, Intermediates, Equations, Settings #Constants Optimal_Temp = m.Const(value=Optimal_temp, name='Optimal_Temp') MW = m.Const(value=0.0289644, name='MW') #kg/mol Cp_air = m.Const(value=975.4, name='Cp_air') #J/kgK (found using HYSYS calculations) kf_air = m.Const(value=.01855, name='kf_air') #W/mK (found using HYSYS calculations) mass_batt = m.Const(value=142 / 4, name='mass_batt') #kg Cp_batt = m.Const(value=900, name='Cp_batt') #J/kgK kf_batt = m.Const(value=6, name='kf_batt') #W/mK radius_batt = m.Const(value=.5 / 2, name='radius_batt') #m (Estimation based on photographs) length_total = m.Const( value=2, name='length_total') #m (Estimation based on photographs) length_motor = m.Const(value=.5, name='length_motor') #m (Guess) V_open_circuit = m.Const(value=44, name='V_open_circuit') #Volts mu2 = m.Param(value=mu, name='mu2')
interpolant = interpolate.InterpolatedUnivariateSpline(sim_t, sim_u0, k=5) # host_cell = virus = default_host_cell(t1) u_step = interpolant(np.float64(m.time) * T0) plt.plot(t1, u_step) plt.show() u0 = m.Param(value=u_step) u1_ = np.diff(u_step, prepend=u_step[0]) / ((end / nt) / T0) u1 = m.Param(value=u1_) u2_ = np.diff(u1_, prepend=u1_[0]) / ((end / nt) / T0) u2 = m.Param(value=u2_) alpha_v = m.Const(virus.alpha) beta_v = m.Const(virus.beta) gamma_v = m.Const(virus.gamma) phi_v = m.Const(virus.phi) xi_v = m.Const(virus.xi) SX0 = m.Const(X0) SU0 = m.Const(U0) ST0 = m.Const(T0) m.Equation(x1_v == x0_v.dt()) m.Equation(x2_v == x1_v.dt()) m.Equation(x2_v == ((SU0 / (ST0**2)) * alpha_v * u2 + (SU0 / ST0) * beta_v * u1 + gamma_v * SU0 * u0 - phi_v * (SX0 / ST0) * x1_v - xi_v * SX0 * x0_v) / (SX0 / (ST0**2)))
import matplotlib.pyplot as plt #number of points in time discretization n = 91 #Initialize Model m = GEKKO() #define time discretization m.time = np.linspace(0, 90, n) #make array of drag coefficients, changing at time 60 drag = [(0.2 if t <= 60 else 10) for t in m.time] #define constants g = m.Const(value=9.81) mass = m.Const(value=80) #define drag parameter d = m.Param(value=drag) #initialize variables x, y, vx, vy, v, Fx, Fy = [m.Var(value=0) for i in range(7)] #initial conditions y.value = 5000 vx.value = 50 #Equations # force balance m.Equation(Fx == -d * vx**2)
from gekko import GEKKO import numpy as np import sys #Initialize Model m = GEKKO(remote=True) #help(m) #define parameter max = m.Const(value=3008) min = m.Const(value=128) step = m.Const(value=64) limit = m.Const(value=400) a = m.Const(value=sys.argv[2]) b = m.Const(value=sys.argv[2]) #initialize variables x = m.CV(integer=True) w = m.Var(integer=True) #lower bounds x.lower = min w.lower = 0 #upper bounds x.upper = max #equations m.Equation(x - w * step == 0) m.Equation(a * x + b <= limit)
Pg=148.5 Hg=237.88 Gg=86.88 Rg=47.0 #lengths from FIG. 1 - Beam Pumping Unit Shown as a Four-Bar Linkage L_1 = Rg L_2 = np.sqrt((Hg-Gg)**2.0+Ig**2.0) L_3 = Cg L_4 = Pg L_5 = Ag #Simulation######################################## #Constants sim.API = sim.Const(value = 45) #API gravity of fluid, unitless sim.c = sim.Const(value = 0.000013) #Compressibility, psi^-1 sim.k = sim.Const(value = 15) #Permeability, md sim.Bo = sim.Const(value = 1.2) #FVF, rb/STB sim.A_d = sim.Const(value = 2) #Drainage Area, Acres sim.sw = sim.Const(value = 0.2) #Water Saturation sim.porosity = sim.Const(value = 0.08) #Porosity, unitless sim.gamma_E = sim.Const(value = 1.78) #Euler Constant sim.C_a = sim.Const(value = 31.6) #Drainage Area Shape Factor (Circular) sim.rw = sim.Const(value = 0.328) #Welbore radius, ft sim.S = sim.Const(value = 0) #unitless sim.u_visc = sim.Const(value = 1.5) # Viscosity, cp sim.h_pz = sim.Const(value = 8) #pay zone thickness, ft sim.D_t = sim.Const(value = 2.5) # tubing diameter, in sim.St_length = sim.Const(value = 85) # rod pump stroke length, in
mdelH = 5e4 # E - Activation energy in the Arrhenius Equation (J/mol) # R - Universal Gas Constant = 8.31451 J/mol-K EoverR = 8750 # Pre-exponential factor (1/sec) k0 = 7.2e10 # U - Overall Heat Transfer Coefficient (W/m^2-K) # A - Area - this value is specific for the U calculation (m^2) UA = 5e4 # initial conditions Tc0 = 280 T0 = 304 Ca0 = 1.0 tau = m.Const(value=0.5) Kp = m.Const(value=1) m.Tc = m.MV(value=Tc0, lb=250, ub=350) m.T = m.CV(value=T_ss) m.rA = m.Var(value=0) m.Ca = m.CV(value=Ca_ss) m.Equation(m.rA == k0 * m.exp(-EoverR / m.T) * m.Ca) m.Equation(m.T.dt() == q/V*(Tf - m.T) \ + mdelH/(rho*Cp)*m.rA \ + UA/V/rho/Cp*(m.Tc-m.T)) m.Equation(m.Ca.dt() == q / V * (Caf - m.Ca) - m.rA)
def HE_solve(T_hot, T_cold, Num_HE): inf = 1000000000 m = GEKKO(remote=True) ''' Nh=random.randint(low=1,high=4) #corresponds to i Nc=random.randint(low=1,high=4) #corresponds to j Nhe=5 #corresponds to m ''' Nh = T_hot.__len__() #corresponds to i Nc = T_cold.__len__() #corresponds to j Nhe = Num_HE #corresponds to m #Initialize Cp array Cph = [m.Const(value=4.2) for i in range(Nh)] Cpc = [m.Const(value=4.2) for i in range(Nc)] #Initialize h array hh = [m.Const(value=1) for i in range(Nh)] hc = [m.Const(value=1) for i in range(Nc)] EMAT = [m.Const(value=10) for i in range(Nhe)] objMES = True #Temperature for Hot Stream #tracking array trackhot = [] trackcold = [] ''' #random inlet hot temperature case Th_i0=m.Array(m.Const,(1,Nh)) Thf=[m.Const(value=200) for i in range(Nh)]#250 for Th in Th_i0[0]: #Th.value=random.uniform(low=260,high=400) #random inlet Th.value=400 trackhot.append(Th.value) #random inlet cold temperature case Tc_j0=m.Array(m.Const,(1,Nc)) Tcf=[m.Const(value=150) for i in range(Nc)] for Tc in Tc_j0[0]: #Tc.value=random.uniform(low=0,high=50) Tc.value=40 trackcold.append(Tc.value) ''' #random inlet hot temperature case Th_i0 = m.Array(m.Const, (1, Nh)) Thf = [m.Const(value=200) for i in range(Nh)] #250 for i in range(Nh): #Th.value=random.uniform(low=260,high=400) #random inlet Th_i0[0][i].value = T_hot[i] trackhot.append(Th_i0[0][i].value) #random inlet cold temperature case Tc_j0 = m.Array(m.Const, (1, Nc)) Tcf = [m.Const(value=150) for i in range(Nc)] for i in range(Nc): #Tc.value=random.uniform(low=0,high=50) Tc_j0[0][i].value = T_cold[i] trackcold.append(Tc_j0[0][i].value) #announce heat exchanger print("Number of hot stream = ", Nh) print("Number of cold stream = ", Nc) print("Hot Stream Temperature=", trackhot) print("Cold Stream Temperature=", trackcold) #Integrated Heat Qm = [m.Var(lb=0, ub=inf) for i in range(Nhe)] #Heat cascade Thin = [[m.Var(lb=0, ub=inf) for i in range(Nhe)] for i in range(Nh)] Thout = [[m.Var(lb=0, ub=inf) for i in range(Nhe)] for i in range(Nh)] #Cold cascade Tcin = [[m.Var(lb=0, ub=inf) for i in range(Nhe)] for i in range(Nc)] Tcout = [[m.Var(lb=0, ub=inf) for i in range(Nhe)] for i in range(Nc)] #Heat and cool binary wh = [[m.Var(lb=-0.1, ub=1.1, integer=True) for i in range(Nhe)] for i in range(Nh)] wc = [[m.Var(lb=-0.1, ub=1.1, integer=True) for i in range(Nhe)] for i in range(Nc)] for k in range(Nhe): m.Equation(sum([wh[j][k] for j in range(Nh)]) == 1) m.Equation(sum([wc[j][k] for j in range(Nc)]) == 1) #Eq2 First row of Heat Matrix for i in range(Nh): m.Equation(Thin[i][0] == Th_i0[0][i]) #Eq5 First Row of Cool Matrix for i in range(Nc): m.Equation(Tcin[i][Nhe - 1] == Tc_j0[0][i]) #Eq3 and Eq6 Heat balance and Eq4 and Eq7 cascade operation for k in range(Nhe): for i in range(Nh): m.Equation(Thout[i][k] == Thin[i][k] - wh[i][k] * Qm[k] / Cph[i]) if k != 0: m.Equation(Thin[i][k] == Thout[i][k - 1]) for j in range(Nc): m.Equation(Tcout[j][k] == Tcin[j][k] + wc[j][k] * Qm[k] / Cpc[j]) if k != Nhe - 1: m.Equation(Tcin[i][k] == Tcout[i][k - 1]) #Additional cooler and heater Qcu = [m.Var(lb=0, ub=inf) for i in range(Nh)] Qhu = [m.Var(lb=0, ub=inf) for i in range(Nc)] #for MES Objective whQ = [[m.Var(value=0, lb=0, ub=inf) for i in range(Nhe)] for i in range(Nh)] wcQ = [[m.Var(value=0, lb=0, ub=inf) for i in range(Nhe)] for i in range(Nc)] for k in range(Nhe): for i in range(Nh): m.Equation(whQ[i][k] == wh[i][k] * Qm[k]) for j in range(Nc): m.Equation(wcQ[j][k] == wc[j][k] * Qm[k]) for i in range(Nh): m.Equation(Qcu[i] == Cph[i] * (Th_i0[0][i] - Thf[i]) - sum(whQ[i])) for i in range(Nc): m.Equation(Qhu[i] == Cpc[i] * (Tcf[i] - Tc_j0[0][i]) - sum(wcQ[i])) if objMES == True: m.Obj(0.2 * sum(Qcu) + 0.8 * sum(Qhu)) #Temperature of Recovert Heat Exchanger Thhin = [m.Var(lb=0, ub=inf) for i in range(Nhe)] Thhout = [m.Var(lb=0, ub=inf) for i in range(Nhe)] Tccin = [m.Var(lb=0, ub=inf) for i in range(Nhe)] Tccout = [m.Var(lb=0, ub=inf) for i in range(Nhe)] for k in range(Nhe): m.Equation( Thhout[k] == sum([wh[i][k] * Thout[i][k] for i in range(Nh)])) m.Equation( Tccout[k] == sum([wc[i][k] * Tcout[i][k] for i in range(Nc)])) m.Equation(Thhin[k] == sum([wh[i][k] * Thin[i][k] for i in range(Nh)])) m.Equation(Tccin[k] == sum([wc[i][k] * Tcin[i][k] for i in range(Nc)])) #Feasibility constraint for k in range(Nhe): m.Equation(Thhin[k] >= Tccout[k] + EMAT[k]) m.Equation(Thhout[k] >= Tccin[k] + EMAT[k]) for i in range(Nh): m.Equation(sum(whQ[i]) >= 0) m.Equation(sum(whQ[i]) <= Cph[i] * (Th_i0[0][i] - Thf[i])) m.Equation(Qcu[i] >= 0) m.Equation(Qcu[i] <= Cph[i] * (Th_i0[0][i] - Thf[i])) #m.Equation(Thout[i][Nhe-1]>=Thf[i])#Temperature down in cooler for j in range(Nc): m.Equation(sum(wcQ[j]) >= 0) m.Equation(sum(wcQ[j]) <= Cpc[j] * (Tcf[j] - Tc_j0[0][j])) m.Equation(Qhu[j] >= 0) #m.Equation(Tcout[j][0]<=Tcf[j]) #Temperature up in heater m.Equation(Qhu[j] <= Cpc[j] * (Tcf[j] - Tc_j0[0][j])) ''' #Making sure that temperature follows sequence for k in range(Nhe-1): for i in range(Nh): m.Equation(Thout[i][k]>=Thout[i][k+1]) #follow sequence for j in range(Nc): m.Equation(Tcout[j][k]<=Tcout[i][k+1]) #follow sequence ''' #Eqn 19-22 Cooler Heater Temperature Tcooler_hin = [m.Var(lb=0, ub=inf) for i in range(Nh)] Tcooler_hout = [m.Var(lb=0, ub=inf) for i in range(Nh)] Theater_cin = [m.Var(lb=0, ub=inf) for i in range(Nc)] Theater_cout = [m.Var(lb=0, ub=inf) for i in range(Nc)] for i in range(Nh): m.Equation(Tcooler_hin[i] == Thout[i][Nhe - 1]) m.Equation(Tcooler_hout[i] == Thf[i]) #m.Equation(Thf[i]>=Thout[i][Nhe-1]) for i in range(Nc): m.Equation(Theater_cin[i] == Tcout[i][0]) m.Equation(Theater_cout[i] == Tcf[i]) #m.Equation(Tcf[i]<=Tcout[i][0]) m.options.SOLVER = 1 m.solver_options = ['minlp_maximum_iterations 5000', \ # minlp iterations with integer solution 'minlp_max_iter_with_int_sol 5000', \ # treat minlp as nlp 'minlp_as_nlp 0', \ # nlp sub-problem max iterations 'nlp_maximum_iterations 2000', \ # 1 = depth first, 2 = breadth first 'minlp_branch_method 1', \ # maximum deviation from whole number 'minlp_integer_tol 0.001', \ # covergence tolerance 'minlp_gap_tol 0.00001'] try: m.solve() except: return inf print( sum([ Cph[i].value * (Th_i0[0][i].value - Thf[i].value) for i in range(Nh) ])) print( sum([ Cpc[i].value * (Tcf[i].value - Tc_j0[0][i].value) for i in range(Nc) ])) import pprint pp = pprint.PrettyPrinter(indent=4) print("Number of hot stream = ", Nh) print("Number of cold stream = ", Nc) print("Hot Stream Temperature=", trackhot) print("Cold Stream Temperature=", trackcold) import pprint print("Heat Recovered:", Qm) print("Hot binary:") print(wh) print("Cold binary:") print(wc) print("Hot Temperature in:", Thhin) print("Hot Temperature out:", Thout) print("Cooler Temperature:", Tcooler_hin, Tcooler_hout) print("Cold Temperature in:", Tcin) print("Cold Temperature out:", Tcout) print("Heater Temperature:", Theater_cin, Theater_cout) return m.options.objfcnval
from gekko import GEKKO import numpy as np import sys #Initialize Model m = GEKKO(remote=True) #help(m) #define parameter max = m.Const(value=3008) step = m.Const(value=64) a = m.Const(value=sys.argv[2]) b = m.Const(value=sys.argv[4]) min = m.Const(value=sys.argv[6]) index = m.Const(value=sys.argv[8]) limit = m.Const(value=sys.argv[10]) #initialize variables x = m.CV(integer=True) w = m.Var(integer=True) #lower bounds x.lower = min w.lower = 0 #upper bounds x.upper = max #equations
# m = GEKKO(remote=False) m.time = np.linspace(0, 5, 501) # m.options.SOLVER = 3 # Choose the solver, 1: APOPT, 2: BPOPT, 3: IPOPT (default) # m.options.MAX_ITER = 250 # Set the maximum number of solver iterations. Default is 250. # m.options.COLDSTART = 2 # m.options.AUTO_COLD = 5 # m.options.REDUCE = 10 # Parameters and Constants # https://gekko.readthedocs.io/en/latest/quick_start.html#parameters # https://gekko.readthedocs.io/en/latest/quick_start.html#constants mass_1 = 1.0 # (kg) mass_2 = 1.0 # (kg) c = m.Const(value=0.0) # Spring constant (N/m/s) k = m.Const(value=(2 * 2 * np.pi)**2) # Spring constant (N/m) MAX_FORCE = 50.0 # maximum force from actuator (N) MIN_FORCE = -50.0 # minimum force from actuator (N) # Manipulated variables - Here, it's just the force acting on the mass # https://gekko.readthedocs.io/en/latest/quick_start.html#manipulated-variable u = m.MV(value=0, lb=MIN_FORCE, ub=MAX_FORCE) u.STATUS = 1 # allow optimizer to change this variable # Weight on control input # https://gekko.readthedocs.io/en/latest/tuning_params.html#cost u.COST = 1e-6 # In general, we'll need to set the _.COST value. # The others below are optional
def solve(self, pre_calc, hour, first_hour, final_hour, prev_result): # over the whole year hp_performance = pre_calc['hp_performance'] surplus = pre_calc['surplus'] deficit = pre_calc['deficit'] match = pre_calc['match'] generation_total = pre_calc['generation_total'] wind = pre_calc['wind'] PV = pre_calc['PV'] # over the first hour to final hour plus horizon # indexed from 0 to final hour plus horizon max_capacity = pre_calc['max_capacity'] # temp parameters rt = self.return_temp st = self.source_temp ft = self.flow_temp sdt = self.source_delta_t number_timesteps = final_hour - hour t1 = hour - first_hour t2 = final_hour - first_hour m = GEKKO(remote=False) m.time = np.linspace(0, number_timesteps - 1, number_timesteps) # cost coefficient... import cost IC = m.Param(value=list(self.import_cost[hour:final_hour])) # elec demand and res used for elec demand # renewable used RES = m.Param(value=list(generation_total[hour:final_hour])) RES_ed = m.Var(value=prev_result['RES_ed'], lb=0) # elec demand ed = m.Param(value=list(self.elec_demand[hour:final_hour].values)) # surplus = m.Param(value=surplus[hour:final_hour].values) export = m.Var(value=prev_result['export'], lb=0) # import straight to electrical demand imp_ed = m.Var(value=prev_result['imp_ed'], lb=0) # heat demand hd = m.Param(value=list(self.heat_demand[hour:final_hour].values)) # heat pump output from renewables # heat pump renewable thermal output to demand HPtrd = m.Var(value=prev_result['HPtrd'], lb=0) # heat pump renewable to storage # maximum charging to storage with renewables HPtrs = m.Var(value=prev_result['HPtrs'], lb=0) # heat pump output from imports to demand HPtid = m.Var(value=prev_result['HPtid'], lb=0) # heat pump output from imports to storage HPtis = m.Var(value=prev_result['HPtis'], lb=0) # heat pump output from ES to demand HPtesd = m.Var(value=prev_result['HPtis'], lb=0) # performance of heat pump parameters cop = [] duty = [] HP_min = [] for i in range(hour, final_hour): if self.myHeatPump == 0: duty.append(0.0) else: duty.append(hp_performance[i]['duty']) cop.append(hp_performance[i]['cop']) HP_min.append(hp_performance[i]['duty'] * self.myHeatPump.minimum_output * self.myHeatPump.minimum_runtime / 60 / 100.0) cop = m.Param(value=cop) duty = m.Param(value=duty) HP_min = m.Param(value=HP_min) # heat pump on/off status HP_status = m.Var(value=prev_result['HP_status'], lb=0, ub=1, integer=True) # heat pump output without on/off status HPt_var = m.Var(value=prev_result['HPt_var'], lb=0) # heat pump total output HPt = m.Intermediate(HP_status * HPt_var) # thermal storage parameters # max capacity max_cap = m.Param(value=list(max_capacity[t1:t2])) # initial state of charge init_soc = self.myHotWaterTank.max_energy_in_out( 'discharging', prev_result['final_nodes_temp'], st[hour], ft[hour], rt, hour) # max_charge = m.Intermediate(max_cap - init_soc) # for clarity max discharge is simply the soc # storage charge/discharge TSc = m.Var(value=prev_result['TSc'], lb=0) TSd = m.Var(value=prev_result['TSd'], lb=0) # soc = m.Intermediate(init_soc + TSc - TSd) soc = m.Var(value=init_soc, lb=0) max_charge = m.Intermediate(max_cap - soc) loss = m.Intermediate(0.01 * soc) # loss = m.Intermediate(0.0) # electrical storage parameters # max capacity max_cap_ES = m.Const(self.myElectricalStorage.capacity) # print(max_cap_ES.value) # print(prev_result['soc_ES']) max_charge_ES = m.Const(self.myElectricalStorage.charge_max) max_discharge_ES = m.Const(self.myElectricalStorage.discharge_max) charge_eff = m.Const(self.myElectricalStorage.charge_eff) discharge_eff = m.Const(self.myElectricalStorage.discharge_eff) # for clarity max discharge is simply the soc # storage charge/discharge ESc = m.Var(value=prev_result['ESc'], lb=0) ESc_res = m.Var(value=prev_result['ESc_res'], lb=0) ESc_imp = m.Var(value=prev_result['ESc_imp'], lb=0) ESd = m.Var(value=prev_result['ESd'], lb=0) ESd_hp = m.Var(value=prev_result['ESd_hp'], lb=0) ESd_aux = m.Var(value=prev_result['ESd_aux'], lb=0) ESd_ed = m.Var(value=prev_result['ESd_ed'], lb=0) soc_ES = m.Var(value=prev_result['soc_ES'], lb=0) loss_ES = m.Intermediate(self.myElectricalStorage.self_discharge * soc_ES) # aux parameters # back-up electrical heater # capacity is equal to peak demand aux_cap = np.amax(self.heat_demand.values) # aux from renewables to demand # only electric aux aux_rd = m.Var(value=prev_result['aux_rd'], lb=0) # only electric aux # aux from from renewables to storage aux_rs = m.Var(value=prev_result['aux_rs'], lb=0) # aux for demand aux_d = m.Var(value=prev_result['aux_d'], lb=0) # aux for storage aux_s = m.Var(value=prev_result['aux_s'], lb=0) # aux_cap = 1000 aux = m.Var(value=prev_result['aux'], lb=0) if self.myAux.fuel == 'Electric': aux_cost = m.Param(value=list(self.import_cost[hour:final_hour])) else: aux_cost = m.Const(self.myAux.cost) # equations # different for electric since it can use RES production if self.myAux.fuel == 'Electric': # equalities m.Equations([ soc_ES.dt() == ESc * charge_eff - ESd - loss_ES, ESd == ESd_ed + ESd_hp + ESd_aux, ESc == ESc_res + ESc_imp, ed == RES_ed + imp_ed + ESd_ed * discharge_eff, hd == HPtrd + HPtid + HPtesd + aux_d + aux_rd + ESd_aux * discharge_eff + TSd, soc.dt() == TSc - TSd - loss, HPtesd == ESd_hp * cop * discharge_eff, HP_status * HPt_var == HPtrs + HPtrd + HPtid + HPtis + HPtesd, aux == aux_d + aux_s + aux_rd + aux_rs + ESd_aux, TSc == HPtrs + HPtis + aux_rs + aux_s, RES == RES_ed + (HPtrs + HPtrd) / cop + ESc_res + aux_rd + aux_rs + export ]) # other auxiliary sources can't use the RES else: # equalities m.Equations( # heat demand must be met, # but can be exceeded if needed to store more [ soc_ES.dt() == ESc * charge_eff - ESd - loss_ES, ESd == ESd_ed, ESc == ESc_res + ESc_imp, ed == RES_ed + imp_ed + ESd_ed * discharge_eff, hd == HPtrd + HPtid + aux_d + TSd, soc.dt() == TSc - TSd - loss, HP_status * HPt_var == HPtrs + HPtrd + HPtid + HPtis, aux == aux_d + aux_s, TSc == HPtrs + HPtis + aux_s, RES == RES_ed + (HPtrs + HPtrd) / cop + ESc_res + export ]) # inequalities m.Equations([ HPt_var <= duty, HPt_var >= HP_min, soc <= max_cap, TSc <= max_charge, TSd <= soc, soc_ES <= max_cap_ES, ESc <= max_charge_ES * charge_eff, ESd <= max_discharge_ES, ESd <= soc_ES, aux <= aux_cap ]) # self.export_cost = 1 # objective # last term is to disadvantage charging e store with res m.Obj(IC / 1000 * ((HPt - HPtrd - HPtesd - HPtrs) / cop + imp_ed + ESc_imp) + (aux - aux_rs - aux_rd - ESd_aux) * aux_cost / 1000 - export * self.export_cost / 1000 + 0.00001 * ESc_res) m.options.IMODE = 6 # MPC mode m.options.SOLVER = 1 # APOPT for solving MINLP problems if self.myHeatPump.minimum_output == 0: i = 'minlp_as_nlp 1' else: i = 'minlp_as_nlp 0' m.solver_options = ['minlp_maximum_iterations 500', \ # minlp iterations with integer solution 'minlp_max_iter_with_int_sol 500', \ # treat minlp as nlp 'minlp_as_nlp 1', \ # nlp sub-problem max iterations 'nlp_maximum_iterations 500', \ # 1 = depth first, 2 = breadth first 'minlp_branch_method 1', \ # maximum deviation from whole number 'minlp_integer_tol 0.05', \ # covergence tolerance 'minlp_gap_tol 0.05'] m.solve(disp=False) # print hour # print HP_status[1], 'status' # print HP_min[1], 'min' # print HPt[1], 'HPt' # print HPtrs[1], 'HPtrs' # print HPtrd[1], 'HPtrd' # print HPtis[1], 'HPtis' # print HPtid[1], 'HPtid' # print aux[1], 'aux' # print aux_rs[1], 'aux res store' # print aux_rd[1], 'aux res dem' # print aux_d[1], 'aux_d' # print aux_s[1], 'aux_s' # print TSc[1], 'TSc' # print max_charge[1], 'max_charge' # print TSd[1], 'TSd' # print soc[1], 'soc' # print hd[1], 'hd' # print IC[1], 'import_price' h = 1 TSc_ = round(TSc[h] - TSd[h], 2) TSd_ = round(TSd[h] - TSc[h], 2) if TSc_ > TSd_: max_c = self.myHotWaterTank.max_energy_in_out( 'charging', prev_result['final_nodes_temp'], st[h], ft[h], rt, h) TSc[h] = min(max_c, TSc_) TSd[h] = 0 if TSc[h] == 0.0: state = 'standby' else: state = 'charging' elif TSd_ > TSc_: max_d = self.myHotWaterTank.max_energy_in_out( 'discharging', prev_result['final_nodes_temp'], st[h], ft[h], rt, h) TSd[h] = min(max_d, TSd_) TSc[h] = 0.0 if TSd[h] == 0.0: state = 'standby' else: state = 'discharging' else: state = 'standby' # thermal_output = HPt[h] + aux[h] # next_nodes_temp = self.myHotWaterTank.new_nodes_temp( # state, prev_result['final_nodes_temp'], st[h], sdt, ft[h], # rt, thermal_output, hd[h], hour + h) if self.myHotWaterTank.capacity == 0: final_nodes_temp = prev_result['final_nodes_temp'] else: if self.myAux.fuel == 'Electric': # thermal_output = HPt[h] + aux[h] next_nodes_temp = self.myHotWaterTank.new_nodes_temp( state, prev_result['final_nodes_temp'], st[h], sdt, ft[h], rt, TSc[h], TSd[h], hour + h) final_nodes_temp = next_nodes_temp[-1] # final_nodes_temp = [round(elem, 2) for elem in final_nodes_temp] # print prev_result['final_nodes_temp'], 'prev_result' # print final_nodes_temp # results are for the second timestep timestep = hour + 1 results = self.set_of_results() # elec demand results results['elec_demand']['elec_demand'] = self.elec_demand[timestep] results['elec_demand']['RES'] = RES_ed[h] results['elec_demand']['import'] = imp_ed[h] results['elec_demand']['ES'] = (ESd_ed[h] * self.myElectricalStorage.discharge_eff) # RES results results['RES']['generation_total'] = generation_total[timestep] results['RES']['wind'] = wind[timestep] results['RES']['PV'] = PV[timestep] results['RES']['elec_demand'] = RES_ed[h] results['RES']['HP'] = ((HPtrd[h] + HPtrs[h]) / hp_performance[timestep]['cop']) # print results['RES']['HP'] if self.myAux.fuel == 'Electric': results['RES']['aux'] = aux_rd[h] + aux_rs[h] results['RES']['export'] = export[h] # heat pump results results['HP']['cop'] = hp_performance[timestep]['cop'] results['HP']['duty'] = hp_performance[timestep]['duty'] results['HP']['heat_total_output'] = HPt[h] results['HP']['heat_from_ES_to_demand'] = HPtesd[h] results['HP']['heat_to_heat_demand'] = min( results['HP']['heat_total_output'], hd[h]) results['HP']['heat_to_TS'] = (results['HP']['heat_total_output'] - results['HP']['heat_to_heat_demand']) results['HP']['elec_total_usage'] = ( results['HP']['heat_total_output'] / results['HP']['cop']) results['HP']['elec_RES_usage'] = (results['RES']['HP']) results['HP']['elec_from_ES_to_demand'] = ( results['HP']['heat_from_ES_to_demand'] / results['HP']['cop']) results['HP']['elec_import_usage'] = ( results['HP']['elec_total_usage'] - results['HP']['elec_RES_usage'] - results['HP']['elec_from_ES_to_demand']) # heat demand results results['heat_demand']['TS'] = TSd[h] results['heat_demand']['heat_demand'] = self.heat_demand[timestep] results['heat_demand']['HP'] = results['HP']['heat_to_heat_demand'] results['heat_demand']['aux'] = aux[h] # TS results results['TS']['charging_total'] = TSc[h] results['TS']['discharging_total'] = TSd[h] results['TS']['HP_to_TS'] = (results['HP']['heat_to_TS']) results['TS']['HP_from_RES_to_TS'] = ( results['HP']['heat_from_RES_to_TS']) results['TS']['HP_from_import_to_TS'] = ( results['HP']['heat_from_import_to_TS']) results['TS']['aux_to_TS'] = (results['TS']['charging_total'] - results['TS']['HP_to_TS']) results['TS']['final_nodes_temp'] = final_nodes_temp # # ES results results['ES']['charging_from_RES'] = ESc_res[h] results['ES']['charging_from_import'] = ESc_imp[h] results['ES']['charging_total'] = ( results['ES']['charging_from_RES'] + results['ES']['charging_from_import']) results['ES']['discharging_to_demand'] = ( ESd_ed[h] * self.myElectricalStorage.discharge_eff) results['ES']['discharging_to_HP'] = ESd_hp[h] results['ES']['discharging_to_aux'] = ESd_aux[h] results['ES']['discharging_total'] = ( results['ES']['discharging_to_demand'] + results['ES']['discharging_to_HP'] + results['ES']['discharging_to_aux']) # update state of charge # new soc final_soc = soc_ES[h] results['ES']['final_soc'] = final_soc # AUX results results['aux']['demand'] = aux[h] results['aux']['usage'] = self.myAux.fuel_usage( results['aux']['demand']) if self.myAux.fuel == 'Electric': aux_price = results['grid']['import_price'] / 1000. density = 0.0 else: aux_price = self.myAux.cost / 1000. density = self.myAux.energy_density results['aux']['cost'] = aux_price * results['aux']['demand'] results['aux']['mass'] = density * results['aux']['usage'] # grid results results['grid']['import_for_elec_demand'] = ( results['elec_demand']['import']) results['grid']['import_for_heat_pump_total'] = ( results['HP']['elec_import_usage']) results['grid']['import_for_ES'] = ( results['ES']['charging_from_import']) results['grid']['total_export'] = results['RES']['export'] if self.myAux.fuel == 'Electric': results['grid']['total_import'] = ( results['grid']['import_for_elec_demand'] + results['grid']['import_for_heat_pump_total'] + results['grid']['import_for_ES'] + results['aux']['demand'] - results['RES']['aux']) else: results['grid']['total_import'] = ( results['grid']['import_for_elec_demand'] + results['grid']['import_for_heat_pump_total'] + results['grid']['import_for_ES']) results['grid']['import_price'] = IC[h] # this is in pounds # divide by 1000 because import price in pound/MWh # total import is in kWH results['grid']['import_costs'] = (results['grid']['total_import'] * results['grid']['import_price'] / 1000.) results['grid']['export_income'] = (results['grid']['total_export'] * self.export_cost / 1000.) results['grid']['cashflow'] = (results['grid']['export_income'] - results['grid']['import_costs']) results['grid']['surplus'] = surplus[timestep] results['grid']['deficit'] = deficit[timestep] results['grid']['match'] = match[timestep] next_results = { 'HPt': HPt[h], 'HPtrs': HPtrs[h], 'HPtrd': HPtrd[h], 'HPtid': HPtid[h], 'HPtis': HPtis[h], 'HPtesd': HPtesd[h], 'HP_status': HP_status[h], 'HPt_var': HPt_var[h], 'aux': aux[h], 'aux_rd': aux_rd[h], 'aux_rs': aux_rs[h], 'aux_d': aux_d[h], 'aux_s': aux_s[h], 'TSc': TSc[h], 'TSd': TSd[h], 'final_nodes_temp': final_nodes_temp, 'state': state, 'import_cost': IC[h], 'export': export[h], 'soc_ES': soc_ES[h], 'ESc': ESc[h], 'ESc_imp': ESc_imp[h], 'ESc_res': ESc_res[h], 'ESd': ESd[h], 'ESd_ed': ESd_ed[h], 'ESd_hp': ESd_hp[h], 'ESd_aux': ESd_aux[h], 'imp_ed': imp_ed[h], 'RES_ed': RES_ed[h] } # print results # print hd.value, 'hd' # print HPt.value, 'HPt' # print duty.value, 'duty' # print cop.value, 'cop' # print aux.value, 'aux' # print TSc.value, 'charging' # print TSd.value, 'discharging' # print soc.value, 'soc' # print IC.value, 'import cost' # print aux_cost.value, 'auxiliary cost' # print HPtrd.value, 'heat pump renewable to demand' # print HPtrs.value, 'heat pump renewable to storage' # print cop.value, 'cop' # print max_charge.value, 'max charge' # print final_nodes_temp, 'final_nodes_temp' # print HPtrs.value, 'HPtrs' # print HP_left.value, 'heat pump capacity left' # print max_charge.value, 'max charge available' # print HPtrd.value, 'HPtrd' # print HPtrd_max.value, 'HPtrd_max' # print surplus[first_hour:final_hour].values, 'surplus' # print cop.value, 'cop' # print duty.value, 'duty' # Plot solution # plt.figure() # plt.subplot(4, 1, 1) # plt.plot(m.time, HPt.value, 'r', LineWidth=2) # plt.plot(m.time, aux.value, 'y', LineWidth=2) # plt.plot(m.time, hd.value, 'g', LineWidth=2) # plt.ylabel(['HPt', 'aux', 'HD']) # plt.legend(['HPt', 'aux', 'HD'], loc='best') # plt.subplot(4, 1, 2) # plt.plot(m.time, soc.value, 'b', LineWidth=2) # plt.legend(['SOC'], loc='best') # plt.ylabel('SOC') # plt.subplot(4, 1, 3) # plt.plot(m.time, IC.value, 'g', LineWidth=2) # plt.legend(['Import cost'], loc='best') # plt.ylabel('Import cost') # plt.subplot(4, 1, 4) # plt.plot(m.time, surplus.value, 'm', LineWidth=2) # plt.legend([r'surplus'], loc='best') # plt.ylabel('Surplus') # plt.xlabel('Time') # plt.show() return {'results': results, 'next_results': next_results}
TC1.UPPER = 200 TC2 = m.CV(value=T2m[0], name='tc2') TC2.STATUS = 1 # minimize error between simulation and measurement TC2.FSTATUS = 1 # receive measurement TC2.MEAS_GAP = 0.1 # measurement deadband gap TC2.LOWER = 0 TC2.UPPER = 200 Ta = m.Param(value=23.0 + 273.15) # K mass = m.Param(value=4.0 / 1000.0) # kg Cp = m.Param(value=0.5 * 1000.0) # J/kg-K A = m.Param(value=10.0 / 100.0**2) # Area not between heaters in m^2 As = m.Param(value=2.0 / 100.0**2) # Area between heaters in m^2 eps = m.Param(value=0.9) # Emissivity sigma = m.Const(5.67e-8) # Stefan-Boltzman # Heater temperatures T1 = m.Intermediate(TH1 + 273.15) T2 = m.Intermediate(TH2 + 273.15) # Heat transfer between two heaters Q_C12 = m.Intermediate(U * As * (T2 - T1)) # Convective Q_R12 = m.Intermediate(eps * sigma * As * (T2**4 - T1**4)) # Radiative # Semi-fundamental correlations (energy balances) m.Equation(TH1.dt() == (1.0/(mass*Cp))*(U*A*(Ta-T1) \ + eps * sigma * A * (Ta**4 - T1**4) \ + Q_C12 + Q_R12 \ + alpha1*Q1))
# Parameters and Constants # https://gekko.readthedocs.io/en/latest/quick_start.html#parameters # https://gekko.readthedocs.io/en/latest/quick_start.html#constants # Define the model parameters mass_1 = 1.0 # (kg) mass_2 = 1.0 # (kg) SPRING_CONSTANT = (2 * 2 * np.pi)**2 # Spring constant (N/m) DAMPING_COEFF = 1.0 # Damping coefficient (N/m/s) MAX_FORCE = 50.0 # maximum force from actuator (N) MIN_FORCE = -50.0 # minimum force from actuator (N) #Make the damping coefficient and spring constant parameters # https://gekko.readthedocs.io/en/latest/quick_start.html#parameters c = m.Const(value=DAMPING_COEFF) k = m.Const(value=SPRING_CONSTANT) # Manipulated variables - Here, it's just the force acting on the mass # https://gekko.readthedocs.io/en/latest/quick_start.html#manipulated-variable u = m.MV(value=0, lb=MIN_FORCE, ub=MAX_FORCE) u.STATUS = 1 # allow optimizer to change this variable u.FSTATUS = 0 # Weight on control input # https://gekko.readthedocs.io/en/latest/tuning_params.html#cost u.COST = 1e-9 # In general, we'll need to set the _.COST value. # The others below are optional
A_1_initial_guess = 0.114e6 # m ^ 3 / mol s A_2_initial_guess = 3.06e9 # 1 / s4 alpha_initial_guess = 1 # reaction order beta_initial_guess = 1 # reaction order Ph_2_minus_initial = ph_abs[0] # get from data #Ph_3_minus_initial = 1.145565033 - ph_abs[0] Ph_3_minus_initial = 0.994207 - ph_abs[0] # initialize model reaction_model = GEKKO() # set model time as time gathered data reaction_model.time = time # Constants R = reaction_model.Const(8.3145) # Gas constant J / mol K # Parameters T = reaction_model.Param(temperature) # Fixed Variables to change # bounds """ E_a_1 = reaction_model.FV(E_a_1_initial_guess, lb = 33900, ub = 37900) E_a_2 = reaction_model.FV(E_a_2_initial_guess, lb = 73000, ub = 81000) A_1 = reaction_model.FV(A_1_initial_guess, lb = 0) A_2 = reaction_model.FV(A_2_initial_guess, lb = 0) alpha = reaction_model.FV(alpha_initial_guess)#, lb = 0, ub = 1) beta = reaction_model.FV(beta_initial_guess)#, lb = 0, ub = 1) """
def solve(self, num_slices=0, remote_flag=True, solver=1): try: # Initialize gekko model (server='http://xps.apmonitor.com') m = GEKKO(remote=remote_flag, name="optimal_wifi_slicing" ) # (optional) server='http://xps.apmonitor.com' # Initialize Solver (1=ADOPT, 2=BPOPT, len(slices)=IPOPT) ''' 1- ADOPT: is generally the best when warm-starting from a prior solution or when the number of degrees of freedom (Number of Variables - Number of Equations) is less than 2000 2- BPOPT has been found to be the best for systems biology applications. 3- IPOPT is generally the best for problems with large numbers of degrees of freedom or when starting without a good initial guess. ''' m.options.SOLVER = solver #m.solver_options = ['minlp_gap_tol 1.0e-2', # 'minlp_maximum_iterations 10000', # 'minlp_max_iter_with_int_sol 500', # 'nlp_maximum_iterations 200'] # ------- Initialize Input ------- # # Problem Input initialization rb_max_throughput = m.Const( self.data['resource_block']['max_throughput'], 'rb_max_throughput') # Resource Block max throughput min_quantum = m.Const( self.data['minimum_bounds']['minimum_quantum'], 'min_quantum') lambda_s = [] avg_airtimes = [] # equal lambdas # if num_slices > 0: # equal_lambdas = 39 / num_slices # else: # equal_lambdas = 39 for slc in self.data['slices']: lambda_s.append(slc['req_throughput']) # lambda_s.append(equal_lambdas) avg_airtimes.append(slc['airtime_needed_avg']) print('RB Max Throughput: ' + str(rb_max_throughput.value)) print('Sum of Lambdas_s: ' + str(sum(lambda_s)) + ' Mbps') print('Slices loaded: ' + str(len(self.data['slices']))) # Solve for x number of slices print('Solving for: ' + str(num_slices) + ' slices!') print('New sum of lambdas: ' + str(sum(lambda_s[0:num_slices])) + ' Mbps') # ------- Initialize variables ------- # q_s = [] # Quantum values on slice s mu_s_pkt = [] # Packet dequeuing rate on slice s mu_s = [] # Dequeuing rate on slice s p_s = [] # Service utilization on slice s n_s = [] # Average service rate on slice s t_s = [] # Average time in the system on slice s for i in range(num_slices): q_s.append( m.Var(lb=min_quantum, ub=avg_airtimes[i], name='q_s' + str(self.data['slices'][i]['id']))) mu_s_pkt.append( m.Intermediate(q_s[i] / avg_airtimes[i], name='mu_s_pkt_' + str(i))) for i in range(num_slices): mu_s.append( m.Intermediate( (mu_s_pkt[i] / sum(mu_s_pkt)) * rb_max_throughput, name='mu_s_' + str(i))) p_s.append( m.Intermediate(lambda_s[i] / mu_s[i], name='p_s_' + str(i))) n_s.append( m.Intermediate(p_s[i] / (1 - p_s[i]), name='n_s_' + str(i))) t_s.append( m.Intermediate(n_s[i] / lambda_s[i], name='t_s_' + str(i))) # ------- End variables ------- # # ------- Equations ------- # for i in range(num_slices): m.Equation(mu_s[i] >= lambda_s[i]) m.Equation(sum(mu_s) <= rb_max_throughput) for i in range(num_slices): m.Equation(p_s[i] < 1) # ------- End equations ------- # # Objectives are always minimized (maximization is possible by multiplying the objective by -1) obj = (sum(t_s)) m.Obj(obj) #m.open_folder() m.solve(disp=True, debug=True) # Solve for i in range(num_slices): print('q_s ' + str(i) + ': ' + str(q_s[i].value)) for i in range(num_slices): print('mu_s_pkt ' + str(i) + ': ' + str(mu_s_pkt[i].value)) for i in range(num_slices): print('mu_s ' + str(i) + ': ' + str(mu_s[i].value)) for i in range(num_slices): print('p_s ' + str(i) + ': ' + str(p_s[i].value)) for i in range(num_slices): print('n_s ' + str(i) + ': ' + str(n_s[i].value)) for i in range(num_slices): print('t_s ' + str(i) + ': ' + str(t_s[i].value)) except: print("An exception occurred")
from gekko import GEKKO import numpy as np import matplotlib.pyplot as plt m = GEKKO() tf = 200 m.time = np.linspace(0, tf, 2 * tf + 1) step = np.zeros(2 * tf + 1) step[3:40] = 2.0 step[40:] = 5.0 # Controller model Kc = 15.0 # controller gain tauI = 2.0 # controller reset time tauD = 1.0 # derivative constant OP_0 = m.Const(value=0.0) # OP bias OP = m.Var(value=0.0) # controller output PV = m.Var(value=0.0) # process variable SP = m.Param(value=step) # set point Intgl = m.Var(value=0.0) # integral of the error err = m.Intermediate(SP - PV) # set point error m.Equation(Intgl.dt() == err) # integral of the error m.Equation(OP == OP_0 + Kc * err + (Kc / tauI) * Intgl - PV.dt()) # Process model Kp = 0.5 # process gain tauP = 10.0 # process time constant m.Equation(tauP * PV.dt() + PV == Kp * OP) m.options.IMODE = 4 m.solve(disp=False)
t = m.Param(value=m.time) u0 = m.Var(value=1) # m.Equation(u0 == 1) #doesn't seem to behave well with this discontinuity. # the gaussian pulse below works much better. # m.Equation(u0 == m.sin(t)) # for simulation # m.Equation(u0 == m.exp(-((((t*T0)-((end*T0))/2.0))**2.0)/(2.0*(((((end*T0)/10.0))**2.0))))) # for simulation m.fix(u0, val=0, pos=0) u1 = m.Var() m.Equation(u1 == u0.dt()) u2 = m.Var() m.Equation(u2 == u1.dt()) alpha_h = m.Const(host_cell.alpha) beta_h = m.Const(host_cell.beta) gamma_h = m.Const(host_cell.gamma) phi_h = m.Const(host_cell.phi) xi_h = m.Const(host_cell.xi) alpha_v = m.Const(virus.alpha) beta_v = m.Const(virus.beta) gamma_v = m.Const(virus.gamma) phi_v = m.Const(virus.phi) xi_v = m.Const(virus.xi) SX0 = m.Const(X0) SU0 = m.Const(U0) ST0 = m.Const(T0)