ngspice/src/spicelib/devices/isrc/isrcload.c

386 lines
15 KiB
C
Raw Normal View History

2000-04-27 22:03:57 +02:00
/**********
Copyright 1990 Regents of the University of California. All rights reserved.
Author: 1985 Thomas L. Quarles
Modified: 2000 Alansfixes
$Id$
2000-04-27 22:03:57 +02:00
**********/
#include "ngspice.h"
#include "cktdefs.h"
#include "isrcdefs.h"
#include "trandefs.h"
#include "sperror.h"
#include "suffix.h"
#include "1-f-code.h"
2000-04-27 22:03:57 +02:00
#ifdef XSPICE_EXP
/* gtri - begin - wbk - modify for supply ramping option */
#include "cmproto.h"
/* gtri - end - wbk - modify for supply ramping option */
#endif
2000-04-27 22:03:57 +02:00
int
ISRCload(GENmodel *inModel, CKTcircuit *ckt)
/* actually load the current value into the
* sparse matrix previously provided
2000-04-27 22:03:57 +02:00
*/
{
2011-02-27 21:40:48 +01:00
ISRCmodel *model = (ISRCmodel *) inModel;
ISRCinstance *here;
2000-04-27 22:03:57 +02:00
double value;
double time;
/* loop through all the source models */
2000-04-27 22:03:57 +02:00
for( ; model != NULL; model = model->ISRCnextModel ) {
/* loop through all the instances of the model */
for (here = model->ISRCinstances; here != NULL ;
here=here->ISRCnextInstance) {
if (here->ISRCowner != ARCHme) continue;
2011-02-27 21:40:48 +01:00
2000-04-27 22:03:57 +02:00
if( (ckt->CKTmode & (MODEDCOP | MODEDCTRANCURVE)) &&
here->ISRCdcGiven ) {
/* load using DC value */
#ifdef XSPICE_EXP
/* gtri - begin - wbk - modify to process srcFact, etc. for all sources */
value = here->ISRCdcValue;
#else
2000-04-27 22:03:57 +02:00
value = here->ISRCdcValue * ckt->CKTsrcFact;
#endif
2000-04-27 22:03:57 +02:00
} else {
if(ckt->CKTmode & (MODEDC)) {
time = 0;
} else {
time = ckt->CKTtime;
}
/* use the transient functions */
2000-04-27 22:03:57 +02:00
switch(here->ISRCfunctionType) {
default:
#ifdef XSPICE_EXP
value = here->ISRCdcValue;
#else
value = here->ISRCdcValue * ckt->CKTsrcFact;
#endif
break;
2011-02-27 21:40:48 +01:00
case PULSE: {
2011-02-27 21:40:48 +01:00
double V1, V2, TD, TR, TF, PW, PER;
double basetime = 0;
#ifdef XSPICE
double PHASE;
double phase;
double deltat;
double basephase;
#endif
V1 = here->ISRCcoeffs[0];
V2 = here->ISRCcoeffs[1];
TD = here->ISRCfunctionOrder > 2
? here->ISRCcoeffs[2] : 0.0;
TR = here->ISRCfunctionOrder > 3
&& here->ISRCcoeffs[3] != 0.0
? here->ISRCcoeffs[3] : ckt->CKTstep;
TF = here->ISRCfunctionOrder > 4
&& here->ISRCcoeffs[4] != 0.0
? here->ISRCcoeffs[4] : ckt->CKTstep;
PW = here->ISRCfunctionOrder > 5
&& here->ISRCcoeffs[5] != 0.0
? here->ISRCcoeffs[5] : ckt->CKTfinalTime;
PER = here->ISRCfunctionOrder > 6
&& here->ISRCcoeffs[6] != 0.0
? here->ISRCcoeffs[6] : ckt->CKTfinalTime;
#ifdef XSPICE
2011-02-27 21:40:48 +01:00
/* gtri - begin - wbk - add PHASE parameter */
PHASE = here->ISRCfunctionOrder > 7
? here->ISRCcoeffs[7] : 0.0;
2011-02-27 21:40:48 +01:00
/* normalize phase to 0 - 2PI */
phase = PHASE * M_PI / 180.0;
basephase = 2 * M_PI * floor(phase / (2 * M_PI));
phase -= basephase;
2011-02-27 21:40:48 +01:00
/* compute equivalent delta time and add to time */
deltat = (phase / (2 * M_PI)) * PER;
time += deltat;
2011-02-27 21:40:48 +01:00
/* gtri - end - wbk - add PHASE parameter */
#endif
time -= TD;
2011-02-27 21:40:48 +01:00
if(time > PER) {
/* repeating signal - figure out where we are */
/* in period */
basetime = PER * floor(time/PER);
time -= basetime;
}
2011-02-27 21:40:48 +01:00
if (time <= 0 || time >= TR + PW + TF) {
value = V1;
2011-02-27 21:40:48 +01:00
} else if (time >= TR && time <= TR + PW) {
value = V2;
} else if (time > 0 && time < TR) {
value = V1 + (V2 - V1) * (time) / TR;
} else { /* time > TR + PW && < TR + PW + TF */
value = V2 + (V1 - V2) * (time - (TR + PW)) / TF;
}
2000-04-27 22:03:57 +02:00
}
break;
2000-04-27 22:03:57 +02:00
case SINE: {
2011-02-27 21:40:48 +01:00
double VO, VA, FREQ, TD, THETA;
/* gtri - begin - wbk - add PHASE parameter */
#ifdef XSPICE
double PHASE;
double phase;
PHASE = here->ISRCfunctionOrder > 5
? here->ISRCcoeffs[5] : 0.0;
2011-02-27 21:40:48 +01:00
/* compute phase in radians */
phase = PHASE * M_PI / 180.0;
#endif
VO = here->ISRCcoeffs[0];
VA = here->ISRCcoeffs[1];
2011-02-27 21:40:48 +01:00
FREQ = here->ISRCfunctionOrder > 2
&& here->ISRCcoeffs[2] != 0.0
? here->ISRCcoeffs[2] : (1/ckt->CKTfinalTime);
TD = here->ISRCfunctionOrder > 3
? here->ISRCcoeffs[3] : 0.0;
THETA = here->ISRCfunctionOrder > 4
? here->ISRCcoeffs[4] : 0.0;
time -= TD;
if (time <= 0) {
#ifdef XSPICE
value = VO + VA * sin(phase);
} else {
2011-02-27 21:40:48 +01:00
value = VO + VA * sin(FREQ*time * 2.0 * M_PI + phase) *
exp(-time*THETA);
2009-12-20 09:44:50 +01:00
#else
value = VO;
} else {
2011-02-27 21:40:48 +01:00
value = VO + VA * sin(FREQ * time * 2.0 * M_PI) *
exp(-time*THETA);
#endif
2011-02-27 21:40:48 +01:00
/* gtri - end - wbk - add PHASE parameter */
}
2000-04-27 22:03:57 +02:00
}
break;
case EXP: {
double V1, V2, TD1, TD2, TAU1, TAU2;
2011-02-27 21:40:48 +01:00
V1 = here->ISRCcoeffs[0];
V2 = here->ISRCcoeffs[1];
2011-02-27 21:40:48 +01:00
TD1 = here->ISRCfunctionOrder > 2
&& here->ISRCcoeffs[2] != 0.0
? here->ISRCcoeffs[2] : ckt->CKTstep;
2011-02-27 21:40:48 +01:00
TAU1 = here->ISRCfunctionOrder > 3
&& here->ISRCcoeffs[3] != 0.0
? here->ISRCcoeffs[3] : ckt->CKTstep;
2011-02-27 21:40:48 +01:00
TD2 = here->ISRCfunctionOrder > 4
&& here->ISRCcoeffs[4] != 0.0
? here->ISRCcoeffs[4] : TD1 + ckt->CKTstep;
2011-02-27 21:40:48 +01:00
TAU2 = here->ISRCfunctionOrder > 5
&& here->ISRCcoeffs[5]
? here->ISRCcoeffs[5] : ckt->CKTstep;
2011-02-27 21:40:48 +01:00
if(time <= TD1) {
value = V1;
} else if (time <= TD2) {
value = V1 + (V2-V1)*(1-exp(-(time-TD1)/TAU1));
} else {
value = V1 + (V2-V1)*(1-exp(-(time-TD1)/TAU1)) +
2011-02-27 21:40:48 +01:00
(V1-V2)*(1-exp(-(time-TD2)/TAU2)) ;
}
2000-04-27 22:03:57 +02:00
}
break;
2011-02-27 21:40:48 +01:00
case SFFM: {
double VO, VA, FC, MDI, FS;
/* gtri - begin - wbk - add PHASE parameters */
#ifdef XSPICE
double PHASEC, PHASES;
double phasec;
double phases;
2011-02-27 21:40:48 +01:00
PHASEC = here->ISRCfunctionOrder > 5
2011-02-27 21:40:48 +01:00
? here->ISRCcoeffs[5] : 0.0;
PHASES = here->ISRCfunctionOrder > 6
2011-02-27 21:40:48 +01:00
? here->ISRCcoeffs[6] : 0.0;
/* compute phases in radians */
phasec = PHASEC * M_PI / 180.0;
2011-02-27 21:40:48 +01:00
phases = PHASES * M_PI / 180.0;
#endif
VO = here->ISRCcoeffs[0];
VA = here->ISRCcoeffs[1];
2011-02-27 21:40:48 +01:00
FC = here->ISRCfunctionOrder > 2
&& here->ISRCcoeffs[2]
? here->ISRCcoeffs[2] : (1/ckt->CKTfinalTime);
MDI = here->ISRCfunctionOrder > 3
? here->ISRCcoeffs[3] : 0.0;
2011-02-27 21:40:48 +01:00
FS = here->ISRCfunctionOrder > 4
&& here->ISRCcoeffs[4]
? here->ISRCcoeffs[4] : (1/ckt->CKTfinalTime);
#ifdef XSPICE
2011-02-27 21:40:48 +01:00
/* compute waveform value */
value = VO + VA *
sin((2.0 * M_PI * FC * time + phasec) +
MDI * sin(2.0 * M_PI * FS * time + phases));
#else
2011-02-27 21:40:48 +01:00
value = VO + VA *
sin((2.0 * M_PI * FC * time) +
MDI * sin(2.0 * M_PI * FS * time));
#endif
/* gtri - end - wbk - add PHASE parameters */
}
break;
2011-02-27 21:40:48 +01:00
case AM: {
double VA, FC, MF, VO, TD;
/* gtri - begin - wbk - add PHASE parameters */
#ifdef XSPICE
double PHASEC, PHASES;
double phasec;
double phases;
2011-02-27 21:40:48 +01:00
PHASEC = here->ISRCfunctionOrder > 5
2011-02-27 21:40:48 +01:00
? here->ISRCcoeffs[5] : 0.0;
PHASES = here->ISRCfunctionOrder > 6
2011-02-27 21:40:48 +01:00
? here->ISRCcoeffs[6] : 0.0;
/* compute phases in radians */
phasec = PHASEC * M_PI / 180.0;
2011-02-27 21:40:48 +01:00
phases = PHASES * M_PI / 180.0;
#endif
VA = here->ISRCcoeffs[0];
VO = here->ISRCcoeffs[1];
2011-02-27 21:40:48 +01:00
MF = here->ISRCfunctionOrder > 2
&& here->ISRCcoeffs[2]
? here->ISRCcoeffs[2] : (1/ckt->CKTfinalTime);
FC = here->ISRCfunctionOrder > 3
? here->ISRCcoeffs[3] : 0.0;
2011-02-27 21:40:48 +01:00
TD = here->ISRCfunctionOrder > 4
&& here->ISRCcoeffs[4]
? here->ISRCcoeffs[4] : 0.0;
time -= TD;
if (time <= 0) {
value = 0;
} else {
#ifdef XSPICE
2011-02-27 21:40:48 +01:00
/* compute waveform value */
value = VA * (VO + sin(2.0 * M_PI * MF * time + phases )) *
sin(2.0 * M_PI * FC * time + phases);
#else
2011-02-27 21:40:48 +01:00
value = VA * (VO + sin(2.0 * M_PI * MF * time)) *
sin(2.0 * M_PI * FC * time);
#endif
}
2011-02-27 21:40:48 +01:00
/* gtri - end - wbk - add PHASE parameters */
}
break;
case PWL: {
int i;
2011-02-27 21:40:48 +01:00
if(time < *(here->ISRCcoeffs)) {
value = *(here->ISRCcoeffs + 1) ;
break;
2000-04-27 22:03:57 +02:00
}
for(i=0;i<=(here->ISRCfunctionOrder/2)-1;i++) {
if((*(here->ISRCcoeffs+2*i)==time)) {
value = *(here->ISRCcoeffs+2*i+1);
goto loadDone;
}
if((*(here->ISRCcoeffs+2*i)<time) &&
(*(here->ISRCcoeffs+2*(i+1)) >time)) {
value = *(here->ISRCcoeffs+2*i+1) +
(((time-*(here->ISRCcoeffs+2*i))/
2011-02-27 21:40:48 +01:00
(*(here->ISRCcoeffs+2*(i+1)) -
*(here->ISRCcoeffs+2*i))) *
2011-02-27 21:40:48 +01:00
(*(here->ISRCcoeffs+2*i+3) -
*(here->ISRCcoeffs+2*i+1)));
goto loadDone;
}
2000-04-27 22:03:57 +02:00
}
value = *(here->ISRCcoeffs+ here->ISRCfunctionOrder-1) ;
break;
2000-04-27 22:03:57 +02:00
}
2011-02-27 21:40:48 +01:00
/**** tansient noise routines:
INoi2 2 0 DC 0 TRNOISE(10n 0.5n 0 0n) : generate gaussian distributed noise
rms value, time step, 0 0
INoi1 1 0 DC 0 TRNOISE(0n 0.5n 1 10n) : generate 1/f noise
0, time step, exponent < 2, rms value
*/
case TRNOISE: {
struct trnoise_state *state = here -> ISRCtrnoise_state;
double TS = state -> TS;
2010-12-18 18:05:44 +01:00
double RTSAM = state->RTSAM;
2010-12-18 18:05:44 +01:00
/* no noise */
if(TS == 0.0) {
value = 0.0;
} else {
2010-12-18 18:05:44 +01:00
/* 1/f and white noise */
size_t n1 = (size_t) floor(time / TS);
double V1 = trnoise_state_get(state, ckt, n1);
double V2 = trnoise_state_get(state, ckt, n1+1);
value = V1 + (V2 - V1) * (time / TS - n1);
}
2010-12-18 18:05:44 +01:00
/* RTS noise */
if (RTSAM > 0) {
double RTScapTime = state->RTScapTime;
if (time >= RTScapTime)
value += RTSAM;
}
/* DC value */
if(here -> ISRCdcGiven)
value += here->ISRCdcValue;
}
2011-02-27 21:40:48 +01:00
break;
2011-02-27 21:40:48 +01:00
} // switch
} // else (line 48)
2000-04-27 22:03:57 +02:00
loadDone:
/* gtri - begin - wbk - modify for supply ramping option */
#ifdef XSPICE_EXP
2011-02-27 21:40:48 +01:00
value *= ckt->CKTsrcFact;
value *= cm_analog_ramp_factor();
#else
if (ckt->CKTmode & MODETRANOP)
value *= ckt->CKTsrcFact;
#endif
/* gtri - end - wbk - modify for supply ramping option */
2000-04-27 22:03:57 +02:00
*(ckt->CKTrhs + (here->ISRCposNode)) += value;
*(ckt->CKTrhs + (here->ISRCnegNode)) -= value;
/* gtri - end - wbk - modify to process srcFact, etc. for all sources */
#ifdef XSPICE
/* gtri - begin - wbk - record value so it can be output if requested */
here->ISRCcurrent = value;
/* gtri - end - wbk - record value so it can be output if requested */
#endif
} // for loop instances
} // for loop models
2011-02-27 21:40:48 +01:00
2000-04-27 22:03:57 +02:00
return(OK);
}