[BACK]Return to phc6.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / phc

Annotation of OpenXM/src/phc/phc6.c, Revision 1.5

1.1       maekawa     1: /*  phc6.c ,  yama:1999/sm1-prog/phc6.c */
1.5     ! takayama    2: /* $OpenXM: OpenXM/src/phc/phc6.c,v 1.4 2002/05/02 02:51:37 takayama Exp $ */
1.1       maekawa     3: /* This is a simple C interface to the black-box solver of phc.
                      4: ** Requirements:
                      5: **  1) executable version of phc will be searched in the following order:
                      6: **    OpenXM_HOME/bin/, /usr/local/bin/phc, /tmp/phc, your command search path.
                      7: **     Here, PHC_LIBDIR is an environment variable.
                      8: **  2) user of this program has write permissions to create
                      9: **     the files "tmp.input.xx" and "tmp.output.xx" in the directory where
                     10: **     this program is executed.  xx are numbers.
                     11: */
                     12:
                     13: #include <stdio.h>
                     14: #include <sys/stat.h>
                     15: #include <unistd.h>
                     16: #include <stdlib.h>
                     17:
                     18: /* Definition of class identifiers. */
                     19: #define Snull             0
                     20: #define SstringObject     5
                     21: #define Sarray            6
                     22: #define SlongdoubleComplex  601
                     23:
                     24: /* Definition of Object */
                     25: union cell {
                     26:   int ival;
                     27:   char *str;
                     28:   struct phc_object *op;
1.4       takayama   29:   double longdouble;
1.1       maekawa    30: };
                     31: struct phc_object{
                     32:   int tag;                /* class identifier */
                     33:   union cell lc;          /* left cell */
                     34:   union cell rc;          /* right cell */
                     35: };
                     36:
                     37: /* Memory allocation function.
                     38:    Use your favorite memory allocation function.
                     39:    I recommend not to use malloc and to use gc4.14 for large applications. */
                     40: #define sGC_malloc(n)   malloc(n)
                     41:
                     42: /********** macros to use Sarray **************/
                     43: /* put to Object Array */
                     44: #define phc_putoa(ob,i,cc) {\
1.4       takayama   45:                    if ((ob).tag != Sarray) {fprintf(stderr,"Warning: PUTOA is for an array of objects\n");} else \
                     46:                      {if ((0 <= (i)) && ((i) < (ob).lc.ival)) {\
                     47:                       (ob.rc.op)[i] = cc;\
                     48:                     }else{\
                     49:                        fprintf(stderr,"Warning: PUTOA, the size is %d.\n",(ob).lc.ival);\
                     50:                   }}}
1.1       maekawa    51: #define phc_getoa(ob,i) ((ob.rc.op)[i])
                     52: #define phc_getoaSize(ob) ((ob).lc.ival)
                     53:
                     54: /* prototypes */
                     55: struct phc_object phc_newObjectArray(int size);
                     56: void phc_printObject(FILE *fp,struct phc_object ob);
                     57: char *phc_generateUniqueFileName(char *s);
                     58: char *phc_which(char *s);   /* search a path for the file s */
1.4       takayama   59: struct phc_object phc_complexTo(double r, double i);
1.1       maekawa    60:
                     61:
