46 #include "Epetra_MpiComm.h"
49 #include "Epetra_SerialComm.h"
52 #include "Epetra_Map.h"
53 #include "Epetra_CrsGraph.h"
54 #include "Epetra_CrsMatrix.h"
55 #include "Epetra_MultiVector.h"
56 #include "Teuchos_ParameterList.hpp"
57 #include "Teuchos_XMLParameterListWriter.hpp"
58 #include "Teuchos_Assert.hpp"
60 using namespace Teuchos;
64 XMLWriter(
const Epetra_Comm& comm,
const std::string& FileName) :
74 if (Comm_.MyPID() == 0)
76 std::ofstream of(FileName_.c_str());
77 of <<
"<ObjectCollection Label=\"" << Label <<
"\">" << std::endl;
87 if (Comm_.MyPID() == 0)
89 std::ofstream of(FileName_.c_str(), std::ios::app);
90 of <<
"</ObjectCollection>" << std::endl;
99 Write(
const std::string& Label,
const std::vector<std::string>& Content)
101 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
102 "No file has been opened");
104 if (Comm_.MyPID())
return;
106 std::ofstream of(FileName_.c_str(), std::ios::app);
108 of <<
"<Text Label=\"" << Label <<
"\">" << std::endl;
109 int Csize = (int) Content.size();
110 for (
int i = 0; i < Csize; ++i)
111 of << Content[i] << std::endl;
113 of <<
"</Text>" << std::endl;
120 Write(
const std::string& Label,
const Epetra_RowMatrix& Matrix)
122 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
123 "No file has been opened");
125 int Rows = Matrix.NumGlobalRows();
126 int Cols = Matrix.NumGlobalRows();
127 int Nonzeros = Matrix.NumGlobalNonzeros();
129 if (Comm_.MyPID() == 0)
131 std::ofstream of(FileName_.c_str(), std::ios::app);
132 of <<
"<PointMatrix Label=\"" << Label <<
'"'
133 <<
" Rows=\"" << Rows <<
'"'
134 <<
" Columns=\"" << Cols<<
'"'
135 <<
" Nonzeros=\"" << Nonzeros <<
'"'
136 <<
" Type=\"double\" StartingIndex=\"0\">" << std::endl;
139 int Length = Matrix.MaxNumEntries();
140 std::vector<int> Indices(Length);
141 std::vector<double> Values(Length);
143 for (
int iproc = 0; iproc < Comm_.NumProc(); iproc++)
145 if (iproc == Comm_.MyPID())
147 std::ofstream of(FileName_.c_str(), std::ios::app);
150 for (
int i = 0; i < Matrix.NumMyRows(); ++i)
153 Matrix.ExtractMyRowCopy(i, Length, NumMyEntries, &Values[0], &Indices[0]);
155 int GRID = Matrix.RowMatrixRowMap().GID(i);
157 for (
int j = 0; j < NumMyEntries; ++j)
158 of << GRID <<
" " << Matrix.RowMatrixColMap().GID(Indices[j])
159 <<
" " << std::setiosflags(std::ios::scientific) << Values[j] << std::endl;
166 if (Comm_.MyPID() == 0)
168 std::ofstream of(FileName_.c_str(), std::ios::app);
169 of <<
"</PointMatrix>" << std::endl;
176 Write(
const std::string& Label,
const Epetra_MultiVector& MultiVector)
178 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
179 "No file has been opened");
181 int Length = MultiVector.GlobalLength();
182 int NumVectors = MultiVector.NumVectors();
184 if (Comm_.MyPID() == 0)
186 std::ofstream of(FileName_.c_str(), std::ios::app);
188 of <<
"<MultiVector Label=\"" << Label
189 <<
"\" Length=\"" << Length <<
'"'
190 <<
" NumVectors=\"" << NumVectors <<
'"'
191 <<
" Type=\"double\">" << std::endl;
195 for (
int iproc = 0; iproc < Comm_.NumProc(); iproc++)
197 if (iproc == Comm_.MyPID())
199 std::ofstream of(FileName_.c_str(), std::ios::app);
202 for (
int i = 0; i < MultiVector.MyLength(); ++i)
204 for (
int j = 0; j < NumVectors; ++j)
205 of << std::setiosflags(std::ios::scientific) << MultiVector[j][i] <<
" ";
213 if (Comm_.MyPID() == 0)
215 std::ofstream of(FileName_.c_str(), std::ios::app);
216 of <<
"</MultiVector>" << std::endl;
223 Write(
const std::string& Label,
const Epetra_Map& Map)
225 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
226 "No file has been opened");
228 int NumGlobalElements = Map.NumGlobalElements();
229 int* MyGlobalElements = Map.MyGlobalElements();
231 if (Comm_.MyPID() == 0)
233 std::ofstream of(FileName_.c_str(), std::ios::app);
235 of <<
"<Map Label=\"" << Label
236 <<
"\" NumElements=\"" << NumGlobalElements <<
'"'
237 <<
" IndexBase=\"" << Map.IndexBase() <<
'"'
238 <<
" NumProc=\"" << Comm_.NumProc() <<
'"';
243 for (
int iproc = 0; iproc < Comm_.NumProc(); ++iproc)
245 if (iproc == Comm_.MyPID())
247 std::ofstream of(FileName_.c_str(), std::ios::app);
249 of <<
" ElementsOnProc" << iproc <<
"=\"" << Map.NumMyElements() <<
'"';
255 if (Comm_.MyPID() == 0)
257 std::ofstream of(FileName_.c_str(), std::ios::app);
258 of << '>
' << std::endl;
262 for (int iproc = 0; iproc < Comm_.NumProc(); iproc++)
264 if (iproc == Comm_.MyPID())
266 std::ofstream of(FileName_.c_str(), std::ios::app);
268 of << "<Proc ID=\"" << Comm_.MyPID() << "\">" << std::endl;
270 for (int i = 0; i < Map.NumMyElements(); ++i)
272 of << MyGlobalElements[i] << std::endl;
275 of << "</Proc>" << std::endl;
281 if (Comm_.MyPID() == 0)
283 std::ofstream of(FileName_.c_str(), std::ios::app);
284 of << "</Map>" << std::endl;
289 // ============================================================================
290 void EpetraExt::XMLWriter::
291 Write(const std::string& Label, Teuchos::ParameterList& List)
293 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
294 "No file has been opened");
296 if (Comm_.MyPID()) return;
298 std::ofstream of(FileName_.c_str(), std::ios::app);
300 of << "<List Label=\"" << Label << "\">" << std::endl;
302 XMLParameterListWriter Writer;
303 XMLObject Obj = Writer.toXML(List);
305 of << Obj.toString();
307 of << "</List>" << std::endl;
XMLWriter(const Epetra_Comm &Comm, const std::string &FileName)
ctor
void Create(const std::string &Label)
Creates the file, giving Label to the whole object.
void Close()
Closes the file. No Write operations can follow.
void Write(const std::string &Label, const Epetra_Map &Map)
Writes an Epetra_Map using label Label.