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