|
Intrepid
|
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[4]; 00157 int maxOffset[4]; 00158 int numPoly[4]; 00159 int numAnalytic[4]; 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] = std::min(INTREPID_CUBATURE_LINE_GAUSS_MAX, INTREPID_CUBATURE_TRI_DEFAULT_MAX); 00164 maxDeg[3] = std::min(INTREPID_CUBATURE_LINE_GAUSS_MAX, INTREPID_CUBATURE_LINE_GAUSSJACOBI20_MAX); 00165 // max polynomial degree recorded in analytic comparison files, per cell type: 00166 maxOffset[0] = INTREPID_CUBATURE_TET_DEFAULT_MAX; 00167 maxOffset[1] = INTREPID_CUBATURE_LINE_GAUSS_MAX; 00168 maxOffset[2] = std::min(INTREPID_CUBATURE_LINE_GAUSS_MAX, INTREPID_CUBATURE_TRI_DEFAULT_MAX); 00169 maxOffset[3] = std::min(INTREPID_CUBATURE_LINE_GAUSS_MAX, INTREPID_CUBATURE_LINE_GAUSSJACOBI20_MAX); 00170 for (int i=0; i<4; i++) { 00171 numPoly[i] = (maxDeg[i]+1)*(maxDeg[i]+2)*(maxDeg[i]+3)/6; 00172 } 00173 for (int i=0; i<4; i++) { 00174 numAnalytic[i] = (maxOffset[i]+1)*(maxOffset[i]+2)*(maxOffset[i]+3)/6; 00175 } 00176 00177 // get names of files with analytic values 00178 std::string basedir = "./data"; 00179 std::stringstream namestream[4]; 00180 std::string filename[4]; 00181 namestream[0] << basedir << "/TET_integrals" << ".dat"; 00182 namestream[0] >> filename[0]; 00183 namestream[1] << basedir << "/HEX_integrals" << ".dat"; 00184 namestream[1] >> filename[1]; 00185 namestream[2] << basedir << "/TRIPRISM_integrals" << ".dat"; 00186 namestream[2] >> filename[2]; 00187 namestream[3] << basedir << "/PYR_integrals" << ".dat"; 00188 namestream[3] >> filename[3]; 00189 00190 // reference cells tested 00191 shards::CellTopology cellType[] = {shards::getCellTopologyData< shards::Tetrahedron<> >(), 00192 shards::getCellTopologyData< shards::Hexahedron<> >(), 00193 shards::getCellTopologyData< shards::Wedge<> >(), 00194 shards::getCellTopologyData< shards::Pyramid<> >() }; 00195 // format of data files with analytic values 00196 TypeOfExactData dataFormat[] = {INTREPID_UTILS_SCALAR, INTREPID_UTILS_FRACTION, INTREPID_UTILS_FRACTION, INTREPID_UTILS_FRACTION}; 00197 00198 // compute and compare integrals 00199 try { 00200 for (int cellCt=0; cellCt < 4; cellCt++) { 00201 testInt.assign(numPoly[cellCt], tmparray); 00202 analyticInt.assign(numAnalytic[cellCt], tmparray); 00203 00204 *outStream << "\nIntegrals of monomials on a reference " << cellType[cellCt].getBaseCellTopologyData()->name << ":\n"; 00205 std::ifstream filecompare(&filename[cellCt][0]); 00206 // compute integrals 00207 for (int cubDeg=0; cubDeg <= maxDeg[cellCt]; cubDeg++) { 00208 polyCt = 0; 00209 testInt[cubDeg].resize((cubDeg+1)*(cubDeg+2)*(cubDeg+3)/6); 00210 for (int xDeg=0; xDeg <= cubDeg; xDeg++) { 00211 for (int yDeg=0; yDeg <= cubDeg-xDeg; yDeg++) { 00212 for (int zDeg=0; zDeg <= cubDeg-xDeg-yDeg; zDeg++) { 00213 testInt[cubDeg][polyCt] = computeIntegral(cellType[cellCt], cubDeg, xDeg, yDeg, zDeg); 00214 polyCt++; 00215 } 00216 } 00217 } 00218 } 00219 // get analytic values 00220 if (filecompare.is_open()) { 00221 getAnalytic(analyticInt, filecompare, dataFormat[cellCt]); 00222 // close file 00223 filecompare.close(); 00224 } 00225 // perform comparison 00226 for (int cubDeg=0; cubDeg <= maxDeg[cellCt]; cubDeg++) { 00227 polyCt = 0; 00228 offset = 0; 00229 int oldErrorFlag = errorFlag; 00230 for (int xDeg=0; xDeg <= cubDeg; xDeg++) { 00231 for (int yDeg=0; yDeg <= cubDeg-xDeg; yDeg++) { 00232 for (int zDeg=0; zDeg <= cubDeg-xDeg-yDeg; zDeg++) { 00233 double abstol = ( analyticInt[polyCt+offset][0] == 0.0 ? reltol : std::fabs(reltol*analyticInt[polyCt+offset][0]) ); 00234 double absdiff = std::fabs(analyticInt[polyCt+offset][0] - testInt[cubDeg][polyCt]); 00235 00236 *outStream << "Cubature order " << std::setw(2) << std::left << cubDeg << " integrating " 00237 << "x^" << std::setw(2) << std::left << xDeg << " * y^" << std::setw(2) << yDeg 00238 << " * z^" << std::setw(2) << zDeg << ":" << " " 00239 << std::scientific << std::setprecision(16) 00240 << testInt[cubDeg][polyCt] << " " << analyticInt[polyCt+offset][0] << " " 00241 << std::setprecision(4) << absdiff << " " << "<?" << " " << abstol << "\n"; 00242 if (absdiff > abstol) { 00243 errorFlag++; 00244 *outStream << std::right << std::setw(118) << "^^^^---FAILURE!\n"; 00245 } 00246 polyCt++; 00247 } 00248 offset = offset + maxOffset[cellCt] - cubDeg; 00249 } 00250 offset = offset + (maxOffset[cellCt] - cubDeg)*(maxOffset[cellCt] - cubDeg + 1)/2; 00251 } 00252 *outStream << "Cubature order " << std::setw(2) << std::left << cubDeg; 00253 if (errorFlag == oldErrorFlag) 00254 *outStream << " passed.\n"; 00255 else 00256 *outStream << " failed.\n"; 00257 } 00258 *outStream << "\n"; 00259 } // end for cellCt 00260 } 00261 catch (std::logic_error err) { 00262 *outStream << err.what() << "\n"; 00263 errorFlag = -1; 00264 }; 00265 00266 00267 if (errorFlag != 0) 00268 std::cout << "End Result: TEST FAILED\n"; 00269 else 00270 std::cout << "End Result: TEST PASSED\n"; 00271 00272 // reset format state of std::cout 00273 std::cout.copyfmt(oldFormatState); 00274 00275 return errorFlag; 00276 }
1.7.6.1