Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

dumpMesh.C

Go to the documentation of this file.
00001 
00002 #include "mpi.h"
00003 #include "petsc.h"
00004 #include "sys.h"
00005 #include "parUtils.h"
00006 #include "TreeNode.h"
00007 #include "oda.h"
00008 #include <cstdlib>
00009 #include "colors.h"
00010 #include "externVars.h"
00011 #include "dendro.h"
00012 
00013 //Don't want time to be synchronized. Need to check load imbalance.
00014 #ifdef MPI_WTIME_IS_GLOBAL
00015 #undef MPI_WTIME_IS_GLOBAL
00016 #endif
00017 
00018 int main(int argc, char ** argv ) {     
00019   int size, rank;
00020   char bFile[50];
00021   DendroIntL locSz, totalSz;
00022 
00023   PetscInitialize(&argc,&argv,0,NULL);
00024   ot::RegisterEvents();
00025   ot::DA_Initialize(MPI_COMM_WORLD);
00026 
00027   if(argc < 2) {
00028     std::cerr << "Usage: " << argv[0] << "inpfile " << std::endl;
00029     return -1;
00030   }
00031 
00032   MPI_Comm_size(MPI_COMM_WORLD,&size);
00033   MPI_Comm_rank(MPI_COMM_WORLD,&rank);
00034   sprintf(bFile,"%s%d_%d.ot",argv[1],rank,size);
00035 
00036   if(!rank){
00037     std::cout << " reading  "<<bFile<<std::endl; // Point size
00038   }
00039   std::vector<ot::TreeNode> balOct;
00040   ot::readNodesFromFile (bFile,balOct);
00041 
00042   MPI_Barrier(MPI_COMM_WORLD);
00043 
00044   if(!rank){
00045     std::cout << " finished reading  "<<bFile<<std::endl; // Point size
00046   }
00047 
00048   // compute total inp size and output size
00049   locSz = balOct.size();
00050   par::Mpi_Reduce<DendroIntL>(&locSz, &totalSz, 1, MPI_SUM, 0, MPI_COMM_WORLD);
00051 
00052   MPI_Barrier(MPI_COMM_WORLD);
00053   if(!rank) {
00054     std::cout << "# of Balanced Octants: "<< totalSz << std::endl;       
00055   }
00056 
00057   //ODA ...
00058   MPI_Barrier(MPI_COMM_WORLD);  
00059 
00060   assert(!(balOct.empty()));
00061   ot::DA da(balOct,MPI_COMM_WORLD, MPI_COMM_WORLD, false,NULL,NULL);
00062 
00063   balOct.clear();
00064 
00065   MPI_Barrier(MPI_COMM_WORLD);
00066 
00067   if(!rank) {
00068     std::cout << "Finished Meshing "<< std::endl;       
00069   }
00070 
00071   MPI_Barrier(MPI_COMM_WORLD);
00072 
00073   unsigned int maxDepth = (da.getMaxDepth()-1);
00074 
00075   //ComputeLocalToGlobalElemMappings
00076   if( !(da.computedLocalToGlobalElems()) ) {
00077     da.computeLocalToGlobalElemMappings();
00078   }
00079 
00080   MPI_Barrier(MPI_COMM_WORLD);
00081 
00082   if(!rank) {
00083     std::cout << "Computed Local To Global (Nodes) Maps "<< std::endl;       
00084   }
00085 
00086   MPI_Barrier(MPI_COMM_WORLD);
00087 
00088   //ComputeLocalToGlobalNodeMappings
00089   if( !(da.computedLocalToGlobal()) ) {
00090     da.computeLocalToGlobalMappings();
00091   }
00092 
00093   MPI_Barrier(MPI_COMM_WORLD);
00094 
00095   if(!rank) {
00096     std::cout << "Computed Local To Global (Elements) Maps "<< std::endl;       
00097   }
00098 
00099   if(!rank) {
00100     std::cout << "Dumping Mesh... "<< std::endl;       
00101   }
00102 
00103   DendroIntL* localToGlobalMap = da.getLocalToGlobalMap();
00104   DendroIntL* localToGlobalElemsMap = da.getLocalToGlobalElemsMap();
00105 
00106   unsigned int localElemSize = da.getElementSize();
00107   unsigned int localNodeSize = da.getNodeSize();
00108 
00109   std::vector<char> nodeVisited;
00110   da.createVector<char>(nodeVisited, false, false, 1);
00111 
00112   //Initialize
00113   for(unsigned int i = 0; i < nodeVisited.size(); i++) {
00114     nodeVisited[i] = 0;
00115   }
00116 
00117   char* nodeVisitedBuffer = NULL;
00118   da.vecGetBuffer<char>(nodeVisited, nodeVisitedBuffer, false, false, true, 1);
00119 
00120   unsigned int idxElemBegin = da.getIdxElementBegin();
00121   unsigned int idxPostGhostBegin = da.getIdxPostGhostBegin();
00122 
00123   char nodeListFileName[100];
00124   sprintf(nodeListFileName,"%s_NL_%d_%d.out",argv[1],rank,size);
00125   FILE* outNodeListFile = fopen(nodeListFileName,"wb");
00126   fwrite((&localNodeSize), sizeof(unsigned int), 1, outNodeListFile);
00127 
00128   unsigned int nodeCnt = 0;
00129   if(da.iAmActive()) {
00130     for( da.init<ot::DA_FLAGS::ALL>(); 
00131         da.curr() < da.end<ot::DA_FLAGS::ALL>();
00132         da.next<ot::DA_FLAGS::ALL>() ) {
00133       Point currPt = da.getCurrentOffset();
00134       unsigned int Lev = da.getLevel(da.curr());
00135       unsigned int xCurr = currPt.xint();
00136       unsigned int yCurr = currPt.yint();
00137       unsigned int zCurr = currPt.zint();
00138 
00139       ot::TreeNode current(xCurr, yCurr, zCurr, Lev, 3, (maxDepth+1));
00140       ot::TreeNode parent = current.getParent();
00141 
00142       unsigned char hnType = da.getHangingNodeIndex(da.curr());
00143       unsigned int xyz[8][3];
00144 
00145       //node 0
00146       if(hnType & (1 << 0)) {
00147         xyz[0][0] = parent.minX();
00148         xyz[0][1] = parent.minY();
00149         xyz[0][2] = parent.minZ();
00150       } else {
00151         xyz[0][0] = current.minX();
00152         xyz[0][1] = current.minY();
00153         xyz[0][2] = current.minZ();
00154       }
00155 
00156       //node 1
00157       if(hnType & (1 << 1)) {
00158         xyz[1][0] = parent.maxX();
00159         xyz[1][1] = parent.minY();
00160         xyz[1][2] = parent.minZ();
00161       } else {
00162         xyz[1][0] = current.maxX();
00163         xyz[1][1] = current.minY();
00164         xyz[1][2] = current.minZ();
00165       }
00166 
00167       //node 2
00168       if(hnType & (1 << 2)) {
00169         xyz[2][0] = parent.minX();
00170         xyz[2][1] = parent.maxY();
00171         xyz[2][2] = parent.minZ();
00172       } else {
00173         xyz[2][0] = current.minX();
00174         xyz[2][1] = current.maxY();
00175         xyz[2][2] = current.minZ();
00176       }
00177 
00178       //node 3
00179       if(hnType & (1 << 3)) {
00180         xyz[3][0] = parent.maxX();
00181         xyz[3][1] = parent.maxY();
00182         xyz[3][2] = parent.minZ();
00183       } else {
00184         xyz[3][0] = current.maxX();
00185         xyz[3][1] = current.maxY();
00186         xyz[3][2] = current.minZ();
00187       }
00188 
00189       //node 4
00190       if(hnType & (1 << 4)) {
00191         xyz[4][0] = parent.minX();
00192         xyz[4][1] = parent.minY();
00193         xyz[4][2] = parent.maxZ();
00194       } else {
00195         xyz[4][0] = current.minX();
00196         xyz[4][1] = current.minY();
00197         xyz[4][2] = current.maxZ();
00198       }
00199 
00200       //node 5
00201       if(hnType & (1 << 5)) {
00202         xyz[5][0] = parent.maxX();
00203         xyz[5][1] = parent.minY();
00204         xyz[5][2] = parent.maxZ();
00205       } else {
00206         xyz[5][0] = current.maxX();
00207         xyz[5][1] = current.minY();
00208         xyz[5][2] = current.maxZ();
00209       }
00210 
00211       //node 6
00212       if(hnType & (1 << 6)) {
00213         xyz[6][0] = parent.minX();
00214         xyz[6][1] = parent.maxY();
00215         xyz[6][2] = parent.maxZ();
00216       } else {
00217         xyz[6][0] = current.minX();
00218         xyz[6][1] = current.maxY();
00219         xyz[6][2] = current.maxZ();
00220       }
00221 
00222       //node 7
00223       if(hnType & (1 << 7)) {
00224         xyz[7][0] = parent.maxX();
00225         xyz[7][1] = parent.maxY();
00226         xyz[7][2] = parent.maxZ();
00227       } else {
00228         xyz[7][0] = current.maxX();
00229         xyz[7][1] = current.maxY();
00230         xyz[7][2] = current.maxZ();
00231       }
00232 
00233       unsigned int indices[8];
00234       da.getNodeIndices(indices);
00235 
00236       for(unsigned int j = 0; j < 8; j++) {
00237         if( (indices[j] >= idxElemBegin) && (indices[j] < idxPostGhostBegin) &&
00238             (nodeVisitedBuffer[indices[j]] == 0) ) {
00239           fwrite(xyz[j], sizeof(unsigned int), 3,  outNodeListFile);
00240           fwrite((&(localToGlobalMap[indices[j]])), sizeof(DendroIntL), 1, outNodeListFile);
00241           nodeVisitedBuffer[indices[j]] = 1;
00242           nodeCnt++;
00243         }
00244       }//end for j
00245     }//end loop
00246   }//end if active
00247 
00248   da.vecRestoreBuffer<char>(nodeVisited, nodeVisitedBuffer, false, false, true, 1);
00249   nodeVisited.clear();
00250 
00251   assert(nodeCnt == localNodeSize);
00252   fclose(outNodeListFile);
00253 
00254   MPI_Barrier(MPI_COMM_WORLD);
00255   if(!rank) {
00256     std::cout<<"Finished Writing Node List."<<std::endl;
00257   }
00258   MPI_Barrier(MPI_COMM_WORLD);
00259 
00260 
00261   char elemListFileName[100];
00262   sprintf(elemListFileName,"%s_EL_%d_%d.out",argv[1],rank,size);
00263   FILE* outElemListFile = fopen(elemListFileName,"wb");
00264   fwrite(&maxDepth, sizeof(unsigned int), 1, outElemListFile);
00265   fwrite(&localElemSize, sizeof(unsigned int), 1, outElemListFile);
00266 
00267   unsigned int writableCnt = 0;
00268   if(da.iAmActive()) {
00269     for( da.init<ot::DA_FLAGS::WRITABLE>(); da.curr() < da.end<ot::DA_FLAGS::WRITABLE>(); da.next<ot::DA_FLAGS::WRITABLE>() ) {
00270       Point currPt = da.getCurrentOffset();
00271       DendroIntL xyzdi[5];
00272       xyzdi[0] = currPt.xint();
00273       xyzdi[1] = currPt.yint();
00274       xyzdi[2] = currPt.zint();
00275       xyzdi[3] = ( (da.getLevel(da.curr())) - 1 );
00276       xyzdi[4] = localToGlobalElemsMap[da.curr()];
00277 
00278       fwrite(xyzdi, sizeof(DendroIntL), 5, outElemListFile);
00279 
00280       writableCnt++; 
00281     }
00282   }
00283 
00284   assert(writableCnt == localElemSize); 
00285   fclose(outElemListFile);
00286 
00287   MPI_Barrier(MPI_COMM_WORLD);
00288   if(!rank) {
00289     std::cout<<"Finished Writing Element List."<<std::endl;
00290   }
00291   MPI_Barrier(MPI_COMM_WORLD);
00292 
00293   char meshFileName[100];
00294   sprintf(meshFileName,"%s_M_%d_%d.out",argv[1],rank,size);
00295   FILE* outMeshFile = fopen(meshFileName,"wb");
00296   fwrite(&localElemSize, sizeof(unsigned int), 1, outMeshFile);
00297 
00298   if(da.iAmActive()) {
00299     for( da.init<ot::DA_FLAGS::WRITABLE>();
00300         da.curr() < da.end<ot::DA_FLAGS::WRITABLE>();
00301         da.next<ot::DA_FLAGS::WRITABLE>() ) {
00302       unsigned int indices[8];
00303       da.getNodeIndices(indices);
00304 
00305       DendroIntL record[9];
00306       record[0] = localToGlobalElemsMap[da.curr()];
00307 
00308       for(unsigned int j = 0; j < 8; j++) {
00309         record[j+1] = localToGlobalMap[indices[j]];
00310       }
00311 
00312       fwrite(record, sizeof(DendroIntL), 9, outMeshFile);
00313 
00314       writableCnt++; 
00315     }
00316   }
00317 
00318   fclose(outMeshFile);
00319 
00320   MPI_Barrier(MPI_COMM_WORLD);
00321   if(!rank) {
00322     std::cout<<"Finished Dumping Mesh."<<std::endl;
00323   }
00324   MPI_Barrier(MPI_COMM_WORLD);
00325 
00326   if (!rank) {
00327     std::cout << GRN << "Finalizing PETSC" << NRM << std::endl;
00328   }
00329 
00330   ot::DA_Finalize();
00331   PetscFinalize();
00332 }//end function
00333 

Generated on Tue Mar 24 16:13:59 2009 for DENDRO by  doxygen 1.3.9.1