test_tools.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 of math tools
  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 <grass/gis.h>
  17. #include <grass/N_pde.h>
  18. #include "test_gpde_lib.h"
  19. /* prototypes */
  20. static int test_mean_calc(void);
  21. /* *************************************************************** */
  22. /* Perfrome the math tool tests ********************************** */
  23. /* *************************************************************** */
  24. int unit_test_tools(void)
  25. {
  26. int sum = 0;
  27. G_message("\n++ Running math tool unit tests ++");
  28. sum += test_mean_calc();
  29. if (sum > 0)
  30. G_warning("\n-- math tool unit tests failure --");
  31. else
  32. G_message("\n-- math tool unit tests finished successfully --");
  33. return sum;
  34. }
  35. /* *************************************************************** */
  36. /* Test the mean calculation functions *************************** */
  37. /* *************************************************************** */
  38. int test_mean_calc(void)
  39. {
  40. double a, b, mean_n, mean, vector, distance, D, weight;
  41. double v[2];
  42. double array[10] = {0,0,0,0,0,0,0,0,0,0};
  43. int i;
  44. int sum = 0;
  45. char buff1[10];
  46. for(i = 0; i < 10; i++)
  47. array[i] += i;
  48. a = 1.0/3.0;
  49. b = 3.0;
  50. v[0] = a; v[1] = b;
  51. /*arith mean*/
  52. mean = N_calc_arith_mean(a, b);
  53. G_message("N_calc_arith_mean: calc a %g and b %g = %12.18lf", a, b, mean);
  54. mean_n = N_calc_arith_mean_n(v, 2);
  55. G_message("N_calc_arith_mean_n: calc a %g and b %g = %12.18lf", v[0], v[1], mean_n);
  56. if(mean != mean_n)
  57. sum++;
  58. /*geom mean*/
  59. mean = N_calc_geom_mean(a, b);
  60. G_message("N_calc_geom_mean: calc a %g and b %g = %12.18lf", a, b, mean);
  61. mean_n = N_calc_geom_mean_n(v, 2);
  62. G_message("N_calc_geom_mean_n: calc a %g and b %g = %12.18lf", v[0], v[1], mean_n);
  63. if(mean != mean_n)
  64. sum++;
  65. /*harmonic mean*/
  66. mean = N_calc_harmonic_mean(a, b);
  67. G_message("N_calc_harmonic_mean: calc a %g and b %g = %12.18lf", a, b, mean);
  68. mean_n = N_calc_harmonic_mean_n(v, 2);
  69. G_message("N_calc_harmonic_mean_n: calc a %g and b %g = %12.18lf", v[0], v[1], mean_n);
  70. if(mean != mean_n)
  71. sum++;
  72. /*null test*/
  73. a = 2;
  74. b = 0;
  75. v[0] = a; v[1] = b;
  76. mean = N_calc_harmonic_mean(a, b);
  77. G_message("N_calc_harmonic_mean: calc a %g and b %g = %12.18lf", a, b, mean);
  78. mean_n = N_calc_harmonic_mean_n(v, 2);
  79. G_message("N_calc_harmonic_mean_n: calc a %g and b %g = %12.18lf", v[0], v[1], mean_n);
  80. if(mean != mean_n)
  81. sum++;
  82. /*quadratic mean*/
  83. a = 1.0/3.0;
  84. b = 3.0;
  85. v[0] = a; v[1] = b;
  86. mean = N_calc_quad_mean(a, b);
  87. G_message("N_calc_quad_mean: calc a %g and b %g = %12.18lf", a, b, mean);
  88. mean_n = N_calc_quad_mean_n(v, 2);
  89. G_message("N_calc_quad_mean_n: calc a %g and b %g = %12.18lf", v[0], v[1], mean_n);
  90. if(mean != mean_n)
  91. sum++;
  92. /*Test the full upwind stabailization*/
  93. vector= -0.000001;
  94. distance = 20;
  95. D = 0.000001;
  96. weight = N_full_upwinding(vector, distance, D);
  97. G_message("N_full_upwinding: vector %g distance %g D %g weight %g\n", vector, distance, D, weight);
  98. if(weight != 0)
  99. {
  100. G_warning("Error detected in N_full_upwinding");
  101. sum++;
  102. }
  103. vector= 0.000001;
  104. weight = N_full_upwinding(vector, distance, D);
  105. G_message("N_full_upwinding: vector %g distance %g D %g weight %g\n", vector, distance, D, weight);
  106. if(weight != 1)
  107. {
  108. G_warning("Error detected in N_full_upwinding");
  109. sum++;
  110. }
  111. D = 0.0;
  112. weight = N_full_upwinding(vector, distance, D);
  113. G_message("N_full_upwinding: vector %g distance %g D %g weight %g\n", vector, distance, D, weight);
  114. if(weight != 0.5)
  115. {
  116. G_warning("Error detected in N_full_upwinding");
  117. sum++;
  118. }
  119. /*Test the exponential upwind stabailization*/
  120. vector= -0.000001;
  121. distance = 20;
  122. D = 0.000001;
  123. weight = N_exp_upwinding(vector, distance, D);
  124. G_message("N_exp_upwinding: vector %g distance %g D %g weight %g\n", vector, distance, D, weight);
  125. sprintf(buff1, "%1.2lf", weight);
  126. sscanf(buff1, "%lf", &weight);
  127. if(weight != 0.05)
  128. {
  129. G_warning("Error detected in N_exp_upwinding");
  130. sum++;
  131. }
  132. vector= 0.000001;
  133. weight = N_exp_upwinding(vector, distance, D);
  134. G_message("N_exp_upwinding: vector %g distance %g D %g weight %g\n", vector, distance, D, weight);
  135. sprintf(buff1, "%1.2lf", weight);
  136. sscanf(buff1, "%lf", &weight);
  137. if(weight != 0.95)
  138. {
  139. G_warning("Error detected in N_exp_upwinding");
  140. sum++;
  141. }
  142. D = 0.0;
  143. weight = N_exp_upwinding(vector, distance, D);
  144. G_message("N_exp_upwinding: vector %g distance %g D %g weight %g\n", vector, distance, D, weight);
  145. if(weight != 0.5)
  146. {
  147. G_warning("Error detected in N_exp_upwinding");
  148. sum++;
  149. }
  150. return sum;
  151. }