/* input.c (simlib), 20.nov.2002, JH */ #include #include #include #include #include #include #include #include /* ************************************************************** */ /* GRASS input procedures, allocations */ /* *************************************************************** */ /*! * \brief allocate memory, read input rasters, assign UNDEF to NODATA * * \return int */ int input_data() { FCELL *cell1, *cell4b,*cell5; FCELL *cell9, *cell10,*cell11; DCELL *cell2, *cell3, *cell4, *cell4a,*cell12; int fd1, fd2, fd3, fd4, fd4a, fd4b, fd5, row, row_rev; int fd9, fd10, fd11, fd12; int l,j; int nn,cc,ii,dd; char *mapset; Site *site; char msg[1024]; npoints = 0; npoints_alloc = 0; /* put some warning messages about diff. in resol., etc. */ if (sfile != NULL) { fw=fopen("simwe_data.txt","w"); /* open ascii file for ascii output of gamma */ mapset = G_find_file ("site_lists", sfile, ""); if (mapset == NULL) { sprintf (msg, "file [%s] not found", sfile); G_fatal_error (msg); } if ((fdsfile = G_fopen_sites_old (sfile, mapset)) == NULL) { sprintf (msg, "Cannot open %s", sfile); G_fatal_error (msg); } if (G_site_describe (fdsfile, &nn, &cc, &ii, &dd)!=0) G_fatal_error("failed to guess format"); site = G_site_new_struct (cc, nn, ii, dd); fprintf (stderr, "Reading sites map (%s) ...\n\n\n", sfile); /* if (dd==0) { fprintf(stderr,"\n"); G_warning("I'm finding records that do not have a floating point attributes (fields prefixed with '%')."); } */ while (G_site_get(fdsfile, site) >= 0) { if (npoints_alloc <= npoints) { npoints_alloc += 128; points = (struct Point*) G_realloc(points, npoints_alloc * sizeof (struct Point)); } points[npoints].east = site->east * conv; points[npoints].north = site->north * conv; points[npoints].z1 = 0.; /*site->dbl_att[0];*/ /*printf("\n%f %f",points[npoints].east/conv,points[npoints].north/conv);*/ if((points[npoints].east/conv <= cellhd.east && points[npoints].east/conv >= cellhd.west) && (points[npoints].north/conv <= cellhd.north && points[npoints].north/conv >= cellhd.south)) npoints++; } fclose(fdsfile); } cell1=G_allocate_f_raster_buf(); cell2=G_allocate_d_raster_buf(); cell3=G_allocate_d_raster_buf(); if(rain != NULL) cell4=G_allocate_d_raster_buf(); if(infil != NULL) cell4a=G_allocate_d_raster_buf(); if(traps != NULL) cell4b=G_allocate_f_raster_buf(); cell5=G_allocate_f_raster_buf(); if(detin!=NULL) cell9=G_allocate_f_raster_buf(); if(tranin!=NULL) cell10=G_allocate_f_raster_buf(); if(tauin!=NULL) cell11=G_allocate_f_raster_buf(); if(wdepth!=NULL) cell12=G_allocate_d_raster_buf(); zz = (float **)G_malloc (sizeof(float *)*(my)); v1 = (double **)G_malloc (sizeof(double *)*(my)); v2 = (double **)G_malloc (sizeof(double *)*(my)); if(rain != NULL) si = (double **)G_malloc (sizeof(double *)*(my)); if(infil != NULL) inf = (double **)G_malloc (sizeof(double *)*(my)); if(traps != NULL) trap = (float **)G_malloc (sizeof(float *)*(my)); cchez = (float **)G_malloc (sizeof(float *)*(my)); if(detin!=NULL) dc = (float **)G_malloc (sizeof(float *)*(my)); if(tranin!=NULL) ct = (float **)G_malloc (sizeof(float *)*(my)); if(tauin!=NULL) tau = (float **)G_malloc (sizeof(float *)*(my)); if(wdepth!=NULL) gama = (double **)G_malloc (sizeof(double *)*(my)); for(l=0;l= shear then all zero */ si[k][l] = 0.; sigma[k][l] = 0.; } else { si[k][l] = (double) (dc[k][l] * (sheer - tau[k][l])); sigma[k][l] = (double) (dc[k][l]/ct[k][l])*(sheer-tau[k][l])/(pow(sheer,1.5)); /* rill erosion=1.5, sheet = 1.1 */ } } sisum += si[k][l]; smin = amin1(smin,si[k][l]); smax = amax1(smax,si[k][l]); if (inf != NULL) { infsum += inf[k][l]; infmin = amin1(infmin,inf[k][l]); infmax = amax1(infmax,inf[k][l]); } vmax = amax1(vmax,slope[k][l]); vsum += slope[k][l]; chsum += cchez[k][l]; zmin = amin1(zmin,(double)zz[k][l]); zmax = amax1(zmax,(double)zz[k][l]); /* not clear were needed */ if (wdepth != NULL) sigmax = amax1(sigmax,sigma[k][l]); cchezmax = amax1(cchezmax,cchez[k][l]); cchez[k][l] *= sqrt(sinsl); /* saved sqrt(sinsl)*cchez to cchez array for output*/ } /* DEFined area */ } } if (inf != NULL && smax < infmax) G_warning("Infiltration exceeds the rainfall rate everywhere! No overland flow.\n"); cc = (double) mx * my; si0 = sisum / cc; vmean = vsum / cc; chmean = chsum / cc; if (inf != NULL) infmean = infsum / cc; if (wdepth != NULL) deltaw = 0.8/(sigmax * vmax); deltap = 0.25 * sqrt(stepx * stepy)/vmean; if(deltaw > deltap) timec = 4.; else timec = 1.25; printf("\n"); printf("\n zmin,zmax %f %f",zmin,zmax); printf("\n simean,vmean,chmean,deltap,deltaw %f %f %f %f %f",si0,vmean,chmean,deltap,deltaw); printf("\n MITER, timec %d %f",miter,timec); deltap = amin1(deltap,deltaw); if (wdepth != NULL) printf("\n sigmax,vmax,deltapused %f %f %f",sigmax,vmax,deltaw); /* if (wdepth != NULL) deltap = 0.1; deltap for sediment is ar. average deltap and deltaw */ /* if (wdepth != NULL) deltap = (deltaw+deltap)/2.; deltap for sediment is ar. average deltap and deltaw */ miter = (int)(timesec / (deltap * timec)); /* number of iterations */ iterout = (int)(iterout / (deltap * timec)); /* iterations for time series */ /*! For each cell (k,l) compute the length s=(v1,v2) of the path * that the particle will travel per one time step * \f$ s(k,l)=v(k,l)*dt \f$, [m]=[m/s]*[s] * give warning if there is a cell that will lead to path longer than 2 cells * * if running erosion, compute sediment transport capacity for each cell si(k,l) * \f$ * T({\bf r})=K_t({\bf r}) \bigl[\tau({\bf r})\bigr]^p * =K_t({\bf r}) \bigl[\rho_w\, g h({\bf r}) \sin \beta ({\bf r}) \bigr]^p * \f$ * [kg/ms]=... */ for (k = 0; k < my; k++) { for (l = 0; l < mx; l++) { if (zz[k][l] != UNDEF) { v1[k][l] *= deltap; v2[k][l] *= deltap; /* if(v1[k][l]*v1[k][l]+v2[k][l]*v2[k][l] > cellsize, warning, napocitaj * ak viac ako 10%a*/ if (inf!=NULL) inf[k][l] *= timesec; /* THIS IS CORRECT SOLUTION currently commented out*/ if(wdepth != NULL) gama[k][l] = 0.; if(et!=NULL) { if(sigma[k][l] == 0. || slope[k][l] == 0.) si[k][l] = 0.; else si[k][l] = si[k][l] / (slope[k][l] * sigma[k][l]); /* temp for transp. cap. erod */ } } /* DEFined area */ } } /*! compute transport capacity limted erosion/deposition et * as a divergence of sediment transport capacity * \f$ D_T({\bf r})= \nabla\cdot {\bf T}({\bf r}) * \f$ */ if(et!=NULL) { erod(si); /* compute divergence of t.capc */ if (output_et() != 1) G_fatal_error ("Cannot write et file"); } /*! compute the inversion operator and store it in sigma - note that after this * sigma does not store the first order reaction coefficient but the operator * WRITE the equation here */ if(wdepth != NULL) { for (k = 0; k < my; k++) { for (l = 0; l < mx; l++) { if (zz[k][l] != UNDEF) { if(et!=NULL) si[k][l] = si[k][l] * slope[k][l] * sigma[k][l]; /* get back from temp */ if( sigma[k][l] != 0.) /* rate of weight loss - w=w*sigma , vaha prechadzky po n-krokoch je sigma^n */ sigma[k][l] = exp(-sigma[k][l] * deltap * slope[k][l]); /* not clear what's here :-\ */ /* if(sigma[k][l]<0.5) warning, napocitaj, ak vacsie ako 50% skonci, zmensi deltap)*/ } } /*DEFined area */ } } return (1); } double amax1 (double arg1, double arg2) { double res; if (arg1>=arg2) { res = arg1; } else { res = arg2; } return res; } double amin1 (double arg1, double arg2) { double res; if (arg1<=arg2) { res = arg1; } else { res = arg2; } return res; } int min (int arg1, int arg2) { int res; if (arg1 <= arg2) { res = arg1; } else { res = arg2; } return res; } int max (int arg1, int arg2) { int res; if (arg1>=arg2) { res = arg1; } else { res = arg2; } return res; }