gpdelib.dox 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /*! \page gpdelib GRASS Partial differential equations Library (GPDE)
  2. <h2>GRASS Partial Differential Equations Library (GPDE)</h2>
  3. <P>
  4. Author: Soeren Gebbert
  5. <b>Overview</b>
  6. The GRASS partial differential equation library is designed to support the
  7. solution of PDE's within GRASS.
  8. Therefore it provides functions to create and solve linear equation systems.
  9. The library design is thread safe and supports threaded parallelism with OpenMP.
  10. Most of the available solvers (expect the gauss seidel and jacobi solver)
  11. and the assembling of the linear equation systems are parallelized with OpenMP.
  12. The creation of a linear equation system can be done by using quadratic or sparse matrices.
  13. Sparse and quadratic matrices are supported by the iterative equation solvers,
  14. the direct equation solvers only support regular quadratic matrices.
  15. <p>
  16. To enable parallel computing, you will need a compiler with OpenMP capabilities
  17. and a computer with at least two cores or cpu's.
  18. Most proprietary compilers support OpenMP.
  19. The free gnu-C compiler supports OpenMP since version 4.2.
  20. More information about OpenMP are available at http://www.openmp.org .
  21. <br>
  22. <br>
  23. Based on the finite volume discretization for structured grids,
  24. groundwater flow and solute transport in 2d and 3d are implemented.
  25. It is IMHO easy to add any partial differential equation which can be
  26. solved with the finite volume or the finite differences method based on structured grids.
  27. The basis for the discretisation is the geometrical structure of raster and volume maps.
  28. <br>
  29. <br>
  30. There are plans to implement heat flow in two and three dimensions.
  31. <br>
  32. <br>
  33. Currently only planimetric projections are supported for numerical calculations.
  34. <br>
  35. <br>
  36. The gpde library provides an easy to handle interface for reading raster and volume data into
  37. specific data arrays (in memory) and for writing data arrays to raster and volume maps.
  38. These data arrays supports all raster and volume datatypes, overlapping boundaries
  39. and provide a sophisticated null value support and array access functions.
  40. Basic mathematical operations are available for the arrays.
  41. \section PDE The Creation of a lineare equation systems
  42. The principle to create and solve a linear equation systems with this library:
  43. \verbatim
  44. 1.) Read all needed raster/volume data into the memory
  45. - use the the two and three dimensional data arrays N_array_2d and N_array_3d
  46. to manage the data
  47. 2.) Assemble the linear equation system
  48. - use the sparse matrix structure to save lots of memory and cpu time
  49. 3.) Solve the linear equation system with the gauss, lu, jacobi, sor, cg or bicgstab method
  50. - always prefer the iterative krylov-space methods for solving
  51. - if the linear equation systems are in a bad condition try the jacobian solver
  52. - the available direct solvers don't support sparse matrices
  53. 4.) Write the result back as raster/volume map
  54. \endverbatim
  55. The following code shows how to implement this principle with the GRASS PDE lib:
  56. \verbatim
  57. /* *************************************************************** */
  58. /* ****** calculate 2d dimensional groundwater flow ************** */
  59. /* *************************************************************** */
  60. int main()
  61. {
  62. int i, j;
  63. N_gwflow_data2d *data = NULL; /* data structure with the groundwater data */
  64. N_geom_data *geom = NULL; /* geometry of the calculation area (region) */
  65. N_les *les = NULL; /* the linear equation system structure */
  66. N_les_callback_2d *call = NULL; /* the callback for the groundwater flow calculation */
  67. /* allocate the callback structure */
  68. call = N_alloc_les_callback_2d();
  69. /* set the callback to groundwater flow calculation */
  70. N_set_les_callback_2d_func(call, (*N_callback_gwflow_2d));
  71. /* allocate the groundwater data structure with one million cells */
  72. data = N_alloc_gwflow_data2d(1000, 1000);
  73. /* get the current region */
  74. G_get_set_window(&region);
  75. /* allocate and initiate the geometry structure for geometry and area calculation
  76. needed by the groundwater calculation callback */
  77. geom = N_init_geom_data_2d(&region, geom);
  78. /*fill the data arrays with raster maps*/
  79. N_read_rast_to_array_2d("phead_map_name", data->phead);
  80. N_read_rast_to_array_2d("phead_map_name", data->phead_start);
  81. N_read_rast_to_array_2d("status_map_name", data->status);
  82. N_read_rast_to_array_2d("hc_x_map_name", data->hc_x);
  83. N_read_rast_to_array_2d("hc_y_map_name", data->hc_y);
  84. N_read_rast_to_array_2d("q_map_name", data->q);
  85. N_read_rast_to_array_2d("s_map_name", data->s);
  86. N_read_rast_to_array_2d("top_map_name", data->top);
  87. N_read_rast_to_array_2d("bottom_map_name", data->bottom);
  88. N_read_rast_to_array_2d("r_map_name", data->r);
  89. /* Set the inactive cells to zero, to assure a no flow boundary */
  90. /* notice: the data arrays are of type DCELL, so the put_*_d_value functions are used */
  91. for (y = 0; y < geom->rows; y++) {
  92. for (x = 0; x < geom->cols; x++) {
  93. stat = (int)N_get_array_2d_d_value(data->status, x, y);
  94. if (stat == N_CELL_INACTIVE) { /*only inactive cells */
  95. N_put_array_2d_d_value(data->hc_x, x, y, 0);
  96. N_put_array_2d_d_value(data->hc_y, x, y, 0);
  97. N_put_array_2d_d_value(data->s, x, y, 0);
  98. N_put_array_2d_d_value(data->q, x, y, 0);
  99. }
  100. }
  101. }
  102. /*Assemble the sparse matrix */
  103. les = N_assemble_les_2d(N_SPARSE_LES, geom, data->status, data->phead_start, (void *)data, call);
  104. /*Solve the linear equation system with the cg method*/
  105. N_solver_cg(les, 100000, 0.1e-8);
  106. /* now copy the result from the x vector of the linear equation system
  107. into a data array, this function is not provided by the gpde lib,
  108. you have to write your own: take a look at r.gwflow
  109. */
  110. copy_x_vect_to_data_array(les->x, data->phead, geom);
  111. /*write the x vector of the les back into a raster map*/
  112. N_write_array_2d_to_rast(data->phead, "output_map_name");
  113. /*free the memory*/
  114. N_free_les(les);
  115. N_free_gwflow_data2d(data);
  116. N_free_geom_data(geom);
  117. G_free(call);
  118. return 0;
  119. }
  120. \endverbatim
  121. The assembling of lineare equation systems is based on a 5 point star for raster maps and a
  122. 7 point star for volume maps, implemented as finite volume/differences discretization.
  123. <p>
  124. All raster and volume maps which are used to create a linear equation system
  125. must be loaded completely into the memory.<br>
  126. Therefore 2d and 3d data structures are implemented to support this principle:
  127. \subsection rastermaps Raster maps
  128. \verbatim
  129. This structure manages two dimensional data
  130. typedef struct
  131. {
  132. int type; /* which raster type CELL_TYPE, FCELL_TYPE, DCELL_TYPE */
  133. int rows, cols;
  134. int rows_intern, cols_intern;
  135. int offset; /*number of cols/rows offset at each boundary */
  136. CELL *cell_array;
  137. FCELL *fcell_array;
  138. DCELL *dcell_array;
  139. } N_array_2d;
  140. \endverbatim
  141. For performance reasons the data arrays are stored as a one dimensional array internally.<br>
  142. Use the following functions for memory allocation and value handling:
  143. <p>
  144. Memory allocation<br>
  145. N_array_2d *#N_alloc_array_2d(int rows, int cols, int offset, int type);
  146. <p>
  147. void #N_free_array_2d(N_array_2d * data_array);
  148. <p>
  149. Get the type of the array<br>
  150. int #N_get_array_2d_type(N_array_2d * array2d);
  151. <p>
  152. To access the 2d arrays use the following functions for reading and writing of data values
  153. <p>
  154. void #N_get_array_2d_value(N_array_2d * array2d, int row, int col, void *value);
  155. <p>
  156. CELL #N_get_array_2d_c_value(N_array_2d * array2d, int row, int col);
  157. <p>
  158. FCELL #N_get_array_2d_f_value(N_array_2d * array2d, int row, int col);
  159. <p>
  160. DCELL #N_get_array_2d_d_value(N_array_2d * array2d, int row, int col);
  161. <p>
  162. void #N_put_array_2d_value(N_array_2d * array2d, int row, int col, char *value);
  163. <p>
  164. void #N_put_array_2d_c_value(N_array_2d * array2d, int row, int col, CELL value);
  165. <p>
  166. void #N_put_array_2d_f_value(N_array_2d * array2d, int row, int col, FCELL value);
  167. <p>
  168. void #N_put_array_2d_d_value(N_array_2d * array2d, int row, int col, DCELL value);
  169. <p>
  170. \subsubsection highlevel Higher level array functions
  171. To copy one array to another use this function<br>
  172. void #N_copy_array_2d(N_array_2d * source, N_array_2d * target);
  173. <p>
  174. Print the content of the array to stdout<br>
  175. void #N_print_array_2d (N_array_2d * data);
  176. <p>
  177. Compute the norm of two arrays<br>
  178. double #N_norm_array_2d (N_array_2d * array1, N_array_2d * array2, int type);
  179. <p>
  180. Perform some basic mathematical operations with two arrays<br>
  181. N_array_2d * #N_math_array_2d (N_array_2d * array1, N_array_2d * array2, N_array_2d * result, int type);
  182. <p>
  183. Convert all null values to zero<br>
  184. int #N_convert_array_2d_null_to_zero (N_array_2d * a);
  185. <p>
  186. Read a raster map into the memory<br>
  187. N_array_2d * #N_read_rast_to_array_2d (char *name, N_array_2d * array);
  188. <p>
  189. Write a raster map to the disk<br>
  190. void #N_write_array_2d_to_rast (N_array_2d * array, char *name);
  191. <P>
  192. <b>Example implementation:</b><br>
  193. The GRASS module <a href="http://grass.osgeo.org/grass72/manuals/r.gwflow.html">r.gwflow</a>
  194. implements numerical calculation program for transient,
  195. confined and unconfined groundwater flow in two dimensions.
  196. \subsection g3dmaps Volume maps
  197. \verbatim
  198. typedef struct
  199. {
  200. This structure manages three dimensional data
  201. int type; /* which raster type FCELL_TYPE, DCELL_TYPE */
  202. int rows, cols, depths;
  203. int rows_intern, cols_intern, depths_intern;
  204. int offset; /*number of cols/rows/depths offset at each boundary */
  205. float *fcell_array;
  206. double *dcell_array;
  207. } N_array_3d;
  208. \endverbatim
  209. For performance reasons the data arrays are stored as a one dimensional array internally.
  210. <br><br>
  211. The following functions should be used for memory allocation and value handling:
  212. <p>
  213. N_array_3d *#N_alloc_array_3d(int depths, int rows, int cols, int offset, int type);
  214. <p>
  215. void #N_free_array_3d(N_array_3d * data_array);
  216. <p>
  217. int #N_get_array_3d_type(N_array_3d * array);
  218. <p>
  219. To access the 3d arrays use the following functions for reading and writing of data values
  220. <p>
  221. void #N_get_array_3d_value(N_array_3d * array3d, int depth, int row, int col, void *value);
  222. <p>
  223. float #N_get_array_3d_f_value(N_array_3d * array3d, int depth, int row, int col);
  224. <p>
  225. double #N_get_array_3d_d_value(N_array_3d * array3d, int depth, int row, int col);
  226. <p>
  227. void #N_put_array_3d_value(N_array_3d * array3d, int depth, int row, int col, char *value);
  228. <p>
  229. void #N_put_array_3d_f_value(N_array_3d * array3d, int depth, int row, int col, float value);
  230. <p>
  231. void #N_put_array_3d_d_value(N_array_3d * array3d, int depth, int row, int col, double value);
  232. <p>
  233. \subsubsection highlevel Higher level array functions
  234. To copy one array to another use this function
  235. <br>
  236. void #N_copy_array_3d(N_array_3d * source, N_array_3d * target);
  237. <p>
  238. Print the content of the array to stdout<br>
  239. void #N_print_array_3d (N_array_3d * data);
  240. <p>
  241. Compute the norm of two arrays<br>
  242. double #N_norm_array_3d (N_array_3d * array1, N_array_3d * array2, int type);
  243. <p>
  244. Perform some basic mathematical operations with two arrays<br>
  245. N_array_3d * #N_math_array_3d (N_array_3d * array1, N_array_3d * array2, N_array_3d * result, int type);
  246. <p>
  247. Convert all null values to zero<br>
  248. int #N_convert_array_3d_null_to_zero (N_array_3d * a);
  249. <p>
  250. Read a volume map into the memory<br>
  251. N_array_3d * #N_read_rast3d_to_array_3d (char *name, N_array_3d * array, int mask);
  252. <p>
  253. Write a volume map to the disk<br>
  254. void #N_write_array_3d_to_rast3d (N_array_3d * array, char *name, int mask);
  255. <p>
  256. <P>
  257. <b>Example implementation:</b><br>
  258. The GRASS module <a href="http://grass.itc.it/grass73/manuals/r3.gwflow.html">r3.gwflow</a>
  259. implements numerical calculation program for transient, confined
  260. groundwater flow in three dimensions.
  261. \subsection les Entries in the linear equation system
  262. To make entries in the linear equation system, a special structure must be provided.
  263. Currently implemented structures includes the 5 point and 7 point star scheme:
  264. \verbatim
  265. Matrix entries for the mass balance of a 5 star system
  266. The entries are center, east, west, north, south and the
  267. right side vector b of Ax = b. This system is typical used in 2d.
  268. N
  269. |
  270. W-- C --E
  271. |
  272. S
  273. Matrix entries for the mass balance of a 7 star system
  274. The entries are center, east, west, north, south, top, bottom and the
  275. right side vector b of Ax = b. This system is typical used in 3d.
  276. T N
  277. |/
  278. W-- C --E
  279. /|
  280. S B
  281. Matrix entries for the mass balance of a 9 star system
  282. The entries are center, east, west, north, south, north-east, south-east,
  283. north-wast, south-west and the
  284. right side vector b of Ax = b. This system is typical used in 2d.
  285. NW N NE
  286. \ | /
  287. W-- C --E
  288. / | \
  289. SW S SE
  290. typedef struct
  291. {
  292. int type;
  293. int count;
  294. double W, E, N, S, C, T, B, NE, NW, SE, SW, V;
  295. } N_data_star;
  296. \endverbatim
  297. The following functions should be used to create and handle the N_data_star structures:
  298. <p>
  299. Memory allocation<br>
  300. N_data_star *#N_alloc_5star(void);
  301. <p>
  302. N_data_star *#N_alloc_7star(void);
  303. <p>
  304. N_data_star *#N_alloc_9star(void);
  305. <p>
  306. Memory allocation with initialization<br>
  307. N_data_star *#N_create_5star(double C, double W, double E, double N, double S, double V);
  308. <p>
  309. N_data_star *#N_create_7star(double C, double W, double E, double N, double S, double T, double B, double V);
  310. <p>
  311. N_data_star *#N_create_9star(double C, double W, double E, double N, double S, double NW, double SW, double NE, double SE, double V);
  312. <p>
  313. <p>
  314. \subsection les functions to assemble the lineare equation system
  315. <p>
  316. Setting the callback function which fills the 5 or 7 point structure for every row of the linear equation system (eg: for each cell of the raster or volume map)
  317. <p>
  318. void #N_set_les_callback_3d_func(N_les_callback_3d * data, N_data_star * (*callback_func_3d) ());
  319. <p>
  320. void #N_set_les_callback_2d_func(N_les_callback_2d * data, N_data_star * (*callback_func_2d) ());
  321. <p>
  322. Allocation the callback structure for 2d and 3d
  323. <p>
  324. N_les_callback_3d *#N_alloc_les_callback_3d(void);
  325. <p>
  326. N_les_callback_2d *#N_alloc_les_callback_2d(void);
  327. <p>
  328. Assemble the linear equation system in 2d or 3d
  329. <p>
  330. N_les *#N_assemble_les_3d(int les_type, N_geom_data * geom, N_array_3d * status, N_array_3d * start_val, void *data, N_les_callback_3d * callback);
  331. <p>
  332. N_les *#N_assemble_les_2d(int les_type, N_geom_data * geom, N_array_2d * status, N_array_2d * start_val, void *data, N_les_callback_2d * callback);
  333. <p>
  334. templates for callback functions
  335. <p>
  336. N_data_star *#N_callback_template_3d(void *data, N_geom_data * geom, int depth, int row, int col);
  337. <p>
  338. N_data_star *#N_callback_template_2d(void *data, N_geom_data * geom, int row, int col);
  339. \section solvers Available solvers
  340. \subsection direct_solvers Direct solvers
  341. Two direct solvers are implemented, the gauss elimination solver and the lu decomposition solver.
  342. The direct solvers only work with regular quadratic matrices, not with sparse matrices.
  343. <p>
  344. int #N_solver_gauss (N_les * les);
  345. <p>
  346. int #N_solver_lu (N_les * les);
  347. \subsection iterative_solvers Iterative solvers
  348. The iterative solvers work with regular quadartic and sparse matrices.
  349. <p>
  350. To solve symmetric and positive definite linear equation systems the iterative
  351. conjugated gradient method with additional diagonal preconditioning are implemented.
  352. int #N_solver_cg(N_les * les, int maxit, double error);
  353. <p>
  354. int #N_solver_pcg(N_les * les, int maxit, double error);
  355. <p>
  356. To solve unsymmetric non definite linear equation system the iterative BiCGSatb method is implemented
  357. <p>
  358. int #N_solver_bicgstab(N_les * les, int maxit, double error);
  359. <p>
  360. Additionally jacobi and Gauss-Seidel iterative solvers with relaxation are implemented
  361. <p>
  362. int #N_solver_jacobi (N_les * L, int maxit, double sor, double error);
  363. <p>
  364. int #N_solver_SOR (N_les * L, int maxit, double sor, double error);
  365. \section available_pdes Implemented PDE's
  366. Groundwater flow in 2 and 3 dimensions are implemented.
  367. This data structure contains all data needed to compute the
  368. groundwater mass balance in three dimension
  369. \verbatim
  370. typedef struct
  371. {
  372. N_array_3d *phead; /*!piezometric head */
  373. N_array_3d *phead_start; /*!start conditions */
  374. N_array_3d *hc_x; /*!x part of the hydraulic conductivity tensor */
  375. N_array_3d *hc_y; /*!y part of the hydraulic conductivity tensor */
  376. N_array_3d *hc_z; /*!z part of the hydraulic conductivity tensor */
  377. N_array_3d *q; /*!sources and sinks */
  378. N_array_2d *r; /*!recharge at the top of the gw leayer */
  379. N_array_3d *s; /*!specific yield */
  380. N_array_3d *nf; /*!effective porosity */
  381. N_array_3d *status; /*!active/inactive/dirichlet cell status */
  382. double dt; /*!calculation time */
  383. } N_gwflow_data3d;
  384. \endverbatim
  385. This data structure contains all data needed to compute the
  386. groundwater mass balance in two dimension
  387. \verbatim
  388. typedef struct
  389. {
  390. N_array_2d *phead; /*!piezometric head */
  391. N_array_2d *phead_start; /*!start conditions */
  392. N_array_2d *hc_x; /*!x part of the hydraulic conductivity tensor */
  393. N_array_2d *hc_y; /*!y part of the hydraulic conductivity tensor */
  394. N_array_2d *q; /*!sources and sinks */
  395. N_array_2d *r; /*!recharge at the top of the gw leayer */
  396. N_array_2d *s; /*!specific yield */
  397. N_array_2d *nf; /*!effective porosity */
  398. N_array_2d *top; /*!top surface of the quifer */
  399. N_array_2d *bottom; /*!bottom of the aquifer */
  400. N_array_2d *status; /*!active/inactive/dirichlet cell status */
  401. double dt; /*!calculation time */
  402. int gwtype; /*!Which type of groundwater, N_GW_CONFINED or N_GW_UNCONFIED */
  403. } N_gwflow_data2d;
  404. \endverbatim
  405. <p>
  406. The callback to compute a les row entry in 3d dimesnions<br>
  407. N_data_star *#N_callback_gwflow_3d (void *gwdata, N_geom_data * geom, int col, int row, int depth);
  408. <p>
  409. The callback to compute a les row entry in 2d dimesnions<br>
  410. N_data_star *#N_callback_gwflow_2d (void *gwdata, N_geom_data * geom, int col, int row);
  411. <p>
  412. Allocation the 3d groundwater flow data structure<br>
  413. N_gwflow_data3d *#N_alloc_gwflow_data3d (int cols, int rows, int depths);
  414. <p>
  415. Allocation the 2d groundwater flow data structure<br>
  416. N_gwflow_data2d *#N_alloc_gwflow_data2d (int cols, int rows);
  417. <p>
  418. Releasing memory<br>
  419. void #N_free_gwflow_data3d (N_gwflow_data3d * data);
  420. <p>
  421. Releasing memory<br>
  422. void #N_free_gwflow_data2d (N_gwflow_data2d * data);
  423. <p>
  424. \section geom_data Handling geometrical data
  425. To handle geometrical data a special data structure was implemented.<br>
  426. \verbatim
  427. Geometric information about the structured grid is stored in this structure
  428. typedef struct
  429. {
  430. int planimetric; /*If the projection is not planimetric (0), the array calculation is different for each row*/
  431. double *area; /* the vector of area values for non-planimetric projection for each row*/
  432. int dim; /* 2 or 3*/
  433. double dx;
  434. double dy;
  435. double dz;
  436. double Az;
  437. int depths;
  438. int rows;
  439. int cols;
  440. } N_geom_data;
  441. \endverbatim
  442. Use the following functions to handle the geometric data structures:
  443. <p>
  444. Creating a N_geom_data structure<br>
  445. N_geom_data *#N_alloc_geom_data (void);
  446. <p>
  447. Releasing memory<br>
  448. void #N_free_geom_data (N_geom_data *geodata);
  449. <p>
  450. Initialize the N_geom_data structure with a RASTER3D_Region<br>
  451. N_geom_data *#N_init_geom_data_3d(RASTER3D_Region * region3d, N_geom_data * geodata);
  452. <p>
  453. Initialize the N_geom_data structure with a 2d region<br>
  454. N_geom_data *#N_init_geom_data_2d(struct Cell_head * region, N_geom_data * geodata);
  455. <p>
  456. Get the area of a cell in row<br>
  457. double #N_get_geom_data_area_of_cell(N_geom_data * geom, int row);
  458. \section mathtools Mathematical tools
  459. Several mean calculation algorithms are implemented.
  460. Two versions of each algorithm are available working
  461. with two values or a vector of values.
  462. <p>
  463. double #N_calc_arith_mean(double a, double b)
  464. <p>
  465. double #N_calc_arith_mean_n(double *a, int size)
  466. <p>
  467. double #N_calc_geom_mean(double a, double b)
  468. <p>
  469. double #N_calc_geom_mean_n(double *a, int size)
  470. <p>
  471. double #N_calc_harmonic_mean(double a, double b)
  472. <p>
  473. double #N_calc_harmonic_mean_n(double *a, int size)
  474. <p>
  475. double #N_calc_quad_mean(double a, double b)
  476. <p>
  477. double #N_calc_quad_mean_n(double *a, int size)
  478. <p>
  479. Two methods for upwinding stabilization are implemented.
  480. double #N_full_upwinding(double vector, double distance, double D)
  481. <p>
  482. double #N_exp_upwinding(double vector, double distance, double D)
  483. \section gradient Calculating and managing gradient and vector field data
  484. To compute and manage gradient and vector field data, specific data structures with access and management functions
  485. are implemented. Gradient and vector field data is often needed in transport calculation like solute/heat transport
  486. or navier stokes equations. The following structures and functions provide a concient way to perform
  487. gradient and vector field calculations.
  488. <p>
  489. The gradient of one cell to there neighbours in each direction has the following components:
  490. \verbatim
  491. The two dimensional gradient consits of 4 values between the neighbour cells
  492. ______________
  493. | | | |
  494. | | | |
  495. |----|-NC-|----|
  496. | | | |
  497. | WC EC |
  498. | | | |
  499. |----|-SC-|----|
  500. | | | |
  501. |____|____|____|
  502. The three dimensional gradient consits of 6 values between the neighbour cells
  503. | /
  504. TC NC
  505. |/
  506. --WC-----EC--
  507. /|
  508. SC BC
  509. / |
  510. \endverbatim
  511. Gradient between the cells in X and Y direction
  512. \verbatim
  513. typedef struct {
  514. double NC, SC, WC, EC;
  515. } N_gradient_2d;
  516. \endverbatim
  517. Gradient between the cells in X, Y and Z direction
  518. \verbatim
  519. typedef struct {
  520. double NC, SC, WC, EC, TC, BC;
  521. } N_gradient_3d;
  522. \endverbatim
  523. The gradients between the neighbours of the center cell is needed for tensor
  524. calculation in the discretization of several partial differential equations.
  525. \verbatim
  526. Gradient in X direction between the cell neighbours
  527. ____ ____ ____
  528. | | | |
  529. | NWN NEN |
  530. |____|____|____|
  531. | | | |
  532. | WN EN |
  533. |____|____|____|
  534. | | | |
  535. | SWS SES |
  536. |____|____|____|
  537. Gradient in Y direction between the cell neighbours
  538. ______________
  539. | | | |
  540. | | | |
  541. |NWW-|-NC-|-NEE|
  542. | | | |
  543. | | | |
  544. |SWW-|-SC-|-SEE|
  545. | | | |
  546. |____|____|____|
  547. Gradient in Z direction between the cell neighbours
  548. /______________/
  549. /| | | |
  550. | NWZ| NZ | NEZ|
  551. |____|____|____|
  552. /| | | |
  553. | WZ | CZ | EZ |
  554. |____|____|____|
  555. /| | | |
  556. | SWZ| SZ | SEZ|
  557. |____|____|____|
  558. /____/____/____/
  559. \endverbatim
  560. Gradient between the cell neighbours in X direction
  561. \verbatim
  562. typedef struct {
  563. double NWN, NEN, WC, EC, SWS, SES;
  564. } N_gradient_neighbours_x;
  565. \endverbatim
  566. Gradient between the cell neighbours in Y direction
  567. \verbatim
  568. typedef struct {
  569. double NWW, NEE, NC, SC, SWW, SEE;
  570. } N_gradient_neighbours_y;
  571. \endverbatim
  572. Gradient between the cell neighbours in Z direction
  573. \verbatim
  574. typedef struct {
  575. double NWZ, NZ, NEZ, WZ, CZ, EZ, SWZ, SZ, SEZ;
  576. } N_gradient_neighbours_z;
  577. \endverbatim
  578. Gradient between the cell neighbours in X and Y direction
  579. \verbatim
  580. typedef struct {
  581. N_gradient_neighbours_x *x;
  582. N_gradient_neighbours_y *y;
  583. } N_gradient_neighbours_2d;
  584. \endverbatim
  585. Gradient between the cell neighbours in X, Y and Z direction
  586. \verbatim
  587. typedef struct {
  588. N_gradient_neighbours_x *xt; /*top values*/
  589. N_gradient_neighbours_x *xc; /*center values*/
  590. N_gradient_neighbours_x *xb; /*bottom values*/
  591. N_gradient_neighbours_y *yt; /*top values*/
  592. N_gradient_neighbours_y *yc; /*center values*/
  593. N_gradient_neighbours_y *yb; /*bottom values*/
  594. N_gradient_neighbours_z *zt; /*top-center values*/
  595. N_gradient_neighbours_z *zb; /*bottom-center values*/
  596. } N_gradient_neighbours_3d;
  597. \endverbatim
  598. Two dimensional gradient field
  599. \verbatim
  600. typedef struct {
  601. N_array_2d *x_array;
  602. N_array_2d *y_array;
  603. } N_gradient_field_2d;
  604. \endverbatim
  605. Three dimensional gradient field
  606. \verbatim
  607. typedef struct {
  608. N_array_3d *x_array;
  609. N_array_3d *y_array;
  610. N_array_3d *z_array;
  611. } N_gradient_field_3d;
  612. \endverbatim
  613. Use the following functions for allocation and data handling:
  614. <p>
  615. Functions to handle a 2d gradient
  616. <p>
  617. N_gradient_2d * #N_alloc_gradient_2d(void);
  618. <p>
  619. void #N_free_gradient_2d(N_gradient_2d * grad);
  620. <p>
  621. N_gradient_2d * #N_create_gradient_2d(double NC, double SC, double WC, double EC);
  622. <p>
  623. int #N_copy_gradient_2d(N_gradient_2d * source, N_gradient_2d *target);
  624. <p>
  625. N_gradient_2d * #N_get_gradient_2d(N_gradient_field_2d *field, N_gradient_2d * gradient, int col, int row);
  626. <p>
  627. <p>
  628. Functions to handle a 2d gradient
  629. <p>
  630. N_gradient_3d * #N_alloc_gradient_3d(void);
  631. <p>
  632. void #N_free_gradient_3d(N_gradient_3d * grad);
  633. <p>
  634. N_gradient_3d * #N_create_gradient_3d(double NC, double SC, double WC, double EC, double TC, double BC);
  635. <p>
  636. int #N_copy_gradient_3d(N_gradient_3d * source, N_gradient_3d *target);
  637. <p>
  638. N_gradient_3d * #N_get_gradient_3d(N_gradient_field_3d *field, N_gradient_3d * gradient, int col, int row, int depth);
  639. <p>
  640. <p>
  641. Functions to handle gradient neighbours in x direction of the center cell
  642. <p>
  643. N_gradient_neighbours_x * #N_alloc_gradient_neighbours_x(void);
  644. <p>
  645. void #N_free_gradient_neighbours_x(N_gradient_neighbours_x *grad);
  646. <p>
  647. N_gradient_neighbours_x * #N_create_gradient_neighbours_x(double NWN, double NEN, double WC, double EC, double SWS, double SES);
  648. <p>
  649. int #N_copy_gradient_neighbours_x(N_gradient_neighbours_x * source, N_gradient_neighbours_x *target);
  650. <p>
  651. <p>
  652. Functions to handle gradient neighbours in y direction of the center cell
  653. <p>
  654. N_gradient_neighbours_y * #N_alloc_gradient_neighbours_y(void);
  655. <p>
  656. void #N_free_gradient_neighbours_y(N_gradient_neighbours_y *grad);
  657. <p>
  658. N_gradient_neighbours_y * #N_create_gradient_neighbours_y(double NWW, double NEE, double NC, double SC, double SWW, double SEE);
  659. <p>
  660. int #N_copy_gradient_neighbours_y(N_gradient_neighbours_y * source, N_gradient_neighbours_y *target);
  661. <p>
  662. <p>
  663. Functions to handle gradient neighbours in z direction of the center cell
  664. <p>
  665. N_gradient_neighbours_z * #N_alloc_gradient_neighbours_z(void);
  666. <p>
  667. void #N_free_gradient_neighbours_z(N_gradient_neighbours_z *grad);
  668. <p>
  669. N_gradient_neighbours_z * #N_create_gradient_neighbours_z(double NWZ, double NZ, double NEZ, double WZ, double CZ, double EZ,
  670. double SWZ, double SZ, double SEZ);
  671. <p>
  672. int #N_copy_gradient_neighbours_z(N_gradient_neighbours_z * source, N_gradient_neighbours_z *target);
  673. <p>
  674. <p>
  675. Functions to handle a 2d gradient neighbour structure of the center cell
  676. <p>
  677. N_gradient_neighbours_2d * #N_alloc_gradient_neighbours_2d(void);
  678. <p>
  679. void #N_free_gradient_neighbours_2d(N_gradient_neighbours_2d *grad);
  680. <p>
  681. N_gradient_neighbours_2d * #N_create_gradient_neighbours_2d(N_gradient_neighbours_x *x, N_gradient_neighbours_y *y);
  682. <p>
  683. int #N_copy_gradient_neighbours_2d(N_gradient_neighbours_2d *source, N_gradient_neighbours_2d *target);
  684. <p>
  685. <p>
  686. Functions to handle a 2d gradient neighbour structure of the center cell
  687. <p>
  688. N_gradient_neighbours_3d * #N_alloc_gradient_neighbours_3d(void);
  689. <p>
  690. void #N_free_gradient_neighbours_3d(N_gradient_neighbours_3d *grad);
  691. <p>
  692. int #N_copy_gradient_neighbours_3d(N_gradient_neighbours_3d *source, N_gradient_neighbours_3d *target);
  693. <p>
  694. <p>
  695. <p>
  696. To compute and handle a 2d gradient field the following functions are implemented:
  697. <p>
  698. N_gradient_field_2d * #N_alloc_gradient_field_2d(int cols, int rows);
  699. <p>
  700. void #N_free_gradient_field_2d(N_gradient_field_2d *field);
  701. <p>
  702. int #N_copy_gradient_field_2d(N_gradient_field_2d *source, N_gradient_field_2d *target);
  703. <p>
  704. The gradient is calculated between cells for each cell and direction.
  705. The creation of a 2d gradient field is based on the following scheme:
  706. \verbatim
  707. ______________
  708. | | | |
  709. | | | |
  710. |----|-NC-|----|
  711. | | | |
  712. | WC EC |
  713. | | | |
  714. |----|-SC-|----|
  715. | | | |
  716. |____|____|____|
  717. x - direction:
  718. r = 2 * weight[row][col]*weight[row][col + 1] / (weight[row][col]*weight[row][col + 1])
  719. EC = r * (pot[row][col] - pot[row][col + 1])/dx
  720. y - direction:
  721. r = 2 * weight[row][col]*weight[row + 1][col] / (weight[row][col]*weight[row + 1][col])
  722. SC = r * (pot[row][col] - pot[row + 1][col])/dy
  723. the values SC and EC are the values of the next row/col
  724. \endverbatim
  725. <p>
  726. N_gradient_field_2d * #N_compute_gradient_field_2d(N_array_2d *pot, N_array_2d *weight_x, N_array_2d *weight_y, N_geom_data *geom, N_gradient_field_2d *gradfield);
  727. <p>
  728. The computation of the gradient vectors for each cell is based on this scheme
  729. \verbatim
  730. ______________
  731. | | | |
  732. | | | |
  733. |----|-NC-|----|
  734. | | | |
  735. | WC EC |
  736. | | | |
  737. |----|-SC-|----|
  738. | | | |
  739. |____|____|____|
  740. x vector component:
  741. x = (WC + EC) / 2
  742. y vector component:
  743. y = (NC + SC) / 2
  744. \endverbatim
  745. <p>
  746. void #N_compute_gradient_field_components_2d(N_gradient_field_2d *field, N_array_2d *x_comp, N_array_2d *y_comp);
  747. <p>
  748. <p>
  749. N_gradient_field_3d * #N_alloc_gradient_field_3d(int cols, int rows, int depths);
  750. <p>
  751. void #N_free_gradient_field_3d(N_gradient_field_3d *field);
  752. <p>
  753. int #N_copy_gradient_field_3d(N_gradient_field_3d *source, N_gradient_field_3d *target);
  754. <p>
  755. The gradient is calculated between cells for each cell and direction.
  756. The creation of a 2d gradient field is based on the following scheme:
  757. \verbatim
  758. | /
  759. TC NC
  760. |/
  761. --WC-----EC--
  762. /|
  763. SC BC
  764. / |
  765. x - direction:
  766. r = 2 * weight_x[depth][row][col]*weight_x[depth][row][col + 1] / (weight_X[depth][row][col]*weight_x[depth][row][col + 1])
  767. EC = r * (pot[depth][row][col] - pot[depth][row][col + 1])/dx
  768. y - direction:
  769. r = 2 * weight_y[depth][row][col]*weight_y[depth][row + 1][col] / (weight_y[depth][row][col]*weight_y[depth][row + 1][col])
  770. SC = r * (pot[depth][row][col] - pot[depth][row + 1][col])/dy
  771. z - direction:
  772. r = 2 * weight_z[depth][row][col]*weight_z[depth + 1][row][col] / (weight_z[depth][row][col]*weight_z[depth + 1][row][col])
  773. TC = r * (pot[depth][row][col] - pot[depth + 1][row][col])/dy
  774. the values BC, NC, WC are the values of the next depth/row/col
  775. \endverbatim
  776. <p>
  777. N_gradient_field_3d *#N_compute_gradient_field_3d(N_array_3d * pot, N_array_3d * weight_x, N_array_3d * weight_y, N_array_3d * weight_z, N_geom_data * geom, N_gradient_field_3d *gradfield)
  778. <p>
  779. Based on this storages scheme the gradient vector for each cell is
  780. calculated and stored in the provided N_array_3d structures
  781. \verbatim
  782. | /
  783. TC NC
  784. |/
  785. --WC-----EC--
  786. /|
  787. SC BC
  788. / |
  789. x vector component:
  790. x = (WC + EC) / 2
  791. y vector component:
  792. y = (NC + SC) / 2
  793. z vector component:
  794. z = (TC + BC) / 2
  795. \endverbatim
  796. <p>
  797. N_compute_gradient_field_components_3d(N_gradient_field_3d * field, N_array_3d * x_comp, N_array_3d * y_comp, N_array_3d * z_comp)
  798. */