main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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/G3d.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. G3D_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 G3D 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. G3D_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(_("voxel"));
  136. G_add_keyword(_("groundwater"));
  137. G_add_keyword(_("numeric"));
  138. G_add_keyword(_("simulation"));
  139. module->description = _("Numerical calculation program for transient, confined groundwater flow in three dimensions");
  140. /* Get parameters from user */
  141. set_params();
  142. /* Have GRASS get pheads */
  143. if (G_parser(argc, argv))
  144. exit(EXIT_FAILURE);
  145. /*Set the maximum iterations */
  146. sscanf(param.maxit->answer, "%i", &(maxit));
  147. /*Set the calculation error break criteria */
  148. sscanf(param.error->answer, "%lf", &(error));
  149. /*Set the solver */
  150. solver = param.solver->answer;
  151. if (strcmp(solver, G_MATH_SOLVER_DIRECT_CHOLESKY) == 0 && !param.full_les->answer)
  152. G_fatal_error(_("The cholesky solver dos not work with sparse matrices.\n"
  153. "You may choose a full filled quadratic matrix, flag -f. "));
  154. /*Set the defaults */
  155. G3d_initDefaults();
  156. /*get the current region */
  157. G3d_getWindow(&region);
  158. /*allocate the geometry structure for geometry and area calculation */
  159. geom = N_init_geom_data_3d(&region, geom);
  160. /*Set the function callback to the groundwater flow function */
  161. call = N_alloc_les_callback_3d();
  162. N_set_les_callback_3d_func(call, (*N_callback_gwflow_3d)); /*gwflow 3d */
  163. /*Allocate the groundwater flow data structure */
  164. data = N_alloc_gwflow_data3d(geom->cols, geom->rows, geom->depths, 0, 0);
  165. /*Set the calculation time */
  166. sscanf(param.dt->answer, "%lf", &(data->dt));
  167. /*read the g3d maps into the memory and convert the null values */
  168. N_read_rast3d_to_array_3d(param.phead->answer, data->phead,
  169. param.mask->answer);
  170. N_convert_array_3d_null_to_zero(data->phead);
  171. N_read_rast3d_to_array_3d(param.phead->answer, data->phead_start,
  172. param.mask->answer);
  173. N_convert_array_3d_null_to_zero(data->phead_start);
  174. N_read_rast3d_to_array_3d(param.status->answer, data->status,
  175. param.mask->answer);
  176. N_convert_array_3d_null_to_zero(data->status);
  177. N_read_rast3d_to_array_3d(param.hc_x->answer, data->hc_x,
  178. param.mask->answer);
  179. N_convert_array_3d_null_to_zero(data->hc_x);
  180. N_read_rast3d_to_array_3d(param.hc_y->answer, data->hc_y,
  181. param.mask->answer);
  182. N_convert_array_3d_null_to_zero(data->hc_y);
  183. N_read_rast3d_to_array_3d(param.hc_z->answer, data->hc_z,
  184. param.mask->answer);
  185. N_convert_array_3d_null_to_zero(data->hc_z);
  186. N_read_rast3d_to_array_3d(param.q->answer, data->q, param.mask->answer);
  187. N_convert_array_3d_null_to_zero(data->q);
  188. N_read_rast3d_to_array_3d(param.s->answer, data->s, param.mask->answer);
  189. N_convert_array_3d_null_to_zero(data->s);
  190. /* Set the inactive values to zero, to assure a no flow boundary */
  191. for (z = 0; z < geom->depths; z++) {
  192. for (y = 0; y < geom->rows; y++) {
  193. for (x = 0; x < geom->cols; x++) {
  194. stat = (int)N_get_array_3d_d_value(data->status, x, y, z);
  195. if (stat == N_CELL_INACTIVE) { /*only inactive cells */
  196. N_put_array_3d_d_value(data->hc_x, x, y, z, 0);
  197. N_put_array_3d_d_value(data->hc_y, x, y, z, 0);
  198. N_put_array_3d_d_value(data->hc_z, x, y, z, 0);
  199. N_put_array_3d_d_value(data->s, x, y, z, 0);
  200. N_put_array_3d_d_value(data->q, x, y, z, 0);
  201. }
  202. }
  203. }
  204. }
  205. /*assemble the linear equation system */
  206. if (!param.full_les->answer) {
  207. les =
  208. N_assemble_les_3d(N_SPARSE_LES, geom, data->status, data->phead,
  209. (void *)data, call);
  210. }
  211. else {
  212. les =
  213. N_assemble_les_3d(N_NORMAL_LES, geom, data->status, data->phead,
  214. (void *)data, call);
  215. }
  216. if (les && les->type == N_NORMAL_LES) {
  217. if (strcmp(solver, G_MATH_SOLVER_ITERATIVE_CG) == 0)
  218. G_math_solver_cg(les->A, les->x, les->b, les->rows, maxit, error);
  219. if (strcmp(solver, G_MATH_SOLVER_ITERATIVE_PCG) == 0)
  220. G_math_solver_pcg(les->A, les->x, les->b, les->rows, maxit, error,
  221. G_MATH_DIAGONAL_PRECONDITION);
  222. if (strcmp(solver, G_MATH_SOLVER_DIRECT_CHOLESKY) == 0)
  223. G_math_solver_cholesky(les->A, les->x, les->b, les->rows,
  224. les->rows);
  225. }
  226. else if (les && les->type == N_SPARSE_LES) {
  227. if (strcmp(solver, G_MATH_SOLVER_ITERATIVE_CG) == 0)
  228. G_math_solver_sparse_cg(les->Asp, les->x, les->b, les->rows,
  229. maxit, error);
  230. if (strcmp(solver, G_MATH_SOLVER_ITERATIVE_PCG) == 0)
  231. G_math_solver_sparse_pcg(les->Asp, les->x, les->b, les->rows,
  232. maxit, error, G_MATH_DIAGONAL_PRECONDITION);
  233. }
  234. if (les == NULL)
  235. G_fatal_error(_("Unable to create and solve the linear equation system"));
  236. /*write the result to the output file and copy the values to the data->phead array */
  237. write_result(data->status, data->phead_start, data->phead, les->x,
  238. &region, param.output->answer);
  239. N_free_les(les);
  240. /* Compute the water budget for each cell */
  241. N_array_3d *budget = N_alloc_array_3d(geom->cols, geom->rows, geom->depths, 1, DCELL_TYPE);
  242. N_gwflow_3d_calc_water_budget(data, geom, budget);
  243. /*Write the water balance */
  244. if(param.budget->answer)
  245. {
  246. N_write_array_3d_to_rast3d(budget, param.budget->answer, 1);
  247. }
  248. /*Compute the the velocity field if required and write the result into three rast3d maps */
  249. if (param.vector_x->answer || param.vector_y->answer || param.vector_z->answer) {
  250. field =
  251. N_compute_gradient_field_3d(data->phead, data->hc_x, data->hc_y,
  252. data->hc_z, geom, NULL);
  253. xcomp =
  254. N_alloc_array_3d(geom->cols, geom->rows, geom->depths, 1,
  255. DCELL_TYPE);
  256. ycomp =
  257. N_alloc_array_3d(geom->cols, geom->rows, geom->depths, 1,
  258. DCELL_TYPE);
  259. zcomp =
  260. N_alloc_array_3d(geom->cols, geom->rows, geom->depths, 1,
  261. DCELL_TYPE);
  262. N_compute_gradient_field_components_3d(field, xcomp, ycomp, zcomp);
  263. if(param.vector_x->answer)
  264. N_write_array_3d_to_rast3d(xcomp, param.vector_x->answer, 1);
  265. if(param.vector_y->answer)
  266. N_write_array_3d_to_rast3d(ycomp, param.vector_y->answer, 1);
  267. if(param.vector_z->answer)
  268. N_write_array_3d_to_rast3d(zcomp, param.vector_z->answer, 1);
  269. if (xcomp)
  270. N_free_array_3d(xcomp);
  271. if (ycomp)
  272. N_free_array_3d(ycomp);
  273. if (zcomp)
  274. N_free_array_3d(zcomp);
  275. if (field)
  276. N_free_gradient_field_3d(field);
  277. }
  278. if (data)
  279. N_free_gwflow_data3d(data);
  280. if (geom)
  281. N_free_geom_data(geom);
  282. if (call)
  283. G_free(call);
  284. return (EXIT_SUCCESS);
  285. }
  286. /* ************************************************************************* */
  287. /* this function writes the result from the x vector to a g3d map ********** */
  288. /* ************************************************************************* */
  289. void
  290. write_result(N_array_3d * status, N_array_3d * phead_start,
  291. N_array_3d * phead, double *result, G3D_Region * region,
  292. char *name)
  293. {
  294. void *map = NULL;
  295. int changemask = 0;
  296. int z, y, x, rows, cols, depths, count, stat;
  297. double d1 = 0;
  298. rows = region->rows;
  299. cols = region->cols;
  300. depths = region->depths;
  301. /*Open the new map */
  302. map = G3d_openCellNew(name, DCELL_TYPE, G3D_USE_CACHE_DEFAULT, region);
  303. if (map == NULL)
  304. G3d_fatalError(_("Error opening g3d map <%s>"), name);
  305. G_message(_("Write the result to g3d map <%s>"), name);
  306. /*if requested set the Mask on */
  307. if (param.mask->answer) {
  308. if (G3d_maskFileExists()) {
  309. changemask = 0;
  310. if (G3d_maskIsOff(map)) {
  311. G3d_maskOn(map);
  312. changemask = 1;
  313. }
  314. }
  315. }
  316. count = 0;
  317. for (z = 0; z < depths; z++) {
  318. G_percent(z, depths - 1, 10);
  319. for (y = 0; y < rows; y++) {
  320. for (x = 0; x < cols; x++) {
  321. stat = (int)N_get_array_3d_d_value(status, x, y, z);
  322. if (stat == N_CELL_ACTIVE) { /*only active cells */
  323. d1 = result[count];
  324. /*copy the values */
  325. N_put_array_3d_d_value(phead, x, y, z, d1);
  326. count++;
  327. }
  328. else if (stat == N_CELL_DIRICHLET) { /*dirichlet cells */
  329. d1 = N_get_array_3d_d_value(phead_start, x, y, z);
  330. }
  331. else {
  332. G3d_setNullValue(&d1, 1, DCELL_TYPE);
  333. }
  334. G3d_putDouble(map, x, y, z, d1);
  335. }
  336. }
  337. }
  338. /*We set the Mask off, if it was off before */
  339. if (param.mask->answer) {
  340. if (G3d_maskFileExists())
  341. if (G3d_maskIsOn(map) && changemask)
  342. G3d_maskOff(map);
  343. }
  344. if (!G3d_closeCell(map))
  345. G3d_fatalError(map, NULL, 0, _("Error closing g3d file"));
  346. return;
  347. }