SundanceBamgMeshReader.cpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                             Sundance
00005 //                 Copyright 2011 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
00041 
00042 
00043 #include "SundanceBamgMeshReader.hpp"
00044 #include "SundanceOut.hpp"
00045 #include "PlayaExceptions.hpp"
00046 #include "Teuchos_StrUtils.hpp"
00047 
00048 using namespace Teuchos;
00049 using namespace Sundance;
00050 
00051 
00052 BamgMeshReader::BamgMeshReader(const std::string& fname,
00053                                const MeshType& meshType, bool bbAttr,
00054   int verbosity,
00055                                const MPIComm& comm)
00056   : MeshReaderBase(fname, meshType, verbosity, comm),
00057     nodeFilename_(filename()),
00058     elemFilename_(filename()),
00059     parFilename_(filename()),
00060     meshFilename_(filename()), //new
00061     bbFilename_(filename()), //new
00062     bbAttr_(bbAttr) //new
00063   
00064 {
00065   
00066   //keep, but assume nProc() = 1
00067   if (nProc() > 1)
00068     {
00069       nodeFilename_ = nodeFilename_ + Teuchos::toString(myRank());
00070       parFilename_ = parFilename_ + Teuchos::toString(myRank());
00071       elemFilename_ = elemFilename_ + Teuchos::toString(myRank());
00072     }
00073   //
00074 
00075   nodeFilename_ = nodeFilename_ + ".node";
00076   elemFilename_ = elemFilename_ + ".ele";
00077   parFilename_ = parFilename_ + ".par";
00078   meshFilename_ = meshFilename_ + ".mesh"; //new
00079   bbFilename_ = bbFilename_ + ".bb"; //new
00080   
00081   SUNDANCE_OUT(this->verb() > 1,
00082                "node filename = " << nodeFilename_);
00083   
00084   SUNDANCE_OUT(this->verb() > 1,
00085                "elem filename = " << elemFilename_);
00086 
00087   SUNDANCE_OUT(this->verb() > 1,
00088                "mesh filename = " << meshFilename_); //new
00089 
00090   SUNDANCE_OUT(this->verb() > 1,
00091                "bb filename = " << bbFilename_); //new
00092   
00093 }
00094 BamgMeshReader::BamgMeshReader(const ParameterList& params)
00095   : MeshReaderBase(params),
00096     nodeFilename_(filename()),
00097     elemFilename_(filename()),
00098     parFilename_(filename()),
00099     meshFilename_(filename()) //new
00100 {
00101   
00102   //keep, but assume nProc() = 1
00103   if (nProc() > 1)
00104     {
00105       nodeFilename_ = nodeFilename_ + Teuchos::toString(myRank());
00106       parFilename_ = parFilename_ + Teuchos::toString(myRank());
00107       elemFilename_ = elemFilename_ + Teuchos::toString(myRank());
00108     }
00109   //
00110 
00111   nodeFilename_ = nodeFilename_ + ".node";
00112   elemFilename_ = elemFilename_ + ".ele";
00113   parFilename_ = parFilename_ + ".par";
00114   meshFilename_ = meshFilename_ + ".mesh"; //new
00115   
00116   SUNDANCE_OUT(this->verb() > 1,
00117                "node filename = " << nodeFilename_);
00118   
00119   SUNDANCE_OUT(this->verb() > 1,
00120                "elem filename = " << elemFilename_);
00121 
00122   SUNDANCE_OUT(this->verb() > 1,
00123                "mesh filename = " << meshFilename_); //new
00124 
00125   SUNDANCE_OUT(this->verb() > 1,
00126                "bb filename = " << bbFilename_); //new
00127   
00128 }
00129 
00130 
00131 Mesh BamgMeshReader::fillMesh() const 
00132 {
00133   Mesh mesh;
00134 
00135   Array<int> ptGID;
00136   Array<int> ptOwner;
00137   Array<int> cellGID;
00138   Array<int> cellOwner;
00139 
00140   readParallelInfo(ptGID, ptOwner, cellGID, cellOwner);
00141 
00142   //mesh = readNodes(ptGID, ptOwner); //replaced by readMesh
00143 
00144   //readElems(mesh, ptGID, cellGID, cellOwner); //readMesh reads nodes+elems
00145 
00146   mesh = readMesh(ptGID, ptOwner); //new -- replaces readNodes, readElems
00147 
00148   //  mesh.assignGlobalIndices();
00149 
00150   return mesh;
00151 }
00152 
00153 void BamgMeshReader::readParallelInfo(Array<int>& ptGID, 
00154                                       Array<int>& ptOwner,
00155                                       Array<int>& cellGID, 
00156                                       Array<int>& cellOwner) const
00157 {
00158   int nPoints;
00159   int nElems;
00160   std::string line;
00161   Array<string> tokens;
00162   try
00163     {
00164       ptGID.resize(0);
00165       ptOwner.resize(0);
00166       cellGID.resize(0);
00167       cellOwner.resize(0);
00168 
00169       //keep, but assume nProc() = 1
00170       /* if we're running in parallel, read the info on processor 
00171        * distribution */
00172       if (nProc() > 1)
00173         {
00174           RCP<std::ifstream> parStream 
00175             = openFile(parFilename_, "parallel info");
00176      
00177           /* read the number of processors and the processor rank in 
00178            * the file. These must be consistent with the current number of
00179            * processors and the current rank */
00180           getNextLine(*parStream, line, tokens, '#');
00181       
00182           TEUCHOS_TEST_FOR_EXCEPTION(tokens.length() != 2, std::runtime_error,
00183                              "TriangleMeshReader::getMesh() expects 2 entries "
00184                              "on the first line of .par file. In " 
00185                              << parFilename_ << " I found \n[" << line << "]\n");
00186 
00187           int np = atoi(tokens[1]);
00188           int pid = atoi(tokens[0]);
00189 
00190           /* check consistency with the current number of
00191            * processors and the current rank */
00192       
00193           TEUCHOS_TEST_FOR_EXCEPTION(np != nProc(), std::runtime_error,
00194                              "TriangleMeshReader::getMesh() found "
00195                              "a mismatch between the current number of processors="
00196                              << nProc() << 
00197                              "and the number of processors=" << np
00198                              << "in the file " << parFilename_);
00199 
00200           TEUCHOS_TEST_FOR_EXCEPTION(pid != myRank(), std::runtime_error,
00201                              "TriangleMeshReader::getMesh() found "
00202                              "a mismatch between the current processor rank="
00203                              << myRank() << "and the processor rank="
00204                              << pid << " in the file " << parFilename_);
00205 
00206           /* read the number of points */
00207           getNextLine(*parStream, line, tokens, '#');
00208 
00209           TEUCHOS_TEST_FOR_EXCEPTION(tokens.length() != 1, std::runtime_error,
00210                              "TriangleMeshReader::getMesh() requires 1 entry "
00211                              "on the second line of .par file. Found line \n[" 
00212                              << line << "]\n in file " << parFilename_);
00213       
00214           nPoints = StrUtils::atoi(tokens[0]);
00215 
00216           /* read the global ID and the owner PID for each point */
00217           ptGID.resize(nPoints);
00218           ptOwner.resize(nPoints);
00219 
00220           for (int i=0; i<nPoints; i++)
00221             {
00222               getNextLine(*parStream, line, tokens, '#');
00223 
00224               TEUCHOS_TEST_FOR_EXCEPTION(tokens.length() != 3, std::runtime_error,
00225                                  "TriangleMeshReader::getMesh() requires 3 "
00226                                  "entries on each line of the point section in "
00227                                  "the .par file. Found line \n[" << line
00228                                  << "]\n in file " << parFilename_);
00229 
00230               ptGID[i] = StrUtils::atoi(tokens[1]);
00231               ptOwner[i] = StrUtils::atoi(tokens[2]);
00232             }
00233 
00234 
00235           /* Read the number of elements */
00236 
00237           getNextLine(*parStream, line, tokens, '#');
00238 
00239           TEUCHOS_TEST_FOR_EXCEPTION(tokens.length() != 1, std::runtime_error,
00240                              "TriangleMeshReader::getMesh() requires 1 entry "
00241                              "on the cell count line of .par file. Found line \n[" 
00242                              << line << "]\n in file " << parFilename_);
00243 
00244           nElems = StrUtils::atoi(tokens[0]);
00245 
00246           SUNDANCE_OUT(this->verb() > 1,
00247                        "read nElems = " << nElems);
00248 
00249 
00250           /* read the global ID and the owner PID for each element */
00251 
00252           cellGID.resize(nElems);
00253           cellOwner.resize(nElems);
00254           for (int i=0; i<nElems; i++)
00255             {
00256               getNextLine(*parStream, line, tokens, '#');
00257 
00258               TEUCHOS_TEST_FOR_EXCEPTION(tokens.length() != 3, std::runtime_error,
00259                                  "TriangleMeshReader::getMesh() requires 3 "
00260                                  "entries on each line of the element section in "
00261                                  "the .par file. Found line \n[" << line
00262                                  << "]\n in file " << parFilename_);
00263 
00264               cellGID[i] = StrUtils::atoi(tokens[1]);
00265               cellOwner[i] = StrUtils::atoi(tokens[2]);
00266             }
00267         }
00268 
00269 
00270       nPoints = ptGID.length();
00271       nElems = cellGID.length();
00272     }
00273   catch(std::exception& e)
00274     {
00275       SUNDANCE_TRACE(e);
00276     }
00277 }
00278 
00279 
00280 //Mesh TriangleMeshReader::readNodes(Array<int>& ptGID, 
00281 //                                   Array<int>& ptOwner) const 
00282 Mesh BamgMeshReader::readMesh(Array<int>& ptGID, 
00283                               Array<int>& ptOwner) const 
00284 {
00285   Mesh mesh;
00286   std::string line;
00287   Array<string> tokens;
00288   int nPoints=0;
00289 
00290   /* Open the mesh file so we can read in the nodes and elements */
00291   
00292   ///////////////////////////////////////////////////////////////////
00293     // skip this part and plug in the corresponding Bamg reader code //
00294     ///////////////////////////////////////////////////////////////////
00295 
00296     /*
00297 
00298     RCP<std::ifstream> nodeStream = openFile(nodeFilename_, "node info");
00299     // read the header line //
00300     getNextLine(*nodeStream, line, tokens, '#');
00301     TEUCHOS_TEST_FOR_EXCEPTION(tokens.length() != 4, std::runtime_error,
00302     "TriangleMeshReader::getMesh() requires 4 "
00303     "entries on the header line in "
00304     "the .node file. Found line \n[" << line
00305     << "]\n in file " << nodeFilename_);
00306     std::string headerLine = line;
00307     SUNDANCE_OUT(this->verb() > 2,
00308     "read point header " << line);
00309   
00310   
00311     if (nProc()==1)
00312     {
00313     nPoints = atoi(tokens[0]);
00314     ptGID.resize(nPoints);
00315     ptOwner.resize(nPoints);
00316     for (int i=0; i<nPoints; i++)
00317     {
00318     ptGID[i] = i;
00319     ptOwner[i] = 0;
00320     }
00321     }
00322     else
00323     {
00324     // If we're running in parallel, we'd better have consistent numbers
00325     // of points in the .node and .par file. //
00326     TEUCHOS_TEST_FOR_EXCEPTION(atoi(tokens[0]) != nPoints, std::runtime_error,
00327     "TriangleMeshReader::getMesh() found inconsistent "
00328     "numbers of points in .node file and par file. Node "
00329     "file " << nodeFilename_ << " had nPoints=" 
00330     << atoi(tokens[0]) << " but .par file " 
00331     << parFilename_ << " had nPoints=" << nPoints);
00332     }
00333 
00334     SUNDANCE_OUT(this->verb() > 3,
00335     "expecting to read " << nPoints << " points");
00336   
00337     int dimension = atoi(tokens[1]);
00338     int nAttributes = atoi(tokens[2]);
00339     int nBdryMarkers = atoi(tokens[3]);
00340 
00341     // now that we know the dimension, we can build the mesh object //
00342     mesh = createMesh(dimension);
00343 
00344     // size point-related arrays //
00345     Array<int> ptIndices(nPoints);
00346     Array<bool> usedPoint(nPoints);
00347     nodeAttributes()->resize(nPoints);
00348 
00349     int offset=0;
00350     bool first = true;
00351 
00352     // read all the points //
00353     for (int count=0; count<nPoints; count++)
00354     {
00355     getNextLine(*nodeStream, line, tokens, '#');
00356       
00357     TEUCHOS_TEST_FOR_EXCEPTION(tokens.length() 
00358     != (1 + dimension + nAttributes + nBdryMarkers),
00359     std::runtime_error,
00360     "TriangleMeshReader::getMesh() found bad node input "
00361     "line. Expected " 
00362     << (1 + dimension + nAttributes + nBdryMarkers)
00363     << " entries but found line \n[" << 
00364     line << "]\n in file " << nodeFilename_);
00365     // Triangle files can use either 0-offset or 1-offset numbering. We'll
00366     // inspect the first node line to decide which numbering to use. //
00367     if (first)
00368     {
00369     offset = atoi(tokens[0]);
00370     TEUCHOS_TEST_FOR_EXCEPTION(offset < 0 || offset > 1, std::runtime_error,
00371     "TriangleMeshReader::getMesh() expected "
00372     "either 0-offset or 1-offset numbering. Found an "
00373     "initial offset of " << offset << " in line \n["
00374     << line << "]\n of file " << nodeFilename_);
00375     first = false;
00376     }
00377     } //added this '}' because the for loop continued in TriangleMeshReader
00378   
00379     */
00380 
00381     /////////// here's the Bamg reader for reading nodes /////////////
00382     std::cerr << "starting to read meshFile" << std::endl;
00383 
00384     RCP<std::ifstream> meshStream = openFile(meshFilename_, 
00385                                                 "node & elem info");
00386     //Array<string> liners = StrUtils::readFile(meshStream, '#');
00387     Array<string> liners = StrUtils::readFile(*meshStream, '#');
00388     
00389     std::cerr << "defined liners" << std::endl;
00390     //nodeStream.close(); //old
00391 
00392     SUNDANCE_OUT(this->verb() > 3, 
00393                  "reading nodes from " + meshFilename_);
00394 
00395     // extract dimension, nodes, elements (triangles) from lines
00396     int dimension=0;
00397     int dimensionIndex = 0;
00398     int lineIndex = 0;
00399     int verticesIndex = 0;
00400     int edgesIndex = 0;
00401     int triangleIndex = 0;
00402     int nCells=0;
00403     std::cerr << "DimensionIndex = " << dimensionIndex << std::endl;
00404 
00405     //int linerssize = liners.size();
00406     int linerssize = liners.length();
00407     std::cerr << "number of lines in liners = " << linerssize << std::endl;
00408 
00409     for (int i = lineIndex; i < linerssize; i++)
00410       {
00411         tokens = StrUtils::stringTokenizer(liners[i]);
00412         std::cerr << "test: i = " << i << "tokens[0] = " << tokens[0] << std::endl;
00413         if (i < 20)
00414           {
00415             std::cerr << "i = " << i << ": number of tokens = " << tokens.length() 
00416                  << std::endl;
00417             for(int j = 0; j < tokens.length();j++)
00418               {
00419                 std::cerr << "     token " << j << "  = " << tokens[j] << std::endl;
00420                 std::cerr << "     length of token[" << j << "] = " 
00421                      << tokens[j].length() << std::endl;
00422               }
00423           }
00424         if (tokens[0] == "Dimension") 
00425           {
00426             dimensionIndex = i;
00427             std::cerr << "token[0] = Dimension" << std::endl;
00428             break;
00429           }
00430       }
00431     std::cerr << "DimensionIndex = " << dimensionIndex << std::endl;
00432     if (dimensionIndex > 0)
00433       {
00434         tokens = StrUtils::stringTokenizer(liners[dimensionIndex + 1]);
00435         dimension = atoi(tokens[0]);
00436         std::cerr << "dimension = " << dimension << std::endl;
00437         lineIndex = dimensionIndex + 2;
00438       }
00439 
00440     for (int i = lineIndex; i < liners.size(); i++)
00441       {
00442         tokens = StrUtils::stringTokenizer(liners[i]);
00443         if (tokens[0] == "Vertices") verticesIndex = i;
00444         if (tokens[0] == "Vertices") break;
00445       }
00446     if (verticesIndex > 0)
00447       {
00448         tokens = StrUtils::stringTokenizer(liners[verticesIndex + 1]);
00449         nPoints = atoi(tokens[0]);
00450         std::cerr << "nPoints = " << nPoints << std::endl;
00451         ptGID.resize(nPoints);
00452         ptOwner.resize(nPoints);
00453         for (int i=0; i<nPoints; i++)
00454           {
00455             ptGID[i] = i;
00456             ptOwner[i] = 0;
00457           }
00458         lineIndex = verticesIndex + 2; 
00459         //assume node data starts 2 lines below "Vertices"
00460       }
00461   
00462     Array<int> ptIndices(nPoints);
00463     Array<int> rawIndices(nPoints);
00464 
00465     Array<double> velVector(2*nPoints);
00466     int numbbAttr = 0; //will reset if bbAttr_ = true
00467     
00468     
00469     ///// read the velocity data from bb file if bbAttr_ = true ////////////
00470       if (bbAttr_)
00471         {
00472           RCP<std::ifstream> bbStream = openFile(bbFilename_, 
00473                                                     "velocity info");
00474           Array<string> bbliners = StrUtils::readFile(*bbStream, '#');
00475 
00476           //extract dimension, # solutions, # vertices, solution type 
00477           //from first line
00478 
00479           int bbdimension;
00480           int bbnumSolns;
00481           int bbnumPoints=0;
00482           int bbsolnType;
00483           int bbdimensionIndex = 0;
00484           int bblineIndex = 0;
00485           //          int bbverticesIndex = 0;
00486           std::cerr << "bbDimensionIndex = " << bbdimensionIndex << std::endl;
00487 
00488           int bblinerssize = bbliners.size();
00489           std::cerr << "number of lines in bbliners = " << bblinerssize << std::endl;
00490 
00491           for (int i = bblineIndex; i < bblinerssize; i++)
00492             {
00493               Array<string> bbtokens = StrUtils::stringTokenizer(bbliners[i]);
00494               if(bbtokens.length() > 0) //read first nonblank line
00495                 {
00496                   if(bbtokens.length() != 4)
00497                     {
00498                       std::cerr << 
00499                         "warning: .bb header line should have 4 tokens, read " 
00500                            << bbtokens.length() << std::endl;
00501                     }          
00502                   std::cerr << "bbline " << i << ": read bbtokens " << bbtokens[0] 
00503                        << " " << bbtokens[1] << " " << bbtokens[2] << " " 
00504                        << bbtokens[3] << std::endl;
00505                   bbdimensionIndex = i;
00506                   break;
00507                 }
00508             }
00509           bblineIndex = bbdimensionIndex + 1;
00510           std::cerr << "bblineIndex = " << bblineIndex << std::endl;
00511           if (bblineIndex > 0)
00512             {
00513               Array<string> bbtokens = 
00514                 StrUtils::stringTokenizer(bbliners[bbdimensionIndex]);
00515               bbdimension = StrUtils::atoi(bbtokens[0]);
00516               std::cerr << "bbdimension = " << bbdimension << std::endl;
00517               if (bbdimension != 2) 
00518                 std::cerr << "Error! bbdimension should be 2" << std::endl;
00519               bbnumSolns = StrUtils::atoi(bbtokens[1]);
00520               numbbAttr = bbnumSolns; //sets the number of attributes
00521               std::cerr << "number of solutions per vertex = " << bbnumSolns << std::endl;
00522               //if (bbnumSolns != nAttributes) 
00523               //  std::cerr << "Error: bbnumSolns not equal to nAttributes!" << std::endl;
00524               bbnumPoints = StrUtils::atoi(bbtokens[2]);
00525               std::cerr << "number of vertices = " << bbnumPoints << std::endl;
00526               if (bbnumPoints != nPoints) 
00527                 std::cerr << "Error!! number of bb points != nPoints" << std::endl;
00528               bbsolnType = StrUtils::atoi(bbtokens[3]);
00529               std::cerr << "bbsolution type = " << bbsolnType  << std::endl;
00530             }
00531 
00532           //assume solution data starts immediately below header 
00533           //and has no blank lines
00534           for (int i = bblineIndex; i < bbnumPoints + bblineIndex; i++) 
00535             {
00536               Array<string> bbtokens = StrUtils::stringTokenizer(bbliners[i]);
00537               if (bbtokens.length() == 0) 
00538                 std::cerr << "warning: encountered a blank soln line" << std::endl;
00539               int ii = i - bblineIndex;
00540               //pointAttributes_[ii].resize(nAttributes);
00541               velVector[ii] = StrUtils::atof(bbtokens[0]);
00542               velVector[ii+bbnumPoints] = StrUtils::atof(bbtokens[1]);
00543               //pointAttributes_[ii][0] = StrUtils::atof(bbtokens[0]);
00544               //pointAttributes_[ii][1] = StrUtils::atof(bbtokens[1]);
00545       
00546               if( i < 5 || i > bbnumPoints - 5) //check velocities
00547                 {
00548                   //std::cerr << "vel0[" << ii << "] = " 
00549                   //     << pointAttributes_[ii][0] << std::endl;
00550                   //std::cerr << "vel1[" << ii << "] = " << pointAttributes_[ii][1] 
00551                   //     << std::endl;
00552                   std::cerr << "vel0[" << ii << "] = " << velVector[ii] << std::endl;
00553                   std::cerr << "vel1[" << ii << "] = " << velVector[ii+bbnumPoints] 
00554                        << std::endl;
00555                 }
00556       
00557             }
00558         }
00559       //////////// got velocity vector ///////////////////////
00560   
00561 
00562       int nAttributes = 0; // value from Triangle .node file
00563       if (bbAttr_) nAttributes = numbbAttr; // expect two velocities per node
00564       std::cerr << "nAttributes = " << nAttributes << std::endl;
00565       //      int nBdryMarkers = 1; // unused - commented out - KL
00566       //value from Triangle .node file; consistent with Bamg .mesh file
00567 
00568       //Mesh mesh(dimension); /old
00569       mesh = createMesh(dimension);
00570       int count=0;
00571       int offset=0; 
00572       //initialization--will later set = 1 since Bamg #'s count from 1, not 0
00573       Array<bool> usedPoint(nPoints);
00574 
00575       //pointAttributes_.resize(nPoints); //old
00576       nodeAttributes()->resize(nPoints);
00577 
00578       //bool first = true;// unused - commented out - KL
00579 
00580       /* now we can add the point to the mesh */
00581       for (int i = lineIndex; i < liners.size(); i++)  
00582         //proceed to read nodes, forget bdry markers
00583         {
00584           tokens = StrUtils::stringTokenizer(liners[i]);
00585           if (tokens.length() > 1)
00586             {
00587               double x = atof(tokens[0]); 
00588               double y = atof(tokens[1]);
00589               count = i - lineIndex;//assume no blank lines once node data begins
00590               rawIndices[count] = count;
00591               if ((i == lineIndex) || (i == lineIndex + 1) || 
00592                   (i > lineIndex + nPoints - 2))
00593                 {
00594                   std::cerr << "i = " << i << ";  node = (" << x << "," << y << ")" 
00595                        << std::endl;
00596                   std::cerr << "count = " << count << std::endl;
00597                 }
00598 
00599               double z = 0.0;
00600               Point pt;
00601               int ptLabel = 0;
00602       
00603               if (dimension==3)
00604                 {
00605                   z = atof(tokens[3]);
00606                   pt = Point(x,y,z);
00607                 }
00608               else 
00609                 {
00610                   pt = Point(x,y);
00611                 }
00612 
00613               ptIndices[count] 
00614                 = mesh.addVertex(ptGID[count], pt, ptOwner[count], ptLabel);
00615     
00616               (*nodeAttributes())[count].resize(nAttributes);
00617               for (int i=0; i<nAttributes; i++)
00618                 {
00619                   //(*nodeAttributes())[count][i] = atof(tokens[dimension+1+i]);
00620                   if(i == 0) (*nodeAttributes())[count][i] = velVector[count];
00621                   if(i == 1) (*nodeAttributes())[count][i] = 
00622                     velVector[count + nPoints];
00623                 }
00624             }
00625 
00626           if (tokens[0] == "Edges") 
00627             {
00628               edgesIndex = i;
00629               break;
00630             }
00631         }
00632       std::cerr << "edgesIndex = " << edgesIndex << std::endl;
00633       std::cerr << "count = " << count << "; npoints - 1 = " << nPoints - 1 << std::endl;
00634   
00635       if (count != nPoints - 1) std::cout << "error: # of nodes != # of vertices" 
00636                                      << std::endl;
00637       else std::cerr << "successfully read node data" << std::endl;
00638       lineIndex = edgesIndex + 1;
00639 
00640       // done reading nodes; now read connectivity data.
00641 
00642       std::cerr << "lineIndex = " << lineIndex << "; triangleIndex = " 
00643            << triangleIndex << std::endl;
00644       for (int i = lineIndex; i < liners.size(); i++)
00645         {
00646           tokens = StrUtils::stringTokenizer(liners[i]);
00647           if (tokens[0] == "Triangles") {triangleIndex = i; break;}
00648           if (tokens[0] == "CrackedEdges") {triangleIndex = i+2; break;} 
00649           //skip over CrackedEdges info 
00650         }
00651       std::cerr << "lineIndex = " << lineIndex << "; triangleIndex = " 
00652            << triangleIndex << std::endl;
00653       std::cerr << "tokens[0] = " << tokens[0] << std::endl;
00654     
00655       lineIndex = triangleIndex + 1;
00656 
00657       Array<int> elemGID;
00658       Array<int> elemOwner;
00659 
00660       if (triangleIndex > 0)
00661         {
00662           tokens = StrUtils::stringTokenizer(liners[lineIndex]);
00663           std::cerr << "lineIndex = " << lineIndex << "; tokens[0] = " << tokens[0] 
00664                << std::endl;
00665           nCells = atoi(tokens[0]);
00666           elemGID.resize(nCells);
00667           elemOwner.resize(nCells);
00668           std::cerr << "nCells = " << nCells << std::endl;
00669           for (int i=0; i<nCells; i++)
00670             {
00671               elemGID[i] = i;
00672               elemOwner[i] = 0;
00673             }
00674           lineIndex = lineIndex + 1;
00675         }
00676 
00677 
00678       // continue to read element data from mesh file//
00679       /*
00680         void TriangleMeshReader::readElems(Mesh& mesh,
00681         const Array<int>& ptGID,
00682         Array<int>& elemGID,
00683         Array<int>& elemOwner) const 
00684       */
00685 
00686       //string line; //already defined
00687       //Array<string> tokens; //already defined
00688 
00689       ///////////////////////////////////////////////////////////////////
00690         // skip this part and plug in the corresponding Bamg reader code //
00691         ///////////////////////////////////////////////////////////////////
00692 
00693         /*
00694    
00695         // Open the element file //
00696         RCP<std::ifstream> elemStream = openFile(elemFilename_, "element info");
00697 
00698         getNextLine(*elemStream, line, tokens, '#');
00699      
00700         TEUCHOS_TEST_FOR_EXCEPTION(tokens.length() != 3, std::runtime_error,
00701         "TriangleMeshReader::getMesh() requires 3 "
00702         "entries on the header line in "
00703         "the .ele file. Found line \n[" << line
00704         << "]\n in file " << elemFilename_);
00705                    
00706         int nElems = 0;
00707         int offset = 1;
00708         if (nProc()==1)
00709         {
00710         nElems = atoi(tokens[0]);
00711         elemGID.resize(nElems);
00712         elemOwner.resize(nElems);
00713         for (int i=0; i<nElems; i++)
00714         {
00715         elemGID[i] = i;
00716         elemOwner[i] = 0;
00717         }
00718         }
00719         else
00720         {
00721         // If we're running in parallel, we'd better have consistent numbers
00722         // of points in the .node and .par file. //
00723         TEUCHOS_TEST_FOR_EXCEPTION(atoi(tokens[0]) != nElems, std::runtime_error,
00724         "TriangleMeshReader::readElems() found inconsistent "
00725         "numbers of elements in .ele file and par file. Elem "
00726         "file " << elemFilename_ << " had nElems=" 
00727         << atoi(tokens[0]) << " but .par file " 
00728         << parFilename_ << " had nElems=" << nElems);
00729         }
00730 
00731         int ptsPerElem = atoi(tokens[1]);
00732 
00733         TEUCHOS_TEST_FOR_EXCEPTION(ptsPerElem != mesh.spatialDim()+1, std::runtime_error,
00734         "TriangleMeshReader::readElems() found inconsistency "
00735         "between number of points per element=" << ptsPerElem 
00736         << " and dimension=" << mesh.spatialDim() << ". Number of pts "
00737         "per element should be dimension + 1");
00738 
00739         int nAttributes = atoi(tokens[2]);
00740         elemAttributes()->resize(nElems);
00741 
00742         int dim = mesh.spatialDim();
00743         Array<int> nodes(dim+1);
00744 
00745         for (int count=0; count<nElems; count++)
00746         {
00747         getNextLine(*elemStream, line, tokens, '#');
00748       
00749         TEUCHOS_TEST_FOR_EXCEPTION(tokens.length() 
00750         != (1 + ptsPerElem + nAttributes),
00751         std::runtime_error,
00752         "TriangleMeshReader::readElems() found bad elem "
00753         "input line. Expected " 
00754         << (1 + ptsPerElem + nAttributes)
00755         << " entries but found line \n[" << 
00756         line << "]\n in file " << elemFilename_);
00757 
00758         for (int d=0; d<=dim; d++)
00759         {
00760         nodes[d] = ptGID[atoi(tokens[d+1])-offset];
00761         }
00762 
00763         int elemLabel = 0;
00764         mesh.addElement(elemGID[count], nodes, elemOwner[count], elemLabel);
00765        
00766         (*elemAttributes())[count].resize(nAttributes);
00767         for (int i=0; i<nAttributes; i++)
00768         {
00769         (*elemAttributes())[count][i] = atof(tokens[1+ptsPerElem+i]);
00770         }
00771         }
00772         */
00773 
00774         //////// here's the Bamg code for reading triangles (elements) ////////
00775         std::cerr << "# of triangles = " << nCells 
00776              << "; starting to read triangle data" << std::endl;
00777         SUNDANCE_OUT(this->verb() > 3, 
00778                      "done reading nodes, ready to read elements from " + meshFilename_);
00779 
00780         int nElems = nCells;
00781         int ptsPerElem = dimension + 1;
00782         elemGID.resize(nElems);
00783         elemOwner.resize(nElems);
00784         offset = 1; //assume Bamg node #'s start with 1, not 0
00785 
00786         for (int i=0; i<nElems; i++)
00787           {
00788             elemGID[i] = i;
00789             elemOwner[i] = 0;
00790           }
00791         nAttributes = 0;
00792         //cellAttributes_.resize(nCells); //old
00793         elemAttributes()->resize(nElems);
00794         count = 0;
00795 
00796         int dim = mesh.spatialDim(); //should equal dimension
00797         Array<int> nodes(dim+1); 
00798         if (dim != dimension) std::cerr << "ERROR: dim = " << dim << "!= dimension = "
00799                                    << dimension << std::endl;
00800       
00801         std::cerr << "lineIndex = " << lineIndex << std::endl;
00802         std::cerr << "size of liners = " << liners.size() << std::endl;
00803         std::cerr << "lineIndex+nCells-1 = " << lineIndex+nCells-1 << std::endl;
00804         for (int i = lineIndex; i < liners.size();i++) 
00805           //proceed to read elements, forget bdry markers
00806           {
00807             tokens = StrUtils::stringTokenizer(liners[i]);
00808             if (tokens.length() > 1)
00809               {
00810                 if (i < lineIndex + 5) std::cerr << "i = " << i << " first triangles: " 
00811                                             << tokens[0] << " " << tokens[1] 
00812                                             << " " << tokens[2] << std::endl;
00813                 if (i == lineIndex+nCells-1) std::cerr << "i = " << i 
00814                                                   << " last triangle: " << tokens[0] 
00815                                                   << " " << tokens[1] << " " 
00816                                                   << tokens[2] << std::endl;
00817 
00818                 for (int d=0; d<=dim; d++)
00819                   {
00820                     //nodes[d] = ptGID[atoi(tokens[d+1])-offset]; 
00821                     //a Triangle .ele file reads the element no. first, 
00822                     //so must offset d by 1
00823                     nodes[d] = ptGID[atoi(tokens[d])-offset]; 
00824                     //no need to offset d since a Bamg .mesh file doesn't list 
00825                     //node numbers; offset=1 since Bamg #'s start from 1, not 0
00826                   }
00827 
00828                 count = i - lineIndex; 
00829                 int elemLabel = 0;
00830                 mesh.addElement(elemGID[count], nodes, elemOwner[count], elemLabel);
00831        
00832                 (*elemAttributes())[count].resize(nAttributes);
00833                 for (int i=0; i<nAttributes; i++)
00834                   {
00835                     //(*elemAttributes())[count][i] = atof(tokens[1+ptsPerElem+i]);
00836                     //offset by 1 since a Triangle .ele file reads the 
00837                     //element no. first
00838                     (*elemAttributes())[count][i] = atof(tokens[ptsPerElem+i]);
00839                     //no need to offset by 1 since a Bamg .mesh file doesn't 
00840                     //list node numbers;
00841                   }
00842               }
00843             if (tokens[0] == "SubDomainFromMesh") break; //we're done
00844           }
00845   
00846         std::cerr << "# of cells read = " << count + 1 << std::endl;
00847         if (count != nCells - 1) std::cerr << "error: # of cells read != nCells" << std::endl;
00848 
00849         // done reading elements
00850 
00851         return mesh;
00852 }
00853 
00854 ///////////////////////////////////////////////////////////////////////////
00855 //ignore this -- we read velocities from bb file within readMesh method
00856 /*
00857 
00858 Array<Array<double> > BamgMeshReader::getVelocityField(const std::string& bbFilename) const
00859 // read .bb file for 2-D velocity field & return a ListExpr for the field //
00860 {
00861 RCP<std::ifstream> bbStream = openFile(bbFilename_, "velocity info");
00862 Array<string> liners = StrUtils::readFile(*bbStream, '#');
00863 
00864 //extract dimension, # solutions, # vertices, solution type from first line
00865 
00866 int dimension;
00867 int numSolns;
00868 int numPoints;
00869 int solnType;
00870 int dimensionIndex = 0;
00871 int lineIndex = 0;
00872 int verticesIndex = 0;
00873 std::cerr << "DimensionIndex = " << dimensionIndex << std::endl;
00874 
00875 int linerssize = liners.size();
00876 std::cerr << "number of lines in liners = " << linerssize << std::endl;
00877 
00878 for (int i = lineIndex; i < linerssize; i++)
00879 {
00880 Array<string> tokens = StrUtils::stringTokenizer(liners[i]);
00881 //replaced 'PlayaArray' with 'Array'
00882 if(tokens.length() > 0) //read first nonblank line
00883 {
00884 if(tokens.length() != 4)
00885 {
00886 std::cerr << "warning: .bb header line should have 4 tokens, read " 
00887 << tokens.length() << std::endl;
00888 }          
00889 std::cerr << "line " << i << ": read tokens " << tokens[0] << " " 
00890 << tokens[1] << " " << tokens[2] << " " << tokens[3] << std::endl;
00891 dimensionIndex = i;
00892 break;
00893 }
00894 }
00895 lineIndex = dimensionIndex + 1;
00896 std::cerr << "lineIndex = " << lineIndex << std::endl;
00897 if (lineIndex > 0)
00898 {
00899 Array<string> tokens = StrUtils::stringTokenizer(liners[dimensionIndex]);
00900 //replaced 'PlayaArray' with 'Array'
00901 dimension = StrUtils::atoi(tokens[0]);
00902 std::cerr << "dimension = " << dimension << std::endl;
00903 if (dimension != 2) std::cerr << "Error! dimension should be 2" << std::endl;
00904 numSolns = StrUtils::atoi(tokens[1]);
00905 std::cerr << "number of solutions per vertex = " << numSolns << std::endl;
00906 //if (numSolns != nAttributes) 
00907 //  std::cerr << "Error: numSolns not equal to nAttributes!" << std::endl;
00908 numPoints = StrUtils::atoi(tokens[2]);
00909 std::cerr << "number of vetices = " << numPoints << std::endl;
00910 solnType = StrUtils::atoi(tokens[3]);
00911 std::cerr << "solution type = " << solnType  << std::endl;
00912 }
00913 
00914 //BasisFamily lagr = new Lagrange(1);
00915 //VectorType<double> petra = new EpetraVectorType();
00916 //Array<BasisFamily> lagrVelBasis;
00917 //for (int n = 0; n < dimension; n++) lagrVelBasis.append(lagr);
00918 //DiscreteSpace dStateSpace(mesh, lagrVelBasis, petra);
00919 
00920 //Array<Expr> vel0(numPoints);
00921 //Array<Expr> vel1(numPoints);
00922 
00923 Array<double> vel(2*numPoints); //concatenated velocity vectors
00924 
00925 //assume solution data starts immediately below header and has no blank lines
00926 for (int i = lineIndex; i < numPoints + lineIndex; i++) 
00927 {
00928 Array<string> tokens = StrUtils::stringTokenizer(liners[i]);
00929 if (tokens.length() == 0) 
00930 std::cerr << "warning: encountered a blank soln line" << std::endl;
00931 int ii = i - lineIndex;
00932 //pointAttributes_[ii].resize(nAttributes);
00933 vel[ii] = StrUtils::atof(tokens[0]);
00934 vel[ii+numPoints] = StrUtils::atof(tokens[1]);
00935 //pointAttributes_[ii][0] = StrUtils::atof(tokens[0]);
00936 //pointAttributes_[ii][1] = StrUtils::atof(tokens[1]);
00937       
00938 if( i < 5 || i > numPoints - 5) //check to see if we got velocities
00939 {
00940 std::cerr << "vel0[" << ii << "] = " << pointAttributes_[ii][0] << std::endl;
00941 std::cerr << "vel1[" << ii << "] = " << pointAttributes_[ii][1] << std::endl;
00942 }
00943  
00944 }
00945 
00946 return vel;
00947 }
00948 
00949 */

Site Contact