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