def generate_expression(self, variable_list): """ The ``generate_expression`` method returns the Pyomo expression for the Kriging model trained. The expression is constructed based on the supplied list of variables **variable_list** and the results of the previous Kriging training process. Args: variable_list(list) : List of input variables to be used in generating expression. This can be the a list generated from the output of ``get_feature_vector``. The user can also choose to supply a new list of the appropriate length. Returns: Pyomo Expression : Pyomo expression of the Kriging model based on the variables provided in **variable_list** """ t1 = np.array([variable_list]) phi_var = [] for i in range(0, self.x_data.shape[0]): curr_term = sum(self.optimal_weights[j] * ( ((t1[0, j] - self.x_data_min[0, j])/(self.x_data_max[0, j] - self.x_data_min[0, j])) - self.x_data_scaled[i, j]) ** self.optimal_p for j in range(0, self.x_data.shape[1])) curr_term = exp(-curr_term) phi_var.append(curr_term) phi_var_array = np.asarray(phi_var) phi_inv_times_y_mu = np.matmul(self.covariance_matrix_inverse, self.optimal_y_mu) phi_inv_times_y_mu = phi_inv_times_y_mu.reshape(phi_inv_times_y_mu.shape[0], ) kriging_expr = self.optimal_mean[0,0] kriging_expr += sum(w * t for w, t in zip(np.nditer(phi_inv_times_y_mu), np.nditer(phi_var_array, flags=['refs_ok']) )) return kriging_expr
def rule_odes(m,t): Ra = m.P['k']*pyomo.exp(-m.P['ER']/m.X[t,'T'])*m.Z[t,'A'] exprs = dict() exprs['A'] = C['F']/C['V']*(m.P['Cfa']-m.Z[t,'A']) - Ra exprs['T'] = C['F']/C['V']*(m.P['Tf']-m.X[t,'T']) + m.P['delH']/(m.P['rho'])/C['Cp']*Ra - m.P['h']*C['A']/(m.P['rho'])/C['Cp']/C['V']*(m.X[t,'T'] - m.X[t,'Tc']) exprs['Tc'] = C['Fc']/C['Vc']*(m.P['Tfc']-m.X[t,'Tc']) + m.P['h']*C['A']/(m.P['rhoc'])/C['Cpc']/C['Vc']*(m.X[t,'T'] - m.X[t,'Tc']) return exprs
def test_mc_3d(self): m = ConcreteModel() m.x = Var(bounds=(-2, 1), initialize=-1) m.y = Var(bounds=(-1, 2), initialize=0) m.e = Expression(expr=m.x * pow(exp(m.x) - m.y, 2)) ccSurf, cvSurf, ccAffine, cvAffine = make3dPlot(m.e.expr, 30) self.assertAlmostEqual(ccSurf[48], 11.5655473482574) self.assertAlmostEqual(cvSurf[48], -15.28102124928224) self.assertAlmostEqual(ccAffine[48], 11.565547348257398) self.assertAlmostEqual(cvAffine[48], -23.131094696514797)
def test_mc_3d(self): m = ConcreteModel() m.x = Var(bounds=(-2, 1), initialize=-1) m.y = Var(bounds=(-1, 2), initialize=0) m.e = Expression(expr=m.x * pow(exp(m.x) - m.y, 2)) ccSurf, cvSurf, ccAffine, cvAffine = make3dPlot(m.e.expr, 30) self.assertAlmostEqual(ccSurf[48], 11.5655473482574) self.assertAlmostEqual(cvSurf[48], -15.28102124928224) self.assertAlmostEqual(ccAffine[48], 11.565547348257398) self.assertAlmostEqual(cvAffine[48], -23.131094696514797)
def test_divide_by_mutable(self): # # Test from https://github.com/Pyomo/pyomo/issues/153 # m = ConcreteModel() m.x = Var(bounds=(1,5)) m.p = Param(initialize=100, mutable=True) m.con = Constraint(expr=exp(5*(1/m.x - 1/m.p))<=10) m.obj = Objective(expr=m.x**2) test = gar(m.con.body) self.assertEqual(test.constant, 0) self.assertEqual(test.linear_vars, tuple()) self.assertEqual(test.linear_coefs, tuple()) self.assertEqual(set(id(v) for v in test.nonlinear_vars), set([id(m.x)])) self.assertIs(test.nonlinear_expr, m.con.body)
ans *= i return ans def _sum(*x): return sum(i for i in x) def _nondifferentiable(*x): raise NondifferentiableError( "The sub-expression '%s' is not differentiable with respect to %s" % (x[0], x[1])) _operatorMap = { sympy.Add: _sum, sympy.Mul: _prod, sympy.Pow: lambda x, y: x**y, sympy.exp: lambda x: core.exp(x), sympy.log: lambda x: core.log(x), sympy.sin: lambda x: core.sin(x), sympy.asin: lambda x: core.asin(x), sympy.sinh: lambda x: core.sinh(x), sympy.asinh: lambda x: core.asinh(x), sympy.cos: lambda x: core.cos(x), sympy.acos: lambda x: core.acos(x), sympy.cosh: lambda x: core.cosh(x), sympy.acosh: lambda x: core.acosh(x), sympy.tan: lambda x: core.tan(x), sympy.atan: lambda x: core.atan(x), sympy.tanh: lambda x: core.tanh(x), sympy.atanh: lambda x: core.atanh(x), sympy.ceiling: lambda x: core.ceil(x), sympy.floor: lambda x: core.floor(x),
# Model constants cA0 = r1.constant('ca0', value=4.0) v0 = r1.constant('v0', value=240) cpA = r1.constant('cpA', value=30) cpB = r1.constant('cpB', value=60) cpC = r1.constant('cpC', value=20) TA0 = r1.constant('TA0', value=305) hrxnA = r1.constant('hrxnA', value=6500) hrxnB = r1.constant('hrxnB', value=8000) Tc = r1.constant('Tc', value=298) Uc = r1.constant('Uc', value=35000) # Define the expressions k1 = r1.add_expression('k1', 1.25 * exp( (9500 / 1.987) * (1 / 320.0 - 1 / T))) k2 = r1.add_expression('k2', 0.08 * exp( (7000 / 1.987) * (1 / 290.0 - 1 / T))) rA = r1.add_reaction('rA', k1 * A) rB = r1.add_reaction('rB', k2 * B) SM = {'A': [-1, 0], 'B': [0.5, -1], 'C': [0, 3]} RE = r1.reactions_from_stoich(SM, add_odes=False) RE['A'] += cA0 * v0 / V r1.add_odes(RE) # Energy balances T1 = Uc * (Tc - T) # heat transfer T2 = cA0 * v0 * cpA * (T - TA0) # enthalpy from adding A
filename = 'data/init_X.csv' rm.add_data('init_X', category='trajectory', file=filename) filename = 'data/init_Y.csv' rm.add_data('init_Y', category='trajectory', file=filename) # Reactions r0 = rm.add_reaction('r0', k0 * SA * AA, description='Reaction 0') r1 = rm.add_reaction('r1', k1 * ASA * AA, description='Reaction 1') r2 = rm.add_reaction('r2', k2 * ASAA * H2O, description='Reaction 2') r3 = rm.add_reaction('r3', k3 * AA * H2O, description='Reaction 3') #step = r1.step('step', time=210, fixed=False, switch='off') step = 1 / (1 + exp(-Msa / 1e-4)) r4 = rm.add_reaction('r4', kd * (Csa - SA + 1e-6)**1.90 * step, description='Reaction 4') diff = ASA - Csat r5 = rm.add_reaction('r5', 0.3950206559 * kc * (diff + ((diff)**2 + 1e-6)**0.5)**1.34, description='Reaction 5') # Generate the ODEs for the reactions based on the stoichiometric matrix # Since we need to modfiy the ODEs, add_odes should be False odes = rm.reactions_from_stoich(gammas, add_odes=False) # Build expression for the volume