SundanceTriangleWriter.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 #include "SundanceTriangleWriter.hpp"
00043 #include "PlayaExceptions.hpp"
00044 #include "SundanceOut.hpp"
00045 #include "PlayaTabs.hpp"
00046 
00047 
00048 using namespace Sundance;
00049 using namespace Sundance;
00050 using namespace Sundance;
00051 using namespace Teuchos;
00052 
00053 
00054 void TriangleWriter::write() const 
00055 {
00056   std::string f = filename();
00057   if (nProc() > 1) f = f + Teuchos::toString(myRank());
00058 
00059   /* write header information on root proc only */
00060   if (nProc() > 1 && myRank()==0) writeHeader(filename());
00061 
00062   /* write local mesh on all procs */
00063   writePoints(f);
00064   writeCells(f);
00065   writeEdges(f);
00066   if (mesh().spatialDim() > 2)
00067     {
00068       writeFaces(f);
00069     }
00070   if (nProc() > 1) writeParallelInfo(f);
00071 }
00072 
00073 void TriangleWriter::writeHeader(const std::string& filename) const 
00074 {
00075   std::string hdrfile = filename + ".hdr";
00076   std::ofstream os(hdrfile.c_str());
00077 
00078   os << nProc() << std::endl;
00079   for (int p=0; p<nProc(); p++) 
00080     {
00081       os << filename + Teuchos::toString(p) << std::endl;
00082     }
00083 
00084   os << pointScalarNames().length() << std::endl;
00085   for (int i=0; i<pointScalarNames().length(); i++)
00086     {
00087       os << i << " " << pointScalarNames()[i] << std::endl;
00088     }
00089   os << cellScalarNames().length() << std::endl;
00090   for (int i=0; i<cellScalarNames().length(); i++)
00091     {
00092       os << i << " " << cellScalarNames()[i] << std::endl;
00093     }
00094   
00095   for (int i=0; i<comments().length(); i++)
00096     {
00097       os << "# " << comments()[i] << std::endl;
00098     }
00099 }
00100 
00101 
00102 void TriangleWriter::writePoints(const std::string& filename) const 
00103 {
00104   int nPts = mesh().numCells(0);
00105   int dim = mesh().spatialDim();
00106   int nAttr = pointScalarFields().length();
00107   int nBdryMarkers = 0;
00108 
00109   std::string nodefile = filename + ".node";
00110   std::ofstream os(nodefile.c_str());
00111 
00112   os << nPts << " " << dim << " " << nAttr << " " << nBdryMarkers << std::endl;
00113 
00114   for (int i=0; i<nPts; i++)
00115     {
00116       os << i+indexOffset_;
00117       const Point& x = mesh().nodePosition(i);
00118       for (int d=0; d<dim; d++)
00119         {
00120           os << " " << x[d];
00121         }
00122       /*
00123       for (int f=0; f<nAttr; f++)
00124         {
00125           double val = pointScalarFields()[f].probeAtMeshPoint(i);
00126           os << " " << val;
00127         }
00128       */
00129       for (int b=0; b<nBdryMarkers; b++)
00130         {
00131     //bvbw          SUNDANCE_ERROR("Boundary markers not supported yet");
00132     assert(0);
00133         }
00134       os << std::endl;
00135     }
00136   
00137   for (int i=0; i<comments().length(); i++)
00138     {
00139       os << "# " << comments()[i] << std::endl;
00140     }
00141 }
00142 
00143 void TriangleWriter::writeFaces(const std::string& filename) const 
00144 {
00145   std::string facefile = filename + ".face";
00146   std::ofstream os(facefile.c_str());
00147 
00148   int dim = 2;
00149   int nFaces = mesh().numCells(dim);
00150   int dummySign;
00151 
00152   os << nFaces << " 0" << std::endl;
00153 
00154   for (int c=0; c<nFaces; c++)
00155     {
00156       os << c + indexOffset_;
00157       int nNodes = 3;
00158       
00159       for (int i=0; i<nNodes; i++)
00160         {
00161           os << " " << mesh().facetLID(2,c,0,i,dummySign) + indexOffset_;
00162         }
00163       os << std::endl;
00164     }
00165   
00166   for (int i=0; i<comments().length(); i++)
00167     {
00168       os << "# " << comments()[i] << std::endl;
00169     }
00170 }
00171 
00172 void TriangleWriter::writeEdges(const std::string& filename) const 
00173 {
00174   std::string edgefile = filename + ".edge";
00175   std::ofstream os(edgefile.c_str());
00176 
00177   int dim = 1;
00178   int nEdges = mesh().numCells(dim);
00179   int nNodes = 2;
00180   int dummySign;
00181 
00182   os << nEdges << " 0" << std::endl;
00183 
00184   for (int c=0; c<nEdges; c++)
00185     {
00186       os << c + indexOffset_;
00187       for (int i=0; i<nNodes; i++)
00188         {
00189           os << " " << mesh().facetLID(1,c,0,i,dummySign) + indexOffset_;
00190         }
00191       os << std::endl;
00192     }
00193   
00194   for (int i=0; i<comments().length(); i++)
00195     {
00196       os << "# " << comments()[i] << std::endl;
00197     }
00198 }
00199 
00200 void TriangleWriter::writeCells(const std::string& filename) const 
00201 {
00202   std::string elefile = filename + ".ele";
00203   std::ofstream os(elefile.c_str());
00204 
00205   int dim = mesh().spatialDim();
00206   int nCells = mesh().numCells(dim);
00207   int nAttr = cellScalarFields().length();
00208   int dummySign;
00209 
00210   os << nCells << " " << dim+1 << " " << nAttr << std::endl;
00211 
00212   for (int c=0; c<nCells; c++)
00213     {
00214       os << c + indexOffset_;
00215       int nNodes = dim+1;
00216       
00217       for (int i=0; i<nNodes; i++)
00218         {
00219           os << " " << mesh().facetLID(dim,c,0,i,dummySign) + indexOffset_;
00220         }
00221       /*
00222       for (int f=0; f<nAttr; f++)
00223         {
00224           os << " " << cellScalarFields()[f].average(cell).value();
00225         }
00226       */
00227       os << std::endl;
00228     }
00229   
00230   for (int i=0; i<comments().length(); i++)
00231     {
00232       os << "# " << comments()[i] << std::endl;
00233     }
00234 }
00235 
00236 
00237 void TriangleWriter::writeParallelInfo(const std::string& filename) const 
00238 {
00239   std::string parfile = filename + ".par";
00240   std::ofstream os(parfile.c_str());
00241 
00242   int dim = mesh().spatialDim();
00243   int nCells = mesh().numCells(dim);
00244   int nEdges = mesh().numCells(1);
00245   int nPts = mesh().numCells(0);
00246 
00247   os << myRank() << " " << nProc() << std::endl;
00248 
00249   os << nPts << std::endl;
00250   for (int i=0; i<nPts; i++)
00251     {
00252       os << i << " " << mesh().mapLIDToGID(0,i) 
00253          << " " << mesh().ownerProcID(0,i) << std::endl;
00254     }
00255 
00256   os << nCells << std::endl;
00257   for (int c=0; c<nCells; c++)
00258     {
00259       os << c << " " << mesh().mapLIDToGID(dim,c) 
00260          << " " << mesh().ownerProcID(dim,c) << std::endl;
00261     }
00262 
00263   os << nEdges << std::endl;
00264   for (int c=0; c<nEdges; c++)
00265     {
00266       os << c << " " << mesh().mapLIDToGID(1,c) 
00267          << " " << mesh().ownerProcID(1,c) << std::endl;
00268     }
00269 
00270   if (dim > 2)
00271     {
00272       int nFaces = mesh().numCells(2);
00273       os << nFaces << std::endl;
00274       for (int c=0; c<nFaces; c++)
00275         {
00276           os << c << " " << mesh().mapLIDToGID(2,c) 
00277              << " " << mesh().ownerProcID(2,c) << std::endl;
00278         }
00279     }
00280   
00281   for (int i=0; i<comments().length(); i++)
00282     {
00283       os << "# " << comments()[i] << std::endl;
00284     }
00285 }
00286 
00287 

Site Contact