Intrepid
/usr/src/RPM/BUILD/trilinos10-10.6.4/packages/intrepid/test/Discretization/Basis/HCURL_TET_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 // 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_HCURL_TET_I1_FEM.hpp"
00037 #include "Teuchos_oblackholestream.hpp"
00038 #include "Teuchos_RCP.hpp"
00039 #include "Teuchos_GlobalMPISession.hpp"
00040 
00041 using namespace std;
00042 using namespace Intrepid;
00043 
00044 #define INTREPID_TEST_COMMAND( S , throwCounter, nException )                                                              \
00045 {                                                                                                                          \
00046   ++nException;                                                                                                            \
00047   try {                                                                                                                    \
00048     S ;                                                                                                                    \
00049   }                                                                                                                        \
00050   catch (std::logic_error err) {                                                                                           \
00051       ++throwCounter;                                                                                                      \
00052       *outStream << "Expected Error " << nException << " -------------------------------------------------------------\n"; \
00053       *outStream << err.what() << '\n';                                                                                    \
00054       *outStream << "-------------------------------------------------------------------------------" << "\n\n";           \
00055   };                                                                                                                       \
00056 }
00057 
00058 int main(int argc, char *argv[]) {
00059   
00060   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00061 
00062   // This little trick lets us print to std::cout only if
00063   // a (dummy) command-line argument is provided.
00064   int iprint     = argc - 1;
00065   Teuchos::RCP<std::ostream> outStream;
00066   Teuchos::oblackholestream bhs; // outputs nothing
00067   if (iprint > 0)
00068     outStream = Teuchos::rcp(&std::cout, false);
00069   else
00070     outStream = Teuchos::rcp(&bhs, false);
00071   
00072   // Save the format state of the original std::cout.
00073   Teuchos::oblackholestream oldFormatState;
00074   oldFormatState.copyfmt(std::cout);
00075   
00076   *outStream \
00077     << "===============================================================================\n" \
00078     << "|                                                                             |\n" \
00079     << "|                 Unit Test (Basis_HCURL_TET_I1_FEM)                          |\n" \
00080     << "|                                                                             |\n" \
00081     << "|     1) Conversion of Dof tags into Dof ordinals and back                    |\n" \
00082     << "|     2) Basis values for VALUE and CURL operators                            |\n" \
00083     << "|                                                                             |\n" \
00084     << "|  Questions? Contact  Pavel Bochev (pbboche@sandia.gov) or                   |\n" \
00085     << "|                      Denis Ridzal (dridzal@sandia.gov).                     |\n" \
00086     << "|                                                                             |\n" \
00087     << "|  Intrepid's website: http://trilinos.sandia.gov/packages/intrepid           |\n" \
00088     << "|  Trilinos website:   http://trilinos.sandia.gov                             |\n" \
00089     << "|                                                                             |\n" \
00090     << "===============================================================================\n"\
00091     << "| TEST 1: Basis creation, exception testing                                   |\n"\
00092     << "===============================================================================\n";
00093   
00094   // Define basis and error flag
00095   Basis_HCURL_TET_I1_FEM<double, FieldContainer<double> > tetBasis;
00096   int errorFlag = 0;
00097 
00098 // Define throw number for exception testing
00099   int nException     = 0;
00100   int throwCounter   = 0;
00101 
00102   // Define array containing the 4 vertices of the reference TET and its 6 edge centers.
00103   FieldContainer<double> tetNodes(10, 3);
00104   tetNodes(0,0) =  0.0;  tetNodes(0,1) =  0.0;  tetNodes(0,2) =  0.0;
00105   tetNodes(1,0) =  1.0;  tetNodes(1,1) =  0.0;  tetNodes(1,2) =  0.0;
00106   tetNodes(2,0) =  0.0;  tetNodes(2,1) =  1.0;  tetNodes(2,2) =  0.0;
00107   tetNodes(3,0) =  0.0;  tetNodes(3,1) =  0.0;  tetNodes(3,2) =  1.0;
00108   tetNodes(4,0) =  0.5;  tetNodes(4,1) =  0.0;  tetNodes(4,2) =  0.0;
00109   tetNodes(5,0) =  0.5;  tetNodes(5,1) =  0.5;  tetNodes(5,2) =  0.0;
00110   tetNodes(6,0) =  0.0;  tetNodes(6,1) =  0.5;  tetNodes(6,2) =  0.0;
00111   tetNodes(7,0) =  0.0;  tetNodes(7,1) =  0.0;  tetNodes(7,2) =  0.5;
00112   tetNodes(8,0) =  0.5;  tetNodes(8,1) =  0.0;  tetNodes(8,2) =  0.5;
00113   tetNodes(9,0) =  0.0;  tetNodes(9,1) =  0.5;  tetNodes(9,2) =  0.5;
00114 
00115 
00116   // Generic array for the output values; needs to be properly resized depending on the operator type
00117   FieldContainer<double> vals;
00118 
00119 
00120   try{
00121     // exception #1: GRAD cannot be applied to HCURL functions 
00122     // resize vals to rank-3 container with dimensions (num. basis functions, num. points, arbitrary)
00123      vals.resize(tetBasis.getCardinality(), tetNodes.dimension(0), 3 );
00124      INTREPID_TEST_COMMAND(  tetBasis.getValues(vals, tetNodes, OPERATOR_GRAD), throwCounter, nException );
00125 
00126     // exception #2: DIV cannot be applied to HCURL functions
00127     // resize vals to rank-2 container with dimensions (num. basis functions, num. points)
00128      vals.resize(tetBasis.getCardinality(), tetNodes.dimension(0) );
00129      INTREPID_TEST_COMMAND(  tetBasis.getValues(vals, tetNodes, OPERATOR_DIV), throwCounter, nException );
00130         
00131     // Exceptions 3-7: all bf tags/bf Ids below are wrong and should cause getDofOrdinal() and 
00132     // getDofTag() to access invalid array elements thereby causing bounds check exception
00133     // exception #3
00134     INTREPID_TEST_COMMAND( tetBasis.getDofOrdinal(3,0,0), throwCounter, nException );
00135     // exception #4
00136     INTREPID_TEST_COMMAND( tetBasis.getDofOrdinal(1,1,1), throwCounter, nException );
00137     // exception #5
00138     INTREPID_TEST_COMMAND( tetBasis.getDofOrdinal(0,4,1), throwCounter, nException );
00139     // exception #6
00140     INTREPID_TEST_COMMAND( tetBasis.getDofTag(7), throwCounter, nException );
00141     // exception #7
00142     INTREPID_TEST_COMMAND( tetBasis.getDofTag(-1), throwCounter, nException );
00143 
00144 #ifdef HAVE_INTREPID_DEBUG  
00145     // Exceptions 8-15 test exception handling with incorrectly dimensioned input/output arrays
00146     // exception #8: input points array must be of rank-2
00147     FieldContainer<double> badPoints1(4, 5, 3);
00148     INTREPID_TEST_COMMAND( tetBasis.getValues(vals,badPoints1,OPERATOR_VALUE), throwCounter, nException );
00149 
00150     // exception #9 dimension 1 in the input point array must equal space dimension of the cell
00151     FieldContainer<double> badPoints2(4, 2);
00152     INTREPID_TEST_COMMAND( tetBasis.getValues(vals,badPoints2,OPERATOR_VALUE), throwCounter, nException );
00153     
00154     // exception #10 output values must be of rank-3 for OPERATOR_VALUE
00155     FieldContainer<double> badVals1(4, 3);
00156     INTREPID_TEST_COMMAND( tetBasis.getValues(badVals1,tetNodes,OPERATOR_VALUE), throwCounter, nException );
00157  
00158     // exception #11 output values must be of rank-3 for OPERATOR_CURL
00159     INTREPID_TEST_COMMAND( tetBasis.getValues(badVals1,tetNodes,OPERATOR_CURL), throwCounter, nException );
00160     
00161     // exception #12 incorrect 0th dimension of output array (must equal number of basis functions)
00162     FieldContainer<double> badVals2(tetBasis.getCardinality() + 1, tetNodes.dimension(0), 3);
00163     INTREPID_TEST_COMMAND( tetBasis.getValues(badVals2,tetNodes,OPERATOR_VALUE), throwCounter, nException );
00164     
00165     // exception #13 incorrect 1st dimension of output array (must equal number of points)
00166     FieldContainer<double> badVals3(tetBasis.getCardinality(), tetNodes.dimension(0) + 1, 3);
00167     INTREPID_TEST_COMMAND( tetBasis.getValues(badVals3,tetNodes,OPERATOR_VALUE), throwCounter, nException );
00168 
00169     // exception #14: incorrect 2nd dimension of output array (must equal the space dimension)
00170     FieldContainer<double> badVals4(tetBasis.getCardinality(), tetNodes.dimension(0), 4);
00171     INTREPID_TEST_COMMAND( tetBasis.getValues(badVals4,tetNodes,OPERATOR_VALUE), throwCounter, nException );
00172     
00173     // exception #15: incorrect 2nd dimension of output array (must equal the space dimension)
00174     INTREPID_TEST_COMMAND( tetBasis.getValues(badVals4,tetNodes,OPERATOR_CURL), throwCounter, nException );
00175 #endif
00176     
00177   }
00178   catch (std::logic_error err) {
00179     *outStream << "UNEXPECTED ERROR !!! ----------------------------------------------------------\n";
00180     *outStream << err.what() << '\n';
00181     *outStream << "-------------------------------------------------------------------------------" << "\n\n";
00182     errorFlag = -1000;
00183   };
00184   
00185   // Check if number of thrown exceptions matches the one we expect
00186   // Note Teuchos throw number will not pick up exceptions 3-7 and therefore will not match.
00187   if (throwCounter != nException) {
00188     errorFlag++;
00189     *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00190   }
00191   
00192   *outStream \
00193     << "\n"
00194     << "===============================================================================\n"\
00195     << "| TEST 2: correctness of tag to enum and enum to tag lookups                  |\n"\
00196     << "===============================================================================\n";
00197   
00198   try{
00199     std::vector<std::vector<int> > allTags = tetBasis.getAllDofTags();
00200     
00201     // Loop over all tags, lookup the associated dof enumeration and then lookup the tag again
00202     for (unsigned i = 0; i < allTags.size(); i++) {
00203       int bfOrd  = tetBasis.getDofOrdinal(allTags[i][0], allTags[i][1], allTags[i][2]);
00204       
00205       std::vector<int> myTag = tetBasis.getDofTag(bfOrd);
00206        if( !( (myTag[0] == allTags[i][0]) &&
00207               (myTag[1] == allTags[i][1]) &&
00208               (myTag[2] == allTags[i][2]) &&
00209               (myTag[3] == allTags[i][3]) ) ) {
00210         errorFlag++;
00211         *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00212         *outStream << " getDofOrdinal( {" 
00213           << allTags[i][0] << ", " 
00214           << allTags[i][1] << ", " 
00215           << allTags[i][2] << ", " 
00216           << allTags[i][3] << "}) = " << bfOrd <<" but \n";   
00217         *outStream << " getDofTag(" << bfOrd << ") = { "
00218           << myTag[0] << ", " 
00219           << myTag[1] << ", " 
00220           << myTag[2] << ", " 
00221           << myTag[3] << "}\n";        
00222       }
00223     }
00224     
00225     // Now do the same but loop over basis functions
00226     for( int bfOrd = 0; bfOrd < tetBasis.getCardinality(); bfOrd++) {
00227       std::vector<int> myTag  = tetBasis.getDofTag(bfOrd);
00228       int myBfOrd = tetBasis.getDofOrdinal(myTag[0], myTag[1], myTag[2]);
00229       if( bfOrd != myBfOrd) {
00230         errorFlag++;
00231         *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00232         *outStream << " getDofTag(" << bfOrd << ") = { "
00233           << myTag[0] << ", " 
00234           << myTag[1] << ", " 
00235           << myTag[2] << ", " 
00236           << myTag[3] << "} but getDofOrdinal({" 
00237           << myTag[0] << ", " 
00238           << myTag[1] << ", " 
00239           << myTag[2] << ", " 
00240           << myTag[3] << "} ) = " << myBfOrd << "\n";
00241       }
00242     }
00243   }
00244   catch (std::logic_error err){
00245     *outStream << err.what() << "\n\n";
00246     errorFlag = -1000;
00247   };
00248   
00249   *outStream \
00250     << "\n"
00251     << "===============================================================================\n"\
00252     << "| TEST 3: correctness of basis function values                                |\n"\
00253     << "===============================================================================\n";
00254   
00255   outStream -> precision(20);
00256   
00257   // VALUE: Each row pair gives the 6x3 correct basis set values at an evaluation point: (P,F,D) layout
00258   double basisValues[] = {
00259     // 4 vertices
00260      1.0,0.,0.,  0.,0.,0.,  0.,-1.0,0.,  0.,0.,1.0, 
00261      0.,0.,0.,  0.,0.,0.,
00262 
00263      1.0,1.0,1.0,  0.,1.,0.,  0.,0.,0.,  0.,0.,0.,
00264      0.,0.,1.,  0.,0.,0.,
00265 
00266      0.,0.,0.,  -1.,0.,0.,  -1.0,-1.0,-1.0,
00267      0.,0.,0.,  0.,0.,0.,  0.,0.,1.,
00268 
00269      0.,0.,0.,  0.,0.,0.,  0.,0.,0.,  1.0,1.0,1.0,
00270      -1.,0.,0.,  0.,-1.,0.,
00271 
00272     // 6 edge centers
00273      1.0,0.5,0.5,  0.,0.5,0.,  0.,-0.5,0.,
00274      0.,0.,0.5,  0.,0.,0.5,  0.,0.,0.,
00275 
00276      0.5,0.5,0.5,  -0.5,0.5,0.,
00277     -0.5,-0.5,-0.5,  0.,0.,0.,  0.,0.,0.5,  0.,0.,0.5,
00278 
00279      0.5,0.,0.,  -0.5,0.,0.,  -0.5,-1.0,-0.5,
00280      0.,0.,0.5,  0.,0.,0.,  0.,0.,0.5,
00281  
00282      0.5,0.,0.,  0.,0.,0.,  0.,-0.5,0.,  0.5,0.5,1.0,
00283      -0.5,0.,0.,  0.,-0.5,0.,
00284 
00285      0.5,0.5,0.5,  0.,0.5,0.,  0.,0.,0., 0.5,0.5,0.5,
00286     -0.5,0.,0.5,  0.,-0.5,0.,
00287 
00288      0.,0.,0.,  -0.5,0.,0.,  -0.5,-0.5,-0.5,  0.5,0.5,0.5,
00289     -0.5,0.,0.,  0.,-0.5,0.5
00290   };
00291   
00292   // CURL: each row pair gives the 3x12 correct values of the curls of the 12 basis functions: (P,F,D) layout
00293   double basisCurls[] = {   
00294     // 4 vertices
00295      0.,-2.0,2.0,  0.,0.,2.0,  -2.0,0.,2.0,  -2.0,2.0,0.,
00296      0.,-2.0,0.,  2.0,0.,0.,
00297 
00298      0.,-2.0,2.0,  0.,0.,2.0,  -2.0,0.,2.0,  -2.0,2.0,0.,
00299      0.,-2.0,0.,  2.0,0.,0.,
00300 
00301      0.,-2.0,2.0,  0.,0.,2.0,  -2.0,0.,2.0,  -2.0,2.0,0.,
00302      0.,-2.0,0.,  2.0,0.,0.,
00303 
00304      0.,-2.0,2.0,  0.,0.,2.0,  -2.0,0.,2.0,  -2.0,2.0,0.,
00305      0.,-2.0,0.,  2.0,0.,0.,
00306 
00307     // 6 edge centers
00308      0.,-2.0,2.0,  0.,0.,2.0,  -2.0,0.,2.0,  -2.0,2.0,0.,
00309      0.,-2.0,0.,  2.0,0.,0.,
00310 
00311      0.,-2.0,2.0,  0.,0.,2.0,  -2.0,0.,2.0,  -2.0,2.0,0.,
00312      0.,-2.0,0.,  2.0,0.,0.,
00313 
00314      0.,-2.0,2.0,  0.,0.,2.0,  -2.0,0.,2.0,  -2.0,2.0,0.,
00315      0.,-2.0,0.,  2.0,0.,0.,
00316 
00317      0.,-2.0,2.0,  0.,0.,2.0,  -2.0,0.,2.0,  -2.0,2.0,0.,
00318      0.,-2.0,0.,  2.0,0.,0.,
00319 
00320      0.,-2.0,2.0,  0.,0.,2.0,  -2.0,0.,2.0,  -2.0,2.0,0.,
00321      0.,-2.0,0.,  2.0,0.,0.,
00322 
00323      0.,-2.0,2.0,  0.,0.,2.0,  -2.0,0.,2.0,  -2.0,2.0,0.,
00324      0.,-2.0,0.,  2.0,0.,0.,
00325   };
00326   
00327   try{
00328         
00329     // Dimensions for the output arrays:
00330     int numFields = tetBasis.getCardinality();
00331     int numPoints = tetNodes.dimension(0);
00332     int spaceDim  = tetBasis.getBaseCellTopology().getDimension();
00333     
00334     // Generic array for values and curls that will be properly sized before each call
00335     FieldContainer<double> vals;
00336     
00337     // Check VALUE of basis functions: resize vals to rank-3 container:
00338     vals.resize(numFields, numPoints, spaceDim);
00339     tetBasis.getValues(vals, tetNodes, OPERATOR_VALUE);
00340     for (int i = 0; i < numFields; i++) {
00341       for (int j = 0; j < numPoints; j++) {
00342         for (int k = 0; k < spaceDim; k++) {
00343           
00344           // compute offset for (P,F,D) data layout: indices are P->j, F->i, D->k
00345            int l = k + i * spaceDim + j * spaceDim * numFields;
00346            if (std::abs(vals(i,j,k) - basisValues[l]) > INTREPID_TOL) {
00347              errorFlag++;
00348              *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00349 
00350              // Output the multi-index of the value where the error is:
00351              *outStream << " At multi-index { ";
00352              *outStream << i << " ";*outStream << j << " ";*outStream << k << " ";
00353              *outStream << "}  computed value: " << vals(i,j,k)
00354                << " but reference value: " << basisValues[l] << "\n";
00355             }
00356          }
00357       }
00358     }
00359     
00360     // Check CURL of basis function: resize vals to rank-3 container
00361     vals.resize(numFields, numPoints, spaceDim);
00362     tetBasis.getValues(vals, tetNodes, OPERATOR_CURL);
00363     for (int i = 0; i < numFields; i++) {
00364       for (int j = 0; j < numPoints; j++) {
00365         for (int k = 0; k < spaceDim; k++) {
00366           
00367           // compute offset for (P,F,D) data layout: indices are P->j, F->i, D->k
00368            int l = k + i * spaceDim + j * spaceDim * numFields;
00369            if (std::abs(vals(i,j,k) - basisCurls[l]) > INTREPID_TOL) {
00370              errorFlag++;
00371              *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00372 
00373              // Output the multi-index of the value where the error is:
00374              *outStream << " At multi-index { ";
00375              *outStream << i << " ";*outStream << j << " ";*outStream << k << " ";
00376              *outStream << "}  computed curl component: " << vals(i,j,k)
00377                << " but reference curl component: " << basisCurls[l] << "\n";
00378             }
00379          }
00380       }
00381     }
00382 
00383    }    
00384   
00385   // Catch unexpected errors
00386   catch (std::logic_error err) {
00387     *outStream << err.what() << "\n\n";
00388     errorFlag = -1000;
00389   };
00390   
00391   if (errorFlag != 0)
00392     std::cout << "End Result: TEST FAILED\n";
00393   else
00394     std::cout << "End Result: TEST PASSED\n";
00395   
00396   // reset format state of std::cout
00397   std::cout.copyfmt(oldFormatState);
00398   
00399   return errorFlag;
00400 }