GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test_tools.c
Go to the documentation of this file.
1 
2 /*****************************************************************************
3 *
4 * MODULE: Grass PDE Numerical Library
5 * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
6 * soerengebbert <at> gmx <dot> de
7 *
8 * PURPOSE: Unit tests of math tools
9 *
10 * COPYRIGHT: (C) 2000 by the GRASS Development Team
11 *
12 * This program is free software under the GNU General Public
13 * License (>=v2). Read the file COPYING that comes with GRASS
14 * for details.
15 *
16 *****************************************************************************/
17 
18 #include <grass/gis.h>
19 #include <grass/glocale.h>
20 #include <grass/N_pde.h>
21 #include "test_gpde_lib.h"
22 
23 
24 /* prototypes */
25 static int test_mean_calc(void);
26 
27 /* *************************************************************** */
28 /* Perfrome the math tool tests ********************************** */
29 /* *************************************************************** */
30 int unit_test_tools(void)
31 {
32  int sum = 0;
33 
34  G_message(_("\n++ Running math tool unit tests ++"));
35 
36  sum += test_mean_calc();
37 
38  if (sum > 0)
39  G_warning(_("\n-- math tool unit tests failure --"));
40  else
41  G_message(_("\n-- math tool unit tests finished successfully --"));
42 
43  return sum;
44 }
45 
46 /* *************************************************************** */
47 /* Test the mean calculation functions *************************** */
48 /* *************************************************************** */
49 int test_mean_calc(void)
50 {
51  double a, b, mean_n, mean, vector, distance, D, weight;
52  double v[2];
53  double array[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
54  int i;
55  int sum = 0;
56  char buff1[10];
57 
58  for (i = 0; i < 10; i++)
59  array[i] += i;
60 
61  a = 1.0 / 3.0;
62  b = 3.0;
63  v[0] = a;
64  v[1] = b;
65 
66  /*arith mean */
67  mean = N_calc_arith_mean(a, b);
68  G_message("N_calc_arith_mean: calc a %g and b %g = %12.18lf", a, b, mean);
69  mean_n = N_calc_arith_mean_n(v, 2);
70  G_message("N_calc_arith_mean_n: calc a %g and b %g = %12.18lf", v[0],
71  v[1], mean_n);
72  if (mean != mean_n)
73  sum++;
74 
75  /*geom mean */
76  mean = N_calc_geom_mean(a, b);
77  G_message("N_calc_geom_mean: calc a %g and b %g = %12.18lf", a, b, mean);
78  mean_n = N_calc_geom_mean_n(v, 2);
79  G_message("N_calc_geom_mean_n: calc a %g and b %g = %12.18lf", v[0], v[1],
80  mean_n);
81  if (mean != mean_n)
82  sum++;
83 
84  /*harmonic mean */
85  mean = N_calc_harmonic_mean(a, b);
86  G_message("N_calc_harmonic_mean: calc a %g and b %g = %12.18lf", a, b,
87  mean);
88  mean_n = N_calc_harmonic_mean_n(v, 2);
89  G_message("N_calc_harmonic_mean_n: calc a %g and b %g = %12.18lf", v[0],
90  v[1], mean_n);
91  if (mean != mean_n)
92  sum++;
93  /*null test */
94  a = 2;
95  b = 0;
96  v[0] = a;
97  v[1] = b;
98  mean = N_calc_harmonic_mean(a, b);
99  G_message("N_calc_harmonic_mean: calc a %g and b %g = %12.18lf", a, b,
100  mean);
101  mean_n = N_calc_harmonic_mean_n(v, 2);
102  G_message("N_calc_harmonic_mean_n: calc a %g and b %g = %12.18lf", v[0],
103  v[1], mean_n);
104  if (mean != mean_n)
105  sum++;
106 
107  /*quadratic mean */
108  a = 1.0 / 3.0;
109  b = 3.0;
110  v[0] = a;
111  v[1] = b;
112 
113  mean = N_calc_quad_mean(a, b);
114  G_message("N_calc_quad_mean: calc a %g and b %g = %12.18lf", a, b, mean);
115  mean_n = N_calc_quad_mean_n(v, 2);
116  G_message("N_calc_quad_mean_n: calc a %g and b %g = %12.18lf", v[0], v[1],
117  mean_n);
118  if (mean != mean_n)
119  sum++;
120 
121  /*Test the full upwind stabailization */
122  vector = -0.000001;
123  distance = 20;
124  D = 0.000001;
125 
126  weight = N_full_upwinding(vector, distance, D);
127  G_message("N_full_upwinding: vector %g distance %g D %g weight %g\n",
128  vector, distance, D, weight);
129 
130  if (weight != 0) {
131  G_warning("Error detected in N_full_upwinding");
132  sum++;
133  }
134 
135  vector = 0.000001;
136 
137  weight = N_full_upwinding(vector, distance, D);
138  G_message("N_full_upwinding: vector %g distance %g D %g weight %g\n",
139  vector, distance, D, weight);
140  if (weight != 1) {
141  G_warning("Error detected in N_full_upwinding");
142  sum++;
143  }
144 
145  D = 0.0;
146 
147  weight = N_full_upwinding(vector, distance, D);
148  G_message("N_full_upwinding: vector %g distance %g D %g weight %g\n",
149  vector, distance, D, weight);
150  if (weight != 0.5) {
151  G_warning("Error detected in N_full_upwinding");
152  sum++;
153  }
154 
155 
156  /*Test the exponential upwind stabailization */
157  vector = -0.000001;
158  distance = 20;
159  D = 0.000001;
160 
161  weight = N_exp_upwinding(vector, distance, D);
162  G_message("N_exp_upwinding: vector %g distance %g D %g weight %g\n",
163  vector, distance, D, weight);
164  sprintf(buff1, "%1.2lf", weight);
165  sscanf(buff1, "%lf", &weight);
166 
167  if (weight != 0.05) {
168  G_warning("Error detected in N_exp_upwinding");
169  sum++;
170  }
171 
172  vector = 0.000001;
173 
174  weight = N_exp_upwinding(vector, distance, D);
175  G_message("N_exp_upwinding: vector %g distance %g D %g weight %g\n",
176  vector, distance, D, weight);
177  sprintf(buff1, "%1.2lf", weight);
178  sscanf(buff1, "%lf", &weight);
179 
180  if (weight != 0.95) {
181  G_warning("Error detected in N_exp_upwinding");
182  sum++;
183  }
184 
185  D = 0.0;
186 
187  weight = N_exp_upwinding(vector, distance, D);
188  G_message("N_exp_upwinding: vector %g distance %g D %g weight %g\n",
189  vector, distance, D, weight);
190  if (weight != 0.5) {
191  G_warning("Error detected in N_exp_upwinding");
192  sum++;
193  }
194 
195  return sum;
196 }