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

Annotation of OpenXM/src/hgm/mh/src/wmain.c, Revision 1.29

1.1       takayama    1: /*
1.29    ! takayama    2:   $OpenXM: OpenXM/src/hgm/mh/src/wmain.c,v 1.28 2016/02/13 02:19:00 takayama Exp $
1.10      takayama    3:   License: LGPL
                      4: */
1.1       takayama    5: #include <stdio.h>
                      6: #include <stdlib.h>
                      7: #include <math.h>
                      8: #include <string.h>
1.4       takayama    9: #include "sfile.h"
1.11      takayama   10: #include "mh.h"
1.1       takayama   11: #define SMAX 4096
1.22      takayama   12: #define inci(i) { i++; if (i >= argc) { oxprintfe("Option argument is not given.\n"); return(NULL); }}
1.28      takayama   13: #define VSTRING "%!version2.0"
1.3       takayama   14: int MH_deallocate=0;
                     15:
1.20      takayama   16: /*
                     17:   changelog
1.28      takayama   18:   2016.02.13  code-n.c and code-n-2f1.c are linked together. New idata format.
1.27      takayama   19:   2016.02.04  MH_Ef_type  exponential or scalar factor
1.26      takayama   20:   2016.02.01  C_2F1
1.20      takayama   21:   2014.03.15  --strategy 1 is default. A new parser in setParam()
                     22:  */
1.1       takayama   23: extern char *MH_Gfname;
                     24: extern char *MH_Dfname;
                     25:
                     26: /* global variables. They are set in setParam() */
1.4       takayama   27: int MH_byFile=1;
1.1       takayama   28: int MH_RANK;
                     29: int MH_M;
                     30:
                     31: int MH_Mg;  /* m */
                     32: double *MH_Beta; /* beta[0], ..., beta[m-1] */
1.2       takayama   33: double *MH_Ng;   /* freedom n.  c=(m+1)/2+n/2; Note that it is a pointer */
                     34: double MH_X0g;   /* initial point */
                     35: static double *Iv;   /* Initial values of mhg sorted by mhbase() in rd.rr at beta*x0 */
                     36: static double Ef;   /* exponential factor at beta*x0 */
                     37: extern double MH_Hg;   /* step size of rk defined in rk.c */
                     38: int MH_Dp;      /* Data sampling period */
                     39: static double Xng=0.0;   /* the last point */
                     40: int MH_RawName = 0;
                     41: static int Testrank=0;
1.20      takayama   42: /* If MH_success is set to 1, then strategy, MH_abserr, MH_relerr seem to
                     43:   be properly set.
                     44: */
                     45: int MH_success=0;
                     46: /*
                     47:   Estimation of the maximal coeff of A in y'=Ay.
                     48:   This might be too rough estimate.
                     49:  */
                     50: double MH_coeff_max;
                     51: /*
                     52:   Estimation of h by MH_coeff_max;
                     53:  */
                     54: double MH_estimated_start_step;
1.26      takayama   55:
                     56: int MH_P_pFq=1;
                     57: int MH_Q_pFq=1;
1.28      takayama   58: double *MH_A_pFq=NULL;
                     59: double *MH_B_pFq=NULL;
1.27      takayama   60: int MH_Ef_type=1;
1.28      takayama   61: MH_RF mh_rf=NULL;  /* function pointer to mh_rf */
                     62:
1.2       takayama   63: extern int MH_Verbose;
1.20      takayama   64: extern int MH_strategy;
                     65: extern double MH_abserr;
                     66: extern double MH_relerr;
1.16      takayama   67:
1.2       takayama   68: extern int MH_P95;  /* 95 % points */
                     69: int mh_gopen_file(void);
                     70: static int setParamTest(void);
                     71: static int setParamDefault(void);
                     72: static int setParam(char *fname);
                     73: static int showParam(void);
1.6       takayama   74: static int next(struct SFILE *fp,char *s, char *msg);
1.20      takayama   75: static double estimateHg(int m, double beta[],double x0);
1.1       takayama   76:
1.28      takayama   77:
1.1       takayama   78: /* #define DEBUG */
                     79: #ifdef DEBUG
1.2       takayama   80: char *MH_Dfname; char *MH_Gfname; double MH_Hg;
                     81: int mh_gopen_file(void) { }
