#include #include #include #include #include "delta.h" #include "kirkrand.h" int main(int argc, char *argv[]) { int season, step, x, y; double water, year; double *h2oregime, **topo; struct Run run; struct Basin basin; struct Lifehist lifehist; struct Plant ***plant; char read[1] = {"r"}, params[20]; time_t t, start, end; start = time(NULL); /*Check initial arguments*/ if(argc != 2) { printf("Call program with: "); exit(EXIT_FAILURE); } else { strcpy(params, argv[1]); } OpenFile(params,read); GetRunParam(&basin, &lifehist, &run); fclose(fpin); ShowRunParam(&basin, &lifehist, &run); CheckRunParam(&basin, &lifehist, &run); if(run.basinfile[0]) { /*get basin dimensions from file*/ OpenFile(run.basinname,read); GetBasinParams(&basin); fclose(fpin); } else { GetSize(&basin); } if(run.waterfile[0]) { /*get runlngth from water regime file*/ OpenFile(run.watername,read); GetNewRunlength(&run); fclose(fpin); } /*****************Initialize Plant structure*********************************/ plant = NULL; plant = (struct Plant ***) calloc((basin.xmax[0]), sizeof(struct Plant **)); if( plant == NULL) { printf("\nAborted due to insufficient memory 1.\n"); exit(EXIT_FAILURE); } for(x = 0;x < basin.xmax[0]; x++) { plant[x] = NULL; plant[x] = (struct Plant **) calloc((basin.ymax[0]), sizeof(struct Plant *)); if(plant[x] == NULL) { printf("\nAborted due to insufficient memory 2.\n"); exit(EXIT_FAILURE); } for(y = 0 ;y < basin.ymax[0]; y++) { plant[x][y] = NULL; plant[x][y] = (struct Plant *) calloc(2, sizeof(struct Plant)); if( plant[x][y] == NULL) { printf("\nAborted due to insufficient memory 3.\n"); exit(EXIT_FAILURE); } else { memset(plant[x][y], 0, 2*sizeof(struct Plant)); } } } /*****************Initialize topographic array*********************************/ topo = NULL; topo = (double **) calloc((basin.xmax[0]), sizeof(double *)); if( topo == NULL) { printf("\nAborted due to insufficient memory 1.\n"); exit(EXIT_FAILURE); } for(x = 0;x < basin.xmax[0]; x++) { topo[x] = NULL; topo[x] = (double *) calloc((basin.ymax[0]), sizeof(double)); if(topo[x] == NULL) { printf("\nAborted due to insufficient memory 2.\n"); exit(EXIT_FAILURE); } else { memset(topo[x], 0, basin.ymax[0]*sizeof(double)); } } /*****************Initialize h2oregime Array*********************************/ h2oregime = NULL; h2oregime = (double *) calloc(run.h2olength[0], sizeof(double)); memset(h2oregime,0, run.h2olength[0]*sizeof(double)); if( plant == NULL) { printf("\nAborted due to insufficient memory 1.\n"); exit(EXIT_FAILURE); } /*************************End of Initialization*****************************/ if(run.waterfile[0]) { /*read in water regime*/ OpenFile(run.watername,read); GetWaterRegime(h2oregime, &run); fclose(fpin); } if(run.basinfile[0]) { /*read in basin topography*/ OpenFile(run.basinname,read); GetBasinTopo(topo, &basin); fclose(fpin); } for(step = 0; step < run.runlength[0]; step++) { /*Start model run*/ year = (((double)step)/4); season = (step % 4); if(run.waterfile[0]) { if(step < run.h2olength[0]) { water = h2oregime[step]; } else { water = h2oregime[(run.h2olength[0]-1)]; } } else { GetWaterElev(&basin, &water, step); } if(step==0) { r250_init((int)time(&t)); /*first init random number series*/ if(run.adultfile[0]) { /*read in adult topography*/ OpenFile(run.adultname,read); GetAdults(&basin, &lifehist, plant, &run, topo, &water); fclose(fpin); } else { /*generate adult distribution if there is not a file */ InitAdults(&basin, &lifehist, plant, &run, topo, &water); } InitSeeds(&basin, &lifehist, plant, &run, topo); ShowBasin(&basin, &run, topo); WriteBasin(&basin, &run, topo); } if(season == 3) { /*run year end processes*/ #ifdef SCREENIO printf("\n+++++++Year: %6.2f Water Elevation: %1.2f ++++++++",year,water); #endif r250_init((int)time(&t)); /*init random number series each year*/ /* AgeSeed(&basin, &lifehist, plant); DispersePrim();*/ if(year>=1) { /*show plants after water death in all later years */ DisperseVege(&basin, &lifehist, plant, &run, topo, &water);/*t = {0,1} updated within step*/ Germinate(&basin, &lifehist, plant, &run, topo, &water); SelectAdult(&basin, plant); KillSeedling(&basin, plant); } ShowAdult(&basin, plant, &run, topo, &water); ShowPlant(&basin, plant, &run); WriteAdult(&basin, plant, &run, year, topo, &water); WaterDeath(&basin, &lifehist, plant, &run, topo, &water); KillAnnuals(&basin, &lifehist, plant); } MemStep(&basin, plant); } /****************Free Plant Structure***************************************/ for(x = 0;x < basin.xmax[0]; x++) { for(y = 0 ;y < basin.ymax[0]; y++) { if(plant[x][y] != NULL) { free((void*)plant[x][y]); } } if(plant[x] != NULL) { free((void*)plant[x]); } } if(plant != NULL) { free((void*)plant); } /****************Free topo array***************************************/ for(x = 0;x < basin.xmax[0]; x++) { if(topo[x] != NULL) { free((void*)topo[x]); } } if(topo != NULL) { free((void*)topo); } /****************Free h2oregime array***************************************/ if(h2oregime != NULL) { free((void*)h2oregime); } /********************End of Free****************************/ end = time(NULL); #ifdef SCREENIO PrintTime(end-start); #endif return 0; }