def initialize(self, tau=1.0, scale=1.0): ''' Build the dae system of transient CSTR model :param tau: space time of CSTR model ''' Ptot = cas.sumRows(self._partP_in) # Total Pressure for i in range(self.ngas): self._partP[i] = self._flow[i]/self._Flowtot * Ptot self.build_kinetic() self.build_rate(scale=1) for j in range(self.ngas): self._d_flow[j] = self._partP_in[j] / Ptot * self._Flowtot \ - self._flow[j] for i in range(self.nrxn): self._d_flow[j] += self.stoimat[i][j] * self._rate[i] # Scale the flow rate self._d_flow[j] /= tau for j in range(self.nsurf): self._d_cover[j] = 0 for i in range(self.nrxn): self._d_cover[j] += self.stoimat[i][j + self.ngas] * self._rate[i] self._x = cas.vertcat([self._flow, self._cover]) # ??? self._p = cas.vertcat([self._dEa, self._dBE]) self._u = cas.vertcat([self._partP_in, self._Tem, self._Flowtot]) self._xdot = cas.vertcat([self._d_flow, self._d_cover]) #self._dae_ = dict(x=self._x, p=self._p, ode=self._xdot) self._dae_ = cas.SXFunction('dae', cas.controldaeIn(x=self._x, p=self._p, u=self._u, t=self._t), cas.daeOut(ode=self._xdot))
def get_dependency(expression): sym = symvar(expression) f = MXFunction('f', sym, [expression]) dep = {} for index, sym in enumerate(sym): J = f.jacSparsity(index, 0) dep[sym] = sorted(sumRows(J).find()) return dep
def set_data(self, data): """ Attach experimental measurement data. data : a pd.DataFrame object Data should have columns corresponding to the state labels in self.state_names, with an index corresponding to the measurement times. """ # Should raise an error if no state name is present df = data.loc[:, self.state_names] # Rename columns with state indicies df.columns = np.arange(self.NEQ) # Remove empty (nonmeasured) states self.data = df.loc[:, ~pd.isnull(df).all(0)] obj_list = [] for ((ti, state), xi) in self.data.stack().iteritems(): obj_list += [ (self._get_interp(ti, [state]) - xi) / self.data[state].max() ] obj_resid = cs.sum_square(cs.vertcat(obj_list)) # ts = data.index # Define objective function # obj_resid = cs.sum_square(cs.vertcat( # [(self._get_interp(ti, self._states_indicies) - xi)/ xs.max(0) # for ti, xi in zip(ts, xs)])) alpha = cs.SX.sym('alpha') obj_lasso = alpha * cs.sumRows(cs.fabs(self._P)) self._obj = obj_resid + obj_lasso # Create the solver object self._nlp = cs.SXFunction('nlp', cs.nlpIn(x=self._V, p=alpha), cs.nlpOut(f=self._obj, g=self._g))
def set_data(self, data): """ Attach experimental measurement data. data : a pd.DataFrame object Data should have columns corresponding to the state labels in self.state_names, with an index corresponding to the measurement times. """ # Should raise an error if no state name is present df = data.loc[:, self.state_names] # Rename columns with state indicies df.columns = np.arange(self.NEQ) # Remove empty (nonmeasured) states self.data = df.loc[:, ~pd.isnull(df).all(0)] obj_list = [] for ((ti, state), xi) in self.data.stack().iteritems(): obj_list += [(self._get_interp(ti, [state]) - xi) / self.data[state].max()] obj_resid = cs.sum_square(cs.vertcat(obj_list)) # ts = data.index # Define objective function # obj_resid = cs.sum_square(cs.vertcat( # [(self._get_interp(ti, self._states_indicies) - xi)/ xs.max(0) # for ti, xi in zip(ts, xs)])) alpha = cs.SX.sym("alpha") obj_lasso = alpha * cs.sumRows(cs.fabs(self._P)) self._obj = obj_resid + obj_lasso # Create the solver object self._nlp = cs.SXFunction("nlp", cs.nlpIn(x=self._V, p=alpha), cs.nlpOut(f=self._obj, g=self._g))
def test_sum(self): self.message("sum") D=DMatrix([[1,2,3],[4,5,6],[7,8,9]]) self.checkarray(c.sumRows(D),array([[12,15,18]]),'sum()') self.checkarray(c.sumCols(D),array([[6,15,24]]).T,'sum()')
def test_sum(self): self.message("sum") D = DMatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) self.checkarray(c.sumRows(D), array([[12, 15, 18]]), 'sum()') self.checkarray(c.sumCols(D), array([[6, 15, 24]]).T, 'sum()')