def reset(self): self.m = 0 self.n = 0 self.clear_fg() self.clear_xy() self.clear_z() self.clear_ijv() self.clear_ts() self.Tf = np.ones(self.n)
def check(self): """ Check the bounds and equality conditions. """ if not self.enable: return def _not_all_close(a, b): return np.logical_not(np.isclose(a, b)) if self._v is None: self._v = np.zeros_like(self.u.v) checks = [(self.lower, np.less_equal, "violation of the lower limit", "limit"), (self.upper, np.greater_equal, "violation of the upper limit", "limit"), (self.equal, _not_all_close, 'should be equal', "expected"), (self.not_equal, np.equal, 'should not be equal', "not expected")] for check in checks: limit = check[0] func = check[1] text = check[2] text2 = check[3] if limit is None: continue self.v[:] = np.logical_or(self.v, func(self.u.v, limit.v)) pos = np.argwhere(func(self.u.v, limit.v)).ravel() if len(pos) == 0: continue idx = [self.owner.idx.v[i] for i in pos] lim_v = limit.v * np.ones(self.n) title = f'{self.owner.class_name} {self.info} {text}.' err_dict = OrderedDict([ ('idx', idx), ('values', self.u.v[pos]), (f'{text2}', lim_v[pos]), ]) data = list(map(list, zip(*err_dict.values()))) tab = Tab(title=title, data=data, header=list(err_dict.keys())) if self.error_out: logger.error(tab.draw()) else: logger.warning(tab.draw()) self.v[:] = np.logical_not(self.v)
def store_sparse_pattern(self, models: Optional[Union[str, List, OrderedDict]] = None): models = self._get_models(models) self._call_models_method('store_sparse_pattern', models) # add variable jacobian values for j_name in self.dae.jac_name: ii, jj, vv = list(), list(), list() # for `gy` matrix, always make sure the diagonal is reserved # It is a safeguard if the modeling user omitted the diagonal # term in the equations if j_name == 'gy': ii.extend(np.arange(self.dae.m)) jj.extend(np.arange(self.dae.m)) vv.extend(np.zeros(self.dae.m)) # logger.debug(f'Jac <{j_name}>, row={ii}') for mdl in models.values(): row_idx = mdl.row_of(f'{j_name}') col_idx = mdl.col_of(f'{j_name}') # logger.debug(f'Model <{name}>, row={row_idx}') ii.extend(row_idx) jj.extend(col_idx) vv.extend(np.zeros(len(np.array(row_idx)))) # add the constant jacobian values for row, col, val in mdl.zip_ijv(f'{j_name}c'): ii.extend(row) jj.extend(col) if isinstance(val, (float, int)): vv.extend(val * np.ones(len(row))) elif isinstance(val, (list, np.ndarray)): vv.extend(val) else: raise TypeError( f'Unknown type {type(val)} in constant jacobian {j_name}' ) if len(ii) > 0: ii = np.array(ii).astype(int) jj = np.array(jj).astype(int) vv = np.array(vv).astype(float) self.dae.store_sparse_ijv(j_name, ii, jj, vv) self.dae.build_pattern(j_name)
def resize_array(self): """ Resize arrays to the new `m` and `n` Returns ------- """ self.x = np.append(self.x, np.zeros(self.n - len(self.x))) self.y = np.append(self.y, np.zeros(self.m - len(self.y))) self.z = np.append(self.z, np.zeros(self.o - len(self.z))) self.f = np.append(self.f, np.zeros(self.n - len(self.f))) self.g = np.append(self.g, np.zeros(self.m - len(self.g))) self.Tf = np.append(self.Tf, np.ones(self.n - len(self.Tf)))
def check_var(self, *args, **kwargs): if not self.enable: return super().check_var() if self.n_select is not None and self.n_select > 0: asc = np.argsort(self.u.v - self.lower.v) # ascending order desc = np.argsort(self.upper.v - self.u.v) lowest_n = asc[:self.n_select] highest_n = desc[:self.n_select] reset_in = np.ones(self.u.v.shape) reset_in[lowest_n] = 0 reset_in[highest_n] = 0 reset_out = 1 - reset_in self.zi[:] = np.logical_or(reset_in, self.zi) self.zl[:] = np.logical_and(reset_out, self.zl) self.zu[:] = np.logical_and(reset_out, self.zu)
def list2array(self, n): for flag in self.export_flags: self.__dict__[flag] = self.__dict__[flag] * np.ones(n, dtype=float)