1.2       takayama   62: int phc_scan_for_string(FILE *fp, char str[]);
1.1       maekawa    63: struct phc_object phc_scan_solutions(FILE *fp, int npaths, int dim );
                     64: struct phc_object phc_scan_output_of_phc(char *fname);
                     65: struct phc_object phc_call_phc(char *sys);
                     66:
                     67: int phc_verbose = 0;
                     68: int phc_overwrite = 1;  /* Always use tmp.input.0 and tmp.output.0
1.4       takayama   69:                            for work files. */
1.1       maekawa    70:
                     71: main(int argc, char *argv[]) {
                     72:   struct phc_object ob;
                     73:   int n,i,dim;
                     74: #define INPUTSIZE 4096
                     75:   char input[INPUTSIZE];
1.4       takayama   76:   char fname[INPUTSIZE];
1.1       maekawa    77: #define A_SIZE 1024
                     78:   char a[A_SIZE];
                     79:   int message = 0;
                     80:   for (i=1; i<argc; i++) {
                     81:     if (strcmp(argv[i],"-v") == 0) {
1.2       takayama   82:       phc_verbose = 1; message=1;
1.1       maekawa    83:     }else if (strcmp(argv[i],"-g") == 0) {
                     84:       phc_overwrite = 0;
1.4       takayama   85:     }else if (strcmp(argv[i],"-file") == 0) {
                     86:       /* For debugging. */
                     87:       i++;
                     88:       strncpy(fname,argv[i],INPUTSIZE-1);
                     89:       ob = phc_scan_output_of_phc(fname);
                     90:       n = phc_getoaSize(ob);
                     91:       printf("[\n");
                     92:       for (i=0; i<n; i++) {
                     93:         phc_printObject(stdout,phc_getoa(ob,i));
                     94:         if (i != n-1) printf(" ,\n"); else printf(" \n");
                     95:       }
                     96:       printf("]\n");
                     97:       exit(0);
1.1       maekawa    98:     }else if (strcmp(argv[i],"-i") == 0) {
                     99:       ob = phc_call_phc(argv[i+1]);
                    100:       n = phc_getoaSize(ob);
                    101:       printf("[\n");
                    102:       for (i=0; i<n; i++) {
1.4       takayama  103:         phc_printObject(stdout,phc_getoa(ob,i));
                    104:         if (i != n-1) printf(" ,\n"); else printf(" \n");
1.1       maekawa   105:       }
                    106:       printf("]\n");
                    107:       exit(0);
                    108:     }
                    109:   }
                    110:   if (message) {
                    111:     printf("Input example:\n 2 \n x**2 + y**2 - 1;\n x**2 + y**2 - 8*x - 3;\n");
                    112:     printf("Note that input length is limited.\n");
                    113:   }
                    114:   while (1) {
                    115:     if (message) printf("dim= ");
1.4       takayama  116:     if (fgets(input,INPUTSIZE,stdin) <= 0) break;
1.2       takayama  117:     sscanf(input,"%d",&dim);
1.1       maekawa   118:     sprintf(input,"%d\n",dim);
                    119:     if (message) printf("Input %d equations please.\n",dim);
                    120:     for (i=0; i<dim; i++) {
1.4       takayama  121:       if (message) {printf("eq[%d] = ",i);  fflush(stdout);}
                    122:       do {
                    123:         fgets(a,A_SIZE-1, stdin);
                    124:       } while (strlen(a) == 0);
                    125:       if (strlen(a) >= A_SIZE-3) {
                    126:         fprintf(stderr,"Too big input for the input buffer a.\n"); exit(10);
                    127:       }
                    128:       if (strlen(input)+strlen(a) >= INPUTSIZE) {
                    129:         fprintf(stderr,"Too big input for the input buffer input.\n"); exit(10);
                    130:       }
                    131:       sprintf(input+strlen(input),"%s\n",a);
1.1       maekawa   132:     }
                    133:     ob = phc_call_phc(input);
                    134:     if (message) {
                    135:       printf("-----------------------------------------------------------\n");
                    136:     }
                    137:     n = phc_getoaSize(ob);
                    138:     printf("[\n");
                    139:     for (i=0; i<n; i++) {
                    140:       phc_printObject(stdout,phc_getoa(ob,i));
                    141:       if (i != n-1) printf(" ,\n"); else printf(" \n");
                    142:     }
                    143:     printf("]\n");
                    144:   }
                    145: }
                    146:
