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