def simulate_amplifier(): circuit = Circuit('Amplifier') R_1 = [100] R_2 = [20] R_C = [10] R_E = [2] R_L = [1] C_1 = [10] C_2 = [10] R1 = random.choice(R_1) R2 = random.choice(R_2) RC = random.choice(R_C) RE = random.choice(R_E) RL = random.choice(R_L) C1 = random.choice(C_1) C2 = random.choice(C_2) circuit.R(1, 5, 2, R1@u_kΩ) #kOhm circuit.R(2, 2, 0, R2@u_kΩ) #kOhm circuit.R('C', 5, 4, RC@u_kΩ) #kOhm circuit.R('E', 3, 0, RE@u_kΩ) #kOhm circuit.R('Load', 'out', 0, RL@u_MΩ) #MOhm circuit.C(1, 'in', 2, C1@u_uF) #uF circuit.C(2, 4, 'out', C2@u_uF) #uF circuit.BJT(1, 4, 2, 3, model='bjt') # Q is mapped to BJT ! circuit.model('bjt', 'npn', bf=80, cjc=pico(5), rb=100) V_vector = [0.5] w_vector = [1E3] V = random.choice(V_vector) w = random.choice(w_vector) circuit.V('power', 5, circuit.gnd, 15@u_V) circuit.V('var','in',circuit.gnd, f'DC 0 AC {V} SIN(0 {V}V {w})') simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time = (1/w/25)@u_s,end_time=(4/w)@u_s) figure = plt.figure(1, (20, 10)) axe = plt.subplot(111) plt.title('') plt.xlabel('Time [s]') plt.ylabel('Voltage [V]') plt.grid() plot(analysis['in'], axis=axe) plot(analysis.out, axis=axe) plt.legend(('input', 'output'), loc=(.05,.1)) plt.tight_layout() plt.savefig('gallery/amplifier.png') return
def plot(self, analysis, pathInNode, pathOutNode): figure = plt.figure(1, (10, 5)) axe = plt.subplot(111) plt.title('') plt.xlabel('Time [s]') plt.ylabel('Voltage [V]') plt.grid() plot(analysis[pathInNode], axis=axe) ledgend = ['In'] for i in range(self.__num_inversores - 1): node = "tmp" + str(i+1) plot(analysis[node], axis=axe) ledgend.append(node) ledgend.append(pathOutNode) plot(analysis[pathOutNode], axis=axe) plt.legend(ledgend, loc=(.05,.1)) plt.tight_layout() plt.show()
# circuit.Pulse('pulse', 'gate', 'source', 0@u_V, Vin, duty_cycle, period) circuit.R('gate', 'gate', 'clock', 1 @ u_Ω) circuit.Pulse('pulse', 'clock', circuit.gnd, 0 @ u_V, 2. * Vin, duty_cycle, period) circuit.X('D', '1N5822', circuit.gnd, 'source') circuit.L(1, 'source', 1, L) circuit.R('L', 1, 'out', RL) circuit.C(1, 'out', circuit.gnd, Cout) # , initial_condition=0@u_V circuit.R('load', 'out', circuit.gnd, Rload) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=period / 300, end_time=period * 150) figure = plt.figure(1, (20, 10)) axe = plt.subplot(111) plot(analysis.out, axis=axe) plot(analysis['source'], axis=axe) # plot(analysis['source'] - analysis['out'], axis=axe) # plot(analysis['gate'], axis=axe) plt.axhline(y=float(Vout), color='red') plt.legend(('Vout [V]', 'Vsource [V]'), loc=(.8, .8)) plt.grid() plt.xlabel('t [s]') plt.ylabel('[V]') plt.tight_layout() plt.show() #fig# save_figure(figure, 'buck-converter.png')
circuit.R(1, multiplier, multiplier+1, mega(1)) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=source.period/200, end_time=source.period*20) #################################################################################################### figure = plt.figure(1, (20, 10)) axe = plt.subplot(111) axe.set_title('Voltage Multiplier') axe.set_xlabel('Time [s]') axe.set_ylabel('Voltage [V]') axe.grid() # Fixme: axis vs axe ... plot(analysis['in'], axis=axe) for i in range(1, multiplier+1): y = analysis[str(i)] if i & 1: # for odd multiplier the ground is permuted y -= analysis['in'] plot(y, axis=axe) # axe.axhline(-multiplier*source.amplitude) axe.set_ylim(-multiplier*1.1*source.amplitude, 1.1*source.amplitude) axe.legend(['input'] + ['*' + str(i) for i in range(1, multiplier+1)] , loc=(.2,.8)) plt.tight_layout() plt.show() #fig# save_figure(figure, 'voltage-multiplier.png')
else: print('tau = ', float(circuit['L1'].inductance) / float(circuit['R1'].resistance)) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=micro(1), end_time=source.period*3) if element_type == 'capacitor': axe = plt.subplot(121) title = "Capacitor: voltage is constant" else: axe = plt.subplot(122) title = "Inductor: current is constant" axe.set_title(title) axe.grid() current_scale = 1000 plot(analysis['in']) plot(analysis['out']) # Fixme: resistor current, scale plot((analysis['in'] - analysis.out)/float(circuit['R1'].resistance)*current_scale) axe.set_ylim(-11, 11) axe.set_xlabel('t [s]') axe.set_ylabel('[V]') axe.legend(('Vin [V]', 'Vout [V]', 'I'), loc=(.8,.8)) plt.tight_layout() plt.show() #fig# save_figure(figure, 'capacitor-inductor.png') #################################################################################################### # # End
circuit.C(1, 'comparator', circuit.gnd, 100 @ u_nF) # Reference circuit.R(2, 'output', 'reference', 100 @ u_kΩ) circuit.R(3, 'vcc', 'reference', 100 @ u_kΩ) circuit.R(4, 'reference', circuit.gnd, 100 @ u_kΩ) # Comparator # Fixme: ngspice is buggy with such subcircuit # circuit.subcircuit(basic_comparator) # circuit.X('comparator', 'BasicComparator', 'reference', 'comparator', 'vcc', circuit.gnd, 'output') circuit.NonLinearVoltageSource(1, 'output', circuit.gnd, expression='V(reference, comparator)', table=((-micro(1), 0), (micro(1), source.dc_value))) simulator = circuit.simulator(temperature=25, nominal_temperature=25) simulator.initial_condition( comparator=0) # Fixme: simulator.nodes.comparator == 0 analysis = simulator.transient(step_time=1 @ u_us, end_time=500 @ u_us) # Fixme: Xyce fails with Time step too small figure = plt.figure(1, (20, 10)) plot(analysis.reference) plot(analysis.comparator) plot(analysis.output) plt.tight_layout() plt.show() # f# save_figure('figure', 'astable.png')
circuit.V('supply', 'vdd', 'vss', supply) circuit.V('ground', 'vss', circuit.gnd, circuit.gnd) #################################################################################################### # SIMULATE IMPORTED CIRCUIT print("Simulated circuit:") print(str(circuit)) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=0.001@u_ns, end_time=4@u_ns) #################################################################################################### # PLOT RESULTS OF SIMULATION plt.subplot(2, 1, 1) plot(analysis['A']) plot(analysis['B']) plt.title('NAND2') plt.legend(('A [V]', 'B [V]'), loc=(.8,.8)) plt.ylabel('Vin [V]') plt.subplot(2, 1, 2) plot(analysis['output']) plt.xlabel('t [s]') plt.ylabel('Vout [V]') plt.tight_layout() plt.show()
libraries_path = find_libraries() spice_library = SpiceLibrary(libraries_path) figure1 = plt.figure(1, (20, 10)) circuit = Circuit('half-wave rectification') circuit.include(spice_library['1N4148']) source = circuit.SinusoidalVoltageSource('input', 'in', circuit.gnd, amplitude=10 @ u_V, frequency=50 @ u_Hz) circuit.X('D1', '1N4148', 'in', 'output') circuit.R('load', 'output', circuit.gnd, 100 @ u_Ω) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=source.period / 200, end_time=source.period * 2) axe = plt.subplot(221) plt.title('Half-Wave Rectification') plt.xlabel('Time [s]') plt.ylabel('Voltage [V]') plt.grid() plot(analysis['in'], axis=axe) plot(analysis.output, axis=axe) plt.legend(('input', 'output'), loc=(.05, .1)) plt.ylim(float(-source.amplitude * 1.1), float(source.amplitude * 1.1)) plt.show()
circuit.X('Dz', 'd1n5919brl', circuit.gnd, 2) circuit.X('D', '1N4148', 2, 'out') circuit.C('', circuit.gnd, 'out', 220@u_uF) circuit.R('load', circuit.gnd, 'out', 1@u_kΩ) #?# Fixme: circuit.nodes[2].v, circuit.branch.current # print circuit.nodes # Simulator(circuit, ...).transient(...) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=ac_line.period/200, end_time=ac_line.period*10) figure = plt.figure(1, (20, 10)) axe = plt.subplot(111) plot(analysis['L'] / 100, axis=axe) plot(analysis.out, axis=axe) ###plot((analysis.out - analysis['L']) / 100, axis=axe) ###plot(analysis.out - analysis['2'], axis=axe) ###plot((analysis['2'] - analysis['1']) / 100, axis=axe) # or: # plt.plot(analysis.out.abscissa, analysis.out) plt.legend(('Vin [V]', 'Vout [V]'), loc=(.8,.8)) plt.grid() plt.xlabel('t [s]') plt.ylabel('[V]') plt.tight_layout() plt.show() #f# save_figure('figure', 'capacitive-half-wave-rectification-post-zener.png')
# bottom_inductor = circuit.L('output_bottom', 'output_bottom', circuit.gnd, inner_inductance) # output_inductor = circuit.L('output', 'output', circuit.gnd, outer_inductance) # circuit.CoupledInductor('output_top', output_inductor.name, top_inductor.name, coupling) # circuit.CoupledInductor('output_bottom', output_inductor.name, bottom_inductor.name, coupling) circuit.R('load', 'output', circuit.gnd, kilo(1)) simulator = circuit.simulator(temperature=25, nominal_temperature=25) # simulator.initial_condition(input_top=0, input_bottom=0, output_top=0, output_bottom=0) analysis = simulator.transient(step_time=modulator.period/1000, end_time=modulator.period) figure = plt.figure(1, (20, 10)) plt.title('Ring Modulator') plt.xlabel('Time [s]') plt.ylabel('Voltage [V]') plt.grid() plot(analysis['Vmodulator']) plot(analysis['Vcarrier']) # plot(analysis['output']) plt.legend(('modulator', 'carrier', 'output'), loc=(.05,.1)) #################################################################################################### plt.show() #################################################################################################### # # End # ####################################################################################################
circuit.VoltageControlledSwitch('input', 'sw_node', 'sw_drive', circuit.gnd, 'sw1', model='switch1') circuit.model('switch1', 'SW', Ron=.002@u_Ohm, Roff=1@u_MOhm, Vt=3.0@u_V) print(circuit) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=0.1E-6, end_time=50E-3) NUMBER_PLOTS = '2' #plots of circuit components figure = plt.figure(1, (10, 5)) plot1 = plt.subplot(int(NUMBER_PLOTS+'11')) plot(analysis.sw_drive, color='r') plt.grid() plt.xlabel('t [s]') plt.ylabel('[V]') plt.legend(('Switch Drive',''), loc=(.05,.1)) plot2 = plt.subplot(int(NUMBER_PLOTS+'12')) plot(analysis.sw_node, color='r') plot((analysis.sw_node)/circuit['Rload'].resistance,color='b') plt.grid() plt.xlabel('t [s]') plt.ylabel('[V]') plt.legend(('Switch Output','Load Current'), loc=(.05,.1))
circuit.R(2, 2, 0, 20@u_kΩ) circuit.R('C', 5, 4, 10@u_kΩ) circuit.BJT(1, 4, 2, 3, model='bjt') # Q is mapped to BJT ! circuit.model('bjt', 'npn', bf=80, cjc=pico(5), rb=100) circuit.R('E', 3, 0, 2@u_kΩ) circuit.C(2, 4, 'out', 10@u_uF) circuit.R('Load', 'out', 0, 1@u_MΩ) #################################################################################################### figure = plt.figure(1, (20, 10)) # .ac dec 5 10m 1G simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=source.period/200, end_time=source.period*2) axe = plt.subplot(111) plt.title('') plt.xlabel('Time [s]') plt.ylabel('Voltage [V]') plt.grid() plot(analysis['in'], axis=axe) plot(analysis.out, axis=axe) plt.legend(('input', 'output'), loc=(.05,.1)) plt.tight_layout() plt.show() #f# save_figure('figure', 'ac-coupled-amplifier-plot.png')
sim_end_time = datetime.datetime.now() print() print('Simulation End Time =', sim_end_time) elapsed = sim_end_time - sim_start_time print('Total Simulation Time =', elapsed) print() ##### # Plotting ##### #plots of circuit components figure = plt.figure(1, (10, 5)) axe = plt.subplot(111) plot(analysis.out, axis=axe) # plot(analysis.gate_drive1_net, axis=axe) plot(analysis.source2/circuit['Rload_on'].resistance,axis=axe) plt.axhline(y=float(Vout), color='red') plt.grid() plt.xlabel('t [s]') plt.ylabel('[V]') plt.tight_layout() plt.show() # plots of mcu internal signals ''' figure = plt.figure(2, (10, 5)) axe = plt.subplot(111)
circuit.X('D2', '1N4148', 'output_minus', circuit.gnd) circuit.X('D3', '1N4148', circuit.gnd, 'output_plus') circuit.X('D4', '1N4148', 'output_minus', 'bridge_in_plus') # load and filter cap circuit.X('load', 'CONSTANT_POWER_LOAD', 'output_plus', 'output_minus', P=Pl) circuit.C('1', 'output_plus', 'output_minus', C, initial_condition=10 @ u_V) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=source.period / 2000, end_time=source.period * 20, use_initial_condition=True) axe = plt.subplot(111) plt.title('Dynamo Hub LED Driver\nRegulator Modeled As Constant Power Load') plt.xlabel('Time [s]') plt.ylabel('Voltage [V]') plt.grid() plot(analysis['in'], axis=axe) plot(analysis.output_plus - analysis.output_minus, axis=axe, linewidth=2.0) plt.legend(( 'Internal Hub Voltage Source', 'Voltage Across Regulator Input', ), loc='best') plt.ylim(float(-source.amplitude * 1.1), float(source.amplitude * 1.1)) plt.tight_layout() plt.show()
sim_end_time = datetime.datetime.now() print() print('Simulation End Time =', sim_end_time) elapsed = sim_end_time - sim_start_time print('Total Simulation Time =', elapsed) print() ##### # Plotting ##### #plots of circuit components figure = plt.figure(1, (10, 5)) axe = plt.subplot(111) plot(analysis.sense_net, axis=axe) plot(analysis.esr, axis=axe) # plot(analysis.gate_drive1_net, axis=axe) # plot(analysis.source2/circuit['Rload_on'].resistance,axis=axe) plt.grid() plt.xlabel('t [s]') plt.ylabel('[V]') plt.tight_layout() plt.show() # plots of mcu internal signals ''' figure = plt.figure(2, (10, 5)) axe = plt.subplot(111)
circuit.gnd, amplitude=10 @ u_V, frequency=50 @ u_Hz) circuit.X('D1', '1N4148', 'in', 'output') circuit.R('load', 'output', circuit.gnd, 100 @ u_Ω) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=source.period / 200, end_time=source.period * 2) axe = plt.subplot(221) plt.title('Half-Wave Rectification') plt.xlabel('Time [s]') plt.ylabel('Voltage [V]') plt.grid() plot(analysis['in'], axis=axe) plot(analysis.output, axis=axe) plt.legend(('input', 'output'), loc=(.05, .1)) plt.ylim(float(-source.amplitude * 1.1), float(source.amplitude * 1.1)) #################################################################################################### #cm# half-wave-rectification.m4 circuit.C('1', 'output', circuit.gnd, 1 @ u_mF) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=source.period / 200, end_time=source.period * 2) axe = plt.subplot(222)
tau_measured = popt[0] # Fixme: use Unit().canonise() print('tau {0} = {1}'.format(element_type, tau.canonise().str_space())) print('tau measured {0} = {1:.1f} ms'.format(element_type, tau_measured * 1000)) if element_type == 'capacitor': axe = plt.subplot(121) title = "Capacitor: voltage is constant" else: axe = plt.subplot(122) title = "Inductor: current is constant" axe.set_title(title) axe.grid() current_scale = 1000 plot(analysis['in']) plot(analysis['out']) # Fixme: resistor current, scale plot((analysis['in'] - analysis.out)/float(circuit['R1'].resistance)*current_scale) axe.axvline(x=float(tau), color='red') axe.set_ylim(-11, 11) axe.set_xlabel('t [s]') axe.set_ylabel('[V]') axe.legend(('Vin [V]', 'Vout [V]', 'I'), loc=(.8,.8)) #o# plt.tight_layout() plt.show() #fig# save_figure(figure, 'capacitor-inductor.png')
# circuit.PulseVoltageSource('pulse', 'gate', 'source', 0@u_V, Vin, duty_cycle, period) circuit.R('gate', 'gate', 'clock', 1@u_Ω) circuit.PulseVoltageSource('pulse', 'clock', circuit.gnd, 0@u_V, 2.*Vin, duty_cycle, period) circuit.X('D', '1N5822', circuit.gnd, 'source') circuit.L(1, 'source', 1, L) circuit.R('L', 1, 'out', RL) circuit.C(1, 'out', circuit.gnd, Cout) # , initial_condition=0@u_V circuit.R('load', 'out', circuit.gnd, Rload) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=period/300, end_time=period*150) figure = plt.figure(1, (20, 10)) axe = plt.subplot(111) plot(analysis.out, axis=axe) plot(analysis['source'], axis=axe) # plot(analysis['source'] - analysis['out'], axis=axe) # plot(analysis['gate'], axis=axe) plt.axhline(y=float(Vout), color='red') plt.legend(('Vout [V]', 'Vsource [V]'), loc=(.8,.8)) plt.grid() plt.xlabel('t [s]') plt.ylabel('[V]') plt.tight_layout() plt.show() #f# save_figure('figure', 'buck-converter.png')
circuit = Circuit('half-wave rectification') circuit.include(spice_library['1N4148']) source = circuit.Sinusoidal('input', 'in', circuit.gnd, amplitude=10, frequency=50) circuit.X('D1', '1N4148', 'in', 'output') circuit.R('load', 'output', circuit.gnd, 100) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=source.period/200, end_time=source.period*2) axe = plt.subplot(221) plt.title('Half-Wave Rectification') plt.xlabel('Time [s]') plt.ylabel('Voltage [V]') plt.grid() plot(analysis['in'], axis=axe) plot(analysis.output, axis=axe) plt.legend(('input', 'output'), loc=(.05,.1)) plt.ylim(-source.amplitude*1.1, source.amplitude*1.1) #################################################################################################### #cm# half-wave-rectification.m4 circuit.C('1', 'output', circuit.gnd, milli(1)) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=source.period/200, end_time=source.period*2) axe = plt.subplot(222) plt.title('Half-Wave Rectification with filtering')
circuit.R('relay', 'VccAnalog', 1, 50@u_Ω) circuit.L('relay', 1, 'collector', 100@u_mH) circuit.include(spice_library['1N5822']) # Schottky diode diode = circuit.X('D', '1N5822', 'collector', 'VccAnalog') # Fixme: subcircuit node # diode.minus.add_current_probe(circuit) #################################################################################################### figure = plt.figure(1, (20, 10)) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=period/1000, end_time=period*1.1) axe = plt.subplot(111) plt.title('') plt.xlabel('Time [s]') plt.ylabel('Voltage [V]') plt.grid() plot(analysis.base, axis=axe) plot(analysis.collector, axis=axe) # Fixme: current probe # plot((analysis['1'] - analysis.collector)/circuit.Rrelay.resistance, axis=axe) plot(analysis['1'] - analysis.collector, axis=axe) plt.legend(('Vbase', 'Vcollector'), loc=(.05,.1)) plt.tight_layout() plt.show() #f# save_figure('figure', 'relay.png')
circuit.X('D', '1N4148', 2, 'out') circuit.C('', circuit.gnd, 'out', 220 @ u_uF) circuit.R('load', circuit.gnd, 'out', 1 @ u_kΩ) # # Fixme: circuit.nodes[2].v, circuit.branch.current # print circuit.nodes # Simulator(circuit, ...).transient(...) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=ac_line.period / 200, end_time=ac_line.period * 10) figure = plt.figure(1, (20, 10)) axe = plt.subplot(111) plot(analysis['L'] / 100, axis=axe) plot(analysis.out, axis=axe) ###plot((analysis.out - analysis['L']) / 100, axis=axe) ###plot(analysis.out - analysis['2'], axis=axe) ###plot((analysis['2'] - analysis['1']) / 100, axis=axe) # or: # plt.plot(analysis.out.abscissa, analysis.out) plt.legend(('Vin [V]', 'Vout [V]'), loc=(.8, .8)) plt.grid() plt.xlabel('t [s]') plt.ylabel('[V]') plt.tight_layout() plt.show() #fig# save_figure(figure, 'capacitive-half-wave-rectification-post-zener.png')
from PySpice.Probe.Plot import plot from PySpice.Spice.Netlist import Circuit from PySpice.Unit import * #################################################################################################### #r# We will drive the transmission line with a pulse source and use a standard 50 Ω load. circuit = Circuit('Transmission Line') circuit.PulseVoltageSource('pulse', 'input', circuit.gnd, 0@u_V, 1@u_V, 1@u_ns, 1@u_us) circuit.LosslessTransmissionLine('delay', 'output', circuit.gnd, 'input', circuit.gnd, impedance=50, time_delay=40e-9) circuit.R('load', 'output', circuit.gnd, 50@u_Ω) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=1e-11, end_time=100e-9) #################################################################################################### figure = plt.figure(None, (20, 6)) plot(analysis['input']) plot(analysis['output']) plt.xlabel('Time [s]') plt.ylabel('Voltage (V)') plt.grid() plt.legend(['input', 'output'], loc='upper right') plt.show() #f# save_figure('figure', 'time-delay.png')
rms_voltage=230 @ u_V, frequency=50 @ u_Hz) # circuit.subcircuit(HP54501A(diode_model='1N4148')) # circuit.X('hp54501a', 'HP54501A', 'input', circuit.gnd) circuit.C(1, 'input', circuit.gnd, 1 @ u_uF) circuit.X('D1', diode_model, 'line_plus', 'top') circuit.X('D2', diode_model, 'scope_ground', 'input') circuit.X('D3', diode_model, circuit.gnd, 'top') circuit.X('D4', diode_model, 'scope_ground', circuit.gnd) circuit.R(1, 'top', 'output', 10 @ u_Ω) circuit.C(2, 'output', 'scope_ground', 50 @ u_uF) circuit.R(2, 'output', 'scope_ground', 900 @ u_Ω) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=ac_line.period / 100, end_time=ac_line.period * 3) figure = plt.figure(None, (20, 6)) plot(analysis.input) plot(analysis.Vinput) plot(analysis.output - analysis.scope_ground) plt.legend(('Vin [V]', 'I [A]'), loc=(.8, .8)) plt.grid() plt.xlabel('t [s]') plt.ylabel('[V]') plt.show() #fig# save_figure(figure, 'hp54501a-cem.png')
circuit.X('D1', '1N4148', circuit.gnd, 1) circuit.X('Dz1', 'd1n5919brl', 1, 'out') circuit.C('ac', 1, 2, nano(470)) circuit.R('ac', 2, 'in', 470) # # Fixme: circuit.nodes[2].v, circuit.branch.current # print circuit.nodes # Simulator(circuit, ...).transient(...) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=ac_line.period/200, end_time=ac_line.period*10) figure = plt.figure(1, (20, 10)) axe = plt.subplot(111) plot(analysis['in'] / 100, axis=axe) plot(analysis.out, axis=axe) plot((analysis.out - analysis['in']) / 100, axis=axe) plot(analysis.out - analysis['1'], axis=axe) plot((analysis['1'] - analysis['2']) / 100, axis=axe) # or: # plt.plot(analysis.out.abscissa, analysis.out) plt.legend(('Vin [V]', 'Vout [V]'), loc=(.8,.8)) plt.grid() plt.xlabel('t [s]') plt.ylabel('[V]') plt.tight_layout() plt.show() #fig# save_figure(figure, 'transform-less-power-supply.png')
from PySpice.Probe.Plot import plot from PySpice.Spice.Netlist import Circuit from PySpice.Unit import * #################################################################################################### #r# We will drive the transmission line with a pulse source and use a standard 50 Ω load. circuit = Circuit('Transmission Line') circuit.PulseVoltageSource('pulse', 'input', circuit.gnd, 0 @ u_V, 1 @ u_V, 1 @ u_ns, 1 @ u_us) circuit.LosslessTransmissionLine('delay', 'output', circuit.gnd, 'input', circuit.gnd, impedance=50, time_delay=40e-9) circuit.R('load', 'output', circuit.gnd, 50 @ u_Ω) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=1e-11, end_time=100e-9) #################################################################################################### figure = plt.figure(None, (20, 6)) plot(analysis['input']) plot(analysis['output']) plt.xlabel('Time [s]') plt.ylabel('Voltage (V)') plt.grid() plt.legend(['input', 'output'], loc='upper right') plt.show() #f# save_figure('figure', 'time-delay.png')
circuit.X('Dz1', 'd1n5919brl', 1, 'out') circuit.C('ac', 1, 2, nano(470)) circuit.R('ac', 2, 'in', 470) # # Fixme: circuit.nodes[2].v, circuit.branch.current # print circuit.nodes # Simulator(circuit, ...).transient(...) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=ac_line.period / 200, end_time=ac_line.period * 10) figure = plt.figure(1, (20, 10)) axe = plt.subplot(111) plot(analysis['in'] / 100, axis=axe) plot(analysis.out, axis=axe) plot((analysis.out - analysis['in']) / 100, axis=axe) plot(analysis.out - analysis['1'], axis=axe) plot((analysis['1'] - analysis['2']) / 100, axis=axe) # or: # plt.plot(analysis.out.abscissa, analysis.out) plt.legend(('Vin [V]', 'Vout [V]'), loc=(.8, .8)) plt.grid() plt.xlabel('t [s]') plt.ylabel('[V]') plt.tight_layout() plt.show() #fig# save_figure(figure, 'transform-less-power-supply.png')
#!# We include the operational amplifier module. circuit.include(spice_library['LMV981']) #!# We define the subcircuits. for subcircuit in (PowerIn(), Opamp(), JackIn(), JackOut()): circuit.subcircuit(subcircuit) # print(str(circuit)) #!# We perform a transient simulation. simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=milli(.1), end_time=milli(3)) figure = plt.figure(1, (20, 10)) plot(analysis['2']) # JackIn input plot(analysis['7']) # Opamp output plt.legend(('Vin [V]', 'Vout [V]'), loc=(.8,.8)) plt.grid() plt.xlabel('t [s]') plt.ylabel('[V]') plt.tight_layout() plt.show() #fig# save_figure(figure, 'kicad-example.png') #################################################################################################### # # End # ####################################################################################################
circuit.X('D2', '1N4148', 3, 2) circuit.X('Dz', 'd1n5919brl', circuit.gnd, 'out') circuit.C('', circuit.gnd, 'out', 250@u_uF) circuit.R('load', circuit.gnd, 'out', 1@u_kΩ) #?# Fixme: circuit.nodes[2].v, circuit.branch.current # print circuit.nodes # Simulator(circuit, ...).transient(...) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=ac_line.period/200, end_time=ac_line.period*50) figure = plt.figure(1, (20, 10)) axe = plt.subplot(111) plot(analysis.L / 100, axis=axe) plot(analysis.out, axis=axe) plot(analysis['2'], axis=axe) plot(analysis['3'], axis=axe) plot((analysis.out - analysis['3']), axis=axe) # plot((analysis['2'] - analysis['3']), axis=axe) #plot((analysis.L - analysis['1']) / 100, axis=axe) ###plot((analysis.out - analysis['L']) / 100, axis=axe) ###plot(analysis.out - analysis['2'], axis=axe) ###plot((analysis['2'] - analysis['1']) / 100, axis=axe) # or: # plt.plot(analysis.out.abscissa, analysis.out) plt.legend(('Vin [V]', 'Vout [V]', 'V2 [V]', 'V3 [V]', 'VC2 [V]'), loc=(.8,.8))
simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=source.period / 200, end_time=source.period * 20) #################################################################################################### figure = plt.figure(1, (20, 10)) axe = plt.subplot(111) axe.set_title('Voltage Multiplier') axe.set_xlabel('Time [s]') axe.set_ylabel('Voltage [V]') axe.grid() # Fixme: axis vs axe ... plot(analysis['in'], axis=axe) for i in range(1, multiplier + 1): y = analysis[str(i)] if i & 1: # for odd multiplier the ground is permuted y -= analysis['in'] plot(y, axis=axe) # axe.axhline(-multiplier*source.amplitude) axe.set_ylim(float(-multiplier * 1.1 * source.amplitude), float(1.1 * source.amplitude)) axe.legend(['input'] + ['*' + str(i) for i in range(1, multiplier + 1)], loc=(.2, .8)) plt.tight_layout() plt.show() #f# save_figure('figure', 'voltage-multiplier.png')
from Transformer import Transformer #################################################################################################### circuit = Circuit('Transformer') ac_line = circuit.AcLine('input', 'input', circuit.gnd, rms_voltage=230, frequency=50) circuit.subcircuit(Transformer(turn_ratio=10)) circuit.X('transformer', 'Transformer', 'input', circuit.gnd, 'output', circuit.gnd) circuit.R('load', 'output', circuit.gnd, kilo(1)) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=ac_line.period/200, end_time=ac_line.period*3) figure = plt.figure(1, (20, 10)) plot(analysis.input) plot(analysis.output) plt.legend(('Vin [V]', 'Vout [V]'), loc=(.8,.8)) plt.grid() plt.xlabel('t [s]') plt.ylabel('[V]') plt.tight_layout() plt.show() #fig# save_figure(figure, 'transformer.png') #################################################################################################### # # End # ####################################################################################################
'kicad-pyspice-example.cir') parser = SpiceParser(path=kicad_netlist_path) #!# We build the circuit and translate the ground (5 to 0). circuit = parser.build_circuit(ground=5) #!# We include the operational amplifier module. circuit.include(spice_library['LMV981']) #!# We define the subcircuits. for subcircuit in (PowerIn(), Opamp(), JackIn(), JackOut()): circuit.subcircuit(subcircuit) # print(str(circuit)) #!# We perform a transient simulation. simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=100 @ u_us, end_time=3 @ u_ms) figure = plt.figure(1, (20, 10)) plot(analysis['2']) # JackIn input plot(analysis['7']) # Opamp output plt.legend(('Vin [V]', 'Vout [V]'), loc=(.8, .8)) plt.grid() plt.xlabel('t [s]') plt.ylabel('[V]') plt.tight_layout() plt.show() #fig# save_figure(figure, 'kicad-example.png')
circuit.X('Dz', 'd1n5919brl', circuit.gnd, 'out') circuit.C('', circuit.gnd, 'out', micro(250)) circuit.R('load', circuit.gnd, 'out', kilo(1)) # # Fixme: circuit.nodes[2].v, circuit.branch.current # print circuit.nodes # Simulator(circuit, ...).transient(...) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=ac_line.period / 200, end_time=ac_line.period * 50) figure = plt.figure(1, (20, 10)) axe = plt.subplot(111) plot(analysis.L / 100, axis=axe) plot(analysis.out, axis=axe) plot(analysis['2'], axis=axe) plot(analysis['3'], axis=axe) plot((analysis.out - analysis['3']), axis=axe) # plot((analysis['2'] - analysis['3']), axis=axe) #plot((analysis.L - analysis['1']) / 100, axis=axe) ###plot((analysis.out - analysis['L']) / 100, axis=axe) ###plot(analysis.out - analysis['2'], axis=axe) ###plot((analysis['2'] - analysis['1']) / 100, axis=axe) # or: # plt.plot(analysis.out.abscissa, analysis.out) plt.legend(('Vin [V]', 'Vout [V]', 'V2 [V]', 'V3 [V]', 'VC2 [V]'),
# Simulation parameters ##### period = 2.5 @ u_us print() simulator = circuit.simulator(temperature=25, nominal_temperature=25) simulator.initial_condition(output_v=0) analysis = simulator.transient(step_time=period / 300, end_time=period * 300) ##### # Plot ##### figure = plt.figure(1, (10, 5)) axe = plt.subplot(111) plot(analysis.sw_node, axis=axe) # plot(analysis.output_v, axis=axe) # plot current # plot(analysis.output_v/5, axis=axe) plt.legend(('Vout [V]', 'Iout [A]'), loc=(.8, .8)) plt.grid() plt.xlabel('t [s]') plt.ylabel('[V]') plt.tight_layout() plt.show() #f# save_figure('figure', 'buck-converter.png')
circuit.R(2, 'output', 'reference', kilo(100)) circuit.R(3, 'vcc', 'reference', kilo(100)) circuit.R(4, 'reference', circuit.gnd, kilo(100)) # Comparator # Fixme: ngspice is buggy with such subcircuit # circuit.subcircuit(basic_comparator) # circuit.X('comparator', 'BasicComparator', 'reference', 'comparator', 'vcc', circuit.gnd, 'output') circuit.NonLinearVoltageSource(1, 'output', circuit.gnd, expression='V(reference, comparator)', table=((-micro(1), 0), (micro(1), source.dc_value)) ) simulator = circuit.simulator(temperature=25, nominal_temperature=25) simulator.initial_condition(comparator=0) # Fixme: simulator.nodes.comparator == 0 analysis = simulator.transient(step_time=micro(1), end_time=micro(500)) figure = plt.figure(1, (20, 10)) plot(analysis.reference) plot(analysis.comparator) plot(analysis.output) plt.tight_layout() plt.show() #fig# save_figure(figure, 'astable.png') #################################################################################################### # # End # ####################################################################################################
circuit.R(1, 'input', 'output', 10 @ u_kΩ) circuit.R(2, 'output', circuit.gnd, 1 @ u_kΩ) amplitude = 10 @ u_V frequency = 50 @ u_Hz ngspice_shared = MyNgSpiceShared(amplitude=amplitude, frequency=frequency, send_data=False) simulator = circuit.simulator(temperature=25, nominal_temperature=25, simulator='shared', ngspice_shared=ngspice_shared) period = float(frequency.period) analysis = simulator.transient(step_time=period / 200, end_time=period * 2) #################################################################################################### figure1 = plt.figure(1, (20, 10)) plt.title('Voltage Divider') plt.xlabel('Time [s]') plt.ylabel('Voltage [V]') plt.grid() plot(analysis.input) plot(analysis.output) plt.legend(('input', 'output'), loc=(.05, .1)) plt.ylim(float(-amplitude * 1.1), float(amplitude * 1.1)) plt.tight_layout() plt.show() #fig# save_figure(figure1, 'voltage-divider.png')
circuit.R('relay', 'VccAnalog', 1, 50@u_Ω) circuit.L('relay', 1, 'collector', 100@u_mH) circuit.include(spice_library['1N5822']) # Schottky diode diode = circuit.X('D', '1N5822', 'collector', 'VccAnalog') # Fixme: subcircuit node # diode.minus.add_current_probe(circuit) #################################################################################################### figure = plt.figure(1, (20, 10)) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=period/1000, end_time=period*1.1) axe = plt.subplot(111) plt.title('') plt.xlabel('Time [s]') plt.ylabel('Voltage [V]') plt.grid() plot(analysis.base, axis=axe) plot(analysis.collector, axis=axe) # Fixme: current probe # plot((analysis['1'] - analysis.collector)/circuit.Rrelay.resistance, axis=axe) plot(analysis['1'] - analysis.collector, axis=axe) plt.legend(('Vbase', 'Vcollector'), loc=(.05,.1)) plt.tight_layout() plt.show() #fig# save_figure(figure, 'relay.png')
circuit.include(spice_library['1N4148']) diode_model = '1N4148' ac_line = circuit.AcLine('input', 'input', circuit.gnd, rms_voltage=230@u_V, frequency=50@u_Hz) # circuit.subcircuit(HP54501A(diode_model='1N4148')) # circuit.X('hp54501a', 'HP54501A', 'input', circuit.gnd) circuit.C(1, 'input', circuit.gnd, 1@u_uF) circuit.X('D1', diode_model, 'line_plus', 'top') circuit.X('D2', diode_model, 'scope_ground', 'input') circuit.X('D3', diode_model, circuit.gnd, 'top') circuit.X('D4', diode_model, 'scope_ground', circuit.gnd) circuit.R(1, 'top', 'output', 10@u_Ω) circuit.C(2, 'output', 'scope_ground', 50@u_uF) circuit.R(2, 'output', 'scope_ground', 900@u_Ω) simulator = circuit.simulator(temperature=25, nominal_temperature=25) analysis = simulator.transient(step_time=ac_line.period/100, end_time=ac_line.period*3) figure = plt.figure(None, (20, 6)) plot(analysis.input) plot(analysis.Vinput) plot(analysis.output - analysis.scope_ground) plt.legend(('Vin [V]', 'I [A]'), loc=(.8,.8)) plt.grid() plt.xlabel('t [s]') plt.ylabel('[V]') plt.show() #f# save_figure('figure', 'hp54501a-cem.png')
# output_inductor = circuit.L('output', 'output', circuit.gnd, outer_inductance) # circuit.CoupledInductor('output_top', output_inductor.name, top_inductor.name, coupling) # circuit.CoupledInductor('output_bottom', output_inductor.name, bottom_inductor.name, coupling) circuit.R('load', 'output', circuit.gnd, kilo(1)) simulator = circuit.simulator(temperature=25, nominal_temperature=25) # simulator.initial_condition(input_top=0, input_bottom=0, output_top=0, output_bottom=0) analysis = simulator.transient(step_time=modulator.period / 1000, end_time=modulator.period) figure = plt.figure(1, (20, 10)) plt.title('Ring Modulator') plt.xlabel('Time [s]') plt.ylabel('Voltage [V]') plt.grid() plot(analysis['Vmodulator']) plot(analysis['Vcarrier']) # plot(analysis['output']) plt.legend(('modulator', 'carrier', 'output'), loc=(.05, .1)) #################################################################################################### plt.show() #################################################################################################### # # End # ####################################################################################################