EpetraExt Development
EpetraExt_XMLWriter.cpp
Go to the documentation of this file.
00001 /*
00002 //@HEADER
00003 // ***********************************************************************
00004 //
00005 //     EpetraExt: Epetra Extended - Linear Algebra Services Package
00006 //                 Copyright (2011) Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00039 //
00040 // ***********************************************************************
00041 //@HEADER
00042 */
00043 
00044 #include "EpetraExt_ConfigDefs.h"
00045 #ifdef HAVE_MPI
00046 #include "Epetra_MpiComm.h"
00047 #include "mpi.h"
00048 #else
00049 #include "Epetra_SerialComm.h"
00050 #endif
00051 #include "EpetraExt_XMLWriter.h"
00052 #include "Epetra_Map.h"
00053 #include "Epetra_CrsGraph.h"
00054 #include "Epetra_CrsMatrix.h"
00055 #include "Epetra_MultiVector.h"
00056 #include "Teuchos_ParameterList.hpp"
00057 #include "Teuchos_XMLParameterListWriter.hpp"
00058 #include "Teuchos_Assert.hpp"
00059 
00060 using namespace Teuchos;
00061 
00062 // ============================================================================
00063 EpetraExt::XMLWriter::
00064 XMLWriter(const Epetra_Comm& comm, const std::string& FileName) :
00065   Comm_(comm),
00066   FileName_(FileName),
00067   IsOpen_(false)
00068 {}
00069 
00070 // ============================================================================
00071 void EpetraExt::XMLWriter::
00072 Create(const std::string& Label)
00073 {
00074   if (Comm_.MyPID() == 0) 
00075   {
00076     std::ofstream of(FileName_.c_str());
00077     of << "<ObjectCollection Label=\"" << Label << "\">" << std::endl;
00078     of.close();
00079   }
00080 
00081   IsOpen_ = true;
00082 }
00083   
00084 // ============================================================================
00085 void EpetraExt::XMLWriter:: Close()
00086 {
00087   if (Comm_.MyPID() == 0) 
00088   {
00089     std::ofstream of(FileName_.c_str(), std::ios::app);
00090     of << "</ObjectCollection>" << std::endl;
00091     of.close();
00092   }
00093 
00094   IsOpen_ = false;
00095 }
00096   
00097 // ============================================================================
00098 void EpetraExt::XMLWriter::
00099 Write(const std::string& Label, const std::vector<std::string>& Content)
00100 {
00101   TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
00102                      "No file has been opened");
00103 
00104   if (Comm_.MyPID()) return;
00105 
00106   std::ofstream of(FileName_.c_str(), std::ios::app);
00107 
00108   of << "<Text Label=\"" << Label << "\">" << std::endl;
00109   int Csize = (int) Content.size();
00110   for (int i = 0; i < Csize; ++i)
00111     of << Content[i] << std::endl;
00112 
00113   of << "</Text>" << std::endl;
00114 
00115   of.close();
00116 }
00117 
00118 // ============================================================================
00119 void EpetraExt::XMLWriter::
00120 Write(const std::string& Label, const Epetra_RowMatrix& Matrix)
00121 {
00122   TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
00123                      "No file has been opened");
00124 
00125   long long Rows = Matrix.NumGlobalRows64();
00126   long long Cols = Matrix.NumGlobalRows64();
00127   long long Nonzeros = Matrix.NumGlobalNonzeros64();
00128 
00129   if (Comm_.MyPID() == 0)
00130   {
00131     std::ofstream of(FileName_.c_str(), std::ios::app);
00132     of << "<PointMatrix Label=\"" << Label << '"'
00133       << " Rows=\"" << Rows << '"'
00134       << " Columns=\"" << Cols<< '"'
00135       << " Nonzeros=\"" << Nonzeros << '"'
00136       << " Type=\"double\" StartingIndex=\"0\">" << std::endl;
00137   }
00138 
00139   int Length = Matrix.MaxNumEntries();
00140   std::vector<int> Indices(Length);
00141   std::vector<double> Values(Length);
00142 
00143   for (int iproc = 0; iproc < Comm_.NumProc(); iproc++)
00144   {
00145     if (iproc == Comm_.MyPID())
00146     {
00147       std::ofstream of(FileName_.c_str(), std::ios::app);
00148       of.precision(15);
00149 
00150       for (int i = 0; i < Matrix.NumMyRows(); ++i)
00151       {
00152         int NumMyEntries;
00153         Matrix.ExtractMyRowCopy(i, Length, NumMyEntries, &Values[0], &Indices[0]);
00154 
00155         long long GRID = Matrix.RowMatrixRowMap().GID64(i);
00156 
00157         for (int j = 0; j < NumMyEntries; ++j)
00158           of << GRID << " " << Matrix.RowMatrixColMap().GID64(Indices[j])
00159              << " " << std::setiosflags(std::ios::scientific) << Values[j] << std::endl;
00160       }
00161       of.close();
00162     }
00163     Comm_.Barrier();
00164   }
00165 
00166   if (Comm_.MyPID() == 0)
00167   {
00168     std::ofstream of(FileName_.c_str(), std::ios::app);
00169     of << "</PointMatrix>" << std::endl;
00170     of.close();
00171   }
00172 }
00173 
00174 // ============================================================================
00175 void EpetraExt::XMLWriter::
00176 Write(const std::string& Label, const Epetra_MultiVector& MultiVector)
00177 {
00178   TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
00179                      "No file has been opened");
00180 
00181   long long Length = MultiVector.GlobalLength64();
00182   int NumVectors = MultiVector.NumVectors();
00183 
00184   if (Comm_.MyPID() == 0)
00185   {
00186     std::ofstream of(FileName_.c_str(), std::ios::app);
00187 
00188     of << "<MultiVector Label=\"" << Label 
00189       << "\" Length=\"" << Length << '"'
00190       << " NumVectors=\"" << NumVectors << '"'
00191       << " Type=\"double\">" << std::endl;
00192   }
00193 
00194 
00195   for (int iproc = 0; iproc < Comm_.NumProc(); iproc++)
00196   {
00197     if (iproc == Comm_.MyPID())
00198     {
00199       std::ofstream of(FileName_.c_str(), std::ios::app);
00200 
00201       of.precision(15);
00202       for (int i = 0; i < MultiVector.MyLength(); ++i)
00203       {
00204         for (int j = 0; j < NumVectors; ++j)
00205           of << std::setiosflags(std::ios::scientific) << MultiVector[j][i] << " ";
00206         of << std::endl;
00207       }
00208       of.close();
00209     }
00210     Comm_.Barrier();
00211   }
00212 
00213   if (Comm_.MyPID() == 0)
00214   {
00215     std::ofstream of(FileName_.c_str(), std::ios::app);
00216     of << "</MultiVector>" << std::endl;
00217     of.close();
00218   }
00219 }
00220 
00221 // ============================================================================
00222 void EpetraExt::XMLWriter::
00223 Write(const std::string& Label, const Epetra_Map& Map)
00224 {
00225   TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
00226                      "No file has been opened");
00227 
00228   long long NumGlobalElements = Map.NumGlobalElements64();
00229   const int* MyGlobalElements_int = 0;
00230   const long long* MyGlobalElements_LL = 0;
00231   Map.MyGlobalElements(MyGlobalElements_int, MyGlobalElements_LL);
00232 
00233   if(!MyGlobalElements_int || !MyGlobalElements_LL)
00234     throw "EpetraExt::XMLWriter::Write: ERROR, GlobalIndices type unknown.";
00235 
00236   if (Comm_.MyPID() == 0)
00237   {
00238     std::ofstream of(FileName_.c_str(), std::ios::app);
00239 
00240     of << "<Map Label=\"" << Label 
00241       << "\" NumElements=\"" << NumGlobalElements << '"'
00242       << " IndexBase=\"" << Map.IndexBase64() << '"'
00243       << " NumProc=\"" << Comm_.NumProc() << '"';
00244 
00245     of.close();
00246   }
00247 
00248   for (int iproc = 0; iproc < Comm_.NumProc(); ++iproc)
00249   {
00250     if (iproc == Comm_.MyPID())
00251     {
00252       std::ofstream of(FileName_.c_str(), std::ios::app);
00253 
00254       of << " ElementsOnProc" << iproc << "=\"" << Map.NumMyElements() << '"';
00255       of.close();
00256     }
00257     Comm_.Barrier();
00258   }
00259 
00260   if (Comm_.MyPID() == 0)
00261   {
00262     std::ofstream of(FileName_.c_str(), std::ios::app);
00263     of << '>' << std::endl;
00264     of.close();
00265   }
00266 
00267   for (int iproc = 0; iproc < Comm_.NumProc(); iproc++)
00268   {
00269     if (iproc == Comm_.MyPID())
00270     {
00271       std::ofstream of(FileName_.c_str(), std::ios::app);
00272 
00273       of << "<Proc ID=\"" << Comm_.MyPID() << "\">" << std::endl;
00274 
00275       if(MyGlobalElements_int)
00276       {
00277         for (int i = 0; i < Map.NumMyElements(); ++i)
00278         {
00279           of << MyGlobalElements_int[i] << std::endl;
00280         }
00281       }
00282       else
00283       {
00284         for (int i = 0; i < Map.NumMyElements(); ++i)
00285         {
00286           of << MyGlobalElements_LL[i] << std::endl;
00287         }
00288       }
00289 
00290       of << "</Proc>" << std::endl;
00291       of.close();
00292     }
00293     Comm_.Barrier();
00294   }
00295 
00296   if (Comm_.MyPID() == 0)
00297   {
00298     std::ofstream of(FileName_.c_str(), std::ios::app);
00299     of << "</Map>" << std::endl;
00300     of.close();
00301   }
00302 }
00303 
00304 // ============================================================================
00305 void EpetraExt::XMLWriter::
00306 Write(const std::string& Label, Teuchos::ParameterList& List)
00307 {
00308   TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
00309                      "No file has been opened");
00310 
00311   if (Comm_.MyPID()) return;
00312 
00313   std::ofstream of(FileName_.c_str(), std::ios::app);
00314 
00315   of << "<List Label=\"" << Label << "\">" << std::endl;
00316 
00317   XMLParameterListWriter Writer;
00318   XMLObject Obj = Writer.toXML(List);
00319 
00320   of << Obj.toString();
00321 
00322   of << "</List>" << std::endl;
00323 
00324   of.close();
00325 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines