⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cplload.c

📁 ngspice又一个电子CAD仿真软件代码.功能更全
💻 C
📖 第 1 页 / 共 2 页
字号:
/**********Copyright 1992 Regents of the University of California.  All rightsreserved.Author: 1992 Charles Hough**********/#include "ngspice.h"#include "cpldefs.h"#include "sperror.h"#include "suffix.h"VI_list *pool_vi;static double ratio[MAX_CP_TX_LINES];static VI_list *new_vi(void);static void free_vi(VI_list*);static int get_pvs_vi();static int update_cnv(CPLine*, float);static int add_new_vi(CPLinstance*, CKTcircuit*, int);static int right_consts(CPLinstance*, CPLine*, int, int, double, double, int*, int*, CKTcircuit*);static int update_delayed_cnv(CPLine*, float);static int multC(double, double, double, double, double*, double*);static int expC(double, double, double, double*, double*);static int divC(double, double, double, double, double*, double*);static void update_cnv_a(TMS*, float, double, double, double, double, double, double);static void copy_cp(CPLine*, CPLine*);/*ARGSUSED*/intCPLload(GENmodel *inModel, CKTcircuit *ckt){        CPLmodel *model = (CPLmodel *)inModel;        CPLinstance *here;	CPLine *cp, *cp2;	int *k, *l;	int time, time2;	double h, h1, f;	int hint;	float hf;	NODE *nd;	double v, v1, g;	int cond1, i;	int noL, m, p, q;	CKTnode *node;	VI_list *vi, *vi_before;	int before, delta;	int resindex;	h = ckt->CKTdelta;	h1 = 0.5 * h;	time2 = (int) (ckt->CKTtime * 1e12);	hint = (int)(h * 1e12);	hf = (float)(h * 1e12);	time = (int) ((ckt->CKTtime - ckt->CKTdelta) * 1e12);	cond1= ckt->CKTmode & MODEDC;	for( ; model != NULL; model = model->CPLnextModel ) {		for (here = model->CPLinstances; here != NULL ;			here=here->CPLnextInstance) {			cp = here->cplines;			if (cond1 || cp->vi_head == NULL) continue;			noL = cp->noL = here->dimension;			if (cp->vi_tail->time > time) {				time = cp->vi_tail->time;				hint = time2 - time;			}					 	before = cp->vi_tail->time;		 	vi_before = cp->vi_tail;			if (time > cp->vi_tail->time) {				copy_cp(cp, here->cplines2);				add_new_vi(here, ckt, time);				delta = time - before;				for (m = 0; m < noL; m++) {					nd = cp->in_node[m];					v = vi_before->v_i[m];					v1 = nd->V = cp->vi_tail->v_i[m];					nd->dv = (v1 - v) / delta;				}				for (m = 0; m < noL; m++) {					nd = cp->out_node[m];					v = vi_before->v_o[m];					v1 = nd->V = cp->vi_tail->v_o[m];					nd->dv = (v1 - v) / delta;				}				update_cnv(cp, (float)delta);				if (cp->ext) update_delayed_cnv(cp, (float)delta);			}		}	}	model = (CPLmodel *)inModel;    /*  loop through all the models */    for( ; model != NULL; model = model->CPLnextModel ) {        /* loop through all the instances of the model */        for (here = model->CPLinstances; here != NULL ; 			here=here->CPLnextInstance) { 			double mintaul = 123456789.0;			cp = here->cplines;			cp2 = here->cplines2;			for (i = 0; i < cp->noL; i++) {				if (mintaul > cp->taul[i]) mintaul = cp->taul[i];			}			if (mintaul < hf) {						fprintf(stderr, "your time step is too large for tau.\n");	fprintf(stderr, "please decrease max time step in .tran card.\n");	fprintf(stderr, ".tran tstep tstop tstart tmax.\n");	fprintf(stderr, "make tmax smaller than %e and try again.\n", 		mintaul * 1e-12);	return (1111);			}			noL = cp->noL = here->dimension;			if (cond1) {				resindex = 0;				for (m = 0; m < noL; m++) {					if (here->CPLlengthGiven)						g = model->Rm[resindex] * here->CPLlength;					else g = model->Rm[resindex] * here->CPLmodPtr->length;					*(here->CPLposIbr1[m]) += 1.0;					*(here->CPLnegIbr2[m]) += 1.0;					*(here->CPLibr1Ibr1[m]) += 1.0;					*(here->CPLibr1Ibr2[m][m]) += 1.0;					*(here->CPLibr2Pos[m][m]) += 1.0;					*(here->CPLibr2Neg[m][m]) -= 1.0;					*(here->CPLibr2Ibr1[m][m]) -= g;					resindex = resindex + noL - m;				}				continue;			}			/* dc setup */			if (here->CPLdcGiven == 0 && !cond1) {				for (i = 0; i < cp->noL; i++) {					nd = cp->in_node[i];					for(node = ckt->CKTnodes;node; node = node->next) {						if (strcmp(nd->name->id, node->name) == 0) {							cp->dc1[i] = ckt->CKTrhsOld[node->number];							cp2->dc1[i] = nd->V = cp->dc1[i];							break;						}					}					nd = cp->out_node[i];					for(node = ckt->CKTnodes;node; node = node->next) {						if (strcmp(nd->name->id, node->name) == 0) {							cp->dc2[i] = ckt->CKTrhsOld[node->number];							cp2->dc2[i] = nd->V = cp->dc2[i];							break;						}					}				}				here->CPLdcGiven = 1;				vi = new_vi();				vi->time = 0;				{				int i, j, k, l;				for (i = 0; i < cp->noL; i++) {					for (j = 0; j < cp->noL; j++) {						TMS *tms;						double a, b;						tms = cp->h1t[i][j];						if (tms->ifImg) {							tms->tm[0].cnv_i = - cp->dc1[j] *								tms->tm[0].c / tms->tm[0].x;							tms->tm[0].cnv_o = - cp->dc2[j] *								tms->tm[0].c / tms->tm[0].x;							divC(tms->tm[1].c, tms->tm[2].c,								tms->tm[1].x, tms->tm[2].x, &a, &b);							tms->tm[1].cnv_i = - cp->dc1[j] * a;							tms->tm[1].cnv_o = - cp->dc2[j] * a;							tms->tm[2].cnv_i = - cp->dc1[j] * b;							tms->tm[2].cnv_o = - cp->dc2[j] * b;						} else							for (k = 0; k < 3; k++) {								tms->tm[k].cnv_i = - cp->dc1[j] *								   	tms->tm[k].c / tms->tm[k].x;								tms->tm[k].cnv_o = - cp->dc2[j] *								  	tms->tm[k].c / tms->tm[k].x;							}						for (l = 0; l < cp->noL; l++) {							tms = cp->h2t[i][j][l];							for (k = 0; k < 3; k++) {								tms->tm[k].cnv_i = 0.0;								tms->tm[k].cnv_o = 0.0; 							}						}						for (l = 0; l < cp->noL; l++) {							tms = cp->h3t[i][j][l];							if (tms->ifImg) {								tms->tm[0].cnv_i = - cp->dc1[j] *									tms->tm[0].c / tms->tm[0].x;								tms->tm[0].cnv_o = - cp->dc2[j] *									tms->tm[0].c / tms->tm[0].x;								divC(tms->tm[1].c, tms->tm[2].c,									tms->tm[1].x, tms->tm[2].x, &a, &b);								tms->tm[1].cnv_i = - cp->dc1[j] * a;								tms->tm[1].cnv_o = - cp->dc2[j] * a;								tms->tm[2].cnv_i = - cp->dc1[j] * b;								tms->tm[2].cnv_o = - cp->dc2[j] * b;							} else								for (k = 0; k < 3; k++) {									tms->tm[k].cnv_i = - cp->dc1[j] * 										tms->tm[k].c / tms->tm[k].x; 									tms->tm[k].cnv_o = - cp->dc2[j] * 										tms->tm[k].c / tms->tm[k].x;								}						}					}					for (i = 0; i < cp->noL; i++) {						vi->i_i[i] = vi->i_o[i] = 0.0;						vi->v_i[i] = cp->dc1[i];						vi->v_o[i] = cp->dc2[i];					}				}				vi->next = NULL;				cp->vi_tail = vi;				cp->vi_head = vi;				cp2->vi_tail = vi;				cp2->vi_head = vi;				}			}			for (m = 0; m < noL; m++) {				*(here->CPLibr1Ibr1[m]) = -1.0;				*(here->CPLibr2Ibr2[m]) = -1.0;			}			for (m = 0; m < noL; m++) {				*(here->CPLposIbr1[m]) = 1.0;				*(here->CPLnegIbr2[m]) = 1.0;			}			for (m = 0; m < noL; m++) {				for (p = 0; p < noL; p++) {					*(here->CPLibr1Pos[m][p]) =						cp->h1t[m][p]->aten + h1 * cp->h1C[m][p];					*(here->CPLibr2Neg[m][p]) =						cp->h1t[m][p]->aten + h1 * cp->h1C[m][p];				}			}			k = here->CPLibr1;			l = here->CPLibr2;			copy_cp(cp2, cp);			if (right_consts(here,cp2, time,time2,h,h1,k,l,ckt)) {				cp2->ext = 1;				for (q = 0; q < noL; q++) {					cp->ratio[q] = ratio[q];					if (ratio[q] > 0.0) {						for (m = 0; m < noL; m++) {							for (p = 0; p < noL; p++) {					          			if (cp->h3t[m][p][q]) {             			f = ratio[q] * (h1 * cp->h3C[m][p][q] +                			cp->h3t[m][p][q]->aten);						*(here->CPLibr1Neg[m][p]) = -f;						*(here->CPLibr2Pos[m][p]) = -f;          			}          			if (cp->h2t[m][p][q]) {             			f = ratio[q] * (h1 * cp->h2C[m][p][q] +                			cp->h2t[m][p][q]->aten);						*(here->CPLibr1Ibr2[m][p]) = -f;						*(here->CPLibr2Ibr1[m][p]) = -f;          			}           					}      					}					}				}			}   			else cp->ext = 0;     	}	}    return(OK);}static voidcopy_cp(CPLine *new, CPLine *old){	int i, j, k, l, m;	VI_list *temp;	new->noL = m = old->noL;	new->ext = old->ext;	for (i = 0; i < m; i++) {		new->ratio[i] = old->ratio[i];		new->taul[i] = old->taul[i];		for (j = 0; j < m; j++) {			if (new->h1t[i][j] == NULL)				new->h1t[i][j] = (TMS *) malloc(sizeof (TMS));			new->h1t[i][j]->ifImg = old->h1t[i][j]->ifImg;			new->h1t[i][j]->aten = old->h1t[i][j]->aten;			new->h1C[i][j] = old->h1C[i][j];			for (k = 0; k < 3; k++) {				new->h1t[i][j]->tm[k].c = old->h1t[i][j]->tm[k].c;				new->h1t[i][j]->tm[k].x = old->h1t[i][j]->tm[k].x;				new->h1t[i][j]->tm[k].cnv_i = old->h1t[i][j]->tm[k].cnv_i;				new->h1t[i][j]->tm[k].cnv_o = old->h1t[i][j]->tm[k].cnv_o;				new->h1e[i][j][k] = old->h1e[i][j][k];			}			for (l = 0; l < m; l++) {				if (new->h2t[i][j][l] == NULL)					new->h2t[i][j][l] = (TMS *) malloc(sizeof (TMS));				new->h2t[i][j][l]->ifImg = old->h2t[i][j][l]->ifImg;				new->h2t[i][j][l]->aten = old->h2t[i][j][l]->aten;				new->h2C[i][j][l] = old->h2C[i][j][l];				new->h3C[i][j][l] = old->h3C[i][j][l];				for (k = 0; k < 3; k++) {					new->h2t[i][j][l]->tm[k].c = old->h2t[i][j][l]->tm[k].c;					new->h2t[i][j][l]->tm[k].x = old->h2t[i][j][l]->tm[k].x;					new->h2t[i][j][l]->tm[k].cnv_i 						= old->h2t[i][j][l]->tm[k].cnv_i;					new->h2t[i][j][l]->tm[k].cnv_o 						= old->h2t[i][j][l]->tm[k].cnv_o;				}				if (new->h3t[i][j][l] == NULL)					new->h3t[i][j][l] = (TMS *) malloc(sizeof (TMS));				new->h3t[i][j][l]->ifImg = old->h3t[i][j][l]->ifImg;				new->h3t[i][j][l]->aten = old->h3t[i][j][l]->aten;				for (k = 0; k < 3; k++) {					new->h3t[i][j][l]->tm[k].c = old->h3t[i][j][l]->tm[k].c;					new->h3t[i][j][l]->tm[k].x = old->h3t[i][j][l]->tm[k].x;					new->h3t[i][j][l]->tm[k].cnv_i 						= old->h3t[i][j][l]->tm[k].cnv_i;					new->h3t[i][j][l]->tm[k].cnv_o 						= old->h3t[i][j][l]->tm[k].cnv_o;				}			}		}	}	while (new->vi_head->time < old->vi_head->time) {		temp = new->vi_head;		new->vi_head = new->vi_head->next;		free_vi(temp);	}}static intright_consts(CPLinstance *here, CPLine *cp, int t, int time, double h, double h1,              int *l1, int *l2, CKTcircuit *ckt){   int i, j, k, l;   double e;   double ff[MAX_CP_TX_LINES], gg[MAX_CP_TX_LINES];   double v1_i[MAX_CP_TX_LINES][MAX_CP_TX_LINES];   double v2_i[MAX_CP_TX_LINES][MAX_CP_TX_LINES];   double i1_i[MAX_CP_TX_LINES][MAX_CP_TX_LINES];   double i2_i[MAX_CP_TX_LINES][MAX_CP_TX_LINES];    double v1_o[MAX_CP_TX_LINES][MAX_CP_TX_LINES];    double v2_o[MAX_CP_TX_LINES][MAX_CP_TX_LINES];   double i1_o[MAX_CP_TX_LINES][MAX_CP_TX_LINES];    double i2_o[MAX_CP_TX_LINES][MAX_CP_TX_LINES];   int ext;   int noL;   noL = cp->noL;   for (j = 0; j < noL; j++) {       double ff1;      ff[j] = 0.0;      gg[j] = 0.0;      for (k = 0; k < noL; k++)      if (cp->h1t[j][k]) {        if (cp->h1t[j][k]->ifImg) {           double er, ei, a, b, a1, b1;           TMS *tms;           tms = cp->h1t[j][k];           cp->h1e[j][k][0] = e = exp((double) tms->tm[0].x * h);           expC(tms->tm[1].x, tms->tm[2].x, h, &er, &ei);           cp->h1e[j][k][1] = er;           cp->h1e[j][k][2] = ei;           ff1 = tms->tm[0].c * e * h1;           ff[j]  -= tms->tm[0].cnv_i * e;           gg[j]  -= tms->tm[0].cnv_o * e;           ff[j]  -= ff1 * cp->in_node[k]->V;           gg[j]  -= ff1 * cp->out_node[k]->V;           multC(tms->tm[1].c, tms->tm[2].c, er, ei, &a1, &b1);           multC(tms->tm[1].cnv_i, tms->tm[2].cnv_i, er, ei, &a, &b);           ff[j] -= 2.0 * (a1 * h1 * cp->in_node[k]->V + a);           multC(tms->tm[1].cnv_o, tms->tm[2].cnv_o, er, ei, &a, &b);           gg[j] -= 2.0 * (a1 * h1 * cp->out_node[k]->V + a);        } else {           ff1 = 0.0;           for (i = 0; i < 3; i++) {          cp->h1e[j][k][i] = e = exp((double) cp->h1t[j][k]->tm[i].x * h);          ff1 -= cp->h1t[j][k]->tm[i].c * e;          ff[j]  -= cp->h1t[j][k]->tm[i].cnv_i * e;          gg[j]  -= cp->h1t[j][k]->tm[i].cnv_o * e;           }           ff[j] += ff1 * h1 * cp->in_node[k]->V;           gg[j] += ff1 * h1 * cp->out_node[k]->V;        }     }   }   ext = get_pvs_vi(t, time, cp, v1_i, v2_i, i1_i, i2_i,          v1_o, v2_o, i1_o, i2_o);   for (j  = 0; j < noL; j++) {       /**  current eqn  **/       TERM *tm;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -