Intrepid
http://trilinos.sandia.gov/packages/docs/r10.12/packages/intrepid/test/Discretization/Basis/HGRAD_LINE_C1_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 
00049 #include "Intrepid_FieldContainer.hpp"
00050 #include "Intrepid_HGRAD_LINE_C1_FEM.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_HGRAD_LINE_C1_FEM)                         |\n" \
00094     << "|                                                                             |\n" \
00095     << "|     1) Conversion of Dof tags into Dof ordinals and back                    |\n" \
00096     << "|     2) Basis values for VALUE, GRAD, CURL, DIV, and Dk operators            |\n" \
00097     << "|                                                                             |\n" \
00098     << "|  Questions? Contact  Pavel Bochev  (pbboche@sandia.gov),                    |\n" \
00099     << "|                      Denis Ridzal  (dridzal@sandia.gov),                    |\n" \
00100     << "|                      Kara Peterson (dridzal@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   
00110   // Define basis and error flag
00111   Basis_HGRAD_LINE_C1_FEM<double, FieldContainer<double> > lineBasis;
00112   int errorFlag = 0;
00113 
00114   // Initialize throw counter for exception testing
00115   int nException     = 0;
00116   int throwCounter   = 0;
00117   
00118   // Define array containing the 2 vertices of the reference Line, its center and another point  
00119   FieldContainer<double> lineNodes(4, 1);
00120   lineNodes(0,0) =  -1.0;
00121   lineNodes(1,0) =   1.0;
00122   lineNodes(2,0) =   0.0;
00123   lineNodes(3,0) =   0.5;
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     // resize vals to rank-2 container with dimensions (num. points, num. basis functions)
00130     vals.resize(lineBasis.getCardinality(), lineNodes.dimension(0) );
00131     
00132     // Exceptions 1-5: all bf tags/bf Ids below are wrong and should cause getDofOrdinal() and 
00133     // getDofTag() to access invalid array elements thereby causing bounds check exception
00134     // exception #1
00135     INTREPID_TEST_COMMAND( lineBasis.getDofOrdinal(2,0,0), throwCounter, nException );
00136     // exception #2
00137     INTREPID_TEST_COMMAND( lineBasis.getDofOrdinal(1,1,1), throwCounter, nException );
00138     // exception #3
00139     INTREPID_TEST_COMMAND( lineBasis.getDofOrdinal(0,4,0), throwCounter, nException );
00140     // exception #4
00141     INTREPID_TEST_COMMAND( lineBasis.getDofTag(5), throwCounter, nException );
00142     // exception #5
00143     INTREPID_TEST_COMMAND( lineBasis.getDofTag(-1), throwCounter, nException );
00144 
00145 #ifdef HAVE_INTREPID_DEBUG
00146     // Exceptions 6-17 test exception handling with incorrectly dimensioned input/output arrays
00147     // exception #6: input points array must be of rank-2
00148     FieldContainer<double> badPoints1(4, 5, 3);
00149     INTREPID_TEST_COMMAND( lineBasis.getValues(vals, badPoints1, OPERATOR_VALUE), throwCounter, nException );
00150     
00151     // exception #7 dimension 1 in the input point array must equal space dimension of the cell
00152     FieldContainer<double> badPoints2(4, 2);
00153     INTREPID_TEST_COMMAND( lineBasis.getValues(vals, badPoints2, OPERATOR_VALUE), throwCounter, nException );
00154     
00155     // exception #8 output values must be of rank-2 for OPERATOR_VALUE
00156     FieldContainer<double> badVals1(4, 3, 1);
00157     INTREPID_TEST_COMMAND( lineBasis.getValues(badVals1, lineNodes, OPERATOR_VALUE), throwCounter, nException );
00158     
00159     // exception #9 output values must be of rank-3 for OPERATOR_GRAD
00160     FieldContainer<double> badVals2(4, 3);
00161     INTREPID_TEST_COMMAND( lineBasis.getValues(badVals2, lineNodes, OPERATOR_GRAD), throwCounter, nException );
00162 
00163     // exception #10 output values must be of rank-3 for OPERATOR_DIV
00164     INTREPID_TEST_COMMAND( lineBasis.getValues(badVals2, lineNodes, OPERATOR_DIV), throwCounter, nException );
00165     
00166     // exception #11 output values must be of rank-3 for OPERATOR_CURL
00167     INTREPID_TEST_COMMAND( lineBasis.getValues(badVals2, lineNodes, OPERATOR_CURL), throwCounter, nException );
00168     
00169     // exception #12 output values must be of rank-3 for OPERATOR_D2
00170     INTREPID_TEST_COMMAND( lineBasis.getValues(badVals2, lineNodes, OPERATOR_D2), throwCounter, nException );
00171     
00172     // exception #13 incorrect 1st dimension of output array (must equal number of basis functions)
00173     FieldContainer<double> badVals3(lineBasis.getCardinality() + 1, lineNodes.dimension(0));
00174     INTREPID_TEST_COMMAND( lineBasis.getValues(badVals3, lineNodes, OPERATOR_VALUE), throwCounter, nException );
00175     
00176     // exception #14 incorrect 0th dimension of output array (must equal number of points)
00177     FieldContainer<double> badVals4(lineBasis.getCardinality(), lineNodes.dimension(0) + 1);
00178     INTREPID_TEST_COMMAND( lineBasis.getValues(badVals4, lineNodes, OPERATOR_VALUE), throwCounter, nException );
00179     
00180     // exception #15: incorrect 2nd dimension of output array (must equal the space dimension)
00181     FieldContainer<double> badVals5(lineBasis.getCardinality(), lineNodes.dimension(0), 2);
00182     INTREPID_TEST_COMMAND( lineBasis.getValues(badVals5, lineNodes, OPERATOR_GRAD), throwCounter, nException );
00183     
00184     // exception #16: incorrect 2nd dimension of output array (must equal D2 cardinality in 1D)
00185     FieldContainer<double> badVals6(lineBasis.getCardinality(), lineNodes.dimension(0), 40);
00186     INTREPID_TEST_COMMAND( lineBasis.getValues(badVals6, lineNodes, OPERATOR_D2), throwCounter, nException );
00187     
00188     // exception #17: incorrect 2nd dimension of output array (must equal D3 cardinality in 1D)
00189     INTREPID_TEST_COMMAND( lineBasis.getValues(badVals6, lineNodes, OPERATOR_D3), throwCounter, nException );
00190 #endif
00191     
00192   }
00193   catch (std::logic_error err) {
00194     *outStream << "UNEXPECTED ERROR !!! ----------------------------------------------------------\n";
00195     *outStream << err.what() << '\n';
00196     *outStream << "-------------------------------------------------------------------------------" << "\n\n";
00197     errorFlag = -1000;
00198   };
00199   
00200   // Check if number of thrown exceptions matches the one we expect
00201   if (throwCounter != nException) {
00202     errorFlag++;
00203     *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00204   }
00205   
00206   *outStream \
00207     << "\n"
00208     << "===============================================================================\n"\
00209     << "| TEST 2: correctness of tag to enum and enum to tag lookups                  |\n"\
00210     << "===============================================================================\n";
00211 
00212   try{
00213     std::vector<std::vector<int> > allTags = lineBasis.getAllDofTags();
00214     
00215     // Loop over all tags, lookup the associated dof enumeration and then lookup the tag again
00216     for (unsigned i = 0; i < allTags.size(); i++) {
00217       int bfOrd  = lineBasis.getDofOrdinal(allTags[i][0], allTags[i][1], allTags[i][2]);
00218       
00219       std::vector<int> myTag = lineBasis.getDofTag(bfOrd);
00220       if( !( (myTag[0] == allTags[i][0]) &&
00221              (myTag[1] == allTags[i][1]) &&
00222              (myTag[2] == allTags[i][2]) &&
00223              (myTag[3] == allTags[i][3]) ) ) {
00224         errorFlag++;
00225         *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00226         *outStream << " getDofOrdinal( {" 
00227           << allTags[i][0] << ", " 
00228           << allTags[i][1] << ", " 
00229           << allTags[i][2] << ", " 
00230           << allTags[i][3] << "}) = " << bfOrd <<" but \n";   
00231         *outStream << " getDofTag(" << bfOrd << ") = { "
00232           << myTag[0] << ", " 
00233           << myTag[1] << ", " 
00234           << myTag[2] << ", " 
00235           << myTag[3] << "}\n";        
00236       }
00237     }
00238     
00239     // Now do the same but loop over basis functions
00240     for( int bfOrd = 0; bfOrd < lineBasis.getCardinality(); bfOrd++) {
00241       std::vector<int> myTag  = lineBasis.getDofTag(bfOrd);
00242       int myBfOrd = lineBasis.getDofOrdinal(myTag[0], myTag[1], myTag[2]);
00243       if( bfOrd != myBfOrd) {
00244         errorFlag++;
00245         *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00246         *outStream << " getDofTag(" << bfOrd << ") = { "
00247           << myTag[0] << ", " 
00248           << myTag[1] << ", " 
00249           << myTag[2] << ", " 
00250           << myTag[3] << "} but getDofOrdinal({" 
00251           << myTag[0] << ", " 
00252           << myTag[1] << ", " 
00253           << myTag[2] << ", " 
00254           << myTag[3] << "} ) = " << myBfOrd << "\n";
00255       }
00256     }
00257   }
00258   catch (std::logic_error err){
00259     *outStream << err.what() << "\n\n";
00260     errorFlag = -1000;
00261   };
00262   
00263   *outStream \
00264     << "\n"
00265     << "===============================================================================\n"\
00266     << "| TEST 3: correctness of basis function values                                |\n"\
00267     << "===============================================================================\n";
00268 
00269   outStream -> precision(20);
00270 
00271   // VALUE: Each row gives the 2 correct basis set values at an evaluation point
00272   double basisValues[] = {
00273     1.0, 0.0,
00274     0.0, 1.0,
00275     0.5, 0.5,
00276     0.25, 0.75
00277   };
00278 
00279   // GRAD, DIV, CURL and D1: each row gives the 2 correct values of the gradients of the 2 basis functions
00280   double basisDerivs[] = {
00281     -0.5, 0.5,
00282     -0.5, 0.5,
00283     -0.5, 0.5,
00284     -0.5, 0.5
00285   };
00286 
00287   try{
00288     
00289     // Dimensions for the output arrays:
00290     int numFields = lineBasis.getCardinality();
00291     int numPoints = lineNodes.dimension(0);
00292     int spaceDim  = lineBasis.getBaseCellTopology().getDimension();
00293     
00294     // Generic array for values, grads, curls, etc. that will be properly sized before each call
00295     FieldContainer<double> vals;
00296     
00297     // Check VALUE of basis functions: resize vals to rank-2 container:
00298     vals.resize(numFields, numPoints);
00299     lineBasis.getValues(vals, lineNodes, OPERATOR_VALUE);
00300     for (int i = 0; i < numFields; i++) {
00301       for (int j = 0; j < numPoints; j++) {
00302           int l =  i + j * numFields;
00303            if (std::abs(vals(i,j) - basisValues[l]) > INTREPID_TOL) {
00304              errorFlag++;
00305              *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00306 
00307              // Output the multi-index of the value where the error is:
00308              *outStream << " At multi-index { ";
00309              *outStream << i << " ";*outStream << j << " ";
00310              *outStream << "}  computed value: " << vals(i,j)
00311                << " but reference value: " << basisValues[l] << "\n";
00312          }
00313       }
00314     }
00315     
00316     // Check derivatives of basis function: resize vals to rank-3 container
00317     vals.resize(numFields, numPoints, spaceDim);
00318     lineBasis.getValues(vals, lineNodes, OPERATOR_GRAD);
00319     for (int i = 0; i < numFields; i++) {
00320       for (int j = 0; j < numPoints; j++) {
00321         for (int k = 0; k < spaceDim; k++) {
00322            int l = k + i * spaceDim + j * spaceDim * numFields;
00323            if (std::abs(vals(i,j,k) - basisDerivs[l]) > INTREPID_TOL) {
00324              errorFlag++;
00325              *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00326 
00327              // Output the multi-index of the value where the error is:
00328              *outStream << " At multi-index { ";
00329              *outStream << i << " ";*outStream << j << " ";*outStream << k << " ";
00330              *outStream << "}  computed grad component: " << vals(i,j,k)
00331                << " but reference grad component: " << basisDerivs[l] << "\n";
00332             }
00333          }
00334       }
00335     }
00336 
00337     // Check D1 of basis function (do not resize vals because it has the correct size: D1 = GRAD)
00338     lineBasis.getValues(vals, lineNodes, OPERATOR_D1);
00339     for (int i = 0; i < numFields; i++) {
00340       for (int j = 0; j < numPoints; j++) {
00341         for (int k = 0; k < spaceDim; k++) {
00342            int l = k + i * spaceDim + j * spaceDim * numFields;
00343            if (std::abs(vals(i,j,k) - basisDerivs[l]) > INTREPID_TOL) {
00344              errorFlag++;
00345              *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00346 
00347              // Output the multi-index of the value where the error is:
00348              *outStream << " At multi-index { ";
00349              *outStream << i << " ";*outStream << j << " ";*outStream << k << " ";
00350              *outStream << "}  computed D1 component: " << vals(i,j,k)
00351                << " but reference D1 component: " << basisDerivs[l] << "\n";
00352             }
00353          }
00354       }
00355     }
00356 
00357     
00358     // Check CURL of basis function: resize vals just for illustration! 
00359     vals.resize(numFields, numPoints, spaceDim);
00360     lineBasis.getValues(vals, lineNodes, OPERATOR_CURL);
00361     for (int i = 0; i < numFields; i++) {
00362       for (int j = 0; j < numPoints; j++) {
00363         for (int k = 0; k < spaceDim; k++) {
00364            int l = k + i * spaceDim + j * spaceDim * numFields;
00365            if (std::abs(vals(i,j,k) - basisDerivs[l]) > INTREPID_TOL) {
00366              errorFlag++;
00367              *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00368 
00369              // Output the multi-index of the value where the error is:
00370              *outStream << " At multi-index { ";
00371              *outStream << i << " ";*outStream << j << " ";*outStream << k << " ";
00372              *outStream << "}  computed curl component: " << vals(i,j,k)
00373                << " but reference curl component: " << basisDerivs[l] << "\n";
00374             }
00375          }
00376       }
00377     }
00378 
00379 
00380     // Check DIV of basis function (do not resize vals because it has the correct size: DIV = CURL)
00381     lineBasis.getValues(vals, lineNodes, OPERATOR_DIV);
00382     for (int i = 0; i < numFields; i++) {
00383       for (int j = 0; j < numPoints; j++) {
00384         for (int k = 0; k < spaceDim; k++) {
00385            int l = k + i * spaceDim + j * spaceDim * numFields;
00386            if (std::abs(vals(i,j,k) - basisDerivs[l]) > INTREPID_TOL) {
00387              errorFlag++;
00388              *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00389 
00390              // Output the multi-index of the value where the error is:
00391              *outStream << " At multi-index { ";
00392              *outStream << i << " ";*outStream << j << " ";*outStream << k << " ";
00393              *outStream << "}  computed D1 component: " << vals(i,j,k)
00394                << " but reference D1 component: " << basisDerivs[l] << "\n";
00395             }
00396          }
00397       }
00398     }
00399 
00400 
00401     // Check all higher derivatives - must be zero. 
00402     for(EOperator op = OPERATOR_D2; op < OPERATOR_MAX; op++) {
00403       
00404       // The last dimension is the number of kth derivatives and needs to be resized for every Dk
00405       int DkCardin  = Intrepid::getDkCardinality(op, spaceDim);
00406       vals.resize(numFields, numPoints, DkCardin);    
00407       
00408       lineBasis.getValues(vals, lineNodes, op);
00409       for (int i = 0; i < vals.size(); i++) {
00410         if (std::abs(vals[i]) > INTREPID_TOL) {
00411           errorFlag++;
00412           *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
00413           
00414           // Get the multi-index of the value where the error is and the operator order
00415           std::vector<int> myIndex;
00416           vals.getMultiIndex(myIndex,i);
00417           int ord = Intrepid::getOperatorOrder(op);
00418           *outStream << " At multi-index { ";
00419           for(int j = 0; j < vals.rank(); j++) {
00420             *outStream << myIndex[j] << " ";
00421           }
00422           *outStream << "}  computed D"<< ord <<" component: " << vals[i] 
00423             << " but reference D" << ord << " component:  0 \n";
00424         }
00425       }
00426     }    
00427   }
00428 
00429   // Catch unexpected errors
00430   catch (std::logic_error err) {
00431     *outStream << err.what() << "\n\n";
00432     errorFlag = -1000;
00433   };
00434 
00435   if (errorFlag != 0)
00436     std::cout << "End Result: TEST FAILED\n";
00437   else
00438     std::cout << "End Result: TEST PASSED\n";
00439 
00440   // reset format state of std::cout
00441   std::cout.copyfmt(oldFormatState);
00442 
00443   return errorFlag;
00444 }