From 56d1d3901dbec47d32d9012df0905c6d44dc172e Mon Sep 17 00:00:00 2001 From: rlar Date: Sat, 10 Mar 2018 11:01:23 +0100 Subject: [PATCH] spicelib/devices/{sw,csw}, #4/, clean up negative hysteresis Rewrite special behaviour for negative hysteresis. This is a functional invariant cleanup of commit: > commit 0dae4607a01e2bbf93a925f5c37385ad9ce232f2 > Date: Wed Apr 25 18:28:20 2001 +0000 > > Added a patch to csw and sw from Jon Engelbert dealing with negative histeresys --- src/spicelib/devices/csw/cswload.c | 41 +++++++++++------------------ src/spicelib/devices/sw/swload.c | 42 ++++++++++++------------------ 2 files changed, 31 insertions(+), 52 deletions(-) diff --git a/src/spicelib/devices/csw/cswload.c b/src/spicelib/devices/csw/cswload.c index dddbc9169..d3a232176 100644 --- a/src/spicelib/devices/csw/cswload.c +++ b/src/spicelib/devices/csw/cswload.c @@ -56,21 +56,17 @@ CSWload(GENmodel *inModel, CKTcircuit *ckt) current_state = previous_state; } else if (ckt->CKTmode & (MODEINITFLOAT)) { + /* fixme, missleading comment: */ /* use state0 since INITTRAN or INITPRED already called */ - if (model->CSWiHysteresis > 0) { - if (i_ctrl > (model->CSWiThreshold + fabs(model->CSWiHysteresis))) - current_state = REALLY_ON; - else if (i_ctrl < (model->CSWiThreshold - fabs(model->CSWiHysteresis))) - current_state = REALLY_OFF; - else + if (i_ctrl > (model->CSWiThreshold + fabs(model->CSWiHysteresis))) + current_state = REALLY_ON; + else if (i_ctrl < (model->CSWiThreshold - fabs(model->CSWiHysteresis))) + current_state = REALLY_OFF; + else + if (model->CSWiHysteresis > 0) { current_state = previous_state; - } else { - if (i_ctrl > (model->CSWiThreshold + fabs(model->CSWiHysteresis))) - current_state = REALLY_ON; - else if (i_ctrl < (model->CSWiThreshold - fabs(model->CSWiHysteresis))) - current_state = REALLY_OFF; - else { + } else { /* in hysteresis... change value if going from low to hysteresis, * or from hi to hysteresis. */ @@ -84,7 +80,6 @@ CSWload(GENmodel *inModel, CKTcircuit *ckt) else internalerror("bad value for previous region in swload"); } - } if (current_state != old_current_state) { ckt->CKTnoncon++; /* ensure one more iteration */ @@ -93,19 +88,14 @@ CSWload(GENmodel *inModel, CKTcircuit *ckt) } else if (ckt->CKTmode & (MODEINITTRAN | MODEINITPRED)) { - if (model->CSWiHysteresis > 0) { - if (i_ctrl > (model->CSWiThreshold + fabs(model->CSWiHysteresis))) - current_state = REALLY_ON; - else if (i_ctrl < (model->CSWiThreshold - fabs(model->CSWiHysteresis))) - current_state = REALLY_OFF; - else + if (i_ctrl > (model->CSWiThreshold + fabs(model->CSWiHysteresis))) + current_state = REALLY_ON; + else if (i_ctrl < (model->CSWiThreshold - fabs(model->CSWiHysteresis))) + current_state = REALLY_OFF; + else + if (model->CSWiHysteresis > 0) { current_state = previous_state; - } else { - if (i_ctrl > (model->CSWiThreshold + fabs(model->CSWiHysteresis))) - current_state = REALLY_ON; - else if (i_ctrl < (model->CSWiThreshold - fabs(model->CSWiHysteresis))) - current_state = REALLY_OFF; - else { + } else { /* in hysteresis... change value if going from low to hysteresis, * or from hi to hysteresis. */ @@ -119,7 +109,6 @@ CSWload(GENmodel *inModel, CKTcircuit *ckt) else internalerror("bad value for previous region in cswload"); } - } } ckt->CKTstate0[here->CSWswitchstate] = current_state; diff --git a/src/spicelib/devices/sw/swload.c b/src/spicelib/devices/sw/swload.c index 44e73a517..5067898bc 100644 --- a/src/spicelib/devices/sw/swload.c +++ b/src/spicelib/devices/sw/swload.c @@ -61,20 +61,17 @@ SWload(GENmodel *inModel, CKTcircuit *ckt) } else if (ckt->CKTmode & (MODEINITFLOAT)) { + /* fixme, missleading comment: */ /* use state0 since INITTRAN or INITPRED already called */ - if (model->SWvHysteresis > 0) { - if (v_ctrl > (model->SWvThreshold + fabs(model->SWvHysteresis))) - current_state = REALLY_ON; - else if (v_ctrl < (model->SWvThreshold - fabs(model->SWvHysteresis))) - current_state = REALLY_OFF; - else + if (v_ctrl > (model->SWvThreshold + fabs(model->SWvHysteresis))) + current_state = REALLY_ON; + else if (v_ctrl < (model->SWvThreshold - fabs(model->SWvHysteresis))) + current_state = REALLY_OFF; + else + if (model->SWvHysteresis > 0) { current_state = old_current_state; - } else { // negative hysteresis case. - if (v_ctrl > (model->SWvThreshold + fabs(model->SWvHysteresis))) - current_state = REALLY_ON; - else if (v_ctrl < (model->SWvThreshold - fabs(model->SWvHysteresis))) - current_state = REALLY_OFF; - else { // in hysteresis... change value if going from low to hysteresis, or from hi to hysteresis. + } else { + // in hysteresis... change value if going from low to hysteresis, or from hi to hysteresis. // if previous state was in hysteresis, then don't change the state.. if (previous_state == HYST_OFF || previous_state == HYST_ON) current_state = previous_state; @@ -85,7 +82,6 @@ SWload(GENmodel *inModel, CKTcircuit *ckt) else internalerror("bad value for previous state in swload"); } - } if (current_state != old_current_state) { ckt->CKTnoncon++; /* ensure one more iteration */ @@ -94,19 +90,14 @@ SWload(GENmodel *inModel, CKTcircuit *ckt) } else if (ckt->CKTmode & (MODEINITTRAN | MODEINITPRED)) { - if (model->SWvHysteresis > 0) { - if (v_ctrl > (model->SWvThreshold + fabs(model->SWvHysteresis))) - current_state = REALLY_ON; - else if (v_ctrl < (model->SWvThreshold - fabs(model->SWvHysteresis))) - current_state = REALLY_OFF; - else + if (v_ctrl > (model->SWvThreshold + fabs(model->SWvHysteresis))) + current_state = REALLY_ON; + else if (v_ctrl < (model->SWvThreshold - fabs(model->SWvHysteresis))) + current_state = REALLY_OFF; + else + if (model->SWvHysteresis > 0) { current_state = previous_state; - } else { // negative hysteresis case. - if (v_ctrl > (model->SWvThreshold + fabs(model->SWvHysteresis))) - current_state = REALLY_ON; - else if (v_ctrl < (model->SWvThreshold - fabs(model->SWvHysteresis))) - current_state = REALLY_OFF; - else { + } else { if (previous_state == HYST_ON || previous_state == HYST_OFF) current_state = previous_state; else if (previous_state == REALLY_ON) @@ -114,7 +105,6 @@ SWload(GENmodel *inModel, CKTcircuit *ckt) else if (previous_state == REALLY_OFF) current_state = REALLY_ON; } - } } // code added to force the state to be updated.