main.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /****************************************************************************
  2. *
  3. * MODULE: r.solute.transport
  4. *
  5. * AUTHOR(S): Original author
  6. * Soeren Gebbert soerengebbert <at> gmx <dot> de
  7. * 27 11 2006 Berlin
  8. * PURPOSE: Calculates transient two dimensional solute transport
  9. * in porous media
  10. *
  11. * COPYRIGHT: (C) 2006-2009 by Soeren Gebbert, and the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include <grass/gis.h>
  23. #include <grass/raster.h>
  24. #include <grass/glocale.h>
  25. #include <grass/gmath.h>
  26. #include <grass/N_pde.h>
  27. #include <grass/N_solute_transport.h>
  28. /*- Parameters and global variables -----------------------------------------*/
  29. typedef struct
  30. {
  31. struct Option *output, *phead, *hc_x, *hc_y,
  32. *c, *status, *diff_x, *diff_y, *q, *cs, *r, *top, *nf, *cin,
  33. *bottom, *vector_x, *vector_y, *type, *dt, *maxit, *error, *solver, *sor,
  34. *al, *at, *loops, *stab;
  35. struct Flag *full_les;
  36. struct Flag *cfl;
  37. } paramType;
  38. paramType param; /*Parameters */
  39. /*- prototypes --------------------------------------------------------------*/
  40. void set_params(); /*Fill the paramType structure */
  41. void copy_result(N_array_2d * status, N_array_2d * c_start, double *result,
  42. struct Cell_head *region, N_array_2d * target, int tflag);
  43. N_les *create_solve_les(N_geom_data * geom, N_solute_transport_data2d * data,
  44. N_les_callback_2d * call, const char *solver, int maxit,
  45. double error, double sor);
  46. /* ************************************************************************* */
  47. /* Set up the arguments we are expecting ********************************** */
  48. /* ************************************************************************* */
  49. void set_params()
  50. {
  51. param.c = G_define_standard_option(G_OPT_R_INPUT);
  52. param.c->key = "c";
  53. param.c->description = _("The initial concentration in [kg/m^3]");
  54. param.phead = G_define_standard_option(G_OPT_R_INPUT);
  55. param.phead->key = "phead";
  56. param.phead->description = _("The piezometric head in [m]");
  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. _("The 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. _("The y-part of the hydraulic conductivity tensor in [m/s]");
  65. param.status = G_define_standard_option(G_OPT_R_INPUT);
  66. param.status->key = "status";
  67. param.status->description =
  68. _("The status for each cell, = 0 - inactive cell, 1 - active cell, "
  69. "2 - dirichlet- and 3 - transfer boundary condition");
  70. param.diff_x = G_define_standard_option(G_OPT_R_INPUT);
  71. param.diff_x->key = "diff_x";
  72. param.diff_x->description =
  73. _("The x-part of the diffusion tensor in [m^2/s]");
  74. param.diff_y = G_define_standard_option(G_OPT_R_INPUT);
  75. param.diff_y->key = "diff_y";
  76. param.diff_y->description =
  77. _("The y-part of the diffusion tensor in [m^2/s]");
  78. param.q = G_define_standard_option(G_OPT_R_INPUT);
  79. param.q->key = "q";
  80. param.q->guisection = _("Water flow");
  81. param.q->required = NO;
  82. param.q->description = _("Groundwater sources and sinks in [m^3/s]");
  83. param.cin = G_define_standard_option(G_OPT_R_INPUT);
  84. param.cin->key = "cin";
  85. param.cin->required = NO;
  86. param.cin->gisprompt = "old,raster,raster";
  87. param.cin->guisection = _("Water flow");
  88. param.cin->description = _("Concentration sources and sinks bounded to a "
  89. "water source or sink in [kg/s]");
  90. param.cs = G_define_standard_option(G_OPT_R_INPUT);
  91. param.cs->key = "cs";
  92. param.cs->type = TYPE_STRING;
  93. param.cs->required = YES;
  94. param.cs->gisprompt = "old,raster,raster";
  95. param.cs->description = _("Concentration of inner sources and inner sinks in [kg/s] "
  96. "(i.e. a chemical reaction)");
  97. param.r = G_define_standard_option(G_OPT_R_INPUT);
  98. param.r->key = "rd";
  99. param.r->description = _("Retardation factor [-]");
  100. param.nf = G_define_standard_option(G_OPT_R_INPUT);
  101. param.nf->key = "nf";
  102. param.nf->description = _("Effective porosity [-]");
  103. param.top = G_define_standard_option(G_OPT_R_INPUT);
  104. param.top->key = "top";
  105. param.top->description = _("Top surface of the aquifer in [m]");
  106. param.bottom = G_define_standard_option(G_OPT_R_INPUT);
  107. param.bottom->key = "bottom";
  108. param.bottom->description = _("Bottom surface of the aquifer in [m]");
  109. param.output = G_define_standard_option(G_OPT_R_OUTPUT);
  110. param.output->description = _("The resulting concentration of the numerical solute "
  111. "transport calculation will be written to this map. [kg/m^3]");
  112. param.vector_x = G_define_standard_option(G_OPT_R_OUTPUT);
  113. param.vector_x->key = "vx";
  114. param.vector_x->required = NO;
  115. param.vector_x->guisection = _("Water flow");
  116. param.vector_x->description =
  117. _("Calculate and store the groundwater filter velocity vector part in x direction [m/s]\n");
  118. param.vector_y = G_define_standard_option(G_OPT_R_OUTPUT);
  119. param.vector_y->key = "vy";
  120. param.vector_y->required = NO;
  121. param.vector_y->guisection = _("Water flow");
  122. param.vector_y->description =
  123. _("Calculate and store the groundwater filter velocity vector part in y direction [m/s]\n");
  124. param.dt = N_define_standard_option(N_OPT_CALC_TIME);
  125. param.maxit = N_define_standard_option(N_OPT_MAX_ITERATIONS);
  126. param.error = N_define_standard_option(N_OPT_ITERATION_ERROR);
  127. param.solver = N_define_standard_option(N_OPT_SOLVER_UNSYMM);
  128. param.sor = N_define_standard_option(N_OPT_SOR_VALUE);
  129. param.al = G_define_option();
  130. param.al->key = "al";
  131. param.al->type = TYPE_DOUBLE;
  132. param.al->required = NO;
  133. param.al->answer = "0.0";
  134. param.al->description =
  135. _("The longditudinal dispersivity length. [m]");
  136. param.at = G_define_option();
  137. param.at->key = "at";
  138. param.at->type = TYPE_DOUBLE;
  139. param.at->required = NO;
  140. param.at->answer = "0.0";
  141. param.at->description =
  142. _("The transversal dispersivity length. [m]");
  143. param.loops = G_define_option();
  144. param.loops->key = "loops";
  145. param.loops->type = TYPE_DOUBLE;
  146. param.loops->required = NO;
  147. param.loops->answer = "1";
  148. param.loops->description =
  149. _("Use this number of time loops if the CFL flag is off. The timestep will become dt/loops.");
  150. param.stab = G_define_option();
  151. param.stab->key = "stab";
  152. param.stab->type = TYPE_STRING;
  153. param.stab->required = NO;
  154. param.stab->answer = "full";
  155. param.stab->options = "full,exp";
  156. param.stab->guisection = "Stabelization";
  157. param.stab->description =
  158. _("Set the flow stabilizing scheme (full or exponential upwinding).");
  159. param.full_les = G_define_flag();
  160. param.full_les->key = 'f';
  161. param.full_les->guisection = "Solver";
  162. param.full_les->description = _("Use a full filled quadratic linear equation system,"
  163. " default is a sparse linear equation system.");
  164. param.cfl = G_define_flag();
  165. param.cfl->key = 'c';
  166. param.cfl->guisection = "Stabelization";
  167. param.cfl->description =
  168. _("Use the Courant-Friedrichs-Lewy criteria for time step calculation");
  169. }
  170. /* ************************************************************************* */
  171. /* Main function *********************************************************** */
  172. /* ************************************************************************* */
  173. int main(int argc, char *argv[])
  174. {
  175. struct GModule *module = NULL;
  176. N_solute_transport_data2d *data = NULL;
  177. N_geom_data *geom = NULL;
  178. N_les *les = NULL;
  179. N_les_callback_2d *call = NULL;
  180. struct Cell_head region;
  181. double error, sor;
  182. char *solver;
  183. int x, y, stat, i, maxit = 1;
  184. double loops = 1;
  185. N_array_2d *xcomp = NULL;
  186. N_array_2d *ycomp = NULL;
  187. N_array_2d *hc_x = NULL;
  188. N_array_2d *hc_y = NULL;
  189. N_array_2d *phead = NULL;
  190. double time_step, cfl, length, time_loops, time_sum;
  191. /* Initialize GRASS */
  192. G_gisinit(argv[0]);
  193. module = G_define_module();
  194. G_add_keyword(_("raster"));
  195. G_add_keyword(_("hydrology"));
  196. G_add_keyword(_("solute transport"));
  197. module->description =
  198. _("Numerical calculation program for transient, confined and unconfined "
  199. "solute transport in two dimensions");
  200. /* Get parameters from user */
  201. set_params();
  202. if (G_parser(argc, argv))
  203. exit(EXIT_FAILURE);
  204. /* Make sure that the current projection is not lat/long */
  205. if ((G_projection() == PROJECTION_LL))
  206. G_fatal_error(_("Lat/Long location is not supported by %s. Please reproject map first."),
  207. G_program_name());
  208. /*Set the maximum iterations */
  209. sscanf(param.maxit->answer, "%i", &(maxit));
  210. /*Set the calculation error break criteria */
  211. sscanf(param.error->answer, "%lf", &(error));
  212. sscanf(param.sor->answer, "%lf", &(sor));
  213. /*number of loops*/
  214. sscanf(param.loops->answer, "%lf", &(loops));
  215. /*Set the solver */
  216. solver = param.solver->answer;
  217. if (strcmp(solver, G_MATH_SOLVER_DIRECT_LU) == 0 && !param.full_les->answer)
  218. G_fatal_error(_("The direct LU solver do not work with sparse matrices"));
  219. if (strcmp(solver, G_MATH_SOLVER_DIRECT_GAUSS) == 0 && !param.full_les->answer)
  220. G_fatal_error(_("The direct Gauss solver do not work with sparse matrices"));
  221. /*get the current region */
  222. G_get_set_window(&region);
  223. /*allocate the geometry structure for geometry and area calculation */
  224. geom = N_init_geom_data_2d(&region, geom);
  225. /*Set the function callback to the groundwater flow function */
  226. call = N_alloc_les_callback_2d();
  227. N_set_les_callback_2d_func(call, (*N_callback_solute_transport_2d)); /*solute_transport 2d */
  228. /*Allocate the groundwater flow data structure */
  229. data = N_alloc_solute_transport_data2d(geom->cols, geom->rows);
  230. /*Set the stabilizing scheme*/
  231. if (strncmp("full", param.stab->answer, 4) == 0) {
  232. data->stab = N_UPWIND_FULL;
  233. }
  234. if (strncmp("exp", param.stab->answer, 3) == 0) {
  235. data->stab = N_UPWIND_EXP;
  236. }
  237. /*the dispersivity lengths*/
  238. sscanf(param.al->answer, "%lf", &(data->al));
  239. sscanf(param.at->answer, "%lf", &(data->at));
  240. /*Set the calculation time */
  241. sscanf(param.dt->answer, "%lf", &(data->dt));
  242. /*read all input maps into the memory and take care of the
  243. * null values.*/
  244. N_read_rast_to_array_2d(param.c->answer, data->c);
  245. N_convert_array_2d_null_to_zero(data->c);
  246. N_read_rast_to_array_2d(param.c->answer, data->c_start);
  247. N_convert_array_2d_null_to_zero(data->c_start);
  248. N_read_rast_to_array_2d(param.status->answer, data->status);
  249. N_convert_array_2d_null_to_zero(data->status);
  250. N_read_rast_to_array_2d(param.diff_x->answer, data->diff_x);
  251. N_convert_array_2d_null_to_zero(data->diff_x);
  252. N_read_rast_to_array_2d(param.diff_y->answer, data->diff_y);
  253. N_convert_array_2d_null_to_zero(data->diff_y);
  254. N_read_rast_to_array_2d(param.q->answer, data->q);
  255. N_convert_array_2d_null_to_zero(data->q);
  256. N_read_rast_to_array_2d(param.nf->answer, data->nf);
  257. N_convert_array_2d_null_to_zero(data->nf);
  258. N_read_rast_to_array_2d(param.cs->answer, data->cs);
  259. N_convert_array_2d_null_to_zero(data->cs);
  260. N_read_rast_to_array_2d(param.top->answer, data->top);
  261. N_convert_array_2d_null_to_zero(data->top);
  262. N_read_rast_to_array_2d(param.bottom->answer, data->bottom);
  263. N_convert_array_2d_null_to_zero(data->bottom);
  264. N_read_rast_to_array_2d(param.r->answer, data->R);
  265. N_convert_array_2d_null_to_zero(data->R);
  266. if(param.cin->answer) {
  267. N_read_rast_to_array_2d(param.cin->answer, data->cin);
  268. N_convert_array_2d_null_to_zero(data->cin);
  269. }
  270. /*initiate the values for velocity calculation*/
  271. hc_x = N_alloc_array_2d(geom->cols, geom->rows, 1, DCELL_TYPE);
  272. hc_x = N_read_rast_to_array_2d(param.hc_x->answer, hc_x);
  273. N_convert_array_2d_null_to_zero(hc_x);
  274. hc_y = N_alloc_array_2d(geom->cols, geom->rows, 1, DCELL_TYPE);
  275. hc_y = N_read_rast_to_array_2d(param.hc_y->answer, hc_y);
  276. N_convert_array_2d_null_to_zero(hc_y);
  277. phead = N_alloc_array_2d(geom->cols, geom->rows, 1, DCELL_TYPE);
  278. phead = N_read_rast_to_array_2d(param.phead->answer, phead);
  279. N_convert_array_2d_null_to_zero(phead);
  280. /* Set the inactive values to zero, to assure a no flow boundary */
  281. for (y = 0; y < geom->rows; y++) {
  282. for (x = 0; x < geom->cols; x++) {
  283. stat = (int)N_get_array_2d_d_value(data->status, x, y);
  284. if (stat == N_CELL_INACTIVE) { /*only inactive cells */
  285. N_put_array_2d_d_value(data->diff_x, x, y, 0);
  286. N_put_array_2d_d_value(data->diff_y, x, y, 0);
  287. N_put_array_2d_d_value(data->cs, x, y, 0);
  288. N_put_array_2d_d_value(data->q, x, y, 0);
  289. }
  290. }
  291. }
  292. /*compute the velocities */
  293. N_math_array_2d(hc_x, data->nf, hc_x, N_ARRAY_DIV);
  294. N_math_array_2d(hc_y, data->nf, hc_y, N_ARRAY_DIV);
  295. N_compute_gradient_field_2d(phead, hc_x, hc_y, geom, data->grad);
  296. /*Now compute the dispersivity tensor*/
  297. N_calc_solute_transport_disptensor_2d(data);
  298. /***************************************/
  299. /*the Courant-Friedrichs-Lewy criteria */
  300. /*Compute the correct time step */
  301. if (geom->dx > geom->dy)
  302. length = geom->dx;
  303. else
  304. length = geom->dy;
  305. if (fabs(data->grad->max) > fabs(data->grad->min)) {
  306. cfl = (double)data->dt * fabs(data->grad->max) / length;
  307. time_step = 1*length / fabs(data->grad->max);
  308. }
  309. else {
  310. cfl = (double)data->dt * fabs(data->grad->min) / length;
  311. time_step = 1*length / fabs(data->grad->min);
  312. }
  313. G_message(_("The Courant-Friedrichs-Lewy criteria is %g it should be within [0:1]"), cfl);
  314. G_message(_("The largest stable time step is %g"), time_step);
  315. /*Set the number of inner loops and the time step*/
  316. if (data->dt > time_step && param.cfl->answer) {
  317. /*safe the user time step */
  318. time_sum = data->dt;
  319. time_loops = data->dt / time_step;
  320. time_loops = floor(time_loops) + 1;
  321. data->dt = data->dt / time_loops;
  322. G_message(_("Number of inner loops is %g"), time_loops);
  323. G_message(_("Time step for each loop %g"), data->dt);
  324. }
  325. else {
  326. if(data->dt > time_step)
  327. G_warning(_("The time step is to large: %gs. The largest time step should be of size %gs."), data->dt, time_step);
  328. time_loops = loops;
  329. data->dt = data->dt / loops;
  330. }
  331. N_free_array_2d(phead);
  332. N_free_array_2d(hc_x);
  333. N_free_array_2d(hc_y);
  334. /*Compute for each time step*/
  335. for (i = 0; i < time_loops; i++) {
  336. G_message(_("Time step %i with time sum %g"), i + 1, (i + 1)*data->dt);
  337. /*assemble the linear equation system and solve it */
  338. les = create_solve_les(geom, data, call, solver, maxit, error, sor);
  339. /* copy the result into the c array for output */
  340. copy_result(data->status, data->c_start, les->x, &region, data->c, 1);
  341. N_convert_array_2d_null_to_zero(data->c_start);
  342. if (les)
  343. N_free_les(les);
  344. /*Set the start array*/
  345. N_copy_array_2d(data->c, data->c_start);
  346. /*Set the transmission boundary*/
  347. N_calc_solute_transport_transmission_2d(data);
  348. }
  349. /*write the result to the output file */
  350. N_write_array_2d_to_rast(data->c, param.output->answer);
  351. /*Compute the the velocity field if required and write the result into three rast maps */
  352. if (param.vector_x->answer || param.vector_y->answer) {
  353. xcomp = N_alloc_array_2d(geom->cols, geom->rows, 1, DCELL_TYPE);
  354. ycomp = N_alloc_array_2d(geom->cols, geom->rows, 1, DCELL_TYPE);
  355. N_compute_gradient_field_components_2d(data->grad, xcomp, ycomp);
  356. if (param.vector_x->answer)
  357. N_write_array_2d_to_rast(xcomp, param.vector_x->answer);
  358. if (param.vector_y->answer)
  359. N_write_array_2d_to_rast(ycomp, param.vector_y->answer);
  360. if (xcomp)
  361. N_free_array_2d(xcomp);
  362. if (ycomp)
  363. N_free_array_2d(ycomp);
  364. }
  365. if (data)
  366. N_free_solute_transport_data2d(data);
  367. if (geom)
  368. N_free_geom_data(geom);
  369. if (call)
  370. G_free(call);
  371. return (EXIT_SUCCESS);
  372. }
  373. /* ************************************************************************* */
  374. /* this function copies the result from the x vector to a N_array_2d array * */
  375. /* ************************************************************************* */
  376. void
  377. copy_result(N_array_2d * status, N_array_2d * c_start, double *result,
  378. struct Cell_head *region, N_array_2d * target, int tflag)
  379. {
  380. int y, x, rows, cols, count, stat;
  381. double d1 = 0;
  382. DCELL val;
  383. rows = region->rows;
  384. cols = region->cols;
  385. count = 0;
  386. for (y = 0; y < rows; y++) {
  387. G_percent(y, rows - 1, 10);
  388. for (x = 0; x < cols; x++) {
  389. stat = (int)N_get_array_2d_d_value(status, x, y);
  390. if (stat == N_CELL_ACTIVE) { /*only active cells */
  391. d1 = result[count];
  392. val = (DCELL) d1;
  393. count++;
  394. }
  395. else if (stat == N_CELL_DIRICHLET) { /*dirichlet cells */
  396. d1 = N_get_array_2d_d_value(c_start, x, y);
  397. val = (DCELL) d1;
  398. }
  399. else if (tflag == 1 && stat == N_CELL_TRANSMISSION) {/*transmission cells*/
  400. d1 = N_get_array_2d_d_value(c_start, x, y);
  401. val = (DCELL) d1;
  402. }
  403. else {
  404. Rast_set_null_value(&val, 1, DCELL_TYPE);
  405. }
  406. N_put_array_2d_d_value(target, x, y, val);
  407. }
  408. }
  409. return;
  410. }
  411. /* *************************************************************** */
  412. /* ***** create and solve the linear equation system ************* */
  413. /* *************************************************************** */
  414. N_les *create_solve_les(N_geom_data * geom, N_solute_transport_data2d * data,
  415. N_les_callback_2d * call, const char *solver, int maxit,
  416. double error, double sor)
  417. {
  418. N_les *les;
  419. /*assemble the linear equation system */
  420. if (param.full_les->answer)
  421. les =
  422. N_assemble_les_2d(N_NORMAL_LES, geom, data->status, data->c,
  423. (void *)data, call);
  424. else
  425. les =
  426. N_assemble_les_2d(N_SPARSE_LES, geom, data->status, data->c,
  427. (void *)data, call);
  428. /*solve the equation system */
  429. if (strcmp(solver, G_MATH_SOLVER_ITERATIVE_JACOBI) == 0)
  430. {
  431. if (!param.full_les->answer)
  432. G_math_solver_sparse_jacobi(les->Asp, les->x, les->b, les->rows, maxit, sor, error);
  433. else
  434. G_math_solver_jacobi(les->A, les->x, les->b, les->rows, maxit, sor, error);
  435. }
  436. if (strcmp(solver, G_MATH_SOLVER_ITERATIVE_SOR) == 0)
  437. {
  438. if (!param.full_les->answer)
  439. G_math_solver_sparse_gs(les->Asp, les->x, les->b, les->rows, maxit, sor, error);
  440. else
  441. G_math_solver_gs(les->A, les->x, les->b, les->rows, maxit, sor, error);
  442. }
  443. if (strcmp(solver, G_MATH_SOLVER_ITERATIVE_BICGSTAB) == 0)
  444. {
  445. if (!param.full_les->answer)
  446. G_math_solver_sparse_bicgstab(les->Asp, les->x, les->b, les->rows, maxit, error);
  447. else
  448. G_math_solver_bicgstab(les->A, les->x, les->b, les->rows, maxit, error);
  449. }
  450. if (strcmp(solver, G_MATH_SOLVER_DIRECT_LU) == 0)
  451. G_math_solver_lu(les->A, les->x, les->b, les->rows);
  452. if (strcmp(solver, G_MATH_SOLVER_DIRECT_GAUSS) == 0)
  453. G_math_solver_gauss(les->A, les->x, les->b, les->rows);
  454. if (les == NULL)
  455. G_fatal_error(_("Could not create and solve the linear equation system"));
  456. return les;
  457. }