Intrepid
http://trilinos.sandia.gov/packages/docs/r10.12/packages/intrepid/test/Discretization/Basis/HDIV_QUAD_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_QUAD_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_QUAD_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   // get points first
00111   const int deg = 1;
00112   shards::CellTopology line(shards::getCellTopologyData< shards::Line<> >()); 
00113   FieldContainer<double> closedPts(PointTools::getLatticeSize(line,deg),1);
00114   FieldContainer<double> openPts(PointTools::getLatticeSize(line,deg+1,1),1);
00115   PointTools::getLattice<double,FieldContainer<double> >(closedPts,line,deg);
00116   PointTools::getLattice<double,FieldContainer<double> >(openPts,line,deg+1,1);
00117 
00118   Basis_HDIV_QUAD_In_FEM<double, FieldContainer<double> > quadBasis(deg,closedPts,openPts);
00119   int errorFlag = 0;
00120 
00121   // Initialize throw counter for exception testing
00122   int nException     = 0;
00123   int throwCounter   = 0;
00124 
00125   // Array with a lattice of 9 points on the quad
00126   FieldContainer<double> quadNodes(9, 2);
00127   quadNodes(0,0) = -1.0;  quadNodes(0,1) = -1.0;  
00128   quadNodes(1,0) =  0.0;  quadNodes(1,1) = -1.0;
00129   quadNodes(2,0) =  1.0;  quadNodes(2,1) = -1.0;
00130   quadNodes(3,0) = -1.0;  quadNodes(3,1) =  0.0;  
00131   quadNodes(4,0) =  0.0;  quadNodes(4,1) =  0.0;
00132   quadNodes(5,0) =  1.0;  quadNodes(5,1) =  0.0;
00133   quadNodes(6,0) = -1.0;  quadNodes(6,1) =  1.0;  
00134   quadNodes(7,0) =  0.0;  quadNodes(7,1) =  1.0;
00135   quadNodes(8,0) =  1.0;  quadNodes(8,1) =  1.0;
00136   
00137   // Generic array for the output values; needs to be properly resized depending on the operator type
00138   FieldContainer<double> vals;
00139 
00140   try{
00141     
00142     int spaceDim  = quadBasis.getBaseCellTopology().getDimension();
00143     
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(quadBasis.getCardinality(), quadNodes.dimension(0), spaceDim );
00147     INTREPID_TEST_COMMAND( quadBasis.getValues(vals, quadNodes, OPERATOR_GRAD), throwCounter, nException );
00148 
00149     // exception #2: CURL cannot be applied to HDIV functions
00150     INTREPID_TEST_COMMAND( quadBasis.getValues(vals, quadNodes, 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( quadBasis.getDofOrdinal(3,0,0), throwCounter, nException );
00156     // exception #4
00157     INTREPID_TEST_COMMAND( quadBasis.getDofOrdinal(1,1,1), throwCounter, nException );
00158     // exception #5
00159     INTREPID_TEST_COMMAND( quadBasis.getDofOrdinal(0,4,1), throwCounter, nException );
00160     // exception #6
00161     INTREPID_TEST_COMMAND( quadBasis.getDofTag(12), throwCounter, nException );
00162     // exception #7
00163     INTREPID_TEST_COMMAND( quadBasis.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( quadBasis.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, spaceDim + 1);
00173     INTREPID_TEST_COMMAND( quadBasis.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, 5);
00177     INTREPID_TEST_COMMAND( quadBasis.getValues(badVals1, quadNodes, OPERATOR_VALUE), throwCounter, nException );
00178  
00179     // exception #11 output values must be of rank-2 for OPERATOR_DIV
00180     FieldContainer<double> badVals2(4, 5, spaceDim);
00181     INTREPID_TEST_COMMAND( quadBasis.getValues(badVals2, quadNodes, OPERATOR_DIV), throwCounter, nException );
00182     
00183     // exception #12 incorrect 0th dimension of output array (must equal number of basis functions)
00184     FieldContainer<double> badVals3(quadBasis.getCardinality() + 1, quadNodes.dimension(0), spaceDim);
00185     INTREPID_TEST_COMMAND( quadBasis.getValues(badVals3, quadNodes, OPERATOR_VALUE), throwCounter, nException );
00186     
00187     // exception #13 incorrect 0th dimension of output array (must equal number of basis functions)
00188     FieldContainer<double> badVals4(quadBasis.getCardinality() + 1, quadNodes.dimension(0));
00189     INTREPID_TEST_COMMAND( quadBasis.getValues(badVals4, quadNodes, OPERATOR_DIV), throwCounter, nException );
00190 
00191     // exception #14 incorrect 1st dimension of output array (must equal number of points)
00192     FieldContainer<double> badVals5(quadBasis.getCardinality(), quadNodes.dimension(0) + 1, spaceDim);
00193     INTREPID_TEST_COMMAND( quadBasis.getValues(badVals5, quadNodes, OPERATOR_VALUE), throwCounter, nException );
00194 
00195     // exception #15 incorrect 1st dimension of output array (must equal number of points)
00196     FieldContainer<double> badVals6(quadBasis.getCardinality(), quadNodes.dimension(0) + 1);
00197     INTREPID_TEST_COMMAND( quadBasis.getValues(badVals6, quadNodes, OPERATOR_DIV), throwCounter, nException );
00198 
00199     // exception #16: incorrect 2nd dimension of output array (must equal the space dimension)
00200     FieldContainer<double> badVals7(quadBasis.getCardinality(), quadNodes.dimension(0), spaceDim + 1);
00201     INTREPID_TEST_COMMAND( quadBasis.getValues(badVals7, quadNodes, 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 = quadBasis.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  = quadBasis.getDofOrdinal(allTags[i][0], allTags[i][1], allTags[i][2]);
00230       
00231       std::vector<int> myTag = quadBasis.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 < quadBasis.getCardinality(); bfOrd++) {
00253       std::vector<int> myTag  = quadBasis.getDofTag(bfOrd);
00254       int myBfOrd = quadBasis.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: 
00284   double basisValues[] = {
00285     // first bf, first row of points
00286     1.0, 0.0, 0.5, 0.0, 0.0, 0.0, 
00287     // first bf, second row of points
00288     1.0, 0.0, 0.5, 0.0, 0.0, 0.0, 
00289     // first bf, third row of points
00290     1.0, 0.0, 0.5, 0.0, 0.0, 0.0, 
00291     // second bf, first row of points
00292     0.0, 0.0, 0.5, 0.0, 1.0, 0.0, 
00293     // second bf, second row of points
00294     0.0, 0.0, 0.5, 0.0, 1.0, 0.0, 
00295     // second bf, third row of points
00296     0.0, 0.0, 0.5, 0.0, 1.0, 0.0, 
00297     // third bf, first row of points
00298     0.0, 1.0, 0.0, 1.0, 0.0, 1.0,
00299     // third bf, second row of points
00300     0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 
00301     // third bf, third row of points
00302     0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
00303     // fourth bf, first row of points
00304     0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
00305     // fourth bf, second row of points
00306     0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 
00307     // fourth bf, third row of points
00308     0.0, 1.0, 0.0, 1.0, 0.0, 1.0
00309   };
00310   
00311   // DIV: each row gives the 6 correct values of the divergence of the 6 basis functions: (P,F) layout
00312   double basisDivs[] = {   
00313     // bf0 has -0.5 divergence
00314     -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, 
00315     // bf1 has 0.5 divergencece
00316     0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 
00317     // bf2 has -0.5 divergence
00318     -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, 
00319     // bf3 has 0.5 divergence
00320     0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 
00321   };
00322   
00323   try{
00324         
00325     // Dimensions for the output arrays:
00326     int numPoints = quadNodes.dimension(0);
00327     int numFields = quadBasis.getCardinality();
00328     int spaceDim  = quadBasis.getBaseCellTopology().getDimension();
00329     
00330     // Generic array for values and curls that will be properly sized before each call
00331     FieldContainer<double> vals;
00332     
00333     // Check VALUE of basis functions: resize vals to rank-3 container:
00334     vals.resize(numFields, numPoints, spaceDim);
00335     quadBasis.getValues(vals, quadNodes, OPERATOR_VALUE);
00336     for (int i = 0; i < numFields; i++) {
00337       for (int j = 0; j < numPoints; j++) {
00338         for (int k = 0; k < spaceDim; k++) {
00339           // compute offset for (F,P,D) data layout: indices are F->i, P-> j, D->k
00340           int l = i * numPoints * spaceDim + j * spaceDim + k; 
00341            if (std::abs(vals(i,j,k) - basisValues[l]) > INTREPID_TOL) {
00342              errorFlag++;
00343              *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00344 
00345              // Output the multi-index of the value where the error is:
00346              *outStream << " At multi-index { ";
00347              *outStream << i << " ";*outStream << j << " ";*outStream << k << " ";
00348              *outStream << "}  computed value: " << vals(i,j,k)
00349                << " but reference value: " << basisValues[l] << "\n";
00350             }
00351          }
00352       }
00353     }
00354 
00355     // Check DIV of basis function: resize vals to rank-2 container
00356     vals.resize(numFields, numPoints);
00357     quadBasis.getValues(vals, quadNodes, OPERATOR_DIV);
00358     for (int i = 0; i < numFields; i++) {
00359       for (int j = 0; j < numPoints; j++) {
00360           int l =  j + i * numPoints;
00361            if (std::abs(vals(i,j) - basisDivs[l]) > INTREPID_TOL) {
00362              errorFlag++;
00363              *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00364 
00365              // Output the multi-index of the value where the error is:
00366              *outStream << " At multi-index { ";
00367              *outStream << i << " ";*outStream << j << " ";
00368              *outStream << "}  computed divergence component: " << vals(i,j)
00369                << " but reference divergence component: " << basisDivs[l] << "\n";
00370          }
00371       }
00372     }
00373     
00374    }    
00375   
00376   // Catch unexpected errors
00377   catch (std::logic_error err) {
00378     *outStream << err.what() << "\n\n";
00379     errorFlag = -1000;
00380   };
00381   
00382   if (errorFlag != 0)
00383     std::cout << "End Result: TEST FAILED\n";
00384   else
00385     std::cout << "End Result: TEST PASSED\n";
00386   
00387   // reset format state of std::cout
00388   std::cout.copyfmt(oldFormatState);
00389   
00390   return errorFlag;
00391 }