/* * SMPreorder() */ int SMPreorder(SMPmatrix *Matrix, double PivTol, double PivRel, double Gmin) { spSetReal( (void *)Matrix ); LoadGmin( (void *)Matrix, Gmin ); return spOrderAndFactor( (void *)Matrix, (spREAL*)NULL, (spREAL)PivRel, (spREAL)PivTol, YES ); }
/* * SMPcReorder() */ int SMPcReorder(SMPmatrix *Matrix, double PivTol, double PivRel, int *NumSwaps) { *NumSwaps = 1; spSetComplex( (void *)Matrix ); return spOrderAndFactor( (void *)Matrix, (spREAL*)NULL, (spREAL)PivRel, (spREAL)PivTol, YES ); }
int StanfordSolveSparseMatrix(Kentry* Kentries, double b[], int numUniqueEntries, int size, double soln[]) { int i; int row; int col; double value; spMatrix A; spError err, Error; spREAL AbsThreshold,RelThreshold; spREAL *pElement; #ifdef REALLY_OUTPUT_A_WHOLE_BUNCH FILE *fp; fp = NULL; fp = fopen("nonzero-inside-solver-call","w"); fprintf(fp,"title goes here (%i nonzero)\n",numUniqueEntries); fprintf(fp,"%i real\n",size); for (i = 0; i < numUniqueEntries; i++) { /* we add 1 to row and col numbers for sparse */ fprintf(fp,"%i %i %lf\n",Kentries[i].row+SPARSE_OFFSET,Kentries[i].col+SPARSE_OFFSET, Kentries[i].value); } fprintf(fp,"0 0 0.0\n"); for (i = 0; i < size; i++) { fprintf(fp,"%lf\n",b[i+SPARSE_OFFSET]); } fclose(fp); #endif /* create the matrix */ debugprint(stddbg," allocate A matrix.\n"); fflush(stdout); A = spCreate(size, 0, &err); if( err >= spFATAL || A == NULL) { fprintf(stderr,"error allocating matrix.\n"); exit(-1); } for (i = 0; i < numUniqueEntries; i++) { if (!(i % (int)(numUniqueEntries/50.0))) { debugprint(stddbg,"inserting into A: %i of %i\n",i,numUniqueEntries); } row = Kentries[i].row+SPARSE_OFFSET; col = Kentries[i].col+SPARSE_OFFSET; value = Kentries[i].value; pElement = spGetElement(A,row,col); if (pElement == NULL) { fprintf(stderr, "error: insufficient memory available.\n"); exit(-1); } *pElement = value; } debugprint(stddbg," free memory for Kentries\n"); deleteKentries(Kentries); spSetReal( A ); #if MODIFIED_NODAL spMNA_Preorder( A ); #endif RelThreshold = 0; AbsThreshold = 0; debugprint(stddbg," order and factor matrix.\n"); Error = spOrderAndFactor( A, b, RelThreshold, AbsThreshold, 1 ); if ( Error >= spFATAL ) { fprintf(stdout,"Fatal error (%i)\n",Error); exit(-1); } /* spPrint( A,1,1,1); */ for (i = 0; i <= size; i++) { soln[0] = 0; } debugprint(stddbg," call spSolve.\n"); spSolve( A, b, soln); debugprint(stddbg," destroy A.\n"); spDestroy(A); return 0; }
int main (int argc, char **argv) { int ch; int errflg=0,i,j; double l,c,ctot,r=0.0,g=0.0,k=0.0,lm=0.0,cm=0.0,len; unsigned gotl=0,gotc=0,gotr=0,gotg=0,gotk=0,gotcm=0,gotlen=0; unsigned gotname=0, gotnum=0; char *name = ""; double **matrix, **inverse; double *tpeigenvalues, *gammaj; char *options; int num, node; char **pname, *s; int use_opt; char *optarg; pname = argv; argv++; argc--; ch = 0; while (argc > 0) { s = *argv++; argc--; while ((ch = *s++)) { if (*s) optarg = s; else if (argc) optarg = *argv; else optarg = NULL; use_opt = 0; switch (ch) { case 'o': name = (char *) tmalloc((unsigned) (strlen(optarg)*sizeof(char))); (void) strcpy(name,optarg); gotname=1; use_opt = 1; break; case 'l': sscanf(optarg,"%lf",&l); gotl=1; use_opt = 1; break; case 'c': sscanf(optarg,"%lf",&c); gotc=1; use_opt = 1; break; case 'r': sscanf(optarg,"%lf",&r); use_opt = 1; gotr=1; break; case 'g': sscanf(optarg,"%lf",&g); use_opt = 1; gotg=1; break; case 'k': sscanf(optarg,"%lf",&k); use_opt = 1; gotk=1; break; case 'x': sscanf(optarg,"%lf",&cm); use_opt = 1; gotcm=1; break; case 'L': sscanf(optarg,"%lf",&len); use_opt = 1; gotlen=1; break; case 'n': sscanf(optarg,"%d",&num); use_opt = 1; gotnum=1; break; case 'h': usage(pname); exit(1); break; case '-': break; default: usage(pname); exit(2); break; } if (use_opt) { if (optarg == s) s += strlen(s); else if (optarg) { argc--; argv++; } } } } if (errflg) { usage(argv); exit (2); } if (gotl + gotc + gotname + gotnum + gotlen < 5) { fprintf(stderr,"l, c, model_name, number_of_conductors and length must be specified.\n"); fprintf(stderr,"%s -u for details.\n",pname[0]); fflush(stdout); exit(1); } if ( (k<0.0?-k:k) >=1.0 ) { fprintf(stderr,"Error: |k| must be less than 1.0\n"); fflush(stderr); exit(1); } if (num == 1) { fprintf(stdout,"* single conductor line\n"); fflush(stdout); exit(1); } lm = l*k; switch(num) { case 1: ctot = c; break; case 2: ctot = c + cm; break; default: ctot = c + 2*cm; break; } comments(r,l,g,c,ctot,cm,lm,k,name,num,len); matrix = (double **) tmalloc((unsigned) (sizeof(double*)*(num+1))); inverse = (double **) tmalloc((unsigned) (sizeof(double*)*(num+1))); tpeigenvalues = (double *) tmalloc((unsigned) (sizeof(double)*(num+1))); for (i=1;i<=num;i++) { matrix[i] = (double *) tmalloc((unsigned) (sizeof(double)*(num+1))); inverse[i] = (double *) tmalloc((unsigned) (sizeof(double)*(num+1))); } for (i=1;i<=num;i++) { tpeigenvalues[i] = -2.0 * cos(M_PI*i/(num+1)); } for (i=1;i<=num;i++) { for (j=1;j<=num;j++) { matrix[i][j] = phi(i-1,tpeigenvalues[j]); } } gammaj = (double *) tmalloc((unsigned) (sizeof(double)*(num+1))); for (j=1;j<=num;j++) { gammaj[j] = 0.0; for (i=1;i<=num;i++) { gammaj[j] += matrix[i][j] * matrix[i][j]; } gammaj[j] = sqrt(gammaj[j]); } for (j=1;j<=num;j++) { for (i=1;i<=num; i++) { matrix[i][j] /= gammaj[j]; } } tfree(gammaj); /* matrix = M set up */ { MatrixPtr othermatrix; double *rhs, *solution; double *irhs, *isolution; int errflg, err, singular_row, singular_col; double *elptr; rhs = (double *) tmalloc((unsigned) (sizeof(double)*(num+1))); irhs = (double *) tmalloc((unsigned) (sizeof(double)*(num+1))); solution = (double *) tmalloc((unsigned) (sizeof(double)*(num+1))); isolution = (double *) tmalloc((unsigned) (sizeof(double)*(num+1))); othermatrix = spCreate(num,0,&errflg); for (i=1;i<=num;i++) { for (j=1; j<=num; j++) { elptr = spGetElement(othermatrix,i,j); *elptr = matrix[i][j]; } } #ifdef DEBUG_LEVEL1 (void) spPrint(othermatrix,0,1,0); #endif for (i=1;i<=num;i++) rhs[i] = 0.0; rhs[1]=1.0; err = spOrderAndFactor(othermatrix,rhs,THRSH,ABS_THRSH,DIAG_PIVOTING); spErrorMessage(othermatrix,stderr,NULL); switch(err) { case spNO_MEMORY: fprintf(stderr,"No memory in spOrderAndFactor\n"); fflush(stderr); exit(1); case spSINGULAR: (void) spWhereSingular(othermatrix,&singular_row,&singular_col); fprintf(stderr,"Singular matrix: problem in row %d and col %d\n", singular_row, singular_col); fflush(stderr); exit(1); default: break; } for (i=1;i<=num;i++) { for (j=1;j<=num;j++) { rhs[j] = (j==i?1.0:0.0); irhs[j] = 0.0; } (void) spSolveTransposed(othermatrix,rhs,solution, irhs, isolution); for (j=1;j<=num;j++) { inverse[i][j] = solution[j]; } } tfree(rhs); tfree(solution); } /* inverse = M^{-1} set up */ fprintf(stdout,"\n"); fprintf(stdout,"* Lossy line models\n"); options = (char *) tmalloc((unsigned) 256); (void) strcpy(options,"rel=1.2 nocontrol"); for (i=1;i<=num;i++) { fprintf(stdout,".model mod%d_%s ltra %s r=%0.12g l=%0.12g g=%0.12g c=%0.12g len=%0.12g\n", i,name,options,r,l+tpeigenvalues[i]*lm,g,ctot-tpeigenvalues[i]*cm,len); /*i,name,options,r,l+tpeigenvalues[i]*lm,g,ctot+tpeigenvalues[i]*cm,len);*/ } fprintf(stdout,"\n"); fprintf(stdout,"* subcircuit m_%s - modal transformation network for %s\n",name,name); fprintf(stdout,".subckt m_%s", name); for (i=1;i<= 2*num; i++) { fprintf(stdout," %d",i); } fprintf(stdout,"\n"); for (j=1;j<=num;j++) fprintf(stdout,"v%d %d 0 0v\n",j,j+2*num); for (j=1;j<=num;j++) { for (i=1; i<=num; i++) { fprintf(stdout,"f%d 0 %d v%d %0.12g\n", (j-1)*num+i,num+j,i,inverse[j][i]); } } node = 3*num+1; for (j=1;j<=num;j++) { fprintf(stdout,"e%d %d %d %d 0 %0.12g\n", (j-1)*num+1, node, 2*num+j, num+1, matrix[j][1]); node++; for (i=2; i<num; i++) { fprintf(stdout,"e%d %d %d %d 0 %0.12g\n", (j-1)*num+i, node,node-1,num+i,matrix[j][i]); node++; } fprintf(stdout,"e%d %d %d %d 0 %0.12g\n", j*num,j,node-1, 2*num,matrix[j][num]); } fprintf(stdout,".ends m_%s\n",name); fprintf(stdout,"\n"); fprintf(stdout,"* Subckt %s\n", name); fprintf(stdout,".subckt %s",name); for (i=1;i<=2*num;i++) { fprintf(stdout," %d",i); } fprintf(stdout,"\n"); fprintf(stdout,"x1"); for (i=1;i<=num;i++) fprintf(stdout," %d", i); for (i=1;i<=num;i++) fprintf(stdout," %d", 2*num+i); fprintf(stdout," m_%s\n",name); for (i=1;i<=num;i++) fprintf(stdout,"o%d %d 0 %d 0 mod%d_%s\n",i,2*num+i,3*num+i,i,name); fprintf(stdout,"x2"); for (i=1;i<=num;i++) fprintf(stdout," %d", num+i); for (i=1;i<=num;i++) fprintf(stdout," %d", 3*num+i); fprintf(stdout," m_%s\n",name); fprintf(stdout,".ends %s\n",name); tfree(tpeigenvalues); for (i=1;i<=num;i++) { tfree(matrix[i]); tfree(inverse[i]); } tfree(matrix); tfree(inverse); tfree(name); tfree(options); return EXIT_NORMAL; }
int main(int argc, char* argv[]) { int i; char title[1024]; char line[1024]; int size; int row; int col; double value; spMatrix A; spREAL x[4096]; spREAL b[4096]; spError err, Error; spREAL AbsThreshold,RelThreshold; spREAL *pElement; FILE *fp = NULL; if (argc != 2) { fprintf(stderr,"usage: sptest <matrix_filename>\n"); exit(-1); } fprintf(stdout,"Reading file [%s]\n",argv[1]); if ((fp = fopen(argv[1],"r")) == NULL) { fprintf(stderr,"Error opening file [%s]\n",argv[1]); exit(-1); } title[0]='\0'; fgets(title,1024,fp); fgets(line,1024,fp); if (sscanf(line,"%i real",&size) != 1) { fprintf(stderr,"Error reading size.\n"); exit(-1); } // create the matrix A = spCreate(size, 0, &err); if( err >= spFATAL || A == NULL) { fprintf(stderr,"error allocating matrix.\n"); exit(-1); } while (0 == 0) { line[0]='\0'; fgets(line,1024,fp); if (sscanf(line,"%i %i %lf",&row,&col,&value) != 3) { fprintf(stderr,"Error reading matrix.\n"); exit(-1); } if (row == 0 && col == 0) break; //spElement *pElement; pElement = spGetElement(A,row,col); if (pElement == NULL) { fprintf(stderr, "error: insufficient memory available.\n"); exit(-1); } *pElement = value; pElement = spGetElement(A,row,col); } b[0] = 0; for (i = 1; i <= size; i++) { line[0]='\0'; fgets(line,1024,fp); if (sscanf(line,"%lf",&value) != 1) { fprintf(stderr,"Error reading RHS.\n"); exit(-1); } b[i] = value; } spSetReal( A ); /*spPrint( A,1,1,1);*/ #if MODIFIED_NODAL spMNA_Preorder( A ); #endif RelThreshold = 0; AbsThreshold = 0; Error = spOrderAndFactor( A, b, RelThreshold, AbsThreshold, 1 ); if ( Error >= spFATAL ) { fprintf(stdout,"Fatal error (%i)\n",Error); exit(-1); } /*spPrint( A,1,1,1);*/ for (i = 0; i <= size; i++) { x[0] = 0; } spSolve( A, b, x); /* Print the Solution. */ for (i = 1; i <= size; i++) { fprintf(stdout,"diplacement[%i] = %lg\n",i,x[i]); } spDestroy(A); return 0; }