Intrepid
/usr/src/RPM/BUILD/trilinos10-10.6.4/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 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Pavel Bochev  (pbboche@sandia.gov), 
00025 //                    Denis Ridzal  (dridzal@sandia.gov),
00026 //                    Kara Peterson (kjpeter@sandia.gov).
00027 //
00028 // ************************************************************************
00029 // @HEADER
00030 
00035 #include "Intrepid_FieldContainer.hpp"
00036 #include "Intrepid_HDIV_HEX_In_FEM.hpp"
00037 #include "Intrepid_PointTools.hpp"
00038 #include "Teuchos_oblackholestream.hpp"
00039 #include "Teuchos_RCP.hpp"
00040 #include "Teuchos_GlobalMPISession.hpp"
00041 
00042 using namespace std;
00043 using namespace Intrepid;
00044 
00045 #define INTREPID_TEST_COMMAND( S , throwCounter, nException )                                                              \
00046 {                                                                                                                          \
00047   ++nException;                                                                                                            \
00048   try {                                                                                                                    \
00049     S ;                                                                                                                    \
00050   }                                                                                                                        \
00051   catch (std::logic_error err) {                                                                                           \
00052       ++throwCounter;                                                                                                      \
00053       *outStream << "Expected Error " << nException << " -------------------------------------------------------------\n"; \
00054       *outStream << err.what() << '\n';                                                                                    \
00055       *outStream << "-------------------------------------------------------------------------------" << "\n\n";           \
00056   };                                                                                                                       \
00057 }
00058 
00059 int main(int argc, char *argv[]) {
00060   
00061   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00062 
00063   // This little trick lets us print to std::cout only if
00064   // a (dummy) command-line argument is provided.
00065   int iprint     = argc - 1;
00066   Teuchos::RCP<std::ostream> outStream;
00067   Teuchos::oblackholestream bhs; // outputs nothing
00068   if (iprint > 0)
00069     outStream = Teuchos::rcp(&std::cout, false);
00070   else
00071     outStream = Teuchos::rcp(&bhs, false);
00072   
00073   // Save the format state of the original std::cout.
00074   Teuchos::oblackholestream oldFormatState;
00075   oldFormatState.copyfmt(std::cout);
00076   
00077   *outStream \
00078     << "===============================================================================\n" \
00079     << "|                                                                             |\n" \
00080     << "|                 Unit Test (Basis_HDIV_HEX_In_FEM)                           |\n" \
00081     << "|                                                                             |\n" \
00082     << "|     1) Conversion of Dof tags into Dof ordinals and back                    |\n" \
00083     << "|     2) Basis values for VALUE and DIV operators                             |\n" \
00084     << "|                                                                             |\n" \
00085     << "|  Questions? Contact  Pavel Bochev  (pbboche@sandia.gov),                    |\n" \
00086     << "|                      Denis Ridzal  (dridzal@sandia.gov),                    |\n" \
00087     << "|                      Kara Peterson (kjpeter@sandia.gov).                    |\n" \
00088     << "|                                                                             |\n" \
00089     << "|  Intrepid's website: http://trilinos.sandia.gov/packages/intrepid           |\n" \
00090     << "|  Trilinos website:   http://trilinos.sandia.gov                             |\n" \
00091     << "|                                                                             |\n" \
00092     << "===============================================================================\n"\
00093     << "| TEST 1: Basis creation, exception testing                                   |\n"\
00094     << "===============================================================================\n";
00095   
00096   // Define basis and error flag
00097   const int deg = 1;
00098   shards::CellTopology line(shards::getCellTopologyData< shards::Line<> >()); 
00099   FieldContainer<double> closedPts(PointTools::getLatticeSize(line,deg),1);
00100   FieldContainer<double> openPts(PointTools::getLatticeSize(line,deg+1,1),1);
00101   PointTools::getLattice<double,FieldContainer<double> >(closedPts,line,deg);
00102   PointTools::getLattice<double,FieldContainer<double> >(openPts,line,deg+1,1);
00103 
00104   Basis_HDIV_HEX_In_FEM<double, FieldContainer<double> > hexBasis(deg,closedPts,openPts);
00105 
00106   int errorFlag = 0;
00107 
00108   // Initialize throw counter for exception testing
00109   int nException     = 0;
00110   int throwCounter   = 0;
00111 
00112   // compute values at vertices: there are 8 of them
00113   FieldContainer<double> hexNodes(8, 3);
00114   hexNodes(0,0) = -1.0; hexNodes(0,1) = -1.0; hexNodes(0,2) = -1.0;
00115   hexNodes(1,0) = 1.0; hexNodes(1,1) = -1.0; hexNodes(1,2) = -1.0;
00116   hexNodes(2,0) = -1.0; hexNodes(2,1) = 1.0; hexNodes(2,2) = -1.0;
00117   hexNodes(3,0) = 1.0; hexNodes(3,1) = 1.0; hexNodes(3,2) = -1.0;
00118   hexNodes(4,0) = -1.0; hexNodes(4,1) = -1.0; hexNodes(4,2) = 1.0;
00119   hexNodes(5,0) = 1.0; hexNodes(5,1) = -1.0; hexNodes(5,2) = 1.0;
00120   hexNodes(6,0) = -1.0; hexNodes(6,1) = 1.0; hexNodes(6,2) = 1.0;
00121   hexNodes(7,0) = 1.0; hexNodes(7,1) = 1.0; hexNodes(7,2) = 1.0;
00122 
00123 
00124   
00125   // Generic array for the output values; needs to be properly resized depending on the operator type
00126   FieldContainer<double> vals;
00127 
00128   try{
00129     // exception #1: GRAD cannot be applied to HDIV functions 
00130     // resize vals to rank-3 container with dimensions (num. basis functions, num. points, arbitrary)
00131     vals.resize(hexBasis.getCardinality(), hexNodes.dimension(0), 3 );
00132     INTREPID_TEST_COMMAND( hexBasis.getValues(vals, hexNodes, OPERATOR_GRAD), throwCounter, nException );
00133 
00134     // exception #2: CURL cannot be applied to HDIV functions
00135     INTREPID_TEST_COMMAND( hexBasis.getValues(vals, hexNodes, OPERATOR_CURL), throwCounter, nException );
00136 
00137     // Exceptions 3-7: all bf tags/bf Ids below are wrong and should cause getDofOrdinal() and 
00138     // getDofTag() to access invalid array elements thereby causing bounds check exception
00139     // exception #3
00140     INTREPID_TEST_COMMAND( hexBasis.getDofOrdinal(3,0,0), throwCounter, nException );
00141     // exception #4
00142     INTREPID_TEST_COMMAND( hexBasis.getDofOrdinal(1,1,1), throwCounter, nException );
00143     // exception #5
00144     INTREPID_TEST_COMMAND( hexBasis.getDofOrdinal(0,4,1), throwCounter, nException );
00145     // exception #6
00146     INTREPID_TEST_COMMAND( hexBasis.getDofTag(12), throwCounter, nException );
00147     // exception #7
00148     INTREPID_TEST_COMMAND( hexBasis.getDofTag(-1), throwCounter, nException );
00149 
00150 #ifdef HAVE_INTREPID_DEBUG
00151     // Exceptions 8- test exception handling with incorrectly dimensioned input/output arrays
00152     // exception #8: input points array must be of rank-2
00153     FieldContainer<double> badPoints1(4, 5, 3);
00154     INTREPID_TEST_COMMAND( hexBasis.getValues(vals, badPoints1, OPERATOR_VALUE), throwCounter, nException );
00155     
00156     // exception #9 dimension 1 in the input point array must equal space dimension of the cell
00157     FieldContainer<double> badPoints2(4, 2);
00158     INTREPID_TEST_COMMAND( hexBasis.getValues(vals, badPoints2, OPERATOR_VALUE), throwCounter, nException );
00159     
00160     // exception #10 output values must be of rank-3 for OPERATOR_VALUE
00161     FieldContainer<double> badVals1(4, 3);
00162     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals1, hexNodes, OPERATOR_VALUE), throwCounter, nException );
00163  
00164     // exception #11 output values must be of rank-2 for OPERATOR_DIV
00165     FieldContainer<double> badVals2(4, 3, 3);
00166     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals2, hexNodes, OPERATOR_DIV), throwCounter, nException );
00167     
00168     // exception #12 incorrect 0th dimension of output array (must equal number of basis functions)
00169     FieldContainer<double> badVals3(hexBasis.getCardinality() + 1, hexNodes.dimension(0), 3);
00170     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals3, hexNodes, OPERATOR_VALUE), throwCounter, nException );
00171     
00172     // exception #13 incorrect 0th dimension of output array (must equal number of basis functions)
00173     FieldContainer<double> badVals4(hexBasis.getCardinality() + 1, hexNodes.dimension(0));
00174     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals4, hexNodes, OPERATOR_DIV), throwCounter, nException );
00175 
00176     // exception #14 incorrect 1st dimension of output array (must equal number of points)
00177     FieldContainer<double> badVals5(hexBasis.getCardinality(), hexNodes.dimension(0) + 1, 3);
00178     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals5, hexNodes, OPERATOR_VALUE), throwCounter, nException );
00179 
00180     // exception #15 incorrect 1st dimension of output array (must equal number of points)
00181     FieldContainer<double> badVals6(hexBasis.getCardinality(), hexNodes.dimension(0) + 1);
00182     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals6, hexNodes, OPERATOR_DIV), throwCounter, nException );
00183 
00184     // exception #16: incorrect 2nd dimension of output array (must equal the space dimension)
00185     FieldContainer<double> badVals7(hexBasis.getCardinality(), hexNodes.dimension(0), 4);
00186     INTREPID_TEST_COMMAND( hexBasis.getValues(badVals7, hexNodes, OPERATOR_VALUE), throwCounter, nException );
00187 #endif
00188     
00189   }
00190   catch (std::logic_error err) {
00191     *outStream << "UNEXPECTED ERROR !!! ----------------------------------------------------------\n";
00192     *outStream << err.what() << '\n';
00193     *outStream << "-------------------------------------------------------------------------------" << "\n\n";
00194     errorFlag = -1000;
00195   };
00196   
00197   // Check if number of thrown exceptions matches the one we expect 
00198   if (throwCounter != nException) {
00199     errorFlag++;
00200     *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00201   }
00202   
00203    *outStream \
00204      << "\n"
00205      << "===============================================================================\n"\
00206      << "| TEST 2: correctness of tag to enum and enum to tag lookups                  |\n"\
00207      << "===============================================================================\n";
00208   
00209    try{
00210      std::vector<std::vector<int> > allTags = hexBasis.getAllDofTags();
00211     
00212      // Loop over all tags, lookup the associated dof enumeration and then lookup the tag again
00213      for (unsigned i = 0; i < allTags.size(); i++) {
00214        int bfOrd  = hexBasis.getDofOrdinal(allTags[i][0], allTags[i][1], allTags[i][2]);
00215      
00216        std::vector<int> myTag = hexBasis.getDofTag(bfOrd);
00217         if( !( (myTag[0] == allTags[i][0]) &&
00218                (myTag[1] == allTags[i][1]) &&
00219                (myTag[2] == allTags[i][2]) &&
00220                (myTag[3] == allTags[i][3]) ) ) {
00221          errorFlag++;
00222          *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00223          *outStream << " getDofOrdinal( {" 
00224            << allTags[i][0] << ", " 
00225            << allTags[i][1] << ", " 
00226            << allTags[i][2] << ", " 
00227            << allTags[i][3] << "}) = " << bfOrd <<" but \n";   
00228          *outStream << " getDofTag(" << bfOrd << ") = { "
00229            << myTag[0] << ", " 
00230            << myTag[1] << ", " 
00231            << myTag[2] << ", " 
00232            << myTag[3] << "}\n";        
00233        }
00234      }
00235   
00236      // Now do the same but loop over basis functions
00237      for( int bfOrd = 0; bfOrd < hexBasis.getCardinality(); bfOrd++) {
00238        std::vector<int> myTag  = hexBasis.getDofTag(bfOrd);
00239        int myBfOrd = hexBasis.getDofOrdinal(myTag[0], myTag[1], myTag[2]);
00240        if( bfOrd != myBfOrd) {
00241          errorFlag++;
00242          *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00243          *outStream << " getDofTag(" << bfOrd << ") = { "
00244            << myTag[0] << ", " 
00245            << myTag[1] << ", " 
00246            << myTag[2] << ", " 
00247            << myTag[3] << "} but getDofOrdinal({" 
00248            << myTag[0] << ", " 
00249            << myTag[1] << ", " 
00250            << myTag[2] << ", " 
00251            << myTag[3] << "} ) = " << myBfOrd << "\n";
00252        }
00253      }
00254    }
00255    catch (std::logic_error err){
00256      *outStream << err.what() << "\n\n";
00257      errorFlag = -1000;
00258    };
00259   
00260    *outStream \
00261      << "\n"
00262      << "===============================================================================\n"\
00263      << "| TEST 3: correctness of basis function values                                |\n"\
00264      << "===============================================================================\n";
00265   
00266    outStream -> precision(20);
00267   
00268   // VALUE: Each row pair gives the 6x3 correct basis set values at an evaluation point: (P,F,D) layout
00269   double basisValues[] = {
00270     // basis function 0 (in from x==-1 plane, y and z are constant functions)
00271     1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
00272     1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
00273     // basis function 1 (out from x==1 plane, y and z are constant functions
00274     0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
00275     0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
00276     // basis function 2 (in from y==-1 plane, x and z are constant functions
00277     0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
00278     0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
00279     // basis function 3 (out from y == 1 plane, x and z are constant function
00280     0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0,
00281     0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0,
00282     // basis function 4 (in from z == -1 plane, x and y are constant function
00283     0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
00284     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
00285     // basis function 4 (out from z == 1 plane, x and y are constant function
00286     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00287     0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1
00288   };
00289   
00290   // DIV: each row gives the 6 correct values of the divergence of the 6 basis functions: (P,F) layout
00291   double basisDivs[] = {   
00292     -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5,
00293     0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
00294     -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5,
00295     0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
00296     -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5,
00297     0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5
00298   };
00299   
00300   try{
00301         
00302     // Dimensions for the output arrays:
00303     int numPoints = hexNodes.dimension(0);
00304     int numFields = hexBasis.getCardinality();
00305     int spaceDim  = hexBasis.getBaseCellTopology().getDimension();
00306     
00307     // Generic array for values and curls that will be properly sized before each call
00308     FieldContainer<double> vals;
00309     
00310     // Check VALUE of basis functions: resize vals to rank-3 container:
00311     vals.resize(numFields, numPoints, spaceDim);
00312     hexBasis.getValues(vals, hexNodes, OPERATOR_VALUE);
00313     for (int i = 0; i < numFields; i++) {
00314       for (int j = 0; j < numPoints; j++) {
00315         for (int k = 0; k < spaceDim; k++) {
00316           int l = k + i * numPoints * spaceDim + j * spaceDim;
00317            if (std::abs(vals(i,j,k) - basisValues[l]) > INTREPID_TOL) {
00318              errorFlag++;
00319              *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00320 
00321              // Output the multi-index of the value where the error is:
00322              *outStream << " At multi-index { ";
00323              *outStream << i << " ";*outStream << j << " ";*outStream << k << " ";
00324              *outStream << "}  computed value: " << vals(i,j,k)
00325                << " but reference value: " << basisValues[l] << "\n";
00326             }
00327          }
00328       }
00329     }
00330 
00331     // Check DIV of basis function: resize vals to rank-2 container
00332     vals.resize(numFields, numPoints);
00333     hexBasis.getValues(vals, hexNodes, OPERATOR_DIV);
00334     for (int i = 0; i < numFields; i++) {
00335       for (int j = 0; j < numPoints; j++) {
00336         int l =  i * numPoints + j;
00337            if (std::abs(vals(i,j) - basisDivs[l]) > INTREPID_TOL) {
00338              errorFlag++;
00339              *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00340 
00341              // Output the multi-index of the value where the error is:
00342              *outStream << " At multi-index { ";
00343              *outStream << i << " ";*outStream << j << " ";
00344              *outStream << "}  computed divergence component: " << vals(i,j)
00345                << " but reference divergence component: " << basisDivs[l] << "\n";
00346          }
00347       }
00348     }
00349     
00350    }    
00351   
00352   // Catch unexpected errors
00353   catch (std::logic_error err) {
00354     *outStream << err.what() << "\n\n";
00355     errorFlag = -1000;
00356   };
00357   
00358   if (errorFlag != 0)
00359     std::cout << "End Result: TEST FAILED\n";
00360   else
00361     std::cout << "End Result: TEST PASSED\n";
00362   
00363   // reset format state of std::cout
00364   std::cout.copyfmt(oldFormatState);
00365   
00366   return errorFlag;
00367 }