|
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_FEVector.h" 00083 #include "Epetra_SerialComm.h" 00084 00085 // Teuchos includes 00086 #include "Teuchos_oblackholestream.hpp" 00087 #include "Teuchos_RCP.hpp" 00088 #include "Teuchos_BLAS.hpp" 00089 #include "Teuchos_BLAS_types.hpp" 00090 00091 // Shards includes 00092 #include "Shards_CellTopology.hpp" 00093 00094 // EpetraExt includes 00095 #include "EpetraExt_MultiVectorOut.h" 00096 00097 using namespace std; 00098 using namespace Intrepid; 00099 00100 int main(int argc, char *argv[]) { 00101 00102 //Check number of arguments 00103 if (argc < 4) { 00104 std::cout <<"\n>>> ERROR: Invalid number of arguments.\n\n"; 00105 std::cout <<"Usage:\n\n"; 00106 std::cout <<" ./Intrepid_example_Drivers_Example_06.exe deg NX NY verbose\n\n"; 00107 std::cout <<" where \n"; 00108 std::cout <<" int deg - polynomial degree to be used (assumed > 1) \n"; 00109 std::cout <<" int NX - num intervals in x direction (assumed box domain, 0,1) \n"; 00110 std::cout <<" int NY - num intervals in y direction (assumed box domain, 0,1) \n"; 00111 std::cout <<" verbose (optional) - any character, indicates verbose output \n\n"; 00112 exit(1); 00113 } 00114 00115 // This little trick lets us print to std::cout only if 00116 // a (dummy) command-line argument is provided. 00117 int iprint = argc - 1; 00118 Teuchos::RCP<std::ostream> outStream; 00119 Teuchos::oblackholestream bhs; // outputs nothing 00120 if (iprint > 2) 00121 outStream = Teuchos::rcp(&std::cout, false); 00122 else 00123 outStream = Teuchos::rcp(&bhs, false); 00124 00125 // Save the format state of the original std::cout. 00126 Teuchos::oblackholestream oldFormatState; 00127 oldFormatState.copyfmt(std::cout); 00128 00129 *outStream \ 00130 << "===============================================================================\n" \ 00131 << "| |\n" \ 00132 << "| Example: Apply Stiffness Matrix for |\n" \ 00133 << "| Poisson Equation on Quadrilateral Mesh |\n" \ 00134 << "| |\n" \ 00135 << "| Questions? Contact Pavel Bochev (pbboche@sandia.gov), |\n" \ 00136 << "| Denis Ridzal (dridzal@sandia.gov), |\n" \ 00137 << "| Kara Peterson (kjpeter@sandia.gov). |\n" \ 00138 << "| |\n" \ 00139 << "| Intrepid's website: http://trilinos.sandia.gov/packages/intrepid |\n" \ 00140 << "| Trilinos website: http://trilinos.sandia.gov |\n" \ 00141 << "| |\n" \ 00142 << "===============================================================================\n"; 00143 00144 00145 // ************************************ GET INPUTS ************************************** 00146 00147 int deg = atoi(argv[1]); // polynomial degree to use 00148 int NX = atoi(argv[2]); // num intervals in x direction (assumed box domain, 0,1) 00149 int NY = atoi(argv[3]); // num intervals in y direction (assumed box domain, 0,1) 00150 00151 00152 // *********************************** CELL TOPOLOGY ********************************** 00153 00154 // Get cell topology for base hexahedron 00155 typedef shards::CellTopology CellTopology; 00156 CellTopology quad_4(shards::getCellTopologyData<shards::Quadrilateral<4> >() ); 00157 00158 // Get dimensions 00159 int numNodesPerElem = quad_4.getNodeCount(); 00160 int spaceDim = quad_4.getDimension(); 00161 00162 // *********************************** GENERATE MESH ************************************ 00163 00164 *outStream << "Generating mesh ... \n\n"; 00165 00166 *outStream << " NX" << " NY\n"; 00167 *outStream << std::setw(5) << NX << 00168 std::setw(5) << NY << "\n\n"; 00169 00170 // Print mesh information 00171 int numElems = NX*NY; 00172 int numNodes = (NX+1)*(NY+1); 00173 *outStream << " Number of Elements: " << numElems << " \n"; 00174 *outStream << " Number of Nodes: " << numNodes << " \n\n"; 00175 00176 // Square 00177 double leftX = 0.0, rightX = 1.0; 00178 double leftY = 0.0, rightY = 1.0; 00179 00180 // Mesh spacing 00181 double hx = (rightX-leftX)/((double)NX); 00182 double hy = (rightY-leftY)/((double)NY); 00183 00184 // Get nodal coordinates 00185 FieldContainer<double> nodeCoord(numNodes, spaceDim); 00186 FieldContainer<int> nodeOnBoundary(numNodes); 00187 int inode = 0; 00188 for (int j=0; j<NY+1; j++) { 00189 for (int i=0; i<NX+1; i++) { 00190 nodeCoord(inode,0) = leftX + (double)i*hx; 00191 nodeCoord(inode,1) = leftY + (double)j*hy; 00192 if (j==0 || i==0 || j==NY || i==NX){ 00193 nodeOnBoundary(inode)=1; 00194 } 00195 else { 00196 nodeOnBoundary(inode)=0; 00197 } 00198 inode++; 00199 } 00200 } 00201 #define DUMP_DATA 00202 #ifdef DUMP_DATA 00203 // Print nodal coords 00204 ofstream fcoordout("coords.dat"); 00205 for (int i=0; i<numNodes; i++) { 00206 fcoordout << nodeCoord(i,0) <<" "; 00207 fcoordout << nodeCoord(i,1) <<"\n"; 00208 } 00209 fcoordout.close(); 00210 #endif 00211 00212 00213 // Element to Node map 00214 // We'll keep it around, but this is only the DOFMap if you are in the lowest order case. 00215 FieldContainer<int> elemToNode(numElems, numNodesPerElem); 00216 int ielem = 0; 00217 for (int j=0; j<NY; j++) { 00218 for (int i=0; i<NX; i++) { 00219 elemToNode(ielem,0) = (NX + 1)*j + i; 00220 elemToNode(ielem,1) = (NX + 1)*j + i + 1; 00221 elemToNode(ielem,2) = (NX + 1)*(j + 1) + i + 1; 00222 elemToNode(ielem,3) = (NX + 1)*(j + 1) + i; 00223 ielem++; 00224 } 00225 } 00226 #ifdef DUMP_DATA 00227 // Output connectivity 00228 ofstream fe2nout("elem2node.dat"); 00229 for (int j=0; j<NY; j++) { 00230 for (int i=0; i<NX; i++) { 00231 int ielem = i + j * NX; 00232 for (int m=0; m<numNodesPerElem; m++){ 00233 fe2nout << elemToNode(ielem,m) <<" "; 00234 } 00235 fe2nout <<"\n"; 00236 } 00237 } 00238 fe2nout.close(); 00239 #endif 00240 00241 // ************************************ CUBATURE ************************************** 00242 *outStream << "Getting cubature ... \n\n"; 00243 00244 // Get numerical integration points and weights 00245 DefaultCubatureFactory<double> cubFactory; 00246 int cubDegree = 2*deg; 00247 Teuchos::RCP<Cubature<double> > quadCub = cubFactory.create(quad_4, cubDegree); 00248 00249 int cubDim = quadCub->getDimension(); 00250 int numCubPoints = quadCub->getNumPoints(); 00251 00252 FieldContainer<double> cubPoints(numCubPoints, cubDim); 00253 FieldContainer<double> cubWeights(numCubPoints); 00254 00255 quadCub->getCubature(cubPoints, cubWeights); 00256 00257 00258 // ************************************** BASIS *************************************** 00259 00260 *outStream << "Getting basis ... \n\n"; 00261 00262 // Define basis 00263 Basis_HGRAD_QUAD_Cn_FEM<double, FieldContainer<double> > quadHGradBasis(deg,POINTTYPE_SPECTRAL); 00264 int numFieldsG = quadHGradBasis.getCardinality(); 00265 FieldContainer<double> quadGVals(numFieldsG, numCubPoints); 00266 FieldContainer<double> quadGrads(numFieldsG, numCubPoints, spaceDim); 00267 00268 // Evaluate basis values and gradients at cubature points 00269 quadHGradBasis.getValues(quadGVals, cubPoints, OPERATOR_VALUE); 00270 quadHGradBasis.getValues(quadGrads, cubPoints, OPERATOR_GRAD); 00271 00272 // create the local-global mapping for higher order elements 00273 FieldContainer<int> ltgMapping(numElems,numFieldsG); 00274 const int numDOF = (NX*deg+1)*(NY*deg+1); 00275 ielem=0; 00276 for (int j=0;j<NY;j++) { 00277 for (int i=0;i<NX;i++) { 00278 const int start = deg * j * ( NX * deg + 1 ) + i * deg; 00279 // loop over local dof on this cell 00280 int local_dof_cur=0; 00281 for (int vertical=0;vertical<=deg;vertical++) { 00282 for (int horizontal=0;horizontal<=deg;horizontal++) { 00283 ltgMapping(ielem,local_dof_cur) = start + vertical*(NX*deg+1)+horizontal; 00284 local_dof_cur++; 00285 } 00286 } 00287 ielem++; 00288 } 00289 } 00290 #ifdef DUMP_DATA 00291 // Output ltg mapping 00292 // ofstream ltgout("ltg.dat"); 00293 // for (int j=0; j<NY; j++) { 00294 // for (int i=0; i<NX; i++) { 00295 // int ielem = i + j * NX; 00296 // for (int m=0; m<numFieldsG; m++){ 00297 // ltgout << ltgMapping(ielem,m) <<" "; 00298 // } 00299 // ltgout <<"\n"; 00300 // } 00301 // } 00302 // ltgout.close(); 00303 #endif 00304 00305 // ******** CREATE A SINGLE STIFFNESS MATRIX, WHICH IS REPLICATED ON ALL ELEMENTS ********* 00306 *outStream << "Applying stiffness matrix and right hand side ... \n\n"; 00307 00308 // Settings and data structures for mass and stiffness matrices 00309 typedef CellTools<double> CellTools; 00310 typedef FunctionSpaceTools fst; 00311 int numCells = 1; 00312 00313 // Container for nodes 00314 FieldContainer<double> refQuadNodes(numCells, numNodesPerElem, spaceDim); 00315 // Containers for Jacobian 00316 FieldContainer<double> refQuadJacobian(numCells, numCubPoints, spaceDim, spaceDim); 00317 FieldContainer<double> refQuadJacobInv(numCells, numCubPoints, spaceDim, spaceDim); 00318 FieldContainer<double> refQuadJacobDet(numCells, numCubPoints); 00319 // Containers for element HGRAD stiffness matrix 00320 FieldContainer<double> localStiffMatrix(numCells, numFieldsG, numFieldsG); 00321 FieldContainer<double> weightedMeasure(numCells, numCubPoints); 00322 FieldContainer<double> quadGradsTransformed(numCells, numFieldsG, numCubPoints, spaceDim); 00323 FieldContainer<double> quadGradsTransformedWeighted(numCells, numFieldsG, numCubPoints, spaceDim); 00324 // Containers for right hand side vectors 00325 FieldContainer<double> rhsData(numCells, numCubPoints); 00326 FieldContainer<double> localRHS(numCells, numFieldsG); 00327 FieldContainer<double> quadGValsTransformed(numCells, numFieldsG, numCubPoints); 00328 FieldContainer<double> quadGValsTransformedWeighted(numCells, numFieldsG, numCubPoints); 00329 // Container for cubature points in physical space 00330 FieldContainer<double> physCubPoints(numCells, numCubPoints, cubDim); 00331 00332 // Global arrays in Epetra format 00333 Epetra_SerialComm Comm; 00334 Epetra_Map globalMapG(numDOF, 0, Comm); 00335 Epetra_FEVector u(globalMapG); 00336 Epetra_FEVector Ku(globalMapG); 00337 u.Random(); 00338 00339 std::cout << "About to start ref element matrix\n"; 00340 00341 // ************************** Compute element HGrad stiffness matrices ******************************* 00342 refQuadNodes(0,0,0) = 0.0; 00343 refQuadNodes(0,0,1) = 0.0; 00344 refQuadNodes(0,1,0) = hx; 00345 refQuadNodes(0,1,1) = 0.0; 00346 refQuadNodes(0,2,0) = hx; 00347 refQuadNodes(0,2,1) = hy; 00348 refQuadNodes(0,3,0) = 0.0; 00349 refQuadNodes(0,3,1) = hy; 00350 00351 // Compute cell Jacobians, their inverses and their determinants 00352 CellTools::setJacobian(refQuadJacobian, cubPoints, refQuadNodes, quad_4); 00353 CellTools::setJacobianInv(refQuadJacobInv, refQuadJacobian ); 00354 CellTools::setJacobianDet(refQuadJacobDet, refQuadJacobian ); 00355 00356 // transform from [-1,1]^2 to [0,hx]x[0,hy] 00357 fst::HGRADtransformGRAD<double>(quadGradsTransformed, refQuadJacobInv, quadGrads); 00358 00359 // compute weighted measure 00360 fst::computeCellMeasure<double>(weightedMeasure, refQuadJacobDet, cubWeights); 00361 00362 // multiply values with weighted measure 00363 fst::multiplyMeasure<double>(quadGradsTransformedWeighted, 00364 weightedMeasure, quadGradsTransformed); 00365 00366 // integrate to compute element stiffness matrix 00367 fst::integrate<double>(localStiffMatrix, 00368 quadGradsTransformed, quadGradsTransformedWeighted, COMP_BLAS); 00369 00370 std::cout << "Finished with reference element matrix\n"; 00371 00372 00373 // now we will scatter global degrees of freedom, apply the local stiffness matrix 00374 // with BLAS, and then gather the results 00375 FieldContainer<double> uScattered(numElems,numFieldsG); 00376 FieldContainer<double> KuScattered(numElems,numFieldsG); 00377 00378 // to extract info from u 00379 00380 u.GlobalAssemble(); 00381 00382 Epetra_Time multTimer(Comm); 00383 00384 Ku.PutScalar(0.0); 00385 Ku.GlobalAssemble(); 00386 00387 double *uVals = u[0]; 00388 double *KuVals = Ku[0]; 00389 00390 Teuchos::BLAS<int,double> blas; 00391 Epetra_Time scatterTime(Comm); 00392 std::cout << "Scattering\n"; 00393 // Scatter 00394 for (int k=0; k<numElems; k++) 00395 { 00396 for (int i=0;i<numFieldsG;i++) 00397 { 00398 uScattered(k,i) = uVals[ltgMapping(k,i)]; 00399 } 00400 } 00401 const double scatTime = scatterTime.ElapsedTime(); 00402 std::cout << "Scattered in time " << scatTime << "\n"; 00403 00404 Epetra_Time blasTimer(Comm); 00405 blas.GEMM(Teuchos::NO_TRANS , Teuchos::NO_TRANS , 00406 numFieldsG , numElems, numFieldsG , 00407 1.0 , 00408 &localStiffMatrix(0,0,0) , 00409 numFieldsG , 00410 &uScattered(0,0) , 00411 numFieldsG , 00412 0.0 , 00413 &KuScattered(0,0) , 00414 numFieldsG ); 00415 const double blasTime = blasTimer.ElapsedTime(); 00416 std::cout << "Element matrices applied in " << blasTime << "\n"; 00417 00418 Epetra_Time gatherTimer(Comm); 00419 // Gather 00420 for (int k=0;k<numElems;k++) 00421 { 00422 for (int i=0;i<numFieldsG;i++) 00423 { 00424 KuVals[ltgMapping(k,i)] += KuScattered(k,i); 00425 } 00426 } 00427 00428 const double gatherTime = gatherTimer.ElapsedTime(); 00429 std::cout << "Gathered in " << gatherTime << "\n"; 00430 00431 00432 const double applyTime = gatherTime + blasTime + scatTime; 00433 std::cout << "Time to do matrix-free product: " << applyTime << std::endl; 00434 00435 00436 std::cout << "End Result: TEST PASSED\n"; 00437 00438 // reset format state of std::cout 00439 std::cout.copyfmt(oldFormatState); 00440 00441 return 0; 00442 } 00443
1.7.4