main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /****************************************************************************
  2. *
  3. * MODULE: r.gwflow
  4. *
  5. * AUTHOR(S): Original author
  6. * Soeren Gebbert soerengebbert <at> gmx <dot> de
  7. * 27 11 2006 Berlin
  8. * PURPOSE: Calculates confiend and unconfined transient two 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 <math.h>
  21. #include <grass/gis.h>
  22. #include <grass/raster.h>
  23. #include <grass/gmath.h>
  24. #include <grass/glocale.h>
  25. #include <grass/N_pde.h>
  26. #include <grass/N_gwflow.h>
  27. /*- Parameters and global variables -----------------------------------------*/
  28. typedef struct
  29. {
  30. struct Option *output, *phead, *status, *hc_x, *hc_y, *q, *s, *r, *top,
  31. *bottom, *vector_x, *vector_y, *budget, *type,
  32. *river_head, *river_bed, *river_leak, *drain_bed, *drain_leak,
  33. *dt, *maxit, *innerit, *error, *solver;
  34. struct Flag *full_les;
  35. } paramType;
  36. paramType param; /*Parameters */
  37. /*- prototypes --------------------------------------------------------------*/
  38. static void set_params(void); /*Fill the paramType structure */
  39. static void copy_result(N_array_2d * status, N_array_2d * phead_start,
  40. double *result, struct Cell_head *region,
  41. N_array_2d * target);
  42. static N_les *create_solve_les(N_geom_data * geom, N_gwflow_data2d * data,
  43. N_les_callback_2d * call, const char *solver, int maxit,
  44. double error);
  45. /* ************************************************************************* */
  46. /* Set up the arguments we are expecting ********************************** */
  47. /* ************************************************************************* */
  48. void set_params(void)
  49. {
  50. param.phead = G_define_standard_option(G_OPT_R_INPUT);
  51. param.phead->key = "phead";
  52. param.phead->description = _("Name of input raster map with initial piezometric head in [m]");
  53. param.status = G_define_standard_option(G_OPT_R_INPUT);
  54. param.status->key = "status";
  55. param.status->description =
  56. _("Name of input raster map providing boundary condition status: 0-inactive, 1-active, 2-dirichlet");
  57. param.hc_x =G_define_standard_option(G_OPT_R_INPUT);
  58. param.hc_x->key = "hc_x";
  59. param.hc_x->description =
  60. _("Name of input raster map with x-part of the hydraulic conductivity tensor in [m/s]");
  61. param.hc_y = G_define_standard_option(G_OPT_R_INPUT);
  62. param.hc_y->key = "hc_y";
  63. param.hc_y->description =
  64. _("Name of input raster map with y-part of the hydraulic conductivity tensor in [m/s]");
  65. param.q = G_define_standard_option(G_OPT_R_INPUT);
  66. param.q->key = "q";
  67. param.q->required = NO;
  68. param.q->description = _("Name of input raster map with water sources and sinks in [m^3/s]");
  69. param.s = G_define_standard_option(G_OPT_R_INPUT);
  70. param.s->key = "s";
  71. param.s->description = _("Name of input raster map with storativity for confined or effective porosity for unconfined groundwater flow booth in [-] ");
  72. param.r = G_define_standard_option(G_OPT_R_INPUT);
  73. param.r->key = "recharge";
  74. param.r->required = NO;
  75. param.r->guisection = _("Recharge");
  76. param.r->description =
  77. _("Recharge input raster map e.g: 6*10^-9 per cell in [m^3/s*m^2]");
  78. param.top = G_define_standard_option(G_OPT_R_INPUT);
  79. param.top->key = "top";
  80. param.top->description = _("Name of input raster map describing the top surface of the aquifer in [m]");
  81. param.bottom = G_define_standard_option(G_OPT_R_INPUT);
  82. param.bottom->key = "bottom";
  83. param.bottom->description = _("Name of input raster map describing the bottom surface of the aquifer in [m]");
  84. param.output = G_define_standard_option(G_OPT_R_OUTPUT);
  85. param.output->key = "output";
  86. param.output->description = _("Output raster map storing the numerical result [m]");
  87. param.vector_x = G_define_standard_option(G_OPT_R_OUTPUT);
  88. param.vector_x->key = "vx";
  89. param.vector_x->required = NO;
  90. param.vector_x->description =
  91. _("Output raster map to store the groundwater filter velocity vector part in x direction [m/s]");
  92. param.vector_y = G_define_standard_option(G_OPT_R_OUTPUT);
  93. param.vector_y->key = "vy";
  94. param.vector_y->required = NO;
  95. param.vector_y->description =
  96. _("Output raster map to store the groundwater filter velocity vector part in y direction [m/s]");
  97. param.budget = G_define_standard_option(G_OPT_R_OUTPUT);
  98. param.budget->key = "budget";
  99. param.budget->required = NO;
  100. param.budget->description =
  101. _("Output raster map to store the groundwater budget for each cell [m^3/s]");
  102. param.type = G_define_option();
  103. param.type->key = "type";
  104. param.type->type = TYPE_STRING;
  105. param.type->required = YES;
  106. param.type->answer = "confined";
  107. param.type->options = "confined,unconfined";
  108. param.type->description = _("The type of groundwater flow");
  109. /*Variants of the cauchy boundary condition */
  110. param.river_bed = G_define_standard_option(G_OPT_R_INPUT);
  111. param.river_bed->key = "river_bed";
  112. param.river_bed->required = NO;
  113. param.river_bed->description = _("Name of input raster map providing the height of the river bed in [m]");
  114. param.river_bed->guisection = "River";
  115. param.river_head = G_define_standard_option(G_OPT_R_INPUT);
  116. param.river_head->key = "river_head";
  117. param.river_head->required = NO;
  118. param.river_head->guisection = "River";
  119. param.river_head->description =
  120. _("Name of input raster map providing the water level (head) of the river with leakage connection in [m]");
  121. param.river_leak = G_define_standard_option(G_OPT_R_INPUT);
  122. param.river_leak->key = "river_leak";
  123. param.river_leak->required = NO;
  124. param.river_leak->guisection = "River";
  125. param.river_leak->description =
  126. _("Name of input raster map providing the leakage coefficient of the river bed in [1/s].");
  127. param.drain_bed = G_define_standard_option(G_OPT_R_INPUT);
  128. param.drain_bed->key = "drain_bed";
  129. param.drain_bed->type = TYPE_STRING;
  130. param.drain_bed->required = NO;
  131. param.drain_bed->gisprompt = "old,raster,raster";
  132. param.drain_bed->guisection = "Drainage";
  133. param.drain_bed->description = _("Name of input raster map providing the height of the drainage bed in [m]");
  134. param.drain_leak = G_define_standard_option(G_OPT_R_INPUT);
  135. param.drain_leak->key = "drain_leak";
  136. param.drain_leak->required = NO;
  137. param.drain_leak->guisection = "Drainage";
  138. param.drain_leak->description =
  139. _("Name of input raster map providing the leakage coefficient of the drainage bed in [1/s]");
  140. param.dt = N_define_standard_option(N_OPT_CALC_TIME);
  141. param.maxit = N_define_standard_option(N_OPT_MAX_ITERATIONS);
  142. param.innerit = N_define_standard_option(N_OPT_MAX_ITERATIONS);
  143. param.innerit->description =_("The maximum number of iterations in the linearization approach");
  144. param.innerit->answer = "25";
  145. param.error = N_define_standard_option(N_OPT_ITERATION_ERROR);
  146. param.solver = N_define_standard_option(N_OPT_SOLVER_SYMM);
  147. param.solver->options = "cg,pcg,cholesky";
  148. param.full_les = G_define_flag();
  149. param.full_les->key = 'f';
  150. param.full_les->guisection = "Solver";
  151. param.full_les->description = _("Allocate a full quadratic linear equation system,"
  152. " default is a sparse linear equation system.");
  153. }
  154. /* ************************************************************************* */
  155. /* Main function *********************************************************** */
  156. /* ************************************************************************* */
  157. int main(int argc, char *argv[])
  158. {
  159. struct GModule *module = NULL;
  160. N_gwflow_data2d *data = NULL;
  161. N_geom_data *geom = NULL;
  162. N_les *les = NULL;
  163. N_les_callback_2d *call = NULL;
  164. double *tmp_vect = NULL;
  165. struct Cell_head region;
  166. double error, max_norm = 0, tmp;
  167. int maxit, i, innerit, inner_count = 0;
  168. char *solver;
  169. int x, y, stat;
  170. N_gradient_field_2d *field = NULL;
  171. N_array_2d *xcomp = NULL;
  172. N_array_2d *ycomp = NULL;
  173. char *buff = NULL;
  174. int with_river = 0, with_drain = 0;
  175. /* Initialize GRASS */
  176. G_gisinit(argv[0]);
  177. module = G_define_module();
  178. G_add_keyword(_("raster"));
  179. G_add_keyword(_("groundwater flow"));
  180. G_add_keyword(_("hydrology"));
  181. module->description =
  182. _("Numerical calculation program for transient, confined and unconfined groundwater flow in two dimensions.");
  183. /* Get parameters from user */
  184. set_params();
  185. if (G_parser(argc, argv))
  186. exit(EXIT_FAILURE);
  187. /* Make sure that the current projection is not lat/long */
  188. if ((G_projection() == PROJECTION_LL))
  189. G_fatal_error(_("Lat/Long location is not supported by %s. Please reproject map first."),
  190. G_program_name());
  191. /*Check the river parameters */
  192. if (param.river_leak->answer == NULL && param.river_bed->answer == NULL &&
  193. param.river_head->answer == NULL) {
  194. with_river = 0;
  195. }
  196. else if (param.river_leak->answer != NULL &&
  197. param.river_bed->answer != NULL &&
  198. param.river_head->answer != NULL) {
  199. with_river = 1;
  200. }
  201. else {
  202. G_fatal_error
  203. (_("Please provide river_head, river_leak and river_bed maps"));
  204. }
  205. /*Check the drainage parameters */
  206. if (param.drain_leak->answer == NULL && param.drain_bed->answer == NULL) {
  207. with_drain = 0;
  208. }
  209. else if (param.drain_leak->answer != NULL &&
  210. param.drain_bed->answer != NULL) {
  211. with_drain = 1;
  212. }
  213. else {
  214. G_fatal_error(_("Please provide drain_head and drain_leak maps"));
  215. }
  216. /*Set the maximum iterations */
  217. sscanf(param.maxit->answer, "%i", &(maxit));
  218. /*Set the maximum number of inner iterations */
  219. sscanf(param.innerit->answer, "%i", &(innerit));
  220. /*Set the calculation error break criteria */
  221. sscanf(param.error->answer, "%lf", &(error));
  222. /*set the solver */
  223. solver = param.solver->answer;
  224. if (strcmp(solver, G_MATH_SOLVER_DIRECT_CHOLESKY) == 0 && !param.full_les->answer)
  225. G_fatal_error(_("The cholesky solver dos not work with sparse matrices. "
  226. "You may choose a full filled quadratic matrix, flag -f."));
  227. /*get the current region */
  228. G_get_set_window(&region);
  229. /*allocate the geometry structure for geometry and area calculation */
  230. geom = N_init_geom_data_2d(&region, geom);
  231. /*Set the function callback to the groundwater flow function */
  232. call = N_alloc_les_callback_2d();
  233. N_set_les_callback_2d_func(call, (*N_callback_gwflow_2d)); /*gwflow 2d */
  234. /*Allocate the groundwater flow data structure */
  235. data =
  236. N_alloc_gwflow_data2d(geom->cols, geom->rows, with_river, with_drain);
  237. /* set the groundwater type */
  238. if (param.type->answer) {
  239. if (strncmp("unconfined", param.type->answer, 10) == 0) {
  240. data->gwtype = N_GW_UNCONFINED;
  241. }
  242. else {
  243. data->gwtype = N_GW_CONFINED;
  244. }
  245. }
  246. /*Set the calculation time */
  247. sscanf(param.dt->answer, "%lf", &(data->dt));
  248. G_message(_("Calculation time: %g"), data->dt);
  249. /*read all input maps into the memory and take care of the
  250. * null values.*/
  251. N_read_rast_to_array_2d(param.phead->answer, data->phead);
  252. N_convert_array_2d_null_to_zero(data->phead);
  253. N_copy_array_2d(data->phead, data->phead_start);
  254. N_read_rast_to_array_2d(param.status->answer, data->status);
  255. N_convert_array_2d_null_to_zero(data->status);
  256. N_read_rast_to_array_2d(param.hc_x->answer, data->hc_x);
  257. N_convert_array_2d_null_to_zero(data->hc_x);
  258. N_read_rast_to_array_2d(param.hc_y->answer, data->hc_y);
  259. N_convert_array_2d_null_to_zero(data->hc_y);
  260. N_read_rast_to_array_2d(param.s->answer, data->s);
  261. N_convert_array_2d_null_to_zero(data->s);
  262. N_read_rast_to_array_2d(param.top->answer, data->top);
  263. N_convert_array_2d_null_to_zero(data->top);
  264. N_read_rast_to_array_2d(param.bottom->answer, data->bottom);
  265. N_convert_array_2d_null_to_zero(data->bottom);
  266. /*river is optional */
  267. if (with_river) {
  268. N_read_rast_to_array_2d(param.river_bed->answer, data->river_bed);
  269. N_read_rast_to_array_2d(param.river_head->answer, data->river_head);
  270. N_read_rast_to_array_2d(param.river_leak->answer, data->river_leak);
  271. N_convert_array_2d_null_to_zero(data->river_bed);
  272. N_convert_array_2d_null_to_zero(data->river_head);
  273. N_convert_array_2d_null_to_zero(data->river_leak);
  274. }
  275. /*drainage is optional */
  276. if (with_drain) {
  277. N_read_rast_to_array_2d(param.drain_bed->answer, data->drain_bed);
  278. N_read_rast_to_array_2d(param.drain_leak->answer, data->drain_leak);
  279. N_convert_array_2d_null_to_zero(data->drain_bed);
  280. N_convert_array_2d_null_to_zero(data->drain_leak);
  281. }
  282. /*Recharge is optional */
  283. if (param.r->answer) {
  284. N_read_rast_to_array_2d(param.r->answer, data->r);
  285. N_convert_array_2d_null_to_zero(data->r);
  286. }
  287. /*Sources or sinks are optional */
  288. if (param.q->answer) {
  289. N_read_rast_to_array_2d(param.q->answer, data->q);
  290. N_convert_array_2d_null_to_zero(data->q);
  291. }
  292. /* Set the inactive values to zero, to assure a no flow boundary */
  293. for (y = 0; y < geom->rows; y++) {
  294. for (x = 0; x < geom->cols; x++) {
  295. stat = N_get_array_2d_c_value(data->status, x, y);
  296. if (stat == N_CELL_INACTIVE) { /*only inactive cells */
  297. N_put_array_2d_d_value(data->hc_x, x, y, 0);
  298. N_put_array_2d_d_value(data->hc_y, x, y, 0);
  299. N_put_array_2d_d_value(data->s, x, y, 0);
  300. N_put_array_2d_d_value(data->q, x, y, 0);
  301. }
  302. }
  303. }
  304. /*assemble the linear equation system and solve it */
  305. les = create_solve_les(geom, data, call, solver, maxit, error);
  306. /* copy the result into the phead array for output or unconfined calculation */
  307. copy_result(data->status, data->phead_start, les->x, &region,
  308. data->phead);
  309. N_convert_array_2d_null_to_zero(data->phead);
  310. /****************************************************/
  311. /*explicite calculation of free groundwater surface */
  312. /****************************************************/
  313. if (data->gwtype == N_GW_UNCONFINED) {
  314. /* allocate memory and copy the result into a new temporal vector */
  315. tmp_vect = (double *)G_calloc(les->rows, sizeof(double));
  316. /*copy data */
  317. for (i = 0; i < les->rows; i++)
  318. tmp_vect[i] = les->x[i];
  319. /*count the number of inner iterations */
  320. inner_count = 0;
  321. do {
  322. G_message(_("Calculation of unconfined groundwater flow loop %i"),
  323. inner_count + 1);
  324. /* we will allocate a new les for each loop */
  325. if (les)
  326. N_free_les(les);
  327. /*assemble the linear equation system and solve it */
  328. les =
  329. create_solve_les(geom, data, call, solver, maxit, error);
  330. /*calculate the maximum norm of the groundwater height difference */
  331. tmp = 0;
  332. max_norm = 0;
  333. for (i = 0; i < les->rows; i++) {
  334. tmp = fabs(les->x[i] - tmp_vect[i]);
  335. if (max_norm < tmp)
  336. max_norm = tmp;
  337. /*copy the result */
  338. tmp_vect[i] = les->x[i];
  339. }
  340. G_message(_("Maximum difference between this and last increment: %g"),
  341. max_norm);
  342. /* copy the result into the phead array */
  343. copy_result(data->status, data->phead_start, les->x, &region,
  344. data->phead);
  345. N_convert_array_2d_null_to_zero(data->phead);
  346. /**/ inner_count++;
  347. }
  348. while (max_norm > 0.01 && inner_count < innerit);
  349. if (tmp_vect)
  350. free(tmp_vect);
  351. }
  352. /*release the memory */
  353. if (les)
  354. N_free_les(les);
  355. /* Compute the water budget for each cell */
  356. N_array_2d *budget = N_alloc_array_2d(geom->cols, geom->rows, 1, DCELL_TYPE);
  357. N_gwflow_2d_calc_water_budget(data, geom, budget);
  358. /*write the result to the output file */
  359. N_write_array_2d_to_rast(data->phead, param.output->answer);
  360. /*Write the water balance */
  361. if(param.budget->answer)
  362. {
  363. N_write_array_2d_to_rast(budget, param.budget->answer);
  364. }
  365. /*Compute the the velocity field if required and write the result into two raster maps */
  366. if (param.vector_x->answer && param.vector_y->answer) {
  367. field =
  368. N_compute_gradient_field_2d(data->phead, data->hc_x, data->hc_y,
  369. geom, NULL);
  370. xcomp = N_alloc_array_2d(geom->cols, geom->rows, 1, DCELL_TYPE);
  371. ycomp = N_alloc_array_2d(geom->cols, geom->rows, 1, DCELL_TYPE);
  372. N_compute_gradient_field_components_2d(field, xcomp, ycomp);
  373. N_write_array_2d_to_rast(xcomp, param.vector_x->answer);
  374. N_write_array_2d_to_rast(ycomp, param.vector_y->answer);
  375. if (buff)
  376. G_free(buff);
  377. if (xcomp)
  378. N_free_array_2d(xcomp);
  379. if (ycomp)
  380. N_free_array_2d(ycomp);
  381. if (field)
  382. N_free_gradient_field_2d(field);
  383. }
  384. if(budget)
  385. N_free_array_2d(budget);
  386. if (data)
  387. N_free_gwflow_data2d(data);
  388. if (geom)
  389. N_free_geom_data(geom);
  390. if (call)
  391. G_free(call);
  392. return (EXIT_SUCCESS);
  393. }
  394. /* ************************************************************************* */
  395. /* this function copies the result into a N_array_2d struct */
  396. /* ************************************************************************* */
  397. void
  398. copy_result(N_array_2d * status, N_array_2d * phead_start, double *result,
  399. struct Cell_head *region, N_array_2d * target)
  400. {
  401. int y, x, rows, cols, count, stat;
  402. double d1 = 0;
  403. DCELL val;
  404. rows = region->rows;
  405. cols = region->cols;
  406. count = 0;
  407. for (y = 0; y < rows; y++) {
  408. G_percent(y, rows - 1, 10);
  409. for (x = 0; x < cols; x++) {
  410. stat = N_get_array_2d_c_value(status, x, y);
  411. if (stat == N_CELL_ACTIVE) { /*only active cells */
  412. d1 = result[count];
  413. val = (DCELL) d1;
  414. count++;
  415. }
  416. else if (stat == N_CELL_DIRICHLET) { /*dirichlet cells */
  417. d1 = N_get_array_2d_d_value(phead_start, x, y);
  418. val = (DCELL) d1;
  419. count++;
  420. }
  421. else {
  422. Rast_set_null_value(&val, 1, DCELL_TYPE);
  423. }
  424. N_put_array_2d_d_value(target, x, y, val);
  425. }
  426. }
  427. return;
  428. }
  429. /* *************************************************************** */
  430. /* ***** create and solve the linear equation system ************* */
  431. /* *************************************************************** */
  432. N_les *create_solve_les(N_geom_data * geom, N_gwflow_data2d * data,
  433. N_les_callback_2d * call, const char *solver, int maxit,
  434. double error)
  435. {
  436. N_les *les;
  437. /*assemble the linear equation system */
  438. if (!param.full_les->answer)
  439. les = N_assemble_les_2d_dirichlet(N_SPARSE_LES, geom, data->status, data->phead, (void *)data, call);
  440. else
  441. les = N_assemble_les_2d_dirichlet(N_NORMAL_LES, geom, data->status, data->phead, (void *)data, call);
  442. N_les_integrate_dirichlet_2d(les, geom, data->status, data->phead);
  443. /*solve the linear equation system */
  444. if(les && les->type == N_NORMAL_LES) {
  445. if (strcmp(solver, G_MATH_SOLVER_ITERATIVE_CG) == 0)
  446. G_math_solver_cg(les->A, les->x, les->b, les->rows, maxit, error);
  447. if (strcmp(solver, G_MATH_SOLVER_ITERATIVE_PCG) == 0)
  448. G_math_solver_pcg(les->A, les->x, les->b, les->rows, maxit, error, G_MATH_DIAGONAL_PRECONDITION);
  449. if (strcmp(solver, G_MATH_SOLVER_DIRECT_CHOLESKY) == 0)
  450. G_math_solver_cholesky(les->A, les->x, les->b, les->rows, les->rows);
  451. }
  452. else if (les && les->type == N_SPARSE_LES) {
  453. if (strcmp(solver, G_MATH_SOLVER_ITERATIVE_CG) == 0)
  454. G_math_solver_sparse_cg(les->Asp, les->x, les->b, les->rows, maxit, error);
  455. if (strcmp(solver, G_MATH_SOLVER_ITERATIVE_PCG) == 0)
  456. G_math_solver_sparse_pcg(les->Asp, les->x, les->b, les->rows, maxit, error, G_MATH_DIAGONAL_PRECONDITION);
  457. }
  458. if (les == NULL)
  459. G_fatal_error(_("Unable to create and solve the linear equation system"));
  460. return les;
  461. }