test_tools.c 5.0 KB

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