Intrepid
http://trilinos.sandia.gov/packages/docs/r10.10/packages/intrepid/test/Discretization/Basis/HGRAD_HEX_Cn_FEM/test_01.cpp
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 
00048 #include "Intrepid_FieldContainer.hpp"
00049 #include "Intrepid_HGRAD_HEX_Cn_FEM.hpp"
00050 #include "Intrepid_PointTools.hpp"
00051 #include "Teuchos_oblackholestream.hpp"
00052 #include "Teuchos_RCP.hpp"
00053 #include "Teuchos_GlobalMPISession.hpp"
00054 
00055 using namespace std;
00056 using namespace Intrepid;
00057 
00058 #define INTREPID_TEST_COMMAND( S , throwCounter, nException )                                                              \
00059 {                                                                                                                          \
00060   ++nException;                                                                                                            \
00061   try {                                                                                                                    \
00062     S ;                                                                                                                    \
00063   }                                                                                                                        \
00064   catch (std::logic_error err) {                                                                                           \
00065       ++throwCounter;                                                                                                      \
00066       *outStream << "Expected Error " << nException << " -------------------------------------------------------------\n"; \
00067       *outStream << err.what() << '\n';                                                                                    \
00068       *outStream << "-------------------------------------------------------------------------------" << "\n\n";           \
00069   };                                                                                                                       \
00070 }
00071 
00072 int main(int argc, char *argv[]) {
00073   
00074   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00075 
00076   // This little trick lets us print to std::cout only if
00077   // a (dummy) command-line argument is provided.
00078   int iprint     = argc - 1;
00079   Teuchos::RCP<std::ostream> outStream;
00080   Teuchos::oblackholestream bhs; // outputs nothing
00081   if (iprint > 0)
00082     outStream = Teuchos::rcp(&std::cout, false);
00083   else
00084     outStream = Teuchos::rcp(&bhs, false);
00085   
00086   // Save the format state of the original std::cout.
00087   Teuchos::oblackholestream oldFormatState;
00088   oldFormatState.copyfmt(std::cout);
00089   
00090   *outStream \
00091     << "===============================================================================\n" \
00092     << "|                                                                             |\n" \
00093     << "|                 Unit Test (Basis_HGRAD_HEX_C2_FEM)                          |\n" \
00094     << "|                                                                             |\n" \
00095     << "|     1) Conversion of Dof tags into Dof ordinals and back                    |\n" \
00096     << "|     2) Basis values for VALUE, GRAD, and Dk operators                       |\n" \
00097     << "|                                                                             |\n" \
00098     << "|  Questions? Contact  Pavel Bochev  (pbboche@sandia.gov),                    |\n" \
00099     << "|                      Robert Kirby  (robert.c.kirby@ttu.edu),                |\n" \
00100     << "|                      Denis Ridzal  (dridzal@sandia.gov),                    |\n" \
00101     << "|                      Kara Peterson (kjpeter@sandia.gov).                    |\n" \
00102     << "|                                                                             |\n" \
00103     << "|  Intrepid's website: http://trilinos.sandia.gov/packages/intrepid           |\n" \
00104     << "|  Trilinos website:   http://trilinos.sandia.gov                             |\n" \
00105     << "|                                                                             |\n" \
00106     << "===============================================================================\n"\
00107     << "| TEST 1: Basis creation, exception testing                                   |\n"\
00108     << "===============================================================================\n";
00109   
00110   // Define basis and error flag
00111   // get points
00112   const int deg=2;
00113   shards::CellTopology line(shards::getCellTopologyData< shards::Line<> >()); 
00114   FieldContainer<double> pts(PointTools::getLatticeSize(line,deg),1);
00115   PointTools::getLattice<double,FieldContainer<double> >(pts,line,deg);
00116 
00117   Basis_HGRAD_HEX_Cn_FEM<double, FieldContainer<double> > hexBasis(deg,deg,deg,pts,pts,pts);
00118 
00119   int errorFlag = 0;
00120 
00121   // Initialize throw counter for exception testing
00122   int nException     = 0;
00123   int throwCounter   = 0;
00124 
00125   // Define arrayS containing the 27 nodes of hexahedron<27> topology
00126   FieldContainer<double> hexNodes(27, 3);
00127   // do it lexicographically as a lattice
00128   hexNodes(0, 0) = -1.0;   hexNodes(0, 1) = -1.0;  hexNodes(0, 2) = -1.0;
00129   hexNodes(1, 0) =  0.0;   hexNodes(1, 1) = -1.0;  hexNodes(1, 2) = -1.0;
00130   hexNodes(2, 0) =  1.0;   hexNodes(2, 1) = -1.0;  hexNodes(2, 2) = -1.0;
00131   hexNodes(3, 0) = -1.0;   hexNodes(3, 1) =  0.0;  hexNodes(3, 2) = -1.0;
00132   hexNodes(4, 0) =  0.0;   hexNodes(4, 1) =  0.0;  hexNodes(4, 2) = -1.0;
00133   hexNodes(5, 0) =  1.0;   hexNodes(5, 1) =  0.0;  hexNodes(5, 2) = -1.0;
00134   hexNodes(6, 0) = -1.0;   hexNodes(6, 1) =  1.0;  hexNodes(6, 2) = -1.0;
00135   hexNodes(7, 0) = 0.0;    hexNodes(7, 1) =  1.0;  hexNodes(7, 2) = -1.0;
00136   hexNodes(8, 0) = 1.0;    hexNodes(8, 1) =  1.0;  hexNodes(8, 2) = -1.0;
00137   hexNodes(9, 0) = -1.0;   hexNodes(9, 1) = -1.0;  hexNodes(9, 2) = 0.0;
00138   hexNodes(10, 0) =  0.0;   hexNodes(10, 1) = -1.0;  hexNodes(10, 2) = 0.0;
00139   hexNodes(11, 0) =  1.0;   hexNodes(11, 1) = -1.0;  hexNodes(11, 2) = 0.0;
00140   hexNodes(12, 0) = -1.0;   hexNodes(12, 1) =  0.0;  hexNodes(12, 2) = 0.0;
00141   hexNodes(13, 0) =  0.0;   hexNodes(13, 1) =  0.0;  hexNodes(13, 2) = 0.0;
00142   hexNodes(14, 0) =  1.0;   hexNodes(14, 1) =  0.0;  hexNodes(14, 2) = 0.0;
00143   hexNodes(15, 0) = -1.0;   hexNodes(15, 1) =  1.0;  hexNodes(15, 2) = 0.0;
00144   hexNodes(16, 0) = 0.0;    hexNodes(16, 1) =  1.0;  hexNodes(16, 2) = 0.0;
00145   hexNodes(17, 0) = 1.0;    hexNodes(17, 1) =  1.0;  hexNodes(17, 2) = 0.0;
00146   hexNodes(18, 0) = -1.0;   hexNodes(18, 1) = -1.0;  hexNodes(18, 2) = 1.0;
00147   hexNodes(19, 0) =  0.0;   hexNodes(19, 1) = -1.0;  hexNodes(19, 2) = 1.0;
00148   hexNodes(20, 0) =  1.0;   hexNodes(20, 1) = -1.0;  hexNodes(20, 2) = 1.0;
00149   hexNodes(21, 0) = -1.0;   hexNodes(21, 1) =  0.0;  hexNodes(21, 2) = 1.0;
00150   hexNodes(22, 0) =  0.0;   hexNodes(22, 1) =  0.0;  hexNodes(22, 2) = 1.0;
00151   hexNodes(23, 0) =  1.0;   hexNodes(23, 1) =  0.0;  hexNodes(23, 2) = 1.0;
00152   hexNodes(24, 0) = -1.0;   hexNodes(24, 1) =  1.0;  hexNodes(24, 2) = 1.0;
00153   hexNodes(25, 0) = 0.0;    hexNodes(25, 1) =  1.0;  hexNodes(25, 2) = 1.0;
00154   hexNodes(26, 0) = 1.0;    hexNodes(26, 1) =  1.0;  hexNodes(26, 2) = 1.0;
00155 
00156  
00157   // Generic array for the output values; needs to be properly resized depending on the operator type
00158   FieldContainer<double> vals;
00159 
00160   try{
00161     // exception #1: CURL cannot be applied to scalar functions in 3D
00162     // resize vals to rank-3 container with dimensions (num. basis functions, num. points, arbitrary)
00163     vals.resize(hexBasis.getCardinality(), hexNodes.dimension(0), 4 );
00164     INTREPID_TEST_COMMAND( hexBasis.getValues(vals, hexNodes, OPERATOR_CURL), throwCounter, nException );
00165 
00166     // exception #2: DIV cannot be applied to scalar functions in 3D
00167     // resize vals to rank-2 container with dimensions (num. basis functions, num. points)
00168     vals.resize(hexBasis.getCardinality(), hexNodes.dimension(0) );
00169     INTREPID_TEST_COMMAND( hexBasis.getValues(vals, hexNodes, OPERATOR_DIV), throwCounter, nException );
00170         
00171     // Exceptions 3-7: all bf tags/bf Ids below are wrong and should cause getDofOrdinal() and 
00172     // getDofTag() to access invalid array elements thereby causing bounds check exception
00173     // exception #3
00174     INTREPID_TEST_COMMAND( hexBasis.getDofOrdinal(3,10,0), throwCounter, nException );    
00175     // exception #4
00176     INTREPID_TEST_COMMAND( hexBasis.getDofOrdinal(1,2,1), throwCounter, nException );
00177     // exception #5
00178     INTREPID_TEST_COMMAND( hexBasis.getDofOrdinal(0,4,1), throwCounter, nException );
00179     // exception #6
00180     INTREPID_TEST_COMMAND( hexBasis.getDofTag(28), throwCounter, nException );
00181     // exception #7
00182     INTREPID_TEST_COMMAND( hexBasis.getDofTag(-1), throwCounter, nException );
00183 
00184 #ifdef HAVE_INTREPID_DEBUG 
00185     // Exceptions 8-18 test exception handling with incorrectly dimensioned input/output arrays
00186     // exception #8: input points array must be of rank-2
00187     FieldContainer<double> badPoints1(4, 5, 3);
00188     INTREPID_TEST_COMMAND( hexBasis.getValues(vals, badPoints1, OPERATOR_VALUE), throwCounter, nException );
00189 
00190     // exception #9 dimension 1 in the input point array must equal space dimension of the cell
00191     FieldContainer<double> badPoints2(4, hexBasis.getBaseCellTopology().getDimension() - 1);
00192     INTREPID_TEST_COMMAND( hexBasis.getValues(vals, badPoints2, OPERATOR_VALUE), throwCounter, nException );
00193 
00194     // exception #10 output values must be of rank-2 for OPERATOR_VALUE
00195     FieldContainer<double> badVals1(4, 3, 1);
00196     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals1, hexNodes, OPERATOR_VALUE), throwCounter, nException );
00197  
00198     // exception #11 output values must be of rank-3 for OPERATOR_GRAD
00199     FieldContainer<double> badVals2(4, 3);
00200     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals2, hexNodes, OPERATOR_GRAD), throwCounter, nException );
00201     
00202     // exception #12 output values must be of rank-3 for OPERATOR_D1
00203     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals2, hexNodes, OPERATOR_D1), throwCounter, nException );
00204     
00205     // exception #13 output values must be of rank-3 for OPERATOR_D2
00206     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals2, hexNodes, OPERATOR_D2), throwCounter, nException );
00207     
00208     // exception #14 incorrect 0th dimension of output array (must equal number of basis functions)
00209     FieldContainer<double> badVals3(hexBasis.getCardinality() + 1, hexNodes.dimension(0));
00210     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals3, hexNodes, OPERATOR_VALUE), throwCounter, nException );
00211     
00212     // exception #15 incorrect 1st dimension of output array (must equal number of points)
00213     FieldContainer<double> badVals4(hexBasis.getCardinality(), hexNodes.dimension(0) + 1);
00214     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals4, hexNodes, OPERATOR_VALUE), throwCounter, nException );
00215     
00216     // exception #16: incorrect 2nd dimension of output array (must equal the space dimension)
00217     FieldContainer<double> badVals5(hexBasis.getCardinality(), hexNodes.dimension(0), hexBasis.getBaseCellTopology().getDimension() - 1);
00218     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals5, hexNodes, OPERATOR_GRAD), throwCounter, nException );
00219     
00220     // exception #17: incorrect 2nd dimension of output array (must equal D2 cardinality in 3D)
00221     FieldContainer<double> badVals6(hexBasis.getCardinality(), hexNodes.dimension(0), 40);
00222     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals6, hexNodes, OPERATOR_D2), throwCounter, nException );
00223     
00224     // exception #18: incorrect 2nd dimension of output array (must equal D3 cardinality in 3D)
00225     FieldContainer<double> badVals7(hexBasis.getCardinality(), hexNodes.dimension(0), 50);
00226     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals7, hexNodes, OPERATOR_D3), throwCounter, nException );
00227 #endif
00228 
00229   }
00230   catch (std::logic_error err) {
00231     *outStream << "UNEXPECTED ERROR !!! ----------------------------------------------------------\n";
00232     *outStream << err.what() << '\n';
00233     *outStream << "-------------------------------------------------------------------------------" << "\n\n";
00234     errorFlag = -1000;
00235   };
00236   
00237   // Check if number of thrown exceptions matches the one we expect 
00238   // Note Teuchos throw number will not pick up exceptions 3-7 and therefore will not match.
00239   if (throwCounter != nException) {
00240     errorFlag++;
00241     *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00242   }
00243   
00244   *outStream \
00245     << "\n"
00246     << "===============================================================================\n"\
00247     << "| TEST 2: correctness of tag to enum and enum to tag lookups                  |\n"\
00248     << "===============================================================================\n";
00249   
00250   try{
00251     std::vector<std::vector<int> > allTags = hexBasis.getAllDofTags();
00252     
00253     // Loop over all tags, lookup the associated dof enumeration and then lookup the tag again
00254     for (unsigned i = 0; i < allTags.size(); i++) {
00255       int bfOrd  = hexBasis.getDofOrdinal(allTags[i][0], allTags[i][1], allTags[i][2]);
00256       
00257 
00258 
00259       std::vector<int> myTag = hexBasis.getDofTag(bfOrd);
00260        if( !( (myTag[0] == allTags[i][0]) &&
00261               (myTag[1] == allTags[i][1]) &&
00262               (myTag[2] == allTags[i][2]) &&
00263               (myTag[3] == allTags[i][3]) ) ) {
00264         errorFlag++;
00265         *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00266         *outStream << " getDofOrdinal( {" 
00267           << allTags[i][0] << ", " 
00268           << allTags[i][1] << ", " 
00269           << allTags[i][2] << ", " 
00270           << allTags[i][3] << "}) = " << bfOrd <<" but \n";   
00271         *outStream << " getDofTag(" << bfOrd << ") = { "
00272           << myTag[0] << ", " 
00273           << myTag[1] << ", " 
00274           << myTag[2] << ", " 
00275           << myTag[3] << "}\n";        
00276       }
00277     }
00278     
00279     // Now do the same but loop over basis functions
00280     for( int bfOrd = 0; bfOrd < hexBasis.getCardinality(); bfOrd++) {
00281       std::vector<int> myTag  = hexBasis.getDofTag(bfOrd);
00282       int myBfOrd = hexBasis.getDofOrdinal(myTag[0], myTag[1], myTag[2]);
00283       if( bfOrd != myBfOrd) {
00284         errorFlag++;
00285         *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00286         *outStream << " getDofTag(" << bfOrd << ") = { "
00287           << myTag[0] << ", " 
00288           << myTag[1] << ", " 
00289           << myTag[2] << ", " 
00290           << myTag[3] << "} but getDofOrdinal({" 
00291           << myTag[0] << ", " 
00292           << myTag[1] << ", " 
00293           << myTag[2] << ", " 
00294           << myTag[3] << "} ) = " << myBfOrd << "\n";
00295       }
00296     }
00297   }
00298   catch (std::logic_error err){
00299     *outStream << err.what() << "\n\n";
00300     errorFlag = -1000;
00301   };
00302   
00303   
00304   *outStream \
00305     << "\n"
00306     << "===============================================================================\n"\
00307     << "| TEST 3: correctness of basis function values                                |\n"\
00308     << "===============================================================================\n";
00309   
00310   outStream -> precision(20);
00311   
00312   // VALUE: Each row gives the 27 correct basis set values at an evaluation point
00313   double basisValues[] = {
00314     1.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
00315     0, 1.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
00316     0, 0, 1.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
00317     0, 0, 0, 1.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
00318     0, 0, 0, 0, 1.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
00319     0, 0, 0, 0, 0, 1.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
00320     0, 0, 0, 0, 0, 0, 1.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
00321     0, 0, 0, 0, 0, 0, 0, 1.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
00322     0, 0, 0, 0, 0, 0, 0, 0, 1.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
00323     0, 0, 0, 0, 0, 0, 0, 0, 0, 1.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
00324     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
00325     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
00326     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
00327     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
00328     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
00329     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
00330     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
00331     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
00332     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.000, 0, 0, 0, 0, 0, 0, 0, 0, \
00333     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.000, 0, 0, 0, 0, 0, 0, 0, \
00334     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.000, 0, 0, 0, 0, 0, 0, \
00335     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.000, 0, 0, 0, 0, 0, \
00336     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.000, 0, 0, 0, 0, \
00337     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.000, 0, 0, 0, \
00338     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.000, 0, 0, \
00339     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.000, 0, \
00340     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.000  };
00341   
00342   
00343   // GRAD, D1, D2, D3 and D4 test values are stored in files due to their large size
00344   std::string     fileName;
00345   std::ifstream   dataFile;
00346 
00347   // GRAD and D1 values are stored in (F,P,D) format in a data file. Read file and do the test
00348   std::vector<double> basisGrads;           // Flat array for the gradient values.
00349   
00350   fileName = "./testdata/HEX_C2_GradVals.dat";
00351   dataFile.open(fileName.c_str());
00352   TEUCHOS_TEST_FOR_EXCEPTION( !dataFile.good(), std::logic_error,
00353                       ">>> ERROR (HGRAD_HEX_C2/test01): could not open GRAD values data file, test aborted.");
00354   while (!dataFile.eof() ){
00355     double temp;
00356     string line;                            // string for one line of input file
00357     std::getline(dataFile, line);           // get next line from file
00358     stringstream data_line(line);           // convert to stringstream
00359     while(data_line >> temp){               // extract value from line
00360       basisGrads.push_back(temp);           // push into vector
00361     }
00362   }
00363   // It turns out that just closing and then opening the ifstream variable does not reset it
00364   // and subsequent open() command fails. One fix is to explicitely clear the ifstream, or
00365   // scope the variables.
00366   dataFile.close();
00367   dataFile.clear();
00368     
00369    
00370   //D2: flat array with the values of D2 applied to basis functions. Multi-index is (F,P,D2cardinality)
00371   std::vector<double> basisD2; 
00372   fileName = "./testdata/HEX_C2_D2Vals.dat";  
00373   dataFile.open(fileName.c_str());
00374   TEUCHOS_TEST_FOR_EXCEPTION( !dataFile.good(), std::logic_error,
00375                       ">>> ERROR (HGRAD_HEX_C2/test01): could not open D2 values data file, test aborted.");
00376   while (!dataFile.eof() ){
00377     double temp;
00378     string line;                            // string for one line of input file
00379     std::getline(dataFile, line);           // get next line from file
00380     stringstream data_line(line);           // convert to stringstream
00381     while(data_line >> temp){               // extract value from line
00382       basisD2.push_back(temp);              // push into vector
00383     }
00384   }
00385   dataFile.close();
00386   dataFile.clear();
00387 
00388   
00389   //D3: flat array with the values of D3 applied to basis functions. Multi-index is (F,P,D3cardinality)
00390   std::vector<double> basisD3;
00391   
00392   fileName = "./testdata/HEX_C2_D3Vals.dat";  
00393   dataFile.open(fileName.c_str());
00394   TEUCHOS_TEST_FOR_EXCEPTION( !dataFile.good(), std::logic_error,
00395                       ">>> ERROR (HGRAD_HEX_C2/test01): could not open D3 values data file, test aborted.");
00396   
00397   while (!dataFile.eof() ){
00398     double temp;
00399     string line;                            // string for one line of input file
00400     std::getline(dataFile, line);           // get next line from file
00401     stringstream data_line(line);           // convert to stringstream
00402     while(data_line >> temp){               // extract value from line
00403       basisD3.push_back(temp);              // push into vector
00404     }
00405   }
00406   dataFile.close();
00407   dataFile.clear();
00408  
00409   
00410   //D4: flat array with the values of D applied to basis functions. Multi-index is (F,P,D4cardinality)
00411   std::vector<double> basisD4;
00412   
00413   fileName = "./testdata/HEX_C2_D4Vals.dat";
00414   dataFile.open(fileName.c_str());
00415   TEUCHOS_TEST_FOR_EXCEPTION( !dataFile.good(), std::logic_error,
00416                       ">>> ERROR (HGRAD_HEX_C2/test01): could not open D4 values data file, test aborted.");
00417   
00418   while (!dataFile.eof() ){
00419     double temp;
00420     string line;                            // string for one line of input file
00421     std::getline(dataFile, line);           // get next line from file
00422     stringstream data_line(line);           // convert to stringstream
00423     while(data_line >> temp){               // extract value from line
00424       basisD4.push_back(temp);              // push into vector
00425     }
00426   }
00427   dataFile.close();
00428   dataFile.clear();
00429   
00430 
00431   try{
00432         
00433     // Dimensions for the output arrays:
00434     int numFields = hexBasis.getCardinality();
00435     int numPoints = hexNodes.dimension(0);
00436     int spaceDim  = hexBasis.getBaseCellTopology().getDimension();
00437     
00438     // Generic array for values, grads, curls, etc. that will be properly sized before each call
00439     FieldContainer<double> vals;
00440     
00441     // Check VALUE of basis functions: resize vals to rank-2 container:
00442     vals.resize(numFields, numPoints);
00443     hexBasis.getValues(vals, hexNodes, OPERATOR_VALUE);
00444     for (int i = 0; i < numFields; i++) {
00445       for (int j = 0; j < numPoints; j++) {
00446         int l =  i + j * numFields;
00447         if (std::abs(vals(i,j) - basisValues[l]) > INTREPID_TOL) {
00448           errorFlag++;
00449           *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00450           
00451           // Output the multi-index of the value where the error is:
00452           *outStream << " At multi-index { ";
00453           *outStream << i << " ";*outStream << j << " ";
00454           *outStream << "}  computed value: " << vals(i,j)
00455                      << " but reference value: " << basisValues[l] << "\n";
00456         }
00457       }
00458     }
00459     
00460     // Check GRAD of basis function: resize vals to rank-3 container
00461     vals.resize(numFields, numPoints, spaceDim);
00462     hexBasis.getValues(vals, hexNodes, OPERATOR_GRAD);
00463     for (int i = 0; i < numFields; i++) {
00464       for (int j = 0; j < numPoints; j++) {
00465         for (int k = 0; k < spaceDim; k++) {
00466           
00467           // basisGrads is (F,P,D), compute offset:
00468           int l = k + j * spaceDim + i * spaceDim * numPoints;
00469            if (std::abs(vals(i,j,k) - basisGrads[l]) > INTREPID_TOL) {
00470              errorFlag++;
00471              *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00472 
00473              // Output the multi-index of the value where the error is:
00474              *outStream << " At multi-index { ";
00475              *outStream << i << " ";*outStream << j << " ";*outStream << k << " ";
00476              *outStream << "}  computed grad component: " << vals(i,j,k)
00477                << " but reference grad component: " << basisGrads[l] << "\n";
00478             }
00479          }
00480       }
00481     }
00482     
00483     // Check D1 of basis function (do not resize vals because it has the correct size: D1 = GRAD)
00484     hexBasis.getValues(vals, hexNodes, OPERATOR_D1);
00485     for (int i = 0; i < numFields; i++) {
00486       for (int j = 0; j < numPoints; j++) {
00487         for (int k = 0; k < spaceDim; k++) {
00488 
00489           // basisGrads is (F,P,D), compute offset:
00490           int l = k + j * spaceDim + i * spaceDim * numPoints;
00491            if (std::abs(vals(i,j,k) - basisGrads[l]) > INTREPID_TOL) {
00492              errorFlag++;
00493              *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00494 
00495              // Output the multi-index of the value where the error is:
00496              *outStream << " At multi-index { ";
00497              *outStream << i << " ";*outStream << j << " ";*outStream << k << " ";
00498              *outStream << "}  computed D1 component: " << vals(i,j,k)
00499                << " but reference D1 component: " << basisGrads[l] << "\n";
00500             }
00501          }
00502       }
00503     }
00504 
00505     
00506     // Check D2 of basis function
00507     int D2cardinality = Intrepid::getDkCardinality(OPERATOR_D2, spaceDim);
00508     vals.resize(numFields, numPoints, D2cardinality);    
00509     hexBasis.getValues(vals, hexNodes, OPERATOR_D2);
00510     for (int i = 0; i < numFields; i++) {
00511       for (int j = 0; j < numPoints; j++) {
00512         for (int k = 0; k < D2cardinality; k++) {
00513  
00514           // basisD2 is (F,P,Dk), compute offset:
00515           int l = k + j * D2cardinality + i * D2cardinality * numPoints;
00516            if (std::abs(vals(i,j,k) - basisD2[l]) > INTREPID_TOL) {
00517              errorFlag++;
00518              *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00519 
00520              // Output the multi-index of the value where the error is:
00521              *outStream << " At multi-index { ";
00522              *outStream << i << " ";*outStream << j << " ";*outStream << k << " ";
00523              *outStream << "}  computed D2 component: " << vals(i,j,k)
00524                << " but reference D2 component: " << basisD2[l] << "\n";
00525             }
00526          }
00527       }
00528     }
00529 
00530     
00531     // Check D3 of basis function
00532     int D3cardinality = Intrepid::getDkCardinality(OPERATOR_D3, spaceDim);
00533     vals.resize(numFields, numPoints, D3cardinality);    
00534     hexBasis.getValues(vals, hexNodes, OPERATOR_D3);
00535      
00536     for (int i = 0; i < numFields; i++) {
00537       for (int j = 0; j < numPoints; j++) {
00538         for (int k = 0; k < D3cardinality; k++) {
00539           
00540           // basisD3 is (F,P,Dk), compute offset:
00541           int l = k + j * D3cardinality + i * D3cardinality * numPoints;
00542           if (std::abs(vals(i,j,k) - basisD3[l]) > INTREPID_TOL) {
00543             errorFlag++;
00544             *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00545             
00546             // Output the multi-index of the value where the error is:
00547             *outStream << " At multi-index { ";
00548             *outStream << i << " ";*outStream << j << " ";*outStream << k << " ";
00549             *outStream << "}  computed D3 component: " << vals(i,j,k)
00550               << " but reference D3 component: " << basisD3[l] << "\n";
00551           }
00552         }
00553       }
00554     }
00555 
00556     
00557     // Check D4 of basis function
00558     int D4cardinality = Intrepid::getDkCardinality(OPERATOR_D4, spaceDim);
00559     vals.resize(numFields, numPoints, D4cardinality);    
00560     hexBasis.getValues(vals, hexNodes, OPERATOR_D4);
00561     for (int i = 0; i < numFields; i++) {
00562       for (int j = 0; j < numPoints; j++) {
00563         for (int k = 0; k < D4cardinality; k++) {
00564           
00565           // basisD4 is (F,P,Dk), compute offset:
00566           int l = k + j * D4cardinality + i * D4cardinality * numPoints;
00567           if (std::abs(vals(i,j,k) - basisD4[l]) > INTREPID_TOL) {
00568             errorFlag++;
00569             *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00570             
00571             // Output the multi-index of the value where the error is:
00572             *outStream << " At multi-index { ";
00573             *outStream << i << " ";*outStream << j << " ";*outStream << k << " ";
00574             *outStream << "}  computed D4 component: " << vals(i,j,k)
00575               << " but reference D4 component: " << basisD2[l] << "\n";
00576           }
00577         }
00578       }
00579     }
00580     
00581 
00582 
00583   // Check D7 to D10 - must be zero. This basis does not cover D5 and D6
00584     for(EOperator op = OPERATOR_D7; op < OPERATOR_MAX; op++) {
00585       
00586       // The last dimension is the number of kth derivatives and needs to be resized for every Dk
00587       int DkCardin  = Intrepid::getDkCardinality(op, spaceDim);
00588       vals.resize(numFields, numPoints, DkCardin);    
00589 
00590       hexBasis.getValues(vals, hexNodes, op);
00591       for (int i = 0; i < vals.size(); i++) {
00592         if (std::abs(vals[i]) > INTREPID_TOL) {
00593           errorFlag++;
00594           *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00595           
00596           // Get the multi-index of the value where the error is and the operator order
00597           std::vector<int> myIndex;
00598           vals.getMultiIndex(myIndex,i);
00599           int ord = Intrepid::getOperatorOrder(op);
00600           *outStream << " At multi-index { ";
00601           for(int j = 0; j < vals.rank(); j++) {
00602             *outStream << myIndex[j] << " ";
00603           }
00604           *outStream << "}  computed D"<< ord <<" component: " << vals[i] 
00605             << " but reference D" << ord << " component:  0 \n";
00606         }
00607       }
00608     }    
00609   }
00610   // Catch unexpected errors
00611   catch (std::logic_error err) {
00612     *outStream << err.what() << "\n\n";
00613     errorFlag = -1000;
00614   };
00615   
00616   if (errorFlag != 0)
00617     std::cout << "End Result: TEST FAILED\n";
00618   else
00619     std::cout << "End Result: TEST PASSED\n";
00620   
00621   // reset format state of std::cout
00622   std::cout.copyfmt(oldFormatState);
00623   
00624   return errorFlag;
00625 }