1.4       takayama  147: int phc_scan_for_string(FILE *fp, char s[])
                    148:      /*
                    149:   **  Scans the file fp for a certain string str of length lenstr+1.
                    150:   **  Reading stops when the string has been found, then the variable
                    151:   **  on return equals 1, otherwise 0 is returned.
                    152:   */
                    153: #define BUF_SIZE 1024
                    154: {
                    155:   char buf[BUF_SIZE];
                    156:   int pt,n,c,i;
                    157:   pt = 0;
                    158:   n=strlen(s);
                    159:   if (n > BUF_SIZE-2) {
                    160:        fprintf(stderr,"Too long string for scan_for_string.\n");
                    161:        exit(1);
                    162:   }
                    163:   for (i=0; i<n; i++) {
                    164:        buf[i] = fgetc(fp); buf[i+1]='\0';
                    165:        if (buf[i] == EOF) return 0;
                    166:   }
                    167:   if (strcmp(s,buf) == 0) return 0;
                    168:   while ((c=fgetc(fp)) != EOF) {
                    169:        for (i=0; i<n; i++) {
                    170:          buf[i] = buf[i+1];
                    171:        }
                    172:        buf[n-1]=c; buf[n] = '\0';
                    173:        if (strcmp(s,buf) == 0) return 1;
                    174:   }
                    175:   return 0;
                    176: }
                    177:
                    178: int phc_scan_for_string_old(FILE *fp, char str[])
1.1       maekawa   179:      /*
                    180:   **  Scans the file fp for a certain string str of length lenstr+1.
                    181:   **  Reading stops when the string has been found, then the variable
                    182:   **  on return equals 1, otherwise 0 is returned.
                    183:   */
                    184: {
1.2       takayama  185: #define BUF_SIZE 1024
                    186:   char buf[BUF_SIZE];
1.1       maekawa   187:   char ch;
                    188:   int index,i,compare,npaths,dim,found;
1.2       takayama  189:   int lenstr;
                    190:   lenstr = strlen(str);
                    191:   if (lenstr >= BUF_SIZE-1) {
1.4       takayama  192:     fprintf(stderr,"Too long string in phc_scan_for_string\n");
                    193:     exit(-1);
1.2       takayama  194:   }
1.1       maekawa   195:   index = -1;
                    196:   found = 0;
1.2       takayama  197:   while (((ch = fgetc(fp))!=EOF) && found == 0)
1.4       takayama  198:     {
                    199:       if (index == -1 && ch == str[0])
                    200:         {
                    201:           index = 0;
                    202:           buf[index] = ch;
                    203:         }
                    204:       else
                    205:         {
                    206:           if (index == lenstr)
                    207:             {
                    208:               compare = 0;
                    209:               for (i=0; str[i] != '\0'; i++)
                    210:                 {
                    211:                   if (buf[i]!=str[i])
                    212:                     {
                    213:                       compare = compare+1;
                    214:                     }
                    215:                 }
                    216:               if (compare == 0)
                    217:                 {
                    218:                   found = 1;
                    219:                 }
                    220:               index = -1;
                    221:             }
                    222:           else
                    223:             if (index > -1 && index < lenstr)
                    224:               {
                    225:                 index = index+1;
                    226:                 buf[index] = ch;
                    227:               }
                    228:         }
                    229:       if (found == 1) break;
                    230:     }
1.1       maekawa   231:   return found;
                    232: }
                    233: struct phc_object phc_scan_solutions(FILE *fp, int npaths, int dim )
                    234:      /*
                    235:   **  Scans the file for the solutions, from a list of length npaths,
                    236:   **  of complex vectors with dim entries.
                    237:   **  The tolerance for the residual to a solution is set to 1.0E-12.
                    238:   **  Returns solutions.
                    239:   */
1.4       takayama  240: #define BUFSIZE 1024
1.1       maekawa   241: {
                    242:   struct phc_object rob,sob;
                    243:   char ch;
                    244:   int fnd,i,j,nsols;
1.2       takayama  245:   double res;
1.4       takayama  246:   double realpart;
                    247:   double imagpart;
                    248:   /*  double realparts[npaths][dim];
                    249:   double imagparts[npaths][dim]; */
                    250:   double *realparts;
                    251:   double *imagparts;
                    252:   char buf[BUFSIZE];
1.1       maekawa   253:   nsols = 0;
1.5     ! takayama  254:   realparts = (double *)sGC_malloc(sizeof(double)*(npaths*dim+1));
        !           255:   imagparts = (double *)sGC_malloc(sizeof(double)*(npaths*dim+1));
1.2       takayama  256:   while ((ch = fgetc(fp)) != EOF)
1.1       maekawa   257:     {
1.2       takayama  258:       fnd = phc_scan_for_string(fp,"start residual :");
1.1       maekawa   259:       if (fnd==1)
1.4       takayama  260:         {
1.5     ! takayama  261:                  fgets(buf,BUFSIZE-1,fp);
        !           262:           sscanf(buf,"%le",&res);
        !           263:                  if (phc_verbose) {
        !           264:              fprintf(stderr,"res in string: %s\n",buf);
        !           265:              fprintf(stderr," residual = "); fprintf(stderr,"%le\n",res);
        !           266:                  }
1.4       takayama  267:           if (res < 1.0E-12) {
                    268:             nsols = nsols+1;
                    269:             if (nsols > npaths) {
                    270:               fprintf(stderr,"Something is wrong in phc_scan_solutions\n");
                    271:               fprintf(stderr,"npaths=%d, nsols=%d \n",npaths,nsols);
                    272:               exit(-1);
                    273:             }
                    274:           }
                    275:           fnd = phc_scan_for_string(fp,"the solution for t :");
                    276:           for (i=0;i<dim;i++)
                    277:             {
                    278:               fnd = phc_scan_for_string(fp,":");
                    279:                          fgets(buf,BUFSIZE-1,fp);
                    280:                          sscanf(buf,"%le %le",&realpart,&imagpart);
                    281:                          if (phc_verbose) fprintf(stderr,"sols in string: %s\n",buf);
                    282:               /*fscanf(fp,"%le",&realpart);
                    283:               fscanf(fp,"%le",&imagpart);*/
                    284:               if (res < 1.0E-12)
                    285:                 {
                    286:                   /*realparts[nsols-1][i] = realpart;
                    287:                   imagparts[nsols-1][i] = imagpart;*/
1.5     ! takayama  288:                   realparts[(nsols-1)*dim+i] = realpart;
        !           289:                   imagparts[(nsols-1)*dim+i] = imagpart;
1.4       takayama  290:                 }
                    291:             }
                    292:         }
1.1       maekawa   293:     }
                    294:   if(phc_verbose) fprintf(stderr,"  number of solutions = %i\n",nsols);
                    295:   rob = phc_newObjectArray(nsols);
                    296:   for (i=0;i<nsols;i++)
                    297:     {
1.5     ! takayama  298:       if (phc_verbose) fprintf(stderr,"Solution %i :\n",i+1);
1.1       maekawa   299:       sob = phc_newObjectArray(dim);
                    300:       for (j=0;j<dim;j++)
1.4       takayama  301:         {
                    302:           /*
                    303:             printf("%20.14le",realparts[i][j]); printf("  ");
                    304:             printf("%20.14le",imagparts[i][j]); printf("\n");
                    305:             */
1.5     ! takayama  306:                  if (phc_verbose) {
        !           307:             printf("%20.14le",realparts[i*dim+j]); printf("  ");
        !           308:             printf("%20.14le",imagparts[i*dim+j]); printf("\n");
        !           309:           }
1.4       takayama  310:
                    311:           /*phc_putoa(sob,j,phc_complexTo(realparts[i][j],imagparts[i][j]));*/
1.5     ! takayama  312:           phc_putoa(sob,j,phc_complexTo(realparts[i*dim+j],
        !           313:                                         imagparts[i*dim+j]));
1.4       takayama  314:         }
1.1       maekawa   315:       phc_putoa(rob,i,sob);
                    316:     }
                    317:   return(rob);
                    318: }
                    319: struct phc_object phc_scan_output_of_phc(char *fname)
                    320:      /*
                    321:   **  Scans the file "output" of phc in two stages to get
                    322:   **   1) the number of paths and the dimension;
                    323:   **   2) the solutions, vectors with residuals < 1.0E-12.
                    324:   */
                    325: {
                    326:   FILE *otp;
                    327:   char ch;
                    328:   int fnd,npaths,dim,i,nsols;
                    329:   otp = fopen(fname,"r");
1.4       takayama  330:   if (otp == NULL) {
                    331:     fprintf(stderr,"The file %d is not found.\n",fname);
                    332:     exit(0);
                    333:   }
1.1       maekawa   334:   if (phc_verbose) fprintf(stderr,"Scanning the %s of phc.\n",fname);
1.2       takayama  335:   fnd = phc_scan_for_string(otp,"THE SOLUTIONS :");
1.1       maekawa   336:   fscanf(otp,"%i",&npaths);
                    337:   if (phc_verbose) fprintf(stderr,"  number of paths traced = %i\n",npaths);
                    338:   fscanf(otp,"%i",&dim);
                    339:   if (phc_verbose) fprintf(stderr,"  dimension of solutions = %i\n",dim);
                    340:   return(phc_scan_solutions(otp,npaths,dim));
                    341: }
                    342: struct phc_object phc_call_phc(char *sys)  /* call phc, system as string */
                    343: {
                    344:   FILE *inp;
                    345:   char *f,*outf;
                    346:   char cmd[1024];
                    347:   char *w;
                    348:   struct phc_object phc_NullObject ;
                    349:   struct stat statbuf;
                    350:
                    351:   phc_NullObject.tag = Snull;
                    352:   f = phc_generateUniqueFileName("tmp.input");
                    353:   if (phc_verbose) fprintf(stderr,"Creating file with name %s.\n",f);
                    354:   outf = phc_generateUniqueFileName("tmp.output");
                    355:   if (stat(outf,&statbuf) == 0) {
                    356:     sprintf(cmd,"/bin/rm -f %s",outf);
                    357:     system(cmd);
                    358:   }
                    359:   inp = fopen(f,"w");
                    360:   fprintf(inp,sys);
                    361:   fclose(inp);
                    362:   if ((w = phc_which("phc")) != NULL) {
                    363:     sprintf(cmd,"%s -b %s %s",w,f,outf);
                    364:   }else{
                    365:     sprintf(cmd,"phc -b %s %s",f,outf);
                    366:   }
                    367:   if (phc_verbose)fprintf(stderr,"Calling %s, black-box solver of phc.\n",cmd);
                    368:   system(cmd);
                    369:   if (stat(outf,&statbuf) < 0) {
                    370:     fprintf(stderr,"execution error of phc.\n");
                    371:     return(phc_NullObject);
                    372:   }else{
                    373:     if (phc_verbose) fprintf(stderr,"See the file %s for results.\n",outf);
                    374:     return(phc_scan_output_of_phc(outf));
                    375:   }
                    376: }
                    377:
                    378:
                    379: struct phc_object phc_newObjectArray(size)
