Annotation of OpenXM/src/hgm/mh/src/wmain.c, Revision 1.30
1.1 takayama 1: /*
1.30 ! takayama 2: $OpenXM: OpenXM/src/hgm/mh/src/wmain.c,v 1.29 2016/02/13 02:34:20 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: }
1.30 ! takayama 374: if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
! 375: MH_abserr = tk.dval;
! 376: }else if (tk.type == MH_TOKEN_INT) {
! 377: MH_abserr = tk.ival;
! 378: }else{
1.22 takayama 379: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
1.20 takayama 380: }
381: continue;
382: }
383: if ((strcmp(s,"relerr")==0) || (strcmp(s,"relerror")==0)) {
384: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
1.22 takayama 385: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
1.20 takayama 386: }
1.30 ! takayama 387: if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
! 388: MH_relerr = tk.dval;
! 389: }else if (tk.type == MH_TOKEN_INT) {
! 390: MH_relerr = tk.dval;
! 391: }else{
1.22 takayama 392: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
1.20 takayama 393: }
394: continue;
395: }
396: if (strcmp(s,"strategy")==0) {
397: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
1.22 takayama 398: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
1.20 takayama 399: }
400: if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
1.22 takayama 401: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
1.20 takayama 402: }
403: MH_strategy = tk.ival;
404: continue;
405: }
1.28 takayama 406: // Format: #p_pFq=2 1.5 3.2 override by new input format.
1.26 takayama 407: if (strcmp(s,"p_pFq")==0) {
408: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
409: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
410: }
411: if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
412: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
413: }
414: MH_P_pFq = tk.ival;
1.28 takayama 415: mh_setA(NULL,tk.ival); /* allocate mem */
1.26 takayama 416: for (i=0; i<MH_P_pFq; i++) {
417: if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
418: MH_A_pFq[i] = tk.dval;
419: }else if (tk.type == MH_TOKEN_INT) {
420: MH_A_pFq[i] = tk.ival;
421: }else{
422: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
423: }
424: }
425: continue;
426: }
427: if (strcmp(s,"q_pFq")==0) {
428: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
429: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
430: }
431: if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
432: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
433: }
434: MH_Q_pFq = tk.ival;
1.28 takayama 435: mh_setB(NULL,tk.ival); /* allocate mem */
1.26 takayama 436: for (i=0; i<MH_Q_pFq; i++) {
437: if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
438: MH_B_pFq[i] = tk.dval;
439: }else if (tk.type == MH_TOKEN_INT) {
440: MH_B_pFq[i] = tk.ival;
441: }else{
442: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
443: }
444: }
445: continue;
446: }
1.27 takayama 447: if (strcmp(s,"ef_type")==0) {
448: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
449: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
450: }
451: if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
452: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
453: }
454: MH_Ef_type = tk.ival;
455: continue;
456: }
1.28 takayama 457:
458: if (strcmp(s,"Mg")==0) {
459: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
460: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
461: }
462: if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
463: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
464: }
465: MH_Mg = tk.ival; MH_M = MH_Mg;
466: MH_RANK = rank = mypower(2,MH_Mg);
467: continue;
468: }
469: if (strcmp(s,"Beta")==0) {
470: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
471: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
472: }
473: if (MH_Mg <= 0) {
474: oxprintfe("Mg should be set before reading Beta.\n"); mh_exit(-1);
475: }
476: MH_Beta = (double *)mh_malloc(sizeof(double)*MH_Mg);
477: for (i=0; i<MH_Mg; i++) {
478: if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
479: MH_Beta[i] = tk.dval;
480: }else if (tk.type == MH_TOKEN_INT) {
481: MH_Beta[i] = tk.ival;
482: }else {
483: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
484: }
485: }
486: Iv = (double *)mh_malloc(sizeof(double)*rank);
487: for (i=0; i<rank; i++) {
488: Iv[i] = 0.0;
489: }
490: continue;
491: }
492: if (strcmp(s,"Ng")==0) {
493: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
494: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
495: }
496:
497: if ((tk=mh_getoken(s,SMAX-1,fp)).type== MH_TOKEN_DOUBLE) {
498: *MH_Ng = tk.dval;
499: }else if (tk.type == MH_TOKEN_INT) {
500: *MH_Ng = tk.ival;
501: }else{
502: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
503: }
504: continue;
505: }
506: if (strcmp(s,"X0g")==0) {
507: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
508: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
509: }
510: if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
511: MH_X0g = tk.dval;
512: }else if (tk.type == MH_TOKEN_INT) {
513: MH_X0g = tk.ival;
514: }else{
515: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
516: }
517: continue;
518: }
519: if (strcmp(s,"Iv")==0) {
520: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
521: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
522: }
523: for (i=0; i<rank; i++) {
524: if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
525: Iv[i] = tk.dval;
526: }else if (tk.type == MH_TOKEN_INT) {
527: Iv[i] = tk.ival;
528: }else{
529: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
530: }
531: }
532: continue;
533: }
534: if (strcmp(s,"Ef")==0) {
535: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
536: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
537: }
538: if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
539: Ef = tk.dval;
540: }else if (tk.type == MH_TOKEN_INT) {
541: Ef = tk.ival;
542: }else{
543: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
544: }
545: continue;
546: }
547: if (strcmp(s,"Hg")==0) {
548: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
549: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
550: }
551: if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
552: MH_Hg = tk.dval;
553: }else if (tk.type == MH_TOKEN_INT) {
554: MH_Hg = tk.ival;
555: }else{
556: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
557: }
558: continue;
559: }
560: if (strcmp(s,"Dp")==0) {
561: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
562: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
563: }
564: if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
565: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
566: }
567: MH_Dp = tk.dval;
568: continue;
569: }
570: if (strcmp(s,"Xng")==0) {
571: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
572: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
573: }
574: if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
575: Xng = tk.dval;
576: }else if (tk.type == MH_TOKEN_INT) {
577: Xng = tk.ival;
578: }else{
579: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
580: }
581: continue;
582: }
583:
1.25 takayama 584: oxprintfe("Unknown ID for wmain.c (old...) at %s.\n",s); mh_exit(-1);
1.20 takayama 585: }
1.28 takayama 586: if (MH_Ef_type == 1) {
587: mh_rf = &mh_rf_ef_type_1;
588: }else if (MH_Ef_type == 2) {
589: mh_rf = &mh_rf_ef_type_2;
590: }else{
591: oxprintfe("Ef_type = %d is not implemented.\n",MH_Ef_type); mh_exit(-1);
592: }
593: if ((MH_M < 0) || (MH_A_pFq==NULL)) {
594: oxprintfe("MH_M and p_pFq, q_pFq are compulsory parameters.\n"); mh_exit(-1);
595: }
1.20 takayama 596:
1.23 takayama 597: mh_fclose(fp); return(0);
1.1 takayama 598: }
599:
1.20 takayama 600: static int showParam() {
1.1 takayama 601: int rank,i;
1.12 takayama 602: extern int MH_strategy;
603: extern double MH_abserr;
604: extern double MH_relerr;
1.1 takayama 605: rank = mypower(2,MH_Mg);
1.22 takayama 606: oxprintf("%%MH_Mg=\n%d\n",MH_Mg);
1.1 takayama 607: for (i=0; i<MH_Mg; i++) {
1.22 takayama 608: oxprintf("%%MH_Beta[%d]=\n%lf\n",i,MH_Beta[i]);
1.1 takayama 609: }
1.22 takayama 610: oxprintf("%%MH_Ng=\n%lf\n",*MH_Ng);
611: oxprintf("%%MH_X0g=\n%lf\n",MH_X0g);
1.1 takayama 612: for (i=0; i<rank; i++) {
1.22 takayama 613: oxprintf("%%Iv[%d]=\n%lg\n",i,Iv[i]);
1.1 takayama 614: }
1.22 takayama 615: oxprintf("%%Ef=\n%lf\n",Ef);
616: oxprintf("%%MH_Hg=\n%lf\n",MH_Hg);
617: oxprintf("%%MH_Dp=\n%d\n",MH_Dp);
618: oxprintf("%%Xng=\n%lf\n",Xng);
619: oxprintf("%%strategy=%d\n",MH_strategy);
620: oxprintf("%%abserr=%lg, %%relerr=%lg\n",MH_abserr,MH_relerr);
621: oxprintf("#MH_success=%d\n",MH_success);
622: oxprintf("#MH_coeff_max=%lg\n",MH_coeff_max);
1.26 takayama 623: oxprintf("#MH_estimated_start_step=%lg\n",MH_estimated_start_step);
1.27 takayama 624: oxprintf("%%ef_type=%d\n",MH_Ef_type);
1.26 takayama 625: oxprintf("%%q_pFq=%d, ",MH_P_pFq);
626: for (i=0; i<MH_P_pFq; i++) {
627: if (i != MH_P_pFq-1) oxprintf(" %lg,",MH_A_pFq[i]);
628: else oxprintf(" %lg\n",MH_A_pFq[i]);
629: }
630: oxprintf("%%q_pFq=%d, ",MH_Q_pFq);
631: for (i=0; i<MH_Q_pFq; i++) {
632: if (i != MH_Q_pFq-1) oxprintf(" %lg,",MH_B_pFq[i]);
633: else oxprintf(" %lg\n",MH_B_pFq[i]);
634: }
635: return(0);
1.1 takayama 636: }
637:
1.20 takayama 638: static double estimateHg(int m, double beta[],double x0) {
639: int i,j;
640: double dmin;
641: double cmax;
642: double h;
643: /* mynote on 2014.03.15 */
644: if (m>1) dmin = myabs(beta[1]-beta[0]);
645: else dmin=myabs(beta[0]);
646: for (i=0; i<m; i++) {
647: for (j=i+1; j<m; j++) {
648: if (myabs(beta[i]-beta[j]) < dmin) dmin = myabs(beta[i]-beta[j]);
649: }
650: }
651: dmin = dmin*x0*2;
652: cmax = 1.0;
653: for (i=0; i<m; i++) cmax = cmax*dmin;
654: cmax = 1.0/cmax;
655: MH_coeff_max=cmax;
656: h = exp(log(MH_abserr/cmax)/5.0);
657: MH_estimated_start_step = h;
658: return h;
659: }
1.28 takayama 660:
661: void mh_setA(double a[],int alen) {
662: int i;
663: if (alen < 0) {
664: if (MH_A_pFq != NULL) mh_free(MH_A_pFq);
665: MH_A_pFq=NULL; MH_P_pFq=-1;
666: return;
667: }
668: if (alen == 0) {
669: MH_P_pFq=0; return;
670: }
671: MH_P_pFq=alen;
672: MH_A_pFq = (double *)mh_malloc(MH_P_pFq*sizeof(double));
673: if (a != NULL) {
674: for (i=0; i<alen; i++) MH_A_pFq[i] = a[i];
675: }else{
676: for (i=0; i<alen; i++) MH_A_pFq[i] = 0.0;
677: }
678: return;
679: }
680: void mh_setB(double b[],int blen) {
681: int i;
682: if (blen < 0) {
683: if (MH_B_pFq != NULL) mh_free(MH_B_pFq);
684: MH_B_pFq=NULL; MH_Q_pFq=-1;
685: return;
686: }
687: if (blen == 0) {
688: MH_Q_pFq=0; return;
689: }
690: MH_Q_pFq=blen;
691: MH_B_pFq = (double *)mh_malloc(MH_Q_pFq*sizeof(double));
692: if (b != NULL) {
693: for (i=0; i<blen; i++) MH_B_pFq[i] = b[i];
694: }else{
695: for (i=0; i<blen; i++) MH_B_pFq[i] = 0.0;
696: }
697: return;
698: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>