Intrepid
http://trilinos.sandia.gov/packages/docs/r10.12/packages/intrepid/test/Discretization/Integration/test_05.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ************************************************************************
00003 //
00004 //                           Intrepid Package
00005 //                 Copyright (2007) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
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 Pavel Bochev  (pbboche@sandia.gov)
00038 //                    Denis Ridzal  (dridzal@sandia.gov), or
00039 //                    Kara Peterson (kjpeter@sandia.gov)
00040 //
00041 // ************************************************************************
00042 // @HEADER
00043 
00044 
00051 #include "Intrepid_DefaultCubatureFactory.hpp"
00052 #include "Intrepid_Utils.hpp"
00053 #include "Teuchos_oblackholestream.hpp"
00054 #include "Teuchos_RCP.hpp"
00055 #include "Teuchos_BLAS.hpp"
00056 #include "Teuchos_GlobalMPISession.hpp"
00057 
00058 using namespace Intrepid;
00059 
00060 /*
00061   Monomial evaluation.
00062     in 1D, for point p(x)    : x^xDeg
00063     in 2D, for point p(x,y)  : x^xDeg * y^yDeg
00064     in 3D, for point p(x,y,z): x^xDeg * y^yDeg * z^zDeg
00065 */
00066 double computeMonomial(FieldContainer<double> & p, int xDeg, int yDeg=0, int zDeg=0) {
00067   double val = 1.0;
00068   int polydeg[3];
00069   polydeg[0] = xDeg; polydeg[1] = yDeg; polydeg[2] = zDeg;
00070   for (int i=0; i<p.dimension(0); i++) {
00071     val *= std::pow(p(i),polydeg[i]);
00072   }
00073   return val;
00074 }
00075 
00076 
00077 /*
00078   Computes integrals of monomials over a given reference cell.
00079 */
00080 double computeIntegral(shards::CellTopology & cellTopology, int cubDegree, int xDeg, int yDeg, int zDeg) {
00081 
00082   DefaultCubatureFactory<double>  cubFactory;                                         // create factory
00083   Teuchos::RCP<Cubature<double> > myCub = cubFactory.create(cellTopology, cubDegree); // create default cubature
00084 
00085   double val       = 0.0;
00086   int cubDim       = myCub->getDimension();
00087   int numCubPoints = myCub->getNumPoints();
00088 
00089   FieldContainer<double> point(cubDim);
00090   FieldContainer<double> cubPoints(numCubPoints, cubDim);
00091   FieldContainer<double> cubWeights(numCubPoints);
00092   FieldContainer<double> functValues(numCubPoints);
00093 
00094   myCub->getCubature(cubPoints, cubWeights);
00095 
00096   for (int i=0; i<numCubPoints; i++) {
00097     for (int j=0; j<cubDim; j++) {
00098       point(j) = cubPoints(i,j);
00099     }
00100     functValues(i) = computeMonomial(point, xDeg, yDeg, zDeg);
00101   }
00102 
00103   Teuchos::BLAS<int, double> myblas;
00104   int inc = 1;
00105   val = myblas.DOT(numCubPoints, &functValues[0], inc, &cubWeights[0], inc);
00106 
00107   return val;
00108 }
00109 
00110 
00111 
00112 int main(int argc, char *argv[]) {
00113 
00114   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00115 
00116   // This little trick lets us print to std::cout only if
00117   // a (dummy) command-line argument is provided.
00118   int iprint     = argc - 1;
00119   Teuchos::RCP<std::ostream> outStream;
00120   Teuchos::oblackholestream bhs; // outputs nothing
00121   if (iprint > 0)
00122     outStream = Teuchos::rcp(&std::cout, false);
00123   else
00124     outStream = Teuchos::rcp(&bhs, false);
00125 
00126   // Save the format state of the original std::cout.
00127   Teuchos::oblackholestream oldFormatState;
00128   oldFormatState.copyfmt(std::cout);
00129  
00130   *outStream \
00131   << "===============================================================================\n" \
00132   << "|                                                                             |\n" \
00133   << "|      Unit Test (CubatureDirect,CubatureTensor,DefaultCubatureFactory)       |\n" \
00134   << "|                                                                             |\n" \
00135   << "|     1) Computing integrals of monomials on reference cells in 3D            |\n" \
00136   << "|                         - using Level 1 BLAS -                              |\n" \
00137   << "|                                                                             |\n" \
00138   << "|  Questions? Contact  Pavel Bochev (pbboche@sandia.gov) or                   |\n" \
00139   << "|                      Denis Ridzal (dridzal@sandia.gov).                     |\n" \
00140   << "|                                                                             |\n" \
00141   << "|  Intrepid's website: http://trilinos.sandia.gov/packages/intrepid           |\n" \
00142   << "|  Trilinos website:   http://trilinos.sandia.gov                             |\n" \
00143   << "|                                                                             |\n" \
00144   << "===============================================================================\n"\
00145   << "| TEST 1: integrals of monomials in 3D (Level 1 BLAS version)                 |\n"\
00146   << "===============================================================================\n";
00147 
00148   // internal variables:
00149   int                                      errorFlag = 0;
00150   int                                      polyCt = 0;
00151   int                                      offset = 0;
00152   Teuchos::Array< Teuchos::Array<double> > testInt;
00153   Teuchos::Array< Teuchos::Array<double> > analyticInt;
00154   Teuchos::Array<double>                   tmparray(1);
00155   double                                   reltol = 1.0e+04 * INTREPID_TOL;
00156   int                                      maxDeg[3];
00157   int                                      maxOffset[3];
00158   int                                      numPoly[3];
00159   int                                      numAnalytic[3];
00160   // max polynomial degree tested, per cell type:
00161   maxDeg[0]                              = INTREPID_CUBATURE_TET_DEFAULT_MAX;
00162   maxDeg[1]                              = 20; // can be as large as INTREPID_CUBATURE_LINE_GAUSS_MAX, but runtime is excessive
00163   maxDeg[2]                              = INTREPID_CUBATURE_TRI_DEFAULT_MAX;
00164   // max polynomial degree recorded in analytic comparison files, per cell type:
00165   maxOffset[0]                           = INTREPID_CUBATURE_TET_DEFAULT_MAX;
00166   maxOffset[1]                           = INTREPID_CUBATURE_LINE_GAUSS_MAX;
00167   maxOffset[2]                           = INTREPID_CUBATURE_TRI_DEFAULT_MAX;
00168   for (int i=0; i<3; i++) {
00169     numPoly[i] = (maxDeg[i]+1)*(maxDeg[i]+2)*(maxDeg[i]+3)/6;
00170   }
00171   for (int i=0; i<3; i++) {
00172     numAnalytic[i] = (maxOffset[i]+1)*(maxOffset[i]+2)*(maxOffset[i]+3)/6;
00173   }
00174 
00175   // get names of files with analytic values
00176   std::string basedir = "./data";
00177   std::stringstream namestream[3];
00178   std::string filename[3];
00179   namestream[0] << basedir << "/TET_integrals" << ".dat";
00180   namestream[0] >> filename[0];
00181   namestream[1] << basedir << "/HEX_integrals" << ".dat";
00182   namestream[1] >> filename[1];
00183   namestream[2] << basedir << "/TRIPRISM_integrals" << ".dat";
00184   namestream[2] >> filename[2];
00185 
00186   // reference cells tested
00187   shards::CellTopology cellType[] = {shards::getCellTopologyData< shards::Tetrahedron<> >(),
00188                                      shards::getCellTopologyData< shards::Hexahedron<> >(),
00189                                      shards::getCellTopologyData< shards::Wedge<> >()};
00190   // format of data files with analytic values
00191   TypeOfExactData dataFormat[] = {INTREPID_UTILS_SCALAR, INTREPID_UTILS_FRACTION, INTREPID_UTILS_FRACTION};
00192 
00193   // compute and compare integrals
00194   try {
00195     for (int cellCt=0; cellCt < 3; cellCt++) {
00196       testInt.assign(numPoly[cellCt], tmparray);
00197       analyticInt.assign(numAnalytic[cellCt], tmparray);
00198 
00199       *outStream << "\nIntegrals of monomials on a reference " << cellType[cellCt].getBaseCellTopologyData()->name << ":\n";
00200       std::ifstream filecompare(&filename[cellCt][0]);
00201       // compute integrals
00202       for (int cubDeg=0; cubDeg <= maxDeg[cellCt]; cubDeg++) {
00203         polyCt = 0;
00204         testInt[cubDeg].resize((cubDeg+1)*(cubDeg+2)*(cubDeg+3)/6);
00205         for (int xDeg=0; xDeg <= cubDeg; xDeg++) {
00206           for (int yDeg=0; yDeg <= cubDeg-xDeg; yDeg++) {
00207             for (int zDeg=0; zDeg <= cubDeg-xDeg-yDeg; zDeg++) {
00208               testInt[cubDeg][polyCt] = computeIntegral(cellType[cellCt], cubDeg, xDeg, yDeg, zDeg);
00209               polyCt++; 
00210             }
00211           }
00212         }
00213       }
00214       // get analytic values
00215       if (filecompare.is_open()) {
00216         getAnalytic(analyticInt, filecompare, dataFormat[cellCt]);
00217         // close file
00218         filecompare.close();
00219       }
00220       // perform comparison
00221       for (int cubDeg=0; cubDeg <= maxDeg[cellCt]; cubDeg++) {
00222         polyCt = 0;
00223         offset = 0;
00224         int oldErrorFlag = errorFlag;
00225         for (int xDeg=0; xDeg <= cubDeg; xDeg++) {
00226           for (int yDeg=0; yDeg <= cubDeg-xDeg; yDeg++) {
00227             for (int zDeg=0; zDeg <= cubDeg-xDeg-yDeg; zDeg++) {
00228               double abstol = ( analyticInt[polyCt+offset][0] == 0.0 ? reltol : std::fabs(reltol*analyticInt[polyCt+offset][0]) );
00229               double absdiff = std::fabs(analyticInt[polyCt+offset][0] - testInt[cubDeg][polyCt]);
00230               if (absdiff > abstol) {
00231                 *outStream << "Cubature order " << std::setw(2) << std::left << cubDeg << " integrating "
00232                            << "x^" << std::setw(2) << std::left << xDeg << " * y^" << std::setw(2) << yDeg
00233                            << " * z^" << std::setw(2) << zDeg << ":" << "   "
00234                            << std::scientific << std::setprecision(16)
00235                            << testInt[cubDeg][polyCt] << "   " << analyticInt[polyCt+offset][0] << "   "
00236                            << std::setprecision(4) << absdiff << "   " << "<?" << "   " << abstol << "\n";
00237                 errorFlag++;
00238                 *outStream << std::right << std::setw(118) << "^^^^---FAILURE!\n";
00239               }
00240               polyCt++;
00241             }
00242             offset = offset + maxOffset[cellCt] - cubDeg;
00243           }
00244           offset = offset + (maxOffset[cellCt] - cubDeg)*(maxOffset[cellCt] - cubDeg + 1)/2;
00245         }
00246         *outStream << "Cubature order " << std::setw(2) << std::left << cubDeg;
00247         if (errorFlag == oldErrorFlag)
00248          *outStream << " passed.\n";
00249         else
00250          *outStream << " failed.\n";
00251       }
00252       *outStream << "\n";
00253     }  // end for cellCt
00254   }
00255   catch (std::logic_error err) {
00256     *outStream << err.what() << "\n";
00257     errorFlag = -1;
00258   };
00259 
00260 
00261   if (errorFlag != 0)
00262     std::cout << "End Result: TEST FAILED\n";
00263   else
00264     std::cout << "End Result: TEST PASSED\n";
00265 
00266   // reset format state of std::cout
00267   std::cout.copyfmt(oldFormatState);
00268 
00269   return errorFlag;
00270 }