SundanceGaussLobatto1D.cpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                             Sundance
00005 //                 Copyright 2011 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
00041 
00042 #include "PlayaExceptions.hpp"
00043 #include "SundanceGaussLobatto1D.hpp"
00044 #ifdef _MSC_VER
00045 # include "winmath.h"
00046 #endif
00047 
00048 using namespace Sundance;
00049 using namespace Teuchos;
00050 
00051 GaussLobatto1D::GaussLobatto1D(int n) :
00052   nodes_(n), weights_(n)
00053 {
00054   computeWeights(n, -1.0, 1.0);
00055 }
00056 
00057 GaussLobatto1D::GaussLobatto1D(int n, double a, double b) :
00058   nodes_(n), weights_(n)
00059 {
00060   computeWeights(n, a, b);
00061 }
00062 
00063 void GaussLobatto1D::computeWeights(int n, double a, double b)
00064 {
00065 
00066   TEUCHOS_TEST_FOR_EXCEPTION(n < 2, std::runtime_error, "number of points=" << n
00067       << " must be at least 2 for Gauss-Lobatto-Legendre quadrature!");
00068 
00069   int m = (n + 1) / 2;
00070 
00071   double xMid = (b + a) / 2.0;
00072   double halfWidth = (b - a) / 2.0;
00073 
00074   for (int i = 0; i < m; i++)
00075   {
00076     // Initial guess (Gauss-Lobatto-Chebyshev roots)
00077     double z = cos(M_PI * i / (n - 1));
00078     double p1;
00079     double zOld;
00080     double tol = 1.0e-14;
00081     // Find the roots of L_(n-1)' (Newton's method)
00082     do
00083     {
00084       p1 = 1.0;
00085       double p2 = 0.0;
00086       for (int j = 1; j < n; j++)
00087       {
00088         double p3 = p2;
00089         p2 = p1;
00090         p1 = ((2.0 * j - 1.0) * z * p2 - (j - 1.0) * p3) / j;
00091       }
00092 
00093       zOld = z;
00094 
00095       // p1' == (n-1)*(z*p1-p2)/(z*z-1) (cf. Wolfram MathWorld)
00096       // p1'' == ((n-1)*n*p1-2*z*p1')/(z*z-1) (Legendre differential equation,
00097       // neglect last summand in numerator since p1' -> 0 and abs(z)<=1)
00098       // This results in following loop:
00099       z = zOld - (z * p1 - p2) / (n * p1);
00100     } while (fabs(z - zOld) > tol);
00101 
00102     if (i == 0)
00103     {
00104       nodes_[0] = a;
00105       nodes_[n - 1] = b;
00106     }
00107     else
00108     {
00109       nodes_[i] = xMid - halfWidth * z;
00110       nodes_[n - i - 1] = xMid + halfWidth * z;
00111     }
00112     weights_[i] = 2.0 * halfWidth / ((n - 1) * n * p1 * p1);
00113     weights_[n - i - 1] = weights_[i];
00114   }
00115 }
00116 
00117 bool GaussLobatto1D::unitTest()
00118 {
00119   std::cerr
00120       << "------------------ GaussLobatto1D unit test ----------------------"
00121       << std::endl;
00122 
00123   GaussLobatto1D q(20, 0.0, M_PI);
00124 
00125   double sum = 0.0;
00126   for (int i = 0; i < q.nPoints(); i++)
00127   {
00128     sum += q.weights()[i] * sin(q.nodes()[i]);
00129   }
00130   std::cerr << "integral of sin(x) over [0, pi] = " << sum << std::endl;
00131   double sumErr = fabs(sum - 2.0);
00132   bool sumPass = sumErr < 1.0e-10;
00133   std::cerr << "error = " << sumErr << std::endl;
00134   if (sumPass)
00135     std::cerr << "GaussLobatto1D sine test PASSED" << std::endl;
00136   else
00137     std::cerr << "GaussLobatto1D sine test FAILED" << std::endl;
00138   std::cerr
00139       << "------------------ End GaussLobatto1D unit test ----------------------"
00140       << std::endl;
00141   return sumPass;
00142 }
00143 

Site Contact