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   int Rows = Matrix.NumGlobalRows();
00126   int Cols = Matrix.NumGlobalRows();
00127   int Nonzeros = Matrix.NumGlobalNonzeros();
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         int GRID = Matrix.RowMatrixRowMap().GID(i);
00156 
00157         for (int j = 0; j < NumMyEntries; ++j)
00158           of << GRID << " " << Matrix.RowMatrixColMap().GID(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   int Length = MultiVector.GlobalLength();
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   int NumGlobalElements = Map.NumGlobalElements();
00229   int* MyGlobalElements = Map.MyGlobalElements();
00230 
00231   if (Comm_.MyPID() == 0)
00232   {
00233     std::ofstream of(FileName_.c_str(), std::ios::app);
00234 
00235     of << "<Map Label=\"" << Label 
00236       << "\" NumElements=\"" << NumGlobalElements << '"'
00237       << " IndexBase=\"" << Map.IndexBase() << '"'
00238       << " NumProc=\"" << Comm_.NumProc() << '"';
00239 
00240     of.close();
00241   }
00242 
00243   for (int iproc = 0; iproc < Comm_.NumProc(); ++iproc)
00244   {
00245     if (iproc == Comm_.MyPID())
00246     {
00247       std::ofstream of(FileName_.c_str(), std::ios::app);
00248 
00249       of << " ElementsOnProc" << iproc << "=\"" << Map.NumMyElements() << '"';
00250       of.close();
00251     }
00252     Comm_.Barrier();
00253   }
00254 
00255   if (Comm_.MyPID() == 0)
00256   {
00257     std::ofstream of(FileName_.c_str(), std::ios::app);
00258     of << '>' << std::endl;
00259     of.close();
00260   }
00261 
00262   for (int iproc = 0; iproc < Comm_.NumProc(); iproc++)
00263   {
00264     if (iproc == Comm_.MyPID())
00265     {
00266       std::ofstream of(FileName_.c_str(), std::ios::app);
00267 
00268       of << "<Proc ID=\"" << Comm_.MyPID() << "\">" << std::endl;
00269 
00270       for (int i = 0; i < Map.NumMyElements(); ++i)
00271       {
00272         of << MyGlobalElements[i] << std::endl;
00273       }
00274 
00275       of << "</Proc>" << std::endl;
00276       of.close();
00277     }
00278     Comm_.Barrier();
00279   }
00280 
00281   if (Comm_.MyPID() == 0)
00282   {
00283     std::ofstream of(FileName_.c_str(), std::ios::app);
00284     of << "</Map>" << std::endl;
00285     of.close();
00286   }
00287 }
00288 
00289 // ============================================================================
00290 void EpetraExt::XMLWriter::
00291 Write(const std::string& Label, Teuchos::ParameterList& List)
00292 {
00293   TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
00294                      "No file has been opened");
00295 
00296   if (Comm_.MyPID()) return;
00297 
00298   std::ofstream of(FileName_.c_str(), std::ios::app);
00299 
00300   of << "<List Label=\"" << Label << "\">" << std::endl;
00301 
00302   XMLParameterListWriter Writer;
00303   XMLObject Obj = Writer.toXML(List);
00304 
00305   of << Obj.toString();
00306 
00307   of << "</List>" << std::endl;
00308 
00309   of.close();
00310 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines