EpetraExt_XMLWriter.cpp

Go to the documentation of this file.
00001 #include "EpetraExt_ConfigDefs.h"
00002 #ifdef HAVE_MPI
00003 #include "Epetra_MpiComm.h"
00004 #include "mpi.h"
00005 #else
00006 #include "Epetra_SerialComm.h"
00007 #endif
00008 #include "EpetraExt_XMLWriter.h"
00009 #include "Epetra_Map.h"
00010 #include "Epetra_CrsGraph.h"
00011 #include "Epetra_CrsMatrix.h"
00012 #include "Epetra_MultiVector.h"
00013 #include "Teuchos_ParameterList.hpp"
00014 #include "Teuchos_XMLParameterListWriter.hpp"
00015 #include "Teuchos_TestForException.hpp"
00016 
00017 using namespace Teuchos;
00018 
00019 // ============================================================================
00020 EpetraExt::XMLWriter::
00021 XMLWriter(const Epetra_Comm& comm, const string& FileName) :
00022   Comm_(comm),
00023   FileName_(FileName),
00024   IsOpen_(false)
00025 {}
00026 
00027 // ============================================================================
00028 void EpetraExt::XMLWriter::
00029 Create(const string& Label)
00030 {
00031   if (Comm_.MyPID() == 0) 
00032   {
00033     std::ofstream of(FileName_.c_str());
00034     of << "<ObjectCollection Label=\"" << Label << "\">" << endl;
00035     of.close();
00036   }
00037 
00038   IsOpen_ = true;
00039 }
00040   
00041 // ============================================================================
00042 void EpetraExt::XMLWriter:: Close()
00043 {
00044   if (Comm_.MyPID() == 0) 
00045   {
00046     std::ofstream of(FileName_.c_str(), std::ios::app);
00047     of << "</ObjectCollection>" << endl;
00048     of.close();
00049   }
00050 
00051   IsOpen_ = false;
00052 }
00053   
00054 // ============================================================================
00055 void EpetraExt::XMLWriter::
00056 Write(const string& Label, const vector<string>& Content)
00057 {
00058   TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
00059                      "No file has been opened");
00060 
00061   if (Comm_.MyPID()) return;
00062 
00063   std::ofstream of(FileName_.c_str(), std::ios::app);
00064 
00065   of << "<Text Label=\"" << Label << "\">" << endl;
00066   int Csize = (int) Content.size();
00067   for (int i = 0; i < Csize; ++i)
00068     of << Content[i] << endl;
00069 
00070   of << "</Text>" << endl;
00071 
00072   of.close();
00073 }
00074 
00075 // ============================================================================
00076 void EpetraExt::XMLWriter::
00077 Write(const string& Label, const Epetra_RowMatrix& Matrix)
00078 {
00079   TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
00080                      "No file has been opened");
00081 
00082   int Rows = Matrix.NumGlobalRows();
00083   int Cols = Matrix.NumGlobalRows();
00084   int Nonzeros = Matrix.NumGlobalNonzeros();
00085 
00086   if (Comm_.MyPID() == 0)
00087   {
00088     std::ofstream of(FileName_.c_str(), std::ios::app);
00089     of << "<PointMatrix Label=\"" << Label << '"'
00090       << " Rows=\"" << Rows << '"'
00091       << " Columns=\"" << Cols<< '"'
00092       << " Nonzeros=\"" << Nonzeros << '"'
00093       << " Type=\"double\" StartingIndex=\"0\">" << endl;
00094   }
00095 
00096   int Length = Matrix.MaxNumEntries();
00097   vector<int> Indices(Length);
00098   vector<double> Values(Length);
00099 
00100   for (int iproc = 0; iproc < Comm_.NumProc(); iproc++)
00101   {
00102     if (iproc == Comm_.MyPID())
00103     {
00104       std::ofstream of(FileName_.c_str(), std::ios::app);
00105       of.precision(15);
00106 
00107       for (int i = 0; i < Matrix.NumMyRows(); ++i)
00108       {
00109         int NumMyEntries;
00110         Matrix.ExtractMyRowCopy(i, Length, NumMyEntries, &Values[0], &Indices[0]);
00111 
00112         int GRID = Matrix.RowMatrixRowMap().GID(i);
00113 
00114         for (int j = 0; j < NumMyEntries; ++j)
00115           of << GRID << " " << Matrix.RowMatrixColMap().GID(Indices[j])
00116              << " " << setiosflags(ios::scientific) << Values[j] << endl;
00117       }
00118       of.close();
00119     }
00120     Comm_.Barrier();
00121   }
00122 
00123   if (Comm_.MyPID() == 0)
00124   {
00125     std::ofstream of(FileName_.c_str(), std::ios::app);
00126     of << "</PointMatrix>" << endl;
00127     of.close();
00128   }
00129 }
00130 
00131 // ============================================================================
00132 void EpetraExt::XMLWriter::
00133 Write(const string& Label, const Epetra_MultiVector& MultiVector)
00134 {
00135   TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
00136                      "No file has been opened");
00137 
00138   int Length = MultiVector.GlobalLength();
00139   int NumVectors = MultiVector.NumVectors();
00140 
00141   if (Comm_.MyPID() == 0)
00142   {
00143     std::ofstream of(FileName_.c_str(), std::ios::app);
00144 
00145     of << "<MultiVector Label=\"" << Label 
00146       << "\" Length=\"" << Length << '"'
00147       << " NumVectors=\"" << NumVectors << '"'
00148       << " Type=\"double\">" << endl;
00149   }
00150 
00151 
00152   for (int iproc = 0; iproc < Comm_.NumProc(); iproc++)
00153   {
00154     if (iproc == Comm_.MyPID())
00155     {
00156       std::ofstream of(FileName_.c_str(), std::ios::app);
00157 
00158       of.precision(15);
00159       for (int i = 0; i < MultiVector.MyLength(); ++i)
00160       {
00161         for (int j = 0; j < NumVectors; ++j)
00162           of << setiosflags(ios::scientific) << MultiVector[j][i] << " ";
00163         of << endl;
00164       }
00165       of.close();
00166     }
00167     Comm_.Barrier();
00168   }
00169 
00170   if (Comm_.MyPID() == 0)
00171   {
00172     std::ofstream of(FileName_.c_str(), std::ios::app);
00173     of << "</MultiVector>" << endl;
00174     of.close();
00175   }
00176 }
00177 
00178 // ============================================================================
00179 void EpetraExt::XMLWriter::
00180 Write(const string& Label, const Epetra_Map& Map)
00181 {
00182   TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
00183                      "No file has been opened");
00184 
00185   int NumGlobalElements = Map.NumGlobalElements();
00186   int* MyGlobalElements = Map.MyGlobalElements();
00187 
00188   if (Comm_.MyPID() == 0)
00189   {
00190     std::ofstream of(FileName_.c_str(), std::ios::app);
00191 
00192     of << "<Map Label=\"" << Label 
00193       << "\" NumElements=\"" << NumGlobalElements << '"'
00194       << " IndexBase=\"" << Map.IndexBase() << '"'
00195       << " NumProc=\"" << Comm_.NumProc() << '"';
00196 
00197     of.close();
00198   }
00199 
00200   for (int iproc = 0; iproc < Comm_.NumProc(); ++iproc)
00201   {
00202     if (iproc == Comm_.MyPID())
00203     {
00204       std::ofstream of(FileName_.c_str(), std::ios::app);
00205 
00206       of << " ElementsOnProc" << iproc << "=\"" << Map.NumMyElements() << '"';
00207       of.close();
00208     }
00209     Comm_.Barrier();
00210   }
00211 
00212   if (Comm_.MyPID() == 0)
00213   {
00214     std::ofstream of(FileName_.c_str(), std::ios::app);
00215     of << '>' << endl;
00216     of.close();
00217   }
00218 
00219   for (int iproc = 0; iproc < Comm_.NumProc(); iproc++)
00220   {
00221     if (iproc == Comm_.MyPID())
00222     {
00223       std::ofstream of(FileName_.c_str(), std::ios::app);
00224 
00225       of << "<Proc ID=\"" << Comm_.MyPID() << "\">" << endl;
00226 
00227       for (int i = 0; i < Map.NumMyElements(); ++i)
00228       {
00229         of << MyGlobalElements[i] << endl;
00230       }
00231 
00232       of << "</Proc>" << endl;
00233       of.close();
00234     }
00235     Comm_.Barrier();
00236   }
00237 
00238   if (Comm_.MyPID() == 0)
00239   {
00240     std::ofstream of(FileName_.c_str(), std::ios::app);
00241     of << "</Map>" << endl;
00242     of.close();
00243   }
00244 }
00245 
00246 // ============================================================================
00247 void EpetraExt::XMLWriter::
00248 Write(const string& Label, Teuchos::ParameterList& List)
00249 {
00250   TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
00251                      "No file has been opened");
00252 
00253   if (Comm_.MyPID()) return;
00254 
00255   std::ofstream of(FileName_.c_str(), std::ios::app);
00256 
00257   of << "<List Label=\"" << Label << "\">" << endl;
00258 
00259   XMLParameterListWriter Writer;
00260   XMLObject Obj = Writer.toXML(List);
00261 
00262   of << Obj.toString();
00263 
00264   of << "</List>" << endl;
00265 
00266   of.close();
00267 }

Generated on Thu Sep 18 12:31:44 2008 for EpetraExt by doxygen 1.3.9.1