Thyra Package Browser (Single Doxygen Collection) Version of the Day
DiagonalQuadraticResponseOnlyModelEvaluator_UnitTests.cpp
Go to the documentation of this file.
00001 #include "Thyra_DiagonalQuadraticResponseOnlyModelEvaluator.hpp"
00002 #include "Thyra_DefaultSpmdVectorSpace.hpp"
00003 #include "Thyra_ModelEvaluatorHelpers.hpp"
00004 #include "Thyra_VectorStdOps.hpp"
00005 #include "Thyra_TestingTools.hpp"
00006 #include "Teuchos_UnitTestHarness.hpp"
00007 #include "Teuchos_DefaultComm.hpp"
00008 
00009 
00010 namespace {
00011 
00012 
00013 //
00014 // Helper code and declarations
00015 //
00016 
00017 
00018 using Teuchos::as;
00019 using Teuchos::null;
00020 using Teuchos::RCP;
00021 using Thyra::createMember;
00022 
00023 
00024 int g_localDim = 4;
00025 
00026 double g_tol_scale = 10.0;
00027 
00028 
00029 TEUCHOS_STATIC_SETUP()
00030 {
00031   Teuchos::UnitTestRepository::getCLP().setOption(
00032     "local-dim", &g_localDim, "Number of local vector elements on each process" );
00033   Teuchos::UnitTestRepository::getCLP().setOption(
00034     "tol-scale", &g_tol_scale, "Floating point tolerance scaling" );
00035 }
00036 
00037 
00038 template<class Scalar>
00039 inline Scalar sqr(const Scalar &x) { return x*x; }
00040 
00041 
00042 template<class Scalar>
00043 inline Scalar cube(const Scalar &x) { return x*x*x; }
00044 
00045 
00046 //
00047 // Unit tests
00048 //
00049 
00050 
00051 //
00052 // Test basic construction 
00053 //
00054 
00055 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( DiagonalQuadraticResponseOnlyModelEvaluator,
00056   basic, Scalar )
00057 {
00058 
00059   typedef Teuchos::ScalarTraits<Scalar> ST;
00060   typedef typename ST::magnitudeType ScalarMag;
00061   typedef Thyra::Ordinal Ordinal;
00062   typedef Thyra::ModelEvaluatorBase MEB;
00063   using Thyra::derivativeGradient;
00064   using Thyra::create_DgDp_mv;
00065   using Thyra::eval_g_DgDp;
00066   using Thyra::get_mv;
00067   using Thyra::get_ele;
00068   using Thyra::norm_2;
00069 
00070   RCP<const Thyra::ModelEvaluator<Scalar> >
00071     model = Thyra::diagonalQuadraticResponseOnlyModelEvaluator<Scalar>(g_localDim);
00072   
00073   TEST_ASSERT(!is_null(model));
00074   TEST_EQUALITY_CONST(model->Np(), 1);
00075   TEST_EQUALITY_CONST(model->Ng(), 1);
00076 
00077   RCP<const Thyra::VectorSpaceBase<Scalar> > p_space = model->get_p_space(0);
00078   RCP<const Thyra::VectorSpaceBase<Scalar> > g_space = model->get_g_space(0);
00079 
00080   RCP<Thyra::VectorBase<Scalar> > p_init = createMember(p_space);
00081   const Scalar val = as<Scalar>(2.0);
00082   out << "\nval = " << val << "\n";
00083   Thyra::V_S(p_init.ptr(), val);
00084 
00085   RCP<Thyra::VectorBase<Scalar> >
00086     g = createMember(g_space),
00087     g_grad = createMember(p_space);
00088 
00089   eval_g_DgDp<Scalar>(*model, 0, *p_init, 0,
00090     g.ptr(),derivativeGradient<Scalar>(g_grad) );
00091 
00092   out << "\ng =\n" << *g;
00093   out << "\ng_grad =\n" << *g_grad;
00094 
00095   const Ordinal globalDim = p_space->dim();
00096   out << "\nglobalDim = " << globalDim << "\n";
00097   
00098   TEST_FLOATING_EQUALITY( get_ele<Scalar>(*g, 0),
00099     as<Scalar>(0.5*globalDim)*val*val, as<ScalarMag>(g_tol_scale*ST::eps()/globalDim));
00100   
00101   TEST_FLOATING_EQUALITY(
00102     norm_2<Scalar>(*g_grad),
00103     ST::magnitude(ST::squareroot(as<Scalar>(globalDim)*val*val)),
00104     as<ScalarMag>(g_tol_scale*ST::eps()/globalDim));
00105   
00106 }
00107 
00108 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_REAL_SCALAR_TYPES(
00109   DiagonalQuadraticResponseOnlyModelEvaluator, basic )
00110 
00111 
00112 //
00113 // Test with all of knobs setup
00114 //
00115 
00116 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( DiagonalQuadraticResponseOnlyModelEvaluator,
00117   offsets, Scalar )
00118 {
00119 
00120   typedef Teuchos::ScalarTraits<Scalar> ST;
00121   typedef typename ST::magnitudeType ScalarMag;
00122   typedef Thyra::Ordinal Ordinal;
00123   typedef Thyra::ModelEvaluatorBase MEB;
00124   using Thyra::VectorSpaceBase;
00125   using Thyra::VectorBase;
00126   using Thyra::derivativeGradient;
00127   using Thyra::eval_g_DgDp;
00128   using Thyra::get_mv;
00129   using Thyra::get_ele;
00130   using Thyra::norm_2;
00131   using Thyra::V_S;
00132 
00133   const RCP<Thyra::DiagonalQuadraticResponseOnlyModelEvaluator<Scalar> >
00134     model = Thyra::diagonalQuadraticResponseOnlyModelEvaluator<Scalar>(g_localDim);
00135 
00136   const RCP<const VectorSpaceBase<Scalar> > p_space = model->get_p_space(0);
00137   const RCP<const VectorSpaceBase<Scalar> > g_space = model->get_g_space(0);
00138 
00139   const Scalar p_soln_val = as<Scalar>(3.0);
00140   {
00141     const RCP<VectorBase<Scalar> > p_soln = createMember(p_space);
00142     V_S(p_soln.ptr(), p_soln_val);
00143     model->setSolutionVector(p_soln);
00144   }
00145 
00146   const Scalar diag_val = as<Scalar>(4.0);
00147   {
00148     const RCP<VectorBase<Scalar> > diag = createMember(p_space);
00149     V_S(diag.ptr(), diag_val);
00150     model->setDiagonalVector(diag);
00151   }
00152 
00153   const Scalar g_offset = as<Scalar>(5.0);
00154   {
00155     model->setScalarOffset(g_offset);
00156   }
00157 
00158   const Scalar nonlinearTermFactor = 1e-3;
00159   {
00160     model->setNonlinearTermFactor(nonlinearTermFactor);
00161   }
00162 
00163   const Scalar p_val = as<Scalar>(2.0);
00164   const RCP<VectorBase<Scalar> > p_init = createMember(p_space);
00165   V_S(p_init.ptr(), p_val);
00166   
00167   RCP<VectorBase<Scalar> >
00168     g = createMember(g_space),
00169     g_grad = createMember(p_space);
00170 
00171   eval_g_DgDp<Scalar>(*model, 0, *p_init, 0,
00172     g.ptr(),derivativeGradient<Scalar>(g_grad) );
00173 
00174   out << "\ng =\n" << *g;
00175   out << "\ng_grad =\n" << *g_grad;
00176 
00177   const Ordinal globalDim = p_space->dim();
00178   out << "\nglobalDim = " << globalDim << "\n";
00179   
00180   TEST_FLOATING_EQUALITY(
00181     get_ele<Scalar>(*g, 0),
00182     as<Scalar>(
00183       0.5 * globalDim
00184       * (diag_val * sqr(p_val - p_soln_val)
00185         + nonlinearTermFactor * cube(p_val - p_soln_val)
00186         )
00187       + g_offset
00188       ),
00189     as<ScalarMag>(g_tol_scale*ST::eps()/globalDim)
00190     );
00191   
00192   TEST_FLOATING_EQUALITY(
00193     sum(*g_grad),
00194     as<Scalar>(
00195       globalDim
00196       * ( diag_val * (p_val - p_soln_val)
00197         + 1.5 * nonlinearTermFactor * sqr(p_val - p_soln_val) )
00198       ),
00199     as<ScalarMag>(g_tol_scale*ST::eps()/globalDim)
00200     );
00201   
00202 }
00203 
00204 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_REAL_SCALAR_TYPES(
00205   DiagonalQuadraticResponseOnlyModelEvaluator, offsets )
00206 
00207 
00208 //
00209 // Test non-Euclidean scalar product
00210 //
00211 
00212 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( DiagonalQuadraticResponseOnlyModelEvaluator,
00213   nonEuclideanScalarProd, Scalar )
00214 {
00215 
00216   typedef Teuchos::ScalarTraits<Scalar> ST;
00217   typedef typename ST::magnitudeType ScalarMag;
00218   typedef Thyra::Ordinal Ordinal;
00219   typedef Thyra::ModelEvaluatorBase MEB;
00220   using Thyra::derivativeGradient;
00221   using Thyra::VectorSpaceBase;
00222   using Thyra::VectorBase;
00223   using Thyra::scalarProd;
00224   using Thyra::eval_g_DgDp;
00225   using Thyra::get_mv;
00226   using Thyra::get_ele;
00227   using Thyra::norm_2;
00228   using Thyra::V_S;
00229 
00230   const RCP<Thyra::DiagonalQuadraticResponseOnlyModelEvaluator<Scalar> >
00231     model = Thyra::diagonalQuadraticResponseOnlyModelEvaluator<Scalar>(g_localDim);
00232 
00233   const RCP<const VectorSpaceBase<Scalar> > p_space = model->get_p_space(0);
00234   const RCP<const VectorSpaceBase<Scalar> > g_space = model->get_g_space(0);
00235 
00236   const Scalar diag_val = as<Scalar>(4.0);
00237   {
00238     const RCP<VectorBase<Scalar> > diag = createMember(p_space);
00239     V_S(diag.ptr(), diag_val);
00240     model->setDiagonalVector(diag);
00241   }
00242 
00243   const Scalar diag_bar_val = as<Scalar>(3.0);
00244   {
00245     const RCP<VectorBase<Scalar> > diag_bar = createMember(p_space);
00246     V_S(diag_bar.ptr(), diag_bar_val);
00247     model->setDiagonalBarVector(diag_bar);
00248   }
00249 
00250   const Scalar g_offset = as<Scalar>(5.0);
00251   {
00252     model->setScalarOffset(g_offset);
00253   }
00254 
00255   const Scalar nonlinearTermFactor = 1e-3;
00256   {
00257     model->setNonlinearTermFactor(nonlinearTermFactor);
00258   }
00259 
00260   const Scalar x_val = as<Scalar>(5.0);
00261   const RCP<VectorBase<Scalar> > x = createMember(p_space);
00262   V_S(x.ptr(), x_val);
00263 
00264   const Scalar y_val = as<Scalar>(6.0);
00265   const RCP<VectorBase<Scalar> > y = createMember(p_space);
00266   V_S(y.ptr(), y_val);
00267 
00268   const Ordinal globalDim = p_space->dim();
00269   out << "\nglobalDim = " << globalDim << "\n";
00270   
00271   TEST_FLOATING_EQUALITY(
00272     scalarProd(*x, *y),
00273     as<Scalar>( globalDim * (diag_val / diag_bar_val) * x_val * y_val ),
00274     as<ScalarMag>(g_tol_scale*ST::eps()/globalDim)
00275     );
00276 
00277   const Scalar p_soln_val = as<Scalar>(3.0);
00278   {
00279     const RCP<VectorBase<Scalar> > p_soln = createMember(p_space);
00280     V_S(p_soln.ptr(), p_soln_val);
00281     model->setSolutionVector(p_soln);
00282   }
00283 
00284   const Scalar p_val = as<Scalar>(2.0);
00285   const RCP<VectorBase<Scalar> > p_init = createMember(p_space);
00286   V_S(p_init.ptr(), p_val);
00287   
00288   RCP<VectorBase<Scalar> >
00289     g = createMember(g_space),
00290     g_grad = createMember(p_space);
00291 
00292   eval_g_DgDp<Scalar>(*model, 0, *p_init, 0,
00293     g.ptr(),derivativeGradient<Scalar>(g_grad) );
00294 
00295   out << "\ng =\n" << *g;
00296   out << "\ng_grad =\n" << *g_grad;
00297 
00298   eval_g_DgDp<Scalar>(*model, 0, *p_init, 0,
00299     g.ptr(),derivativeGradient<Scalar>(g_grad) );
00300   
00301   TEST_FLOATING_EQUALITY(
00302     get_ele<Scalar>(*g, 0),
00303     as<Scalar>(
00304       0.5 * globalDim
00305       * (diag_val * sqr(p_val - p_soln_val)
00306         + nonlinearTermFactor * cube(p_val - p_soln_val)
00307         )
00308       + g_offset
00309       ),
00310     as<ScalarMag>(g_tol_scale*ST::eps()/globalDim)
00311     );
00312   
00313   TEST_FLOATING_EQUALITY(
00314     sum(*g_grad),
00315     as<Scalar>(
00316       globalDim
00317       * (diag_bar_val / diag_val) * ( diag_val * (p_val - p_soln_val)
00318         + 1.5 * nonlinearTermFactor * sqr(p_val - p_soln_val) )
00319       ),
00320     as<ScalarMag>(g_tol_scale*ST::eps()/globalDim)
00321     );
00322   
00323 }
00324 
00325 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_REAL_SCALAR_TYPES(
00326   DiagonalQuadraticResponseOnlyModelEvaluator, nonEuclideanScalarProd )
00327 
00328 
00329 
00330 
00331 
00332 
00333 
00334 } // namespace
00335 
00336 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines