def main(args): if len(args) < 2: print('Usage: python ' + args[0] + ' input.txt [output.txt]') sys.exit() parser = Parser() comps_linear = [] comps_diodes = [] with open(args[1]) as file: for line in file: comp = parser.next_entry(line) if comp is not None: if isinstance(comp, spicemix.Diode): comps_diodes.append(comp) else: comps_linear.append(comp) n, m = find_n_m_size(comps_linear + comps_diodes) builder = StampBuilder(n, m) for comp in comps_linear: builder.add_component(comp) prev = start_appr for d in comps_diodes: d.set_vol_zero(start_appr) if len(comps_diodes) > 0: # the_p = comps_diodes[len(comps_diodes) - 1].get_p_node() min = start_appr while True: prepared_builder = deepcopy(builder) for d in comps_diodes: prepared_builder.add_component(d) prepared_builder.clear_zer() a, z = prepared_builder.get_a_z() solution = gaussian_elimintaion(a, z) for d in comps_diodes: kek = d.get_p_node() - 1 if solution[kek] < min: min = solution[kek] for d in comps_diodes: d.set_vol_zero(min) dx = fabs(comps_diodes[0].get_value() - prev) prev = comps_diodes[0].get_value() if dx < eps: break # why python hasnt do while ? else: builder.clear_zer() a, z = builder.get_a_z() solution = gaussian_elimintaion(a, z) if len(args) > 2: form_output(args[2], solution, n, m) else: print_matrix(solution)
class ParserStampTests(unittest.TestCase): def setUp(self): self.entries = {'* Whole line comment': None, 'R1 1 2 1000 * Это комментарий': (spicemix.Resistor, 1, 1, 2, 1000), 'R2 2 0 1000': (spicemix.Resistor, 2, 2, 0, 1000), 'V1 1 0 2': (spicemix.Voltage, 1, 1, 0, 2), 'D1 2 0': (spicemix.Diode, 1, 2, 0, None), } self.n = 3 self.m = 1 self.v0 = 0.8 i = 1e-12 * (exp(self.v0 / 25.85e-3) - 1) self.cut_matrix_a = [[1e-3, -1e-3, 1], [-1e-3, 1e-3 + 1e-3 + i / 25.85e-3, 0], [1, 0, 0], ] self.cut_matrix_z = [0, -(i - i / 25.85e-3 * self.v0), 2] self.parser = parser.Parser() self.builder = StampBuilder(self.n, self.m) def test_correct_type_nodes_anything(self): comps_linear = [] comps_diodes = [] for entry, value in self.entries.items(): comp = self.parser.next_entry(entry) if isinstance(comp, spicemix.Diode): comp.set_voltage(0.8) if comp is None and value is not None: raise Exception('There is a component but parser returns None') if comp is not None: self.assertEqual(comp.get_id(), value[1], 'Wrong id') self.assertEqual(comp.get_p_node(), value[2], 'Wrong p node') self.assertEqual(comp.get_n_node(), value[3], 'Wrong n node') self.assertEqual(comp.get_value(), value[4], 'Wrong value') assert isinstance(comp, value[0]) if isinstance(comp, spicemix.Diode): comps_diodes.append(comp) else: comps_linear.append(comp) for c in comps_linear: self.builder.add_component(c) for c in comps_diodes: c.set_vol_zero(self.v0) self.builder.add_component(c) n, m = find_n_m_size(comps_linear + comps_diodes) self.assertEqual(n, self.n) self.assertEqual(m, self.m) self.builder.clear_zer() builder_a, builder_z = self.builder.get_a_z() self.assertEqual(self.cut_matrix_a, builder_a) self.assertEqual(self.cut_matrix_z, builder_z) gaussian.print_matrix(gaussian.gaussian_elimintaion(self.cut_matrix_a, self.cut_matrix_z))
class ParserStampTests(unittest.TestCase): def setUp(self): self.entries = { '* Whole line comment': None, 'R1 1 2 1000 * Это комментарий': (spicemix.Resistor, 1, 1, 2, 1000), 'R2 2 0 1000': (spicemix.Resistor, 2, 2, 0, 1000), 'V1 1 0 2': (spicemix.Voltage, 1, 1, 0, 2), 'D1 2 0': (spicemix.Diode, 1, 2, 0, None), } self.n = 3 self.m = 1 self.v0 = 0.8 i = 1e-12 * (exp(self.v0 / 25.85e-3) - 1) self.cut_matrix_a = [ [1e-3, -1e-3, 1], [-1e-3, 1e-3 + 1e-3 + i / 25.85e-3, 0], [1, 0, 0], ] self.cut_matrix_z = [0, -(i - i / 25.85e-3 * self.v0), 2] self.parser = parser.Parser() self.builder = StampBuilder(self.n, self.m) def test_correct_type_nodes_anything(self): comps_linear = [] comps_diodes = [] for entry, value in self.entries.items(): comp = self.parser.next_entry(entry) if isinstance(comp, spicemix.Diode): comp.set_voltage(0.8) if comp is None and value is not None: raise Exception('There is a component but parser returns None') if comp is not None: self.assertEqual(comp.get_id(), value[1], 'Wrong id') self.assertEqual(comp.get_p_node(), value[2], 'Wrong p node') self.assertEqual(comp.get_n_node(), value[3], 'Wrong n node') self.assertEqual(comp.get_value(), value[4], 'Wrong value') assert isinstance(comp, value[0]) if isinstance(comp, spicemix.Diode): comps_diodes.append(comp) else: comps_linear.append(comp) for c in comps_linear: self.builder.add_component(c) for c in comps_diodes: c.set_vol_zero(self.v0) self.builder.add_component(c) n, m = find_n_m_size(comps_linear + comps_diodes) self.assertEqual(n, self.n) self.assertEqual(m, self.m) self.builder.clear_zer() builder_a, builder_z = self.builder.get_a_z() self.assertEqual(self.cut_matrix_a, builder_a) self.assertEqual(self.cut_matrix_z, builder_z) gaussian.print_matrix( gaussian.gaussian_elimintaion(self.cut_matrix_a, self.cut_matrix_z))