MOAB: Mesh Oriented datABase  (version 5.4.1)
StdDevTemplateTest.cpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2006 Sandia National Laboratories.  Developed at the
00005     University of Wisconsin--Madison under SNL contract number
00006     624796.  The U.S. Government and the University of Wisconsin
00007     retain certain rights to this software.
00008 
00009     This library is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU Lesser General Public
00011     License as published by the Free Software Foundation; either
00012     version 2.1 of the License, or (at your option) any later version.
00013 
00014     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     (lgpl.txt) along with this library; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023     (2006) [email protected]
00024 
00025   ***************************************************************** */
00026 
00027 /** \file StdDevTemplateTest.cpp
00028  *  \brief Unit tests for StdDevTemplate and VarianceTemplate
00029  *  \author Jason Kraftcheck
00030  */
00031 
00032 #include "Mesquite.hpp"
00033 #include "StdDevTemplate.hpp"
00034 #include "VarianceTemplate.hpp"
00035 #include "MsqError.hpp"
00036 #include "PatchData.hpp"
00037 #include "ObjectiveFunctionTests.hpp"
00038 #include "MsqHessian.hpp"
00039 
00040 using namespace MBMesquite;
00041 using namespace std;
00042 
00043 const double EPSILON = 1e-4;
00044 
00045 class StdDevTemplateTest : public CppUnit::TestFixture, public ObjectiveFunctionTests
00046 {
00047   private:
00048     CPPUNIT_TEST_SUITE( StdDevTemplateTest );
00049 
00050     CPPUNIT_TEST( test_eval_calc );
00051     CPPUNIT_TEST( test_eval_accum );
00052     CPPUNIT_TEST( test_eval_save );
00053     CPPUNIT_TEST( test_eval_update );
00054     CPPUNIT_TEST( test_eval_temp );
00055 
00056     CPPUNIT_TEST( test_eval_calc_sqr );
00057     CPPUNIT_TEST( test_eval_accum_sqr );
00058     CPPUNIT_TEST( test_eval_save_sqr );
00059     CPPUNIT_TEST( test_eval_update_sqr );
00060     CPPUNIT_TEST( test_eval_temp_sqr );
00061 
00062     CPPUNIT_TEST( test_grad_calc );
00063     CPPUNIT_TEST( test_grad_save );
00064     CPPUNIT_TEST( test_grad_update );
00065     CPPUNIT_TEST( test_grad_temp );
00066 
00067     CPPUNIT_TEST( test_grad_calc_sqr );
00068     CPPUNIT_TEST( test_grad_save_sqr );
00069     CPPUNIT_TEST( test_grad_update_sqr );
00070     CPPUNIT_TEST( test_grad_temp_sqr );
00071 
00072     CPPUNIT_TEST( test_diag_calc );
00073     CPPUNIT_TEST( test_diag_save );
00074     CPPUNIT_TEST( test_diag_update );
00075     CPPUNIT_TEST( test_diag_temp );
00076 
00077     CPPUNIT_TEST( test_diag_calc_sqr );
00078     CPPUNIT_TEST( test_diag_save_sqr );
00079     CPPUNIT_TEST( test_diag_update_sqr );
00080     CPPUNIT_TEST( test_diag_temp_sqr );
00081 
00082     CPPUNIT_TEST( test_hessian_fails );
00083     CPPUNIT_TEST( test_hessian_fails_sqr );
00084 
00085     CPPUNIT_TEST( test_failed_metric_in_eval );
00086     CPPUNIT_TEST( test_failed_metric_in_grad );
00087     CPPUNIT_TEST( test_failed_metric_in_eval_sqr );
00088     CPPUNIT_TEST( test_failed_metric_in_grad_sqr );
00089 
00090     CPPUNIT_TEST( test_false_metric_in_eval );
00091     CPPUNIT_TEST( test_false_metric_in_grad );
00092     CPPUNIT_TEST( test_false_metric_in_eval_sqr );
00093     CPPUNIT_TEST( test_false_metric_in_grad_sqr );
00094 
00095     CPPUNIT_TEST( test_evaluate );
00096     CPPUNIT_TEST( test_evaluate_sqr );
00097 
00098     CPPUNIT_TEST( test_numerical_gradient );
00099     CPPUNIT_TEST( test_numerical_gradient_sqr );
00100 
00101     CPPUNIT_TEST( test_diagonal_gradient );
00102     CPPUNIT_TEST( test_diagonal_gradient_sqr );
00103 
00104     CPPUNIT_TEST( test_hessian_diag );
00105     CPPUNIT_TEST( test_hessian_diag_sqr );
00106 
00107     CPPUNIT_TEST( test_clone );
00108     CPPUNIT_TEST( test_clone_sqr );
00109 
00110     CPPUNIT_TEST( test_eval_negate );
00111     CPPUNIT_TEST( test_eval_negate_sqr );
00112     CPPUNIT_TEST( test_grad_negate );
00113     CPPUNIT_TEST( test_grad_negate_sqr );
00114     CPPUNIT_TEST( test_diag_negate );
00115     CPPUNIT_TEST( test_diag_negate_sqr );
00116 
00117     CPPUNIT_TEST_SUITE_END();
00118 
00119   public:
00120     void test_eval_calc()
00121     {
00122         StdDevTemplate of( NULL );
00123         test_eval_type( ObjectiveFunction::CALCULATE, EVAL, &of );
00124     }
00125     void test_eval_accum()
00126     {
00127         StdDevTemplate of( NULL );
00128         test_eval_type( ObjectiveFunction::ACCUMULATE, EVAL, &of );
00129     }
00130     void test_eval_save()
00131     {
00132         StdDevTemplate of( NULL );
00133         test_eval_type( ObjectiveFunction::SAVE, EVAL, &of );
00134     }
00135     void test_eval_update()
00136     {
00137         StdDevTemplate of( NULL );
00138         test_eval_type( ObjectiveFunction::UPDATE, EVAL, &of );
00139     }
00140     void test_eval_temp()
00141     {
00142         StdDevTemplate of( NULL );
00143         test_eval_type( ObjectiveFunction::TEMPORARY, EVAL, &of );
00144     }
00145 
00146     void test_eval_calc_sqr()
00147     {
00148         VarianceTemplate of( NULL );
00149         test_eval_type( ObjectiveFunction::CALCULATE, EVAL, &of );
00150     }
00151     void test_eval_accum_sqr()
00152     {
00153         VarianceTemplate of( NULL );
00154         test_eval_type( ObjectiveFunction::ACCUMULATE, EVAL, &of );
00155     }
00156     void test_eval_save_sqr()
00157     {
00158         VarianceTemplate of( NULL );
00159         test_eval_type( ObjectiveFunction::SAVE, EVAL, &of );
00160     }
00161     void test_eval_update_sqr()
00162     {
00163         VarianceTemplate of( NULL );
00164         test_eval_type( ObjectiveFunction::UPDATE, EVAL, &of );
00165     }
00166     void test_eval_temp_sqr()
00167     {
00168         VarianceTemplate of( NULL );
00169         test_eval_type( ObjectiveFunction::TEMPORARY, EVAL, &of );
00170     }
00171 
00172     void test_grad_calc()
00173     {
00174         StdDevTemplate of( NULL );
00175         test_eval_type( ObjectiveFunction::CALCULATE, GRAD, &of );
00176     }
00177     void test_grad_accum()
00178     {
00179         StdDevTemplate of( NULL );
00180         test_eval_type( ObjectiveFunction::ACCUMULATE, GRAD, &of );
00181     }
00182     void test_grad_save()
00183     {
00184         StdDevTemplate of( NULL );
00185         test_eval_type( ObjectiveFunction::SAVE, GRAD, &of );
00186     }
00187     void test_grad_update()
00188     {
00189         StdDevTemplate of( NULL );
00190         test_eval_type( ObjectiveFunction::UPDATE, GRAD, &of );
00191     }
00192     void test_grad_temp()
00193     {
00194         StdDevTemplate of( NULL );
00195         test_eval_type( ObjectiveFunction::TEMPORARY, GRAD, &of );
00196     }
00197 
00198     void test_grad_calc_sqr()
00199     {
00200         VarianceTemplate of( NULL );
00201         test_eval_type( ObjectiveFunction::CALCULATE, GRAD, &of );
00202     }
00203     void test_grad_accum_sqr()
00204     {
00205         VarianceTemplate of( NULL );
00206         test_eval_type( ObjectiveFunction::ACCUMULATE, GRAD, &of );
00207     }
00208     void test_grad_save_sqr()
00209     {
00210         VarianceTemplate of( NULL );
00211         test_eval_type( ObjectiveFunction::SAVE, GRAD, &of );
00212     }
00213     void test_grad_update_sqr()
00214     {
00215         VarianceTemplate of( NULL );
00216         test_eval_type( ObjectiveFunction::UPDATE, GRAD, &of );
00217     }
00218     void test_grad_temp_sqr()
00219     {
00220         VarianceTemplate of( NULL );
00221         test_eval_type( ObjectiveFunction::TEMPORARY, GRAD, &of );
00222     }
00223 
00224     void test_diag_calc()
00225     {
00226         StdDevTemplate of( NULL );
00227         test_eval_type( ObjectiveFunction::CALCULATE, DIAG, &of );
00228     }
00229     void test_diag_accum()
00230     {
00231         StdDevTemplate of( NULL );
00232         test_eval_type( ObjectiveFunction::ACCUMULATE, DIAG, &of );
00233     }
00234     void test_diag_save()
00235     {
00236         StdDevTemplate of( NULL );
00237         test_eval_type( ObjectiveFunction::SAVE, DIAG, &of );
00238     }
00239     void test_diag_update()
00240     {
00241         StdDevTemplate of( NULL );
00242         test_eval_type( ObjectiveFunction::UPDATE, DIAG, &of );
00243     }
00244     void test_diag_temp()
00245     {
00246         StdDevTemplate of( NULL );
00247         test_eval_type( ObjectiveFunction::TEMPORARY, DIAG, &of );
00248     }
00249 
00250     void test_diag_calc_sqr()
00251     {
00252         VarianceTemplate of( NULL );
00253         test_eval_type( ObjectiveFunction::CALCULATE, DIAG, &of );
00254     }
00255     void test_diag_accum_sqr()
00256     {
00257         VarianceTemplate of( NULL );
00258         test_eval_type( ObjectiveFunction::ACCUMULATE, DIAG, &of );
00259     }
00260     void test_diag_save_sqr()
00261     {
00262         VarianceTemplate of( NULL );
00263         test_eval_type( ObjectiveFunction::SAVE, DIAG, &of );
00264     }
00265     void test_diag_update_sqr()
00266     {
00267         VarianceTemplate of( NULL );
00268         test_eval_type( ObjectiveFunction::UPDATE, DIAG, &of );
00269     }
00270     void test_diag_temp_sqr()
00271     {
00272         VarianceTemplate of( NULL );
00273         test_eval_type( ObjectiveFunction::TEMPORARY, DIAG, &of );
00274     }
00275 
00276     void test_evaluate();
00277     void test_evaluate_sqr();
00278 
00279     void test_numerical_gradient()
00280     {
00281         StdDevTemplate of( NULL );
00282         compare_numerical_gradient( &of );
00283     }
00284     void test_numerical_gradient_sqr()
00285     {
00286         VarianceTemplate of( NULL );
00287         compare_numerical_gradient( &of );
00288     }
00289 
00290     void test_diagonal_gradient()
00291     {
00292         StdDevTemplate of( NULL );
00293         compare_diagonal_gradient( &of );
00294     }
00295     void test_diagonal_gradient_sqr()
00296     {
00297         VarianceTemplate of( NULL );
00298         compare_diagonal_gradient( &of );
00299     }
00300 
00301     void test_hessian_diag()
00302     {
00303         StdDevTemplate of( NULL );
00304         compare_numerical_hessian_diagonal( &of );
00305     }
00306     void test_hessian_diag_sqr()
00307     {
00308         VarianceTemplate of( NULL );
00309         compare_numerical_hessian_diagonal( &of );
00310     }
00311 
00312     void test_hessian_fails();
00313     void test_hessian_fails_sqr();
00314 
00315     void test_failed_metric_in_eval()
00316     {
00317         StdDevTemplate of( NULL );
00318         test_handles_qm_error( EVAL, &of );
00319     }
00320     void test_failed_metric_in_grad()
00321     {
00322         StdDevTemplate of( NULL );
00323         test_handles_qm_error( GRAD, &of );
00324     }
00325     void test_failed_metric_in_eval_sqr()
00326     {
00327         VarianceTemplate of( NULL );
00328         test_handles_qm_error( EVAL, &of );
00329     }
00330     void test_failed_metric_in_grad_sqr()
00331     {
00332         VarianceTemplate of( NULL );
00333         test_handles_qm_error( GRAD, &of );
00334     }
00335 
00336     void test_false_metric_in_eval()
00337     {
00338         StdDevTemplate of( NULL );
00339         test_handles_invalid_qm( EVAL, &of );
00340     }
00341     void test_false_metric_in_grad()
00342     {
00343         StdDevTemplate of( NULL );
00344         test_handles_invalid_qm( GRAD, &of );
00345     }
00346     void test_false_metric_in_eval_sqr()
00347     {
00348         VarianceTemplate of( NULL );
00349         test_handles_invalid_qm( EVAL, &of );
00350     }
00351     void test_false_metric_in_grad_sqr()
00352     {
00353         VarianceTemplate of( NULL );
00354         test_handles_invalid_qm( GRAD, &of );
00355     }
00356 
00357     void test_clone()
00358     {
00359         StdDevTemplate of( NULL );
00360         ObjectiveFunctionTests::test_clone( &of );
00361     }
00362     void test_clone_sqr()
00363     {
00364         VarianceTemplate of( NULL );
00365         ObjectiveFunctionTests::test_clone( &of );
00366     }
00367 
00368     void test_eval_negate()
00369     {
00370         StdDevTemplate of( NULL );
00371         test_negate_flag( EVAL, &of );
00372     }
00373     void test_eval_negate_sqr()
00374     {
00375         VarianceTemplate of( NULL );
00376         test_negate_flag( EVAL, &of );
00377     }
00378     void test_grad_negate()
00379     {
00380         StdDevTemplate of( NULL );
00381         test_negate_flag( GRAD, &of );
00382     }
00383     void test_grad_negate_sqr()
00384     {
00385         VarianceTemplate of( NULL );
00386         test_negate_flag( GRAD, &of );
00387     }
00388     void test_diag_negate()
00389     {
00390         StdDevTemplate of( NULL );
00391         test_negate_flag( DIAG, &of );
00392     }
00393     void test_diag_negate_sqr()
00394     {
00395         VarianceTemplate of( NULL );
00396         test_negate_flag( DIAG, &of );
00397     }
00398 };
00399 
00400 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StdDevTemplateTest, "StdDevTemplateTest" );
00401 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StdDevTemplateTest, "Unit" );
00402 
00403 static double std_dev_sqr( const double* array, unsigned len )
00404 {
00405     double sum = 0, sqr_sum = 0;
00406     for( size_t i = 0; i < len; ++i )
00407     {
00408         sum += array[i];
00409         sqr_sum += array[i] * array[i];
00410     }
00411 
00412     return sqr_sum / len - ( sum / len ) * ( sum / len );
00413 }
00414 
00415 void StdDevTemplateTest::test_evaluate()
00416 {
00417     StdDevTemplate OF( NULL );
00418 
00419     const double list1[] = { 1.0, 0.0, -5.0, 0.2, 6.0, 3 };
00420     const unsigned len1  = sizeof( list1 ) / sizeof( list1[0] );
00421     test_value( list1, len1, sqrt( std_dev_sqr( list1, len1 ) ), EVAL, &OF );
00422 
00423     const double list2[] = { 20, -30, 40, -50, 60, -70, 80, -90 };
00424     const unsigned len2  = sizeof( list2 ) / sizeof( list2[0] );
00425     test_value( list2, len2, sqrt( std_dev_sqr( list2, len2 ) ), EVAL, &OF );
00426 }
00427 
00428 void StdDevTemplateTest::test_evaluate_sqr()
00429 {
00430     VarianceTemplate OF( NULL );
00431 
00432     const double list1[] = { 1.0, 0.0, -5.0, 0.2, 6.0, 3 };
00433     const unsigned len1  = sizeof( list1 ) / sizeof( list1[0] );
00434     test_value( list1, len1, std_dev_sqr( list1, len1 ), EVAL, &OF );
00435 
00436     const double list2[] = { 20, -30, 40, -50, 60, -70, 80, -90 };
00437     const unsigned len2  = sizeof( list2 ) / sizeof( list2[0] );
00438     test_value( list2, len2, std_dev_sqr( list2, len2 ), EVAL, &OF );
00439 }
00440 
00441 void StdDevTemplateTest::test_hessian_fails()
00442 {
00443     MsqError err;
00444     double value;
00445     bool rval;
00446     vector< Vector3D > grad;
00447     MsqHessian Hess;
00448     Hess.initialize( patch(), err );
00449     CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
00450 
00451     OFTestQM metric( &value, 1 );
00452     StdDevTemplate func( &metric );
00453     rval = func.evaluate_with_Hessian( ObjectiveFunction::CALCULATE, patch(), value, grad, Hess, err );
00454     CPPUNIT_ASSERT( err );
00455 }
00456 
00457 void StdDevTemplateTest::test_hessian_fails_sqr()
00458 {
00459     MsqError err;
00460     double value;
00461     bool rval;
00462     vector< Vector3D > grad;
00463     MsqHessian Hess;
00464     Hess.initialize( patch(), err );
00465     CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );
00466 
00467     OFTestQM metric( &value, 1 );
00468     VarianceTemplate func( &metric );
00469     rval = func.evaluate_with_Hessian( ObjectiveFunction::CALCULATE, patch(), value, grad, Hess, err );
00470     CPPUNIT_ASSERT( err );
00471 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines