예제 #1
0
    dc_offset=5)

skidl_SINI['p', 'n'] += net_1, net_2
skidl_circ = generate_netlist()
print(skidl_circ)

# In[52]:

pyspice_circ = Circuit('')
pyspice_circ.SinusoidalCurrentSource(
    '1',
    'N1',
    'N2',
    #transit sim statments
    offset=5,
    amplitude=5,
    frequency=5,
    delay=5,
    damping_factor=5,
    #ac sim statments
    ac_magnitude=5,
    dc_offset=5)
print(pyspice_circ)

# In[53]:

netlist_comp_check(skidl_circ, pyspice_circ)

# ## AcLine(SinusoidalVoltageSource)
# PySpice/PySpice/Spice/HighLevelElement.py; class AcLine(SinusoidalVoltageSource)
#
    def spice_circuit(self, amplitude=1, frequency=0, initial_condition=0):
        self.amplitude = amplitude
        self.frequency = frequency

        circuit = Circuit('Battery Equivalent model')

        circuit.V('OCV', 1, circuit.gnd,
                  self.Voc @ u_V)  #name,positive node,negative node,value
        circuit.R(
            'Rs', 1, 'cap_input_voltage', self.Rs
            @ u_Ω)  #node 2='source_input_node' and node 3='source_output_node'
        circuit.R('Rp', 'cap_input_voltage', 'cap_output_voltage',
                  self.Rp @ u_Ω)
        circuit.C('Cp',
                  'cap_input_voltage',
                  'cap_output_voltage',
                  self.Cp @ u_F,
                  ic=0 @ u_V)  #ic=initial condition
        circuit.R('Rl', 'cap_output_voltage', 'source_input_node',
                  self.Rl @ u_Ω)  #Rl=0
        #print(circuit)

        if self.frequency == 0:
            circuit.I('current_source', 'source_input_node', circuit.gnd,
                      self.amplitude @ u_A)
        else:
            circuit.SinusoidalCurrentSource(
                'current_source',
                'source_input_node',
                circuit.gnd,
                0 @ u_A,
                offset=0 @ u_A,
                amplitude=self.amplitude @ u_A,
                frequency=self.frequency @ u_Hz)  #freq=0.4

        #ac_line = circuit.AcLine('current_source', 'source_input_node','source_output_node' ,  frequenc=0.4u_Hz)

        #print(circuit)
        print(initial_condition)
        simulator = circuit.simulator(temperature=25, nominal_temperature=25)
        simulator.initial_condition(cap_input_voltage=initial_condition @ u_V)
        analysis = simulator.transient(
            step_time=0.01 @ u_s,
            end_time=1 @ u_s)  #@u_s is sec and @u_us is micro sec

        output_node = []
        input_node = []
        for node in (analysis['cap_output_voltage']):  #
            #print('Node {}: {} V'.format(str(node), float(node)))
            output_node.append(float(node))
    # print(output_node)

        for node in (analysis['cap_input_voltage']):  #
            #print('Node {}: {} V'.format(str(node), float(node)))
            input_node.append(float(node))
# =============================================================================
#     print("output_node - input_node : ",input_node[100]-output_node[100])
#     print("output_node  ",output_node[100])
#     print("input ",input_node[100])
#     print("output_node shape ",len(output_node))
# =============================================================================

# =============================================================================
#     figure1, ax = plt.subplots(figsize=(20, 10))
#     ax.set_title('Battery Equivalent Model')
#     ax.set_xlabel('Time [s]')
#     ax.set_ylabel('Voltage [V]')
#     ax.grid()
#     ax.plot(analysis.cap_input_voltage-analysis.cap_output_voltage)
#     #ax.plot(analysis.cap_input_voltage)
# =============================================================================
        Vb = output_node[100]
        Vb = np.random.normal(Vb, scale=math.sqrt(self.observation_noise))
        return (
            Vb, input_node[100] - output_node[100]
        )  #at every 1 we are returning the output value 100*step_time=1sec,in array it will be 99th value100*step_time=0.1 sec,in array it will be 99th value