1.4       takayama  380:      int size;
1.1       maekawa   381: {
                    382:   struct phc_object rob;
                    383:   struct phc_object *op;
                    384:   if (size > 0) {
                    385:     op = (struct phc_object *)sGC_malloc(size*sizeof(struct phc_object));
                    386:     if (op == (struct phc_object *)NULL) {fprintf(stderr,"No memory\n");exit(1);}
                    387:   }else{
                    388:     op = (struct phc_object *)NULL;
                    389:   }
                    390:   rob.tag = Sarray;
                    391:   rob.lc.ival = size;
                    392:   rob.rc.op = op;
                    393:   return(rob);
                    394: }
                    395:
                    396: void phc_printObject(FILE *fp,struct phc_object ob)
                    397: {
                    398:   int n,i;
                    399:   if (ob.tag == Snull) {
                    400:     fprintf(fp,"null");
                    401:   }else if (ob.tag == SstringObject) {
                    402:     fprintf(fp,"%s",ob.lc.str);
                    403:   }else if (ob.tag == Sarray) {
                    404:     n = phc_getoaSize(ob);
                    405:     fprintf(fp,"[");
                    406:     for (i=0; i<n; i++) {
                    407:       phc_printObject(fp,phc_getoa(ob,i));
                    408:       if (i <n-1) fprintf(fp," , ");
                    409:     }
                    410:     fprintf(fp,"]");
                    411:   }else if (ob.tag == SlongdoubleComplex) {
                    412:     /* Try your favorite way to print complex numbers. */
1.4       takayama  413:     /*fprintf(fp,"(%20.14le)+I*(%20.14le)",ob.lc.longdouble,ob.rc.longdouble);*/
                    414:     fprintf(fp,"[%lf , %lf]",ob.lc.longdouble,ob.rc.longdouble);
1.1       maekawa   415:   }else{
                    416:     fprintf(stderr,"Unknown phc_object tag %d",ob.tag);
                    417:   }
                    418: }
                    419:
                    420:
                    421: char *phc_generateUniqueFileName(char *s)
                    422: {
                    423:   char *t;
                    424:   int i;
                    425:   struct stat statbuf;
                    426:   t = (char *)sGC_malloc(sizeof(char)*strlen(s)+4+2);
                    427:   for (i=0; i<1000; i++) {
                    428:     /* Give up if we failed for 1000 names. */
                    429:     sprintf(t,"%s.%d",s,i);
                    430:     if (phc_overwrite) return(t);
                    431:     if (stat(t,&statbuf) < 0) {
                    432:       return(t);
                    433:     }
                    434:   }
                    435:   fprintf(stderr,"Could not generate a unique file name.");
                    436:   return(NULL);
                    437: }
                    438:
                    439: char *phc_which(char *s) {
                    440:   struct stat statbuf;
                    441:   char *cmd,*a;
                    442:   a = getenv("OpenXM_HOME");
                    443:   if (a != NULL) {
                    444:     cmd = (char *) sGC_malloc(sizeof(char)*(strlen(s)+strlen(a)
1.4       takayama  445:                                             +strlen("/usr/local/bin/")+1));
1.1       maekawa   446:     if (cmd == NULL) {fprintf(stderr,"No more memory.\n"); exit(1);}
                    447:     strcpy(cmd,a); strcat(cmd,"/bin/"); strcat(cmd,s);
                    448:     if (stat(cmd,&statbuf) == 0) {
                    449:       return(cmd);
                    450:     }
                    451:   }
                    452:   cmd = (char *) sGC_malloc(sizeof(char)*(strlen(s)
1.4       takayama  453:                                           +strlen("/usr/local/bin/")+1));
1.1       maekawa   454:   if (cmd == NULL) {fprintf(stderr,"No more memory.\n"); exit(1);}
                    455:   strcpy(cmd,"/usr/local/bin/"); strcat(cmd,s);
                    456:   if (stat(cmd,&statbuf) == 0) {
                    457:     return(cmd);
                    458:   }
                    459:   strcpy(cmd,"/tmp/"); strcat(cmd,s);
                    460:   if (stat(cmd,&statbuf) == 0) {
                    461:     return(cmd);
                    462:   }
                    463:   return(NULL);
                    464: }
                    465:
                    466:
1.4       takayama  467: struct phc_object phc_complexTo(double r, double i)
1.1       maekawa   468: {
                    469:   struct phc_object rob;
                    470:   rob.tag = SlongdoubleComplex;
                    471:   rob.lc.longdouble = r;
                    472:   rob.rc.longdouble = i;
                    473:   return(rob);
                    474: }
                    475:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>