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