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