JKB
JKB

Reputation: 11

PySpice: Simulate circuit with capacitor with defined initial condition

with PySpice I'm trying to simulate the discharging behavior of a capacitor with a defined initial condition in a circuit. This is a simplified example:

import PySpice.Logging.Logging as Logging
logger = Logging.setup_logging()
import matplotlib.pyplot as plt
from PySpice.Spice.Netlist import Circuit
from PySpice.Unit import *

V1 = 10
circuit.V("1","node2", circuit.gnd, V1)
circuit.R("1", "node1", "node2", 3.5)
circuit.C("1", "node1", circuit.gnd, 100, initial_condition = 20)

simulator = circuit.simulator(temperature=25, nominal_temperature=25)

analysis = simulator.transient(step_time=1@u_s, end_time = 10000@u_s)

After running the simulation, I would expect an exponential discharge of the capacitor: The voltage at "node1" should start at "initial_condition" and end at voltage of "node2". But the voltage at "node1" stays at V1 for the complete simulation. What am I doing wrong?

Upvotes: 1

Views: 827

Answers (1)

aras edeş
aras edeş

Reputation: 53

First of all your code is not runnable: You have to define a Circuit object.

There seems to be a discussion relating to this at issue

Reproducing Problem

Modified Code Block:

## imports

V1 = 10
R1 = 3.5
C1 = 100
C1_ic = 20

circuit = Circuit("circuit")

circuit.V("1","node2", circuit.gnd, V1)
circuit.R("1", "node1", "node2", R1)
circuit.C("1", "node1", circuit.gnd, C1, initial_condition=C1_ic)

simulator = circuit.simulator(temperature=25, nominal_temperature=25)
analysis = simulator.transient(step_time=u_ms(1), end_time=u_ms(1000))

print(simulator)

Output Netlist:

.title circuit
V1 node2 0 10
R1 node1 node2 3.5
C1 node1 0 100 ic=20
.options TEMP = 25C
.options TNOM = 25C
.end

This netlist produces the desired output with LTSpice but seems to not work on PySpice for some reason.

Solution

Modified Code Block:

## imports


V1 = 10
R1 = 3.5
C1 = 100
C1_ic = 20

circuit = Circuit("circuit")

circuit.V("1","node2", circuit.gnd, V1)
circuit.R("1", "node1", "node2", R1)
circuit.C("1", "node1", circuit.gnd, C1)

simulator = circuit.simulator(temperature=25, nominal_temperature=25)
## initial condition defined with simulator
simulator.initial_condition(node1=C1_ic)
analysis = simulator.transient(step_time=u_ms(1), end_time=u_ms(1000))

print(simulator)

Output Netlist:

.title circuit
V1 node2 0 10
R1 node1 node2 3.5
C1 node1 0 100
.options TEMP = 25C
.options TNOM = 25C
.ic V(node1)=20
.end

This way of defining initial conditions is valid and produces correct results on both LTSpice and PySpice Simulation.

Electrical Engineering Extra

You can plot the result of analysis in python by adding the following lines:

plt.plot(analysis.time, analysis["node1"], label="node1")
plt.plot(analysis.time, analysis["node2"], label="node2")
plt.legend()
plt.show()

You'll see that "node1" voltage won't deviate from 20 much. This is due to your circuit parameters (resistance and capacitance). Read about Time Constant, Your time constant is too much compared to your simulation time (100C * 3.5R = 35s >> 10s)

Upvotes: 1

Related Questions