예제 #3
0
    def define_circuit(self):
        logger = Logging.setup_logging()
        circuit_lab = self.circuit
        circuit = Circuit(circuit_lab["name"])

        # for complex circuit elements that requires SPICE library
        # libraries_path = find_libraries()

        python_file = os.path.abspath(sys.argv[0])
        examples_root = parent_directory_of(python_file)
        libraries_path = os.path.join(examples_root, 'libraries')

        spice_library = SpiceLibrary(libraries_path)

        # return message
        message = ""

        # add all elements to the PySpice circuit
        for element in circuit_lab:
            if element == "V":
                for dc_voltage_source in circuit_lab["V"]:
                    circuit.V(
                        dc_voltage_source["id"],
                        circuit.gnd if dc_voltage_source["node1"] == "gnd" else
                        dc_voltage_source["node1"],
                        circuit.gnd if dc_voltage_source["node2"] == "gnd" else
                        dc_voltage_source["node2"],
                        dc_voltage_source["value"] @ u_V)

            elif element == "VA":
                for ac_voltage_source in circuit_lab["VA"]:
                    circuit.SinusoidalVoltageSource(
                        ac_voltage_source["id"],
                        circuit.gnd if ac_voltage_source["node1"] == "gnd" else
                        ac_voltage_source["node1"],
                        circuit.gnd if ac_voltage_source["node2"] == "gnd" else
                        ac_voltage_source["node2"],
                        amplitude=ac_voltage_source["amplitude"] @ u_V,
                        frequency=ac_voltage_source["frequency"] @ u_Hz,
                        offset=ac_voltage_source["offset"] @ u_V)

            elif element == "I":
                for dc_current_source in circuit_lab["I"]:
                    circuit.I(
                        dc_current_source["id"],
                        circuit.gnd if dc_current_source["node1"] == "gnd" else
                        dc_current_source["node1"],
                        circuit.gnd if dc_current_source["node2"] == "gnd" else
                        dc_current_source["node2"],
                        dc_current_source["value"] @ u_A)

            elif element == "IA":
                for ac_current_source in circuit_lab["IA"]:
                    circuit.SinusoidalCurrentSource(
                        ac_current_source["id"],
                        circuit.gnd if ac_current_source["node1"] == "gnd" else
                        ac_current_source["node1"],
                        circuit.gnd if ac_current_source["node2"] == "gnd" else
                        ac_current_source["node2"],
                        amplitude=ac_current_source["amplitude"] @ u_A,
                        frequency=ac_current_source["frequency"] @ u_Hz,
                        offset=ac_current_source["offset"] @ u_A)

            elif element == "R":
                for resistor in circuit_lab["R"]:
                    circuit.R(
                        resistor["id"], circuit.gnd if resistor["node1"]
                        == "gnd" else resistor["node1"], circuit.gnd
                        if resistor["node2"] == "gnd" else resistor["node2"],
                        resistor["value"] @ u_Ω)

            elif element == "L":
                for inductor in circuit_lab["L"]:
                    circuit.L(
                        inductor["id"], circuit.gnd if inductor["node1"]
                        == "gnd" else inductor["node1"], circuit.gnd
                        if inductor["node2"] == "gnd" else inductor["node2"],
                        inductor["value"] @ u_H)

            elif element == "C":
                for capacitor in circuit_lab["C"]:
                    circuit.C(
                        capacitor["id"], circuit.gnd if capacitor["node1"]
                        == "gnd" else capacitor["node1"], circuit.gnd
                        if capacitor["node2"] == "gnd" else capacitor["node2"],
                        capacitor["value"] @ u_F)

            elif element == "D":
                for diode in circuit_lab["D"]:
                    try:
                        circuit.include(spice_library[diode["modelType"]])
                        circuit.X(
                            diode["id"], diode["modelType"], circuit.gnd
                            if diode["node1"] == "gnd" else diode["node1"],
                            circuit.gnd
                            if diode["node2"] == "gnd" else diode["node2"])
                    except KeyError as e:
                        message += " " + str(e)

            elif element == "nBJT":
                for nBJT in circuit_lab["nBJT"]:
                    try:
                        circuit.include(spice_library[nBJT["modelType"]])
                        circuit.BJT(nBJT["id"],
                                    circuit.gnd if nBJT["node1"] == "gnd" else
                                    nBJT["node1"],
                                    circuit.gnd if nBJT["node2"] == "gnd" else
                                    nBJT["node2"],
                                    circuit.gnd if nBJT["node3"] == "gnd" else
                                    nBJT["node3"],
                                    model=nBJT["modelType"])
                    except KeyError as e:
                        message += " " + str(e)

            elif element == "pBJT":
                for pBJT in circuit_lab["pBJT"]:
                    try:
                        circuit.include(spice_library[pBJT["modelType"]])
                        circuit.BJT(pBJT["id"],
                                    circuit.gnd if pBJT["node3"] == "gnd" else
                                    pBJT["node3"],
                                    circuit.gnd if pBJT["node2"] == "gnd" else
                                    pBJT["node2"],
                                    circuit.gnd if pBJT["node1"] == "gnd" else
                                    pBJT["node1"],
                                    model=pBJT["modelType"])
                    except KeyError as e:
                        message += " " + str(e)

            elif element == "NMOS":
                for NMOS in circuit_lab["NMOS"]:
                    try:
                        circuit.include(spice_library[NMOS["modelType"]])
                        # nodes are: drain, gate, source, bulk
                        circuit.MOSFET(NMOS["id"],
                                       circuit.gnd if NMOS["node4"] == "gnd"
                                       else NMOS["node4"],
                                       circuit.gnd if NMOS["node2"] == "gnd"
                                       else NMOS["node2"],
                                       circuit.gnd if NMOS["node3"] == "gnd"
                                       else NMOS["node3"],
                                       circuit.gnd if NMOS["node1"] == "gnd"
                                       else NMOS["node1"],
                                       model=NMOS["modelType"])
                    except KeyError as e:
                        message += " " + str(e)

            elif element == "PMOS":
                for PMOS in circuit_lab["PMOS"]:
                    try:
                        circuit.include(spice_library[PMOS["modelType"]])
                        # nodes are: source, gate, drain, bulk
                        circuit.MOSFET(PMOS["id"],
                                       circuit.gnd if PMOS["node1"] == "gnd"
                                       else PMOS["node1"],
                                       circuit.gnd if PMOS["node2"] == "gnd"
                                       else PMOS["node2"],
                                       circuit.gnd if PMOS["node3"] == "gnd"
                                       else PMOS["node3"],
                                       circuit.gnd if PMOS["node4"] == "gnd"
                                       else PMOS["node4"],
                                       model=PMOS["modelType"])
                    except KeyError as e:
                        message += " " + str(e)

            # add ammeter as a 0 volt voltage source
            elif element == "AM":
                for ammeter in circuit_lab["AM"]:
                    circuit.V(
                        ammeter["id"], circuit.gnd if ammeter["node1"] == "gnd"
                        else ammeter["node1"], circuit.gnd if ammeter["node2"]
                        == "gnd" else ammeter["node2"], ammeter["value"] @ u_V)

        if not message:
            self.spice = circuit
            return message
        return "Undefined model type:" + message