Annotation of OpenXM/src/hgm/mh/src/wmain.c, Revision 1.33
1.1 takayama 1: /*
1.33 ! takayama 2: $OpenXM: OpenXM/src/hgm/mh/src/wmain.c,v 1.32 2016/02/16 02:17: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.32 takayama 182: oxprintfe("%%%%Warning, abserr seems not to be small enough, abserr=%lg, y[0]=%lg. Increasing the starting point (q0 or X0g(standalone case)) may or making abserr (err[1] or abserror(standalone case)) smaller will help.\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;
1.33 ! takayama 245: MH_Dp = 1000;
1.23 takayama 246: Xng = 10.0; return(0);
1.1 takayama 247: }
1.31 takayama 248: #ifdef STANDALONE
249: /* may remove */
1.28 takayama 250: static int next_old1(struct SFILE *sfp,char *s,char *msg) {
1.1 takayama 251: s[0] = '%';
252: while (s[0] == '%') {
1.10 takayama 253: if (!mh_fgets(s,SMAX,sfp)) {
1.22 takayama 254: oxprintfe("Data format error at %s\n",msg);
1.10 takayama 255: mh_exit(-1);
256: }
257: if (s[0] != '%') return(0);
1.23 takayama 258: } return(0);
1.1 takayama 259: }
1.31 takayama 260: #endif
1.28 takayama 261: static int next(struct SFILE *sfp,char *s,char *msg) {
262: static int check=1;
263: char *ng="%%Ng=";
264: // int i;
265: s[0] = '%';
266: while ((s[0] == '%') || (s[0] == '#')) {
267: if (!mh_fgets(s,SMAX,sfp)) {
268: oxprintfe("Data format error at %s\n",msg);
269: oxprintfe("Is it version 2.0 format? If so, add\n%s\nat the top.\n",VSTRING);
270: mh_exit(-1);
271: }
272: if ((s[0] == '%') && (s[1] == '%')) continue;
273: if (s[0] == '#') continue;
274: if (strncmp(s,VSTRING,strlen(VSTRING)) == 0) {
275: return(2);
276: }
277: if (check && (strncmp(msg,ng,4)==0)) {
278: if (strncmp(s,ng,5) != 0) {
279: oxprintfe("Warning, there is no %%Ng= at the border of Beta's and Ng, s=%s\n",s);
280: }
281: /* check=0; */
282: }
283: if (s[0] != '%') return(0);
284: }
285: return(0);
286: }
287:
1.8 takayama 288: static int setParam(char *fname) {
1.1 takayama 289: int rank;
290: char s[SMAX];
1.4 takayama 291: struct SFILE *fp;
1.1 takayama 292: int i;
1.28 takayama 293: int version;
294: double a[1]; double b[1]; /* for old inputs */
1.20 takayama 295: struct mh_token tk;
1.3 takayama 296: extern int MH_deallocate;
1.4 takayama 297: extern int MH_byFile;
1.3 takayama 298: if (MH_deallocate) {
1.28 takayama 299: if (MH_Beta) {mh_free(MH_Beta); MH_Beta=NULL;}
300: if (MH_Ng) {mh_free(MH_Ng); MH_Ng=NULL;}
301: if (Iv) {mh_free(Iv); Iv = NULL;}
302: if (MH_A_pFq) {mh_free(MH_A_pFq); MH_A_pFq=NULL; MH_P_pFq = 0;}
303: if (MH_B_pFq) {mh_free(MH_B_pFq); MH_B_pFq=NULL; MH_Q_pFq = 0;}
1.10 takayama 304: return(0);
1.3 takayama 305: }
1.1 takayama 306: if (fname == NULL) return(setParamDefault());
307:
1.4 takayama 308: if ((fp=mh_fopen(fname,"r",MH_byFile)) == NULL) {
1.22 takayama 309: oxprintfe("File %s is not found.\n",fname);
1.10 takayama 310: mh_exit(-1);
1.1 takayama 311: }
1.28 takayama 312: /* set default initial values */
313: MH_Mg=MH_M=-1; /* number of variables */
314: MH_Ng=(double *) mh_malloc(sizeof(double)); *MH_Ng=-1; /* *Ng is the degree of freedom 1F1 */
315: MH_X0g=0.1; /* evaluation point */
316: Ef=1.0; /* exponential factor */
317: MH_Ef_type=1;
318: MH_Hg=0.01; /* step size for RK */
1.33 ! takayama 319: MH_Dp = -1; /* sampling rate */
1.28 takayama 320: Xng = 10.0; /* terminal point for RK */
321:
322: /* Parser for the old style (version <2.0) input */
323: version=next(fp,s,"Mg(m)");
324: if (version == 2) goto mh_myparse;
1.1 takayama 325: sscanf(s,"%d",&MH_Mg); MH_M=MH_Mg;
326: MH_RANK=rank = mypower(2,MH_Mg);
327:
1.2 takayama 328: MH_Beta = (double *)mh_malloc(sizeof(double)*MH_Mg);
1.1 takayama 329: for (i=0; i<MH_Mg; i++) {
330: next(fp,s,"MH_Beta");
1.10 takayama 331: sscanf(s,"%lf",&(MH_Beta[i]));
1.1 takayama 332: }
333:
1.2 takayama 334: next(fp,s,"MH_Ng(freedom parameter n)");
335: sscanf(s,"%lf",MH_Ng);
1.28 takayama 336: /* for old style input only for 1F1 ef_type==1. */
337: MH_P_pFq = 1; MH_Q_pFq = 1;
338: a[0] = (MH_M+1.0)/2.0;
339: b[0] = a[0]+(*MH_Ng)/2.0;
340: mh_setA(a,1); mh_setB(b,1);
341: mh_rf = & mh_rf_ef_type_1;
342:
1.1 takayama 343:
1.2 takayama 344: next(fp,s,"MH_X0g(initial point)");
345: sscanf(s,"%lf",&MH_X0g);
1.1 takayama 346:
1.2 takayama 347: Iv = (double *)mh_malloc(sizeof(double)*rank);
1.1 takayama 348: for (i=0; i<rank; i++) {
1.10 takayama 349: next(fp,s,"Iv(initial values)");
350: sscanf(s,"%lg",&(Iv[i]));
1.1 takayama 351: }
352:
353: next(fp,s,"Ef(exponential factor)");
354: sscanf(s,"%lg",&Ef);
355:
1.2 takayama 356: next(fp,s,"MH_Hg (step size of rk)");
357: sscanf(s,"%lg",&MH_Hg);
1.1 takayama 358:
1.2 takayama 359: next(fp,s,"MH_Dp (data sampling period)");
360: sscanf(s,"%d",&MH_Dp);
1.1 takayama 361:
362: next(fp,s,"Xng (the last point, cf. --xmax)");
363: sscanf(s,"%lf",&Xng);
1.20 takayama 364:
365: /* Reading the optional parameters */
1.28 takayama 366: mh_myparse:
1.20 takayama 367: while ((tk = mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_EOF) {
368: /* expect ID */
369: if (tk.type != MH_TOKEN_ID) {
1.22 takayama 370: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
1.20 takayama 371: }
372: if ((strcmp(s,"abserr")==0) || (strcmp(s,"abserror")==0)) {
373: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
1.22 takayama 374: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
1.20 takayama 375: }
1.30 takayama 376: if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
377: MH_abserr = tk.dval;
378: }else if (tk.type == MH_TOKEN_INT) {
379: MH_abserr = tk.ival;
380: }else{
1.22 takayama 381: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
1.20 takayama 382: }
383: continue;
384: }
385: if ((strcmp(s,"relerr")==0) || (strcmp(s,"relerror")==0)) {
386: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
1.22 takayama 387: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
1.20 takayama 388: }
1.30 takayama 389: if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
390: MH_relerr = tk.dval;
391: }else if (tk.type == MH_TOKEN_INT) {
392: MH_relerr = tk.dval;
393: }else{
1.22 takayama 394: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
1.20 takayama 395: }
396: continue;
397: }
398: if (strcmp(s,"strategy")==0) {
399: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
1.22 takayama 400: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
1.20 takayama 401: }
402: if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
1.22 takayama 403: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
1.20 takayama 404: }
405: MH_strategy = tk.ival;
406: continue;
407: }
1.28 takayama 408: // Format: #p_pFq=2 1.5 3.2 override by new input format.
1.26 takayama 409: if (strcmp(s,"p_pFq")==0) {
410: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
411: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
412: }
413: if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
414: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
415: }
416: MH_P_pFq = tk.ival;
1.28 takayama 417: mh_setA(NULL,tk.ival); /* allocate mem */
1.26 takayama 418: for (i=0; i<MH_P_pFq; i++) {
419: if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
420: MH_A_pFq[i] = tk.dval;
421: }else if (tk.type == MH_TOKEN_INT) {
422: MH_A_pFq[i] = tk.ival;
423: }else{
424: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
425: }
426: }
427: continue;
428: }
429: if (strcmp(s,"q_pFq")==0) {
430: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
431: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
432: }
433: if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
434: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
435: }
436: MH_Q_pFq = tk.ival;
1.28 takayama 437: mh_setB(NULL,tk.ival); /* allocate mem */
1.26 takayama 438: for (i=0; i<MH_Q_pFq; i++) {
439: if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
440: MH_B_pFq[i] = tk.dval;
441: }else if (tk.type == MH_TOKEN_INT) {
442: MH_B_pFq[i] = tk.ival;
443: }else{
444: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
445: }
446: }
447: continue;
448: }
1.27 takayama 449: if (strcmp(s,"ef_type")==0) {
450: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
451: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
452: }
453: if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
454: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
455: }
456: MH_Ef_type = tk.ival;
457: continue;
458: }
1.28 takayama 459:
460: if (strcmp(s,"Mg")==0) {
461: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
462: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
463: }
464: if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
465: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
466: }
467: MH_Mg = tk.ival; MH_M = MH_Mg;
468: MH_RANK = rank = mypower(2,MH_Mg);
469: continue;
470: }
471: if (strcmp(s,"Beta")==0) {
472: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
473: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
474: }
475: if (MH_Mg <= 0) {
476: oxprintfe("Mg should be set before reading Beta.\n"); mh_exit(-1);
477: }
478: MH_Beta = (double *)mh_malloc(sizeof(double)*MH_Mg);
479: for (i=0; i<MH_Mg; i++) {
480: if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
481: MH_Beta[i] = tk.dval;
482: }else if (tk.type == MH_TOKEN_INT) {
483: MH_Beta[i] = tk.ival;
484: }else {
485: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
486: }
487: }
488: Iv = (double *)mh_malloc(sizeof(double)*rank);
489: for (i=0; i<rank; i++) {
490: Iv[i] = 0.0;
491: }
492: continue;
493: }
494: if (strcmp(s,"Ng")==0) {
495: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
496: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
497: }
498:
499: if ((tk=mh_getoken(s,SMAX-1,fp)).type== MH_TOKEN_DOUBLE) {
500: *MH_Ng = tk.dval;
501: }else if (tk.type == MH_TOKEN_INT) {
502: *MH_Ng = tk.ival;
503: }else{
504: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
505: }
506: continue;
507: }
508: if (strcmp(s,"X0g")==0) {
509: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
510: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
511: }
512: if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
513: MH_X0g = tk.dval;
514: }else if (tk.type == MH_TOKEN_INT) {
515: MH_X0g = tk.ival;
516: }else{
517: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
518: }
519: continue;
520: }
521: if (strcmp(s,"Iv")==0) {
522: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
523: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
524: }
525: for (i=0; i<rank; i++) {
526: if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
527: Iv[i] = tk.dval;
528: }else if (tk.type == MH_TOKEN_INT) {
529: Iv[i] = tk.ival;
530: }else{
531: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
532: }
533: }
534: continue;
535: }
536: if (strcmp(s,"Ef")==0) {
537: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
538: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
539: }
540: if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
541: Ef = tk.dval;
542: }else if (tk.type == MH_TOKEN_INT) {
543: Ef = tk.ival;
544: }else{
545: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
546: }
547: continue;
548: }
549: if (strcmp(s,"Hg")==0) {
550: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
551: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
552: }
553: if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
554: MH_Hg = tk.dval;
555: }else if (tk.type == MH_TOKEN_INT) {
556: MH_Hg = tk.ival;
557: }else{
558: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
559: }
560: continue;
561: }
562: if (strcmp(s,"Dp")==0) {
563: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
564: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
565: }
566: if ((tk=mh_getoken(s,SMAX-1,fp)).type != MH_TOKEN_INT) {
567: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
568: }
569: MH_Dp = tk.dval;
570: continue;
571: }
572: if (strcmp(s,"Xng")==0) {
573: if (mh_getoken(s,SMAX-1,fp).type != MH_TOKEN_EQ) {
574: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
575: }
576: if ((tk=mh_getoken(s,SMAX-1,fp)).type == MH_TOKEN_DOUBLE) {
577: Xng = tk.dval;
578: }else if (tk.type == MH_TOKEN_INT) {
579: Xng = tk.ival;
580: }else{
581: oxprintfe("Syntax error at %s\n",s); mh_exit(-1);
582: }
583: continue;
584: }
585:
1.25 takayama 586: oxprintfe("Unknown ID for wmain.c (old...) at %s.\n",s); mh_exit(-1);
1.20 takayama 587: }
1.28 takayama 588: if (MH_Ef_type == 1) {
589: mh_rf = &mh_rf_ef_type_1;
590: }else if (MH_Ef_type == 2) {
591: mh_rf = &mh_rf_ef_type_2;
592: }else{
593: oxprintfe("Ef_type = %d is not implemented.\n",MH_Ef_type); mh_exit(-1);
594: }
595: if ((MH_M < 0) || (MH_A_pFq==NULL)) {
596: oxprintfe("MH_M and p_pFq, q_pFq are compulsory parameters.\n"); mh_exit(-1);
597: }
1.33 ! takayama 598: if (MH_Dp < 0) MH_Dp = (int) floor(1/MH_Hg);
1.20 takayama 599:
1.23 takayama 600: mh_fclose(fp); return(0);
1.1 takayama 601: }
602:
1.20 takayama 603: static int showParam() {
1.1 takayama 604: int rank,i;
1.12 takayama 605: extern int MH_strategy;
606: extern double MH_abserr;
607: extern double MH_relerr;
1.1 takayama 608: rank = mypower(2,MH_Mg);
1.22 takayama 609: oxprintf("%%MH_Mg=\n%d\n",MH_Mg);
1.1 takayama 610: for (i=0; i<MH_Mg; i++) {
1.22 takayama 611: oxprintf("%%MH_Beta[%d]=\n%lf\n",i,MH_Beta[i]);
1.1 takayama 612: }
1.22 takayama 613: oxprintf("%%MH_Ng=\n%lf\n",*MH_Ng);
614: oxprintf("%%MH_X0g=\n%lf\n",MH_X0g);
1.1 takayama 615: for (i=0; i<rank; i++) {
1.22 takayama 616: oxprintf("%%Iv[%d]=\n%lg\n",i,Iv[i]);
1.1 takayama 617: }
1.22 takayama 618: oxprintf("%%Ef=\n%lf\n",Ef);
619: oxprintf("%%MH_Hg=\n%lf\n",MH_Hg);
620: oxprintf("%%MH_Dp=\n%d\n",MH_Dp);
621: oxprintf("%%Xng=\n%lf\n",Xng);
622: oxprintf("%%strategy=%d\n",MH_strategy);
623: oxprintf("%%abserr=%lg, %%relerr=%lg\n",MH_abserr,MH_relerr);
624: oxprintf("#MH_success=%d\n",MH_success);
625: oxprintf("#MH_coeff_max=%lg\n",MH_coeff_max);
1.26 takayama 626: oxprintf("#MH_estimated_start_step=%lg\n",MH_estimated_start_step);
1.27 takayama 627: oxprintf("%%ef_type=%d\n",MH_Ef_type);
1.26 takayama 628: oxprintf("%%q_pFq=%d, ",MH_P_pFq);
629: for (i=0; i<MH_P_pFq; i++) {
630: if (i != MH_P_pFq-1) oxprintf(" %lg,",MH_A_pFq[i]);
631: else oxprintf(" %lg\n",MH_A_pFq[i]);
632: }
633: oxprintf("%%q_pFq=%d, ",MH_Q_pFq);
634: for (i=0; i<MH_Q_pFq; i++) {
635: if (i != MH_Q_pFq-1) oxprintf(" %lg,",MH_B_pFq[i]);
636: else oxprintf(" %lg\n",MH_B_pFq[i]);
637: }
638: return(0);
1.1 takayama 639: }
640:
1.20 takayama 641: static double estimateHg(int m, double beta[],double x0) {
642: int i,j;
643: double dmin;
644: double cmax;
645: double h;
646: /* mynote on 2014.03.15 */
647: if (m>1) dmin = myabs(beta[1]-beta[0]);
648: else dmin=myabs(beta[0]);
649: for (i=0; i<m; i++) {
650: for (j=i+1; j<m; j++) {
651: if (myabs(beta[i]-beta[j]) < dmin) dmin = myabs(beta[i]-beta[j]);
652: }
653: }
654: dmin = dmin*x0*2;
655: cmax = 1.0;
656: for (i=0; i<m; i++) cmax = cmax*dmin;
657: cmax = 1.0/cmax;
658: MH_coeff_max=cmax;
659: h = exp(log(MH_abserr/cmax)/5.0);
660: MH_estimated_start_step = h;
661: return h;
662: }
1.28 takayama 663:
664: void mh_setA(double a[],int alen) {
665: int i;
666: if (alen < 0) {
667: if (MH_A_pFq != NULL) mh_free(MH_A_pFq);
668: MH_A_pFq=NULL; MH_P_pFq=-1;
669: return;
670: }
671: if (alen == 0) {
672: MH_P_pFq=0; return;
673: }
674: MH_P_pFq=alen;
675: MH_A_pFq = (double *)mh_malloc(MH_P_pFq*sizeof(double));
676: if (a != NULL) {
677: for (i=0; i<alen; i++) MH_A_pFq[i] = a[i];
678: }else{
679: for (i=0; i<alen; i++) MH_A_pFq[i] = 0.0;
680: }
681: return;
682: }
683: void mh_setB(double b[],int blen) {
684: int i;
685: if (blen < 0) {
686: if (MH_B_pFq != NULL) mh_free(MH_B_pFq);
687: MH_B_pFq=NULL; MH_Q_pFq=-1;
688: return;
689: }
690: if (blen == 0) {
691: MH_Q_pFq=0; return;
692: }
693: MH_Q_pFq=blen;
694: MH_B_pFq = (double *)mh_malloc(MH_Q_pFq*sizeof(double));
695: if (b != NULL) {
696: for (i=0; i<blen; i++) MH_B_pFq[i] = b[i];
697: }else{
698: for (i=0; i<blen; i++) MH_B_pFq[i] = 0.0;
699: }
700: return;
701: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>