def _ensure_parser(self, symbolic=False, for_filter=False): if self.parser is None or not self.parser.matches(symbolic, for_filter): if self.S is None: raise CircuitException("no netlist defined") if self.V is None and not symbolic: raise CircuitException("no netlist values defined") self.parser = dk_simulator.Parser(self.S, self.V, self.FS, not self.backward_euler, for_filter, symbolic)
def remove_connected(self, net, exclude=None): self._check_netlist() if exclude is None: exclude = {} self.S = list(self.S) comp = {net} found = False last_size = 0 def excluded_row(row): for c in row[1:]: if c in exclude and str(row[0]) in exclude[c]: return True return False while len(comp) > last_size: last_size = len(comp) dl =[] for i, row in enumerate(self.S): for c in row[1:]: if c in comp and not excluded_row(row): comp |= set([j for j in row[1:] if j != models.GND]) dl.append(i) found = True break for i in reversed(dl): del self.S[i] if not found: raise CircuitException("net %s not found" % net) self._clear_calculated() if self.parser is not None: self.parser.update(self.S, self.V)
def load_module(self, filename, clear_all=True): sim_c = dk_simulator.SimulateC(filename) if not clear_all and self.FS != sim_c.fs: raise CircuitException("Samplerate mismatch: %d / %d" % (self.FS, sim_c.FS)) if clear_all: self._clear_all() self.FS = sim_c.fs self.sim_c = sim_c
def remove_element(self, element): self._check_netlist() self.S = list(self.S) for i, row in enumerate(self.S): if str(row[0]) == element: del self.S[i] self._clear_calculated() if self.parser is not None: self.parser.update(self.S, self.V) return raise CircuitException("%s not found int netlist" % element)
def _ensure_filter(self, symbolic): self._ensure_parser(symbolic=symbolic, for_filter=True) if len(self.parser.get_nonlin_funcs()) > 0: if symbolic: raise CircuitException("ciruit is nonlinear: symbolic formula generation not supported") p = dk_simulator.Parser(self.S, self.V, self.FS, not self.backward_euler) sim = dk_simulator.SimulatePy(dk_simulator.EquationSystem(p), self.solver, self.dc_method) J = sim.jacobi() else: J = None self.sim_filter = dk_simulator.LinearFilter(self.parser, J)
def _ensure_sim_py(self): if self.sim_py is None: self._ensure_eq() try: self.sim_py = dk_simulator.SimulatePy(self.eq, self.solver, self.dc_method) if self.sys_reduce_tol >= 0: self.sim_py.balance_realization(self.sys_reduce_tol) if (isinstance(self.eq.nonlin, dk_simulator.PartitionedNonlinEquations) and self.transform_opts.decompose): self.eq.F, self.eq.C = self.eq.nonlin.decompose_blocks(self.eq.F, self.eq.C) except dk_simulator.ConvergenceError as e: raise CircuitException(e)
def join_net(self, net, target_net): self._check_netlist() l = [] found = False for i, row in enumerate(self.S): for j, c in enumerate(row[1:]): if c == net: l.append((i, j, c)) elif c == target_net: found = True if not found: raise CircuitException("target_net %s not found" % target_net) if not l: raise CircuitException("net %s not found" % net) S = list(self.S) for i, j, c in l: S[i] = list(S[i]) S[i][j+1] = target_net self.S = S self._clear_calculated() if self.parser is not None: self.parser.update(self.S, self.V)
def plot(self, sig=None, label=None, clip=-80, nharmonics=8, spectrum=None, freq_range=None): if sig is not None: self.stream(sig) if self.last_output is None: raise CircuitException("nothing to plot") lines = self.last_signal.plot(self.last_output, sig, label, clip, nharmonics, spectrum, freq_range) #if label is None: # label = self._get_sim().out_labels #elif not hasattr(label, "__iter__"): # l = [] # for lbl in self._get_sim().out_labels: # l.append("%s.%s" % (label, lbl)) # label = l Circuit.have_plot = True return lines
def _nodes_from_names(self, elements): d = {} elset = set() for e in self.parser.element_name["N"]: e = e[0] elset.add(e) d[str(e)] = e l = [] for e in elements: if e in d: l.append(d[e]) elif e in elset: l.append(e) else: raise CircuitException("%s unknown. nonlinear circuit elements: %s" % (e, ", ".join(d.keys()))) return l
def _check_netlist(self): if self.S is None: raise CircuitException("no netlist loaded")
def check_keywords(fname, kw, *keywords): for k in kw: if k not in keywords: raise CircuitException("%s: unknown keyword '%s'" % (fname, k))
def _check_input_amplitude(self): if self.input_amplitude is None: raise CircuitException("please define input_amplitude")