/* -- rdparams.c -- * Read text file "inputdeck" (how quaint) * and fill in values in the structure/common block */ #include #ifndef _WIN32 #include #endif #include #include #include "rdparams.h" /* ---------------------------------------------------------------- Read keyword value pairs and initialize control structures. */ static int match( char *token, char *word, int tokenlength ) { int n,i; n = strlen(word); if ( n>tokenlength ) n= tokenlength; i = !strncmp(token,word,n); return i; } /* ---------------------------------------------------------------- ---------------------------------------------------------------- */ void rdparams() { FILE *cmdin; char line[200], *keyword; char name[256]; int nc,i,j; static char *seps = " ,\t\n"; char *cp; char save_root[256]; #ifdef WIN32 #define SLASH "\\" #define CSLASH '\\' #else #define SLASH "/" #define CSLASH '/' #endif double dx; name[0]= 0; /* this will still be nil at the end if there is no 'root' line */ globals.restart= 0; /* Set defaults */ globals.ndump= 0; /* Next dump number to write */ globals.nthreads= 1; globals.dtdump= 0.1f; /* Interval between dumps */ globals.stop_step= 100; globals.stop_time= 100.0f; globals.gamma= 1.4f; globals.safety = 0.8f; globals.dtmin = 0.0f; globals.dtmax = 1000.0f; globals.stop_time = 1.0f; strcpy( globals.runName, "x00a" ); strcpy( globals.rootDir, SLASH ); strcpy( globals.homeDir, SLASH ); strcpy( globals.globalsName, "restart" ); globals.tdump = 0.0f; /* Time of next dump */ globals.ncheckpoint = 0; /* timestep number of next checkpoint */ globals.dtime = 0.001f; /* current dt for ppm */ globals.nstep = 0; /* Last step completed (0 at start) */ globals.time= 0.0f; /* Current simulation time */ /* Problem dependent stuff will go here. No defaults! */ fprintf(stderr,"============================================================\n"); cmdin = fopen( "inputdeck", "r" ); if ( cmdin == NULL ) { perror("Could not open control file 'input'"); exit(1); } #define NXTTOKEN strtok(NULL,seps) while ( fgets(line,100,cmdin) != NULL ) { fputs(line,stdout); if ( line[0] == '#' ) continue; keyword = strtok( line,seps ); if ( keyword == NULL ) continue; nc = strlen(keyword); if (!nc) continue; /* **** Control **** */ if ( match(keyword,"runname",nc) ) { strncpy( globals.runName, NXTTOKEN, 20 ); } else if ( match(keyword,"dtime",nc) ) { if ( globals.restart ) fprintf(stderr,"** restarting, ignoring '%s' keyword\n", keyword ); else { cp = NXTTOKEN; globals.dtime = (float)atof( cp ); /* Optional values after the timestep are safety, dtmin, dtmax */ cp = NXTTOKEN; if ( cp != NULL ) { globals.safety = (float)atof(cp); cp = NXTTOKEN; if ( cp != NULL ) { globals.dtmin = (float)atof(cp); cp = NXTTOKEN; if ( cp!= NULL ) globals.dtmax = (float)atof(cp); } } } } else if ( match(keyword,"stop",nc) ) { globals.stop_step = atoi( NXTTOKEN ); globals.stop_time = (float)atof( NXTTOKEN ); } else if ( match(keyword,"checkpoint",nc) ) { globals.ncheckpoint = atoi( NXTTOKEN ); } else if ( match(keyword,"dtdump",nc) ) { globals.dtdump = (float)atof( NXTTOKEN ); } else if ( match(keyword,"rootdir",nc) ) { strncpy( globals.rootDir, NXTTOKEN,255 ); /* In the unlikely (but done) circumstance that we want to * change this name after reading the the restart, save it */ strcpy( save_root, globals.rootDir ); fprintf(stderr,"*** rootDir='%s'\n", globals.rootDir ); #if 0 /* compose pathname as root directory name + . * If the root dir name * contains a '%', assume its going to be a %d and add in the node number of * this node, which will be 0. */ if ( strrchr(globals.rootDir,'%') != NULL ) sprintf(globals.globalsName, globals.rootDir, 0 ); else strcpy( globals.globalsName, globals.rootDir ); i= strlen(globals.globalsName); if ( i && globals.globalsName[i-1] != CSLASH ) strcat( globals.globalsName, SLASH ); strcat(globals.globalsName,"restart"); strcpy( name, globals.globalsName ); #endif /* If we can successfully read this file into 'globals', set the flag indicating * we're restarting */ if ( readGlobals() ) { globals.restart= 1; /* strcpy( globals.globalsName, name );*/ strcpy( globals.rootDir, save_root ); } else globals.restart= 0; } else if ( match(keyword,"homedir",nc) ) { strncpy( globals.homeDir, NXTTOKEN,255 ); /* Add trailing slash if none */ i= strlen(globals.homeDir); if ( i && globals.homeDir[i-1] != CSLASH ) strcat( globals.homeDir, SLASH ); } else if ( match(keyword,"nthreads",nc) ) { globals.nthreads= atoi( NXTTOKEN ); } /* **** Physics **** */ else if ( match(keyword,"gamma",nc) ) { if ( globals.restart ) fprintf(stderr,"** restarting, ignoring '%s' keyword\n", keyword ); else globals.gamma = (float)atof( NXTTOKEN ); } else if ( match(keyword,"turbulence",nc) ) { if ( globals.restart ) fprintf(stderr,"** restarting, ignoring %s\n", keyword ); else { /* read(params,*,err=666) iseed,kmax,velamp,ratsc,wdthk,roamb,pgamb */ globals.iseed = atoi( NXTTOKEN ); globals.kmax = atoi( NXTTOKEN ); globals.velamp= (float)atof( NXTTOKEN ); globals.ratsc = (float)atof( NXTTOKEN ); globals.wdthk = (float)atof( NXTTOKEN ); globals.roamb = (float)atof( NXTTOKEN ); globals.pgamb = (float)atof( NXTTOKEN ); } } else { fprintf(stderr,"***** '%s' undefined keyword ignored\n",keyword); } /* end while */ } fclose(cmdin); fprintf(stderr,"============================================================\n"); if ( globals.restart ) { fprintf(stderr,"Restarted: step=%d time=%f dtime=%f, tdump=%f, dtdump=%f\n\n\n", globals.nstep, globals.time, globals.dtime, globals.tdump, globals.dtdump ); return; } /* Now define the coordinate mesh. * 1-NBDYF:NX+NBDYF+1, etc. */ dx= 1.0/ (double)NX; i= 0; for (j=-NBDYF; j<=NX+NBDYF+1; ++j ) globals.xxl[i++]= (double)((double)(j) * dx); i= 0; for (j=-NBDYF; j<=NY+NBDYF+1; ++j ) globals.yyl[i++]= (double)((double)(j) * dx); i= 0; for (j=-NBDYF; j<=NZ+NBDYF+1; ++j ) globals.zzl[i++]= (double)((double)(j) * dx); return; }