def get_z(self, models: Optional[Union[str, List, OrderedDict]] = None): """ Get all discrete status flags in a numpy array. Returns ------- numpy.array """ if self.config.store_z != 1: return None z_dict = list() for mdl in models.values(): if mdl.n == 0 or len(mdl._input_z) == 0: continue z_dict.append(np.concatenate(list((mdl._input_z.values())))) return np.concatenate(z_dict)
def link_external(self, ext_model): """ Update variable addresses provided by external models This method sets attributes including `parent_model`, `parent_instance`, `uid`, `a`, `n`, `e_code` and `v_code`. It initializes the `e` and `v` to zero. Returns ------- None Parameters ---------- ext_model : Model Instance of the parent model """ self.parent = ext_model if isinstance(ext_model, GroupBase): if self.indexer.n > 0 and isinstance(self.indexer.v[0], (list, np.ndarray)): self._n = [len(i) for i in self.indexer.v ] # number of elements in each sublist self._idx = np.concatenate( [np.array(i) for i in self.indexer.v]) else: self._n = [len(self.indexer.v)] self._idx = self.indexer.v self.a = ext_model.get(src=self.src, idx=self._idx, attr='a').astype(int) self.n = len(self.a) self.v = np.zeros(self.n) self.e = np.zeros(self.n) else: original_var = ext_model.__dict__[self.src] if self.indexer is not None: uid = ext_model.idx2uid(self.indexer.v) else: uid = np.arange(ext_model.n, dtype=int) self._n = [len(uid)] if len(uid) > 0: self.a = original_var.a[uid] else: self.a = np.array([], dtype=int) # set initial v and e values to zero self.n = len(self.a) self.v = np.zeros(self.n) self.e = np.zeros(self.n)
def link_external(self, ext_model): """ Update variable addresses provided by external models This method sets attributes including `parent_model`, `parent_instance`, `uid`, `a`, `n`, `e_code` and `v_code`. It initializes the `e` and `v` to zero. Returns ------- None Parameters ---------- ext_model : Model Instance of the parent model Warnings -------- `link_external` does not check if the ExtVar type is the same as the original variable to reduce performance overhead. It will be a silent error (a dimension too small error from `dae.build_pattern`) if a model uses `ExtAlgeb` to access a `State`, or vice versa. """ self.parent = ext_model if isinstance(ext_model, GroupBase): # determine the number of elements based on `indexer.v` if self.indexer.n > 0 and isinstance(self.indexer.v[0], (list, np.ndarray)): self._n = [len(i) for i in self.indexer.v ] # number of elements in each sublist self._idx = np.concatenate( [np.array(i) for i in self.indexer.v]) else: self._n = [len(self.indexer.v)] self._idx = self.indexer.v # use `0` for non-existent addresses (corr. to None in indexer) self.a = ext_model.get( src=self.src, idx=self._idx, attr='a', allow_none=self.allow_none, default=0, ).astype(int) # check if source var type is the same as this ExtVar vcodes = np.array( ext_model.get_field(src=self.src, idx=self._idx, field='v_code')) vcodes = vcodes[vcodes != np.array(None)].astype(str) if not all(vcodes == np.array(self.v_code)): raise TypeError( "ExtVar <%s.%s> is of type <%s>, but source Vars <%s.%s> may not." % (self.owner.class_name, self.name, self.v_code, ext_model.class_name, self.src)) self.n = len(self.a) else: original_var = ext_model.__dict__[self.src] if self.allow_none: raise NotImplementedError( f"{self.name}: allow_none not implemented for Model") if original_var.v_code != self.v_code: raise TypeError("Linking %s of %s to %s of %s is not allowed" % (self.name, self.class_name, original_var.name, original_var.class_name)) if self.indexer is not None: uid = ext_model.idx2uid(self.indexer.v) else: uid = np.arange(ext_model.n, dtype=int) self._n = [len(uid)] if len(uid) > 0: self.a = original_var.a[uid] else: self.a = np.array([], dtype=int) # set initial v and e values to zero self.n = len(self.a) self.v = np.zeros(self.n)