|
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_QUAD_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_FECrsMatrix.h" 00083 #include "Epetra_FEVector.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 00091 // Shards includes 00092 #include "Shards_CellTopology.hpp" 00093 00094 // EpetraExt includes 00095 #include "EpetraExt_RowMatrixOut.h" 00096 #include "EpetraExt_MultiVectorOut.h" 00097 00098 using namespace std; 00099 using namespace Intrepid; 00100 00101 // Functions to evaluate exact solution and derivatives 00102 double evalu(double & x, double & y, double & z); 00103 int evalGradu(double & x, double & y, double & z, double & gradu1, double & gradu2, double & gradu3); 00104 double evalDivGradu(double & x, double & y, double & z); 00105 00106 int main(int argc, char *argv[]) { 00107 00108 //Check number of arguments 00109 if (argc < 4) { 00110 std::cout <<"\n>>> ERROR: Invalid number of arguments.\n\n"; 00111 std::cout <<"Usage:\n\n"; 00112 std::cout <<" ./Intrepid_example_Drivers_Example_05.exe deg NX NY verbose\n\n"; 00113 std::cout <<" where \n"; 00114 std::cout <<" int deg - polynomial degree to be used (assumed > 1) \n"; 00115 std::cout <<" int NX - num intervals in x direction (assumed box domain, 0,1) \n"; 00116 std::cout <<" int NY - num intervals in y direction (assumed box domain, 0,1) \n"; 00117 std::cout <<" verbose (optional) - any character, indicates verbose output \n\n"; 00118 exit(1); 00119 } 00120 00121 // This little trick lets us print to std::cout only if 00122 // a (dummy) command-line argument is provided. 00123 int iprint = argc - 1; 00124 Teuchos::RCP<std::ostream> outStream; 00125 Teuchos::oblackholestream bhs; // outputs nothing 00126 if (iprint > 2) 00127 outStream = Teuchos::rcp(&std::cout, false); 00128 else 00129 outStream = Teuchos::rcp(&bhs, false); 00130 00131 // Save the format state of the original std::cout. 00132 Teuchos::oblackholestream oldFormatState; 00133 oldFormatState.copyfmt(std::cout); 00134 00135 *outStream \ 00136 << "===============================================================================\n" \ 00137 << "| |\n" \ 00138 << "| Example: Generate Stiffness Matrix and Right Hand Side Vector for |\n" \ 00139 << "| Poisson Equation on Quadrilateral Mesh |\n" \ 00140 << "| |\n" \ 00141 << "| Questions? Contact Pavel Bochev (pbboche@sandia.gov), |\n" \ 00142 << "| Denis Ridzal (dridzal@sandia.gov), |\n" \ 00143 << "| Kara Peterson (kjpeter@sandia.gov). |\n" \ 00144 << "| |\n" \ 00145 << "| Intrepid's website: http://trilinos.sandia.gov/packages/intrepid |\n" \ 00146 << "| Trilinos website: http://trilinos.sandia.gov |\n" \ 00147 << "| |\n" \ 00148 << "===============================================================================\n"; 00149 00150 00151 // ************************************ GET INPUTS ************************************** 00152 00153 int deg = atoi(argv[1]); // polynomial degree to use 00154 int NX = atoi(argv[2]); // num intervals in x direction (assumed box domain, 0,1) 00155 int NY = atoi(argv[3]); // 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 quad_4(shards::getCellTopologyData<shards::Quadrilateral<4> >() ); 00163 00164 // Get dimensions 00165 int numNodesPerElem = quad_4.getNodeCount(); 00166 int spaceDim = quad_4.getDimension(); 00167 00168 // *********************************** GENERATE MESH ************************************ 00169 00170 *outStream << "Generating mesh ... \n\n"; 00171 00172 *outStream << " NX" << " NY\n"; 00173 *outStream << std::setw(5) << NX << 00174 std::setw(5) << NY << "\n\n"; 00175 00176 // Print mesh information 00177 int numElems = NX*NY; 00178 int numNodes = (NX+1)*(NY+1); 00179 *outStream << " Number of Elements: " << numElems << " \n"; 00180 *outStream << " Number of Nodes: " << numNodes << " \n\n"; 00181 00182 // Square 00183 double leftX = 0.0, rightX = 1.0; 00184 double leftY = 0.0, rightY = 1.0; 00185 00186 // Mesh spacing 00187 double hx = (rightX-leftX)/((double)NX); 00188 double hy = (rightY-leftY)/((double)NY); 00189 00190 // Get nodal coordinates 00191 FieldContainer<double> nodeCoord(numNodes, spaceDim); 00192 FieldContainer<int> nodeOnBoundary(numNodes); 00193 int inode = 0; 00194 for (int j=0; j<NY+1; j++) { 00195 for (int i=0; i<NX+1; i++) { 00196 nodeCoord(inode,0) = leftX + (double)i*hx; 00197 nodeCoord(inode,1) = leftY + (double)j*hy; 00198 if (j==0 || i==0 || j==NY || i==NX){ 00199 nodeOnBoundary(inode)=1; 00200 } 00201 else { 00202 nodeOnBoundary(inode)=0; 00203 } 00204 inode++; 00205 } 00206 } 00207 #define DUMP_DATA 00208 #ifdef DUMP_DATA 00209 // Print nodal coords 00210 ofstream fcoordout("coords.dat"); 00211 for (int i=0; i<numNodes; i++) { 00212 fcoordout << nodeCoord(i,0) <<" "; 00213 fcoordout << nodeCoord(i,1) <<"\n"; 00214 } 00215 fcoordout.close(); 00216 #endif 00217 00218 00219 // Element to Node map 00220 // We'll keep it around, but this is only the DOFMap if you are in the lowest order case. 00221 FieldContainer<int> elemToNode(numElems, numNodesPerElem); 00222 int ielem = 0; 00223 for (int j=0; j<NY; j++) { 00224 for (int i=0; i<NX; i++) { 00225 elemToNode(ielem,0) = (NX + 1)*j + i; 00226 elemToNode(ielem,1) = (NX + 1)*j + i + 1; 00227 elemToNode(ielem,2) = (NX + 1)*(j + 1) + i + 1; 00228 elemToNode(ielem,3) = (NX + 1)*(j + 1) + i; 00229 ielem++; 00230 } 00231 } 00232 #ifdef DUMP_DATA 00233 // Output connectivity 00234 ofstream fe2nout("elem2node.dat"); 00235 for (int j=0; j<NY; j++) { 00236 for (int i=0; i<NX; i++) { 00237 int ielem = i + j * NX; 00238 for (int m=0; m<numNodesPerElem; m++){ 00239 fe2nout << elemToNode(ielem,m) <<" "; 00240 } 00241 fe2nout <<"\n"; 00242 } 00243 } 00244 fe2nout.close(); 00245 #endif 00246 00247 00248 // ************************************ CUBATURE ************************************** 00249 *outStream << "Getting cubature ... \n\n"; 00250 00251 // Get numerical integration points and weights 00252 DefaultCubatureFactory<double> cubFactory; 00253 int cubDegree = 2*deg; 00254 Teuchos::RCP<Cubature<double> > quadCub = cubFactory.create(quad_4, cubDegree); 00255 00256 int cubDim = quadCub->getDimension(); 00257 int numCubPoints = quadCub->getNumPoints(); 00258 00259 FieldContainer<double> cubPoints(numCubPoints, cubDim); 00260 FieldContainer<double> cubWeights(numCubPoints); 00261 00262 quadCub->getCubature(cubPoints, cubWeights); 00263 00264 00265 // ************************************** BASIS *************************************** 00266 00267 *outStream << "Getting basis ... \n\n"; 00268 00269 // Define basis 00270 Basis_HGRAD_QUAD_Cn_FEM<double, FieldContainer<double> > quadHGradBasis(deg,POINTTYPE_SPECTRAL); 00271 int numFieldsG = quadHGradBasis.getCardinality(); 00272 FieldContainer<double> quadGVals(numFieldsG, numCubPoints); 00273 FieldContainer<double> quadGrads(numFieldsG, numCubPoints, spaceDim); 00274 00275 // Evaluate basis values and gradients at cubature points 00276 quadHGradBasis.getValues(quadGVals, cubPoints, OPERATOR_VALUE); 00277 quadHGradBasis.getValues(quadGrads, cubPoints, OPERATOR_GRAD); 00278 00279 // create the local-global mapping for higher order elements 00280 FieldContainer<int> ltgMapping(numElems,numFieldsG); 00281 const int numDOF = (NX*deg+1)*(NY*deg+1); 00282 ielem=0; 00283 for (int j=0;j<NY;j++) { 00284 for (int i=0;i<NX;i++) { 00285 const int start = deg * j * ( NX * deg + 1 ) + i * deg; 00286 // loop over local dof on this cell 00287 int local_dof_cur=0; 00288 for (int vertical=0;vertical<=deg;vertical++) { 00289 for (int horizontal=0;horizontal<=deg;horizontal++) { 00290 ltgMapping(ielem,local_dof_cur) = start + vertical*(NX*deg+1)+horizontal; 00291 local_dof_cur++; 00292 } 00293 } 00294 ielem++; 00295 } 00296 } 00297 #ifdef DUMP_DATA 00298 // Output ltg mapping 00299 ofstream ltgout("ltg.dat"); 00300 for (int j=0; j<NY; j++) { 00301 for (int i=0; i<NX; i++) { 00302 int ielem = i + j * NX; 00303 for (int m=0; m<numFieldsG; m++){ 00304 ltgout << ltgMapping(ielem,m) <<" "; 00305 } 00306 ltgout <<"\n"; 00307 } 00308 } 00309 ltgout.close(); 00310 #endif 00311 00312 // ******** CREATE A SINGLE STIFFNESS MATRIX, WHICH IS REPLICATED ON ALL ELEMENTS ********* 00313 *outStream << "Building stiffness matrix and right hand side ... \n\n"; 00314 00315 // Settings and data structures for mass and stiffness matrices 00316 typedef CellTools<double> CellTools; 00317 typedef FunctionSpaceTools fst; 00318 int numCells = 1; 00319 00320 // Container for nodes 00321 FieldContainer<double> refQuadNodes(numCells, numNodesPerElem, spaceDim); 00322 // Containers for Jacobian 00323 FieldContainer<double> refQuadJacobian(numCells, numCubPoints, spaceDim, spaceDim); 00324 FieldContainer<double> refQuadJacobInv(numCells, numCubPoints, spaceDim, spaceDim); 00325 FieldContainer<double> refQuadJacobDet(numCells, numCubPoints); 00326 // Containers for element HGRAD stiffness matrix 00327 FieldContainer<double> localStiffMatrix(numCells, numFieldsG, numFieldsG); 00328 FieldContainer<double> weightedMeasure(numCells, numCubPoints); 00329 FieldContainer<double> quadGradsTransformed(numCells, numFieldsG, numCubPoints, spaceDim); 00330 FieldContainer<double> quadGradsTransformedWeighted(numCells, numFieldsG, numCubPoints, spaceDim); 00331 // Containers for right hand side vectors 00332 FieldContainer<double> rhsData(numCells, numCubPoints); 00333 FieldContainer<double> localRHS(numCells, numFieldsG); 00334 FieldContainer<double> quadGValsTransformed(numCells, numFieldsG, numCubPoints); 00335 FieldContainer<double> quadGValsTransformedWeighted(numCells, numFieldsG, numCubPoints); 00336 // Container for cubature points in physical space 00337 FieldContainer<double> physCubPoints(numCells, numCubPoints, cubDim); 00338 00339 // Global arrays in Epetra format 00340 // we will explicitly build the sparsity pattern before instantiating the matrix later. 00341 Epetra_SerialComm Comm; 00342 Epetra_Map globalMapG(numDOF, 0, Comm); 00343 Epetra_FEVector u(globalMapG); 00344 Epetra_FEVector Ku(globalMapG); 00345 u.Random(); 00346 00347 // ************************** Compute element HGrad stiffness matrices ******************************* 00348 refQuadNodes(0,0,0) = 0.0; 00349 refQuadNodes(0,0,1) = 0.0; 00350 refQuadNodes(0,1,0) = hx; 00351 refQuadNodes(0,1,1) = 0.0; 00352 refQuadNodes(0,2,0) = hx; 00353 refQuadNodes(0,2,1) = hy; 00354 refQuadNodes(0,3,0) = 0.0; 00355 refQuadNodes(0,3,1) = hy; 00356 00357 // Compute cell Jacobians, their inverses and their determinants 00358 CellTools::setJacobian(refQuadJacobian, cubPoints, refQuadNodes, quad_4); 00359 CellTools::setJacobianInv(refQuadJacobInv, refQuadJacobian ); 00360 CellTools::setJacobianDet(refQuadJacobDet, refQuadJacobian ); 00361 00362 // transform from [-1,1]^2 to [0,hx]x[0,hy] 00363 fst::HGRADtransformGRAD<double>(quadGradsTransformed, refQuadJacobInv, quadGrads); 00364 00365 // compute weighted measure 00366 fst::computeCellMeasure<double>(weightedMeasure, refQuadJacobDet, cubWeights); 00367 00368 // multiply values with weighted measure 00369 fst::multiplyMeasure<double>(quadGradsTransformedWeighted, 00370 weightedMeasure, quadGradsTransformed); 00371 00372 // integrate to compute element stiffness matrix 00373 fst::integrate<double>(localStiffMatrix, 00374 quadGradsTransformed, quadGradsTransformedWeighted, COMP_BLAS); 00375 00376 Epetra_Time graphTimer(Comm); 00377 Epetra_CrsGraph grph( Copy , globalMapG , 4 * numFieldsG ); 00378 for (int k=0;k<numElems;k++) 00379 { 00380 for (int i=0;i<numFieldsG;i++) 00381 { 00382 grph.InsertGlobalIndices(ltgMapping(k,i),numFieldsG,<gMapping(k,0)); 00383 } 00384 } 00385 grph.FillComplete(); 00386 const double graphTime = graphTimer.ElapsedTime(); 00387 std::cout << "Graph computed in " << graphTime << "\n"; 00388 00389 Epetra_Time instantiateTimer( Comm ); 00390 Epetra_FECrsMatrix StiffMatrix( Copy , grph ); 00391 const double instantiateTime = instantiateTimer.ElapsedTime( ); 00392 std::cout << "Matrix instantiated in " << instantiateTime << "\n"; 00393 00394 Epetra_Time assemblyTimer(Comm); 00395 00396 // *** Element loop *** 00397 for (int k=0; k<numElems; k++) 00398 { 00399 // assemble into global matrix 00400 StiffMatrix.InsertGlobalValues(numFieldsG,<gMapping(k,0),numFieldsG,<gMapping(k,0),&localStiffMatrix(0,0,0)); 00401 00402 } 00403 00404 00405 // Assemble global matrices 00406 StiffMatrix.GlobalAssemble(); StiffMatrix.FillComplete(); 00407 00408 double assembleTime = assemblyTimer.ElapsedTime(); 00409 std::cout << "Time to insert reference element matrix into global matrix: " << assembleTime << std::endl; 00410 std::cout << "Total matrix construction time: " << assembleTime + instantiateTime + graphTime << "\n"; 00411 std::cout << "There are " << StiffMatrix.NumGlobalNonzeros() << " nonzeros in the matrix.\n"; 00412 std::cout << "There are " << numDOF << " global degrees of freedom.\n"; 00413 00414 Epetra_Time multTimer(Comm); 00415 StiffMatrix.Apply(u,Ku); 00416 double multTime = multTimer.ElapsedTime(); 00417 std::cout << "Time to apply: " << multTime << std::endl; 00418 00419 00420 #ifdef DUMP_DATA 00421 // Dump matrices to disk 00422 // EpetraExt::RowMatrixToMatlabFile("stiff_matrix.dat",StiffMatrix); 00423 // EpetraExt::MultiVectorToMatrixMarketFile("rhs_vector.dat",rhs,0,0,false); 00424 #endif 00425 00426 std::cout << "End Result: TEST PASSED\n"; 00427 00428 // reset format state of std::cout 00429 std::cout.copyfmt(oldFormatState); 00430 00431 return 0; 00432 } 00433 00434 00435 // Calculates value of exact solution u 00436 double evalu(double & x, double & y, double & z) 00437 { 00438 /* 00439 // function1 00440 double exactu = sin(M_PI*x)*sin(M_PI*y)*sin(M_PI*z); 00441 */ 00442 00443 // function2 00444 double exactu = sin(M_PI*x)*sin(M_PI*y)*sin(M_PI*z)*exp(x+y+z); 00445 00446 return exactu; 00447 } 00448 00449 // Calculates gradient of exact solution u 00450 int evalGradu(double & x, double & y, double & z, double & gradu1, double & gradu2, double & gradu3) 00451 { 00452 /* 00453 // function 1 00454 gradu1 = M_PI*cos(M_PI*x)*sin(M_PI*y)*sin(M_PI*z); 00455 gradu2 = M_PI*sin(M_PI*x)*cos(M_PI*y)*sin(M_PI*z); 00456 gradu3 = M_PI*sin(M_PI*x)*sin(M_PI*y)*cos(M_PI*z); 00457 */ 00458 00459 // function2 00460 gradu1 = (M_PI*cos(M_PI*x)+sin(M_PI*x)) 00461 *sin(M_PI*y)*sin(M_PI*z)*exp(x+y+z); 00462 gradu2 = (M_PI*cos(M_PI*y)+sin(M_PI*y)) 00463 *sin(M_PI*x)*sin(M_PI*z)*exp(x+y+z); 00464 gradu3 = (M_PI*cos(M_PI*z)+sin(M_PI*z)) 00465 *sin(M_PI*x)*sin(M_PI*y)*exp(x+y+z); 00466 00467 return 0; 00468 } 00469 00470 // Calculates Laplacian of exact solution u 00471 double evalDivGradu(double & x, double & y, double & z) 00472 { 00473 /* 00474 // function 1 00475 double divGradu = -3.0*M_PI*M_PI*sin(M_PI*x)*sin(M_PI*y)*sin(M_PI*z); 00476 */ 00477 00478 // function 2 00479 double divGradu = -3.0*M_PI*M_PI*sin(M_PI*x)*sin(M_PI*y)*sin(M_PI*z)*exp(x+y+z) 00480 + 2.0*M_PI*cos(M_PI*x)*sin(M_PI*y)*sin(M_PI*z)*exp(x+y+z) 00481 + 2.0*M_PI*cos(M_PI*y)*sin(M_PI*x)*sin(M_PI*z)*exp(x+y+z) 00482 + 2.0*M_PI*cos(M_PI*z)*sin(M_PI*x)*sin(M_PI*y)*exp(x+y+z) 00483 + 3.0*sin(M_PI*x)*sin(M_PI*y)*sin(M_PI*z)*exp(x+y+z); 00484 00485 return divGradu; 00486 } 00487
1.7.4