Intrepid
http://trilinos.sandia.gov/packages/docs/r10.12/packages/intrepid/test/Discretization/Basis/HDIV_HEX_I1_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_HDIV_HEX_I1_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_HDIV_HEX_I1_FEM)                           |\n" \
00093     << "|                                                                             |\n" \
00094     << "|     1) Conversion of Dof tags into Dof ordinals and back                    |\n" \
00095     << "|     2) Basis values for VALUE and DIV 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_HDIV_HEX_I1_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 array containing the 8 vertices of the reference HEX, its center and 6 face centers
00117   FieldContainer<double> hexNodes(15, 3);
00118   hexNodes(0,0) = -1.0;  hexNodes(0,1) = -1.0;  hexNodes(0,2) = -1.0;
00119   hexNodes(1,0) =  1.0;  hexNodes(1,1) = -1.0;  hexNodes(1,2) = -1.0;
00120   hexNodes(2,0) =  1.0;  hexNodes(2,1) =  1.0;  hexNodes(2,2) = -1.0;
00121   hexNodes(3,0) = -1.0;  hexNodes(3,1) =  1.0;  hexNodes(3,2) = -1.0;
00122   
00123   hexNodes(4,0) = -1.0;  hexNodes(4,1) = -1.0;  hexNodes(4,2) =  1.0;
00124   hexNodes(5,0) =  1.0;  hexNodes(5,1) = -1.0;  hexNodes(5,2) =  1.0;
00125   hexNodes(6,0) =  1.0;  hexNodes(6,1) =  1.0;  hexNodes(6,2) =  1.0;
00126   hexNodes(7,0) = -1.0;  hexNodes(7,1) =  1.0;  hexNodes(7,2) =  1.0;  
00127   
00128   hexNodes(8,0) =  0.0;  hexNodes(8,1) =  0.0;  hexNodes(8,2) =  0.0;
00129   
00130   hexNodes(9,0) =  1.0;  hexNodes(9,1) =  0.0;  hexNodes(9,2) =  0.0;
00131   hexNodes(10,0)= -1.0;  hexNodes(10,1)=  0.0;  hexNodes(10,2)=  0.0;
00132   
00133   hexNodes(11,0)=  0.0;  hexNodes(11,1)=  1.0;  hexNodes(11,2)=  0.0;
00134   hexNodes(12,0)=  0.0;  hexNodes(12,1)= -1.0;  hexNodes(12,2)=  0.0;
00135   
00136   hexNodes(13,0)=  0.0;  hexNodes(13,1)=  0.0;  hexNodes(13,2)=  1.0;
00137   hexNodes(14,0)=  0.0;  hexNodes(14,1)=  0.0;  hexNodes(14,2)= -1.0;
00138 
00139   
00140   // Generic array for the output values; needs to be properly resized depending on the operator type
00141   FieldContainer<double> vals;
00142 
00143   try{
00144     // exception #1: GRAD cannot be applied to HDIV functions 
00145     // resize vals to rank-3 container with dimensions (num. basis functions, num. points, arbitrary)
00146     vals.resize(hexBasis.getCardinality(), hexNodes.dimension(0), 3 );
00147     INTREPID_TEST_COMMAND( hexBasis.getValues(vals, hexNodes, OPERATOR_GRAD), throwCounter, nException );
00148 
00149     // exception #2: CURL cannot be applied to HDIV functions
00150     INTREPID_TEST_COMMAND( hexBasis.getValues(vals, hexNodes, OPERATOR_CURL), throwCounter, nException );
00151 
00152     // Exceptions 3-7: all bf tags/bf Ids below are wrong and should cause getDofOrdinal() and 
00153     // getDofTag() to access invalid array elements thereby causing bounds check exception
00154     // exception #3
00155     INTREPID_TEST_COMMAND( hexBasis.getDofOrdinal(3,0,0), throwCounter, nException );
00156     // exception #4
00157     INTREPID_TEST_COMMAND( hexBasis.getDofOrdinal(1,1,1), throwCounter, nException );
00158     // exception #5
00159     INTREPID_TEST_COMMAND( hexBasis.getDofOrdinal(0,4,1), throwCounter, nException );
00160     // exception #6
00161     INTREPID_TEST_COMMAND( hexBasis.getDofTag(12), throwCounter, nException );
00162     // exception #7
00163     INTREPID_TEST_COMMAND( hexBasis.getDofTag(-1), throwCounter, nException );
00164 
00165 #ifdef HAVE_INTREPID_DEBUG
00166     // Exceptions 8- test exception handling with incorrectly dimensioned input/output arrays
00167     // exception #8: input points array must be of rank-2
00168     FieldContainer<double> badPoints1(4, 5, 3);
00169     INTREPID_TEST_COMMAND( hexBasis.getValues(vals, badPoints1, OPERATOR_VALUE), throwCounter, nException );
00170     
00171     // exception #9 dimension 1 in the input point array must equal space dimension of the cell
00172     FieldContainer<double> badPoints2(4, 2);
00173     INTREPID_TEST_COMMAND( hexBasis.getValues(vals, badPoints2, OPERATOR_VALUE), throwCounter, nException );
00174     
00175     // exception #10 output values must be of rank-3 for OPERATOR_VALUE
00176     FieldContainer<double> badVals1(4, 3);
00177     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals1, hexNodes, OPERATOR_VALUE), throwCounter, nException );
00178  
00179     // exception #11 output values must be of rank-2 for OPERATOR_DIV
00180     FieldContainer<double> badVals2(4, 3, 3);
00181     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals2, hexNodes, OPERATOR_DIV), throwCounter, nException );
00182     
00183     // exception #12 incorrect 0th dimension of output array (must equal number of basis functions)
00184     FieldContainer<double> badVals3(hexBasis.getCardinality() + 1, hexNodes.dimension(0), 3);
00185     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals3, hexNodes, OPERATOR_VALUE), throwCounter, nException );
00186     
00187     // exception #13 incorrect 0th dimension of output array (must equal number of basis functions)
00188     FieldContainer<double> badVals4(hexBasis.getCardinality() + 1, hexNodes.dimension(0));
00189     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals4, hexNodes, OPERATOR_DIV), throwCounter, nException );
00190 
00191     // exception #14 incorrect 1st dimension of output array (must equal number of points)
00192     FieldContainer<double> badVals5(hexBasis.getCardinality(), hexNodes.dimension(0) + 1, 3);
00193     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals5, hexNodes, OPERATOR_VALUE), throwCounter, nException );
00194 
00195     // exception #15 incorrect 1st dimension of output array (must equal number of points)
00196     FieldContainer<double> badVals6(hexBasis.getCardinality(), hexNodes.dimension(0) + 1);
00197     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals6, hexNodes, OPERATOR_DIV), throwCounter, nException );
00198 
00199     // exception #16: incorrect 2nd dimension of output array (must equal the space dimension)
00200     FieldContainer<double> badVals7(hexBasis.getCardinality(), hexNodes.dimension(0), 4);
00201     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals7, hexNodes, OPERATOR_VALUE), throwCounter, nException );
00202 #endif
00203     
00204   }
00205   catch (std::logic_error err) {
00206     *outStream << "UNEXPECTED ERROR !!! ----------------------------------------------------------\n";
00207     *outStream << err.what() << '\n';
00208     *outStream << "-------------------------------------------------------------------------------" << "\n\n";
00209     errorFlag = -1000;
00210   };
00211   
00212   // Check if number of thrown exceptions matches the one we expect 
00213   if (throwCounter != nException) {
00214     errorFlag++;
00215     *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00216   }
00217   
00218   *outStream \
00219     << "\n"
00220     << "===============================================================================\n"\
00221     << "| TEST 2: correctness of tag to enum and enum to tag lookups                  |\n"\
00222     << "===============================================================================\n";
00223   
00224   try{
00225     std::vector<std::vector<int> > allTags = hexBasis.getAllDofTags();
00226     
00227     // Loop over all tags, lookup the associated dof enumeration and then lookup the tag again
00228     for (unsigned i = 0; i < allTags.size(); i++) {
00229       int bfOrd  = hexBasis.getDofOrdinal(allTags[i][0], allTags[i][1], allTags[i][2]);
00230       
00231       std::vector<int> myTag = hexBasis.getDofTag(bfOrd);
00232        if( !( (myTag[0] == allTags[i][0]) &&
00233               (myTag[1] == allTags[i][1]) &&
00234               (myTag[2] == allTags[i][2]) &&
00235               (myTag[3] == allTags[i][3]) ) ) {
00236         errorFlag++;
00237         *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00238         *outStream << " getDofOrdinal( {" 
00239           << allTags[i][0] << ", " 
00240           << allTags[i][1] << ", " 
00241           << allTags[i][2] << ", " 
00242           << allTags[i][3] << "}) = " << bfOrd <<" but \n";   
00243         *outStream << " getDofTag(" << bfOrd << ") = { "
00244           << myTag[0] << ", " 
00245           << myTag[1] << ", " 
00246           << myTag[2] << ", " 
00247           << myTag[3] << "}\n";        
00248       }
00249     }
00250     
00251     // Now do the same but loop over basis functions
00252     for( int bfOrd = 0; bfOrd < hexBasis.getCardinality(); bfOrd++) {
00253       std::vector<int> myTag  = hexBasis.getDofTag(bfOrd);
00254       int myBfOrd = hexBasis.getDofOrdinal(myTag[0], myTag[1], myTag[2]);
00255       if( bfOrd != myBfOrd) {
00256         errorFlag++;
00257         *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00258         *outStream << " getDofTag(" << bfOrd << ") = { "
00259           << myTag[0] << ", " 
00260           << myTag[1] << ", " 
00261           << myTag[2] << ", " 
00262           << myTag[3] << "} but getDofOrdinal({" 
00263           << myTag[0] << ", " 
00264           << myTag[1] << ", " 
00265           << myTag[2] << ", " 
00266           << myTag[3] << "} ) = " << myBfOrd << "\n";
00267       }
00268     }
00269   }
00270   catch (std::logic_error err){
00271     *outStream << err.what() << "\n\n";
00272     errorFlag = -1000;
00273   };
00274   
00275   *outStream \
00276     << "\n"
00277     << "===============================================================================\n"\
00278     << "| TEST 3: correctness of basis function values                                |\n"\
00279     << "===============================================================================\n";
00280   
00281   outStream -> precision(20);
00282   
00283   // VALUE: Each row pair gives the 6x3 correct basis set values at an evaluation point: (P,F,D) layout
00284   double basisValues[] = {
00285     // bottom 4 vertices
00286       0.,-0.25,0.,  0.,0.,0.,  0.,0.,0., -0.25,0.,0.,  0.,0.,-0.25,  0.,0.,0.,
00287       0.,-0.25,0.,  0.25,0.,0.,  0.,0.,0.,  0.,0.,0.,  0.,0.,-0.25,  0.,0.,0.,  
00288       0.,0.,0.,  0.25,0.,0.,  0.,0.25,0.,  0.,0.,0.,  0.,0.,-0.25,  0.,0.,0.,
00289       0.,0.,0.,  0.,0.,0.,  0.,0.25,0., -0.25,0.,0.,  0.,0.,-0.25,  0.,0.,0.,
00290     // top 4 vertices
00291       0.,-0.25,0.,  0.,0.,0.,  0.,0.,0.,  -0.25,0.,0.,  0.,0.,0.,  0.,0.,0.25,
00292       0.,-0.25,0.,  0.25,0.,0.,  0.,0.,0.,  0.,0.,0.,  0.,0.,0.,  0.,0.,0.25,
00293       0.,0.,0.,  0.25,0.,0.,  0.,0.25,0.,  0.,0.,0.,  0.,0.,0.,  0.,0.,0.25,
00294       0.,0.,0.,  0.,0.,0.,  0.,0.25,0.,  -0.25,0.,0.,  0.,0.,0.,  0.,0.,0.25,
00295     // center {0, 0, 0}
00296       0.,-0.125,0.,  0.125,0.,0.,  0.,0.125,0.,  -0.125,0.,0.,  0.,0.,-0.125,  0.,0.,0.125,
00297     // faces { 1, 0, 0} and {-1, 0, 0}
00298       0.,-0.125,0.,  0.25,0.,0.,  0.,0.125,0.,  0.,0.,0.,  0.,0.,-0.125,  0.,0.,0.125, 
00299       0.,-0.125,0.,  0.,0.,0.,  0.,0.125,0., -0.25,0.,0.,  0.,0.,-0.125,  0.,0.,0.125,
00300     // faces { 0, 1, 0} and { 0,-1, 0}
00301       0.,0.,0.,  0.125,0.,0.,  0.,0.25,0.,  -0.125,0.,0.,  0.,0.,-0.125,  0.,0.,0.125,
00302       0.,-0.25,0.,  0.125,0.,0.,  0.,0.,0.,  -0.125,0.,0.,  0.,0.,-0.125,  0.,0.,0.125,
00303     // faces {0, 0, 1} and {0, 0, -1}
00304       0.,-0.125,0.,  0.125,0.,0.,  0.,0.125,0.,  -0.125,0.,0.,  0.,0.,0.,  0.,0.,0.25,
00305       0.,-0.125,0.,  0.125,0.,0.,  0.,0.125,0.,  -0.125,0.,0.,  0.,0.,-0.25,  0.,0.,0.
00306   };
00307   
00308   // DIV: each row gives the 6 correct values of the divergence of the 6 basis functions: (P,F) layout
00309   double basisDivs[] = {   
00310     // bottom 4 vertices
00311       0.125, 0.125, 0.125, 0.125, 0.125, 0.125,  
00312       0.125, 0.125, 0.125, 0.125, 0.125, 0.125,  
00313       0.125, 0.125, 0.125, 0.125, 0.125, 0.125,  
00314       0.125, 0.125, 0.125, 0.125, 0.125, 0.125,  
00315     // top 4 vertices
00316       0.125, 0.125, 0.125, 0.125, 0.125, 0.125,  
00317       0.125, 0.125, 0.125, 0.125, 0.125, 0.125,  
00318       0.125, 0.125, 0.125, 0.125, 0.125, 0.125,  
00319       0.125, 0.125, 0.125, 0.125, 0.125, 0.125,  
00320     // center {0, 0, 0}
00321       0.125, 0.125, 0.125, 0.125, 0.125, 0.125,  
00322     // faces { 1, 0, 0} and {-1, 0, 0}
00323       0.125, 0.125, 0.125, 0.125, 0.125, 0.125,  
00324       0.125, 0.125, 0.125, 0.125, 0.125, 0.125,  
00325     // faces { 0, 1, 0} and { 0,-1, 0}
00326       0.125, 0.125, 0.125, 0.125, 0.125, 0.125,  
00327       0.125, 0.125, 0.125, 0.125, 0.125, 0.125,  
00328     // faces {0, 0, 1} and {0, 0, -1}
00329       0.125, 0.125, 0.125, 0.125, 0.125, 0.125,  
00330       0.125, 0.125, 0.125, 0.125, 0.125, 0.125,  
00331   };
00332   
00333   try{
00334         
00335     // Dimensions for the output arrays:
00336     int numPoints = hexNodes.dimension(0);
00337     int numFields = hexBasis.getCardinality();
00338     int spaceDim  = hexBasis.getBaseCellTopology().getDimension();
00339     
00340     // Generic array for values and curls that will be properly sized before each call
00341     FieldContainer<double> vals;
00342     
00343     // Check VALUE of basis functions: resize vals to rank-3 container:
00344     vals.resize(numFields, numPoints, spaceDim);
00345     hexBasis.getValues(vals, hexNodes, OPERATOR_VALUE);
00346     for (int i = 0; i < numFields; i++) {
00347       for (int j = 0; j < numPoints; j++) {
00348         for (int k = 0; k < spaceDim; k++) {
00349 
00350           // compute offset for (P,F,D) data layout: indices are P->j, F->i, D->k
00351            int l = k + i * spaceDim + j * spaceDim * numFields;
00352            if (std::abs(vals(i,j,k) - basisValues[l]) > INTREPID_TOL) {
00353              errorFlag++;
00354              *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00355 
00356              // Output the multi-index of the value where the error is:
00357              *outStream << " At multi-index { ";
00358              *outStream << i << " ";*outStream << j << " ";*outStream << k << " ";
00359              *outStream << "}  computed value: " << vals(i,j,k)
00360                << " but reference value: " << basisValues[l] << "\n";
00361             }
00362          }
00363       }
00364     }
00365 
00366     // Check DIV of basis function: resize vals to rank-2 container
00367     vals.resize(numFields, numPoints);
00368     hexBasis.getValues(vals, hexNodes, OPERATOR_DIV);
00369     for (int i = 0; i < numFields; i++) {
00370       for (int j = 0; j < numPoints; j++) {
00371           int l =  i + j * numFields;
00372            if (std::abs(vals(i,j) - basisDivs[l]) > INTREPID_TOL) {
00373              errorFlag++;
00374              *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00375 
00376              // Output the multi-index of the value where the error is:
00377              *outStream << " At multi-index { ";
00378              *outStream << i << " ";*outStream << j << " ";
00379              *outStream << "}  computed divergence component: " << vals(i,j)
00380                << " but reference divergence component: " << basisDivs[l] << "\n";
00381          }
00382       }
00383     }
00384     
00385    }    
00386   
00387   // Catch unexpected errors
00388   catch (std::logic_error err) {
00389     *outStream << err.what() << "\n\n";
00390     errorFlag = -1000;
00391   };
00392   
00393   if (errorFlag != 0)
00394     std::cout << "End Result: TEST FAILED\n";
00395   else
00396     std::cout << "End Result: TEST PASSED\n";
00397   
00398   // reset format state of std::cout
00399   std::cout.copyfmt(oldFormatState);
00400   
00401   return errorFlag;
00402 }