GlobiPack Package Browser (Single Doxygen Collection) Version of the Day
TestLagrPolyMeritFunc1D_UnitTests.cpp
Go to the documentation of this file.
00001 /*
00002 // @HEADER
00003 // ***********************************************************************
00004 // 
00005 //    GlobiPack: Collection of Scalar 1D globalizaton utilities
00006 //                 Copyright (2009) Sandia Corporation
00007 // 
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 // 
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 // 
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 // 
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA
00025 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00026 // 
00027 // ***********************************************************************
00028 // @HEADER
00029 */
00030 
00031 
00032 #include "GlobiPack_TestLagrPolyMeritFunc1D.hpp"
00033 #include "Teuchos_UnitTestHarness.hpp"
00034 #include "Teuchos_Tuple.hpp"
00035 
00036 
00037 namespace {
00038 
00039 
00040 //
00041 // Helper code and declarations
00042 //
00043 
00044 
00045 using Teuchos::as;
00046 using Teuchos::outArg;
00047 using Teuchos::null;
00048 using Teuchos::RCP;
00049 using Teuchos::Array;
00050 using Teuchos::tuple;
00051 using GlobiPack::TestLagrPolyMeritFunc1D;
00052 using GlobiPack::computeValue;
00053 
00054 
00055 template<class Scalar>
00056 inline Scalar sqr(const Scalar &x) { return x*x; }
00057 
00058 
00059 double g_tol = Teuchos::ScalarTraits<double>::eps()*100.0;
00060 
00061 
00062 TEUCHOS_STATIC_SETUP()
00063 {
00064   Teuchos::UnitTestRepository::getCLP().setOption(
00065     "tol", &g_tol, "Floating point tolerance" );
00066 }
00067 
00068 
00069 //
00070 // Unit tests
00071 //
00072 
00073 
00074 //
00075 // Here we represent the simple quadratic merit function:
00076 //
00077 //   phi(alpha) = 0.5 * (alpha - 2.0)^2 + 2.0
00078 //
00079 // with the derivative:
00080 //
00081 //   Dphi(alpha) = alpha - 2.0
00082 //
00083 
00084 template<class Scalar>
00085 Scalar phi_quad1(const Scalar &alpha)
00086 {
00087   return as<Scalar>(0.5)*sqr(alpha-as<Scalar>(2.0)) + as<Scalar>(2.0);
00088 }
00089 
00090 template<class Scalar>
00091 Scalar Dphi_quad1(const Scalar &alpha)
00092 {
00093   return alpha-as<Scalar>(2.0);
00094 }
00095 
00096 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( TestLagrPolyMeritFunc1D, basic, Scalar )
00097 {
00098   
00099   typedef Teuchos::ScalarTraits<Scalar> ST;
00100   
00101   ECHO(Array<Scalar> alphaPoints = tuple<Scalar>(0.0, 2.0, 4.0));
00102   ECHO(Array<Scalar> phiPoints = tuple<Scalar>(4.0, 2.0, 4.0));
00103   ECHO(TestLagrPolyMeritFunc1D<Scalar> meritFunc(alphaPoints, phiPoints));
00104   TEST_ASSERT(meritFunc.supportsDerivEvals());
00105 
00106   Array<Scalar> alphaTestPoints = tuple<Scalar>(0.0, 1.0, 2.0, 3.0, 4.0);
00107   for (int test_i = 0; test_i < as<int>(alphaTestPoints.size()); ++test_i) {
00108     out << "\ntest_i="<<test_i<<"\n\n";
00109     Teuchos::OSTab tab(out);
00110     ECHO(const Scalar alpha = alphaTestPoints[test_i]);
00111     out << "alpha="<<alpha<<"\n";
00112     ECHO(Scalar phi = as<Scalar>(-1.0));
00113     ECHO(Scalar Dphi = as<Scalar>(-1.0));
00114     ECHO(meritFunc.eval(alpha, outArg(phi), outArg(Dphi)));
00115     TEST_FLOATING_EQUALITY(phi, phi_quad1(alpha), g_tol);
00116     TEST_FLOATING_EQUALITY(Dphi, Dphi_quad1(alpha), g_tol);
00117   }
00118 }
00119 
00120 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_REAL_SCALAR_TYPES( TestLagrPolyMeritFunc1D, basic )
00121 
00122 
00123 } // namespace
00124 
00125 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends