[BACK]Return to wmain.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / hgm / mh / src

Diff for /OpenXM/src/hgm/mh/src/wmain.c between version 1.4 and 1.10

version 1.4, 2013/02/20 05:20:49 version 1.10, 2013/03/07 03:00:43
Line 1 
Line 1 
 /*  /*
 $OpenXM: OpenXM/src/hgm/mh/src/wmain.c,v 1.3 2013/02/20 01:06:38 takayama Exp $    $OpenXM: OpenXM/src/hgm/mh/src/wmain.c,v 1.9 2013/03/05 05:26:07 takayama Exp $
 License: LGPL    License: LGPL
  */  */
 #include <stdio.h>  #include <stdio.h>
 #include <stdlib.h>  #include <stdlib.h>
 #include <math.h>  #include <math.h>
Line 38  static int setParamTest(void);
Line 38  static int setParamTest(void);
 static int setParamDefault(void);  static int setParamDefault(void);
 static int setParam(char *fname);  static int setParam(char *fname);
 static int showParam(void);  static int showParam(void);
   static int next(struct SFILE *fp,char *s, char *msg);
   
 /* #define DEBUG */  /* #define DEBUG */
 #ifdef DEBUG  #ifdef DEBUG
Line 55  void mh_freeWorkArea(void) {
Line 56  void mh_freeWorkArea(void) {
   mh_rf(0.0, NULL, 0, NULL, 0);    mh_rf(0.0, NULL, 0, NULL, 0);
   MH_deallocate=0; /* switch to the normal mode. */    MH_deallocate=0; /* switch to the normal mode. */
 }  }
 void *mh_malloc(int s) {  static int mypower(int x,int n) {
   void *p;  
   p = (void*)malloc(s);  
   if (p == NULL) {  
         fprintf(stderr,"No memory.\n"); mh_exit(-1);  
   }  
   return(p);  
 }  
 mh_free(void *p) {  
   free(p);  
   return(0);  
 }  
 static mypower(int x,int n) {  
   int a,i;    int a,i;
   a = 1;    a = 1;
   for (i=0; i<n; i++) a = a*x;    for (i=0; i<n; i++) a = a*x;
   return(a);    return(a);
 }  }
 #ifdef STANDALONE  #ifdef STANDALONE2
 main(int argc,char *argv[]) {  main(int argc,char *argv[]) {
 /*  mh_main(argc,argv);    mh_exit(MH_RESET_EXIT); /* standalone mode */
   mh_freeWorkArea(); */    /*  mh_main(argc,argv);
         mh_freeWorkArea(); */
   mh_main(argc,argv);    mh_main(argc,argv);
 }  }
 #endif  #endif
Line 92  struct MH_RESULT *mh_main(int argc,char *argv[]) {
Line 82  struct MH_RESULT *mh_main(int argc,char *argv[]) {
   if (MH_deallocate) { if (y0) mh_free(y0); return(rp); }    if (MH_deallocate) { if (y0) mh_free(y0); return(rp); }
   setParam(NULL); MH_Gfname = MH_Dfname = NULL; MH_Verbose=1;    setParam(NULL); MH_Gfname = MH_Dfname = NULL; MH_Verbose=1;
   for (i=1; i<argc; i++) {    for (i=1; i<argc; i++) {
         if (strcmp(argv[i],"--idata")==0) {      if (strcmp(argv[i],"--idata")==0) {
           inci(i);  
           setParam(argv[i]); MH_Verbose=0;  
         }else if (strcmp(argv[i],"--gnuplotf")==0) {  
           inci(i);  
           MH_Gfname = (char *)mh_malloc(SMAX);  
           strcpy(MH_Gfname,argv[i]);  
         }else if (strcmp(argv[i],"--dataf")==0) {  
           inci(i);  
           MH_Dfname = (char *)mh_malloc(SMAX);  
           strcpy(MH_Dfname,argv[i]);  
         }else if (strcmp(argv[i],"--xmax")==0) {  
       inci(i);        inci(i);
           sscanf(argv[i],"%lf",&Xng);        setParam(argv[i]); MH_Verbose=0;
         }else if (strcmp(argv[i],"--step")==0) {      }else if (strcmp(argv[i],"--gnuplotf")==0) {
       inci(i);        inci(i);
           sscanf(argv[i],"%lg",&MH_Hg);        MH_Gfname = (char *)mh_malloc(SMAX);
         }else if (strcmp(argv[i],"--help")==0) {        strcpy(MH_Gfname,argv[i]);
           mh_usage(); return(rp);      }else if (strcmp(argv[i],"--dataf")==0) {
         }else if (strcmp(argv[i],"--raw")==0) {        inci(i);
           MH_RawName = 1;        MH_Dfname = (char *)mh_malloc(SMAX);
         }else if (strcmp(argv[i],"--test")==0) {        strcpy(MH_Dfname,argv[i]);
           inci(i);      }else if (strcmp(argv[i],"--xmax")==0) {
           sscanf(argv[i],"%d",&Testrank);        inci(i);
           setParamTest();        sscanf(argv[i],"%lf",&Xng);
         }else if (strcmp(argv[i],"--95")==0) {      }else if (strcmp(argv[i],"--step")==0) {
           MH_P95=1;        inci(i);
         }else if (strcmp(argv[i],"--verbose")==0) {        sscanf(argv[i],"%lg",&MH_Hg);
           MH_Verbose=1;      }else if (strcmp(argv[i],"--help")==0) {
         }else if (strcmp(argv[i],"--bystring")==0) {        mh_usage(); return(rp);
           MH_byFile = 0;      }else if (strcmp(argv[i],"--raw")==0) {
         }else {        MH_RawName = 1;
           fprintf(stderr,"Unknown option %s\n",argv[i]);      }else if (strcmp(argv[i],"--test")==0) {
           mh_usage();        inci(i);
           return(rp);        sscanf(argv[i],"%d",&Testrank);
         }        setParamTest();
       }else if (strcmp(argv[i],"--95")==0) {
         MH_P95=1;
       }else if (strcmp(argv[i],"--verbose")==0) {
         MH_Verbose=1;
       }else if (strcmp(argv[i],"--bystring")==0) {
         MH_byFile = 0;
       }else {
         fprintf(stderr,"Unknown option %s\n",argv[i]);
         mh_usage();
         return(rp);
       }
   }    }
   if (MH_Verbose) showParam();    if (MH_Verbose) showParam();
   x0 = MH_X0g;    x0 = MH_X0g;
Line 143  struct MH_RESULT *mh_main(int argc,char *argv[]) {
Line 133  struct MH_RESULT *mh_main(int argc,char *argv[]) {
   return(rp);    return(rp);
 }  }
   
 mh_usage() {  int mh_usage() {
   fprintf(stderr,"Usages:\n");    fprintf(stderr,"Usages:\n");
   fprintf(stderr,"w-m [--idata input_data_file --gnuplotf gnuplot_file_name\n");    fprintf(stderr,"w-m [--idata input_data_file --gnuplotf gnuplot_file_name\n");
   fprintf(stderr," --dataf output_data_file --raw --xmax xmax --test m --step h]\n");    fprintf(stderr," --dataf output_data_file --raw --xmax xmax --test m --step h]\n");
Line 157  mh_usage() {
Line 147  mh_usage() {
   fprintf(stderr," Ef: a scalar factor to the initial value. It may set to 1.\n");    fprintf(stderr," Ef: a scalar factor to the initial value. It may set to 1.\n");
   fprintf(stderr," MH_Hg: h (step size), MH_X0g: starting value of x, Xng: terminating value of x\n");    fprintf(stderr," MH_Hg: h (step size), MH_X0g: starting value of x, Xng: terminating value of x\n");
   fprintf(stderr," MH_Dp: output data is stored in every MH_Dp steps when output_data_file is specified.\n");    fprintf(stderr," MH_Dp: output data is stored in every MH_Dp steps when output_data_file is specified.\n");
   fprintf(stderr," --95: output the 95% point. --verbose: verbose mode.\n");    fprintf(stderr," --95: output the 95%% point. --verbose: verbose mode.\n");
   fprintf(stderr," The line started with %% is a comment line.\n");    fprintf(stderr," The line started with %% is a comment line.\n");
   fprintf(stderr," An example format of the input_data_file can be obtained by executing w-2 with no option.\n");    fprintf(stderr," An example format of the input_data_file can be obtained by executing w-2 with no option.\n");
   fprintf(stderr,"\nExamples:\n");    fprintf(stderr,"\nExamples:\n");
Line 173  mh_usage() {
Line 163  mh_usage() {
   fprintf(stderr,"  tmp-graph is the table of x and the values of Pr({y | y<x}).\n");    fprintf(stderr,"  tmp-graph is the table of x and the values of Pr({y | y<x}).\n");
 }  }
   
 static setParamTest() {  static int setParamTest() {
   int rank;    int rank;
   int i;    int i;
   extern int Testrank;    extern int Testrank;
Line 193  static setParamTest() {
Line 183  static setParamTest() {
   MH_Dp = 1;    MH_Dp = 1;
   Xng = 10.0;    Xng = 10.0;
 }  }
 static setParamDefault() {  static int setParamDefault() {
   int rank;    int rank;
   MH_M=MH_Mg = 2 ;    MH_M=MH_Mg = 2 ;
   MH_RANK=rank = mypower(2,MH_Mg);    MH_RANK=rank = mypower(2,MH_Mg);
Line 212  static setParamDefault() {
Line 202  static setParamDefault() {
   Xng = 10.0;    Xng = 10.0;
 }  }
   
 next(struct SFILE *sfp,char *s,char *msg) {  static int next(struct SFILE *sfp,char *s,char *msg) {
   s[0] = '%';    s[0] = '%';
   while (s[0] == '%') {    while (s[0] == '%') {
         if (!mh_fgets(s,SMAX,sfp)) {      if (!mh_fgets(s,SMAX,sfp)) {
           fprintf(stderr,"Data format error at %s\n",msg);        fprintf(stderr,"Data format error at %s\n",msg);
           mh_exit(-1);        mh_exit(-1);
         }      }
         if (s[0] != '%') return(0);      if (s[0] != '%') return(0);
   }    }
 }  }
 static setParam(char *fname) {  static int setParam(char *fname) {
   int rank;    int rank;
   char s[SMAX];    char s[SMAX];
   struct SFILE *fp;    struct SFILE *fp;
Line 230  static setParam(char *fname) {
Line 220  static setParam(char *fname) {
   extern int MH_deallocate;    extern int MH_deallocate;
   extern int MH_byFile;    extern int MH_byFile;
   if (MH_deallocate) {    if (MH_deallocate) {
         if (MH_Beta) mh_free(MH_Beta);      if (MH_Beta) mh_free(MH_Beta);
         if (MH_Ng) mh_free(MH_Ng);      if (MH_Ng) mh_free(MH_Ng);
         if (Iv) mh_free(Iv);      if (Iv) mh_free(Iv);
         return(0);      return(0);
   }    }
   if (fname == NULL) return(setParamDefault());    if (fname == NULL) return(setParamDefault());
   
   if ((fp=mh_fopen(fname,"r",MH_byFile)) == NULL) {    if ((fp=mh_fopen(fname,"r",MH_byFile)) == NULL) {
         fprintf(stderr,"File %s is not found.\n",fname);      fprintf(stderr,"File %s is not found.\n",fname);
         mh_exit(-1);      mh_exit(-1);
   }    }
   next(fp,s,"MH_Mg(m)");    next(fp,s,"MH_Mg(m)");
   sscanf(s,"%d",&MH_Mg); MH_M=MH_Mg;    sscanf(s,"%d",&MH_Mg); MH_M=MH_Mg;
Line 248  static setParam(char *fname) {
Line 238  static setParam(char *fname) {
   MH_Beta = (double *)mh_malloc(sizeof(double)*MH_Mg);    MH_Beta = (double *)mh_malloc(sizeof(double)*MH_Mg);
   for (i=0; i<MH_Mg; i++) {    for (i=0; i<MH_Mg; i++) {
     next(fp,s,"MH_Beta");      next(fp,s,"MH_Beta");
         sscanf(s,"%lf",&(MH_Beta[i]));      sscanf(s,"%lf",&(MH_Beta[i]));
   }    }
   
   MH_Ng = (double *)mh_malloc(sizeof(double));    MH_Ng = (double *)mh_malloc(sizeof(double));
Line 260  static setParam(char *fname) {
Line 250  static setParam(char *fname) {
   
   Iv = (double *)mh_malloc(sizeof(double)*rank);    Iv = (double *)mh_malloc(sizeof(double)*rank);
   for (i=0; i<rank; i++) {    for (i=0; i<rank; i++) {
         next(fp,s,"Iv(initial values)");      next(fp,s,"Iv(initial values)");
         sscanf(s,"%lg",&(Iv[i]));      sscanf(s,"%lg",&(Iv[i]));
   }    }
   
   next(fp,s,"Ef(exponential factor)");    next(fp,s,"Ef(exponential factor)");
Line 278  static setParam(char *fname) {
Line 268  static setParam(char *fname) {
   mh_fclose(fp);    mh_fclose(fp);
 }  }
   
 showParam() {  int showParam() {
   int rank,i;    int rank,i;
   rank = mypower(2,MH_Mg);    rank = mypower(2,MH_Mg);
   printf("%%MH_Mg=\n%d\n",MH_Mg);    printf("%%MH_Mg=\n%d\n",MH_Mg);
   for (i=0; i<MH_Mg; i++) {    for (i=0; i<MH_Mg; i++) {
         printf("%%MH_Beta[%d]=\n%lf\n",i,MH_Beta[i]);      printf("%%MH_Beta[%d]=\n%lf\n",i,MH_Beta[i]);
   }    }
   printf("%%MH_Ng=\n%lf\n",*MH_Ng);    printf("%%MH_Ng=\n%lf\n",*MH_Ng);
   printf("%%MH_X0g=\n%lf\n",MH_X0g);    printf("%%MH_X0g=\n%lf\n",MH_X0g);
   for (i=0; i<rank; i++) {    for (i=0; i<rank; i++) {
         printf("%%Iv[%d]=\n%lg\n",i,Iv[i]);      printf("%%Iv[%d]=\n%lg\n",i,Iv[i]);
   }    }
   printf("%%Ef=\n%lf\n",Ef);    printf("%%Ef=\n%lf\n",Ef);
   printf("%%MH_Hg=\n%lf\n",MH_Hg);    printf("%%MH_Hg=\n%lf\n",MH_Hg);
Line 296  showParam() {
Line 286  showParam() {
   printf("%%Xng=\n%lf\n",Xng);    printf("%%Xng=\n%lf\n",Xng);
 }  }
   
 mh_exit(int n) {  
 #ifdef STANDALONE  
   exit(n);  
 #else  
   /* todo, use setjmp, longjmp */  
   fprintf(stderr,"Fatal error mh_exit(%d) in mh-w-n.\n",n);  
 #endif  
 }  

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.10

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