GRASS Programmer's Manual  6.4.2(2012)
test_tools.c
Go to the documentation of this file.
00001 
00002 /*****************************************************************************
00003 *
00004 * MODULE:       Grass PDE Numerical Library
00005 * AUTHOR(S):    Soeren Gebbert, Berlin (GER) Dec 2006
00006 *               soerengebbert <at> gmx <dot> de
00007 *               
00008 * PURPOSE:      Unit tests of math tools 
00009 *
00010 * COPYRIGHT:    (C) 2000 by the GRASS Development Team
00011 *
00012 *               This program is free software under the GNU General Public
00013 *               License (>=v2). Read the file COPYING that comes with GRASS
00014 *               for details.
00015 *
00016 *****************************************************************************/
00017 
00018 #include <grass/gis.h>
00019 #include <grass/glocale.h>
00020 #include <grass/N_pde.h>
00021 #include "test_gpde_lib.h"
00022 
00023 
00024 /* prototypes */
00025 static int test_mean_calc(void);
00026 
00027 /* *************************************************************** */
00028 /* Perfrome the math tool tests ********************************** */
00029 /* *************************************************************** */
00030 int unit_test_tools(void)
00031 {
00032     int sum = 0;
00033 
00034     G_message(_("\n++ Running math tool unit tests ++"));
00035 
00036     sum += test_mean_calc();
00037 
00038     if (sum > 0)
00039         G_warning(_("\n-- math tool unit tests failure --"));
00040     else
00041         G_message(_("\n-- math tool unit tests finished successfully --"));
00042 
00043     return sum;
00044 }
00045 
00046 /* *************************************************************** */
00047 /* Test the mean calculation functions *************************** */
00048 /* *************************************************************** */
00049 int test_mean_calc(void)
00050 {
00051     double a, b, mean_n, mean, vector, distance, D, weight;
00052     double v[2];
00053     double array[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
00054     int i;
00055     int sum = 0;
00056     char buff1[10];
00057 
00058     for (i = 0; i < 10; i++)
00059         array[i] += i;
00060 
00061     a = 1.0 / 3.0;
00062     b = 3.0;
00063     v[0] = a;
00064     v[1] = b;
00065 
00066     /*arith mean */
00067     mean = N_calc_arith_mean(a, b);
00068     G_message("N_calc_arith_mean: calc a %g and b %g = %12.18lf", a, b, mean);
00069     mean_n = N_calc_arith_mean_n(v, 2);
00070     G_message("N_calc_arith_mean_n: calc a %g and b %g = %12.18lf", v[0],
00071               v[1], mean_n);
00072     if (mean != mean_n)
00073         sum++;
00074 
00075     /*geom mean */
00076     mean = N_calc_geom_mean(a, b);
00077     G_message("N_calc_geom_mean: calc a %g and b %g = %12.18lf", a, b, mean);
00078     mean_n = N_calc_geom_mean_n(v, 2);
00079     G_message("N_calc_geom_mean_n: calc a %g and b %g = %12.18lf", v[0], v[1],
00080               mean_n);
00081     if (mean != mean_n)
00082         sum++;
00083 
00084     /*harmonic mean */
00085     mean = N_calc_harmonic_mean(a, b);
00086     G_message("N_calc_harmonic_mean: calc a %g and b %g = %12.18lf", a, b,
00087               mean);
00088     mean_n = N_calc_harmonic_mean_n(v, 2);
00089     G_message("N_calc_harmonic_mean_n: calc a %g and b %g = %12.18lf", v[0],
00090               v[1], mean_n);
00091     if (mean != mean_n)
00092         sum++;
00093     /*null test */
00094     a = 2;
00095     b = 0;
00096     v[0] = a;
00097     v[1] = b;
00098     mean = N_calc_harmonic_mean(a, b);
00099     G_message("N_calc_harmonic_mean: calc a %g and b %g = %12.18lf", a, b,
00100               mean);
00101     mean_n = N_calc_harmonic_mean_n(v, 2);
00102     G_message("N_calc_harmonic_mean_n: calc a %g and b %g = %12.18lf", v[0],
00103               v[1], mean_n);
00104     if (mean != mean_n)
00105         sum++;
00106 
00107     /*quadratic mean */
00108     a = 1.0 / 3.0;
00109     b = 3.0;
00110     v[0] = a;
00111     v[1] = b;
00112 
00113     mean = N_calc_quad_mean(a, b);
00114     G_message("N_calc_quad_mean: calc a %g and b %g = %12.18lf", a, b, mean);
00115     mean_n = N_calc_quad_mean_n(v, 2);
00116     G_message("N_calc_quad_mean_n: calc a %g and b %g = %12.18lf", v[0], v[1],
00117               mean_n);
00118     if (mean != mean_n)
00119         sum++;
00120 
00121     /*Test the full upwind stabailization */
00122     vector = -0.000001;
00123     distance = 20;
00124     D = 0.000001;
00125 
00126     weight = N_full_upwinding(vector, distance, D);
00127     G_message("N_full_upwinding: vector %g distance %g D %g weight %g\n",
00128               vector, distance, D, weight);
00129 
00130     if (weight != 0) {
00131         G_warning("Error detected in N_full_upwinding");
00132         sum++;
00133     }
00134 
00135     vector = 0.000001;
00136 
00137     weight = N_full_upwinding(vector, distance, D);
00138     G_message("N_full_upwinding: vector %g distance %g D %g weight %g\n",
00139               vector, distance, D, weight);
00140     if (weight != 1) {
00141         G_warning("Error detected in N_full_upwinding");
00142         sum++;
00143     }
00144 
00145     D = 0.0;
00146 
00147     weight = N_full_upwinding(vector, distance, D);
00148     G_message("N_full_upwinding: vector %g distance %g D %g weight %g\n",
00149               vector, distance, D, weight);
00150     if (weight != 0.5) {
00151         G_warning("Error detected in N_full_upwinding");
00152         sum++;
00153     }
00154 
00155 
00156     /*Test the exponential upwind stabailization */
00157     vector = -0.000001;
00158     distance = 20;
00159     D = 0.000001;
00160 
00161     weight = N_exp_upwinding(vector, distance, D);
00162     G_message("N_exp_upwinding: vector %g distance %g D %g weight %g\n",
00163               vector, distance, D, weight);
00164     sprintf(buff1, "%1.2lf", weight);
00165     sscanf(buff1, "%lf", &weight);
00166 
00167     if (weight != 0.05) {
00168         G_warning("Error detected in N_exp_upwinding");
00169         sum++;
00170     }
00171 
00172     vector = 0.000001;
00173 
00174     weight = N_exp_upwinding(vector, distance, D);
00175     G_message("N_exp_upwinding: vector %g distance %g D %g weight %g\n",
00176               vector, distance, D, weight);
00177     sprintf(buff1, "%1.2lf", weight);
00178     sscanf(buff1, "%lf", &weight);
00179 
00180     if (weight != 0.95) {
00181         G_warning("Error detected in N_exp_upwinding");
00182         sum++;
00183     }
00184 
00185     D = 0.0;
00186 
00187     weight = N_exp_upwinding(vector, distance, D);
00188     G_message("N_exp_upwinding: vector %g distance %g D %g weight %g\n",
00189               vector, distance, D, weight);
00190     if (weight != 0.5) {
00191         G_warning("Error detected in N_exp_upwinding");
00192         sum++;
00193     }
00194 
00195     return sum;
00196 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines