test_assemble.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*****************************************************************************
  2. *
  3. * MODULE: Grass PDE Numerical Library
  4. * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
  5. * soerengebbert <at> gmx <dot> de
  6. *
  7. * PURPOSE: Unit tests for matrix assembling
  8. *
  9. * COPYRIGHT: (C) 2000 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <grass/N_pde.h>
  20. #include "test_gpde_lib.h"
  21. /* prototypes */
  22. static int test_matrix_assemble_2d(void);
  23. static int test_matrix_assemble_3d(void);
  24. static N_array_2d *create_status_array_2d(void);
  25. static N_array_3d *create_status_array_3d(void);
  26. static N_array_2d *create_value_array_2d(void);
  27. static N_array_3d *create_value_array_3d(void);
  28. /* *************************************************************** */
  29. /* Performe the les assmbling tests ****************************** */
  30. /* *************************************************************** */
  31. int unit_test_assemble(void)
  32. {
  33. int sum = 0;
  34. G_message("\n++ Running assembling unit tests ++");
  35. G_message("\t 1. testing 2d assembling");
  36. sum += test_matrix_assemble_2d();
  37. G_message("\t 2. testing 3d assembling");
  38. sum += test_matrix_assemble_3d();
  39. if (sum > 0)
  40. G_warning("\n-- Assembling unit tests failure --");
  41. else
  42. G_message("\n-- Assembling unit tests finished successfully --");
  43. return sum;
  44. }
  45. /* *************************************************************** */
  46. /* Create the status array with values of 1 and 2 **************** */
  47. /* *************************************************************** */
  48. N_array_2d *create_status_array_2d(void)
  49. {
  50. N_array_2d *data;
  51. int i, j;
  52. data = N_alloc_array_2d(TEST_N_NUM_COLS, TEST_N_NUM_ROWS, 1, CELL_TYPE);
  53. #pragma omp parallel for private (i, j) shared (data)
  54. for (j = 0; j < TEST_N_NUM_ROWS; j++) {
  55. for (i = 0; i < TEST_N_NUM_COLS; i++) {
  56. if (j == 1) {
  57. N_put_array_2d_c_value(data, i, j, 2);
  58. }
  59. else {
  60. N_put_array_2d_c_value(data, i, j, 1);
  61. }
  62. }
  63. }
  64. return data;
  65. }
  66. /* *************************************************************** */
  67. /* Create a value array ****************************************** */
  68. /* *************************************************************** */
  69. N_array_2d *create_value_array_2d(void)
  70. {
  71. N_array_2d *data;
  72. int i, j;
  73. data = N_alloc_array_2d(TEST_N_NUM_COLS, TEST_N_NUM_ROWS, 1, DCELL_TYPE);
  74. #pragma omp parallel for private (i, j) shared (data)
  75. for (j = 0; j < TEST_N_NUM_ROWS; j++) {
  76. for (i = 0; i < TEST_N_NUM_COLS; i++) {
  77. if (j == 1) {
  78. N_put_array_2d_d_value(data, i, j, 50);
  79. }
  80. else {
  81. N_put_array_2d_d_value(data, i, j, 1);
  82. }
  83. }
  84. }
  85. return data;
  86. }
  87. /* *************************************************************** */
  88. /* Create the status array with values of 1 and 2 **************** */
  89. /* *************************************************************** */
  90. N_array_3d *create_status_array_3d(void)
  91. {
  92. N_array_3d *data;
  93. int i, j, k;
  94. data =
  95. N_alloc_array_3d(TEST_N_NUM_COLS, TEST_N_NUM_ROWS, TEST_N_NUM_DEPTHS, 1,
  96. FCELL_TYPE);
  97. #pragma omp parallel for private (i, j, k) shared (data)
  98. for (k = 0; k < TEST_N_NUM_DEPTHS; k++)
  99. for (j = 0; j < TEST_N_NUM_ROWS; j++) {
  100. for (i = 0; i < TEST_N_NUM_COLS; i++) {
  101. if (i == 0 && j == 1) {
  102. N_put_array_3d_f_value(data, i, j, k, 2.0);
  103. }
  104. else {
  105. N_put_array_3d_f_value(data, i, j, k, 1.0);
  106. }
  107. }
  108. }
  109. return data;
  110. }
  111. /* *************************************************************** */
  112. /* Create a value array ****************************************** */
  113. /* *************************************************************** */
  114. N_array_3d *create_value_array_3d(void)
  115. {
  116. N_array_3d *data;
  117. int i, j, k;
  118. data =
  119. N_alloc_array_3d(TEST_N_NUM_COLS, TEST_N_NUM_ROWS, TEST_N_NUM_DEPTHS, 1,
  120. DCELL_TYPE);
  121. #pragma omp parallel for private (i, j, k) shared (data)
  122. for (k = 0; k < TEST_N_NUM_DEPTHS; k++)
  123. for (j = 0; j < TEST_N_NUM_ROWS; j++) {
  124. for (i = 0; i < TEST_N_NUM_COLS; i++) {
  125. if (i == 0 && j == 1) {
  126. N_put_array_3d_f_value(data, i, j, k, 50);
  127. }
  128. else {
  129. N_put_array_3d_f_value(data, i, j, k, 1);
  130. }
  131. }
  132. }
  133. return data;
  134. }
  135. /* *************************************************************** */
  136. /* Test the matrix assembling with 3d array data ***************** */
  137. /* *************************************************************** */
  138. int test_matrix_assemble_3d(void)
  139. {
  140. N_geom_data *geom;
  141. N_les *les;
  142. N_les_callback_3d *call;
  143. N_array_3d *status;
  144. N_array_3d *start_val;
  145. /*set the callback to default */
  146. call = N_alloc_les_callback_3d();
  147. status = create_status_array_3d();
  148. start_val = create_value_array_3d();
  149. geom = N_alloc_geom_data();
  150. geom->dx = 1;
  151. geom->dy = 1;
  152. geom->dz = 1;
  153. geom->Az = 1;
  154. geom->depths = TEST_N_NUM_DEPTHS;
  155. geom->rows = TEST_N_NUM_ROWS;
  156. geom->cols = TEST_N_NUM_COLS;
  157. /*Assemble the matrix */
  158. les = N_assemble_les_3d(N_SPARSE_LES, geom, status, start_val, NULL, call);
  159. N_free_les(les);
  160. les = N_assemble_les_3d_active(N_SPARSE_LES, geom, status, start_val, NULL, call);
  161. N_free_les(les);
  162. les = N_assemble_les_3d_dirichlet(N_SPARSE_LES, geom, status, start_val, NULL, call);
  163. N_les_integrate_dirichlet_3d(les, geom, status, start_val);
  164. N_free_les(les);
  165. les = N_assemble_les_3d(N_NORMAL_LES, geom, status, start_val, NULL, call);
  166. N_free_les(les);
  167. les = N_assemble_les_3d_active(N_NORMAL_LES, geom, status, start_val, NULL, call);
  168. N_free_les(les);
  169. les = N_assemble_les_3d_dirichlet(N_NORMAL_LES, geom, status, start_val, NULL, call);
  170. N_les_integrate_dirichlet_3d(les, geom, status, start_val);
  171. N_free_les(les);
  172. G_free(geom);
  173. G_free(call);
  174. return 0;
  175. }
  176. /* *************************************************************** */
  177. /* Test the matrix assembling with 2d array data ***************** */
  178. /* *************************************************************** */
  179. int test_matrix_assemble_2d(void)
  180. {
  181. N_geom_data *geom;
  182. N_les *les;
  183. N_les_callback_2d *call;
  184. N_array_2d *status;
  185. N_array_2d *start_val;
  186. /*set the callback to default */
  187. call = N_alloc_les_callback_2d();
  188. status = create_status_array_2d();
  189. start_val = create_value_array_2d();
  190. geom = N_alloc_geom_data();
  191. geom->dx = 1;
  192. geom->dy = 1;
  193. geom->Az = 1;
  194. geom->rows = TEST_N_NUM_ROWS;
  195. geom->cols = TEST_N_NUM_COLS;
  196. /*Assemble the matrix */
  197. les = N_assemble_les_2d(N_SPARSE_LES, geom, status, start_val, NULL, call);
  198. N_free_les(les);
  199. les = N_assemble_les_2d_active(N_SPARSE_LES, geom, status, start_val, NULL, call);
  200. N_free_les(les);
  201. les = N_assemble_les_2d_dirichlet(N_SPARSE_LES, geom, status, start_val, NULL, call);
  202. N_les_integrate_dirichlet_2d(les, geom, status, start_val);
  203. N_free_les(les);
  204. les = N_assemble_les_2d(N_NORMAL_LES, geom, status, start_val, NULL, call);
  205. N_free_les(les);
  206. les = N_assemble_les_2d_active(N_NORMAL_LES, geom, status, start_val, NULL, call);
  207. N_free_les(les);
  208. les = N_assemble_les_2d_dirichlet(N_NORMAL_LES, geom, status, start_val, NULL, call);
  209. N_les_integrate_dirichlet_2d(les, geom, status, start_val);
  210. N_free_les(les);
  211. G_free(geom);
  212. G_free(call);
  213. return 0;
  214. }