#892 XSPICE Bug: A DC Sweep in a hybrid circuit is ignored
Dear ngspice dev team,
I ran into this while simulating a mixed analog/digital circuit with both a .dc sweep and a .tran analysis.
In short: during a .dc sweep the XSPICE breakpoint stays at zero, so the check for rejected time steps in EVTcall_hybrids() fires and stops the loop after the first hybrid, resulting in incorrect results.
The bug is demonstrated in this simple netlist:
adc_dac_sweep
V1 in 0 DC 0
A1 [in] [d1] adcA
A2 [in] [d2] adcB
A3 [d1] [o1] dacX
A4 [d2] [o2] dacX
R1 o1 0 1k
R2 o2 0 1k
.model adcA adc_bridge(in_low=0.35 in_high=0.45)
.model adcB adc_bridge(in_low=0.75 in_high=0.85)
.model dacX dac_bridge(out_low=0 out_high=1)
.control
dc V1 0 1.2 0.2
print v(o1) v(o2)
.endc
.end
result:
Index v-sweep v(o1) v(o2)
0 0.000000e+00 0.000000e+00 0.000000e+00
1 2.000000e-01 0.000000e+00 0.000000e+00
2 4.000000e-01 5.000000e-01 0.000000e+00
3 6.000000e-01 1.000000e+00 0.000000e+00
4 8.000000e-01 1.000000e+00 0.000000e+00
5 1.000000e+00 1.000000e+00 0.000000e+00
6 1.200000e+00 1.000000e+00 0.000000e+00
result (expected):
Index v-sweep v(o1) v(o2)
0 0.000000e+00 0.000000e+00 0.000000e+00
1 2.000000e-01 0.000000e+00 0.000000e+00
2 4.000000e-01 5.000000e-01 0.000000e+00
3 6.000000e-01 1.000000e+00 0.000000e+00
4 8.000000e-01 1.000000e+00 5.000000e-01
5 1.000000e+00 1.000000e+00 1.000000e+00
6 1.200000e+00 1.000000e+00 1.000000e+00
Cause:
EVTcall_hybrids()leaves its loop over the hybrids as soon as this check is true
(src/xspice/evt/evtcall_hybrids.c:80):
if (g_mif_info.breakpoint.current < ckt->CKTtime) break;- In a DC sweep:
DCtrCurv()puts the swept value inCKTtime(dctrcurv.c:372-378).- It never sets the breakpoint, which starts at 0 (mif.c:57).
- In a transient,
DCtran()sets the breakpoint to 1e30 (dctran.c:279).
Result:
- At every positive sweep point the loop stops after the first hybrid in netlist order.
- A
.tranrun before the sweep leaves 1e30 behind and hides the bug.
Proposed fix
Make the test run only in analyses with time axis. as far as i have seen, all time-stepping analyses set MODETRAN, so they stay unchanged (src/xspice/evt/evtcall_hybrids.c:77-80).
for(i = 0; i < num_hybrids; i++) {
EVTload_with_event(ckt, hybrids[i], MIF_STEP_PENDING);
- if (g_mif_info.breakpoint.current < ckt->CKTtime)
+ if ((ckt->CKTmode & MODETRAN) && g_mif_info.breakpoint.current < ckt->CKTtime)
System:
ngspice 47, windows 11
Disclosure:
LLM-Assistance was used to find the bug in the source code.
The proposed explanation is incoherent. What I suspect is happening is that a DAC bridge is failing to update its output when the loop is truncated. But it seems to me that the real problem is that the DC sweep code is abusing a variable with the dimension of time!
Seems to be an ancient remnant of the times when memory was scarce.
Giles, will you take care? Perhaps simply add another variable to ckt?
Maybe so. I think the first thing to try is simply substituting a local variable there. But to me, there is a bad smell coming from that code. As far as I can see, dctran.c calls EVtop() only once, but here there are two calls with a prod from EVTcall_hybrids() between. I think it is worth looking into fairly deeply, as that may show other issues. It often does.
But I will not be quick, as I have other things to finish first, including testing VCD input.
G.