diff --git a/examples/ddt/ddt-test2.cir b/examples/ddt/ddt-test2.cir index a101f62b3..9dd596cdc 100644 --- a/examples/ddt/ddt-test2.cir +++ b/examples/ddt/ddt-test2.cir @@ -1,4 +1,4 @@ -dd test 2 with .func +ddt test 2 with .func V1 1 0 dc 0 pulse 0 2 0 1 1 1 4 diff --git a/examples/ddt/ddt-test3.cir b/examples/ddt/ddt-test3.cir new file mode 100644 index 000000000..35ead98df --- /dev/null +++ b/examples/ddt/ddt-test3.cir @@ -0,0 +1,17 @@ +ddt test 3 with RC + +V1 1 0 dc 0 pulse 0 2 1u 1n 1n 100u 200u +R1 1 2 1k +C1 2 0 1u + +B2 22 0 v = ddt(V(2))/1000 + +.tran 100n 3m + +.control +run +plot v(2) v(22) +*print v(22) +.endc + +.end diff --git a/src/spicelib/parser/ptfuncs.c b/src/spicelib/parser/ptfuncs.c index 2ea1d8ba4..55e8ec161 100644 --- a/src/spicelib/parser/ptfuncs.c +++ b/src/spicelib/parser/ptfuncs.c @@ -413,15 +413,18 @@ PTnint(double arg1) return nearbyint(arg1); } + +/* Calculate the derivative during a transient simulation. + If time == 0, return 0. + If not transient sim, return 0. + The derivative is then (y2-y1)/(t2-t1). + */ double PTddt(double arg, void* data) { struct ddtdata { int n; double* vals; } *thing = (struct ddtdata*)data; double y, time; - double debarr[7]; - int i; - CKTcircuit* ckt = ft_curckt->ci_ckt; time = ckt->CKTtime; @@ -431,10 +434,9 @@ PTddt(double arg, void* data) return 0; } - if (!(ckt->CKTmode & MODETRAN) /*&& !(ckt->CKTmode & MODEINITTRAN)*/) + if (!(ckt->CKTmode & MODETRAN)) return 0; - if (time > thing->vals[0]) { thing->vals[4] = thing->vals[2]; thing->vals[5] = thing->vals[3]; @@ -443,24 +445,21 @@ PTddt(double arg, void* data) thing->vals[0] = time; thing->vals[1] = arg; - for (i = 0; i < 7; i++) - debarr[i] = thing->vals[i]; - +/* // Some less effective smoothing option + if (thing->vals[2] > 0) { + thing->vals[6] = 0.5 * ((arg - thing->vals[3]) / (time - thing->vals[2]) + thing->vals[6]); + } +*/ if (thing->n > 1) { - // if (thing->vals[2] > 0) { - // debarr[6] = thing->vals[6] = 0.5 * ((arg - thing->vals[3]) / (time - thing->vals[2]) + thing->vals[6]); - debarr[6] = thing->vals[6] = (thing->vals[1] - thing->vals[3]) / (thing->vals[2] - thing->vals[4]); + thing->vals[6] = (thing->vals[1] - thing->vals[3]) / (thing->vals[2] - thing->vals[4]); } else { - debarr[6] = thing->vals[6] = 0; - debarr[3] = thing->vals[3] = arg; + thing->vals[6] = 0; + thing->vals[3] = arg; } thing->n += 1; } - for (i = 0; i < 7; i++) - debarr[i] = thing->vals[i]; - y = thing->vals[6]; return y;