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?
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:
Output Netlist:
This netlist produces the desired output with LTSpice but seems to not work on PySpice for some reason.
Solution
Modified Code Block:
Output Netlist:
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:
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)