1.4       takayama   82: struct MH_RESULT mh_rkmain(double x0,double y0[],double xn) { }
1.1       takayama   83: #endif
                     84:
1.3       takayama   85: void mh_freeWorkArea(void) {
                     86:   extern int MH_deallocate;
                     87:   MH_deallocate=1; /* switch to deallocation mode. */
                     88:   mh_main(0,NULL);
                     89:   setParam(NULL);
                     90:   mh_rkmain(0.0, NULL, 0.0);
1.28      takayama   91:   if (mh_rf) (*mh_rf)(0.0, NULL, 0, NULL, 0);
1.3       takayama   92:   MH_deallocate=0; /* switch to the normal mode. */
                     93: }
1.8       takayama   94: static int mypower(int x,int n) {
1.1       takayama   95:   int a,i;
                     96:   a = 1;
                     97:   for (i=0; i<n; i++) a = a*x;
                     98:   return(a);
                     99: }
1.9       takayama  100: #ifdef STANDALONE2
1.24      takayama  101: int main(int argc,char *argv[]) {
1.12      takayama  102:   int strategy=STRATEGY_DEFAULT;
                    103:   double err[2]={-1.0,-1.0};
                    104:   int i;
1.6       takayama  105:   mh_exit(MH_RESET_EXIT); /* standalone mode */
1.10      takayama  106:   /*  mh_main(argc,argv);
                    107:       mh_freeWorkArea(); */
1.3       takayama  108:   mh_main(argc,argv);
1.12      takayama  109:   /* showParam(); */
1.18      takayama  110:   return(0);
1.2       takayama  111: }
                    112: #endif
1.4       takayama  113: struct MH_RESULT *mh_main(int argc,char *argv[]) {
1.13      takayama  114:   static double *y0=NULL;
1.1       takayama  115:   double x0,xn;
                    116:   double ef;
                    117:   int i,rank;
1.4       takayama  118:   struct MH_RESULT *rp=NULL;
1.3       takayama  119:   extern int MH_deallocate;
1.4       takayama  120:   extern int MH_byFile;
1.28      takayama  121:   extern double Ef;
                    122:   Ef = 1;
1.4       takayama  123:   MH_byFile=1;
                    124:   if (MH_deallocate) { if (y0) mh_free(y0); return(rp); }
1.2       takayama  125:   setParam(NULL); MH_Gfname = MH_Dfname = NULL; MH_Verbose=1;
1.1       takayama  126:   for (i=1; i<argc; i++) {
1.10      takayama  127:     if (strcmp(argv[i],"--idata")==0) {
1.1       takayama  128:       inci(i);
1.10      takayama  129:       setParam(argv[i]); MH_Verbose=0;
                    130:     }else if (strcmp(argv[i],"--gnuplotf")==0) {
1.1       takayama  131:       inci(i);
1.10      takayama  132:       MH_Gfname = (char *)mh_malloc(SMAX);
                    133:       strcpy(MH_Gfname,argv[i]);
                    134:     }else if (strcmp(argv[i],"--dataf")==0) {
                    135:       inci(i);
                    136:       MH_Dfname = (char *)mh_malloc(SMAX);
                    137:       strcpy(MH_Dfname,argv[i]);
                    138:     }else if (strcmp(argv[i],"--xmax")==0) {
                    139:       inci(i);
                    140:       sscanf(argv[i],"%lf",&Xng);
                    141:     }else if (strcmp(argv[i],"--step")==0) {
                    142:       inci(i);
                    143:       sscanf(argv[i],"%lg",&MH_Hg);
                    144:     }else if (strcmp(argv[i],"--help")==0) {
                    145:       mh_usage(); return(rp);
                    146:     }else if (strcmp(argv[i],"--raw")==0) {
                    147:       MH_RawName = 1;
                    148:     }else if (strcmp(argv[i],"--test")==0) {
                    149:       inci(i);
                    150:       sscanf(argv[i],"%d",&Testrank);
                    151:       setParamTest();
                    152:     }else if (strcmp(argv[i],"--95")==0) {
                    153:       MH_P95=1;
                    154:     }else if (strcmp(argv[i],"--verbose")==0) {
                    155:       MH_Verbose=1;
                    156:     }else if (strcmp(argv[i],"--bystring")==0) {
                    157:       MH_byFile = 0;
1.20      takayama  158:     }else if (strcmp(argv[i],"--strategy")==0) {
                    159:       i++; sscanf(argv[i],"%d",&MH_strategy);
                    160:     }else if (strcmp(argv[i],"--abserr")==0) {
                    161:       i++; sscanf(argv[i],"%lg",&MH_abserr);
                    162:     }else if (strcmp(argv[i],"--relerr")==0) {
                    163:       i++; sscanf(argv[i],"%lg",&MH_relerr);
1.10      takayama  164:     }else {
1.22      takayama  165:       oxprintfe("Unknown option %s\n",argv[i]);
1.10      takayama  166:       mh_usage();
                    167:       return(rp);
                    168:     }
1.1       takayama  169:   }
1.2       takayama  170:   x0 = MH_X0g;
1.1       takayama  171:   xn = Xng;
                    172:   ef = Ef;
                    173:   rank = mypower(2,MH_Mg);
1.2       takayama  174:   y0 = (double *) mh_malloc(sizeof(double)*rank);
1.1       takayama  175:   for (i=0; i<rank; i++) y0[i] = ef*Iv[i];
1.2       takayama  176:   mh_gopen_file();
1.20      takayama  177:   rp = (struct MH_RESULT*) mh_malloc(sizeof(struct MH_RESULT));
                    178:
                    179:   if (MH_strategy) {
                    180:     if (MH_abserr > SIGDIGIT_DEFAULT*myabs(y0[0])) {
                    181:       MH_success = 0;
1.22      takayama  182:       oxprintfe("%%%%Warning, abserr seems not to be small enough, abserr=%lg, y[0]=%lg\n",MH_abserr,y0[0]);
1.20      takayama  183:     }else{
                    184:       MH_success = 1;
                    185:     }
                    186:   }else{
                    187:     MH_success = 0;
                    188:   }
                    189:   MH_estimated_start_step = estimateHg(MH_Mg,MH_Beta,MH_X0g);
                    190:   if (MH_Verbose) showParam();
1.22      takayama  191:   if (MH_Verbose) {for (i=0; i<rank; i++) oxprintf("%lf\n",y0[i]); }
1.19      takayama  192:
1.4       takayama  193:   *rp=mh_rkmain(x0,y0,xn);
                    194:   return(rp);
1.1       takayama  195: }
                    196:
