pce_example.cpp

Go to the documentation of this file.
00001 // $Id: pce_example.cpp,v 1.7 2008/08/04 17:23:35 etphipp Exp $ 
00002 // $Source: /space/CVS/Trilinos/packages/sacado/example/pce_example.cpp,v $ 
00003 // @HEADER
00004 // ***********************************************************************
00005 // 
00006 //                           Sacado Package
00007 //                 Copyright (2006) Sandia Corporation
00008 // 
00009 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00010 // the U.S. Government retains certain rights in this software.
00011 // 
00012 // This library is free software; you can redistribute it and/or modify
00013 // it under the terms of the GNU Lesser General Public License as
00014 // published by the Free Software Foundation; either version 2.1 of the
00015 // License, or (at your option) any later version.
00016 //  
00017 // This library is distributed in the hope that it will be useful, but
00018 // WITHOUT ANY WARRANTY; without even the implied warranty of
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 // Lesser General Public License for more details.
00021 //  
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License along with this library; if not, write to the Free Software
00024 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00025 // USA
00026 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
00027 // (etphipp@sandia.gov).
00028 // 
00029 // ***********************************************************************
00030 // @HEADER
00031 
00032 // pce_example
00033 //
00034 //  usage: 
00035 //     pce_example
00036 //
00037 //  output:  
00038 //     prints the Hermite Polynomial Chaos Expansion of a simple function
00039 //     using the class Sacado::PCE::Hermite
00040 
00041 #include <iostream>
00042 #include <iomanip>
00043 #include <math.h>
00044 
00045 #include "Sacado.hpp"
00046 #include "Sacado_PCE_Hermite.hpp"
00047 
00048 #ifdef HAVE_SACADO_STOKHOS
00049 #include "Sacado_PCE_OrthogPoly.hpp"
00050 #include "Stokhos_HermiteEBasis.hpp"
00051 #endif
00052 
00053 // The function to differentiate
00054 template <typename ScalarT>
00055 ScalarT func(const ScalarT& a) {
00056   ScalarT r = std::exp(a);
00057 
00058   return r;
00059 }
00060 
00061 int main(int argc, char **argv)
00062 {
00063   try {
00064     const unsigned int d = 7;
00065     Sacado::PCE::Hermite<double>::initWorkspace(d);
00066     Sacado::PCE::Hermite<double> u(d);
00067     u.fastAccessCoeff(0) = 1.0;
00068     u.fastAccessCoeff(1) = 0.4;
00069     u.fastAccessCoeff(2) = 0.06;
00070     u.fastAccessCoeff(3) = 0.002;
00071 
00072     Sacado::PCE::Hermite<double> v = std::log(u);
00073     //Sacado::PCE::Hermite<double> v = std::sinh(1.0/(w*w + 1.0));
00074 
00075     std::cout.precision(12);
00076     std::cout << "u (hermite basis) = " << u << std::endl;
00077     std::cout << "u (standard basis) = " << u.toStandardBasis() << std::endl;
00078 
00079     std::cout << "v (hermite basis) = " << v << std::endl;
00080     std::cout << "v (standard basis) = " << v.toStandardBasis() << std::endl;
00081 
00082     Sacado::PCE::StandardPoly<double> us = u.toStandardBasis();
00083     Sacado::Tay::Taylor<double> ut(d);
00084     for (unsigned int i=0; i<=d; i++)
00085       ut.fastAccessCoeff(i) = us.coeff(i);
00086 
00087     Sacado::Tay::Taylor<double> vt = std::log(ut);
00088     //Sacado::Tay::Taylor<double> vt = std::sinh(1.0/(wt*wt + 1.0));
00089 
00090     std::cout.precision(12);
00091     std::cout << "u (taylor basis) = " << ut << std::endl;
00092     std::cout << "v (taylor basis) = " << vt << std::endl;
00093 
00094 #ifdef HAVE_SACADO_STOKHOS
00095     typedef Stokhos::HermiteEBasis<double> basis_type;
00096     typedef Sacado::PCE::OrthogPoly<double>::expansion_type expansion_type;
00097     Teuchos::RCP<basis_type> basis = Teuchos::rcp(new basis_type(d));
00098     Teuchos::RCP<expansion_type> expansion = 
00099       Teuchos::rcp(new expansion_type(basis));
00100     Sacado::PCE::OrthogPoly<double>::initExpansion(expansion);
00101     Sacado::PCE::OrthogPoly<double> ue(d+1);
00102     for (unsigned int i=0; i<=d; i++)
00103       ue.fastAccessCoeff(i) = u.coeff(i);
00104 
00105     Sacado::PCE::OrthogPoly<double> ve = std::log(ue);
00106     //Sacado::PCE::OrthogPoly<double> ve = std::sinh(1.0/(we*we + 1.0));
00107 
00108     std::cout << "ue (hermite basis) = " << ue << std::endl;
00109     std::cout << "ue (standard basis) = " << ue.toStandardBasis() << std::endl;
00110     std::cout << "ve (hermite basis) = " << ve << std::endl;
00111     std::cout << "ve (standard basis) = " << ve.toStandardBasis() << std::endl;
00112 #endif
00113 
00114     std::cout << "\nExample passed!" << std::endl;
00115   }
00116   catch (std::exception& e) {
00117     std::cout << e.what() << std::endl;
00118   }
00119 }

Generated on Wed May 12 21:59:04 2010 for Sacado Package Browser (Single Doxygen Collection) by  doxygen 1.4.7