TheBoussinesqModel  3.2.1
 All Data Structures Files Functions Variables Typedefs Macros Pages
main.c
Go to the documentation of this file.
1 
57 #include <sys/stat.h>
58 
59 #include "turtle.h"
60 #include "license.h"
61 #include "get_filenames.h"
62 #include "rw_maps.h"
63 #include "linear_span.h"
64 #include "geometry.h"
65 
66 #include "geometry_utilities.h"
67 #include "read_command_line.h"
69 //#include "geometry_io.h"cccc
70 //#include "geometry_attribute.h"
71 //#include "geometry_freememory.h"
72 
73 
74 #include "g_raster2plvector.h"
75 
76 #include "bigcells2.h"
77 #include "geometry2.h"
78 #include "b_solver.h"
79 #include "b_readgrid.h"
80 #include "b_volumes.h"
81 #include "b_utilities.h"
82 #include "keywords_file_b.h"
83 
84 #define PRINT print
85 
86 #define VERBOSE_M "-verbose"
87 #define CREATE_GRID "-creates_the_grid"
88 #define WRITE_GRID "-writes_the_grid"
89 
90 #define MESH_M "-only_mesh"
91 #define WPATH "-wpath"
92 #define NOWPATH ""
93 
94 
95 #define FLOAT_TYPE 0
96 #define MAP_FORMAT 3
97 
98 #define BOUNDARY -10
99 #define EWRES 1
100 #define NSRES 2
101 
102 #define INTEGER_NULL -99
103 #define D_INIT -999.0
104 
105 #define NO_OPTION "missing_option"
106 #define ARITHMETIC_MEAN0 "--arithmetic-mean0"
107 #define MISSING_FILE join_strings(wpath,NO_OPTION)
108 #define PROGRAM "boussinesq"
109 
110 #define FILE_LINES "-lines-resume"
111 #define FILE_POLYGONS "-polygons-resume"
112 #define FILE_CONNECTIONS "-connections-resume"
113 #define RESULTS_FILES "-results-rasters"
114 
115 #define FILE_DATA "data="
116 
117 #define RFACTOR param->max_error
118 /* START HEADER FILE *
119 
120 /* END HEADER FILE */
121 //char buffer[FILENAME_MAX];
122 
123 
127 
129 
130  /* all variable which are gridded are declared below as global (dynamic) doublevectors */
144 DOUBLEVECTOR *water_depth; /* map of water depth */
145 
150 int read_b_parameters_ft(char *filename_data, short print);
151 char *wpath;
152 
153 int main(int argc,char *argv[]) {
161  short print=read_flag(argc,argv,VERBOSE_M,0);
162 
163  flag=(FLAG *)malloc(sizeof(FLAG));
164  if (!flag) t_error("flag was not allocated!!");
165 
166  flag->arithmetic_mean0=read_flag(argc,argv,ARITHMETIC_MEAN0,print);
167  short cgrid=read_flag(argc,argv,CREATE_GRID,print);
168  short wgrid=read_flag(argc,argv,WRITE_GRID,print);
169  double time_init,time_end;
170  long n_cm_layers,n_fm_layers,i,j;
171 
172  time_init=clock();
173 
174  /* get filanames */
175  wpath=read_option_string(argc,argv,WPATH,NOWPATH,print);
176 
177  filenames=get_filenames_from_keys(wpath,PROGRAM,print);
178 
179  n_cm_layers=N_MAPS; //2;//CM_NLAYERS;
180  n_fm_layers=N_MAPS;
181 
182 
183 
184 
185  draster=get_doubleraster_map(n_cm_layers,n_fm_layers,filenames->element[I_MORPHO_ELEVATION_COARSE]+1,filenames->element[I_MORPHO_ELEVATION_FINE]+1,print);
186 // if (cgrid==1) { //lines commented by Emanuele Cordano on 27-10-2009
187  long dc=draster->coarse->layer[DTM_MASK]->nch*2.0;
188  long df=draster->fine->layer[DTM_MASK]->nch*2.0; /* temporary modification */
189  dsq=get_doublesquare_grid(draster,filenames->element[O_RESUME_FILES]+1,index_pixel_from_a_bin,index_pixel_from_a_bin,dc,df,print);
190  if (wgrid==1) write_doublesquare_grid(dsq);
191  dgrid=new_double_grid_from_doublesquare_grid(dsq); //ec 20100413
192 // } else {
193 // dsq=read_doublesquare_grid(draster,filenames->element[O_RESUME_FILES]+1,index_pixel_from_a_bin,index_pixel_from_a_bin,print);
194 // if (wgrid==1) write_doublesquare_grid(dsq);
195 // }
196  /* mask maps for lines */
197 
198  draster->coarse->layer[H_MASK]=doublemap_from_longmap(dsq->big->indices_horizontal_lines,dsq->big->novalue,draster->coarse->UV->V->element[2]);
199  draster->coarse->layer[V_MASK]=doublemap_from_longmap(dsq->big->indices_vertical_lines,dsq->big->novalue,draster->coarse->UV->V->element[2]);
200 
201  draster->fine->layer[H_MASK]=doublemap_from_longmap(dsq->fine->indices_horizontal_lines,dsq->big->novalue,draster->fine->UV->V->element[2]);
202  draster->fine->layer[V_MASK]=doublemap_from_longmap(dsq->fine->indices_vertical_lines,dsq->big->novalue,draster->fine->UV->V->element[2]);
203 
204 
205  /* abbresses (indices) */
206 
207  if (wgrid==1) write_cell(filenames->element[O_COARSEMAP_INDEX_CELLS]+1,dsq->big->grid->polygons->nh,draster->coarse->UV,dsq->big->indices_pixel,draster->coarse->layer[DTM_MASK],FLOAT_TYPE,MAP_FORMAT);
208  if (wgrid==1) write_cell(filenames->element[O_FINEMAP_INDEX_CELLS]+1,dsq->fine->grid->polygons->nh,draster->fine->UV,dsq->fine->indices_pixel,draster->fine->layer[DTM_MASK],FLOAT_TYPE,MAP_FORMAT);
209 
210 
211  if (wgrid==1) write_lines(filenames->element[O_COARSEMAP_INDEX_LINES]+1,dsq->big->grid->lines->nh,dsq->big->nhorizontal_lines,draster->coarse->UV,dsq->big->indices_horizontal_lines,dsq->big->indices_vertical_lines,draster->coarse->layer[H_MASK],draster->coarse->layer[V_MASK],FLOAT_TYPE,MAP_FORMAT);
212  if (wgrid==1) write_lines(filenames->element[O_FINEMAP_INDEX_LINES]+1,dsq->fine->grid->lines->nh,dsq->fine->nhorizontal_lines,draster->fine->UV,dsq->fine->indices_horizontal_lines,dsq->fine->indices_vertical_lines,draster->fine->layer[H_MASK],draster->fine->layer[V_MASK],FLOAT_TYPE,MAP_FORMAT);
213 
214  /* allocation and initialization of the gridded variables */
215 
216 
217 
218  elevation_bottom_fine=read_doublevector_from_raster(A_FLAG,filenames->element[I_MORPHO_ELEVATION_FINE]+1,draster->fine->layer[DTM_MASK],draster->fine->UV,dsq->fine->indices_pixel);
219  if (elevation_bottom_fine==NULL) printf("Error: map corresponding to %s is missing or does not have an acceptable format \n",filenames->element[I_MORPHO_ELEVATION_FINE]+1);
220 
221  elevation_bottom_coarse=read_doublevector_from_raster(A_FLAG,filenames->element[I_MORPHO_ELEVATION_COARSE]+1,draster->coarse->layer[DTM_MASK],draster->coarse->UV,dsq->big->indices_pixel);
222  if (elevation_bottom_coarse==NULL) printf("Error: map corresponding to %s is missing or does not have an acceptable format \n",filenames->element[I_MORPHO_ELEVATION_COARSE]+1);
223 
224  elevation_terrain_fine=read_doublevector_from_raster(A_FLAG,filenames->element[I_MORPHO_ELEVATION_TERRAINSURFACE_FINE]+1,draster->fine->layer[DTM_MASK],draster->fine->UV,dsq->fine->indices_pixel);
225  if (elevation_terrain_fine==NULL) {
226  elevation_terrain_fine=new_doublevector(elevation_bottom_fine->nh);
227  for (i=elevation_terrain_fine->nl;i<=elevation_terrain_fine->nh;i++) {
228  elevation_terrain_fine->element[i]=elevation_bottom_fine->element[i]+10000.0;
229  }
230  printf("Error: map corresponding to %s is missing or does not have an acceptable format (tha map is automally calculated!) \n",filenames->element[I_MORPHO_ELEVATION_TERRAINSURFACE_FINE]+1);
231  } else {
232 
233  for (i=elevation_terrain_fine->nl;i<=elevation_terrain_fine->nh;i++) {
234  elevation_terrain_fine->element[i]=fmax(elevation_terrain_fine->element[i],elevation_terrain_fine->element[i]);
235  // elevation_terrain_fine->element[i]=elevation_terrain_fine->element[i]+10000.0;
236  }
237  }
238  /* creating the vector containing the lowest point of each coarse cell */
239  elevation_bottom_bottom=new_doublevector(elevation_bottom_coarse->nh);
240  for(i=elevation_bottom_bottom->nl;i<=elevation_bottom_bottom->nh;i++) {
241  elevation_bottom_bottom->element[i]=min_elevation(i);
242  }
243 
244 
245 
246 
247  elevation_bottom_flines=interpolete_volume2lines(elevation_bottom_fine,dsq->fine->grid);
248 
249  elevation_terrain_flines=interpolete_volume2lines(elevation_terrain_fine,dsq->fine->grid); /* added by EC for surface flow on 22 Oct 2009 */
250 
251  /* sorting of internal small cells by Emanueke Cordano on 20100417 */
252  /* start TEST ec 20100421
253  long nex=2;
254  printf(" %ld:",nex);
255 // for (i=NL;i<=dgrid->small_polygon_content->index->element[nex];i++) {
256 // printf(" %ld, ",dgrid->small_polygon_content->element[nex][i]);
257 // }
258 // printf(" \n \n elevation:");
259  for (i=NL;i<=dgrid->small_polygon_content->index->element[nex];i++) {
260  // printf(" %lf,",elevation_bottom_fine->element[dgrid->small_polygon_content->element[nex][i]]);
261  printf(" %ld, %ld ,%lf, \n",i,dgrid->small_polygon_content->element[nex][i],elevation_bottom_fine->element[dgrid->small_polygon_content->element[nex][i]]);
262  }
263 
264  printf("\n");
265 // print_longbin_elements(dgrid->small_polygon_content,100);
266  //stop_execution();
267  END start TEST ec 20100421 */
268  bubble_sort_elevation_in_longbin(dgrid->small_polygon_content,elevation_bottom_fine);
269  /* start TEST ec 20100421
270  //nex=100;
271  printf(" %ld:",nex);
272 // for (i=NL;i<=dgrid->small_polygon_content->index->element[nex];i++) {
273  // printf(" %ld,",dgrid->small_polygon_content->element[nex][i]);
274 // }
275 // printf(" \n \n elevation:");
276  for (i=NL;i<=dgrid->small_polygon_content->index->element[nex];i++) {
277  // printf(" %lf,",elevation_bottom_fine->element[dgrid->small_polygon_content->element[nex][i]]);
278  printf(" %ld, %ld ,%lf, \n",i,dgrid->small_polygon_content->element[nex][i],elevation_bottom_fine->element[dgrid->small_polygon_content->element[nex][i]]);
279  }
280  printf("\n");
281 // print_longbin_elements(dgrid->small_polygon_content,100);
282  stop_execution();
283 // print_longbin_elements(dgrid->small_polygon_content,100);
284  stop_execution();
285  END TEST ec 20100421 */
286  bubble_sort_elevation_in_longbin(dgrid->small_line_content,elevation_bottom_flines);
287  /* end sorting of internal small cells by Emanueke Cordano on 20100417*/
288  water_surface_elevation=read_doublevector_from_raster(A_FLAG,filenames->element[I_INITCOND_WATERSURFACE_ELEVATION]+1,draster->coarse->layer[DTM_MASK],draster->coarse->UV,dsq->big->indices_pixel);
289  if (water_surface_elevation==NULL) printf("Error: map corresponding to %s is missing or does not have an acceptable format \n",filenames->element[I_INITCOND_WATERSURFACE_ELEVATION]+1);
290 
291  porosity_fine=read_doublevector_from_raster(A_FLAG,filenames->element[I_POROSITY_FINE]+1,draster->fine->layer[DTM_MASK],draster->fine->UV,dsq->fine->indices_pixel);
292  if (water_surface_elevation==NULL) printf("Error: map corresponding to %s is missing or does not have an acceptable format \n",filenames->element[I_POROSITY_FINE]+1);
293 
294  water_depth=read_doublevector_from_raster(A_FLAG,filenames->element[I_INITCOND_WATERDEPTH]+1,draster->coarse->layer[DTM_MASK],draster->coarse->UV,dsq->big->indices_pixel);
295  if ((water_depth==NULL) && (water_surface_elevation==NULL)) printf("Error: map corresponding to %s is missing or does not have an acceptable format \n",filenames->element[I_INITCOND_WATERDEPTH]+1);
296  if ((water_depth!=NULL) && (water_surface_elevation==NULL)) { // ec 20100325
297  water_surface_elevation=new_doublevector(water_depth->nh);
298  printf("Warning: map corresponding to %s is missing and is approximately estimated \n",filenames->element[I_INITCOND_WATERSURFACE_ELEVATION]+1);
299  for (i=water_surface_elevation->nl;i<=water_surface_elevation->nh;i++) {
300  water_surface_elevation->element[i]=elevation_bottom_bottom->element[i]+water_depth->element[i]; //ec 20100325 approximate relation
301  }
302  }// end ec 20100325 approximate relation
303  /* modified by Emanuele Cordano on 14/10/2009 */
304  outlet_coefficient_v_fine=read_doublevector_from_raster(A_FLAG,filenames->element[I_OUTLET_COEFFICIENT_FINE]+1,draster->fine->layer[DTM_MASK],draster->fine->UV,dsq->fine->indices_pixel);
305  if (outlet_coefficient_v_fine==NULL) {
306  printf("Warning: map corresponding to %s is missing or does not have an acceptable format, there is no outlets \n",filenames->element[I_OUTLET_COEFFICIENT_FINE]+1);
307  outlet_coefficient=NULL;
308  } else {
309  outlet_coefficient=interpolete_volume2lines(outlet_coefficient_v_fine,dgrid->fine);
310  }
311  /* Emanuele Cordano EC 20100421 end */
312  outlet_coefficient_surf_v_fine=read_doublevector_from_raster(A_FLAG,filenames->element[I_OUTLET_COEFFICIENT_SURF_FINE]+1,draster->fine->layer[DTM_MASK],draster->fine->UV,dsq->fine->indices_pixel);
313  if (outlet_coefficient_surf_v_fine==NULL) {
314  printf("Warning: map corresponding to %s is missing or does not have an acceptable format, there is no outlets \n",filenames->element[I_OUTLET_COEFFICIENT_SURF_FINE]+1);
315  outlet_coefficient_surf=NULL;
316  } else {
317  outlet_coefficient_surf=interpolete_volume2lines(outlet_coefficient_surf_v_fine,dgrid->fine);
318  }
319 
320 
321  /* modified by Emanuele Cordano on 26/10/2009 */
322  runoff_coefficient_v_coarse=read_doublevector_from_raster(A_FLAG,filenames->element[I_RUNOFF_COEFFICIENT_COARSE]+1,draster->coarse->layer[DTM_MASK],draster->coarse->UV,dsq->big->indices_pixel);
323  if (runoff_coefficient_v_coarse==NULL) {
324  printf("Warning: map corresponding to %s is missing or does not have an acceptable format, coefficients are assumed to be zero \n",filenames->element[I_RUNOFF_COEFFICIENT_COARSE]+1);
325  // outlet_coefficient=NULL;
326  runoff_coefficient=new_doublevector(dsq->big->grid->lines->nh);
327  for (i=runoff_coefficient->nl;i<=runoff_coefficient->nh;i++) {
328  runoff_coefficient->element[i]=0;
329  }
330  } else {
331  runoff_coefficient=interpolete_volume2lines(runoff_coefficient_v_coarse,dsq->big->grid);
332  }
333 
334  /* end modified by Emanuele Cordano on 14/10/2009 */
335  water_mass_error=new_doublevector(water_surface_elevation->nh);
336  /* allocation and the initialization of the surface velocity vector */
337  surface_water_velocity=read_doublevector_from_a_ft_format_single_file(filenames->element[I_INITIAL_VELOCITY_FT_ARRAY]+1,print);
338  /* reading subsurface from ascii file */
339 
340  if (surface_water_velocity==NULL) {
341  surface_water_velocity=new_doublevector(dsq->big->grid->lines->nh);
342  for(j=surface_water_velocity->nl;j<=surface_water_velocity->nh;j++) {
343  surface_water_velocity->element[j]=0.0;
344 
345  }
346  }
347  F1_wet_vert_area=new_doublevector(dsq->big->grid->lines->nh);
348  for(j=F1_wet_vert_area->nl;j<=F1_wet_vert_area->nh;j++) {
349  F1_wet_vert_area->element[j]=0.0;
350  }
351 
352  /* end allocation and the infiltration */
353  /* read the parameters */
354  read_b_parameters_ft(filenames->element[I_PARAM_FT_FILE]+1,print);
355 
356  /* time loop */
357 
358  int s=time_loop(print,write_map_results);
359 
360  free_doublevector(elevation_bottom_fine);
361  free_doublevector(elevation_bottom_coarse);
362  free_doublevector(elevation_terrain_fine); /* modified by Emanuele on 22/10/2009 */
363  free_doublevector(elevation_bottom_flines);
364  free_doublevector(elevation_terrain_flines); /* modified by Emanuele on 22/10/2009 */
365  free_doublevector(elevation_bottom_bottom);
366  free_doublevector(porosity_fine);
367  free_doublevector(water_surface_elevation);
368  free_doublevector(water_mass_error);
369 
370  free_doublevector(surface_water_velocity); /* added by Emauele Cordano on 22/10/2009 */
371  free_doublevector(F1_wet_vert_area); /* added by Emauele Cordano on 29/10/2009 */
372 
373  /* modified by Emanuele on 14/10/2009 */
374  if (outlet_coefficient!=NULL) free_doublevector(outlet_coefficient);
375  if (outlet_coefficient_v_fine!=NULL) free_doublevector(outlet_coefficient_v_fine);
376  // ec 20100421
377  if (outlet_coefficient_surf!=NULL) free_doublevector(outlet_coefficient_surf);
378  if (outlet_coefficient_surf_v_fine!=NULL) free_doublevector(outlet_coefficient_surf_v_fine);
379 
380  /* modified by Emanuele on 26/10/2009 */
381  if (runoff_coefficient!=NULL) free_doublevector(runoff_coefficient);
382  if (runoff_coefficient_v_coarse!=NULL) free_doublevector(runoff_coefficient_v_coarse);
383 
384  free_stringbin(filenames);
385  free_doubleraster_map(draster);
386  free_DOUBLE_GRID(dgrid); // ec 20100413
388  free(param);
389  time_end=clock();
390  printf("End of simulation, execution time %lf seconds \n",(time_end-time_init)/CLOCKS_PER_SEC);
391  return 0;
392 
393 }
394 
395 
396 int read_b_parameters_ft(char *filename_data, short print){
408  FILE *fd;
409  DOUBLEVECTOR *scalars;
411  int iks=1;/*#1 ks hydraulic saturated conductivity */
412  int it_start=iks+1; /*#2 t_start - intial time */
413  int it_end=it_start+1; /*#3 t_end - end time */
414  int idt=it_end+1; /*#4 dt integration time step */
415  int idt_print=idt+1;/*#5 dt results printing time steps time */
416  int imaxerror=idt_print+1; /*#6 maximum mass error admitted [m]*/
417  int ixtemp_adm=imaxerror+1; /*#7 massimum absolute tollerance on water surface elevation [m] */
418  int ideta=ixtemp_adm+1; /* #8 variation of water surce elevation utilzed for the first derivative of cell volume vs eta */
419  int i_nondirichlet=ideta+1; /* #9 null value for Dirichlet nodes */
420  int i_exp_dirichlet=i_nondirichlet+1; /* #10 exponent of time in Dirichlet condition according to Lockington, et al., 2000 */
421  int i_exp_outlet=i_exp_dirichlet+1; /* #11 exponent p of the rating curve at the outlet q_discharge=C*h_sup^p */
422  int i_exp_outlet_surf=i_exp_outlet+1; /* #12 exponent p of the rating curve at the outlet (surface discharge) q_discharge=C_surf*h_sup^p_surf */
423  int i_exp_runoff=i_exp_outlet_surf+1; /* #13 exponent for surface flow bottom dissipation (surface runoff) (dimensionless) */
424  int i_gravity=i_exp_runoff+1; /* #14 gravity acceleration */
425  int iscalars=i_gravity;
426  short ifile;
427 
428 
429  fd=t_fopen(filename_data,"r");
430  ifile=read_index(fd,print);
431  scalars=read_doublearray(fd,print);
432  if (scalars->nh!=iscalars) {
433  printf ("Warning in read_boussinesq_data_ft: Unexpected number of parameters %d instead of %ld \n ",iscalars,scalars->nh);
434 
435  }
436  param=(PARAM *)malloc(sizeof(PARAM));
437  if (!param) t_error("(in read_boussinesq_data_ft) param was not allocated");
438 
439 // param->Ks=scalars->element[iks];
440  param->Ks=get_value_from_doublevector(iks,scalars);
441  param->t_start=get_value_from_doublevector(it_start,scalars);
442  // scalars->element[it_start];
443  param->t_end=get_value_from_doublevector(it_end,scalars);
444  // scalars->element[it_end];
445  param->dt=get_value_from_doublevector(idt,scalars);
446  // scalars->element[idt_print];
447  param->dt_print=get_value_from_doublevector(idt_print,scalars);
448  // scalars->element[idt_print];
449  param->max_errors=get_value_from_doublevector(imaxerror,scalars);
450  // scalars->element[imaxerror];
451  param->x_temp_adm=get_value_from_doublevector(ixtemp_adm,scalars);
452 // scalars->element[ixtemp_adm];
453  param->deta=get_value_from_doublevector(ideta,scalars);
454 // scalars->element[ideta];
455  param->null_dirichlet=get_value_from_doublevector(i_nondirichlet,scalars);
456 // scalars->element[i_nondirichlet];
457  param->exp_dirichlet=get_value_from_doublevector(i_exp_dirichlet,scalars);
458 // scalars->element[i_exp_dirichlet];
459  /* added on 14/10/2009 by Emanuele */
460  param->p_outlet=get_value_from_doublevector(i_exp_outlet,scalars);
461  // ec 20100421 i_exp_outlet_surf
462  param->p_outlet_surf=get_value_from_doublevector(i_exp_outlet_surf,scalars);
463  /* added on 22/10/2009 by Emanuele */
464  param->p_runoff=get_value_from_doublevector(i_exp_runoff,scalars);
465  /* added on 2/12/2009 */
466  param->gravity=get_value_from_doublevector(i_gravity,scalars);
467  if (param->gravity==-9999.0) param->gravity=9.81;
468 
469  t_fclose(fd);
470 
471  free_doublevector(scalars);
472  /* Initializa time */
473  param->t=param->t_start;
474 
475  return 0;
476 
477 
478 
479 }
480