1.8       takayama  197: int mh_usage() {
1.28      takayama  198:
                    199: #include "usage-w-n.h"
                    200:
1.29    ! takayama  201:   return(0);
1.1       takayama  202: }
                    203:
1.8       takayama  204: static int setParamTest() {
1.1       takayama  205:   int rank;
                    206:   int i;
                    207:   extern int Testrank;
1.2       takayama  208:   extern int MH_Verbose;
                    209:   MH_Verbose=1;
1.1       takayama  210:   MH_M= MH_Mg = Testrank ;
                    211:   MH_RANK = rank = mypower(2,MH_Mg);
1.2       takayama  212:   MH_Beta = (double *)mh_malloc(sizeof(double)*MH_Mg);
1.1       takayama  213:   for (i=0; i<MH_Mg; i++) MH_Beta[i] = 1.0+0.1*i;
1.2       takayama  214:   MH_Ng = (double *)mh_malloc(sizeof(double)); *MH_Ng = 3.0;
                    215:   Iv = (double *)mh_malloc(sizeof(double)*rank);
1.1       takayama  216:   for (i=0; i<rank; i++) Iv[i] = 0;
                    217:   Iv[0] = 0.001;
                    218:   Ef = 1;
1.2       takayama  219:   MH_X0g = 0.3;
                    220:   MH_Hg = 0.001;
                    221:   MH_Dp = 1;
1.23      takayama  222:   Xng = 10.0; return(0);
1.1       takayama  223: }
1.8       takayama  224: static int setParamDefault() {
1.1       takayama  225:   int rank;
1.28      takayama  226:   double a[1];
                    227:   double b[1];
1.1       takayama  228:   MH_M=MH_Mg = 2 ;
                    229:   MH_RANK=rank = mypower(2,MH_Mg);
1.2       takayama  230:   MH_Beta = (double *)mh_malloc(sizeof(double)*MH_Mg);
1.1       takayama  231:   MH_Beta[0] = 1.0; MH_Beta[1] = 2.0;
1.2       takayama  232:   MH_Ng = (double *)mh_malloc(sizeof(double)); *MH_Ng = 3.0;
1.28      takayama  233:   a[0] = (MH_M+1.0)/2.0;
                    234:   b[0] = a[0]+(*MH_Ng)/2.0;
                    235:   mh_setA(a,1); mh_setB(b,1);
                    236:   mh_rf = & mh_rf_ef_type_1;
1.2       takayama  237:   Iv = (double *)mh_malloc(sizeof(double)*rank);
1.1       takayama  238:   Iv[0] = 1.58693;
                    239:   Iv[1] = 0.811369;
                    240:   Iv[2] = 0.846874;
                    241:   Iv[3] = 0.413438;
                    242:   Ef = 0.01034957388338225707;
1.2       takayama  243:   MH_X0g = 0.3;
                    244:   MH_Hg = 0.001;
                    245:   MH_Dp = 1;
1.23      takayama  246:   Xng = 10.0; return(0);
1.1       takayama  247: }
                    248:
