Art Scott
03/26/2024, 2:05 PMimport pyspice
from pyspice.spice import Spice
# Circuit elements
# L1 (inductor)
L1_value = 1e-6 # 1 uH
L1_name = 'L1'
# C1 (capacitor)
C1_value = 10e-9 # 10 nF
C1_name = 'C1'
# Primary side (source, resistor)
Vin = 1 # Amplitude of the input voltage source
Vin_freq = 1e3 # Frequency of the input voltage source (1 kHz)
R1_value = 1e3 # Resistance on the primary side
# Transformer parameters
# Ideal transformer model (1:1 turns ratio assumed for simplicity)
# You can adjust the turns ratio (n1:n2) as needed
n1 = 1 # Primary turns
n2 = 1 # Secondary turns
K = n1**2 / n2**2 # Transformer coupling coefficient (unity for 1:1 turns ratio)
# Secondary side (resistor)
R2_value = K * R1_value # Reflected impedance on the secondary side
# Create a new circuit simulation
circuit = Spice(Spice.Circuit())
# Add voltage source
Vin_src = circuit.V('input', 1, 0, dc=0, ac=mag=Vin, freq=Vin_freq)
# Add inductors
circuit.L(L1_name, 1, 0, L1_value)
# Add capacitors
circuit.C(C1_name, 2, 0, C1_value)
# Add resistors
circuit.R('R' + R1_name, 1, 0, R1_value)
circuit.R('R' + R2_name, 2, 0, R2_value)
# Ideal transformer model (using VCVS for simplicity)
# Adjust the polarity dots as necessary based on your transformer's winding directions
circuit.V('T1', 2, 0, circuit.model('VCVS', K=K, C=1e-12), ac=1) # Secondary voltage source controlled by primary
# Analysis
analysis = circuit.tran(start=0, stop=1e-3, step=1e-6) # Transient analysis for 1ms with 1us steps
# Simulate the circuit
circuit.simulate(analysis)
# Plot the results
circuit.plot(Vin_src, C1_name+'(V)') # Plot input voltage and capacitor voltage
Explanation:
1. Import Libraries: Import necessary libraries (pyspice
and Spice
from pyspice.spice
).
2. Circuit Elements: Define component values and names for the inductor (L1), capacitor (C1), primary side resistor (R1), and secondary side resistor (R2).
◦ *Transformer:*Set the turns ratio (n1:n2) in the n1
and n2
variables. Adjust these values as needed for your specific transformer.
◦ Calculate the transformer coupling coefficient K
based on the turns ratio.
3. Circuit Creation: Create a new circuit simulation using Spice.Circuit()
.
4. Voltage Source: Add a voltage source (Vin_src
) with the specified amplitude (Vin
), frequency (Vin_freq
), and DC offset (set to 0 here).
5. Inductors and Capacitors: Add the inductor L1
and capacitor C1
to the circuit.
6. Resistors: Add resistors R1
on the primary side and R2
on the secondary side. The value of R2
is calculated based on the transformer coupling coefficient K
to account for the reflected impedance.
◦ *Ideal Transformer Model:*Use a voltage-controlled voltage source (VCVS) to model the ideal transformer. This is a simplified approach.
◦ Set the gain (K
) of the VCVS to the transformer coupling coefficient.
◦ Connect the primary and secondary sides of the transformer using the VCVS element. Adjust the polarity dots based on your transformer's winding directions.
7. Analysis: Define a transient analysis (tran
) to simulate the circuit's behavior over time. Set the start time (start
), stop time (stop
), and step size (step
).
8. Simulation: Simulate the circuit using `circuit.
Slack Conversation