00001
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00047 #include "generic_driver.h"
00048
00058 int main(int argc, char* argv[])
00059 {
00074 int i;
00075 int runSeq;
00076 int RunNum;
00077 int totRuns;
00078 int NumSRunsPerSet = 2;
00079 int SParmNum=0;
00080 int parmID;
00081
00082 ViewParm *view;
00083 dynERRORnum = 0;
00084 time_t begin_sim, end_sim;
00085 struct tm *local_begin_time;
00086
00087
00088 begin_sim = time(NULL);
00089 local_begin_time = localtime(&begin_sim);
00090
00091 local_setup(argc, argv);
00092 setup();
00093 alloc_memory();
00094
00095 read_model_parameters( ProgExec->S_ParmName, ProgExec->S_ParmRelVal );
00096 if (SensiOn) SParmNum = SensiParm_list(ProgExec->S_ParmName);
00097 RunNum = numRuns(SParmNum, NumSRunsPerSet);
00098
00099 init_static_data();
00100
00101 totRuns = RunNum;
00102
00103
00104 while (RunNum) {
00105 RunNum--;
00106 runSeq= totRuns - RunNum;
00107
00108 sync_processors();
00109
00110 open_debug_outFile(runSeq);
00111
00112
00113 if (runSeq == 1) {
00114 usrErr0("Reading output requests...");
00115 view = read_output_parms();
00116 usrErr("Done output requests.");
00117 }
00118
00119 if (SensiOn) {
00120 parmID = (int) (runSeq)/NumSRunsPerSet;
00121 ProgExec = RunList[parmID];
00122
00123 if (runSeq > 1) {
00124 ProgExec->S_ParmRelVal = ProgExec->S_ParmRelVal+1 ;
00125 read_model_parameters( ProgExec->S_ParmName, ProgExec->S_ParmRelVal );
00126 reinitBIR();
00127 }
00128 sprintf(msgStr,"\nSENSITIVITY ANALYSIS: Run %d of %d, relative-value parm ID= %d in %s set.",
00129 runSeq, totRuns, ProgExec->S_ParmRelVal, ProgExec->S_ParmName );
00130 usrErr(msgStr);
00131 WriteMsg(msgStr,1);
00132
00133 }
00134
00135 init_dynam_data();
00136 init_eqns();
00137 if (WatMgmtOn) init_canals(runSeq);
00138 if (HabSwitchOn) init_succession();
00139
00140 usrErr0("Calling dynamic ecological modules:\n\t");
00141 for (i=0; i<NSector; i++) {
00142 sprintf(msgStr, "%d, ", iSector[i]);
00143 usrErr0(msgStr);
00144 }
00145
00146
00147 if(PositAnalOn) {
00148 sprintf(msgStr, "\n\t***running %2d/%2d Position Analysis*** ",mo_R_in,da_R_in);
00149 usrErr0(msgStr);
00150 }
00151
00152
00153 if (ESPmodeON) usrErr0("\n\t***running ESP*** ");
00154
00155 if (WatMgmtOn) usrErr0("\n\twith spatial water managment 'on' ");
00156 else usrErr0("\n\twith spatial water managment 'off' ");
00157
00158 if (HabSwitchOn) usrErr("and succession 'on'.");
00159 else usrErr("and succession 'off'.");
00160
00161 sprintf(msgStr,"\n*** Running simulation from %d/%d/%d to %d/%d/%d ***",
00162 yr_in,mo_in,da_in, yr_end,mo_end,da_end );
00163 usrErr(msgStr);
00164
00165
00166
00167 for(istep=0; istep<N_iter; istep++) {
00168
00169 track_time(istep);
00170
00171
00172
00173
00174 if (dynERRORnum > 500) {
00175 sprintf(msgStr,"\n\n***Sorry! Exiting because of many (%d) ERROR and/or capacityERR messages.\n***See the %s/%s/Debug/DriverX.out (for X'th simulation run) file to determine how to fix things.",dynERRORnum, OutputPath, ProjName);
00176 usrErr(msgStr);
00177 exit(-1);
00178 }
00179
00180 if (PositAnalOn) {
00181 if ( (SimTime.mo[0] == mo_R_in) && (SimTime.da[0] == da_R_in) ) {
00182 usrErr0("\nPOSITION ANALYSIS: Re-initializing...");
00183 init_dynam_data();
00184 init_eqns();
00185 reinitCanals();
00186 reinitBIR();
00187 usrErr("Done re-initializing.");
00188 }
00189 }
00190 if(debug >3) {
00191 sprintf(msgStr,"---------------------------------------Iteration = %d\n",istep);
00192 WriteMsg(msgStr,1); usrErr(msgStr);
00193 }
00194 else
00195 {
00196 sprintf(msgStr,"\r%d/%2d/%2d: ",SimTime.yr[0],SimTime.mo[0],SimTime.da[0] );
00197 usrErr0(msgStr);
00198 }
00199
00200
00201 for(i=0; i<NSector; i++) {
00202
00203 if(debug >3) {
00204 sprintf(msgStr,"Running cell dyn %d of %d\n",iSector[i],NSector);
00205 WriteMsg(msgStr,1);
00206 usrErr(msgStr);
00207 }
00208 call_cell_dyn(iSector[i],istep);
00209 }
00210
00211
00212 if((istep >= 0 && istep <= N_iter) ) {
00213 if(debug >3) {
00214 usrErr("");
00215 sprintf(msgStr,"(%d)Generating Output",procnum); WriteMsg(msgStr,1); usrErr(msgStr);
00216 }
00217 gen_output(istep, view);
00218 }
00219
00220
00221 if ( FMOD(SimTime.TIME, 180.0) == 0 ) {
00222 if (istep == 0) open_point_lists(pSeries,MAX_PTSERIES);
00223 send_point_lists2(pSeries,MAX_PTSERIES);
00224 }
00225 }
00226
00227
00228
00229 send_point_lists2(pSeries,MAX_PTSERIES);
00230
00231 end_sim = time(NULL);
00232
00233 sprintf(msgStr,"########\nThe simulation(s) started on: %s", asctime(local_begin_time) ); WriteMsg(msgStr,1);
00234 sprintf(msgStr,"\nEND. The simulation(s) took %8.3f minutes to run using your %s OS box.\n########",( (end_sim-begin_sim)/60.0), OS_TYPE ); WriteMsg(msgStr,1); usrErr(msgStr);
00235
00236 }
00237
00238 return 0;
00239 }
00240
00241
00246 void setup()
00247 {
00248 int i;
00249 for(i=0; i<MAX_PTSERIES; i++) pSeries[i].data = NULL;
00250 setup_platform();
00251 set_env_vars();
00252 get_parmf();
00253 get_map_dims();
00254 setup_grid();
00255 }
00256
00257
00258
00265 void get_parmf ()
00266 {
00277 int i=0, stop=0;
00278 unsigned char ch;
00279 FILE *infile;
00280 char filename[120], ss[120];
00281 char Re_initDateRead[15], endDateRead[15];
00282 int SParmNum_TEMP = 150;
00283
00284 sprintf(filename,"%s/%s/RunParms/Driver.parm",ModelPath,ProjName);
00285 infile = fopen(filename,"r");
00286 if(infile==NULL) { fprintf(stderr,"Error, can't open file %s",filename); exit(0); }
00287
00288
00289 fgets(ss,120,infile);
00290 sscanf(ss,"%s",outpath);
00291 OutputPath = (char*)&outpath;
00292 open_debug_outFile(0);
00293
00294
00295 fgets(ss,120,infile);
00296 sscanf(ss,"%s",initDateRead);
00297
00298 sprintf(msgStr,"initDateRead=%s\n",initDateRead); WriteMsg(msgStr,1);
00299 sscanf(initDateRead, "%4d/%2d/%2d,",&yr_in,&mo_in,&da_in);
00300
00301
00302 Jdate_init = julday(mo_in, da_in, yr_in, hr_in, mi_in, se_in);
00303
00304
00305
00306 fgets(ss,120,infile);
00307 sscanf(ss,"%s",endDateRead);
00308
00309 sprintf(msgStr,"endDateRead=%s\n",endDateRead); WriteMsg(msgStr,1);
00310 sscanf(endDateRead, "%4d/%2d/%2d,",&yr_end,&mo_end,&da_end);
00311
00312
00313 Jdate_end = julday(mo_end, da_end, yr_end, hr_in, mi_in, se_in);
00314
00315
00316 fgets(ss,120,infile);
00317 sscanf(ss,"%s",Re_initDateRead);
00318
00319 sprintf(msgStr,"Re_initDateRead=%s\n",Re_initDateRead); WriteMsg(msgStr,1);
00320 sscanf(Re_initDateRead, "%2d/%2d,",&mo_R_in,&da_R_in);
00321
00322 if (mo_R_in>0) {
00323 PositAnalOn=True;
00324 sprintf(msgStr,"\n *** WARNING: Position Analysis capabilities are NOT VERIFIED FOR accuracy/consistency in ELMv2.3 - the Avg's, Basin/Indicator-Region output may not be reliable ***\n");
00325 WriteMsg(msgStr,1);
00326 usrErr(msgStr);
00327 }
00328
00329 fgets(ss,120,infile);
00330 sscanf(ss,"%s",modelName);
00331 sprintf(msgStr, "Model Name= %s", modelName); usrErr(msgStr); WriteMsg(msgStr,1);
00332 getString(infile,"Model version=",modelVers);
00333 sprintf(msgStr,"Model version=%s\n",modelVers); WriteMsg(msgStr,1);
00334
00335 getFloat(infile,"CellArea=",&CELL_SIZE);
00336 sprintf(msgStr,"CellArea=%f\n",CELL_SIZE); WriteMsg(msgStr,1);
00337 celWid = sqrt(CELL_SIZE);
00338 sq_celWid = sqrt(celWid);
00339
00340
00341 getFloat(infile,"budg_Intvl=",&budg_Intvl);
00342 sprintf(msgStr,"budg_Intvl=%f\n",budg_Intvl); WriteMsg(msgStr,1);
00343 budgCalendar = ( budg_Intvl == 0.0 ) ? (True) : (False);
00344
00345
00346
00347
00348 getFloat(infile,"avg_Intvl=",&avg_Intvl);
00349 sprintf(msgStr,"avg_Intvl=%f\n",avg_Intvl); WriteMsg(msgStr,1);
00350 if ( avg_Intvl == 0 ) avgCalendar = True;
00351
00352
00353
00354
00355 getFloat(infile,"can_Intvl=",&can_Intvl);
00356 sprintf(msgStr,"can_Intvl=%f\n",can_Intvl); WriteMsg(msgStr,1);
00357 if ( can_Intvl == 0 ) canalCalendar = True;
00358
00359
00360
00361
00362 getInt(infile,"seed=",&seed);
00363 sprintf(msgStr,"seed=%d\n",seed); WriteMsg(msgStr,1);
00364 getFloat(infile,"dt=",&dt);
00365 sprintf(msgStr,"dt=%f\n",(double)dt); WriteMsg(msgStr,1);
00366 getInt(infile,"hyd_iter=",&hyd_iter);
00367 sprintf(msgStr,"hyd_iter=%d\n",hyd_iter); WriteMsg(msgStr,1);
00368
00369 sfstep = dt/hyd_iter;
00370 gwstep = dt/(hyd_iter/2);
00371 canstep = dt/hyd_iter;
00372 step_Cell = sq_celWid * sfstep/CELL_SIZE * sec_per_day;
00373
00374 N_iter= (Jdate_end - Jdate_init + 1)/dt;
00375 sprintf(msgStr,"N_iter=%d\n",N_iter); WriteMsg(msgStr,1);
00376 PORnumday = (int)(Jdate_end - Jdate_init + 1);
00377
00378 getInt(infile,"debug=",&debug);
00379 sprintf(msgStr,"debug=%d\n",debug); WriteMsg(msgStr,1);
00380 getInt(infile,"debug_point=",&dbgPt.x);
00381 getInt(infile,NULL,&dbgPt.y);
00382 sprintf(msgStr,"debug point= (%d,%d)\n",dbgPt.x,dbgPt.y); WriteMsg(msgStr,1);
00383
00384 alloc_mem_runs(SParmNum_TEMP);
00385 RunList[0] = ProgExec;
00386
00387 getString(infile,"S_ParmName=",ProgExec->S_ParmName);
00388
00389
00390
00391
00392 sprintf(msgStr,"S_ParmName=%s\n",ProgExec->S_ParmName); WriteMsg(msgStr,1);
00393
00394 SensiOn = (strcmp(ProgExec->S_ParmName,"NONE")!=0) ? (True) : (False);
00395 ProgExec->S_ParmRelVal = 0;
00396
00397 getInt(infile,"HabSwitchOn=",&HabSwitchOn);
00398 sprintf(msgStr,"HabSwitchOn=%d\n",HabSwitchOn); WriteMsg(msgStr,1);
00399 getInt(infile,"WatMgmtOn=",&WatMgmtOn);
00400 sprintf(msgStr,"WatMgmtOn=%d\n",WatMgmtOn); WriteMsg(msgStr,1);
00401
00402 getString(infile,"Scenario=",SimAlt);
00403 sprintf(msgStr,"SimAlt=%s\n",SimAlt); WriteMsg(msgStr,1);
00404 getString(infile,"Scenario modifier=",SimModif);
00405 sprintf(msgStr,"SimModif=%s\n",SimModif); WriteMsg(msgStr,1);
00406
00407 scan_forward(infile,"Sectors="); i=0; stop=0;
00408 while(1) {
00409 skip_white(infile);
00410 ch=fgetc(infile);
00411 if( isdigit(ch) ) ungetc(ch,infile);
00412 else {
00413 switch (ch) {
00414 case ';': NSector = i; stop=1; break;
00415 case ',': skip_white(infile); break;
00416 }
00417 }
00418 if(stop) break;
00419 else fscanf(infile,"%d",&iSector[i++]);
00420
00421 if (iSector[i-1] == 13) ESPmodeON=1;
00422
00423 }
00424 fclose(infile);
00425 broadcastInt(&NSector);
00426 sprintf(msgStr,"\n(%d) NSector = %d: Sectors=",procnum,NSector); WriteMsg(msgStr,1);
00427 for(i=0; i<NSector; i++) {
00428 broadcastInt(&iSector[i]);
00429 sprintf(msgStr,"(%d:%d)",i,iSector[i]); WriteMsg(msgStr,1);
00430 }
00431
00432 for(i=0; i<MAX_PTSERIES; i++) if(pSeries[i].data) { free((char*)pSeries[i].data); pSeries[i].data = NULL; }
00433 }
00434
00443 void track_time(int istep)
00444 {
00445 int yr_tom[2];
00446 int mo_tom[2];
00447 int da_tom[2];
00448 int hr_tom[2];
00449 int mi_tom[2];
00450 double se_tom[2];
00451
00452 SimTime.TIME = istep*dt;
00453 SimTime.Jdate = Jdate_init+SimTime.TIME;
00454
00455
00456 calcdate( (SimTime.Jdate+1), mo_tom, da_tom, yr_tom, hr_tom, mi_tom, se_tom);
00457 calcdate( SimTime.Jdate, SimTime.mo, SimTime.da, SimTime.yr, SimTime.hr, SimTime.mi, SimTime.se);
00458
00459 SimTime.IsMonthEnd = ( SimTime.mo[0] != mo_tom[0] ) ? (True) : (False);
00460 SimTime.IsYearEnd = ( SimTime.yr[0] != yr_tom[0] ) ? (True) : (False);
00461 SimTime.IsWetSeasEnd = (( SimTime.mo[0] == wetEndMon ) && (SimTime.da[0] == wetEndDay)) ? (True) : (False);
00462 SimTime.IsDrySeasEnd = (( SimTime.mo[0] == dryEndMon ) && (SimTime.da[0] == dryEndDay)) ? (True) : (False);
00463 SimTime.IsSimulationEnd = ((PORnumday-1)==istep) ? (True) : (False);
00464
00465
00466 if (!budgCalendar) {
00467 if ( fmod(SimTime.TIME, budg_Intvl) ==0) {
00468
00469 SimTime.IsBudgEnd = True;
00470 }
00471 else { SimTime.IsBudgEnd = False; }
00472 }
00473 else {
00474 if ( SimTime.IsMonthEnd ) {
00475
00476 SimTime.IsBudgEnd = True;
00477 budg_Intvl= (float)SimTime.da[0];
00478 }
00479 else { SimTime.IsBudgEnd = False; }
00480 }
00481
00482
00483 if (!avgCalendar) {
00484 if ( fmod(SimTime.TIME, avg_Intvl) ==0) {
00485
00486 avgPrint = True; }
00487 else { avgPrint = False; }
00488 }
00489 else {
00490 if ( SimTime.IsMonthEnd ) {
00491
00492 avgPrint = True;
00493 avg_Intvl= (float)SimTime.da[0];
00494 }
00495 else { avgPrint = False; }
00496 }
00497
00498
00499 if (!canalCalendar) {
00500 if ( fmod(SimTime.TIME, can_Intvl) ==0) {
00501
00502 canPrint = True; }
00503 else { canPrint = False; }
00504 }
00505 else {
00506 if ( SimTime.IsMonthEnd ) {
00507
00508 canPrint = True;
00509 can_Intvl= (float)SimTime.da[0];
00510 }
00511 else { canPrint = False; }
00512 }
00513
00514 SimTime.IsBudgFirst = ( (SimTime.TIME/budg_Intvl) > 1 ) ? (False) : (True);
00515 SimTime.IsDay0 = (SimTime.TIME==0) ? (True) : (False);
00516
00517
00518 if (SensiOn) {
00519 BIRavg_Intvl = N_iter - 1;
00520 SimTime.IsBIRavgEnd = ( fmod(SimTime.TIME, BIRavg_Intvl) ==0) ? (True) : (False);
00521
00522 budg_Intvl = N_iter - 1;
00523 SimTime.IsBudgEnd = ( fmod(SimTime.TIME, budg_Intvl) ==0) ? (True) : (False);
00524 }
00525 else {
00526 BIRavg_Intvl = budg_Intvl;
00527 SimTime.IsBIRavgEnd = SimTime.IsBudgEnd;
00528 }
00529
00530 }
00531
00532