1.28      takayama  249: static int next_old1(struct SFILE *sfp,char *s,char *msg) {
1.1       takayama  250:   s[0] = '%';
                    251:   while (s[0] == '%') {
1.10      takayama  252:     if (!mh_fgets(s,SMAX,sfp)) {
1.22      takayama  253:       oxprintfe("Data format error at %s\n",msg);
1.10      takayama  254:       mh_exit(-1);
                    255:     }
                    256:     if (s[0] != '%') return(0);
1.23      takayama  257:   } return(0);
1.1       takayama  258: }
1.28      takayama  259: static int next(struct SFILE *sfp,char *s,char *msg) {
                    260:   static int check=1;
                    261:   char *ng="%%Ng=";
                    262:   // int i;
                    263:   s[0] = '%';
                    264:   while ((s[0] == '%') || (s[0] == '#')) {
                    265:     if (!mh_fgets(s,SMAX,sfp)) {
                    266:       oxprintfe("Data format error at %s\n",msg);
                    267:       oxprintfe("Is it version 2.0 format? If so, add\n%s\nat the top.\n",VSTRING);
                    268:       mh_exit(-1);
                    269:     }
                    270:        if ((s[0] == '%') && (s[1] == '%')) continue;
                    271:     if (s[0] == '#') continue;
                    272:     if (strncmp(s,VSTRING,strlen(VSTRING)) == 0) {
                    273:          return(2);
                    274:        }
                    275:     if (check && (strncmp(msg,ng,4)==0)) {
                    276:       if (strncmp(s,ng,5) != 0) {
                    277:         oxprintfe("Warning, there is no %%Ng= at the border of Beta's and Ng, s=%s\n",s);
                    278:       }
                    279:       /* check=0; */
                    280:     }
                    281:     if (s[0] != '%') return(0);
                    282:   }
                    283:   return(0);
                    284: }
                    285:
