00001
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00054 #include "generic_driver.h"
00055
00065 int main(int argc, char* argv[])
00066 {
00081 int i;
00082 int runSeq;
00083 int RunNum;
00084 int totRuns;
00085 int NumSRunsPerSet = 2;
00086 int SParmNum=0;
00087 int parmID;
00088
00089 ViewParm *view;
00090 dynERRORnum = 0;
00091 time_t begin_sim, end_sim;
00092 struct tm *local_begin_time;
00093
00094
00095 begin_sim = time(NULL);
00096 local_begin_time = localtime(&begin_sim);
00097
00098 local_setup(argc, argv);
00099 setup();
00100 alloc_memory();
00101
00102 isPTSL = PTSL_test();
00103
00104
00105 isModExperim = ModExperim_test();
00106
00107
00108
00109 if (isPTSL) PtInterp_read();
00110
00111 read_model_parameters( ProgExec->S_ParmName, ProgExec->S_ParmRelVal );
00112
00113 if (SensiOn) SParmNum = SensiParm_list(ProgExec->S_ParmName);
00114 RunNum = numRuns(SParmNum, NumSRunsPerSet);
00115
00116 init_static_data();
00117
00118 totRuns = RunNum;
00119
00120
00121 while (RunNum) {
00122 RunNum--;
00123 runSeq= totRuns - RunNum;
00124
00125 sync_processors();
00126
00127 open_debug_outFile(runSeq);
00128
00129
00130 if (runSeq == 1) {
00131 usrErr0("Reading output requests...");
00132 view = read_output_parms();
00133 usrErr("Done output requests.");
00134 }
00135
00136 if (SensiOn) {
00137 parmID = (int) (runSeq)/NumSRunsPerSet;
00138 ProgExec = RunList[parmID];
00139
00140 if (runSeq > 1) {
00141 ProgExec->S_ParmRelVal = ProgExec->S_ParmRelVal+1 ;
00142 read_model_parameters( ProgExec->S_ParmName, ProgExec->S_ParmRelVal );
00143 reinitBIR();
00144 }
00145 sprintf(msgStr,"\nSENSITIVITY ANALYSIS: Run %d of %d, relative-value parm ID= %d in %s set.",
00146 runSeq, totRuns, ProgExec->S_ParmRelVal, ProgExec->S_ParmName );
00147 usrErr(msgStr);
00148 WriteMsg(msgStr,1);
00149
00150 }
00151
00152 init_dynam_data();
00153 init_eqns();
00154 if (WatMgmtOn) init_canals(runSeq);
00155 if (HabSwitchOn) init_succession();
00156
00157 usrErr0("Calling dynamic ecological modules:\n\t");
00158 for (i=0; i<NSector; i++) {
00159 sprintf(msgStr, "%d, ", iSector[i]);
00160 usrErr0(msgStr);
00161 }
00162
00163
00164
00165
00166
00167 IsSubstituteModel = ( (NSector == 2) && (iSector[0] == 1) && (iSector[1] == 99) ) ? (1) : (0);
00168
00169
00170 if(PositAnalOn) {
00171 sprintf(msgStr, "\n\t***running %2d/%2d Position Analysis*** ",mo_R_in,da_R_in);
00172 usrErr0(msgStr);
00173 }
00174
00175
00176 if (ESPmodeON) usrErr0("\n\t***running ESP*** ");
00177
00178 if (WatMgmtOn) usrErr0("\n\twith spatial water managment 'on' ");
00179 else usrErr0("\n\twith spatial water managment 'off' ");
00180
00181 if (HabSwitchOn) usrErr("and succession 'on'.");
00182 else usrErr("and succession 'off'.");
00183
00184 sprintf(msgStr,"\n*** Running simulation from %d/%d/%d to %d/%d/%d ***",
00185 yr_in,mo_in,da_in, yr_end,mo_end,da_end );
00186 usrErr(msgStr);
00187
00188
00189
00190 for(istep=0; istep<N_iter; istep++) {
00191
00192 track_time(istep);
00193
00194
00195
00196
00197 if (dynERRORnum > 500) {
00198 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);
00199 usrErr(msgStr);
00200 exit(-1);
00201 }
00202
00203 if (PositAnalOn) {
00204 if ( (SimTime.mo[0] == mo_R_in) && (SimTime.da[0] == da_R_in) ) {
00205 usrErr0("\nPOSITION ANALYSIS: Re-initializing...");
00206 init_dynam_data();
00207 init_eqns();
00208 reinitCanals();
00209 reinitBIR();
00210 usrErr("Done re-initializing.");
00211 }
00212 }
00213 if(debug >3) {
00214 sprintf(msgStr,"---------------------------------------Iteration = %d\n",istep);
00215 WriteMsg(msgStr,1); usrErr(msgStr);
00216 }
00217 else
00218 {
00219 sprintf(msgStr,"\r%d/%2d/%2d: ",SimTime.yr[0],SimTime.mo[0],SimTime.da[0] );
00220 usrErr0(msgStr);
00221 }
00222
00223
00224 for(i=0; i<NSector; i++) {
00225
00226 if(debug >3) {
00227 sprintf(msgStr,"Running cell dyn %d of %d\n",iSector[i],NSector);
00228 WriteMsg(msgStr,1);
00229 usrErr(msgStr);
00230 }
00231 call_cell_dyn(iSector[i],istep);
00232 }
00233
00234
00235 if((istep >= 0 && istep <= N_iter) ) {
00236 if(debug >3) {
00237 usrErr("");
00238 sprintf(msgStr,"(%d)Generating Output",procnum); WriteMsg(msgStr,1); usrErr(msgStr);
00239 }
00240 gen_output(istep, view);
00241 }
00242
00243
00244 if ( FMOD(SimTime.TIME, 180.0) == 0 ) {
00245 if (istep == 0) open_point_lists(pSeries,MAX_PTSERIES);
00246 send_point_lists2(pSeries,MAX_PTSERIES);
00247 }
00248 }
00249
00250
00251
00252 send_point_lists2(pSeries,MAX_PTSERIES);
00253
00254 end_sim = time(NULL);
00255
00256 sprintf(msgStr,"########\nThe simulation(s) started on: %s", asctime(local_begin_time) ); WriteMsg(msgStr,1);
00257 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);
00258
00259 }
00260
00261 return 0;
00262 }
00263
00264
00269 void setup()
00270 {
00271 int i;
00272 for(i=0; i<MAX_PTSERIES; i++) pSeries[i].data = NULL;
00273 setup_platform();
00274 set_env_vars();
00275 get_parmf();
00276 get_map_dims();
00277 setup_grid();
00278 }
00279
00280
00281
00288 void get_parmf ()
00289 {
00300 int i=0, stop=0;
00301 unsigned char ch;
00302 FILE *infile;
00303 char filename[120], ss[120];
00304 char Re_initDateRead[15], endDateRead[15];
00305 int SParmNum_TEMP = 150;
00306
00307 sprintf(filename,"%s/%s/RunParms/Driver.parm",ModelPath,ProjName);
00308 infile = fopen(filename,"r");
00309 if(infile==NULL) { fprintf(stderr,"Error, can't open file %s",filename); exit(0); }
00310
00311
00312 fgets(ss,120,infile);
00313 sscanf(ss,"%s",outpath);
00314 OutputPath = (char*)&outpath;
00315 open_debug_outFile(0);
00316
00317
00318 fgets(ss,120,infile);
00319 sscanf(ss,"%s",initDateRead);
00320
00321 sprintf(msgStr,"initDateRead=%s\n",initDateRead); WriteMsg(msgStr,1);
00322 sscanf(initDateRead, "%4d/%2d/%2d,",&yr_in,&mo_in,&da_in);
00323
00324
00325 Jdate_init = julday(mo_in, da_in, yr_in, hr_in, mi_in, se_in);
00326
00327
00328
00329 fgets(ss,120,infile);
00330 sscanf(ss,"%s",endDateRead);
00331
00332 sprintf(msgStr,"endDateRead=%s\n",endDateRead); WriteMsg(msgStr,1);
00333 sscanf(endDateRead, "%4d/%2d/%2d,",&yr_end,&mo_end,&da_end);
00334
00335
00336 Jdate_end = julday(mo_end, da_end, yr_end, hr_in, mi_in, se_in);
00337
00338
00339 fgets(ss,120,infile);
00340 sscanf(ss,"%s",Re_initDateRead);
00341
00342 sprintf(msgStr,"Re_initDateRead=%s\n",Re_initDateRead); WriteMsg(msgStr,1);
00343 sscanf(Re_initDateRead, "%2d/%2d,",&mo_R_in,&da_R_in);
00344
00345 if (mo_R_in>0) {
00346 PositAnalOn=True;
00347 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");
00348 WriteMsg(msgStr,1);
00349 usrErr(msgStr);
00350 }
00351
00352 fgets(ss,120,infile);
00353 sscanf(ss,"%s",modelName);
00354 sprintf(msgStr, "Model Name= %s", modelName); usrErr(msgStr); WriteMsg(msgStr,1);
00355 getString(infile,"Model version=",modelVers);
00356 sprintf(msgStr,"Model version=%s\n",modelVers); WriteMsg(msgStr,1);
00357
00358 getFloat(infile,"CellArea=",&CELL_SIZE);
00359 sprintf(msgStr,"CellArea=%f\n",CELL_SIZE); WriteMsg(msgStr,1);
00360 celWid = sqrt(CELL_SIZE);
00361 sq_celWid = sqrt(celWid);
00362
00363
00364 getFloat(infile,"budg_Intvl=",&budg_Intvl);
00365 sprintf(msgStr,"budg_Intvl=%f\n",budg_Intvl); WriteMsg(msgStr,1);
00366 budgCalendar = ( budg_Intvl == 0.0 ) ? (True) : (False);
00367
00368
00369
00370
00371 getFloat(infile,"BIRhyd_avg_Intvl=",&BIRhyd_avg_Intvl);
00372 sprintf(msgStr,"BIRhyd_avg_Intvl=%f\n",BIRhyd_avg_Intvl); WriteMsg(msgStr,1);
00373 BIRhydCalendar = ( BIRhyd_avg_Intvl == 0.0 ) ? (True) : (False);
00374
00375
00376
00377
00378 getFloat(infile,"avg_Intvl=",&avg_Intvl);
00379 sprintf(msgStr,"avg_Intvl=%f\n",avg_Intvl); WriteMsg(msgStr,1);
00380 if ( avg_Intvl == 0 ) avgCalendar = True;
00381
00382
00383
00384
00385 getFloat(infile,"can_Intvl=",&can_Intvl);
00386 sprintf(msgStr,"can_Intvl=%f\n",can_Intvl); WriteMsg(msgStr,1);
00387 if ( can_Intvl == 0 ) canalCalendar = True;
00388
00389
00390
00391
00392 getInt(infile,"seed=",&seed);
00393 sprintf(msgStr,"seed=%d\n",seed); WriteMsg(msgStr,1);
00394 getFloat(infile,"dt=",&dt);
00395 sprintf(msgStr,"dt=%f\n",(double)dt); WriteMsg(msgStr,1);
00396 getInt(infile,"hyd_iter=",&hyd_iter);
00397 sprintf(msgStr,"hyd_iter=%d\n",hyd_iter); WriteMsg(msgStr,1);
00398
00399 sfstep = dt/hyd_iter;
00400 gwstep = dt/(hyd_iter/2);
00401 canstep = dt/hyd_iter;
00402 step_Cell = sq_celWid * sfstep/CELL_SIZE * sec_per_day;
00403
00404 N_iter= (Jdate_end - Jdate_init + 1)/dt;
00405 sprintf(msgStr,"N_iter=%d\n",N_iter); WriteMsg(msgStr,1);
00406 PORnumday = (int)(Jdate_end - Jdate_init + 1);
00407
00408 getInt(infile,"debug=",&debug);
00409 sprintf(msgStr,"debug=%d\n",debug); WriteMsg(msgStr,1);
00410 getInt(infile,"debug_point=",&dbgPt.x);
00411 getInt(infile,NULL,&dbgPt.y);
00412 sprintf(msgStr,"debug point= (%d,%d)\n",dbgPt.x,dbgPt.y); WriteMsg(msgStr,1);
00413
00414 alloc_mem_runs(SParmNum_TEMP);
00415 RunList[0] = ProgExec;
00416
00417 getString(infile,"S_ParmName=",ProgExec->S_ParmName);
00418
00419
00420
00421
00422 sprintf(msgStr,"S_ParmName=%s\n",ProgExec->S_ParmName); WriteMsg(msgStr,1);
00423
00424 SensiOn = (strcmp(ProgExec->S_ParmName,"NONE")!=0) ? (True) : (False);
00425 ProgExec->S_ParmRelVal = 0;
00426
00427 getInt(infile,"HabSwitchOn=",&HabSwitchOn);
00428 sprintf(msgStr,"HabSwitchOn=%d\n",HabSwitchOn); WriteMsg(msgStr,1);
00429 getInt(infile,"WatMgmtOn=",&WatMgmtOn);
00430 sprintf(msgStr,"WatMgmtOn=%d\n",WatMgmtOn); WriteMsg(msgStr,1);
00431
00432 getString(infile,"Scenario=",SimAlt);
00433 sprintf(msgStr,"SimAlt=%s\n",SimAlt); WriteMsg(msgStr,1);
00434 getString(infile,"Scenario modifier=",SimModif);
00435 sprintf(msgStr,"SimModif=%s\n",SimModif); WriteMsg(msgStr,1);
00436
00437 scan_forward(infile,"Sectors="); i=0; stop=0;
00438 while(1) {
00439 skip_white(infile);
00440 ch=fgetc(infile);
00441 if( isdigit(ch) ) ungetc(ch,infile);
00442 else {
00443 switch (ch) {
00444 case ';': NSector = i; stop=1; break;
00445 case ',': skip_white(infile); break;
00446 }
00447 }
00448 if(stop) break;
00449 else fscanf(infile,"%d",&iSector[i++]);
00450
00451 if (iSector[i-1] == 13) ESPmodeON=1;
00452
00453 }
00454 fclose(infile);
00455 broadcastInt(&NSector);
00456 sprintf(msgStr,"\n(%d) NSector = %d: Sectors=",procnum,NSector); WriteMsg(msgStr,1);
00457 for(i=0; i<NSector; i++) {
00458 broadcastInt(&iSector[i]);
00459 sprintf(msgStr,"(%d:%d)",i,iSector[i]); WriteMsg(msgStr,1);
00460 }
00461
00462 for(i=0; i<MAX_PTSERIES; i++) if(pSeries[i].data) { free((char*)pSeries[i].data); pSeries[i].data = NULL; }
00463 }
00464
00473 void track_time(int istep)
00474 {
00475 int yr_tom[2];
00476 int mo_tom[2];
00477 int da_tom[2];
00478 int hr_tom[2];
00479 int mi_tom[2];
00480 double se_tom[2];
00481
00482 SimTime.TIME = istep*dt;
00483 SimTime.Jdate = Jdate_init+SimTime.TIME;
00484
00485
00486 calcdate( (SimTime.Jdate+1), mo_tom, da_tom, yr_tom, hr_tom, mi_tom, se_tom);
00487 calcdate( SimTime.Jdate, SimTime.mo, SimTime.da, SimTime.yr, SimTime.hr, SimTime.mi, SimTime.se);
00488
00489 SimTime.IsMonthEnd = ( SimTime.mo[0] != mo_tom[0] ) ? (True) : (False);
00490 SimTime.IsYearEnd = ( SimTime.yr[0] != yr_tom[0] ) ? (True) : (False);
00491 SimTime.IsWetSeasEnd = (( SimTime.mo[0] == wetEndMon ) && (SimTime.da[0] == wetEndDay)) ? (True) : (False);
00492 SimTime.IsDrySeasEnd = (( SimTime.mo[0] == dryEndMon ) && (SimTime.da[0] == dryEndDay)) ? (True) : (False);
00493 SimTime.IsSimulationEnd = ((PORnumday-1)==istep) ? (True) : (False);
00494
00495
00496 if (!budgCalendar) {
00497 if ( fmod(SimTime.TIME, budg_Intvl) ==0) {
00498
00499 SimTime.IsBudgEnd = True;
00500 }
00501 else { SimTime.IsBudgEnd = False; }
00502 }
00503 else {
00504 if ( SimTime.IsMonthEnd ) {
00505
00506 SimTime.IsBudgEnd = True;
00507 budg_Intvl= (float)SimTime.da[0];
00508 }
00509 else { SimTime.IsBudgEnd = False; }
00510 }
00511
00512
00513 if (!BIRhydCalendar) {
00514 if ( fmod(SimTime.TIME, BIRhyd_avg_Intvl) ==0) {
00515
00516 SimTime.IsBIRhydEnd = True;
00517 }
00518 else { SimTime.IsBIRhydEnd = False; }
00519 }
00520 else {
00521 if ( SimTime.IsMonthEnd ) {
00522
00523 SimTime.IsBIRhydEnd = True;
00524 BIRhyd_avg_Intvl = (float)SimTime.da[0];
00525 }
00526 else { SimTime.IsBIRhydEnd = False; }
00527 }
00528
00529
00530 if (!avgCalendar) {
00531 if ( fmod(SimTime.TIME, avg_Intvl) ==0) {
00532
00533 avgPrint = True; }
00534 else { avgPrint = False; }
00535 }
00536 else {
00537 if ( SimTime.IsMonthEnd ) {
00538
00539 avgPrint = True;
00540 avg_Intvl= (float)SimTime.da[0];
00541 }
00542 else { avgPrint = False; }
00543 }
00544
00545
00546 if (!canalCalendar) {
00547 if ( fmod(SimTime.TIME, can_Intvl) ==0) {
00548
00549 canPrint = True; }
00550 else { canPrint = False; }
00551 }
00552 else {
00553 if ( SimTime.IsMonthEnd ) {
00554
00555 canPrint = True;
00556 can_Intvl= (float)SimTime.da[0];
00557 }
00558 else { canPrint = False; }
00559 }
00560
00561 SimTime.IsBudgFirst = ( (SimTime.TIME/budg_Intvl) > 1 ) ? (False) : (True);
00562 SimTime.IsDay0 = (SimTime.TIME==0) ? (True) : (False);
00563
00564
00565 if (SensiOn) {
00566 BIRavg_Intvl = N_iter - 1;
00567 SimTime.IsBIRavgEnd = ( fmod(SimTime.TIME, BIRavg_Intvl) ==0) ? (True) : (False);
00568
00569 BIRhyd_avg_Intvl = N_iter - 1;
00570 SimTime.IsBIRhydEnd = ( fmod(SimTime.TIME, BIRhyd_avg_Intvl) ==0) ? (True) : (False);
00571
00572 budg_Intvl = N_iter - 1;
00573 SimTime.IsBudgEnd = ( fmod(SimTime.TIME, budg_Intvl) ==0) ? (True) : (False);
00574 }
00575 else {
00576 BIRavg_Intvl = budg_Intvl;
00577 SimTime.IsBIRavgEnd = SimTime.IsBudgEnd;
00578 }
00579
00580 }
00581
00599 int PTSL_test()
00600 {
00601 FILE *testfile;
00602 char filename[120], ss[120];
00603
00604 sprintf(filename,"%s/%s/Data/rain.pts",ModelPath,ProjName);
00605 testfile = fopen(filename,"r");
00606 if(testfile==NULL)
00607 return 0;
00608 else
00609 { fclose(testfile); return 1; }
00610 }
00611
00622 int ModExperim_test()
00623 {
00624 FILE *testfile;
00625 char filename[120], ss[120];
00626
00627 sprintf(filename,"%s/%s/Data/ModExperimParms_NOM",ModelPath,ProjName);
00628 testfile = fopen(filename,"r");
00629 if(testfile==NULL)
00630 return 0;
00631 else
00632 { fclose(testfile); return 1; }
00633 }
00634
00635
00636
00637
00638
00639