ngspice/src/spicelib/devices/vsrc/vsrcload.c

423 lines
16 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
2000-04-27 22:03:57 +02:00
**********/
#include "ngspice/ngspice.h"
#include "ngspice/cktdefs.h"
2000-04-27 22:03:57 +02:00
#include "vsrcdefs.h"
#include "ngspice/trandefs.h"
#include "ngspice/sperror.h"
#include "ngspice/suffix.h"
#include "ngspice/1-f-code.h"
2000-04-27 22:03:57 +02:00
#ifdef XSPICE_EXP
/* gtri - begin - wbk - modify for supply ramping option */
#include "ngspice/cmproto.h"
/* gtri - end - wbk - modify for supply ramping option */
#endif
#ifdef SHARED_MODULE
extern double getvsrcval(double, char*);
#endif
2000-04-27 22:03:57 +02:00
int
VSRCload(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
VSRCmodel *model = (VSRCmodel *) inModel;
VSRCinstance *here;
2000-04-27 22:03:57 +02:00
double time;
2010-11-27 17:36:03 +01:00
double value = 0.0;
2000-04-27 22:03:57 +02:00
/* loop through all the source models */
2000-04-27 22:03:57 +02:00
for( ; model != NULL; model = model->VSRCnextModel ) {
/* loop through all the instances of the model */
for (here = model->VSRCinstances; here != NULL ;
here=here->VSRCnextInstance) {
2011-02-27 21:40:48 +01:00
*(here->VSRCposIbrPtr) += 1.0 ;
*(here->VSRCnegIbrPtr) -= 1.0 ;
*(here->VSRCibrPosPtr) += 1.0 ;
*(here->VSRCibrNegPtr) -= 1.0 ;
2000-04-27 22:03:57 +02:00
if( (ckt->CKTmode & (MODEDCOP | MODEDCTRANCURVE)) &&
here->VSRCdcGiven ) {
/* load using DC value */
#ifdef XSPICE_EXP
/* gtri - begin - wbk - modify to process srcFact, etc. for all sources */
value = here->VSRCdcValue;
#else
value = here->VSRCdcValue * 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 */
switch(here->VSRCfunctionType) {
2011-02-27 21:40:48 +01:00
default:
2010-12-18 18:05:44 +01:00
value = here->VSRCdcValue;
break;
2011-02-27 21:40:48 +01:00
2010-12-18 18:05:44 +01:00
case PULSE: {
2011-01-16 20:19:42 +01:00
double V1, V2, TD, TR, TF, PW, PER;
2010-12-18 18:05:44 +01:00
double basetime = 0;
#ifdef XSPICE
2010-12-18 18:05:44 +01:00
double PHASE;
double phase;
double deltat;
#endif
2010-12-18 18:05:44 +01:00
V1 = here->VSRCcoeffs[0];
V2 = here->VSRCcoeffs[1];
TD = here->VSRCfunctionOrder > 2
? here->VSRCcoeffs[2] : 0.0;
TR = here->VSRCfunctionOrder > 3
&& here->VSRCcoeffs[3] != 0.0
? here->VSRCcoeffs[3] : ckt->CKTstep;
TF = here->VSRCfunctionOrder > 4
&& here->VSRCcoeffs[4] != 0.0
? here->VSRCcoeffs[4] : ckt->CKTstep;
PW = here->VSRCfunctionOrder > 5
&& here->VSRCcoeffs[5] != 0.0
? here->VSRCcoeffs[5] : ckt->CKTfinalTime;
PER = here->VSRCfunctionOrder > 6
&& here->VSRCcoeffs[6] != 0.0
? here->VSRCcoeffs[6] : ckt->CKTfinalTime;
2011-02-27 21:40:48 +01:00
/* shift time by delay time TD */
2010-12-18 18:05:44 +01:00
time -= TD;
#ifdef XSPICE
2011-02-27 21:40:48 +01:00
/* gtri - begin - wbk - add PHASE parameter */
2010-12-18 18:05:44 +01:00
PHASE = here->VSRCfunctionOrder > 7
? here->VSRCcoeffs[7] : 0.0;
2000-04-27 22:03:57 +02:00
2011-01-16 20:19:42 +01:00
/* normalize phase to cycles */
2010-12-18 18:05:44 +01:00
phase = PHASE / 360.0;
phase = fmod(phase, 1.0);
deltat = phase * PER;
2011-02-27 21:40:48 +01:00
while (deltat > 0)
2010-12-18 18:05:44 +01:00
deltat -= PER;
/* shift time by pase (neg. for pos. phase value) */
time += deltat;
2011-02-27 21:40:48 +01:00
/* gtri - end - wbk - add PHASE parameter */
#endif
2010-12-18 18:05:44 +01:00
if(time > PER) {
/* repeating signal - figure out where we are */
/* in period */
basetime = PER * floor(time/PER);
time -= basetime;
}
if (time <= 0 || time >= TR + PW + TF) {
value = V1;
} 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;
}
}
break;
2000-04-27 22:03:57 +02:00
2010-12-18 18:05:44 +01:00
case SINE: {
2011-02-27 21:40:48 +01:00
2010-12-18 18:05:44 +01:00
double VO, VA, FREQ, TD, THETA;
2011-02-27 21:40:48 +01:00
/* gtri - begin - wbk - add PHASE parameter */
#ifdef XSPICE
2010-12-18 18:05:44 +01:00
double PHASE;
double phase;
2010-12-18 18:05:44 +01:00
PHASE = here->VSRCfunctionOrder > 5
? here->VSRCcoeffs[5] : 0.0;
2011-02-27 21:40:48 +01:00
/* compute phase in radians */
2010-12-18 18:05:44 +01:00
phase = PHASE * M_PI / 180.0;
#endif
2010-12-18 18:05:44 +01:00
VO = here->VSRCcoeffs[0];
VA = here->VSRCcoeffs[1];
2011-02-27 21:40:48 +01:00
FREQ = here->VSRCfunctionOrder > 2
2011-01-16 20:19:42 +01:00
&& here->VSRCcoeffs[2] != 0.0
? here->VSRCcoeffs[2] : (1/ckt->CKTfinalTime);
TD = here->VSRCfunctionOrder > 3
? here->VSRCcoeffs[3] : 0.0;
2010-12-18 18:05:44 +01:00
THETA = here->VSRCfunctionOrder > 4
2011-01-16 20:19:42 +01:00
? here->VSRCcoeffs[4] : 0.0;
2011-02-27 21:40:48 +01:00
2010-12-18 18:05:44 +01:00
time -= TD;
if (time <= 0) {
#ifdef XSPICE
2010-12-18 18:05:44 +01:00
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);
#else
2010-12-18 18:05:44 +01:00
value = VO;
2011-02-27 21:40:48 +01:00
} else {
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 */
2010-12-18 18:05:44 +01:00
}
2000-04-27 22:03:57 +02:00
}
2010-12-18 18:05:44 +01:00
break;
2000-04-27 22:03:57 +02:00
2010-12-18 18:05:44 +01:00
case EXP: {
double V1, V2, TD1, TD2, TAU1, TAU2;
2011-02-27 21:40:48 +01:00
2010-12-18 18:05:44 +01:00
V1 = here->VSRCcoeffs[0];
V2 = here->VSRCcoeffs[1];
2011-02-27 21:40:48 +01:00
TD1 = here->VSRCfunctionOrder > 2
2010-12-18 18:05:44 +01:00
&& here->VSRCcoeffs[2] != 0.0
? here->VSRCcoeffs[2] : ckt->CKTstep;
2011-02-27 21:40:48 +01:00
TAU1 = here->VSRCfunctionOrder > 3
2010-12-18 18:05:44 +01:00
&& here->VSRCcoeffs[3] != 0.0
? here->VSRCcoeffs[3] : ckt->CKTstep;
2011-02-27 21:40:48 +01:00
TD2 = here->VSRCfunctionOrder > 4
2010-12-18 18:05:44 +01:00
&& here->VSRCcoeffs[4] != 0.0
? here->VSRCcoeffs[4] : TD1 + ckt->CKTstep;
2011-02-27 21:40:48 +01:00
TAU2 = here->VSRCfunctionOrder > 5
2010-12-18 18:05:44 +01:00
&& here->VSRCcoeffs[5]
? here->VSRCcoeffs[5] : ckt->CKTstep;
2011-02-27 21:40:48 +01:00
2010-12-18 18:05:44 +01:00
if(time <= TD1) {
value = V1;
} else if (time <= TD2) {
2011-02-27 21:40:48 +01:00
value = V1 + (V2-V1)*(1-exp(-(time-TD1)/TAU1));
2010-12-18 18:05:44 +01:00
} else {
value = V1 + (V2-V1)*(1-exp(-(time-TD1)/TAU1)) +
(V1-V2)*(1-exp(-(time-TD2)/TAU2)) ;
}
2000-04-27 22:03:57 +02:00
}
2010-12-18 18:05:44 +01:00
break;
2000-04-27 22:03:57 +02:00
2011-02-27 21:40:48 +01:00
case SFFM: {
2010-12-18 18:05:44 +01:00
double VO, VA, FC, MDI, FS;
/* gtri - begin - wbk - add PHASE parameters */
#ifdef XSPICE
2010-12-18 18:05:44 +01:00
double PHASEC, PHASES;
double phasec;
double phases;
2011-02-27 21:40:48 +01:00
2010-12-18 18:05:44 +01:00
PHASEC = here->VSRCfunctionOrder > 5
2011-02-27 21:40:48 +01:00
? here->VSRCcoeffs[5] : 0.0;
2010-12-18 18:05:44 +01:00
PHASES = here->VSRCfunctionOrder > 6
2011-02-27 21:40:48 +01:00
? here->VSRCcoeffs[6] : 0.0;
2010-12-18 18:05:44 +01:00
/* 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
2010-12-18 18:05:44 +01:00
VO = here->VSRCcoeffs[0];
VA = here->VSRCcoeffs[1];
2011-02-27 21:40:48 +01:00
FC = here->VSRCfunctionOrder > 2
2010-12-18 18:05:44 +01:00
&& here->VSRCcoeffs[2]
? here->VSRCcoeffs[2] : (1/ckt->CKTfinalTime);
MDI = here->VSRCfunctionOrder > 3
? here->VSRCcoeffs[3] : 0.0;
2011-02-27 21:40:48 +01:00
FS = here->VSRCfunctionOrder > 4
2010-12-18 18:05:44 +01:00
&& here->VSRCcoeffs[4]
? here->VSRCcoeffs[4] : (1/ckt->CKTfinalTime);
2011-02-27 21:40:48 +01:00
#ifdef XSPICE
2010-12-18 18:05:44 +01:00
/* compute waveform value */
2011-02-27 21:40:48 +01:00
value = VO + VA *
sin((2.0 * M_PI * FC * time + phasec) +
2010-12-18 18:05:44 +01:00
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
2011-02-27 21:40:48 +01:00
/* gtri - end - wbk - add PHASE parameters */
2010-12-18 18:05:44 +01:00
}
break;
2011-02-27 21:40:48 +01:00
case AM: {
2010-12-18 18:05:44 +01:00
double VA, FC, MF, VO, TD;
2011-02-27 21:40:48 +01:00
/* gtri - begin - wbk - add PHASE parameters */
#ifdef XSPICE
2010-12-18 18:05:44 +01:00
double PHASEC, PHASES;
double phasec;
double phases;
2011-02-27 21:40:48 +01:00
2010-12-18 18:05:44 +01:00
PHASEC = here->VSRCfunctionOrder > 5
2011-02-27 21:40:48 +01:00
? here->VSRCcoeffs[5] : 0.0;
2010-12-18 18:05:44 +01:00
PHASES = here->VSRCfunctionOrder > 6
2011-02-27 21:40:48 +01:00
? here->VSRCcoeffs[6] : 0.0;
2010-12-18 18:05:44 +01:00
/* 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
2010-12-18 18:05:44 +01:00
VA = here->VSRCcoeffs[0];
VO = here->VSRCcoeffs[1];
2011-02-27 21:40:48 +01:00
MF = here->VSRCfunctionOrder > 2
2010-12-18 18:05:44 +01:00
&& here->VSRCcoeffs[2]
? here->VSRCcoeffs[2] : (1/ckt->CKTfinalTime);
FC = here->VSRCfunctionOrder > 3
? here->VSRCcoeffs[3] : 0.0;
2011-02-27 21:40:48 +01:00
TD = here->VSRCfunctionOrder > 4
2010-12-18 18:05:44 +01:00
&& here->VSRCcoeffs[4]
? here->VSRCcoeffs[4] : 0.0;
2010-12-18 18:05:44 +01:00
time -= TD;
if (time <= 0) {
value = 0;
} else {
#ifdef XSPICE
2010-12-18 18:05:44 +01:00
/* compute waveform value */
value = VA * (VO + sin(2.0 * M_PI * MF * time + phases )) *
sin(2.0 * M_PI * FC * time + phases);
2011-02-27 21:40:48 +01:00
#else
2010-12-18 18:05:44 +01:00
value = VA * (VO + sin(2.0 * M_PI * MF * time)) *
sin(2.0 * M_PI * FC * time);
2011-02-27 21:40:48 +01:00
#endif
2010-12-18 18:05:44 +01:00
}
2011-02-27 21:40:48 +01:00
/* gtri - end - wbk - add PHASE parameters */
2010-11-27 17:36:03 +01:00
}
2010-12-18 18:05:44 +01:00
break;
2011-02-27 21:40:48 +01:00
2010-12-18 18:05:44 +01:00
case PWL: {
int i = 0, num_repeat = 0, ii = 0;
double foo, repeat_time = 0, end_time, breakpt_time, itime;
2010-02-26 23:55:40 +01:00
2010-12-18 18:05:44 +01:00
time -= here->VSRCrdelay;
2010-02-26 23:55:40 +01:00
2010-12-18 18:05:44 +01:00
if(time < *(here->VSRCcoeffs)) {
foo = *(here->VSRCcoeffs + 1) ;
value = foo;
goto loadDone;
}
2010-12-18 18:05:44 +01:00
do {
for(i=ii ; i<(here->VSRCfunctionOrder/2)-1; i++ ) {
itime = *(here->VSRCcoeffs+2*i);
if ( AlmostEqualUlps(itime+repeat_time, time, 3 )) {
foo = *(here->VSRCcoeffs+2*i+1);
value = foo;
goto loadDone;
2011-02-27 21:40:48 +01:00
} else if ( (*(here->VSRCcoeffs+2*i)+repeat_time < time)
2011-01-16 20:19:42 +01:00
&& (*(here->VSRCcoeffs+2*(i+1))+repeat_time > time) ) {
2010-12-18 18:05:44 +01:00
foo = *(here->VSRCcoeffs+2*i+1) + (((time-(*(here->VSRCcoeffs+2*i)+repeat_time))/
2011-01-16 20:19:42 +01:00
(*(here->VSRCcoeffs+2*(i+1)) - *(here->VSRCcoeffs+2*i))) *
(*(here->VSRCcoeffs+2*i+3) - *(here->VSRCcoeffs+2*i+1)));
2010-12-18 18:05:44 +01:00
value = foo;
goto loadDone;
}
2010-11-27 17:36:03 +01:00
}
2010-12-18 18:05:44 +01:00
foo = *(here->VSRCcoeffs+ here->VSRCfunctionOrder-1) ;
value = foo;
2010-12-18 18:05:44 +01:00
if ( !here->VSRCrGiven ) goto loadDone;
2011-02-27 21:40:48 +01:00
2010-12-18 18:05:44 +01:00
end_time = *(here->VSRCcoeffs + here->VSRCfunctionOrder-2);
breakpt_time = *(here->VSRCcoeffs + here->VSRCrBreakpt);
repeat_time = end_time + (end_time - breakpt_time)*num_repeat++ - breakpt_time;
ii = here->VSRCrBreakpt/2;
} while ( here->VSRCrGiven );
break;
}
2010-11-27 17:36:03 +01:00
2011-02-27 21:40:48 +01:00
/**** tansient noise routines:
2010-11-27 17:36:03 +01:00
VNoi2 2 0 DC 0 TRNOISE(10n 0.5n 0 0n) : generate gaussian distributed noise
rms value, time step, 0 0
VNoi1 1 0 DC 0 TRNOISE(0n 0.5n 1 10n) : generate 1/f noise
0, time step, exponent < 2, rms value
2010-12-18 18:05:44 +01:00
VNoi3 3 0 DC 0 TRNOISE(0 0 0 0 15m 22u 50u) : generate RTS noise
0 0 0 0, amplitude, capture time, emission time
2010-11-27 17:36:03 +01:00
*/
2010-12-18 18:05:44 +01:00
case TRNOISE: {
2010-11-27 17:36:03 +01:00
2010-12-18 18:05:44 +01:00
struct trnoise_state *state = here -> VSRCtrnoise_state;
2010-11-27 17:36:03 +01:00
2010-12-18 18:05:44 +01:00
double TS = state -> TS;
double RTSAM = state->RTSAM;
2010-11-27 17:36:03 +01:00
/* reset top (hack for repeated tran commands) */
if (time == 0)
state->top = 0;
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);
2010-11-27 17:36:03 +01:00
2010-12-18 18:05:44 +01:00
double V1 = trnoise_state_get(state, ckt, n1);
double V2 = trnoise_state_get(state, ckt, n1+1);
2010-11-27 17:36:03 +01:00
value = V1 + (V2 - V1) * (time / TS - (double)n1);
2010-12-18 18:05:44 +01:00
}
/* RTS noise */
if (RTSAM > 0) {
double RTScapTime = state->RTScapTime;
if (time >= RTScapTime)
value += RTSAM;
}
2010-11-27 17:36:03 +01:00
2011-02-28 21:32:34 +01:00
/* DC value */
2010-12-18 18:05:44 +01:00
if(here -> VSRCdcGiven)
value += here->VSRCdcValue;
}
2011-02-27 21:40:48 +01:00
break;
2011-01-16 20:19:42 +01:00
case TRRANDOM: {
struct trrandom_state *state = here -> VSRCtrrandom_state;
value = state -> value;
2011-02-27 21:40:48 +01:00
/* DC value */
2011-01-16 20:19:42 +01:00
if(here -> VSRCdcGiven)
value += here->VSRCdcValue;
}
break;
#ifdef SHARED_MODULE
case EXTERNAL: {
value = getvsrcval(time, here->VSRCname);
if(here -> VSRCdcGiven)
value += here->VSRCdcValue;
}
break;
#endif
2010-11-27 17:36:03 +01:00
} // switch
} // else (line 48)
loadDone:
2011-02-27 21:40:48 +01:00
/* gtri - begin - wbk - modify for supply ramping option */
#ifdef XSPICE_EXP
value *= ckt->CKTsrcFact;
value *= cm_analog_ramp_factor();
#else
if (ckt->CKTmode & MODETRANOP)
value *= ckt->CKTsrcFact;
#endif
/* gtri - end - wbk - modify to process srcFact, etc. for all sources */
2011-02-27 21:05:32 +01:00
/* load the new voltage value into the matrix */
*(ckt->CKTrhs + (here->VSRCbranch)) += value;
2010-11-27 17:36:03 +01:00
} // for loop instances
} // for loop models
2011-02-27 21:40:48 +01:00
2000-04-27 22:03:57 +02:00
return(OK);
}