1.8       takayama  286: static int setParam(char *fname) {
1.1       takayama  287:   int rank;
                    288:   char s[SMAX];
1.4       takayama  289:   struct SFILE *fp;
1.1       takayama  290:   int i;
1.28      takayama  291:   int version;
                    292:   double a[1]; double b[1]; /* for old inputs */
1.20      takayama  293:   struct mh_token tk;
1.3       takayama  294:   extern int MH_deallocate;
1.4       takayama  295:   extern int MH_byFile;
1.3       takayama  296:   if (MH_deallocate) {
1.28      takayama  297:     if (MH_Beta) {mh_free(MH_Beta); MH_Beta=NULL;}
                    298:     if (MH_Ng) {mh_free(MH_Ng); MH_Ng=NULL;}
                    299:     if (Iv) {mh_free(Iv); Iv = NULL;}
                    300:        if (MH_A_pFq) {mh_free(MH_A_pFq); MH_A_pFq=NULL; MH_P_pFq = 0;}
                    301:        if (MH_B_pFq) {mh_free(MH_B_pFq); MH_B_pFq=NULL; MH_Q_pFq = 0;}
1.10      takayama  302:     return(0);
1.3       takayama  303:   }
1.1       takayama  304:   if (fname == NULL) return(setParamDefault());
                    305:
1.4       takayama  306:   if ((fp=mh_fopen(fname,"r",MH_byFile)) == NULL) {
1.22      takayama  307:     oxprintfe("File %s is not found.\n",fname);
1.10      takayama  308:     mh_exit(-1);
1.1       takayama  309:   }
1.28      takayama  310:   /* set default initial values */
                    311:   MH_Mg=MH_M=-1;  /* number of variables */
                    312:   MH_Ng=(double *) mh_malloc(sizeof(double)); *MH_Ng=-1; /* *Ng is the degree of freedom 1F1 */
                    313:   MH_X0g=0.1;   /* evaluation point */
                    314:   Ef=1.0;    /* exponential factor */
                    315:   MH_Ef_type=1;
                    316:   MH_Hg=0.01;   /* step size for RK */
                    317:   MH_Dp = 1;   /* sampling rate */
                    318:   Xng = 10.0; /* terminal point for RK */
                    319:
                    320:   /* Parser for the old style (version <2.0) input */
                    321:   version=next(fp,s,"Mg(m)");
                    322:   if (version == 2) goto mh_myparse;
1.1       takayama  323:   sscanf(s,"%d",&MH_Mg); MH_M=MH_Mg;
                    324:   MH_RANK=rank = mypower(2,MH_Mg);
                    325:
1.2       takayama  326:   MH_Beta = (double *)mh_malloc(sizeof(double)*MH_Mg);
1.1       takayama  327:   for (i=0; i<MH_Mg; i++) {
                    328:     next(fp,s,"MH_Beta");
1.10      takayama  329:     sscanf(s,"%lf",&(MH_Beta[i]));
1.1       takayama  330:   }
                    331:
1.2       takayama  332:   next(fp,s,"MH_Ng(freedom parameter n)");
                    333:   sscanf(s,"%lf",MH_Ng);
1.28      takayama  334:   /* for old style input only for 1F1 ef_type==1. */
                    335:   MH_P_pFq = 1; MH_Q_pFq = 1;
                    336:   a[0] = (MH_M+1.0)/2.0;
                    337:   b[0] = a[0]+(*MH_Ng)/2.0;
                    338:   mh_setA(a,1); mh_setB(b,1);
                    339:   mh_rf = & mh_rf_ef_type_1;
                    340:
1.1       takayama  341:
1.2       takayama  342:   next(fp,s,"MH_X0g(initial point)");
                    343:   sscanf(s,"%lf",&MH_X0g);
1.1       takayama  344:
1.2       takayama  345:   Iv = (double *)mh_malloc(sizeof(double)*rank);
1.1       takayama  346:   for (i=0; i<rank; i++) {
1.10      takayama  347:     next(fp,s,"Iv(initial values)");
                    348:     sscanf(s,"%lg",&(Iv[i]));
1.1       takayama  349:   }
                    350:
                    351:   next(fp,s,"Ef(exponential factor)");
                    352:   sscanf(s,"%lg",&Ef);
                    353:
1.2       takayama  354:   next(fp,s,"MH_Hg (step size of rk)");
                    355:   sscanf(s,"%lg",&MH_Hg);
1.1       takayama  356:
1.2       takayama  357:   next(fp,s,"MH_Dp (data sampling period)");
                    358:   sscanf(s,"%d",&MH_Dp);
1.1       takayama  359:
                    360:   next(fp,s,"Xng (the last point, cf. --xmax)");
                    361:   sscanf(s,"%lf",&Xng);
1.20      takayama  362:
                    363:   /* Reading the optional parameters */
1.28      takayama  364:  mh_myparse:
1.20      takayama  365:   while ((tk = mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_EOF) {
                    366:     /* expect ID */
                    367:     if (tk.type != MH_TOKEN_ID) {
1.22      takayama  368:       oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
1.20      takayama  369:     }
                    370:     if ((strcmp(s,"abserr")==0) || (strcmp(s,"abserror")==0)) {
                    371:       if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
1.22      takayama  372:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
1.20      takayama  373:       }
                    374:       if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_DOUBLE) {
1.22      takayama  375:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
1.20      takayama  376:       }
                    377:       MH_abserr = tk.dval;
                    378:       continue;
                    379:     }
                    380:     if ((strcmp(s,"relerr")==0) || (strcmp(s,"relerror")==0)) {
                    381:       if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
1.22      takayama  382:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
1.20      takayama  383:       }
                    384:       if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_DOUBLE) {
1.22      takayama  385:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
1.20      takayama  386:       }
                    387:       MH_relerr = tk.dval;
                    388:       continue;
                    389:     }
                    390:     if (strcmp(s,"strategy")==0) {
                    391:       if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
1.22      takayama  392:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
1.20      takayama  393:       }
                    394:       if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
