main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /****************************************************************************
  2. *
  3. * MODULE: r3.gwflow
  4. *
  5. * AUTHOR(S): Original author
  6. * Soeren Gebbert soerengebbert <at> gmx <dot> de
  7. * 27 11 2006 Berlin
  8. * PURPOSE: Calculates confined transient three dimensional groundwater flow
  9. *
  10. * COPYRIGHT: (C) 2006 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. *****************************************************************************/
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <grass/gis.h>
  21. #include <grass/raster3d.h>
  22. #include <grass/gmath.h>
  23. #include <grass/glocale.h>
  24. #include <grass/N_pde.h>
  25. #include <grass/N_gwflow.h>
  26. /*- Parameters and global variables -----------------------------------------*/
  27. typedef struct
  28. {
  29. struct Option *output, *phead, *status, *hc_x, *hc_y, *hc_z, *q, *s, *r,
  30. *vector_x, *vector_y, *vector_z, *budget, *dt, *maxit, *error, *solver;
  31. struct Flag *mask;
  32. struct Flag *full_les;
  33. } paramType;
  34. paramType param; /*Parameters */
  35. /*- prototypes --------------------------------------------------------------*/
  36. static void set_params(void); /*Fill the paramType structure */
  37. static void write_result(N_array_3d * status, N_array_3d * phead_start,
  38. N_array_3d * phead, double *result,
  39. RASTER3D_Region * region, char *name);
  40. /* ************************************************************************* */
  41. /* Set up the arguments we are expecting ********************************** */
  42. /* ************************************************************************* */
  43. void set_params(void)
  44. {
  45. param.phead = G_define_standard_option(G_OPT_R3_INPUT);
  46. param.phead->key = "phead";
  47. param.phead->description = _("Input 3D raster map with initial piezometric heads in [m]");
  48. param.status = G_define_standard_option(G_OPT_R3_INPUT);
  49. param.status->key = "status";
  50. param.status->description =
  51. _
  52. ("Input 3D raster map providing the status for each cell, = 0 - inactive, 1 - active, 2 - dirichlet");
  53. param.hc_x = G_define_standard_option(G_OPT_R3_INPUT);
  54. param.hc_x->key = "hc_x";
  55. param.hc_x->description =
  56. _("Input 3D raster map with the x-part of the hydraulic conductivity tensor in [m/s]");
  57. param.hc_y = G_define_standard_option(G_OPT_R3_INPUT);
  58. param.hc_y->key = "hc_y";
  59. param.hc_y->description =
  60. _("Input 3D raster map with the y-part of the hydraulic conductivity tensor in [m/s]");
  61. param.hc_z = G_define_standard_option(G_OPT_R3_INPUT);
  62. param.hc_z->key = "hc_z";
  63. param.hc_z->description =
  64. _("Input 3D raster map with the z-part of the hydraulic conductivity tensor in [m/s]");
  65. param.q = G_define_standard_option(G_OPT_R3_INPUT);
  66. param.q->key = "q";
  67. param.q->required = NO;
  68. param.q->description = _("Input 3D raster map with sources and sinks in [m^3/s]");
  69. param.s = G_define_standard_option(G_OPT_R3_INPUT);
  70. param.s->key = "s";
  71. param.s->description = _("Specific yield [1/m] input 3D raster map");
  72. param.r = G_define_standard_option(G_OPT_R3_INPUT);
  73. param.r->key = "r";
  74. param.r->required = NO;
  75. param.r->description = _("Recharge input 3D raster map in m^3/s");
  76. param.output = G_define_standard_option(G_OPT_R3_OUTPUT);
  77. param.output->key = "output";
  78. param.output->description = _("Output 3D raster map storing the piezometric head result of the numerical calculation");
  79. param.vector_x = G_define_standard_option(G_OPT_R3_OUTPUT);
  80. param.vector_x->key = "vx";
  81. param.vector_x->required = NO;
  82. param.vector_x->description =
  83. _("Output 3D raster map storing the groundwater filter velocity vector part in x direction [m/s]");
  84. param.vector_y = G_define_standard_option(G_OPT_R3_OUTPUT);
  85. param.vector_y->key = "vy";
  86. param.vector_y->required = NO;
  87. param.vector_y->description =
  88. _("Output 3D raster map storing the groundwater filter velocity vector part in y direction [m/s]");
  89. param.vector_z = G_define_standard_option(G_OPT_R3_OUTPUT);
  90. param.vector_z->key = "vz";
  91. param.vector_z->required = NO;
  92. param.vector_z->description =
  93. _("Output 3D raster map storing the groundwater filter velocity vector part in z direction [m/s]");
  94. param.budget = G_define_standard_option(G_OPT_R3_OUTPUT);
  95. param.budget->key = "budget";
  96. param.budget->required = NO;
  97. param.budget->description =
  98. _("Output 3D raster map Storing the groundwater budget for each cell [m^3/s]\n");
  99. param.dt = N_define_standard_option(N_OPT_CALC_TIME);
  100. param.maxit = N_define_standard_option(N_OPT_MAX_ITERATIONS);
  101. param.error = N_define_standard_option(N_OPT_ITERATION_ERROR);
  102. param.solver = N_define_standard_option(N_OPT_SOLVER_SYMM);
  103. param.solver->options = "cg,pcg,cholesky";
  104. param.mask = G_define_flag();
  105. param.mask->key = 'm';
  106. param.mask->description = _("Use RASTER3D mask (if exists)");
  107. param.full_les = G_define_flag();
  108. param.full_les->key = 'f';
  109. param.full_les->description = _("Use a full filled quadratic linear equation system,"
  110. " default is a sparse linear equation system.");
  111. }
  112. /* ************************************************************************* */
  113. /* Main function *********************************************************** */
  114. /* ************************************************************************* */
  115. int main(int argc, char *argv[])
  116. {
  117. struct GModule *module = NULL;
  118. N_gwflow_data3d *data = NULL;
  119. N_geom_data *geom = NULL;
  120. N_les *les = NULL;
  121. N_les_callback_3d *call = NULL;
  122. RASTER3D_Region region;
  123. N_gradient_field_3d *field = NULL;
  124. N_array_3d *xcomp = NULL;
  125. N_array_3d *ycomp = NULL;
  126. N_array_3d *zcomp = NULL;
  127. double error;
  128. int maxit;
  129. const char *solver;
  130. int x, y, z, stat;
  131. /* Initialize GRASS */
  132. G_gisinit(argv[0]);
  133. module = G_define_module();
  134. G_add_keyword(_("raster3d"));
  135. G_add_keyword(_("groundwater flow"));
  136. G_add_keyword(_("voxel"));
  137. G_add_keyword(_("hydrology"));
  138. module->description = _("Numerical calculation program for transient, confined groundwater flow in three dimensions.");
  139. /* Get parameters from user */
  140. set_params();
  141. /* Have GRASS get pheads */
  142. if (G_parser(argc, argv))
  143. exit(EXIT_FAILURE);
  144. /*Set the maximum iterations */
  145. sscanf(param.maxit->answer, "%i", &(maxit));
  146. /*Set the calculation error break criteria */
  147. sscanf(param.error->answer, "%lf", &(error));
  148. /*Set the solver */
  149. solver = param.solver->answer;
  150. if (strcmp(solver, G_MATH_SOLVER_DIRECT_CHOLESKY) == 0 && !param.full_les->answer)
  151. G_fatal_error(_("The cholesky solver does not work with sparse matrices.\n"
  152. "Consider to choose a full filled quadratic matrix with flag -f "));
  153. /*Set the defaults */
  154. Rast3d_init_defaults();
  155. /*get the current region */
  156. Rast3d_get_window(&region);
  157. /*allocate the geometry structure for geometry and area calculation */
  158. geom = N_init_geom_data_3d(&region, geom);
  159. /*Set the function callback to the groundwater flow function */
  160. call = N_alloc_les_callback_3d();
  161. N_set_les_callback_3d_func(call, (*N_callback_gwflow_3d)); /*gwflow 3d */
  162. /*Allocate the groundwater flow data structure */
  163. data = N_alloc_gwflow_data3d(geom->cols, geom->rows, geom->depths, 0, 0);
  164. /*Set the calculation time */
  165. sscanf(param.dt->answer, "%lf", &(data->dt));
  166. /*read the g3d maps into the memory and convert the null values */
  167. N_read_rast3d_to_array_3d(param.phead->answer, data->phead,
  168. param.mask->answer);
  169. N_convert_array_3d_null_to_zero(data->phead);
  170. N_read_rast3d_to_array_3d(param.phead->answer, data->phead_start,
  171. param.mask->answer);
  172. N_convert_array_3d_null_to_zero(data->phead_start);
  173. N_read_rast3d_to_array_3d(param.status->answer, data->status,
  174. param.mask->answer);
  175. N_convert_array_3d_null_to_zero(data->status);
  176. N_read_rast3d_to_array_3d(param.hc_x->answer, data->hc_x,
  177. param.mask->answer);
  178. N_convert_array_3d_null_to_zero(data->hc_x);
  179. N_read_rast3d_to_array_3d(param.hc_y->answer, data->hc_y,
  180. param.mask->answer);
  181. N_convert_array_3d_null_to_zero(data->hc_y);
  182. N_read_rast3d_to_array_3d(param.hc_z->answer, data->hc_z,
  183. param.mask->answer);
  184. N_convert_array_3d_null_to_zero(data->hc_z);
  185. N_read_rast3d_to_array_3d(param.q->answer, data->q, param.mask->answer);
  186. N_convert_array_3d_null_to_zero(data->q);
  187. N_read_rast3d_to_array_3d(param.s->answer, data->s, param.mask->answer);
  188. N_convert_array_3d_null_to_zero(data->s);
  189. /* Set the inactive values to zero, to assure a no flow boundary */
  190. for (z = 0; z < geom->depths; z++) {
  191. for (y = 0; y < geom->rows; y++) {
  192. for (x = 0; x < geom->cols; x++) {
  193. stat = (int)N_get_array_3d_d_value(data->status, x, y, z);
  194. if (stat == N_CELL_INACTIVE) { /*only inactive cells */
  195. N_put_array_3d_d_value(data->hc_x, x, y, z, 0);
  196. N_put_array_3d_d_value(data->hc_y, x, y, z, 0);
  197. N_put_array_3d_d_value(data->hc_z, x, y, z, 0);
  198. N_put_array_3d_d_value(data->s, x, y, z, 0);
  199. N_put_array_3d_d_value(data->q, x, y, z, 0);
  200. }
  201. }
  202. }
  203. }
  204. /*assemble the linear equation system */
  205. if (!param.full_les->answer) {
  206. les =
  207. N_assemble_les_3d(N_SPARSE_LES, geom, data->status, data->phead,
  208. (void *)data, call);
  209. }
  210. else {
  211. les =
  212. N_assemble_les_3d(N_NORMAL_LES, geom, data->status, data->phead,
  213. (void *)data, call);
  214. }
  215. if (les && les->type == N_NORMAL_LES) {
  216. if (strcmp(solver, G_MATH_SOLVER_ITERATIVE_CG) == 0)
  217. G_math_solver_cg(les->A, les->x, les->b, les->rows, maxit, error);
  218. if (strcmp(solver, G_MATH_SOLVER_ITERATIVE_PCG) == 0)
  219. G_math_solver_pcg(les->A, les->x, les->b, les->rows, maxit, error,
  220. G_MATH_DIAGONAL_PRECONDITION);
  221. if (strcmp(solver, G_MATH_SOLVER_DIRECT_CHOLESKY) == 0)
  222. G_math_solver_cholesky(les->A, les->x, les->b, les->rows,
  223. les->rows);
  224. }
  225. else if (les && les->type == N_SPARSE_LES) {
  226. if (strcmp(solver, G_MATH_SOLVER_ITERATIVE_CG) == 0)
  227. G_math_solver_sparse_cg(les->Asp, les->x, les->b, les->rows,
  228. maxit, error);
  229. if (strcmp(solver, G_MATH_SOLVER_ITERATIVE_PCG) == 0)
  230. G_math_solver_sparse_pcg(les->Asp, les->x, les->b, les->rows,
  231. maxit, error, G_MATH_DIAGONAL_PRECONDITION);
  232. }
  233. if (les == NULL)
  234. G_fatal_error(_("Unable to create and solve the linear equation system"));
  235. /*write the result to the output file and copy the values to the data->phead array */
  236. write_result(data->status, data->phead_start, data->phead, les->x,
  237. &region, param.output->answer);
  238. N_free_les(les);
  239. /* Compute the water budget for each cell */
  240. N_array_3d *budget = N_alloc_array_3d(geom->cols, geom->rows, geom->depths, 1, DCELL_TYPE);
  241. N_gwflow_3d_calc_water_budget(data, geom, budget);
  242. /*Write the water balance */
  243. if(param.budget->answer)
  244. {
  245. N_write_array_3d_to_rast3d(budget, param.budget->answer, 1);
  246. }
  247. /*Compute the the velocity field if required and write the result into three rast3d maps */
  248. if (param.vector_x->answer || param.vector_y->answer || param.vector_z->answer) {
  249. field =
  250. N_compute_gradient_field_3d(data->phead, data->hc_x, data->hc_y,
  251. data->hc_z, geom, NULL);
  252. xcomp =
  253. N_alloc_array_3d(geom->cols, geom->rows, geom->depths, 1,
  254. DCELL_TYPE);
  255. ycomp =
  256. N_alloc_array_3d(geom->cols, geom->rows, geom->depths, 1,
  257. DCELL_TYPE);
  258. zcomp =
  259. N_alloc_array_3d(geom->cols, geom->rows, geom->depths, 1,
  260. DCELL_TYPE);
  261. N_compute_gradient_field_components_3d(field, xcomp, ycomp, zcomp);
  262. if(param.vector_x->answer)
  263. N_write_array_3d_to_rast3d(xcomp, param.vector_x->answer, 1);
  264. if(param.vector_y->answer)
  265. N_write_array_3d_to_rast3d(ycomp, param.vector_y->answer, 1);
  266. if(param.vector_z->answer)
  267. N_write_array_3d_to_rast3d(zcomp, param.vector_z->answer, 1);
  268. if (xcomp)
  269. N_free_array_3d(xcomp);
  270. if (ycomp)
  271. N_free_array_3d(ycomp);
  272. if (zcomp)
  273. N_free_array_3d(zcomp);
  274. if (field)
  275. N_free_gradient_field_3d(field);
  276. }
  277. if (data)
  278. N_free_gwflow_data3d(data);
  279. if (geom)
  280. N_free_geom_data(geom);
  281. if (call)
  282. G_free(call);
  283. return (EXIT_SUCCESS);
  284. }
  285. /* ************************************************************************* */
  286. /* this function writes the result from the x vector to a g3d map ********** */
  287. /* ************************************************************************* */
  288. void
  289. write_result(N_array_3d * status, N_array_3d * phead_start,
  290. N_array_3d * phead, double *result, RASTER3D_Region * region,
  291. char *name)
  292. {
  293. void *map = NULL;
  294. int changemask = 0;
  295. int z, y, x, rows, cols, depths, count, stat;
  296. double d1 = 0;
  297. rows = region->rows;
  298. cols = region->cols;
  299. depths = region->depths;
  300. /*Open the new map */
  301. map = Rast3d_open_new_opt_tile_size(name, RASTER3D_USE_CACHE_XY, region, DCELL_TYPE, 32);
  302. if (map == NULL)
  303. Rast3d_fatal_error(_("Unable to create 3D raster map <%s>"), name);
  304. /*if requested set the Mask on */
  305. if (param.mask->answer) {
  306. if (Rast3d_mask_file_exists()) {
  307. changemask = 0;
  308. if (Rast3d_mask_is_off(map)) {
  309. Rast3d_mask_on(map);
  310. changemask = 1;
  311. }
  312. }
  313. }
  314. count = 0;
  315. for (z = 0; z < depths; z++) {
  316. G_percent(z, depths - 1, 10);
  317. for (y = 0; y < rows; y++) {
  318. for (x = 0; x < cols; x++) {
  319. stat = (int)N_get_array_3d_d_value(status, x, y, z);
  320. if (stat == N_CELL_ACTIVE) { /*only active cells */
  321. d1 = result[count];
  322. /*copy the values */
  323. N_put_array_3d_d_value(phead, x, y, z, d1);
  324. count++;
  325. }
  326. else if (stat == N_CELL_DIRICHLET) { /*dirichlet cells */
  327. d1 = N_get_array_3d_d_value(phead_start, x, y, z);
  328. }
  329. else {
  330. Rast3d_set_null_value(&d1, 1, DCELL_TYPE);
  331. }
  332. Rast3d_put_double(map, x, y, z, d1);
  333. }
  334. }
  335. }
  336. /*We set the Mask off, if it was off before */
  337. if (param.mask->answer) {
  338. if (Rast3d_mask_file_exists())
  339. if (Rast3d_mask_is_on(map) && changemask)
  340. Rast3d_mask_off(map);
  341. }
  342. /* Flush all tile */
  343. if (!Rast3d_flush_all_tiles(map))
  344. Rast3d_fatal_error("Error flushing tiles with Rast3d_flush_all_tiles");
  345. if (!Rast3d_close(map))
  346. Rast3d_fatal_error(map, NULL, 0, _("Unable to close 3D raster map"));
  347. return;
  348. }