Jspice3
srcset.c
Go to the documentation of this file.
1 /***************************************************************************
2 JSPICE3 adaptation of Spice3e2 - Copyright (c) Stephen R. Whiteley 1992
3 Copyright 1990 Regents of the University of California. All rights reserved.
4 Authors: 1985 Thomas L. Quarles
5  1987 Kanwar Jit Singh
6  1992 Stephen R. Whiteley
7 ****************************************************************************/
8 
9 #include "spice.h"
10 #include <stdio.h>
11 #include "srcdefs.h"
12 #include "sperror.h"
13 #include "util.h"
14 #include "cktext.h"
15 
16 #ifdef __STDC__
17 static void src_ccvs(CKTcircuit*,SRCinstance*);
18 static void src_vcvs(CKTcircuit*,SRCinstance*);
19 static void src_cccs(CKTcircuit*,SRCinstance*);
20 static void src_vccs(CKTcircuit*,SRCinstance*);
21 static void src_dc(CKTcircuit*,SRCinstance*);
22 static void src_func0(CKTcircuit*,SRCinstance*);
23 static void src_func1(CKTcircuit*,SRCinstance*);
24 #else
25 static void src_ccvs();
26 static void src_vcvs();
27 static void src_cccs();
28 static void src_vccs();
29 static void src_dc();
30 static void src_func0();
31 static void src_func1();
32 #endif
33 
34 #define MY_TSTALLOC(ptr,first,second) \
35 if((here->ptr = spGetElement(matrix,here->first,((CKTnode*)(second))->number))\
36  ==(double *)NULL){\
37  return(E_NOMEM);\
38 }
39 
40 
41 /*ARGSUSED*/
42 int
43 SRCsetup(matrix,inModel,ckt,states)
44 
45 /* load the voltage source structure with those
46  * pointers needed later for fast matrix loading
47  */
48 SMPmatrix *matrix;
49 GENmodel *inModel;
50 CKTcircuit *ckt;
51 int *states;
52 {
53  SRCinstance *here;
54  SRCmodel *model = (SRCmodel*)inModel;
55  int error, i, j;
56  CKTnode *tmp;
57  double radians;
58 
59  /* loop through all the user models*/
60  for ( ; model != NULL; model = model->SRCnextModel) {
61 
62  /* loop through all the instances of the model */
63  for (here = model->SRCinstances; here != NULL;
64  here = here->SRCnextInstance) {
65 
66  if (here->SRCtype == SRC_V) {
67  if (here->SRCbranch == 0) {
68  error = CKTmkCur(ckt,&tmp,here->SRCname,"branch");
69  if (error)
70  return (error);
71  here->SRCbranch = tmp->number;
72  }
73  }
74 
75  here->SRCvalue = here->SRCdcValue;
76 
77  if (here->SRCacGiven && !here->SRCacMGiven)
78  here->SRCacMag = 1;
79  if (here->SRCacGiven && !here->SRCacPGiven)
80  here->SRCacPhase = 0;
81  radians = here->SRCacPhase * M_PI / 180.0;
82  here->SRCacReal = here->SRCacMag * cos(radians);
83  here->SRCacImag = here->SRCacMag * sin(radians);
84 
85 #ifdef notdef
86  if (!here->SRCdcGiven) {
87  /* no DC value - either have a transient value, or none */
88  if (here->SRCfuncTGiven) {
89  (*(SPfrontEnd->IFerror))(ERR_WARNING,
90  "%s: no DC value, transient time 0 value used",
91  &(here->SRCname));
92  }
93  else {
94  (*(SPfrontEnd->IFerror))(ERR_WARNING,
95  "%s: has no value, DC 0 assumed",
96  &(here->SRCname));
97  }
98  }
99 #endif
100 
101  here->SRCstate = *states;
102 
103  if (here->SRCtree) {
104  if (here->SRCtree->numVars) {
105  *states += here->SRCtree->numVars;
106  here->SRCvalues = (double *)
107  MALLOC((here->SRCtree->numVars)*sizeof(double));
108  if (here->SRCvalues == NULL)
109  return (E_NOMEM);
110  here->SRCderivs = (double *)
111  MALLOC((here->SRCtree->numVars)*sizeof(double));
112  if (here->SRCderivs == NULL)
113  return (E_NOMEM);
114  here->SRCeqns = (int *)
115  MALLOC((here->SRCtree->numVars)*sizeof(int));
116  if (here->SRCeqns == NULL)
117  return (E_NOMEM);
118  }
119  here->SRCacValues = (double *)
120  MALLOC((here->SRCtree->numVars + 1)*sizeof(double));
121  if (here->SRCacValues == NULL)
122  return (E_NOMEM);
123  }
124  else if (here->SRCccCoeffGiven) {
125  here->SRCcontBranch = CKTfndBranch(ckt,here->SRCcontName);
126  if (here->SRCcontBranch == 0) {
127  IFuid namarray[2];
128  namarray[0] = here->SRCname;
129  namarray[1] = here->SRCcontName;
130  (*(SPfrontEnd->IFerror))(ERR_FATAL,
131  "%s: unknown controlling source %s",namarray);
132  return (E_BADPARM);
133  }
134  }
135 
136  if (here->SRCtype == SRC_V) {
137  TSTALLOC(SRCposIbrptr, SRCposNode, SRCbranch)
138  TSTALLOC(SRCnegIbrptr, SRCnegNode, SRCbranch)
139  TSTALLOC(SRCibrNegptr, SRCbranch, SRCnegNode)
140  TSTALLOC(SRCibrPosptr, SRCbranch, SRCposNode)
141 
142  /* for pz analysis, an ac source is open, ac has precedence
143  * over FUNC
144  */
145  if (here->SRCacGiven)
146  TSTALLOC(SRCibrIbrptr, SRCbranch, SRCbranch)
147 
148  if (ckt->CKTpreload) {
149  /* preload constants */
150 
151  if (here->SRCposNode) {
152  *(here->SRCposIbrptr) += 1.0;
153  *(here->SRCibrPosptr) += 1.0;
154  }
155  if (here->SRCnegNode) {
156  *(here->SRCnegIbrptr) -= 1.0;
157  *(here->SRCibrNegptr) -= 1.0;
158  }
159  }
160 
161  if (here->SRCccCoeffGiven) {
162  TSTALLOC(SRCibrContBrptr, SRCbranch, SRCcontBranch)
163  here->SRCtranFunc = src_ccvs;
164  here->SRCdcFunc = src_ccvs;
165  continue;
166  }
167  else if (here->SRCvcCoeffGiven) {
168  TSTALLOC(SRCibrContPosptr, SRCbranch, SRCcontPosNode)
169  TSTALLOC(SRCibrContNegptr, SRCbranch, SRCcontNegNode)
170  here->SRCtranFunc = src_vcvs;
171  here->SRCdcFunc = src_vcvs;
172  continue;
173  }
174  }
175  else {
176 
177  if (here->SRCccCoeffGiven) {
178  TSTALLOC(SRCposContBrptr,SRCposNode,SRCcontBranch)
179  TSTALLOC(SRCnegContBrptr,SRCnegNode,SRCcontBranch)
180  here->SRCtranFunc = src_cccs;
181  here->SRCdcFunc = src_cccs;
182  continue;
183  }
184  else if (here->SRCvcCoeffGiven) {
185  TSTALLOC(SRCposContPosptr, SRCposNode, SRCcontPosNode)
186  TSTALLOC(SRCposContNegptr, SRCposNode, SRCcontNegNode)
187  TSTALLOC(SRCnegContPosptr, SRCnegNode, SRCcontPosNode)
188  TSTALLOC(SRCnegContNegptr, SRCnegNode, SRCcontNegNode)
189  here->SRCtranFunc = src_vccs;
190  here->SRCdcFunc = src_vccs;
191  continue;
192  }
193  }
194 
195  if (here->SRCtree == NULL) {
196  here->SRCtranFunc = src_dc;
197  here->SRCdcFunc = src_dc;
198  continue;
199  }
200  if (here->SRCtree->numVars == 0) {
201  here->SRCtranFunc = src_func0;
202  if (here->SRCdcGiven)
203  here->SRCdcFunc = src_dc;
204  else
205  here->SRCdcFunc = src_func0;
206  continue;
207  }
208  here->SRCtranFunc = src_func1;
209  if (here->SRCdcGiven)
210  here->SRCdcFunc = src_dc;
211  else
212  here->SRCdcFunc = src_func1;
213 
214  /* For each controlling variable set the entries
215  * in the vector of the positions of the SMP
216  */
217 
218  j = here->SRCtree->numVars;
219  if (here->SRCtype == SRC_I) j *= 2;
220 
221  here->SRCposptr = (double **)MALLOC(j * sizeof(double *));
222 
223  for (j = 0,i = 0; i < here->SRCtree->numVars; i++) {
224  switch (here->SRCtree->varTypes[i]) {
225 
226  case IF_INSTANCE:
227  here->SRCcontBranch = CKTfndBranch(ckt,
228  here->SRCtree->vars[i].uValue);
229  if (here->SRCcontBranch == 0) {
230  IFuid namarray[2];
231  namarray[0] = here->SRCname;
232  namarray[1] = here->SRCtree->vars[i].uValue;
233  (*(SPfrontEnd->IFerror))(ERR_FATAL,
234  "%s: unknown controlling source %s",namarray);
235  return (E_BADPARM);
236  }
237 
238  if (here->SRCtype == SRC_V) {
239  TSTALLOC(SRCposptr[j++],SRCbranch,SRCcontBranch);
240  }
241  else if (here->SRCtype == SRC_I) {
242  TSTALLOC(SRCposptr[j++],SRCposNode,SRCcontBranch);
243  TSTALLOC(SRCposptr[j++],SRCnegNode,SRCcontBranch);
244  }
245  else {
246  return (E_BADPARM);
247  }
248  here->SRCeqns[i] = here->SRCcontBranch;
249  break;
250 
251  case IF_NODE:
252  if (here->SRCtype == SRC_V) {
253  MY_TSTALLOC(SRCposptr[j++],SRCbranch,
254  here->SRCtree->vars[i].nValue);
255  }
256  else if(here->SRCtype == SRC_I) {
257  MY_TSTALLOC(SRCposptr[j++],SRCposNode,
258  here->SRCtree->vars[i].nValue);
259  MY_TSTALLOC(SRCposptr[j++],SRCnegNode,
260  here->SRCtree->vars[i].nValue);
261  }
262  else{
263  return (E_BADPARM);
264  }
265  here->SRCeqns[i] = ((CKTnode *)(here->SRCtree->vars[i].
266  nValue))->number;
267  break;
268 
269  default:
270  break;
271  }
272  }
273  }
274  }
275  return (OK);
276 }
277 
278 /* evaluation functions */
279 
280 static void
281 src_ccvs(ckt,here)
282 
283 CKTcircuit* ckt;
284 SRCinstance *here;
285 {
286  *(here->SRCibrContBrptr) += here->SRCcoeff;
287 }
288 
289 
290 static void
291 src_vcvs(ckt,here)
292 
293 CKTcircuit* ckt;
294 SRCinstance *here;
295 {
296  *(here->SRCibrContPosptr) -= here->SRCcoeff;
297  *(here->SRCibrContNegptr) += here->SRCcoeff;
298 }
299 
300 
301 static void
302 src_cccs(ckt,here)
303 
304 CKTcircuit* ckt;
305 SRCinstance *here;
306 {
307  *(here->SRCposContBrptr) += here->SRCcoeff;
308  *(here->SRCnegContBrptr) -= here->SRCcoeff;
309 }
310 
311 
312 static void
313 src_vccs(ckt,here)
314 
315 CKTcircuit* ckt;
316 SRCinstance *here;
317 {
318  *(here->SRCposContPosptr) += here->SRCcoeff;
319  *(here->SRCposContNegptr) -= here->SRCcoeff;
320  *(here->SRCnegContPosptr) -= here->SRCcoeff;
321  *(here->SRCnegContNegptr) += here->SRCcoeff;
322 }
323 
324 
325 static void
326 src_dc(ckt,here)
327 
328 CKTcircuit *ckt;
329 SRCinstance *here;
330 {
331  double rhs = ckt->CKTsrcFact * here->SRCdcValue;
332 
333  if (here->SRCtype == SRC_V) {
334  *(ckt->CKTrhs + here->SRCbranch) = rhs;
335  }
336  else {
337  *(ckt->CKTrhs + (here->SRCposNode)) -= rhs;
338  *(ckt->CKTrhs + (here->SRCnegNode)) += rhs;
339  }
340 }
341 
342 
343 static void
344 src_func0(ckt,here)
345 
346 CKTcircuit *ckt;
347 SRCinstance *here;
348 {
349  double rhs;
350 
351  if (ckt->CKTmode & (MODEINITTRAN|MODEINITPRED|MODEDC)) {
352  rhs = 0;
353 
354  if (ckt->CKTmode & MODEDCTRANCURVE)
355  rhs = ckt->CKTsrcFact * here->SRCdcValue;
356  else if ((*(here->SRCtree->IFeval))
357  (here->SRCtree,ckt->CKTgmin,&rhs,NULL,
358  NULL,&ckt->CKTtime) == OK) {
359  /* !! last arg bogus if ft_sim->specSigs changed !! */
360 
361  here->SRCvalue = rhs;
362 
363  /* Store the rhs for small signal analysis */
364  if (ckt->CKTmode & MODEINITSMSIG) {
365  here->SRCacValues[0] = rhs;
366  }
367  }
368  }
369  else {
370  rhs = here->SRCvalue;
371  }
372 
373  if (here->SRCtype == SRC_V) {
374  *(ckt->CKTrhs + here->SRCbranch) = rhs;
375  }
376  else {
377  *(ckt->CKTrhs + (here->SRCposNode)) -= rhs;
378  *(ckt->CKTrhs + (here->SRCnegNode)) += rhs;
379  }
380 }
381 
382 
383 static void
384 src_func1(ckt,here)
385 
386 CKTcircuit *ckt;
387 SRCinstance *here;
388 {
389  int i, j;
390  double rhs, deriv;
391 
392  if (ckt->CKTmode & (MODEINITFLOAT|MODEINITFIX)) {
393  for (i = 0; i < here->SRCtree->numVars; i++) {
394  here->SRCvalues[i] =
395  *(ckt->CKTrhsOld + here->SRCeqns[i]);
396  }
397  }
398  else if (ckt->CKTmode & MODEINITPRED) {
399  double *d = ckt->CKTpred;
400  for (i = 0; i < here->SRCtree->numVars; i++) {
401  here->SRCvalues[i] =
402  *(ckt->CKTstate1 + here->SRCstate + i)*d[0] +
403  *(ckt->CKTstate2 + here->SRCstate + i)*d[1] +
404  *(ckt->CKTstate3 + here->SRCstate + i)*d[2] +
405  *(ckt->CKTstate0 + here->SRCstate + i)*d[3];
406  }
407  }
408  else if (ckt->CKTmode & MODEINITJCT) {
409  for (i = 0; i < here->SRCtree->numVars; i++) {
410  here->SRCvalues[i] = 0;
411  }
412  }
413  else {
414  for (i = 0; i < here->SRCtree->numVars; i++) {
415  here->SRCvalues[i] =
416  *(ckt->CKTstate1 + here->SRCstate + i);
417  }
418  }
419 
420  for (i = 0; i < here->SRCtree->numVars; i++) {
421  *(ckt->CKTstate0 + here->SRCstate + i) =
422  here->SRCvalues[i];
423  }
424 
425  rhs = 0;
426  if ((*(here->SRCtree->IFeval))
427  (here->SRCtree,ckt->CKTgmin,&rhs,here->SRCvalues,
428  here->SRCderivs,&ckt->CKTtime) == OK) {
429  /* !! last arg bogus if ft_sim->specSigs changed !! */
430 
431  here->SRCprev = rhs;
432 
433  if (here->SRCtype == SRC_V) {
434  for (i = 0; i < here->SRCtree->numVars; i++) {
435  deriv = here->SRCderivs[i];
436  rhs -= (here->SRCvalues[i] * deriv);
437  *(here->SRCposptr[i]) -= deriv;
438  }
439  *(ckt->CKTrhs + here->SRCbranch) = rhs;
440  }
441  else {
442  for (j = 0,i = 0; i < here->SRCtree->numVars; i++) {
443  deriv = here->SRCderivs[i];
444  rhs -= (here->SRCvalues[i] * deriv);
445  *(here->SRCposptr[j++]) += deriv;
446  *(here->SRCposptr[j++]) -= deriv;
447  }
448  *(ckt->CKTrhs + (here->SRCposNode)) -= rhs;
449  *(ckt->CKTrhs + (here->SRCnegNode)) += rhs;
450  }
451 
452  /* Store the rhs for small signal analysis */
453  if (ckt->CKTmode & MODEINITSMSIG) {
454  for (i = 0; i < here->SRCtree->numVars; i++) {
455  here->SRCacValues[i] = here->SRCderivs[i];
456  }
457  here->SRCacValues[here->SRCtree->numVars] = rhs;
458  }
459  }
460 }
#define SRC_I
Definition: srcdefs.h:216
struct sSRCinstance * SRCnextInstance
Definition: srcdefs.h:26
double * SRCibrContBrptr
Definition: srcdefs.h:157
#define SRC_V
Definition: srcdefs.h:217
double * SRCvalues
Definition: srcdefs.h:109
double * SRCibrNegptr
Definition: srcdefs.h:173
unsigned SRCccCoeffGiven
Definition: srcdefs.h:187
double SRCcoeff
Definition: srcdefs.h:71
#define MODEINITPRED
Definition: cktdefs.h:161
#define MODEDC
Definition: cktdefs.h:149
#define MODEDCTRANCURVE
Definition: cktdefs.h:152
int * varTypes
Definition: ifsim.h:160
IFuid SRCcontName
Definition: srcdefs.h:74
int * SRCeqns
Definition: srcdefs.h:118
void(* SRCdcFunc)()
Definition: srcdefs.h:61
double SRCprev
Definition: srcdefs.h:121
int SRCnegNode
Definition: srcdefs.h:38
#define MODEINITFLOAT
Definition: cktdefs.h:156
#define ERR_FATAL
Definition: ifsim.h:518
static void src_func1()
static void src_vcvs()
#define MODEINITTRAN
Definition: cktdefs.h:160
#define M_PI
Definition: spice.h:132
static void src_func0()
IFfrontEnd * SPfrontEnd
Definition: main.c:917
int SRCposNode
Definition: srcdefs.h:35
#define E_BADPARM
Definition: iferrmsg.h:26
double SRCdcValue
Definition: srcdefs.h:77
double * SRCnegContBrptr
Definition: srcdefs.h:145
static void src_dc()
#define IF_INSTANCE
Definition: ifsim.h:112
double * SRCibrContPosptr
Definition: srcdefs.h:149
int SRCstate
Definition: srcdefs.h:32
#define TSTALLOC(ptr, first, second)
Definition: devdefs.h:124
int CKTpreload
Definition: cktdefs.h:170
#define OK
Definition: iferrmsg.h:17
unsigned SRCdcGiven
Definition: srcdefs.h:180
double SRCacReal
Definition: srcdefs.h:85
GENERIC * IFuid
Definition: ifsim.h:72
int SRCbranch
Definition: srcdefs.h:47
Definition: cddefs.h:237
#define MALLOC(x)
Definition: util.h:9
#define NULL
Definition: spdefs.h:121
SRCinstance * SRCinstances
Definition: srcdefs.h:205
#define E_NOMEM
Definition: iferrmsg.h:27
double cos()
IFnode nValue
Definition: ifsim.h:237
int number
Definition: cktdefs.h:39
double * SRCposContPosptr
Definition: srcdefs.h:125
double * SRCnegIbrptr
Definition: srcdefs.h:165
#define MODEINITSMSIG
Definition: cktdefs.h:159
unsigned SRCvcCoeffGiven
Definition: srcdefs.h:188
double * SRCposContNegptr
Definition: srcdefs.h:129
int SRCsetup(SMPmatrix *matrix, GENmodel *inModel, CKTcircuit *ckt, int *states)
Definition: srcset.c:43
#define ERR_WARNING
Definition: ifsim.h:517
double sin()
int SRCcontBranch
Definition: srcdefs.h:50
double * SRCposIbrptr
Definition: srcdefs.h:161
int CKTmkCur()
double ** SRCposptr
Definition: srcdefs.h:53
static char model[32]
Definition: subckt.c:76
unsigned SRCacGiven
Definition: srcdefs.h:181
#define MODEINITFIX
Definition: cktdefs.h:158
#define MY_TSTALLOC(ptr, first, second)
Definition: srcset.c:34
double * SRCacValues
Definition: srcdefs.h:106
int numVars
Definition: ifsim.h:159
double * SRCibrContNegptr
Definition: srcdefs.h:153
double * SRCderivs
Definition: srcdefs.h:115
static void src_ccvs()
static void src_cccs()
int CKTfndBranch()
unsigned SRCacMGiven
Definition: srcdefs.h:182
double * SRCnegContNegptr
Definition: srcdefs.h:137
double SRCvalue
Definition: srcdefs.h:112
double * SRCposContBrptr
Definition: srcdefs.h:141
IFuid SRCname
Definition: srcdefs.h:29
#define SMPmatrix
Definition: smpdefs.h:11
double * SRCibrPosptr
Definition: srcdefs.h:169
static void src_vccs()
int SRCtype
Definition: srcdefs.h:65
#define IF_NODE
Definition: ifsim.h:110
IFparseTree * SRCtree
Definition: srcdefs.h:103
void(* SRCtranFunc)()
Definition: srcdefs.h:60
double SRCacImag
Definition: srcdefs.h:88
#define MODEINITJCT
Definition: cktdefs.h:157
union uIFvalue * vars
Definition: ifsim.h:162
double * SRCnegContPosptr
Definition: srcdefs.h:133
struct sSRCmodel * SRCnextModel
Definition: srcdefs.h:202
IFuid uValue
Definition: ifsim.h:236
unsigned SRCacPGiven
Definition: srcdefs.h:183