1.22      takayama  395:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
1.20      takayama  396:       }
                    397:       MH_strategy = tk.ival;
                    398:       continue;
                    399:     }
1.28      takayama  400:     // Format: #p_pFq=2  1.5  3.2   override by new input format.
1.26      takayama  401:     if (strcmp(s,"p_pFq")==0) {
                    402:       if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
                    403:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    404:       }
                    405:       if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
                    406:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    407:       }
                    408:       MH_P_pFq = tk.ival;
1.28      takayama  409:          mh_setA(NULL,tk.ival); /* allocate mem */
1.26      takayama  410:       for (i=0; i<MH_P_pFq; i++) {
                    411:        if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
                    412:          MH_A_pFq[i] = tk.dval;
                    413:        }else if (tk.type == MH_TOKEN_INT) {
                    414:          MH_A_pFq[i] = tk.ival;
                    415:        }else{
                    416:          oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    417:        }
                    418:       }
                    419:       continue;
                    420:     }
                    421:     if (strcmp(s,"q_pFq")==0) {
                    422:       if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
                    423:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    424:       }
                    425:       if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
                    426:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    427:       }
                    428:       MH_Q_pFq = tk.ival;
1.28      takayama  429:          mh_setB(NULL,tk.ival); /* allocate mem */
1.26      takayama  430:       for (i=0; i<MH_Q_pFq; i++) {
                    431:        if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
                    432:          MH_B_pFq[i] = tk.dval;
                    433:        }else if (tk.type == MH_TOKEN_INT) {
                    434:          MH_B_pFq[i] = tk.ival;
                    435:        }else{
                    436:          oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    437:        }
                    438:       }
                    439:       continue;
                    440:     }
1.27      takayama  441:     if (strcmp(s,"ef_type")==0) {
                    442:       if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
                    443:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    444:       }
                    445:       if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
                    446:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    447:       }
                    448:       MH_Ef_type = tk.ival;
                    449:       continue;
                    450:     }
1.28      takayama  451:
                    452:     if (strcmp(s,"Mg")==0) {
                    453:       if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
                    454:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    455:       }
                    456:       if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
                    457:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    458:       }
                    459:       MH_Mg = tk.ival;  MH_M = MH_Mg;
                    460:       MH_RANK = rank = mypower(2,MH_Mg);
                    461:       continue;
                    462:     }
                    463:     if (strcmp(s,"Beta")==0) {
                    464:       if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
                    465:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    466:       }
                    467:          if (MH_Mg <= 0) {
                    468:         oxprintfe("Mg should be set before reading Beta.\n"); mh_exit(-1);
                    469:       }
                    470:       MH_Beta = (double *)mh_malloc(sizeof(double)*MH_Mg);
                    471:          for (i=0; i<MH_Mg; i++) {
                    472:         if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
                    473:            MH_Beta[i] = tk.dval;
                    474:         }else if (tk.type == MH_TOKEN_INT) {
                    475:            MH_Beta[i] = tk.ival;
                    476:         }else {
                    477:           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    478:         }
                    479:       }
                    480:       Iv = (double *)mh_malloc(sizeof(double)*rank);
                    481:          for (i=0; i<rank; i++) {
                    482:                Iv[i] = 0.0;
                    483:          }
                    484:       continue;
                    485:     }
                    486:     if (strcmp(s,"Ng")==0) {
                    487:       if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
                    488:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    489:       }
                    490:
                    491:       if ((tk=mh_getoken(s,SMAX-1,fp)).type== MH_TOKEN_DOUBLE) {
                    492:                *MH_Ng = tk.dval;
                    493:       }else if (tk.type == MH_TOKEN_INT) {
                    494:         *MH_Ng = tk.ival;
                    495:       }else{
                    496:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    497:       }
                    498:       continue;
                    499:     }
                    500:     if (strcmp(s,"X0g")==0) {
                    501:       if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
                    502:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    503:       }
                    504:       if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
                    505:                MH_X0g = tk.dval;
                    506:          }else if (tk.type == MH_TOKEN_INT) {
                    507:                MH_X0g = tk.ival;
                    508:       }else{
                    509:                oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    510:          }
                    511:       continue;
                    512:     }
                    513:        if (strcmp(s,"Iv")==0) {
                    514:       if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
                    515:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    516:       }
                    517:          for (i=0; i<rank; i++) {
                    518:         if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
                    519:                  Iv[i] = tk.dval;
                    520:                }else if (tk.type == MH_TOKEN_INT) {
                    521:                  Iv[i] = tk.ival;
                    522:         }else{
                    523:           oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    524:                }
                    525:       }
                    526:       continue;
                    527:     }
                    528:     if (strcmp(s,"Ef")==0) {
                    529:       if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
                    530:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    531:       }
                    532:       if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
                    533:                Ef = tk.dval;
                    534:          }else if (tk.type == MH_TOKEN_INT) {
                    535:                Ef = tk.ival;
                    536:       }else{
                    537:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    538:          }
                    539:       continue;
                    540:     }
                    541:     if (strcmp(s,"Hg")==0) {
                    542:       if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
                    543:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    544:       }
                    545:       if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
                    546:                MH_Hg = tk.dval;
                    547:          }else if (tk.type == MH_TOKEN_INT) {
                    548:                MH_Hg = tk.ival;
                    549:       }else{
                    550:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    551:       }
                    552:       continue;
                    553:     }
                    554:     if (strcmp(s,"Dp")==0) {
                    555:       if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
                    556:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    557:       }
                    558:       if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
                    559:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    560:       }
                    561:       MH_Dp = tk.dval;
                    562:       continue;
                    563:     }
                    564:     if (strcmp(s,"Xng")==0) {
                    565:       if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
                    566:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    567:       }
                    568:       if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
                    569:                Xng = tk.dval;
                    570:          }else if (tk.type == MH_TOKEN_INT) {
                    571:                Xng = tk.ival;
                    572:       }else{
                    573:         oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
                    574:          }
                    575:       continue;
                    576:     }
                    577:
1.25      takayama  578:     oxprintfe("Unknown ID for wmain.c (old...) at %s.\n",s);  mh_exit(-1);
1.20      takayama  579:   }
1.28      takayama  580:   if (MH_Ef_type == 1) {
                    581:        mh_rf = &mh_rf_ef_type_1;
                    582:   }else if (MH_Ef_type == 2) {
                    583:        mh_rf = &mh_rf_ef_type_2;
                    584:   }else{
                    585:        oxprintfe("Ef_type = %d is not implemented.\n",MH_Ef_type); mh_exit(-1);
                    586:   }
                    587:   if ((MH_M < 0) || (MH_A_pFq==NULL)) {
                    588:        oxprintfe("MH_M and p_pFq, q_pFq are compulsory parameters.\n"); mh_exit(-1);
                    589:   }
1.20      takayama  590:
1.23      takayama  591:   mh_fclose(fp); return(0);
1.1       takayama  592: }
                    593:
1.20      takayama  594: static int showParam() {
1.1       takayama  595:   int rank,i;
1.12      takayama  596:   extern int MH_strategy;
                    597:   extern double MH_abserr;
                    598:   extern double MH_relerr;
1.1       takayama  599:   rank = mypower(2,MH_Mg);
1.22      takayama  600:   oxprintf("%%MH_Mg=\n%d\n",MH_Mg);
1.1       takayama  601:   for (i=0; i<MH_Mg; i++) {
1.22      takayama  602:     oxprintf("%%MH_Beta[%d]=\n%lf\n",i,MH_Beta[i]);
1.1       takayama  603:   }
1.22      takayama  604:   oxprintf("%%MH_Ng=\n%lf\n",*MH_Ng);
                    605:   oxprintf("%%MH_X0g=\n%lf\n",MH_X0g);
1.1       takayama  606:   for (i=0; i<rank; i++) {
1.22      takayama  607:     oxprintf("%%Iv[%d]=\n%lg\n",i,Iv[i]);
1.1       takayama  608:   }
1.22      takayama  609:   oxprintf("%%Ef=\n%lf\n",Ef);
                    610:   oxprintf("%%MH_Hg=\n%lf\n",MH_Hg);
                    611:   oxprintf("%%MH_Dp=\n%d\n",MH_Dp);
                    612:   oxprintf("%%Xng=\n%lf\n",Xng);
                    613:   oxprintf("%%strategy=%d\n",MH_strategy);
                    614:   oxprintf("%%abserr=%lg, %%relerr=%lg\n",MH_abserr,MH_relerr);
                    615:   oxprintf("#MH_success=%d\n",MH_success);
                    616:   oxprintf("#MH_coeff_max=%lg\n",MH_coeff_max);
1.26      takayama  617:   oxprintf("#MH_estimated_start_step=%lg\n",MH_estimated_start_step);
1.27      takayama  618:   oxprintf("%%ef_type=%d\n",MH_Ef_type);
1.26      takayama  619:   oxprintf("%%q_pFq=%d, ",MH_P_pFq);
                    620:   for (i=0; i<MH_P_pFq; i++) {
                    621:     if (i != MH_P_pFq-1) oxprintf(" %lg,",MH_A_pFq[i]);
                    622:     else oxprintf(" %lg\n",MH_A_pFq[i]);
                    623:   }
                    624:   oxprintf("%%q_pFq=%d, ",MH_Q_pFq);
                    625:   for (i=0; i<MH_Q_pFq; i++) {
                    626:     if (i != MH_Q_pFq-1) oxprintf(" %lg,",MH_B_pFq[i]);
                    627:     else oxprintf(" %lg\n",MH_B_pFq[i]);
                    628:   }
                    629:   return(0);
1.1       takayama  630: }
                    631:
1.20      takayama  632: static double estimateHg(int m, double beta[],double x0) {
                    633:   int i,j;
                    634:   double dmin;
                    635:   double cmax;
                    636:   double h;
                    637:   /* mynote on 2014.03.15 */
                    638:   if (m>1) dmin = myabs(beta[1]-beta[0]);
                    639:   else dmin=myabs(beta[0]);
                    640:   for (i=0; i<m; i++) {
                    641:     for (j=i+1; j<m; j++) {
                    642:       if (myabs(beta[i]-beta[j]) < dmin) dmin = myabs(beta[i]-beta[j]);
                    643:     }
                    644:   }
                    645:   dmin = dmin*x0*2;
                    646:   cmax = 1.0;
                    647:   for (i=0; i<m; i++) cmax = cmax*dmin;
                    648:   cmax = 1.0/cmax;
                    649:   MH_coeff_max=cmax;
                    650:   h = exp(log(MH_abserr/cmax)/5.0);
                    651:   MH_estimated_start_step = h;
                    652:   return h;
                    653: }
1.28      takayama  654:
                    655: void mh_setA(double a[],int alen) {
                    656:   int i;
                    657:   if (alen < 0) {
                    658:        if (MH_A_pFq != NULL) mh_free(MH_A_pFq);
                    659:        MH_A_pFq=NULL; MH_P_pFq=-1;
                    660:        return;
                    661:   }
                    662:   if (alen == 0) {
                    663:        MH_P_pFq=0; return;
                    664:   }
                    665:   MH_P_pFq=alen;
                    666:   MH_A_pFq = (double *)mh_malloc(MH_P_pFq*sizeof(double));
                    667:   if (a != NULL) {
                    668:        for (i=0; i<alen; i++) MH_A_pFq[i] = a[i];
                    669:   }else{
                    670:        for (i=0; i<alen; i++) MH_A_pFq[i] = 0.0;
                    671:   }
                    672:   return;
                    673: }
                    674: void mh_setB(double b[],int blen) {
                    675:   int i;
                    676:   if (blen < 0) {
                    677:        if (MH_B_pFq != NULL) mh_free(MH_B_pFq);
                    678:        MH_B_pFq=NULL; MH_Q_pFq=-1;
                    679:        return;
                    680:   }
                    681:   if (blen == 0) {
                    682:        MH_Q_pFq=0; return;
                    683:   }
                    684:   MH_Q_pFq=blen;
                    685:   MH_B_pFq = (double *)mh_malloc(MH_Q_pFq*sizeof(double));
                    686:   if (b != NULL) {
                    687:        for (i=0; i<blen; i++) MH_B_pFq[i] = b[i];
                    688:   }else{
                    689:        for (i=0; i<blen; i++) MH_B_pFq[i] = 0.0;
                    690:   }
                    691:   return;
                    692: }

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