trad_example.cpp

Go to the documentation of this file.
00001 // $Id$ 
00002 // $Source$ 
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 // trad_example
00033 //
00034 //  usage: 
00035 //     trad_example
00036 //
00037 //  output:  
00038 //     prints the results of differentiating a simple function with reverse
00039 //     mode AD using the Sacado::Rad::ADvar class.
00040 
00041 #include <iostream>
00042 #include <iomanip>
00043 
00044 #include "Sacado.hpp"
00045 
00046 // The function to differentiate
00047 template <typename ScalarT>
00048 ScalarT func(const ScalarT& a, const ScalarT& b, const ScalarT& c) {
00049   ScalarT r = c*std::log(b+1.)/std::sin(a);
00050 
00051   return r;
00052 }
00053 
00054 // The analytic derivative of func(a,b,c) with respect to a and b
00055 void func_deriv(double a, double b, double c, double& drda, double& drdb)
00056 {
00057   drda = -(c*std::log(b+1.)/std::pow(std::sin(a),2))*std::cos(a);
00058   drdb = c / ((b+1.)*std::sin(a));
00059 }
00060 
00061 int main(int argc, char **argv)
00062 {
00063   double pi = std::atan(1.0)*4.0;
00064 
00065   // Values of function arguments
00066   double a = pi/4;
00067   double b = 2.0;
00068   double c = 3.0;
00069 
00070   // Rad objects
00071   Sacado::Rad::ADvar<double> arad = a; 
00072   Sacado::Rad::ADvar<double> brad = b; 
00073   Sacado::Rad::ADvar<double> crad = c;              // Passive variable
00074   Sacado::Rad::ADvar<double> rrad;                  // Result
00075 
00076   // Compute function
00077   double r = func(a, b, c);
00078 
00079   // Compute derivative analytically
00080   double drda, drdb;
00081   func_deriv(a, b, c, drda, drdb);
00082 
00083   // Compute function and derivative with AD
00084   rrad = func(arad, brad, crad);
00085 
00086   Sacado::Rad::ADvar<double>::Gradcomp();
00087 
00088   // Extract value and derivatives
00089   double r_ad = rrad.val();     // r
00090   double drda_ad = arad.adj();  // dr/da
00091   double drdb_ad = brad.adj();  // dr/db
00092 
00093   // Print the results
00094   int p = 4;
00095   int w = p+7;
00096   std::cout.setf(std::ios::scientific);
00097   std::cout.precision(p);
00098   std::cout << "    r =  " << r << " (original) == " << std::setw(w) << r_ad
00099       << " (AD) Error = " << std::setw(w) << r - r_ad << std::endl
00100       << "dr/da = " << std::setw(w) << drda << " (analytic) == " 
00101       << std::setw(w) << drda_ad << " (AD) Error = " << std::setw(w) 
00102       << drda - drda_ad << std::endl
00103       << "dr/db = " << std::setw(w) << drdb << " (analytic) == " 
00104       << std::setw(w) << drdb_ad << " (AD) Error = " << std::setw(w) 
00105       << drdb - drdb_ad << std::endl;
00106 
00107   double tol = 1.0e-14;
00108   
00109   if (std::fabs(r - r_ad)       < tol &&
00110       std::fabs(drda - drda_ad) < tol &&
00111       std::fabs(drdb - drdb_ad) < tol) {
00112     std::cout << "\nExample passed!" << std::endl;
00113     return 0;
00114   }
00115   else {
00116     std::cout <<"\nSomething is wrong, example failed!" << std::endl;
00117     return 1;
00118   }
00119 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 10:19:37 2011 for Sacado Package Browser (Single Doxygen Collection) by  doxygen 1.6.3