00001
00002 #include <string.h>
00003 #include <math.h>
00004
00005 #include <sys/timeb.h>
00006 #include <libxml/xmlmemory.h>
00007 #include <libxml/parser.h>
00008 #include <iostream>
00009 #include "def.h"
00010 #include "run.h"
00011 #include "io.h"
00012 using namespace std;
00013 #ifndef RAND_MAX
00014 #define RAND_MAX 2147483647
00015 #endif
00016 namespace Run {
00017 struct Option option={0,0,0,0};
00018 void (*usage)();
00019 char
00020 programname[MAXLINLEN],
00021 configfile[MAXLINLEN],
00022 outputfile[MAXLINLEN],
00023 inputfile[MAXLINLEN],
00024 outputname[MAXLINLEN];
00025 #ifdef OMP
00026 int nthreads;
00027 #endif
00028 struct timeb worldtime;
00029 struct Time time;
00031 void testrnd() {
00032 for(int i=0;i<20;i++){
00033 long irnd=rand();
00034 double rnd=(double)irnd/(double)(RAND_MAX);
00035 REAL frnd=(REAL)rnd;
00036 cout<<i<<": irnd="<<irnd
00037 <<", RAND_MAX="<<RAND_MAX
00038 <<", rnd="<<rnd
00039 <<", frnd="<<frnd<<endl;
00040 }
00041 }
00043
00044
00045
00046
00047
00048
00049
00051 REAL rnd()
00052 { return (REAL)((double)rand()/RAND_MAX);
00053 }
00057 REAL gauss() {
00058 static int iset = 0;
00059 static REAL gset;
00060 REAL fac,rsq,v1,v2;
00061 if (iset == 0)
00062 { do {
00063 v1=2.*rnd()-1.;
00064 v2=2.*rnd()-1.;
00065 rsq=v1*v1+v2*v2;
00066 } while (rsq >= 1.0 || rsq == 0);
00067 fac=(REAL)sqrt(-2.*log(rsq)/rsq);
00068 gset=v1*fac;
00069 iset=1;
00070 return v2*fac;
00071 }
00072 else
00073 {
00074 iset=0;
00075 return gset;
00076 }
00077 }
00079 void init(int argc, char *argv[])
00080 { using namespace IO;
00081 option.verbose=0;
00082 option.debug=0;
00083 option.mesh=0;
00084 option.xterm=0;
00085 option.restart=0;
00086 ftime(&worldtime);
00087
00088 srandom(777);
00089 readcmdline(argc, argv);
00090 getTime(configfile);
00091 }
00093 void readcmdline(int argc, char *argv[]) {
00094 char *p,*q;
00095 sprintf(configfile,"%s.xml",argv[0]);
00096 sprintf(outputfile,"%s.dat.gz",argv[0]);
00097 inputfile[0]='\0';
00098 strcpy(outputname,argv[0]);
00099 for (p=argv[0];!isalpha(*p)&&*p!='\0';p++);
00100 strcpy(programname,p);
00101 for (int i=1; i<argc; i++)
00102 { if (*argv[i]=='-')
00103 switch (*(p=argv[i]+1))
00104 { case 'f':
00105 if(i++>=argc)goto Usage;
00106 if(*argv[i]=='-')goto Usage;
00107 strcpy(configfile,argv[i]);
00108 break;
00109 case 'm':
00110 option.mesh=1;
00111 break;
00112 case 'v':
00113 option.verbose=1;
00114 cout<<"Verbose Mode\n";
00115 break;
00116 case 'V':
00117 option.debug=1;
00118 cout<<"Debug Mode\n";cout.flush();
00119 break;
00120 case 'o':
00121 if(i++>=argc)goto Usage;
00122 if(*argv[i]=='-')goto Usage;
00123 strcpy(outputfile,argv[i]);
00124 break;
00125 case 'h':
00126 goto Usage;
00127 break;
00128 } else {
00129 sprintf(inputfile,"%s",argv[i]);
00130 }
00131 }
00132 option.restart=inputfile[0]=='\0'?0:1;
00133 return;
00134 Usage:
00135 (*usage)();
00136 exit(1);
00137 }
00138 int elapsed() {
00139 struct timeb currenttime;
00140 ftime(¤ttime);
00141 return currenttime.time-worldtime.time;
00142 }
00143 }