I am experimenting with C blocks. I tried to model various components with software, for example the ideal operational amplifier and I noticed a strange behavior. The circuit implementation and the software implementation realize the same function but the simulation shows totally different results. The circuit works correctly but there is some problem with the software implementation.
C-Block component outputs are always one simulation time-point behind the inputs. The value present at the inverting input at t = n is the output value at t = n-1. In short, C-Block components are not for analog stuff.
Quoting from my first C-Block Basics paper: “Before digging in, let’s clear up a possible misconception: C-Block components are intended for simulating digital stuff. You can do analog-like things for sure. But, if it contains feedback loops, it may not work as you plan.”
I thought the C blocks are regular components just their description equations are not “wired” into QSPICE, but we can write it.
Even simple power supply controller ICs have voltage or current feedback from the output. Will these simulations fail too? I saw a PID controller implementation on your repository. How could that work?
I’ve modified your code and schematic for 64-bit floats and a sin signal input: swopamp.cpp (1.1 KB) BlockOpa.qsch (8.0 KB)
You can reduce the inaccuracies by reducing the timestep using “.option maxstep=” or by using the Trunc() function. Here’s what we get with maxstep=100n.
If you zoom in enough, it still has delay inaccuracies though.
Maxstep slows down the entire simulation, of course. A better solution might be to use Trunc(). With Trunc() you can selectively increase the sample rate when the signal is changing rapidly. I’ll try to code up an example and post that shortly.
OK, here’s something for you to play with. What I’m trying to do is avoid .option maxstep and use Trunc() to limit the timestep only when the output voltage is changing “fast.” I’ve deliberately reduced the timestep only on the rising edge just to demonstrate the difference between when Trunc() is changing the timestep or not.
I should also point out that QSpice uses an adaptive timestep algorithm. This makes analyzing the behavior more difficult. The long .tran time let’s QSpice use longer maximum steps but it may, of course, use smaller steps. Also, adding components can change the simulation points that QSpice selects so, again, more complication when analyzing behavior.
Anyway, you can see that Trunc() tracks the rising edge tightly when the voltage rises rapidly.