Intrepid
http://trilinos.sandia.gov/packages/docs/r10.12/packages/intrepid/example/Drivers/example_12.cpp
Go to the documentation of this file.
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 
00083 // Intrepid includes
00084 #include "Intrepid_FunctionSpaceTools.hpp"
00085 #include "Intrepid_FieldContainer.hpp"
00086 #include "Intrepid_CellTools.hpp"
00087 //#include "Intrepid_ArrayTools.hpp"
00088 #include "Intrepid_HGRAD_HEX_Cn_FEM.hpp"
00089 //#include "Intrepid_RealSpaceTools.hpp"
00090 #include "Intrepid_DefaultCubatureFactory.hpp"
00091 #include "Intrepid_Utils.hpp"
00092 
00093 // Epetra includes
00094 #include "Epetra_Time.h"
00095 #include "Epetra_Map.h"
00096 #include "Epetra_FEVector.h"
00097 #include "Epetra_FECrsMatrix.h"
00098 #include "Epetra_SerialComm.h"
00099 
00100 // Teuchos includes
00101 #include "Teuchos_oblackholestream.hpp"
00102 #include "Teuchos_RCP.hpp"
00103 //#include "Teuchos_BLAS.hpp"
00104 //#include "Teuchos_BLAS_types.hpp"
00105 
00106 // Shards includes
00107 #include "Shards_CellTopology.hpp"
00108 
00109 // EpetraExt includes
00110 #include "EpetraExt_MultiVectorOut.h"
00111 
00112 using namespace std;
00113 using namespace Intrepid;
00114 
00115 int main(int argc, char *argv[]) {
00116 
00117   //Check number of arguments
00118   if (argc < 4) {
00119     std::cout <<"\n>>> ERROR: Invalid number of arguments.\n\n";
00120     std::cout <<"Usage:\n\n";
00121     std::cout <<"  ./Intrepid_example_Drivers_Example_10.exe deg NX NY NZ verbose\n\n";
00122     std::cout <<" where \n";
00123     std::cout <<"   int deg             - polynomial degree to be used (assumed >= 1) \n";
00124     std::cout <<"   int NX              - num intervals in x direction (assumed box domain, 0,1) \n";
00125     std::cout <<"   int NY              - num intervals in y direction (assumed box domain, 0,1) \n";
00126     std::cout <<"   int NZ              - num intervals in y direction (assumed box domain, 0,1) \n";
00127     std::cout <<"   verbose (optional)  - any character, indicates verbose output \n\n";
00128     exit(1);
00129   }
00130   
00131   // This little trick lets us print to std::cout only if
00132   // a (dummy) command-line argument is provided.
00133   int iprint     = argc - 1;
00134   Teuchos::RCP<std::ostream> outStream;
00135   Teuchos::oblackholestream bhs; // outputs nothing
00136   if (iprint > 2)
00137     outStream = Teuchos::rcp(&std::cout, false);
00138   else
00139     outStream = Teuchos::rcp(&bhs, false);
00140   
00141   // Save the format state of the original std::cout.
00142   Teuchos::oblackholestream oldFormatState;
00143   oldFormatState.copyfmt(std::cout);
00144   
00145   *outStream                                                            \
00146     << "===============================================================================\n" \
00147     << "|                                                                             |\n" \
00148     << "|  Example: Build Stiffness Matrix for                                        |\n" \
00149     << "|                   Poisson Equation on Hexahedral Mesh                       |\n" \
00150     << "|                                                                             |\n" \
00151     << "|  Questions? Contact  Pavel Bochev  (pbboche@sandia.gov),                    |\n" \
00152     << "|                      Denis Ridzal  (dridzal@sandia.gov),                    |\n" \
00153     << "|                      Kara Peterson (kjpeter@sandia.gov).                    |\n" \
00154     << "|                                                                             |\n" \
00155     << "|  Intrepid's website: http://trilinos.sandia.gov/packages/intrepid           |\n" \
00156     << "|  Trilinos website:   http://trilinos.sandia.gov                             |\n" \
00157     << "|                                                                             |\n" \
00158     << "===============================================================================\n";
00159 
00160   
00161   // ************************************ GET INPUTS **************************************
00162   
00163   int deg          = atoi(argv[1]);  // polynomial degree to use
00164   int NX           = atoi(argv[2]);  // num intervals in x direction (assumed box domain, 0,1)
00165   int NY           = atoi(argv[3]);  // num intervals in y direction (assumed box domain, 0,1)
00166   int NZ           = atoi(argv[4]);  // num intervals in y direction (assumed box domain, 0,1)
00167   
00168 
00169   // *********************************** CELL TOPOLOGY **********************************
00170   
00171   // Get cell topology for base hexahedron
00172   typedef shards::CellTopology    CellTopology;
00173   CellTopology hex_8(shards::getCellTopologyData<shards::Hexahedron<8> >() );
00174   
00175   // Get dimensions 
00176   int numNodesPerElem = hex_8.getNodeCount();
00177   int spaceDim = hex_8.getDimension();
00178   
00179   // *********************************** GENERATE MESH ************************************
00180   
00181   *outStream << "Generating mesh ... \n\n";
00182   
00183   *outStream << "   NX" << "   NY" << "   NZ\n";
00184   *outStream << std::setw(5) << NX <<
00185     std::setw(5) << NY << std::setw(5) << NZ << "\n\n";
00186   
00187   // Print mesh information
00188   int numElems = NX*NY*NZ;
00189   int numNodes = (NX+1)*(NY+1)*(NZ+1);
00190   *outStream << " Number of Elements: " << numElems << " \n";
00191   *outStream << "    Number of Nodes: " << numNodes << " \n\n";
00192   
00193   // Cube
00194   double leftX = 0.0, rightX = 1.0;
00195   double leftY = 0.0, rightY = 1.0;
00196   double leftZ = 0.0, rightZ = 1.0;
00197 
00198   // Mesh spacing
00199   double hx = (rightX-leftX)/((double)NX);
00200   double hy = (rightY-leftY)/((double)NY);
00201   double hz = (rightZ-leftZ)/((double)NZ);
00202 
00203   // Get nodal coordinates
00204   FieldContainer<double> nodeCoord(numNodes, spaceDim);
00205   FieldContainer<int> nodeOnBoundary(numNodes);
00206   int inode = 0;
00207   for (int k=0; k<NZ+1; k++) 
00208     {
00209       for (int j=0; j<NY+1; j++) 
00210         {
00211           for (int i=0; i<NX+1; i++) 
00212             {
00213               nodeCoord(inode,0) = leftX + (double)i*hx;
00214               nodeCoord(inode,1) = leftY + (double)j*hy;
00215               nodeCoord(inode,2) = leftZ + (double)k*hz;
00216               if (k==0 || k==NZ || j==0 || i==0 || j==NY || i==NX)
00217                 {
00218                   nodeOnBoundary(inode)=1;
00219                 }
00220               else 
00221                 {
00222                   nodeOnBoundary(inode)=0;
00223                 }
00224               inode++;
00225             }
00226         }
00227     }
00228 #define DUMP_DATA
00229 #ifdef DUMP_DATA
00230   // Print nodal coords
00231   ofstream fcoordout("coords.dat");
00232   for (int i=0; i<numNodes; i++) {
00233     fcoordout << nodeCoord(i,0) <<" ";
00234     fcoordout << nodeCoord(i,1) <<" ";
00235     fcoordout << nodeCoord(i,2) <<"\n";
00236   }
00237   fcoordout.close();
00238 #endif
00239   
00240   
00241   // Element to Node map
00242   // We'll keep it around, but this is only the DOFMap if you are in the lowest order case.
00243   FieldContainer<int> elemToNode(numElems, numNodesPerElem);
00244   int ielem = 0;
00245   for (int k=0; k<NZ; k++) 
00246     {
00247       for (int j=0; j<NY; j++) 
00248         {
00249           for (int i=0; i<NX; i++) 
00250             {
00251               elemToNode(ielem,0) = k * ( NX + 1 ) * ( NY + 1 ) + j * ( NX + 1 ) + i;
00252               elemToNode(ielem,1) = k * ( NX + 1 ) * ( NY + 1 ) + j * ( NX + 1 ) + i + 1;
00253               elemToNode(ielem,2) = k * ( NX + 1 ) * ( NY + 1 ) + ( j + 1 ) * ( NX + 1 ) + i + 1;
00254               elemToNode(ielem,3) = k * ( NX + 1 ) * ( NY + 1 ) + ( j + 1 ) * ( NX + 1 ) + i;
00255               elemToNode(ielem,4) = ( k + 1 ) * ( NX + 1 ) * ( NY + 1 ) + j * ( NX + 1 ) + i;
00256               elemToNode(ielem,5) = ( k + 1 ) * ( NX + 1 ) * ( NY + 1 ) + j * ( NX + 1 ) + i + 1;
00257               elemToNode(ielem,6) = ( k + 1 ) * ( NX + 1 ) * ( NY + 1 ) + ( j + 1 ) * ( NX + 1 ) + i + 1;
00258               elemToNode(ielem,7) = ( k + 1 ) * ( NX + 1 ) * ( NY + 1 ) + ( j + 1 ) * ( NX + 1 ) + i;
00259               ielem++;
00260             }
00261         }
00262     }
00263 #ifdef DUMP_DATA
00264   // Output connectivity
00265   ofstream fe2nout("elem2node.dat");
00266   for (int k=0;k<NZ;k++)
00267     {
00268       for (int j=0; j<NY; j++) 
00269         {
00270           for (int i=0; i<NX; i++) 
00271             {
00272               int ielem = i + j * NX + k * NY * NY;
00273               for (int m=0; m<numNodesPerElem; m++)
00274                 {
00275                   fe2nout << elemToNode(ielem,m) <<"  ";
00276                 }
00277               fe2nout <<"\n";
00278             }
00279         }
00280     }
00281   fe2nout.close();
00282 #endif
00283   
00284   // ************************************ CUBATURE ************************************** 
00285   *outStream << "Getting cubature ... \n\n";
00286   
00287   // Get numerical integration points and weights
00288   DefaultCubatureFactory<double>  cubFactory;                                   
00289   int cubDegree = 2*deg;
00290   Teuchos::RCP<Cubature<double> > quadCub = cubFactory.create(hex_8, cubDegree); 
00291   
00292   int cubDim       = quadCub->getDimension();
00293   int numCubPoints = quadCub->getNumPoints();
00294   
00295   FieldContainer<double> cubPoints(numCubPoints, cubDim);
00296   FieldContainer<double> cubWeights(numCubPoints);
00297   
00298   quadCub->getCubature(cubPoints, cubWeights);
00299   
00300 
00301   // ************************************** BASIS ***************************************
00302   
00303   *outStream << "Getting basis ... \n\n";
00304   
00305   // Define basis 
00306   Basis_HGRAD_HEX_Cn_FEM<double, FieldContainer<double> > quadHGradBasis(deg,POINTTYPE_SPECTRAL);
00307   int numFieldsG = quadHGradBasis.getCardinality();
00308   FieldContainer<double> quadGVals(numFieldsG, numCubPoints); 
00309   FieldContainer<double> quadGrads(numFieldsG, numCubPoints, spaceDim); 
00310   
00311   // Evaluate basis values and gradients at cubature points
00312   quadHGradBasis.getValues(quadGVals, cubPoints, OPERATOR_VALUE);
00313   quadHGradBasis.getValues(quadGrads, cubPoints, OPERATOR_GRAD);
00314 
00315   // create the local-global mapping
00316   FieldContainer<int> ltgMapping(numElems,numFieldsG);
00317   const int numDOF = (NX*deg+1)*(NY*deg+1)*(NZ*deg+1);
00318   ielem=0;
00319   for (int k=0;k<NZ;k++) 
00320     {
00321       for (int j=0;j<NY;j++) 
00322         {
00323           for (int i=0;i<NX;i++) 
00324             {
00325               const int start = k * ( NY * deg + 1 ) * ( NX * deg + 1 ) + j * ( NX * deg + 1 ) + i * deg;
00326               // loop over local dof on this cell
00327               int local_dof_cur=0;
00328               for (int kloc=0;kloc<=deg;kloc++) 
00329                 {
00330                   for (int jloc=0;jloc<=deg;jloc++) 
00331                     {
00332                       for (int iloc=0;iloc<=deg;iloc++)
00333                         {
00334                           ltgMapping(ielem,local_dof_cur) = start 
00335                             + kloc * ( NX * deg + 1 ) * ( NY * deg + 1 )
00336                             + jloc * ( NX * deg + 1 )
00337                             + iloc;
00338                           local_dof_cur++;
00339                         }
00340                     }
00341                 }
00342               ielem++;
00343             }
00344         }
00345     }
00346 #ifdef DUMP_DATA
00347   // Output ltg mapping 
00348   ielem = 0;
00349   ofstream ltgout("ltg.dat");
00350   for (int k=0;k<NZ;k++)  
00351     {
00352       for (int j=0; j<NY; j++) 
00353         {
00354           for (int i=0; i<NX; i++) 
00355             {
00356               int ielem = i + j * NX + k * NX * NY;
00357               for (int m=0; m<numFieldsG; m++)
00358                 {
00359                   ltgout << ltgMapping(ielem,m) <<"  ";
00360                 }
00361               ltgout <<"\n";
00362             }
00363         }
00364     }
00365   ltgout.close();
00366 #endif
00367 
00368   // ********** DECLARE GLOBAL OBJECTS *************
00369   Epetra_SerialComm Comm;
00370   Epetra_Map globalMapG(numDOF, 0, Comm);
00371   Epetra_FEVector u(globalMapG);  u.Random();
00372   Epetra_FEVector Ku(globalMapG);
00373 
00374   // Let's preallocate the graph before we instantiate the matrix
00375   Epetra_Time graphTimer(Comm);
00376   Epetra_CrsGraph grph( Copy , globalMapG , 4 * numFieldsG );
00377   for (int k=0;k<numElems;k++) 
00378     {
00379       for (int i=0;i<numFieldsG;i++)
00380         {
00381           grph.InsertGlobalIndices(ltgMapping(k,i),numFieldsG,&ltgMapping(k,0));
00382         }
00383     }
00384   grph.FillComplete();
00385   const double graphTime = graphTimer.ElapsedTime();
00386 
00387 
00388   // time the instantiation 
00389   Epetra_Time instantiateTimer(Comm);
00390   Epetra_FECrsMatrix StiffMatrix(Copy,grph);
00391   const double instantiateTime = instantiateTimer.ElapsedTime();
00392 
00393 
00394   // ********** CONSTRUCT AND INSERT LOCAL STIFFNESS MATRICES ***********
00395   *outStream << "Building local stiffness matrices...\n\n";
00396   typedef CellTools<double>  CellTools;
00397   typedef FunctionSpaceTools fst;
00398   int numCells = numElems; 
00399 
00400 
00401   // jacobian information
00402   FieldContainer<double> refCellNodes(1,numNodesPerElem,spaceDim);
00403   FieldContainer<double> cellJacobian(1,numCubPoints,spaceDim,spaceDim);
00404   FieldContainer<double> cellJacobInv(1,numCubPoints,spaceDim,spaceDim);
00405   FieldContainer<double> cellJacobDet(1,numCubPoints);
00406 
00407   // element stiffness matrices and supporting storage space
00408   FieldContainer<double> localStiffMatrix(1, numFieldsG, numFieldsG);
00409   FieldContainer<double> transformedBasisGradients(1,numFieldsG,numCubPoints,spaceDim);
00410   FieldContainer<double> weightedTransformedBasisGradients(1,numFieldsG,numCubPoints,spaceDim);
00411   FieldContainer<double> weightedMeasure(1, numCubPoints);
00412 
00413 
00414   Epetra_Time localConstructTimer( Comm );
00415   refCellNodes(0,0,0) = 0.0;  refCellNodes(0,0,1) = 0.0;  refCellNodes(0,0,2) = 0.0;
00416   refCellNodes(0,1,0) = hx;   refCellNodes(0,1,1) = 0.0;  refCellNodes(0,1,2) = 0.0;
00417   refCellNodes(0,2,0) = hx;   refCellNodes(0,2,1) = hy;   refCellNodes(0,2,2) = 0.0;
00418   refCellNodes(0,3,0) = 0.0;  refCellNodes(0,3,1) = hy;   refCellNodes(0,3,2) = 0.0;
00419   refCellNodes(0,4,0) = 0.0;  refCellNodes(0,4,1) = 0.0;  refCellNodes(0,4,2) = hz;
00420   refCellNodes(0,5,0) = hx;   refCellNodes(0,5,1) = 0.0;  refCellNodes(0,5,2) = hz;
00421   refCellNodes(0,6,0) = hx;   refCellNodes(0,6,1) = hy;   refCellNodes(0,6,2) = hz;
00422   refCellNodes(0,7,0) = 0.0;  refCellNodes(0,7,1) = hy;   refCellNodes(0,7,2) = hz;
00423 
00424    
00425   // jacobian evaluation 
00426   CellTools::setJacobian(cellJacobian,cubPoints,refCellNodes,hex_8);
00427   CellTools::setJacobianInv(cellJacobInv, cellJacobian );
00428   CellTools::setJacobianDet(cellJacobDet, cellJacobian );
00429 
00430   // transform reference element gradients to each cell
00431   fst::HGRADtransformGRAD<double>(transformedBasisGradients, cellJacobInv, quadGrads);
00432       
00433   // compute weighted measure
00434   fst::computeCellMeasure<double>(weightedMeasure, cellJacobDet, cubWeights);
00435 
00436   // multiply values with weighted measure
00437   fst::multiplyMeasure<double>(weightedTransformedBasisGradients,
00438                                weightedMeasure, transformedBasisGradients);
00439 
00440   // integrate to compute element stiffness matrix
00441   fst::integrate<double>(localStiffMatrix,
00442                          transformedBasisGradients, weightedTransformedBasisGradients , COMP_BLAS);
00443 
00444   const double localConstructTime = localConstructTimer.ElapsedTime();
00445 
00446 
00447   Epetra_Time insertionTimer(Comm);
00448 
00449   // *** Element loop ***
00450   for (int k=0; k<numElems; k++) 
00451     {
00452       // assemble into global matrix
00453       StiffMatrix.InsertGlobalValues(numFieldsG,&ltgMapping(k,0),numFieldsG,&ltgMapping(k,0),&localStiffMatrix(0,0,0));
00454       
00455     }
00456   StiffMatrix.GlobalAssemble(); StiffMatrix.FillComplete();
00457   const double insertionTime = insertionTimer.ElapsedTime( );
00458 
00459   *outStream << "Time to construct matrix graph: " << graphTime << "\n";
00460   *outStream << "Time to instantiate global stiffness matrix: " << instantiateTime << "\n";
00461   *outStream << "Time to build local matrices (including Jacobian computation): "<< localConstructTime << "\n";
00462   *outStream << "Time to assemble global matrix from local matrices: " << insertionTime << "\n";
00463   *outStream << "Total construction time: " << graphTime + instantiateTime + localConstructTime + insertionTime << "\n";
00464 
00465   Epetra_Time applyTimer(Comm);
00466   StiffMatrix.Apply(u,Ku);
00467   const double multTime = applyTimer.ElapsedTime();
00468   *outStream << "Time to multiply onto a vector: " << multTime << "\n";
00469 
00470   *outStream << "End Result: TEST PASSED\n";
00471   
00472   // reset format state of std::cout
00473   std::cout.copyfmt(oldFormatState);
00474   
00475   return 0;
00476 }
00477